macvtap.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/nsproxy.h>
  5. #include <linux/compat.h>
  6. #include <linux/if_tun.h>
  7. #include <linux/module.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/cache.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/wait.h>
  15. #include <linux/cdev.h>
  16. #include <linux/fs.h>
  17. #include <net/net_namespace.h>
  18. #include <net/rtnetlink.h>
  19. #include <net/sock.h>
  20. #include <linux/virtio_net.h>
  21. /*
  22. * A macvtap queue is the central object of this driver, it connects
  23. * an open character device to a macvlan interface. There can be
  24. * multiple queues on one interface, which map back to queues
  25. * implemented in hardware on the underlying device.
  26. *
  27. * macvtap_proto is used to allocate queues through the sock allocation
  28. * mechanism.
  29. *
  30. * TODO: multiqueue support is currently not implemented, even though
  31. * macvtap is basically prepared for that. We will need to add this
  32. * here as well as in virtio-net and qemu to get line rate on 10gbit
  33. * adapters from a guest.
  34. */
  35. struct macvtap_queue {
  36. struct sock sk;
  37. struct socket sock;
  38. struct socket_wq wq;
  39. int vnet_hdr_sz;
  40. struct macvlan_dev __rcu *vlan;
  41. struct file *file;
  42. unsigned int flags;
  43. };
  44. static struct proto macvtap_proto = {
  45. .name = "macvtap",
  46. .owner = THIS_MODULE,
  47. .obj_size = sizeof (struct macvtap_queue),
  48. };
  49. /*
  50. * Minor number matches netdev->ifindex, so need a potentially
  51. * large value. This also makes it possible to split the
  52. * tap functionality out again in the future by offering it
  53. * from other drivers besides macvtap. As long as every device
  54. * only has one tap, the interface numbers assure that the
  55. * device nodes are unique.
  56. */
  57. static dev_t macvtap_major;
  58. #define MACVTAP_NUM_DEVS 65536
  59. #define GOODCOPY_LEN 128
  60. static struct class *macvtap_class;
  61. static struct cdev macvtap_cdev;
  62. static const struct proto_ops macvtap_socket_ops;
  63. /*
  64. * RCU usage:
  65. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  66. * pointers from one to the other can only be read while rcu_read_lock
  67. * or macvtap_lock is held.
  68. *
  69. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  70. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  71. * q->vlan becomes inaccessible. When the files gets closed,
  72. * macvtap_get_queue() fails.
  73. *
  74. * There may still be references to the struct sock inside of the
  75. * queue from outbound SKBs, but these never reference back to the
  76. * file or the dev. The data structure is freed through __sk_free
  77. * when both our references and any pending SKBs are gone.
  78. */
  79. static DEFINE_SPINLOCK(macvtap_lock);
  80. /*
  81. * get_slot: return a [unused/occupied] slot in vlan->taps[]:
  82. * - if 'q' is NULL, return the first empty slot;
  83. * - otherwise, return the slot this pointer occupies.
  84. */
  85. static int get_slot(struct macvlan_dev *vlan, struct macvtap_queue *q)
  86. {
  87. int i;
  88. for (i = 0; i < MAX_MACVTAP_QUEUES; i++) {
  89. if (rcu_dereference(vlan->taps[i]) == 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_assign_pointer(vlan->taps[index], NULL);
  133. rcu_assign_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. if (likely(skb_rx_queue_recorded(skb))) {
  158. rxq = skb_get_rx_queue(skb);
  159. while (unlikely(rxq >= numvtaps))
  160. rxq -= numvtaps;
  161. tap = rcu_dereference(vlan->taps[rxq]);
  162. if (tap)
  163. goto out;
  164. }
  165. /* Check if we can use flow to select a queue */
  166. rxq = skb_get_rxhash(skb);
  167. if (rxq) {
  168. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  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_assign_pointer(vlan->taps[i], NULL);
  199. rcu_assign_pointer(q->vlan, NULL);
  200. vlan->numvtaps--;
  201. }
  202. }
  203. BUG_ON(vlan->numvtaps != 0);
  204. spin_unlock(&macvtap_lock);
  205. synchronize_rcu();
  206. for (--j; j >= 0; j--)
  207. sock_put(&qlist[j]->sk);
  208. }
  209. /*
  210. * Forward happens for data that gets sent from one macvlan
  211. * endpoint to another one in bridge mode. We just take
  212. * the skb and put it into the receive queue.
  213. */
  214. static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
  215. {
  216. struct macvtap_queue *q = macvtap_get_queue(dev, skb);
  217. if (!q)
  218. goto drop;
  219. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  220. goto drop;
  221. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  222. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  223. return NET_RX_SUCCESS;
  224. drop:
  225. kfree_skb(skb);
  226. return NET_RX_DROP;
  227. }
  228. /*
  229. * Receive is for data from the external interface (lowerdev),
  230. * in case of macvtap, we can treat that the same way as
  231. * forward, which macvlan cannot.
  232. */
  233. static int macvtap_receive(struct sk_buff *skb)
  234. {
  235. skb_push(skb, ETH_HLEN);
  236. return macvtap_forward(skb->dev, skb);
  237. }
  238. static int macvtap_newlink(struct net *src_net,
  239. struct net_device *dev,
  240. struct nlattr *tb[],
  241. struct nlattr *data[])
  242. {
  243. struct device *classdev;
  244. dev_t devt;
  245. int err;
  246. err = macvlan_common_newlink(src_net, dev, tb, data,
  247. macvtap_receive, macvtap_forward);
  248. if (err)
  249. goto out;
  250. devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
  251. classdev = device_create(macvtap_class, &dev->dev, devt,
  252. dev, "tap%d", dev->ifindex);
  253. if (IS_ERR(classdev)) {
  254. err = PTR_ERR(classdev);
  255. macvtap_del_queues(dev);
  256. }
  257. out:
  258. return err;
  259. }
  260. static void macvtap_dellink(struct net_device *dev,
  261. struct list_head *head)
  262. {
  263. device_destroy(macvtap_class,
  264. MKDEV(MAJOR(macvtap_major), dev->ifindex));
  265. macvtap_del_queues(dev);
  266. macvlan_dellink(dev, head);
  267. }
  268. static void macvtap_setup(struct net_device *dev)
  269. {
  270. macvlan_common_setup(dev);
  271. dev->tx_queue_len = TUN_READQ_SIZE;
  272. }
  273. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  274. .kind = "macvtap",
  275. .setup = macvtap_setup,
  276. .newlink = macvtap_newlink,
  277. .dellink = macvtap_dellink,
  278. };
  279. static void macvtap_sock_write_space(struct sock *sk)
  280. {
  281. wait_queue_head_t *wqueue;
  282. if (!sock_writeable(sk) ||
  283. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  284. return;
  285. wqueue = sk_sleep(sk);
  286. if (wqueue && waitqueue_active(wqueue))
  287. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  288. }
  289. static int macvtap_open(struct inode *inode, struct file *file)
  290. {
  291. struct net *net = current->nsproxy->net_ns;
  292. struct net_device *dev = dev_get_by_index(net, iminor(inode));
  293. struct macvlan_dev *vlan = netdev_priv(dev);
  294. struct macvtap_queue *q;
  295. int err;
  296. err = -ENODEV;
  297. if (!dev)
  298. goto out;
  299. /* check if this is a macvtap device */
  300. err = -EINVAL;
  301. if (dev->rtnl_link_ops != &macvtap_link_ops)
  302. goto out;
  303. err = -ENOMEM;
  304. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  305. &macvtap_proto);
  306. if (!q)
  307. goto out;
  308. q->sock.wq = &q->wq;
  309. init_waitqueue_head(&q->wq.wait);
  310. q->sock.type = SOCK_RAW;
  311. q->sock.state = SS_CONNECTED;
  312. q->sock.file = file;
  313. q->sock.ops = &macvtap_socket_ops;
  314. sock_init_data(&q->sock, &q->sk);
  315. q->sk.sk_write_space = macvtap_sock_write_space;
  316. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  317. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  318. /*
  319. * so far only KVM virtio_net uses macvtap, enable zero copy between
  320. * guest kernel and host kernel when lower device supports zerocopy
  321. */
  322. if (vlan) {
  323. if ((vlan->lowerdev->features & NETIF_F_HIGHDMA) &&
  324. (vlan->lowerdev->features & NETIF_F_SG))
  325. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  326. }
  327. err = macvtap_set_queue(dev, file, q);
  328. if (err)
  329. sock_put(&q->sk);
  330. out:
  331. if (dev)
  332. dev_put(dev);
  333. return err;
  334. }
  335. static int macvtap_release(struct inode *inode, struct file *file)
  336. {
  337. struct macvtap_queue *q = file->private_data;
  338. macvtap_put_queue(q);
  339. return 0;
  340. }
  341. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  342. {
  343. struct macvtap_queue *q = file->private_data;
  344. unsigned int mask = POLLERR;
  345. if (!q)
  346. goto out;
  347. mask = 0;
  348. poll_wait(file, &q->wq.wait, wait);
  349. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  350. mask |= POLLIN | POLLRDNORM;
  351. if (sock_writeable(&q->sk) ||
  352. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  353. sock_writeable(&q->sk)))
  354. mask |= POLLOUT | POLLWRNORM;
  355. out:
  356. return mask;
  357. }
  358. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  359. size_t len, size_t linear,
  360. int noblock, int *err)
  361. {
  362. struct sk_buff *skb;
  363. /* Under a page? Don't bother with paged skb. */
  364. if (prepad + len < PAGE_SIZE || !linear)
  365. linear = len;
  366. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  367. err);
  368. if (!skb)
  369. return NULL;
  370. skb_reserve(skb, prepad);
  371. skb_put(skb, linear);
  372. skb->data_len = len - linear;
  373. skb->len += len - linear;
  374. return skb;
  375. }
  376. /* set skb frags from iovec, this can move to core network code for reuse */
  377. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  378. int offset, size_t count)
  379. {
  380. int len = iov_length(from, count) - offset;
  381. int copy = skb_headlen(skb);
  382. int size, offset1 = 0;
  383. int i = 0;
  384. skb_frag_t *f;
  385. /* Skip over from offset */
  386. while (count && (offset >= from->iov_len)) {
  387. offset -= from->iov_len;
  388. ++from;
  389. --count;
  390. }
  391. /* copy up to skb headlen */
  392. while (count && (copy > 0)) {
  393. size = min_t(unsigned int, copy, from->iov_len - offset);
  394. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  395. size))
  396. return -EFAULT;
  397. if (copy > size) {
  398. ++from;
  399. --count;
  400. }
  401. copy -= size;
  402. offset1 += size;
  403. offset = 0;
  404. }
  405. if (len == offset1)
  406. return 0;
  407. while (count--) {
  408. struct page *page[MAX_SKB_FRAGS];
  409. int num_pages;
  410. unsigned long base;
  411. len = from->iov_len - offset1;
  412. if (!len) {
  413. offset1 = 0;
  414. ++from;
  415. continue;
  416. }
  417. base = (unsigned long)from->iov_base + offset1;
  418. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  419. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  420. if ((num_pages != size) ||
  421. (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
  422. /* put_page is in skb free */
  423. return -EFAULT;
  424. skb->data_len += len;
  425. skb->len += len;
  426. skb->truesize += len;
  427. atomic_add(len, &skb->sk->sk_wmem_alloc);
  428. while (len) {
  429. f = &skb_shinfo(skb)->frags[i];
  430. f->page = page[i];
  431. f->page_offset = base & ~PAGE_MASK;
  432. f->size = min_t(int, len, PAGE_SIZE - f->page_offset);
  433. skb_shinfo(skb)->nr_frags++;
  434. /* increase sk_wmem_alloc */
  435. base += f->size;
  436. len -= f->size;
  437. i++;
  438. }
  439. offset1 = 0;
  440. ++from;
  441. }
  442. return 0;
  443. }
  444. /*
  445. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  446. * be shared with the tun/tap driver.
  447. */
  448. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  449. struct virtio_net_hdr *vnet_hdr)
  450. {
  451. unsigned short gso_type = 0;
  452. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  453. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  454. case VIRTIO_NET_HDR_GSO_TCPV4:
  455. gso_type = SKB_GSO_TCPV4;
  456. break;
  457. case VIRTIO_NET_HDR_GSO_TCPV6:
  458. gso_type = SKB_GSO_TCPV6;
  459. break;
  460. case VIRTIO_NET_HDR_GSO_UDP:
  461. gso_type = SKB_GSO_UDP;
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  467. gso_type |= SKB_GSO_TCP_ECN;
  468. if (vnet_hdr->gso_size == 0)
  469. return -EINVAL;
  470. }
  471. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  472. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  473. vnet_hdr->csum_offset))
  474. return -EINVAL;
  475. }
  476. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  477. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  478. skb_shinfo(skb)->gso_type = gso_type;
  479. /* Header must be checked, and gso_segs computed. */
  480. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  481. skb_shinfo(skb)->gso_segs = 0;
  482. }
  483. return 0;
  484. }
  485. static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  486. struct virtio_net_hdr *vnet_hdr)
  487. {
  488. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  489. if (skb_is_gso(skb)) {
  490. struct skb_shared_info *sinfo = skb_shinfo(skb);
  491. /* This is a hint as to how much should be linear. */
  492. vnet_hdr->hdr_len = skb_headlen(skb);
  493. vnet_hdr->gso_size = sinfo->gso_size;
  494. if (sinfo->gso_type & SKB_GSO_TCPV4)
  495. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  496. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  497. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  498. else if (sinfo->gso_type & SKB_GSO_UDP)
  499. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  500. else
  501. BUG();
  502. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  503. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  504. } else
  505. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  506. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  507. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  508. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  509. vnet_hdr->csum_offset = skb->csum_offset;
  510. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  511. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  512. } /* else everything is zero */
  513. return 0;
  514. }
  515. /* Get packet from user space buffer */
  516. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  517. const struct iovec *iv, unsigned long total_len,
  518. size_t count, int noblock)
  519. {
  520. struct sk_buff *skb;
  521. struct macvlan_dev *vlan;
  522. unsigned long len = total_len;
  523. int err;
  524. struct virtio_net_hdr vnet_hdr = { 0 };
  525. int vnet_hdr_len = 0;
  526. int copylen;
  527. bool zerocopy = false;
  528. if (q->flags & IFF_VNET_HDR) {
  529. vnet_hdr_len = q->vnet_hdr_sz;
  530. err = -EINVAL;
  531. if (len < vnet_hdr_len)
  532. goto err;
  533. len -= vnet_hdr_len;
  534. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  535. sizeof(vnet_hdr));
  536. if (err < 0)
  537. goto err;
  538. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  539. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  540. vnet_hdr.hdr_len)
  541. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  542. vnet_hdr.csum_offset + 2;
  543. err = -EINVAL;
  544. if (vnet_hdr.hdr_len > len)
  545. goto err;
  546. }
  547. err = -EINVAL;
  548. if (unlikely(len < ETH_HLEN))
  549. goto err;
  550. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
  551. zerocopy = true;
  552. if (zerocopy) {
  553. /* There are 256 bytes to be copied in skb, so there is enough
  554. * room for skb expand head in case it is used.
  555. * The rest buffer is mapped from userspace.
  556. */
  557. copylen = vnet_hdr.hdr_len;
  558. if (!copylen)
  559. copylen = GOODCOPY_LEN;
  560. } else
  561. copylen = len;
  562. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  563. vnet_hdr.hdr_len, noblock, &err);
  564. if (!skb)
  565. goto err;
  566. if (zerocopy) {
  567. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  568. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  569. } else
  570. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  571. len);
  572. if (err)
  573. goto err_kfree;
  574. skb_set_network_header(skb, ETH_HLEN);
  575. skb_reset_mac_header(skb);
  576. skb->protocol = eth_hdr(skb)->h_proto;
  577. if (vnet_hdr_len) {
  578. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  579. if (err)
  580. goto err_kfree;
  581. }
  582. rcu_read_lock_bh();
  583. vlan = rcu_dereference_bh(q->vlan);
  584. /* copy skb_ubuf_info for callback when skb has no error */
  585. if (zerocopy)
  586. skb_shinfo(skb)->destructor_arg = m->msg_control;
  587. if (vlan)
  588. macvlan_start_xmit(skb, vlan->dev);
  589. else
  590. kfree_skb(skb);
  591. rcu_read_unlock_bh();
  592. return total_len;
  593. err_kfree:
  594. kfree_skb(skb);
  595. err:
  596. rcu_read_lock_bh();
  597. vlan = rcu_dereference_bh(q->vlan);
  598. if (vlan)
  599. vlan->dev->stats.tx_dropped++;
  600. rcu_read_unlock_bh();
  601. return err;
  602. }
  603. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  604. unsigned long count, loff_t pos)
  605. {
  606. struct file *file = iocb->ki_filp;
  607. ssize_t result = -ENOLINK;
  608. struct macvtap_queue *q = file->private_data;
  609. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  610. file->f_flags & O_NONBLOCK);
  611. return result;
  612. }
  613. /* Put packet to the user space buffer */
  614. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  615. const struct sk_buff *skb,
  616. const struct iovec *iv, int len)
  617. {
  618. struct macvlan_dev *vlan;
  619. int ret;
  620. int vnet_hdr_len = 0;
  621. if (q->flags & IFF_VNET_HDR) {
  622. struct virtio_net_hdr vnet_hdr;
  623. vnet_hdr_len = q->vnet_hdr_sz;
  624. if ((len -= vnet_hdr_len) < 0)
  625. return -EINVAL;
  626. ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  627. if (ret)
  628. return ret;
  629. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  630. return -EFAULT;
  631. }
  632. len = min_t(int, skb->len, len);
  633. ret = skb_copy_datagram_const_iovec(skb, 0, iv, vnet_hdr_len, len);
  634. rcu_read_lock_bh();
  635. vlan = rcu_dereference_bh(q->vlan);
  636. if (vlan)
  637. macvlan_count_rx(vlan, len, ret == 0, 0);
  638. rcu_read_unlock_bh();
  639. return ret ? ret : (len + vnet_hdr_len);
  640. }
  641. static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
  642. const struct iovec *iv, unsigned long len,
  643. int noblock)
  644. {
  645. DECLARE_WAITQUEUE(wait, current);
  646. struct sk_buff *skb;
  647. ssize_t ret = 0;
  648. add_wait_queue(sk_sleep(&q->sk), &wait);
  649. while (len) {
  650. current->state = TASK_INTERRUPTIBLE;
  651. /* Read frames from the queue */
  652. skb = skb_dequeue(&q->sk.sk_receive_queue);
  653. if (!skb) {
  654. if (noblock) {
  655. ret = -EAGAIN;
  656. break;
  657. }
  658. if (signal_pending(current)) {
  659. ret = -ERESTARTSYS;
  660. break;
  661. }
  662. /* Nothing to read, let's sleep */
  663. schedule();
  664. continue;
  665. }
  666. ret = macvtap_put_user(q, skb, iv, len);
  667. kfree_skb(skb);
  668. break;
  669. }
  670. current->state = TASK_RUNNING;
  671. remove_wait_queue(sk_sleep(&q->sk), &wait);
  672. return ret;
  673. }
  674. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  675. unsigned long count, loff_t pos)
  676. {
  677. struct file *file = iocb->ki_filp;
  678. struct macvtap_queue *q = file->private_data;
  679. ssize_t len, ret = 0;
  680. len = iov_length(iv, count);
  681. if (len < 0) {
  682. ret = -EINVAL;
  683. goto out;
  684. }
  685. ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
  686. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  687. out:
  688. return ret;
  689. }
  690. /*
  691. * provide compatibility with generic tun/tap interface
  692. */
  693. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  694. unsigned long arg)
  695. {
  696. struct macvtap_queue *q = file->private_data;
  697. struct macvlan_dev *vlan;
  698. void __user *argp = (void __user *)arg;
  699. struct ifreq __user *ifr = argp;
  700. unsigned int __user *up = argp;
  701. unsigned int u;
  702. int __user *sp = argp;
  703. int s;
  704. int ret;
  705. switch (cmd) {
  706. case TUNSETIFF:
  707. /* ignore the name, just look at flags */
  708. if (get_user(u, &ifr->ifr_flags))
  709. return -EFAULT;
  710. ret = 0;
  711. if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP))
  712. ret = -EINVAL;
  713. else
  714. q->flags = u;
  715. return ret;
  716. case TUNGETIFF:
  717. rcu_read_lock_bh();
  718. vlan = rcu_dereference_bh(q->vlan);
  719. if (vlan)
  720. dev_hold(vlan->dev);
  721. rcu_read_unlock_bh();
  722. if (!vlan)
  723. return -ENOLINK;
  724. ret = 0;
  725. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  726. put_user(q->flags, &ifr->ifr_flags))
  727. ret = -EFAULT;
  728. dev_put(vlan->dev);
  729. return ret;
  730. case TUNGETFEATURES:
  731. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR, up))
  732. return -EFAULT;
  733. return 0;
  734. case TUNSETSNDBUF:
  735. if (get_user(u, up))
  736. return -EFAULT;
  737. q->sk.sk_sndbuf = u;
  738. return 0;
  739. case TUNGETVNETHDRSZ:
  740. s = q->vnet_hdr_sz;
  741. if (put_user(s, sp))
  742. return -EFAULT;
  743. return 0;
  744. case TUNSETVNETHDRSZ:
  745. if (get_user(s, sp))
  746. return -EFAULT;
  747. if (s < (int)sizeof(struct virtio_net_hdr))
  748. return -EINVAL;
  749. q->vnet_hdr_sz = s;
  750. return 0;
  751. case TUNSETOFFLOAD:
  752. /* let the user check for future flags */
  753. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  754. TUN_F_TSO_ECN | TUN_F_UFO))
  755. return -EINVAL;
  756. /* TODO: only accept frames with the features that
  757. got enabled for forwarded frames */
  758. if (!(q->flags & IFF_VNET_HDR))
  759. return -EINVAL;
  760. return 0;
  761. default:
  762. return -EINVAL;
  763. }
  764. }
  765. #ifdef CONFIG_COMPAT
  766. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  767. unsigned long arg)
  768. {
  769. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  770. }
  771. #endif
  772. static const struct file_operations macvtap_fops = {
  773. .owner = THIS_MODULE,
  774. .open = macvtap_open,
  775. .release = macvtap_release,
  776. .aio_read = macvtap_aio_read,
  777. .aio_write = macvtap_aio_write,
  778. .poll = macvtap_poll,
  779. .llseek = no_llseek,
  780. .unlocked_ioctl = macvtap_ioctl,
  781. #ifdef CONFIG_COMPAT
  782. .compat_ioctl = macvtap_compat_ioctl,
  783. #endif
  784. };
  785. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  786. struct msghdr *m, size_t total_len)
  787. {
  788. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  789. return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
  790. m->msg_flags & MSG_DONTWAIT);
  791. }
  792. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  793. struct msghdr *m, size_t total_len,
  794. int flags)
  795. {
  796. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  797. int ret;
  798. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  799. return -EINVAL;
  800. ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
  801. flags & MSG_DONTWAIT);
  802. if (ret > total_len) {
  803. m->msg_flags |= MSG_TRUNC;
  804. ret = flags & MSG_TRUNC ? ret : total_len;
  805. }
  806. return ret;
  807. }
  808. /* Ops structure to mimic raw sockets with tun */
  809. static const struct proto_ops macvtap_socket_ops = {
  810. .sendmsg = macvtap_sendmsg,
  811. .recvmsg = macvtap_recvmsg,
  812. };
  813. /* Get an underlying socket object from tun file. Returns error unless file is
  814. * attached to a device. The returned object works like a packet socket, it
  815. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  816. * holding a reference to the file for as long as the socket is in use. */
  817. struct socket *macvtap_get_socket(struct file *file)
  818. {
  819. struct macvtap_queue *q;
  820. if (file->f_op != &macvtap_fops)
  821. return ERR_PTR(-EINVAL);
  822. q = file->private_data;
  823. if (!q)
  824. return ERR_PTR(-EBADFD);
  825. return &q->sock;
  826. }
  827. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  828. static int macvtap_init(void)
  829. {
  830. int err;
  831. err = alloc_chrdev_region(&macvtap_major, 0,
  832. MACVTAP_NUM_DEVS, "macvtap");
  833. if (err)
  834. goto out1;
  835. cdev_init(&macvtap_cdev, &macvtap_fops);
  836. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  837. if (err)
  838. goto out2;
  839. macvtap_class = class_create(THIS_MODULE, "macvtap");
  840. if (IS_ERR(macvtap_class)) {
  841. err = PTR_ERR(macvtap_class);
  842. goto out3;
  843. }
  844. err = macvlan_link_register(&macvtap_link_ops);
  845. if (err)
  846. goto out4;
  847. return 0;
  848. out4:
  849. class_unregister(macvtap_class);
  850. out3:
  851. cdev_del(&macvtap_cdev);
  852. out2:
  853. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  854. out1:
  855. return err;
  856. }
  857. module_init(macvtap_init);
  858. static void macvtap_exit(void)
  859. {
  860. rtnl_link_unregister(&macvtap_link_ops);
  861. class_unregister(macvtap_class);
  862. cdev_del(&macvtap_cdev);
  863. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  864. }
  865. module_exit(macvtap_exit);
  866. MODULE_ALIAS_RTNL_LINK("macvtap");
  867. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  868. MODULE_LICENSE("GPL");