Skip to content

Commit 15769ff

Browse files
committed
Merge branch 'VSOCK-add-vsockmon'
Stefan Hajnoczi says: ==================== VSOCK: vsockmon virtual device to monitor AF_VSOCK sockets. v5: * Change vsock_deliver_tap() API to avoid unnecessary skb creation [Jorgen] * Fix skb leak when no taps are registered [Jorgen] * s/cpu_to_le16(pkt->hdr.op)/le16_to_cpu(pkt->hdr.op)/ [Michael] * Add af_vsock_tap.c and vsockmon.[ch] to MAINTAINERS * checkpatch.pl and sparse fixes v4: * Add explicit reserved padding field to struct af_vsockmon_hdr and drop __attribute__((packed)) [Michael, DaveM] * Call synchronize_net() before module_put() [Michael] v3: * Hook virtio_transport.c (guest driver), not just drivers/vhost/vsock.c (host driver) * Fix DEFAULT_MTU macro definition [Zhu Yanjun] * Rename af_vsockmon_hdr->t field ->transport for clarity * Update .ndo_get_stats64() return type since it has changed * Include missing <linux/module.h> header in af_vsock_tap.c This is a continuation of Gerard Garcia's work on the vsockmon packet capture interface for AF_VSOCK. Packet capture is an essential feature for network communication. Gerard began addressing this feature gap in his Google Summer of Code 2016 project. I have cleaned up, rebased, and retested the v2 series he posted previously. The design follows the nlmon packet capture interface closely. This is because vsock has the same problem as netlink: there is no netdev on which packets can be captured. The nlmon driver is a synthetic netdev purely for the purpose of enabling packet capture. We follow the same approach here with vsockmon. See include/uapi/linux/vsockmon.h in this series for details on the packet layout. How to try it: 1. Build tcpdump with vsockmon patches: $ git clone -b vsock https://github.com/stefanha/libpcap $ (cd libcap && ./configure && make) $ git clone -b vsock https://github.com/stefanha/tcpdump $ (cd tcpdump && ./configure && make) 2. Build nc-vsock (a netcat-like tool): $ git clone https://github.com/stefanha/nc-vsock $ (cd nc-vsock && make) 3. Launch a virtual machine: # modprobe vhost_vsock # qemu-system-x86_64 -M accel=kvm -m 1024 -cpu host \ -drive if=virtio,file=test.img,format=raw \ -device vhost-vsock-pci,guest-cid=3 (Assumes guest is running a kernel with this patch) 4. Capture AF_VSOCK traffic in guest and/or host: # modprobe vsockmon # ip link add type vsockmon # ip link set vsockmon0 up # tcpdump -i vsockmon0 -vvv 5. Communicate! (host)$ nc-vsock -l 1234 (guest)$ nc-vsock 2 1234 ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents ac2291c + 82dfb54 commit 15769ff

File tree

14 files changed

+448
-1
lines changed

14 files changed

+448
-1
lines changed

MAINTAINERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13317,8 +13317,11 @@ L: netdev@vger.kernel.org
1331713317
S: Maintained
1331813318
F: include/linux/virtio_vsock.h
1331913319
F: include/uapi/linux/virtio_vsock.h
13320+
F: include/uapi/linux/vsockmon.h
13321+
F: net/vmw_vsock/af_vsock_tap.c
1332013322
F: net/vmw_vsock/virtio_transport_common.c
1332113323
F: net/vmw_vsock/virtio_transport.c
13324+
F: drivers/net/vsockmon.c
1332213325
F: drivers/vhost/vsock.c
1332313326
F: drivers/vhost/vsock.h
1332413327

drivers/net/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ config NET_VRF
355355
This option enables the support for mapping interfaces into VRF's. The
356356
support enables VRF devices.
357357

358+
config VSOCKMON
359+
tristate "Virtual vsock monitoring device"
360+
depends on VHOST_VSOCK
361+
---help---
362+
This option enables a monitoring net device for vsock sockets. It is
363+
mostly intended for developers or support to debug vsock issues. If
364+
unsure, say N.
365+
358366
endif # NET_CORE
359367

