macvtap.c 28 KB

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