macvtap.c 29 KB

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