360368
config SUNGEM_PHY

drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ obj-$(CONFIG_GENEVE) += geneve.o
3030
obj-$(CONFIG_GTP) += gtp.o
3131
obj-$(CONFIG_NLMON) += nlmon.o
3232
obj-$(CONFIG_NET_VRF) += vrf.o
33+
obj-$(CONFIG_VSOCKMON) += vsockmon.o
3334

3435
#
3536
# Networking Drivers

drivers/net/vsockmon.c

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#include <linux/module.h>
2+
#include <linux/kernel.h>
3+
#include <linux/if_arp.h>
4+
#include <net/rtnetlink.h>
5+
#include <net/sock.h>
6+
#include <net/af_vsock.h>
7+
#include <uapi/linux/vsockmon.h>
8+
#include <linux/virtio_vsock.h>
9+
10+
/* Virtio transport max packet size plus header */
11+
#define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
12+
sizeof(struct af_vsockmon_hdr))
13+
14+
struct pcpu_lstats {
15+
u64 rx_packets;
16+
u64 rx_bytes;
17+
struct u64_stats_sync syncp;
18+
};
19+
20+
static int vsockmon_dev_init(struct net_device *dev)
21+
{
22+
dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
23+
if (!dev->lstats)
24+
return -ENOMEM;
25+
return 0;
26+
}
27+
28+
static void vsockmon_dev_uninit(struct net_device *dev)
29+
{
30+
free_percpu(dev->lstats);
31+
}
32+
33+
struct vsockmon {
34+
struct vsock_tap vt;
35+
};
36+
37+
static int vsockmon_open(struct net_device *dev)
38+
{
39+
struct vsockmon *vsockmon = netdev_priv(dev);
40+
41+
vsockmon->vt.dev = dev;
42+
vsockmon->vt.module = THIS_MODULE;
43+
return vsock_add_tap(&vsockmon->vt);
44+
}
45+
46+
static int vsockmon_close(struct net_device *dev)
47+
{
48+
struct vsockmon *vsockmon = netdev_priv(dev);
49+
50+
return vsock_remove_tap(&vsockmon->vt);
51+
}
52+
53+
static netdev_tx_t vsockmon_xmit(struct sk_buff *skb, struct net_device *dev)
54+
{
55+
int len = skb->len;
56+
struct pcpu_lstats *stats = this_cpu_ptr(dev->lstats);
57+
58+
u64_stats_update_begin(&stats->syncp);
59+
stats->rx_bytes += len;
60+
stats->rx_packets++;
61+
u64_stats_update_end(&stats->syncp);
62+
63+
dev_kfree_skb(skb);
64+
65+
return NETDEV_TX_OK;
66+
}
67+
68+
static void
69+
vsockmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
70+
{
71+
int i;
72+
u64 bytes = 0, packets = 0;
73+
74+
for_each_possible_cpu(i) {
75+
const struct pcpu_lstats *vstats;
76+
u64 tbytes, tpackets;
77+
unsigned int start;
78+
79+
vstats = per_cpu_ptr(dev->lstats, i);
80+
81+
do {
82+
start = u64_stats_fetch_begin_irq(&vstats->syncp);
83+
tbytes = vstats->rx_bytes;
84+
tpackets = vstats->rx_packets;
85+
} while (u64_stats_fetch_retry_irq(&vstats->syncp, start));
86+
87+
packets += tpackets;
88+
bytes += tbytes;
89+
}
90+
91+
stats->rx_packets = packets;
92+
stats->tx_packets = 0;
93+
94+
stats->rx_bytes = bytes;
95+
stats->tx_bytes = 0;
96+
}
97+
98+
static int vsockmon_is_valid_mtu(int new_mtu)
99+
{
100+
return new_mtu >= (int)sizeof(struct af_vsockmon_hdr);
101+
}
102+
103+
static int vsockmon_change_mtu(struct net_device *dev, int new_mtu)
104+
{
105+
if (!vsockmon_is_valid_mtu(new_mtu))
106+
return -EINVAL;
107+
108+
dev->mtu = new_mtu;
109+
return 0;
110+
}
111+
112+
static const struct net_device_ops vsockmon_ops = {
113+
.ndo_init = vsockmon_dev_init,
114+
.ndo_uninit = vsockmon_dev_uninit,
115+
.ndo_open = vsockmon_open,
116+
.ndo_stop = vsockmon_close,
117+
.ndo_start_xmit = vsockmon_xmit,
118+
.ndo_get_stats64 = vsockmon_get_stats64,
119+
.ndo_change_mtu = vsockmon_change_mtu,
120+
};
121+
122+
static u32 always_on(struct net_device *dev)
123+
{
124+
return 1;
125+
}
126+
127+
static const struct ethtool_ops vsockmon_ethtool_ops = {
128+
.get_link = always_on,
129+
};
130+
131+
static void vsockmon_setup(struct net_device *dev)
132+
{
133+
dev->type = ARPHRD_VSOCKMON;
134+
dev->priv_flags |= IFF_NO_QUEUE;
135+
136+
dev->netdev_ops = &vsockmon_ops;
137+
dev->ethtool_ops = &vsockmon_ethtool_ops;
138+
dev->destructor = free_netdev;
139+
140+
dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
141+
NETIF_F_HIGHDMA | NETIF_F_LLTX;
142+
143+
dev->flags = IFF_NOARP;
144+
145+
dev->mtu = DEFAULT_MTU;
146+
}
147+
148+
static struct rtnl_link_ops vsockmon_link_ops __read_mostly = {
149+
.kind = "vsockmon",
150+
.priv_size = sizeof(struct vsockmon),
151+
.setup = vsockmon_setup,
152+
};
153+
154+
static __init int vsockmon_register(void)
155+
{
156+
return rtnl_link_register(&vsockmon_link_ops);
157+
}
158+
159+
static __exit void vsockmon_unregister(void)
160+
{
161+
rtnl_link_unregister(&vsockmon_link_ops);
162+
}
163+
164+
module_init(vsockmon_register);
165+
module_exit(vsockmon_unregister);
166+
167+
MODULE_LICENSE("GPL v2");
168+
MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
169+
MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
170+
MODULE_ALIAS_RTNL_LINK("vsockmon");

