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