macvtap.c 28 KB

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