macvtap.c 29 KB

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