macvtap.c 26 KB

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