Linux Audio

Check our new training course

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