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