1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/if_arp.h>
5 #include <net/rtnetlink.h>
7 #include <net/af_vsock.h>
8 #include <uapi/linux/vsockmon.h>
9 #include <linux/virtio_vsock.h>
11 /* Virtio transport max packet size plus header */
12 #define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
13 sizeof(struct af_vsockmon_hdr))
15 static int vsockmon_dev_init(struct net_device
*dev
)
17 dev
->lstats
= netdev_alloc_pcpu_stats(struct pcpu_lstats
);
23 static void vsockmon_dev_uninit(struct net_device
*dev
)
25 free_percpu(dev
->lstats
);
32 static int vsockmon_open(struct net_device
*dev
)
34 struct vsockmon
*vsockmon
= netdev_priv(dev
);
36 vsockmon
->vt
.dev
= dev
;
37 vsockmon
->vt
.module
= THIS_MODULE
;
38 return vsock_add_tap(&vsockmon
->vt
);
41 static int vsockmon_close(struct net_device
*dev
)
43 struct vsockmon
*vsockmon
= netdev_priv(dev
);
45 return vsock_remove_tap(&vsockmon
->vt
);
48 static netdev_tx_t
vsockmon_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
51 struct pcpu_lstats
*stats
= this_cpu_ptr(dev
->lstats
);
53 u64_stats_update_begin(&stats
->syncp
);
56 u64_stats_update_end(&stats
->syncp
);
64 vsockmon_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
67 u64 bytes
= 0, packets
= 0;
69 for_each_possible_cpu(i
) {
70 const struct pcpu_lstats
*vstats
;
74 vstats
= per_cpu_ptr(dev
->lstats
, i
);
77 start
= u64_stats_fetch_begin_irq(&vstats
->syncp
);
78 tbytes
= vstats
->bytes
;
79 tpackets
= vstats
->packets
;
80 } while (u64_stats_fetch_retry_irq(&vstats
->syncp
, start
));
86 stats
->rx_packets
= packets
;
87 stats
->tx_packets
= 0;
89 stats
->rx_bytes
= bytes
;
93 static int vsockmon_is_valid_mtu(int new_mtu
)
95 return new_mtu
>= (int)sizeof(struct af_vsockmon_hdr
);
98 static int vsockmon_change_mtu(struct net_device
*dev
, int new_mtu
)
100 if (!vsockmon_is_valid_mtu(new_mtu
))
107 static const struct net_device_ops vsockmon_ops
= {
108 .ndo_init
= vsockmon_dev_init
,
109 .ndo_uninit
= vsockmon_dev_uninit
,
110 .ndo_open
= vsockmon_open
,
111 .ndo_stop
= vsockmon_close
,
112 .ndo_start_xmit
= vsockmon_xmit
,
113 .ndo_get_stats64
= vsockmon_get_stats64
,
114 .ndo_change_mtu
= vsockmon_change_mtu
,
117 static u32
always_on(struct net_device
*dev
)
122 static const struct ethtool_ops vsockmon_ethtool_ops
= {
123 .get_link
= always_on
,
126 static void vsockmon_setup(struct net_device
*dev
)
128 dev
->type
= ARPHRD_VSOCKMON
;
129 dev
->priv_flags
|= IFF_NO_QUEUE
;
131 dev
->netdev_ops
= &vsockmon_ops
;
132 dev
->ethtool_ops
= &vsockmon_ethtool_ops
;
133 dev
->needs_free_netdev
= true;
135 dev
->features
= NETIF_F_SG
| NETIF_F_FRAGLIST
|
136 NETIF_F_HIGHDMA
| NETIF_F_LLTX
;
138 dev
->flags
= IFF_NOARP
;
140 dev
->mtu
= DEFAULT_MTU
;
143 static struct rtnl_link_ops vsockmon_link_ops __read_mostly
= {
145 .priv_size
= sizeof(struct vsockmon
),
146 .setup
= vsockmon_setup
,
149 static __init
int vsockmon_register(void)
151 return rtnl_link_register(&vsockmon_link_ops
);
154 static __exit
void vsockmon_unregister(void)
156 rtnl_link_unregister(&vsockmon_link_ops
);
159 module_init(vsockmon_register
);
160 module_exit(vsockmon_unregister
);
162 MODULE_LICENSE("GPL v2");
163 MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
164 MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
165 MODULE_ALIAS_RTNL_LINK("vsockmon");