macvtap.c 28 KB

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