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