macvtap.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/compat.h>
  7. #include <linux/if_tun.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/cache.h>
  11. #include <linux/sched.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/cdev.h>
  16. #include <linux/idr.h>
  17. #include <linux/fs.h>
  18. #include <linux/uio.h>
  19. #include <net/net_namespace.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <linux/virtio_net.h>
  23. #include <linux/skb_array.h>
  24. /*
  25. * A macvtap queue is the central object of this driver, it connects
  26. * an open character device to a macvlan interface. There can be
  27. * multiple queues on one interface, which map back to queues
  28. * implemented in hardware on the underlying device.
  29. *
  30. * macvtap_proto is used to allocate queues through the sock allocation
  31. * mechanism.
  32. *
  33. */
  34. struct macvtap_queue {
  35. struct sock sk;
  36. struct socket sock;
  37. struct socket_wq wq;
  38. int vnet_hdr_sz;
  39. struct macvlan_dev __rcu *vlan;
  40. struct file *file;
  41. unsigned int flags;
  42. u16 queue_index;
  43. bool enabled;
  44. struct list_head next;
  45. struct skb_array skb_array;
  46. };
  47. #define MACVTAP_FEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
  48. #define MACVTAP_VNET_LE 0x80000000
  49. #define MACVTAP_VNET_BE 0x40000000
  50. #ifdef CONFIG_TUN_VNET_CROSS_LE
  51. static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
  52. {
  53. return q->flags & MACVTAP_VNET_BE ? false :
  54. virtio_legacy_is_little_endian();
  55. }
  56. static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *sp)
  57. {
  58. int s = !!(q->flags & MACVTAP_VNET_BE);
  59. if (put_user(s, sp))
  60. return -EFAULT;
  61. return 0;
  62. }
  63. static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *sp)
  64. {
  65. int s;
  66. if (get_user(s, sp))
  67. return -EFAULT;
  68. if (s)
  69. q->flags |= MACVTAP_VNET_BE;
  70. else
  71. q->flags &= ~MACVTAP_VNET_BE;
  72. return 0;
  73. }
  74. #else
  75. static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
  76. {
  77. return virtio_legacy_is_little_endian();
  78. }
  79. static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *argp)
  80. {
  81. return -EINVAL;
  82. }
  83. static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *argp)
  84. {
  85. return -EINVAL;
  86. }
  87. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  88. static inline bool macvtap_is_little_endian(struct macvtap_queue *q)
  89. {
  90. return q->flags & MACVTAP_VNET_LE ||
  91. macvtap_legacy_is_little_endian(q);
  92. }
  93. static inline u16 macvtap16_to_cpu(struct macvtap_queue *q, __virtio16 val)
  94. {
  95. return __virtio16_to_cpu(macvtap_is_little_endian(q), val);
  96. }
  97. static inline __virtio16 cpu_to_macvtap16(struct macvtap_queue *q, u16 val)
  98. {
  99. return __cpu_to_virtio16(macvtap_is_little_endian(q), val);
  100. }
  101. static struct proto macvtap_proto = {
  102. .name = "macvtap",
  103. .owner = THIS_MODULE,
  104. .obj_size = sizeof (struct macvtap_queue),
  105. };
  106. /*
  107. * Variables for dealing with macvtaps device numbers.
  108. */
  109. static dev_t macvtap_major;
  110. #define MACVTAP_NUM_DEVS (1U << MINORBITS)
  111. static DEFINE_MUTEX(minor_lock);
  112. static DEFINE_IDR(minor_idr);
  113. #define GOODCOPY_LEN 128
  114. static const void *macvtap_net_namespace(struct device *d)
  115. {
  116. struct net_device *dev = to_net_dev(d->parent);
  117. return dev_net(dev);
  118. }
  119. static struct class macvtap_class = {
  120. .name = "macvtap",
  121. .owner = THIS_MODULE,
  122. .ns_type = &net_ns_type_operations,
  123. .namespace = macvtap_net_namespace,
  124. };
  125. static struct cdev macvtap_cdev;
  126. static const struct proto_ops macvtap_socket_ops;
  127. #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
  128. NETIF_F_TSO6 | NETIF_F_UFO)
  129. #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
  130. #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
  131. static struct macvlan_dev *macvtap_get_vlan_rcu(const struct net_device *dev)
  132. {
  133. return rcu_dereference(dev->rx_handler_data);
  134. }
  135. /*
  136. * RCU usage:
  137. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  138. * pointers from one to the other can only be read while rcu_read_lock
  139. * or rtnl is held.
  140. *
  141. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  142. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  143. * q->vlan becomes inaccessible. When the files gets closed,
  144. * macvtap_get_queue() fails.
  145. *
  146. * There may still be references to the struct sock inside of the
  147. * queue from outbound SKBs, but these never reference back to the
  148. * file or the dev. The data structure is freed through __sk_free
  149. * when both our references and any pending SKBs are gone.
  150. */
  151. static int macvtap_enable_queue(struct net_device *dev, struct file *file,
  152. struct macvtap_queue *q)
  153. {
  154. struct macvlan_dev *vlan = netdev_priv(dev);
  155. int err = -EINVAL;
  156. ASSERT_RTNL();
  157. if (q->enabled)
  158. goto out;
  159. err = 0;
  160. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  161. q->queue_index = vlan->numvtaps;
  162. q->enabled = true;
  163. vlan->numvtaps++;
  164. out:
  165. return err;
  166. }
  167. /* Requires RTNL */
  168. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  169. struct macvtap_queue *q)
  170. {
  171. struct macvlan_dev *vlan = netdev_priv(dev);
  172. if (vlan->numqueues == MAX_MACVTAP_QUEUES)
  173. return -EBUSY;
  174. rcu_assign_pointer(q->vlan, vlan);
  175. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  176. sock_hold(&q->sk);
  177. q->file = file;
  178. q->queue_index = vlan->numvtaps;
  179. q->enabled = true;
  180. file->private_data = q;
  181. list_add_tail(&q->next, &vlan->queue_list);
  182. vlan->numvtaps++;
  183. vlan->numqueues++;
  184. return 0;
  185. }
  186. static int macvtap_disable_queue(struct macvtap_queue *q)
  187. {
  188. struct macvlan_dev *vlan;
  189. struct macvtap_queue *nq;
  190. ASSERT_RTNL();
  191. if (!q->enabled)
  192. return -EINVAL;
  193. vlan = rtnl_dereference(q->vlan);
  194. if (vlan) {
  195. int index = q->queue_index;
  196. BUG_ON(index >= vlan->numvtaps);
  197. nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
  198. nq->queue_index = index;
  199. rcu_assign_pointer(vlan->taps[index], nq);
  200. RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
  201. q->enabled = false;
  202. vlan->numvtaps--;
  203. }
  204. return 0;
  205. }
  206. /*
  207. * The file owning the queue got closed, give up both
  208. * the reference that the files holds as well as the
  209. * one from the macvlan_dev if that still exists.
  210. *
  211. * Using the spinlock makes sure that we don't get
  212. * to the queue again after destroying it.
  213. */
  214. static void macvtap_put_queue(struct macvtap_queue *q)
  215. {
  216. struct macvlan_dev *vlan;
  217. rtnl_lock();
  218. vlan = rtnl_dereference(q->vlan);
  219. if (vlan) {
  220. if (q->enabled)
  221. BUG_ON(macvtap_disable_queue(q));
  222. vlan->numqueues--;
  223. RCU_INIT_POINTER(q->vlan, NULL);
  224. sock_put(&q->sk);
  225. list_del_init(&q->next);
  226. }
  227. rtnl_unlock();
  228. synchronize_rcu();
  229. sock_put(&q->sk);
  230. }
  231. /*
  232. * Select a queue based on the rxq of the device on which this packet
  233. * arrived. If the incoming device is not mq, calculate a flow hash
  234. * to select a queue. If all fails, find the first available queue.
  235. * Cache vlan->numvtaps since it can become zero during the execution
  236. * of this function.
  237. */
  238. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  239. struct sk_buff *skb)
  240. {
  241. struct macvlan_dev *vlan = netdev_priv(dev);
  242. struct macvtap_queue *tap = NULL;
  243. /* Access to taps array is protected by rcu, but access to numvtaps
  244. * isn't. Below we use it to lookup a queue, but treat it as a hint
  245. * and validate that the result isn't NULL - in case we are
  246. * racing against queue removal.
  247. */
  248. int numvtaps = ACCESS_ONCE(vlan->numvtaps);
  249. __u32 rxq;
  250. if (!numvtaps)
  251. goto out;
  252. if (numvtaps == 1)
  253. goto single;
  254. /* Check if we can use flow to select a queue */
  255. rxq = skb_get_hash(skb);
  256. if (rxq) {
  257. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  258. goto out;
  259. }
  260. if (likely(skb_rx_queue_recorded(skb))) {
  261. rxq = skb_get_rx_queue(skb);
  262. while (unlikely(rxq >= numvtaps))
  263. rxq -= numvtaps;
  264. tap = rcu_dereference(vlan->taps[rxq]);
  265. goto out;
  266. }
  267. single:
  268. tap = rcu_dereference(vlan->taps[0]);
  269. out:
  270. return tap;
  271. }
  272. /*
  273. * The net_device is going away, give up the reference
  274. * that it holds on all queues and safely set the pointer
  275. * from the queues to NULL.
  276. */
  277. static void macvtap_del_queues(struct net_device *dev)
  278. {
  279. struct macvlan_dev *vlan = netdev_priv(dev);
  280. struct macvtap_queue *q, *tmp;
  281. ASSERT_RTNL();
  282. list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
  283. list_del_init(&q->next);
  284. RCU_INIT_POINTER(q->vlan, NULL);
  285. if (q->enabled)
  286. vlan->numvtaps--;
  287. vlan->numqueues--;
  288. sock_put(&q->sk);
  289. }
  290. BUG_ON(vlan->numvtaps);
  291. BUG_ON(vlan->numqueues);
  292. /* guarantee that any future macvtap_set_queue will fail */
  293. vlan->numvtaps = MAX_MACVTAP_QUEUES;
  294. }
  295. static rx_handler_result_t macvtap_handle_frame(struct sk_buff **pskb)
  296. {
  297. struct sk_buff *skb = *pskb;
  298. struct net_device *dev = skb->dev;
  299. struct macvlan_dev *vlan;
  300. struct macvtap_queue *q;
  301. netdev_features_t features = TAP_FEATURES;
  302. vlan = macvtap_get_vlan_rcu(dev);
  303. if (!vlan)
  304. return RX_HANDLER_PASS;
  305. q = macvtap_get_queue(dev, skb);
  306. if (!q)
  307. return RX_HANDLER_PASS;
  308. if (__skb_array_full(&q->skb_array))
  309. goto drop;
  310. skb_push(skb, ETH_HLEN);
  311. /* Apply the forward feature mask so that we perform segmentation
  312. * according to users wishes. This only works if VNET_HDR is
  313. * enabled.
  314. */
  315. if (q->flags & IFF_VNET_HDR)
  316. features |= vlan->tap_features;
  317. if (netif_needs_gso(skb, features)) {
  318. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  319. if (IS_ERR(segs))
  320. goto drop;
  321. if (!segs) {
  322. if (skb_array_produce(&q->skb_array, skb))
  323. goto drop;
  324. goto wake_up;
  325. }
  326. consume_skb(skb);
  327. while (segs) {
  328. struct sk_buff *nskb = segs->next;
  329. segs->next = NULL;
  330. if (skb_array_produce(&q->skb_array, segs)) {
  331. kfree_skb(segs);
  332. kfree_skb_list(nskb);
  333. break;
  334. }
  335. segs = nskb;
  336. }
  337. } else {
  338. /* If we receive a partial checksum and the tap side
  339. * doesn't support checksum offload, compute the checksum.
  340. * Note: it doesn't matter which checksum feature to
  341. * check, we either support them all or none.
  342. */
  343. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  344. !(features & NETIF_F_CSUM_MASK) &&
  345. skb_checksum_help(skb))
  346. goto drop;
  347. if (skb_array_produce(&q->skb_array, skb))
  348. goto drop;
  349. }
  350. wake_up:
  351. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  352. return RX_HANDLER_CONSUMED;
  353. drop:
  354. /* Count errors/drops only here, thus don't care about args. */
  355. macvlan_count_rx(vlan, 0, 0, 0);
  356. kfree_skb(skb);
  357. return RX_HANDLER_CONSUMED;
  358. }
  359. static int macvtap_get_minor(struct macvlan_dev *vlan)
  360. {
  361. int retval = -ENOMEM;
  362. mutex_lock(&minor_lock);
  363. retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL);
  364. if (retval >= 0) {
  365. vlan->minor = retval;
  366. } else if (retval == -ENOSPC) {
  367. printk(KERN_ERR "too many macvtap devices\n");
  368. retval = -EINVAL;
  369. }
  370. mutex_unlock(&minor_lock);
  371. return retval < 0 ? retval : 0;
  372. }
  373. static void macvtap_free_minor(struct macvlan_dev *vlan)
  374. {
  375. mutex_lock(&minor_lock);
  376. if (vlan->minor) {
  377. idr_remove(&minor_idr, vlan->minor);
  378. vlan->minor = 0;
  379. }
  380. mutex_unlock(&minor_lock);
  381. }
  382. static struct net_device *dev_get_by_macvtap_minor(int minor)
  383. {
  384. struct net_device *dev = NULL;
  385. struct macvlan_dev *vlan;
  386. mutex_lock(&minor_lock);
  387. vlan = idr_find(&minor_idr, minor);
  388. if (vlan) {
  389. dev = vlan->dev;
  390. dev_hold(dev);
  391. }
  392. mutex_unlock(&minor_lock);
  393. return dev;
  394. }
  395. static int macvtap_newlink(struct net *src_net,
  396. struct net_device *dev,
  397. struct nlattr *tb[],
  398. struct nlattr *data[])
  399. {
  400. struct macvlan_dev *vlan = netdev_priv(dev);
  401. int err;
  402. INIT_LIST_HEAD(&vlan->queue_list);
  403. /* Since macvlan supports all offloads by default, make
  404. * tap support all offloads also.
  405. */
  406. vlan->tap_features = TUN_OFFLOADS;
  407. err = netdev_rx_handler_register(dev, macvtap_handle_frame, vlan);
  408. if (err)
  409. return err;
  410. /* Don't put anything that may fail after macvlan_common_newlink
  411. * because we can't undo what it does.
  412. */
  413. err = macvlan_common_newlink(src_net, dev, tb, data);
  414. if (err) {
  415. netdev_rx_handler_unregister(dev);
  416. return err;
  417. }
  418. return 0;
  419. }
  420. static void macvtap_dellink(struct net_device *dev,
  421. struct list_head *head)
  422. {
  423. netdev_rx_handler_unregister(dev);
  424. macvtap_del_queues(dev);
  425. macvlan_dellink(dev, head);
  426. }
  427. static void macvtap_setup(struct net_device *dev)
  428. {
  429. macvlan_common_setup(dev);
  430. dev->tx_queue_len = TUN_READQ_SIZE;
  431. }
  432. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  433. .kind = "macvtap",
  434. .setup = macvtap_setup,
  435. .newlink = macvtap_newlink,
  436. .dellink = macvtap_dellink,
  437. };
  438. static void macvtap_sock_write_space(struct sock *sk)
  439. {
  440. wait_queue_head_t *wqueue;
  441. if (!sock_writeable(sk) ||
  442. !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  443. return;
  444. wqueue = sk_sleep(sk);
  445. if (wqueue && waitqueue_active(wqueue))
  446. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  447. }
  448. static void macvtap_sock_destruct(struct sock *sk)
  449. {
  450. struct macvtap_queue *q = container_of(sk, struct macvtap_queue, sk);
  451. skb_array_cleanup(&q->skb_array);
  452. }
  453. static int macvtap_open(struct inode *inode, struct file *file)
  454. {
  455. struct net *net = current->nsproxy->net_ns;
  456. struct net_device *dev;
  457. struct macvtap_queue *q;
  458. int err = -ENODEV;
  459. rtnl_lock();
  460. dev = dev_get_by_macvtap_minor(iminor(inode));
  461. if (!dev)
  462. goto err;
  463. err = -ENOMEM;
  464. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  465. &macvtap_proto, 0);
  466. if (!q)
  467. goto err;
  468. if (skb_array_init(&q->skb_array, dev->tx_queue_len, GFP_KERNEL)) {
  469. sk_free(&q->sk);
  470. goto err;
  471. }
  472. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  473. init_waitqueue_head(&q->wq.wait);
  474. q->sock.type = SOCK_RAW;
  475. q->sock.state = SS_CONNECTED;
  476. q->sock.file = file;
  477. q->sock.ops = &macvtap_socket_ops;
  478. sock_init_data(&q->sock, &q->sk);
  479. q->sk.sk_write_space = macvtap_sock_write_space;
  480. q->sk.sk_destruct = macvtap_sock_destruct;
  481. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  482. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  483. /*
  484. * so far only KVM virtio_net uses macvtap, enable zero copy between
  485. * guest kernel and host kernel when lower device supports zerocopy
  486. *
  487. * The macvlan supports zerocopy iff the lower device supports zero
  488. * copy so we don't have to look at the lower device directly.
  489. */
  490. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  491. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  492. err = macvtap_set_queue(dev, file, q);
  493. if (err) {
  494. /* macvtap_sock_destruct() will take care of freeing skb_array */
  495. goto err_put;
  496. }
  497. dev_put(dev);
  498. rtnl_unlock();
  499. return err;
  500. err_put:
  501. sock_put(&q->sk);
  502. err:
  503. if (dev)
  504. dev_put(dev);
  505. rtnl_unlock();
  506. return err;
  507. }
  508. static int macvtap_release(struct inode *inode, struct file *file)
  509. {
  510. struct macvtap_queue *q = file->private_data;
  511. macvtap_put_queue(q);
  512. return 0;
  513. }
  514. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  515. {
  516. struct macvtap_queue *q = file->private_data;
  517. unsigned int mask = POLLERR;
  518. if (!q)
  519. goto out;
  520. mask = 0;
  521. poll_wait(file, &q->wq.wait, wait);
  522. if (!skb_array_empty(&q->skb_array))
  523. mask |= POLLIN | POLLRDNORM;
  524. if (sock_writeable(&q->sk) ||
  525. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
  526. sock_writeable(&q->sk)))
  527. mask |= POLLOUT | POLLWRNORM;
  528. out:
  529. return mask;
  530. }
  531. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  532. size_t len, size_t linear,
  533. int noblock, int *err)
  534. {
  535. struct sk_buff *skb;
  536. /* Under a page? Don't bother with paged skb. */
  537. if (prepad + len < PAGE_SIZE || !linear)
  538. linear = len;
  539. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  540. err, 0);
  541. if (!skb)
  542. return NULL;
  543. skb_reserve(skb, prepad);
  544. skb_put(skb, linear);
  545. skb->data_len = len - linear;
  546. skb->len += len - linear;
  547. return skb;
  548. }
  549. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  550. #define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  551. /* Get packet from user space buffer */
  552. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  553. struct iov_iter *from, int noblock)
  554. {
  555. int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE);
  556. struct sk_buff *skb;
  557. struct macvlan_dev *vlan;
  558. unsigned long total_len = iov_iter_count(from);
  559. unsigned long len = total_len;
  560. int err;
  561. struct virtio_net_hdr vnet_hdr = { 0 };
  562. int vnet_hdr_len = 0;
  563. int copylen = 0;
  564. int depth;
  565. bool zerocopy = false;
  566. size_t linear;
  567. ssize_t n;
  568. if (q->flags & IFF_VNET_HDR) {
  569. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  570. err = -EINVAL;
  571. if (len < vnet_hdr_len)
  572. goto err;
  573. len -= vnet_hdr_len;
  574. err = -EFAULT;
  575. n = copy_from_iter(&vnet_hdr, sizeof(vnet_hdr), from);
  576. if (n != sizeof(vnet_hdr))
  577. goto err;
  578. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  579. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  580. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  581. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  582. macvtap16_to_cpu(q, vnet_hdr.hdr_len))
  583. vnet_hdr.hdr_len = cpu_to_macvtap16(q,
  584. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  585. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  586. err = -EINVAL;
  587. if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  588. goto err;
  589. }
  590. err = -EINVAL;
  591. if (unlikely(len < ETH_HLEN))
  592. goto err;
  593. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  594. struct iov_iter i;
  595. copylen = vnet_hdr.hdr_len ?
  596. macvtap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  597. if (copylen > good_linear)
  598. copylen = good_linear;
  599. else if (copylen < ETH_HLEN)
  600. copylen = ETH_HLEN;
  601. linear = copylen;
  602. i = *from;
  603. iov_iter_advance(&i, copylen);
  604. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  605. zerocopy = true;
  606. }
  607. if (!zerocopy) {
  608. copylen = len;
  609. linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len);
  610. if (linear > good_linear)
  611. linear = good_linear;
  612. else if (linear < ETH_HLEN)
  613. linear = ETH_HLEN;
  614. }
  615. skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
  616. linear, noblock, &err);
  617. if (!skb)
  618. goto err;
  619. if (zerocopy)
  620. err = zerocopy_sg_from_iter(skb, from);
  621. else
  622. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  623. if (err)
  624. goto err_kfree;
  625. skb_set_network_header(skb, ETH_HLEN);
  626. skb_reset_mac_header(skb);
  627. skb->protocol = eth_hdr(skb)->h_proto;
  628. if (vnet_hdr_len) {
  629. err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
  630. macvtap_is_little_endian(q));
  631. if (err)
  632. goto err_kfree;
  633. }
  634. skb_probe_transport_header(skb, ETH_HLEN);
  635. /* Move network header to the right position for VLAN tagged packets */
  636. if ((skb->protocol == htons(ETH_P_8021Q) ||
  637. skb->protocol == htons(ETH_P_8021AD)) &&
  638. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  639. skb_set_network_header(skb, depth);
  640. rcu_read_lock();
  641. vlan = rcu_dereference(q->vlan);
  642. /* copy skb_ubuf_info for callback when skb has no error */
  643. if (zerocopy) {
  644. skb_shinfo(skb)->destructor_arg = m->msg_control;
  645. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  646. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  647. } else if (m && m->msg_control) {
  648. struct ubuf_info *uarg = m->msg_control;
  649. uarg->callback(uarg, false);
  650. }
  651. if (vlan) {
  652. skb->dev = vlan->dev;
  653. dev_queue_xmit(skb);
  654. } else {
  655. kfree_skb(skb);
  656. }
  657. rcu_read_unlock();
  658. return total_len;
  659. err_kfree:
  660. kfree_skb(skb);
  661. err:
  662. rcu_read_lock();
  663. vlan = rcu_dereference(q->vlan);
  664. if (vlan)
  665. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  666. rcu_read_unlock();
  667. return err;
  668. }
  669. static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  670. {
  671. struct file *file = iocb->ki_filp;
  672. struct macvtap_queue *q = file->private_data;
  673. return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  674. }
  675. /* Put packet to the user space buffer */
  676. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  677. const struct sk_buff *skb,
  678. struct iov_iter *iter)
  679. {
  680. int ret;
  681. int vnet_hdr_len = 0;
  682. int vlan_offset = 0;
  683. int total;
  684. if (q->flags & IFF_VNET_HDR) {
  685. struct virtio_net_hdr vnet_hdr;
  686. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  687. if (iov_iter_count(iter) < vnet_hdr_len)
  688. return -EINVAL;
  689. ret = virtio_net_hdr_from_skb(skb, &vnet_hdr,
  690. macvtap_is_little_endian(q), true);
  691. if (ret)
  692. BUG();
  693. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  694. sizeof(vnet_hdr))
  695. return -EFAULT;
  696. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  697. }
  698. total = vnet_hdr_len;
  699. total += skb->len;
  700. if (skb_vlan_tag_present(skb)) {
  701. struct {
  702. __be16 h_vlan_proto;
  703. __be16 h_vlan_TCI;
  704. } veth;
  705. veth.h_vlan_proto = skb->vlan_proto;
  706. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  707. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  708. total += VLAN_HLEN;
  709. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  710. if (ret || !iov_iter_count(iter))
  711. goto done;
  712. ret = copy_to_iter(&veth, sizeof(veth), iter);
  713. if (ret != sizeof(veth) || !iov_iter_count(iter))
  714. goto done;
  715. }
  716. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  717. skb->len - vlan_offset);
  718. done:
  719. return ret ? ret : total;
  720. }
  721. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  722. struct iov_iter *to,
  723. int noblock)
  724. {
  725. DEFINE_WAIT(wait);
  726. struct sk_buff *skb;
  727. ssize_t ret = 0;
  728. if (!iov_iter_count(to))
  729. return 0;
  730. while (1) {
  731. if (!noblock)
  732. prepare_to_wait(sk_sleep(&q->sk), &wait,
  733. TASK_INTERRUPTIBLE);
  734. /* Read frames from the queue */
  735. skb = skb_array_consume(&q->skb_array);
  736. if (skb)
  737. break;
  738. if (noblock) {
  739. ret = -EAGAIN;
  740. break;
  741. }
  742. if (signal_pending(current)) {
  743. ret = -ERESTARTSYS;
  744. break;
  745. }
  746. /* Nothing to read, let's sleep */
  747. schedule();
  748. }
  749. if (!noblock)
  750. finish_wait(sk_sleep(&q->sk), &wait);
  751. if (skb) {
  752. ret = macvtap_put_user(q, skb, to);
  753. if (unlikely(ret < 0))
  754. kfree_skb(skb);
  755. else
  756. consume_skb(skb);
  757. }
  758. return ret;
  759. }
  760. static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  761. {
  762. struct file *file = iocb->ki_filp;
  763. struct macvtap_queue *q = file->private_data;
  764. ssize_t len = iov_iter_count(to), ret;
  765. ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK);
  766. ret = min_t(ssize_t, ret, len);
  767. if (ret > 0)
  768. iocb->ki_pos = ret;
  769. return ret;
  770. }
  771. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  772. {
  773. struct macvlan_dev *vlan;
  774. ASSERT_RTNL();
  775. vlan = rtnl_dereference(q->vlan);
  776. if (vlan)
  777. dev_hold(vlan->dev);
  778. return vlan;
  779. }
  780. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  781. {
  782. dev_put(vlan->dev);
  783. }
  784. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  785. {
  786. struct macvtap_queue *q = file->private_data;
  787. struct macvlan_dev *vlan;
  788. int ret;
  789. vlan = macvtap_get_vlan(q);
  790. if (!vlan)
  791. return -EINVAL;
  792. if (flags & IFF_ATTACH_QUEUE)
  793. ret = macvtap_enable_queue(vlan->dev, file, q);
  794. else if (flags & IFF_DETACH_QUEUE)
  795. ret = macvtap_disable_queue(q);
  796. else
  797. ret = -EINVAL;
  798. macvtap_put_vlan(vlan);
  799. return ret;
  800. }
  801. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  802. {
  803. struct macvlan_dev *vlan;
  804. netdev_features_t features;
  805. netdev_features_t feature_mask = 0;
  806. vlan = rtnl_dereference(q->vlan);
  807. if (!vlan)
  808. return -ENOLINK;
  809. features = vlan->dev->features;
  810. if (arg & TUN_F_CSUM) {
  811. feature_mask = NETIF_F_HW_CSUM;
  812. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  813. if (arg & TUN_F_TSO_ECN)
  814. feature_mask |= NETIF_F_TSO_ECN;
  815. if (arg & TUN_F_TSO4)
  816. feature_mask |= NETIF_F_TSO;
  817. if (arg & TUN_F_TSO6)
  818. feature_mask |= NETIF_F_TSO6;
  819. }
  820. if (arg & TUN_F_UFO)
  821. feature_mask |= NETIF_F_UFO;
  822. }
  823. /* tun/tap driver inverts the usage for TSO offloads, where
  824. * setting the TSO bit means that the userspace wants to
  825. * accept TSO frames and turning it off means that user space
  826. * does not support TSO.
  827. * For macvtap, we have to invert it to mean the same thing.
  828. * When user space turns off TSO, we turn off GSO/LRO so that
  829. * user-space will not receive TSO frames.
  830. */
  831. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  832. features |= RX_OFFLOADS;
  833. else
  834. features &= ~RX_OFFLOADS;
  835. /* tap_features are the same as features on tun/tap and
  836. * reflect user expectations.
  837. */
  838. vlan->tap_features = feature_mask;
  839. vlan->set_features = features;
  840. netdev_update_features(vlan->dev);
  841. return 0;
  842. }
  843. /*
  844. * provide compatibility with generic tun/tap interface
  845. */
  846. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  847. unsigned long arg)
  848. {
  849. struct macvtap_queue *q = file->private_data;
  850. struct macvlan_dev *vlan;
  851. void __user *argp = (void __user *)arg;
  852. struct ifreq __user *ifr = argp;
  853. unsigned int __user *up = argp;
  854. unsigned short u;
  855. int __user *sp = argp;
  856. struct sockaddr sa;
  857. int s;
  858. int ret;
  859. switch (cmd) {
  860. case TUNSETIFF:
  861. /* ignore the name, just look at flags */
  862. if (get_user(u, &ifr->ifr_flags))
  863. return -EFAULT;
  864. ret = 0;
  865. if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP))
  866. ret = -EINVAL;
  867. else
  868. q->flags = (q->flags & ~MACVTAP_FEATURES) | u;
  869. return ret;
  870. case TUNGETIFF:
  871. rtnl_lock();
  872. vlan = macvtap_get_vlan(q);
  873. if (!vlan) {
  874. rtnl_unlock();
  875. return -ENOLINK;
  876. }
  877. ret = 0;
  878. u = q->flags;
  879. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  880. put_user(u, &ifr->ifr_flags))
  881. ret = -EFAULT;
  882. macvtap_put_vlan(vlan);
  883. rtnl_unlock();
  884. return ret;
  885. case TUNSETQUEUE:
  886. if (get_user(u, &ifr->ifr_flags))
  887. return -EFAULT;
  888. rtnl_lock();
  889. ret = macvtap_ioctl_set_queue(file, u);
  890. rtnl_unlock();
  891. return ret;
  892. case TUNGETFEATURES:
  893. if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up))
  894. return -EFAULT;
  895. return 0;
  896. case TUNSETSNDBUF:
  897. if (get_user(s, sp))
  898. return -EFAULT;
  899. if (s <= 0)
  900. return -EINVAL;
  901. q->sk.sk_sndbuf = s;
  902. return 0;
  903. case TUNGETVNETHDRSZ:
  904. s = q->vnet_hdr_sz;
  905. if (put_user(s, sp))
  906. return -EFAULT;
  907. return 0;
  908. case TUNSETVNETHDRSZ:
  909. if (get_user(s, sp))
  910. return -EFAULT;
  911. if (s < (int)sizeof(struct virtio_net_hdr))
  912. return -EINVAL;
  913. q->vnet_hdr_sz = s;
  914. return 0;
  915. case TUNGETVNETLE:
  916. s = !!(q->flags & MACVTAP_VNET_LE);
  917. if (put_user(s, sp))
  918. return -EFAULT;
  919. return 0;
  920. case TUNSETVNETLE:
  921. if (get_user(s, sp))
  922. return -EFAULT;
  923. if (s)
  924. q->flags |= MACVTAP_VNET_LE;
  925. else
  926. q->flags &= ~MACVTAP_VNET_LE;
  927. return 0;
  928. case TUNGETVNETBE:
  929. return macvtap_get_vnet_be(q, sp);
  930. case TUNSETVNETBE:
  931. return macvtap_set_vnet_be(q, sp);
  932. case TUNSETOFFLOAD:
  933. /* let the user check for future flags */
  934. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  935. TUN_F_TSO_ECN | TUN_F_UFO))
  936. return -EINVAL;
  937. rtnl_lock();
  938. ret = set_offload(q, arg);
  939. rtnl_unlock();
  940. return ret;
  941. case SIOCGIFHWADDR:
  942. rtnl_lock();
  943. vlan = macvtap_get_vlan(q);
  944. if (!vlan) {
  945. rtnl_unlock();
  946. return -ENOLINK;
  947. }
  948. ret = 0;
  949. u = vlan->dev->type;
  950. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  951. copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) ||
  952. put_user(u, &ifr->ifr_hwaddr.sa_family))
  953. ret = -EFAULT;
  954. macvtap_put_vlan(vlan);
  955. rtnl_unlock();
  956. return ret;
  957. case SIOCSIFHWADDR:
  958. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  959. return -EFAULT;
  960. rtnl_lock();
  961. vlan = macvtap_get_vlan(q);
  962. if (!vlan) {
  963. rtnl_unlock();
  964. return -ENOLINK;
  965. }
  966. ret = dev_set_mac_address(vlan->dev, &sa);
  967. macvtap_put_vlan(vlan);
  968. rtnl_unlock();
  969. return ret;
  970. default:
  971. return -EINVAL;
  972. }
  973. }
  974. #ifdef CONFIG_COMPAT
  975. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  976. unsigned long arg)
  977. {
  978. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  979. }
  980. #endif
  981. static const struct file_operations macvtap_fops = {
  982. .owner = THIS_MODULE,
  983. .open = macvtap_open,
  984. .release = macvtap_release,
  985. .read_iter = macvtap_read_iter,
  986. .write_iter = macvtap_write_iter,
  987. .poll = macvtap_poll,
  988. .llseek = no_llseek,
  989. .unlocked_ioctl = macvtap_ioctl,
  990. #ifdef CONFIG_COMPAT
  991. .compat_ioctl = macvtap_compat_ioctl,
  992. #endif
  993. };
  994. static int macvtap_sendmsg(struct socket *sock, struct msghdr *m,
  995. size_t total_len)
  996. {
  997. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  998. return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  999. }
  1000. static int macvtap_recvmsg(struct socket *sock, struct msghdr *m,
  1001. size_t total_len, int flags)
  1002. {
  1003. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1004. int ret;
  1005. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1006. return -EINVAL;
  1007. ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT);
  1008. if (ret > total_len) {
  1009. m->msg_flags |= MSG_TRUNC;
  1010. ret = flags & MSG_TRUNC ? ret : total_len;
  1011. }
  1012. return ret;
  1013. }
  1014. static int macvtap_peek_len(struct socket *sock)
  1015. {
  1016. struct macvtap_queue *q = container_of(sock, struct macvtap_queue,
  1017. sock);
  1018. return skb_array_peek_len(&q->skb_array);
  1019. }
  1020. /* Ops structure to mimic raw sockets with tun */
  1021. static const struct proto_ops macvtap_socket_ops = {
  1022. .sendmsg = macvtap_sendmsg,
  1023. .recvmsg = macvtap_recvmsg,
  1024. .peek_len = macvtap_peek_len,
  1025. };
  1026. /* Get an underlying socket object from tun file. Returns error unless file is
  1027. * attached to a device. The returned object works like a packet socket, it
  1028. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1029. * holding a reference to the file for as long as the socket is in use. */
  1030. struct socket *macvtap_get_socket(struct file *file)
  1031. {
  1032. struct macvtap_queue *q;
  1033. if (file->f_op != &macvtap_fops)
  1034. return ERR_PTR(-EINVAL);
  1035. q = file->private_data;
  1036. if (!q)
  1037. return ERR_PTR(-EBADFD);
  1038. return &q->sock;
  1039. }
  1040. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  1041. static int macvtap_queue_resize(struct macvlan_dev *vlan)
  1042. {
  1043. struct net_device *dev = vlan->dev;
  1044. struct macvtap_queue *q;
  1045. struct skb_array **arrays;
  1046. int n = vlan->numqueues;
  1047. int ret, i = 0;
  1048. arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL);
  1049. if (!arrays)
  1050. return -ENOMEM;
  1051. list_for_each_entry(q, &vlan->queue_list, next)
  1052. arrays[i++] = &q->skb_array;
  1053. ret = skb_array_resize_multiple(arrays, n,
  1054. dev->tx_queue_len, GFP_KERNEL);
  1055. kfree(arrays);
  1056. return ret;
  1057. }
  1058. static int macvtap_device_event(struct notifier_block *unused,
  1059. unsigned long event, void *ptr)
  1060. {
  1061. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1062. struct macvlan_dev *vlan;
  1063. struct device *classdev;
  1064. dev_t devt;
  1065. int err;
  1066. char tap_name[IFNAMSIZ];
  1067. if (dev->rtnl_link_ops != &macvtap_link_ops)
  1068. return NOTIFY_DONE;
  1069. snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
  1070. vlan = netdev_priv(dev);
  1071. switch (event) {
  1072. case NETDEV_REGISTER:
  1073. /* Create the device node here after the network device has
  1074. * been registered but before register_netdevice has
  1075. * finished running.
  1076. */
  1077. err = macvtap_get_minor(vlan);
  1078. if (err)
  1079. return notifier_from_errno(err);
  1080. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1081. classdev = device_create(&macvtap_class, &dev->dev, devt,
  1082. dev, tap_name);
  1083. if (IS_ERR(classdev)) {
  1084. macvtap_free_minor(vlan);
  1085. return notifier_from_errno(PTR_ERR(classdev));
  1086. }
  1087. err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
  1088. tap_name);
  1089. if (err)
  1090. return notifier_from_errno(err);
  1091. break;
  1092. case NETDEV_UNREGISTER:
  1093. /* vlan->minor == 0 if NETDEV_REGISTER above failed */
  1094. if (vlan->minor == 0)
  1095. break;
  1096. sysfs_remove_link(&dev->dev.kobj, tap_name);
  1097. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1098. device_destroy(&macvtap_class, devt);
  1099. macvtap_free_minor(vlan);
  1100. break;
  1101. case NETDEV_CHANGE_TX_QUEUE_LEN:
  1102. if (macvtap_queue_resize(vlan))
  1103. return NOTIFY_BAD;
  1104. break;
  1105. }
  1106. return NOTIFY_DONE;
  1107. }
  1108. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1109. .notifier_call = macvtap_device_event,
  1110. };
  1111. static int macvtap_init(void)
  1112. {
  1113. int err;
  1114. err = alloc_chrdev_region(&macvtap_major, 0,
  1115. MACVTAP_NUM_DEVS, "macvtap");
  1116. if (err)
  1117. goto out1;
  1118. cdev_init(&macvtap_cdev, &macvtap_fops);
  1119. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1120. if (err)
  1121. goto out2;
  1122. err = class_register(&macvtap_class);
  1123. if (err)
  1124. goto out3;
  1125. err = register_netdevice_notifier(&macvtap_notifier_block);
  1126. if (err)
  1127. goto out4;
  1128. err = macvlan_link_register(&macvtap_link_ops);
  1129. if (err)
  1130. goto out5;
  1131. return 0;
  1132. out5:
  1133. unregister_netdevice_notifier(&macvtap_notifier_block);
  1134. out4:
  1135. class_unregister(&macvtap_class);
  1136. out3:
  1137. cdev_del(&macvtap_cdev);
  1138. out2:
  1139. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1140. out1:
  1141. return err;
  1142. }
  1143. module_init(macvtap_init);
  1144. static void macvtap_exit(void)
  1145. {
  1146. rtnl_link_unregister(&macvtap_link_ops);
  1147. unregister_netdevice_notifier(&macvtap_notifier_block);
  1148. class_unregister(&macvtap_class);
  1149. cdev_del(&macvtap_cdev);
  1150. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1151. idr_destroy(&minor_idr);
  1152. }
  1153. module_exit(macvtap_exit);
  1154. MODULE_ALIAS_RTNL_LINK("macvtap");
  1155. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1156. MODULE_LICENSE("GPL");