drivers/vhost/vsock.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
176176
restart_tx = true;
177177
}
178178

179+
/* Deliver to monitoring devices all correctly transmitted
180+
* packets.
181+
*/
182+
virtio_transport_deliver_tap_pkt(pkt);
183+
179184
virtio_transport_free_pkt(pkt);
180185
}
181186
if (added)
@@ -383,6 +388,9 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
383388

384389
len = pkt->len;
385390

391+
/* Deliver to monitoring devices all received packets */
392+
virtio_transport_deliver_tap_pkt(pkt);
393+
386394
/* Only accept correctly addressed packets */
387395
if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
388396
virtio_transport_recv_pkt(pkt);

include/linux/virtio_vsock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,6 @@ void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt);
153153
void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt);
154154
u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
155155
void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
156+
void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt);
156157

157158
#endif /* _LINUX_VIRTIO_VSOCK_H */

include/net/af_vsock.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,17 @@ struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
188188
void vsock_remove_sock(struct vsock_sock *vsk);
189189
void vsock_for_each_connected_socket(void (*fn)(struct sock *sk));
190190

191+
/**** TAP ****/
192+
193+
struct vsock_tap {
194+
struct net_device *dev;
195+
struct module *module;
196+
struct list_head list;
197+
};
198+
199+
int vsock_init_tap(void);
200+
int vsock_add_tap(struct vsock_tap *vt);
201+
int vsock_remove_tap(struct vsock_tap *vt);
202+
void vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque);
203+
191204
#endif /* __AF_VSOCK_H__ */

