Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

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