macvtap.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. vnet_hdr->csum_offset = skb->csum_offset;
  530. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  531. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  532. } /* else everything is zero */
  533. }
  534. /* Get packet from user space buffer */
  535. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  536. const struct iovec *iv, unsigned long total_len,
  537. size_t count, int noblock)
  538. {
  539. int good_linear = SKB_MAX_HEAD(NET_IP_ALIGN);
  540. struct sk_buff *skb;
  541. struct macvlan_dev *vlan;
  542. unsigned long len = total_len;
  543. int err;
  544. struct virtio_net_hdr vnet_hdr = { 0 };
  545. int vnet_hdr_len = 0;
  546. int copylen = 0;
  547. bool zerocopy = false;
  548. size_t linear;
  549. if (q->flags & IFF_VNET_HDR) {
  550. vnet_hdr_len = q->vnet_hdr_sz;
  551. err = -EINVAL;
  552. if (len < vnet_hdr_len)
  553. goto err;
  554. len -= vnet_hdr_len;
  555. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  556. sizeof(vnet_hdr));
  557. if (err < 0)
  558. goto err;
  559. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  560. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  561. vnet_hdr.hdr_len)
  562. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  563. vnet_hdr.csum_offset + 2;
  564. err = -EINVAL;
  565. if (vnet_hdr.hdr_len > len)
  566. goto err;
  567. }
  568. err = -EINVAL;
  569. if (unlikely(len < ETH_HLEN))
  570. goto err;
  571. err = -EMSGSIZE;
  572. if (unlikely(count > UIO_MAXIOV))
  573. goto err;
  574. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  575. copylen = vnet_hdr.hdr_len ? vnet_hdr.hdr_len : GOODCOPY_LEN;
  576. if (copylen > good_linear)
  577. copylen = good_linear;
  578. linear = copylen;
  579. if (iov_pages(iv, vnet_hdr_len + copylen, count)
  580. <= MAX_SKB_FRAGS)
  581. zerocopy = true;
  582. }
  583. if (!zerocopy) {
  584. copylen = len;
  585. if (vnet_hdr.hdr_len > good_linear)
  586. linear = good_linear;
  587. else
  588. linear = vnet_hdr.hdr_len;
  589. }
  590. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  591. linear, noblock, &err);
  592. if (!skb)
  593. goto err;
  594. if (zerocopy)
  595. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  596. else {
  597. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  598. len);
  599. if (!err && m && m->msg_control) {
  600. struct ubuf_info *uarg = m->msg_control;
  601. uarg->callback(uarg, false);
  602. }
  603. }
  604. if (err)
  605. goto err_kfree;
  606. skb_set_network_header(skb, ETH_HLEN);
  607. skb_reset_mac_header(skb);
  608. skb->protocol = eth_hdr(skb)->h_proto;
  609. if (vnet_hdr_len) {
  610. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  611. if (err)
  612. goto err_kfree;
  613. }
  614. skb_probe_transport_header(skb, ETH_HLEN);
  615. rcu_read_lock();
  616. vlan = rcu_dereference(q->vlan);
  617. /* copy skb_ubuf_info for callback when skb has no error */
  618. if (zerocopy) {
  619. skb_shinfo(skb)->destructor_arg = m->msg_control;
  620. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  621. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  622. }
  623. if (vlan) {
  624. skb->dev = vlan->dev;
  625. dev_queue_xmit(skb);
  626. } else {
  627. kfree_skb(skb);
  628. }
  629. rcu_read_unlock();
  630. return total_len;
  631. err_kfree:
  632. kfree_skb(skb);
  633. err:
  634. rcu_read_lock();
  635. vlan = rcu_dereference(q->vlan);
  636. if (vlan)
  637. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  638. rcu_read_unlock();
  639. return err;
  640. }
  641. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  642. unsigned long count, loff_t pos)
  643. {
  644. struct file *file = iocb->ki_filp;
  645. ssize_t result = -ENOLINK;
  646. struct macvtap_queue *q = file->private_data;
  647. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  648. file->f_flags & O_NONBLOCK);
  649. return result;
  650. }
  651. /* Put packet to the user space buffer */
  652. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  653. const struct sk_buff *skb,
  654. const struct iovec *iv, int len)
  655. {
  656. int ret;
  657. int vnet_hdr_len = 0;
  658. int vlan_offset = 0;
  659. int copied, total;
  660. if (q->flags & IFF_VNET_HDR) {
  661. struct virtio_net_hdr vnet_hdr;
  662. vnet_hdr_len = q->vnet_hdr_sz;
  663. if ((len -= vnet_hdr_len) < 0)
  664. return -EINVAL;
  665. macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  666. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  667. return -EFAULT;
  668. }
  669. total = copied = vnet_hdr_len;
  670. total += skb->len;
  671. if (!vlan_tx_tag_present(skb))
  672. len = min_t(int, skb->len, len);
  673. else {
  674. int copy;
  675. struct {
  676. __be16 h_vlan_proto;
  677. __be16 h_vlan_TCI;
  678. } veth;
  679. veth.h_vlan_proto = skb->vlan_proto;
  680. veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
  681. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  682. len = min_t(int, skb->len + VLAN_HLEN, len);
  683. total += VLAN_HLEN;
  684. copy = min_t(int, vlan_offset, len);
  685. ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
  686. len -= copy;
  687. copied += copy;
  688. if (ret || !len)
  689. goto done;
  690. copy = min_t(int, sizeof(veth), len);
  691. ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
  692. len -= copy;
  693. copied += copy;
  694. if (ret || !len)
  695. goto done;
  696. }
  697. ret = skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
  698. done:
  699. return ret ? ret : total;
  700. }
  701. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  702. const struct iovec *iv, unsigned long len,
  703. int noblock)
  704. {
  705. DEFINE_WAIT(wait);
  706. struct sk_buff *skb;
  707. ssize_t ret = 0;
  708. while (len) {
  709. if (!noblock)
  710. prepare_to_wait(sk_sleep(&q->sk), &wait,
  711. TASK_INTERRUPTIBLE);
  712. /* Read frames from the queue */
  713. skb = skb_dequeue(&q->sk.sk_receive_queue);
  714. if (!skb) {
  715. if (noblock) {
  716. ret = -EAGAIN;
  717. break;
  718. }
  719. if (signal_pending(current)) {
  720. ret = -ERESTARTSYS;
  721. break;
  722. }
  723. /* Nothing to read, let's sleep */
  724. schedule();
  725. continue;
  726. }
  727. ret = macvtap_put_user(q, skb, iv, len);
  728. kfree_skb(skb);
  729. break;
  730. }
  731. if (!noblock)
  732. finish_wait(sk_sleep(&q->sk), &wait);
  733. return ret;
  734. }
  735. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  736. unsigned long count, loff_t pos)
  737. {
  738. struct file *file = iocb->ki_filp;
  739. struct macvtap_queue *q = file->private_data;
  740. ssize_t len, ret = 0;
  741. len = iov_length(iv, count);
  742. if (len < 0) {
  743. ret = -EINVAL;
  744. goto out;
  745. }
  746. ret = macvtap_do_read(q, iv, len, file->f_flags & O_NONBLOCK);
  747. ret = min_t(ssize_t, ret, len);
  748. if (ret > 0)
  749. iocb->ki_pos = ret;
  750. out:
  751. return ret;
  752. }
  753. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  754. {
  755. struct macvlan_dev *vlan;
  756. ASSERT_RTNL();
  757. vlan = rtnl_dereference(q->vlan);
  758. if (vlan)
  759. dev_hold(vlan->dev);
  760. return vlan;
  761. }
  762. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  763. {
  764. dev_put(vlan->dev);
  765. }
  766. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  767. {
  768. struct macvtap_queue *q = file->private_data;
  769. struct macvlan_dev *vlan;
  770. int ret;
  771. vlan = macvtap_get_vlan(q);
  772. if (!vlan)
  773. return -EINVAL;
  774. if (flags & IFF_ATTACH_QUEUE)
  775. ret = macvtap_enable_queue(vlan->dev, file, q);
  776. else if (flags & IFF_DETACH_QUEUE)
  777. ret = macvtap_disable_queue(q);
  778. else
  779. ret = -EINVAL;
  780. macvtap_put_vlan(vlan);
  781. return ret;
  782. }
  783. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  784. {
  785. struct macvlan_dev *vlan;
  786. netdev_features_t features;
  787. netdev_features_t feature_mask = 0;
  788. vlan = rtnl_dereference(q->vlan);
  789. if (!vlan)
  790. return -ENOLINK;
  791. features = vlan->dev->features;
  792. if (arg & TUN_F_CSUM) {
  793. feature_mask = NETIF_F_HW_CSUM;
  794. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  795. if (arg & TUN_F_TSO_ECN)
  796. feature_mask |= NETIF_F_TSO_ECN;
  797. if (arg & TUN_F_TSO4)
  798. feature_mask |= NETIF_F_TSO;
  799. if (arg & TUN_F_TSO6)
  800. feature_mask |= NETIF_F_TSO6;
  801. }
  802. }
  803. /* tun/tap driver inverts the usage for TSO offloads, where
  804. * setting the TSO bit means that the userspace wants to
  805. * accept TSO frames and turning it off means that user space
  806. * does not support TSO.
  807. * For macvtap, we have to invert it to mean the same thing.
  808. * When user space turns off TSO, we turn off GSO/LRO so that
  809. * user-space will not receive TSO frames.
  810. */
  811. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
  812. features |= RX_OFFLOADS;
  813. else
  814. features &= ~RX_OFFLOADS;
  815. /* tap_features are the same as features on tun/tap and
  816. * reflect user expectations.
  817. */
  818. vlan->tap_features = feature_mask;
  819. vlan->set_features = features;
  820. netdev_update_features(vlan->dev);
  821. return 0;
  822. }
  823. /*
  824. * provide compatibility with generic tun/tap interface
  825. */
  826. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  827. unsigned long arg)
  828. {
  829. struct macvtap_queue *q = file->private_data;
  830. struct macvlan_dev *vlan;
  831. void __user *argp = (void __user *)arg;
  832. struct ifreq __user *ifr = argp;
  833. unsigned int __user *up = argp;
  834. unsigned int u;
  835. int __user *sp = argp;
  836. int s;
  837. int ret;
  838. switch (cmd) {
  839. case TUNSETIFF:
  840. /* ignore the name, just look at flags */
  841. if (get_user(u, &ifr->ifr_flags))
  842. return -EFAULT;
  843. ret = 0;
  844. if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
  845. (IFF_NO_PI | IFF_TAP))
  846. ret = -EINVAL;
  847. else
  848. q->flags = u;
  849. return ret;
  850. case TUNGETIFF:
  851. rtnl_lock();
  852. vlan = macvtap_get_vlan(q);
  853. if (!vlan) {
  854. rtnl_unlock();
  855. return -ENOLINK;
  856. }
  857. ret = 0;
  858. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  859. put_user(q->flags, &ifr->ifr_flags))
  860. ret = -EFAULT;
  861. macvtap_put_vlan(vlan);
  862. rtnl_unlock();
  863. return ret;
  864. case TUNSETQUEUE:
  865. if (get_user(u, &ifr->ifr_flags))
  866. return -EFAULT;
  867. rtnl_lock();
  868. ret = macvtap_ioctl_set_queue(file, u);
  869. rtnl_unlock();
  870. return ret;
  871. case TUNGETFEATURES:
  872. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR |
  873. IFF_MULTI_QUEUE, up))
  874. return -EFAULT;
  875. return 0;
  876. case TUNSETSNDBUF:
  877. if (get_user(u, up))
  878. return -EFAULT;
  879. q->sk.sk_sndbuf = u;
  880. return 0;
  881. case TUNGETVNETHDRSZ:
  882. s = q->vnet_hdr_sz;
  883. if (put_user(s, sp))
  884. return -EFAULT;
  885. return 0;
  886. case TUNSETVNETHDRSZ:
  887. if (get_user(s, sp))
  888. return -EFAULT;
  889. if (s < (int)sizeof(struct virtio_net_hdr))
  890. return -EINVAL;
  891. q->vnet_hdr_sz = s;
  892. return 0;
  893. case TUNSETOFFLOAD:
  894. /* let the user check for future flags */
  895. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  896. TUN_F_TSO_ECN))
  897. return -EINVAL;
  898. rtnl_lock();
  899. ret = set_offload(q, arg);
  900. rtnl_unlock();
  901. return ret;
  902. default:
  903. return -EINVAL;
  904. }
  905. }
  906. #ifdef CONFIG_COMPAT
  907. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  908. unsigned long arg)
  909. {
  910. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  911. }
  912. #endif
  913. static const struct file_operations macvtap_fops = {
  914. .owner = THIS_MODULE,
  915. .open = macvtap_open,
  916. .release = macvtap_release,
  917. .aio_read = macvtap_aio_read,
  918. .aio_write = macvtap_aio_write,
  919. .poll = macvtap_poll,
  920. .llseek = no_llseek,
  921. .unlocked_ioctl = macvtap_ioctl,
  922. #ifdef CONFIG_COMPAT
  923. .compat_ioctl = macvtap_compat_ioctl,
  924. #endif
  925. };
  926. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  927. struct msghdr *m, size_t total_len)
  928. {
  929. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  930. return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
  931. m->msg_flags & MSG_DONTWAIT);
  932. }
  933. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  934. struct msghdr *m, size_t total_len,
  935. int flags)
  936. {
  937. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  938. int ret;
  939. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  940. return -EINVAL;
  941. ret = macvtap_do_read(q, m->msg_iov, total_len,
  942. flags & MSG_DONTWAIT);
  943. if (ret > total_len) {
  944. m->msg_flags |= MSG_TRUNC;
  945. ret = flags & MSG_TRUNC ? ret : total_len;
  946. }
  947. return ret;
  948. }
  949. /* Ops structure to mimic raw sockets with tun */
  950. static const struct proto_ops macvtap_socket_ops = {
  951. .sendmsg = macvtap_sendmsg,
  952. .recvmsg = macvtap_recvmsg,
  953. };
  954. /* Get an underlying socket object from tun file. Returns error unless file is
  955. * attached to a device. The returned object works like a packet socket, it
  956. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  957. * holding a reference to the file for as long as the socket is in use. */
  958. struct socket *macvtap_get_socket(struct file *file)
  959. {
  960. struct macvtap_queue *q;
  961. if (file->f_op != &macvtap_fops)
  962. return ERR_PTR(-EINVAL);
  963. q = file->private_data;
  964. if (!q)
  965. return ERR_PTR(-EBADFD);
  966. return &q->sock;
  967. }
  968. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  969. static int macvtap_device_event(struct notifier_block *unused,
  970. unsigned long event, void *ptr)
  971. {
  972. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  973. struct macvlan_dev *vlan;
  974. struct device *classdev;
  975. dev_t devt;
  976. int err;
  977. if (dev->rtnl_link_ops != &macvtap_link_ops)
  978. return NOTIFY_DONE;
  979. vlan = netdev_priv(dev);
  980. switch (event) {
  981. case NETDEV_REGISTER:
  982. /* Create the device node here after the network device has
  983. * been registered but before register_netdevice has
  984. * finished running.
  985. */
  986. err = macvtap_get_minor(vlan);
  987. if (err)
  988. return notifier_from_errno(err);
  989. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  990. classdev = device_create(macvtap_class, &dev->dev, devt,
  991. dev, "tap%d", dev->ifindex);
  992. if (IS_ERR(classdev)) {
  993. macvtap_free_minor(vlan);
  994. return notifier_from_errno(PTR_ERR(classdev));
  995. }
  996. break;
  997. case NETDEV_UNREGISTER:
  998. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  999. device_destroy(macvtap_class, devt);
  1000. macvtap_free_minor(vlan);
  1001. break;
  1002. }
  1003. return NOTIFY_DONE;
  1004. }
  1005. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1006. .notifier_call = macvtap_device_event,
  1007. };
  1008. static int macvtap_init(void)
  1009. {
  1010. int err;
  1011. err = alloc_chrdev_region(&macvtap_major, 0,
  1012. MACVTAP_NUM_DEVS, "macvtap");
  1013. if (err)
  1014. goto out1;
  1015. cdev_init(&macvtap_cdev, &macvtap_fops);
  1016. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1017. if (err)
  1018. goto out2;
  1019. macvtap_class = class_create(THIS_MODULE, "macvtap");
  1020. if (IS_ERR(macvtap_class)) {
  1021. err = PTR_ERR(macvtap_class);
  1022. goto out3;
  1023. }
  1024. err = register_netdevice_notifier(&macvtap_notifier_block);
  1025. if (err)
  1026. goto out4;
  1027. err = macvlan_link_register(&macvtap_link_ops);
  1028. if (err)
  1029. goto out5;
  1030. return 0;
  1031. out5:
  1032. unregister_netdevice_notifier(&macvtap_notifier_block);
  1033. out4:
  1034. class_unregister(macvtap_class);
  1035. out3:
  1036. cdev_del(&macvtap_cdev);
  1037. out2:
  1038. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1039. out1:
  1040. return err;
  1041. }
  1042. module_init(macvtap_init);
  1043. static void macvtap_exit(void)
  1044. {
  1045. rtnl_link_unregister(&macvtap_link_ops);
  1046. unregister_netdevice_notifier(&macvtap_notifier_block);
  1047. class_unregister(macvtap_class);
  1048. cdev_del(&macvtap_cdev);
  1049. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1050. }
  1051. module_exit(macvtap_exit);
  1052. MODULE_ALIAS_RTNL_LINK("macvtap");
  1053. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1054. MODULE_LICENSE("GPL");