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>
8 #include <net/af_vsock.h>
9 #include <uapi/linux/vsockmon.h>
10 #include <linux/virtio_vsock.h>
12 /* Virtio transport max packet size plus header */
13 #define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
14 sizeof(struct af_vsockmon_hdr))
16 static int vsockmon_dev_init(struct net_device
*dev
)
18 dev
->lstats
= netdev_alloc_pcpu_stats(struct pcpu_lstats
);
24 static void vsockmon_dev_uninit(struct net_device
*dev
)
26 free_percpu(dev
->lstats
);
33 static int vsockmon_open(struct net_device
*dev
)
35 struct vsockmon
*vsockmon
= netdev_priv(dev
);
37 vsockmon
->vt
.dev
= dev
;
38 vsockmon
->vt
.module
= THIS_MODULE
;
39 return vsock_add_tap(&vsockmon
->vt
);
42 static int vsockmon_close(struct net_device
*dev
)
44 struct vsockmon
*vsockmon
= netdev_priv(dev
);
46 return vsock_remove_tap(&vsockmon
->vt
);
49 static netdev_tx_t
vsockmon_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
51 dev_lstats_add(dev
, skb
->len
);
59 vsockmon_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
61 dev_lstats_read(dev
, &stats
->rx_packets
, &stats
->rx_bytes
);
63 stats
->tx_packets
= 0;
67 static int vsockmon_is_valid_mtu(int new_mtu
)
69 return new_mtu
>= (int)sizeof(struct af_vsockmon_hdr
);
72 static int vsockmon_change_mtu(struct net_device
*dev
, int new_mtu
)
74 if (!vsockmon_is_valid_mtu(new_mtu
))
81 static const struct net_device_ops vsockmon_ops
= {
82 .ndo_init
= vsockmon_dev_init
,
83 .ndo_uninit
= vsockmon_dev_uninit
,
84 .ndo_open
= vsockmon_open
,
85 .ndo_stop
= vsockmon_close
,
86 .ndo_start_xmit
= vsockmon_xmit
,
87 .ndo_get_stats64
= vsockmon_get_stats64
,
88 .ndo_change_mtu
= vsockmon_change_mtu
,
91 static u32
always_on(struct net_device
*dev
)
96 static const struct ethtool_ops vsockmon_ethtool_ops
= {
97 .get_link
= always_on
,
100 static void vsockmon_setup(struct net_device
*dev
)
102 dev
->type
= ARPHRD_VSOCKMON
;
103 dev
->priv_flags
|= IFF_NO_QUEUE
;
105 dev
->netdev_ops
= &vsockmon_ops
;
106 dev
->ethtool_ops
= &vsockmon_ethtool_ops
;
107 dev
->needs_free_netdev
= true;
109 dev
->features
= NETIF_F_SG
| NETIF_F_FRAGLIST
|
110 NETIF_F_HIGHDMA
| NETIF_F_LLTX
;
112 dev
->flags
= IFF_NOARP
;
114 dev
->mtu
= DEFAULT_MTU
;
117 static struct rtnl_link_ops vsockmon_link_ops __read_mostly
= {
119 .priv_size
= sizeof(struct vsockmon
),
120 .setup
= vsockmon_setup
,
123 static __init
int vsockmon_register(void)
125 return rtnl_link_register(&vsockmon_link_ops
);
128 static __exit
void vsockmon_unregister(void)
130 rtnl_link_unregister(&vsockmon_link_ops
);
133 module_init(vsockmon_register
);
134 module_exit(vsockmon_unregister
);
136 MODULE_LICENSE("GPL v2");
137 MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
138 MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
139 MODULE_ALIAS_RTNL_LINK("vsockmon");