include/uapi/linux/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ header-y += virtio_types.h
477477
header-y += virtio_vsock.h
478478
header-y += virtio_crypto.h
479479
header-y += vm_sockets.h
480+
header-y += vsockmon.h
480481
header-y += vt.h
481482
header-y += vtpm_proxy.h
482483
header-y += wait.h

include/uapi/linux/if_arp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
#define ARPHRD_IP6GRE 823 /* GRE over IPv6 */
9696
#define ARPHRD_NETLINK 824 /* Netlink header */
9797
#define ARPHRD_6LOWPAN 825 /* IPv6 over LoWPAN */
98+
#define ARPHRD_VSOCKMON 826 /* Vsock monitor header */
9899

99100
#define ARPHRD_VOID 0xFFFF /* Void type, nothing is known */
100101
#define ARPHRD_NONE 0xFFFE /* zero header length */

include/uapi/linux/vsockmon.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef _UAPI_VSOCKMON_H
2+
#define _UAPI_VSOCKMON_H
3+
4+
#include <linux/virtio_vsock.h>
5+
6+
/*
7+
* vsockmon is the AF_VSOCK packet capture device. Packets captured have the
8+
* following layout:
9+
*
10+
* +-----------------------------------+
11+
* | vsockmon header |
12+
* | (struct af_vsockmon_hdr) |
13+
* +-----------------------------------+
14+
* | transport header |
15+
* | (af_vsockmon_hdr->len bytes long) |
16+
* +-----------------------------------+
17+
* | payload |
18+
* | (until end of packet) |
19+
* +-----------------------------------+
20+
*
21+
* The vsockmon header is a transport-independent description of the packet.
22+
* It duplicates some of the information from the transport header so that
23+
* no transport-specific knowledge is necessary to process packets.
24+
*
25+
* The transport header is useful for low-level transport-specific packet
26+
* analysis. Transport type is given in af_vsockmon_hdr->transport and
27+
* transport header length is given in af_vsockmon_hdr->len.
28+
*
29+
* If af_vsockmon_hdr->op is AF_VSOCK_OP_PAYLOAD then the payload follows the
30+
* transport header. Other ops do not have a payload.
31+
*/
32+
33+
struct af_vsockmon_hdr {
34+
__le64 src_cid;
35+
__le64 dst_cid;
36+
__le32 src_port;
37+
__le32 dst_port;
38+
__le16 op; /* enum af_vsockmon_op */
39+
__le16 transport; /* enum af_vsockmon_transport */
40+
__le16 len; /* Transport header length */
41+
__u8 reserved[2];
42+
};
43+
44+
enum af_vsockmon_op {
45+
AF_VSOCK_OP_UNKNOWN = 0,
46+
AF_VSOCK_OP_CONNECT = 1,
47+
AF_VSOCK_OP_DISCONNECT = 2,
48+
AF_VSOCK_OP_CONTROL = 3,
49+
AF_VSOCK_OP_PAYLOAD = 4,
50+
};
51+
52+
enum af_vsockmon_transport {
53+
AF_VSOCK_TRANSPORT_UNKNOWN = 0,
54+
AF_VSOCK_TRANSPORT_NO_INFO = 1, /* No transport information */
55+
56+
/* Transport header type: struct virtio_vsock_hdr */
57+
AF_VSOCK_TRANSPORT_VIRTIO = 2,
58+
};
59+
60+
#endif

net/vmw_vsock/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ obj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o
33
obj-$(CONFIG_VIRTIO_VSOCKETS) += vmw_vsock_virtio_transport.o
44
obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o
55

6-
vsock-y += af_vsock.o vsock_addr.o
6+
vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o
77

88
vmw_vsock_vmci_transport-y += vmci_transport.o vmci_transport_notify.o \
99
vmci_transport_notify_qstate.o

0 commit comments

Comments
 (0)