xref: /linux/drivers/net/vsockmon.c (revision bfb921b2a9d5d1123d1d10b196a39db629ddef87)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/ethtool.h>
3 #include <linux/module.h>
4 #include <linux/kernel.h>
5 #include <linux/if_arp.h>
6 #include <net/rtnetlink.h>
7 #include <net/sock.h>
8 #include <net/af_vsock.h>
9 #include <uapi/linux/vsockmon.h>
10 #include <linux/virtio_vsock.h>
11 
12 /* Virtio transport max packet size plus header */
13 #define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
14 		     sizeof(struct af_vsockmon_hdr))
15 
16 struct vsockmon {
17 	struct vsock_tap vt;
18 };
19 
20 static int vsockmon_open(struct net_device *dev)
21 {
22 	struct vsockmon *vsockmon = netdev_priv(dev);
23 
24 	vsockmon->vt.dev = dev;
25 	vsockmon->vt.module = THIS_MODULE;
26 	return vsock_add_tap(&vsockmon->vt);
27 }
28 
29 static int vsockmon_close(struct net_device *dev)
30 {
31 	struct vsockmon *vsockmon = netdev_priv(dev);
32 
33 	return vsock_remove_tap(&vsockmon->vt);
34 }
35 
36 static netdev_tx_t vsockmon_xmit(struct sk_buff *skb, struct net_device *dev)
37 {
38 	dev_lstats_add(dev, skb->len);
39 
40 	dev_kfree_skb(skb);
41 
42 	return NETDEV_TX_OK;
43 }
44 
45 static void
46 vsockmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
47 {
48 	dev_lstats_read(dev, &stats->rx_packets, &stats->rx_bytes);
49 }
50 
51 static int vsockmon_is_valid_mtu(int new_mtu)
52 {
53 	return new_mtu >= (int)sizeof(struct af_vsockmon_hdr);
54 }
55 
56 static int vsockmon_change_mtu(struct net_device *dev, int new_mtu)
57 {
58 	if (!vsockmon_is_valid_mtu(new_mtu))
59 		return -EINVAL;
60 
61 	WRITE_ONCE(dev->mtu, new_mtu);
62 	return 0;
63 }
64 
65 static const struct net_device_ops vsockmon_ops = {
66 	.ndo_open = vsockmon_open,
67 	.ndo_stop = vsockmon_close,
68 	.ndo_start_xmit = vsockmon_xmit,
69 	.ndo_get_stats64 = vsockmon_get_stats64,
70 	.ndo_change_mtu = vsockmon_change_mtu,
71 };
72 
73 static u32 always_on(struct net_device *dev)
74 {
75 	return 1;
76 }
77 
78 static const struct ethtool_ops vsockmon_ethtool_ops = {
79 	.get_link = always_on,
80 };
81 
82 static void vsockmon_setup(struct net_device *dev)
83 {
84 	dev->type = ARPHRD_VSOCKMON;
85 	dev->priv_flags |= IFF_NO_QUEUE;
86 
87 	dev->netdev_ops	= &vsockmon_ops;
88 	dev->ethtool_ops = &vsockmon_ethtool_ops;
89 	dev->needs_free_netdev = true;
90 
91 	dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
92 			NETIF_F_HIGHDMA | NETIF_F_LLTX;
93 
94 	dev->flags = IFF_NOARP;
95 
96 	dev->mtu = DEFAULT_MTU;
97 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
98 }
99 
100 static struct rtnl_link_ops vsockmon_link_ops __read_mostly = {
101 	.kind			= "vsockmon",
102 	.priv_size		= sizeof(struct vsockmon),
103 	.setup			= vsockmon_setup,
104 };
105 
106 static __init int vsockmon_register(void)
107 {
108 	return rtnl_link_register(&vsockmon_link_ops);
109 }
110 
111 static __exit void vsockmon_unregister(void)
112 {
113 	rtnl_link_unregister(&vsockmon_link_ops);
114 }
115 
116 module_init(vsockmon_register);
117 module_exit(vsockmon_unregister);
118 
119 MODULE_LICENSE("GPL v2");
120 MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
121 MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
122 MODULE_ALIAS_RTNL_LINK("vsockmon");
123