macvtap.c 29 KB

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