blob: 569e87a51a3336cb630fe2d752546e867e62f318 [file] [log] [blame]
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001/*
2 * drivers/net/veth.c
3 *
4 * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
5 *
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
8 *
9 */
10
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070011#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070013#include <linux/ethtool.h>
14#include <linux/etherdevice.h>
Eric Dumazetcf05c702011-06-19 22:48:34 -070015#include <linux/u64_stats_sync.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070016
Jiri Pirkof7b12602014-02-18 20:53:18 +010017#include <net/rtnetlink.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070018#include <net/dst.h>
19#include <net/xfrm.h>
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +090020#include <net/xdp.h>
Stephen Hemmingerecef9692007-12-25 17:23:59 -080021#include <linux/veth.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040022#include <linux/module.h>
Toshiaki Makita948d4f22018-08-03 16:58:10 +090023#include <linux/bpf.h>
24#include <linux/filter.h>
25#include <linux/ptr_ring.h>
Toshiaki Makita948d4f22018-08-03 16:58:10 +090026#include <linux/bpf_trace.h>
Michael Walleaa4e6892018-08-29 17:24:11 +020027#include <linux/net_tstamp.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070028
29#define DRV_NAME "veth"
30#define DRV_VERSION "1.0"
31
Toshiaki Makita9fc8d512018-08-03 16:58:13 +090032#define VETH_XDP_FLAG BIT(0)
Toshiaki Makita948d4f22018-08-03 16:58:10 +090033#define VETH_RING_SIZE 256
34#define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
35
Toshiaki Makitad1396002018-08-03 16:58:17 +090036/* Separating two types of XDP xmit */
37#define VETH_XDP_TX BIT(0)
38#define VETH_XDP_REDIR BIT(1)
39
Toshiaki Makita4195e542018-10-11 18:36:49 +090040struct veth_rq_stats {
41 u64 xdp_packets;
42 u64 xdp_bytes;
43 u64 xdp_drops;
44 struct u64_stats_sync syncp;
45};
46
Toshiaki Makita638264d2018-08-03 16:58:18 +090047struct veth_rq {
Toshiaki Makita948d4f22018-08-03 16:58:10 +090048 struct napi_struct xdp_napi;
49 struct net_device *dev;
50 struct bpf_prog __rcu *xdp_prog;
Toshiaki Makitad1396002018-08-03 16:58:17 +090051 struct xdp_mem_info xdp_mem;
Toshiaki Makita4195e542018-10-11 18:36:49 +090052 struct veth_rq_stats stats;
Toshiaki Makita948d4f22018-08-03 16:58:10 +090053 bool rx_notify_masked;
54 struct ptr_ring xdp_ring;
55 struct xdp_rxq_info xdp_rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070056};
57
Toshiaki Makita638264d2018-08-03 16:58:18 +090058struct veth_priv {
59 struct net_device __rcu *peer;
60 atomic64_t dropped;
61 struct bpf_prog *_xdp_prog;
62 struct veth_rq *rq;
63 unsigned int requested_headroom;
64};
65
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070066/*
67 * ethtool interface
68 */
69
Toshiaki Makitad397b962018-10-11 18:36:50 +090070struct veth_q_stat_desc {
71 char desc[ETH_GSTRING_LEN];
72 size_t offset;
73};
74
75#define VETH_RQ_STAT(m) offsetof(struct veth_rq_stats, m)
76
77static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
78 { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
79 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
80 { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
81};
82
83#define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
84
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070085static struct {
86 const char string[ETH_GSTRING_LEN];
87} ethtool_stats_keys[] = {
88 { "peer_ifindex" },
89};
90
Philippe Reynes56607b92017-03-29 08:24:21 +020091static int veth_get_link_ksettings(struct net_device *dev,
92 struct ethtool_link_ksettings *cmd)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070093{
Philippe Reynes56607b92017-03-29 08:24:21 +020094 cmd->base.speed = SPEED_10000;
95 cmd->base.duplex = DUPLEX_FULL;
96 cmd->base.port = PORT_TP;
97 cmd->base.autoneg = AUTONEG_DISABLE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070098 return 0;
99}
100
101static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
102{
Rick Jones33a5ba12011-11-15 14:59:53 +0000103 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
104 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700105}
106
107static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
108{
Toshiaki Makitad397b962018-10-11 18:36:50 +0900109 char *p = (char *)buf;
110 int i, j;
111
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700112 switch(stringset) {
113 case ETH_SS_STATS:
Toshiaki Makitad397b962018-10-11 18:36:50 +0900114 memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
115 p += sizeof(ethtool_stats_keys);
116 for (i = 0; i < dev->real_num_rx_queues; i++) {
117 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
Florian Fainelliabdf47a2019-02-21 20:09:29 -0800118 snprintf(p, ETH_GSTRING_LEN,
119 "rx_queue_%u_%.11s",
Toshiaki Makitad397b962018-10-11 18:36:50 +0900120 i, veth_rq_stats_desc[j].desc);
121 p += ETH_GSTRING_LEN;
122 }
123 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700124 break;
125 }
126}
127
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700128static int veth_get_sset_count(struct net_device *dev, int sset)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700129{
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700130 switch (sset) {
131 case ETH_SS_STATS:
Toshiaki Makitad397b962018-10-11 18:36:50 +0900132 return ARRAY_SIZE(ethtool_stats_keys) +
133 VETH_RQ_STATS_LEN * dev->real_num_rx_queues;
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700134 default:
135 return -EOPNOTSUPP;
136 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700137}
138
139static void veth_get_ethtool_stats(struct net_device *dev,
140 struct ethtool_stats *stats, u64 *data)
141{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000142 struct veth_priv *priv = netdev_priv(dev);
143 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makitad397b962018-10-11 18:36:50 +0900144 int i, j, idx;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700145
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000146 data[0] = peer ? peer->ifindex : 0;
Toshiaki Makitad397b962018-10-11 18:36:50 +0900147 idx = 1;
148 for (i = 0; i < dev->real_num_rx_queues; i++) {
149 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
150 const void *stats_base = (void *)rq_stats;
151 unsigned int start;
152 size_t offset;
153
154 do {
155 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
156 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
157 offset = veth_rq_stats_desc[j].offset;
158 data[idx + j] = *(u64 *)(stats_base + offset);
159 }
160 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
161 idx += VETH_RQ_STATS_LEN;
162 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700163}
164
Michael Walleaa4e6892018-08-29 17:24:11 +0200165static int veth_get_ts_info(struct net_device *dev,
166 struct ethtool_ts_info *info)
167{
168 info->so_timestamping =
169 SOF_TIMESTAMPING_TX_SOFTWARE |
170 SOF_TIMESTAMPING_RX_SOFTWARE |
171 SOF_TIMESTAMPING_SOFTWARE;
172 info->phc_index = -1;
173
174 return 0;
175}
176
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700177static const struct ethtool_ops veth_ethtool_ops = {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700178 .get_drvinfo = veth_get_drvinfo,
179 .get_link = ethtool_op_get_link,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700180 .get_strings = veth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700181 .get_sset_count = veth_get_sset_count,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700182 .get_ethtool_stats = veth_get_ethtool_stats,
Philippe Reynes56607b92017-03-29 08:24:21 +0200183 .get_link_ksettings = veth_get_link_ksettings,
Michael Walleaa4e6892018-08-29 17:24:11 +0200184 .get_ts_info = veth_get_ts_info,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700185};
186
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900187/* general routines */
188
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900189static bool veth_is_xdp_frame(void *ptr)
190{
191 return (unsigned long)ptr & VETH_XDP_FLAG;
192}
193
194static void *veth_ptr_to_xdp(void *ptr)
195{
196 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
197}
198
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900199static void *veth_xdp_to_ptr(void *ptr)
200{
201 return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
202}
203
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900204static void veth_ptr_free(void *ptr)
205{
206 if (veth_is_xdp_frame(ptr))
207 xdp_return_frame(veth_ptr_to_xdp(ptr));
208 else
209 kfree_skb(ptr);
210}
211
Toshiaki Makita638264d2018-08-03 16:58:18 +0900212static void __veth_xdp_flush(struct veth_rq *rq)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900213{
214 /* Write ptr_ring before reading rx_notify_masked */
215 smp_mb();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900216 if (!rq->rx_notify_masked) {
217 rq->rx_notify_masked = true;
218 napi_schedule(&rq->xdp_napi);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900219 }
220}
221
Toshiaki Makita638264d2018-08-03 16:58:18 +0900222static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900223{
Toshiaki Makita638264d2018-08-03 16:58:18 +0900224 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900225 dev_kfree_skb_any(skb);
226 return NET_RX_DROP;
227 }
228
229 return NET_RX_SUCCESS;
230}
231
Toshiaki Makita638264d2018-08-03 16:58:18 +0900232static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
233 struct veth_rq *rq, bool xdp)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700234{
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900235 return __dev_forward_skb(dev, skb) ?: xdp ?
Toshiaki Makita638264d2018-08-03 16:58:18 +0900236 veth_xdp_rx(rq, skb) :
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900237 netif_rx(skb);
238}
239
240static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
241{
242 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900243 struct veth_rq *rq = NULL;
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000244 struct net_device *rcv;
Eric Dumazet26811282012-12-29 16:02:43 +0000245 int length = skb->len;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900246 bool rcv_xdp = false;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900247 int rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700248
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000249 rcu_read_lock();
250 rcv = rcu_dereference(priv->peer);
251 if (unlikely(!rcv)) {
252 kfree_skb(skb);
253 goto drop;
254 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700255
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900256 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900257 rxq = skb_get_queue_mapping(skb);
258 if (rxq < rcv->real_num_rx_queues) {
259 rq = &rcv_priv->rq[rxq];
260 rcv_xdp = rcu_access_pointer(rq->xdp_prog);
261 if (rcv_xdp)
262 skb_record_rx_queue(skb, rxq);
263 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900264
Michael Walleaa4e6892018-08-29 17:24:11 +0200265 skb_tx_timestamp(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900266 if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900267 if (!rcv_xdp) {
268 struct pcpu_lstats *stats = this_cpu_ptr(dev->lstats);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700269
Toshiaki Makita4195e542018-10-11 18:36:49 +0900270 u64_stats_update_begin(&stats->syncp);
271 stats->bytes += length;
272 stats->packets++;
273 u64_stats_update_end(&stats->syncp);
274 }
Eric Dumazet26811282012-12-29 16:02:43 +0000275 } else {
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000276drop:
Eric Dumazet26811282012-12-29 16:02:43 +0000277 atomic64_inc(&priv->dropped);
278 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900279
280 if (rcv_xdp)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900281 __veth_xdp_flush(rq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900282
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000283 rcu_read_unlock();
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900284
Patrick McHardy6ed10652009-06-23 06:03:08 +0000285 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700286}
287
Toshiaki Makita4195e542018-10-11 18:36:49 +0900288static u64 veth_stats_tx(struct pcpu_lstats *result, struct net_device *dev)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700289{
Eric Dumazetcf05c702011-06-19 22:48:34 -0700290 struct veth_priv *priv = netdev_priv(dev);
David S. Miller11687a12009-06-25 02:45:42 -0700291 int cpu;
David S. Miller11687a12009-06-25 02:45:42 -0700292
Eric Dumazet26811282012-12-29 16:02:43 +0000293 result->packets = 0;
294 result->bytes = 0;
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000295 for_each_possible_cpu(cpu) {
Li RongQing14d73412018-09-17 18:46:55 +0800296 struct pcpu_lstats *stats = per_cpu_ptr(dev->lstats, cpu);
Eric Dumazet26811282012-12-29 16:02:43 +0000297 u64 packets, bytes;
Eric Dumazetcf05c702011-06-19 22:48:34 -0700298 unsigned int start;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700299
Eric Dumazetcf05c702011-06-19 22:48:34 -0700300 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700301 start = u64_stats_fetch_begin_irq(&stats->syncp);
Eric Dumazet26811282012-12-29 16:02:43 +0000302 packets = stats->packets;
303 bytes = stats->bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700304 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
Eric Dumazet26811282012-12-29 16:02:43 +0000305 result->packets += packets;
306 result->bytes += bytes;
David S. Miller11687a12009-06-25 02:45:42 -0700307 }
Eric Dumazet26811282012-12-29 16:02:43 +0000308 return atomic64_read(&priv->dropped);
309}
310
Toshiaki Makita4195e542018-10-11 18:36:49 +0900311static void veth_stats_rx(struct veth_rq_stats *result, struct net_device *dev)
312{
313 struct veth_priv *priv = netdev_priv(dev);
314 int i;
315
316 result->xdp_packets = 0;
317 result->xdp_bytes = 0;
318 result->xdp_drops = 0;
319 for (i = 0; i < dev->num_rx_queues; i++) {
320 struct veth_rq_stats *stats = &priv->rq[i].stats;
321 u64 packets, bytes, drops;
322 unsigned int start;
323
324 do {
325 start = u64_stats_fetch_begin_irq(&stats->syncp);
326 packets = stats->xdp_packets;
327 bytes = stats->xdp_bytes;
328 drops = stats->xdp_drops;
329 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
330 result->xdp_packets += packets;
331 result->xdp_bytes += bytes;
332 result->xdp_drops += drops;
333 }
334}
335
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800336static void veth_get_stats64(struct net_device *dev,
337 struct rtnl_link_stats64 *tot)
Eric Dumazet26811282012-12-29 16:02:43 +0000338{
339 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000340 struct net_device *peer;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900341 struct veth_rq_stats rx;
342 struct pcpu_lstats tx;
Eric Dumazet26811282012-12-29 16:02:43 +0000343
Toshiaki Makita4195e542018-10-11 18:36:49 +0900344 tot->tx_dropped = veth_stats_tx(&tx, dev);
345 tot->tx_bytes = tx.bytes;
346 tot->tx_packets = tx.packets;
347
348 veth_stats_rx(&rx, dev);
349 tot->rx_dropped = rx.xdp_drops;
350 tot->rx_bytes = rx.xdp_bytes;
351 tot->rx_packets = rx.xdp_packets;
Eric Dumazet26811282012-12-29 16:02:43 +0000352
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000353 rcu_read_lock();
354 peer = rcu_dereference(priv->peer);
355 if (peer) {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900356 tot->rx_dropped += veth_stats_tx(&tx, peer);
357 tot->rx_bytes += tx.bytes;
358 tot->rx_packets += tx.packets;
359
360 veth_stats_rx(&rx, peer);
361 tot->tx_bytes += rx.xdp_bytes;
362 tot->tx_packets += rx.xdp_packets;
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000363 }
364 rcu_read_unlock();
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700365}
366
Gao feng5c70ef82013-10-04 16:52:24 +0800367/* fake multicast ability */
368static void veth_set_multicast_list(struct net_device *dev)
369{
370}
371
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900372static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
373 int buflen)
374{
375 struct sk_buff *skb;
376
377 if (!buflen) {
378 buflen = SKB_DATA_ALIGN(headroom + len) +
379 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
380 }
381 skb = build_skb(head, buflen);
382 if (!skb)
383 return NULL;
384
385 skb_reserve(skb, headroom);
386 skb_put(skb, len);
387
388 return skb;
389}
390
Toshiaki Makita638264d2018-08-03 16:58:18 +0900391static int veth_select_rxq(struct net_device *dev)
392{
393 return smp_processor_id() % dev->real_num_rx_queues;
394}
395
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900396static int veth_xdp_xmit(struct net_device *dev, int n,
397 struct xdp_frame **frames, u32 flags)
398{
399 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
400 struct net_device *rcv;
Toshiaki Makita21314792018-10-11 18:36:48 +0900401 int i, ret, drops = n;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900402 unsigned int max_len;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900403 struct veth_rq *rq;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900404
Toshiaki Makita21314792018-10-11 18:36:48 +0900405 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
406 ret = -EINVAL;
407 goto drop;
408 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900409
410 rcv = rcu_dereference(priv->peer);
Toshiaki Makita21314792018-10-11 18:36:48 +0900411 if (unlikely(!rcv)) {
412 ret = -ENXIO;
413 goto drop;
414 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900415
416 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900417 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900418 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
419 * side. This means an XDP program is loaded on the peer and the peer
420 * device is up.
421 */
Toshiaki Makita21314792018-10-11 18:36:48 +0900422 if (!rcu_access_pointer(rq->xdp_prog)) {
423 ret = -ENXIO;
424 goto drop;
425 }
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900426
Toshiaki Makita21314792018-10-11 18:36:48 +0900427 drops = 0;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900428 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
429
Toshiaki Makita638264d2018-08-03 16:58:18 +0900430 spin_lock(&rq->xdp_ring.producer_lock);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900431 for (i = 0; i < n; i++) {
432 struct xdp_frame *frame = frames[i];
433 void *ptr = veth_xdp_to_ptr(frame);
434
435 if (unlikely(frame->len > max_len ||
Toshiaki Makita638264d2018-08-03 16:58:18 +0900436 __ptr_ring_produce(&rq->xdp_ring, ptr))) {
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900437 xdp_return_frame_rx_napi(frame);
438 drops++;
439 }
440 }
Toshiaki Makita638264d2018-08-03 16:58:18 +0900441 spin_unlock(&rq->xdp_ring.producer_lock);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900442
443 if (flags & XDP_XMIT_FLUSH)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900444 __veth_xdp_flush(rq);
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900445
Toshiaki Makita21314792018-10-11 18:36:48 +0900446 if (likely(!drops))
447 return n;
448
449 ret = n - drops;
450drop:
451 atomic64_add(drops, &priv->dropped);
452
453 return ret;
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900454}
455
Toshiaki Makitad1396002018-08-03 16:58:17 +0900456static void veth_xdp_flush(struct net_device *dev)
457{
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();
463 rcv = rcu_dereference(priv->peer);
464 if (unlikely(!rcv))
465 goto out;
466
467 rcv_priv = netdev_priv(rcv);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900468 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
Toshiaki Makitad1396002018-08-03 16:58:17 +0900469 /* xdp_ring is initialized on receive side? */
Toshiaki Makita638264d2018-08-03 16:58:18 +0900470 if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
Toshiaki Makitad1396002018-08-03 16:58:17 +0900471 goto out;
472
Toshiaki Makita638264d2018-08-03 16:58:18 +0900473 __veth_xdp_flush(rq);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900474out:
475 rcu_read_unlock();
476}
477
478static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
479{
480 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
481
482 if (unlikely(!frame))
483 return -EOVERFLOW;
484
485 return veth_xdp_xmit(dev, 1, &frame, 0);
486}
487
Toshiaki Makita638264d2018-08-03 16:58:18 +0900488static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
Toshiaki Makitad1396002018-08-03 16:58:17 +0900489 struct xdp_frame *frame,
490 unsigned int *xdp_xmit)
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900491{
492 void *hard_start = frame->data - frame->headroom;
493 void *head = hard_start - sizeof(struct xdp_frame);
494 int len = frame->len, delta = 0;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900495 struct xdp_frame orig_frame;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900496 struct bpf_prog *xdp_prog;
497 unsigned int headroom;
498 struct sk_buff *skb;
499
500 rcu_read_lock();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900501 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900502 if (likely(xdp_prog)) {
503 struct xdp_buff xdp;
504 u32 act;
505
506 xdp.data_hard_start = hard_start;
507 xdp.data = frame->data;
508 xdp.data_end = frame->data + frame->len;
509 xdp.data_meta = frame->data - frame->metasize;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900510 xdp.rxq = &rq->xdp_rxq;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900511
512 act = bpf_prog_run_xdp(xdp_prog, &xdp);
513
514 switch (act) {
515 case XDP_PASS:
516 delta = frame->data - xdp.data;
517 len = xdp.data_end - xdp.data;
518 break;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900519 case XDP_TX:
520 orig_frame = *frame;
521 xdp.data_hard_start = head;
522 xdp.rxq->mem = frame->mem;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900523 if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
524 trace_xdp_exception(rq->dev, xdp_prog, act);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900525 frame = &orig_frame;
526 goto err_xdp;
527 }
528 *xdp_xmit |= VETH_XDP_TX;
529 rcu_read_unlock();
530 goto xdp_xmit;
531 case XDP_REDIRECT:
532 orig_frame = *frame;
533 xdp.data_hard_start = head;
534 xdp.rxq->mem = frame->mem;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900535 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
Toshiaki Makitad1396002018-08-03 16:58:17 +0900536 frame = &orig_frame;
537 goto err_xdp;
538 }
539 *xdp_xmit |= VETH_XDP_REDIR;
540 rcu_read_unlock();
541 goto xdp_xmit;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900542 default:
543 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600544 /* fall through */
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900545 case XDP_ABORTED:
Toshiaki Makita638264d2018-08-03 16:58:18 +0900546 trace_xdp_exception(rq->dev, xdp_prog, act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600547 /* fall through */
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900548 case XDP_DROP:
549 goto err_xdp;
550 }
551 }
552 rcu_read_unlock();
553
554 headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
555 skb = veth_build_skb(head, headroom, len, 0);
556 if (!skb) {
557 xdp_return_frame(frame);
558 goto err;
559 }
560
561 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 Makitad1396002018-08-03 16:58:17 +0900573 unsigned int *xdp_xmit)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900574{
575 u32 pktlen, headroom, act, metalen;
576 void *orig_data, *orig_data_end;
577 struct bpf_prog *xdp_prog;
578 int mac_len, delta, off;
579 struct xdp_buff xdp;
580
Toshiaki Makita4bf9ffa2018-09-14 13:33:44 +0900581 skb_orphan(skb);
582
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900583 rcu_read_lock();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900584 xdp_prog = rcu_dereference(rq->xdp_prog);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900585 if (unlikely(!xdp_prog)) {
586 rcu_read_unlock();
587 goto out;
588 }
589
590 mac_len = skb->data - skb_mac_header(skb);
591 pktlen = skb->len + mac_len;
592 headroom = skb_headroom(skb) - mac_len;
593
594 if (skb_shared(skb) || skb_head_is_locked(skb) ||
595 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
596 struct sk_buff *nskb;
597 int size, head_off;
598 void *head, *start;
599 struct page *page;
600
601 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
602 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
603 if (size > PAGE_SIZE)
604 goto drop;
605
606 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
607 if (!page)
608 goto drop;
609
610 head = page_address(page);
611 start = head + VETH_XDP_HEADROOM;
612 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
613 page_frag_free(head);
614 goto drop;
615 }
616
617 nskb = veth_build_skb(head,
618 VETH_XDP_HEADROOM + mac_len, skb->len,
619 PAGE_SIZE);
620 if (!nskb) {
621 page_frag_free(head);
622 goto drop;
623 }
624
625 skb_copy_header(nskb, skb);
626 head_off = skb_headroom(nskb) - skb_headroom(skb);
627 skb_headers_offset_update(nskb, head_off);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900628 consume_skb(skb);
629 skb = nskb;
630 }
631
632 xdp.data_hard_start = skb->head;
633 xdp.data = skb_mac_header(skb);
634 xdp.data_end = xdp.data + pktlen;
635 xdp.data_meta = xdp.data;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900636 xdp.rxq = &rq->xdp_rxq;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900637 orig_data = xdp.data;
638 orig_data_end = xdp.data_end;
639
640 act = bpf_prog_run_xdp(xdp_prog, &xdp);
641
642 switch (act) {
643 case XDP_PASS:
644 break;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900645 case XDP_TX:
646 get_page(virt_to_page(xdp.data));
647 consume_skb(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900648 xdp.rxq->mem = rq->xdp_mem;
649 if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
650 trace_xdp_exception(rq->dev, xdp_prog, act);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900651 goto err_xdp;
652 }
653 *xdp_xmit |= VETH_XDP_TX;
654 rcu_read_unlock();
655 goto xdp_xmit;
656 case XDP_REDIRECT:
657 get_page(virt_to_page(xdp.data));
658 consume_skb(skb);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900659 xdp.rxq->mem = rq->xdp_mem;
660 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog))
Toshiaki Makitad1396002018-08-03 16:58:17 +0900661 goto err_xdp;
662 *xdp_xmit |= VETH_XDP_REDIR;
663 rcu_read_unlock();
664 goto xdp_xmit;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900665 default:
666 bpf_warn_invalid_xdp_action(act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600667 /* fall through */
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900668 case XDP_ABORTED:
Toshiaki Makita638264d2018-08-03 16:58:18 +0900669 trace_xdp_exception(rq->dev, xdp_prog, act);
Gustavo A. R. Silvaa9b6d9e2019-02-08 12:37:33 -0600670 /* fall through */
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900671 case XDP_DROP:
672 goto drop;
673 }
674 rcu_read_unlock();
675
676 delta = orig_data - xdp.data;
677 off = mac_len + delta;
678 if (off > 0)
679 __skb_push(skb, off);
680 else if (off < 0)
681 __skb_pull(skb, -off);
682 skb->mac_header -= delta;
683 off = xdp.data_end - orig_data_end;
684 if (off != 0)
685 __skb_put(skb, off);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900686 skb->protocol = eth_type_trans(skb, rq->dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900687
688 metalen = xdp.data - xdp.data_meta;
689 if (metalen)
690 skb_metadata_set(skb, metalen);
691out:
692 return skb;
693drop:
694 rcu_read_unlock();
695 kfree_skb(skb);
696 return NULL;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900697err_xdp:
698 rcu_read_unlock();
699 page_frag_free(xdp.data);
700xdp_xmit:
701 return NULL;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900702}
703
Toshiaki Makita638264d2018-08-03 16:58:18 +0900704static int veth_xdp_rcv(struct veth_rq *rq, int budget, unsigned int *xdp_xmit)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900705{
Toshiaki Makita4195e542018-10-11 18:36:49 +0900706 int i, done = 0, drops = 0, bytes = 0;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900707
708 for (i = 0; i < budget; i++) {
Toshiaki Makita638264d2018-08-03 16:58:18 +0900709 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
Toshiaki Makita4195e542018-10-11 18:36:49 +0900710 unsigned int xdp_xmit_one = 0;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900711 struct sk_buff *skb;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900712
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900713 if (!ptr)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900714 break;
715
Toshiaki Makitad1396002018-08-03 16:58:17 +0900716 if (veth_is_xdp_frame(ptr)) {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900717 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
718
719 bytes += frame->len;
720 skb = veth_xdp_rcv_one(rq, frame, &xdp_xmit_one);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900721 } else {
Toshiaki Makita4195e542018-10-11 18:36:49 +0900722 skb = ptr;
723 bytes += skb->len;
724 skb = veth_xdp_rcv_skb(rq, skb, &xdp_xmit_one);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900725 }
Toshiaki Makita4195e542018-10-11 18:36:49 +0900726 *xdp_xmit |= xdp_xmit_one;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900727
728 if (skb)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900729 napi_gro_receive(&rq->xdp_napi, skb);
Toshiaki Makita4195e542018-10-11 18:36:49 +0900730 else if (!xdp_xmit_one)
731 drops++;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900732
733 done++;
734 }
735
Toshiaki Makita4195e542018-10-11 18:36:49 +0900736 u64_stats_update_begin(&rq->stats.syncp);
737 rq->stats.xdp_packets += done;
738 rq->stats.xdp_bytes += bytes;
739 rq->stats.xdp_drops += drops;
740 u64_stats_update_end(&rq->stats.syncp);
741
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900742 return done;
743}
744
745static int veth_poll(struct napi_struct *napi, int budget)
746{
Toshiaki Makita638264d2018-08-03 16:58:18 +0900747 struct veth_rq *rq =
748 container_of(napi, struct veth_rq, xdp_napi);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900749 unsigned int xdp_xmit = 0;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900750 int done;
751
Toshiaki Makitad1396002018-08-03 16:58:17 +0900752 xdp_set_return_frame_no_direct();
Toshiaki Makita638264d2018-08-03 16:58:18 +0900753 done = veth_xdp_rcv(rq, budget, &xdp_xmit);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900754
755 if (done < budget && napi_complete_done(napi, done)) {
756 /* Write rx_notify_masked before reading ptr_ring */
Toshiaki Makita638264d2018-08-03 16:58:18 +0900757 smp_store_mb(rq->rx_notify_masked, false);
758 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
759 rq->rx_notify_masked = true;
760 napi_schedule(&rq->xdp_napi);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900761 }
762 }
763
Toshiaki Makitad1396002018-08-03 16:58:17 +0900764 if (xdp_xmit & VETH_XDP_TX)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900765 veth_xdp_flush(rq->dev);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900766 if (xdp_xmit & VETH_XDP_REDIR)
767 xdp_do_flush_map();
768 xdp_clear_return_frame_no_direct();
769
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900770 return done;
771}
772
773static int veth_napi_add(struct net_device *dev)
774{
775 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900776 int err, i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900777
Toshiaki Makita638264d2018-08-03 16:58:18 +0900778 for (i = 0; i < dev->real_num_rx_queues; i++) {
779 struct veth_rq *rq = &priv->rq[i];
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900780
Toshiaki Makita638264d2018-08-03 16:58:18 +0900781 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
782 if (err)
783 goto err_xdp_ring;
784 }
785
786 for (i = 0; i < dev->real_num_rx_queues; i++) {
787 struct veth_rq *rq = &priv->rq[i];
788
789 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
790 napi_enable(&rq->xdp_napi);
791 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900792
793 return 0;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900794err_xdp_ring:
795 for (i--; i >= 0; i--)
796 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
797
798 return err;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900799}
800
801static void veth_napi_del(struct net_device *dev)
802{
803 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900804 int i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900805
Toshiaki Makita638264d2018-08-03 16:58:18 +0900806 for (i = 0; i < dev->real_num_rx_queues; i++) {
807 struct veth_rq *rq = &priv->rq[i];
808
809 napi_disable(&rq->xdp_napi);
810 napi_hash_del(&rq->xdp_napi);
811 }
812 synchronize_net();
813
814 for (i = 0; i < dev->real_num_rx_queues; i++) {
815 struct veth_rq *rq = &priv->rq[i];
816
817 netif_napi_del(&rq->xdp_napi);
818 rq->rx_notify_masked = false;
819 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
820 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900821}
822
823static int veth_enable_xdp(struct net_device *dev)
824{
825 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900826 int err, i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900827
Toshiaki Makita638264d2018-08-03 16:58:18 +0900828 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
829 for (i = 0; i < dev->real_num_rx_queues; i++) {
830 struct veth_rq *rq = &priv->rq[i];
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900831
Toshiaki Makita638264d2018-08-03 16:58:18 +0900832 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i);
833 if (err < 0)
834 goto err_rxq_reg;
835
836 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
837 MEM_TYPE_PAGE_SHARED,
838 NULL);
839 if (err < 0)
840 goto err_reg_mem;
841
842 /* Save original mem info as it can be overwritten */
843 rq->xdp_mem = rq->xdp_rxq.mem;
844 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900845
846 err = veth_napi_add(dev);
847 if (err)
Toshiaki Makita638264d2018-08-03 16:58:18 +0900848 goto err_rxq_reg;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900849 }
850
Toshiaki Makita638264d2018-08-03 16:58:18 +0900851 for (i = 0; i < dev->real_num_rx_queues; i++)
852 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900853
854 return 0;
Toshiaki Makita638264d2018-08-03 16:58:18 +0900855err_reg_mem:
856 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
857err_rxq_reg:
858 for (i--; i >= 0; i--)
859 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900860
861 return err;
862}
863
864static void veth_disable_xdp(struct net_device *dev)
865{
866 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900867 int i;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900868
Toshiaki Makita638264d2018-08-03 16:58:18 +0900869 for (i = 0; i < dev->real_num_rx_queues; i++)
870 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900871 veth_napi_del(dev);
Toshiaki Makita638264d2018-08-03 16:58:18 +0900872 for (i = 0; i < dev->real_num_rx_queues; i++) {
873 struct veth_rq *rq = &priv->rq[i];
874
875 rq->xdp_rxq.mem = rq->xdp_mem;
876 xdp_rxq_info_unreg(&rq->xdp_rxq);
877 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900878}
879
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700880static int veth_open(struct net_device *dev)
881{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000882 struct veth_priv *priv = netdev_priv(dev);
883 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900884 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700885
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000886 if (!peer)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700887 return -ENOTCONN;
888
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900889 if (priv->_xdp_prog) {
890 err = veth_enable_xdp(dev);
891 if (err)
892 return err;
893 }
894
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000895 if (peer->flags & IFF_UP) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700896 netif_carrier_on(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000897 netif_carrier_on(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700898 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900899
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700900 return 0;
901}
902
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000903static int veth_close(struct net_device *dev)
904{
905 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000906 struct net_device *peer = rtnl_dereference(priv->peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000907
908 netif_carrier_off(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000909 if (peer)
910 netif_carrier_off(peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000911
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900912 if (priv->_xdp_prog)
913 veth_disable_xdp(dev);
914
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000915 return 0;
916}
917
Jarod Wilson91572082016-10-20 13:55:20 -0400918static int is_valid_veth_mtu(int mtu)
Eric Biederman38d40812009-03-03 23:36:04 -0800919{
Jarod Wilson91572082016-10-20 13:55:20 -0400920 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
Eric Biederman38d40812009-03-03 23:36:04 -0800921}
922
Toshiaki Makita7797b932018-08-15 17:07:29 +0900923static int veth_alloc_queues(struct net_device *dev)
924{
925 struct veth_priv *priv = netdev_priv(dev);
926 int i;
927
928 priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
929 if (!priv->rq)
930 return -ENOMEM;
931
Toshiaki Makita4195e542018-10-11 18:36:49 +0900932 for (i = 0; i < dev->num_rx_queues; i++) {
Toshiaki Makita7797b932018-08-15 17:07:29 +0900933 priv->rq[i].dev = dev;
Toshiaki Makita4195e542018-10-11 18:36:49 +0900934 u64_stats_init(&priv->rq[i].stats.syncp);
935 }
Toshiaki Makita7797b932018-08-15 17:07:29 +0900936
937 return 0;
938}
939
940static void veth_free_queues(struct net_device *dev)
941{
942 struct veth_priv *priv = netdev_priv(dev);
943
944 kfree(priv->rq);
945}
946
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700947static int veth_dev_init(struct net_device *dev)
948{
Toshiaki Makita7797b932018-08-15 17:07:29 +0900949 int err;
950
Li RongQing14d73412018-09-17 18:46:55 +0800951 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
952 if (!dev->lstats)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700953 return -ENOMEM;
Toshiaki Makita7797b932018-08-15 17:07:29 +0900954
955 err = veth_alloc_queues(dev);
956 if (err) {
Li RongQing14d73412018-09-17 18:46:55 +0800957 free_percpu(dev->lstats);
Toshiaki Makita7797b932018-08-15 17:07:29 +0900958 return err;
959 }
960
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700961 return 0;
962}
963
David S. Miller11687a12009-06-25 02:45:42 -0700964static void veth_dev_free(struct net_device *dev)
965{
Toshiaki Makita7797b932018-08-15 17:07:29 +0900966 veth_free_queues(dev);
Li RongQing14d73412018-09-17 18:46:55 +0800967 free_percpu(dev->lstats);
David S. Miller11687a12009-06-25 02:45:42 -0700968}
969
WANG Congbb446c12014-06-23 15:36:02 -0700970#ifdef CONFIG_NET_POLL_CONTROLLER
971static void veth_poll_controller(struct net_device *dev)
972{
973 /* veth only receives frames when its peer sends one
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900974 * Since it has nothing to do with disabling irqs, we are guaranteed
WANG Congbb446c12014-06-23 15:36:02 -0700975 * never to have pending data when we poll for it so
976 * there is nothing to do here.
977 *
978 * We need this though so netpoll recognizes us as an interface that
979 * supports polling, which enables bridge devices in virt setups to
980 * still use netconsole
981 */
982}
983#endif /* CONFIG_NET_POLL_CONTROLLER */
984
Nicolas Dichtela45253b2015-04-02 17:07:11 +0200985static int veth_get_iflink(const struct net_device *dev)
986{
987 struct veth_priv *priv = netdev_priv(dev);
988 struct net_device *peer;
989 int iflink;
990
991 rcu_read_lock();
992 peer = rcu_dereference(priv->peer);
993 iflink = peer ? peer->ifindex : 0;
994 rcu_read_unlock();
995
996 return iflink;
997}
998
Toshiaki Makitadc224822018-08-03 16:58:11 +0900999static netdev_features_t veth_fix_features(struct net_device *dev,
1000 netdev_features_t features)
1001{
1002 struct veth_priv *priv = netdev_priv(dev);
1003 struct net_device *peer;
1004
1005 peer = rtnl_dereference(priv->peer);
1006 if (peer) {
1007 struct veth_priv *peer_priv = netdev_priv(peer);
1008
1009 if (peer_priv->_xdp_prog)
1010 features &= ~NETIF_F_GSO_SOFTWARE;
1011 }
1012
1013 return features;
1014}
1015
Paolo Abeni163e5292016-02-26 10:45:41 +01001016static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1017{
1018 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1019 struct net_device *peer;
1020
1021 if (new_hr < 0)
1022 new_hr = 0;
1023
1024 rcu_read_lock();
1025 peer = rcu_dereference(priv->peer);
1026 if (unlikely(!peer))
1027 goto out;
1028
1029 peer_priv = netdev_priv(peer);
1030 priv->requested_headroom = new_hr;
1031 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1032 dev->needed_headroom = new_hr;
1033 peer->needed_headroom = new_hr;
1034
1035out:
1036 rcu_read_unlock();
1037}
1038
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001039static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1040 struct netlink_ext_ack *extack)
1041{
1042 struct veth_priv *priv = netdev_priv(dev);
1043 struct bpf_prog *old_prog;
1044 struct net_device *peer;
Toshiaki Makitadc224822018-08-03 16:58:11 +09001045 unsigned int max_mtu;
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001046 int err;
1047
1048 old_prog = priv->_xdp_prog;
1049 priv->_xdp_prog = prog;
1050 peer = rtnl_dereference(priv->peer);
1051
1052 if (prog) {
1053 if (!peer) {
1054 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1055 err = -ENOTCONN;
1056 goto err;
1057 }
1058
Toshiaki Makitadc224822018-08-03 16:58:11 +09001059 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1060 peer->hard_header_len -
1061 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1062 if (peer->mtu > max_mtu) {
1063 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1064 err = -ERANGE;
1065 goto err;
1066 }
1067
Toshiaki Makita638264d2018-08-03 16:58:18 +09001068 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1069 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1070 err = -ENOSPC;
1071 goto err;
1072 }
1073
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001074 if (dev->flags & IFF_UP) {
1075 err = veth_enable_xdp(dev);
1076 if (err) {
1077 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1078 goto err;
1079 }
1080 }
Toshiaki Makitadc224822018-08-03 16:58:11 +09001081
1082 if (!old_prog) {
1083 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1084 peer->max_mtu = max_mtu;
1085 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001086 }
1087
1088 if (old_prog) {
Toshiaki Makitadc224822018-08-03 16:58:11 +09001089 if (!prog) {
1090 if (dev->flags & IFF_UP)
1091 veth_disable_xdp(dev);
1092
1093 if (peer) {
1094 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1095 peer->max_mtu = ETH_MAX_MTU;
1096 }
1097 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001098 bpf_prog_put(old_prog);
1099 }
1100
Toshiaki Makitadc224822018-08-03 16:58:11 +09001101 if ((!!old_prog ^ !!prog) && peer)
1102 netdev_update_features(peer);
1103
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001104 return 0;
1105err:
1106 priv->_xdp_prog = old_prog;
1107
1108 return err;
1109}
1110
1111static u32 veth_xdp_query(struct net_device *dev)
1112{
1113 struct veth_priv *priv = netdev_priv(dev);
1114 const struct bpf_prog *xdp_prog;
1115
1116 xdp_prog = priv->_xdp_prog;
1117 if (xdp_prog)
1118 return xdp_prog->aux->id;
1119
1120 return 0;
1121}
1122
1123static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1124{
1125 switch (xdp->command) {
1126 case XDP_SETUP_PROG:
1127 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1128 case XDP_QUERY_PROG:
1129 xdp->prog_id = veth_xdp_query(dev);
1130 return 0;
1131 default:
1132 return -EINVAL;
1133 }
1134}
1135
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001136static const struct net_device_ops veth_netdev_ops = {
Daniel Lezcanoee923622009-02-22 00:04:45 -08001137 .ndo_init = veth_dev_init,
1138 .ndo_open = veth_open,
Eric W. Biederman2cf48a12009-02-25 19:47:29 +00001139 .ndo_stop = veth_close,
Daniel Lezcanoee923622009-02-22 00:04:45 -08001140 .ndo_start_xmit = veth_xmit,
stephen hemminger6311cc42011-06-08 14:53:59 +00001141 .ndo_get_stats64 = veth_get_stats64,
Gao feng5c70ef82013-10-04 16:52:24 +08001142 .ndo_set_rx_mode = veth_set_multicast_list,
Daniel Lezcanoee923622009-02-22 00:04:45 -08001143 .ndo_set_mac_address = eth_mac_addr,
WANG Congbb446c12014-06-23 15:36:02 -07001144#ifdef CONFIG_NET_POLL_CONTROLLER
1145 .ndo_poll_controller = veth_poll_controller,
1146#endif
Nicolas Dichtela45253b2015-04-02 17:07:11 +02001147 .ndo_get_iflink = veth_get_iflink,
Toshiaki Makitadc224822018-08-03 16:58:11 +09001148 .ndo_fix_features = veth_fix_features,
Toshiaki Makita1a04a822015-07-31 15:03:25 +09001149 .ndo_features_check = passthru_features_check,
Paolo Abeni163e5292016-02-26 10:45:41 +01001150 .ndo_set_rx_headroom = veth_set_rx_headroom,
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001151 .ndo_bpf = veth_xdp,
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +09001152 .ndo_xdp_xmit = veth_xdp_xmit,
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001153};
1154
Alexander Duyck732912d72016-04-19 14:02:26 -04001155#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
Xin Longc80fafb2016-08-25 13:21:49 +08001156 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
Alexander Duyck732912d72016-04-19 14:02:26 -04001157 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
Patrick McHardy28d2b132013-04-19 02:04:32 +00001158 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1159 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
Eric Dumazet80933152012-12-29 16:26:10 +00001160
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001161static void veth_setup(struct net_device *dev)
1162{
1163 ether_setup(dev);
1164
Neil Horman550fd082011-07-26 06:05:38 +00001165 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Hannes Frederic Sowa23ea5a92012-10-30 16:22:01 +00001166 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Phil Sutter02f01ec2015-08-18 10:30:29 +02001167 dev->priv_flags |= IFF_NO_QUEUE;
Paolo Abeni163e5292016-02-26 10:45:41 +01001168 dev->priv_flags |= IFF_PHONY_HEADROOM;
Neil Horman550fd082011-07-26 06:05:38 +00001169
Stephen Hemminger4456e7b2008-11-19 21:50:10 -08001170 dev->netdev_ops = &veth_netdev_ops;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001171 dev->ethtool_ops = &veth_ethtool_ops;
1172 dev->features |= NETIF_F_LLTX;
Eric Dumazet80933152012-12-29 16:26:10 +00001173 dev->features |= VETH_FEATURES;
Toshiaki Makita8d0d21f2014-02-18 21:20:08 +09001174 dev->vlan_features = dev->features &
Vlad Yasevich3f8c7072014-03-27 22:14:48 -04001175 ~(NETIF_F_HW_VLAN_CTAG_TX |
1176 NETIF_F_HW_VLAN_STAG_TX |
1177 NETIF_F_HW_VLAN_CTAG_RX |
1178 NETIF_F_HW_VLAN_STAG_RX);
David S. Millercf124db2017-05-08 12:52:56 -04001179 dev->needs_free_netdev = true;
1180 dev->priv_destructor = veth_dev_free;
Jarod Wilson91572082016-10-20 13:55:20 -04001181 dev->max_mtu = ETH_MAX_MTU;
Michał Mirosława2c725f2011-03-31 01:01:35 +00001182
Eric Dumazet80933152012-12-29 16:26:10 +00001183 dev->hw_features = VETH_FEATURES;
Eric Dumazet82d81892013-10-25 18:25:03 -07001184 dev->hw_enc_features = VETH_FEATURES;
David Ahern607fca92016-08-24 20:10:45 -07001185 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001186}
1187
1188/*
1189 * netlink interface
1190 */
1191
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001192static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1193 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001194{
1195 if (tb[IFLA_ADDRESS]) {
1196 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1197 return -EINVAL;
1198 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1199 return -EADDRNOTAVAIL;
1200 }
Eric Biederman38d40812009-03-03 23:36:04 -08001201 if (tb[IFLA_MTU]) {
1202 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1203 return -EINVAL;
1204 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001205 return 0;
1206}
1207
1208static struct rtnl_link_ops veth_link_ops;
1209
Eric W. Biederman81adee42009-11-08 00:53:51 -08001210static int veth_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001211 struct nlattr *tb[], struct nlattr *data[],
1212 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001213{
Toshiaki Makita7797b932018-08-15 17:07:29 +09001214 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001215 struct net_device *peer;
1216 struct veth_priv *priv;
1217 char ifname[IFNAMSIZ];
1218 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
Tom Gundersen55177502014-07-14 16:37:25 +02001219 unsigned char name_assign_type;
Patrick McHardy3729d502010-02-26 06:34:54 +00001220 struct ifinfomsg *ifmp;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001221 struct net *net;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001222
1223 /*
1224 * create and register peer first
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001225 */
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001226 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1227 struct nlattr *nla_peer;
1228
1229 nla_peer = data[VETH_INFO_PEER];
Patrick McHardy3729d502010-02-26 06:34:54 +00001230 ifmp = nla_data(nla_peer);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001231 err = rtnl_nla_parse_ifla(peer_tb,
1232 nla_data(nla_peer) + sizeof(struct ifinfomsg),
Johannes Bergfceb6432017-04-12 14:34:07 +02001233 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1234 NULL);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001235 if (err < 0)
1236 return err;
1237
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001238 err = veth_validate(peer_tb, NULL, extack);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001239 if (err < 0)
1240 return err;
1241
1242 tbp = peer_tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001243 } else {
1244 ifmp = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001245 tbp = tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001246 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001247
Serhey Popovych191cdb32017-06-21 12:12:24 +03001248 if (ifmp && tbp[IFLA_IFNAME]) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001249 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
Tom Gundersen55177502014-07-14 16:37:25 +02001250 name_assign_type = NET_NAME_USER;
1251 } else {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001252 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
Tom Gundersen55177502014-07-14 16:37:25 +02001253 name_assign_type = NET_NAME_ENUM;
1254 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001255
Eric W. Biederman81adee42009-11-08 00:53:51 -08001256 net = rtnl_link_get_net(src_net, tbp);
1257 if (IS_ERR(net))
1258 return PTR_ERR(net);
1259
Tom Gundersen55177502014-07-14 16:37:25 +02001260 peer = rtnl_create_link(net, ifname, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001261 &veth_link_ops, tbp, extack);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001262 if (IS_ERR(peer)) {
1263 put_net(net);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001264 return PTR_ERR(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001265 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001266
Serhey Popovych191cdb32017-06-21 12:12:24 +03001267 if (!ifmp || !tbp[IFLA_ADDRESS])
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001268 eth_hw_addr_random(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001269
Pavel Emelyanove6f8f1a2012-08-08 21:53:03 +00001270 if (ifmp && (dev->ifindex != 0))
1271 peer->ifindex = ifmp->ifi_index;
1272
Stephen Hemminger72d249552017-12-07 15:40:20 -08001273 peer->gso_max_size = dev->gso_max_size;
1274 peer->gso_max_segs = dev->gso_max_segs;
1275
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001276 err = register_netdevice(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001277 put_net(net);
1278 net = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001279 if (err < 0)
1280 goto err_register_peer;
1281
1282 netif_carrier_off(peer);
1283
Patrick McHardy3729d502010-02-26 06:34:54 +00001284 err = rtnl_configure_link(peer, ifmp);
1285 if (err < 0)
1286 goto err_configure_peer;
1287
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001288 /*
1289 * register dev last
1290 *
1291 * note, that since we've registered new device the dev's name
1292 * should be re-allocated
1293 */
1294
1295 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001296 eth_hw_addr_random(dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001297
Jiri Pirko6c8c4442011-04-30 01:28:17 +00001298 if (tb[IFLA_IFNAME])
1299 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1300 else
1301 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1302
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001303 err = register_netdevice(dev);
1304 if (err < 0)
1305 goto err_register_dev;
1306
1307 netif_carrier_off(dev);
1308
1309 /*
1310 * tie the deviced together
1311 */
1312
1313 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001314 rcu_assign_pointer(priv->peer, peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001315
1316 priv = netdev_priv(peer);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001317 rcu_assign_pointer(priv->peer, dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001318
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001319 return 0;
1320
1321err_register_dev:
1322 /* nothing to do */
Patrick McHardy3729d502010-02-26 06:34:54 +00001323err_configure_peer:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001324 unregister_netdevice(peer);
1325 return err;
1326
1327err_register_peer:
1328 free_netdev(peer);
1329 return err;
1330}
1331
Eric Dumazet23289a32009-10-27 07:06:36 +00001332static void veth_dellink(struct net_device *dev, struct list_head *head)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001333{
1334 struct veth_priv *priv;
1335 struct net_device *peer;
1336
1337 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001338 peer = rtnl_dereference(priv->peer);
1339
1340 /* Note : dellink() is called from default_device_exit_batch(),
1341 * before a rcu_synchronize() point. The devices are guaranteed
1342 * not being freed before one RCU grace period.
1343 */
1344 RCU_INIT_POINTER(priv->peer, NULL);
Eric Dumazet24540532009-10-30 01:00:27 -07001345 unregister_netdevice_queue(dev, head);
Eric Dumazetf45a5c22013-02-08 20:10:49 +00001346
1347 if (peer) {
1348 priv = netdev_priv(peer);
1349 RCU_INIT_POINTER(priv->peer, NULL);
1350 unregister_netdevice_queue(peer, head);
1351 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001352}
1353
Thomas Graf23711432012-02-15 04:09:46 +00001354static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1355 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1356};
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001357
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001358static struct net *veth_get_link_net(const struct net_device *dev)
1359{
1360 struct veth_priv *priv = netdev_priv(dev);
1361 struct net_device *peer = rtnl_dereference(priv->peer);
1362
1363 return peer ? dev_net(peer) : dev_net(dev);
1364}
1365
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001366static struct rtnl_link_ops veth_link_ops = {
1367 .kind = DRV_NAME,
1368 .priv_size = sizeof(struct veth_priv),
1369 .setup = veth_setup,
1370 .validate = veth_validate,
1371 .newlink = veth_newlink,
1372 .dellink = veth_dellink,
1373 .policy = veth_policy,
1374 .maxtype = VETH_INFO_MAX,
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001375 .get_link_net = veth_get_link_net,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001376};
1377
1378/*
1379 * init/fini
1380 */
1381
1382static __init int veth_init(void)
1383{
1384 return rtnl_link_register(&veth_link_ops);
1385}
1386
1387static __exit void veth_exit(void)
1388{
Patrick McHardy68365452008-01-20 17:25:14 -08001389 rtnl_link_unregister(&veth_link_ops);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001390}
1391
1392module_init(veth_init);
1393module_exit(veth_exit);
1394
1395MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1396MODULE_LICENSE("GPL v2");
1397MODULE_ALIAS_RTNL_LINK(DRV_NAME);