macvtap.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/etherdevice.h>
  3. #include <linux/if_macvlan.h>
  4. #include <linux/if_tap.h>
  5. #include <linux/if_vlan.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/nsproxy.h>
  8. #include <linux/compat.h>
  9. #include <linux/if_tun.h>
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/cache.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/wait.h>
  17. #include <linux/cdev.h>
  18. #include <linux/idr.h>
  19. #include <linux/fs.h>
  20. #include <linux/uio.h>
  21. #include <net/net_namespace.h>
  22. #include <net/rtnetlink.h>
  23. #include <net/sock.h>
  24. #include <linux/virtio_net.h>
  25. #include <linux/skb_array.h>
  26. struct macvtap_dev {
  27. struct macvlan_dev vlan;
  28. struct tap_dev tap;
  29. };
  30. /*
  31. * Variables for dealing with macvtaps device numbers.
  32. */
  33. static dev_t macvtap_major;
  34. static const void *macvtap_net_namespace(struct device *d)
  35. {
  36. struct net_device *dev = to_net_dev(d->parent);
  37. return dev_net(dev);
  38. }
  39. static struct class macvtap_class = {
  40. .name = "macvtap",
  41. .owner = THIS_MODULE,
  42. .ns_type = &net_ns_type_operations,
  43. .namespace = macvtap_net_namespace,
  44. };
  45. static struct cdev macvtap_cdev;
  46. #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
  47. NETIF_F_TSO6)
  48. static void macvtap_count_tx_dropped(struct tap_dev *tap)
  49. {
  50. struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
  51. struct macvlan_dev *vlan = &vlantap->vlan;
  52. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  53. }
  54. static void macvtap_count_rx_dropped(struct tap_dev *tap)
  55. {
  56. struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
  57. struct macvlan_dev *vlan = &vlantap->vlan;
  58. macvlan_count_rx(vlan, 0, 0, 0);
  59. }
  60. static void macvtap_update_features(struct tap_dev *tap,
  61. netdev_features_t features)
  62. {
  63. struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
  64. struct macvlan_dev *vlan = &vlantap->vlan;
  65. vlan->set_features = features;
  66. netdev_update_features(vlan->dev);
  67. }
  68. static int macvtap_newlink(struct net *src_net, struct net_device *dev,
  69. struct nlattr *tb[], struct nlattr *data[],
  70. struct netlink_ext_ack *extack)
  71. {
  72. struct macvtap_dev *vlantap = netdev_priv(dev);
  73. int err;
  74. INIT_LIST_HEAD(&vlantap->tap.queue_list);
  75. /* Since macvlan supports all offloads by default, make
  76. * tap support all offloads also.
  77. */
  78. vlantap->tap.tap_features = TUN_OFFLOADS;
  79. /* Register callbacks for rx/tx drops accounting and updating
  80. * net_device features
  81. */
  82. vlantap->tap.count_tx_dropped = macvtap_count_tx_dropped;
  83. vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped;
  84. vlantap->tap.update_features = macvtap_update_features;
  85. err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap);
  86. if (err)
  87. return err;
  88. /* Don't put anything that may fail after macvlan_common_newlink
  89. * because we can't undo what it does.
  90. */
  91. err = macvlan_common_newlink(src_net, dev, tb, data, extack);
  92. if (err) {
  93. netdev_rx_handler_unregister(dev);
  94. return err;
  95. }
  96. vlantap->tap.dev = vlantap->vlan.dev;
  97. return 0;
  98. }
  99. static void macvtap_dellink(struct net_device *dev,
  100. struct list_head *head)
  101. {
  102. struct macvtap_dev *vlantap = netdev_priv(dev);
  103. netdev_rx_handler_unregister(dev);
  104. tap_del_queues(&vlantap->tap);
  105. macvlan_dellink(dev, head);
  106. }
  107. static void macvtap_setup(struct net_device *dev)
  108. {
  109. macvlan_common_setup(dev);
  110. dev->tx_queue_len = TUN_READQ_SIZE;
  111. }
  112. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  113. .kind = "macvtap",
  114. .setup = macvtap_setup,
  115. .newlink = macvtap_newlink,
  116. .dellink = macvtap_dellink,
  117. .priv_size = sizeof(struct macvtap_dev),
  118. };
  119. static int macvtap_device_event(struct notifier_block *unused,
  120. unsigned long event, void *ptr)
  121. {
  122. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  123. struct macvtap_dev *vlantap;
  124. struct device *classdev;
  125. dev_t devt;
  126. int err;
  127. char tap_name[IFNAMSIZ];
  128. if (dev->rtnl_link_ops != &macvtap_link_ops)
  129. return NOTIFY_DONE;
  130. snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
  131. vlantap = netdev_priv(dev);
  132. switch (event) {
  133. case NETDEV_REGISTER:
  134. /* Create the device node here after the network device has
  135. * been registered but before register_netdevice has
  136. * finished running.
  137. */
  138. err = tap_get_minor(macvtap_major, &vlantap->tap);
  139. if (err)
  140. return notifier_from_errno(err);
  141. devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
  142. classdev = device_create(&macvtap_class, &dev->dev, devt,
  143. dev, tap_name);
  144. if (IS_ERR(classdev)) {
  145. tap_free_minor(macvtap_major, &vlantap->tap);
  146. return notifier_from_errno(PTR_ERR(classdev));
  147. }
  148. err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
  149. tap_name);
  150. if (err)
  151. return notifier_from_errno(err);
  152. break;
  153. case NETDEV_UNREGISTER:
  154. /* vlan->minor == 0 if NETDEV_REGISTER above failed */
  155. if (vlantap->tap.minor == 0)
  156. break;
  157. sysfs_remove_link(&dev->dev.kobj, tap_name);
  158. devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
  159. device_destroy(&macvtap_class, devt);
  160. tap_free_minor(macvtap_major, &vlantap->tap);
  161. break;
  162. case NETDEV_CHANGE_TX_QUEUE_LEN:
  163. if (tap_queue_resize(&vlantap->tap))
  164. return NOTIFY_BAD;
  165. break;
  166. }
  167. return NOTIFY_DONE;
  168. }
  169. static struct notifier_block macvtap_notifier_block __read_mostly = {
  170. .notifier_call = macvtap_device_event,
  171. };
  172. static int macvtap_init(void)
  173. {
  174. int err;
  175. err = tap_create_cdev(&macvtap_cdev, &macvtap_major, "macvtap",
  176. THIS_MODULE);
  177. if (err)
  178. goto out1;
  179. err = class_register(&macvtap_class);
  180. if (err)
  181. goto out2;
  182. err = register_netdevice_notifier(&macvtap_notifier_block);
  183. if (err)
  184. goto out3;
  185. err = macvlan_link_register(&macvtap_link_ops);
  186. if (err)
  187. goto out4;
  188. return 0;
  189. out4:
  190. unregister_netdevice_notifier(&macvtap_notifier_block);
  191. out3:
  192. class_unregister(&macvtap_class);
  193. out2:
  194. tap_destroy_cdev(macvtap_major, &macvtap_cdev);
  195. out1:
  196. return err;
  197. }
  198. module_init(macvtap_init);
  199. static void macvtap_exit(void)
  200. {
  201. rtnl_link_unregister(&macvtap_link_ops);
  202. unregister_netdevice_notifier(&macvtap_notifier_block);
  203. class_unregister(&macvtap_class);
  204. tap_destroy_cdev(macvtap_major, &macvtap_cdev);
  205. }
  206. module_exit(macvtap_exit);
  207. MODULE_ALIAS_RTNL_LINK("macvtap");
  208. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  209. MODULE_LICENSE("GPL");