macvtap.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. /* 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_newlink(struct net *src_net,
  241. struct net_device *dev,
  242. struct nlattr *tb[],
  243. struct nlattr *data[])
  244. {
  245. /* Don't put anything that may fail after macvlan_common_newlink
  246. * because we can't undo what it does.
  247. */
  248. return macvlan_common_newlink(src_net, dev, tb, data,
  249. macvtap_receive, macvtap_forward);
  250. }
  251. static void macvtap_dellink(struct net_device *dev,
  252. struct list_head *head)
  253. {
  254. macvtap_del_queues(dev);
  255. macvlan_dellink(dev, head);
  256. }
  257. static void macvtap_setup(struct net_device *dev)
  258. {
  259. macvlan_common_setup(dev);
  260. dev->tx_queue_len = TUN_READQ_SIZE;
  261. }
  262. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  263. .kind = "macvtap",
  264. .setup = macvtap_setup,
  265. .newlink = macvtap_newlink,
  266. .dellink = macvtap_dellink,
  267. };
  268. static void macvtap_sock_write_space(struct sock *sk)
  269. {
  270. wait_queue_head_t *wqueue;
  271. if (!sock_writeable(sk) ||
  272. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  273. return;
  274. wqueue = sk_sleep(sk);
  275. if (wqueue && waitqueue_active(wqueue))
  276. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  277. }
  278. static void macvtap_sock_destruct(struct sock *sk)
  279. {
  280. skb_queue_purge(&sk->sk_receive_queue);
  281. }
  282. static int macvtap_open(struct inode *inode, struct file *file)
  283. {
  284. struct net *net = current->nsproxy->net_ns;
  285. struct net_device *dev = dev_get_by_index(net, iminor(inode));
  286. struct macvtap_queue *q;
  287. int err;
  288. err = -ENODEV;
  289. if (!dev)
  290. goto out;
  291. /* check if this is a macvtap device */
  292. err = -EINVAL;
  293. if (dev->rtnl_link_ops != &macvtap_link_ops)
  294. goto out;
  295. err = -ENOMEM;
  296. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  297. &macvtap_proto);
  298. if (!q)
  299. goto out;
  300. q->sock.wq = &q->wq;
  301. init_waitqueue_head(&q->wq.wait);
  302. q->sock.type = SOCK_RAW;
  303. q->sock.state = SS_CONNECTED;
  304. q->sock.file = file;
  305. q->sock.ops = &macvtap_socket_ops;
  306. sock_init_data(&q->sock, &q->sk);
  307. q->sk.sk_write_space = macvtap_sock_write_space;
  308. q->sk.sk_destruct = macvtap_sock_destruct;
  309. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  310. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  311. /*
  312. * so far only KVM virtio_net uses macvtap, enable zero copy between
  313. * guest kernel and host kernel when lower device supports zerocopy
  314. *
  315. * The macvlan supports zerocopy iff the lower device supports zero
  316. * copy so we don't have to look at the lower device directly.
  317. */
  318. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  319. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  320. err = macvtap_set_queue(dev, file, q);
  321. if (err)
  322. sock_put(&q->sk);
  323. out:
  324. if (dev)
  325. dev_put(dev);
  326. return err;
  327. }
  328. static int macvtap_release(struct inode *inode, struct file *file)
  329. {
  330. struct macvtap_queue *q = file->private_data;
  331. macvtap_put_queue(q);
  332. return 0;
  333. }
  334. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  335. {
  336. struct macvtap_queue *q = file->private_data;
  337. unsigned int mask = POLLERR;
  338. if (!q)
  339. goto out;
  340. mask = 0;
  341. poll_wait(file, &q->wq.wait, wait);
  342. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  343. mask |= POLLIN | POLLRDNORM;
  344. if (sock_writeable(&q->sk) ||
  345. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  346. sock_writeable(&q->sk)))
  347. mask |= POLLOUT | POLLWRNORM;
  348. out:
  349. return mask;
  350. }
  351. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  352. size_t len, size_t linear,
  353. int noblock, int *err)
  354. {
  355. struct sk_buff *skb;
  356. /* Under a page? Don't bother with paged skb. */
  357. if (prepad + len < PAGE_SIZE || !linear)
  358. linear = len;
  359. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  360. err);
  361. if (!skb)
  362. return NULL;
  363. skb_reserve(skb, prepad);
  364. skb_put(skb, linear);
  365. skb->data_len = len - linear;
  366. skb->len += len - linear;
  367. return skb;
  368. }
  369. /* set skb frags from iovec, this can move to core network code for reuse */
  370. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  371. int offset, size_t count)
  372. {
  373. int len = iov_length(from, count) - offset;
  374. int copy = skb_headlen(skb);
  375. int size, offset1 = 0;
  376. int i = 0;
  377. /* Skip over from offset */
  378. while (count && (offset >= from->iov_len)) {
  379. offset -= from->iov_len;
  380. ++from;
  381. --count;
  382. }
  383. /* copy up to skb headlen */
  384. while (count && (copy > 0)) {
  385. size = min_t(unsigned int, copy, from->iov_len - offset);
  386. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  387. size))
  388. return -EFAULT;
  389. if (copy > size) {
  390. ++from;
  391. --count;
  392. }
  393. copy -= size;
  394. offset1 += size;
  395. offset = 0;
  396. }
  397. if (len == offset1)
  398. return 0;
  399. while (count--) {
  400. struct page *page[MAX_SKB_FRAGS];
  401. int num_pages;
  402. unsigned long base;
  403. len = from->iov_len - offset1;
  404. if (!len) {
  405. offset1 = 0;
  406. ++from;
  407. continue;
  408. }
  409. base = (unsigned long)from->iov_base + offset1;
  410. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  411. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  412. if ((num_pages != size) ||
  413. (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
  414. /* put_page is in skb free */
  415. return -EFAULT;
  416. skb->data_len += len;
  417. skb->len += len;
  418. skb->truesize += len;
  419. atomic_add(len, &skb->sk->sk_wmem_alloc);
  420. while (len) {
  421. int off = base & ~PAGE_MASK;
  422. int size = min_t(int, len, PAGE_SIZE - off);
  423. __skb_fill_page_desc(skb, i, page[i], off, size);
  424. skb_shinfo(skb)->nr_frags++;
  425. /* increase sk_wmem_alloc */
  426. base += size;
  427. len -= size;
  428. i++;
  429. }
  430. offset1 = 0;
  431. ++from;
  432. }
  433. return 0;
  434. }
  435. /*
  436. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  437. * be shared with the tun/tap driver.
  438. */
  439. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  440. struct virtio_net_hdr *vnet_hdr)
  441. {
  442. unsigned short gso_type = 0;
  443. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  444. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  445. case VIRTIO_NET_HDR_GSO_TCPV4:
  446. gso_type = SKB_GSO_TCPV4;
  447. break;
  448. case VIRTIO_NET_HDR_GSO_TCPV6:
  449. gso_type = SKB_GSO_TCPV6;
  450. break;
  451. case VIRTIO_NET_HDR_GSO_UDP:
  452. gso_type = SKB_GSO_UDP;
  453. break;
  454. default:
  455. return -EINVAL;
  456. }
  457. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  458. gso_type |= SKB_GSO_TCP_ECN;
  459. if (vnet_hdr->gso_size == 0)
  460. return -EINVAL;
  461. }
  462. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  463. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  464. vnet_hdr->csum_offset))
  465. return -EINVAL;
  466. }
  467. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  468. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  469. skb_shinfo(skb)->gso_type = gso_type;
  470. /* Header must be checked, and gso_segs computed. */
  471. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  472. skb_shinfo(skb)->gso_segs = 0;
  473. }
  474. return 0;
  475. }
  476. static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  477. struct virtio_net_hdr *vnet_hdr)
  478. {
  479. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  480. if (skb_is_gso(skb)) {
  481. struct skb_shared_info *sinfo = skb_shinfo(skb);
  482. /* This is a hint as to how much should be linear. */
  483. vnet_hdr->hdr_len = skb_headlen(skb);
  484. vnet_hdr->gso_size = sinfo->gso_size;
  485. if (sinfo->gso_type & SKB_GSO_TCPV4)
  486. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  487. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  488. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  489. else if (sinfo->gso_type & SKB_GSO_UDP)
  490. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  491. else
  492. BUG();
  493. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  494. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  495. } else
  496. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  497. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  498. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  499. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  500. vnet_hdr->csum_offset = skb->csum_offset;
  501. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  502. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  503. } /* else everything is zero */
  504. return 0;
  505. }
  506. /* Get packet from user space buffer */
  507. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  508. const struct iovec *iv, unsigned long total_len,
  509. size_t count, int noblock)
  510. {
  511. struct sk_buff *skb;
  512. struct macvlan_dev *vlan;
  513. unsigned long len = total_len;
  514. int err;
  515. struct virtio_net_hdr vnet_hdr = { 0 };
  516. int vnet_hdr_len = 0;
  517. int copylen;
  518. bool zerocopy = false;
  519. if (q->flags & IFF_VNET_HDR) {
  520. vnet_hdr_len = q->vnet_hdr_sz;
  521. err = -EINVAL;
  522. if (len < vnet_hdr_len)
  523. goto err;
  524. len -= vnet_hdr_len;
  525. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  526. sizeof(vnet_hdr));
  527. if (err < 0)
  528. goto err;
  529. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  530. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  531. vnet_hdr.hdr_len)
  532. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  533. vnet_hdr.csum_offset + 2;
  534. err = -EINVAL;
  535. if (vnet_hdr.hdr_len > len)
  536. goto err;
  537. }
  538. err = -EINVAL;
  539. if (unlikely(len < ETH_HLEN))
  540. goto err;
  541. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
  542. zerocopy = true;
  543. if (zerocopy) {
  544. /* There are 256 bytes to be copied in skb, so there is enough
  545. * room for skb expand head in case it is used.
  546. * The rest buffer is mapped from userspace.
  547. */
  548. copylen = vnet_hdr.hdr_len;
  549. if (!copylen)
  550. copylen = GOODCOPY_LEN;
  551. } else
  552. copylen = len;
  553. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  554. vnet_hdr.hdr_len, noblock, &err);
  555. if (!skb)
  556. goto err;
  557. if (zerocopy) {
  558. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  559. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  560. } else
  561. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  562. len);
  563. if (err)
  564. goto err_kfree;
  565. skb_set_network_header(skb, ETH_HLEN);
  566. skb_reset_mac_header(skb);
  567. skb->protocol = eth_hdr(skb)->h_proto;
  568. if (vnet_hdr_len) {
  569. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  570. if (err)
  571. goto err_kfree;
  572. }
  573. rcu_read_lock_bh();
  574. vlan = rcu_dereference_bh(q->vlan);
  575. /* copy skb_ubuf_info for callback when skb has no error */
  576. if (zerocopy)
  577. skb_shinfo(skb)->destructor_arg = m->msg_control;
  578. if (vlan)
  579. macvlan_start_xmit(skb, vlan->dev);
  580. else
  581. kfree_skb(skb);
  582. rcu_read_unlock_bh();
  583. return total_len;
  584. err_kfree:
  585. kfree_skb(skb);
  586. err:
  587. rcu_read_lock_bh();
  588. vlan = rcu_dereference_bh(q->vlan);
  589. if (vlan)
  590. vlan->dev->stats.tx_dropped++;
  591. rcu_read_unlock_bh();
  592. return err;
  593. }
  594. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  595. unsigned long count, loff_t pos)
  596. {
  597. struct file *file = iocb->ki_filp;
  598. ssize_t result = -ENOLINK;
  599. struct macvtap_queue *q = file->private_data;
  600. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  601. file->f_flags & O_NONBLOCK);
  602. return result;
  603. }
  604. /* Put packet to the user space buffer */
  605. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  606. const struct sk_buff *skb,
  607. const struct iovec *iv, int len)
  608. {
  609. struct macvlan_dev *vlan;
  610. int ret;
  611. int vnet_hdr_len = 0;
  612. if (q->flags & IFF_VNET_HDR) {
  613. struct virtio_net_hdr vnet_hdr;
  614. vnet_hdr_len = q->vnet_hdr_sz;
  615. if ((len -= vnet_hdr_len) < 0)
  616. return -EINVAL;
  617. ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  618. if (ret)
  619. return ret;
  620. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  621. return -EFAULT;
  622. }
  623. len = min_t(int, skb->len, len);
  624. ret = skb_copy_datagram_const_iovec(skb, 0, iv, vnet_hdr_len, len);
  625. rcu_read_lock_bh();
  626. vlan = rcu_dereference_bh(q->vlan);
  627. if (vlan)
  628. macvlan_count_rx(vlan, len, ret == 0, 0);
  629. rcu_read_unlock_bh();
  630. return ret ? ret : (len + vnet_hdr_len);
  631. }
  632. static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
  633. const struct iovec *iv, unsigned long len,
  634. int noblock)
  635. {
  636. DECLARE_WAITQUEUE(wait, current);
  637. struct sk_buff *skb;
  638. ssize_t ret = 0;
  639. add_wait_queue(sk_sleep(&q->sk), &wait);
  640. while (len) {
  641. current->state = TASK_INTERRUPTIBLE;
  642. /* Read frames from the queue */
  643. skb = skb_dequeue(&q->sk.sk_receive_queue);
  644. if (!skb) {
  645. if (noblock) {
  646. ret = -EAGAIN;
  647. break;
  648. }
  649. if (signal_pending(current)) {
  650. ret = -ERESTARTSYS;
  651. break;
  652. }
  653. /* Nothing to read, let's sleep */
  654. schedule();
  655. continue;
  656. }
  657. ret = macvtap_put_user(q, skb, iv, len);
  658. kfree_skb(skb);
  659. break;
  660. }
  661. current->state = TASK_RUNNING;
  662. remove_wait_queue(sk_sleep(&q->sk), &wait);
  663. return ret;
  664. }
  665. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  666. unsigned long count, loff_t pos)
  667. {
  668. struct file *file = iocb->ki_filp;
  669. struct macvtap_queue *q = file->private_data;
  670. ssize_t len, ret = 0;
  671. len = iov_length(iv, count);
  672. if (len < 0) {
  673. ret = -EINVAL;
  674. goto out;
  675. }
  676. ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
  677. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  678. out:
  679. return ret;
  680. }
  681. /*
  682. * provide compatibility with generic tun/tap interface
  683. */
  684. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  685. unsigned long arg)
  686. {
  687. struct macvtap_queue *q = file->private_data;
  688. struct macvlan_dev *vlan;
  689. void __user *argp = (void __user *)arg;
  690. struct ifreq __user *ifr = argp;
  691. unsigned int __user *up = argp;
  692. unsigned int u;
  693. int __user *sp = argp;
  694. int s;
  695. int ret;
  696. switch (cmd) {
  697. case TUNSETIFF:
  698. /* ignore the name, just look at flags */
  699. if (get_user(u, &ifr->ifr_flags))
  700. return -EFAULT;
  701. ret = 0;
  702. if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP))
  703. ret = -EINVAL;
  704. else
  705. q->flags = u;
  706. return ret;
  707. case TUNGETIFF:
  708. rcu_read_lock_bh();
  709. vlan = rcu_dereference_bh(q->vlan);
  710. if (vlan)
  711. dev_hold(vlan->dev);
  712. rcu_read_unlock_bh();
  713. if (!vlan)
  714. return -ENOLINK;
  715. ret = 0;
  716. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  717. put_user(q->flags, &ifr->ifr_flags))
  718. ret = -EFAULT;
  719. dev_put(vlan->dev);
  720. return ret;
  721. case TUNGETFEATURES:
  722. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR, up))
  723. return -EFAULT;
  724. return 0;
  725. case TUNSETSNDBUF:
  726. if (get_user(u, up))
  727. return -EFAULT;
  728. q->sk.sk_sndbuf = u;
  729. return 0;
  730. case TUNGETVNETHDRSZ:
  731. s = q->vnet_hdr_sz;
  732. if (put_user(s, sp))
  733. return -EFAULT;
  734. return 0;
  735. case TUNSETVNETHDRSZ:
  736. if (get_user(s, sp))
  737. return -EFAULT;
  738. if (s < (int)sizeof(struct virtio_net_hdr))
  739. return -EINVAL;
  740. q->vnet_hdr_sz = s;
  741. return 0;
  742. case TUNSETOFFLOAD:
  743. /* let the user check for future flags */
  744. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  745. TUN_F_TSO_ECN | TUN_F_UFO))
  746. return -EINVAL;
  747. /* TODO: only accept frames with the features that
  748. got enabled for forwarded frames */
  749. if (!(q->flags & IFF_VNET_HDR))
  750. return -EINVAL;
  751. return 0;
  752. default:
  753. return -EINVAL;
  754. }
  755. }
  756. #ifdef CONFIG_COMPAT
  757. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  758. unsigned long arg)
  759. {
  760. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  761. }
  762. #endif
  763. static const struct file_operations macvtap_fops = {
  764. .owner = THIS_MODULE,
  765. .open = macvtap_open,
  766. .release = macvtap_release,
  767. .aio_read = macvtap_aio_read,
  768. .aio_write = macvtap_aio_write,
  769. .poll = macvtap_poll,
  770. .llseek = no_llseek,
  771. .unlocked_ioctl = macvtap_ioctl,
  772. #ifdef CONFIG_COMPAT
  773. .compat_ioctl = macvtap_compat_ioctl,
  774. #endif
  775. };
  776. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  777. struct msghdr *m, size_t total_len)
  778. {
  779. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  780. return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
  781. m->msg_flags & MSG_DONTWAIT);
  782. }
  783. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  784. struct msghdr *m, size_t total_len,
  785. int flags)
  786. {
  787. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  788. int ret;
  789. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  790. return -EINVAL;
  791. ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
  792. flags & MSG_DONTWAIT);
  793. if (ret > total_len) {
  794. m->msg_flags |= MSG_TRUNC;
  795. ret = flags & MSG_TRUNC ? ret : total_len;
  796. }
  797. return ret;
  798. }
  799. /* Ops structure to mimic raw sockets with tun */
  800. static const struct proto_ops macvtap_socket_ops = {
  801. .sendmsg = macvtap_sendmsg,
  802. .recvmsg = macvtap_recvmsg,
  803. };
  804. /* Get an underlying socket object from tun file. Returns error unless file is
  805. * attached to a device. The returned object works like a packet socket, it
  806. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  807. * holding a reference to the file for as long as the socket is in use. */
  808. struct socket *macvtap_get_socket(struct file *file)
  809. {
  810. struct macvtap_queue *q;
  811. if (file->f_op != &macvtap_fops)
  812. return ERR_PTR(-EINVAL);
  813. q = file->private_data;
  814. if (!q)
  815. return ERR_PTR(-EBADFD);
  816. return &q->sock;
  817. }
  818. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  819. static int macvtap_device_event(struct notifier_block *unused,
  820. unsigned long event, void *ptr)
  821. {
  822. struct net_device *dev = ptr;
  823. struct device *classdev;
  824. dev_t devt;
  825. if (dev->rtnl_link_ops != &macvtap_link_ops)
  826. return NOTIFY_DONE;
  827. switch (event) {
  828. case NETDEV_REGISTER:
  829. /* Create the device node here after the network device has
  830. * been registered but before register_netdevice has
  831. * finished running.
  832. */
  833. devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
  834. classdev = device_create(macvtap_class, &dev->dev, devt,
  835. dev, "tap%d", dev->ifindex);
  836. if (IS_ERR(classdev))
  837. return notifier_from_errno(PTR_ERR(classdev));
  838. break;
  839. case NETDEV_UNREGISTER:
  840. devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
  841. device_destroy(macvtap_class, devt);
  842. break;
  843. }
  844. return NOTIFY_DONE;
  845. }
  846. static struct notifier_block macvtap_notifier_block __read_mostly = {
  847. .notifier_call = macvtap_device_event,
  848. };
  849. static int macvtap_init(void)
  850. {
  851. int err;
  852. err = alloc_chrdev_region(&macvtap_major, 0,
  853. MACVTAP_NUM_DEVS, "macvtap");
  854. if (err)
  855. goto out1;
  856. cdev_init(&macvtap_cdev, &macvtap_fops);
  857. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  858. if (err)
  859. goto out2;
  860. macvtap_class = class_create(THIS_MODULE, "macvtap");
  861. if (IS_ERR(macvtap_class)) {
  862. err = PTR_ERR(macvtap_class);
  863. goto out3;
  864. }
  865. err = register_netdevice_notifier(&macvtap_notifier_block);
  866. if (err)
  867. goto out4;
  868. err = macvlan_link_register(&macvtap_link_ops);
  869. if (err)
  870. goto out5;
  871. return 0;
  872. out5:
  873. unregister_netdevice_notifier(&macvtap_notifier_block);
  874. out4:
  875. class_unregister(macvtap_class);
  876. out3:
  877. cdev_del(&macvtap_cdev);
  878. out2:
  879. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  880. out1:
  881. return err;
  882. }
  883. module_init(macvtap_init);
  884. static void macvtap_exit(void)
  885. {
  886. rtnl_link_unregister(&macvtap_link_ops);
  887. unregister_netdevice_notifier(&macvtap_notifier_block);
  888. class_unregister(macvtap_class);
  889. cdev_del(&macvtap_cdev);
  890. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  891. }
  892. module_exit(macvtap_exit);
  893. MODULE_ALIAS_RTNL_LINK("macvtap");
  894. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  895. MODULE_LICENSE("GPL");