macvtap.c 26 KB

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