macvtap.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. netdev_err(vlan->dev, "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. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  469. init_waitqueue_head(&q->wq.wait);
  470. q->sock.type = SOCK_RAW;
  471. q->sock.state = SS_CONNECTED;
  472. q->sock.file = file;
  473. q->sock.ops = &macvtap_socket_ops;
  474. sock_init_data(&q->sock, &q->sk);
  475. q->sk.sk_write_space = macvtap_sock_write_space;
  476. q->sk.sk_destruct = macvtap_sock_destruct;
  477. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  478. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  479. /*
  480. * so far only KVM virtio_net uses macvtap, enable zero copy between
  481. * guest kernel and host kernel when lower device supports zerocopy
  482. *
  483. * The macvlan supports zerocopy iff the lower device supports zero
  484. * copy so we don't have to look at the lower device directly.
  485. */
  486. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  487. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  488. err = -ENOMEM;
  489. if (skb_array_init(&q->skb_array, dev->tx_queue_len, GFP_KERNEL))
  490. goto err_array;
  491. err = macvtap_set_queue(dev, file, q);
  492. if (err)
  493. goto err_queue;
  494. dev_put(dev);
  495. rtnl_unlock();
  496. return err;
  497. err_queue:
  498. skb_array_cleanup(&q->skb_array);
  499. err_array:
  500. sock_put(&q->sk);
  501. err:
  502. if (dev)
  503. dev_put(dev);
  504. rtnl_unlock();
  505. return err;
  506. }
  507. static int macvtap_release(struct inode *inode, struct file *file)
  508. {
  509. struct macvtap_queue *q = file->private_data;
  510. macvtap_put_queue(q);
  511. return 0;
  512. }
  513. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  514. {
  515. struct macvtap_queue *q = file->private_data;
  516. unsigned int mask = POLLERR;
  517. if (!q)
  518. goto out;
  519. mask = 0;
  520. poll_wait(file, &q->wq.wait, wait);
  521. if (!skb_array_empty(&q->skb_array))
  522. mask |= POLLIN | POLLRDNORM;
  523. if (sock_writeable(&q->sk) ||
  524. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
  525. sock_writeable(&q->sk)))
  526. mask |= POLLOUT | POLLWRNORM;
  527. out:
  528. return mask;
  529. }
  530. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  531. size_t len, size_t linear,
  532. int noblock, int *err)
  533. {
  534. struct sk_buff *skb;
  535. /* Under a page? Don't bother with paged skb. */
  536. if (prepad + len < PAGE_SIZE || !linear)
  537. linear = len;
  538. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  539. err, 0);
  540. if (!skb)
  541. return NULL;
  542. skb_reserve(skb, prepad);
  543. skb_put(skb, linear);
  544. skb->data_len = len - linear;
  545. skb->len += len - linear;
  546. return skb;
  547. }
  548. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  549. #define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  550. /* Get packet from user space buffer */
  551. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  552. struct iov_iter *from, int noblock)
  553. {
  554. int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE);
  555. struct sk_buff *skb;
  556. struct macvlan_dev *vlan;
  557. unsigned long total_len = iov_iter_count(from);
  558. unsigned long len = total_len;
  559. int err;
  560. struct virtio_net_hdr vnet_hdr = { 0 };
  561. int vnet_hdr_len = 0;
  562. int copylen = 0;
  563. int depth;
  564. bool zerocopy = false;
  565. size_t linear;
  566. if (q->flags & IFF_VNET_HDR) {
  567. vnet_hdr_len = q->vnet_hdr_sz;
  568. err = -EINVAL;
  569. if (len < vnet_hdr_len)
  570. goto err;
  571. len -= vnet_hdr_len;
  572. err = -EFAULT;
  573. if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
  574. goto err;
  575. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  576. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  577. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  578. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  579. macvtap16_to_cpu(q, vnet_hdr.hdr_len))
  580. vnet_hdr.hdr_len = cpu_to_macvtap16(q,
  581. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  582. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  583. err = -EINVAL;
  584. if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  585. goto err;
  586. }
  587. err = -EINVAL;
  588. if (unlikely(len < ETH_HLEN))
  589. goto err;
  590. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  591. struct iov_iter i;
  592. copylen = vnet_hdr.hdr_len ?
  593. macvtap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  594. if (copylen > good_linear)
  595. copylen = good_linear;
  596. else if (copylen < ETH_HLEN)
  597. copylen = ETH_HLEN;
  598. linear = copylen;
  599. i = *from;
  600. iov_iter_advance(&i, copylen);
  601. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  602. zerocopy = true;
  603. }
  604. if (!zerocopy) {
  605. copylen = len;
  606. linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len);
  607. if (linear > good_linear)
  608. linear = good_linear;
  609. else if (linear < ETH_HLEN)
  610. linear = ETH_HLEN;
  611. }
  612. skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
  613. linear, noblock, &err);
  614. if (!skb)
  615. goto err;
  616. if (zerocopy)
  617. err = zerocopy_sg_from_iter(skb, from);
  618. else
  619. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  620. if (err)
  621. goto err_kfree;
  622. skb_set_network_header(skb, ETH_HLEN);
  623. skb_reset_mac_header(skb);
  624. skb->protocol = eth_hdr(skb)->h_proto;
  625. if (vnet_hdr_len) {
  626. err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
  627. macvtap_is_little_endian(q));
  628. if (err)
  629. goto err_kfree;
  630. }
  631. skb_probe_transport_header(skb, ETH_HLEN);
  632. /* Move network header to the right position for VLAN tagged packets */
  633. if ((skb->protocol == htons(ETH_P_8021Q) ||
  634. skb->protocol == htons(ETH_P_8021AD)) &&
  635. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  636. skb_set_network_header(skb, depth);
  637. rcu_read_lock();
  638. vlan = rcu_dereference(q->vlan);
  639. /* copy skb_ubuf_info for callback when skb has no error */
  640. if (zerocopy) {
  641. skb_shinfo(skb)->destructor_arg = m->msg_control;
  642. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  643. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  644. } else if (m && m->msg_control) {
  645. struct ubuf_info *uarg = m->msg_control;
  646. uarg->callback(uarg, false);
  647. }
  648. if (vlan) {
  649. skb->dev = vlan->dev;
  650. dev_queue_xmit(skb);
  651. } else {
  652. kfree_skb(skb);
  653. }
  654. rcu_read_unlock();
  655. return total_len;
  656. err_kfree:
  657. kfree_skb(skb);
  658. err:
  659. rcu_read_lock();
  660. vlan = rcu_dereference(q->vlan);
  661. if (vlan)
  662. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  663. rcu_read_unlock();
  664. return err;
  665. }
  666. static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  667. {
  668. struct file *file = iocb->ki_filp;
  669. struct macvtap_queue *q = file->private_data;
  670. return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  671. }
  672. /* Put packet to the user space buffer */
  673. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  674. const struct sk_buff *skb,
  675. struct iov_iter *iter)
  676. {
  677. int ret;
  678. int vnet_hdr_len = 0;
  679. int vlan_offset = 0;
  680. int total;
  681. if (q->flags & IFF_VNET_HDR) {
  682. struct virtio_net_hdr vnet_hdr;
  683. vnet_hdr_len = q->vnet_hdr_sz;
  684. if (iov_iter_count(iter) < vnet_hdr_len)
  685. return -EINVAL;
  686. if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
  687. macvtap_is_little_endian(q)))
  688. BUG();
  689. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  690. sizeof(vnet_hdr))
  691. return -EFAULT;
  692. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  693. }
  694. total = vnet_hdr_len;
  695. total += skb->len;
  696. if (skb_vlan_tag_present(skb)) {
  697. struct {
  698. __be16 h_vlan_proto;
  699. __be16 h_vlan_TCI;
  700. } veth;
  701. veth.h_vlan_proto = skb->vlan_proto;
  702. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  703. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  704. total += VLAN_HLEN;
  705. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  706. if (ret || !iov_iter_count(iter))
  707. goto done;
  708. ret = copy_to_iter(&veth, sizeof(veth), iter);
  709. if (ret != sizeof(veth) || !iov_iter_count(iter))
  710. goto done;
  711. }
  712. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  713. skb->len - vlan_offset);
  714. done:
  715. return ret ? ret : total;
  716. }
  717. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  718. struct iov_iter *to,
  719. int noblock)
  720. {
  721. DEFINE_WAIT(wait);
  722. struct sk_buff *skb;
  723. ssize_t ret = 0;
  724. if (!iov_iter_count(to))
  725. return 0;
  726. while (1) {
  727. if (!noblock)
  728. prepare_to_wait(sk_sleep(&q->sk), &wait,
  729. TASK_INTERRUPTIBLE);
  730. /* Read frames from the queue */
  731. skb = skb_array_consume(&q->skb_array);
  732. if (skb)
  733. break;
  734. if (noblock) {
  735. ret = -EAGAIN;
  736. break;
  737. }
  738. if (signal_pending(current)) {
  739. ret = -ERESTARTSYS;
  740. break;
  741. }
  742. /* Nothing to read, let's sleep */
  743. schedule();
  744. }
  745. if (!noblock)
  746. finish_wait(sk_sleep(&q->sk), &wait);
  747. if (skb) {
  748. ret = macvtap_put_user(q, skb, to);
  749. if (unlikely(ret < 0))
  750. kfree_skb(skb);
  751. else
  752. consume_skb(skb);
  753. }
  754. return ret;
  755. }
  756. static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  757. {
  758. struct file *file = iocb->ki_filp;
  759. struct macvtap_queue *q = file->private_data;
  760. ssize_t len = iov_iter_count(to), ret;
  761. ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK);
  762. ret = min_t(ssize_t, ret, len);
  763. if (ret > 0)
  764. iocb->ki_pos = ret;
  765. return ret;
  766. }
  767. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  768. {
  769. struct macvlan_dev *vlan;
  770. ASSERT_RTNL();
  771. vlan = rtnl_dereference(q->vlan);
  772. if (vlan)
  773. dev_hold(vlan->dev);
  774. return vlan;
  775. }
  776. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  777. {
  778. dev_put(vlan->dev);
  779. }
  780. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  781. {
  782. struct macvtap_queue *q = file->private_data;
  783. struct macvlan_dev *vlan;
  784. int ret;
  785. vlan = macvtap_get_vlan(q);
  786. if (!vlan)
  787. return -EINVAL;
  788. if (flags & IFF_ATTACH_QUEUE)
  789. ret = macvtap_enable_queue(vlan->dev, file, q);
  790. else if (flags & IFF_DETACH_QUEUE)
  791. ret = macvtap_disable_queue(q);
  792. else
  793. ret = -EINVAL;
  794. macvtap_put_vlan(vlan);
  795. return ret;
  796. }
  797. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  798. {
  799. struct macvlan_dev *vlan;
  800. netdev_features_t features;
  801. netdev_features_t feature_mask = 0;
  802. vlan = rtnl_dereference(q->vlan);
  803. if (!vlan)
  804. return -ENOLINK;
  805. features = vlan->dev->features;
  806. if (arg & TUN_F_CSUM) {
  807. feature_mask = NETIF_F_HW_CSUM;
  808. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  809. if (arg & TUN_F_TSO_ECN)
  810. feature_mask |= NETIF_F_TSO_ECN;
  811. if (arg & TUN_F_TSO4)
  812. feature_mask |= NETIF_F_TSO;
  813. if (arg & TUN_F_TSO6)
  814. feature_mask |= NETIF_F_TSO6;
  815. }
  816. if (arg & TUN_F_UFO)
  817. feature_mask |= NETIF_F_UFO;
  818. }
  819. /* tun/tap driver inverts the usage for TSO offloads, where
  820. * setting the TSO bit means that the userspace wants to
  821. * accept TSO frames and turning it off means that user space
  822. * does not support TSO.
  823. * For macvtap, we have to invert it to mean the same thing.
  824. * When user space turns off TSO, we turn off GSO/LRO so that
  825. * user-space will not receive TSO frames.
  826. */
  827. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  828. features |= RX_OFFLOADS;
  829. else
  830. features &= ~RX_OFFLOADS;
  831. /* tap_features are the same as features on tun/tap and
  832. * reflect user expectations.
  833. */
  834. vlan->tap_features = feature_mask;
  835. vlan->set_features = features;
  836. netdev_update_features(vlan->dev);
  837. return 0;
  838. }
  839. /*
  840. * provide compatibility with generic tun/tap interface
  841. */
  842. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  843. unsigned long arg)
  844. {
  845. struct macvtap_queue *q = file->private_data;
  846. struct macvlan_dev *vlan;
  847. void __user *argp = (void __user *)arg;
  848. struct ifreq __user *ifr = argp;
  849. unsigned int __user *up = argp;
  850. unsigned short u;
  851. int __user *sp = argp;
  852. struct sockaddr sa;
  853. int s;
  854. int ret;
  855. switch (cmd) {
  856. case TUNSETIFF:
  857. /* ignore the name, just look at flags */
  858. if (get_user(u, &ifr->ifr_flags))
  859. return -EFAULT;
  860. ret = 0;
  861. if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP))
  862. ret = -EINVAL;
  863. else
  864. q->flags = (q->flags & ~MACVTAP_FEATURES) | u;
  865. return ret;
  866. case TUNGETIFF:
  867. rtnl_lock();
  868. vlan = macvtap_get_vlan(q);
  869. if (!vlan) {
  870. rtnl_unlock();
  871. return -ENOLINK;
  872. }
  873. ret = 0;
  874. u = q->flags;
  875. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  876. put_user(u, &ifr->ifr_flags))
  877. ret = -EFAULT;
  878. macvtap_put_vlan(vlan);
  879. rtnl_unlock();
  880. return ret;
  881. case TUNSETQUEUE:
  882. if (get_user(u, &ifr->ifr_flags))
  883. return -EFAULT;
  884. rtnl_lock();
  885. ret = macvtap_ioctl_set_queue(file, u);
  886. rtnl_unlock();
  887. return ret;
  888. case TUNGETFEATURES:
  889. if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up))
  890. return -EFAULT;
  891. return 0;
  892. case TUNSETSNDBUF:
  893. if (get_user(s, sp))
  894. return -EFAULT;
  895. q->sk.sk_sndbuf = s;
  896. return 0;
  897. case TUNGETVNETHDRSZ:
  898. s = q->vnet_hdr_sz;
  899. if (put_user(s, sp))
  900. return -EFAULT;
  901. return 0;
  902. case TUNSETVNETHDRSZ:
  903. if (get_user(s, sp))
  904. return -EFAULT;
  905. if (s < (int)sizeof(struct virtio_net_hdr))
  906. return -EINVAL;
  907. q->vnet_hdr_sz = s;
  908. return 0;
  909. case TUNGETVNETLE:
  910. s = !!(q->flags & MACVTAP_VNET_LE);
  911. if (put_user(s, sp))
  912. return -EFAULT;
  913. return 0;
  914. case TUNSETVNETLE:
  915. if (get_user(s, sp))
  916. return -EFAULT;
  917. if (s)
  918. q->flags |= MACVTAP_VNET_LE;
  919. else
  920. q->flags &= ~MACVTAP_VNET_LE;
  921. return 0;
  922. case TUNGETVNETBE:
  923. return macvtap_get_vnet_be(q, sp);
  924. case TUNSETVNETBE:
  925. return macvtap_set_vnet_be(q, sp);
  926. case TUNSETOFFLOAD:
  927. /* let the user check for future flags */
  928. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  929. TUN_F_TSO_ECN | TUN_F_UFO))
  930. return -EINVAL;
  931. rtnl_lock();
  932. ret = set_offload(q, arg);
  933. rtnl_unlock();
  934. return ret;
  935. case SIOCGIFHWADDR:
  936. rtnl_lock();
  937. vlan = macvtap_get_vlan(q);
  938. if (!vlan) {
  939. rtnl_unlock();
  940. return -ENOLINK;
  941. }
  942. ret = 0;
  943. u = vlan->dev->type;
  944. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  945. copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) ||
  946. put_user(u, &ifr->ifr_hwaddr.sa_family))
  947. ret = -EFAULT;
  948. macvtap_put_vlan(vlan);
  949. rtnl_unlock();
  950. return ret;
  951. case SIOCSIFHWADDR:
  952. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  953. return -EFAULT;
  954. rtnl_lock();
  955. vlan = macvtap_get_vlan(q);
  956. if (!vlan) {
  957. rtnl_unlock();
  958. return -ENOLINK;
  959. }
  960. ret = dev_set_mac_address(vlan->dev, &sa);
  961. macvtap_put_vlan(vlan);
  962. rtnl_unlock();
  963. return ret;
  964. default:
  965. return -EINVAL;
  966. }
  967. }
  968. #ifdef CONFIG_COMPAT
  969. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  970. unsigned long arg)
  971. {
  972. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  973. }
  974. #endif
  975. static const struct file_operations macvtap_fops = {
  976. .owner = THIS_MODULE,
  977. .open = macvtap_open,
  978. .release = macvtap_release,
  979. .read_iter = macvtap_read_iter,
  980. .write_iter = macvtap_write_iter,
  981. .poll = macvtap_poll,
  982. .llseek = no_llseek,
  983. .unlocked_ioctl = macvtap_ioctl,
  984. #ifdef CONFIG_COMPAT
  985. .compat_ioctl = macvtap_compat_ioctl,
  986. #endif
  987. };
  988. static int macvtap_sendmsg(struct socket *sock, struct msghdr *m,
  989. size_t total_len)
  990. {
  991. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  992. return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  993. }
  994. static int macvtap_recvmsg(struct socket *sock, struct msghdr *m,
  995. size_t total_len, int flags)
  996. {
  997. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  998. int ret;
  999. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1000. return -EINVAL;
  1001. ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT);
  1002. if (ret > total_len) {
  1003. m->msg_flags |= MSG_TRUNC;
  1004. ret = flags & MSG_TRUNC ? ret : total_len;
  1005. }
  1006. return ret;
  1007. }
  1008. static int macvtap_peek_len(struct socket *sock)
  1009. {
  1010. struct macvtap_queue *q = container_of(sock, struct macvtap_queue,
  1011. sock);
  1012. return skb_array_peek_len(&q->skb_array);
  1013. }
  1014. /* Ops structure to mimic raw sockets with tun */
  1015. static const struct proto_ops macvtap_socket_ops = {
  1016. .sendmsg = macvtap_sendmsg,
  1017. .recvmsg = macvtap_recvmsg,
  1018. .peek_len = macvtap_peek_len,
  1019. };
  1020. /* Get an underlying socket object from tun file. Returns error unless file is
  1021. * attached to a device. The returned object works like a packet socket, it
  1022. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1023. * holding a reference to the file for as long as the socket is in use. */
  1024. struct socket *macvtap_get_socket(struct file *file)
  1025. {
  1026. struct macvtap_queue *q;
  1027. if (file->f_op != &macvtap_fops)
  1028. return ERR_PTR(-EINVAL);
  1029. q = file->private_data;
  1030. if (!q)
  1031. return ERR_PTR(-EBADFD);
  1032. return &q->sock;
  1033. }
  1034. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  1035. static int macvtap_queue_resize(struct macvlan_dev *vlan)
  1036. {
  1037. struct net_device *dev = vlan->dev;
  1038. struct macvtap_queue *q;
  1039. struct skb_array **arrays;
  1040. int n = vlan->numqueues;
  1041. int ret, i = 0;
  1042. arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL);
  1043. if (!arrays)
  1044. return -ENOMEM;
  1045. list_for_each_entry(q, &vlan->queue_list, next)
  1046. arrays[i++] = &q->skb_array;
  1047. ret = skb_array_resize_multiple(arrays, n,
  1048. dev->tx_queue_len, GFP_KERNEL);
  1049. kfree(arrays);
  1050. return ret;
  1051. }
  1052. static int macvtap_device_event(struct notifier_block *unused,
  1053. unsigned long event, void *ptr)
  1054. {
  1055. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1056. struct macvlan_dev *vlan;
  1057. struct device *classdev;
  1058. dev_t devt;
  1059. int err;
  1060. char tap_name[IFNAMSIZ];
  1061. if (dev->rtnl_link_ops != &macvtap_link_ops)
  1062. return NOTIFY_DONE;
  1063. snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
  1064. vlan = netdev_priv(dev);
  1065. switch (event) {
  1066. case NETDEV_REGISTER:
  1067. /* Create the device node here after the network device has
  1068. * been registered but before register_netdevice has
  1069. * finished running.
  1070. */
  1071. err = macvtap_get_minor(vlan);
  1072. if (err)
  1073. return notifier_from_errno(err);
  1074. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1075. classdev = device_create(&macvtap_class, &dev->dev, devt,
  1076. dev, tap_name);
  1077. if (IS_ERR(classdev)) {
  1078. macvtap_free_minor(vlan);
  1079. return notifier_from_errno(PTR_ERR(classdev));
  1080. }
  1081. err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
  1082. tap_name);
  1083. if (err)
  1084. return notifier_from_errno(err);
  1085. break;
  1086. case NETDEV_UNREGISTER:
  1087. /* vlan->minor == 0 if NETDEV_REGISTER above failed */
  1088. if (vlan->minor == 0)
  1089. break;
  1090. sysfs_remove_link(&dev->dev.kobj, tap_name);
  1091. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1092. device_destroy(&macvtap_class, devt);
  1093. macvtap_free_minor(vlan);
  1094. break;
  1095. case NETDEV_CHANGE_TX_QUEUE_LEN:
  1096. if (macvtap_queue_resize(vlan))
  1097. return NOTIFY_BAD;
  1098. break;
  1099. }
  1100. return NOTIFY_DONE;
  1101. }
  1102. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1103. .notifier_call = macvtap_device_event,
  1104. };
  1105. static int macvtap_init(void)
  1106. {
  1107. int err;
  1108. err = alloc_chrdev_region(&macvtap_major, 0,
  1109. MACVTAP_NUM_DEVS, "macvtap");
  1110. if (err)
  1111. goto out1;
  1112. cdev_init(&macvtap_cdev, &macvtap_fops);
  1113. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1114. if (err)
  1115. goto out2;
  1116. err = class_register(&macvtap_class);
  1117. if (err)
  1118. goto out3;
  1119. err = register_netdevice_notifier(&macvtap_notifier_block);
  1120. if (err)
  1121. goto out4;
  1122. err = macvlan_link_register(&macvtap_link_ops);
  1123. if (err)
  1124. goto out5;
  1125. return 0;
  1126. out5:
  1127. unregister_netdevice_notifier(&macvtap_notifier_block);
  1128. out4:
  1129. class_unregister(&macvtap_class);
  1130. out3:
  1131. cdev_del(&macvtap_cdev);
  1132. out2:
  1133. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1134. out1:
  1135. return err;
  1136. }
  1137. module_init(macvtap_init);
  1138. static void macvtap_exit(void)
  1139. {
  1140. rtnl_link_unregister(&macvtap_link_ops);
  1141. unregister_netdevice_notifier(&macvtap_notifier_block);
  1142. class_unregister(&macvtap_class);
  1143. cdev_del(&macvtap_cdev);
  1144. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1145. idr_destroy(&minor_idr);
  1146. }
  1147. module_exit(macvtap_exit);
  1148. MODULE_ALIAS_RTNL_LINK("macvtap");
  1149. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1150. MODULE_LICENSE("GPL");