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