macvtap.c 31 KB

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