macvtap.c 29 KB

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