macvtap.c 27 KB

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