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