Skip to content

Commit 531b374

Browse files
Gerard Garciadavem330
authored andcommitted
VSOCK: Add vsockmon tap functions
Add tap functions that can be used by the vsock transports to deliver packets to vsockmon virtual network devices. Signed-off-by: Gerard Garcia <ggarcia@deic.uab.cat> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Jorgen Hansen <jhansen@vmware.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ac2291c commit 531b374

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13317,6 +13317,7 @@ 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: net/vmw_vsock/af_vsock_tap.c
1332013321
F: net/vmw_vsock/virtio_transport_common.c
1332113322
F: net/vmw_vsock/virtio_transport.c
1332213323
F: drivers/vhost/vsock.c

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/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 */

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

net/vmw_vsock/af_vsock_tap.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Tap functions for AF_VSOCK sockets.
3+
*
4+
* Code based on net/netlink/af_netlink.c tap functions.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* as published by the Free Software Foundation; either version
9+
* 2 of the License, or (at your option) any later version.
10+
*/
11+
12+
#include <linux/module.h>
13+
#include <net/sock.h>
14+
#include <net/af_vsock.h>
15+
#include <linux/if_arp.h>
16+
17+
static DEFINE_SPINLOCK(vsock_tap_lock);
18+
static struct list_head vsock_tap_all __read_mostly =
19+
LIST_HEAD_INIT(vsock_tap_all);
20+
21+
int vsock_add_tap(struct vsock_tap *vt)
22+
{
23+
if (unlikely(vt->dev->type != ARPHRD_VSOCKMON))
24+
return -EINVAL;
25+
26+
__module_get(vt->module);
27+
28+
spin_lock(&vsock_tap_lock);
29+
list_add_rcu(&vt->list, &vsock_tap_all);
30+
spin_unlock(&vsock_tap_lock);
31+
32+
return 0;
33+
}
34+
EXPORT_SYMBOL_GPL(vsock_add_tap);
35+
36+
int vsock_remove_tap(struct vsock_tap *vt)
37+
{
38+
struct vsock_tap *tmp;
39+
bool found = false;
40+
41+
spin_lock(&vsock_tap_lock);
42+
43+
list_for_each_entry(tmp, &vsock_tap_all, list) {
44+
if (vt == tmp) {
45+
list_del_rcu(&vt->list);
46+
found = true;
47+
goto out;
48+
}
49+
}
50+
51+
pr_warn("vsock_remove_tap: %p not found\n", vt);
52+
out:
53+
spin_unlock(&vsock_tap_lock);
54+
55+
synchronize_net();
56+
57+
if (found)
58+
module_put(vt->module);
59+
60+
return found ? 0 : -ENODEV;
61+
}
62+
EXPORT_SYMBOL_GPL(vsock_remove_tap);
63+
64+
static int __vsock_deliver_tap_skb(struct sk_buff *skb,
65+
struct net_device *dev)
66+
{
67+
int ret = 0;
68+
struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
69+
70+
if (nskb) {
71+
dev_hold(dev);
72+
73+
nskb->dev = dev;
74+
ret = dev_queue_xmit(nskb);
75+
if (unlikely(ret > 0))
76+
ret = net_xmit_errno(ret);
77+
78+
dev_put(dev);
79+
}
80+
81+
return ret;
82+
}
83+
84+
static void __vsock_deliver_tap(struct sk_buff *skb)
85+
{
86+
int ret;
87+
struct vsock_tap *tmp;
88+
89+
list_for_each_entry_rcu(tmp, &vsock_tap_all, list) {
90+
ret = __vsock_deliver_tap_skb(skb, tmp->dev);
91+
if (unlikely(ret))
92+
break;
93+
}
94+
}
95+
96+
void vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque)
97+
{
98+
struct sk_buff *skb;
99+
100+
rcu_read_lock();
101+
102+
if (likely(list_empty(&vsock_tap_all)))
103+
goto out;
104+
105+
skb = build_skb(opaque);
106+
if (skb) {
107+
__vsock_deliver_tap(skb);
108+
consume_skb(skb);
109+
}
110+
111+
out:
112+
rcu_read_unlock();
113+
}
114+
EXPORT_SYMBOL_GPL(vsock_deliver_tap);

0 commit comments

Comments
 (0)