macvtap.c 29 KB

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