macvtap.c 30 KB

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