macvtap.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/nsproxy.h>
  5. #include <linux/compat.h>
  6. #include <linux/if_tun.h>
  7. #include <linux/module.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/cache.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.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. * TODO: multiqueue support is currently not implemented, even though
  32. * macvtap is basically prepared for that. We will need to add this
  33. * here as well as in virtio-net and qemu to get line rate on 10gbit
  34. * adapters from a guest.
  35. */
  36. struct macvtap_queue {
  37. struct sock sk;
  38. struct socket sock;
  39. struct socket_wq wq;
  40. int vnet_hdr_sz;
  41. struct macvlan_dev __rcu *vlan;
  42. struct file *file;
  43. unsigned int flags;
  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. /*
  62. * RCU usage:
  63. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  64. * pointers from one to the other can only be read while rcu_read_lock
  65. * or macvtap_lock is held.
  66. *
  67. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  68. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  69. * q->vlan becomes inaccessible. When the files gets closed,
  70. * macvtap_get_queue() fails.
  71. *
  72. * There may still be references to the struct sock inside of the
  73. * queue from outbound SKBs, but these never reference back to the
  74. * file or the dev. The data structure is freed through __sk_free
  75. * when both our references and any pending SKBs are gone.
  76. */
  77. static DEFINE_SPINLOCK(macvtap_lock);
  78. /*
  79. * get_slot: return a [unused/occupied] slot in vlan->taps[]:
  80. * - if 'q' is NULL, return the first empty slot;
  81. * - otherwise, return the slot this pointer occupies.
  82. */
  83. static int get_slot(struct macvlan_dev *vlan, struct macvtap_queue *q)
  84. {
  85. int i;
  86. for (i = 0; i < MAX_MACVTAP_QUEUES; i++) {
  87. if (rcu_dereference(vlan->taps[i]) == q)
  88. return i;
  89. }
  90. /* Should never happen */
  91. BUG_ON(1);
  92. }
  93. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  94. struct macvtap_queue *q)
  95. {
  96. struct macvlan_dev *vlan = netdev_priv(dev);
  97. int index;
  98. int err = -EBUSY;
  99. spin_lock(&macvtap_lock);
  100. if (vlan->numvtaps == MAX_MACVTAP_QUEUES)
  101. goto out;
  102. err = 0;
  103. index = get_slot(vlan, NULL);
  104. rcu_assign_pointer(q->vlan, vlan);
  105. rcu_assign_pointer(vlan->taps[index], q);
  106. sock_hold(&q->sk);
  107. q->file = file;
  108. file->private_data = q;
  109. vlan->numvtaps++;
  110. out:
  111. spin_unlock(&macvtap_lock);
  112. return err;
  113. }
  114. /*
  115. * The file owning the queue got closed, give up both
  116. * the reference that the files holds as well as the
  117. * one from the macvlan_dev if that still exists.
  118. *
  119. * Using the spinlock makes sure that we don't get
  120. * to the queue again after destroying it.
  121. */
  122. static void macvtap_put_queue(struct macvtap_queue *q)
  123. {
  124. struct macvlan_dev *vlan;
  125. spin_lock(&macvtap_lock);
  126. vlan = rcu_dereference_protected(q->vlan,
  127. lockdep_is_held(&macvtap_lock));
  128. if (vlan) {
  129. int index = get_slot(vlan, q);
  130. RCU_INIT_POINTER(vlan->taps[index], NULL);
  131. RCU_INIT_POINTER(q->vlan, NULL);
  132. sock_put(&q->sk);
  133. --vlan->numvtaps;
  134. }
  135. spin_unlock(&macvtap_lock);
  136. synchronize_rcu();
  137. sock_put(&q->sk);
  138. }
  139. /*
  140. * Select a queue based on the rxq of the device on which this packet
  141. * arrived. If the incoming device is not mq, calculate a flow hash
  142. * to select a queue. If all fails, find the first available queue.
  143. * Cache vlan->numvtaps since it can become zero during the execution
  144. * of this function.
  145. */
  146. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  147. struct sk_buff *skb)
  148. {
  149. struct macvlan_dev *vlan = netdev_priv(dev);
  150. struct macvtap_queue *tap = NULL;
  151. int numvtaps = vlan->numvtaps;
  152. __u32 rxq;
  153. if (!numvtaps)
  154. goto out;
  155. /* Check if we can use flow to select a queue */
  156. rxq = skb_get_rxhash(skb);
  157. if (rxq) {
  158. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  159. if (tap)
  160. goto out;
  161. }
  162. if (likely(skb_rx_queue_recorded(skb))) {
  163. rxq = skb_get_rx_queue(skb);
  164. while (unlikely(rxq >= numvtaps))
  165. rxq -= numvtaps;
  166. tap = rcu_dereference(vlan->taps[rxq]);
  167. if (tap)
  168. goto out;
  169. }
  170. /* Everything failed - find first available queue */
  171. for (rxq = 0; rxq < MAX_MACVTAP_QUEUES; rxq++) {
  172. tap = rcu_dereference(vlan->taps[rxq]);
  173. if (tap)
  174. break;
  175. }
  176. out:
  177. return tap;
  178. }
  179. /*
  180. * The net_device is going away, give up the reference
  181. * that it holds on all queues and safely set the pointer
  182. * from the queues to NULL.
  183. */
  184. static void macvtap_del_queues(struct net_device *dev)
  185. {
  186. struct macvlan_dev *vlan = netdev_priv(dev);
  187. struct macvtap_queue *q, *qlist[MAX_MACVTAP_QUEUES];
  188. int i, j = 0;
  189. /* macvtap_put_queue can free some slots, so go through all slots */
  190. spin_lock(&macvtap_lock);
  191. for (i = 0; i < MAX_MACVTAP_QUEUES && vlan->numvtaps; i++) {
  192. q = rcu_dereference_protected(vlan->taps[i],
  193. lockdep_is_held(&macvtap_lock));
  194. if (q) {
  195. qlist[j++] = q;
  196. RCU_INIT_POINTER(vlan->taps[i], NULL);
  197. RCU_INIT_POINTER(q->vlan, NULL);
  198. vlan->numvtaps--;
  199. }
  200. }
  201. BUG_ON(vlan->numvtaps != 0);
  202. /* guarantee that any future macvtap_set_queue will fail */
  203. vlan->numvtaps = MAX_MACVTAP_QUEUES;
  204. spin_unlock(&macvtap_lock);
  205. synchronize_rcu();
  206. for (--j; j >= 0; j--)
  207. sock_put(&qlist[j]->sk);
  208. }
  209. /*
  210. * Forward happens for data that gets sent from one macvlan
  211. * endpoint to another one in bridge mode. We just take
  212. * the skb and put it into the receive queue.
  213. */
  214. static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
  215. {
  216. struct macvtap_queue *q = macvtap_get_queue(dev, skb);
  217. if (!q)
  218. goto drop;
  219. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  220. goto drop;
  221. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  222. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  223. return NET_RX_SUCCESS;
  224. drop:
  225. kfree_skb(skb);
  226. return NET_RX_DROP;
  227. }
  228. /*
  229. * Receive is for data from the external interface (lowerdev),
  230. * in case of macvtap, we can treat that the same way as
  231. * forward, which macvlan cannot.
  232. */
  233. static int macvtap_receive(struct sk_buff *skb)
  234. {
  235. skb_push(skb, ETH_HLEN);
  236. return macvtap_forward(skb->dev, skb);
  237. }
  238. static int macvtap_get_minor(struct macvlan_dev *vlan)
  239. {
  240. int retval = -ENOMEM;
  241. int id;
  242. mutex_lock(&minor_lock);
  243. if (idr_pre_get(&minor_idr, GFP_KERNEL) == 0)
  244. goto exit;
  245. retval = idr_get_new_above(&minor_idr, vlan, 1, &id);
  246. if (retval < 0) {
  247. if (retval == -EAGAIN)
  248. retval = -ENOMEM;
  249. goto exit;
  250. }
  251. if (id < MACVTAP_NUM_DEVS) {
  252. vlan->minor = id;
  253. } else {
  254. printk(KERN_ERR "too many macvtap devices\n");
  255. retval = -EINVAL;
  256. idr_remove(&minor_idr, id);
  257. }
  258. exit:
  259. mutex_unlock(&minor_lock);
  260. return retval;
  261. }
  262. static void macvtap_free_minor(struct macvlan_dev *vlan)
  263. {
  264. mutex_lock(&minor_lock);
  265. if (vlan->minor) {
  266. idr_remove(&minor_idr, vlan->minor);
  267. vlan->minor = 0;
  268. }
  269. mutex_unlock(&minor_lock);
  270. }
  271. static struct net_device *dev_get_by_macvtap_minor(int minor)
  272. {
  273. struct net_device *dev = NULL;
  274. struct macvlan_dev *vlan;
  275. mutex_lock(&minor_lock);
  276. vlan = idr_find(&minor_idr, minor);
  277. if (vlan) {
  278. dev = vlan->dev;
  279. dev_hold(dev);
  280. }
  281. mutex_unlock(&minor_lock);
  282. return dev;
  283. }
  284. static int macvtap_newlink(struct net *src_net,
  285. struct net_device *dev,
  286. struct nlattr *tb[],
  287. struct nlattr *data[])
  288. {
  289. /* Don't put anything that may fail after macvlan_common_newlink
  290. * because we can't undo what it does.
  291. */
  292. return macvlan_common_newlink(src_net, dev, tb, data,
  293. macvtap_receive, macvtap_forward);
  294. }
  295. static void macvtap_dellink(struct net_device *dev,
  296. struct list_head *head)
  297. {
  298. macvtap_del_queues(dev);
  299. macvlan_dellink(dev, head);
  300. }
  301. static void macvtap_setup(struct net_device *dev)
  302. {
  303. macvlan_common_setup(dev);
  304. dev->tx_queue_len = TUN_READQ_SIZE;
  305. }
  306. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  307. .kind = "macvtap",
  308. .setup = macvtap_setup,
  309. .newlink = macvtap_newlink,
  310. .dellink = macvtap_dellink,
  311. };
  312. static void macvtap_sock_write_space(struct sock *sk)
  313. {
  314. wait_queue_head_t *wqueue;
  315. if (!sock_writeable(sk) ||
  316. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  317. return;
  318. wqueue = sk_sleep(sk);
  319. if (wqueue && waitqueue_active(wqueue))
  320. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  321. }
  322. static void macvtap_sock_destruct(struct sock *sk)
  323. {
  324. skb_queue_purge(&sk->sk_receive_queue);
  325. }
  326. static int macvtap_open(struct inode *inode, struct file *file)
  327. {
  328. struct net *net = current->nsproxy->net_ns;
  329. struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
  330. struct macvtap_queue *q;
  331. int err;
  332. err = -ENODEV;
  333. if (!dev)
  334. goto out;
  335. err = -ENOMEM;
  336. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  337. &macvtap_proto);
  338. if (!q)
  339. goto out;
  340. q->sock.wq = &q->wq;
  341. init_waitqueue_head(&q->wq.wait);
  342. q->sock.type = SOCK_RAW;
  343. q->sock.state = SS_CONNECTED;
  344. q->sock.file = file;
  345. q->sock.ops = &macvtap_socket_ops;
  346. sock_init_data(&q->sock, &q->sk);
  347. q->sk.sk_write_space = macvtap_sock_write_space;
  348. q->sk.sk_destruct = macvtap_sock_destruct;
  349. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  350. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  351. /*
  352. * so far only KVM virtio_net uses macvtap, enable zero copy between
  353. * guest kernel and host kernel when lower device supports zerocopy
  354. *
  355. * The macvlan supports zerocopy iff the lower device supports zero
  356. * copy so we don't have to look at the lower device directly.
  357. */
  358. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  359. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  360. err = macvtap_set_queue(dev, file, q);
  361. if (err)
  362. sock_put(&q->sk);
  363. out:
  364. if (dev)
  365. dev_put(dev);
  366. return err;
  367. }
  368. static int macvtap_release(struct inode *inode, struct file *file)
  369. {
  370. struct macvtap_queue *q = file->private_data;
  371. macvtap_put_queue(q);
  372. return 0;
  373. }
  374. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  375. {
  376. struct macvtap_queue *q = file->private_data;
  377. unsigned int mask = POLLERR;
  378. if (!q)
  379. goto out;
  380. mask = 0;
  381. poll_wait(file, &q->wq.wait, wait);
  382. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  383. mask |= POLLIN | POLLRDNORM;
  384. if (sock_writeable(&q->sk) ||
  385. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  386. sock_writeable(&q->sk)))
  387. mask |= POLLOUT | POLLWRNORM;
  388. out:
  389. return mask;
  390. }
  391. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  392. size_t len, size_t linear,
  393. int noblock, int *err)
  394. {
  395. struct sk_buff *skb;
  396. /* Under a page? Don't bother with paged skb. */
  397. if (prepad + len < PAGE_SIZE || !linear)
  398. linear = len;
  399. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  400. err);
  401. if (!skb)
  402. return NULL;
  403. skb_reserve(skb, prepad);
  404. skb_put(skb, linear);
  405. skb->data_len = len - linear;
  406. skb->len += len - linear;
  407. return skb;
  408. }
  409. /* set skb frags from iovec, this can move to core network code for reuse */
  410. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  411. int offset, size_t count)
  412. {
  413. int len = iov_length(from, count) - offset;
  414. int copy = skb_headlen(skb);
  415. int size, offset1 = 0;
  416. int i = 0;
  417. /* Skip over from offset */
  418. while (count && (offset >= from->iov_len)) {
  419. offset -= from->iov_len;
  420. ++from;
  421. --count;
  422. }
  423. /* copy up to skb headlen */
  424. while (count && (copy > 0)) {
  425. size = min_t(unsigned int, copy, from->iov_len - offset);
  426. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  427. size))
  428. return -EFAULT;
  429. if (copy > size) {
  430. ++from;
  431. --count;
  432. offset = 0;
  433. } else
  434. offset += size;
  435. copy -= size;
  436. offset1 += size;
  437. }
  438. if (len == offset1)
  439. return 0;
  440. while (count--) {
  441. struct page *page[MAX_SKB_FRAGS];
  442. int num_pages;
  443. unsigned long base;
  444. unsigned long truesize;
  445. len = from->iov_len - offset;
  446. if (!len) {
  447. offset = 0;
  448. ++from;
  449. continue;
  450. }
  451. base = (unsigned long)from->iov_base + offset;
  452. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  453. if (i + size > MAX_SKB_FRAGS)
  454. return -EMSGSIZE;
  455. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  456. if (num_pages != size) {
  457. for (i = 0; i < num_pages; i++)
  458. put_page(page[i]);
  459. return -EFAULT;
  460. }
  461. truesize = size * PAGE_SIZE;
  462. skb->data_len += len;
  463. skb->len += len;
  464. skb->truesize += truesize;
  465. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  466. while (len) {
  467. int off = base & ~PAGE_MASK;
  468. int size = min_t(int, len, PAGE_SIZE - off);
  469. __skb_fill_page_desc(skb, i, page[i], off, size);
  470. skb_shinfo(skb)->nr_frags++;
  471. /* increase sk_wmem_alloc */
  472. base += size;
  473. len -= size;
  474. i++;
  475. }
  476. offset = 0;
  477. ++from;
  478. }
  479. return 0;
  480. }
  481. /*
  482. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  483. * be shared with the tun/tap driver.
  484. */
  485. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  486. struct virtio_net_hdr *vnet_hdr)
  487. {
  488. unsigned short gso_type = 0;
  489. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  490. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  491. case VIRTIO_NET_HDR_GSO_TCPV4:
  492. gso_type = SKB_GSO_TCPV4;
  493. break;
  494. case VIRTIO_NET_HDR_GSO_TCPV6:
  495. gso_type = SKB_GSO_TCPV6;
  496. break;
  497. case VIRTIO_NET_HDR_GSO_UDP:
  498. gso_type = SKB_GSO_UDP;
  499. break;
  500. default:
  501. return -EINVAL;
  502. }
  503. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  504. gso_type |= SKB_GSO_TCP_ECN;
  505. if (vnet_hdr->gso_size == 0)
  506. return -EINVAL;
  507. }
  508. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  509. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  510. vnet_hdr->csum_offset))
  511. return -EINVAL;
  512. }
  513. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  514. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  515. skb_shinfo(skb)->gso_type = gso_type;
  516. /* Header must be checked, and gso_segs computed. */
  517. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  518. skb_shinfo(skb)->gso_segs = 0;
  519. }
  520. return 0;
  521. }
  522. static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  523. struct virtio_net_hdr *vnet_hdr)
  524. {
  525. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  526. if (skb_is_gso(skb)) {
  527. struct skb_shared_info *sinfo = skb_shinfo(skb);
  528. /* This is a hint as to how much should be linear. */
  529. vnet_hdr->hdr_len = skb_headlen(skb);
  530. vnet_hdr->gso_size = sinfo->gso_size;
  531. if (sinfo->gso_type & SKB_GSO_TCPV4)
  532. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  533. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  534. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  535. else if (sinfo->gso_type & SKB_GSO_UDP)
  536. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  537. else
  538. BUG();
  539. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  540. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  541. } else
  542. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  543. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  544. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  545. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  546. vnet_hdr->csum_offset = skb->csum_offset;
  547. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  548. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  549. } /* else everything is zero */
  550. return 0;
  551. }
  552. /* Get packet from user space buffer */
  553. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  554. const struct iovec *iv, unsigned long total_len,
  555. size_t count, int noblock)
  556. {
  557. struct sk_buff *skb;
  558. struct macvlan_dev *vlan;
  559. unsigned long len = total_len;
  560. int err;
  561. struct virtio_net_hdr vnet_hdr = { 0 };
  562. int vnet_hdr_len = 0;
  563. int copylen = 0;
  564. bool zerocopy = false;
  565. if (q->flags & IFF_VNET_HDR) {
  566. vnet_hdr_len = q->vnet_hdr_sz;
  567. err = -EINVAL;
  568. if (len < vnet_hdr_len)
  569. goto err;
  570. len -= vnet_hdr_len;
  571. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  572. sizeof(vnet_hdr));
  573. if (err < 0)
  574. goto err;
  575. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  576. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  577. vnet_hdr.hdr_len)
  578. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  579. vnet_hdr.csum_offset + 2;
  580. err = -EINVAL;
  581. if (vnet_hdr.hdr_len > len)
  582. goto err;
  583. }
  584. err = -EINVAL;
  585. if (unlikely(len < ETH_HLEN))
  586. goto err;
  587. err = -EMSGSIZE;
  588. if (unlikely(count > UIO_MAXIOV))
  589. goto err;
  590. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
  591. zerocopy = true;
  592. if (zerocopy) {
  593. /* Userspace may produce vectors with count greater than
  594. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  595. * to let the rest of data to be fit in the frags.
  596. */
  597. if (count > MAX_SKB_FRAGS) {
  598. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  599. if (copylen < vnet_hdr_len)
  600. copylen = 0;
  601. else
  602. copylen -= vnet_hdr_len;
  603. }
  604. /* There are 256 bytes to be copied in skb, so there is enough
  605. * room for skb expand head in case it is used.
  606. * The rest buffer is mapped from userspace.
  607. */
  608. if (copylen < vnet_hdr.hdr_len)
  609. copylen = vnet_hdr.hdr_len;
  610. if (!copylen)
  611. copylen = GOODCOPY_LEN;
  612. } else
  613. copylen = len;
  614. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  615. vnet_hdr.hdr_len, noblock, &err);
  616. if (!skb)
  617. goto err;
  618. if (zerocopy)
  619. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  620. else
  621. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  622. len);
  623. if (err)
  624. goto err_kfree;
  625. skb_set_network_header(skb, ETH_HLEN);
  626. skb_reset_mac_header(skb);
  627. skb->protocol = eth_hdr(skb)->h_proto;
  628. if (vnet_hdr_len) {
  629. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  630. if (err)
  631. goto err_kfree;
  632. }
  633. rcu_read_lock_bh();
  634. vlan = rcu_dereference_bh(q->vlan);
  635. /* copy skb_ubuf_info for callback when skb has no error */
  636. if (zerocopy) {
  637. skb_shinfo(skb)->destructor_arg = m->msg_control;
  638. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  639. }
  640. if (vlan)
  641. macvlan_start_xmit(skb, vlan->dev);
  642. else
  643. kfree_skb(skb);
  644. rcu_read_unlock_bh();
  645. return total_len;
  646. err_kfree:
  647. kfree_skb(skb);
  648. err:
  649. rcu_read_lock_bh();
  650. vlan = rcu_dereference_bh(q->vlan);
  651. if (vlan)
  652. vlan->dev->stats.tx_dropped++;
  653. rcu_read_unlock_bh();
  654. return err;
  655. }
  656. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  657. unsigned long count, loff_t pos)
  658. {
  659. struct file *file = iocb->ki_filp;
  660. ssize_t result = -ENOLINK;
  661. struct macvtap_queue *q = file->private_data;
  662. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  663. file->f_flags & O_NONBLOCK);
  664. return result;
  665. }
  666. /* Put packet to the user space buffer */
  667. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  668. const struct sk_buff *skb,
  669. const struct iovec *iv, int len)
  670. {
  671. struct macvlan_dev *vlan;
  672. int ret;
  673. int vnet_hdr_len = 0;
  674. if (q->flags & IFF_VNET_HDR) {
  675. struct virtio_net_hdr vnet_hdr;
  676. vnet_hdr_len = q->vnet_hdr_sz;
  677. if ((len -= vnet_hdr_len) < 0)
  678. return -EINVAL;
  679. ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  680. if (ret)
  681. return ret;
  682. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  683. return -EFAULT;
  684. }
  685. len = min_t(int, skb->len, len);
  686. ret = skb_copy_datagram_const_iovec(skb, 0, iv, vnet_hdr_len, len);
  687. rcu_read_lock_bh();
  688. vlan = rcu_dereference_bh(q->vlan);
  689. if (vlan)
  690. macvlan_count_rx(vlan, len, ret == 0, 0);
  691. rcu_read_unlock_bh();
  692. return ret ? ret : (len + vnet_hdr_len);
  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. DECLARE_WAITQUEUE(wait, current);
  699. struct sk_buff *skb;
  700. ssize_t ret = 0;
  701. add_wait_queue(sk_sleep(&q->sk), &wait);
  702. while (len) {
  703. current->state = TASK_INTERRUPTIBLE;
  704. /* Read frames from the queue */
  705. skb = skb_dequeue(&q->sk.sk_receive_queue);
  706. if (!skb) {
  707. if (noblock) {
  708. ret = -EAGAIN;
  709. break;
  710. }
  711. if (signal_pending(current)) {
  712. ret = -ERESTARTSYS;
  713. break;
  714. }
  715. /* Nothing to read, let's sleep */
  716. schedule();
  717. continue;
  718. }
  719. ret = macvtap_put_user(q, skb, iv, len);
  720. kfree_skb(skb);
  721. break;
  722. }
  723. current->state = TASK_RUNNING;
  724. remove_wait_queue(sk_sleep(&q->sk), &wait);
  725. return ret;
  726. }
  727. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  728. unsigned long count, loff_t pos)
  729. {
  730. struct file *file = iocb->ki_filp;
  731. struct macvtap_queue *q = file->private_data;
  732. ssize_t len, ret = 0;
  733. len = iov_length(iv, count);
  734. if (len < 0) {
  735. ret = -EINVAL;
  736. goto out;
  737. }
  738. ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
  739. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  740. out:
  741. return ret;
  742. }
  743. /*
  744. * provide compatibility with generic tun/tap interface
  745. */
  746. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  747. unsigned long arg)
  748. {
  749. struct macvtap_queue *q = file->private_data;
  750. struct macvlan_dev *vlan;
  751. void __user *argp = (void __user *)arg;
  752. struct ifreq __user *ifr = argp;
  753. unsigned int __user *up = argp;
  754. unsigned int u;
  755. int __user *sp = argp;
  756. int s;
  757. int ret;
  758. switch (cmd) {
  759. case TUNSETIFF:
  760. /* ignore the name, just look at flags */
  761. if (get_user(u, &ifr->ifr_flags))
  762. return -EFAULT;
  763. ret = 0;
  764. if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP))
  765. ret = -EINVAL;
  766. else
  767. q->flags = u;
  768. return ret;
  769. case TUNGETIFF:
  770. rcu_read_lock_bh();
  771. vlan = rcu_dereference_bh(q->vlan);
  772. if (vlan)
  773. dev_hold(vlan->dev);
  774. rcu_read_unlock_bh();
  775. if (!vlan)
  776. return -ENOLINK;
  777. ret = 0;
  778. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  779. put_user(q->flags, &ifr->ifr_flags))
  780. ret = -EFAULT;
  781. dev_put(vlan->dev);
  782. return ret;
  783. case TUNGETFEATURES:
  784. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR, up))
  785. return -EFAULT;
  786. return 0;
  787. case TUNSETSNDBUF:
  788. if (get_user(u, up))
  789. return -EFAULT;
  790. q->sk.sk_sndbuf = u;
  791. return 0;
  792. case TUNGETVNETHDRSZ:
  793. s = q->vnet_hdr_sz;
  794. if (put_user(s, sp))
  795. return -EFAULT;
  796. return 0;
  797. case TUNSETVNETHDRSZ:
  798. if (get_user(s, sp))
  799. return -EFAULT;
  800. if (s < (int)sizeof(struct virtio_net_hdr))
  801. return -EINVAL;
  802. q->vnet_hdr_sz = s;
  803. return 0;
  804. case TUNSETOFFLOAD:
  805. /* let the user check for future flags */
  806. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  807. TUN_F_TSO_ECN | TUN_F_UFO))
  808. return -EINVAL;
  809. /* TODO: only accept frames with the features that
  810. got enabled for forwarded frames */
  811. if (!(q->flags & IFF_VNET_HDR))
  812. return -EINVAL;
  813. return 0;
  814. default:
  815. return -EINVAL;
  816. }
  817. }
  818. #ifdef CONFIG_COMPAT
  819. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  820. unsigned long arg)
  821. {
  822. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  823. }
  824. #endif
  825. static const struct file_operations macvtap_fops = {
  826. .owner = THIS_MODULE,
  827. .open = macvtap_open,
  828. .release = macvtap_release,
  829. .aio_read = macvtap_aio_read,
  830. .aio_write = macvtap_aio_write,
  831. .poll = macvtap_poll,
  832. .llseek = no_llseek,
  833. .unlocked_ioctl = macvtap_ioctl,
  834. #ifdef CONFIG_COMPAT
  835. .compat_ioctl = macvtap_compat_ioctl,
  836. #endif
  837. };
  838. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  839. struct msghdr *m, size_t total_len)
  840. {
  841. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  842. return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
  843. m->msg_flags & MSG_DONTWAIT);
  844. }
  845. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  846. struct msghdr *m, size_t total_len,
  847. int flags)
  848. {
  849. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  850. int ret;
  851. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  852. return -EINVAL;
  853. ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
  854. flags & MSG_DONTWAIT);
  855. if (ret > total_len) {
  856. m->msg_flags |= MSG_TRUNC;
  857. ret = flags & MSG_TRUNC ? ret : total_len;
  858. }
  859. return ret;
  860. }
  861. /* Ops structure to mimic raw sockets with tun */
  862. static const struct proto_ops macvtap_socket_ops = {
  863. .sendmsg = macvtap_sendmsg,
  864. .recvmsg = macvtap_recvmsg,
  865. };
  866. /* Get an underlying socket object from tun file. Returns error unless file is
  867. * attached to a device. The returned object works like a packet socket, it
  868. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  869. * holding a reference to the file for as long as the socket is in use. */
  870. struct socket *macvtap_get_socket(struct file *file)
  871. {
  872. struct macvtap_queue *q;
  873. if (file->f_op != &macvtap_fops)
  874. return ERR_PTR(-EINVAL);
  875. q = file->private_data;
  876. if (!q)
  877. return ERR_PTR(-EBADFD);
  878. return &q->sock;
  879. }
  880. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  881. static int macvtap_device_event(struct notifier_block *unused,
  882. unsigned long event, void *ptr)
  883. {
  884. struct net_device *dev = ptr;
  885. struct macvlan_dev *vlan;
  886. struct device *classdev;
  887. dev_t devt;
  888. int err;
  889. if (dev->rtnl_link_ops != &macvtap_link_ops)
  890. return NOTIFY_DONE;
  891. vlan = netdev_priv(dev);
  892. switch (event) {
  893. case NETDEV_REGISTER:
  894. /* Create the device node here after the network device has
  895. * been registered but before register_netdevice has
  896. * finished running.
  897. */
  898. err = macvtap_get_minor(vlan);
  899. if (err)
  900. return notifier_from_errno(err);
  901. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  902. classdev = device_create(macvtap_class, &dev->dev, devt,
  903. dev, "tap%d", dev->ifindex);
  904. if (IS_ERR(classdev)) {
  905. macvtap_free_minor(vlan);
  906. return notifier_from_errno(PTR_ERR(classdev));
  907. }
  908. break;
  909. case NETDEV_UNREGISTER:
  910. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  911. device_destroy(macvtap_class, devt);
  912. macvtap_free_minor(vlan);
  913. break;
  914. }
  915. return NOTIFY_DONE;
  916. }
  917. static struct notifier_block macvtap_notifier_block __read_mostly = {
  918. .notifier_call = macvtap_device_event,
  919. };
  920. static int macvtap_init(void)
  921. {
  922. int err;
  923. err = alloc_chrdev_region(&macvtap_major, 0,
  924. MACVTAP_NUM_DEVS, "macvtap");
  925. if (err)
  926. goto out1;
  927. cdev_init(&macvtap_cdev, &macvtap_fops);
  928. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  929. if (err)
  930. goto out2;
  931. macvtap_class = class_create(THIS_MODULE, "macvtap");
  932. if (IS_ERR(macvtap_class)) {
  933. err = PTR_ERR(macvtap_class);
  934. goto out3;
  935. }
  936. err = register_netdevice_notifier(&macvtap_notifier_block);
  937. if (err)
  938. goto out4;
  939. err = macvlan_link_register(&macvtap_link_ops);
  940. if (err)
  941. goto out5;
  942. return 0;
  943. out5:
  944. unregister_netdevice_notifier(&macvtap_notifier_block);
  945. out4:
  946. class_unregister(macvtap_class);
  947. out3:
  948. cdev_del(&macvtap_cdev);
  949. out2:
  950. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  951. out1:
  952. return err;
  953. }
  954. module_init(macvtap_init);
  955. static void macvtap_exit(void)
  956. {
  957. rtnl_link_unregister(&macvtap_link_ops);
  958. unregister_netdevice_notifier(&macvtap_notifier_block);
  959. class_unregister(macvtap_class);
  960. cdev_del(&macvtap_cdev);
  961. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  962. }
  963. module_exit(macvtap_exit);
  964. MODULE_ALIAS_RTNL_LINK("macvtap");
  965. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  966. MODULE_LICENSE("GPL");