macvtap.c 29 KB

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