macvtap.c 28 KB

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