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