Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/ethtool.h>
0003 #include <linux/module.h>
0004 #include <linux/kernel.h>
0005 #include <linux/if_arp.h>
0006 #include <net/rtnetlink.h>
0007 #include <net/sock.h>
0008 #include <net/af_vsock.h>
0009 #include <uapi/linux/vsockmon.h>
0010 #include <linux/virtio_vsock.h>
0011 
0012 /* Virtio transport max packet size plus header */
0013 #define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
0014              sizeof(struct af_vsockmon_hdr))
0015 
0016 static int vsockmon_dev_init(struct net_device *dev)
0017 {
0018     dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
0019     if (!dev->lstats)
0020         return -ENOMEM;
0021     return 0;
0022 }
0023 
0024 static void vsockmon_dev_uninit(struct net_device *dev)
0025 {
0026     free_percpu(dev->lstats);
0027 }
0028 
0029 struct vsockmon {
0030     struct vsock_tap vt;
0031 };
0032 
0033 static int vsockmon_open(struct net_device *dev)
0034 {
0035     struct vsockmon *vsockmon = netdev_priv(dev);
0036 
0037     vsockmon->vt.dev = dev;
0038     vsockmon->vt.module = THIS_MODULE;
0039     return vsock_add_tap(&vsockmon->vt);
0040 }
0041 
0042 static int vsockmon_close(struct net_device *dev)
0043 {
0044     struct vsockmon *vsockmon = netdev_priv(dev);
0045 
0046     return vsock_remove_tap(&vsockmon->vt);
0047 }
0048 
0049 static netdev_tx_t vsockmon_xmit(struct sk_buff *skb, struct net_device *dev)
0050 {
0051     dev_lstats_add(dev, skb->len);
0052 
0053     dev_kfree_skb(skb);
0054 
0055     return NETDEV_TX_OK;
0056 }
0057 
0058 static void
0059 vsockmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
0060 {
0061     dev_lstats_read(dev, &stats->rx_packets, &stats->rx_bytes);
0062 
0063     stats->tx_packets = 0;
0064     stats->tx_bytes = 0;
0065 }
0066 
0067 static int vsockmon_is_valid_mtu(int new_mtu)
0068 {
0069     return new_mtu >= (int)sizeof(struct af_vsockmon_hdr);
0070 }
0071 
0072 static int vsockmon_change_mtu(struct net_device *dev, int new_mtu)
0073 {
0074     if (!vsockmon_is_valid_mtu(new_mtu))
0075         return -EINVAL;
0076 
0077     dev->mtu = new_mtu;
0078     return 0;
0079 }
0080 
0081 static const struct net_device_ops vsockmon_ops = {
0082     .ndo_init = vsockmon_dev_init,
0083     .ndo_uninit = vsockmon_dev_uninit,
0084     .ndo_open = vsockmon_open,
0085     .ndo_stop = vsockmon_close,
0086     .ndo_start_xmit = vsockmon_xmit,
0087     .ndo_get_stats64 = vsockmon_get_stats64,
0088     .ndo_change_mtu = vsockmon_change_mtu,
0089 };
0090 
0091 static u32 always_on(struct net_device *dev)
0092 {
0093     return 1;
0094 }
0095 
0096 static const struct ethtool_ops vsockmon_ethtool_ops = {
0097     .get_link = always_on,
0098 };
0099 
0100 static void vsockmon_setup(struct net_device *dev)
0101 {
0102     dev->type = ARPHRD_VSOCKMON;
0103     dev->priv_flags |= IFF_NO_QUEUE;
0104 
0105     dev->netdev_ops = &vsockmon_ops;
0106     dev->ethtool_ops = &vsockmon_ethtool_ops;
0107     dev->needs_free_netdev = true;
0108 
0109     dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
0110             NETIF_F_HIGHDMA | NETIF_F_LLTX;
0111 
0112     dev->flags = IFF_NOARP;
0113 
0114     dev->mtu = DEFAULT_MTU;
0115 }
0116 
0117 static struct rtnl_link_ops vsockmon_link_ops __read_mostly = {
0118     .kind           = "vsockmon",
0119     .priv_size      = sizeof(struct vsockmon),
0120     .setup          = vsockmon_setup,
0121 };
0122 
0123 static __init int vsockmon_register(void)
0124 {
0125     return rtnl_link_register(&vsockmon_link_ops);
0126 }
0127 
0128 static __exit void vsockmon_unregister(void)
0129 {
0130     rtnl_link_unregister(&vsockmon_link_ops);
0131 }
0132 
0133 module_init(vsockmon_register);
0134 module_exit(vsockmon_unregister);
0135 
0136 MODULE_LICENSE("GPL v2");
0137 MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
0138 MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
0139 MODULE_ALIAS_RTNL_LINK("vsockmon");