Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 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 | |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 15 | static 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 | |
| 23 | static void vsockmon_dev_uninit(struct net_device *dev) |
| 24 | { |
| 25 | free_percpu(dev->lstats); |
| 26 | } |
| 27 | |
| 28 | struct vsockmon { |
| 29 | struct vsock_tap vt; |
| 30 | }; |
| 31 | |
| 32 | static 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 | |
| 41 | static 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 | |
| 48 | static netdev_tx_t vsockmon_xmit(struct sk_buff *skb, struct net_device *dev) |
| 49 | { |
Eric Dumazet | 4f77eb0 | 2019-11-07 16:27:18 -0800 | [diff] [blame] | 50 | dev_lstats_add(dev, skb->len); |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 51 | |
| 52 | dev_kfree_skb(skb); |
| 53 | |
| 54 | return NETDEV_TX_OK; |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | vsockmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) |
| 59 | { |
Eric Dumazet | 4f77eb0 | 2019-11-07 16:27:18 -0800 | [diff] [blame] | 60 | dev_lstats_read(dev, &stats->rx_packets, &stats->rx_bytes); |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 61 | |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 62 | stats->tx_packets = 0; |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 63 | stats->tx_bytes = 0; |
| 64 | } |
| 65 | |
| 66 | static int vsockmon_is_valid_mtu(int new_mtu) |
| 67 | { |
| 68 | return new_mtu >= (int)sizeof(struct af_vsockmon_hdr); |
| 69 | } |
| 70 | |
| 71 | static int vsockmon_change_mtu(struct net_device *dev, int new_mtu) |
| 72 | { |
| 73 | if (!vsockmon_is_valid_mtu(new_mtu)) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | dev->mtu = new_mtu; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static const struct net_device_ops vsockmon_ops = { |
| 81 | .ndo_init = vsockmon_dev_init, |
| 82 | .ndo_uninit = vsockmon_dev_uninit, |
| 83 | .ndo_open = vsockmon_open, |
| 84 | .ndo_stop = vsockmon_close, |
| 85 | .ndo_start_xmit = vsockmon_xmit, |
| 86 | .ndo_get_stats64 = vsockmon_get_stats64, |
| 87 | .ndo_change_mtu = vsockmon_change_mtu, |
| 88 | }; |
| 89 | |
| 90 | static u32 always_on(struct net_device *dev) |
| 91 | { |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | static const struct ethtool_ops vsockmon_ethtool_ops = { |
| 96 | .get_link = always_on, |
| 97 | }; |
| 98 | |
| 99 | static void vsockmon_setup(struct net_device *dev) |
| 100 | { |
| 101 | dev->type = ARPHRD_VSOCKMON; |
| 102 | dev->priv_flags |= IFF_NO_QUEUE; |
| 103 | |
| 104 | dev->netdev_ops = &vsockmon_ops; |
| 105 | dev->ethtool_ops = &vsockmon_ethtool_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 106 | dev->needs_free_netdev = true; |
Gerard Garcia | 0b2e664 | 2017-04-21 10:10:45 +0100 | [diff] [blame] | 107 | |
| 108 | dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | |
| 109 | NETIF_F_HIGHDMA | NETIF_F_LLTX; |
| 110 | |
| 111 | dev->flags = IFF_NOARP; |
| 112 | |
| 113 | dev->mtu = DEFAULT_MTU; |
| 114 | } |
| 115 | |
| 116 | static struct rtnl_link_ops vsockmon_link_ops __read_mostly = { |
| 117 | .kind = "vsockmon", |
| 118 | .priv_size = sizeof(struct vsockmon), |
| 119 | .setup = vsockmon_setup, |
| 120 | }; |
| 121 | |
| 122 | static __init int vsockmon_register(void) |
| 123 | { |
| 124 | return rtnl_link_register(&vsockmon_link_ops); |
| 125 | } |
| 126 | |
| 127 | static __exit void vsockmon_unregister(void) |
| 128 | { |
| 129 | rtnl_link_unregister(&vsockmon_link_ops); |
| 130 | } |
| 131 | |
| 132 | module_init(vsockmon_register); |
| 133 | module_exit(vsockmon_unregister); |
| 134 | |
| 135 | MODULE_LICENSE("GPL v2"); |
| 136 | MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>"); |
| 137 | MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device."); |
| 138 | MODULE_ALIAS_RTNL_LINK("vsockmon"); |