blob: 2307696d489734cfbabfcde844517d6584130cdc [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07002/*
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 Emelyanove314dbd2007-09-25 16:14:46 -070012#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070014#include <linux/ethtool.h>
15#include <linux/etherdevice.h>
Eric Dumazetcf05c702011-06-19 22:48:34 -070016#include <linux/u64_stats_sync.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070017
Jiri Pirkof7b12602014-02-18 20:53:18 +010018#include <net/rtnetlink.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070019#include <net/dst.h>
20#include <net/xfrm.h>
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +090021#include <net/xdp.h>
Stephen Hemmingerecef9692007-12-25 17:23:59 -080022#include <linux/veth.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040023#include <linux/module.h>
Toshiaki Makita948d4f22018-08-03 16:58:10 +090024#include <linux/bpf.h>
25#include <linux/filter.h>
26#include <linux/ptr_ring.h>
Toshiaki Makita948d4f22018-08-03 16:58:10 +090027#include <linux/bpf_trace.h>
Michael Walleaa4e6892018-08-29 17:24:11 +020028#include <linux/net_tstamp.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070029
30#define DRV_NAME "veth"
31#define DRV_VERSION "1.0"
32
Toshiaki Makita9fc8d512018-08-03 16:58:13 +090033#define VETH_XDP_FLAG BIT(0)
Toshiaki Makita948d4f22018-08-03 16:58:10 +090034#define VETH_RING_SIZE 256
35#define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
36
Toshiaki Makita9cda7802019-06-13 18:39:59 +090037#define VETH_XDP_TX_BULK_SIZE 16
38
Lorenzo Bianconi65780c52020-03-19 17:41:25 +010039struct veth_stats {
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +010040 u64 rx_drops;
41 /* xdp */
Lorenzo Bianconi65780c52020-03-19 17:41:25 +010042 u64 xdp_packets;
43 u64 xdp_bytes;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +010044 u64 xdp_redirect;
Lorenzo Bianconi65780c52020-03-19 17:41:25 +010045 u64 xdp_drops;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +010046 u64 xdp_tx;
Lorenzo Bianconi65780c52020-03-19 17:41:25 +010047};
48
Toshiaki Makita4195e542018-10-11 18:36:49 +090049struct veth_rq_stats {
Lorenzo Bianconi65780c52020-03-19 17:41:25 +010050 struct veth_stats vs;
Toshiaki Makita4195e542018-10-11 18:36:49 +090051 struct u64_stats_sync syncp;
52};
53
Toshiaki Makita638264d2018-08-03 16:58:18 +090054struct veth_rq {
Toshiaki Makita948d4f22018-08-03 16:58:10 +090055 struct napi_struct xdp_napi;
56 struct net_device *dev;
57 struct bpf_prog __rcu *xdp_prog;
Toshiaki Makitad1396002018-08-03 16:58:17 +090058 struct xdp_mem_info xdp_mem;
Toshiaki Makita4195e542018-10-11 18:36:49 +090059 struct veth_rq_stats stats;
Toshiaki Makita948d4f22018-08-03 16:58:10 +090060 bool rx_notify_masked;
61 struct ptr_ring xdp_ring;
62 struct xdp_rxq_info xdp_rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070063};
64
Toshiaki Makita638264d2018-08-03 16:58:18 +090065struct 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 Makita9cda7802019-06-13 18:39:59 +090073struct veth_xdp_tx_bq {
74 struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
75 unsigned int count;
76};
77
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070078/*
79 * ethtool interface
80 */
81
Toshiaki Makitad397b962018-10-11 18:36:50 +090082struct veth_q_stat_desc {
83 char desc[ETH_GSTRING_LEN];
84 size_t offset;
85};
86
Lorenzo Bianconi65780c52020-03-19 17:41:25 +010087#define VETH_RQ_STAT(m) offsetof(struct veth_stats, m)
Toshiaki Makitad397b962018-10-11 18:36:50 +090088
89static 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 Bianconi66fe4a02020-03-19 17:41:27 +010093 { "rx_drops", VETH_RQ_STAT(rx_drops) },
Toshiaki Makitad397b962018-10-11 18:36:50 +090094};
95
96#define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
97
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070098static struct {
99 const char string[ETH_GSTRING_LEN];
100} ethtool_stats_keys[] = {
101 { "peer_ifindex" },
102};
103
Philippe Reynes56607b92017-03-29 08:24:21 +0200104static int veth_get_link_ksettings(struct net_device *dev,
105 struct ethtool_link_ksettings *cmd)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700106{
Philippe Reynes56607b92017-03-29 08:24:21 +0200107 cmd->base.speed = SPEED_10000;
108 cmd->base.duplex = DUPLEX_FULL;
109 cmd->base.port = PORT_TP;
110 cmd->base.autoneg = AUTONEG_DISABLE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700111 return 0;
112}
113
114static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
115{
Rick Jones33a5ba12011-11-15 14:59:53 +0000116 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
117 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700118}
119
120static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
121{
Toshiaki Makitad397b962018-10-11 18:36:50 +0900122 char *p = (char *)buf;
123 int i, j;
124
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700125 switch(stringset) {
126 case ETH_SS_STATS:
Toshiaki Makitad397b962018-10-11 18:36:50 +0900127 memcpy(p, &ethtool_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 Fainelliabdf47a2019-02-21 20:09:29 -0800131 snprintf(p, ETH_GSTRING_LEN,
132 "rx_queue_%u_%.11s",
Toshiaki Makitad397b962018-10-11 18:36:50 +0900133 i, veth_rq_stats_desc[j].desc);
134 p += ETH_GSTRING_LEN;
135 }
136 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700137 break;
138 }
139}
140
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700141static int veth_get_sset_count(struct net_device *dev, int sset)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700142{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700143 switch (sset) {
144 case ETH_SS_STATS:
Toshiaki Makitad397b962018-10-11 18:36:50 +0900145 return ARRAY_SIZE(ethtool_stats_keys) +
146 VETH_RQ_STATS_LEN * dev->real_num_rx_queues;
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700147 default:
148 return -EOPNOTSUPP;
149 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700150}
151
152static void veth_get_ethtool_stats(struct net_device *dev,
153 struct ethtool_stats *stats, u64 *data)
154{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000155 struct veth_priv *priv = netdev_priv(dev);
156 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makitad397b962018-10-11 18:36:50 +0900157 int i, j, idx;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700158
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000159 data[0] = peer ? peer->ifindex : 0;
Toshiaki Makitad397b962018-10-11 18:36:50 +0900160 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 Bianconi65780c52020-03-19 17:41:25 +0100163 const void *stats_base = (void *)&rq_stats->vs;
Toshiaki Makitad397b962018-10-11 18:36:50 +0900164 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 Emelyanove314dbd2007-09-25 16:14:46 -0700176}
177
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700178static const struct ethtool_ops veth_ethtool_ops = {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700179 .get_drvinfo = veth_get_drvinfo,
180 .get_link = ethtool_op_get_link,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700181 .get_strings = veth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700182 .get_sset_count = veth_get_sset_count,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700183 .get_ethtool_stats = veth_get_ethtool_stats,
Philippe Reynes56607b92017-03-29 08:24:21 +0200184 .get_link_ksettings = veth_get_link_ksettings,
Julian Wiedmann056b21f2019-04-12 13:06:15 +0200185 .get_ts_info = ethtool_op_get_ts_info,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700186};
187
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900188/* general routines */
189
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900190static bool veth_is_xdp_frame(void *ptr)
191{
192 return (unsigned long)ptr & VETH_XDP_FLAG;
193}
194
195static void *veth_ptr_to_xdp(void *ptr)
196{
197 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
198}
199
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900200static void *veth_xdp_to_ptr(void *ptr)
201{
202 return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
203}
204
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900205static 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 Makita638264d2018-08-03 16:58:18 +0900213static void __veth_xdp_flush(struct veth_rq *rq)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900214{
215 /* Write ptr_ring before reading rx_notify_masked */
216 smp_mb();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900217 if (!rq->rx_notify_masked) {
218 rq->rx_notify_masked = true;
219 napi_schedule(&rq->xdp_napi);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900220 }
221}
222
Toshiaki Makita638264d2018-08-03 16:58:18 +0900223static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900224{
Toshiaki Makita638264d2018-08-03 16:58:18 +0900225 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900226 dev_kfree_skb_any(skb);
227 return NET_RX_DROP;
228 }
229
230 return NET_RX_SUCCESS;
231}
232
Toshiaki Makita638264d2018-08-03 16:58:18 +0900233static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
234 struct veth_rq *rq, bool xdp)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700235{
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900236 return __dev_forward_skb(dev, skb) ?: xdp ?
Toshiaki Makita638264d2018-08-03 16:58:18 +0900237 veth_xdp_rx(rq, skb) :
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900238 netif_rx(skb);
239}
240
241static 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 Makita638264d2018-08-03 16:58:18 +0900244 struct veth_rq *rq = NULL;
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000245 struct net_device *rcv;
Eric Dumazet26811282012-12-29 16:02:43 +0000246 int length = skb->len;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900247 bool rcv_xdp = false;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900248 int rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700249
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000250 rcu_read_lock();
251 rcv = rcu_dereference(priv->peer);
252 if (unlikely(!rcv)) {
253 kfree_skb(skb);
254 goto drop;
255 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700256
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900257 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900258 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 Makita948d4f22018-08-03 16:58:10 +0900265
Michael Walleaa4e6892018-08-29 17:24:11 +0200266 skb_tx_timestamp(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900267 if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
Eric Dumazetb4fba472019-11-07 16:27:17 -0800268 if (!rcv_xdp)
269 dev_lstats_add(dev, length);
Eric Dumazet26811282012-12-29 16:02:43 +0000270 } else {
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000271drop:
Eric Dumazet26811282012-12-29 16:02:43 +0000272 atomic64_inc(&priv->dropped);
273 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900274
275 if (rcv_xdp)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900276 __veth_xdp_flush(rq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900277
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000278 rcu_read_unlock();
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900279
Patrick McHardy6ed10652009-06-23 06:03:08 +0000280 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700281}
282
Eric Dumazetb4fba472019-11-07 16:27:17 -0800283static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700284{
Eric Dumazetcf05c702011-06-19 22:48:34 -0700285 struct veth_priv *priv = netdev_priv(dev);
David S. Miller11687a12009-06-25 02:45:42 -0700286
Eric Dumazetb4fba472019-11-07 16:27:17 -0800287 dev_lstats_read(dev, packets, bytes);
Eric Dumazet26811282012-12-29 16:02:43 +0000288 return atomic64_read(&priv->dropped);
289}
290
Lorenzo Bianconi65780c52020-03-19 17:41:25 +0100291static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
Toshiaki Makita4195e542018-10-11 18:36:49 +0900292{
293 struct veth_priv *priv = netdev_priv(dev);
294 int i;
295
296 result->xdp_packets = 0;
297 result->xdp_bytes = 0;
Lorenzo Bianconi66fe4a02020-03-19 17:41:27 +0100298 result->rx_drops = 0;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900299 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 Bianconi65780c52020-03-19 17:41:25 +0100306 packets = stats->vs.xdp_packets;
307 bytes = stats->vs.xdp_bytes;
Lorenzo Bianconi66fe4a02020-03-19 17:41:27 +0100308 drops = stats->vs.rx_drops;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900309 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
310 result->xdp_packets += packets;
311 result->xdp_bytes += bytes;
Lorenzo Bianconi66fe4a02020-03-19 17:41:27 +0100312 result->rx_drops += drops;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900313 }
314}
315
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800316static void veth_get_stats64(struct net_device *dev,
317 struct rtnl_link_stats64 *tot)
Eric Dumazet26811282012-12-29 16:02:43 +0000318{
319 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000320 struct net_device *peer;
Lorenzo Bianconi65780c52020-03-19 17:41:25 +0100321 struct veth_stats rx;
Eric Dumazetb4fba472019-11-07 16:27:17 -0800322 u64 packets, bytes;
Eric Dumazet26811282012-12-29 16:02:43 +0000323
Eric Dumazetb4fba472019-11-07 16:27:17 -0800324 tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
325 tot->tx_bytes = bytes;
326 tot->tx_packets = packets;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900327
328 veth_stats_rx(&rx, dev);
Lorenzo Bianconi66fe4a02020-03-19 17:41:27 +0100329 tot->rx_dropped = rx.rx_drops;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900330 tot->rx_bytes = rx.xdp_bytes;
331 tot->rx_packets = rx.xdp_packets;
Eric Dumazet26811282012-12-29 16:02:43 +0000332
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000333 rcu_read_lock();
334 peer = rcu_dereference(priv->peer);
335 if (peer) {
Jiang Lidonge25d5db2020-03-04 09:49:29 +0800336 veth_stats_tx(peer, &packets, &bytes);
Eric Dumazetb4fba472019-11-07 16:27:17 -0800337 tot->rx_bytes += bytes;
338 tot->rx_packets += packets;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900339
340 veth_stats_rx(&rx, peer);
341 tot->tx_bytes += rx.xdp_bytes;
342 tot->tx_packets += rx.xdp_packets;
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000343 }
344 rcu_read_unlock();
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700345}
346
Gao feng5c70ef82013-10-04 16:52:24 +0800347/* fake multicast ability */
348static void veth_set_multicast_list(struct net_device *dev)
349{
350}
351
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900352static 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 Makita638264d2018-08-03 16:58:18 +0900371static int veth_select_rxq(struct net_device *dev)
372{
373 return smp_processor_id() % dev->real_num_rx_queues;
374}
375
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900376static 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 Makita21314792018-10-11 18:36:48 +0900381 int i, ret, drops = n;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900382 unsigned int max_len;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900383 struct veth_rq *rq;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900384
John Fastabendb23bfa52020-01-26 16:14:02 -0800385 rcu_read_lock();
Toshiaki Makita21314792018-10-11 18:36:48 +0900386 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
387 ret = -EINVAL;
388 goto drop;
389 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900390
391 rcv = rcu_dereference(priv->peer);
Toshiaki Makita21314792018-10-11 18:36:48 +0900392 if (unlikely(!rcv)) {
393 ret = -ENXIO;
394 goto drop;
395 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900396
397 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900398 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900399 /* 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 Makita21314792018-10-11 18:36:48 +0900403 if (!rcu_access_pointer(rq->xdp_prog)) {
404 ret = -ENXIO;
405 goto drop;
406 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900407
Toshiaki Makita21314792018-10-11 18:36:48 +0900408 drops = 0;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900409 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
410
Toshiaki Makita638264d2018-08-03 16:58:18 +0900411 spin_lock(&rq->xdp_ring.producer_lock);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900412 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 Makita638264d2018-08-03 16:58:18 +0900417 __ptr_ring_produce(&rq->xdp_ring, ptr))) {
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900418 xdp_return_frame_rx_napi(frame);
419 drops++;
420 }
421 }
Toshiaki Makita638264d2018-08-03 16:58:18 +0900422 spin_unlock(&rq->xdp_ring.producer_lock);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900423
424 if (flags & XDP_XMIT_FLUSH)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900425 __veth_xdp_flush(rq);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900426
John Fastabendb23bfa52020-01-26 16:14:02 -0800427 if (likely(!drops)) {
428 rcu_read_unlock();
Toshiaki Makita21314792018-10-11 18:36:48 +0900429 return n;
John Fastabendb23bfa52020-01-26 16:14:02 -0800430 }
Toshiaki Makita21314792018-10-11 18:36:48 +0900431
432 ret = n - drops;
433drop:
John Fastabendb23bfa52020-01-26 16:14:02 -0800434 rcu_read_unlock();
Toshiaki Makita21314792018-10-11 18:36:48 +0900435 atomic64_add(drops, &priv->dropped);
436
437 return ret;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900438}
439
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900440static 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
456static void veth_xdp_flush(struct net_device *dev, struct veth_xdp_tx_bq *bq)
Toshiaki Makitad1396002018-08-03 16:58:17 +0900457{
458 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
459 struct net_device *rcv;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900460 struct veth_rq *rq;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900461
462 rcu_read_lock();
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900463 veth_xdp_flush_bq(dev, bq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900464 rcv = rcu_dereference(priv->peer);
465 if (unlikely(!rcv))
466 goto out;
467
468 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900469 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
Toshiaki Makitad1396002018-08-03 16:58:17 +0900470 /* xdp_ring is initialized on receive side? */
Toshiaki Makita638264d2018-08-03 16:58:18 +0900471 if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
Toshiaki Makitad1396002018-08-03 16:58:17 +0900472 goto out;
473
Toshiaki Makita638264d2018-08-03 16:58:18 +0900474 __veth_xdp_flush(rq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900475out:
476 rcu_read_unlock();
477}
478
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900479static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp,
480 struct veth_xdp_tx_bq *bq)
Toshiaki Makitad1396002018-08-03 16:58:17 +0900481{
482 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
483
484 if (unlikely(!frame))
485 return -EOVERFLOW;
486
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900487 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 Makitad1396002018-08-03 16:58:17 +0900493}
494
Toshiaki Makita638264d2018-08-03 16:58:18 +0900495static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
Toshiaki Makitad1396002018-08-03 16:58:17 +0900496 struct xdp_frame *frame,
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100497 struct veth_xdp_tx_bq *bq,
498 struct veth_stats *stats)
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900499{
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 Makitad1396002018-08-03 16:58:17 +0900503 struct xdp_frame orig_frame;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900504 struct bpf_prog *xdp_prog;
505 unsigned int headroom;
506 struct sk_buff *skb;
507
508 rcu_read_lock();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900509 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900510 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 Makita638264d2018-08-03 16:58:18 +0900518 xdp.rxq = &rq->xdp_rxq;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900519
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 Makitad1396002018-08-03 16:58:17 +0900527 case XDP_TX:
528 orig_frame = *frame;
529 xdp.data_hard_start = head;
530 xdp.rxq->mem = frame->mem;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900531 if (unlikely(veth_xdp_tx(rq->dev, &xdp, bq) < 0)) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900532 trace_xdp_exception(rq->dev, xdp_prog, act);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900533 frame = &orig_frame;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100534 stats->rx_drops++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900535 goto err_xdp;
536 }
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100537 stats->xdp_tx++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900538 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 Makita638264d2018-08-03 16:58:18 +0900544 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
Toshiaki Makitad1396002018-08-03 16:58:17 +0900545 frame = &orig_frame;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100546 stats->rx_drops++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900547 goto err_xdp;
548 }
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100549 stats->xdp_redirect++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900550 rcu_read_unlock();
551 goto xdp_xmit;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900552 default:
553 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600554 /* fall through */
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900555 case XDP_ABORTED:
Toshiaki Makita638264d2018-08-03 16:58:18 +0900556 trace_xdp_exception(rq->dev, xdp_prog, act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600557 /* fall through */
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900558 case XDP_DROP:
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100559 stats->xdp_drops++;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900560 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 Bianconi1c5b82e52020-03-19 17:41:26 +0100569 stats->rx_drops++;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900570 goto err;
571 }
572
Jesper Dangaard Brouercbf33512019-06-18 15:05:32 +0200573 xdp_release_frame(frame);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900574 xdp_scrub_frame(frame);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900575 skb->protocol = eth_type_trans(skb, rq->dev);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900576err:
577 return skb;
578err_xdp:
579 rcu_read_unlock();
580 xdp_return_frame(frame);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900581xdp_xmit:
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900582 return NULL;
583}
584
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100585static 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 Makita948d4f22018-08-03 16:58:10 +0900589{
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 Makita4bf9ffa2018-09-14 13:33:44 +0900596 skb_orphan(skb);
597
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900598 rcu_read_lock();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900599 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900600 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 Makita948d4f22018-08-03 16:58:10 +0900643 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 Makita638264d2018-08-03 16:58:18 +0900651 xdp.rxq = &rq->xdp_rxq;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900652 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 Makitad1396002018-08-03 16:58:17 +0900660 case XDP_TX:
661 get_page(virt_to_page(xdp.data));
662 consume_skb(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900663 xdp.rxq->mem = rq->xdp_mem;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900664 if (unlikely(veth_xdp_tx(rq->dev, &xdp, bq) < 0)) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900665 trace_xdp_exception(rq->dev, xdp_prog, act);
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100666 stats->rx_drops++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900667 goto err_xdp;
668 }
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100669 stats->xdp_tx++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900670 rcu_read_unlock();
671 goto xdp_xmit;
672 case XDP_REDIRECT:
673 get_page(virt_to_page(xdp.data));
674 consume_skb(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900675 xdp.rxq->mem = rq->xdp_mem;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100676 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
677 stats->rx_drops++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900678 goto err_xdp;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100679 }
680 stats->xdp_redirect++;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900681 rcu_read_unlock();
682 goto xdp_xmit;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900683 default:
684 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600685 /* fall through */
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900686 case XDP_ABORTED:
Toshiaki Makita638264d2018-08-03 16:58:18 +0900687 trace_xdp_exception(rq->dev, xdp_prog, act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600688 /* fall through */
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900689 case XDP_DROP:
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100690 stats->xdp_drops++;
691 goto xdp_drop;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900692 }
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 Makita638264d2018-08-03 16:58:18 +0900705 skb->protocol = eth_type_trans(skb, rq->dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900706
707 metalen = xdp.data - xdp.data_meta;
708 if (metalen)
709 skb_metadata_set(skb, metalen);
710out:
711 return skb;
712drop:
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100713 stats->rx_drops++;
714xdp_drop:
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900715 rcu_read_unlock();
716 kfree_skb(skb);
717 return NULL;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900718err_xdp:
719 rcu_read_unlock();
720 page_frag_free(xdp.data);
721xdp_xmit:
722 return NULL;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900723}
724
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100725static int veth_xdp_rcv(struct veth_rq *rq, int budget,
726 struct veth_xdp_tx_bq *bq,
727 struct veth_stats *stats)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900728{
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100729 int i, done = 0;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900730
731 for (i = 0; i < budget; i++) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900732 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900733 struct sk_buff *skb;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900734
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900735 if (!ptr)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900736 break;
737
Toshiaki Makitad1396002018-08-03 16:58:17 +0900738 if (veth_is_xdp_frame(ptr)) {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900739 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
740
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100741 stats->xdp_bytes += frame->len;
742 skb = veth_xdp_rcv_one(rq, frame, bq, stats);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900743 } else {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900744 skb = ptr;
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100745 stats->xdp_bytes += skb->len;
746 skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900747 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900748
749 if (skb)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900750 napi_gro_receive(&rq->xdp_napi, skb);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900751
752 done++;
753 }
754
Toshiaki Makita4195e542018-10-11 18:36:49 +0900755 u64_stats_update_begin(&rq->stats.syncp);
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100756 rq->stats.vs.xdp_bytes += stats->xdp_bytes;
Lorenzo Bianconi66fe4a02020-03-19 17:41:27 +0100757 rq->stats.vs.xdp_drops += stats->xdp_drops;
758 rq->stats.vs.rx_drops += stats->rx_drops;
Lorenzo Bianconi65780c52020-03-19 17:41:25 +0100759 rq->stats.vs.xdp_packets += done;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900760 u64_stats_update_end(&rq->stats.syncp);
761
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900762 return done;
763}
764
765static int veth_poll(struct napi_struct *napi, int budget)
766{
Toshiaki Makita638264d2018-08-03 16:58:18 +0900767 struct veth_rq *rq =
768 container_of(napi, struct veth_rq, xdp_napi);
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100769 struct veth_stats stats = {};
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900770 struct veth_xdp_tx_bq bq;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900771 int done;
772
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900773 bq.count = 0;
774
Toshiaki Makitad1396002018-08-03 16:58:17 +0900775 xdp_set_return_frame_no_direct();
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100776 done = veth_xdp_rcv(rq, budget, &bq, &stats);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900777
778 if (done < budget && napi_complete_done(napi, done)) {
779 /* Write rx_notify_masked before reading ptr_ring */
Toshiaki Makita638264d2018-08-03 16:58:18 +0900780 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 Makita948d4f22018-08-03 16:58:10 +0900784 }
785 }
786
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100787 if (stats.xdp_tx > 0)
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900788 veth_xdp_flush(rq->dev, &bq);
Lorenzo Bianconi1c5b82e52020-03-19 17:41:26 +0100789 if (stats.xdp_redirect > 0)
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100790 xdp_do_flush();
Toshiaki Makitad1396002018-08-03 16:58:17 +0900791 xdp_clear_return_frame_no_direct();
792
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900793 return done;
794}
795
796static int veth_napi_add(struct net_device *dev)
797{
798 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900799 int err, i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900800
Toshiaki Makita638264d2018-08-03 16:58:18 +0900801 for (i = 0; i < dev->real_num_rx_queues; i++) {
802 struct veth_rq *rq = &priv->rq[i];
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900803
Toshiaki Makita638264d2018-08-03 16:58:18 +0900804 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 Makita948d4f22018-08-03 16:58:10 +0900815
816 return 0;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900817err_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 Makita948d4f22018-08-03 16:58:10 +0900822}
823
824static void veth_napi_del(struct net_device *dev)
825{
826 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900827 int i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900828
Toshiaki Makita638264d2018-08-03 16:58:18 +0900829 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 Makita948d4f22018-08-03 16:58:10 +0900844}
845
846static int veth_enable_xdp(struct net_device *dev)
847{
848 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900849 int err, i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900850
Toshiaki Makita638264d2018-08-03 16:58:18 +0900851 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 Makita948d4f22018-08-03 16:58:10 +0900854
Toshiaki Makita638264d2018-08-03 16:58:18 +0900855 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 Makita948d4f22018-08-03 16:58:10 +0900868
869 err = veth_napi_add(dev);
870 if (err)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900871 goto err_rxq_reg;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900872 }
873
Toshiaki Makita638264d2018-08-03 16:58:18 +0900874 for (i = 0; i < dev->real_num_rx_queues; i++)
875 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900876
877 return 0;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900878err_reg_mem:
879 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
880err_rxq_reg:
881 for (i--; i >= 0; i--)
882 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900883
884 return err;
885}
886
887static void veth_disable_xdp(struct net_device *dev)
888{
889 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900890 int i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900891
Toshiaki Makita638264d2018-08-03 16:58:18 +0900892 for (i = 0; i < dev->real_num_rx_queues; i++)
893 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900894 veth_napi_del(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900895 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 Makita948d4f22018-08-03 16:58:10 +0900901}
902
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700903static int veth_open(struct net_device *dev)
904{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000905 struct veth_priv *priv = netdev_priv(dev);
906 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900907 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700908
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000909 if (!peer)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700910 return -ENOTCONN;
911
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900912 if (priv->_xdp_prog) {
913 err = veth_enable_xdp(dev);
914 if (err)
915 return err;
916 }
917
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000918 if (peer->flags & IFF_UP) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700919 netif_carrier_on(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000920 netif_carrier_on(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700921 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900922
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700923 return 0;
924}
925
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000926static int veth_close(struct net_device *dev)
927{
928 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000929 struct net_device *peer = rtnl_dereference(priv->peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000930
931 netif_carrier_off(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000932 if (peer)
933 netif_carrier_off(peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000934
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900935 if (priv->_xdp_prog)
936 veth_disable_xdp(dev);
937
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000938 return 0;
939}
940
Jarod Wilson91572082016-10-20 13:55:20 -0400941static int is_valid_veth_mtu(int mtu)
Eric Biederman38d40812009-03-03 23:36:04 -0800942{
Jarod Wilson91572082016-10-20 13:55:20 -0400943 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
Eric Biederman38d40812009-03-03 23:36:04 -0800944}
945
Toshiaki Makita7797b932018-08-15 17:07:29 +0900946static 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 Makita4195e542018-10-11 18:36:49 +0900955 for (i = 0; i < dev->num_rx_queues; i++) {
Toshiaki Makita7797b932018-08-15 17:07:29 +0900956 priv->rq[i].dev = dev;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900957 u64_stats_init(&priv->rq[i].stats.syncp);
958 }
Toshiaki Makita7797b932018-08-15 17:07:29 +0900959
960 return 0;
961}
962
963static 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 Emelyanove314dbd2007-09-25 16:14:46 -0700970static int veth_dev_init(struct net_device *dev)
971{
Toshiaki Makita7797b932018-08-15 17:07:29 +0900972 int err;
973
Li RongQing14d73412018-09-17 18:46:55 +0800974 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
975 if (!dev->lstats)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700976 return -ENOMEM;
Toshiaki Makita7797b932018-08-15 17:07:29 +0900977
978 err = veth_alloc_queues(dev);
979 if (err) {
Li RongQing14d73412018-09-17 18:46:55 +0800980 free_percpu(dev->lstats);
Toshiaki Makita7797b932018-08-15 17:07:29 +0900981 return err;
982 }
983
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700984 return 0;
985}
986
David S. Miller11687a12009-06-25 02:45:42 -0700987static void veth_dev_free(struct net_device *dev)
988{
Toshiaki Makita7797b932018-08-15 17:07:29 +0900989 veth_free_queues(dev);
Li RongQing14d73412018-09-17 18:46:55 +0800990 free_percpu(dev->lstats);
David S. Miller11687a12009-06-25 02:45:42 -0700991}
992
WANG Congbb446c12014-06-23 15:36:02 -0700993#ifdef CONFIG_NET_POLL_CONTROLLER
994static void veth_poll_controller(struct net_device *dev)
995{
996 /* veth only receives frames when its peer sends one
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900997 * Since it has nothing to do with disabling irqs, we are guaranteed
WANG Congbb446c12014-06-23 15:36:02 -0700998 * 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 Dichtela45253b2015-04-02 17:07:11 +02001008static 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 Makitadc224822018-08-03 16:58:11 +09001022static 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 Abeni163e5292016-02-26 10:45:41 +01001039static 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
1058out:
1059 rcu_read_unlock();
1060}
1061
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001062static 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 Makitadc224822018-08-03 16:58:11 +09001068 unsigned int max_mtu;
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001069 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 Makitadc224822018-08-03 16:58:11 +09001082 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 Makita638264d2018-08-03 16:58:18 +09001091 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 Makita948d4f22018-08-03 16:58:10 +09001097 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 Makitadc224822018-08-03 16:58:11 +09001104
1105 if (!old_prog) {
1106 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1107 peer->max_mtu = max_mtu;
1108 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001109 }
1110
1111 if (old_prog) {
Toshiaki Makitadc224822018-08-03 16:58:11 +09001112 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 Makita948d4f22018-08-03 16:58:10 +09001121 bpf_prog_put(old_prog);
1122 }
1123
Toshiaki Makitadc224822018-08-03 16:58:11 +09001124 if ((!!old_prog ^ !!prog) && peer)
1125 netdev_update_features(peer);
1126
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001127 return 0;
1128err:
1129 priv->_xdp_prog = old_prog;
1130
1131 return err;
1132}
1133
1134static 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
1146static 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 Hemminger4456e7b2008-11-19 21:50:10 -08001159static const struct net_device_ops veth_netdev_ops = {
Daniel Lezcanoee923622009-02-22 00:04:45 -08001160 .ndo_init = veth_dev_init,
1161 .ndo_open = veth_open,
Eric W. Biederman2cf48a12009-02-25 19:47:29 +00001162 .ndo_stop = veth_close,
Daniel Lezcanoee923622009-02-22 00:04:45 -08001163 .ndo_start_xmit = veth_xmit,
stephen hemminger6311cc42011-06-08 14:53:59 +00001164 .ndo_get_stats64 = veth_get_stats64,
Gao feng5c70ef82013-10-04 16:52:24 +08001165 .ndo_set_rx_mode = veth_set_multicast_list,
Daniel Lezcanoee923622009-02-22 00:04:45 -08001166 .ndo_set_mac_address = eth_mac_addr,
WANG Congbb446c12014-06-23 15:36:02 -07001167#ifdef CONFIG_NET_POLL_CONTROLLER
1168 .ndo_poll_controller = veth_poll_controller,
1169#endif
Nicolas Dichtela45253b2015-04-02 17:07:11 +02001170 .ndo_get_iflink = veth_get_iflink,
Toshiaki Makitadc224822018-08-03 16:58:11 +09001171 .ndo_fix_features = veth_fix_features,
Toshiaki Makita1a04a822015-07-31 15:03:25 +09001172 .ndo_features_check = passthru_features_check,
Paolo Abeni163e5292016-02-26 10:45:41 +01001173 .ndo_set_rx_headroom = veth_set_rx_headroom,
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001174 .ndo_bpf = veth_xdp,
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +09001175 .ndo_xdp_xmit = veth_xdp_xmit,
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001176};
1177
Alexander Duyck732912d72016-04-19 14:02:26 -04001178#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
Xin Longc80fafb2016-08-25 13:21:49 +08001179 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
Alexander Duyck732912d72016-04-19 14:02:26 -04001180 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
Patrick McHardy28d2b132013-04-19 02:04:32 +00001181 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 Dumazet80933152012-12-29 16:26:10 +00001183
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001184static void veth_setup(struct net_device *dev)
1185{
1186 ether_setup(dev);
1187
Neil Horman550fd082011-07-26 06:05:38 +00001188 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Hannes Frederic Sowa23ea5a92012-10-30 16:22:01 +00001189 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Phil Sutter02f01ec2015-08-18 10:30:29 +02001190 dev->priv_flags |= IFF_NO_QUEUE;
Paolo Abeni163e5292016-02-26 10:45:41 +01001191 dev->priv_flags |= IFF_PHONY_HEADROOM;
Neil Horman550fd082011-07-26 06:05:38 +00001192
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001193 dev->netdev_ops = &veth_netdev_ops;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001194 dev->ethtool_ops = &veth_ethtool_ops;
1195 dev->features |= NETIF_F_LLTX;
Eric Dumazet80933152012-12-29 16:26:10 +00001196 dev->features |= VETH_FEATURES;
Toshiaki Makita8d0d21f2014-02-18 21:20:08 +09001197 dev->vlan_features = dev->features &
Vlad Yasevich3f8c7072014-03-27 22:14:48 -04001198 ~(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. Millercf124db2017-05-08 12:52:56 -04001202 dev->needs_free_netdev = true;
1203 dev->priv_destructor = veth_dev_free;
Jarod Wilson91572082016-10-20 13:55:20 -04001204 dev->max_mtu = ETH_MAX_MTU;
Michał Mirosława2c725f2011-03-31 01:01:35 +00001205
Eric Dumazet80933152012-12-29 16:26:10 +00001206 dev->hw_features = VETH_FEATURES;
Eric Dumazet82d81892013-10-25 18:25:03 -07001207 dev->hw_enc_features = VETH_FEATURES;
David Ahern607fca92016-08-24 20:10:45 -07001208 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001209}
1210
1211/*
1212 * netlink interface
1213 */
1214
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001215static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1216 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001217{
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 Biederman38d40812009-03-03 23:36:04 -08001224 if (tb[IFLA_MTU]) {
1225 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1226 return -EINVAL;
1227 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001228 return 0;
1229}
1230
1231static struct rtnl_link_ops veth_link_ops;
1232
Eric W. Biederman81adee42009-11-08 00:53:51 -08001233static int veth_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001234 struct nlattr *tb[], struct nlattr *data[],
1235 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001236{
Toshiaki Makita7797b932018-08-15 17:07:29 +09001237 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001238 struct net_device *peer;
1239 struct veth_priv *priv;
1240 char ifname[IFNAMSIZ];
1241 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
Tom Gundersen55177502014-07-14 16:37:25 +02001242 unsigned char name_assign_type;
Patrick McHardy3729d502010-02-26 06:34:54 +00001243 struct ifinfomsg *ifmp;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001244 struct net *net;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001245
1246 /*
1247 * create and register peer first
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001248 */
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001249 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1250 struct nlattr *nla_peer;
1251
1252 nla_peer = data[VETH_INFO_PEER];
Patrick McHardy3729d502010-02-26 06:34:54 +00001253 ifmp = nla_data(nla_peer);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001254 err = rtnl_nla_parse_ifla(peer_tb,
1255 nla_data(nla_peer) + sizeof(struct ifinfomsg),
Johannes Bergfceb6432017-04-12 14:34:07 +02001256 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1257 NULL);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001258 if (err < 0)
1259 return err;
1260
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001261 err = veth_validate(peer_tb, NULL, extack);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001262 if (err < 0)
1263 return err;
1264
1265 tbp = peer_tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001266 } else {
1267 ifmp = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001268 tbp = tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001269 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001270
Serhey Popovych191cdb32017-06-21 12:12:24 +03001271 if (ifmp && tbp[IFLA_IFNAME]) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001272 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
Tom Gundersen55177502014-07-14 16:37:25 +02001273 name_assign_type = NET_NAME_USER;
1274 } else {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001275 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
Tom Gundersen55177502014-07-14 16:37:25 +02001276 name_assign_type = NET_NAME_ENUM;
1277 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001278
Eric W. Biederman81adee42009-11-08 00:53:51 -08001279 net = rtnl_link_get_net(src_net, tbp);
1280 if (IS_ERR(net))
1281 return PTR_ERR(net);
1282
Tom Gundersen55177502014-07-14 16:37:25 +02001283 peer = rtnl_create_link(net, ifname, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001284 &veth_link_ops, tbp, extack);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001285 if (IS_ERR(peer)) {
1286 put_net(net);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001287 return PTR_ERR(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001288 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001289
Serhey Popovych191cdb32017-06-21 12:12:24 +03001290 if (!ifmp || !tbp[IFLA_ADDRESS])
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001291 eth_hw_addr_random(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001292
Pavel Emelyanove6f8f1a2012-08-08 21:53:03 +00001293 if (ifmp && (dev->ifindex != 0))
1294 peer->ifindex = ifmp->ifi_index;
1295
Stephen Hemminger72d249552017-12-07 15:40:20 -08001296 peer->gso_max_size = dev->gso_max_size;
1297 peer->gso_max_segs = dev->gso_max_segs;
1298
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001299 err = register_netdevice(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001300 put_net(net);
1301 net = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001302 if (err < 0)
1303 goto err_register_peer;
1304
1305 netif_carrier_off(peer);
1306
Patrick McHardy3729d502010-02-26 06:34:54 +00001307 err = rtnl_configure_link(peer, ifmp);
1308 if (err < 0)
1309 goto err_configure_peer;
1310
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001311 /*
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 Kukawkaf2cedb62012-02-15 06:45:39 +00001319 eth_hw_addr_random(dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001320
Jiri Pirko6c8c4442011-04-30 01:28:17 +00001321 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 Emelyanove314dbd2007-09-25 16:14:46 -07001326 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 Dumazetd0e2c552013-01-04 15:42:40 +00001337 rcu_assign_pointer(priv->peer, peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001338
1339 priv = netdev_priv(peer);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001340 rcu_assign_pointer(priv->peer, dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001341
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001342 return 0;
1343
1344err_register_dev:
1345 /* nothing to do */
Patrick McHardy3729d502010-02-26 06:34:54 +00001346err_configure_peer:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001347 unregister_netdevice(peer);
1348 return err;
1349
1350err_register_peer:
1351 free_netdev(peer);
1352 return err;
1353}
1354
Eric Dumazet23289a32009-10-27 07:06:36 +00001355static void veth_dellink(struct net_device *dev, struct list_head *head)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001356{
1357 struct veth_priv *priv;
1358 struct net_device *peer;
1359
1360 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001361 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 Dumazet24540532009-10-30 01:00:27 -07001368 unregister_netdevice_queue(dev, head);
Eric Dumazetf45a5c22013-02-08 20:10:49 +00001369
1370 if (peer) {
1371 priv = netdev_priv(peer);
1372 RCU_INIT_POINTER(priv->peer, NULL);
1373 unregister_netdevice_queue(peer, head);
1374 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001375}
1376
Thomas Graf23711432012-02-15 04:09:46 +00001377static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1378 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1379};
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001380
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001381static 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 Emelyanove314dbd2007-09-25 16:14:46 -07001389static 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 Dichtele5f4e7b2015-01-20 15:15:46 +01001398 .get_link_net = veth_get_link_net,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001399};
1400
1401/*
1402 * init/fini
1403 */
1404
1405static __init int veth_init(void)
1406{
1407 return rtnl_link_register(&veth_link_ops);
1408}
1409
1410static __exit void veth_exit(void)
1411{
Patrick McHardy68365452008-01-20 17:25:14 -08001412 rtnl_link_unregister(&veth_link_ops);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001413}
1414
1415module_init(veth_init);
1416module_exit(veth_exit);
1417
1418MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1419MODULE_LICENSE("GPL v2");
1420MODULE_ALIAS_RTNL_LINK(DRV_NAME);