blob: a552df37a347c72fed84909c45188e5dd1201df7 [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 Makitad1396002018-08-03 16:58:17 +090037/* Separating two types of XDP xmit */
38#define VETH_XDP_TX BIT(0)
39#define VETH_XDP_REDIR BIT(1)
40
Toshiaki Makita9cda7802019-06-13 18:39:59 +090041#define VETH_XDP_TX_BULK_SIZE 16
42
Toshiaki Makita4195e542018-10-11 18:36:49 +090043struct veth_rq_stats {
44 u64 xdp_packets;
45 u64 xdp_bytes;
46 u64 xdp_drops;
47 struct u64_stats_sync syncp;
48};
49
Toshiaki Makita638264d2018-08-03 16:58:18 +090050struct veth_rq {
Toshiaki Makita948d4f22018-08-03 16:58:10 +090051 struct napi_struct xdp_napi;
52 struct net_device *dev;
53 struct bpf_prog __rcu *xdp_prog;
Toshiaki Makitad1396002018-08-03 16:58:17 +090054 struct xdp_mem_info xdp_mem;
Toshiaki Makita4195e542018-10-11 18:36:49 +090055 struct veth_rq_stats stats;
Toshiaki Makita948d4f22018-08-03 16:58:10 +090056 bool rx_notify_masked;
57 struct ptr_ring xdp_ring;
58 struct xdp_rxq_info xdp_rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070059};
60
Toshiaki Makita638264d2018-08-03 16:58:18 +090061struct veth_priv {
62 struct net_device __rcu *peer;
63 atomic64_t dropped;
64 struct bpf_prog *_xdp_prog;
65 struct veth_rq *rq;
66 unsigned int requested_headroom;
67};
68
Toshiaki Makita9cda7802019-06-13 18:39:59 +090069struct veth_xdp_tx_bq {
70 struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
71 unsigned int count;
72};
73
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070074/*
75 * ethtool interface
76 */
77
Toshiaki Makitad397b962018-10-11 18:36:50 +090078struct veth_q_stat_desc {
79 char desc[ETH_GSTRING_LEN];
80 size_t offset;
81};
82
83#define VETH_RQ_STAT(m) offsetof(struct veth_rq_stats, m)
84
85static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
86 { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
87 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
88 { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
89};
90
91#define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
92
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070093static struct {
94 const char string[ETH_GSTRING_LEN];
95} ethtool_stats_keys[] = {
96 { "peer_ifindex" },
97};
98
Philippe Reynes56607b92017-03-29 08:24:21 +020099static int veth_get_link_ksettings(struct net_device *dev,
100 struct ethtool_link_ksettings *cmd)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700101{
Philippe Reynes56607b92017-03-29 08:24:21 +0200102 cmd->base.speed = SPEED_10000;
103 cmd->base.duplex = DUPLEX_FULL;
104 cmd->base.port = PORT_TP;
105 cmd->base.autoneg = AUTONEG_DISABLE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700106 return 0;
107}
108
109static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
110{
Rick Jones33a5ba12011-11-15 14:59:53 +0000111 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
112 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700113}
114
115static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
116{
Toshiaki Makitad397b962018-10-11 18:36:50 +0900117 char *p = (char *)buf;
118 int i, j;
119
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700120 switch(stringset) {
121 case ETH_SS_STATS:
Toshiaki Makitad397b962018-10-11 18:36:50 +0900122 memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
123 p += sizeof(ethtool_stats_keys);
124 for (i = 0; i < dev->real_num_rx_queues; i++) {
125 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
Florian Fainelliabdf47a2019-02-21 20:09:29 -0800126 snprintf(p, ETH_GSTRING_LEN,
127 "rx_queue_%u_%.11s",
Toshiaki Makitad397b962018-10-11 18:36:50 +0900128 i, veth_rq_stats_desc[j].desc);
129 p += ETH_GSTRING_LEN;
130 }
131 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700132 break;
133 }
134}
135
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700136static int veth_get_sset_count(struct net_device *dev, int sset)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700137{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700138 switch (sset) {
139 case ETH_SS_STATS:
Toshiaki Makitad397b962018-10-11 18:36:50 +0900140 return ARRAY_SIZE(ethtool_stats_keys) +
141 VETH_RQ_STATS_LEN * dev->real_num_rx_queues;
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700142 default:
143 return -EOPNOTSUPP;
144 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700145}
146
147static void veth_get_ethtool_stats(struct net_device *dev,
148 struct ethtool_stats *stats, u64 *data)
149{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000150 struct veth_priv *priv = netdev_priv(dev);
151 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makitad397b962018-10-11 18:36:50 +0900152 int i, j, idx;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700153
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000154 data[0] = peer ? peer->ifindex : 0;
Toshiaki Makitad397b962018-10-11 18:36:50 +0900155 idx = 1;
156 for (i = 0; i < dev->real_num_rx_queues; i++) {
157 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
158 const void *stats_base = (void *)rq_stats;
159 unsigned int start;
160 size_t offset;
161
162 do {
163 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
164 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
165 offset = veth_rq_stats_desc[j].offset;
166 data[idx + j] = *(u64 *)(stats_base + offset);
167 }
168 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
169 idx += VETH_RQ_STATS_LEN;
170 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700171}
172
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700173static const struct ethtool_ops veth_ethtool_ops = {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700174 .get_drvinfo = veth_get_drvinfo,
175 .get_link = ethtool_op_get_link,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700176 .get_strings = veth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700177 .get_sset_count = veth_get_sset_count,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700178 .get_ethtool_stats = veth_get_ethtool_stats,
Philippe Reynes56607b92017-03-29 08:24:21 +0200179 .get_link_ksettings = veth_get_link_ksettings,
Julian Wiedmann056b21f2019-04-12 13:06:15 +0200180 .get_ts_info = ethtool_op_get_ts_info,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700181};
182
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900183/* general routines */
184
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900185static bool veth_is_xdp_frame(void *ptr)
186{
187 return (unsigned long)ptr & VETH_XDP_FLAG;
188}
189
190static void *veth_ptr_to_xdp(void *ptr)
191{
192 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
193}
194
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900195static void *veth_xdp_to_ptr(void *ptr)
196{
197 return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
198}
199
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900200static void veth_ptr_free(void *ptr)
201{
202 if (veth_is_xdp_frame(ptr))
203 xdp_return_frame(veth_ptr_to_xdp(ptr));
204 else
205 kfree_skb(ptr);
206}
207
Toshiaki Makita638264d2018-08-03 16:58:18 +0900208static void __veth_xdp_flush(struct veth_rq *rq)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900209{
210 /* Write ptr_ring before reading rx_notify_masked */
211 smp_mb();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900212 if (!rq->rx_notify_masked) {
213 rq->rx_notify_masked = true;
214 napi_schedule(&rq->xdp_napi);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900215 }
216}
217
Toshiaki Makita638264d2018-08-03 16:58:18 +0900218static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900219{
Toshiaki Makita638264d2018-08-03 16:58:18 +0900220 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900221 dev_kfree_skb_any(skb);
222 return NET_RX_DROP;
223 }
224
225 return NET_RX_SUCCESS;
226}
227
Toshiaki Makita638264d2018-08-03 16:58:18 +0900228static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
229 struct veth_rq *rq, bool xdp)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700230{
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900231 return __dev_forward_skb(dev, skb) ?: xdp ?
Toshiaki Makita638264d2018-08-03 16:58:18 +0900232 veth_xdp_rx(rq, skb) :
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900233 netif_rx(skb);
234}
235
236static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
237{
238 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900239 struct veth_rq *rq = NULL;
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000240 struct net_device *rcv;
Eric Dumazet26811282012-12-29 16:02:43 +0000241 int length = skb->len;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900242 bool rcv_xdp = false;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900243 int rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700244
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000245 rcu_read_lock();
246 rcv = rcu_dereference(priv->peer);
247 if (unlikely(!rcv)) {
248 kfree_skb(skb);
249 goto drop;
250 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700251
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900252 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900253 rxq = skb_get_queue_mapping(skb);
254 if (rxq < rcv->real_num_rx_queues) {
255 rq = &rcv_priv->rq[rxq];
256 rcv_xdp = rcu_access_pointer(rq->xdp_prog);
257 if (rcv_xdp)
258 skb_record_rx_queue(skb, rxq);
259 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900260
Michael Walleaa4e6892018-08-29 17:24:11 +0200261 skb_tx_timestamp(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900262 if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
Eric Dumazetb4fba472019-11-07 16:27:17 -0800263 if (!rcv_xdp)
264 dev_lstats_add(dev, length);
Eric Dumazet26811282012-12-29 16:02:43 +0000265 } else {
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000266drop:
Eric Dumazet26811282012-12-29 16:02:43 +0000267 atomic64_inc(&priv->dropped);
268 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900269
270 if (rcv_xdp)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900271 __veth_xdp_flush(rq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900272
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000273 rcu_read_unlock();
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900274
Patrick McHardy6ed10652009-06-23 06:03:08 +0000275 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700276}
277
Eric Dumazetb4fba472019-11-07 16:27:17 -0800278static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700279{
Eric Dumazetcf05c702011-06-19 22:48:34 -0700280 struct veth_priv *priv = netdev_priv(dev);
David S. Miller11687a12009-06-25 02:45:42 -0700281
Eric Dumazetb4fba472019-11-07 16:27:17 -0800282 dev_lstats_read(dev, packets, bytes);
Eric Dumazet26811282012-12-29 16:02:43 +0000283 return atomic64_read(&priv->dropped);
284}
285
Toshiaki Makita4195e542018-10-11 18:36:49 +0900286static void veth_stats_rx(struct veth_rq_stats *result, struct net_device *dev)
287{
288 struct veth_priv *priv = netdev_priv(dev);
289 int i;
290
291 result->xdp_packets = 0;
292 result->xdp_bytes = 0;
293 result->xdp_drops = 0;
294 for (i = 0; i < dev->num_rx_queues; i++) {
295 struct veth_rq_stats *stats = &priv->rq[i].stats;
296 u64 packets, bytes, drops;
297 unsigned int start;
298
299 do {
300 start = u64_stats_fetch_begin_irq(&stats->syncp);
301 packets = stats->xdp_packets;
302 bytes = stats->xdp_bytes;
303 drops = stats->xdp_drops;
304 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
305 result->xdp_packets += packets;
306 result->xdp_bytes += bytes;
307 result->xdp_drops += drops;
308 }
309}
310
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800311static void veth_get_stats64(struct net_device *dev,
312 struct rtnl_link_stats64 *tot)
Eric Dumazet26811282012-12-29 16:02:43 +0000313{
314 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000315 struct net_device *peer;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900316 struct veth_rq_stats rx;
Eric Dumazetb4fba472019-11-07 16:27:17 -0800317 u64 packets, bytes;
Eric Dumazet26811282012-12-29 16:02:43 +0000318
Eric Dumazetb4fba472019-11-07 16:27:17 -0800319 tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
320 tot->tx_bytes = bytes;
321 tot->tx_packets = packets;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900322
323 veth_stats_rx(&rx, dev);
324 tot->rx_dropped = rx.xdp_drops;
325 tot->rx_bytes = rx.xdp_bytes;
326 tot->rx_packets = rx.xdp_packets;
Eric Dumazet26811282012-12-29 16:02:43 +0000327
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000328 rcu_read_lock();
329 peer = rcu_dereference(priv->peer);
330 if (peer) {
Eric Dumazetb4fba472019-11-07 16:27:17 -0800331 tot->rx_dropped += veth_stats_tx(peer, &packets, &bytes);
332 tot->rx_bytes += bytes;
333 tot->rx_packets += packets;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900334
335 veth_stats_rx(&rx, peer);
336 tot->tx_bytes += rx.xdp_bytes;
337 tot->tx_packets += rx.xdp_packets;
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000338 }
339 rcu_read_unlock();
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700340}
341
Gao feng5c70ef82013-10-04 16:52:24 +0800342/* fake multicast ability */
343static void veth_set_multicast_list(struct net_device *dev)
344{
345}
346
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900347static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
348 int buflen)
349{
350 struct sk_buff *skb;
351
352 if (!buflen) {
353 buflen = SKB_DATA_ALIGN(headroom + len) +
354 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
355 }
356 skb = build_skb(head, buflen);
357 if (!skb)
358 return NULL;
359
360 skb_reserve(skb, headroom);
361 skb_put(skb, len);
362
363 return skb;
364}
365
Toshiaki Makita638264d2018-08-03 16:58:18 +0900366static int veth_select_rxq(struct net_device *dev)
367{
368 return smp_processor_id() % dev->real_num_rx_queues;
369}
370
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900371static int veth_xdp_xmit(struct net_device *dev, int n,
372 struct xdp_frame **frames, u32 flags)
373{
374 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
375 struct net_device *rcv;
Toshiaki Makita21314792018-10-11 18:36:48 +0900376 int i, ret, drops = n;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900377 unsigned int max_len;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900378 struct veth_rq *rq;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900379
Toshiaki Makita21314792018-10-11 18:36:48 +0900380 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
381 ret = -EINVAL;
382 goto drop;
383 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900384
385 rcv = rcu_dereference(priv->peer);
Toshiaki Makita21314792018-10-11 18:36:48 +0900386 if (unlikely(!rcv)) {
387 ret = -ENXIO;
388 goto drop;
389 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900390
391 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900392 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900393 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
394 * side. This means an XDP program is loaded on the peer and the peer
395 * device is up.
396 */
Toshiaki Makita21314792018-10-11 18:36:48 +0900397 if (!rcu_access_pointer(rq->xdp_prog)) {
398 ret = -ENXIO;
399 goto drop;
400 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900401
Toshiaki Makita21314792018-10-11 18:36:48 +0900402 drops = 0;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900403 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
404
Toshiaki Makita638264d2018-08-03 16:58:18 +0900405 spin_lock(&rq->xdp_ring.producer_lock);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900406 for (i = 0; i < n; i++) {
407 struct xdp_frame *frame = frames[i];
408 void *ptr = veth_xdp_to_ptr(frame);
409
410 if (unlikely(frame->len > max_len ||
Toshiaki Makita638264d2018-08-03 16:58:18 +0900411 __ptr_ring_produce(&rq->xdp_ring, ptr))) {
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900412 xdp_return_frame_rx_napi(frame);
413 drops++;
414 }
415 }
Toshiaki Makita638264d2018-08-03 16:58:18 +0900416 spin_unlock(&rq->xdp_ring.producer_lock);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900417
418 if (flags & XDP_XMIT_FLUSH)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900419 __veth_xdp_flush(rq);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900420
Toshiaki Makita21314792018-10-11 18:36:48 +0900421 if (likely(!drops))
422 return n;
423
424 ret = n - drops;
425drop:
426 atomic64_add(drops, &priv->dropped);
427
428 return ret;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900429}
430
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900431static void veth_xdp_flush_bq(struct net_device *dev, struct veth_xdp_tx_bq *bq)
432{
433 int sent, i, err = 0;
434
435 sent = veth_xdp_xmit(dev, bq->count, bq->q, 0);
436 if (sent < 0) {
437 err = sent;
438 sent = 0;
439 for (i = 0; i < bq->count; i++)
440 xdp_return_frame(bq->q[i]);
441 }
442 trace_xdp_bulk_tx(dev, sent, bq->count - sent, err);
443
444 bq->count = 0;
445}
446
447static void veth_xdp_flush(struct net_device *dev, struct veth_xdp_tx_bq *bq)
Toshiaki Makitad1396002018-08-03 16:58:17 +0900448{
449 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
450 struct net_device *rcv;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900451 struct veth_rq *rq;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900452
453 rcu_read_lock();
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900454 veth_xdp_flush_bq(dev, bq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900455 rcv = rcu_dereference(priv->peer);
456 if (unlikely(!rcv))
457 goto out;
458
459 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900460 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
Toshiaki Makitad1396002018-08-03 16:58:17 +0900461 /* xdp_ring is initialized on receive side? */
Toshiaki Makita638264d2018-08-03 16:58:18 +0900462 if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
Toshiaki Makitad1396002018-08-03 16:58:17 +0900463 goto out;
464
Toshiaki Makita638264d2018-08-03 16:58:18 +0900465 __veth_xdp_flush(rq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900466out:
467 rcu_read_unlock();
468}
469
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900470static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp,
471 struct veth_xdp_tx_bq *bq)
Toshiaki Makitad1396002018-08-03 16:58:17 +0900472{
473 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
474
475 if (unlikely(!frame))
476 return -EOVERFLOW;
477
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900478 if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
479 veth_xdp_flush_bq(dev, bq);
480
481 bq->q[bq->count++] = frame;
482
483 return 0;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900484}
485
Toshiaki Makita638264d2018-08-03 16:58:18 +0900486static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
Toshiaki Makitad1396002018-08-03 16:58:17 +0900487 struct xdp_frame *frame,
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900488 unsigned int *xdp_xmit,
489 struct veth_xdp_tx_bq *bq)
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900490{
491 void *hard_start = frame->data - frame->headroom;
492 void *head = hard_start - sizeof(struct xdp_frame);
493 int len = frame->len, delta = 0;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900494 struct xdp_frame orig_frame;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900495 struct bpf_prog *xdp_prog;
496 unsigned int headroom;
497 struct sk_buff *skb;
498
499 rcu_read_lock();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900500 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900501 if (likely(xdp_prog)) {
502 struct xdp_buff xdp;
503 u32 act;
504
505 xdp.data_hard_start = hard_start;
506 xdp.data = frame->data;
507 xdp.data_end = frame->data + frame->len;
508 xdp.data_meta = frame->data - frame->metasize;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900509 xdp.rxq = &rq->xdp_rxq;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900510
511 act = bpf_prog_run_xdp(xdp_prog, &xdp);
512
513 switch (act) {
514 case XDP_PASS:
515 delta = frame->data - xdp.data;
516 len = xdp.data_end - xdp.data;
517 break;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900518 case XDP_TX:
519 orig_frame = *frame;
520 xdp.data_hard_start = head;
521 xdp.rxq->mem = frame->mem;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900522 if (unlikely(veth_xdp_tx(rq->dev, &xdp, bq) < 0)) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900523 trace_xdp_exception(rq->dev, xdp_prog, act);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900524 frame = &orig_frame;
525 goto err_xdp;
526 }
527 *xdp_xmit |= VETH_XDP_TX;
528 rcu_read_unlock();
529 goto xdp_xmit;
530 case XDP_REDIRECT:
531 orig_frame = *frame;
532 xdp.data_hard_start = head;
533 xdp.rxq->mem = frame->mem;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900534 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
Toshiaki Makitad1396002018-08-03 16:58:17 +0900535 frame = &orig_frame;
536 goto err_xdp;
537 }
538 *xdp_xmit |= VETH_XDP_REDIR;
539 rcu_read_unlock();
540 goto xdp_xmit;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900541 default:
542 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600543 /* fall through */
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900544 case XDP_ABORTED:
Toshiaki Makita638264d2018-08-03 16:58:18 +0900545 trace_xdp_exception(rq->dev, xdp_prog, act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600546 /* fall through */
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900547 case XDP_DROP:
548 goto err_xdp;
549 }
550 }
551 rcu_read_unlock();
552
553 headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
554 skb = veth_build_skb(head, headroom, len, 0);
555 if (!skb) {
556 xdp_return_frame(frame);
557 goto err;
558 }
559
Jesper Dangaard Brouercbf33512019-06-18 15:05:32 +0200560 xdp_release_frame(frame);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900561 xdp_scrub_frame(frame);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900562 skb->protocol = eth_type_trans(skb, rq->dev);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900563err:
564 return skb;
565err_xdp:
566 rcu_read_unlock();
567 xdp_return_frame(frame);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900568xdp_xmit:
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900569 return NULL;
570}
571
Toshiaki Makita638264d2018-08-03 16:58:18 +0900572static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq, struct sk_buff *skb,
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900573 unsigned int *xdp_xmit,
574 struct veth_xdp_tx_bq *bq)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900575{
576 u32 pktlen, headroom, act, metalen;
577 void *orig_data, *orig_data_end;
578 struct bpf_prog *xdp_prog;
579 int mac_len, delta, off;
580 struct xdp_buff xdp;
581
Toshiaki Makita4bf9ffa2018-09-14 13:33:44 +0900582 skb_orphan(skb);
583
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900584 rcu_read_lock();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900585 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900586 if (unlikely(!xdp_prog)) {
587 rcu_read_unlock();
588 goto out;
589 }
590
591 mac_len = skb->data - skb_mac_header(skb);
592 pktlen = skb->len + mac_len;
593 headroom = skb_headroom(skb) - mac_len;
594
595 if (skb_shared(skb) || skb_head_is_locked(skb) ||
596 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
597 struct sk_buff *nskb;
598 int size, head_off;
599 void *head, *start;
600 struct page *page;
601
602 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
603 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
604 if (size > PAGE_SIZE)
605 goto drop;
606
607 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
608 if (!page)
609 goto drop;
610
611 head = page_address(page);
612 start = head + VETH_XDP_HEADROOM;
613 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
614 page_frag_free(head);
615 goto drop;
616 }
617
618 nskb = veth_build_skb(head,
619 VETH_XDP_HEADROOM + mac_len, skb->len,
620 PAGE_SIZE);
621 if (!nskb) {
622 page_frag_free(head);
623 goto drop;
624 }
625
626 skb_copy_header(nskb, skb);
627 head_off = skb_headroom(nskb) - skb_headroom(skb);
628 skb_headers_offset_update(nskb, head_off);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900629 consume_skb(skb);
630 skb = nskb;
631 }
632
633 xdp.data_hard_start = skb->head;
634 xdp.data = skb_mac_header(skb);
635 xdp.data_end = xdp.data + pktlen;
636 xdp.data_meta = xdp.data;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900637 xdp.rxq = &rq->xdp_rxq;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900638 orig_data = xdp.data;
639 orig_data_end = xdp.data_end;
640
641 act = bpf_prog_run_xdp(xdp_prog, &xdp);
642
643 switch (act) {
644 case XDP_PASS:
645 break;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900646 case XDP_TX:
647 get_page(virt_to_page(xdp.data));
648 consume_skb(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900649 xdp.rxq->mem = rq->xdp_mem;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900650 if (unlikely(veth_xdp_tx(rq->dev, &xdp, bq) < 0)) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900651 trace_xdp_exception(rq->dev, xdp_prog, act);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900652 goto err_xdp;
653 }
654 *xdp_xmit |= VETH_XDP_TX;
655 rcu_read_unlock();
656 goto xdp_xmit;
657 case XDP_REDIRECT:
658 get_page(virt_to_page(xdp.data));
659 consume_skb(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900660 xdp.rxq->mem = rq->xdp_mem;
661 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog))
Toshiaki Makitad1396002018-08-03 16:58:17 +0900662 goto err_xdp;
663 *xdp_xmit |= VETH_XDP_REDIR;
664 rcu_read_unlock();
665 goto xdp_xmit;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900666 default:
667 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600668 /* fall through */
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900669 case XDP_ABORTED:
Toshiaki Makita638264d2018-08-03 16:58:18 +0900670 trace_xdp_exception(rq->dev, xdp_prog, act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600671 /* fall through */
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900672 case XDP_DROP:
673 goto drop;
674 }
675 rcu_read_unlock();
676
677 delta = orig_data - xdp.data;
678 off = mac_len + delta;
679 if (off > 0)
680 __skb_push(skb, off);
681 else if (off < 0)
682 __skb_pull(skb, -off);
683 skb->mac_header -= delta;
684 off = xdp.data_end - orig_data_end;
685 if (off != 0)
686 __skb_put(skb, off);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900687 skb->protocol = eth_type_trans(skb, rq->dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900688
689 metalen = xdp.data - xdp.data_meta;
690 if (metalen)
691 skb_metadata_set(skb, metalen);
692out:
693 return skb;
694drop:
695 rcu_read_unlock();
696 kfree_skb(skb);
697 return NULL;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900698err_xdp:
699 rcu_read_unlock();
700 page_frag_free(xdp.data);
701xdp_xmit:
702 return NULL;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900703}
704
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900705static int veth_xdp_rcv(struct veth_rq *rq, int budget, unsigned int *xdp_xmit,
706 struct veth_xdp_tx_bq *bq)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900707{
Toshiaki Makita4195e542018-10-11 18:36:49 +0900708 int i, done = 0, drops = 0, bytes = 0;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900709
710 for (i = 0; i < budget; i++) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900711 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
Toshiaki Makita4195e542018-10-11 18:36:49 +0900712 unsigned int xdp_xmit_one = 0;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900713 struct sk_buff *skb;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900714
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900715 if (!ptr)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900716 break;
717
Toshiaki Makitad1396002018-08-03 16:58:17 +0900718 if (veth_is_xdp_frame(ptr)) {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900719 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
720
721 bytes += frame->len;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900722 skb = veth_xdp_rcv_one(rq, frame, &xdp_xmit_one, bq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900723 } else {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900724 skb = ptr;
725 bytes += skb->len;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900726 skb = veth_xdp_rcv_skb(rq, skb, &xdp_xmit_one, bq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900727 }
Toshiaki Makita4195e542018-10-11 18:36:49 +0900728 *xdp_xmit |= xdp_xmit_one;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900729
730 if (skb)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900731 napi_gro_receive(&rq->xdp_napi, skb);
Toshiaki Makita4195e542018-10-11 18:36:49 +0900732 else if (!xdp_xmit_one)
733 drops++;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900734
735 done++;
736 }
737
Toshiaki Makita4195e542018-10-11 18:36:49 +0900738 u64_stats_update_begin(&rq->stats.syncp);
739 rq->stats.xdp_packets += done;
740 rq->stats.xdp_bytes += bytes;
741 rq->stats.xdp_drops += drops;
742 u64_stats_update_end(&rq->stats.syncp);
743
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900744 return done;
745}
746
747static int veth_poll(struct napi_struct *napi, int budget)
748{
Toshiaki Makita638264d2018-08-03 16:58:18 +0900749 struct veth_rq *rq =
750 container_of(napi, struct veth_rq, xdp_napi);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900751 unsigned int xdp_xmit = 0;
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900752 struct veth_xdp_tx_bq bq;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900753 int done;
754
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900755 bq.count = 0;
756
Toshiaki Makitad1396002018-08-03 16:58:17 +0900757 xdp_set_return_frame_no_direct();
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900758 done = veth_xdp_rcv(rq, budget, &xdp_xmit, &bq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900759
760 if (done < budget && napi_complete_done(napi, done)) {
761 /* Write rx_notify_masked before reading ptr_ring */
Toshiaki Makita638264d2018-08-03 16:58:18 +0900762 smp_store_mb(rq->rx_notify_masked, false);
763 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
764 rq->rx_notify_masked = true;
765 napi_schedule(&rq->xdp_napi);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900766 }
767 }
768
Toshiaki Makitad1396002018-08-03 16:58:17 +0900769 if (xdp_xmit & VETH_XDP_TX)
Toshiaki Makita9cda7802019-06-13 18:39:59 +0900770 veth_xdp_flush(rq->dev, &bq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900771 if (xdp_xmit & VETH_XDP_REDIR)
772 xdp_do_flush_map();
773 xdp_clear_return_frame_no_direct();
774
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900775 return done;
776}
777
778static int veth_napi_add(struct net_device *dev)
779{
780 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900781 int err, i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900782
Toshiaki Makita638264d2018-08-03 16:58:18 +0900783 for (i = 0; i < dev->real_num_rx_queues; i++) {
784 struct veth_rq *rq = &priv->rq[i];
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900785
Toshiaki Makita638264d2018-08-03 16:58:18 +0900786 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
787 if (err)
788 goto err_xdp_ring;
789 }
790
791 for (i = 0; i < dev->real_num_rx_queues; i++) {
792 struct veth_rq *rq = &priv->rq[i];
793
794 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
795 napi_enable(&rq->xdp_napi);
796 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900797
798 return 0;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900799err_xdp_ring:
800 for (i--; i >= 0; i--)
801 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
802
803 return err;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900804}
805
806static void veth_napi_del(struct net_device *dev)
807{
808 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900809 int i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900810
Toshiaki Makita638264d2018-08-03 16:58:18 +0900811 for (i = 0; i < dev->real_num_rx_queues; i++) {
812 struct veth_rq *rq = &priv->rq[i];
813
814 napi_disable(&rq->xdp_napi);
815 napi_hash_del(&rq->xdp_napi);
816 }
817 synchronize_net();
818
819 for (i = 0; i < dev->real_num_rx_queues; i++) {
820 struct veth_rq *rq = &priv->rq[i];
821
822 netif_napi_del(&rq->xdp_napi);
823 rq->rx_notify_masked = false;
824 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
825 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900826}
827
828static int veth_enable_xdp(struct net_device *dev)
829{
830 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900831 int err, i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900832
Toshiaki Makita638264d2018-08-03 16:58:18 +0900833 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
834 for (i = 0; i < dev->real_num_rx_queues; i++) {
835 struct veth_rq *rq = &priv->rq[i];
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900836
Toshiaki Makita638264d2018-08-03 16:58:18 +0900837 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i);
838 if (err < 0)
839 goto err_rxq_reg;
840
841 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
842 MEM_TYPE_PAGE_SHARED,
843 NULL);
844 if (err < 0)
845 goto err_reg_mem;
846
847 /* Save original mem info as it can be overwritten */
848 rq->xdp_mem = rq->xdp_rxq.mem;
849 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900850
851 err = veth_napi_add(dev);
852 if (err)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900853 goto err_rxq_reg;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900854 }
855
Toshiaki Makita638264d2018-08-03 16:58:18 +0900856 for (i = 0; i < dev->real_num_rx_queues; i++)
857 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900858
859 return 0;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900860err_reg_mem:
861 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
862err_rxq_reg:
863 for (i--; i >= 0; i--)
864 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900865
866 return err;
867}
868
869static void veth_disable_xdp(struct net_device *dev)
870{
871 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900872 int i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900873
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, NULL);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900876 veth_napi_del(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900877 for (i = 0; i < dev->real_num_rx_queues; i++) {
878 struct veth_rq *rq = &priv->rq[i];
879
880 rq->xdp_rxq.mem = rq->xdp_mem;
881 xdp_rxq_info_unreg(&rq->xdp_rxq);
882 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900883}
884
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700885static int veth_open(struct net_device *dev)
886{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000887 struct veth_priv *priv = netdev_priv(dev);
888 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900889 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700890
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000891 if (!peer)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700892 return -ENOTCONN;
893
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900894 if (priv->_xdp_prog) {
895 err = veth_enable_xdp(dev);
896 if (err)
897 return err;
898 }
899
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000900 if (peer->flags & IFF_UP) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700901 netif_carrier_on(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000902 netif_carrier_on(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700903 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900904
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700905 return 0;
906}
907
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000908static int veth_close(struct net_device *dev)
909{
910 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000911 struct net_device *peer = rtnl_dereference(priv->peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000912
913 netif_carrier_off(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000914 if (peer)
915 netif_carrier_off(peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000916
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900917 if (priv->_xdp_prog)
918 veth_disable_xdp(dev);
919
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000920 return 0;
921}
922
Jarod Wilson91572082016-10-20 13:55:20 -0400923static int is_valid_veth_mtu(int mtu)
Eric Biederman38d40812009-03-03 23:36:04 -0800924{
Jarod Wilson91572082016-10-20 13:55:20 -0400925 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
Eric Biederman38d40812009-03-03 23:36:04 -0800926}
927
Toshiaki Makita7797b932018-08-15 17:07:29 +0900928static int veth_alloc_queues(struct net_device *dev)
929{
930 struct veth_priv *priv = netdev_priv(dev);
931 int i;
932
933 priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
934 if (!priv->rq)
935 return -ENOMEM;
936
Toshiaki Makita4195e542018-10-11 18:36:49 +0900937 for (i = 0; i < dev->num_rx_queues; i++) {
Toshiaki Makita7797b932018-08-15 17:07:29 +0900938 priv->rq[i].dev = dev;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900939 u64_stats_init(&priv->rq[i].stats.syncp);
940 }
Toshiaki Makita7797b932018-08-15 17:07:29 +0900941
942 return 0;
943}
944
945static void veth_free_queues(struct net_device *dev)
946{
947 struct veth_priv *priv = netdev_priv(dev);
948
949 kfree(priv->rq);
950}
951
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700952static int veth_dev_init(struct net_device *dev)
953{
Toshiaki Makita7797b932018-08-15 17:07:29 +0900954 int err;
955
Li RongQing14d73412018-09-17 18:46:55 +0800956 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
957 if (!dev->lstats)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700958 return -ENOMEM;
Toshiaki Makita7797b932018-08-15 17:07:29 +0900959
960 err = veth_alloc_queues(dev);
961 if (err) {
Li RongQing14d73412018-09-17 18:46:55 +0800962 free_percpu(dev->lstats);
Toshiaki Makita7797b932018-08-15 17:07:29 +0900963 return err;
964 }
965
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700966 return 0;
967}
968
David S. Miller11687a12009-06-25 02:45:42 -0700969static void veth_dev_free(struct net_device *dev)
970{
Toshiaki Makita7797b932018-08-15 17:07:29 +0900971 veth_free_queues(dev);
Li RongQing14d73412018-09-17 18:46:55 +0800972 free_percpu(dev->lstats);
David S. Miller11687a12009-06-25 02:45:42 -0700973}
974
WANG Congbb446c12014-06-23 15:36:02 -0700975#ifdef CONFIG_NET_POLL_CONTROLLER
976static void veth_poll_controller(struct net_device *dev)
977{
978 /* veth only receives frames when its peer sends one
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900979 * Since it has nothing to do with disabling irqs, we are guaranteed
WANG Congbb446c12014-06-23 15:36:02 -0700980 * never to have pending data when we poll for it so
981 * there is nothing to do here.
982 *
983 * We need this though so netpoll recognizes us as an interface that
984 * supports polling, which enables bridge devices in virt setups to
985 * still use netconsole
986 */
987}
988#endif /* CONFIG_NET_POLL_CONTROLLER */
989
Nicolas Dichtela45253b2015-04-02 17:07:11 +0200990static int veth_get_iflink(const struct net_device *dev)
991{
992 struct veth_priv *priv = netdev_priv(dev);
993 struct net_device *peer;
994 int iflink;
995
996 rcu_read_lock();
997 peer = rcu_dereference(priv->peer);
998 iflink = peer ? peer->ifindex : 0;
999 rcu_read_unlock();
1000
1001 return iflink;
1002}
1003
Toshiaki Makitadc224822018-08-03 16:58:11 +09001004static netdev_features_t veth_fix_features(struct net_device *dev,
1005 netdev_features_t features)
1006{
1007 struct veth_priv *priv = netdev_priv(dev);
1008 struct net_device *peer;
1009
1010 peer = rtnl_dereference(priv->peer);
1011 if (peer) {
1012 struct veth_priv *peer_priv = netdev_priv(peer);
1013
1014 if (peer_priv->_xdp_prog)
1015 features &= ~NETIF_F_GSO_SOFTWARE;
1016 }
1017
1018 return features;
1019}
1020
Paolo Abeni163e5292016-02-26 10:45:41 +01001021static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1022{
1023 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1024 struct net_device *peer;
1025
1026 if (new_hr < 0)
1027 new_hr = 0;
1028
1029 rcu_read_lock();
1030 peer = rcu_dereference(priv->peer);
1031 if (unlikely(!peer))
1032 goto out;
1033
1034 peer_priv = netdev_priv(peer);
1035 priv->requested_headroom = new_hr;
1036 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1037 dev->needed_headroom = new_hr;
1038 peer->needed_headroom = new_hr;
1039
1040out:
1041 rcu_read_unlock();
1042}
1043
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001044static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1045 struct netlink_ext_ack *extack)
1046{
1047 struct veth_priv *priv = netdev_priv(dev);
1048 struct bpf_prog *old_prog;
1049 struct net_device *peer;
Toshiaki Makitadc224822018-08-03 16:58:11 +09001050 unsigned int max_mtu;
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001051 int err;
1052
1053 old_prog = priv->_xdp_prog;
1054 priv->_xdp_prog = prog;
1055 peer = rtnl_dereference(priv->peer);
1056
1057 if (prog) {
1058 if (!peer) {
1059 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1060 err = -ENOTCONN;
1061 goto err;
1062 }
1063
Toshiaki Makitadc224822018-08-03 16:58:11 +09001064 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1065 peer->hard_header_len -
1066 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1067 if (peer->mtu > max_mtu) {
1068 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1069 err = -ERANGE;
1070 goto err;
1071 }
1072
Toshiaki Makita638264d2018-08-03 16:58:18 +09001073 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1074 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1075 err = -ENOSPC;
1076 goto err;
1077 }
1078
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001079 if (dev->flags & IFF_UP) {
1080 err = veth_enable_xdp(dev);
1081 if (err) {
1082 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1083 goto err;
1084 }
1085 }
Toshiaki Makitadc224822018-08-03 16:58:11 +09001086
1087 if (!old_prog) {
1088 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1089 peer->max_mtu = max_mtu;
1090 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001091 }
1092
1093 if (old_prog) {
Toshiaki Makitadc224822018-08-03 16:58:11 +09001094 if (!prog) {
1095 if (dev->flags & IFF_UP)
1096 veth_disable_xdp(dev);
1097
1098 if (peer) {
1099 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1100 peer->max_mtu = ETH_MAX_MTU;
1101 }
1102 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001103 bpf_prog_put(old_prog);
1104 }
1105
Toshiaki Makitadc224822018-08-03 16:58:11 +09001106 if ((!!old_prog ^ !!prog) && peer)
1107 netdev_update_features(peer);
1108
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001109 return 0;
1110err:
1111 priv->_xdp_prog = old_prog;
1112
1113 return err;
1114}
1115
1116static u32 veth_xdp_query(struct net_device *dev)
1117{
1118 struct veth_priv *priv = netdev_priv(dev);
1119 const struct bpf_prog *xdp_prog;
1120
1121 xdp_prog = priv->_xdp_prog;
1122 if (xdp_prog)
1123 return xdp_prog->aux->id;
1124
1125 return 0;
1126}
1127
1128static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1129{
1130 switch (xdp->command) {
1131 case XDP_SETUP_PROG:
1132 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1133 case XDP_QUERY_PROG:
1134 xdp->prog_id = veth_xdp_query(dev);
1135 return 0;
1136 default:
1137 return -EINVAL;
1138 }
1139}
1140
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001141static const struct net_device_ops veth_netdev_ops = {
Daniel Lezcanoee923622009-02-22 00:04:45 -08001142 .ndo_init = veth_dev_init,
1143 .ndo_open = veth_open,
Eric W. Biederman2cf48a12009-02-25 19:47:29 +00001144 .ndo_stop = veth_close,
Daniel Lezcanoee923622009-02-22 00:04:45 -08001145 .ndo_start_xmit = veth_xmit,
stephen hemminger6311cc42011-06-08 14:53:59 +00001146 .ndo_get_stats64 = veth_get_stats64,
Gao feng5c70ef82013-10-04 16:52:24 +08001147 .ndo_set_rx_mode = veth_set_multicast_list,
Daniel Lezcanoee923622009-02-22 00:04:45 -08001148 .ndo_set_mac_address = eth_mac_addr,
WANG Congbb446c12014-06-23 15:36:02 -07001149#ifdef CONFIG_NET_POLL_CONTROLLER
1150 .ndo_poll_controller = veth_poll_controller,
1151#endif
Nicolas Dichtela45253b2015-04-02 17:07:11 +02001152 .ndo_get_iflink = veth_get_iflink,
Toshiaki Makitadc224822018-08-03 16:58:11 +09001153 .ndo_fix_features = veth_fix_features,
Toshiaki Makita1a04a822015-07-31 15:03:25 +09001154 .ndo_features_check = passthru_features_check,
Paolo Abeni163e5292016-02-26 10:45:41 +01001155 .ndo_set_rx_headroom = veth_set_rx_headroom,
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001156 .ndo_bpf = veth_xdp,
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +09001157 .ndo_xdp_xmit = veth_xdp_xmit,
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001158};
1159
Alexander Duyck732912d72016-04-19 14:02:26 -04001160#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
Xin Longc80fafb2016-08-25 13:21:49 +08001161 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
Alexander Duyck732912d72016-04-19 14:02:26 -04001162 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
Patrick McHardy28d2b132013-04-19 02:04:32 +00001163 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1164 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
Eric Dumazet80933152012-12-29 16:26:10 +00001165
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001166static void veth_setup(struct net_device *dev)
1167{
1168 ether_setup(dev);
1169
Neil Horman550fd082011-07-26 06:05:38 +00001170 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Hannes Frederic Sowa23ea5a92012-10-30 16:22:01 +00001171 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Phil Sutter02f01ec2015-08-18 10:30:29 +02001172 dev->priv_flags |= IFF_NO_QUEUE;
Paolo Abeni163e5292016-02-26 10:45:41 +01001173 dev->priv_flags |= IFF_PHONY_HEADROOM;
Neil Horman550fd082011-07-26 06:05:38 +00001174
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001175 dev->netdev_ops = &veth_netdev_ops;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001176 dev->ethtool_ops = &veth_ethtool_ops;
1177 dev->features |= NETIF_F_LLTX;
Eric Dumazet80933152012-12-29 16:26:10 +00001178 dev->features |= VETH_FEATURES;
Toshiaki Makita8d0d21f2014-02-18 21:20:08 +09001179 dev->vlan_features = dev->features &
Vlad Yasevich3f8c7072014-03-27 22:14:48 -04001180 ~(NETIF_F_HW_VLAN_CTAG_TX |
1181 NETIF_F_HW_VLAN_STAG_TX |
1182 NETIF_F_HW_VLAN_CTAG_RX |
1183 NETIF_F_HW_VLAN_STAG_RX);
David S. Millercf124db2017-05-08 12:52:56 -04001184 dev->needs_free_netdev = true;
1185 dev->priv_destructor = veth_dev_free;
Jarod Wilson91572082016-10-20 13:55:20 -04001186 dev->max_mtu = ETH_MAX_MTU;
Michał Mirosława2c725f2011-03-31 01:01:35 +00001187
Eric Dumazet80933152012-12-29 16:26:10 +00001188 dev->hw_features = VETH_FEATURES;
Eric Dumazet82d81892013-10-25 18:25:03 -07001189 dev->hw_enc_features = VETH_FEATURES;
David Ahern607fca92016-08-24 20:10:45 -07001190 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001191}
1192
1193/*
1194 * netlink interface
1195 */
1196
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001197static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1198 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001199{
1200 if (tb[IFLA_ADDRESS]) {
1201 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1202 return -EINVAL;
1203 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1204 return -EADDRNOTAVAIL;
1205 }
Eric Biederman38d40812009-03-03 23:36:04 -08001206 if (tb[IFLA_MTU]) {
1207 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1208 return -EINVAL;
1209 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001210 return 0;
1211}
1212
1213static struct rtnl_link_ops veth_link_ops;
1214
Eric W. Biederman81adee42009-11-08 00:53:51 -08001215static int veth_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001216 struct nlattr *tb[], struct nlattr *data[],
1217 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001218{
Toshiaki Makita7797b932018-08-15 17:07:29 +09001219 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001220 struct net_device *peer;
1221 struct veth_priv *priv;
1222 char ifname[IFNAMSIZ];
1223 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
Tom Gundersen55177502014-07-14 16:37:25 +02001224 unsigned char name_assign_type;
Patrick McHardy3729d502010-02-26 06:34:54 +00001225 struct ifinfomsg *ifmp;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001226 struct net *net;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001227
1228 /*
1229 * create and register peer first
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001230 */
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001231 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1232 struct nlattr *nla_peer;
1233
1234 nla_peer = data[VETH_INFO_PEER];
Patrick McHardy3729d502010-02-26 06:34:54 +00001235 ifmp = nla_data(nla_peer);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001236 err = rtnl_nla_parse_ifla(peer_tb,
1237 nla_data(nla_peer) + sizeof(struct ifinfomsg),
Johannes Bergfceb6432017-04-12 14:34:07 +02001238 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1239 NULL);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001240 if (err < 0)
1241 return err;
1242
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001243 err = veth_validate(peer_tb, NULL, extack);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001244 if (err < 0)
1245 return err;
1246
1247 tbp = peer_tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001248 } else {
1249 ifmp = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001250 tbp = tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001251 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001252
Serhey Popovych191cdb32017-06-21 12:12:24 +03001253 if (ifmp && tbp[IFLA_IFNAME]) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001254 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
Tom Gundersen55177502014-07-14 16:37:25 +02001255 name_assign_type = NET_NAME_USER;
1256 } else {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001257 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
Tom Gundersen55177502014-07-14 16:37:25 +02001258 name_assign_type = NET_NAME_ENUM;
1259 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001260
Eric W. Biederman81adee42009-11-08 00:53:51 -08001261 net = rtnl_link_get_net(src_net, tbp);
1262 if (IS_ERR(net))
1263 return PTR_ERR(net);
1264
Tom Gundersen55177502014-07-14 16:37:25 +02001265 peer = rtnl_create_link(net, ifname, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001266 &veth_link_ops, tbp, extack);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001267 if (IS_ERR(peer)) {
1268 put_net(net);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001269 return PTR_ERR(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001270 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001271
Serhey Popovych191cdb32017-06-21 12:12:24 +03001272 if (!ifmp || !tbp[IFLA_ADDRESS])
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001273 eth_hw_addr_random(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001274
Pavel Emelyanove6f8f1a2012-08-08 21:53:03 +00001275 if (ifmp && (dev->ifindex != 0))
1276 peer->ifindex = ifmp->ifi_index;
1277
Stephen Hemminger72d249552017-12-07 15:40:20 -08001278 peer->gso_max_size = dev->gso_max_size;
1279 peer->gso_max_segs = dev->gso_max_segs;
1280
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001281 err = register_netdevice(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001282 put_net(net);
1283 net = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001284 if (err < 0)
1285 goto err_register_peer;
1286
1287 netif_carrier_off(peer);
1288
Patrick McHardy3729d502010-02-26 06:34:54 +00001289 err = rtnl_configure_link(peer, ifmp);
1290 if (err < 0)
1291 goto err_configure_peer;
1292
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001293 /*
1294 * register dev last
1295 *
1296 * note, that since we've registered new device the dev's name
1297 * should be re-allocated
1298 */
1299
1300 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001301 eth_hw_addr_random(dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001302
Jiri Pirko6c8c4442011-04-30 01:28:17 +00001303 if (tb[IFLA_IFNAME])
1304 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1305 else
1306 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1307
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001308 err = register_netdevice(dev);
1309 if (err < 0)
1310 goto err_register_dev;
1311
1312 netif_carrier_off(dev);
1313
1314 /*
1315 * tie the deviced together
1316 */
1317
1318 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001319 rcu_assign_pointer(priv->peer, peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001320
1321 priv = netdev_priv(peer);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001322 rcu_assign_pointer(priv->peer, dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001323
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001324 return 0;
1325
1326err_register_dev:
1327 /* nothing to do */
Patrick McHardy3729d502010-02-26 06:34:54 +00001328err_configure_peer:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001329 unregister_netdevice(peer);
1330 return err;
1331
1332err_register_peer:
1333 free_netdev(peer);
1334 return err;
1335}
1336
Eric Dumazet23289a32009-10-27 07:06:36 +00001337static void veth_dellink(struct net_device *dev, struct list_head *head)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001338{
1339 struct veth_priv *priv;
1340 struct net_device *peer;
1341
1342 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001343 peer = rtnl_dereference(priv->peer);
1344
1345 /* Note : dellink() is called from default_device_exit_batch(),
1346 * before a rcu_synchronize() point. The devices are guaranteed
1347 * not being freed before one RCU grace period.
1348 */
1349 RCU_INIT_POINTER(priv->peer, NULL);
Eric Dumazet24540532009-10-30 01:00:27 -07001350 unregister_netdevice_queue(dev, head);
Eric Dumazetf45a5c22013-02-08 20:10:49 +00001351
1352 if (peer) {
1353 priv = netdev_priv(peer);
1354 RCU_INIT_POINTER(priv->peer, NULL);
1355 unregister_netdevice_queue(peer, head);
1356 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001357}
1358
Thomas Graf23711432012-02-15 04:09:46 +00001359static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1360 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1361};
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001362
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001363static struct net *veth_get_link_net(const struct net_device *dev)
1364{
1365 struct veth_priv *priv = netdev_priv(dev);
1366 struct net_device *peer = rtnl_dereference(priv->peer);
1367
1368 return peer ? dev_net(peer) : dev_net(dev);
1369}
1370
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001371static struct rtnl_link_ops veth_link_ops = {
1372 .kind = DRV_NAME,
1373 .priv_size = sizeof(struct veth_priv),
1374 .setup = veth_setup,
1375 .validate = veth_validate,
1376 .newlink = veth_newlink,
1377 .dellink = veth_dellink,
1378 .policy = veth_policy,
1379 .maxtype = VETH_INFO_MAX,
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001380 .get_link_net = veth_get_link_net,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001381};
1382
1383/*
1384 * init/fini
1385 */
1386
1387static __init int veth_init(void)
1388{
1389 return rtnl_link_register(&veth_link_ops);
1390}
1391
1392static __exit void veth_exit(void)
1393{
Patrick McHardy68365452008-01-20 17:25:14 -08001394 rtnl_link_unregister(&veth_link_ops);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001395}
1396
1397module_init(veth_init);
1398module_exit(veth_exit);
1399
1400MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1401MODULE_LICENSE("GPL v2");
1402MODULE_ALIAS_RTNL_LINK(DRV_NAME);