Commit | Line | Data |
---|---|---|
20d29d7a AB |
1 | #include <linux/etherdevice.h> |
2 | #include <linux/if_macvlan.h> | |
f09e2249 | 3 | #include <linux/if_vlan.h> |
20d29d7a AB |
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> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
20d29d7a AB |
14 | #include <linux/wait.h> |
15 | #include <linux/cdev.h> | |
40401530 | 16 | #include <linux/idr.h> |
20d29d7a | 17 | #include <linux/fs.h> |
6c36d2e2 | 18 | #include <linux/uio.h> |
20d29d7a AB |
19 | |
20 | #include <net/net_namespace.h> | |
21 | #include <net/rtnetlink.h> | |
22 | #include <net/sock.h> | |
b9fb9ee0 | 23 | #include <linux/virtio_net.h> |
362899b8 | 24 | #include <linux/skb_array.h> |
20d29d7a AB |
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 | * | |
20d29d7a AB |
35 | */ |
36 | struct macvtap_queue { | |
37 | struct sock sk; | |
38 | struct socket sock; | |
43815482 | 39 | struct socket_wq wq; |
55afbd08 | 40 | int vnet_hdr_sz; |
13707f9e | 41 | struct macvlan_dev __rcu *vlan; |
20d29d7a | 42 | struct file *file; |
b9fb9ee0 | 43 | unsigned int flags; |
376b1aab | 44 | u16 queue_index; |
815f236d JW |
45 | bool enabled; |
46 | struct list_head next; | |
362899b8 | 47 | struct skb_array skb_array; |
20d29d7a AB |
48 | }; |
49 | ||
01b07fb3 MT |
50 | #define MACVTAP_FEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE) |
51 | ||
52 | #define MACVTAP_VNET_LE 0x80000000 | |
8b8e658b GK |
53 | #define MACVTAP_VNET_BE 0x40000000 |
54 | ||
55 | #ifdef CONFIG_TUN_VNET_CROSS_LE | |
56 | static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q) | |
57 | { | |
58 | return q->flags & MACVTAP_VNET_BE ? false : | |
59 | virtio_legacy_is_little_endian(); | |
60 | } | |
61 | ||
62 | static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *sp) | |
63 | { | |
64 | int s = !!(q->flags & MACVTAP_VNET_BE); | |
65 | ||
66 | if (put_user(s, sp)) | |
67 | return -EFAULT; | |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
72 | static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *sp) | |
73 | { | |
74 | int s; | |
75 | ||
76 | if (get_user(s, sp)) | |
77 | return -EFAULT; | |
78 | ||
79 | if (s) | |
80 | q->flags |= MACVTAP_VNET_BE; | |
81 | else | |
82 | q->flags &= ~MACVTAP_VNET_BE; | |
83 | ||
84 | return 0; | |
85 | } | |
86 | #else | |
87 | static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q) | |
88 | { | |
89 | return virtio_legacy_is_little_endian(); | |
90 | } | |
91 | ||
92 | static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *argp) | |
93 | { | |
94 | return -EINVAL; | |
95 | } | |
96 | ||
97 | static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *argp) | |
98 | { | |
99 | return -EINVAL; | |
100 | } | |
101 | #endif /* CONFIG_TUN_VNET_CROSS_LE */ | |
6ae7feb3 | 102 | |
5b11e15f GK |
103 | static inline bool macvtap_is_little_endian(struct macvtap_queue *q) |
104 | { | |
7d824109 | 105 | return q->flags & MACVTAP_VNET_LE || |
8b8e658b | 106 | macvtap_legacy_is_little_endian(q); |
5b11e15f | 107 | } |
6ae7feb3 MT |
108 | |
109 | static inline u16 macvtap16_to_cpu(struct macvtap_queue *q, __virtio16 val) | |
110 | { | |
5b11e15f | 111 | return __virtio16_to_cpu(macvtap_is_little_endian(q), val); |
6ae7feb3 MT |
112 | } |
113 | ||
114 | static inline __virtio16 cpu_to_macvtap16(struct macvtap_queue *q, u16 val) | |
115 | { | |
5b11e15f | 116 | return __cpu_to_virtio16(macvtap_is_little_endian(q), val); |
6ae7feb3 MT |
117 | } |
118 | ||
20d29d7a AB |
119 | static struct proto macvtap_proto = { |
120 | .name = "macvtap", | |
121 | .owner = THIS_MODULE, | |
122 | .obj_size = sizeof (struct macvtap_queue), | |
123 | }; | |
124 | ||
125 | /* | |
e09eff7f | 126 | * Variables for dealing with macvtaps device numbers. |
20d29d7a | 127 | */ |
1ebed71a | 128 | static dev_t macvtap_major; |
e09eff7f EB |
129 | #define MACVTAP_NUM_DEVS (1U << MINORBITS) |
130 | static DEFINE_MUTEX(minor_lock); | |
131 | static DEFINE_IDR(minor_idr); | |
132 | ||
97bc3633 | 133 | #define GOODCOPY_LEN 128 |
17af2bce MA |
134 | static const void *macvtap_net_namespace(struct device *d) |
135 | { | |
136 | struct net_device *dev = to_net_dev(d->parent); | |
137 | return dev_net(dev); | |
138 | } | |
139 | ||
140 | static struct class macvtap_class = { | |
141 | .name = "macvtap", | |
142 | .owner = THIS_MODULE, | |
143 | .ns_type = &net_ns_type_operations, | |
144 | .namespace = macvtap_net_namespace, | |
145 | }; | |
20d29d7a AB |
146 | static struct cdev macvtap_cdev; |
147 | ||
501c774c AB |
148 | static const struct proto_ops macvtap_socket_ops; |
149 | ||
2be5c767 | 150 | #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \ |
e3e3c423 | 151 | NETIF_F_TSO6 | NETIF_F_UFO) |
2be5c767 | 152 | #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO) |
f23d538b | 153 | #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST) |
a567dd62 | 154 | |
6acf54f1 VY |
155 | static struct macvlan_dev *macvtap_get_vlan_rcu(const struct net_device *dev) |
156 | { | |
157 | return rcu_dereference(dev->rx_handler_data); | |
158 | } | |
159 | ||
20d29d7a AB |
160 | /* |
161 | * RCU usage: | |
02df55d2 AB |
162 | * The macvtap_queue and the macvlan_dev are loosely coupled, the |
163 | * pointers from one to the other can only be read while rcu_read_lock | |
441ac0fc | 164 | * or rtnl is held. |
20d29d7a | 165 | * |
02df55d2 AB |
166 | * Both the file and the macvlan_dev hold a reference on the macvtap_queue |
167 | * through sock_hold(&q->sk). When the macvlan_dev goes away first, | |
168 | * q->vlan becomes inaccessible. When the files gets closed, | |
169 | * macvtap_get_queue() fails. | |
20d29d7a | 170 | * |
02df55d2 AB |
171 | * There may still be references to the struct sock inside of the |
172 | * queue from outbound SKBs, but these never reference back to the | |
173 | * file or the dev. The data structure is freed through __sk_free | |
174 | * when both our references and any pending SKBs are gone. | |
20d29d7a | 175 | */ |
20d29d7a | 176 | |
815f236d | 177 | static int macvtap_enable_queue(struct net_device *dev, struct file *file, |
20d29d7a | 178 | struct macvtap_queue *q) |
815f236d JW |
179 | { |
180 | struct macvlan_dev *vlan = netdev_priv(dev); | |
181 | int err = -EINVAL; | |
182 | ||
441ac0fc | 183 | ASSERT_RTNL(); |
815f236d JW |
184 | |
185 | if (q->enabled) | |
186 | goto out; | |
187 | ||
188 | err = 0; | |
189 | rcu_assign_pointer(vlan->taps[vlan->numvtaps], q); | |
190 | q->queue_index = vlan->numvtaps; | |
191 | q->enabled = true; | |
192 | ||
193 | vlan->numvtaps++; | |
194 | out: | |
815f236d JW |
195 | return err; |
196 | } | |
197 | ||
40b8fe45 | 198 | /* Requires RTNL */ |
815f236d JW |
199 | static int macvtap_set_queue(struct net_device *dev, struct file *file, |
200 | struct macvtap_queue *q) | |
20d29d7a AB |
201 | { |
202 | struct macvlan_dev *vlan = netdev_priv(dev); | |
20d29d7a | 203 | |
815f236d | 204 | if (vlan->numqueues == MAX_MACVTAP_QUEUES) |
40b8fe45 | 205 | return -EBUSY; |
20d29d7a | 206 | |
02df55d2 | 207 | rcu_assign_pointer(q->vlan, vlan); |
376b1aab | 208 | rcu_assign_pointer(vlan->taps[vlan->numvtaps], q); |
02df55d2 | 209 | sock_hold(&q->sk); |
20d29d7a AB |
210 | |
211 | q->file = file; | |
376b1aab | 212 | q->queue_index = vlan->numvtaps; |
815f236d | 213 | q->enabled = true; |
02df55d2 | 214 | file->private_data = q; |
815f236d | 215 | list_add_tail(&q->next, &vlan->queue_list); |
20d29d7a | 216 | |
1565c7c1 | 217 | vlan->numvtaps++; |
815f236d | 218 | vlan->numqueues++; |
1565c7c1 | 219 | |
40b8fe45 | 220 | return 0; |
20d29d7a AB |
221 | } |
222 | ||
441ac0fc | 223 | static int macvtap_disable_queue(struct macvtap_queue *q) |
815f236d JW |
224 | { |
225 | struct macvlan_dev *vlan; | |
226 | struct macvtap_queue *nq; | |
227 | ||
441ac0fc | 228 | ASSERT_RTNL(); |
815f236d JW |
229 | if (!q->enabled) |
230 | return -EINVAL; | |
231 | ||
441ac0fc VY |
232 | vlan = rtnl_dereference(q->vlan); |
233 | ||
815f236d JW |
234 | if (vlan) { |
235 | int index = q->queue_index; | |
236 | BUG_ON(index >= vlan->numvtaps); | |
441ac0fc | 237 | nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]); |
815f236d JW |
238 | nq->queue_index = index; |
239 | ||
240 | rcu_assign_pointer(vlan->taps[index], nq); | |
241 | RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL); | |
242 | q->enabled = false; | |
243 | ||
244 | vlan->numvtaps--; | |
245 | } | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
20d29d7a | 250 | /* |
02df55d2 AB |
251 | * The file owning the queue got closed, give up both |
252 | * the reference that the files holds as well as the | |
253 | * one from the macvlan_dev if that still exists. | |
20d29d7a AB |
254 | * |
255 | * Using the spinlock makes sure that we don't get | |
256 | * to the queue again after destroying it. | |
20d29d7a | 257 | */ |
02df55d2 | 258 | static void macvtap_put_queue(struct macvtap_queue *q) |
20d29d7a | 259 | { |
02df55d2 | 260 | struct macvlan_dev *vlan; |
20d29d7a | 261 | |
441ac0fc VY |
262 | rtnl_lock(); |
263 | vlan = rtnl_dereference(q->vlan); | |
264 | ||
02df55d2 | 265 | if (vlan) { |
815f236d | 266 | if (q->enabled) |
441ac0fc | 267 | BUG_ON(macvtap_disable_queue(q)); |
376b1aab | 268 | |
815f236d | 269 | vlan->numqueues--; |
2cfa5a04 | 270 | RCU_INIT_POINTER(q->vlan, NULL); |
02df55d2 | 271 | sock_put(&q->sk); |
815f236d | 272 | list_del_init(&q->next); |
20d29d7a AB |
273 | } |
274 | ||
441ac0fc | 275 | rtnl_unlock(); |
20d29d7a AB |
276 | |
277 | synchronize_rcu(); | |
362899b8 | 278 | skb_array_cleanup(&q->skb_array); |
20d29d7a AB |
279 | sock_put(&q->sk); |
280 | } | |
281 | ||
282 | /* | |
1565c7c1 KK |
283 | * Select a queue based on the rxq of the device on which this packet |
284 | * arrived. If the incoming device is not mq, calculate a flow hash | |
285 | * to select a queue. If all fails, find the first available queue. | |
286 | * Cache vlan->numvtaps since it can become zero during the execution | |
287 | * of this function. | |
20d29d7a AB |
288 | */ |
289 | static struct macvtap_queue *macvtap_get_queue(struct net_device *dev, | |
290 | struct sk_buff *skb) | |
291 | { | |
292 | struct macvlan_dev *vlan = netdev_priv(dev); | |
1565c7c1 | 293 | struct macvtap_queue *tap = NULL; |
815f236d JW |
294 | /* Access to taps array is protected by rcu, but access to numvtaps |
295 | * isn't. Below we use it to lookup a queue, but treat it as a hint | |
296 | * and validate that the result isn't NULL - in case we are | |
297 | * racing against queue removal. | |
298 | */ | |
ed0483fa | 299 | int numvtaps = ACCESS_ONCE(vlan->numvtaps); |
1565c7c1 KK |
300 | __u32 rxq; |
301 | ||
302 | if (!numvtaps) | |
303 | goto out; | |
304 | ||
1b16bf42 JW |
305 | if (numvtaps == 1) |
306 | goto single; | |
307 | ||
ef0002b5 | 308 | /* Check if we can use flow to select a queue */ |
3958afa1 | 309 | rxq = skb_get_hash(skb); |
ef0002b5 KK |
310 | if (rxq) { |
311 | tap = rcu_dereference(vlan->taps[rxq % numvtaps]); | |
376b1aab | 312 | goto out; |
ef0002b5 KK |
313 | } |
314 | ||
1565c7c1 KK |
315 | if (likely(skb_rx_queue_recorded(skb))) { |
316 | rxq = skb_get_rx_queue(skb); | |
20d29d7a | 317 | |
1565c7c1 KK |
318 | while (unlikely(rxq >= numvtaps)) |
319 | rxq -= numvtaps; | |
320 | ||
321 | tap = rcu_dereference(vlan->taps[rxq]); | |
376b1aab | 322 | goto out; |
1565c7c1 KK |
323 | } |
324 | ||
1b16bf42 | 325 | single: |
376b1aab | 326 | tap = rcu_dereference(vlan->taps[0]); |
1565c7c1 KK |
327 | out: |
328 | return tap; | |
20d29d7a AB |
329 | } |
330 | ||
02df55d2 AB |
331 | /* |
332 | * The net_device is going away, give up the reference | |
1565c7c1 KK |
333 | * that it holds on all queues and safely set the pointer |
334 | * from the queues to NULL. | |
02df55d2 | 335 | */ |
20d29d7a AB |
336 | static void macvtap_del_queues(struct net_device *dev) |
337 | { | |
338 | struct macvlan_dev *vlan = netdev_priv(dev); | |
dfe816c5 | 339 | struct macvtap_queue *q, *tmp; |
02df55d2 | 340 | |
441ac0fc | 341 | ASSERT_RTNL(); |
815f236d JW |
342 | list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) { |
343 | list_del_init(&q->next); | |
376b1aab | 344 | RCU_INIT_POINTER(q->vlan, NULL); |
815f236d JW |
345 | if (q->enabled) |
346 | vlan->numvtaps--; | |
347 | vlan->numqueues--; | |
dfe816c5 | 348 | sock_put(&q->sk); |
564517e8 | 349 | } |
815f236d JW |
350 | BUG_ON(vlan->numvtaps); |
351 | BUG_ON(vlan->numqueues); | |
99f34b38 EB |
352 | /* guarantee that any future macvtap_set_queue will fail */ |
353 | vlan->numvtaps = MAX_MACVTAP_QUEUES; | |
20d29d7a AB |
354 | } |
355 | ||
6acf54f1 | 356 | static rx_handler_result_t macvtap_handle_frame(struct sk_buff **pskb) |
20d29d7a | 357 | { |
6acf54f1 VY |
358 | struct sk_buff *skb = *pskb; |
359 | struct net_device *dev = skb->dev; | |
360 | struct macvlan_dev *vlan; | |
361 | struct macvtap_queue *q; | |
a567dd62 VY |
362 | netdev_features_t features = TAP_FEATURES; |
363 | ||
6acf54f1 VY |
364 | vlan = macvtap_get_vlan_rcu(dev); |
365 | if (!vlan) | |
366 | return RX_HANDLER_PASS; | |
367 | ||
368 | q = macvtap_get_queue(dev, skb); | |
20d29d7a | 369 | if (!q) |
6acf54f1 | 370 | return RX_HANDLER_PASS; |
8a35747a | 371 | |
362899b8 | 372 | if (__skb_array_full(&q->skb_array)) |
8a35747a | 373 | goto drop; |
20d29d7a | 374 | |
6acf54f1 VY |
375 | skb_push(skb, ETH_HLEN); |
376 | ||
3e4f8b78 | 377 | /* Apply the forward feature mask so that we perform segmentation |
e5733321 VY |
378 | * according to users wishes. This only works if VNET_HDR is |
379 | * enabled. | |
3e4f8b78 | 380 | */ |
e5733321 VY |
381 | if (q->flags & IFF_VNET_HDR) |
382 | features |= vlan->tap_features; | |
8b86a61d | 383 | if (netif_needs_gso(skb, features)) { |
3e4f8b78 VY |
384 | struct sk_buff *segs = __skb_gso_segment(skb, features, false); |
385 | ||
386 | if (IS_ERR(segs)) | |
387 | goto drop; | |
388 | ||
389 | if (!segs) { | |
362899b8 JW |
390 | if (skb_array_produce(&q->skb_array, skb)) |
391 | goto drop; | |
3e4f8b78 VY |
392 | goto wake_up; |
393 | } | |
394 | ||
be0bd316 | 395 | consume_skb(skb); |
3e4f8b78 VY |
396 | while (segs) { |
397 | struct sk_buff *nskb = segs->next; | |
398 | ||
399 | segs->next = NULL; | |
362899b8 JW |
400 | if (skb_array_produce(&q->skb_array, segs)) { |
401 | kfree_skb(segs); | |
402 | kfree_skb_list(nskb); | |
403 | break; | |
404 | } | |
3e4f8b78 VY |
405 | segs = nskb; |
406 | } | |
407 | } else { | |
cbdb0427 VY |
408 | /* If we receive a partial checksum and the tap side |
409 | * doesn't support checksum offload, compute the checksum. | |
410 | * Note: it doesn't matter which checksum feature to | |
411 | * check, we either support them all or none. | |
412 | */ | |
413 | if (skb->ip_summed == CHECKSUM_PARTIAL && | |
a188222b | 414 | !(features & NETIF_F_CSUM_MASK) && |
cbdb0427 VY |
415 | skb_checksum_help(skb)) |
416 | goto drop; | |
362899b8 JW |
417 | if (skb_array_produce(&q->skb_array, skb)) |
418 | goto drop; | |
3e4f8b78 VY |
419 | } |
420 | ||
421 | wake_up: | |
4a4771a5 | 422 | wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND); |
6acf54f1 | 423 | return RX_HANDLER_CONSUMED; |
8a35747a HX |
424 | |
425 | drop: | |
6acf54f1 VY |
426 | /* Count errors/drops only here, thus don't care about args. */ |
427 | macvlan_count_rx(vlan, 0, 0, 0); | |
8a35747a | 428 | kfree_skb(skb); |
6acf54f1 | 429 | return RX_HANDLER_CONSUMED; |
20d29d7a AB |
430 | } |
431 | ||
e09eff7f EB |
432 | static int macvtap_get_minor(struct macvlan_dev *vlan) |
433 | { | |
434 | int retval = -ENOMEM; | |
e09eff7f EB |
435 | |
436 | mutex_lock(&minor_lock); | |
ec09ebc1 TH |
437 | retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL); |
438 | if (retval >= 0) { | |
439 | vlan->minor = retval; | |
440 | } else if (retval == -ENOSPC) { | |
e09eff7f EB |
441 | printk(KERN_ERR "too many macvtap devices\n"); |
442 | retval = -EINVAL; | |
e09eff7f | 443 | } |
e09eff7f | 444 | mutex_unlock(&minor_lock); |
ec09ebc1 | 445 | return retval < 0 ? retval : 0; |
e09eff7f EB |
446 | } |
447 | ||
448 | static void macvtap_free_minor(struct macvlan_dev *vlan) | |
449 | { | |
450 | mutex_lock(&minor_lock); | |
451 | if (vlan->minor) { | |
452 | idr_remove(&minor_idr, vlan->minor); | |
453 | vlan->minor = 0; | |
454 | } | |
455 | mutex_unlock(&minor_lock); | |
456 | } | |
457 | ||
458 | static struct net_device *dev_get_by_macvtap_minor(int minor) | |
459 | { | |
460 | struct net_device *dev = NULL; | |
461 | struct macvlan_dev *vlan; | |
462 | ||
463 | mutex_lock(&minor_lock); | |
464 | vlan = idr_find(&minor_idr, minor); | |
465 | if (vlan) { | |
466 | dev = vlan->dev; | |
467 | dev_hold(dev); | |
468 | } | |
469 | mutex_unlock(&minor_lock); | |
470 | return dev; | |
471 | } | |
472 | ||
20d29d7a AB |
473 | static int macvtap_newlink(struct net *src_net, |
474 | struct net_device *dev, | |
475 | struct nlattr *tb[], | |
476 | struct nlattr *data[]) | |
477 | { | |
815f236d | 478 | struct macvlan_dev *vlan = netdev_priv(dev); |
6acf54f1 VY |
479 | int err; |
480 | ||
815f236d JW |
481 | INIT_LIST_HEAD(&vlan->queue_list); |
482 | ||
2be5c767 VY |
483 | /* Since macvlan supports all offloads by default, make |
484 | * tap support all offloads also. | |
485 | */ | |
486 | vlan->tap_features = TUN_OFFLOADS; | |
487 | ||
6acf54f1 VY |
488 | err = netdev_rx_handler_register(dev, macvtap_handle_frame, vlan); |
489 | if (err) | |
490 | return err; | |
491 | ||
9bf1907f EB |
492 | /* Don't put anything that may fail after macvlan_common_newlink |
493 | * because we can't undo what it does. | |
494 | */ | |
2f6a1b66 | 495 | return macvlan_common_newlink(src_net, dev, tb, data); |
20d29d7a AB |
496 | } |
497 | ||
498 | static void macvtap_dellink(struct net_device *dev, | |
499 | struct list_head *head) | |
500 | { | |
6acf54f1 | 501 | netdev_rx_handler_unregister(dev); |
20d29d7a AB |
502 | macvtap_del_queues(dev); |
503 | macvlan_dellink(dev, head); | |
504 | } | |
505 | ||
8a35747a HX |
506 | static void macvtap_setup(struct net_device *dev) |
507 | { | |
508 | macvlan_common_setup(dev); | |
509 | dev->tx_queue_len = TUN_READQ_SIZE; | |
510 | } | |
511 | ||
20d29d7a AB |
512 | static struct rtnl_link_ops macvtap_link_ops __read_mostly = { |
513 | .kind = "macvtap", | |
8a35747a | 514 | .setup = macvtap_setup, |
20d29d7a AB |
515 | .newlink = macvtap_newlink, |
516 | .dellink = macvtap_dellink, | |
517 | }; | |
518 | ||
519 | ||
520 | static void macvtap_sock_write_space(struct sock *sk) | |
521 | { | |
43815482 ED |
522 | wait_queue_head_t *wqueue; |
523 | ||
20d29d7a | 524 | if (!sock_writeable(sk) || |
9cd3e072 | 525 | !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
20d29d7a AB |
526 | return; |
527 | ||
43815482 ED |
528 | wqueue = sk_sleep(sk); |
529 | if (wqueue && waitqueue_active(wqueue)) | |
530 | wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND); | |
20d29d7a AB |
531 | } |
532 | ||
2259fef0 EB |
533 | static void macvtap_sock_destruct(struct sock *sk) |
534 | { | |
362899b8 JW |
535 | struct macvtap_queue *q = container_of(sk, struct macvtap_queue, sk); |
536 | struct sk_buff *skb; | |
537 | ||
538 | while ((skb = skb_array_consume(&q->skb_array)) != NULL) | |
539 | kfree(skb); | |
2259fef0 EB |
540 | } |
541 | ||
20d29d7a AB |
542 | static int macvtap_open(struct inode *inode, struct file *file) |
543 | { | |
544 | struct net *net = current->nsproxy->net_ns; | |
40b8fe45 | 545 | struct net_device *dev; |
20d29d7a | 546 | struct macvtap_queue *q; |
40b8fe45 | 547 | int err = -ENODEV; |
20d29d7a | 548 | |
40b8fe45 VY |
549 | rtnl_lock(); |
550 | dev = dev_get_by_macvtap_minor(iminor(inode)); | |
20d29d7a | 551 | if (!dev) |
362899b8 | 552 | goto err; |
20d29d7a | 553 | |
20d29d7a AB |
554 | err = -ENOMEM; |
555 | q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, | |
11aa9c28 | 556 | &macvtap_proto, 0); |
20d29d7a | 557 | if (!q) |
362899b8 | 558 | goto err; |
20d29d7a | 559 | |
d9a90a31 | 560 | RCU_INIT_POINTER(q->sock.wq, &q->wq); |
43815482 | 561 | init_waitqueue_head(&q->wq.wait); |
20d29d7a AB |
562 | q->sock.type = SOCK_RAW; |
563 | q->sock.state = SS_CONNECTED; | |
501c774c AB |
564 | q->sock.file = file; |
565 | q->sock.ops = &macvtap_socket_ops; | |
20d29d7a | 566 | sock_init_data(&q->sock, &q->sk); |
20d29d7a | 567 | q->sk.sk_write_space = macvtap_sock_write_space; |
2259fef0 | 568 | q->sk.sk_destruct = macvtap_sock_destruct; |
b9fb9ee0 | 569 | q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP; |
55afbd08 | 570 | q->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
20d29d7a | 571 | |
97bc3633 SM |
572 | /* |
573 | * so far only KVM virtio_net uses macvtap, enable zero copy between | |
574 | * guest kernel and host kernel when lower device supports zerocopy | |
047af9cf EB |
575 | * |
576 | * The macvlan supports zerocopy iff the lower device supports zero | |
577 | * copy so we don't have to look at the lower device directly. | |
97bc3633 | 578 | */ |
047af9cf EB |
579 | if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG)) |
580 | sock_set_flag(&q->sk, SOCK_ZEROCOPY); | |
97bc3633 | 581 | |
362899b8 JW |
582 | err = -ENOMEM; |
583 | if (skb_array_init(&q->skb_array, dev->tx_queue_len, GFP_KERNEL)) | |
584 | goto err_array; | |
585 | ||
20d29d7a AB |
586 | err = macvtap_set_queue(dev, file, q); |
587 | if (err) | |
362899b8 | 588 | goto err_queue; |
20d29d7a | 589 | |
362899b8 JW |
590 | dev_put(dev); |
591 | ||
592 | rtnl_unlock(); | |
593 | return err; | |
594 | ||
595 | err_queue: | |
596 | skb_array_cleanup(&q->skb_array); | |
597 | err_array: | |
598 | sock_put(&q->sk); | |
599 | err: | |
20d29d7a AB |
600 | if (dev) |
601 | dev_put(dev); | |
602 | ||
40b8fe45 | 603 | rtnl_unlock(); |
20d29d7a AB |
604 | return err; |
605 | } | |
606 | ||
607 | static int macvtap_release(struct inode *inode, struct file *file) | |
608 | { | |
02df55d2 AB |
609 | struct macvtap_queue *q = file->private_data; |
610 | macvtap_put_queue(q); | |
20d29d7a AB |
611 | return 0; |
612 | } | |
613 | ||
614 | static unsigned int macvtap_poll(struct file *file, poll_table * wait) | |
615 | { | |
02df55d2 | 616 | struct macvtap_queue *q = file->private_data; |
20d29d7a AB |
617 | unsigned int mask = POLLERR; |
618 | ||
619 | if (!q) | |
620 | goto out; | |
621 | ||
622 | mask = 0; | |
43815482 | 623 | poll_wait(file, &q->wq.wait, wait); |
20d29d7a | 624 | |
362899b8 | 625 | if (!skb_array_empty(&q->skb_array)) |
20d29d7a AB |
626 | mask |= POLLIN | POLLRDNORM; |
627 | ||
628 | if (sock_writeable(&q->sk) || | |
9cd3e072 | 629 | (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) && |
20d29d7a AB |
630 | sock_writeable(&q->sk))) |
631 | mask |= POLLOUT | POLLWRNORM; | |
632 | ||
633 | out: | |
20d29d7a AB |
634 | return mask; |
635 | } | |
636 | ||
b9fb9ee0 AB |
637 | static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad, |
638 | size_t len, size_t linear, | |
639 | int noblock, int *err) | |
640 | { | |
641 | struct sk_buff *skb; | |
642 | ||
643 | /* Under a page? Don't bother with paged skb. */ | |
644 | if (prepad + len < PAGE_SIZE || !linear) | |
645 | linear = len; | |
646 | ||
647 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, | |
28d64271 | 648 | err, 0); |
b9fb9ee0 AB |
649 | if (!skb) |
650 | return NULL; | |
651 | ||
652 | skb_reserve(skb, prepad); | |
653 | skb_put(skb, linear); | |
654 | skb->data_len = len - linear; | |
655 | skb->len += len - linear; | |
656 | ||
657 | return skb; | |
658 | } | |
659 | ||
2f1d8b9e ED |
660 | /* Neighbour code has some assumptions on HH_DATA_MOD alignment */ |
661 | #define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN) | |
662 | ||
20d29d7a | 663 | /* Get packet from user space buffer */ |
97bc3633 | 664 | static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, |
f5ff53b4 | 665 | struct iov_iter *from, int noblock) |
20d29d7a | 666 | { |
2f1d8b9e | 667 | int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE); |
20d29d7a | 668 | struct sk_buff *skb; |
02df55d2 | 669 | struct macvlan_dev *vlan; |
f5ff53b4 | 670 | unsigned long total_len = iov_iter_count(from); |
97bc3633 | 671 | unsigned long len = total_len; |
20d29d7a | 672 | int err; |
b9fb9ee0 AB |
673 | struct virtio_net_hdr vnet_hdr = { 0 }; |
674 | int vnet_hdr_len = 0; | |
b92946e2 | 675 | int copylen = 0; |
c5c62f1b | 676 | int depth; |
97bc3633 | 677 | bool zerocopy = false; |
61d46bf9 | 678 | size_t linear; |
f5ff53b4 | 679 | ssize_t n; |
b9fb9ee0 AB |
680 | |
681 | if (q->flags & IFF_VNET_HDR) { | |
55afbd08 | 682 | vnet_hdr_len = q->vnet_hdr_sz; |
b9fb9ee0 AB |
683 | |
684 | err = -EINVAL; | |
ce3c8692 | 685 | if (len < vnet_hdr_len) |
b9fb9ee0 | 686 | goto err; |
ce3c8692 | 687 | len -= vnet_hdr_len; |
b9fb9ee0 | 688 | |
f5ff53b4 AV |
689 | err = -EFAULT; |
690 | n = copy_from_iter(&vnet_hdr, sizeof(vnet_hdr), from); | |
691 | if (n != sizeof(vnet_hdr)) | |
b9fb9ee0 | 692 | goto err; |
f5ff53b4 | 693 | iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr)); |
b9fb9ee0 | 694 | if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
6ae7feb3 MT |
695 | macvtap16_to_cpu(q, vnet_hdr.csum_start) + |
696 | macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2 > | |
697 | macvtap16_to_cpu(q, vnet_hdr.hdr_len)) | |
698 | vnet_hdr.hdr_len = cpu_to_macvtap16(q, | |
699 | macvtap16_to_cpu(q, vnet_hdr.csum_start) + | |
700 | macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2); | |
b9fb9ee0 | 701 | err = -EINVAL; |
6ae7feb3 | 702 | if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > len) |
b9fb9ee0 AB |
703 | goto err; |
704 | } | |
20d29d7a | 705 | |
b9fb9ee0 | 706 | err = -EINVAL; |
20d29d7a | 707 | if (unlikely(len < ETH_HLEN)) |
b9fb9ee0 | 708 | goto err; |
20d29d7a | 709 | |
ece793fc | 710 | if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) { |
f5ff53b4 AV |
711 | struct iov_iter i; |
712 | ||
6ae7feb3 MT |
713 | copylen = vnet_hdr.hdr_len ? |
714 | macvtap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN; | |
16a3fa28 JW |
715 | if (copylen > good_linear) |
716 | copylen = good_linear; | |
8e2ad411 WB |
717 | else if (copylen < ETH_HLEN) |
718 | copylen = ETH_HLEN; | |
61d46bf9 | 719 | linear = copylen; |
f5ff53b4 AV |
720 | i = *from; |
721 | iov_iter_advance(&i, copylen); | |
722 | if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) | |
ece793fc JW |
723 | zerocopy = true; |
724 | } | |
725 | ||
726 | if (!zerocopy) { | |
97bc3633 | 727 | copylen = len; |
8e2ad411 WB |
728 | linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len); |
729 | if (linear > good_linear) | |
16a3fa28 | 730 | linear = good_linear; |
8e2ad411 WB |
731 | else if (linear < ETH_HLEN) |
732 | linear = ETH_HLEN; | |
61d46bf9 | 733 | } |
97bc3633 | 734 | |
2f1d8b9e | 735 | skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen, |
61d46bf9 | 736 | linear, noblock, &err); |
02df55d2 AB |
737 | if (!skb) |
738 | goto err; | |
20d29d7a | 739 | |
01d6657b | 740 | if (zerocopy) |
f5ff53b4 | 741 | err = zerocopy_sg_from_iter(skb, from); |
ece793fc | 742 | else { |
f5ff53b4 | 743 | err = skb_copy_datagram_from_iter(skb, 0, from, len); |
ece793fc JW |
744 | if (!err && m && m->msg_control) { |
745 | struct ubuf_info *uarg = m->msg_control; | |
746 | uarg->callback(uarg, false); | |
747 | } | |
748 | } | |
749 | ||
02df55d2 | 750 | if (err) |
b9fb9ee0 | 751 | goto err_kfree; |
20d29d7a AB |
752 | |
753 | skb_set_network_header(skb, ETH_HLEN); | |
b9fb9ee0 AB |
754 | skb_reset_mac_header(skb); |
755 | skb->protocol = eth_hdr(skb)->h_proto; | |
756 | ||
757 | if (vnet_hdr_len) { | |
fd88d68b MR |
758 | err = virtio_net_hdr_to_skb(skb, &vnet_hdr, |
759 | macvtap_is_little_endian(q)); | |
b9fb9ee0 AB |
760 | if (err) |
761 | goto err_kfree; | |
762 | } | |
763 | ||
40893fd0 | 764 | skb_probe_transport_header(skb, ETH_HLEN); |
9b4d669b | 765 | |
c5c62f1b IV |
766 | /* Move network header to the right position for VLAN tagged packets */ |
767 | if ((skb->protocol == htons(ETH_P_8021Q) || | |
768 | skb->protocol == htons(ETH_P_8021AD)) && | |
769 | __vlan_get_protocol(skb, skb->protocol, &depth) != 0) | |
770 | skb_set_network_header(skb, depth); | |
771 | ||
ac4e4af1 VY |
772 | rcu_read_lock(); |
773 | vlan = rcu_dereference(q->vlan); | |
97bc3633 | 774 | /* copy skb_ubuf_info for callback when skb has no error */ |
01d6657b | 775 | if (zerocopy) { |
97bc3633 | 776 | skb_shinfo(skb)->destructor_arg = m->msg_control; |
01d6657b | 777 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
c9af6db4 | 778 | skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; |
01d6657b | 779 | } |
29d79196 | 780 | if (vlan) { |
6acf54f1 VY |
781 | skb->dev = vlan->dev; |
782 | dev_queue_xmit(skb); | |
29d79196 | 783 | } else { |
02df55d2 | 784 | kfree_skb(skb); |
29d79196 | 785 | } |
ac4e4af1 | 786 | rcu_read_unlock(); |
20d29d7a | 787 | |
97bc3633 | 788 | return total_len; |
02df55d2 | 789 | |
b9fb9ee0 AB |
790 | err_kfree: |
791 | kfree_skb(skb); | |
792 | ||
02df55d2 | 793 | err: |
ac4e4af1 VY |
794 | rcu_read_lock(); |
795 | vlan = rcu_dereference(q->vlan); | |
02df55d2 | 796 | if (vlan) |
cd3e22b7 | 797 | this_cpu_inc(vlan->pcpu_stats->tx_dropped); |
ac4e4af1 | 798 | rcu_read_unlock(); |
02df55d2 | 799 | |
02df55d2 | 800 | return err; |
20d29d7a AB |
801 | } |
802 | ||
f5ff53b4 | 803 | static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from) |
20d29d7a AB |
804 | { |
805 | struct file *file = iocb->ki_filp; | |
02df55d2 | 806 | struct macvtap_queue *q = file->private_data; |
20d29d7a | 807 | |
f5ff53b4 | 808 | return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK); |
20d29d7a AB |
809 | } |
810 | ||
811 | /* Put packet to the user space buffer */ | |
812 | static ssize_t macvtap_put_user(struct macvtap_queue *q, | |
813 | const struct sk_buff *skb, | |
6c36d2e2 | 814 | struct iov_iter *iter) |
20d29d7a | 815 | { |
20d29d7a | 816 | int ret; |
b9fb9ee0 | 817 | int vnet_hdr_len = 0; |
f09e2249 | 818 | int vlan_offset = 0; |
6c36d2e2 | 819 | int total; |
b9fb9ee0 AB |
820 | |
821 | if (q->flags & IFF_VNET_HDR) { | |
822 | struct virtio_net_hdr vnet_hdr; | |
55afbd08 | 823 | vnet_hdr_len = q->vnet_hdr_sz; |
6c36d2e2 | 824 | if (iov_iter_count(iter) < vnet_hdr_len) |
b9fb9ee0 AB |
825 | return -EINVAL; |
826 | ||
fd88d68b MR |
827 | ret = virtio_net_hdr_from_skb(skb, &vnet_hdr, |
828 | macvtap_is_little_endian(q)); | |
829 | if (ret) | |
830 | BUG(); | |
b9fb9ee0 | 831 | |
6c36d2e2 HX |
832 | if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) != |
833 | sizeof(vnet_hdr)) | |
b9fb9ee0 | 834 | return -EFAULT; |
7cc76f51 JW |
835 | |
836 | iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr)); | |
b9fb9ee0 | 837 | } |
6c36d2e2 | 838 | total = vnet_hdr_len; |
ce232ce0 | 839 | total += skb->len; |
f09e2249 | 840 | |
df8a39de | 841 | if (skb_vlan_tag_present(skb)) { |
f09e2249 BG |
842 | struct { |
843 | __be16 h_vlan_proto; | |
844 | __be16 h_vlan_TCI; | |
845 | } veth; | |
0fbe0d47 | 846 | veth.h_vlan_proto = skb->vlan_proto; |
df8a39de | 847 | veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); |
f09e2249 BG |
848 | |
849 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); | |
ce232ce0 | 850 | total += VLAN_HLEN; |
f09e2249 | 851 | |
6c36d2e2 HX |
852 | ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); |
853 | if (ret || !iov_iter_count(iter)) | |
f09e2249 BG |
854 | goto done; |
855 | ||
6c36d2e2 HX |
856 | ret = copy_to_iter(&veth, sizeof(veth), iter); |
857 | if (ret != sizeof(veth) || !iov_iter_count(iter)) | |
f09e2249 BG |
858 | goto done; |
859 | } | |
20d29d7a | 860 | |
6c36d2e2 HX |
861 | ret = skb_copy_datagram_iter(skb, vlan_offset, iter, |
862 | skb->len - vlan_offset); | |
20d29d7a | 863 | |
f09e2249 | 864 | done: |
ce232ce0 | 865 | return ret ? ret : total; |
20d29d7a AB |
866 | } |
867 | ||
55ec8e25 | 868 | static ssize_t macvtap_do_read(struct macvtap_queue *q, |
3af0bfe5 | 869 | struct iov_iter *to, |
501c774c | 870 | int noblock) |
20d29d7a | 871 | { |
ccf7e72b | 872 | DEFINE_WAIT(wait); |
20d29d7a | 873 | struct sk_buff *skb; |
501c774c | 874 | ssize_t ret = 0; |
20d29d7a | 875 | |
3af0bfe5 AV |
876 | if (!iov_iter_count(to)) |
877 | return 0; | |
878 | ||
879 | while (1) { | |
89cee917 JW |
880 | if (!noblock) |
881 | prepare_to_wait(sk_sleep(&q->sk), &wait, | |
882 | TASK_INTERRUPTIBLE); | |
20d29d7a AB |
883 | |
884 | /* Read frames from the queue */ | |
362899b8 | 885 | skb = skb_array_consume(&q->skb_array); |
3af0bfe5 AV |
886 | if (skb) |
887 | break; | |
888 | if (noblock) { | |
889 | ret = -EAGAIN; | |
890 | break; | |
20d29d7a | 891 | } |
3af0bfe5 AV |
892 | if (signal_pending(current)) { |
893 | ret = -ERESTARTSYS; | |
894 | break; | |
895 | } | |
896 | /* Nothing to read, let's sleep */ | |
897 | schedule(); | |
898 | } | |
a499a2e9 VY |
899 | if (!noblock) |
900 | finish_wait(sk_sleep(&q->sk), &wait); | |
901 | ||
3af0bfe5 AV |
902 | if (skb) { |
903 | ret = macvtap_put_user(q, skb, to); | |
f51a5e82 JW |
904 | if (unlikely(ret < 0)) |
905 | kfree_skb(skb); | |
906 | else | |
907 | consume_skb(skb); | |
20d29d7a | 908 | } |
501c774c AB |
909 | return ret; |
910 | } | |
911 | ||
3af0bfe5 | 912 | static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to) |
501c774c AB |
913 | { |
914 | struct file *file = iocb->ki_filp; | |
915 | struct macvtap_queue *q = file->private_data; | |
3af0bfe5 | 916 | ssize_t len = iov_iter_count(to), ret; |
20d29d7a | 917 | |
3af0bfe5 | 918 | ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK); |
ce232ce0 | 919 | ret = min_t(ssize_t, ret, len); |
e6ebc7f1 ZYW |
920 | if (ret > 0) |
921 | iocb->ki_pos = ret; | |
20d29d7a AB |
922 | return ret; |
923 | } | |
924 | ||
8f475a31 JW |
925 | static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q) |
926 | { | |
927 | struct macvlan_dev *vlan; | |
928 | ||
441ac0fc VY |
929 | ASSERT_RTNL(); |
930 | vlan = rtnl_dereference(q->vlan); | |
8f475a31 JW |
931 | if (vlan) |
932 | dev_hold(vlan->dev); | |
8f475a31 JW |
933 | |
934 | return vlan; | |
935 | } | |
936 | ||
937 | static void macvtap_put_vlan(struct macvlan_dev *vlan) | |
938 | { | |
939 | dev_put(vlan->dev); | |
940 | } | |
941 | ||
815f236d JW |
942 | static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags) |
943 | { | |
944 | struct macvtap_queue *q = file->private_data; | |
945 | struct macvlan_dev *vlan; | |
946 | int ret; | |
947 | ||
948 | vlan = macvtap_get_vlan(q); | |
949 | if (!vlan) | |
950 | return -EINVAL; | |
951 | ||
952 | if (flags & IFF_ATTACH_QUEUE) | |
953 | ret = macvtap_enable_queue(vlan->dev, file, q); | |
954 | else if (flags & IFF_DETACH_QUEUE) | |
955 | ret = macvtap_disable_queue(q); | |
f57855a5 JW |
956 | else |
957 | ret = -EINVAL; | |
815f236d JW |
958 | |
959 | macvtap_put_vlan(vlan); | |
960 | return ret; | |
961 | } | |
962 | ||
2be5c767 VY |
963 | static int set_offload(struct macvtap_queue *q, unsigned long arg) |
964 | { | |
965 | struct macvlan_dev *vlan; | |
966 | netdev_features_t features; | |
967 | netdev_features_t feature_mask = 0; | |
968 | ||
969 | vlan = rtnl_dereference(q->vlan); | |
970 | if (!vlan) | |
971 | return -ENOLINK; | |
972 | ||
973 | features = vlan->dev->features; | |
974 | ||
975 | if (arg & TUN_F_CSUM) { | |
976 | feature_mask = NETIF_F_HW_CSUM; | |
977 | ||
978 | if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) { | |
979 | if (arg & TUN_F_TSO_ECN) | |
980 | feature_mask |= NETIF_F_TSO_ECN; | |
981 | if (arg & TUN_F_TSO4) | |
982 | feature_mask |= NETIF_F_TSO; | |
983 | if (arg & TUN_F_TSO6) | |
984 | feature_mask |= NETIF_F_TSO6; | |
985 | } | |
e3e3c423 VY |
986 | |
987 | if (arg & TUN_F_UFO) | |
988 | feature_mask |= NETIF_F_UFO; | |
2be5c767 VY |
989 | } |
990 | ||
991 | /* tun/tap driver inverts the usage for TSO offloads, where | |
992 | * setting the TSO bit means that the userspace wants to | |
993 | * accept TSO frames and turning it off means that user space | |
994 | * does not support TSO. | |
995 | * For macvtap, we have to invert it to mean the same thing. | |
996 | * When user space turns off TSO, we turn off GSO/LRO so that | |
997 | * user-space will not receive TSO frames. | |
998 | */ | |
e3e3c423 | 999 | if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO)) |
2be5c767 VY |
1000 | features |= RX_OFFLOADS; |
1001 | else | |
1002 | features &= ~RX_OFFLOADS; | |
1003 | ||
1004 | /* tap_features are the same as features on tun/tap and | |
1005 | * reflect user expectations. | |
1006 | */ | |
a567dd62 | 1007 | vlan->tap_features = feature_mask; |
2be5c767 VY |
1008 | vlan->set_features = features; |
1009 | netdev_update_features(vlan->dev); | |
1010 | ||
1011 | return 0; | |
1012 | } | |
1013 | ||
20d29d7a AB |
1014 | /* |
1015 | * provide compatibility with generic tun/tap interface | |
1016 | */ | |
1017 | static long macvtap_ioctl(struct file *file, unsigned int cmd, | |
1018 | unsigned long arg) | |
1019 | { | |
02df55d2 AB |
1020 | struct macvtap_queue *q = file->private_data; |
1021 | struct macvlan_dev *vlan; | |
20d29d7a AB |
1022 | void __user *argp = (void __user *)arg; |
1023 | struct ifreq __user *ifr = argp; | |
1024 | unsigned int __user *up = argp; | |
39ec7de7 | 1025 | unsigned short u; |
55afbd08 | 1026 | int __user *sp = argp; |
7f460d30 | 1027 | struct sockaddr sa; |
55afbd08 | 1028 | int s; |
02df55d2 | 1029 | int ret; |
20d29d7a AB |
1030 | |
1031 | switch (cmd) { | |
1032 | case TUNSETIFF: | |
1033 | /* ignore the name, just look at flags */ | |
1034 | if (get_user(u, &ifr->ifr_flags)) | |
1035 | return -EFAULT; | |
b9fb9ee0 AB |
1036 | |
1037 | ret = 0; | |
6ae7feb3 | 1038 | if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP)) |
b9fb9ee0 AB |
1039 | ret = -EINVAL; |
1040 | else | |
39ec7de7 | 1041 | q->flags = (q->flags & ~MACVTAP_FEATURES) | u; |
b9fb9ee0 AB |
1042 | |
1043 | return ret; | |
20d29d7a AB |
1044 | |
1045 | case TUNGETIFF: | |
441ac0fc | 1046 | rtnl_lock(); |
8f475a31 | 1047 | vlan = macvtap_get_vlan(q); |
441ac0fc VY |
1048 | if (!vlan) { |
1049 | rtnl_unlock(); | |
20d29d7a | 1050 | return -ENOLINK; |
441ac0fc | 1051 | } |
20d29d7a | 1052 | |
02df55d2 | 1053 | ret = 0; |
39ec7de7 | 1054 | u = q->flags; |
13707f9e | 1055 | if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) || |
39ec7de7 | 1056 | put_user(u, &ifr->ifr_flags)) |
02df55d2 | 1057 | ret = -EFAULT; |
8f475a31 | 1058 | macvtap_put_vlan(vlan); |
441ac0fc | 1059 | rtnl_unlock(); |
02df55d2 | 1060 | return ret; |
20d29d7a | 1061 | |
815f236d JW |
1062 | case TUNSETQUEUE: |
1063 | if (get_user(u, &ifr->ifr_flags)) | |
1064 | return -EFAULT; | |
441ac0fc VY |
1065 | rtnl_lock(); |
1066 | ret = macvtap_ioctl_set_queue(file, u); | |
1067 | rtnl_unlock(); | |
82a19eb8 | 1068 | return ret; |
815f236d | 1069 | |
20d29d7a | 1070 | case TUNGETFEATURES: |
6ae7feb3 | 1071 | if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up)) |
20d29d7a AB |
1072 | return -EFAULT; |
1073 | return 0; | |
1074 | ||
1075 | case TUNSETSNDBUF: | |
3ea79249 | 1076 | if (get_user(s, sp)) |
20d29d7a AB |
1077 | return -EFAULT; |
1078 | ||
3ea79249 | 1079 | q->sk.sk_sndbuf = s; |
20d29d7a AB |
1080 | return 0; |
1081 | ||
55afbd08 MT |
1082 | case TUNGETVNETHDRSZ: |
1083 | s = q->vnet_hdr_sz; | |
1084 | if (put_user(s, sp)) | |
1085 | return -EFAULT; | |
1086 | return 0; | |
1087 | ||
1088 | case TUNSETVNETHDRSZ: | |
1089 | if (get_user(s, sp)) | |
1090 | return -EFAULT; | |
1091 | if (s < (int)sizeof(struct virtio_net_hdr)) | |
1092 | return -EINVAL; | |
1093 | ||
1094 | q->vnet_hdr_sz = s; | |
1095 | return 0; | |
1096 | ||
01b07fb3 MT |
1097 | case TUNGETVNETLE: |
1098 | s = !!(q->flags & MACVTAP_VNET_LE); | |
1099 | if (put_user(s, sp)) | |
1100 | return -EFAULT; | |
1101 | return 0; | |
1102 | ||
1103 | case TUNSETVNETLE: | |
1104 | if (get_user(s, sp)) | |
1105 | return -EFAULT; | |
1106 | if (s) | |
1107 | q->flags |= MACVTAP_VNET_LE; | |
1108 | else | |
1109 | q->flags &= ~MACVTAP_VNET_LE; | |
1110 | return 0; | |
1111 | ||
8b8e658b GK |
1112 | case TUNGETVNETBE: |
1113 | return macvtap_get_vnet_be(q, sp); | |
1114 | ||
1115 | case TUNSETVNETBE: | |
1116 | return macvtap_set_vnet_be(q, sp); | |
1117 | ||
20d29d7a AB |
1118 | case TUNSETOFFLOAD: |
1119 | /* let the user check for future flags */ | |
1120 | if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | | |
e3e3c423 | 1121 | TUN_F_TSO_ECN | TUN_F_UFO)) |
20d29d7a AB |
1122 | return -EINVAL; |
1123 | ||
2be5c767 VY |
1124 | rtnl_lock(); |
1125 | ret = set_offload(q, arg); | |
1126 | rtnl_unlock(); | |
1127 | return ret; | |
20d29d7a | 1128 | |
b5082083 JC |
1129 | case SIOCGIFHWADDR: |
1130 | rtnl_lock(); | |
1131 | vlan = macvtap_get_vlan(q); | |
1132 | if (!vlan) { | |
1133 | rtnl_unlock(); | |
1134 | return -ENOLINK; | |
1135 | } | |
1136 | ret = 0; | |
1137 | u = vlan->dev->type; | |
1138 | if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) || | |
1139 | copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) || | |
1140 | put_user(u, &ifr->ifr_hwaddr.sa_family)) | |
1141 | ret = -EFAULT; | |
1142 | macvtap_put_vlan(vlan); | |
1143 | rtnl_unlock(); | |
1144 | return ret; | |
1145 | ||
1146 | case SIOCSIFHWADDR: | |
7f460d30 JC |
1147 | if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa))) |
1148 | return -EFAULT; | |
b5082083 JC |
1149 | rtnl_lock(); |
1150 | vlan = macvtap_get_vlan(q); | |
1151 | if (!vlan) { | |
1152 | rtnl_unlock(); | |
1153 | return -ENOLINK; | |
1154 | } | |
7f460d30 | 1155 | ret = dev_set_mac_address(vlan->dev, &sa); |
b5082083 JC |
1156 | macvtap_put_vlan(vlan); |
1157 | rtnl_unlock(); | |
1158 | return ret; | |
1159 | ||
20d29d7a AB |
1160 | default: |
1161 | return -EINVAL; | |
1162 | } | |
1163 | } | |
1164 | ||
1165 | #ifdef CONFIG_COMPAT | |
1166 | static long macvtap_compat_ioctl(struct file *file, unsigned int cmd, | |
1167 | unsigned long arg) | |
1168 | { | |
1169 | return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); | |
1170 | } | |
1171 | #endif | |
1172 | ||
1173 | static const struct file_operations macvtap_fops = { | |
1174 | .owner = THIS_MODULE, | |
1175 | .open = macvtap_open, | |
1176 | .release = macvtap_release, | |
3af0bfe5 | 1177 | .read_iter = macvtap_read_iter, |
f5ff53b4 | 1178 | .write_iter = macvtap_write_iter, |
20d29d7a AB |
1179 | .poll = macvtap_poll, |
1180 | .llseek = no_llseek, | |
1181 | .unlocked_ioctl = macvtap_ioctl, | |
1182 | #ifdef CONFIG_COMPAT | |
1183 | .compat_ioctl = macvtap_compat_ioctl, | |
1184 | #endif | |
1185 | }; | |
1186 | ||
1b784140 YX |
1187 | static int macvtap_sendmsg(struct socket *sock, struct msghdr *m, |
1188 | size_t total_len) | |
501c774c AB |
1189 | { |
1190 | struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock); | |
c0371da6 | 1191 | return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT); |
501c774c AB |
1192 | } |
1193 | ||
1b784140 YX |
1194 | static int macvtap_recvmsg(struct socket *sock, struct msghdr *m, |
1195 | size_t total_len, int flags) | |
501c774c AB |
1196 | { |
1197 | struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock); | |
1198 | int ret; | |
1199 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) | |
1200 | return -EINVAL; | |
c0371da6 | 1201 | ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT); |
de2aa476 DM |
1202 | if (ret > total_len) { |
1203 | m->msg_flags |= MSG_TRUNC; | |
1204 | ret = flags & MSG_TRUNC ? ret : total_len; | |
1205 | } | |
501c774c AB |
1206 | return ret; |
1207 | } | |
1208 | ||
362899b8 JW |
1209 | static int macvtap_peek_len(struct socket *sock) |
1210 | { | |
1211 | struct macvtap_queue *q = container_of(sock, struct macvtap_queue, | |
1212 | sock); | |
1213 | return skb_array_peek_len(&q->skb_array); | |
1214 | } | |
1215 | ||
501c774c AB |
1216 | /* Ops structure to mimic raw sockets with tun */ |
1217 | static const struct proto_ops macvtap_socket_ops = { | |
1218 | .sendmsg = macvtap_sendmsg, | |
1219 | .recvmsg = macvtap_recvmsg, | |
362899b8 | 1220 | .peek_len = macvtap_peek_len, |
501c774c AB |
1221 | }; |
1222 | ||
1223 | /* Get an underlying socket object from tun file. Returns error unless file is | |
1224 | * attached to a device. The returned object works like a packet socket, it | |
1225 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for | |
1226 | * holding a reference to the file for as long as the socket is in use. */ | |
1227 | struct socket *macvtap_get_socket(struct file *file) | |
1228 | { | |
1229 | struct macvtap_queue *q; | |
1230 | if (file->f_op != &macvtap_fops) | |
1231 | return ERR_PTR(-EINVAL); | |
1232 | q = file->private_data; | |
1233 | if (!q) | |
1234 | return ERR_PTR(-EBADFD); | |
1235 | return &q->sock; | |
1236 | } | |
1237 | EXPORT_SYMBOL_GPL(macvtap_get_socket); | |
1238 | ||
362899b8 JW |
1239 | static int macvtap_queue_resize(struct macvlan_dev *vlan) |
1240 | { | |
1241 | struct net_device *dev = vlan->dev; | |
1242 | struct macvtap_queue *q; | |
1243 | struct skb_array **arrays; | |
1244 | int n = vlan->numqueues; | |
1245 | int ret, i = 0; | |
1246 | ||
1247 | arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL); | |
1248 | if (!arrays) | |
1249 | return -ENOMEM; | |
1250 | ||
1251 | list_for_each_entry(q, &vlan->queue_list, next) | |
1252 | arrays[i++] = &q->skb_array; | |
1253 | ||
1254 | ret = skb_array_resize_multiple(arrays, n, | |
1255 | dev->tx_queue_len, GFP_KERNEL); | |
1256 | ||
1257 | kfree(arrays); | |
1258 | return ret; | |
1259 | } | |
1260 | ||
9bf1907f EB |
1261 | static int macvtap_device_event(struct notifier_block *unused, |
1262 | unsigned long event, void *ptr) | |
1263 | { | |
351638e7 | 1264 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
e09eff7f | 1265 | struct macvlan_dev *vlan; |
9bf1907f EB |
1266 | struct device *classdev; |
1267 | dev_t devt; | |
e09eff7f | 1268 | int err; |
17af2bce | 1269 | char tap_name[IFNAMSIZ]; |
9bf1907f EB |
1270 | |
1271 | if (dev->rtnl_link_ops != &macvtap_link_ops) | |
1272 | return NOTIFY_DONE; | |
1273 | ||
17af2bce | 1274 | snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex); |
e09eff7f | 1275 | vlan = netdev_priv(dev); |
9bf1907f EB |
1276 | |
1277 | switch (event) { | |
1278 | case NETDEV_REGISTER: | |
1279 | /* Create the device node here after the network device has | |
1280 | * been registered but before register_netdevice has | |
1281 | * finished running. | |
1282 | */ | |
e09eff7f EB |
1283 | err = macvtap_get_minor(vlan); |
1284 | if (err) | |
1285 | return notifier_from_errno(err); | |
1286 | ||
1287 | devt = MKDEV(MAJOR(macvtap_major), vlan->minor); | |
17af2bce MA |
1288 | classdev = device_create(&macvtap_class, &dev->dev, devt, |
1289 | dev, tap_name); | |
e09eff7f EB |
1290 | if (IS_ERR(classdev)) { |
1291 | macvtap_free_minor(vlan); | |
9bf1907f | 1292 | return notifier_from_errno(PTR_ERR(classdev)); |
e09eff7f | 1293 | } |
17af2bce MA |
1294 | err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj, |
1295 | tap_name); | |
1296 | if (err) | |
1297 | return notifier_from_errno(err); | |
9bf1907f EB |
1298 | break; |
1299 | case NETDEV_UNREGISTER: | |
e96c37f1 FR |
1300 | /* vlan->minor == 0 if NETDEV_REGISTER above failed */ |
1301 | if (vlan->minor == 0) | |
1302 | break; | |
17af2bce | 1303 | sysfs_remove_link(&dev->dev.kobj, tap_name); |
e09eff7f | 1304 | devt = MKDEV(MAJOR(macvtap_major), vlan->minor); |
17af2bce | 1305 | device_destroy(&macvtap_class, devt); |
e09eff7f | 1306 | macvtap_free_minor(vlan); |
9bf1907f | 1307 | break; |
362899b8 JW |
1308 | case NETDEV_CHANGE_TX_QUEUE_LEN: |
1309 | if (macvtap_queue_resize(vlan)) | |
1310 | return NOTIFY_BAD; | |
1311 | break; | |
9bf1907f EB |
1312 | } |
1313 | ||
1314 | return NOTIFY_DONE; | |
1315 | } | |
1316 | ||
1317 | static struct notifier_block macvtap_notifier_block __read_mostly = { | |
1318 | .notifier_call = macvtap_device_event, | |
1319 | }; | |
1320 | ||
20d29d7a AB |
1321 | static int macvtap_init(void) |
1322 | { | |
1323 | int err; | |
1324 | ||
1325 | err = alloc_chrdev_region(&macvtap_major, 0, | |
1326 | MACVTAP_NUM_DEVS, "macvtap"); | |
1327 | if (err) | |
1328 | goto out1; | |
1329 | ||
1330 | cdev_init(&macvtap_cdev, &macvtap_fops); | |
1331 | err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS); | |
1332 | if (err) | |
1333 | goto out2; | |
1334 | ||
17af2bce MA |
1335 | err = class_register(&macvtap_class); |
1336 | if (err) | |
20d29d7a | 1337 | goto out3; |
20d29d7a | 1338 | |
9bf1907f | 1339 | err = register_netdevice_notifier(&macvtap_notifier_block); |
20d29d7a AB |
1340 | if (err) |
1341 | goto out4; | |
1342 | ||
9bf1907f EB |
1343 | err = macvlan_link_register(&macvtap_link_ops); |
1344 | if (err) | |
1345 | goto out5; | |
1346 | ||
20d29d7a AB |
1347 | return 0; |
1348 | ||
9bf1907f EB |
1349 | out5: |
1350 | unregister_netdevice_notifier(&macvtap_notifier_block); | |
20d29d7a | 1351 | out4: |
17af2bce | 1352 | class_unregister(&macvtap_class); |
20d29d7a AB |
1353 | out3: |
1354 | cdev_del(&macvtap_cdev); | |
1355 | out2: | |
1356 | unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS); | |
1357 | out1: | |
1358 | return err; | |
1359 | } | |
1360 | module_init(macvtap_init); | |
1361 | ||
1362 | static void macvtap_exit(void) | |
1363 | { | |
1364 | rtnl_link_unregister(&macvtap_link_ops); | |
9bf1907f | 1365 | unregister_netdevice_notifier(&macvtap_notifier_block); |
17af2bce | 1366 | class_unregister(&macvtap_class); |
20d29d7a AB |
1367 | cdev_del(&macvtap_cdev); |
1368 | unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS); | |
d5de1987 | 1369 | idr_destroy(&minor_idr); |
20d29d7a AB |
1370 | } |
1371 | module_exit(macvtap_exit); | |
1372 | ||
1373 | MODULE_ALIAS_RTNL_LINK("macvtap"); | |
1374 | MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); | |
1375 | MODULE_LICENSE("GPL"); |