Commit | Line | Data |
---|---|---|
e314dbdc PE |
1 | /* |
2 | * drivers/net/veth.c | |
3 | * | |
4 | * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc | |
5 | * | |
6 | * Author: Pavel Emelianov <xemul@openvz.org> | |
7 | * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com> | |
8 | * | |
9 | */ | |
10 | ||
e314dbdc | 11 | #include <linux/netdevice.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
e314dbdc PE |
13 | #include <linux/ethtool.h> |
14 | #include <linux/etherdevice.h> | |
cf05c700 | 15 | #include <linux/u64_stats_sync.h> |
e314dbdc | 16 | |
f7b12606 | 17 | #include <net/rtnetlink.h> |
e314dbdc PE |
18 | #include <net/dst.h> |
19 | #include <net/xfrm.h> | |
af87a3aa | 20 | #include <net/xdp.h> |
ecef969e | 21 | #include <linux/veth.h> |
9d9779e7 | 22 | #include <linux/module.h> |
948d4f21 TM |
23 | #include <linux/bpf.h> |
24 | #include <linux/filter.h> | |
25 | #include <linux/ptr_ring.h> | |
948d4f21 | 26 | #include <linux/bpf_trace.h> |
e314dbdc PE |
27 | |
28 | #define DRV_NAME "veth" | |
29 | #define DRV_VERSION "1.0" | |
30 | ||
9fc8d518 | 31 | #define VETH_XDP_FLAG BIT(0) |
948d4f21 TM |
32 | #define VETH_RING_SIZE 256 |
33 | #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN) | |
34 | ||
d1396004 TM |
35 | /* Separating two types of XDP xmit */ |
36 | #define VETH_XDP_TX BIT(0) | |
37 | #define VETH_XDP_REDIR BIT(1) | |
38 | ||
2681128f ED |
39 | struct pcpu_vstats { |
40 | u64 packets; | |
41 | u64 bytes; | |
cf05c700 | 42 | struct u64_stats_sync syncp; |
e314dbdc PE |
43 | }; |
44 | ||
45 | struct veth_priv { | |
948d4f21 TM |
46 | struct napi_struct xdp_napi; |
47 | struct net_device *dev; | |
48 | struct bpf_prog __rcu *xdp_prog; | |
49 | struct bpf_prog *_xdp_prog; | |
d0e2c55e | 50 | struct net_device __rcu *peer; |
2681128f | 51 | atomic64_t dropped; |
d1396004 | 52 | struct xdp_mem_info xdp_mem; |
163e5292 | 53 | unsigned requested_headroom; |
948d4f21 TM |
54 | bool rx_notify_masked; |
55 | struct ptr_ring xdp_ring; | |
56 | struct xdp_rxq_info xdp_rxq; | |
e314dbdc PE |
57 | }; |
58 | ||
e314dbdc PE |
59 | /* |
60 | * ethtool interface | |
61 | */ | |
62 | ||
63 | static struct { | |
64 | const char string[ETH_GSTRING_LEN]; | |
65 | } ethtool_stats_keys[] = { | |
66 | { "peer_ifindex" }, | |
67 | }; | |
68 | ||
56607b98 PR |
69 | static int veth_get_link_ksettings(struct net_device *dev, |
70 | struct ethtool_link_ksettings *cmd) | |
e314dbdc | 71 | { |
56607b98 PR |
72 | cmd->base.speed = SPEED_10000; |
73 | cmd->base.duplex = DUPLEX_FULL; | |
74 | cmd->base.port = PORT_TP; | |
75 | cmd->base.autoneg = AUTONEG_DISABLE; | |
e314dbdc PE |
76 | return 0; |
77 | } | |
78 | ||
79 | static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) | |
80 | { | |
33a5ba14 RJ |
81 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
82 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); | |
e314dbdc PE |
83 | } |
84 | ||
85 | static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf) | |
86 | { | |
87 | switch(stringset) { | |
88 | case ETH_SS_STATS: | |
89 | memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); | |
90 | break; | |
91 | } | |
92 | } | |
93 | ||
b9f2c044 | 94 | static int veth_get_sset_count(struct net_device *dev, int sset) |
e314dbdc | 95 | { |
b9f2c044 JG |
96 | switch (sset) { |
97 | case ETH_SS_STATS: | |
98 | return ARRAY_SIZE(ethtool_stats_keys); | |
99 | default: | |
100 | return -EOPNOTSUPP; | |
101 | } | |
e314dbdc PE |
102 | } |
103 | ||
104 | static void veth_get_ethtool_stats(struct net_device *dev, | |
105 | struct ethtool_stats *stats, u64 *data) | |
106 | { | |
d0e2c55e ED |
107 | struct veth_priv *priv = netdev_priv(dev); |
108 | struct net_device *peer = rtnl_dereference(priv->peer); | |
e314dbdc | 109 | |
d0e2c55e | 110 | data[0] = peer ? peer->ifindex : 0; |
e314dbdc PE |
111 | } |
112 | ||
0fc0b732 | 113 | static const struct ethtool_ops veth_ethtool_ops = { |
e314dbdc PE |
114 | .get_drvinfo = veth_get_drvinfo, |
115 | .get_link = ethtool_op_get_link, | |
e314dbdc | 116 | .get_strings = veth_get_strings, |
b9f2c044 | 117 | .get_sset_count = veth_get_sset_count, |
e314dbdc | 118 | .get_ethtool_stats = veth_get_ethtool_stats, |
56607b98 | 119 | .get_link_ksettings = veth_get_link_ksettings, |
e314dbdc PE |
120 | }; |
121 | ||
948d4f21 TM |
122 | /* general routines */ |
123 | ||
9fc8d518 TM |
124 | static bool veth_is_xdp_frame(void *ptr) |
125 | { | |
126 | return (unsigned long)ptr & VETH_XDP_FLAG; | |
127 | } | |
128 | ||
129 | static void *veth_ptr_to_xdp(void *ptr) | |
130 | { | |
131 | return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG); | |
132 | } | |
133 | ||
af87a3aa TM |
134 | static void *veth_xdp_to_ptr(void *ptr) |
135 | { | |
136 | return (void *)((unsigned long)ptr | VETH_XDP_FLAG); | |
137 | } | |
138 | ||
9fc8d518 TM |
139 | static void veth_ptr_free(void *ptr) |
140 | { | |
141 | if (veth_is_xdp_frame(ptr)) | |
142 | xdp_return_frame(veth_ptr_to_xdp(ptr)); | |
143 | else | |
144 | kfree_skb(ptr); | |
145 | } | |
146 | ||
948d4f21 TM |
147 | static void __veth_xdp_flush(struct veth_priv *priv) |
148 | { | |
149 | /* Write ptr_ring before reading rx_notify_masked */ | |
150 | smp_mb(); | |
151 | if (!priv->rx_notify_masked) { | |
152 | priv->rx_notify_masked = true; | |
153 | napi_schedule(&priv->xdp_napi); | |
154 | } | |
155 | } | |
156 | ||
157 | static int veth_xdp_rx(struct veth_priv *priv, struct sk_buff *skb) | |
158 | { | |
159 | if (unlikely(ptr_ring_produce(&priv->xdp_ring, skb))) { | |
160 | dev_kfree_skb_any(skb); | |
161 | return NET_RX_DROP; | |
162 | } | |
163 | ||
164 | return NET_RX_SUCCESS; | |
165 | } | |
166 | ||
167 | static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb, bool xdp) | |
e314dbdc | 168 | { |
2681128f | 169 | struct veth_priv *priv = netdev_priv(dev); |
948d4f21 TM |
170 | |
171 | return __dev_forward_skb(dev, skb) ?: xdp ? | |
172 | veth_xdp_rx(priv, skb) : | |
173 | netif_rx(skb); | |
174 | } | |
175 | ||
176 | static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev) | |
177 | { | |
178 | struct veth_priv *rcv_priv, *priv = netdev_priv(dev); | |
d0e2c55e | 179 | struct net_device *rcv; |
2681128f | 180 | int length = skb->len; |
948d4f21 | 181 | bool rcv_xdp = false; |
e314dbdc | 182 | |
d0e2c55e ED |
183 | rcu_read_lock(); |
184 | rcv = rcu_dereference(priv->peer); | |
185 | if (unlikely(!rcv)) { | |
186 | kfree_skb(skb); | |
187 | goto drop; | |
188 | } | |
e314dbdc | 189 | |
948d4f21 TM |
190 | rcv_priv = netdev_priv(rcv); |
191 | rcv_xdp = rcu_access_pointer(rcv_priv->xdp_prog); | |
192 | ||
193 | if (likely(veth_forward_skb(rcv, skb, rcv_xdp) == NET_RX_SUCCESS)) { | |
2681128f | 194 | struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats); |
e314dbdc | 195 | |
2681128f ED |
196 | u64_stats_update_begin(&stats->syncp); |
197 | stats->bytes += length; | |
198 | stats->packets++; | |
199 | u64_stats_update_end(&stats->syncp); | |
200 | } else { | |
d0e2c55e | 201 | drop: |
2681128f ED |
202 | atomic64_inc(&priv->dropped); |
203 | } | |
948d4f21 TM |
204 | |
205 | if (rcv_xdp) | |
206 | __veth_xdp_flush(rcv_priv); | |
207 | ||
d0e2c55e | 208 | rcu_read_unlock(); |
948d4f21 | 209 | |
6ed10654 | 210 | return NETDEV_TX_OK; |
e314dbdc PE |
211 | } |
212 | ||
2681128f | 213 | static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev) |
e314dbdc | 214 | { |
cf05c700 | 215 | struct veth_priv *priv = netdev_priv(dev); |
11687a10 | 216 | int cpu; |
e314dbdc | 217 | |
2681128f ED |
218 | result->packets = 0; |
219 | result->bytes = 0; | |
2b1c8b0f | 220 | for_each_possible_cpu(cpu) { |
2681128f ED |
221 | struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu); |
222 | u64 packets, bytes; | |
cf05c700 ED |
223 | unsigned int start; |
224 | ||
225 | do { | |
57a7744e | 226 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
2681128f ED |
227 | packets = stats->packets; |
228 | bytes = stats->bytes; | |
57a7744e | 229 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
2681128f ED |
230 | result->packets += packets; |
231 | result->bytes += bytes; | |
11687a10 | 232 | } |
2681128f ED |
233 | return atomic64_read(&priv->dropped); |
234 | } | |
235 | ||
bc1f4470 | 236 | static void veth_get_stats64(struct net_device *dev, |
237 | struct rtnl_link_stats64 *tot) | |
2681128f ED |
238 | { |
239 | struct veth_priv *priv = netdev_priv(dev); | |
d0e2c55e | 240 | struct net_device *peer; |
2681128f ED |
241 | struct pcpu_vstats one; |
242 | ||
243 | tot->tx_dropped = veth_stats_one(&one, dev); | |
244 | tot->tx_bytes = one.bytes; | |
245 | tot->tx_packets = one.packets; | |
246 | ||
d0e2c55e ED |
247 | rcu_read_lock(); |
248 | peer = rcu_dereference(priv->peer); | |
249 | if (peer) { | |
250 | tot->rx_dropped = veth_stats_one(&one, peer); | |
251 | tot->rx_bytes = one.bytes; | |
252 | tot->rx_packets = one.packets; | |
253 | } | |
254 | rcu_read_unlock(); | |
e314dbdc PE |
255 | } |
256 | ||
5c70ef85 G |
257 | /* fake multicast ability */ |
258 | static void veth_set_multicast_list(struct net_device *dev) | |
259 | { | |
260 | } | |
261 | ||
948d4f21 TM |
262 | static struct sk_buff *veth_build_skb(void *head, int headroom, int len, |
263 | int buflen) | |
264 | { | |
265 | struct sk_buff *skb; | |
266 | ||
267 | if (!buflen) { | |
268 | buflen = SKB_DATA_ALIGN(headroom + len) + | |
269 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
270 | } | |
271 | skb = build_skb(head, buflen); | |
272 | if (!skb) | |
273 | return NULL; | |
274 | ||
275 | skb_reserve(skb, headroom); | |
276 | skb_put(skb, len); | |
277 | ||
278 | return skb; | |
279 | } | |
280 | ||
af87a3aa TM |
281 | static int veth_xdp_xmit(struct net_device *dev, int n, |
282 | struct xdp_frame **frames, u32 flags) | |
283 | { | |
284 | struct veth_priv *rcv_priv, *priv = netdev_priv(dev); | |
285 | struct net_device *rcv; | |
286 | unsigned int max_len; | |
287 | int i, drops = 0; | |
288 | ||
289 | if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) | |
290 | return -EINVAL; | |
291 | ||
292 | rcv = rcu_dereference(priv->peer); | |
293 | if (unlikely(!rcv)) | |
294 | return -ENXIO; | |
295 | ||
296 | rcv_priv = netdev_priv(rcv); | |
297 | /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive | |
298 | * side. This means an XDP program is loaded on the peer and the peer | |
299 | * device is up. | |
300 | */ | |
301 | if (!rcu_access_pointer(rcv_priv->xdp_prog)) | |
302 | return -ENXIO; | |
303 | ||
304 | max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN; | |
305 | ||
306 | spin_lock(&rcv_priv->xdp_ring.producer_lock); | |
307 | for (i = 0; i < n; i++) { | |
308 | struct xdp_frame *frame = frames[i]; | |
309 | void *ptr = veth_xdp_to_ptr(frame); | |
310 | ||
311 | if (unlikely(frame->len > max_len || | |
312 | __ptr_ring_produce(&rcv_priv->xdp_ring, ptr))) { | |
313 | xdp_return_frame_rx_napi(frame); | |
314 | drops++; | |
315 | } | |
316 | } | |
317 | spin_unlock(&rcv_priv->xdp_ring.producer_lock); | |
318 | ||
319 | if (flags & XDP_XMIT_FLUSH) | |
320 | __veth_xdp_flush(rcv_priv); | |
321 | ||
322 | return n - drops; | |
323 | } | |
324 | ||
d1396004 TM |
325 | static void veth_xdp_flush(struct net_device *dev) |
326 | { | |
327 | struct veth_priv *rcv_priv, *priv = netdev_priv(dev); | |
328 | struct net_device *rcv; | |
329 | ||
330 | rcu_read_lock(); | |
331 | rcv = rcu_dereference(priv->peer); | |
332 | if (unlikely(!rcv)) | |
333 | goto out; | |
334 | ||
335 | rcv_priv = netdev_priv(rcv); | |
336 | /* xdp_ring is initialized on receive side? */ | |
337 | if (unlikely(!rcu_access_pointer(rcv_priv->xdp_prog))) | |
338 | goto out; | |
339 | ||
340 | __veth_xdp_flush(rcv_priv); | |
341 | out: | |
342 | rcu_read_unlock(); | |
343 | } | |
344 | ||
345 | static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp) | |
346 | { | |
347 | struct xdp_frame *frame = convert_to_xdp_frame(xdp); | |
348 | ||
349 | if (unlikely(!frame)) | |
350 | return -EOVERFLOW; | |
351 | ||
352 | return veth_xdp_xmit(dev, 1, &frame, 0); | |
353 | } | |
354 | ||
9fc8d518 | 355 | static struct sk_buff *veth_xdp_rcv_one(struct veth_priv *priv, |
d1396004 TM |
356 | struct xdp_frame *frame, |
357 | unsigned int *xdp_xmit) | |
9fc8d518 TM |
358 | { |
359 | void *hard_start = frame->data - frame->headroom; | |
360 | void *head = hard_start - sizeof(struct xdp_frame); | |
361 | int len = frame->len, delta = 0; | |
d1396004 | 362 | struct xdp_frame orig_frame; |
9fc8d518 TM |
363 | struct bpf_prog *xdp_prog; |
364 | unsigned int headroom; | |
365 | struct sk_buff *skb; | |
366 | ||
367 | rcu_read_lock(); | |
368 | xdp_prog = rcu_dereference(priv->xdp_prog); | |
369 | if (likely(xdp_prog)) { | |
370 | struct xdp_buff xdp; | |
371 | u32 act; | |
372 | ||
373 | xdp.data_hard_start = hard_start; | |
374 | xdp.data = frame->data; | |
375 | xdp.data_end = frame->data + frame->len; | |
376 | xdp.data_meta = frame->data - frame->metasize; | |
377 | xdp.rxq = &priv->xdp_rxq; | |
378 | ||
379 | act = bpf_prog_run_xdp(xdp_prog, &xdp); | |
380 | ||
381 | switch (act) { | |
382 | case XDP_PASS: | |
383 | delta = frame->data - xdp.data; | |
384 | len = xdp.data_end - xdp.data; | |
385 | break; | |
d1396004 TM |
386 | case XDP_TX: |
387 | orig_frame = *frame; | |
388 | xdp.data_hard_start = head; | |
389 | xdp.rxq->mem = frame->mem; | |
390 | if (unlikely(veth_xdp_tx(priv->dev, &xdp) < 0)) { | |
391 | trace_xdp_exception(priv->dev, xdp_prog, act); | |
392 | frame = &orig_frame; | |
393 | goto err_xdp; | |
394 | } | |
395 | *xdp_xmit |= VETH_XDP_TX; | |
396 | rcu_read_unlock(); | |
397 | goto xdp_xmit; | |
398 | case XDP_REDIRECT: | |
399 | orig_frame = *frame; | |
400 | xdp.data_hard_start = head; | |
401 | xdp.rxq->mem = frame->mem; | |
402 | if (xdp_do_redirect(priv->dev, &xdp, xdp_prog)) { | |
403 | frame = &orig_frame; | |
404 | goto err_xdp; | |
405 | } | |
406 | *xdp_xmit |= VETH_XDP_REDIR; | |
407 | rcu_read_unlock(); | |
408 | goto xdp_xmit; | |
9fc8d518 TM |
409 | default: |
410 | bpf_warn_invalid_xdp_action(act); | |
411 | case XDP_ABORTED: | |
412 | trace_xdp_exception(priv->dev, xdp_prog, act); | |
413 | case XDP_DROP: | |
414 | goto err_xdp; | |
415 | } | |
416 | } | |
417 | rcu_read_unlock(); | |
418 | ||
419 | headroom = sizeof(struct xdp_frame) + frame->headroom - delta; | |
420 | skb = veth_build_skb(head, headroom, len, 0); | |
421 | if (!skb) { | |
422 | xdp_return_frame(frame); | |
423 | goto err; | |
424 | } | |
425 | ||
426 | xdp_scrub_frame(frame); | |
427 | skb->protocol = eth_type_trans(skb, priv->dev); | |
428 | err: | |
429 | return skb; | |
430 | err_xdp: | |
431 | rcu_read_unlock(); | |
432 | xdp_return_frame(frame); | |
d1396004 | 433 | xdp_xmit: |
9fc8d518 TM |
434 | return NULL; |
435 | } | |
436 | ||
948d4f21 | 437 | static struct sk_buff *veth_xdp_rcv_skb(struct veth_priv *priv, |
d1396004 TM |
438 | struct sk_buff *skb, |
439 | unsigned int *xdp_xmit) | |
948d4f21 TM |
440 | { |
441 | u32 pktlen, headroom, act, metalen; | |
442 | void *orig_data, *orig_data_end; | |
443 | struct bpf_prog *xdp_prog; | |
444 | int mac_len, delta, off; | |
445 | struct xdp_buff xdp; | |
446 | ||
447 | rcu_read_lock(); | |
448 | xdp_prog = rcu_dereference(priv->xdp_prog); | |
449 | if (unlikely(!xdp_prog)) { | |
450 | rcu_read_unlock(); | |
451 | goto out; | |
452 | } | |
453 | ||
454 | mac_len = skb->data - skb_mac_header(skb); | |
455 | pktlen = skb->len + mac_len; | |
456 | headroom = skb_headroom(skb) - mac_len; | |
457 | ||
458 | if (skb_shared(skb) || skb_head_is_locked(skb) || | |
459 | skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) { | |
460 | struct sk_buff *nskb; | |
461 | int size, head_off; | |
462 | void *head, *start; | |
463 | struct page *page; | |
464 | ||
465 | size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) + | |
466 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
467 | if (size > PAGE_SIZE) | |
468 | goto drop; | |
469 | ||
470 | page = alloc_page(GFP_ATOMIC | __GFP_NOWARN); | |
471 | if (!page) | |
472 | goto drop; | |
473 | ||
474 | head = page_address(page); | |
475 | start = head + VETH_XDP_HEADROOM; | |
476 | if (skb_copy_bits(skb, -mac_len, start, pktlen)) { | |
477 | page_frag_free(head); | |
478 | goto drop; | |
479 | } | |
480 | ||
481 | nskb = veth_build_skb(head, | |
482 | VETH_XDP_HEADROOM + mac_len, skb->len, | |
483 | PAGE_SIZE); | |
484 | if (!nskb) { | |
485 | page_frag_free(head); | |
486 | goto drop; | |
487 | } | |
488 | ||
489 | skb_copy_header(nskb, skb); | |
490 | head_off = skb_headroom(nskb) - skb_headroom(skb); | |
491 | skb_headers_offset_update(nskb, head_off); | |
492 | if (skb->sk) | |
493 | skb_set_owner_w(nskb, skb->sk); | |
494 | consume_skb(skb); | |
495 | skb = nskb; | |
496 | } | |
497 | ||
498 | xdp.data_hard_start = skb->head; | |
499 | xdp.data = skb_mac_header(skb); | |
500 | xdp.data_end = xdp.data + pktlen; | |
501 | xdp.data_meta = xdp.data; | |
502 | xdp.rxq = &priv->xdp_rxq; | |
503 | orig_data = xdp.data; | |
504 | orig_data_end = xdp.data_end; | |
505 | ||
506 | act = bpf_prog_run_xdp(xdp_prog, &xdp); | |
507 | ||
508 | switch (act) { | |
509 | case XDP_PASS: | |
510 | break; | |
d1396004 TM |
511 | case XDP_TX: |
512 | get_page(virt_to_page(xdp.data)); | |
513 | consume_skb(skb); | |
514 | xdp.rxq->mem = priv->xdp_mem; | |
515 | if (unlikely(veth_xdp_tx(priv->dev, &xdp) < 0)) { | |
516 | trace_xdp_exception(priv->dev, xdp_prog, act); | |
517 | goto err_xdp; | |
518 | } | |
519 | *xdp_xmit |= VETH_XDP_TX; | |
520 | rcu_read_unlock(); | |
521 | goto xdp_xmit; | |
522 | case XDP_REDIRECT: | |
523 | get_page(virt_to_page(xdp.data)); | |
524 | consume_skb(skb); | |
525 | xdp.rxq->mem = priv->xdp_mem; | |
526 | if (xdp_do_redirect(priv->dev, &xdp, xdp_prog)) | |
527 | goto err_xdp; | |
528 | *xdp_xmit |= VETH_XDP_REDIR; | |
529 | rcu_read_unlock(); | |
530 | goto xdp_xmit; | |
948d4f21 TM |
531 | default: |
532 | bpf_warn_invalid_xdp_action(act); | |
533 | case XDP_ABORTED: | |
534 | trace_xdp_exception(priv->dev, xdp_prog, act); | |
535 | case XDP_DROP: | |
536 | goto drop; | |
537 | } | |
538 | rcu_read_unlock(); | |
539 | ||
540 | delta = orig_data - xdp.data; | |
541 | off = mac_len + delta; | |
542 | if (off > 0) | |
543 | __skb_push(skb, off); | |
544 | else if (off < 0) | |
545 | __skb_pull(skb, -off); | |
546 | skb->mac_header -= delta; | |
547 | off = xdp.data_end - orig_data_end; | |
548 | if (off != 0) | |
549 | __skb_put(skb, off); | |
550 | skb->protocol = eth_type_trans(skb, priv->dev); | |
551 | ||
552 | metalen = xdp.data - xdp.data_meta; | |
553 | if (metalen) | |
554 | skb_metadata_set(skb, metalen); | |
555 | out: | |
556 | return skb; | |
557 | drop: | |
558 | rcu_read_unlock(); | |
559 | kfree_skb(skb); | |
560 | return NULL; | |
d1396004 TM |
561 | err_xdp: |
562 | rcu_read_unlock(); | |
563 | page_frag_free(xdp.data); | |
564 | xdp_xmit: | |
565 | return NULL; | |
948d4f21 TM |
566 | } |
567 | ||
d1396004 TM |
568 | static int veth_xdp_rcv(struct veth_priv *priv, int budget, |
569 | unsigned int *xdp_xmit) | |
948d4f21 TM |
570 | { |
571 | int i, done = 0; | |
572 | ||
573 | for (i = 0; i < budget; i++) { | |
9fc8d518 TM |
574 | void *ptr = __ptr_ring_consume(&priv->xdp_ring); |
575 | struct sk_buff *skb; | |
948d4f21 | 576 | |
9fc8d518 | 577 | if (!ptr) |
948d4f21 TM |
578 | break; |
579 | ||
d1396004 TM |
580 | if (veth_is_xdp_frame(ptr)) { |
581 | skb = veth_xdp_rcv_one(priv, veth_ptr_to_xdp(ptr), | |
582 | xdp_xmit); | |
583 | } else { | |
584 | skb = veth_xdp_rcv_skb(priv, ptr, xdp_xmit); | |
585 | } | |
948d4f21 TM |
586 | |
587 | if (skb) | |
588 | napi_gro_receive(&priv->xdp_napi, skb); | |
589 | ||
590 | done++; | |
591 | } | |
592 | ||
593 | return done; | |
594 | } | |
595 | ||
596 | static int veth_poll(struct napi_struct *napi, int budget) | |
597 | { | |
598 | struct veth_priv *priv = | |
599 | container_of(napi, struct veth_priv, xdp_napi); | |
d1396004 | 600 | unsigned int xdp_xmit = 0; |
948d4f21 TM |
601 | int done; |
602 | ||
d1396004 TM |
603 | xdp_set_return_frame_no_direct(); |
604 | done = veth_xdp_rcv(priv, budget, &xdp_xmit); | |
948d4f21 TM |
605 | |
606 | if (done < budget && napi_complete_done(napi, done)) { | |
607 | /* Write rx_notify_masked before reading ptr_ring */ | |
608 | smp_store_mb(priv->rx_notify_masked, false); | |
609 | if (unlikely(!__ptr_ring_empty(&priv->xdp_ring))) { | |
610 | priv->rx_notify_masked = true; | |
611 | napi_schedule(&priv->xdp_napi); | |
612 | } | |
613 | } | |
614 | ||
d1396004 TM |
615 | if (xdp_xmit & VETH_XDP_TX) |
616 | veth_xdp_flush(priv->dev); | |
617 | if (xdp_xmit & VETH_XDP_REDIR) | |
618 | xdp_do_flush_map(); | |
619 | xdp_clear_return_frame_no_direct(); | |
620 | ||
948d4f21 TM |
621 | return done; |
622 | } | |
623 | ||
624 | static int veth_napi_add(struct net_device *dev) | |
625 | { | |
626 | struct veth_priv *priv = netdev_priv(dev); | |
627 | int err; | |
628 | ||
629 | err = ptr_ring_init(&priv->xdp_ring, VETH_RING_SIZE, GFP_KERNEL); | |
630 | if (err) | |
631 | return err; | |
632 | ||
633 | netif_napi_add(dev, &priv->xdp_napi, veth_poll, NAPI_POLL_WEIGHT); | |
634 | napi_enable(&priv->xdp_napi); | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
639 | static void veth_napi_del(struct net_device *dev) | |
640 | { | |
641 | struct veth_priv *priv = netdev_priv(dev); | |
642 | ||
643 | napi_disable(&priv->xdp_napi); | |
644 | netif_napi_del(&priv->xdp_napi); | |
645 | priv->rx_notify_masked = false; | |
9fc8d518 | 646 | ptr_ring_cleanup(&priv->xdp_ring, veth_ptr_free); |
948d4f21 TM |
647 | } |
648 | ||
649 | static int veth_enable_xdp(struct net_device *dev) | |
650 | { | |
651 | struct veth_priv *priv = netdev_priv(dev); | |
652 | int err; | |
653 | ||
654 | if (!xdp_rxq_info_is_reg(&priv->xdp_rxq)) { | |
655 | err = xdp_rxq_info_reg(&priv->xdp_rxq, dev, 0); | |
656 | if (err < 0) | |
657 | return err; | |
658 | ||
659 | err = xdp_rxq_info_reg_mem_model(&priv->xdp_rxq, | |
660 | MEM_TYPE_PAGE_SHARED, NULL); | |
661 | if (err < 0) | |
662 | goto err; | |
663 | ||
664 | err = veth_napi_add(dev); | |
665 | if (err) | |
666 | goto err; | |
d1396004 TM |
667 | |
668 | /* Save original mem info as it can be overwritten */ | |
669 | priv->xdp_mem = priv->xdp_rxq.mem; | |
948d4f21 TM |
670 | } |
671 | ||
672 | rcu_assign_pointer(priv->xdp_prog, priv->_xdp_prog); | |
673 | ||
674 | return 0; | |
675 | err: | |
676 | xdp_rxq_info_unreg(&priv->xdp_rxq); | |
677 | ||
678 | return err; | |
679 | } | |
680 | ||
681 | static void veth_disable_xdp(struct net_device *dev) | |
682 | { | |
683 | struct veth_priv *priv = netdev_priv(dev); | |
684 | ||
685 | rcu_assign_pointer(priv->xdp_prog, NULL); | |
686 | veth_napi_del(dev); | |
d1396004 | 687 | priv->xdp_rxq.mem = priv->xdp_mem; |
948d4f21 TM |
688 | xdp_rxq_info_unreg(&priv->xdp_rxq); |
689 | } | |
690 | ||
e314dbdc PE |
691 | static int veth_open(struct net_device *dev) |
692 | { | |
d0e2c55e ED |
693 | struct veth_priv *priv = netdev_priv(dev); |
694 | struct net_device *peer = rtnl_dereference(priv->peer); | |
948d4f21 | 695 | int err; |
e314dbdc | 696 | |
d0e2c55e | 697 | if (!peer) |
e314dbdc PE |
698 | return -ENOTCONN; |
699 | ||
948d4f21 TM |
700 | if (priv->_xdp_prog) { |
701 | err = veth_enable_xdp(dev); | |
702 | if (err) | |
703 | return err; | |
704 | } | |
705 | ||
d0e2c55e | 706 | if (peer->flags & IFF_UP) { |
e314dbdc | 707 | netif_carrier_on(dev); |
d0e2c55e | 708 | netif_carrier_on(peer); |
e314dbdc | 709 | } |
948d4f21 | 710 | |
e314dbdc PE |
711 | return 0; |
712 | } | |
713 | ||
2cf48a10 EB |
714 | static int veth_close(struct net_device *dev) |
715 | { | |
716 | struct veth_priv *priv = netdev_priv(dev); | |
2efd32ee | 717 | struct net_device *peer = rtnl_dereference(priv->peer); |
2cf48a10 EB |
718 | |
719 | netif_carrier_off(dev); | |
2efd32ee ED |
720 | if (peer) |
721 | netif_carrier_off(peer); | |
2cf48a10 | 722 | |
948d4f21 TM |
723 | if (priv->_xdp_prog) |
724 | veth_disable_xdp(dev); | |
725 | ||
2cf48a10 EB |
726 | return 0; |
727 | } | |
728 | ||
91572088 | 729 | static int is_valid_veth_mtu(int mtu) |
38d40815 | 730 | { |
91572088 | 731 | return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU; |
38d40815 EB |
732 | } |
733 | ||
e314dbdc PE |
734 | static int veth_dev_init(struct net_device *dev) |
735 | { | |
1c213bd2 | 736 | dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats); |
2681128f | 737 | if (!dev->vstats) |
e314dbdc | 738 | return -ENOMEM; |
e314dbdc PE |
739 | return 0; |
740 | } | |
741 | ||
11687a10 DM |
742 | static void veth_dev_free(struct net_device *dev) |
743 | { | |
2681128f | 744 | free_percpu(dev->vstats); |
11687a10 DM |
745 | } |
746 | ||
bb446c19 WC |
747 | #ifdef CONFIG_NET_POLL_CONTROLLER |
748 | static void veth_poll_controller(struct net_device *dev) | |
749 | { | |
750 | /* veth only receives frames when its peer sends one | |
948d4f21 | 751 | * Since it has nothing to do with disabling irqs, we are guaranteed |
bb446c19 WC |
752 | * never to have pending data when we poll for it so |
753 | * there is nothing to do here. | |
754 | * | |
755 | * We need this though so netpoll recognizes us as an interface that | |
756 | * supports polling, which enables bridge devices in virt setups to | |
757 | * still use netconsole | |
758 | */ | |
759 | } | |
760 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
761 | ||
a45253bf ND |
762 | static int veth_get_iflink(const struct net_device *dev) |
763 | { | |
764 | struct veth_priv *priv = netdev_priv(dev); | |
765 | struct net_device *peer; | |
766 | int iflink; | |
767 | ||
768 | rcu_read_lock(); | |
769 | peer = rcu_dereference(priv->peer); | |
770 | iflink = peer ? peer->ifindex : 0; | |
771 | rcu_read_unlock(); | |
772 | ||
773 | return iflink; | |
774 | } | |
775 | ||
dc224822 TM |
776 | static netdev_features_t veth_fix_features(struct net_device *dev, |
777 | netdev_features_t features) | |
778 | { | |
779 | struct veth_priv *priv = netdev_priv(dev); | |
780 | struct net_device *peer; | |
781 | ||
782 | peer = rtnl_dereference(priv->peer); | |
783 | if (peer) { | |
784 | struct veth_priv *peer_priv = netdev_priv(peer); | |
785 | ||
786 | if (peer_priv->_xdp_prog) | |
787 | features &= ~NETIF_F_GSO_SOFTWARE; | |
788 | } | |
789 | ||
790 | return features; | |
791 | } | |
792 | ||
163e5292 PA |
793 | static void veth_set_rx_headroom(struct net_device *dev, int new_hr) |
794 | { | |
795 | struct veth_priv *peer_priv, *priv = netdev_priv(dev); | |
796 | struct net_device *peer; | |
797 | ||
798 | if (new_hr < 0) | |
799 | new_hr = 0; | |
800 | ||
801 | rcu_read_lock(); | |
802 | peer = rcu_dereference(priv->peer); | |
803 | if (unlikely(!peer)) | |
804 | goto out; | |
805 | ||
806 | peer_priv = netdev_priv(peer); | |
807 | priv->requested_headroom = new_hr; | |
808 | new_hr = max(priv->requested_headroom, peer_priv->requested_headroom); | |
809 | dev->needed_headroom = new_hr; | |
810 | peer->needed_headroom = new_hr; | |
811 | ||
812 | out: | |
813 | rcu_read_unlock(); | |
814 | } | |
815 | ||
948d4f21 TM |
816 | static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog, |
817 | struct netlink_ext_ack *extack) | |
818 | { | |
819 | struct veth_priv *priv = netdev_priv(dev); | |
820 | struct bpf_prog *old_prog; | |
821 | struct net_device *peer; | |
dc224822 | 822 | unsigned int max_mtu; |
948d4f21 TM |
823 | int err; |
824 | ||
825 | old_prog = priv->_xdp_prog; | |
826 | priv->_xdp_prog = prog; | |
827 | peer = rtnl_dereference(priv->peer); | |
828 | ||
829 | if (prog) { | |
830 | if (!peer) { | |
831 | NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached"); | |
832 | err = -ENOTCONN; | |
833 | goto err; | |
834 | } | |
835 | ||
dc224822 TM |
836 | max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM - |
837 | peer->hard_header_len - | |
838 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
839 | if (peer->mtu > max_mtu) { | |
840 | NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP"); | |
841 | err = -ERANGE; | |
842 | goto err; | |
843 | } | |
844 | ||
948d4f21 TM |
845 | if (dev->flags & IFF_UP) { |
846 | err = veth_enable_xdp(dev); | |
847 | if (err) { | |
848 | NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed"); | |
849 | goto err; | |
850 | } | |
851 | } | |
dc224822 TM |
852 | |
853 | if (!old_prog) { | |
854 | peer->hw_features &= ~NETIF_F_GSO_SOFTWARE; | |
855 | peer->max_mtu = max_mtu; | |
856 | } | |
948d4f21 TM |
857 | } |
858 | ||
859 | if (old_prog) { | |
dc224822 TM |
860 | if (!prog) { |
861 | if (dev->flags & IFF_UP) | |
862 | veth_disable_xdp(dev); | |
863 | ||
864 | if (peer) { | |
865 | peer->hw_features |= NETIF_F_GSO_SOFTWARE; | |
866 | peer->max_mtu = ETH_MAX_MTU; | |
867 | } | |
868 | } | |
948d4f21 TM |
869 | bpf_prog_put(old_prog); |
870 | } | |
871 | ||
dc224822 TM |
872 | if ((!!old_prog ^ !!prog) && peer) |
873 | netdev_update_features(peer); | |
874 | ||
948d4f21 TM |
875 | return 0; |
876 | err: | |
877 | priv->_xdp_prog = old_prog; | |
878 | ||
879 | return err; | |
880 | } | |
881 | ||
882 | static u32 veth_xdp_query(struct net_device *dev) | |
883 | { | |
884 | struct veth_priv *priv = netdev_priv(dev); | |
885 | const struct bpf_prog *xdp_prog; | |
886 | ||
887 | xdp_prog = priv->_xdp_prog; | |
888 | if (xdp_prog) | |
889 | return xdp_prog->aux->id; | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp) | |
895 | { | |
896 | switch (xdp->command) { | |
897 | case XDP_SETUP_PROG: | |
898 | return veth_xdp_set(dev, xdp->prog, xdp->extack); | |
899 | case XDP_QUERY_PROG: | |
900 | xdp->prog_id = veth_xdp_query(dev); | |
901 | return 0; | |
902 | default: | |
903 | return -EINVAL; | |
904 | } | |
905 | } | |
906 | ||
4456e7bd | 907 | static const struct net_device_ops veth_netdev_ops = { |
ee923623 DL |
908 | .ndo_init = veth_dev_init, |
909 | .ndo_open = veth_open, | |
2cf48a10 | 910 | .ndo_stop = veth_close, |
ee923623 | 911 | .ndo_start_xmit = veth_xmit, |
6311cc44 | 912 | .ndo_get_stats64 = veth_get_stats64, |
5c70ef85 | 913 | .ndo_set_rx_mode = veth_set_multicast_list, |
ee923623 | 914 | .ndo_set_mac_address = eth_mac_addr, |
bb446c19 WC |
915 | #ifdef CONFIG_NET_POLL_CONTROLLER |
916 | .ndo_poll_controller = veth_poll_controller, | |
917 | #endif | |
a45253bf | 918 | .ndo_get_iflink = veth_get_iflink, |
dc224822 | 919 | .ndo_fix_features = veth_fix_features, |
1a04a821 | 920 | .ndo_features_check = passthru_features_check, |
163e5292 | 921 | .ndo_set_rx_headroom = veth_set_rx_headroom, |
948d4f21 | 922 | .ndo_bpf = veth_xdp, |
af87a3aa | 923 | .ndo_xdp_xmit = veth_xdp_xmit, |
4456e7bd SH |
924 | }; |
925 | ||
732912d7 | 926 | #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \ |
c80fafbb | 927 | NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \ |
732912d7 | 928 | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \ |
28d2b136 PM |
929 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \ |
930 | NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX ) | |
8093315a | 931 | |
e314dbdc PE |
932 | static void veth_setup(struct net_device *dev) |
933 | { | |
934 | ether_setup(dev); | |
935 | ||
550fd08c | 936 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
23ea5a96 | 937 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
02f01ec1 | 938 | dev->priv_flags |= IFF_NO_QUEUE; |
163e5292 | 939 | dev->priv_flags |= IFF_PHONY_HEADROOM; |
550fd08c | 940 | |
4456e7bd | 941 | dev->netdev_ops = &veth_netdev_ops; |
e314dbdc PE |
942 | dev->ethtool_ops = &veth_ethtool_ops; |
943 | dev->features |= NETIF_F_LLTX; | |
8093315a | 944 | dev->features |= VETH_FEATURES; |
8d0d21f4 | 945 | dev->vlan_features = dev->features & |
3f8c707b VY |
946 | ~(NETIF_F_HW_VLAN_CTAG_TX | |
947 | NETIF_F_HW_VLAN_STAG_TX | | |
948 | NETIF_F_HW_VLAN_CTAG_RX | | |
949 | NETIF_F_HW_VLAN_STAG_RX); | |
cf124db5 DM |
950 | dev->needs_free_netdev = true; |
951 | dev->priv_destructor = veth_dev_free; | |
91572088 | 952 | dev->max_mtu = ETH_MAX_MTU; |
a2c725fa | 953 | |
8093315a | 954 | dev->hw_features = VETH_FEATURES; |
82d81898 | 955 | dev->hw_enc_features = VETH_FEATURES; |
607fca9a | 956 | dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE; |
e314dbdc PE |
957 | } |
958 | ||
959 | /* | |
960 | * netlink interface | |
961 | */ | |
962 | ||
a8b8a889 MS |
963 | static int veth_validate(struct nlattr *tb[], struct nlattr *data[], |
964 | struct netlink_ext_ack *extack) | |
e314dbdc PE |
965 | { |
966 | if (tb[IFLA_ADDRESS]) { | |
967 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
968 | return -EINVAL; | |
969 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
970 | return -EADDRNOTAVAIL; | |
971 | } | |
38d40815 EB |
972 | if (tb[IFLA_MTU]) { |
973 | if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU]))) | |
974 | return -EINVAL; | |
975 | } | |
e314dbdc PE |
976 | return 0; |
977 | } | |
978 | ||
979 | static struct rtnl_link_ops veth_link_ops; | |
980 | ||
81adee47 | 981 | static int veth_newlink(struct net *src_net, struct net_device *dev, |
7a3f4a18 MS |
982 | struct nlattr *tb[], struct nlattr *data[], |
983 | struct netlink_ext_ack *extack) | |
e314dbdc PE |
984 | { |
985 | int err; | |
986 | struct net_device *peer; | |
987 | struct veth_priv *priv; | |
988 | char ifname[IFNAMSIZ]; | |
989 | struct nlattr *peer_tb[IFLA_MAX + 1], **tbp; | |
5517750f | 990 | unsigned char name_assign_type; |
3729d502 | 991 | struct ifinfomsg *ifmp; |
81adee47 | 992 | struct net *net; |
e314dbdc PE |
993 | |
994 | /* | |
995 | * create and register peer first | |
e314dbdc | 996 | */ |
e314dbdc PE |
997 | if (data != NULL && data[VETH_INFO_PEER] != NULL) { |
998 | struct nlattr *nla_peer; | |
999 | ||
1000 | nla_peer = data[VETH_INFO_PEER]; | |
3729d502 | 1001 | ifmp = nla_data(nla_peer); |
f7b12606 JP |
1002 | err = rtnl_nla_parse_ifla(peer_tb, |
1003 | nla_data(nla_peer) + sizeof(struct ifinfomsg), | |
fceb6435 JB |
1004 | nla_len(nla_peer) - sizeof(struct ifinfomsg), |
1005 | NULL); | |
e314dbdc PE |
1006 | if (err < 0) |
1007 | return err; | |
1008 | ||
a8b8a889 | 1009 | err = veth_validate(peer_tb, NULL, extack); |
e314dbdc PE |
1010 | if (err < 0) |
1011 | return err; | |
1012 | ||
1013 | tbp = peer_tb; | |
3729d502 PM |
1014 | } else { |
1015 | ifmp = NULL; | |
e314dbdc | 1016 | tbp = tb; |
3729d502 | 1017 | } |
e314dbdc | 1018 | |
191cdb38 | 1019 | if (ifmp && tbp[IFLA_IFNAME]) { |
e314dbdc | 1020 | nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ); |
5517750f TG |
1021 | name_assign_type = NET_NAME_USER; |
1022 | } else { | |
e314dbdc | 1023 | snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d"); |
5517750f TG |
1024 | name_assign_type = NET_NAME_ENUM; |
1025 | } | |
e314dbdc | 1026 | |
81adee47 EB |
1027 | net = rtnl_link_get_net(src_net, tbp); |
1028 | if (IS_ERR(net)) | |
1029 | return PTR_ERR(net); | |
1030 | ||
5517750f TG |
1031 | peer = rtnl_create_link(net, ifname, name_assign_type, |
1032 | &veth_link_ops, tbp); | |
81adee47 EB |
1033 | if (IS_ERR(peer)) { |
1034 | put_net(net); | |
e314dbdc | 1035 | return PTR_ERR(peer); |
81adee47 | 1036 | } |
e314dbdc | 1037 | |
191cdb38 | 1038 | if (!ifmp || !tbp[IFLA_ADDRESS]) |
f2cedb63 | 1039 | eth_hw_addr_random(peer); |
e6f8f1a7 PE |
1040 | |
1041 | if (ifmp && (dev->ifindex != 0)) | |
1042 | peer->ifindex = ifmp->ifi_index; | |
e314dbdc | 1043 | |
72d24955 SH |
1044 | peer->gso_max_size = dev->gso_max_size; |
1045 | peer->gso_max_segs = dev->gso_max_segs; | |
1046 | ||
e314dbdc | 1047 | err = register_netdevice(peer); |
81adee47 EB |
1048 | put_net(net); |
1049 | net = NULL; | |
e314dbdc PE |
1050 | if (err < 0) |
1051 | goto err_register_peer; | |
1052 | ||
1053 | netif_carrier_off(peer); | |
1054 | ||
3729d502 PM |
1055 | err = rtnl_configure_link(peer, ifmp); |
1056 | if (err < 0) | |
1057 | goto err_configure_peer; | |
1058 | ||
e314dbdc PE |
1059 | /* |
1060 | * register dev last | |
1061 | * | |
1062 | * note, that since we've registered new device the dev's name | |
1063 | * should be re-allocated | |
1064 | */ | |
1065 | ||
1066 | if (tb[IFLA_ADDRESS] == NULL) | |
f2cedb63 | 1067 | eth_hw_addr_random(dev); |
e314dbdc | 1068 | |
6c8c4446 JP |
1069 | if (tb[IFLA_IFNAME]) |
1070 | nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ); | |
1071 | else | |
1072 | snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d"); | |
1073 | ||
e314dbdc PE |
1074 | err = register_netdevice(dev); |
1075 | if (err < 0) | |
1076 | goto err_register_dev; | |
1077 | ||
1078 | netif_carrier_off(dev); | |
1079 | ||
1080 | /* | |
1081 | * tie the deviced together | |
1082 | */ | |
1083 | ||
1084 | priv = netdev_priv(dev); | |
948d4f21 | 1085 | priv->dev = dev; |
d0e2c55e | 1086 | rcu_assign_pointer(priv->peer, peer); |
e314dbdc PE |
1087 | |
1088 | priv = netdev_priv(peer); | |
948d4f21 | 1089 | priv->dev = peer; |
d0e2c55e | 1090 | rcu_assign_pointer(priv->peer, dev); |
948d4f21 | 1091 | |
e314dbdc PE |
1092 | return 0; |
1093 | ||
1094 | err_register_dev: | |
1095 | /* nothing to do */ | |
3729d502 | 1096 | err_configure_peer: |
e314dbdc PE |
1097 | unregister_netdevice(peer); |
1098 | return err; | |
1099 | ||
1100 | err_register_peer: | |
1101 | free_netdev(peer); | |
1102 | return err; | |
1103 | } | |
1104 | ||
23289a37 | 1105 | static void veth_dellink(struct net_device *dev, struct list_head *head) |
e314dbdc PE |
1106 | { |
1107 | struct veth_priv *priv; | |
1108 | struct net_device *peer; | |
1109 | ||
1110 | priv = netdev_priv(dev); | |
d0e2c55e ED |
1111 | peer = rtnl_dereference(priv->peer); |
1112 | ||
1113 | /* Note : dellink() is called from default_device_exit_batch(), | |
1114 | * before a rcu_synchronize() point. The devices are guaranteed | |
1115 | * not being freed before one RCU grace period. | |
1116 | */ | |
1117 | RCU_INIT_POINTER(priv->peer, NULL); | |
24540535 | 1118 | unregister_netdevice_queue(dev, head); |
f45a5c26 ED |
1119 | |
1120 | if (peer) { | |
1121 | priv = netdev_priv(peer); | |
1122 | RCU_INIT_POINTER(priv->peer, NULL); | |
1123 | unregister_netdevice_queue(peer, head); | |
1124 | } | |
e314dbdc PE |
1125 | } |
1126 | ||
23711438 TG |
1127 | static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = { |
1128 | [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) }, | |
1129 | }; | |
e314dbdc | 1130 | |
e5f4e7b9 ND |
1131 | static struct net *veth_get_link_net(const struct net_device *dev) |
1132 | { | |
1133 | struct veth_priv *priv = netdev_priv(dev); | |
1134 | struct net_device *peer = rtnl_dereference(priv->peer); | |
1135 | ||
1136 | return peer ? dev_net(peer) : dev_net(dev); | |
1137 | } | |
1138 | ||
e314dbdc PE |
1139 | static struct rtnl_link_ops veth_link_ops = { |
1140 | .kind = DRV_NAME, | |
1141 | .priv_size = sizeof(struct veth_priv), | |
1142 | .setup = veth_setup, | |
1143 | .validate = veth_validate, | |
1144 | .newlink = veth_newlink, | |
1145 | .dellink = veth_dellink, | |
1146 | .policy = veth_policy, | |
1147 | .maxtype = VETH_INFO_MAX, | |
e5f4e7b9 | 1148 | .get_link_net = veth_get_link_net, |
e314dbdc PE |
1149 | }; |
1150 | ||
1151 | /* | |
1152 | * init/fini | |
1153 | */ | |
1154 | ||
1155 | static __init int veth_init(void) | |
1156 | { | |
1157 | return rtnl_link_register(&veth_link_ops); | |
1158 | } | |
1159 | ||
1160 | static __exit void veth_exit(void) | |
1161 | { | |
68365458 | 1162 | rtnl_link_unregister(&veth_link_ops); |
e314dbdc PE |
1163 | } |
1164 | ||
1165 | module_init(veth_init); | |
1166 | module_exit(veth_exit); | |
1167 | ||
1168 | MODULE_DESCRIPTION("Virtual Ethernet Tunnel"); | |
1169 | MODULE_LICENSE("GPL v2"); | |
1170 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |