vsockmon.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /* Virtio transport max packet size plus header */
  12. #define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
  13. sizeof(struct af_vsockmon_hdr))
  14. struct vsockmon {
  15. struct vsock_tap vt;
  16. };
  17. static int vsockmon_open(struct net_device *dev)
  18. {
  19. struct vsockmon *vsockmon = netdev_priv(dev);
  20. vsockmon->vt.dev = dev;
  21. vsockmon->vt.module = THIS_MODULE;
  22. return vsock_add_tap(&vsockmon->vt);
  23. }
  24. static int vsockmon_close(struct net_device *dev)
  25. {
  26. struct vsockmon *vsockmon = netdev_priv(dev);
  27. return vsock_remove_tap(&vsockmon->vt);
  28. }
  29. static netdev_tx_t vsockmon_xmit(struct sk_buff *skb, struct net_device *dev)
  30. {
  31. dev_lstats_add(dev, skb->len);
  32. dev_kfree_skb(skb);
  33. return NETDEV_TX_OK;
  34. }
  35. static void
  36. vsockmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  37. {
  38. dev_lstats_read(dev, &stats->rx_packets, &stats->rx_bytes);
  39. }
  40. static int vsockmon_is_valid_mtu(int new_mtu)
  41. {
  42. return new_mtu >= (int)sizeof(struct af_vsockmon_hdr);
  43. }
  44. static int vsockmon_change_mtu(struct net_device *dev, int new_mtu)
  45. {
  46. if (!vsockmon_is_valid_mtu(new_mtu))
  47. return -EINVAL;
  48. WRITE_ONCE(dev->mtu, new_mtu);
  49. return 0;
  50. }
  51. static const struct net_device_ops vsockmon_ops = {
  52. .ndo_open = vsockmon_open,
  53. .ndo_stop = vsockmon_close,
  54. .ndo_start_xmit = vsockmon_xmit,
  55. .ndo_get_stats64 = vsockmon_get_stats64,
  56. .ndo_change_mtu = vsockmon_change_mtu,
  57. };
  58. static u32 always_on(struct net_device *dev)
  59. {
  60. return 1;
  61. }
  62. static const struct ethtool_ops vsockmon_ethtool_ops = {
  63. .get_link = always_on,
  64. };
  65. static void vsockmon_setup(struct net_device *dev)
  66. {
  67. dev->type = ARPHRD_VSOCKMON;
  68. dev->priv_flags |= IFF_NO_QUEUE;
  69. dev->lltx = true;
  70. dev->netdev_ops = &vsockmon_ops;
  71. dev->ethtool_ops = &vsockmon_ethtool_ops;
  72. dev->needs_free_netdev = true;
  73. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  74. dev->flags = IFF_NOARP;
  75. dev->mtu = DEFAULT_MTU;
  76. dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
  77. }
  78. static struct rtnl_link_ops vsockmon_link_ops __read_mostly = {
  79. .kind = "vsockmon",
  80. .priv_size = sizeof(struct vsockmon),
  81. .setup = vsockmon_setup,
  82. };
  83. static __init int vsockmon_register(void)
  84. {
  85. return rtnl_link_register(&vsockmon_link_ops);
  86. }
  87. static __exit void vsockmon_unregister(void)
  88. {
  89. rtnl_link_unregister(&vsockmon_link_ops);
  90. }
  91. module_init(vsockmon_register);
  92. module_exit(vsockmon_unregister);
  93. MODULE_LICENSE("GPL v2");
  94. MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
  95. MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
  96. MODULE_ALIAS_RTNL_LINK("vsockmon");