vsockmon.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /* Virtio transport max packet size plus header */
  10. #define DEFAULT_MTU (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + \
  11. sizeof(struct af_vsockmon_hdr))
  12. struct pcpu_lstats {
  13. u64 rx_packets;
  14. u64 rx_bytes;
  15. struct u64_stats_sync syncp;
  16. };
  17. static int vsockmon_dev_init(struct net_device *dev)
  18. {
  19. dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
  20. if (!dev->lstats)
  21. return -ENOMEM;
  22. return 0;
  23. }
  24. static void vsockmon_dev_uninit(struct net_device *dev)
  25. {
  26. free_percpu(dev->lstats);
  27. }
  28. struct vsockmon {
  29. struct vsock_tap vt;
  30. };
  31. static int vsockmon_open(struct net_device *dev)
  32. {
  33. struct vsockmon *vsockmon = netdev_priv(dev);
  34. vsockmon->vt.dev = dev;
  35. vsockmon->vt.module = THIS_MODULE;
  36. return vsock_add_tap(&vsockmon->vt);
  37. }
  38. static int vsockmon_close(struct net_device *dev)
  39. {
  40. struct vsockmon *vsockmon = netdev_priv(dev);
  41. return vsock_remove_tap(&vsockmon->vt);
  42. }
  43. static netdev_tx_t vsockmon_xmit(struct sk_buff *skb, struct net_device *dev)
  44. {
  45. int len = skb->len;
  46. struct pcpu_lstats *stats = this_cpu_ptr(dev->lstats);
  47. u64_stats_update_begin(&stats->syncp);
  48. stats->rx_bytes += len;
  49. stats->rx_packets++;
  50. u64_stats_update_end(&stats->syncp);
  51. dev_kfree_skb(skb);
  52. return NETDEV_TX_OK;
  53. }
  54. static void
  55. vsockmon_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  56. {
  57. int i;
  58. u64 bytes = 0, packets = 0;
  59. for_each_possible_cpu(i) {
  60. const struct pcpu_lstats *vstats;
  61. u64 tbytes, tpackets;
  62. unsigned int start;
  63. vstats = per_cpu_ptr(dev->lstats, i);
  64. do {
  65. start = u64_stats_fetch_begin_irq(&vstats->syncp);
  66. tbytes = vstats->rx_bytes;
  67. tpackets = vstats->rx_packets;
  68. } while (u64_stats_fetch_retry_irq(&vstats->syncp, start));
  69. packets += tpackets;
  70. bytes += tbytes;
  71. }
  72. stats->rx_packets = packets;
  73. stats->tx_packets = 0;
  74. stats->rx_bytes = bytes;
  75. stats->tx_bytes = 0;
  76. }
  77. static int vsockmon_is_valid_mtu(int new_mtu)
  78. {
  79. return new_mtu >= (int)sizeof(struct af_vsockmon_hdr);
  80. }
  81. static int vsockmon_change_mtu(struct net_device *dev, int new_mtu)
  82. {
  83. if (!vsockmon_is_valid_mtu(new_mtu))
  84. return -EINVAL;
  85. dev->mtu = new_mtu;
  86. return 0;
  87. }
  88. static const struct net_device_ops vsockmon_ops = {
  89. .ndo_init = vsockmon_dev_init,
  90. .ndo_uninit = vsockmon_dev_uninit,
  91. .ndo_open = vsockmon_open,
  92. .ndo_stop = vsockmon_close,
  93. .ndo_start_xmit = vsockmon_xmit,
  94. .ndo_get_stats64 = vsockmon_get_stats64,
  95. .ndo_change_mtu = vsockmon_change_mtu,
  96. };
  97. static u32 always_on(struct net_device *dev)
  98. {
  99. return 1;
  100. }
  101. static const struct ethtool_ops vsockmon_ethtool_ops = {
  102. .get_link = always_on,
  103. };
  104. static void vsockmon_setup(struct net_device *dev)
  105. {
  106. dev->type = ARPHRD_VSOCKMON;
  107. dev->priv_flags |= IFF_NO_QUEUE;
  108. dev->netdev_ops = &vsockmon_ops;
  109. dev->ethtool_ops = &vsockmon_ethtool_ops;
  110. dev->needs_free_netdev = true;
  111. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST |
  112. NETIF_F_HIGHDMA | NETIF_F_LLTX;
  113. dev->flags = IFF_NOARP;
  114. dev->mtu = DEFAULT_MTU;
  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. static __init int vsockmon_register(void)
  122. {
  123. return rtnl_link_register(&vsockmon_link_ops);
  124. }
  125. static __exit void vsockmon_unregister(void)
  126. {
  127. rtnl_link_unregister(&vsockmon_link_ops);
  128. }
  129. module_init(vsockmon_register);
  130. module_exit(vsockmon_unregister);
  131. MODULE_LICENSE("GPL v2");
  132. MODULE_AUTHOR("Gerard Garcia <ggarcia@deic.uab.cat>");
  133. MODULE_DESCRIPTION("Vsock monitoring device. Based on nlmon device.");
  134. MODULE_ALIAS_RTNL_LINK("vsockmon");