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