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