macvtap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/compat.h>
  7. #include <linux/if_tun.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/cache.h>
  11. #include <linux/sched.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/wait.h>
  16. #include <linux/cdev.h>
  17. #include <linux/idr.h>
  18. #include <linux/fs.h>
  19. #include <net/net_namespace.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <linux/virtio_net.h>
  23. /*
  24. * A macvtap queue is the central object of this driver, it connects
  25. * an open character device to a macvlan interface. There can be
  26. * multiple queues on one interface, which map back to queues
  27. * implemented in hardware on the underlying device.
  28. *
  29. * macvtap_proto is used to allocate queues through the sock allocation
  30. * mechanism.
  31. *
  32. */
  33. struct macvtap_queue {
  34. struct sock sk;
  35. struct socket sock;
  36. struct socket_wq wq;
  37. int vnet_hdr_sz;
  38. struct macvlan_dev __rcu *vlan;
  39. struct file *file;
  40. unsigned int flags;
  41. u16 queue_index;
  42. bool enabled;
  43. struct list_head next;
  44. };
  45. static struct proto macvtap_proto = {
  46. .name = "macvtap",
  47. .owner = THIS_MODULE,
  48. .obj_size = sizeof (struct macvtap_queue),
  49. };
  50. /*
  51. * Variables for dealing with macvtaps device numbers.
  52. */
  53. static dev_t macvtap_major;
  54. #define MACVTAP_NUM_DEVS (1U << MINORBITS)
  55. static DEFINE_MUTEX(minor_lock);
  56. static DEFINE_IDR(minor_idr);
  57. #define GOODCOPY_LEN 128
  58. static struct class *macvtap_class;
  59. static struct cdev macvtap_cdev;
  60. static const struct proto_ops macvtap_socket_ops;
  61. #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
  62. NETIF_F_TSO6 | NETIF_F_UFO)
  63. #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
  64. #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG)
  65. 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. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  102. struct macvtap_queue *q)
  103. {
  104. struct macvlan_dev *vlan = netdev_priv(dev);
  105. int err = -EBUSY;
  106. rtnl_lock();
  107. if (vlan->numqueues == MAX_MACVTAP_QUEUES)
  108. goto out;
  109. err = 0;
  110. rcu_assign_pointer(q->vlan, vlan);
  111. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  112. sock_hold(&q->sk);
  113. q->file = file;
  114. q->queue_index = vlan->numvtaps;
  115. q->enabled = true;
  116. file->private_data = q;
  117. list_add_tail(&q->next, &vlan->queue_list);
  118. vlan->numvtaps++;
  119. vlan->numqueues++;
  120. out:
  121. rtnl_unlock();
  122. return err;
  123. }
  124. static int macvtap_disable_queue(struct macvtap_queue *q)
  125. {
  126. struct macvlan_dev *vlan;
  127. struct macvtap_queue *nq;
  128. ASSERT_RTNL();
  129. if (!q->enabled)
  130. return -EINVAL;
  131. vlan = rtnl_dereference(q->vlan);
  132. if (vlan) {
  133. int index = q->queue_index;
  134. BUG_ON(index >= vlan->numvtaps);
  135. nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
  136. nq->queue_index = index;
  137. rcu_assign_pointer(vlan->taps[index], nq);
  138. RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
  139. q->enabled = false;
  140. vlan->numvtaps--;
  141. }
  142. return 0;
  143. }
  144. /*
  145. * The file owning the queue got closed, give up both
  146. * the reference that the files holds as well as the
  147. * one from the macvlan_dev if that still exists.
  148. *
  149. * Using the spinlock makes sure that we don't get
  150. * to the queue again after destroying it.
  151. */
  152. static void macvtap_put_queue(struct macvtap_queue *q)
  153. {
  154. struct macvlan_dev *vlan;
  155. rtnl_lock();
  156. vlan = rtnl_dereference(q->vlan);
  157. if (vlan) {
  158. if (q->enabled)
  159. BUG_ON(macvtap_disable_queue(q));
  160. vlan->numqueues--;
  161. RCU_INIT_POINTER(q->vlan, NULL);
  162. sock_put(&q->sk);
  163. list_del_init(&q->next);
  164. }
  165. rtnl_unlock();
  166. synchronize_rcu();
  167. sock_put(&q->sk);
  168. }
  169. /*
  170. * Select a queue based on the rxq of the device on which this packet
  171. * arrived. If the incoming device is not mq, calculate a flow hash
  172. * to select a queue. If all fails, find the first available queue.
  173. * Cache vlan->numvtaps since it can become zero during the execution
  174. * of this function.
  175. */
  176. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  177. struct sk_buff *skb)
  178. {
  179. struct macvlan_dev *vlan = netdev_priv(dev);
  180. struct macvtap_queue *tap = NULL;
  181. /* Access to taps array is protected by rcu, but access to numvtaps
  182. * isn't. Below we use it to lookup a queue, but treat it as a hint
  183. * and validate that the result isn't NULL - in case we are
  184. * racing against queue removal.
  185. */
  186. int numvtaps = ACCESS_ONCE(vlan->numvtaps);
  187. __u32 rxq;
  188. if (!numvtaps)
  189. goto out;
  190. /* Check if we can use flow to select a queue */
  191. rxq = skb_get_hash(skb);
  192. if (rxq) {
  193. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  194. goto out;
  195. }
  196. if (likely(skb_rx_queue_recorded(skb))) {
  197. rxq = skb_get_rx_queue(skb);
  198. while (unlikely(rxq >= numvtaps))
  199. rxq -= numvtaps;
  200. tap = rcu_dereference(vlan->taps[rxq]);
  201. goto out;
  202. }
  203. tap = rcu_dereference(vlan->taps[0]);
  204. out:
  205. return tap;
  206. }
  207. /*
  208. * The net_device is going away, give up the reference
  209. * that it holds on all queues and safely set the pointer
  210. * from the queues to NULL.
  211. */
  212. static void macvtap_del_queues(struct net_device *dev)
  213. {
  214. struct macvlan_dev *vlan = netdev_priv(dev);
  215. struct macvtap_queue *q, *tmp, *qlist[MAX_MACVTAP_QUEUES];
  216. int i, j = 0;
  217. ASSERT_RTNL();
  218. list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
  219. list_del_init(&q->next);
  220. qlist[j++] = q;
  221. RCU_INIT_POINTER(q->vlan, NULL);
  222. if (q->enabled)
  223. vlan->numvtaps--;
  224. vlan->numqueues--;
  225. }
  226. for (i = 0; i < vlan->numvtaps; i++)
  227. RCU_INIT_POINTER(vlan->taps[i], NULL);
  228. BUG_ON(vlan->numvtaps);
  229. BUG_ON(vlan->numqueues);
  230. /* guarantee that any future macvtap_set_queue will fail */
  231. vlan->numvtaps = MAX_MACVTAP_QUEUES;
  232. for (--j; j >= 0; j--)
  233. sock_put(&qlist[j]->sk);
  234. }
  235. static rx_handler_result_t macvtap_handle_frame(struct sk_buff **pskb)
  236. {
  237. struct sk_buff *skb = *pskb;
  238. struct net_device *dev = skb->dev;
  239. struct macvlan_dev *vlan;
  240. struct macvtap_queue *q;
  241. netdev_features_t features = TAP_FEATURES;
  242. vlan = macvtap_get_vlan_rcu(dev);
  243. if (!vlan)
  244. return RX_HANDLER_PASS;
  245. q = macvtap_get_queue(dev, skb);
  246. if (!q)
  247. return RX_HANDLER_PASS;
  248. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  249. goto drop;
  250. skb_push(skb, ETH_HLEN);
  251. /* Apply the forward feature mask so that we perform segmentation
  252. * according to users wishes. This only works if VNET_HDR is
  253. * enabled.
  254. */
  255. if (q->flags & IFF_VNET_HDR)
  256. features |= vlan->tap_features;
  257. if (netif_needs_gso(skb, features)) {
  258. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  259. if (IS_ERR(segs))
  260. goto drop;
  261. if (!segs) {
  262. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  263. goto wake_up;
  264. }
  265. kfree_skb(skb);
  266. while (segs) {
  267. struct sk_buff *nskb = segs->next;
  268. segs->next = NULL;
  269. skb_queue_tail(&q->sk.sk_receive_queue, segs);
  270. segs = nskb;
  271. }
  272. } else {
  273. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  274. }
  275. wake_up:
  276. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  277. return RX_HANDLER_CONSUMED;
  278. drop:
  279. /* Count errors/drops only here, thus don't care about args. */
  280. macvlan_count_rx(vlan, 0, 0, 0);
  281. kfree_skb(skb);
  282. return RX_HANDLER_CONSUMED;
  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. int err;
  327. INIT_LIST_HEAD(&vlan->queue_list);
  328. /* Since macvlan supports all offloads by default, make
  329. * tap support all offloads also.
  330. */
  331. vlan->tap_features = TUN_OFFLOADS;
  332. err = netdev_rx_handler_register(dev, macvtap_handle_frame, vlan);
  333. if (err)
  334. return err;
  335. /* Don't put anything that may fail after macvlan_common_newlink
  336. * because we can't undo what it does.
  337. */
  338. return macvlan_common_newlink(src_net, dev, tb, data);
  339. }
  340. static void macvtap_dellink(struct net_device *dev,
  341. struct list_head *head)
  342. {
  343. netdev_rx_handler_unregister(dev);
  344. macvtap_del_queues(dev);
  345. macvlan_dellink(dev, head);
  346. }
  347. static void macvtap_setup(struct net_device *dev)
  348. {
  349. macvlan_common_setup(dev);
  350. dev->tx_queue_len = TUN_READQ_SIZE;
  351. }
  352. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  353. .kind = "macvtap",
  354. .setup = macvtap_setup,
  355. .newlink = macvtap_newlink,
  356. .dellink = macvtap_dellink,
  357. };
  358. static void macvtap_sock_write_space(struct sock *sk)
  359. {
  360. wait_queue_head_t *wqueue;
  361. if (!sock_writeable(sk) ||
  362. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  363. return;
  364. wqueue = sk_sleep(sk);
  365. if (wqueue && waitqueue_active(wqueue))
  366. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  367. }
  368. static void macvtap_sock_destruct(struct sock *sk)
  369. {
  370. skb_queue_purge(&sk->sk_receive_queue);
  371. }
  372. static int macvtap_open(struct inode *inode, struct file *file)
  373. {
  374. struct net *net = current->nsproxy->net_ns;
  375. struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
  376. struct macvtap_queue *q;
  377. int err;
  378. err = -ENODEV;
  379. if (!dev)
  380. goto out;
  381. err = -ENOMEM;
  382. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  383. &macvtap_proto);
  384. if (!q)
  385. goto out;
  386. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  387. init_waitqueue_head(&q->wq.wait);
  388. q->sock.type = SOCK_RAW;
  389. q->sock.state = SS_CONNECTED;
  390. q->sock.file = file;
  391. q->sock.ops = &macvtap_socket_ops;
  392. sock_init_data(&q->sock, &q->sk);
  393. q->sk.sk_write_space = macvtap_sock_write_space;
  394. q->sk.sk_destruct = macvtap_sock_destruct;
  395. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  396. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  397. /*
  398. * so far only KVM virtio_net uses macvtap, enable zero copy between
  399. * guest kernel and host kernel when lower device supports zerocopy
  400. *
  401. * The macvlan supports zerocopy iff the lower device supports zero
  402. * copy so we don't have to look at the lower device directly.
  403. */
  404. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  405. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  406. err = macvtap_set_queue(dev, file, q);
  407. if (err)
  408. sock_put(&q->sk);
  409. out:
  410. if (dev)
  411. dev_put(dev);
  412. return err;
  413. }
  414. static int macvtap_release(struct inode *inode, struct file *file)
  415. {
  416. struct macvtap_queue *q = file->private_data;
  417. macvtap_put_queue(q);
  418. return 0;
  419. }
  420. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  421. {
  422. struct macvtap_queue *q = file->private_data;
  423. unsigned int mask = POLLERR;
  424. if (!q)
  425. goto out;
  426. mask = 0;
  427. poll_wait(file, &q->wq.wait, wait);
  428. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  429. mask |= POLLIN | POLLRDNORM;
  430. if (sock_writeable(&q->sk) ||
  431. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  432. sock_writeable(&q->sk)))
  433. mask |= POLLOUT | POLLWRNORM;
  434. out:
  435. return mask;
  436. }
  437. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  438. size_t len, size_t linear,
  439. int noblock, int *err)
  440. {
  441. struct sk_buff *skb;
  442. /* Under a page? Don't bother with paged skb. */
  443. if (prepad + len < PAGE_SIZE || !linear)
  444. linear = len;
  445. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  446. err, 0);
  447. if (!skb)
  448. return NULL;
  449. skb_reserve(skb, prepad);
  450. skb_put(skb, linear);
  451. skb->data_len = len - linear;
  452. skb->len += len - linear;
  453. return skb;
  454. }
  455. /*
  456. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  457. * be shared with the tun/tap driver.
  458. */
  459. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  460. struct virtio_net_hdr *vnet_hdr)
  461. {
  462. unsigned short gso_type = 0;
  463. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  464. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  465. case VIRTIO_NET_HDR_GSO_TCPV4:
  466. gso_type = SKB_GSO_TCPV4;
  467. break;
  468. case VIRTIO_NET_HDR_GSO_TCPV6:
  469. gso_type = SKB_GSO_TCPV6;
  470. break;
  471. case VIRTIO_NET_HDR_GSO_UDP:
  472. gso_type = SKB_GSO_UDP;
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  478. gso_type |= SKB_GSO_TCP_ECN;
  479. if (vnet_hdr->gso_size == 0)
  480. return -EINVAL;
  481. }
  482. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  483. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  484. vnet_hdr->csum_offset))
  485. return -EINVAL;
  486. }
  487. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  488. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  489. skb_shinfo(skb)->gso_type = gso_type;
  490. /* Header must be checked, and gso_segs computed. */
  491. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  492. skb_shinfo(skb)->gso_segs = 0;
  493. }
  494. return 0;
  495. }
  496. static void macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  497. struct virtio_net_hdr *vnet_hdr)
  498. {
  499. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  500. if (skb_is_gso(skb)) {
  501. struct skb_shared_info *sinfo = skb_shinfo(skb);
  502. /* This is a hint as to how much should be linear. */
  503. vnet_hdr->hdr_len = skb_headlen(skb);
  504. vnet_hdr->gso_size = sinfo->gso_size;
  505. if (sinfo->gso_type & SKB_GSO_TCPV4)
  506. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  507. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  508. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  509. else if (sinfo->gso_type & SKB_GSO_UDP)
  510. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  511. else
  512. BUG();
  513. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  514. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  515. } else
  516. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  517. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  518. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  519. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  520. vnet_hdr->csum_offset = skb->csum_offset;
  521. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  522. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  523. } /* else everything is zero */
  524. }
  525. /* Get packet from user space buffer */
  526. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  527. const struct iovec *iv, unsigned long total_len,
  528. size_t count, int noblock)
  529. {
  530. int good_linear = SKB_MAX_HEAD(NET_IP_ALIGN);
  531. struct sk_buff *skb;
  532. struct macvlan_dev *vlan;
  533. unsigned long len = total_len;
  534. int err;
  535. struct virtio_net_hdr vnet_hdr = { 0 };
  536. int vnet_hdr_len = 0;
  537. int copylen = 0;
  538. bool zerocopy = false;
  539. size_t linear;
  540. if (q->flags & IFF_VNET_HDR) {
  541. vnet_hdr_len = q->vnet_hdr_sz;
  542. err = -EINVAL;
  543. if (len < vnet_hdr_len)
  544. goto err;
  545. len -= vnet_hdr_len;
  546. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  547. sizeof(vnet_hdr));
  548. if (err < 0)
  549. goto err;
  550. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  551. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  552. vnet_hdr.hdr_len)
  553. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  554. vnet_hdr.csum_offset + 2;
  555. err = -EINVAL;
  556. if (vnet_hdr.hdr_len > len)
  557. goto err;
  558. }
  559. err = -EINVAL;
  560. if (unlikely(len < ETH_HLEN))
  561. goto err;
  562. err = -EMSGSIZE;
  563. if (unlikely(count > UIO_MAXIOV))
  564. goto err;
  565. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  566. copylen = vnet_hdr.hdr_len ? vnet_hdr.hdr_len : GOODCOPY_LEN;
  567. if (copylen > good_linear)
  568. copylen = good_linear;
  569. linear = copylen;
  570. if (iov_pages(iv, vnet_hdr_len + copylen, count)
  571. <= MAX_SKB_FRAGS)
  572. zerocopy = true;
  573. }
  574. if (!zerocopy) {
  575. copylen = len;
  576. if (vnet_hdr.hdr_len > good_linear)
  577. linear = good_linear;
  578. else
  579. linear = vnet_hdr.hdr_len;
  580. }
  581. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  582. linear, noblock, &err);
  583. if (!skb)
  584. goto err;
  585. if (zerocopy)
  586. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  587. else {
  588. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  589. len);
  590. if (!err && m && m->msg_control) {
  591. struct ubuf_info *uarg = m->msg_control;
  592. uarg->callback(uarg, false);
  593. }
  594. }
  595. if (err)
  596. goto err_kfree;
  597. skb_set_network_header(skb, ETH_HLEN);
  598. skb_reset_mac_header(skb);
  599. skb->protocol = eth_hdr(skb)->h_proto;
  600. if (vnet_hdr_len) {
  601. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  602. if (err)
  603. goto err_kfree;
  604. }
  605. skb_probe_transport_header(skb, ETH_HLEN);
  606. rcu_read_lock();
  607. vlan = rcu_dereference(q->vlan);
  608. /* copy skb_ubuf_info for callback when skb has no error */
  609. if (zerocopy) {
  610. skb_shinfo(skb)->destructor_arg = m->msg_control;
  611. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  612. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  613. }
  614. if (vlan) {
  615. skb->dev = vlan->dev;
  616. dev_queue_xmit(skb);
  617. } else {
  618. kfree_skb(skb);
  619. }
  620. rcu_read_unlock();
  621. return total_len;
  622. err_kfree:
  623. kfree_skb(skb);
  624. err:
  625. rcu_read_lock();
  626. vlan = rcu_dereference(q->vlan);
  627. if (vlan)
  628. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  629. rcu_read_unlock();
  630. return err;
  631. }
  632. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  633. unsigned long count, loff_t pos)
  634. {
  635. struct file *file = iocb->ki_filp;
  636. ssize_t result = -ENOLINK;
  637. struct macvtap_queue *q = file->private_data;
  638. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  639. file->f_flags & O_NONBLOCK);
  640. return result;
  641. }
  642. /* Put packet to the user space buffer */
  643. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  644. const struct sk_buff *skb,
  645. const struct iovec *iv, int len)
  646. {
  647. int ret;
  648. int vnet_hdr_len = 0;
  649. int vlan_offset = 0;
  650. int copied;
  651. if (q->flags & IFF_VNET_HDR) {
  652. struct virtio_net_hdr vnet_hdr;
  653. vnet_hdr_len = q->vnet_hdr_sz;
  654. if ((len -= vnet_hdr_len) < 0)
  655. return -EINVAL;
  656. macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  657. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  658. return -EFAULT;
  659. }
  660. copied = vnet_hdr_len;
  661. if (!vlan_tx_tag_present(skb))
  662. len = min_t(int, skb->len, len);
  663. else {
  664. int copy;
  665. struct {
  666. __be16 h_vlan_proto;
  667. __be16 h_vlan_TCI;
  668. } veth;
  669. veth.h_vlan_proto = skb->vlan_proto;
  670. veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
  671. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  672. len = min_t(int, skb->len + VLAN_HLEN, len);
  673. copy = min_t(int, vlan_offset, len);
  674. ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
  675. len -= copy;
  676. copied += copy;
  677. if (ret || !len)
  678. goto done;
  679. copy = min_t(int, sizeof(veth), len);
  680. ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
  681. len -= copy;
  682. copied += copy;
  683. if (ret || !len)
  684. goto done;
  685. }
  686. ret = skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
  687. copied += len;
  688. done:
  689. return ret ? ret : copied;
  690. }
  691. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  692. const struct iovec *iv, unsigned long len,
  693. int noblock)
  694. {
  695. DEFINE_WAIT(wait);
  696. struct sk_buff *skb;
  697. ssize_t ret = 0;
  698. while (len) {
  699. if (!noblock)
  700. prepare_to_wait(sk_sleep(&q->sk), &wait,
  701. TASK_INTERRUPTIBLE);
  702. /* Read frames from the queue */
  703. skb = skb_dequeue(&q->sk.sk_receive_queue);
  704. if (!skb) {
  705. if (noblock) {
  706. ret = -EAGAIN;
  707. break;
  708. }
  709. if (signal_pending(current)) {
  710. ret = -ERESTARTSYS;
  711. break;
  712. }
  713. /* Nothing to read, let's sleep */
  714. schedule();
  715. continue;
  716. }
  717. ret = macvtap_put_user(q, skb, iv, len);
  718. kfree_skb(skb);
  719. break;
  720. }
  721. if (!noblock)
  722. finish_wait(sk_sleep(&q->sk), &wait);
  723. return ret;
  724. }
  725. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  726. unsigned long count, loff_t pos)
  727. {
  728. struct file *file = iocb->ki_filp;
  729. struct macvtap_queue *q = file->private_data;
  730. ssize_t len, ret = 0;
  731. len = iov_length(iv, count);
  732. if (len < 0) {
  733. ret = -EINVAL;
  734. goto out;
  735. }
  736. ret = macvtap_do_read(q, iv, len, file->f_flags & O_NONBLOCK);
  737. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  738. if (ret > 0)
  739. iocb->ki_pos = ret;
  740. out:
  741. return ret;
  742. }
  743. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  744. {
  745. struct macvlan_dev *vlan;
  746. ASSERT_RTNL();
  747. vlan = rtnl_dereference(q->vlan);
  748. if (vlan)
  749. dev_hold(vlan->dev);
  750. return vlan;
  751. }
  752. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  753. {
  754. dev_put(vlan->dev);
  755. }
  756. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  757. {
  758. struct macvtap_queue *q = file->private_data;
  759. struct macvlan_dev *vlan;
  760. int ret;
  761. vlan = macvtap_get_vlan(q);
  762. if (!vlan)
  763. return -EINVAL;
  764. if (flags & IFF_ATTACH_QUEUE)
  765. ret = macvtap_enable_queue(vlan->dev, file, q);
  766. else if (flags & IFF_DETACH_QUEUE)
  767. ret = macvtap_disable_queue(q);
  768. else
  769. ret = -EINVAL;
  770. macvtap_put_vlan(vlan);
  771. return ret;
  772. }
  773. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  774. {
  775. struct macvlan_dev *vlan;
  776. netdev_features_t features;
  777. netdev_features_t feature_mask = 0;
  778. vlan = rtnl_dereference(q->vlan);
  779. if (!vlan)
  780. return -ENOLINK;
  781. features = vlan->dev->features;
  782. if (arg & TUN_F_CSUM) {
  783. feature_mask = NETIF_F_HW_CSUM;
  784. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  785. if (arg & TUN_F_TSO_ECN)
  786. feature_mask |= NETIF_F_TSO_ECN;
  787. if (arg & TUN_F_TSO4)
  788. feature_mask |= NETIF_F_TSO;
  789. if (arg & TUN_F_TSO6)
  790. feature_mask |= NETIF_F_TSO6;
  791. }
  792. if (arg & TUN_F_UFO)
  793. feature_mask |= NETIF_F_UFO;
  794. }
  795. /* tun/tap driver inverts the usage for TSO offloads, where
  796. * setting the TSO bit means that the userspace wants to
  797. * accept TSO frames and turning it off means that user space
  798. * does not support TSO.
  799. * For macvtap, we have to invert it to mean the same thing.
  800. * When user space turns off TSO, we turn off GSO/LRO so that
  801. * user-space will not receive TSO frames.
  802. */
  803. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  804. features |= RX_OFFLOADS;
  805. else
  806. features &= ~RX_OFFLOADS;
  807. /* tap_features are the same as features on tun/tap and
  808. * reflect user expectations.
  809. */
  810. vlan->tap_features = feature_mask;
  811. vlan->set_features = features;
  812. netdev_update_features(vlan->dev);
  813. return 0;
  814. }
  815. /*
  816. * provide compatibility with generic tun/tap interface
  817. */
  818. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  819. unsigned long arg)
  820. {
  821. struct macvtap_queue *q = file->private_data;
  822. struct macvlan_dev *vlan;
  823. void __user *argp = (void __user *)arg;
  824. struct ifreq __user *ifr = argp;
  825. unsigned int __user *up = argp;
  826. unsigned int u;
  827. int __user *sp = argp;
  828. int s;
  829. int ret;
  830. switch (cmd) {
  831. case TUNSETIFF:
  832. /* ignore the name, just look at flags */
  833. if (get_user(u, &ifr->ifr_flags))
  834. return -EFAULT;
  835. ret = 0;
  836. if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
  837. (IFF_NO_PI | IFF_TAP))
  838. ret = -EINVAL;
  839. else
  840. q->flags = u;
  841. return ret;
  842. case TUNGETIFF:
  843. rtnl_lock();
  844. vlan = macvtap_get_vlan(q);
  845. if (!vlan) {
  846. rtnl_unlock();
  847. return -ENOLINK;
  848. }
  849. ret = 0;
  850. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  851. put_user(q->flags, &ifr->ifr_flags))
  852. ret = -EFAULT;
  853. macvtap_put_vlan(vlan);
  854. rtnl_unlock();
  855. return ret;
  856. case TUNSETQUEUE:
  857. if (get_user(u, &ifr->ifr_flags))
  858. return -EFAULT;
  859. rtnl_lock();
  860. ret = macvtap_ioctl_set_queue(file, u);
  861. rtnl_unlock();
  862. return ret;
  863. case TUNGETFEATURES:
  864. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR |
  865. IFF_MULTI_QUEUE, up))
  866. return -EFAULT;
  867. return 0;
  868. case TUNSETSNDBUF:
  869. if (get_user(u, up))
  870. return -EFAULT;
  871. q->sk.sk_sndbuf = u;
  872. return 0;
  873. case TUNGETVNETHDRSZ:
  874. s = q->vnet_hdr_sz;
  875. if (put_user(s, sp))
  876. return -EFAULT;
  877. return 0;
  878. case TUNSETVNETHDRSZ:
  879. if (get_user(s, sp))
  880. return -EFAULT;
  881. if (s < (int)sizeof(struct virtio_net_hdr))
  882. return -EINVAL;
  883. q->vnet_hdr_sz = s;
  884. return 0;
  885. case TUNSETOFFLOAD:
  886. /* let the user check for future flags */
  887. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  888. TUN_F_TSO_ECN | TUN_F_UFO))
  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, 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");