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