net: broadcom: bcm4908enet: add BCM4908 controller driver
[linux-2.6-block.git] / net / core / dev.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
722c9a0c 3 * NET3 Protocol independent device support routines.
1da177e4 4 *
1da177e4 5 * Derived from the non IP parts of dev.c 1.0.19
722c9a0c 6 * Authors: Ross Biro
1da177e4
LT
7 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8 * Mark Evans, <evansmp@uhura.aston.ac.uk>
9 *
10 * Additional Authors:
11 * Florian la Roche <rzsfl@rz.uni-sb.de>
12 * Alan Cox <gw4pts@gw4pts.ampr.org>
13 * David Hinds <dahinds@users.sourceforge.net>
14 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
15 * Adam Sulmicki <adam@cfar.umd.edu>
16 * Pekka Riikonen <priikone@poesidon.pspt.fi>
17 *
18 * Changes:
19 * D.J. Barrow : Fixed bug where dev->refcnt gets set
722c9a0c 20 * to 2 if register_netdev gets called
21 * before net_dev_init & also removed a
22 * few lines of code in the process.
1da177e4
LT
23 * Alan Cox : device private ioctl copies fields back.
24 * Alan Cox : Transmit queue code does relevant
25 * stunts to keep the queue safe.
26 * Alan Cox : Fixed double lock.
27 * Alan Cox : Fixed promisc NULL pointer trap
28 * ???????? : Support the full private ioctl range
29 * Alan Cox : Moved ioctl permission check into
30 * drivers
31 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
32 * Alan Cox : 100 backlog just doesn't cut it when
33 * you start doing multicast video 8)
34 * Alan Cox : Rewrote net_bh and list manager.
722c9a0c 35 * Alan Cox : Fix ETH_P_ALL echoback lengths.
1da177e4
LT
36 * Alan Cox : Took out transmit every packet pass
37 * Saved a few bytes in the ioctl handler
38 * Alan Cox : Network driver sets packet type before
39 * calling netif_rx. Saves a function
40 * call a packet.
41 * Alan Cox : Hashed net_bh()
42 * Richard Kooijman: Timestamp fixes.
43 * Alan Cox : Wrong field in SIOCGIFDSTADDR
44 * Alan Cox : Device lock protection.
722c9a0c 45 * Alan Cox : Fixed nasty side effect of device close
1da177e4
LT
46 * changes.
47 * Rudi Cilibrasi : Pass the right thing to
48 * set_mac_address()
49 * Dave Miller : 32bit quantity for the device lock to
50 * make it work out on a Sparc.
51 * Bjorn Ekwall : Added KERNELD hack.
52 * Alan Cox : Cleaned up the backlog initialise.
53 * Craig Metz : SIOCGIFCONF fix if space for under
54 * 1 device.
55 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
56 * is no device open function.
57 * Andi Kleen : Fix error reporting for SIOCGIFCONF
58 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
59 * Cyrus Durgin : Cleaned for KMOD
60 * Adam Sulmicki : Bug Fix : Network Device Unload
61 * A network device unload needs to purge
62 * the backlog queue.
63 * Paul Rusty Russell : SIOCSIFNAME
64 * Pekka Riikonen : Netdev boot-time settings code
65 * Andrew Morton : Make unregister_netdevice wait
722c9a0c 66 * indefinitely on dev->refcnt
67 * J Hadi Salim : - Backlog queue sampling
1da177e4
LT
68 * - netif_rx() feedback
69 */
70
7c0f6ba6 71#include <linux/uaccess.h>
1da177e4 72#include <linux/bitops.h>
4fc268d2 73#include <linux/capability.h>
1da177e4
LT
74#include <linux/cpu.h>
75#include <linux/types.h>
76#include <linux/kernel.h>
08e9897d 77#include <linux/hash.h>
5a0e3ad6 78#include <linux/slab.h>
1da177e4 79#include <linux/sched.h>
f1083048 80#include <linux/sched/mm.h>
4a3e2f71 81#include <linux/mutex.h>
11d6011c 82#include <linux/rwsem.h>
1da177e4
LT
83#include <linux/string.h>
84#include <linux/mm.h>
85#include <linux/socket.h>
86#include <linux/sockios.h>
87#include <linux/errno.h>
88#include <linux/interrupt.h>
89#include <linux/if_ether.h>
90#include <linux/netdevice.h>
91#include <linux/etherdevice.h>
0187bdfb 92#include <linux/ethtool.h>
1da177e4 93#include <linux/skbuff.h>
a7862b45 94#include <linux/bpf.h>
b5cdae32 95#include <linux/bpf_trace.h>
457c4cbc 96#include <net/net_namespace.h>
1da177e4 97#include <net/sock.h>
02d62e86 98#include <net/busy_poll.h>
1da177e4 99#include <linux/rtnetlink.h>
1da177e4 100#include <linux/stat.h>
b14a9fc4 101#include <net/dsa.h>
1da177e4 102#include <net/dst.h>
fc4099f1 103#include <net/dst_metadata.h>
04f00ab2 104#include <net/gro.h>
1da177e4 105#include <net/pkt_sched.h>
87d83093 106#include <net/pkt_cls.h>
1da177e4 107#include <net/checksum.h>
44540960 108#include <net/xfrm.h>
1da177e4
LT
109#include <linux/highmem.h>
110#include <linux/init.h>
1da177e4 111#include <linux/module.h>
1da177e4
LT
112#include <linux/netpoll.h>
113#include <linux/rcupdate.h>
114#include <linux/delay.h>
1da177e4 115#include <net/iw_handler.h>
1da177e4 116#include <asm/current.h>
5bdb9886 117#include <linux/audit.h>
db217334 118#include <linux/dmaengine.h>
f6a78bfc 119#include <linux/err.h>
c7fa9d18 120#include <linux/ctype.h>
723e98b7 121#include <linux/if_arp.h>
6de329e2 122#include <linux/if_vlan.h>
8f0f2223 123#include <linux/ip.h>
ad55dcaf 124#include <net/ip.h>
25cd9ba0 125#include <net/mpls.h>
8f0f2223
DM
126#include <linux/ipv6.h>
127#include <linux/in.h>
b6b2fed1
DM
128#include <linux/jhash.h>
129#include <linux/random.h>
9cbc1cb8 130#include <trace/events/napi.h>
cf66ba58 131#include <trace/events/net.h>
07dc22e7 132#include <trace/events/skb.h>
caeda9b9 133#include <linux/inetdevice.h>
c445477d 134#include <linux/cpu_rmap.h>
c5905afb 135#include <linux/static_key.h>
af12fa6e 136#include <linux/hashtable.h>
60877a32 137#include <linux/vmalloc.h>
529d0489 138#include <linux/if_macvlan.h>
e7fd2885 139#include <linux/errqueue.h>
3b47d303 140#include <linux/hrtimer.h>
357b6cc5 141#include <linux/netfilter_ingress.h>
40e4e713 142#include <linux/crash_dump.h>
b72b5bf6 143#include <linux/sctp.h>
ae847f40 144#include <net/udp_tunnel.h>
6621dd29 145#include <linux/net_namespace.h>
aaa5d90b 146#include <linux/indirect_call_wrapper.h>
af3836df 147#include <net/devlink.h>
bd869245 148#include <linux/pm_runtime.h>
3744741a 149#include <linux/prandom.h>
1da177e4 150
342709ef
PE
151#include "net-sysfs.h"
152
d565b0a1
HX
153#define MAX_GRO_SKBS 8
154
5d38a079
HX
155/* This should be increased if a protocol with a bigger head is added. */
156#define GRO_MAX_HEAD (MAX_HEADER + 128)
157
1da177e4 158static DEFINE_SPINLOCK(ptype_lock);
62532da9 159static DEFINE_SPINLOCK(offload_lock);
900ff8c6
CW
160struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
161struct list_head ptype_all __read_mostly; /* Taps */
62532da9 162static struct list_head offload_base __read_mostly;
1da177e4 163
ae78dbfa 164static int netif_rx_internal(struct sk_buff *skb);
54951194 165static int call_netdevice_notifiers_info(unsigned long val,
54951194 166 struct netdev_notifier_info *info);
26372605
PM
167static int call_netdevice_notifiers_extack(unsigned long val,
168 struct net_device *dev,
169 struct netlink_ext_ack *extack);
90b602f8 170static struct napi_struct *napi_by_id(unsigned int napi_id);
ae78dbfa 171
1da177e4 172/*
7562f876 173 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
174 * semaphore.
175 *
c6d14c84 176 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
1da177e4
LT
177 *
178 * Writers must hold the rtnl semaphore while they loop through the
7562f876 179 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
180 * actual updates. This allows pure readers to access the list even
181 * while a writer is preparing to update it.
182 *
183 * To put it another way, dev_base_lock is held for writing only to
184 * protect against pure readers; the rtnl semaphore provides the
185 * protection against other writers.
186 *
187 * See, for example usages, register_netdevice() and
188 * unregister_netdevice(), which must be called with the rtnl
189 * semaphore held.
190 */
1da177e4 191DEFINE_RWLOCK(dev_base_lock);
1da177e4
LT
192EXPORT_SYMBOL(dev_base_lock);
193
6c557001
FW
194static DEFINE_MUTEX(ifalias_mutex);
195
af12fa6e
ET
196/* protects napi_hash addition/deletion and napi_gen_id */
197static DEFINE_SPINLOCK(napi_hash_lock);
198
52bd2d62 199static unsigned int napi_gen_id = NR_CPUS;
6180d9de 200static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
af12fa6e 201
11d6011c 202static DECLARE_RWSEM(devnet_rename_sem);
c91f6df2 203
4e985ada
TG
204static inline void dev_base_seq_inc(struct net *net)
205{
643aa9cb 206 while (++net->dev_base_seq == 0)
207 ;
4e985ada
TG
208}
209
881d966b 210static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
1da177e4 211{
8387ff25 212 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
95c96174 213
08e9897d 214 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
1da177e4
LT
215}
216
881d966b 217static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
1da177e4 218{
7c28bd0b 219 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
1da177e4
LT
220}
221
e36fa2f7 222static inline void rps_lock(struct softnet_data *sd)
152102c7
CG
223{
224#ifdef CONFIG_RPS
e36fa2f7 225 spin_lock(&sd->input_pkt_queue.lock);
152102c7
CG
226#endif
227}
228
e36fa2f7 229static inline void rps_unlock(struct softnet_data *sd)
152102c7
CG
230{
231#ifdef CONFIG_RPS
e36fa2f7 232 spin_unlock(&sd->input_pkt_queue.lock);
152102c7
CG
233#endif
234}
235
ff927412
JP
236static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
237 const char *name)
238{
239 struct netdev_name_node *name_node;
240
241 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
242 if (!name_node)
243 return NULL;
244 INIT_HLIST_NODE(&name_node->hlist);
245 name_node->dev = dev;
246 name_node->name = name;
247 return name_node;
248}
249
250static struct netdev_name_node *
251netdev_name_node_head_alloc(struct net_device *dev)
252{
36fbf1e5
JP
253 struct netdev_name_node *name_node;
254
255 name_node = netdev_name_node_alloc(dev, dev->name);
256 if (!name_node)
257 return NULL;
258 INIT_LIST_HEAD(&name_node->list);
259 return name_node;
ff927412
JP
260}
261
262static void netdev_name_node_free(struct netdev_name_node *name_node)
263{
264 kfree(name_node);
265}
266
267static void netdev_name_node_add(struct net *net,
268 struct netdev_name_node *name_node)
269{
270 hlist_add_head_rcu(&name_node->hlist,
271 dev_name_hash(net, name_node->name));
272}
273
274static void netdev_name_node_del(struct netdev_name_node *name_node)
275{
276 hlist_del_rcu(&name_node->hlist);
277}
278
279static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
280 const char *name)
281{
282 struct hlist_head *head = dev_name_hash(net, name);
283 struct netdev_name_node *name_node;
284
285 hlist_for_each_entry(name_node, head, hlist)
286 if (!strcmp(name_node->name, name))
287 return name_node;
288 return NULL;
289}
290
291static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
292 const char *name)
293{
294 struct hlist_head *head = dev_name_hash(net, name);
295 struct netdev_name_node *name_node;
296
297 hlist_for_each_entry_rcu(name_node, head, hlist)
298 if (!strcmp(name_node->name, name))
299 return name_node;
300 return NULL;
301}
302
36fbf1e5
JP
303int netdev_name_node_alt_create(struct net_device *dev, const char *name)
304{
305 struct netdev_name_node *name_node;
306 struct net *net = dev_net(dev);
307
308 name_node = netdev_name_node_lookup(net, name);
309 if (name_node)
310 return -EEXIST;
311 name_node = netdev_name_node_alloc(dev, name);
312 if (!name_node)
313 return -ENOMEM;
314 netdev_name_node_add(net, name_node);
315 /* The node that holds dev->name acts as a head of per-device list. */
316 list_add_tail(&name_node->list, &dev->name_node->list);
317
318 return 0;
319}
320EXPORT_SYMBOL(netdev_name_node_alt_create);
321
322static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
323{
324 list_del(&name_node->list);
325 netdev_name_node_del(name_node);
326 kfree(name_node->name);
327 netdev_name_node_free(name_node);
328}
329
330int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
331{
332 struct netdev_name_node *name_node;
333 struct net *net = dev_net(dev);
334
335 name_node = netdev_name_node_lookup(net, name);
336 if (!name_node)
337 return -ENOENT;
e08ad805
ED
338 /* lookup might have found our primary name or a name belonging
339 * to another device.
340 */
341 if (name_node == dev->name_node || name_node->dev != dev)
342 return -EINVAL;
343
36fbf1e5
JP
344 __netdev_name_node_alt_destroy(name_node);
345
346 return 0;
347}
348EXPORT_SYMBOL(netdev_name_node_alt_destroy);
349
350static void netdev_name_node_alt_flush(struct net_device *dev)
351{
352 struct netdev_name_node *name_node, *tmp;
353
354 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
355 __netdev_name_node_alt_destroy(name_node);
356}
357
ce286d32 358/* Device list insertion */
53759be9 359static void list_netdevice(struct net_device *dev)
ce286d32 360{
c346dca1 361 struct net *net = dev_net(dev);
ce286d32
EB
362
363 ASSERT_RTNL();
364
365 write_lock_bh(&dev_base_lock);
c6d14c84 366 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
ff927412 367 netdev_name_node_add(net, dev->name_node);
fb699dfd
ED
368 hlist_add_head_rcu(&dev->index_hlist,
369 dev_index_hash(net, dev->ifindex));
ce286d32 370 write_unlock_bh(&dev_base_lock);
4e985ada
TG
371
372 dev_base_seq_inc(net);
ce286d32
EB
373}
374
fb699dfd
ED
375/* Device list removal
376 * caller must respect a RCU grace period before freeing/reusing dev
377 */
ce286d32
EB
378static void unlist_netdevice(struct net_device *dev)
379{
380 ASSERT_RTNL();
381
382 /* Unlink dev from the device chain */
383 write_lock_bh(&dev_base_lock);
c6d14c84 384 list_del_rcu(&dev->dev_list);
ff927412 385 netdev_name_node_del(dev->name_node);
fb699dfd 386 hlist_del_rcu(&dev->index_hlist);
ce286d32 387 write_unlock_bh(&dev_base_lock);
4e985ada
TG
388
389 dev_base_seq_inc(dev_net(dev));
ce286d32
EB
390}
391
1da177e4
LT
392/*
393 * Our notifier list
394 */
395
f07d5b94 396static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
397
398/*
399 * Device drivers call our routines to queue packets here. We empty the
400 * queue in the local softnet handler.
401 */
bea3348e 402
9958da05 403DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
d1b19dff 404EXPORT_PER_CPU_SYMBOL(softnet_data);
1da177e4 405
1a33e10e
CW
406#ifdef CONFIG_LOCKDEP
407/*
408 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
409 * according to dev->type
410 */
411static const unsigned short netdev_lock_type[] = {
412 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
413 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
414 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
415 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
416 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
417 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
418 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
419 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
420 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
421 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
422 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
423 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
424 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
425 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
426 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
427
428static const char *const netdev_lock_name[] = {
429 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
430 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
431 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
432 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
433 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
434 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
435 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
436 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
437 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
438 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
439 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
440 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
441 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
442 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
443 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
444
445static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
845e0ebb 446static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
1a33e10e
CW
447
448static inline unsigned short netdev_lock_pos(unsigned short dev_type)
449{
450 int i;
451
452 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
453 if (netdev_lock_type[i] == dev_type)
454 return i;
455 /* the last key is used by default */
456 return ARRAY_SIZE(netdev_lock_type) - 1;
457}
458
459static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
460 unsigned short dev_type)
461{
462 int i;
463
464 i = netdev_lock_pos(dev_type);
465 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
466 netdev_lock_name[i]);
467}
845e0ebb
CW
468
469static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
470{
471 int i;
472
473 i = netdev_lock_pos(dev->type);
474 lockdep_set_class_and_name(&dev->addr_list_lock,
475 &netdev_addr_lock_key[i],
476 netdev_lock_name[i]);
477}
1a33e10e
CW
478#else
479static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
480 unsigned short dev_type)
481{
482}
845e0ebb
CW
483
484static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
485{
486}
1a33e10e
CW
487#endif
488
1da177e4 489/*******************************************************************************
eb13da1a 490 *
491 * Protocol management and registration routines
492 *
493 *******************************************************************************/
1da177e4 494
1da177e4 495
1da177e4
LT
496/*
497 * Add a protocol ID to the list. Now that the input handler is
498 * smarter we can dispense with all the messy stuff that used to be
499 * here.
500 *
501 * BEWARE!!! Protocol handlers, mangling input packets,
502 * MUST BE last in hash buckets and checking protocol handlers
503 * MUST start from promiscuous ptype_all chain in net_bh.
504 * It is true now, do not change it.
505 * Explanation follows: if protocol handler, mangling packet, will
506 * be the first on list, it is not able to sense, that packet
507 * is cloned and should be copied-on-write, so that it will
508 * change it and subsequent readers will get broken packet.
509 * --ANK (980803)
510 */
511
c07b68e8
ED
512static inline struct list_head *ptype_head(const struct packet_type *pt)
513{
514 if (pt->type == htons(ETH_P_ALL))
7866a621 515 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
c07b68e8 516 else
7866a621
SN
517 return pt->dev ? &pt->dev->ptype_specific :
518 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
c07b68e8
ED
519}
520
1da177e4
LT
521/**
522 * dev_add_pack - add packet handler
523 * @pt: packet type declaration
524 *
525 * Add a protocol handler to the networking stack. The passed &packet_type
526 * is linked into kernel lists and may not be freed until it has been
527 * removed from the kernel lists.
528 *
4ec93edb 529 * This call does not sleep therefore it can not
1da177e4
LT
530 * guarantee all CPU's that are in middle of receiving packets
531 * will see the new packet type (until the next received packet).
532 */
533
534void dev_add_pack(struct packet_type *pt)
535{
c07b68e8 536 struct list_head *head = ptype_head(pt);
1da177e4 537
c07b68e8
ED
538 spin_lock(&ptype_lock);
539 list_add_rcu(&pt->list, head);
540 spin_unlock(&ptype_lock);
1da177e4 541}
d1b19dff 542EXPORT_SYMBOL(dev_add_pack);
1da177e4 543
1da177e4
LT
544/**
545 * __dev_remove_pack - remove packet handler
546 * @pt: packet type declaration
547 *
548 * Remove a protocol handler that was previously added to the kernel
549 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
550 * from the kernel lists and can be freed or reused once this function
4ec93edb 551 * returns.
1da177e4
LT
552 *
553 * The packet type might still be in use by receivers
554 * and must not be freed until after all the CPU's have gone
555 * through a quiescent state.
556 */
557void __dev_remove_pack(struct packet_type *pt)
558{
c07b68e8 559 struct list_head *head = ptype_head(pt);
1da177e4
LT
560 struct packet_type *pt1;
561
c07b68e8 562 spin_lock(&ptype_lock);
1da177e4
LT
563
564 list_for_each_entry(pt1, head, list) {
565 if (pt == pt1) {
566 list_del_rcu(&pt->list);
567 goto out;
568 }
569 }
570
7b6cd1ce 571 pr_warn("dev_remove_pack: %p not found\n", pt);
1da177e4 572out:
c07b68e8 573 spin_unlock(&ptype_lock);
1da177e4 574}
d1b19dff
ED
575EXPORT_SYMBOL(__dev_remove_pack);
576
1da177e4
LT
577/**
578 * dev_remove_pack - remove packet handler
579 * @pt: packet type declaration
580 *
581 * Remove a protocol handler that was previously added to the kernel
582 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
583 * from the kernel lists and can be freed or reused once this function
584 * returns.
585 *
586 * This call sleeps to guarantee that no CPU is looking at the packet
587 * type after return.
588 */
589void dev_remove_pack(struct packet_type *pt)
590{
591 __dev_remove_pack(pt);
4ec93edb 592
1da177e4
LT
593 synchronize_net();
594}
d1b19dff 595EXPORT_SYMBOL(dev_remove_pack);
1da177e4 596
62532da9
VY
597
598/**
599 * dev_add_offload - register offload handlers
600 * @po: protocol offload declaration
601 *
602 * Add protocol offload handlers to the networking stack. The passed
603 * &proto_offload is linked into kernel lists and may not be freed until
604 * it has been removed from the kernel lists.
605 *
606 * This call does not sleep therefore it can not
607 * guarantee all CPU's that are in middle of receiving packets
608 * will see the new offload handlers (until the next received packet).
609 */
610void dev_add_offload(struct packet_offload *po)
611{
bdef7de4 612 struct packet_offload *elem;
62532da9
VY
613
614 spin_lock(&offload_lock);
bdef7de4
DM
615 list_for_each_entry(elem, &offload_base, list) {
616 if (po->priority < elem->priority)
617 break;
618 }
619 list_add_rcu(&po->list, elem->list.prev);
62532da9
VY
620 spin_unlock(&offload_lock);
621}
622EXPORT_SYMBOL(dev_add_offload);
623
624/**
625 * __dev_remove_offload - remove offload handler
626 * @po: packet offload declaration
627 *
628 * Remove a protocol offload handler that was previously added to the
629 * kernel offload handlers by dev_add_offload(). The passed &offload_type
630 * is removed from the kernel lists and can be freed or reused once this
631 * function returns.
632 *
633 * The packet type might still be in use by receivers
634 * and must not be freed until after all the CPU's have gone
635 * through a quiescent state.
636 */
1d143d9f 637static void __dev_remove_offload(struct packet_offload *po)
62532da9
VY
638{
639 struct list_head *head = &offload_base;
640 struct packet_offload *po1;
641
c53aa505 642 spin_lock(&offload_lock);
62532da9
VY
643
644 list_for_each_entry(po1, head, list) {
645 if (po == po1) {
646 list_del_rcu(&po->list);
647 goto out;
648 }
649 }
650
651 pr_warn("dev_remove_offload: %p not found\n", po);
652out:
c53aa505 653 spin_unlock(&offload_lock);
62532da9 654}
62532da9
VY
655
656/**
657 * dev_remove_offload - remove packet offload handler
658 * @po: packet offload declaration
659 *
660 * Remove a packet offload handler that was previously added to the kernel
661 * offload handlers by dev_add_offload(). The passed &offload_type is
662 * removed from the kernel lists and can be freed or reused once this
663 * function returns.
664 *
665 * This call sleeps to guarantee that no CPU is looking at the packet
666 * type after return.
667 */
668void dev_remove_offload(struct packet_offload *po)
669{
670 __dev_remove_offload(po);
671
672 synchronize_net();
673}
674EXPORT_SYMBOL(dev_remove_offload);
675
1da177e4 676/******************************************************************************
eb13da1a 677 *
678 * Device Boot-time Settings Routines
679 *
680 ******************************************************************************/
1da177e4
LT
681
682/* Boot time configuration table */
683static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
684
685/**
686 * netdev_boot_setup_add - add new setup entry
687 * @name: name of the device
688 * @map: configured settings for the device
689 *
690 * Adds new setup entry to the dev_boot_setup list. The function
691 * returns 0 on error and 1 on success. This is a generic routine to
692 * all netdevices.
693 */
694static int netdev_boot_setup_add(char *name, struct ifmap *map)
695{
696 struct netdev_boot_setup *s;
697 int i;
698
699 s = dev_boot_setup;
700 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
701 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
702 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 703 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
704 memcpy(&s[i].map, map, sizeof(s[i].map));
705 break;
706 }
707 }
708
709 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
710}
711
712/**
722c9a0c 713 * netdev_boot_setup_check - check boot time settings
714 * @dev: the netdevice
1da177e4 715 *
722c9a0c 716 * Check boot time settings for the device.
717 * The found settings are set for the device to be used
718 * later in the device probing.
719 * Returns 0 if no settings found, 1 if they are.
1da177e4
LT
720 */
721int netdev_boot_setup_check(struct net_device *dev)
722{
723 struct netdev_boot_setup *s = dev_boot_setup;
724 int i;
725
726 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
727 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 728 !strcmp(dev->name, s[i].name)) {
722c9a0c 729 dev->irq = s[i].map.irq;
730 dev->base_addr = s[i].map.base_addr;
731 dev->mem_start = s[i].map.mem_start;
732 dev->mem_end = s[i].map.mem_end;
1da177e4
LT
733 return 1;
734 }
735 }
736 return 0;
737}
d1b19dff 738EXPORT_SYMBOL(netdev_boot_setup_check);
1da177e4
LT
739
740
741/**
722c9a0c 742 * netdev_boot_base - get address from boot time settings
743 * @prefix: prefix for network device
744 * @unit: id for network device
745 *
746 * Check boot time settings for the base address of device.
747 * The found settings are set for the device to be used
748 * later in the device probing.
749 * Returns 0 if no settings found.
1da177e4
LT
750 */
751unsigned long netdev_boot_base(const char *prefix, int unit)
752{
753 const struct netdev_boot_setup *s = dev_boot_setup;
754 char name[IFNAMSIZ];
755 int i;
756
757 sprintf(name, "%s%d", prefix, unit);
758
759 /*
760 * If device already registered then return base of 1
761 * to indicate not to probe for this interface
762 */
881d966b 763 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
764 return 1;
765
766 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
767 if (!strcmp(name, s[i].name))
768 return s[i].map.base_addr;
769 return 0;
770}
771
772/*
773 * Saves at boot time configured settings for any netdevice.
774 */
775int __init netdev_boot_setup(char *str)
776{
777 int ints[5];
778 struct ifmap map;
779
780 str = get_options(str, ARRAY_SIZE(ints), ints);
781 if (!str || !*str)
782 return 0;
783
784 /* Save settings */
785 memset(&map, 0, sizeof(map));
786 if (ints[0] > 0)
787 map.irq = ints[1];
788 if (ints[0] > 1)
789 map.base_addr = ints[2];
790 if (ints[0] > 2)
791 map.mem_start = ints[3];
792 if (ints[0] > 3)
793 map.mem_end = ints[4];
794
795 /* Add new entry to the list */
796 return netdev_boot_setup_add(str, &map);
797}
798
799__setup("netdev=", netdev_boot_setup);
800
801/*******************************************************************************
eb13da1a 802 *
803 * Device Interface Subroutines
804 *
805 *******************************************************************************/
1da177e4 806
a54acb3a
ND
807/**
808 * dev_get_iflink - get 'iflink' value of a interface
809 * @dev: targeted interface
810 *
811 * Indicates the ifindex the interface is linked to.
812 * Physical interfaces have the same 'ifindex' and 'iflink' values.
813 */
814
815int dev_get_iflink(const struct net_device *dev)
816{
817 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
818 return dev->netdev_ops->ndo_get_iflink(dev);
819
7a66bbc9 820 return dev->ifindex;
a54acb3a
ND
821}
822EXPORT_SYMBOL(dev_get_iflink);
823
fc4099f1
PS
824/**
825 * dev_fill_metadata_dst - Retrieve tunnel egress information.
826 * @dev: targeted interface
827 * @skb: The packet.
828 *
829 * For better visibility of tunnel traffic OVS needs to retrieve
830 * egress tunnel information for a packet. Following API allows
831 * user to get this info.
832 */
833int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
834{
835 struct ip_tunnel_info *info;
836
837 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
838 return -EINVAL;
839
840 info = skb_tunnel_info_unclone(skb);
841 if (!info)
842 return -ENOMEM;
843 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
844 return -EINVAL;
845
846 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
847}
848EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
849
1da177e4
LT
850/**
851 * __dev_get_by_name - find a device by its name
c4ea43c5 852 * @net: the applicable net namespace
1da177e4
LT
853 * @name: name to find
854 *
855 * Find an interface by name. Must be called under RTNL semaphore
856 * or @dev_base_lock. If the name is found a pointer to the device
857 * is returned. If the name is not found then %NULL is returned. The
858 * reference counters are not incremented so the caller must be
859 * careful with locks.
860 */
861
881d966b 862struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4 863{
ff927412 864 struct netdev_name_node *node_name;
1da177e4 865
ff927412
JP
866 node_name = netdev_name_node_lookup(net, name);
867 return node_name ? node_name->dev : NULL;
1da177e4 868}
d1b19dff 869EXPORT_SYMBOL(__dev_get_by_name);
1da177e4 870
72c9528b 871/**
722c9a0c 872 * dev_get_by_name_rcu - find a device by its name
873 * @net: the applicable net namespace
874 * @name: name to find
875 *
876 * Find an interface by name.
877 * If the name is found a pointer to the device is returned.
878 * If the name is not found then %NULL is returned.
879 * The reference counters are not incremented so the caller must be
880 * careful with locks. The caller must hold RCU lock.
72c9528b
ED
881 */
882
883struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
884{
ff927412 885 struct netdev_name_node *node_name;
72c9528b 886
ff927412
JP
887 node_name = netdev_name_node_lookup_rcu(net, name);
888 return node_name ? node_name->dev : NULL;
72c9528b
ED
889}
890EXPORT_SYMBOL(dev_get_by_name_rcu);
891
1da177e4
LT
892/**
893 * dev_get_by_name - find a device by its name
c4ea43c5 894 * @net: the applicable net namespace
1da177e4
LT
895 * @name: name to find
896 *
897 * Find an interface by name. This can be called from any
898 * context and does its own locking. The returned handle has
899 * the usage count incremented and the caller must use dev_put() to
900 * release it when it is no longer needed. %NULL is returned if no
901 * matching device is found.
902 */
903
881d966b 904struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
905{
906 struct net_device *dev;
907
72c9528b
ED
908 rcu_read_lock();
909 dev = dev_get_by_name_rcu(net, name);
1da177e4
LT
910 if (dev)
911 dev_hold(dev);
72c9528b 912 rcu_read_unlock();
1da177e4
LT
913 return dev;
914}
d1b19dff 915EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
916
917/**
918 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 919 * @net: the applicable net namespace
1da177e4
LT
920 * @ifindex: index of device
921 *
922 * Search for an interface by index. Returns %NULL if the device
923 * is not found or a pointer to the device. The device has not
924 * had its reference counter increased so the caller must be careful
925 * about locking. The caller must hold either the RTNL semaphore
926 * or @dev_base_lock.
927 */
928
881d966b 929struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4 930{
0bd8d536
ED
931 struct net_device *dev;
932 struct hlist_head *head = dev_index_hash(net, ifindex);
1da177e4 933
b67bfe0d 934 hlist_for_each_entry(dev, head, index_hlist)
1da177e4
LT
935 if (dev->ifindex == ifindex)
936 return dev;
0bd8d536 937
1da177e4
LT
938 return NULL;
939}
d1b19dff 940EXPORT_SYMBOL(__dev_get_by_index);
1da177e4 941
fb699dfd
ED
942/**
943 * dev_get_by_index_rcu - find a device by its ifindex
944 * @net: the applicable net namespace
945 * @ifindex: index of device
946 *
947 * Search for an interface by index. Returns %NULL if the device
948 * is not found or a pointer to the device. The device has not
949 * had its reference counter increased so the caller must be careful
950 * about locking. The caller must hold RCU lock.
951 */
952
953struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
954{
fb699dfd
ED
955 struct net_device *dev;
956 struct hlist_head *head = dev_index_hash(net, ifindex);
957
b67bfe0d 958 hlist_for_each_entry_rcu(dev, head, index_hlist)
fb699dfd
ED
959 if (dev->ifindex == ifindex)
960 return dev;
961
962 return NULL;
963}
964EXPORT_SYMBOL(dev_get_by_index_rcu);
965
1da177e4
LT
966
967/**
968 * dev_get_by_index - find a device by its ifindex
c4ea43c5 969 * @net: the applicable net namespace
1da177e4
LT
970 * @ifindex: index of device
971 *
972 * Search for an interface by index. Returns NULL if the device
973 * is not found or a pointer to the device. The device returned has
974 * had a reference added and the pointer is safe until the user calls
975 * dev_put to indicate they have finished with it.
976 */
977
881d966b 978struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
979{
980 struct net_device *dev;
981
fb699dfd
ED
982 rcu_read_lock();
983 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
984 if (dev)
985 dev_hold(dev);
fb699dfd 986 rcu_read_unlock();
1da177e4
LT
987 return dev;
988}
d1b19dff 989EXPORT_SYMBOL(dev_get_by_index);
1da177e4 990
90b602f8
ML
991/**
992 * dev_get_by_napi_id - find a device by napi_id
993 * @napi_id: ID of the NAPI struct
994 *
995 * Search for an interface by NAPI ID. Returns %NULL if the device
996 * is not found or a pointer to the device. The device has not had
997 * its reference counter increased so the caller must be careful
998 * about locking. The caller must hold RCU lock.
999 */
1000
1001struct net_device *dev_get_by_napi_id(unsigned int napi_id)
1002{
1003 struct napi_struct *napi;
1004
1005 WARN_ON_ONCE(!rcu_read_lock_held());
1006
1007 if (napi_id < MIN_NAPI_ID)
1008 return NULL;
1009
1010 napi = napi_by_id(napi_id);
1011
1012 return napi ? napi->dev : NULL;
1013}
1014EXPORT_SYMBOL(dev_get_by_napi_id);
1015
5dbe7c17
NS
1016/**
1017 * netdev_get_name - get a netdevice name, knowing its ifindex.
1018 * @net: network namespace
1019 * @name: a pointer to the buffer where the name will be stored.
1020 * @ifindex: the ifindex of the interface to get the name from.
5dbe7c17
NS
1021 */
1022int netdev_get_name(struct net *net, char *name, int ifindex)
1023{
1024 struct net_device *dev;
11d6011c 1025 int ret;
5dbe7c17 1026
11d6011c 1027 down_read(&devnet_rename_sem);
5dbe7c17 1028 rcu_read_lock();
11d6011c 1029
5dbe7c17
NS
1030 dev = dev_get_by_index_rcu(net, ifindex);
1031 if (!dev) {
11d6011c
AD
1032 ret = -ENODEV;
1033 goto out;
5dbe7c17
NS
1034 }
1035
1036 strcpy(name, dev->name);
5dbe7c17 1037
11d6011c
AD
1038 ret = 0;
1039out:
1040 rcu_read_unlock();
1041 up_read(&devnet_rename_sem);
1042 return ret;
5dbe7c17
NS
1043}
1044
1da177e4 1045/**
941666c2 1046 * dev_getbyhwaddr_rcu - find a device by its hardware address
c4ea43c5 1047 * @net: the applicable net namespace
1da177e4
LT
1048 * @type: media type of device
1049 * @ha: hardware address
1050 *
1051 * Search for an interface by MAC address. Returns NULL if the device
c506653d
ED
1052 * is not found or a pointer to the device.
1053 * The caller must hold RCU or RTNL.
941666c2 1054 * The returned device has not had its ref count increased
1da177e4
LT
1055 * and the caller must therefore be careful about locking
1056 *
1da177e4
LT
1057 */
1058
941666c2
ED
1059struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
1060 const char *ha)
1da177e4
LT
1061{
1062 struct net_device *dev;
1063
941666c2 1064 for_each_netdev_rcu(net, dev)
1da177e4
LT
1065 if (dev->type == type &&
1066 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
1067 return dev;
1068
1069 return NULL;
1da177e4 1070}
941666c2 1071EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
cf309e3f 1072
881d966b 1073struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b 1074{
99fe3c39 1075 struct net_device *dev, *ret = NULL;
4e9cac2b 1076
99fe3c39
ED
1077 rcu_read_lock();
1078 for_each_netdev_rcu(net, dev)
1079 if (dev->type == type) {
1080 dev_hold(dev);
1081 ret = dev;
1082 break;
1083 }
1084 rcu_read_unlock();
1085 return ret;
1da177e4 1086}
1da177e4
LT
1087EXPORT_SYMBOL(dev_getfirstbyhwtype);
1088
1089/**
6c555490 1090 * __dev_get_by_flags - find any device with given flags
c4ea43c5 1091 * @net: the applicable net namespace
1da177e4
LT
1092 * @if_flags: IFF_* values
1093 * @mask: bitmask of bits in if_flags to check
1094 *
1095 * Search for any interface with the given flags. Returns NULL if a device
bb69ae04 1096 * is not found or a pointer to the device. Must be called inside
6c555490 1097 * rtnl_lock(), and result refcount is unchanged.
1da177e4
LT
1098 */
1099
6c555490
WC
1100struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1101 unsigned short mask)
1da177e4 1102{
7562f876 1103 struct net_device *dev, *ret;
1da177e4 1104
6c555490
WC
1105 ASSERT_RTNL();
1106
7562f876 1107 ret = NULL;
6c555490 1108 for_each_netdev(net, dev) {
1da177e4 1109 if (((dev->flags ^ if_flags) & mask) == 0) {
7562f876 1110 ret = dev;
1da177e4
LT
1111 break;
1112 }
1113 }
7562f876 1114 return ret;
1da177e4 1115}
6c555490 1116EXPORT_SYMBOL(__dev_get_by_flags);
1da177e4
LT
1117
1118/**
1119 * dev_valid_name - check if name is okay for network device
1120 * @name: name string
1121 *
1122 * Network device names need to be valid file names to
4250b75b 1123 * allow sysfs to work. We also disallow any kind of
c7fa9d18 1124 * whitespace.
1da177e4 1125 */
95f050bf 1126bool dev_valid_name(const char *name)
1da177e4 1127{
c7fa9d18 1128 if (*name == '\0')
95f050bf 1129 return false;
a9d48205 1130 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
95f050bf 1131 return false;
c7fa9d18 1132 if (!strcmp(name, ".") || !strcmp(name, ".."))
95f050bf 1133 return false;
c7fa9d18
DM
1134
1135 while (*name) {
a4176a93 1136 if (*name == '/' || *name == ':' || isspace(*name))
95f050bf 1137 return false;
c7fa9d18
DM
1138 name++;
1139 }
95f050bf 1140 return true;
1da177e4 1141}
d1b19dff 1142EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
1143
1144/**
b267b179
EB
1145 * __dev_alloc_name - allocate a name for a device
1146 * @net: network namespace to allocate the device name in
1da177e4 1147 * @name: name format string
b267b179 1148 * @buf: scratch buffer and result name string
1da177e4
LT
1149 *
1150 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
1151 * id. It scans list of devices to build up a free map, then chooses
1152 * the first empty slot. The caller must hold the dev_base or rtnl lock
1153 * while allocating the name and adding the device in order to avoid
1154 * duplicates.
1155 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1156 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
1157 */
1158
b267b179 1159static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
1160{
1161 int i = 0;
1da177e4
LT
1162 const char *p;
1163 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 1164 unsigned long *inuse;
1da177e4
LT
1165 struct net_device *d;
1166
93809105
RV
1167 if (!dev_valid_name(name))
1168 return -EINVAL;
1169
51f299dd 1170 p = strchr(name, '%');
1da177e4
LT
1171 if (p) {
1172 /*
1173 * Verify the string as this thing may have come from
1174 * the user. There must be either one "%d" and no other "%"
1175 * characters.
1176 */
1177 if (p[1] != 'd' || strchr(p + 2, '%'))
1178 return -EINVAL;
1179
1180 /* Use one page as a bit array of possible slots */
cfcabdcc 1181 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
1182 if (!inuse)
1183 return -ENOMEM;
1184
881d966b 1185 for_each_netdev(net, d) {
1da177e4
LT
1186 if (!sscanf(d->name, name, &i))
1187 continue;
1188 if (i < 0 || i >= max_netdevices)
1189 continue;
1190
1191 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 1192 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
1193 if (!strncmp(buf, d->name, IFNAMSIZ))
1194 set_bit(i, inuse);
1195 }
1196
1197 i = find_first_zero_bit(inuse, max_netdevices);
1198 free_page((unsigned long) inuse);
1199 }
1200
6224abda 1201 snprintf(buf, IFNAMSIZ, name, i);
b267b179 1202 if (!__dev_get_by_name(net, buf))
1da177e4 1203 return i;
1da177e4
LT
1204
1205 /* It is possible to run out of possible slots
1206 * when the name is long and there isn't enough space left
1207 * for the digits, or if all bits are used.
1208 */
029b6d14 1209 return -ENFILE;
1da177e4
LT
1210}
1211
2c88b855
RV
1212static int dev_alloc_name_ns(struct net *net,
1213 struct net_device *dev,
1214 const char *name)
1215{
1216 char buf[IFNAMSIZ];
1217 int ret;
1218
c46d7642 1219 BUG_ON(!net);
2c88b855
RV
1220 ret = __dev_alloc_name(net, name, buf);
1221 if (ret >= 0)
1222 strlcpy(dev->name, buf, IFNAMSIZ);
1223 return ret;
1da177e4
LT
1224}
1225
b267b179
EB
1226/**
1227 * dev_alloc_name - allocate a name for a device
1228 * @dev: device
1229 * @name: name format string
1230 *
1231 * Passed a format string - eg "lt%d" it will try and find a suitable
1232 * id. It scans list of devices to build up a free map, then chooses
1233 * the first empty slot. The caller must hold the dev_base or rtnl lock
1234 * while allocating the name and adding the device in order to avoid
1235 * duplicates.
1236 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1237 * Returns the number of the unit assigned or a negative errno code.
1238 */
1239
1240int dev_alloc_name(struct net_device *dev, const char *name)
1241{
c46d7642 1242 return dev_alloc_name_ns(dev_net(dev), dev, name);
b267b179 1243}
d1b19dff 1244EXPORT_SYMBOL(dev_alloc_name);
b267b179 1245
bacb7e18
ED
1246static int dev_get_valid_name(struct net *net, struct net_device *dev,
1247 const char *name)
828de4f6 1248{
55a5ec9b
DM
1249 BUG_ON(!net);
1250
1251 if (!dev_valid_name(name))
1252 return -EINVAL;
1253
1254 if (strchr(name, '%'))
1255 return dev_alloc_name_ns(net, dev, name);
1256 else if (__dev_get_by_name(net, name))
1257 return -EEXIST;
1258 else if (dev->name != name)
1259 strlcpy(dev->name, name, IFNAMSIZ);
1260
1261 return 0;
d9031024 1262}
1da177e4
LT
1263
1264/**
1265 * dev_change_name - change name of a device
1266 * @dev: device
1267 * @newname: name (or format string) must be at least IFNAMSIZ
1268 *
1269 * Change name of a device, can pass format strings "eth%d".
1270 * for wildcarding.
1271 */
cf04a4c7 1272int dev_change_name(struct net_device *dev, const char *newname)
1da177e4 1273{
238fa362 1274 unsigned char old_assign_type;
fcc5a03a 1275 char oldname[IFNAMSIZ];
1da177e4 1276 int err = 0;
fcc5a03a 1277 int ret;
881d966b 1278 struct net *net;
1da177e4
LT
1279
1280 ASSERT_RTNL();
c346dca1 1281 BUG_ON(!dev_net(dev));
1da177e4 1282
c346dca1 1283 net = dev_net(dev);
8065a779
SWL
1284
1285 /* Some auto-enslaved devices e.g. failover slaves are
1286 * special, as userspace might rename the device after
1287 * the interface had been brought up and running since
1288 * the point kernel initiated auto-enslavement. Allow
1289 * live name change even when these slave devices are
1290 * up and running.
1291 *
1292 * Typically, users of these auto-enslaving devices
1293 * don't actually care about slave name change, as
1294 * they are supposed to operate on master interface
1295 * directly.
1296 */
1297 if (dev->flags & IFF_UP &&
1298 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
1da177e4
LT
1299 return -EBUSY;
1300
11d6011c 1301 down_write(&devnet_rename_sem);
c91f6df2
BH
1302
1303 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
11d6011c 1304 up_write(&devnet_rename_sem);
c8d90dca 1305 return 0;
c91f6df2 1306 }
c8d90dca 1307
fcc5a03a
HX
1308 memcpy(oldname, dev->name, IFNAMSIZ);
1309
828de4f6 1310 err = dev_get_valid_name(net, dev, newname);
c91f6df2 1311 if (err < 0) {
11d6011c 1312 up_write(&devnet_rename_sem);
d9031024 1313 return err;
c91f6df2 1314 }
1da177e4 1315
6fe82a39
VF
1316 if (oldname[0] && !strchr(oldname, '%'))
1317 netdev_info(dev, "renamed from %s\n", oldname);
1318
238fa362
TG
1319 old_assign_type = dev->name_assign_type;
1320 dev->name_assign_type = NET_NAME_RENAMED;
1321
fcc5a03a 1322rollback:
a1b3f594
EB
1323 ret = device_rename(&dev->dev, dev->name);
1324 if (ret) {
1325 memcpy(dev->name, oldname, IFNAMSIZ);
238fa362 1326 dev->name_assign_type = old_assign_type;
11d6011c 1327 up_write(&devnet_rename_sem);
a1b3f594 1328 return ret;
dcc99773 1329 }
7f988eab 1330
11d6011c 1331 up_write(&devnet_rename_sem);
c91f6df2 1332
5bb025fa
VF
1333 netdev_adjacent_rename_links(dev, oldname);
1334
7f988eab 1335 write_lock_bh(&dev_base_lock);
ff927412 1336 netdev_name_node_del(dev->name_node);
72c9528b
ED
1337 write_unlock_bh(&dev_base_lock);
1338
1339 synchronize_rcu();
1340
1341 write_lock_bh(&dev_base_lock);
ff927412 1342 netdev_name_node_add(net, dev->name_node);
7f988eab
HX
1343 write_unlock_bh(&dev_base_lock);
1344
056925ab 1345 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
1346 ret = notifier_to_errno(ret);
1347
1348 if (ret) {
91e9c07b
ED
1349 /* err >= 0 after dev_alloc_name() or stores the first errno */
1350 if (err >= 0) {
fcc5a03a 1351 err = ret;
11d6011c 1352 down_write(&devnet_rename_sem);
fcc5a03a 1353 memcpy(dev->name, oldname, IFNAMSIZ);
5bb025fa 1354 memcpy(oldname, newname, IFNAMSIZ);
238fa362
TG
1355 dev->name_assign_type = old_assign_type;
1356 old_assign_type = NET_NAME_RENAMED;
fcc5a03a 1357 goto rollback;
91e9c07b 1358 } else {
7b6cd1ce 1359 pr_err("%s: name change rollback failed: %d\n",
91e9c07b 1360 dev->name, ret);
fcc5a03a
HX
1361 }
1362 }
1da177e4
LT
1363
1364 return err;
1365}
1366
0b815a1a
SH
1367/**
1368 * dev_set_alias - change ifalias of a device
1369 * @dev: device
1370 * @alias: name up to IFALIASZ
f0db275a 1371 * @len: limit of bytes to copy from info
0b815a1a
SH
1372 *
1373 * Set ifalias for a device,
1374 */
1375int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1376{
6c557001 1377 struct dev_ifalias *new_alias = NULL;
0b815a1a
SH
1378
1379 if (len >= IFALIASZ)
1380 return -EINVAL;
1381
6c557001
FW
1382 if (len) {
1383 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1384 if (!new_alias)
1385 return -ENOMEM;
1386
1387 memcpy(new_alias->ifalias, alias, len);
1388 new_alias->ifalias[len] = 0;
96ca4a2c
OH
1389 }
1390
6c557001 1391 mutex_lock(&ifalias_mutex);
e3f0d761
PM
1392 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1393 mutex_is_locked(&ifalias_mutex));
6c557001
FW
1394 mutex_unlock(&ifalias_mutex);
1395
1396 if (new_alias)
1397 kfree_rcu(new_alias, rcuhead);
0b815a1a 1398
0b815a1a
SH
1399 return len;
1400}
0fe554a4 1401EXPORT_SYMBOL(dev_set_alias);
0b815a1a 1402
6c557001
FW
1403/**
1404 * dev_get_alias - get ifalias of a device
1405 * @dev: device
20e88320 1406 * @name: buffer to store name of ifalias
6c557001
FW
1407 * @len: size of buffer
1408 *
1409 * get ifalias for a device. Caller must make sure dev cannot go
1410 * away, e.g. rcu read lock or own a reference count to device.
1411 */
1412int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1413{
1414 const struct dev_ifalias *alias;
1415 int ret = 0;
1416
1417 rcu_read_lock();
1418 alias = rcu_dereference(dev->ifalias);
1419 if (alias)
1420 ret = snprintf(name, len, "%s", alias->ifalias);
1421 rcu_read_unlock();
1422
1423 return ret;
1424}
0b815a1a 1425
d8a33ac4 1426/**
3041a069 1427 * netdev_features_change - device changes features
d8a33ac4
SH
1428 * @dev: device to cause notification
1429 *
1430 * Called to indicate a device has changed features.
1431 */
1432void netdev_features_change(struct net_device *dev)
1433{
056925ab 1434 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
1435}
1436EXPORT_SYMBOL(netdev_features_change);
1437
1da177e4
LT
1438/**
1439 * netdev_state_change - device changes state
1440 * @dev: device to cause notification
1441 *
1442 * Called to indicate a device has changed state. This function calls
1443 * the notifier chains for netdev_chain and sends a NEWLINK message
1444 * to the routing socket.
1445 */
1446void netdev_state_change(struct net_device *dev)
1447{
1448 if (dev->flags & IFF_UP) {
51d0c047
DA
1449 struct netdev_notifier_change_info change_info = {
1450 .info.dev = dev,
1451 };
54951194 1452
51d0c047 1453 call_netdevice_notifiers_info(NETDEV_CHANGE,
54951194 1454 &change_info.info);
7f294054 1455 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1da177e4
LT
1456 }
1457}
d1b19dff 1458EXPORT_SYMBOL(netdev_state_change);
1da177e4 1459
7061eb8c
LP
1460/**
1461 * __netdev_notify_peers - notify network peers about existence of @dev,
1462 * to be called when rtnl lock is already held.
1463 * @dev: network device
1464 *
1465 * Generate traffic such that interested network peers are aware of
1466 * @dev, such as by generating a gratuitous ARP. This may be used when
1467 * a device wants to inform the rest of the network about some sort of
1468 * reconfiguration such as a failover event or virtual machine
1469 * migration.
1470 */
1471void __netdev_notify_peers(struct net_device *dev)
1472{
1473 ASSERT_RTNL();
1474 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1475 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1476}
1477EXPORT_SYMBOL(__netdev_notify_peers);
1478
ee89bab1 1479/**
722c9a0c 1480 * netdev_notify_peers - notify network peers about existence of @dev
1481 * @dev: network device
ee89bab1
AW
1482 *
1483 * Generate traffic such that interested network peers are aware of
1484 * @dev, such as by generating a gratuitous ARP. This may be used when
1485 * a device wants to inform the rest of the network about some sort of
1486 * reconfiguration such as a failover event or virtual machine
1487 * migration.
1488 */
1489void netdev_notify_peers(struct net_device *dev)
c1da4ac7 1490{
ee89bab1 1491 rtnl_lock();
7061eb8c 1492 __netdev_notify_peers(dev);
ee89bab1 1493 rtnl_unlock();
c1da4ac7 1494}
ee89bab1 1495EXPORT_SYMBOL(netdev_notify_peers);
c1da4ac7 1496
40c900aa 1497static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1da177e4 1498{
d314774c 1499 const struct net_device_ops *ops = dev->netdev_ops;
3b8bcfd5 1500 int ret;
1da177e4 1501
e46b66bc
BH
1502 ASSERT_RTNL();
1503
bd869245
HK
1504 if (!netif_device_present(dev)) {
1505 /* may be detached because parent is runtime-suspended */
1506 if (dev->dev.parent)
1507 pm_runtime_resume(dev->dev.parent);
1508 if (!netif_device_present(dev))
1509 return -ENODEV;
1510 }
1da177e4 1511
ca99ca14
NH
1512 /* Block netpoll from trying to do any rx path servicing.
1513 * If we don't do this there is a chance ndo_poll_controller
1514 * or ndo_poll may be running while we open the device
1515 */
66b5552f 1516 netpoll_poll_disable(dev);
ca99ca14 1517
40c900aa 1518 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
3b8bcfd5
JB
1519 ret = notifier_to_errno(ret);
1520 if (ret)
1521 return ret;
1522
1da177e4 1523 set_bit(__LINK_STATE_START, &dev->state);
bada339b 1524
d314774c
SH
1525 if (ops->ndo_validate_addr)
1526 ret = ops->ndo_validate_addr(dev);
bada339b 1527
d314774c
SH
1528 if (!ret && ops->ndo_open)
1529 ret = ops->ndo_open(dev);
1da177e4 1530
66b5552f 1531 netpoll_poll_enable(dev);
ca99ca14 1532
bada339b
JG
1533 if (ret)
1534 clear_bit(__LINK_STATE_START, &dev->state);
1535 else {
1da177e4 1536 dev->flags |= IFF_UP;
4417da66 1537 dev_set_rx_mode(dev);
1da177e4 1538 dev_activate(dev);
7bf23575 1539 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 1540 }
bada339b 1541
1da177e4
LT
1542 return ret;
1543}
1544
1545/**
bd380811 1546 * dev_open - prepare an interface for use.
00f54e68
PM
1547 * @dev: device to open
1548 * @extack: netlink extended ack
1da177e4 1549 *
bd380811
PM
1550 * Takes a device from down to up state. The device's private open
1551 * function is invoked and then the multicast lists are loaded. Finally
1552 * the device is moved into the up state and a %NETDEV_UP message is
1553 * sent to the netdev notifier chain.
1554 *
1555 * Calling this function on an active interface is a nop. On a failure
1556 * a negative errno code is returned.
1da177e4 1557 */
00f54e68 1558int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
bd380811
PM
1559{
1560 int ret;
1561
bd380811
PM
1562 if (dev->flags & IFF_UP)
1563 return 0;
1564
40c900aa 1565 ret = __dev_open(dev, extack);
bd380811
PM
1566 if (ret < 0)
1567 return ret;
1568
7f294054 1569 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
bd380811
PM
1570 call_netdevice_notifiers(NETDEV_UP, dev);
1571
1572 return ret;
1573}
1574EXPORT_SYMBOL(dev_open);
1575
7051b88a 1576static void __dev_close_many(struct list_head *head)
1da177e4 1577{
44345724 1578 struct net_device *dev;
e46b66bc 1579
bd380811 1580 ASSERT_RTNL();
9d5010db
DM
1581 might_sleep();
1582
5cde2829 1583 list_for_each_entry(dev, head, close_list) {
3f4df206 1584 /* Temporarily disable netpoll until the interface is down */
66b5552f 1585 netpoll_poll_disable(dev);
3f4df206 1586
44345724 1587 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1588
44345724 1589 clear_bit(__LINK_STATE_START, &dev->state);
1da177e4 1590
44345724
OP
1591 /* Synchronize to scheduled poll. We cannot touch poll list, it
1592 * can be even on different cpu. So just clear netif_running().
1593 *
1594 * dev->stop() will invoke napi_disable() on all of it's
1595 * napi_struct instances on this device.
1596 */
4e857c58 1597 smp_mb__after_atomic(); /* Commit netif_running(). */
44345724 1598 }
1da177e4 1599
44345724 1600 dev_deactivate_many(head);
d8b2a4d2 1601
5cde2829 1602 list_for_each_entry(dev, head, close_list) {
44345724 1603 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4 1604
44345724
OP
1605 /*
1606 * Call the device specific close. This cannot fail.
1607 * Only if device is UP
1608 *
1609 * We allow it to be called even after a DETACH hot-plug
1610 * event.
1611 */
1612 if (ops->ndo_stop)
1613 ops->ndo_stop(dev);
1614
44345724 1615 dev->flags &= ~IFF_UP;
66b5552f 1616 netpoll_poll_enable(dev);
44345724 1617 }
44345724
OP
1618}
1619
7051b88a 1620static void __dev_close(struct net_device *dev)
44345724
OP
1621{
1622 LIST_HEAD(single);
1623
5cde2829 1624 list_add(&dev->close_list, &single);
7051b88a 1625 __dev_close_many(&single);
f87e6f47 1626 list_del(&single);
44345724
OP
1627}
1628
7051b88a 1629void dev_close_many(struct list_head *head, bool unlink)
44345724
OP
1630{
1631 struct net_device *dev, *tmp;
1da177e4 1632
5cde2829
EB
1633 /* Remove the devices that don't need to be closed */
1634 list_for_each_entry_safe(dev, tmp, head, close_list)
44345724 1635 if (!(dev->flags & IFF_UP))
5cde2829 1636 list_del_init(&dev->close_list);
44345724
OP
1637
1638 __dev_close_many(head);
1da177e4 1639
5cde2829 1640 list_for_each_entry_safe(dev, tmp, head, close_list) {
7f294054 1641 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
44345724 1642 call_netdevice_notifiers(NETDEV_DOWN, dev);
99c4a26a
DM
1643 if (unlink)
1644 list_del_init(&dev->close_list);
44345724 1645 }
bd380811 1646}
99c4a26a 1647EXPORT_SYMBOL(dev_close_many);
bd380811
PM
1648
1649/**
1650 * dev_close - shutdown an interface.
1651 * @dev: device to shutdown
1652 *
1653 * This function moves an active device into down state. A
1654 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1655 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1656 * chain.
1657 */
7051b88a 1658void dev_close(struct net_device *dev)
bd380811 1659{
e14a5993
ED
1660 if (dev->flags & IFF_UP) {
1661 LIST_HEAD(single);
1da177e4 1662
5cde2829 1663 list_add(&dev->close_list, &single);
99c4a26a 1664 dev_close_many(&single, true);
e14a5993
ED
1665 list_del(&single);
1666 }
1da177e4 1667}
d1b19dff 1668EXPORT_SYMBOL(dev_close);
1da177e4
LT
1669
1670
0187bdfb
BH
1671/**
1672 * dev_disable_lro - disable Large Receive Offload on a device
1673 * @dev: device
1674 *
1675 * Disable Large Receive Offload (LRO) on a net device. Must be
1676 * called under RTNL. This is needed if received packets may be
1677 * forwarded to another interface.
1678 */
1679void dev_disable_lro(struct net_device *dev)
1680{
fbe168ba
MK
1681 struct net_device *lower_dev;
1682 struct list_head *iter;
529d0489 1683
bc5787c6
MM
1684 dev->wanted_features &= ~NETIF_F_LRO;
1685 netdev_update_features(dev);
27660515 1686
22d5969f
MM
1687 if (unlikely(dev->features & NETIF_F_LRO))
1688 netdev_WARN(dev, "failed to disable LRO!\n");
fbe168ba
MK
1689
1690 netdev_for_each_lower_dev(dev, lower_dev, iter)
1691 dev_disable_lro(lower_dev);
0187bdfb
BH
1692}
1693EXPORT_SYMBOL(dev_disable_lro);
1694
56f5aa77
MC
1695/**
1696 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1697 * @dev: device
1698 *
1699 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1700 * called under RTNL. This is needed if Generic XDP is installed on
1701 * the device.
1702 */
1703static void dev_disable_gro_hw(struct net_device *dev)
1704{
1705 dev->wanted_features &= ~NETIF_F_GRO_HW;
1706 netdev_update_features(dev);
1707
1708 if (unlikely(dev->features & NETIF_F_GRO_HW))
1709 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1710}
1711
ede2762d
KT
1712const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1713{
1714#define N(val) \
1715 case NETDEV_##val: \
1716 return "NETDEV_" __stringify(val);
1717 switch (cmd) {
1718 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1719 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1720 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1721 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1722 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1723 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1724 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
9daae9bd
GP
1725 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1726 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
1570415f 1727 N(PRE_CHANGEADDR)
3f5ecd8a 1728 }
ede2762d
KT
1729#undef N
1730 return "UNKNOWN_NETDEV_EVENT";
1731}
1732EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1733
351638e7
JP
1734static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1735 struct net_device *dev)
1736{
51d0c047
DA
1737 struct netdev_notifier_info info = {
1738 .dev = dev,
1739 };
351638e7 1740
351638e7
JP
1741 return nb->notifier_call(nb, val, &info);
1742}
0187bdfb 1743
afa0df59
JP
1744static int call_netdevice_register_notifiers(struct notifier_block *nb,
1745 struct net_device *dev)
1746{
1747 int err;
1748
1749 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1750 err = notifier_to_errno(err);
1751 if (err)
1752 return err;
1753
1754 if (!(dev->flags & IFF_UP))
1755 return 0;
1756
1757 call_netdevice_notifier(nb, NETDEV_UP, dev);
1758 return 0;
1759}
1760
1761static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1762 struct net_device *dev)
1763{
1764 if (dev->flags & IFF_UP) {
1765 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1766 dev);
1767 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1768 }
1769 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1770}
1771
1772static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1773 struct net *net)
1774{
1775 struct net_device *dev;
1776 int err;
1777
1778 for_each_netdev(net, dev) {
1779 err = call_netdevice_register_notifiers(nb, dev);
1780 if (err)
1781 goto rollback;
1782 }
1783 return 0;
1784
1785rollback:
1786 for_each_netdev_continue_reverse(net, dev)
1787 call_netdevice_unregister_notifiers(nb, dev);
1788 return err;
1789}
1790
1791static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1792 struct net *net)
1793{
1794 struct net_device *dev;
1795
1796 for_each_netdev(net, dev)
1797 call_netdevice_unregister_notifiers(nb, dev);
1798}
1799
881d966b
EB
1800static int dev_boot_phase = 1;
1801
1da177e4 1802/**
722c9a0c 1803 * register_netdevice_notifier - register a network notifier block
1804 * @nb: notifier
1da177e4 1805 *
722c9a0c 1806 * Register a notifier to be called when network device events occur.
1807 * The notifier passed is linked into the kernel structures and must
1808 * not be reused until it has been unregistered. A negative errno code
1809 * is returned on a failure.
1da177e4 1810 *
722c9a0c 1811 * When registered all registration and up events are replayed
1812 * to the new notifier to allow device to have a race free
1813 * view of the network device list.
1da177e4
LT
1814 */
1815
1816int register_netdevice_notifier(struct notifier_block *nb)
1817{
881d966b 1818 struct net *net;
1da177e4
LT
1819 int err;
1820
328fbe74
KT
1821 /* Close race with setup_net() and cleanup_net() */
1822 down_write(&pernet_ops_rwsem);
1da177e4 1823 rtnl_lock();
f07d5b94 1824 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1825 if (err)
1826 goto unlock;
881d966b
EB
1827 if (dev_boot_phase)
1828 goto unlock;
1829 for_each_net(net) {
afa0df59
JP
1830 err = call_netdevice_register_net_notifiers(nb, net);
1831 if (err)
1832 goto rollback;
1da177e4 1833 }
fcc5a03a
HX
1834
1835unlock:
1da177e4 1836 rtnl_unlock();
328fbe74 1837 up_write(&pernet_ops_rwsem);
1da177e4 1838 return err;
fcc5a03a
HX
1839
1840rollback:
afa0df59
JP
1841 for_each_net_continue_reverse(net)
1842 call_netdevice_unregister_net_notifiers(nb, net);
c67625a1
PE
1843
1844 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1845 goto unlock;
1da177e4 1846}
d1b19dff 1847EXPORT_SYMBOL(register_netdevice_notifier);
1da177e4
LT
1848
1849/**
722c9a0c 1850 * unregister_netdevice_notifier - unregister a network notifier block
1851 * @nb: notifier
1da177e4 1852 *
722c9a0c 1853 * Unregister a notifier previously registered by
1854 * register_netdevice_notifier(). The notifier is unlinked into the
1855 * kernel structures and may then be reused. A negative errno code
1856 * is returned on a failure.
7d3d43da 1857 *
722c9a0c 1858 * After unregistering unregister and down device events are synthesized
1859 * for all devices on the device list to the removed notifier to remove
1860 * the need for special case cleanup code.
1da177e4
LT
1861 */
1862
1863int unregister_netdevice_notifier(struct notifier_block *nb)
1864{
7d3d43da 1865 struct net *net;
9f514950
HX
1866 int err;
1867
328fbe74
KT
1868 /* Close race with setup_net() and cleanup_net() */
1869 down_write(&pernet_ops_rwsem);
9f514950 1870 rtnl_lock();
f07d5b94 1871 err = raw_notifier_chain_unregister(&netdev_chain, nb);
7d3d43da
EB
1872 if (err)
1873 goto unlock;
1874
48b3a137
JP
1875 for_each_net(net)
1876 call_netdevice_unregister_net_notifiers(nb, net);
1877
7d3d43da 1878unlock:
9f514950 1879 rtnl_unlock();
328fbe74 1880 up_write(&pernet_ops_rwsem);
9f514950 1881 return err;
1da177e4 1882}
d1b19dff 1883EXPORT_SYMBOL(unregister_netdevice_notifier);
1da177e4 1884
1f637703
JP
1885static int __register_netdevice_notifier_net(struct net *net,
1886 struct notifier_block *nb,
1887 bool ignore_call_fail)
1888{
1889 int err;
1890
1891 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1892 if (err)
1893 return err;
1894 if (dev_boot_phase)
1895 return 0;
1896
1897 err = call_netdevice_register_net_notifiers(nb, net);
1898 if (err && !ignore_call_fail)
1899 goto chain_unregister;
1900
1901 return 0;
1902
1903chain_unregister:
1904 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1905 return err;
1906}
1907
1908static int __unregister_netdevice_notifier_net(struct net *net,
1909 struct notifier_block *nb)
1910{
1911 int err;
1912
1913 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1914 if (err)
1915 return err;
1916
1917 call_netdevice_unregister_net_notifiers(nb, net);
1918 return 0;
1919}
1920
a30c7b42
JP
1921/**
1922 * register_netdevice_notifier_net - register a per-netns network notifier block
1923 * @net: network namespace
1924 * @nb: notifier
1925 *
1926 * Register a notifier to be called when network device events occur.
1927 * The notifier passed is linked into the kernel structures and must
1928 * not be reused until it has been unregistered. A negative errno code
1929 * is returned on a failure.
1930 *
1931 * When registered all registration and up events are replayed
1932 * to the new notifier to allow device to have a race free
1933 * view of the network device list.
1934 */
1935
1936int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1937{
1938 int err;
1939
1940 rtnl_lock();
1f637703 1941 err = __register_netdevice_notifier_net(net, nb, false);
a30c7b42
JP
1942 rtnl_unlock();
1943 return err;
a30c7b42
JP
1944}
1945EXPORT_SYMBOL(register_netdevice_notifier_net);
1946
1947/**
1948 * unregister_netdevice_notifier_net - unregister a per-netns
1949 * network notifier block
1950 * @net: network namespace
1951 * @nb: notifier
1952 *
1953 * Unregister a notifier previously registered by
1954 * register_netdevice_notifier(). The notifier is unlinked into the
1955 * kernel structures and may then be reused. A negative errno code
1956 * is returned on a failure.
1957 *
1958 * After unregistering unregister and down device events are synthesized
1959 * for all devices on the device list to the removed notifier to remove
1960 * the need for special case cleanup code.
1961 */
1962
1963int unregister_netdevice_notifier_net(struct net *net,
1964 struct notifier_block *nb)
1965{
1966 int err;
1967
1968 rtnl_lock();
1f637703 1969 err = __unregister_netdevice_notifier_net(net, nb);
a30c7b42
JP
1970 rtnl_unlock();
1971 return err;
1972}
1973EXPORT_SYMBOL(unregister_netdevice_notifier_net);
a30c7b42 1974
93642e14
JP
1975int register_netdevice_notifier_dev_net(struct net_device *dev,
1976 struct notifier_block *nb,
1977 struct netdev_net_notifier *nn)
1978{
1979 int err;
a30c7b42 1980
93642e14
JP
1981 rtnl_lock();
1982 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1983 if (!err) {
1984 nn->nb = nb;
1985 list_add(&nn->list, &dev->net_notifier_list);
1986 }
a30c7b42
JP
1987 rtnl_unlock();
1988 return err;
1989}
93642e14
JP
1990EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1991
1992int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1993 struct notifier_block *nb,
1994 struct netdev_net_notifier *nn)
1995{
1996 int err;
1997
1998 rtnl_lock();
1999 list_del(&nn->list);
2000 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
2001 rtnl_unlock();
2002 return err;
2003}
2004EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
2005
2006static void move_netdevice_notifiers_dev_net(struct net_device *dev,
2007 struct net *net)
2008{
2009 struct netdev_net_notifier *nn;
2010
2011 list_for_each_entry(nn, &dev->net_notifier_list, list) {
2012 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
2013 __register_netdevice_notifier_net(net, nn->nb, true);
2014 }
2015}
a30c7b42 2016
351638e7
JP
2017/**
2018 * call_netdevice_notifiers_info - call all network notifier blocks
2019 * @val: value passed unmodified to notifier function
351638e7
JP
2020 * @info: notifier information data
2021 *
2022 * Call all network notifier blocks. Parameters and return value
2023 * are as for raw_notifier_call_chain().
2024 */
2025
1d143d9f 2026static int call_netdevice_notifiers_info(unsigned long val,
1d143d9f 2027 struct netdev_notifier_info *info)
351638e7 2028{
a30c7b42
JP
2029 struct net *net = dev_net(info->dev);
2030 int ret;
2031
351638e7 2032 ASSERT_RTNL();
a30c7b42
JP
2033
2034 /* Run per-netns notifier block chain first, then run the global one.
2035 * Hopefully, one day, the global one is going to be removed after
2036 * all notifier block registrators get converted to be per-netns.
2037 */
2038 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
2039 if (ret & NOTIFY_STOP_MASK)
2040 return ret;
351638e7
JP
2041 return raw_notifier_call_chain(&netdev_chain, val, info);
2042}
351638e7 2043
26372605
PM
2044static int call_netdevice_notifiers_extack(unsigned long val,
2045 struct net_device *dev,
2046 struct netlink_ext_ack *extack)
2047{
2048 struct netdev_notifier_info info = {
2049 .dev = dev,
2050 .extack = extack,
2051 };
2052
2053 return call_netdevice_notifiers_info(val, &info);
2054}
2055
1da177e4
LT
2056/**
2057 * call_netdevice_notifiers - call all network notifier blocks
2058 * @val: value passed unmodified to notifier function
c4ea43c5 2059 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
2060 *
2061 * Call all network notifier blocks. Parameters and return value
f07d5b94 2062 * are as for raw_notifier_call_chain().
1da177e4
LT
2063 */
2064
ad7379d4 2065int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 2066{
26372605 2067 return call_netdevice_notifiers_extack(val, dev, NULL);
1da177e4 2068}
edf947f1 2069EXPORT_SYMBOL(call_netdevice_notifiers);
1da177e4 2070
af7d6cce
SD
2071/**
2072 * call_netdevice_notifiers_mtu - call all network notifier blocks
2073 * @val: value passed unmodified to notifier function
2074 * @dev: net_device pointer passed unmodified to notifier function
2075 * @arg: additional u32 argument passed to the notifier function
2076 *
2077 * Call all network notifier blocks. Parameters and return value
2078 * are as for raw_notifier_call_chain().
2079 */
2080static int call_netdevice_notifiers_mtu(unsigned long val,
2081 struct net_device *dev, u32 arg)
2082{
2083 struct netdev_notifier_info_ext info = {
2084 .info.dev = dev,
2085 .ext.mtu = arg,
2086 };
2087
2088 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
2089
2090 return call_netdevice_notifiers_info(val, &info.info);
2091}
2092
1cf51900 2093#ifdef CONFIG_NET_INGRESS
aabf6772 2094static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
4577139b
DB
2095
2096void net_inc_ingress_queue(void)
2097{
aabf6772 2098 static_branch_inc(&ingress_needed_key);
4577139b
DB
2099}
2100EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2101
2102void net_dec_ingress_queue(void)
2103{
aabf6772 2104 static_branch_dec(&ingress_needed_key);
4577139b
DB
2105}
2106EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2107#endif
2108
1f211a1b 2109#ifdef CONFIG_NET_EGRESS
aabf6772 2110static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
1f211a1b
DB
2111
2112void net_inc_egress_queue(void)
2113{
aabf6772 2114 static_branch_inc(&egress_needed_key);
1f211a1b
DB
2115}
2116EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2117
2118void net_dec_egress_queue(void)
2119{
aabf6772 2120 static_branch_dec(&egress_needed_key);
1f211a1b
DB
2121}
2122EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2123#endif
2124
39e83922 2125static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
e9666d10 2126#ifdef CONFIG_JUMP_LABEL
b90e5794 2127static atomic_t netstamp_needed_deferred;
13baa00a 2128static atomic_t netstamp_wanted;
5fa8bbda 2129static void netstamp_clear(struct work_struct *work)
1da177e4 2130{
b90e5794 2131 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
13baa00a 2132 int wanted;
b90e5794 2133
13baa00a
ED
2134 wanted = atomic_add_return(deferred, &netstamp_wanted);
2135 if (wanted > 0)
39e83922 2136 static_branch_enable(&netstamp_needed_key);
13baa00a 2137 else
39e83922 2138 static_branch_disable(&netstamp_needed_key);
5fa8bbda
ED
2139}
2140static DECLARE_WORK(netstamp_work, netstamp_clear);
b90e5794 2141#endif
5fa8bbda
ED
2142
2143void net_enable_timestamp(void)
2144{
e9666d10 2145#ifdef CONFIG_JUMP_LABEL
13baa00a
ED
2146 int wanted;
2147
2148 while (1) {
2149 wanted = atomic_read(&netstamp_wanted);
2150 if (wanted <= 0)
2151 break;
2152 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2153 return;
2154 }
2155 atomic_inc(&netstamp_needed_deferred);
2156 schedule_work(&netstamp_work);
2157#else
39e83922 2158 static_branch_inc(&netstamp_needed_key);
13baa00a 2159#endif
1da177e4 2160}
d1b19dff 2161EXPORT_SYMBOL(net_enable_timestamp);
1da177e4
LT
2162
2163void net_disable_timestamp(void)
2164{
e9666d10 2165#ifdef CONFIG_JUMP_LABEL
13baa00a
ED
2166 int wanted;
2167
2168 while (1) {
2169 wanted = atomic_read(&netstamp_wanted);
2170 if (wanted <= 1)
2171 break;
2172 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2173 return;
2174 }
2175 atomic_dec(&netstamp_needed_deferred);
5fa8bbda
ED
2176 schedule_work(&netstamp_work);
2177#else
39e83922 2178 static_branch_dec(&netstamp_needed_key);
5fa8bbda 2179#endif
1da177e4 2180}
d1b19dff 2181EXPORT_SYMBOL(net_disable_timestamp);
1da177e4 2182
3b098e2d 2183static inline void net_timestamp_set(struct sk_buff *skb)
1da177e4 2184{
2456e855 2185 skb->tstamp = 0;
39e83922 2186 if (static_branch_unlikely(&netstamp_needed_key))
a61bbcf2 2187 __net_timestamp(skb);
1da177e4
LT
2188}
2189
39e83922
DB
2190#define net_timestamp_check(COND, SKB) \
2191 if (static_branch_unlikely(&netstamp_needed_key)) { \
2192 if ((COND) && !(SKB)->tstamp) \
2193 __net_timestamp(SKB); \
2194 } \
3b098e2d 2195
f4b05d27 2196bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
79b569f0
DL
2197{
2198 unsigned int len;
2199
2200 if (!(dev->flags & IFF_UP))
2201 return false;
2202
2203 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2204 if (skb->len <= len)
2205 return true;
2206
2207 /* if TSO is enabled, we don't care about the length as the packet
2208 * could be forwarded without being segmented before
2209 */
2210 if (skb_is_gso(skb))
2211 return true;
2212
2213 return false;
2214}
1ee481fb 2215EXPORT_SYMBOL_GPL(is_skb_forwardable);
79b569f0 2216
a0265d28
HX
2217int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2218{
4e3264d2 2219 int ret = ____dev_forward_skb(dev, skb);
a0265d28 2220
4e3264d2
MKL
2221 if (likely(!ret)) {
2222 skb->protocol = eth_type_trans(skb, dev);
2223 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
2224 }
a0265d28 2225
4e3264d2 2226 return ret;
a0265d28
HX
2227}
2228EXPORT_SYMBOL_GPL(__dev_forward_skb);
2229
44540960
AB
2230/**
2231 * dev_forward_skb - loopback an skb to another netif
2232 *
2233 * @dev: destination network device
2234 * @skb: buffer to forward
2235 *
2236 * return values:
2237 * NET_RX_SUCCESS (no congestion)
6ec82562 2238 * NET_RX_DROP (packet was dropped, but freed)
44540960
AB
2239 *
2240 * dev_forward_skb can be used for injecting an skb from the
2241 * start_xmit function of one device into the receive queue
2242 * of another device.
2243 *
2244 * The receiving device may be in another namespace, so
2245 * we have to clear all information in the skb that could
2246 * impact namespace isolation.
2247 */
2248int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2249{
a0265d28 2250 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
44540960
AB
2251}
2252EXPORT_SYMBOL_GPL(dev_forward_skb);
2253
71d9dec2
CG
2254static inline int deliver_skb(struct sk_buff *skb,
2255 struct packet_type *pt_prev,
2256 struct net_device *orig_dev)
2257{
1f8b977a 2258 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
1080e512 2259 return -ENOMEM;
63354797 2260 refcount_inc(&skb->users);
71d9dec2
CG
2261 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2262}
2263
7866a621
SN
2264static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2265 struct packet_type **pt,
fbcb2170
JP
2266 struct net_device *orig_dev,
2267 __be16 type,
7866a621
SN
2268 struct list_head *ptype_list)
2269{
2270 struct packet_type *ptype, *pt_prev = *pt;
2271
2272 list_for_each_entry_rcu(ptype, ptype_list, list) {
2273 if (ptype->type != type)
2274 continue;
2275 if (pt_prev)
fbcb2170 2276 deliver_skb(skb, pt_prev, orig_dev);
7866a621
SN
2277 pt_prev = ptype;
2278 }
2279 *pt = pt_prev;
2280}
2281
c0de08d0
EL
2282static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2283{
a3d744e9 2284 if (!ptype->af_packet_priv || !skb->sk)
c0de08d0
EL
2285 return false;
2286
2287 if (ptype->id_match)
2288 return ptype->id_match(ptype, skb->sk);
2289 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2290 return true;
2291
2292 return false;
2293}
2294
9f9a742d
MR
2295/**
2296 * dev_nit_active - return true if any network interface taps are in use
2297 *
2298 * @dev: network device to check for the presence of taps
2299 */
2300bool dev_nit_active(struct net_device *dev)
2301{
2302 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2303}
2304EXPORT_SYMBOL_GPL(dev_nit_active);
2305
1da177e4
LT
2306/*
2307 * Support routine. Sends outgoing frames to any network
2308 * taps currently in use.
2309 */
2310
74b20582 2311void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
2312{
2313 struct packet_type *ptype;
71d9dec2
CG
2314 struct sk_buff *skb2 = NULL;
2315 struct packet_type *pt_prev = NULL;
7866a621 2316 struct list_head *ptype_list = &ptype_all;
a61bbcf2 2317
1da177e4 2318 rcu_read_lock();
7866a621
SN
2319again:
2320 list_for_each_entry_rcu(ptype, ptype_list, list) {
fa788d98
VW
2321 if (ptype->ignore_outgoing)
2322 continue;
2323
1da177e4
LT
2324 /* Never send packets back to the socket
2325 * they originated from - MvS (miquels@drinkel.ow.org)
2326 */
7866a621
SN
2327 if (skb_loop_sk(ptype, skb))
2328 continue;
71d9dec2 2329
7866a621
SN
2330 if (pt_prev) {
2331 deliver_skb(skb2, pt_prev, skb->dev);
2332 pt_prev = ptype;
2333 continue;
2334 }
1da177e4 2335
7866a621
SN
2336 /* need to clone skb, done only once */
2337 skb2 = skb_clone(skb, GFP_ATOMIC);
2338 if (!skb2)
2339 goto out_unlock;
70978182 2340
7866a621 2341 net_timestamp_set(skb2);
1da177e4 2342
7866a621
SN
2343 /* skb->nh should be correctly
2344 * set by sender, so that the second statement is
2345 * just protection against buggy protocols.
2346 */
2347 skb_reset_mac_header(skb2);
2348
2349 if (skb_network_header(skb2) < skb2->data ||
2350 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2351 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2352 ntohs(skb2->protocol),
2353 dev->name);
2354 skb_reset_network_header(skb2);
1da177e4 2355 }
7866a621
SN
2356
2357 skb2->transport_header = skb2->network_header;
2358 skb2->pkt_type = PACKET_OUTGOING;
2359 pt_prev = ptype;
2360 }
2361
2362 if (ptype_list == &ptype_all) {
2363 ptype_list = &dev->ptype_all;
2364 goto again;
1da177e4 2365 }
7866a621 2366out_unlock:
581fe0ea
WB
2367 if (pt_prev) {
2368 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2369 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2370 else
2371 kfree_skb(skb2);
2372 }
1da177e4
LT
2373 rcu_read_unlock();
2374}
74b20582 2375EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
1da177e4 2376
2c53040f
BH
2377/**
2378 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
4f57c087
JF
2379 * @dev: Network device
2380 * @txq: number of queues available
2381 *
2382 * If real_num_tx_queues is changed the tc mappings may no longer be
2383 * valid. To resolve this verify the tc mapping remains valid and if
2384 * not NULL the mapping. With no priorities mapping to this
2385 * offset/count pair it will no longer be used. In the worst case TC0
2386 * is invalid nothing can be done so disable priority mappings. If is
2387 * expected that drivers will fix this mapping if they can before
2388 * calling netif_set_real_num_tx_queues.
2389 */
bb134d22 2390static void netif_setup_tc(struct net_device *dev, unsigned int txq)
4f57c087
JF
2391{
2392 int i;
2393 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2394
2395 /* If TC0 is invalidated disable TC mapping */
2396 if (tc->offset + tc->count > txq) {
7b6cd1ce 2397 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
4f57c087
JF
2398 dev->num_tc = 0;
2399 return;
2400 }
2401
2402 /* Invalidated prio to tc mappings set to TC0 */
2403 for (i = 1; i < TC_BITMASK + 1; i++) {
2404 int q = netdev_get_prio_tc_map(dev, i);
2405
2406 tc = &dev->tc_to_txq[q];
2407 if (tc->offset + tc->count > txq) {
7b6cd1ce
JP
2408 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2409 i, q);
4f57c087
JF
2410 netdev_set_prio_tc_map(dev, i, 0);
2411 }
2412 }
2413}
2414
8d059b0f
AD
2415int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2416{
2417 if (dev->num_tc) {
2418 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2419 int i;
2420
ffcfe25b 2421 /* walk through the TCs and see if it falls into any of them */
8d059b0f
AD
2422 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2423 if ((txq - tc->offset) < tc->count)
2424 return i;
2425 }
2426
ffcfe25b 2427 /* didn't find it, just return -1 to indicate no match */
8d059b0f
AD
2428 return -1;
2429 }
2430
2431 return 0;
2432}
8a5f2166 2433EXPORT_SYMBOL(netdev_txq_to_tc);
8d059b0f 2434
537c00de 2435#ifdef CONFIG_XPS
04157469
AN
2436struct static_key xps_needed __read_mostly;
2437EXPORT_SYMBOL(xps_needed);
2438struct static_key xps_rxqs_needed __read_mostly;
2439EXPORT_SYMBOL(xps_rxqs_needed);
537c00de
AD
2440static DEFINE_MUTEX(xps_map_mutex);
2441#define xmap_dereference(P) \
2442 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2443
6234f874
AD
2444static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2445 int tci, u16 index)
537c00de 2446{
10cdc3f3
AD
2447 struct xps_map *map = NULL;
2448 int pos;
537c00de 2449
10cdc3f3 2450 if (dev_maps)
80d19669 2451 map = xmap_dereference(dev_maps->attr_map[tci]);
6234f874
AD
2452 if (!map)
2453 return false;
537c00de 2454
6234f874
AD
2455 for (pos = map->len; pos--;) {
2456 if (map->queues[pos] != index)
2457 continue;
2458
2459 if (map->len > 1) {
2460 map->queues[pos] = map->queues[--map->len];
10cdc3f3 2461 break;
537c00de 2462 }
6234f874 2463
80d19669 2464 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
6234f874
AD
2465 kfree_rcu(map, rcu);
2466 return false;
537c00de
AD
2467 }
2468
6234f874 2469 return true;
10cdc3f3
AD
2470}
2471
6234f874
AD
2472static bool remove_xps_queue_cpu(struct net_device *dev,
2473 struct xps_dev_maps *dev_maps,
2474 int cpu, u16 offset, u16 count)
2475{
184c449f
AD
2476 int num_tc = dev->num_tc ? : 1;
2477 bool active = false;
2478 int tci;
6234f874 2479
184c449f
AD
2480 for (tci = cpu * num_tc; num_tc--; tci++) {
2481 int i, j;
2482
2483 for (i = count, j = offset; i--; j++) {
6358d49a 2484 if (!remove_xps_queue(dev_maps, tci, j))
184c449f
AD
2485 break;
2486 }
2487
2488 active |= i < 0;
6234f874
AD
2489 }
2490
184c449f 2491 return active;
6234f874
AD
2492}
2493
867d0ad4
SD
2494static void reset_xps_maps(struct net_device *dev,
2495 struct xps_dev_maps *dev_maps,
2496 bool is_rxqs_map)
2497{
2498 if (is_rxqs_map) {
2499 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2500 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2501 } else {
2502 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2503 }
2504 static_key_slow_dec_cpuslocked(&xps_needed);
2505 kfree_rcu(dev_maps, rcu);
2506}
2507
80d19669
AN
2508static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2509 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2510 u16 offset, u16 count, bool is_rxqs_map)
2511{
2512 bool active = false;
2513 int i, j;
2514
2515 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2516 j < nr_ids;)
2517 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2518 count);
867d0ad4
SD
2519 if (!active)
2520 reset_xps_maps(dev, dev_maps, is_rxqs_map);
80d19669 2521
f28c020f
SD
2522 if (!is_rxqs_map) {
2523 for (i = offset + (count - 1); count--; i--) {
2524 netdev_queue_numa_node_write(
2525 netdev_get_tx_queue(dev, i),
2526 NUMA_NO_NODE);
80d19669 2527 }
80d19669
AN
2528 }
2529}
2530
6234f874
AD
2531static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2532 u16 count)
10cdc3f3 2533{
80d19669 2534 const unsigned long *possible_mask = NULL;
10cdc3f3 2535 struct xps_dev_maps *dev_maps;
80d19669 2536 unsigned int nr_ids;
10cdc3f3 2537
04157469
AN
2538 if (!static_key_false(&xps_needed))
2539 return;
10cdc3f3 2540
4d99f660 2541 cpus_read_lock();
04157469 2542 mutex_lock(&xps_map_mutex);
10cdc3f3 2543
04157469
AN
2544 if (static_key_false(&xps_rxqs_needed)) {
2545 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2546 if (dev_maps) {
2547 nr_ids = dev->num_rx_queues;
2548 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2549 offset, count, true);
2550 }
537c00de
AD
2551 }
2552
80d19669
AN
2553 dev_maps = xmap_dereference(dev->xps_cpus_map);
2554 if (!dev_maps)
2555 goto out_no_maps;
2556
2557 if (num_possible_cpus() > 1)
2558 possible_mask = cpumask_bits(cpu_possible_mask);
2559 nr_ids = nr_cpu_ids;
2560 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2561 false);
024e9679 2562
537c00de
AD
2563out_no_maps:
2564 mutex_unlock(&xps_map_mutex);
4d99f660 2565 cpus_read_unlock();
537c00de
AD
2566}
2567
6234f874
AD
2568static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2569{
2570 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2571}
2572
80d19669
AN
2573static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2574 u16 index, bool is_rxqs_map)
01c5f864
AD
2575{
2576 struct xps_map *new_map;
2577 int alloc_len = XPS_MIN_MAP_ALLOC;
2578 int i, pos;
2579
2580 for (pos = 0; map && pos < map->len; pos++) {
2581 if (map->queues[pos] != index)
2582 continue;
2583 return map;
2584 }
2585
80d19669 2586 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
01c5f864
AD
2587 if (map) {
2588 if (pos < map->alloc_len)
2589 return map;
2590
2591 alloc_len = map->alloc_len * 2;
2592 }
2593
80d19669
AN
2594 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2595 * map
2596 */
2597 if (is_rxqs_map)
2598 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2599 else
2600 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2601 cpu_to_node(attr_index));
01c5f864
AD
2602 if (!new_map)
2603 return NULL;
2604
2605 for (i = 0; i < pos; i++)
2606 new_map->queues[i] = map->queues[i];
2607 new_map->alloc_len = alloc_len;
2608 new_map->len = pos;
2609
2610 return new_map;
2611}
2612
4d99f660 2613/* Must be called under cpus_read_lock */
80d19669
AN
2614int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2615 u16 index, bool is_rxqs_map)
537c00de 2616{
80d19669 2617 const unsigned long *online_mask = NULL, *possible_mask = NULL;
01c5f864 2618 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
80d19669 2619 int i, j, tci, numa_node_id = -2;
184c449f 2620 int maps_sz, num_tc = 1, tc = 0;
537c00de 2621 struct xps_map *map, *new_map;
01c5f864 2622 bool active = false;
80d19669 2623 unsigned int nr_ids;
537c00de 2624
184c449f 2625 if (dev->num_tc) {
ffcfe25b 2626 /* Do not allow XPS on subordinate device directly */
184c449f 2627 num_tc = dev->num_tc;
ffcfe25b
AD
2628 if (num_tc < 0)
2629 return -EINVAL;
2630
2631 /* If queue belongs to subordinate dev use its map */
2632 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2633
184c449f
AD
2634 tc = netdev_txq_to_tc(dev, index);
2635 if (tc < 0)
2636 return -EINVAL;
2637 }
2638
537c00de 2639 mutex_lock(&xps_map_mutex);
80d19669
AN
2640 if (is_rxqs_map) {
2641 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2642 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2643 nr_ids = dev->num_rx_queues;
2644 } else {
2645 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2646 if (num_possible_cpus() > 1) {
2647 online_mask = cpumask_bits(cpu_online_mask);
2648 possible_mask = cpumask_bits(cpu_possible_mask);
2649 }
2650 dev_maps = xmap_dereference(dev->xps_cpus_map);
2651 nr_ids = nr_cpu_ids;
2652 }
537c00de 2653
80d19669
AN
2654 if (maps_sz < L1_CACHE_BYTES)
2655 maps_sz = L1_CACHE_BYTES;
537c00de 2656
01c5f864 2657 /* allocate memory for queue storage */
80d19669
AN
2658 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2659 j < nr_ids;) {
01c5f864
AD
2660 if (!new_dev_maps)
2661 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2bb60cb9
AD
2662 if (!new_dev_maps) {
2663 mutex_unlock(&xps_map_mutex);
01c5f864 2664 return -ENOMEM;
2bb60cb9 2665 }
01c5f864 2666
80d19669
AN
2667 tci = j * num_tc + tc;
2668 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
01c5f864
AD
2669 NULL;
2670
80d19669 2671 map = expand_xps_map(map, j, index, is_rxqs_map);
01c5f864
AD
2672 if (!map)
2673 goto error;
2674
80d19669 2675 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
01c5f864
AD
2676 }
2677
2678 if (!new_dev_maps)
2679 goto out_no_new_maps;
2680
867d0ad4
SD
2681 if (!dev_maps) {
2682 /* Increment static keys at most once per type */
2683 static_key_slow_inc_cpuslocked(&xps_needed);
2684 if (is_rxqs_map)
2685 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2686 }
04157469 2687
80d19669
AN
2688 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2689 j < nr_ids;) {
184c449f 2690 /* copy maps belonging to foreign traffic classes */
80d19669 2691 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
184c449f 2692 /* fill in the new device map from the old device map */
80d19669
AN
2693 map = xmap_dereference(dev_maps->attr_map[tci]);
2694 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
184c449f
AD
2695 }
2696
2697 /* We need to explicitly update tci as prevous loop
2698 * could break out early if dev_maps is NULL.
2699 */
80d19669 2700 tci = j * num_tc + tc;
184c449f 2701
80d19669
AN
2702 if (netif_attr_test_mask(j, mask, nr_ids) &&
2703 netif_attr_test_online(j, online_mask, nr_ids)) {
2704 /* add tx-queue to CPU/rx-queue maps */
01c5f864
AD
2705 int pos = 0;
2706
80d19669 2707 map = xmap_dereference(new_dev_maps->attr_map[tci]);
01c5f864
AD
2708 while ((pos < map->len) && (map->queues[pos] != index))
2709 pos++;
2710
2711 if (pos == map->len)
2712 map->queues[map->len++] = index;
537c00de 2713#ifdef CONFIG_NUMA
80d19669
AN
2714 if (!is_rxqs_map) {
2715 if (numa_node_id == -2)
2716 numa_node_id = cpu_to_node(j);
2717 else if (numa_node_id != cpu_to_node(j))
2718 numa_node_id = -1;
2719 }
537c00de 2720#endif
01c5f864
AD
2721 } else if (dev_maps) {
2722 /* fill in the new device map from the old device map */
80d19669
AN
2723 map = xmap_dereference(dev_maps->attr_map[tci]);
2724 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
537c00de 2725 }
01c5f864 2726
184c449f
AD
2727 /* copy maps belonging to foreign traffic classes */
2728 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2729 /* fill in the new device map from the old device map */
80d19669
AN
2730 map = xmap_dereference(dev_maps->attr_map[tci]);
2731 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
184c449f 2732 }
537c00de
AD
2733 }
2734
80d19669
AN
2735 if (is_rxqs_map)
2736 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2737 else
2738 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
01c5f864 2739
537c00de 2740 /* Cleanup old maps */
184c449f
AD
2741 if (!dev_maps)
2742 goto out_no_old_maps;
2743
80d19669
AN
2744 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2745 j < nr_ids;) {
2746 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2747 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2748 map = xmap_dereference(dev_maps->attr_map[tci]);
01c5f864
AD
2749 if (map && map != new_map)
2750 kfree_rcu(map, rcu);
2751 }
537c00de
AD
2752 }
2753
184c449f
AD
2754 kfree_rcu(dev_maps, rcu);
2755
2756out_no_old_maps:
01c5f864
AD
2757 dev_maps = new_dev_maps;
2758 active = true;
537c00de 2759
01c5f864 2760out_no_new_maps:
80d19669
AN
2761 if (!is_rxqs_map) {
2762 /* update Tx queue numa node */
2763 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2764 (numa_node_id >= 0) ?
2765 numa_node_id : NUMA_NO_NODE);
2766 }
537c00de 2767
01c5f864
AD
2768 if (!dev_maps)
2769 goto out_no_maps;
2770
80d19669
AN
2771 /* removes tx-queue from unused CPUs/rx-queues */
2772 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2773 j < nr_ids;) {
2774 for (i = tc, tci = j * num_tc; i--; tci++)
184c449f 2775 active |= remove_xps_queue(dev_maps, tci, index);
80d19669
AN
2776 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2777 !netif_attr_test_online(j, online_mask, nr_ids))
184c449f
AD
2778 active |= remove_xps_queue(dev_maps, tci, index);
2779 for (i = num_tc - tc, tci++; --i; tci++)
2780 active |= remove_xps_queue(dev_maps, tci, index);
01c5f864
AD
2781 }
2782
2783 /* free map if not active */
867d0ad4
SD
2784 if (!active)
2785 reset_xps_maps(dev, dev_maps, is_rxqs_map);
01c5f864
AD
2786
2787out_no_maps:
537c00de
AD
2788 mutex_unlock(&xps_map_mutex);
2789
2790 return 0;
2791error:
01c5f864 2792 /* remove any maps that we added */
80d19669
AN
2793 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2794 j < nr_ids;) {
2795 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2796 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
184c449f 2797 map = dev_maps ?
80d19669 2798 xmap_dereference(dev_maps->attr_map[tci]) :
184c449f
AD
2799 NULL;
2800 if (new_map && new_map != map)
2801 kfree(new_map);
2802 }
01c5f864
AD
2803 }
2804
537c00de
AD
2805 mutex_unlock(&xps_map_mutex);
2806
537c00de
AD
2807 kfree(new_dev_maps);
2808 return -ENOMEM;
2809}
4d99f660 2810EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
80d19669
AN
2811
2812int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2813 u16 index)
2814{
4d99f660
AV
2815 int ret;
2816
2817 cpus_read_lock();
2818 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2819 cpus_read_unlock();
2820
2821 return ret;
80d19669 2822}
537c00de
AD
2823EXPORT_SYMBOL(netif_set_xps_queue);
2824
2825#endif
ffcfe25b
AD
2826static void netdev_unbind_all_sb_channels(struct net_device *dev)
2827{
2828 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2829
2830 /* Unbind any subordinate channels */
2831 while (txq-- != &dev->_tx[0]) {
2832 if (txq->sb_dev)
2833 netdev_unbind_sb_channel(dev, txq->sb_dev);
2834 }
2835}
2836
9cf1f6a8
AD
2837void netdev_reset_tc(struct net_device *dev)
2838{
6234f874
AD
2839#ifdef CONFIG_XPS
2840 netif_reset_xps_queues_gt(dev, 0);
2841#endif
ffcfe25b
AD
2842 netdev_unbind_all_sb_channels(dev);
2843
2844 /* Reset TC configuration of device */
9cf1f6a8
AD
2845 dev->num_tc = 0;
2846 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2847 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2848}
2849EXPORT_SYMBOL(netdev_reset_tc);
2850
2851int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2852{
2853 if (tc >= dev->num_tc)
2854 return -EINVAL;
2855
6234f874
AD
2856#ifdef CONFIG_XPS
2857 netif_reset_xps_queues(dev, offset, count);
2858#endif
9cf1f6a8
AD
2859 dev->tc_to_txq[tc].count = count;
2860 dev->tc_to_txq[tc].offset = offset;
2861 return 0;
2862}
2863EXPORT_SYMBOL(netdev_set_tc_queue);
2864
2865int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2866{
2867 if (num_tc > TC_MAX_QUEUE)
2868 return -EINVAL;
2869
6234f874
AD
2870#ifdef CONFIG_XPS
2871 netif_reset_xps_queues_gt(dev, 0);
2872#endif
ffcfe25b
AD
2873 netdev_unbind_all_sb_channels(dev);
2874
9cf1f6a8
AD
2875 dev->num_tc = num_tc;
2876 return 0;
2877}
2878EXPORT_SYMBOL(netdev_set_num_tc);
2879
ffcfe25b
AD
2880void netdev_unbind_sb_channel(struct net_device *dev,
2881 struct net_device *sb_dev)
2882{
2883 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2884
2885#ifdef CONFIG_XPS
2886 netif_reset_xps_queues_gt(sb_dev, 0);
2887#endif
2888 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2889 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2890
2891 while (txq-- != &dev->_tx[0]) {
2892 if (txq->sb_dev == sb_dev)
2893 txq->sb_dev = NULL;
2894 }
2895}
2896EXPORT_SYMBOL(netdev_unbind_sb_channel);
2897
2898int netdev_bind_sb_channel_queue(struct net_device *dev,
2899 struct net_device *sb_dev,
2900 u8 tc, u16 count, u16 offset)
2901{
2902 /* Make certain the sb_dev and dev are already configured */
2903 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2904 return -EINVAL;
2905
2906 /* We cannot hand out queues we don't have */
2907 if ((offset + count) > dev->real_num_tx_queues)
2908 return -EINVAL;
2909
2910 /* Record the mapping */
2911 sb_dev->tc_to_txq[tc].count = count;
2912 sb_dev->tc_to_txq[tc].offset = offset;
2913
2914 /* Provide a way for Tx queue to find the tc_to_txq map or
2915 * XPS map for itself.
2916 */
2917 while (count--)
2918 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2919
2920 return 0;
2921}
2922EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2923
2924int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2925{
2926 /* Do not use a multiqueue device to represent a subordinate channel */
2927 if (netif_is_multiqueue(dev))
2928 return -ENODEV;
2929
2930 /* We allow channels 1 - 32767 to be used for subordinate channels.
2931 * Channel 0 is meant to be "native" mode and used only to represent
2932 * the main root device. We allow writing 0 to reset the device back
2933 * to normal mode after being used as a subordinate channel.
2934 */
2935 if (channel > S16_MAX)
2936 return -EINVAL;
2937
2938 dev->num_tc = -channel;
2939
2940 return 0;
2941}
2942EXPORT_SYMBOL(netdev_set_sb_channel);
2943
f0796d5c
JF
2944/*
2945 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
3a053b1a 2946 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
f0796d5c 2947 */
e6484930 2948int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
f0796d5c 2949{
ac5b7019 2950 bool disabling;
1d24eb48
TH
2951 int rc;
2952
ac5b7019
JK
2953 disabling = txq < dev->real_num_tx_queues;
2954
e6484930
TH
2955 if (txq < 1 || txq > dev->num_tx_queues)
2956 return -EINVAL;
f0796d5c 2957
5c56580b
BH
2958 if (dev->reg_state == NETREG_REGISTERED ||
2959 dev->reg_state == NETREG_UNREGISTERING) {
e6484930
TH
2960 ASSERT_RTNL();
2961
1d24eb48
TH
2962 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2963 txq);
bf264145
TH
2964 if (rc)
2965 return rc;
2966
4f57c087
JF
2967 if (dev->num_tc)
2968 netif_setup_tc(dev, txq);
2969
ac5b7019
JK
2970 dev->real_num_tx_queues = txq;
2971
2972 if (disabling) {
2973 synchronize_net();
e6484930 2974 qdisc_reset_all_tx_gt(dev, txq);
024e9679
AD
2975#ifdef CONFIG_XPS
2976 netif_reset_xps_queues_gt(dev, txq);
2977#endif
2978 }
ac5b7019
JK
2979 } else {
2980 dev->real_num_tx_queues = txq;
f0796d5c 2981 }
e6484930 2982
e6484930 2983 return 0;
f0796d5c
JF
2984}
2985EXPORT_SYMBOL(netif_set_real_num_tx_queues);
56079431 2986
a953be53 2987#ifdef CONFIG_SYSFS
62fe0b40
BH
2988/**
2989 * netif_set_real_num_rx_queues - set actual number of RX queues used
2990 * @dev: Network device
2991 * @rxq: Actual number of RX queues
2992 *
2993 * This must be called either with the rtnl_lock held or before
2994 * registration of the net device. Returns 0 on success, or a
4e7f7951
BH
2995 * negative error code. If called before registration, it always
2996 * succeeds.
62fe0b40
BH
2997 */
2998int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2999{
3000 int rc;
3001
bd25fa7b
TH
3002 if (rxq < 1 || rxq > dev->num_rx_queues)
3003 return -EINVAL;
3004
62fe0b40
BH
3005 if (dev->reg_state == NETREG_REGISTERED) {
3006 ASSERT_RTNL();
3007
62fe0b40
BH
3008 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
3009 rxq);
3010 if (rc)
3011 return rc;
62fe0b40
BH
3012 }
3013
3014 dev->real_num_rx_queues = rxq;
3015 return 0;
3016}
3017EXPORT_SYMBOL(netif_set_real_num_rx_queues);
3018#endif
3019
2c53040f
BH
3020/**
3021 * netif_get_num_default_rss_queues - default number of RSS queues
16917b87
YM
3022 *
3023 * This routine should set an upper limit on the number of RSS queues
3024 * used by default by multiqueue devices.
3025 */
a55b138b 3026int netif_get_num_default_rss_queues(void)
16917b87 3027{
40e4e713
HS
3028 return is_kdump_kernel() ?
3029 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
16917b87
YM
3030}
3031EXPORT_SYMBOL(netif_get_num_default_rss_queues);
3032
3bcb846c 3033static void __netif_reschedule(struct Qdisc *q)
56079431 3034{
def82a1d
JP
3035 struct softnet_data *sd;
3036 unsigned long flags;
56079431 3037
def82a1d 3038 local_irq_save(flags);
903ceff7 3039 sd = this_cpu_ptr(&softnet_data);
a9cbd588
CG
3040 q->next_sched = NULL;
3041 *sd->output_queue_tailp = q;
3042 sd->output_queue_tailp = &q->next_sched;
def82a1d
JP
3043 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3044 local_irq_restore(flags);
3045}
3046
3047void __netif_schedule(struct Qdisc *q)
3048{
3049 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
3050 __netif_reschedule(q);
56079431
DV
3051}
3052EXPORT_SYMBOL(__netif_schedule);
3053
e6247027
ED
3054struct dev_kfree_skb_cb {
3055 enum skb_free_reason reason;
3056};
3057
3058static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
56079431 3059{
e6247027
ED
3060 return (struct dev_kfree_skb_cb *)skb->cb;
3061}
3062
46e5da40
JF
3063void netif_schedule_queue(struct netdev_queue *txq)
3064{
3065 rcu_read_lock();
5be5515a 3066 if (!netif_xmit_stopped(txq)) {
46e5da40
JF
3067 struct Qdisc *q = rcu_dereference(txq->qdisc);
3068
3069 __netif_schedule(q);
3070 }
3071 rcu_read_unlock();
3072}
3073EXPORT_SYMBOL(netif_schedule_queue);
3074
46e5da40
JF
3075void netif_tx_wake_queue(struct netdev_queue *dev_queue)
3076{
3077 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3078 struct Qdisc *q;
3079
3080 rcu_read_lock();
3081 q = rcu_dereference(dev_queue->qdisc);
3082 __netif_schedule(q);
3083 rcu_read_unlock();
3084 }
3085}
3086EXPORT_SYMBOL(netif_tx_wake_queue);
3087
e6247027 3088void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
56079431 3089{
e6247027 3090 unsigned long flags;
56079431 3091
9899886d
MJ
3092 if (unlikely(!skb))
3093 return;
3094
63354797 3095 if (likely(refcount_read(&skb->users) == 1)) {
e6247027 3096 smp_rmb();
63354797
RE
3097 refcount_set(&skb->users, 0);
3098 } else if (likely(!refcount_dec_and_test(&skb->users))) {
e6247027 3099 return;
bea3348e 3100 }
e6247027
ED
3101 get_kfree_skb_cb(skb)->reason = reason;
3102 local_irq_save(flags);
3103 skb->next = __this_cpu_read(softnet_data.completion_queue);
3104 __this_cpu_write(softnet_data.completion_queue, skb);
3105 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3106 local_irq_restore(flags);
56079431 3107}
e6247027 3108EXPORT_SYMBOL(__dev_kfree_skb_irq);
56079431 3109
e6247027 3110void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
56079431
DV
3111{
3112 if (in_irq() || irqs_disabled())
e6247027 3113 __dev_kfree_skb_irq(skb, reason);
56079431
DV
3114 else
3115 dev_kfree_skb(skb);
3116}
e6247027 3117EXPORT_SYMBOL(__dev_kfree_skb_any);
56079431
DV
3118
3119
bea3348e
SH
3120/**
3121 * netif_device_detach - mark device as removed
3122 * @dev: network device
3123 *
3124 * Mark device as removed from system and therefore no longer available.
3125 */
56079431
DV
3126void netif_device_detach(struct net_device *dev)
3127{
3128 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3129 netif_running(dev)) {
d543103a 3130 netif_tx_stop_all_queues(dev);
56079431
DV
3131 }
3132}
3133EXPORT_SYMBOL(netif_device_detach);
3134
bea3348e
SH
3135/**
3136 * netif_device_attach - mark device as attached
3137 * @dev: network device
3138 *
3139 * Mark device as attached from system and restart if needed.
3140 */
56079431
DV
3141void netif_device_attach(struct net_device *dev)
3142{
3143 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3144 netif_running(dev)) {
d543103a 3145 netif_tx_wake_all_queues(dev);
4ec93edb 3146 __netdev_watchdog_up(dev);
56079431
DV
3147 }
3148}
3149EXPORT_SYMBOL(netif_device_attach);
3150
5605c762
JP
3151/*
3152 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3153 * to be used as a distribution range.
3154 */
eadec877
AD
3155static u16 skb_tx_hash(const struct net_device *dev,
3156 const struct net_device *sb_dev,
3157 struct sk_buff *skb)
5605c762
JP
3158{
3159 u32 hash;
3160 u16 qoffset = 0;
1b837d48 3161 u16 qcount = dev->real_num_tx_queues;
5605c762 3162
eadec877
AD
3163 if (dev->num_tc) {
3164 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3165
3166 qoffset = sb_dev->tc_to_txq[tc].offset;
3167 qcount = sb_dev->tc_to_txq[tc].count;
3168 }
3169
5605c762
JP
3170 if (skb_rx_queue_recorded(skb)) {
3171 hash = skb_get_rx_queue(skb);
6e11d157
AN
3172 if (hash >= qoffset)
3173 hash -= qoffset;
1b837d48
AD
3174 while (unlikely(hash >= qcount))
3175 hash -= qcount;
eadec877 3176 return hash + qoffset;
5605c762
JP
3177 }
3178
3179 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3180}
5605c762 3181
36c92474
BH
3182static void skb_warn_bad_offload(const struct sk_buff *skb)
3183{
84d15ae5 3184 static const netdev_features_t null_features;
36c92474 3185 struct net_device *dev = skb->dev;
88ad4175 3186 const char *name = "";
36c92474 3187
c846ad9b
BG
3188 if (!net_ratelimit())
3189 return;
3190
88ad4175
BM
3191 if (dev) {
3192 if (dev->dev.parent)
3193 name = dev_driver_string(dev->dev.parent);
3194 else
3195 name = netdev_name(dev);
3196 }
6413139d
WB
3197 skb_dump(KERN_WARNING, skb, false);
3198 WARN(1, "%s: caps=(%pNF, %pNF)\n",
88ad4175 3199 name, dev ? &dev->features : &null_features,
6413139d 3200 skb->sk ? &skb->sk->sk_route_caps : &null_features);
36c92474
BH
3201}
3202
1da177e4
LT
3203/*
3204 * Invalidate hardware checksum when packet is to be mangled, and
3205 * complete checksum manually on outgoing path.
3206 */
84fa7933 3207int skb_checksum_help(struct sk_buff *skb)
1da177e4 3208{
d3bc23e7 3209 __wsum csum;
663ead3b 3210 int ret = 0, offset;
1da177e4 3211
84fa7933 3212 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
3213 goto out_set_summed;
3214
3aefd7d6 3215 if (unlikely(skb_is_gso(skb))) {
36c92474
BH
3216 skb_warn_bad_offload(skb);
3217 return -EINVAL;
1da177e4
LT
3218 }
3219
cef401de
ED
3220 /* Before computing a checksum, we should make sure no frag could
3221 * be modified by an external entity : checksum could be wrong.
3222 */
3223 if (skb_has_shared_frag(skb)) {
3224 ret = __skb_linearize(skb);
3225 if (ret)
3226 goto out;
3227 }
3228
55508d60 3229 offset = skb_checksum_start_offset(skb);
a030847e
HX
3230 BUG_ON(offset >= skb_headlen(skb));
3231 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3232
3233 offset += skb->csum_offset;
3234 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3235
8211fbfa
HK
3236 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3237 if (ret)
3238 goto out;
1da177e4 3239
4f2e4ad5 3240 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
a430a43d 3241out_set_summed:
1da177e4 3242 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 3243out:
1da177e4
LT
3244 return ret;
3245}
d1b19dff 3246EXPORT_SYMBOL(skb_checksum_help);
1da177e4 3247
b72b5bf6
DC
3248int skb_crc32c_csum_help(struct sk_buff *skb)
3249{
3250 __le32 crc32c_csum;
3251 int ret = 0, offset, start;
3252
3253 if (skb->ip_summed != CHECKSUM_PARTIAL)
3254 goto out;
3255
3256 if (unlikely(skb_is_gso(skb)))
3257 goto out;
3258
3259 /* Before computing a checksum, we should make sure no frag could
3260 * be modified by an external entity : checksum could be wrong.
3261 */
3262 if (unlikely(skb_has_shared_frag(skb))) {
3263 ret = __skb_linearize(skb);
3264 if (ret)
3265 goto out;
3266 }
3267 start = skb_checksum_start_offset(skb);
3268 offset = start + offsetof(struct sctphdr, checksum);
3269 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3270 ret = -EINVAL;
3271 goto out;
3272 }
8211fbfa
HK
3273
3274 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3275 if (ret)
3276 goto out;
3277
b72b5bf6
DC
3278 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3279 skb->len - start, ~(__u32)0,
3280 crc32c_csum_stub));
3281 *(__le32 *)(skb->data + offset) = crc32c_csum;
3282 skb->ip_summed = CHECKSUM_NONE;
dba00306 3283 skb->csum_not_inet = 0;
b72b5bf6
DC
3284out:
3285 return ret;
3286}
3287
53d6471c 3288__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
f6a78bfc 3289{
252e3346 3290 __be16 type = skb->protocol;
f6a78bfc 3291
19acc327
PS
3292 /* Tunnel gso handlers can set protocol to ethernet. */
3293 if (type == htons(ETH_P_TEB)) {
3294 struct ethhdr *eth;
3295
3296 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3297 return 0;
3298
1dfe82eb 3299 eth = (struct ethhdr *)skb->data;
19acc327
PS
3300 type = eth->h_proto;
3301 }
3302
d4bcef3f 3303 return __vlan_get_protocol(skb, type, depth);
ec5f0615
PS
3304}
3305
3306/**
3307 * skb_mac_gso_segment - mac layer segmentation handler.
3308 * @skb: buffer to segment
3309 * @features: features for the output path (see dev->features)
3310 */
3311struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3312 netdev_features_t features)
3313{
3314 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3315 struct packet_offload *ptype;
53d6471c
VY
3316 int vlan_depth = skb->mac_len;
3317 __be16 type = skb_network_protocol(skb, &vlan_depth);
ec5f0615
PS
3318
3319 if (unlikely(!type))
3320 return ERR_PTR(-EINVAL);
3321
53d6471c 3322 __skb_pull(skb, vlan_depth);
f6a78bfc
HX
3323
3324 rcu_read_lock();
22061d80 3325 list_for_each_entry_rcu(ptype, &offload_base, list) {
f191a1d1 3326 if (ptype->type == type && ptype->callbacks.gso_segment) {
f191a1d1 3327 segs = ptype->callbacks.gso_segment(skb, features);
f6a78bfc
HX
3328 break;
3329 }
3330 }
3331 rcu_read_unlock();
3332
98e399f8 3333 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 3334
f6a78bfc
HX
3335 return segs;
3336}
05e8ef4a
PS
3337EXPORT_SYMBOL(skb_mac_gso_segment);
3338
3339
3340/* openvswitch calls this on rx path, so we need a different check.
3341 */
3342static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3343{
3344 if (tx_path)
0c19f846
WB
3345 return skb->ip_summed != CHECKSUM_PARTIAL &&
3346 skb->ip_summed != CHECKSUM_UNNECESSARY;
6e7bc478
ED
3347
3348 return skb->ip_summed == CHECKSUM_NONE;
05e8ef4a
PS
3349}
3350
3351/**
3352 * __skb_gso_segment - Perform segmentation on skb.
3353 * @skb: buffer to segment
3354 * @features: features for the output path (see dev->features)
3355 * @tx_path: whether it is called in TX path
3356 *
3357 * This function segments the given skb and returns a list of segments.
3358 *
3359 * It may return NULL if the skb requires no segmentation. This is
3360 * only possible when GSO is used for verifying header integrity.
9207f9d4 3361 *
a08e7fd9 3362 * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
05e8ef4a
PS
3363 */
3364struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3365 netdev_features_t features, bool tx_path)
3366{
b2504a5d
ED
3367 struct sk_buff *segs;
3368
05e8ef4a
PS
3369 if (unlikely(skb_needs_check(skb, tx_path))) {
3370 int err;
3371
b2504a5d 3372 /* We're going to init ->check field in TCP or UDP header */
a40e0a66 3373 err = skb_cow_head(skb, 0);
3374 if (err < 0)
05e8ef4a
PS
3375 return ERR_PTR(err);
3376 }
3377
802ab55a
AD
3378 /* Only report GSO partial support if it will enable us to
3379 * support segmentation on this frame without needing additional
3380 * work.
3381 */
3382 if (features & NETIF_F_GSO_PARTIAL) {
3383 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3384 struct net_device *dev = skb->dev;
3385
3386 partial_features |= dev->features & dev->gso_partial_features;
3387 if (!skb_gso_ok(skb, features | partial_features))
3388 features &= ~NETIF_F_GSO_PARTIAL;
3389 }
3390
a08e7fd9 3391 BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
9207f9d4
KK
3392 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3393
68c33163 3394 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
3347c960
ED
3395 SKB_GSO_CB(skb)->encap_level = 0;
3396
05e8ef4a
PS
3397 skb_reset_mac_header(skb);
3398 skb_reset_mac_len(skb);
3399
b2504a5d
ED
3400 segs = skb_mac_gso_segment(skb, features);
3401
3a1296a3 3402 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
b2504a5d
ED
3403 skb_warn_bad_offload(skb);
3404
3405 return segs;
05e8ef4a 3406}
12b0004d 3407EXPORT_SYMBOL(__skb_gso_segment);
f6a78bfc 3408
fb286bb2
HX
3409/* Take action when hardware reception checksum errors are detected. */
3410#ifdef CONFIG_BUG
7fe50ac8 3411void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
fb286bb2
HX
3412{
3413 if (net_ratelimit()) {
7b6cd1ce 3414 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
6413139d 3415 skb_dump(KERN_ERR, skb, true);
fb286bb2
HX
3416 dump_stack();
3417 }
3418}
3419EXPORT_SYMBOL(netdev_rx_csum_fault);
3420#endif
3421
ab74cfeb 3422/* XXX: check that highmem exists at all on the given machine. */
c1e756bf 3423static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1da177e4 3424{
3d3a8533 3425#ifdef CONFIG_HIGHMEM
1da177e4 3426 int i;
f4563a75 3427
5acbbd42 3428 if (!(dev->features & NETIF_F_HIGHDMA)) {
ea2ab693
IC
3429 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3430 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
f4563a75 3431
ea2ab693 3432 if (PageHighMem(skb_frag_page(frag)))
5acbbd42 3433 return 1;
ea2ab693 3434 }
5acbbd42 3435 }
3d3a8533 3436#endif
1da177e4
LT
3437 return 0;
3438}
1da177e4 3439
3b392ddb
SH
3440/* If MPLS offload request, verify we are testing hardware MPLS features
3441 * instead of standard features for the netdev.
3442 */
d0edc7bf 3443#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3b392ddb
SH
3444static netdev_features_t net_mpls_features(struct sk_buff *skb,
3445 netdev_features_t features,
3446 __be16 type)
3447{
25cd9ba0 3448 if (eth_p_mpls(type))
3b392ddb
SH
3449 features &= skb->dev->mpls_features;
3450
3451 return features;
3452}
3453#else
3454static netdev_features_t net_mpls_features(struct sk_buff *skb,
3455 netdev_features_t features,
3456 __be16 type)
3457{
3458 return features;
3459}
3460#endif
3461
c8f44aff 3462static netdev_features_t harmonize_features(struct sk_buff *skb,
c1e756bf 3463 netdev_features_t features)
f01a5236 3464{
3b392ddb
SH
3465 __be16 type;
3466
9fc95f50 3467 type = skb_network_protocol(skb, NULL);
3b392ddb 3468 features = net_mpls_features(skb, features, type);
53d6471c 3469
c0d680e5 3470 if (skb->ip_summed != CHECKSUM_NONE &&
3b392ddb 3471 !can_checksum_protocol(features, type)) {
996e8021 3472 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
f01a5236 3473 }
7be2c82c
ED
3474 if (illegal_highdma(skb->dev, skb))
3475 features &= ~NETIF_F_SG;
f01a5236
JG
3476
3477 return features;
3478}
3479
e38f3025
TM
3480netdev_features_t passthru_features_check(struct sk_buff *skb,
3481 struct net_device *dev,
3482 netdev_features_t features)
3483{
3484 return features;
3485}
3486EXPORT_SYMBOL(passthru_features_check);
3487
7ce23672 3488static netdev_features_t dflt_features_check(struct sk_buff *skb,
8cb65d00
TM
3489 struct net_device *dev,
3490 netdev_features_t features)
3491{
3492 return vlan_features_check(skb, features);
3493}
3494
cbc53e08
AD
3495static netdev_features_t gso_features_check(const struct sk_buff *skb,
3496 struct net_device *dev,
3497 netdev_features_t features)
3498{
3499 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3500
3501 if (gso_segs > dev->gso_max_segs)
3502 return features & ~NETIF_F_GSO_MASK;
3503
1d155dfd
HK
3504 if (!skb_shinfo(skb)->gso_type) {
3505 skb_warn_bad_offload(skb);
3506 return features & ~NETIF_F_GSO_MASK;
3507 }
3508
802ab55a
AD
3509 /* Support for GSO partial features requires software
3510 * intervention before we can actually process the packets
3511 * so we need to strip support for any partial features now
3512 * and we can pull them back in after we have partially
3513 * segmented the frame.
3514 */
3515 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3516 features &= ~dev->gso_partial_features;
3517
3518 /* Make sure to clear the IPv4 ID mangling feature if the
3519 * IPv4 header has the potential to be fragmented.
cbc53e08
AD
3520 */
3521 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3522 struct iphdr *iph = skb->encapsulation ?
3523 inner_ip_hdr(skb) : ip_hdr(skb);
3524
3525 if (!(iph->frag_off & htons(IP_DF)))
3526 features &= ~NETIF_F_TSO_MANGLEID;
3527 }
3528
3529 return features;
3530}
3531
c1e756bf 3532netdev_features_t netif_skb_features(struct sk_buff *skb)
58e998c6 3533{
5f35227e 3534 struct net_device *dev = skb->dev;
fcbeb976 3535 netdev_features_t features = dev->features;
58e998c6 3536
cbc53e08
AD
3537 if (skb_is_gso(skb))
3538 features = gso_features_check(skb, dev, features);
30b678d8 3539
5f35227e
JG
3540 /* If encapsulation offload request, verify we are testing
3541 * hardware encapsulation features instead of standard
3542 * features for the netdev
3543 */
3544 if (skb->encapsulation)
3545 features &= dev->hw_enc_features;
3546
f5a7fb88
TM
3547 if (skb_vlan_tagged(skb))
3548 features = netdev_intersect_features(features,
3549 dev->vlan_features |
3550 NETIF_F_HW_VLAN_CTAG_TX |
3551 NETIF_F_HW_VLAN_STAG_TX);
f01a5236 3552
5f35227e
JG
3553 if (dev->netdev_ops->ndo_features_check)
3554 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3555 features);
8cb65d00
TM
3556 else
3557 features &= dflt_features_check(skb, dev, features);
5f35227e 3558
c1e756bf 3559 return harmonize_features(skb, features);
58e998c6 3560}
c1e756bf 3561EXPORT_SYMBOL(netif_skb_features);
58e998c6 3562
2ea25513 3563static int xmit_one(struct sk_buff *skb, struct net_device *dev,
95f6b3dd 3564 struct netdev_queue *txq, bool more)
f6a78bfc 3565{
2ea25513
DM
3566 unsigned int len;
3567 int rc;
00829823 3568
9f9a742d 3569 if (dev_nit_active(dev))
2ea25513 3570 dev_queue_xmit_nit(skb, dev);
fc741216 3571
2ea25513 3572 len = skb->len;
3744741a 3573 PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies);
2ea25513 3574 trace_net_dev_start_xmit(skb, dev);
95f6b3dd 3575 rc = netdev_start_xmit(skb, dev, txq, more);
2ea25513 3576 trace_net_dev_xmit(skb, rc, dev, len);
adf30907 3577
2ea25513
DM
3578 return rc;
3579}
7b9c6090 3580
8dcda22a
DM
3581struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3582 struct netdev_queue *txq, int *ret)
7f2e870f
DM
3583{
3584 struct sk_buff *skb = first;
3585 int rc = NETDEV_TX_OK;
7b9c6090 3586
7f2e870f
DM
3587 while (skb) {
3588 struct sk_buff *next = skb->next;
fc70fb64 3589
a8305bff 3590 skb_mark_not_on_list(skb);
95f6b3dd 3591 rc = xmit_one(skb, dev, txq, next != NULL);
7f2e870f
DM
3592 if (unlikely(!dev_xmit_complete(rc))) {
3593 skb->next = next;
3594 goto out;
3595 }
6afff0ca 3596
7f2e870f 3597 skb = next;
fe60faa5 3598 if (netif_tx_queue_stopped(txq) && skb) {
7f2e870f
DM
3599 rc = NETDEV_TX_BUSY;
3600 break;
9ccb8975 3601 }
7f2e870f 3602 }
9ccb8975 3603
7f2e870f
DM
3604out:
3605 *ret = rc;
3606 return skb;
3607}
b40863c6 3608
1ff0dc94
ED
3609static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3610 netdev_features_t features)
f6a78bfc 3611{
df8a39de 3612 if (skb_vlan_tag_present(skb) &&
5968250c
JP
3613 !vlan_hw_offload_capable(features, skb->vlan_proto))
3614 skb = __vlan_hwaccel_push_inside(skb);
eae3f88e
DM
3615 return skb;
3616}
f6a78bfc 3617
43c26a1a
DC
3618int skb_csum_hwoffload_help(struct sk_buff *skb,
3619 const netdev_features_t features)
3620{
fa821170 3621 if (unlikely(skb_csum_is_sctp(skb)))
43c26a1a
DC
3622 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3623 skb_crc32c_csum_help(skb);
3624
62fafcd6
XL
3625 if (features & NETIF_F_HW_CSUM)
3626 return 0;
3627
3628 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3629 switch (skb->csum_offset) {
3630 case offsetof(struct tcphdr, check):
3631 case offsetof(struct udphdr, check):
3632 return 0;
3633 }
3634 }
3635
3636 return skb_checksum_help(skb);
43c26a1a
DC
3637}
3638EXPORT_SYMBOL(skb_csum_hwoffload_help);
3639
f53c7239 3640static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
eae3f88e
DM
3641{
3642 netdev_features_t features;
f6a78bfc 3643
eae3f88e
DM
3644 features = netif_skb_features(skb);
3645 skb = validate_xmit_vlan(skb, features);
3646 if (unlikely(!skb))
3647 goto out_null;
7b9c6090 3648
ebf4e808
IL
3649 skb = sk_validate_xmit_skb(skb, dev);
3650 if (unlikely(!skb))
3651 goto out_null;
3652
8b86a61d 3653 if (netif_needs_gso(skb, features)) {
ce93718f
DM
3654 struct sk_buff *segs;
3655
3656 segs = skb_gso_segment(skb, features);
cecda693 3657 if (IS_ERR(segs)) {
af6dabc9 3658 goto out_kfree_skb;
cecda693
JW
3659 } else if (segs) {
3660 consume_skb(skb);
3661 skb = segs;
f6a78bfc 3662 }
eae3f88e
DM
3663 } else {
3664 if (skb_needs_linearize(skb, features) &&
3665 __skb_linearize(skb))
3666 goto out_kfree_skb;
4ec93edb 3667
eae3f88e
DM
3668 /* If packet is not checksummed and device does not
3669 * support checksumming for this protocol, complete
3670 * checksumming here.
3671 */
3672 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3673 if (skb->encapsulation)
3674 skb_set_inner_transport_header(skb,
3675 skb_checksum_start_offset(skb));
3676 else
3677 skb_set_transport_header(skb,
3678 skb_checksum_start_offset(skb));
43c26a1a 3679 if (skb_csum_hwoffload_help(skb, features))
eae3f88e 3680 goto out_kfree_skb;
7b9c6090 3681 }
0c772159 3682 }
7b9c6090 3683
f53c7239 3684 skb = validate_xmit_xfrm(skb, features, again);
3dca3f38 3685
eae3f88e 3686 return skb;
fc70fb64 3687
f6a78bfc
HX
3688out_kfree_skb:
3689 kfree_skb(skb);
eae3f88e 3690out_null:
d21fd63e 3691 atomic_long_inc(&dev->tx_dropped);
eae3f88e
DM
3692 return NULL;
3693}
6afff0ca 3694
f53c7239 3695struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
55a93b3e
ED
3696{
3697 struct sk_buff *next, *head = NULL, *tail;
3698
bec3cfdc 3699 for (; skb != NULL; skb = next) {
55a93b3e 3700 next = skb->next;
a8305bff 3701 skb_mark_not_on_list(skb);
bec3cfdc
ED
3702
3703 /* in case skb wont be segmented, point to itself */
3704 skb->prev = skb;
3705
f53c7239 3706 skb = validate_xmit_skb(skb, dev, again);
bec3cfdc
ED
3707 if (!skb)
3708 continue;
55a93b3e 3709
bec3cfdc
ED
3710 if (!head)
3711 head = skb;
3712 else
3713 tail->next = skb;
3714 /* If skb was segmented, skb->prev points to
3715 * the last segment. If not, it still contains skb.
3716 */
3717 tail = skb->prev;
55a93b3e
ED
3718 }
3719 return head;
f6a78bfc 3720}
104ba78c 3721EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
f6a78bfc 3722
1def9238
ED
3723static void qdisc_pkt_len_init(struct sk_buff *skb)
3724{
3725 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3726
3727 qdisc_skb_cb(skb)->pkt_len = skb->len;
3728
3729 /* To get more precise estimation of bytes sent on wire,
3730 * we add to pkt_len the headers size of all segments
3731 */
a0dce875 3732 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
757b8b1d 3733 unsigned int hdr_len;
15e5a030 3734 u16 gso_segs = shinfo->gso_segs;
1def9238 3735
757b8b1d
ED
3736 /* mac layer + network layer */
3737 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3738
3739 /* + transport layer */
7c68d1a6
ED
3740 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3741 const struct tcphdr *th;
3742 struct tcphdr _tcphdr;
3743
3744 th = skb_header_pointer(skb, skb_transport_offset(skb),
3745 sizeof(_tcphdr), &_tcphdr);
3746 if (likely(th))
3747 hdr_len += __tcp_hdrlen(th);
3748 } else {
3749 struct udphdr _udphdr;
3750
3751 if (skb_header_pointer(skb, skb_transport_offset(skb),
3752 sizeof(_udphdr), &_udphdr))
3753 hdr_len += sizeof(struct udphdr);
3754 }
15e5a030
JW
3755
3756 if (shinfo->gso_type & SKB_GSO_DODGY)
3757 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3758 shinfo->gso_size);
3759
3760 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
1def9238
ED
3761 }
3762}
3763
bbd8a0d3
KK
3764static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3765 struct net_device *dev,
3766 struct netdev_queue *txq)
3767{
3768 spinlock_t *root_lock = qdisc_lock(q);
520ac30f 3769 struct sk_buff *to_free = NULL;
a2da570d 3770 bool contended;
bbd8a0d3
KK
3771 int rc;
3772
a2da570d 3773 qdisc_calculate_pkt_len(skb, q);
6b3ba914
JF
3774
3775 if (q->flags & TCQ_F_NOLOCK) {
ac5c66f2 3776 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
379349e9 3777 qdisc_run(q);
6b3ba914
JF
3778
3779 if (unlikely(to_free))
3780 kfree_skb_list(to_free);
3781 return rc;
3782 }
3783
79640a4c
ED
3784 /*
3785 * Heuristic to force contended enqueues to serialize on a
3786 * separate lock before trying to get qdisc main lock.
f9eb8aea 3787 * This permits qdisc->running owner to get the lock more
9bf2b8c2 3788 * often and dequeue packets faster.
79640a4c 3789 */
a2da570d 3790 contended = qdisc_is_running(q);
79640a4c
ED
3791 if (unlikely(contended))
3792 spin_lock(&q->busylock);
3793
bbd8a0d3
KK
3794 spin_lock(root_lock);
3795 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
520ac30f 3796 __qdisc_drop(skb, &to_free);
bbd8a0d3
KK
3797 rc = NET_XMIT_DROP;
3798 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
bc135b23 3799 qdisc_run_begin(q)) {
bbd8a0d3
KK
3800 /*
3801 * This is a work-conserving queue; there are no old skbs
3802 * waiting to be sent out; and the qdisc is not running -
3803 * xmit the skb directly.
3804 */
bfe0d029 3805
bfe0d029
ED
3806 qdisc_bstats_update(q, skb);
3807
55a93b3e 3808 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
79640a4c
ED
3809 if (unlikely(contended)) {
3810 spin_unlock(&q->busylock);
3811 contended = false;
3812 }
bbd8a0d3 3813 __qdisc_run(q);
6c148184 3814 }
bbd8a0d3 3815
6c148184 3816 qdisc_run_end(q);
bbd8a0d3
KK
3817 rc = NET_XMIT_SUCCESS;
3818 } else {
ac5c66f2 3819 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
79640a4c
ED
3820 if (qdisc_run_begin(q)) {
3821 if (unlikely(contended)) {
3822 spin_unlock(&q->busylock);
3823 contended = false;
3824 }
3825 __qdisc_run(q);
6c148184 3826 qdisc_run_end(q);
79640a4c 3827 }
bbd8a0d3
KK
3828 }
3829 spin_unlock(root_lock);
520ac30f
ED
3830 if (unlikely(to_free))
3831 kfree_skb_list(to_free);
79640a4c
ED
3832 if (unlikely(contended))
3833 spin_unlock(&q->busylock);
bbd8a0d3
KK
3834 return rc;
3835}
3836
86f8515f 3837#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
5bc1421e
NH
3838static void skb_update_prio(struct sk_buff *skb)
3839{
4dcb31d4
ED
3840 const struct netprio_map *map;
3841 const struct sock *sk;
3842 unsigned int prioidx;
5bc1421e 3843
4dcb31d4
ED
3844 if (skb->priority)
3845 return;
3846 map = rcu_dereference_bh(skb->dev->priomap);
3847 if (!map)
3848 return;
3849 sk = skb_to_full_sk(skb);
3850 if (!sk)
3851 return;
91c68ce2 3852
4dcb31d4
ED
3853 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3854
3855 if (prioidx < map->priomap_len)
3856 skb->priority = map->priomap[prioidx];
5bc1421e
NH
3857}
3858#else
3859#define skb_update_prio(skb)
3860#endif
3861
95603e22
MM
3862/**
3863 * dev_loopback_xmit - loop back @skb
0c4b51f0
EB
3864 * @net: network namespace this loopback is happening in
3865 * @sk: sk needed to be a netfilter okfn
95603e22
MM
3866 * @skb: buffer to transmit
3867 */
0c4b51f0 3868int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
95603e22
MM
3869{
3870 skb_reset_mac_header(skb);
3871 __skb_pull(skb, skb_network_offset(skb));
3872 skb->pkt_type = PACKET_LOOPBACK;
3873 skb->ip_summed = CHECKSUM_UNNECESSARY;
3874 WARN_ON(!skb_dst(skb));
3875 skb_dst_force(skb);
3876 netif_rx_ni(skb);
3877 return 0;
3878}
3879EXPORT_SYMBOL(dev_loopback_xmit);
3880
1f211a1b
DB
3881#ifdef CONFIG_NET_EGRESS
3882static struct sk_buff *
3883sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3884{
46209401 3885 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
1f211a1b
DB
3886 struct tcf_result cl_res;
3887
46209401 3888 if (!miniq)
1f211a1b
DB
3889 return skb;
3890
8dc07fdb 3891 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
aadaca9e 3892 qdisc_skb_cb(skb)->mru = 0;
7baf2429 3893 qdisc_skb_cb(skb)->post_ct = false;
46209401 3894 mini_qdisc_bstats_cpu_update(miniq, skb);
1f211a1b 3895
46209401 3896 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
1f211a1b
DB
3897 case TC_ACT_OK:
3898 case TC_ACT_RECLASSIFY:
3899 skb->tc_index = TC_H_MIN(cl_res.classid);
3900 break;
3901 case TC_ACT_SHOT:
46209401 3902 mini_qdisc_qstats_cpu_drop(miniq);
1f211a1b 3903 *ret = NET_XMIT_DROP;
7e2c3aea
DB
3904 kfree_skb(skb);
3905 return NULL;
1f211a1b
DB
3906 case TC_ACT_STOLEN:
3907 case TC_ACT_QUEUED:
e25ea21f 3908 case TC_ACT_TRAP:
1f211a1b 3909 *ret = NET_XMIT_SUCCESS;
7e2c3aea 3910 consume_skb(skb);
1f211a1b
DB
3911 return NULL;
3912 case TC_ACT_REDIRECT:
3913 /* No need to push/pop skb's mac_header here on egress! */
3914 skb_do_redirect(skb);
3915 *ret = NET_XMIT_SUCCESS;
3916 return NULL;
3917 default:
3918 break;
3919 }
357b6cc5 3920
1f211a1b
DB
3921 return skb;
3922}
3923#endif /* CONFIG_NET_EGRESS */
3924
fc9bab24
AN
3925#ifdef CONFIG_XPS
3926static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3927 struct xps_dev_maps *dev_maps, unsigned int tci)
3928{
3929 struct xps_map *map;
3930 int queue_index = -1;
3931
3932 if (dev->num_tc) {
3933 tci *= dev->num_tc;
3934 tci += netdev_get_prio_tc_map(dev, skb->priority);
3935 }
3936
3937 map = rcu_dereference(dev_maps->attr_map[tci]);
3938 if (map) {
3939 if (map->len == 1)
3940 queue_index = map->queues[0];
3941 else
3942 queue_index = map->queues[reciprocal_scale(
3943 skb_get_hash(skb), map->len)];
3944 if (unlikely(queue_index >= dev->real_num_tx_queues))
3945 queue_index = -1;
3946 }
3947 return queue_index;
3948}
3949#endif
3950
eadec877
AD
3951static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3952 struct sk_buff *skb)
638b2a69
JP
3953{
3954#ifdef CONFIG_XPS
3955 struct xps_dev_maps *dev_maps;
fc9bab24 3956 struct sock *sk = skb->sk;
638b2a69
JP
3957 int queue_index = -1;
3958
04157469
AN
3959 if (!static_key_false(&xps_needed))
3960 return -1;
3961
638b2a69 3962 rcu_read_lock();
fc9bab24
AN
3963 if (!static_key_false(&xps_rxqs_needed))
3964 goto get_cpus_map;
3965
eadec877 3966 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
638b2a69 3967 if (dev_maps) {
fc9bab24 3968 int tci = sk_rx_queue_get(sk);
184c449f 3969
fc9bab24
AN
3970 if (tci >= 0 && tci < dev->num_rx_queues)
3971 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3972 tci);
3973 }
184c449f 3974
fc9bab24
AN
3975get_cpus_map:
3976 if (queue_index < 0) {
eadec877 3977 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
fc9bab24
AN
3978 if (dev_maps) {
3979 unsigned int tci = skb->sender_cpu - 1;
3980
3981 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3982 tci);
638b2a69
JP
3983 }
3984 }
3985 rcu_read_unlock();
3986
3987 return queue_index;
3988#else
3989 return -1;
3990#endif
3991}
3992
a4ea8a3d 3993u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
a350ecce 3994 struct net_device *sb_dev)
a4ea8a3d
AD
3995{
3996 return 0;
3997}
3998EXPORT_SYMBOL(dev_pick_tx_zero);
3999
4000u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
a350ecce 4001 struct net_device *sb_dev)
a4ea8a3d
AD
4002{
4003 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
4004}
4005EXPORT_SYMBOL(dev_pick_tx_cpu_id);
4006
b71b5837
PA
4007u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
4008 struct net_device *sb_dev)
638b2a69
JP
4009{
4010 struct sock *sk = skb->sk;
4011 int queue_index = sk_tx_queue_get(sk);
4012
eadec877
AD
4013 sb_dev = sb_dev ? : dev;
4014
638b2a69
JP
4015 if (queue_index < 0 || skb->ooo_okay ||
4016 queue_index >= dev->real_num_tx_queues) {
eadec877 4017 int new_index = get_xps_queue(dev, sb_dev, skb);
f4563a75 4018
638b2a69 4019 if (new_index < 0)
eadec877 4020 new_index = skb_tx_hash(dev, sb_dev, skb);
638b2a69
JP
4021
4022 if (queue_index != new_index && sk &&
004a5d01 4023 sk_fullsock(sk) &&
638b2a69
JP
4024 rcu_access_pointer(sk->sk_dst_cache))
4025 sk_tx_queue_set(sk, new_index);
4026
4027 queue_index = new_index;
4028 }
4029
4030 return queue_index;
4031}
b71b5837 4032EXPORT_SYMBOL(netdev_pick_tx);
638b2a69 4033
4bd97d51
PA
4034struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
4035 struct sk_buff *skb,
4036 struct net_device *sb_dev)
638b2a69
JP
4037{
4038 int queue_index = 0;
4039
4040#ifdef CONFIG_XPS
52bd2d62
ED
4041 u32 sender_cpu = skb->sender_cpu - 1;
4042
4043 if (sender_cpu >= (u32)NR_CPUS)
638b2a69
JP
4044 skb->sender_cpu = raw_smp_processor_id() + 1;
4045#endif
4046
4047 if (dev->real_num_tx_queues != 1) {
4048 const struct net_device_ops *ops = dev->netdev_ops;
f4563a75 4049
638b2a69 4050 if (ops->ndo_select_queue)
a350ecce 4051 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
638b2a69 4052 else
4bd97d51 4053 queue_index = netdev_pick_tx(dev, skb, sb_dev);
638b2a69 4054
d584527c 4055 queue_index = netdev_cap_txqueue(dev, queue_index);
638b2a69
JP
4056 }
4057
4058 skb_set_queue_mapping(skb, queue_index);
4059 return netdev_get_tx_queue(dev, queue_index);
4060}
4061
d29f749e 4062/**
9d08dd3d 4063 * __dev_queue_xmit - transmit a buffer
d29f749e 4064 * @skb: buffer to transmit
eadec877 4065 * @sb_dev: suboordinate device used for L2 forwarding offload
d29f749e
DJ
4066 *
4067 * Queue a buffer for transmission to a network device. The caller must
4068 * have set the device and priority and built the buffer before calling
4069 * this function. The function can be called from an interrupt.
4070 *
4071 * A negative errno code is returned on a failure. A success does not
4072 * guarantee the frame will be transmitted as it may be dropped due
4073 * to congestion or traffic shaping.
4074 *
4075 * -----------------------------------------------------------------------------------
4076 * I notice this method can also return errors from the queue disciplines,
4077 * including NET_XMIT_DROP, which is a positive value. So, errors can also
4078 * be positive.
4079 *
4080 * Regardless of the return value, the skb is consumed, so it is currently
4081 * difficult to retry a send to this method. (You can bump the ref count
4082 * before sending to hold a reference for retry if you are careful.)
4083 *
4084 * When calling this method, interrupts MUST be enabled. This is because
4085 * the BH enable code must have IRQs enabled so that it will not deadlock.
4086 * --BLG
4087 */
eadec877 4088static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
1da177e4
LT
4089{
4090 struct net_device *dev = skb->dev;
dc2b4847 4091 struct netdev_queue *txq;
1da177e4
LT
4092 struct Qdisc *q;
4093 int rc = -ENOMEM;
f53c7239 4094 bool again = false;
1da177e4 4095
6d1ccff6
ED
4096 skb_reset_mac_header(skb);
4097
e7fd2885 4098 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
e7ed11ee 4099 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED);
e7fd2885 4100
4ec93edb
YH
4101 /* Disable soft irqs for various locks below. Also
4102 * stops preemption for RCU.
1da177e4 4103 */
4ec93edb 4104 rcu_read_lock_bh();
1da177e4 4105
5bc1421e
NH
4106 skb_update_prio(skb);
4107
1f211a1b
DB
4108 qdisc_pkt_len_init(skb);
4109#ifdef CONFIG_NET_CLS_ACT
8dc07fdb 4110 skb->tc_at_ingress = 0;
357b6cc5 4111# ifdef CONFIG_NET_EGRESS
aabf6772 4112 if (static_branch_unlikely(&egress_needed_key)) {
1f211a1b
DB
4113 skb = sch_handle_egress(skb, &rc, dev);
4114 if (!skb)
4115 goto out;
4116 }
357b6cc5 4117# endif
1f211a1b 4118#endif
02875878
ED
4119 /* If device/qdisc don't need skb->dst, release it right now while
4120 * its hot in this cpu cache.
4121 */
4122 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4123 skb_dst_drop(skb);
4124 else
4125 skb_dst_force(skb);
4126
4bd97d51 4127 txq = netdev_core_pick_tx(dev, skb, sb_dev);
a898def2 4128 q = rcu_dereference_bh(txq->qdisc);
37437bb2 4129
cf66ba58 4130 trace_net_dev_queue(skb);
1da177e4 4131 if (q->enqueue) {
bbd8a0d3 4132 rc = __dev_xmit_skb(skb, q, dev, txq);
37437bb2 4133 goto out;
1da177e4
LT
4134 }
4135
4136 /* The device has no queue. Common case for software devices:
eb13da1a 4137 * loopback, all the sorts of tunnels...
1da177e4 4138
eb13da1a 4139 * Really, it is unlikely that netif_tx_lock protection is necessary
4140 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4141 * counters.)
4142 * However, it is possible, that they rely on protection
4143 * made by us here.
1da177e4 4144
eb13da1a 4145 * Check this and shot the lock. It is not prone from deadlocks.
4146 *Either shot noqueue qdisc, it is even simpler 8)
1da177e4
LT
4147 */
4148 if (dev->flags & IFF_UP) {
4149 int cpu = smp_processor_id(); /* ok because BHs are off */
4150
c773e847 4151 if (txq->xmit_lock_owner != cpu) {
97cdcf37 4152 if (dev_xmit_recursion())
745e20f1
ED
4153 goto recursion_alert;
4154
f53c7239 4155 skb = validate_xmit_skb(skb, dev, &again);
1f59533f 4156 if (!skb)
d21fd63e 4157 goto out;
1f59533f 4158
3744741a 4159 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
c773e847 4160 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 4161
73466498 4162 if (!netif_xmit_stopped(txq)) {
97cdcf37 4163 dev_xmit_recursion_inc();
ce93718f 4164 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
97cdcf37 4165 dev_xmit_recursion_dec();
572a9d7b 4166 if (dev_xmit_complete(rc)) {
c773e847 4167 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
4168 goto out;
4169 }
4170 }
c773e847 4171 HARD_TX_UNLOCK(dev, txq);
e87cc472
JP
4172 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4173 dev->name);
1da177e4
LT
4174 } else {
4175 /* Recursion is detected! It is possible,
745e20f1
ED
4176 * unfortunately
4177 */
4178recursion_alert:
e87cc472
JP
4179 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4180 dev->name);
1da177e4
LT
4181 }
4182 }
4183
4184 rc = -ENETDOWN;
d4828d85 4185 rcu_read_unlock_bh();
1da177e4 4186
015f0688 4187 atomic_long_inc(&dev->tx_dropped);
1f59533f 4188 kfree_skb_list(skb);
1da177e4
LT
4189 return rc;
4190out:
d4828d85 4191 rcu_read_unlock_bh();
1da177e4
LT
4192 return rc;
4193}
f663dd9a 4194
2b4aa3ce 4195int dev_queue_xmit(struct sk_buff *skb)
f663dd9a
JW
4196{
4197 return __dev_queue_xmit(skb, NULL);
4198}
2b4aa3ce 4199EXPORT_SYMBOL(dev_queue_xmit);
1da177e4 4200
eadec877 4201int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
f663dd9a 4202{
eadec877 4203 return __dev_queue_xmit(skb, sb_dev);
f663dd9a
JW
4204}
4205EXPORT_SYMBOL(dev_queue_xmit_accel);
4206
36ccdf85 4207int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
865b03f2
MK
4208{
4209 struct net_device *dev = skb->dev;
4210 struct sk_buff *orig_skb = skb;
4211 struct netdev_queue *txq;
4212 int ret = NETDEV_TX_BUSY;
4213 bool again = false;
4214
4215 if (unlikely(!netif_running(dev) ||
4216 !netif_carrier_ok(dev)))
4217 goto drop;
4218
4219 skb = validate_xmit_skb_list(skb, dev, &again);
4220 if (skb != orig_skb)
4221 goto drop;
4222
4223 skb_set_queue_mapping(skb, queue_id);
4224 txq = skb_get_tx_queue(dev, skb);
3744741a 4225 PRANDOM_ADD_NOISE(skb, dev, txq, jiffies);
865b03f2
MK
4226
4227 local_bh_disable();
4228
0ad6f6e7 4229 dev_xmit_recursion_inc();
865b03f2
MK
4230 HARD_TX_LOCK(dev, txq, smp_processor_id());
4231 if (!netif_xmit_frozen_or_drv_stopped(txq))
4232 ret = netdev_start_xmit(skb, dev, txq, false);
4233 HARD_TX_UNLOCK(dev, txq);
0ad6f6e7 4234 dev_xmit_recursion_dec();
865b03f2
MK
4235
4236 local_bh_enable();
865b03f2
MK
4237 return ret;
4238drop:
4239 atomic_long_inc(&dev->tx_dropped);
4240 kfree_skb_list(skb);
4241 return NET_XMIT_DROP;
4242}
36ccdf85 4243EXPORT_SYMBOL(__dev_direct_xmit);
1da177e4 4244
eb13da1a 4245/*************************************************************************
4246 * Receiver routines
4247 *************************************************************************/
1da177e4 4248
6b2bedc3 4249int netdev_max_backlog __read_mostly = 1000;
c9e6bc64
ED
4250EXPORT_SYMBOL(netdev_max_backlog);
4251
3b098e2d 4252int netdev_tstamp_prequeue __read_mostly = 1;
6b2bedc3 4253int netdev_budget __read_mostly = 300;
a4837980
KK
4254/* Must be at least 2 jiffes to guarantee 1 jiffy timeout */
4255unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ;
3d48b53f
MT
4256int weight_p __read_mostly = 64; /* old backlog weight */
4257int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4258int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4259int dev_rx_weight __read_mostly = 64;
4260int dev_tx_weight __read_mostly = 64;
323ebb61
EC
4261/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4262int gro_normal_batch __read_mostly = 8;
1da177e4 4263
eecfd7c4
ED
4264/* Called with irq disabled */
4265static inline void ____napi_schedule(struct softnet_data *sd,
4266 struct napi_struct *napi)
4267{
4268 list_add_tail(&napi->poll_list, &sd->poll_list);
4269 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4270}
4271
bfb564e7
KK
4272#ifdef CONFIG_RPS
4273
4274/* One global table that all flow-based protocols share. */
6e3f7faf 4275struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
bfb564e7 4276EXPORT_SYMBOL(rps_sock_flow_table);
567e4b79
ED
4277u32 rps_cpu_mask __read_mostly;
4278EXPORT_SYMBOL(rps_cpu_mask);
bfb564e7 4279
dc05360f 4280struct static_key_false rps_needed __read_mostly;
3df97ba8 4281EXPORT_SYMBOL(rps_needed);
dc05360f 4282struct static_key_false rfs_needed __read_mostly;
13bfff25 4283EXPORT_SYMBOL(rfs_needed);
adc9300e 4284
c445477d
BH
4285static struct rps_dev_flow *
4286set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4287 struct rps_dev_flow *rflow, u16 next_cpu)
4288{
a31196b0 4289 if (next_cpu < nr_cpu_ids) {
c445477d
BH
4290#ifdef CONFIG_RFS_ACCEL
4291 struct netdev_rx_queue *rxqueue;
4292 struct rps_dev_flow_table *flow_table;
4293 struct rps_dev_flow *old_rflow;
4294 u32 flow_id;
4295 u16 rxq_index;
4296 int rc;
4297
4298 /* Should we steer this flow to a different hardware queue? */
69a19ee6
BH
4299 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4300 !(dev->features & NETIF_F_NTUPLE))
c445477d
BH
4301 goto out;
4302 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4303 if (rxq_index == skb_get_rx_queue(skb))
4304 goto out;
4305
4306 rxqueue = dev->_rx + rxq_index;
4307 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4308 if (!flow_table)
4309 goto out;
61b905da 4310 flow_id = skb_get_hash(skb) & flow_table->mask;
c445477d
BH
4311 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4312 rxq_index, flow_id);
4313 if (rc < 0)
4314 goto out;
4315 old_rflow = rflow;
4316 rflow = &flow_table->flows[flow_id];
c445477d
BH
4317 rflow->filter = rc;
4318 if (old_rflow->filter == rflow->filter)
4319 old_rflow->filter = RPS_NO_FILTER;
4320 out:
4321#endif
4322 rflow->last_qtail =
09994d1b 4323 per_cpu(softnet_data, next_cpu).input_queue_head;
c445477d
BH
4324 }
4325
09994d1b 4326 rflow->cpu = next_cpu;
c445477d
BH
4327 return rflow;
4328}
4329
bfb564e7
KK
4330/*
4331 * get_rps_cpu is called from netif_receive_skb and returns the target
4332 * CPU from the RPS map of the receiving queue for a given skb.
4333 * rcu_read_lock must be held on entry.
4334 */
4335static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4336 struct rps_dev_flow **rflowp)
4337{
567e4b79
ED
4338 const struct rps_sock_flow_table *sock_flow_table;
4339 struct netdev_rx_queue *rxqueue = dev->_rx;
bfb564e7 4340 struct rps_dev_flow_table *flow_table;
567e4b79 4341 struct rps_map *map;
bfb564e7 4342 int cpu = -1;
567e4b79 4343 u32 tcpu;
61b905da 4344 u32 hash;
bfb564e7
KK
4345
4346 if (skb_rx_queue_recorded(skb)) {
4347 u16 index = skb_get_rx_queue(skb);
567e4b79 4348
62fe0b40
BH
4349 if (unlikely(index >= dev->real_num_rx_queues)) {
4350 WARN_ONCE(dev->real_num_rx_queues > 1,
4351 "%s received packet on queue %u, but number "
4352 "of RX queues is %u\n",
4353 dev->name, index, dev->real_num_rx_queues);
bfb564e7
KK
4354 goto done;
4355 }
567e4b79
ED
4356 rxqueue += index;
4357 }
bfb564e7 4358
567e4b79
ED
4359 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4360
4361 flow_table = rcu_dereference(rxqueue->rps_flow_table);
6e3f7faf 4362 map = rcu_dereference(rxqueue->rps_map);
567e4b79 4363 if (!flow_table && !map)
bfb564e7
KK
4364 goto done;
4365
2d47b459 4366 skb_reset_network_header(skb);
61b905da
TH
4367 hash = skb_get_hash(skb);
4368 if (!hash)
bfb564e7
KK
4369 goto done;
4370
fec5e652
TH
4371 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4372 if (flow_table && sock_flow_table) {
fec5e652 4373 struct rps_dev_flow *rflow;
567e4b79
ED
4374 u32 next_cpu;
4375 u32 ident;
4376
4377 /* First check into global flow table if there is a match */
4378 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4379 if ((ident ^ hash) & ~rps_cpu_mask)
4380 goto try_rps;
fec5e652 4381
567e4b79
ED
4382 next_cpu = ident & rps_cpu_mask;
4383
4384 /* OK, now we know there is a match,
4385 * we can look at the local (per receive queue) flow table
4386 */
61b905da 4387 rflow = &flow_table->flows[hash & flow_table->mask];
fec5e652
TH
4388 tcpu = rflow->cpu;
4389
fec5e652
TH
4390 /*
4391 * If the desired CPU (where last recvmsg was done) is
4392 * different from current CPU (one in the rx-queue flow
4393 * table entry), switch if one of the following holds:
a31196b0 4394 * - Current CPU is unset (>= nr_cpu_ids).
fec5e652
TH
4395 * - Current CPU is offline.
4396 * - The current CPU's queue tail has advanced beyond the
4397 * last packet that was enqueued using this table entry.
4398 * This guarantees that all previous packets for the flow
4399 * have been dequeued, thus preserving in order delivery.
4400 */
4401 if (unlikely(tcpu != next_cpu) &&
a31196b0 4402 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
fec5e652 4403 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
baefa31d
TH
4404 rflow->last_qtail)) >= 0)) {
4405 tcpu = next_cpu;
c445477d 4406 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
baefa31d 4407 }
c445477d 4408
a31196b0 4409 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
fec5e652
TH
4410 *rflowp = rflow;
4411 cpu = tcpu;
4412 goto done;
4413 }
4414 }
4415
567e4b79
ED
4416try_rps:
4417
0a9627f2 4418 if (map) {
8fc54f68 4419 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
0a9627f2
TH
4420 if (cpu_online(tcpu)) {
4421 cpu = tcpu;
4422 goto done;
4423 }
4424 }
4425
4426done:
0a9627f2
TH
4427 return cpu;
4428}
4429
c445477d
BH
4430#ifdef CONFIG_RFS_ACCEL
4431
4432/**
4433 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4434 * @dev: Device on which the filter was set
4435 * @rxq_index: RX queue index
4436 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4437 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4438 *
4439 * Drivers that implement ndo_rx_flow_steer() should periodically call
4440 * this function for each installed filter and remove the filters for
4441 * which it returns %true.
4442 */
4443bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4444 u32 flow_id, u16 filter_id)
4445{
4446 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4447 struct rps_dev_flow_table *flow_table;
4448 struct rps_dev_flow *rflow;
4449 bool expire = true;
a31196b0 4450 unsigned int cpu;
c445477d
BH
4451
4452 rcu_read_lock();
4453 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4454 if (flow_table && flow_id <= flow_table->mask) {
4455 rflow = &flow_table->flows[flow_id];
6aa7de05 4456 cpu = READ_ONCE(rflow->cpu);
a31196b0 4457 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
c445477d
BH
4458 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4459 rflow->last_qtail) <
4460 (int)(10 * flow_table->mask)))
4461 expire = false;
4462 }
4463 rcu_read_unlock();
4464 return expire;
4465}
4466EXPORT_SYMBOL(rps_may_expire_flow);
4467
4468#endif /* CONFIG_RFS_ACCEL */
4469
0a9627f2 4470/* Called from hardirq (IPI) context */
e36fa2f7 4471static void rps_trigger_softirq(void *data)
0a9627f2 4472{
e36fa2f7
ED
4473 struct softnet_data *sd = data;
4474
eecfd7c4 4475 ____napi_schedule(sd, &sd->backlog);
dee42870 4476 sd->received_rps++;
0a9627f2 4477}
e36fa2f7 4478
fec5e652 4479#endif /* CONFIG_RPS */
0a9627f2 4480
e36fa2f7
ED
4481/*
4482 * Check if this softnet_data structure is another cpu one
4483 * If yes, queue it to our IPI list and return 1
4484 * If no, return 0
4485 */
4486static int rps_ipi_queued(struct softnet_data *sd)
4487{
4488#ifdef CONFIG_RPS
903ceff7 4489 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
e36fa2f7
ED
4490
4491 if (sd != mysd) {
4492 sd->rps_ipi_next = mysd->rps_ipi_list;
4493 mysd->rps_ipi_list = sd;
4494
4495 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4496 return 1;
4497 }
4498#endif /* CONFIG_RPS */
4499 return 0;
4500}
4501
99bbc707
WB
4502#ifdef CONFIG_NET_FLOW_LIMIT
4503int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4504#endif
4505
4506static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4507{
4508#ifdef CONFIG_NET_FLOW_LIMIT
4509 struct sd_flow_limit *fl;
4510 struct softnet_data *sd;
4511 unsigned int old_flow, new_flow;
4512
4513 if (qlen < (netdev_max_backlog >> 1))
4514 return false;
4515
903ceff7 4516 sd = this_cpu_ptr(&softnet_data);
99bbc707
WB
4517
4518 rcu_read_lock();
4519 fl = rcu_dereference(sd->flow_limit);
4520 if (fl) {
3958afa1 4521 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
99bbc707
WB
4522 old_flow = fl->history[fl->history_head];
4523 fl->history[fl->history_head] = new_flow;
4524
4525 fl->history_head++;
4526 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4527
4528 if (likely(fl->buckets[old_flow]))
4529 fl->buckets[old_flow]--;
4530
4531 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4532 fl->count++;
4533 rcu_read_unlock();
4534 return true;
4535 }
4536 }
4537 rcu_read_unlock();
4538#endif
4539 return false;
4540}
4541
0a9627f2
TH
4542/*
4543 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4544 * queue (may be a remote CPU queue).
4545 */
fec5e652
TH
4546static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4547 unsigned int *qtail)
0a9627f2 4548{
e36fa2f7 4549 struct softnet_data *sd;
0a9627f2 4550 unsigned long flags;
99bbc707 4551 unsigned int qlen;
0a9627f2 4552
e36fa2f7 4553 sd = &per_cpu(softnet_data, cpu);
0a9627f2
TH
4554
4555 local_irq_save(flags);
0a9627f2 4556
e36fa2f7 4557 rps_lock(sd);
e9e4dd32
JA
4558 if (!netif_running(skb->dev))
4559 goto drop;
99bbc707
WB
4560 qlen = skb_queue_len(&sd->input_pkt_queue);
4561 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
e008f3f0 4562 if (qlen) {
0a9627f2 4563enqueue:
e36fa2f7 4564 __skb_queue_tail(&sd->input_pkt_queue, skb);
76cc8b13 4565 input_queue_tail_incr_save(sd, qtail);
e36fa2f7 4566 rps_unlock(sd);
152102c7 4567 local_irq_restore(flags);
0a9627f2
TH
4568 return NET_RX_SUCCESS;
4569 }
4570
ebda37c2
ED
4571 /* Schedule NAPI for backlog device
4572 * We can use non atomic operation since we own the queue lock
4573 */
4574 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
e36fa2f7 4575 if (!rps_ipi_queued(sd))
eecfd7c4 4576 ____napi_schedule(sd, &sd->backlog);
0a9627f2
TH
4577 }
4578 goto enqueue;
4579 }
4580
e9e4dd32 4581drop:
dee42870 4582 sd->dropped++;
e36fa2f7 4583 rps_unlock(sd);
0a9627f2 4584
0a9627f2
TH
4585 local_irq_restore(flags);
4586
caf586e5 4587 atomic_long_inc(&skb->dev->rx_dropped);
0a9627f2
TH
4588 kfree_skb(skb);
4589 return NET_RX_DROP;
4590}
1da177e4 4591
e817f856
JDB
4592static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4593{
4594 struct net_device *dev = skb->dev;
4595 struct netdev_rx_queue *rxqueue;
4596
4597 rxqueue = dev->_rx;
4598
4599 if (skb_rx_queue_recorded(skb)) {
4600 u16 index = skb_get_rx_queue(skb);
4601
4602 if (unlikely(index >= dev->real_num_rx_queues)) {
4603 WARN_ONCE(dev->real_num_rx_queues > 1,
4604 "%s received packet on queue %u, but number "
4605 "of RX queues is %u\n",
4606 dev->name, index, dev->real_num_rx_queues);
4607
4608 return rxqueue; /* Return first rxqueue */
4609 }
4610 rxqueue += index;
4611 }
4612 return rxqueue;
4613}
4614
d4455169 4615static u32 netif_receive_generic_xdp(struct sk_buff *skb,
02671e23 4616 struct xdp_buff *xdp,
d4455169
JF
4617 struct bpf_prog *xdp_prog)
4618{
be9df4af 4619 void *orig_data, *orig_data_end, *hard_start;
e817f856 4620 struct netdev_rx_queue *rxqueue;
de8f3a83 4621 u32 metalen, act = XDP_DROP;
43b5169d 4622 u32 mac_len, frame_sz;
29724956
JDB
4623 __be16 orig_eth_type;
4624 struct ethhdr *eth;
4625 bool orig_bcast;
be9df4af 4626 int off;
d4455169
JF
4627
4628 /* Reinjected packets coming from act_mirred or similar should
4629 * not get XDP generic processing.
4630 */
2c64605b 4631 if (skb_is_redirected(skb))
d4455169
JF
4632 return XDP_PASS;
4633
de8f3a83
DB
4634 /* XDP packets must be linear and must have sufficient headroom
4635 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4636 * native XDP provides, thus we need to do it here as well.
4637 */
ad1e03b2 4638 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
de8f3a83
DB
4639 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4640 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4641 int troom = skb->tail + skb->data_len - skb->end;
4642
4643 /* In case we have to go down the path and also linearize,
4644 * then lets do the pskb_expand_head() work just once here.
4645 */
4646 if (pskb_expand_head(skb,
4647 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4648 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4649 goto do_drop;
2d17d8d7 4650 if (skb_linearize(skb))
de8f3a83
DB
4651 goto do_drop;
4652 }
d4455169
JF
4653
4654 /* The XDP program wants to see the packet starting at the MAC
4655 * header.
4656 */
4657 mac_len = skb->data - skb_mac_header(skb);
be9df4af 4658 hard_start = skb->data - skb_headroom(skb);
a075767b
JDB
4659
4660 /* SKB "head" area always have tailroom for skb_shared_info */
be9df4af 4661 frame_sz = (void *)skb_end_pointer(skb) - hard_start;
43b5169d 4662 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
a075767b 4663
be9df4af
LB
4664 rxqueue = netif_get_rxqueue(skb);
4665 xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
4666 xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
4667 skb_headlen(skb) + mac_len, true);
a075767b 4668
02671e23
BT
4669 orig_data_end = xdp->data_end;
4670 orig_data = xdp->data;
29724956
JDB
4671 eth = (struct ethhdr *)xdp->data;
4672 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4673 orig_eth_type = eth->h_proto;
d4455169 4674
02671e23 4675 act = bpf_prog_run_xdp(xdp_prog, xdp);
d4455169 4676
065af355 4677 /* check if bpf_xdp_adjust_head was used */
02671e23 4678 off = xdp->data - orig_data;
065af355
JDB
4679 if (off) {
4680 if (off > 0)
4681 __skb_pull(skb, off);
4682 else if (off < 0)
4683 __skb_push(skb, -off);
4684
4685 skb->mac_header += off;
4686 skb_reset_network_header(skb);
4687 }
d4455169 4688
a075767b
JDB
4689 /* check if bpf_xdp_adjust_tail was used */
4690 off = xdp->data_end - orig_data_end;
f7613120 4691 if (off != 0) {
02671e23 4692 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
a075767b 4693 skb->len += off; /* positive on grow, negative on shrink */
f7613120 4694 }
198d83bb 4695
29724956
JDB
4696 /* check if XDP changed eth hdr such SKB needs update */
4697 eth = (struct ethhdr *)xdp->data;
4698 if ((orig_eth_type != eth->h_proto) ||
4699 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4700 __skb_push(skb, ETH_HLEN);
4701 skb->protocol = eth_type_trans(skb, skb->dev);
4702 }
4703
d4455169 4704 switch (act) {
6103aa96 4705 case XDP_REDIRECT:
d4455169
JF
4706 case XDP_TX:
4707 __skb_push(skb, mac_len);
de8f3a83 4708 break;
d4455169 4709 case XDP_PASS:
02671e23 4710 metalen = xdp->data - xdp->data_meta;
de8f3a83
DB
4711 if (metalen)
4712 skb_metadata_set(skb, metalen);
d4455169 4713 break;
d4455169
JF
4714 default:
4715 bpf_warn_invalid_xdp_action(act);
df561f66 4716 fallthrough;
d4455169
JF
4717 case XDP_ABORTED:
4718 trace_xdp_exception(skb->dev, xdp_prog, act);
df561f66 4719 fallthrough;
d4455169
JF
4720 case XDP_DROP:
4721 do_drop:
4722 kfree_skb(skb);
4723 break;
4724 }
4725
4726 return act;
4727}
4728
4729/* When doing generic XDP we have to bypass the qdisc layer and the
4730 * network taps in order to match in-driver-XDP behavior.
4731 */
7c497478 4732void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
d4455169
JF
4733{
4734 struct net_device *dev = skb->dev;
4735 struct netdev_queue *txq;
4736 bool free_skb = true;
4737 int cpu, rc;
4738
4bd97d51 4739 txq = netdev_core_pick_tx(dev, skb, NULL);
d4455169
JF
4740 cpu = smp_processor_id();
4741 HARD_TX_LOCK(dev, txq, cpu);
4742 if (!netif_xmit_stopped(txq)) {
4743 rc = netdev_start_xmit(skb, dev, txq, 0);
4744 if (dev_xmit_complete(rc))
4745 free_skb = false;
4746 }
4747 HARD_TX_UNLOCK(dev, txq);
4748 if (free_skb) {
4749 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4750 kfree_skb(skb);
4751 }
4752}
4753
02786475 4754static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
d4455169 4755
7c497478 4756int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
d4455169 4757{
d4455169 4758 if (xdp_prog) {
02671e23
BT
4759 struct xdp_buff xdp;
4760 u32 act;
6103aa96 4761 int err;
d4455169 4762
02671e23 4763 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
d4455169 4764 if (act != XDP_PASS) {
6103aa96
JF
4765 switch (act) {
4766 case XDP_REDIRECT:
2facaad6 4767 err = xdp_do_generic_redirect(skb->dev, skb,
02671e23 4768 &xdp, xdp_prog);
6103aa96
JF
4769 if (err)
4770 goto out_redir;
02671e23 4771 break;
6103aa96 4772 case XDP_TX:
d4455169 4773 generic_xdp_tx(skb, xdp_prog);
6103aa96
JF
4774 break;
4775 }
d4455169
JF
4776 return XDP_DROP;
4777 }
4778 }
4779 return XDP_PASS;
6103aa96 4780out_redir:
6103aa96
JF
4781 kfree_skb(skb);
4782 return XDP_DROP;
d4455169 4783}
7c497478 4784EXPORT_SYMBOL_GPL(do_xdp_generic);
d4455169 4785
ae78dbfa 4786static int netif_rx_internal(struct sk_buff *skb)
1da177e4 4787{
b0e28f1e 4788 int ret;
1da177e4 4789
588f0330 4790 net_timestamp_check(netdev_tstamp_prequeue, skb);
1da177e4 4791
cf66ba58 4792 trace_netif_rx(skb);
d4455169 4793
df334545 4794#ifdef CONFIG_RPS
dc05360f 4795 if (static_branch_unlikely(&rps_needed)) {
fec5e652 4796 struct rps_dev_flow voidflow, *rflow = &voidflow;
b0e28f1e
ED
4797 int cpu;
4798
cece1945 4799 preempt_disable();
b0e28f1e 4800 rcu_read_lock();
fec5e652
TH
4801
4802 cpu = get_rps_cpu(skb->dev, skb, &rflow);
b0e28f1e
ED
4803 if (cpu < 0)
4804 cpu = smp_processor_id();
fec5e652
TH
4805
4806 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4807
b0e28f1e 4808 rcu_read_unlock();
cece1945 4809 preempt_enable();
adc9300e
ED
4810 } else
4811#endif
fec5e652
TH
4812 {
4813 unsigned int qtail;
f4563a75 4814
fec5e652
TH
4815 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4816 put_cpu();
4817 }
b0e28f1e 4818 return ret;
1da177e4 4819}
ae78dbfa
BH
4820
4821/**
4822 * netif_rx - post buffer to the network code
4823 * @skb: buffer to post
4824 *
4825 * This function receives a packet from a device driver and queues it for
4826 * the upper (protocol) levels to process. It always succeeds. The buffer
4827 * may be dropped during processing for congestion control or by the
4828 * protocol layers.
4829 *
4830 * return values:
4831 * NET_RX_SUCCESS (no congestion)
4832 * NET_RX_DROP (packet was dropped)
4833 *
4834 */
4835
4836int netif_rx(struct sk_buff *skb)
4837{
b0e3f1bd
GB
4838 int ret;
4839
ae78dbfa
BH
4840 trace_netif_rx_entry(skb);
4841
b0e3f1bd
GB
4842 ret = netif_rx_internal(skb);
4843 trace_netif_rx_exit(ret);
4844
4845 return ret;
ae78dbfa 4846}
d1b19dff 4847EXPORT_SYMBOL(netif_rx);
1da177e4
LT
4848
4849int netif_rx_ni(struct sk_buff *skb)
4850{
4851 int err;
4852
ae78dbfa
BH
4853 trace_netif_rx_ni_entry(skb);
4854
1da177e4 4855 preempt_disable();
ae78dbfa 4856 err = netif_rx_internal(skb);
1da177e4
LT
4857 if (local_softirq_pending())
4858 do_softirq();
4859 preempt_enable();
b0e3f1bd 4860 trace_netif_rx_ni_exit(err);
1da177e4
LT
4861
4862 return err;
4863}
1da177e4
LT
4864EXPORT_SYMBOL(netif_rx_ni);
4865
c11171a4
SAS
4866int netif_rx_any_context(struct sk_buff *skb)
4867{
4868 /*
4869 * If invoked from contexts which do not invoke bottom half
4870 * processing either at return from interrupt or when softrqs are
4871 * reenabled, use netif_rx_ni() which invokes bottomhalf processing
4872 * directly.
4873 */
4874 if (in_interrupt())
4875 return netif_rx(skb);
4876 else
4877 return netif_rx_ni(skb);
4878}
4879EXPORT_SYMBOL(netif_rx_any_context);
4880
0766f788 4881static __latent_entropy void net_tx_action(struct softirq_action *h)
1da177e4 4882{
903ceff7 4883 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
1da177e4
LT
4884
4885 if (sd->completion_queue) {
4886 struct sk_buff *clist;
4887
4888 local_irq_disable();
4889 clist = sd->completion_queue;
4890 sd->completion_queue = NULL;
4891 local_irq_enable();
4892
4893 while (clist) {
4894 struct sk_buff *skb = clist;
f4563a75 4895
1da177e4
LT
4896 clist = clist->next;
4897
63354797 4898 WARN_ON(refcount_read(&skb->users));
e6247027
ED
4899 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4900 trace_consume_skb(skb);
4901 else
4902 trace_kfree_skb(skb, net_tx_action);
15fad714
JDB
4903
4904 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4905 __kfree_skb(skb);
4906 else
4907 __kfree_skb_defer(skb);
1da177e4 4908 }
15fad714
JDB
4909
4910 __kfree_skb_flush();
1da177e4
LT
4911 }
4912
4913 if (sd->output_queue) {
37437bb2 4914 struct Qdisc *head;
1da177e4
LT
4915
4916 local_irq_disable();
4917 head = sd->output_queue;
4918 sd->output_queue = NULL;
a9cbd588 4919 sd->output_queue_tailp = &sd->output_queue;
1da177e4
LT
4920 local_irq_enable();
4921
4922 while (head) {
37437bb2 4923 struct Qdisc *q = head;
6b3ba914 4924 spinlock_t *root_lock = NULL;
37437bb2 4925
1da177e4
LT
4926 head = head->next_sched;
4927
6b3ba914
JF
4928 if (!(q->flags & TCQ_F_NOLOCK)) {
4929 root_lock = qdisc_lock(q);
4930 spin_lock(root_lock);
4931 }
3bcb846c
ED
4932 /* We need to make sure head->next_sched is read
4933 * before clearing __QDISC_STATE_SCHED
4934 */
4935 smp_mb__before_atomic();
4936 clear_bit(__QDISC_STATE_SCHED, &q->state);
4937 qdisc_run(q);
6b3ba914
JF
4938 if (root_lock)
4939 spin_unlock(root_lock);
1da177e4
LT
4940 }
4941 }
f53c7239
SK
4942
4943 xfrm_dev_backlog(sd);
1da177e4
LT
4944}
4945
181402a5 4946#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
da678292
MM
4947/* This hook is defined here for ATM LANE */
4948int (*br_fdb_test_addr_hook)(struct net_device *dev,
4949 unsigned char *addr) __read_mostly;
4fb019a0 4950EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
da678292 4951#endif
1da177e4 4952
1f211a1b
DB
4953static inline struct sk_buff *
4954sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
9aa1206e 4955 struct net_device *orig_dev, bool *another)
f697c3e8 4956{
e7582bab 4957#ifdef CONFIG_NET_CLS_ACT
46209401 4958 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
d2788d34 4959 struct tcf_result cl_res;
24824a09 4960
c9e99fd0
DB
4961 /* If there's at least one ingress present somewhere (so
4962 * we get here via enabled static key), remaining devices
4963 * that are not configured with an ingress qdisc will bail
d2788d34 4964 * out here.
c9e99fd0 4965 */
46209401 4966 if (!miniq)
4577139b 4967 return skb;
46209401 4968
f697c3e8
HX
4969 if (*pt_prev) {
4970 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4971 *pt_prev = NULL;
1da177e4
LT
4972 }
4973
3365495c 4974 qdisc_skb_cb(skb)->pkt_len = skb->len;
aadaca9e 4975 qdisc_skb_cb(skb)->mru = 0;
7baf2429 4976 qdisc_skb_cb(skb)->post_ct = false;
8dc07fdb 4977 skb->tc_at_ingress = 1;
46209401 4978 mini_qdisc_bstats_cpu_update(miniq, skb);
c9e99fd0 4979
7d17c544
PB
4980 switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list,
4981 &cl_res, false)) {
d2788d34
DB
4982 case TC_ACT_OK:
4983 case TC_ACT_RECLASSIFY:
4984 skb->tc_index = TC_H_MIN(cl_res.classid);
4985 break;
4986 case TC_ACT_SHOT:
46209401 4987 mini_qdisc_qstats_cpu_drop(miniq);
8a3a4c6e
ED
4988 kfree_skb(skb);
4989 return NULL;
d2788d34
DB
4990 case TC_ACT_STOLEN:
4991 case TC_ACT_QUEUED:
e25ea21f 4992 case TC_ACT_TRAP:
8a3a4c6e 4993 consume_skb(skb);
d2788d34 4994 return NULL;
27b29f63
AS
4995 case TC_ACT_REDIRECT:
4996 /* skb_mac_header check was done by cls/act_bpf, so
4997 * we can safely push the L2 header back before
4998 * redirecting to another netdev
4999 */
5000 __skb_push(skb, skb->mac_len);
9aa1206e
DB
5001 if (skb_do_redirect(skb) == -EAGAIN) {
5002 __skb_pull(skb, skb->mac_len);
5003 *another = true;
5004 break;
5005 }
27b29f63 5006 return NULL;
720f22fe 5007 case TC_ACT_CONSUMED:
cd11b164 5008 return NULL;
d2788d34
DB
5009 default:
5010 break;
f697c3e8 5011 }
e7582bab 5012#endif /* CONFIG_NET_CLS_ACT */
e687ad60
PN
5013 return skb;
5014}
1da177e4 5015
24b27fc4
MB
5016/**
5017 * netdev_is_rx_handler_busy - check if receive handler is registered
5018 * @dev: device to check
5019 *
5020 * Check if a receive handler is already registered for a given device.
5021 * Return true if there one.
5022 *
5023 * The caller must hold the rtnl_mutex.
5024 */
5025bool netdev_is_rx_handler_busy(struct net_device *dev)
5026{
5027 ASSERT_RTNL();
5028 return dev && rtnl_dereference(dev->rx_handler);
5029}
5030EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5031
ab95bfe0
JP
5032/**
5033 * netdev_rx_handler_register - register receive handler
5034 * @dev: device to register a handler for
5035 * @rx_handler: receive handler to register
93e2c32b 5036 * @rx_handler_data: data pointer that is used by rx handler
ab95bfe0 5037 *
e227867f 5038 * Register a receive handler for a device. This handler will then be
ab95bfe0
JP
5039 * called from __netif_receive_skb. A negative errno code is returned
5040 * on a failure.
5041 *
5042 * The caller must hold the rtnl_mutex.
8a4eb573
JP
5043 *
5044 * For a general description of rx_handler, see enum rx_handler_result.
ab95bfe0
JP
5045 */
5046int netdev_rx_handler_register(struct net_device *dev,
93e2c32b
JP
5047 rx_handler_func_t *rx_handler,
5048 void *rx_handler_data)
ab95bfe0 5049{
1b7cd004 5050 if (netdev_is_rx_handler_busy(dev))
ab95bfe0
JP
5051 return -EBUSY;
5052
f5426250
PA
5053 if (dev->priv_flags & IFF_NO_RX_HANDLER)
5054 return -EINVAL;
5055
00cfec37 5056 /* Note: rx_handler_data must be set before rx_handler */
93e2c32b 5057 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
ab95bfe0
JP
5058 rcu_assign_pointer(dev->rx_handler, rx_handler);
5059
5060 return 0;
5061}
5062EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5063
5064/**
5065 * netdev_rx_handler_unregister - unregister receive handler
5066 * @dev: device to unregister a handler from
5067 *
166ec369 5068 * Unregister a receive handler from a device.
ab95bfe0
JP
5069 *
5070 * The caller must hold the rtnl_mutex.
5071 */
5072void netdev_rx_handler_unregister(struct net_device *dev)
5073{
5074
5075 ASSERT_RTNL();
a9b3cd7f 5076 RCU_INIT_POINTER(dev->rx_handler, NULL);
00cfec37
ED
5077 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5078 * section has a guarantee to see a non NULL rx_handler_data
5079 * as well.
5080 */
5081 synchronize_net();
a9b3cd7f 5082 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
ab95bfe0
JP
5083}
5084EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5085
b4b9e355
MG
5086/*
5087 * Limit the use of PFMEMALLOC reserves to those protocols that implement
5088 * the special handling of PFMEMALLOC skbs.
5089 */
5090static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5091{
5092 switch (skb->protocol) {
2b8837ae
JP
5093 case htons(ETH_P_ARP):
5094 case htons(ETH_P_IP):
5095 case htons(ETH_P_IPV6):
5096 case htons(ETH_P_8021Q):
5097 case htons(ETH_P_8021AD):
b4b9e355
MG
5098 return true;
5099 default:
5100 return false;
5101 }
5102}
5103
e687ad60
PN
5104static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5105 int *ret, struct net_device *orig_dev)
5106{
5107 if (nf_hook_ingress_active(skb)) {
2c1e2703
AC
5108 int ingress_retval;
5109
e687ad60
PN
5110 if (*pt_prev) {
5111 *ret = deliver_skb(skb, *pt_prev, orig_dev);
5112 *pt_prev = NULL;
5113 }
5114
2c1e2703
AC
5115 rcu_read_lock();
5116 ingress_retval = nf_hook_ingress(skb);
5117 rcu_read_unlock();
5118 return ingress_retval;
e687ad60
PN
5119 }
5120 return 0;
5121}
e687ad60 5122
c0bbbdc3 5123static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
88eb1944 5124 struct packet_type **ppt_prev)
1da177e4
LT
5125{
5126 struct packet_type *ptype, *pt_prev;
ab95bfe0 5127 rx_handler_func_t *rx_handler;
c0bbbdc3 5128 struct sk_buff *skb = *pskb;
f2ccd8fa 5129 struct net_device *orig_dev;
8a4eb573 5130 bool deliver_exact = false;
1da177e4 5131 int ret = NET_RX_DROP;
252e3346 5132 __be16 type;
1da177e4 5133
588f0330 5134 net_timestamp_check(!netdev_tstamp_prequeue, skb);
81bbb3d4 5135
cf66ba58 5136 trace_netif_receive_skb(skb);
9b22ea56 5137
cc9bd5ce 5138 orig_dev = skb->dev;
8f903c70 5139
c1d2bbe1 5140 skb_reset_network_header(skb);
fda55eca
ED
5141 if (!skb_transport_header_was_set(skb))
5142 skb_reset_transport_header(skb);
0b5c9db1 5143 skb_reset_mac_len(skb);
1da177e4
LT
5144
5145 pt_prev = NULL;
5146
63d8ea7f 5147another_round:
b6858177 5148 skb->skb_iif = skb->dev->ifindex;
63d8ea7f
DM
5149
5150 __this_cpu_inc(softnet_data.processed);
5151
458bf2f2
SH
5152 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5153 int ret2;
5154
5155 preempt_disable();
5156 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5157 preempt_enable();
5158
c0bbbdc3
BS
5159 if (ret2 != XDP_PASS) {
5160 ret = NET_RX_DROP;
5161 goto out;
5162 }
458bf2f2
SH
5163 skb_reset_mac_len(skb);
5164 }
5165
324cefaf 5166 if (eth_type_vlan(skb->protocol)) {
0d5501c1 5167 skb = skb_vlan_untag(skb);
bcc6d479 5168 if (unlikely(!skb))
2c17d27c 5169 goto out;
bcc6d479
JP
5170 }
5171
e7246e12
WB
5172 if (skb_skip_tc_classify(skb))
5173 goto skip_classify;
1da177e4 5174
9754e293 5175 if (pfmemalloc)
b4b9e355
MG
5176 goto skip_taps;
5177
1da177e4 5178 list_for_each_entry_rcu(ptype, &ptype_all, list) {
7866a621
SN
5179 if (pt_prev)
5180 ret = deliver_skb(skb, pt_prev, orig_dev);
5181 pt_prev = ptype;
5182 }
5183
5184 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5185 if (pt_prev)
5186 ret = deliver_skb(skb, pt_prev, orig_dev);
5187 pt_prev = ptype;
1da177e4
LT
5188 }
5189
b4b9e355 5190skip_taps:
1cf51900 5191#ifdef CONFIG_NET_INGRESS
aabf6772 5192 if (static_branch_unlikely(&ingress_needed_key)) {
9aa1206e
DB
5193 bool another = false;
5194
5195 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5196 &another);
5197 if (another)
5198 goto another_round;
4577139b 5199 if (!skb)
2c17d27c 5200 goto out;
e687ad60
PN
5201
5202 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
2c17d27c 5203 goto out;
4577139b 5204 }
1cf51900 5205#endif
2c64605b 5206 skb_reset_redirect(skb);
e7246e12 5207skip_classify:
9754e293 5208 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
b4b9e355
MG
5209 goto drop;
5210
df8a39de 5211 if (skb_vlan_tag_present(skb)) {
2425717b
JF
5212 if (pt_prev) {
5213 ret = deliver_skb(skb, pt_prev, orig_dev);
5214 pt_prev = NULL;
5215 }
48cc32d3 5216 if (vlan_do_receive(&skb))
2425717b
JF
5217 goto another_round;
5218 else if (unlikely(!skb))
2c17d27c 5219 goto out;
2425717b
JF
5220 }
5221
48cc32d3 5222 rx_handler = rcu_dereference(skb->dev->rx_handler);
ab95bfe0
JP
5223 if (rx_handler) {
5224 if (pt_prev) {
5225 ret = deliver_skb(skb, pt_prev, orig_dev);
5226 pt_prev = NULL;
5227 }
8a4eb573
JP
5228 switch (rx_handler(&skb)) {
5229 case RX_HANDLER_CONSUMED:
3bc1b1ad 5230 ret = NET_RX_SUCCESS;
2c17d27c 5231 goto out;
8a4eb573 5232 case RX_HANDLER_ANOTHER:
63d8ea7f 5233 goto another_round;
8a4eb573
JP
5234 case RX_HANDLER_EXACT:
5235 deliver_exact = true;
5236 case RX_HANDLER_PASS:
5237 break;
5238 default:
5239 BUG();
5240 }
ab95bfe0 5241 }
1da177e4 5242
b14a9fc4 5243 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
36b2f61a
GV
5244check_vlan_id:
5245 if (skb_vlan_tag_get_id(skb)) {
5246 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5247 * find vlan device.
5248 */
d4b812de 5249 skb->pkt_type = PACKET_OTHERHOST;
324cefaf 5250 } else if (eth_type_vlan(skb->protocol)) {
36b2f61a
GV
5251 /* Outer header is 802.1P with vlan 0, inner header is
5252 * 802.1Q or 802.1AD and vlan_do_receive() above could
5253 * not find vlan dev for vlan id 0.
5254 */
5255 __vlan_hwaccel_clear_tag(skb);
5256 skb = skb_vlan_untag(skb);
5257 if (unlikely(!skb))
5258 goto out;
5259 if (vlan_do_receive(&skb))
5260 /* After stripping off 802.1P header with vlan 0
5261 * vlan dev is found for inner header.
5262 */
5263 goto another_round;
5264 else if (unlikely(!skb))
5265 goto out;
5266 else
5267 /* We have stripped outer 802.1P vlan 0 header.
5268 * But could not find vlan dev.
5269 * check again for vlan id to set OTHERHOST.
5270 */
5271 goto check_vlan_id;
5272 }
d4b812de
ED
5273 /* Note: we might in the future use prio bits
5274 * and set skb->priority like in vlan_do_receive()
5275 * For the time being, just ignore Priority Code Point
5276 */
b1817524 5277 __vlan_hwaccel_clear_tag(skb);
d4b812de 5278 }
48cc32d3 5279
7866a621
SN
5280 type = skb->protocol;
5281
63d8ea7f 5282 /* deliver only exact match when indicated */
7866a621
SN
5283 if (likely(!deliver_exact)) {
5284 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5285 &ptype_base[ntohs(type) &
5286 PTYPE_HASH_MASK]);
5287 }
1f3c8804 5288
7866a621
SN
5289 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5290 &orig_dev->ptype_specific);
5291
5292 if (unlikely(skb->dev != orig_dev)) {
5293 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5294 &skb->dev->ptype_specific);
1da177e4
LT
5295 }
5296
5297 if (pt_prev) {
1f8b977a 5298 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
0e698bf6 5299 goto drop;
88eb1944 5300 *ppt_prev = pt_prev;
1da177e4 5301 } else {
b4b9e355 5302drop:
6e7333d3
JW
5303 if (!deliver_exact)
5304 atomic_long_inc(&skb->dev->rx_dropped);
5305 else
5306 atomic_long_inc(&skb->dev->rx_nohandler);
1da177e4
LT
5307 kfree_skb(skb);
5308 /* Jamal, now you will not able to escape explaining
5309 * me how you were going to use this. :-)
5310 */
5311 ret = NET_RX_DROP;
5312 }
5313
2c17d27c 5314out:
c0bbbdc3
BS
5315 /* The invariant here is that if *ppt_prev is not NULL
5316 * then skb should also be non-NULL.
5317 *
5318 * Apparently *ppt_prev assignment above holds this invariant due to
5319 * skb dereferencing near it.
5320 */
5321 *pskb = skb;
9754e293
DM
5322 return ret;
5323}
5324
88eb1944
EC
5325static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5326{
5327 struct net_device *orig_dev = skb->dev;
5328 struct packet_type *pt_prev = NULL;
5329 int ret;
5330
c0bbbdc3 5331 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
88eb1944 5332 if (pt_prev)
f5737cba
PA
5333 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5334 skb->dev, pt_prev, orig_dev);
88eb1944
EC
5335 return ret;
5336}
5337
1c601d82
JDB
5338/**
5339 * netif_receive_skb_core - special purpose version of netif_receive_skb
5340 * @skb: buffer to process
5341 *
5342 * More direct receive version of netif_receive_skb(). It should
5343 * only be used by callers that have a need to skip RPS and Generic XDP.
2de9780f 5344 * Caller must also take care of handling if ``(page_is_)pfmemalloc``.
1c601d82
JDB
5345 *
5346 * This function may only be called from softirq context and interrupts
5347 * should be enabled.
5348 *
5349 * Return values (usually ignored):
5350 * NET_RX_SUCCESS: no congestion
5351 * NET_RX_DROP: packet was dropped
5352 */
5353int netif_receive_skb_core(struct sk_buff *skb)
5354{
5355 int ret;
5356
5357 rcu_read_lock();
88eb1944 5358 ret = __netif_receive_skb_one_core(skb, false);
1c601d82
JDB
5359 rcu_read_unlock();
5360
5361 return ret;
5362}
5363EXPORT_SYMBOL(netif_receive_skb_core);
5364
88eb1944
EC
5365static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5366 struct packet_type *pt_prev,
5367 struct net_device *orig_dev)
4ce0017a
EC
5368{
5369 struct sk_buff *skb, *next;
5370
88eb1944
EC
5371 if (!pt_prev)
5372 return;
5373 if (list_empty(head))
5374 return;
17266ee9 5375 if (pt_prev->list_func != NULL)
fdf71426
PA
5376 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5377 ip_list_rcv, head, pt_prev, orig_dev);
17266ee9 5378 else
9a5a90d1
AL
5379 list_for_each_entry_safe(skb, next, head, list) {
5380 skb_list_del_init(skb);
fdf71426 5381 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
9a5a90d1 5382 }
88eb1944
EC
5383}
5384
5385static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5386{
5387 /* Fast-path assumptions:
5388 * - There is no RX handler.
5389 * - Only one packet_type matches.
5390 * If either of these fails, we will end up doing some per-packet
5391 * processing in-line, then handling the 'last ptype' for the whole
5392 * sublist. This can't cause out-of-order delivery to any single ptype,
5393 * because the 'last ptype' must be constant across the sublist, and all
5394 * other ptypes are handled per-packet.
5395 */
5396 /* Current (common) ptype of sublist */
5397 struct packet_type *pt_curr = NULL;
5398 /* Current (common) orig_dev of sublist */
5399 struct net_device *od_curr = NULL;
5400 struct list_head sublist;
5401 struct sk_buff *skb, *next;
5402
9af86f93 5403 INIT_LIST_HEAD(&sublist);
88eb1944
EC
5404 list_for_each_entry_safe(skb, next, head, list) {
5405 struct net_device *orig_dev = skb->dev;
5406 struct packet_type *pt_prev = NULL;
5407
22f6bbb7 5408 skb_list_del_init(skb);
c0bbbdc3 5409 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
9af86f93
EC
5410 if (!pt_prev)
5411 continue;
88eb1944
EC
5412 if (pt_curr != pt_prev || od_curr != orig_dev) {
5413 /* dispatch old sublist */
88eb1944
EC
5414 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5415 /* start new sublist */
9af86f93 5416 INIT_LIST_HEAD(&sublist);
88eb1944
EC
5417 pt_curr = pt_prev;
5418 od_curr = orig_dev;
5419 }
9af86f93 5420 list_add_tail(&skb->list, &sublist);
88eb1944
EC
5421 }
5422
5423 /* dispatch final sublist */
9af86f93 5424 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
4ce0017a
EC
5425}
5426
9754e293
DM
5427static int __netif_receive_skb(struct sk_buff *skb)
5428{
5429 int ret;
5430
5431 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
f1083048 5432 unsigned int noreclaim_flag;
9754e293
DM
5433
5434 /*
5435 * PFMEMALLOC skbs are special, they should
5436 * - be delivered to SOCK_MEMALLOC sockets only
5437 * - stay away from userspace
5438 * - have bounded memory usage
5439 *
5440 * Use PF_MEMALLOC as this saves us from propagating the allocation
5441 * context down to all allocation sites.
5442 */
f1083048 5443 noreclaim_flag = memalloc_noreclaim_save();
88eb1944 5444 ret = __netif_receive_skb_one_core(skb, true);
f1083048 5445 memalloc_noreclaim_restore(noreclaim_flag);
9754e293 5446 } else
88eb1944 5447 ret = __netif_receive_skb_one_core(skb, false);
9754e293 5448
1da177e4
LT
5449 return ret;
5450}
0a9627f2 5451
4ce0017a
EC
5452static void __netif_receive_skb_list(struct list_head *head)
5453{
5454 unsigned long noreclaim_flag = 0;
5455 struct sk_buff *skb, *next;
5456 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5457
5458 list_for_each_entry_safe(skb, next, head, list) {
5459 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5460 struct list_head sublist;
5461
5462 /* Handle the previous sublist */
5463 list_cut_before(&sublist, head, &skb->list);
b9f463d6
EC
5464 if (!list_empty(&sublist))
5465 __netif_receive_skb_list_core(&sublist, pfmemalloc);
4ce0017a
EC
5466 pfmemalloc = !pfmemalloc;
5467 /* See comments in __netif_receive_skb */
5468 if (pfmemalloc)
5469 noreclaim_flag = memalloc_noreclaim_save();
5470 else
5471 memalloc_noreclaim_restore(noreclaim_flag);
5472 }
5473 }
5474 /* Handle the remaining sublist */
b9f463d6
EC
5475 if (!list_empty(head))
5476 __netif_receive_skb_list_core(head, pfmemalloc);
4ce0017a
EC
5477 /* Restore pflags */
5478 if (pfmemalloc)
5479 memalloc_noreclaim_restore(noreclaim_flag);
5480}
5481
f4e63525 5482static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
b5cdae32 5483{
58038695 5484 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
b5cdae32
DM
5485 struct bpf_prog *new = xdp->prog;
5486 int ret = 0;
5487
fbee97fe
DA
5488 if (new) {
5489 u32 i;
5490
984fe94f
YZ
5491 mutex_lock(&new->aux->used_maps_mutex);
5492
fbee97fe
DA
5493 /* generic XDP does not work with DEVMAPs that can
5494 * have a bpf_prog installed on an entry
5495 */
5496 for (i = 0; i < new->aux->used_map_cnt; i++) {
984fe94f
YZ
5497 if (dev_map_can_have_prog(new->aux->used_maps[i]) ||
5498 cpu_map_prog_allowed(new->aux->used_maps[i])) {
5499 mutex_unlock(&new->aux->used_maps_mutex);
92164774 5500 return -EINVAL;
984fe94f 5501 }
fbee97fe 5502 }
984fe94f
YZ
5503
5504 mutex_unlock(&new->aux->used_maps_mutex);
fbee97fe
DA
5505 }
5506
b5cdae32 5507 switch (xdp->command) {
58038695 5508 case XDP_SETUP_PROG:
b5cdae32
DM
5509 rcu_assign_pointer(dev->xdp_prog, new);
5510 if (old)
5511 bpf_prog_put(old);
5512
5513 if (old && !new) {
02786475 5514 static_branch_dec(&generic_xdp_needed_key);
b5cdae32 5515 } else if (new && !old) {
02786475 5516 static_branch_inc(&generic_xdp_needed_key);
b5cdae32 5517 dev_disable_lro(dev);
56f5aa77 5518 dev_disable_gro_hw(dev);
b5cdae32
DM
5519 }
5520 break;
b5cdae32 5521
b5cdae32
DM
5522 default:
5523 ret = -EINVAL;
5524 break;
5525 }
5526
5527 return ret;
5528}
5529
ae78dbfa 5530static int netif_receive_skb_internal(struct sk_buff *skb)
0a9627f2 5531{
2c17d27c
JA
5532 int ret;
5533
588f0330 5534 net_timestamp_check(netdev_tstamp_prequeue, skb);
3b098e2d 5535
c1f19b51
RC
5536 if (skb_defer_rx_timestamp(skb))
5537 return NET_RX_SUCCESS;
5538
bbbe211c 5539 rcu_read_lock();
df334545 5540#ifdef CONFIG_RPS
dc05360f 5541 if (static_branch_unlikely(&rps_needed)) {
3b098e2d 5542 struct rps_dev_flow voidflow, *rflow = &voidflow;
2c17d27c 5543 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
0a9627f2 5544
3b098e2d
ED
5545 if (cpu >= 0) {
5546 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5547 rcu_read_unlock();
adc9300e 5548 return ret;
3b098e2d 5549 }
fec5e652 5550 }
1e94d72f 5551#endif
2c17d27c
JA
5552 ret = __netif_receive_skb(skb);
5553 rcu_read_unlock();
5554 return ret;
0a9627f2 5555}
ae78dbfa 5556
7da517a3
EC
5557static void netif_receive_skb_list_internal(struct list_head *head)
5558{
7da517a3 5559 struct sk_buff *skb, *next;
8c057efa 5560 struct list_head sublist;
7da517a3 5561
8c057efa 5562 INIT_LIST_HEAD(&sublist);
7da517a3
EC
5563 list_for_each_entry_safe(skb, next, head, list) {
5564 net_timestamp_check(netdev_tstamp_prequeue, skb);
22f6bbb7 5565 skb_list_del_init(skb);
8c057efa
EC
5566 if (!skb_defer_rx_timestamp(skb))
5567 list_add_tail(&skb->list, &sublist);
7da517a3 5568 }
8c057efa 5569 list_splice_init(&sublist, head);
7da517a3 5570
7da517a3
EC
5571 rcu_read_lock();
5572#ifdef CONFIG_RPS
dc05360f 5573 if (static_branch_unlikely(&rps_needed)) {
7da517a3
EC
5574 list_for_each_entry_safe(skb, next, head, list) {
5575 struct rps_dev_flow voidflow, *rflow = &voidflow;
5576 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5577
5578 if (cpu >= 0) {
8c057efa 5579 /* Will be handled, remove from list */
22f6bbb7 5580 skb_list_del_init(skb);
8c057efa 5581 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
7da517a3
EC
5582 }
5583 }
5584 }
5585#endif
5586 __netif_receive_skb_list(head);
5587 rcu_read_unlock();
5588}
5589
ae78dbfa
BH
5590/**
5591 * netif_receive_skb - process receive buffer from network
5592 * @skb: buffer to process
5593 *
5594 * netif_receive_skb() is the main receive data processing function.
5595 * It always succeeds. The buffer may be dropped during processing
5596 * for congestion control or by the protocol layers.
5597 *
5598 * This function may only be called from softirq context and interrupts
5599 * should be enabled.
5600 *
5601 * Return values (usually ignored):
5602 * NET_RX_SUCCESS: no congestion
5603 * NET_RX_DROP: packet was dropped
5604 */
04eb4489 5605int netif_receive_skb(struct sk_buff *skb)
ae78dbfa 5606{
b0e3f1bd
GB
5607 int ret;
5608
ae78dbfa
BH
5609 trace_netif_receive_skb_entry(skb);
5610
b0e3f1bd
GB
5611 ret = netif_receive_skb_internal(skb);
5612 trace_netif_receive_skb_exit(ret);
5613
5614 return ret;
ae78dbfa 5615}
04eb4489 5616EXPORT_SYMBOL(netif_receive_skb);
1da177e4 5617
f6ad8c1b
EC
5618/**
5619 * netif_receive_skb_list - process many receive buffers from network
5620 * @head: list of skbs to process.
5621 *
7da517a3
EC
5622 * Since return value of netif_receive_skb() is normally ignored, and
5623 * wouldn't be meaningful for a list, this function returns void.
f6ad8c1b
EC
5624 *
5625 * This function may only be called from softirq context and interrupts
5626 * should be enabled.
5627 */
5628void netif_receive_skb_list(struct list_head *head)
5629{
7da517a3 5630 struct sk_buff *skb;
f6ad8c1b 5631
b9f463d6
EC
5632 if (list_empty(head))
5633 return;
b0e3f1bd
GB
5634 if (trace_netif_receive_skb_list_entry_enabled()) {
5635 list_for_each_entry(skb, head, list)
5636 trace_netif_receive_skb_list_entry(skb);
5637 }
7da517a3 5638 netif_receive_skb_list_internal(head);
b0e3f1bd 5639 trace_netif_receive_skb_list_exit(0);
f6ad8c1b
EC
5640}
5641EXPORT_SYMBOL(netif_receive_skb_list);
5642
ce1e2a77 5643static DEFINE_PER_CPU(struct work_struct, flush_works);
145dd5f9
PA
5644
5645/* Network device is going away, flush any packets still pending */
5646static void flush_backlog(struct work_struct *work)
6e583ce5 5647{
6e583ce5 5648 struct sk_buff *skb, *tmp;
145dd5f9
PA
5649 struct softnet_data *sd;
5650
5651 local_bh_disable();
5652 sd = this_cpu_ptr(&softnet_data);
6e583ce5 5653
145dd5f9 5654 local_irq_disable();
e36fa2f7 5655 rps_lock(sd);
6e7676c1 5656 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
41852497 5657 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
e36fa2f7 5658 __skb_unlink(skb, &sd->input_pkt_queue);
7df5cb75 5659 dev_kfree_skb_irq(skb);
76cc8b13 5660 input_queue_head_incr(sd);
6e583ce5 5661 }
6e7676c1 5662 }
e36fa2f7 5663 rps_unlock(sd);
145dd5f9 5664 local_irq_enable();
6e7676c1
CG
5665
5666 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
41852497 5667 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
6e7676c1
CG
5668 __skb_unlink(skb, &sd->process_queue);
5669 kfree_skb(skb);
76cc8b13 5670 input_queue_head_incr(sd);
6e7676c1
CG
5671 }
5672 }
145dd5f9
PA
5673 local_bh_enable();
5674}
5675
2de79ee2
PA
5676static bool flush_required(int cpu)
5677{
5678#if IS_ENABLED(CONFIG_RPS)
5679 struct softnet_data *sd = &per_cpu(softnet_data, cpu);
5680 bool do_flush;
5681
5682 local_irq_disable();
5683 rps_lock(sd);
5684
5685 /* as insertion into process_queue happens with the rps lock held,
5686 * process_queue access may race only with dequeue
5687 */
5688 do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
5689 !skb_queue_empty_lockless(&sd->process_queue);
5690 rps_unlock(sd);
5691 local_irq_enable();
5692
5693 return do_flush;
5694#endif
5695 /* without RPS we can't safely check input_pkt_queue: during a
5696 * concurrent remote skb_queue_splice() we can detect as empty both
5697 * input_pkt_queue and process_queue even if the latter could end-up
5698 * containing a lot of packets.
5699 */
5700 return true;
5701}
5702
41852497 5703static void flush_all_backlogs(void)
145dd5f9 5704{
2de79ee2 5705 static cpumask_t flush_cpus;
145dd5f9
PA
5706 unsigned int cpu;
5707
2de79ee2
PA
5708 /* since we are under rtnl lock protection we can use static data
5709 * for the cpumask and avoid allocating on stack the possibly
5710 * large mask
5711 */
5712 ASSERT_RTNL();
5713
145dd5f9
PA
5714 get_online_cpus();
5715
2de79ee2
PA
5716 cpumask_clear(&flush_cpus);
5717 for_each_online_cpu(cpu) {
5718 if (flush_required(cpu)) {
5719 queue_work_on(cpu, system_highpri_wq,
5720 per_cpu_ptr(&flush_works, cpu));
5721 cpumask_set_cpu(cpu, &flush_cpus);
5722 }
5723 }
145dd5f9 5724
2de79ee2 5725 /* we can have in flight packet[s] on the cpus we are not flushing,
0cbe1e57 5726 * synchronize_net() in unregister_netdevice_many() will take care of
2de79ee2
PA
5727 * them
5728 */
5729 for_each_cpu(cpu, &flush_cpus)
41852497 5730 flush_work(per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
5731
5732 put_online_cpus();
6e583ce5
SH
5733}
5734
c8079432
MM
5735/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5736static void gro_normal_list(struct napi_struct *napi)
5737{
5738 if (!napi->rx_count)
5739 return;
5740 netif_receive_skb_list_internal(&napi->rx_list);
5741 INIT_LIST_HEAD(&napi->rx_list);
5742 napi->rx_count = 0;
5743}
5744
5745/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5746 * pass the whole batch up to the stack.
5747 */
5748static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5749{
5750 list_add_tail(&skb->list, &napi->rx_list);
5751 if (++napi->rx_count >= gro_normal_batch)
5752 gro_normal_list(napi);
5753}
5754
c8079432 5755static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1 5756{
22061d80 5757 struct packet_offload *ptype;
d565b0a1 5758 __be16 type = skb->protocol;
22061d80 5759 struct list_head *head = &offload_base;
d565b0a1
HX
5760 int err = -ENOENT;
5761
c3c7c254
ED
5762 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5763
fc59f9a3
HX
5764 if (NAPI_GRO_CB(skb)->count == 1) {
5765 skb_shinfo(skb)->gso_size = 0;
d565b0a1 5766 goto out;
fc59f9a3 5767 }
d565b0a1
HX
5768
5769 rcu_read_lock();
5770 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 5771 if (ptype->type != type || !ptype->callbacks.gro_complete)
d565b0a1
HX
5772 continue;
5773
aaa5d90b
PA
5774 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5775 ipv6_gro_complete, inet_gro_complete,
5776 skb, 0);
d565b0a1
HX
5777 break;
5778 }
5779 rcu_read_unlock();
5780
5781 if (err) {
5782 WARN_ON(&ptype->list == head);
5783 kfree_skb(skb);
5784 return NET_RX_SUCCESS;
5785 }
5786
5787out:
c8079432
MM
5788 gro_normal_one(napi, skb);
5789 return NET_RX_SUCCESS;
d565b0a1
HX
5790}
5791
6312fe77 5792static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
07d78363 5793 bool flush_old)
d565b0a1 5794{
6312fe77 5795 struct list_head *head = &napi->gro_hash[index].list;
d4546c25 5796 struct sk_buff *skb, *p;
2e71a6f8 5797
07d78363 5798 list_for_each_entry_safe_reverse(skb, p, head, list) {
2e71a6f8
ED
5799 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5800 return;
992cba7e 5801 skb_list_del_init(skb);
c8079432 5802 napi_gro_complete(napi, skb);
6312fe77 5803 napi->gro_hash[index].count--;
d565b0a1 5804 }
d9f37d01
LR
5805
5806 if (!napi->gro_hash[index].count)
5807 __clear_bit(index, &napi->gro_bitmask);
d565b0a1 5808}
07d78363 5809
6312fe77 5810/* napi->gro_hash[].list contains packets ordered by age.
07d78363
DM
5811 * youngest packets at the head of it.
5812 * Complete skbs in reverse order to reduce latencies.
5813 */
5814void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5815{
42519ede
ED
5816 unsigned long bitmask = napi->gro_bitmask;
5817 unsigned int i, base = ~0U;
07d78363 5818
42519ede
ED
5819 while ((i = ffs(bitmask)) != 0) {
5820 bitmask >>= i;
5821 base += i;
5822 __napi_gro_flush_chain(napi, base, flush_old);
d9f37d01 5823 }
07d78363 5824}
86cac58b 5825EXPORT_SYMBOL(napi_gro_flush);
d565b0a1 5826
07d78363
DM
5827static struct list_head *gro_list_prepare(struct napi_struct *napi,
5828 struct sk_buff *skb)
89c5fa33 5829{
89c5fa33 5830 unsigned int maclen = skb->dev->hard_header_len;
0b4cec8c 5831 u32 hash = skb_get_hash_raw(skb);
07d78363 5832 struct list_head *head;
d4546c25 5833 struct sk_buff *p;
89c5fa33 5834
6312fe77 5835 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
07d78363 5836 list_for_each_entry(p, head, list) {
89c5fa33
ED
5837 unsigned long diffs;
5838
0b4cec8c
TH
5839 NAPI_GRO_CB(p)->flush = 0;
5840
5841 if (hash != skb_get_hash_raw(p)) {
5842 NAPI_GRO_CB(p)->same_flow = 0;
5843 continue;
5844 }
5845
89c5fa33 5846 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
b1817524
MM
5847 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5848 if (skb_vlan_tag_present(p))
fc5141cb 5849 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
ce87fc6c 5850 diffs |= skb_metadata_dst_cmp(p, skb);
de8f3a83 5851 diffs |= skb_metadata_differs(p, skb);
89c5fa33
ED
5852 if (maclen == ETH_HLEN)
5853 diffs |= compare_ether_header(skb_mac_header(p),
a50e233c 5854 skb_mac_header(skb));
89c5fa33
ED
5855 else if (!diffs)
5856 diffs = memcmp(skb_mac_header(p),
a50e233c 5857 skb_mac_header(skb),
89c5fa33
ED
5858 maclen);
5859 NAPI_GRO_CB(p)->same_flow = !diffs;
89c5fa33 5860 }
07d78363
DM
5861
5862 return head;
89c5fa33
ED
5863}
5864
299603e8
JC
5865static void skb_gro_reset_offset(struct sk_buff *skb)
5866{
5867 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5868 const skb_frag_t *frag0 = &pinfo->frags[0];
5869
5870 NAPI_GRO_CB(skb)->data_offset = 0;
5871 NAPI_GRO_CB(skb)->frag0 = NULL;
5872 NAPI_GRO_CB(skb)->frag0_len = 0;
5873
8aef998d 5874 if (!skb_headlen(skb) && pinfo->nr_frags &&
299603e8
JC
5875 !PageHighMem(skb_frag_page(frag0))) {
5876 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
7cfd5fd5
ED
5877 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5878 skb_frag_size(frag0),
5879 skb->end - skb->tail);
89c5fa33
ED
5880 }
5881}
5882
a50e233c
ED
5883static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5884{
5885 struct skb_shared_info *pinfo = skb_shinfo(skb);
5886
5887 BUG_ON(skb->end - skb->tail < grow);
5888
5889 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5890
5891 skb->data_len -= grow;
5892 skb->tail += grow;
5893
b54c9d5b 5894 skb_frag_off_add(&pinfo->frags[0], grow);
a50e233c
ED
5895 skb_frag_size_sub(&pinfo->frags[0], grow);
5896
5897 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5898 skb_frag_unref(skb, 0);
5899 memmove(pinfo->frags, pinfo->frags + 1,
5900 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5901 }
5902}
5903
c8079432 5904static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
07d78363 5905{
6312fe77 5906 struct sk_buff *oldest;
07d78363 5907
6312fe77 5908 oldest = list_last_entry(head, struct sk_buff, list);
07d78363 5909
6312fe77 5910 /* We are called with head length >= MAX_GRO_SKBS, so this is
07d78363
DM
5911 * impossible.
5912 */
5913 if (WARN_ON_ONCE(!oldest))
5914 return;
5915
d9f37d01
LR
5916 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5917 * SKB to the chain.
07d78363 5918 */
ece23711 5919 skb_list_del_init(oldest);
c8079432 5920 napi_gro_complete(napi, oldest);
07d78363
DM
5921}
5922
bb728820 5923static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1 5924{
6312fe77 5925 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
d4546c25 5926 struct list_head *head = &offload_base;
22061d80 5927 struct packet_offload *ptype;
d565b0a1 5928 __be16 type = skb->protocol;
07d78363 5929 struct list_head *gro_head;
d4546c25 5930 struct sk_buff *pp = NULL;
5b252f0c 5931 enum gro_result ret;
d4546c25 5932 int same_flow;
a50e233c 5933 int grow;
d565b0a1 5934
b5cdae32 5935 if (netif_elide_gro(skb->dev))
d565b0a1
HX
5936 goto normal;
5937
07d78363 5938 gro_head = gro_list_prepare(napi, skb);
89c5fa33 5939
d565b0a1
HX
5940 rcu_read_lock();
5941 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 5942 if (ptype->type != type || !ptype->callbacks.gro_receive)
d565b0a1
HX
5943 continue;
5944
86911732 5945 skb_set_network_header(skb, skb_gro_offset(skb));
efd9450e 5946 skb_reset_mac_len(skb);
d565b0a1 5947 NAPI_GRO_CB(skb)->same_flow = 0;
d61d072e 5948 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
5d38a079 5949 NAPI_GRO_CB(skb)->free = 0;
fac8e0f5 5950 NAPI_GRO_CB(skb)->encap_mark = 0;
fcd91dd4 5951 NAPI_GRO_CB(skb)->recursion_counter = 0;
a0ca153f 5952 NAPI_GRO_CB(skb)->is_fou = 0;
1530545e 5953 NAPI_GRO_CB(skb)->is_atomic = 1;
15e2396d 5954 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
d565b0a1 5955
662880f4
TH
5956 /* Setup for GRO checksum validation */
5957 switch (skb->ip_summed) {
5958 case CHECKSUM_COMPLETE:
5959 NAPI_GRO_CB(skb)->csum = skb->csum;
5960 NAPI_GRO_CB(skb)->csum_valid = 1;
5961 NAPI_GRO_CB(skb)->csum_cnt = 0;
5962 break;
5963 case CHECKSUM_UNNECESSARY:
5964 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5965 NAPI_GRO_CB(skb)->csum_valid = 0;
5966 break;
5967 default:
5968 NAPI_GRO_CB(skb)->csum_cnt = 0;
5969 NAPI_GRO_CB(skb)->csum_valid = 0;
5970 }
d565b0a1 5971
aaa5d90b
PA
5972 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5973 ipv6_gro_receive, inet_gro_receive,
5974 gro_head, skb);
d565b0a1
HX
5975 break;
5976 }
5977 rcu_read_unlock();
5978
5979 if (&ptype->list == head)
5980 goto normal;
5981
45586c70 5982 if (PTR_ERR(pp) == -EINPROGRESS) {
25393d3f
SK
5983 ret = GRO_CONSUMED;
5984 goto ok;
5985 }
5986
0da2afd5 5987 same_flow = NAPI_GRO_CB(skb)->same_flow;
5d0d9be8 5988 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
0da2afd5 5989
d565b0a1 5990 if (pp) {
992cba7e 5991 skb_list_del_init(pp);
c8079432 5992 napi_gro_complete(napi, pp);
6312fe77 5993 napi->gro_hash[hash].count--;
d565b0a1
HX
5994 }
5995
0da2afd5 5996 if (same_flow)
d565b0a1
HX
5997 goto ok;
5998
600adc18 5999 if (NAPI_GRO_CB(skb)->flush)
d565b0a1 6000 goto normal;
d565b0a1 6001
6312fe77 6002 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
c8079432 6003 gro_flush_oldest(napi, gro_head);
600adc18 6004 } else {
6312fe77 6005 napi->gro_hash[hash].count++;
600adc18 6006 }
d565b0a1 6007 NAPI_GRO_CB(skb)->count = 1;
2e71a6f8 6008 NAPI_GRO_CB(skb)->age = jiffies;
29e98242 6009 NAPI_GRO_CB(skb)->last = skb;
86911732 6010 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
07d78363 6011 list_add(&skb->list, gro_head);
5d0d9be8 6012 ret = GRO_HELD;
d565b0a1 6013
ad0f9904 6014pull:
a50e233c
ED
6015 grow = skb_gro_offset(skb) - skb_headlen(skb);
6016 if (grow > 0)
6017 gro_pull_from_frag0(skb, grow);
d565b0a1 6018ok:
d9f37d01
LR
6019 if (napi->gro_hash[hash].count) {
6020 if (!test_bit(hash, &napi->gro_bitmask))
6021 __set_bit(hash, &napi->gro_bitmask);
6022 } else if (test_bit(hash, &napi->gro_bitmask)) {
6023 __clear_bit(hash, &napi->gro_bitmask);
6024 }
6025
5d0d9be8 6026 return ret;
d565b0a1
HX
6027
6028normal:
ad0f9904
HX
6029 ret = GRO_NORMAL;
6030 goto pull;
5d38a079 6031}
96e93eab 6032
bf5a755f
JC
6033struct packet_offload *gro_find_receive_by_type(__be16 type)
6034{
6035 struct list_head *offload_head = &offload_base;
6036 struct packet_offload *ptype;
6037
6038 list_for_each_entry_rcu(ptype, offload_head, list) {
6039 if (ptype->type != type || !ptype->callbacks.gro_receive)
6040 continue;
6041 return ptype;
6042 }
6043 return NULL;
6044}
e27a2f83 6045EXPORT_SYMBOL(gro_find_receive_by_type);
bf5a755f
JC
6046
6047struct packet_offload *gro_find_complete_by_type(__be16 type)
6048{
6049 struct list_head *offload_head = &offload_base;
6050 struct packet_offload *ptype;
6051
6052 list_for_each_entry_rcu(ptype, offload_head, list) {
6053 if (ptype->type != type || !ptype->callbacks.gro_complete)
6054 continue;
6055 return ptype;
6056 }
6057 return NULL;
6058}
e27a2f83 6059EXPORT_SYMBOL(gro_find_complete_by_type);
5d38a079 6060
e44699d2
MK
6061static void napi_skb_free_stolen_head(struct sk_buff *skb)
6062{
6063 skb_dst_drop(skb);
174e2381 6064 skb_ext_put(skb);
e44699d2
MK
6065 kmem_cache_free(skbuff_head_cache, skb);
6066}
6067
6570bc79
AL
6068static gro_result_t napi_skb_finish(struct napi_struct *napi,
6069 struct sk_buff *skb,
6070 gro_result_t ret)
5d38a079 6071{
5d0d9be8
HX
6072 switch (ret) {
6073 case GRO_NORMAL:
6570bc79 6074 gro_normal_one(napi, skb);
c7c4b3b6 6075 break;
5d38a079 6076
daa86548 6077 case GRO_MERGED_FREE:
e44699d2
MK
6078 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6079 napi_skb_free_stolen_head(skb);
6080 else
d7e8883c 6081 __kfree_skb(skb);
daa86548
ED
6082 break;
6083
5b252f0c
BH
6084 case GRO_HELD:
6085 case GRO_MERGED:
25393d3f 6086 case GRO_CONSUMED:
5b252f0c 6087 break;
5d38a079
HX
6088 }
6089
c7c4b3b6 6090 return ret;
5d0d9be8 6091}
5d0d9be8 6092
c7c4b3b6 6093gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
5d0d9be8 6094{
b0e3f1bd
GB
6095 gro_result_t ret;
6096
93f93a44 6097 skb_mark_napi_id(skb, napi);
ae78dbfa 6098 trace_napi_gro_receive_entry(skb);
86911732 6099
a50e233c
ED
6100 skb_gro_reset_offset(skb);
6101
6570bc79 6102 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
b0e3f1bd
GB
6103 trace_napi_gro_receive_exit(ret);
6104
6105 return ret;
d565b0a1
HX
6106}
6107EXPORT_SYMBOL(napi_gro_receive);
6108
d0c2b0d2 6109static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
96e93eab 6110{
93a35f59
ED
6111 if (unlikely(skb->pfmemalloc)) {
6112 consume_skb(skb);
6113 return;
6114 }
96e93eab 6115 __skb_pull(skb, skb_headlen(skb));
2a2a459e
ED
6116 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
6117 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
b1817524 6118 __vlan_hwaccel_clear_tag(skb);
66c46d74 6119 skb->dev = napi->dev;
6d152e23 6120 skb->skb_iif = 0;
33d9a2c7
ED
6121
6122 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
6123 skb->pkt_type = PACKET_HOST;
6124
c3caf119
JC
6125 skb->encapsulation = 0;
6126 skb_shinfo(skb)->gso_type = 0;
e33d0ba8 6127 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
174e2381 6128 skb_ext_reset(skb);
96e93eab
HX
6129
6130 napi->skb = skb;
6131}
96e93eab 6132
76620aaf 6133struct sk_buff *napi_get_frags(struct napi_struct *napi)
5d38a079 6134{
5d38a079 6135 struct sk_buff *skb = napi->skb;
5d38a079
HX
6136
6137 if (!skb) {
fd11a83d 6138 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
e2f9dc3b
ED
6139 if (skb) {
6140 napi->skb = skb;
6141 skb_mark_napi_id(skb, napi);
6142 }
80595d59 6143 }
96e93eab
HX
6144 return skb;
6145}
76620aaf 6146EXPORT_SYMBOL(napi_get_frags);
96e93eab 6147
a50e233c
ED
6148static gro_result_t napi_frags_finish(struct napi_struct *napi,
6149 struct sk_buff *skb,
6150 gro_result_t ret)
96e93eab 6151{
5d0d9be8
HX
6152 switch (ret) {
6153 case GRO_NORMAL:
a50e233c
ED
6154 case GRO_HELD:
6155 __skb_push(skb, ETH_HLEN);
6156 skb->protocol = eth_type_trans(skb, skb->dev);
323ebb61
EC
6157 if (ret == GRO_NORMAL)
6158 gro_normal_one(napi, skb);
86911732 6159 break;
5d38a079 6160
e44699d2
MK
6161 case GRO_MERGED_FREE:
6162 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
6163 napi_skb_free_stolen_head(skb);
6164 else
6165 napi_reuse_skb(napi, skb);
6166 break;
6167
5b252f0c 6168 case GRO_MERGED:
25393d3f 6169 case GRO_CONSUMED:
5b252f0c 6170 break;
5d0d9be8 6171 }
5d38a079 6172
c7c4b3b6 6173 return ret;
5d38a079 6174}
5d0d9be8 6175
a50e233c
ED
6176/* Upper GRO stack assumes network header starts at gro_offset=0
6177 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6178 * We copy ethernet header into skb->data to have a common layout.
6179 */
4adb9c4a 6180static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
76620aaf
HX
6181{
6182 struct sk_buff *skb = napi->skb;
a50e233c
ED
6183 const struct ethhdr *eth;
6184 unsigned int hlen = sizeof(*eth);
76620aaf
HX
6185
6186 napi->skb = NULL;
6187
a50e233c
ED
6188 skb_reset_mac_header(skb);
6189 skb_gro_reset_offset(skb);
6190
a50e233c
ED
6191 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6192 eth = skb_gro_header_slow(skb, hlen, 0);
6193 if (unlikely(!eth)) {
4da46ceb
AC
6194 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6195 __func__, napi->dev->name);
a50e233c
ED
6196 napi_reuse_skb(napi, skb);
6197 return NULL;
6198 }
6199 } else {
a4270d67 6200 eth = (const struct ethhdr *)skb->data;
a50e233c
ED
6201 gro_pull_from_frag0(skb, hlen);
6202 NAPI_GRO_CB(skb)->frag0 += hlen;
6203 NAPI_GRO_CB(skb)->frag0_len -= hlen;
76620aaf 6204 }
a50e233c
ED
6205 __skb_pull(skb, hlen);
6206
6207 /*
6208 * This works because the only protocols we care about don't require
6209 * special handling.
6210 * We'll fix it up properly in napi_frags_finish()
6211 */
6212 skb->protocol = eth->h_proto;
76620aaf 6213
76620aaf
HX
6214 return skb;
6215}
76620aaf 6216
c7c4b3b6 6217gro_result_t napi_gro_frags(struct napi_struct *napi)
5d0d9be8 6218{
b0e3f1bd 6219 gro_result_t ret;
76620aaf 6220 struct sk_buff *skb = napi_frags_skb(napi);
5d0d9be8 6221
ae78dbfa
BH
6222 trace_napi_gro_frags_entry(skb);
6223
b0e3f1bd
GB
6224 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6225 trace_napi_gro_frags_exit(ret);
6226
6227 return ret;
5d0d9be8 6228}
5d38a079
HX
6229EXPORT_SYMBOL(napi_gro_frags);
6230
573e8fca
TH
6231/* Compute the checksum from gro_offset and return the folded value
6232 * after adding in any pseudo checksum.
6233 */
6234__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6235{
6236 __wsum wsum;
6237 __sum16 sum;
6238
6239 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6240
6241 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6242 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
14641931 6243 /* See comments in __skb_checksum_complete(). */
573e8fca
TH
6244 if (likely(!sum)) {
6245 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6246 !skb->csum_complete_sw)
7fe50ac8 6247 netdev_rx_csum_fault(skb->dev, skb);
573e8fca
TH
6248 }
6249
6250 NAPI_GRO_CB(skb)->csum = wsum;
6251 NAPI_GRO_CB(skb)->csum_valid = 1;
6252
6253 return sum;
6254}
6255EXPORT_SYMBOL(__skb_gro_checksum_complete);
6256
773fc8f6 6257static void net_rps_send_ipi(struct softnet_data *remsd)
6258{
6259#ifdef CONFIG_RPS
6260 while (remsd) {
6261 struct softnet_data *next = remsd->rps_ipi_next;
6262
6263 if (cpu_online(remsd->cpu))
6264 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6265 remsd = next;
6266 }
6267#endif
6268}
6269
e326bed2 6270/*
855abcf0 6271 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
e326bed2
ED
6272 * Note: called with local irq disabled, but exits with local irq enabled.
6273 */
6274static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6275{
6276#ifdef CONFIG_RPS
6277 struct softnet_data *remsd = sd->rps_ipi_list;
6278
6279 if (remsd) {
6280 sd->rps_ipi_list = NULL;
6281
6282 local_irq_enable();
6283
6284 /* Send pending IPI's to kick RPS processing on remote cpus. */
773fc8f6 6285 net_rps_send_ipi(remsd);
e326bed2
ED
6286 } else
6287#endif
6288 local_irq_enable();
6289}
6290
d75b1ade
ED
6291static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6292{
6293#ifdef CONFIG_RPS
6294 return sd->rps_ipi_list != NULL;
6295#else
6296 return false;
6297#endif
6298}
6299
bea3348e 6300static int process_backlog(struct napi_struct *napi, int quota)
1da177e4 6301{
eecfd7c4 6302 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
145dd5f9
PA
6303 bool again = true;
6304 int work = 0;
1da177e4 6305
e326bed2
ED
6306 /* Check if we have pending ipi, its better to send them now,
6307 * not waiting net_rx_action() end.
6308 */
d75b1ade 6309 if (sd_has_rps_ipi_waiting(sd)) {
e326bed2
ED
6310 local_irq_disable();
6311 net_rps_action_and_irq_enable(sd);
6312 }
d75b1ade 6313
3d48b53f 6314 napi->weight = dev_rx_weight;
145dd5f9 6315 while (again) {
1da177e4 6316 struct sk_buff *skb;
6e7676c1
CG
6317
6318 while ((skb = __skb_dequeue(&sd->process_queue))) {
2c17d27c 6319 rcu_read_lock();
6e7676c1 6320 __netif_receive_skb(skb);
2c17d27c 6321 rcu_read_unlock();
76cc8b13 6322 input_queue_head_incr(sd);
145dd5f9 6323 if (++work >= quota)
76cc8b13 6324 return work;
145dd5f9 6325
6e7676c1 6326 }
1da177e4 6327
145dd5f9 6328 local_irq_disable();
e36fa2f7 6329 rps_lock(sd);
11ef7a89 6330 if (skb_queue_empty(&sd->input_pkt_queue)) {
eecfd7c4
ED
6331 /*
6332 * Inline a custom version of __napi_complete().
6333 * only current cpu owns and manipulates this napi,
11ef7a89
TH
6334 * and NAPI_STATE_SCHED is the only possible flag set
6335 * on backlog.
6336 * We can use a plain write instead of clear_bit(),
eecfd7c4
ED
6337 * and we dont need an smp_mb() memory barrier.
6338 */
eecfd7c4 6339 napi->state = 0;
145dd5f9
PA
6340 again = false;
6341 } else {
6342 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6343 &sd->process_queue);
bea3348e 6344 }
e36fa2f7 6345 rps_unlock(sd);
145dd5f9 6346 local_irq_enable();
6e7676c1 6347 }
1da177e4 6348
bea3348e
SH
6349 return work;
6350}
1da177e4 6351
bea3348e
SH
6352/**
6353 * __napi_schedule - schedule for receive
c4ea43c5 6354 * @n: entry to schedule
bea3348e 6355 *
bc9ad166
ED
6356 * The entry's receive function will be scheduled to run.
6357 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
bea3348e 6358 */
b5606c2d 6359void __napi_schedule(struct napi_struct *n)
bea3348e
SH
6360{
6361 unsigned long flags;
1da177e4 6362
bea3348e 6363 local_irq_save(flags);
903ceff7 6364 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
bea3348e 6365 local_irq_restore(flags);
1da177e4 6366}
bea3348e
SH
6367EXPORT_SYMBOL(__napi_schedule);
6368
39e6c820
ED
6369/**
6370 * napi_schedule_prep - check if napi can be scheduled
6371 * @n: napi context
6372 *
6373 * Test if NAPI routine is already running, and if not mark
ee1a4c84 6374 * it as running. This is used as a condition variable to
39e6c820
ED
6375 * insure only one NAPI poll instance runs. We also make
6376 * sure there is no pending NAPI disable.
6377 */
6378bool napi_schedule_prep(struct napi_struct *n)
6379{
6380 unsigned long val, new;
6381
6382 do {
6383 val = READ_ONCE(n->state);
6384 if (unlikely(val & NAPIF_STATE_DISABLE))
6385 return false;
6386 new = val | NAPIF_STATE_SCHED;
6387
6388 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6389 * This was suggested by Alexander Duyck, as compiler
6390 * emits better code than :
6391 * if (val & NAPIF_STATE_SCHED)
6392 * new |= NAPIF_STATE_MISSED;
6393 */
6394 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6395 NAPIF_STATE_MISSED;
6396 } while (cmpxchg(&n->state, val, new) != val);
6397
6398 return !(val & NAPIF_STATE_SCHED);
6399}
6400EXPORT_SYMBOL(napi_schedule_prep);
6401
bc9ad166
ED
6402/**
6403 * __napi_schedule_irqoff - schedule for receive
6404 * @n: entry to schedule
6405 *
6406 * Variant of __napi_schedule() assuming hard irqs are masked
6407 */
6408void __napi_schedule_irqoff(struct napi_struct *n)
6409{
6410 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6411}
6412EXPORT_SYMBOL(__napi_schedule_irqoff);
6413
364b6055 6414bool napi_complete_done(struct napi_struct *n, int work_done)
d565b0a1 6415{
6f8b12d6
ED
6416 unsigned long flags, val, new, timeout = 0;
6417 bool ret = true;
d565b0a1
HX
6418
6419 /*
217f6974
ED
6420 * 1) Don't let napi dequeue from the cpu poll list
6421 * just in case its running on a different cpu.
6422 * 2) If we are busy polling, do nothing here, we have
6423 * the guarantee we will be called later.
d565b0a1 6424 */
217f6974
ED
6425 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6426 NAPIF_STATE_IN_BUSY_POLL)))
364b6055 6427 return false;
d565b0a1 6428
6f8b12d6
ED
6429 if (work_done) {
6430 if (n->gro_bitmask)
7e417a66
ED
6431 timeout = READ_ONCE(n->dev->gro_flush_timeout);
6432 n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
6f8b12d6
ED
6433 }
6434 if (n->defer_hard_irqs_count > 0) {
6435 n->defer_hard_irqs_count--;
7e417a66 6436 timeout = READ_ONCE(n->dev->gro_flush_timeout);
6f8b12d6
ED
6437 if (timeout)
6438 ret = false;
6439 }
6440 if (n->gro_bitmask) {
605108ac
PA
6441 /* When the NAPI instance uses a timeout and keeps postponing
6442 * it, we need to bound somehow the time packets are kept in
6443 * the GRO layer
6444 */
6445 napi_gro_flush(n, !!timeout);
3b47d303 6446 }
c8079432
MM
6447
6448 gro_normal_list(n);
6449
02c1602e 6450 if (unlikely(!list_empty(&n->poll_list))) {
d75b1ade
ED
6451 /* If n->poll_list is not empty, we need to mask irqs */
6452 local_irq_save(flags);
02c1602e 6453 list_del_init(&n->poll_list);
d75b1ade
ED
6454 local_irq_restore(flags);
6455 }
39e6c820
ED
6456
6457 do {
6458 val = READ_ONCE(n->state);
6459
6460 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6461
7fd3253a
BT
6462 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
6463 NAPIF_STATE_PREFER_BUSY_POLL);
39e6c820
ED
6464
6465 /* If STATE_MISSED was set, leave STATE_SCHED set,
6466 * because we will call napi->poll() one more time.
6467 * This C code was suggested by Alexander Duyck to help gcc.
6468 */
6469 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6470 NAPIF_STATE_SCHED;
6471 } while (cmpxchg(&n->state, val, new) != val);
6472
6473 if (unlikely(val & NAPIF_STATE_MISSED)) {
6474 __napi_schedule(n);
6475 return false;
6476 }
6477
6f8b12d6
ED
6478 if (timeout)
6479 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6480 HRTIMER_MODE_REL_PINNED);
6481 return ret;
d565b0a1 6482}
3b47d303 6483EXPORT_SYMBOL(napi_complete_done);
d565b0a1 6484
af12fa6e 6485/* must be called under rcu_read_lock(), as we dont take a reference */
02d62e86 6486static struct napi_struct *napi_by_id(unsigned int napi_id)
af12fa6e
ET
6487{
6488 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6489 struct napi_struct *napi;
6490
6491 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6492 if (napi->napi_id == napi_id)
6493 return napi;
6494
6495 return NULL;
6496}
02d62e86
ED
6497
6498#if defined(CONFIG_NET_RX_BUSY_POLL)
217f6974 6499
7fd3253a 6500static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
217f6974 6501{
7fd3253a
BT
6502 if (!skip_schedule) {
6503 gro_normal_list(napi);
6504 __napi_schedule(napi);
6505 return;
6506 }
217f6974 6507
7fd3253a
BT
6508 if (napi->gro_bitmask) {
6509 /* flush too old packets
6510 * If HZ < 1000, flush all packets.
6511 */
6512 napi_gro_flush(napi, HZ >= 1000);
6513 }
217f6974 6514
7fd3253a
BT
6515 gro_normal_list(napi);
6516 clear_bit(NAPI_STATE_SCHED, &napi->state);
6517}
6518
7c951caf
BT
6519static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll,
6520 u16 budget)
217f6974 6521{
7fd3253a
BT
6522 bool skip_schedule = false;
6523 unsigned long timeout;
217f6974
ED
6524 int rc;
6525
39e6c820
ED
6526 /* Busy polling means there is a high chance device driver hard irq
6527 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6528 * set in napi_schedule_prep().
6529 * Since we are about to call napi->poll() once more, we can safely
6530 * clear NAPI_STATE_MISSED.
6531 *
6532 * Note: x86 could use a single "lock and ..." instruction
6533 * to perform these two clear_bit()
6534 */
6535 clear_bit(NAPI_STATE_MISSED, &napi->state);
217f6974
ED
6536 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6537
6538 local_bh_disable();
6539
7fd3253a
BT
6540 if (prefer_busy_poll) {
6541 napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
6542 timeout = READ_ONCE(napi->dev->gro_flush_timeout);
6543 if (napi->defer_hard_irqs_count && timeout) {
6544 hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
6545 skip_schedule = true;
6546 }
6547 }
6548
217f6974
ED
6549 /* All we really want here is to re-enable device interrupts.
6550 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6551 */
7c951caf 6552 rc = napi->poll(napi, budget);
323ebb61
EC
6553 /* We can't gro_normal_list() here, because napi->poll() might have
6554 * rearmed the napi (napi_complete_done()) in which case it could
6555 * already be running on another CPU.
6556 */
7c951caf 6557 trace_napi_poll(napi, rc, budget);
217f6974 6558 netpoll_poll_unlock(have_poll_lock);
7c951caf 6559 if (rc == budget)
7fd3253a 6560 __busy_poll_stop(napi, skip_schedule);
217f6974 6561 local_bh_enable();
217f6974
ED
6562}
6563
7db6b048
SS
6564void napi_busy_loop(unsigned int napi_id,
6565 bool (*loop_end)(void *, unsigned long),
7c951caf 6566 void *loop_end_arg, bool prefer_busy_poll, u16 budget)
02d62e86 6567{
7db6b048 6568 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
217f6974 6569 int (*napi_poll)(struct napi_struct *napi, int budget);
217f6974 6570 void *have_poll_lock = NULL;
02d62e86 6571 struct napi_struct *napi;
217f6974
ED
6572
6573restart:
217f6974 6574 napi_poll = NULL;
02d62e86 6575
2a028ecb 6576 rcu_read_lock();
02d62e86 6577
545cd5e5 6578 napi = napi_by_id(napi_id);
02d62e86
ED
6579 if (!napi)
6580 goto out;
6581
217f6974
ED
6582 preempt_disable();
6583 for (;;) {
2b5cd0df
AD
6584 int work = 0;
6585
2a028ecb 6586 local_bh_disable();
217f6974
ED
6587 if (!napi_poll) {
6588 unsigned long val = READ_ONCE(napi->state);
6589
6590 /* If multiple threads are competing for this napi,
6591 * we avoid dirtying napi->state as much as we can.
6592 */
6593 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
7fd3253a
BT
6594 NAPIF_STATE_IN_BUSY_POLL)) {
6595 if (prefer_busy_poll)
6596 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
217f6974 6597 goto count;
7fd3253a 6598 }
217f6974
ED
6599 if (cmpxchg(&napi->state, val,
6600 val | NAPIF_STATE_IN_BUSY_POLL |
7fd3253a
BT
6601 NAPIF_STATE_SCHED) != val) {
6602 if (prefer_busy_poll)
6603 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
217f6974 6604 goto count;
7fd3253a 6605 }
217f6974
ED
6606 have_poll_lock = netpoll_poll_lock(napi);
6607 napi_poll = napi->poll;
6608 }
7c951caf
BT
6609 work = napi_poll(napi, budget);
6610 trace_napi_poll(napi, work, budget);
323ebb61 6611 gro_normal_list(napi);
217f6974 6612count:
2b5cd0df 6613 if (work > 0)
7db6b048 6614 __NET_ADD_STATS(dev_net(napi->dev),
2b5cd0df 6615 LINUX_MIB_BUSYPOLLRXPACKETS, work);
2a028ecb 6616 local_bh_enable();
02d62e86 6617
7db6b048 6618 if (!loop_end || loop_end(loop_end_arg, start_time))
217f6974 6619 break;
02d62e86 6620
217f6974
ED
6621 if (unlikely(need_resched())) {
6622 if (napi_poll)
7c951caf 6623 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
217f6974
ED
6624 preempt_enable();
6625 rcu_read_unlock();
6626 cond_resched();
7db6b048 6627 if (loop_end(loop_end_arg, start_time))
2b5cd0df 6628 return;
217f6974
ED
6629 goto restart;
6630 }
6cdf89b1 6631 cpu_relax();
217f6974
ED
6632 }
6633 if (napi_poll)
7c951caf 6634 busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget);
217f6974 6635 preempt_enable();
02d62e86 6636out:
2a028ecb 6637 rcu_read_unlock();
02d62e86 6638}
7db6b048 6639EXPORT_SYMBOL(napi_busy_loop);
02d62e86
ED
6640
6641#endif /* CONFIG_NET_RX_BUSY_POLL */
af12fa6e 6642
149d6ad8 6643static void napi_hash_add(struct napi_struct *napi)
af12fa6e 6644{
4d092dd2 6645 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
52bd2d62 6646 return;
af12fa6e 6647
52bd2d62 6648 spin_lock(&napi_hash_lock);
af12fa6e 6649
545cd5e5 6650 /* 0..NR_CPUS range is reserved for sender_cpu use */
52bd2d62 6651 do {
545cd5e5
AD
6652 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6653 napi_gen_id = MIN_NAPI_ID;
52bd2d62
ED
6654 } while (napi_by_id(napi_gen_id));
6655 napi->napi_id = napi_gen_id;
af12fa6e 6656
52bd2d62
ED
6657 hlist_add_head_rcu(&napi->napi_hash_node,
6658 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
af12fa6e 6659
52bd2d62 6660 spin_unlock(&napi_hash_lock);
af12fa6e 6661}
af12fa6e
ET
6662
6663/* Warning : caller is responsible to make sure rcu grace period
6664 * is respected before freeing memory containing @napi
6665 */
5198d545 6666static void napi_hash_del(struct napi_struct *napi)
af12fa6e
ET
6667{
6668 spin_lock(&napi_hash_lock);
6669
4d092dd2 6670 hlist_del_init_rcu(&napi->napi_hash_node);
5198d545 6671
af12fa6e
ET
6672 spin_unlock(&napi_hash_lock);
6673}
af12fa6e 6674
3b47d303
ED
6675static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6676{
6677 struct napi_struct *napi;
6678
6679 napi = container_of(timer, struct napi_struct, timer);
39e6c820
ED
6680
6681 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6682 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6683 */
6f8b12d6 6684 if (!napi_disable_pending(napi) &&
7fd3253a
BT
6685 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
6686 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
39e6c820 6687 __napi_schedule_irqoff(napi);
7fd3253a 6688 }
3b47d303
ED
6689
6690 return HRTIMER_NORESTART;
6691}
6692
7c4ec749 6693static void init_gro_hash(struct napi_struct *napi)
d565b0a1 6694{
07d78363
DM
6695 int i;
6696
6312fe77
LR
6697 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6698 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6699 napi->gro_hash[i].count = 0;
6700 }
7c4ec749
DM
6701 napi->gro_bitmask = 0;
6702}
6703
6704void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6705 int (*poll)(struct napi_struct *, int), int weight)
6706{
4d092dd2
JK
6707 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6708 return;
6709
7c4ec749 6710 INIT_LIST_HEAD(&napi->poll_list);
4d092dd2 6711 INIT_HLIST_NODE(&napi->napi_hash_node);
7c4ec749
DM
6712 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6713 napi->timer.function = napi_watchdog;
6714 init_gro_hash(napi);
5d38a079 6715 napi->skb = NULL;
323ebb61
EC
6716 INIT_LIST_HEAD(&napi->rx_list);
6717 napi->rx_count = 0;
d565b0a1 6718 napi->poll = poll;
82dc3c63 6719 if (weight > NAPI_POLL_WEIGHT)
bf29e9e9
QC
6720 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6721 weight);
d565b0a1 6722 napi->weight = weight;
d565b0a1 6723 napi->dev = dev;
5d38a079 6724#ifdef CONFIG_NETPOLL
d565b0a1
HX
6725 napi->poll_owner = -1;
6726#endif
6727 set_bit(NAPI_STATE_SCHED, &napi->state);
96e97bc0
JK
6728 set_bit(NAPI_STATE_NPSVC, &napi->state);
6729 list_add_rcu(&napi->dev_list, &dev->napi_list);
93d05d4a 6730 napi_hash_add(napi);
d565b0a1
HX
6731}
6732EXPORT_SYMBOL(netif_napi_add);
6733
3b47d303
ED
6734void napi_disable(struct napi_struct *n)
6735{
6736 might_sleep();
6737 set_bit(NAPI_STATE_DISABLE, &n->state);
6738
6739 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6740 msleep(1);
2d8bff12
NH
6741 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6742 msleep(1);
3b47d303
ED
6743
6744 hrtimer_cancel(&n->timer);
6745
7fd3253a 6746 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
3b47d303
ED
6747 clear_bit(NAPI_STATE_DISABLE, &n->state);
6748}
6749EXPORT_SYMBOL(napi_disable);
6750
07d78363 6751static void flush_gro_hash(struct napi_struct *napi)
d4546c25 6752{
07d78363 6753 int i;
d4546c25 6754
07d78363
DM
6755 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6756 struct sk_buff *skb, *n;
6757
6312fe77 6758 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
07d78363 6759 kfree_skb(skb);
6312fe77 6760 napi->gro_hash[i].count = 0;
07d78363 6761 }
d4546c25
DM
6762}
6763
93d05d4a 6764/* Must be called in process context */
5198d545 6765void __netif_napi_del(struct napi_struct *napi)
d565b0a1 6766{
4d092dd2
JK
6767 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6768 return;
6769
5198d545 6770 napi_hash_del(napi);
5251ef82 6771 list_del_rcu(&napi->dev_list);
76620aaf 6772 napi_free_frags(napi);
d565b0a1 6773
07d78363 6774 flush_gro_hash(napi);
d9f37d01 6775 napi->gro_bitmask = 0;
d565b0a1 6776}
5198d545 6777EXPORT_SYMBOL(__netif_napi_del);
d565b0a1 6778
726ce70e
HX
6779static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6780{
6781 void *have;
6782 int work, weight;
6783
6784 list_del_init(&n->poll_list);
6785
6786 have = netpoll_poll_lock(n);
6787
6788 weight = n->weight;
6789
6790 /* This NAPI_STATE_SCHED test is for avoiding a race
6791 * with netpoll's poll_napi(). Only the entity which
6792 * obtains the lock and sees NAPI_STATE_SCHED set will
6793 * actually make the ->poll() call. Therefore we avoid
6794 * accidentally calling ->poll() when NAPI is not scheduled.
6795 */
6796 work = 0;
6797 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6798 work = n->poll(n, weight);
1db19db7 6799 trace_napi_poll(n, work, weight);
726ce70e
HX
6800 }
6801
427d5838
ED
6802 if (unlikely(work > weight))
6803 pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6804 n->poll, work, weight);
726ce70e
HX
6805
6806 if (likely(work < weight))
6807 goto out_unlock;
6808
6809 /* Drivers must not modify the NAPI state if they
6810 * consume the entire weight. In such cases this code
6811 * still "owns" the NAPI instance and therefore can
6812 * move the instance around on the list at-will.
6813 */
6814 if (unlikely(napi_disable_pending(n))) {
6815 napi_complete(n);
6816 goto out_unlock;
6817 }
6818
7fd3253a
BT
6819 /* The NAPI context has more processing work, but busy-polling
6820 * is preferred. Exit early.
6821 */
6822 if (napi_prefer_busy_poll(n)) {
6823 if (napi_complete_done(n, work)) {
6824 /* If timeout is not set, we need to make sure
6825 * that the NAPI is re-scheduled.
6826 */
6827 napi_schedule(n);
6828 }
6829 goto out_unlock;
6830 }
6831
d9f37d01 6832 if (n->gro_bitmask) {
726ce70e
HX
6833 /* flush too old packets
6834 * If HZ < 1000, flush all packets.
6835 */
6836 napi_gro_flush(n, HZ >= 1000);
6837 }
6838
c8079432
MM
6839 gro_normal_list(n);
6840
001ce546
HX
6841 /* Some drivers may have called napi_schedule
6842 * prior to exhausting their budget.
6843 */
6844 if (unlikely(!list_empty(&n->poll_list))) {
6845 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6846 n->dev ? n->dev->name : "backlog");
6847 goto out_unlock;
6848 }
6849
726ce70e
HX
6850 list_add_tail(&n->poll_list, repoll);
6851
6852out_unlock:
6853 netpoll_poll_unlock(have);
6854
6855 return work;
6856}
6857
0766f788 6858static __latent_entropy void net_rx_action(struct softirq_action *h)
1da177e4 6859{
903ceff7 6860 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
7acf8a1e
MW
6861 unsigned long time_limit = jiffies +
6862 usecs_to_jiffies(netdev_budget_usecs);
51b0bded 6863 int budget = netdev_budget;
d75b1ade
ED
6864 LIST_HEAD(list);
6865 LIST_HEAD(repoll);
53fb95d3 6866
1da177e4 6867 local_irq_disable();
d75b1ade
ED
6868 list_splice_init(&sd->poll_list, &list);
6869 local_irq_enable();
1da177e4 6870
ceb8d5bf 6871 for (;;) {
bea3348e 6872 struct napi_struct *n;
1da177e4 6873
ceb8d5bf
HX
6874 if (list_empty(&list)) {
6875 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
f52dffe0 6876 goto out;
ceb8d5bf
HX
6877 break;
6878 }
6879
6bd373eb
HX
6880 n = list_first_entry(&list, struct napi_struct, poll_list);
6881 budget -= napi_poll(n, &repoll);
6882
d75b1ade 6883 /* If softirq window is exhausted then punt.
24f8b238
SH
6884 * Allow this to run for 2 jiffies since which will allow
6885 * an average latency of 1.5/HZ.
bea3348e 6886 */
ceb8d5bf
HX
6887 if (unlikely(budget <= 0 ||
6888 time_after_eq(jiffies, time_limit))) {
6889 sd->time_squeeze++;
6890 break;
6891 }
1da177e4 6892 }
d75b1ade 6893
d75b1ade
ED
6894 local_irq_disable();
6895
6896 list_splice_tail_init(&sd->poll_list, &list);
6897 list_splice_tail(&repoll, &list);
6898 list_splice(&list, &sd->poll_list);
6899 if (!list_empty(&sd->poll_list))
6900 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6901
e326bed2 6902 net_rps_action_and_irq_enable(sd);
f52dffe0
ED
6903out:
6904 __kfree_skb_flush();
1da177e4
LT
6905}
6906
aa9d8560 6907struct netdev_adjacent {
9ff162a8 6908 struct net_device *dev;
5d261913
VF
6909
6910 /* upper master flag, there can only be one master device per list */
9ff162a8 6911 bool master;
5d261913 6912
32b6d34f
TY
6913 /* lookup ignore flag */
6914 bool ignore;
6915
5d261913
VF
6916 /* counter for the number of times this device was added to us */
6917 u16 ref_nr;
6918
402dae96
VF
6919 /* private field for the users */
6920 void *private;
6921
9ff162a8
JP
6922 struct list_head list;
6923 struct rcu_head rcu;
9ff162a8
JP
6924};
6925
6ea29da1 6926static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
2f268f12 6927 struct list_head *adj_list)
9ff162a8 6928{
5d261913 6929 struct netdev_adjacent *adj;
5d261913 6930
2f268f12 6931 list_for_each_entry(adj, adj_list, list) {
5d261913
VF
6932 if (adj->dev == adj_dev)
6933 return adj;
9ff162a8
JP
6934 }
6935 return NULL;
6936}
6937
eff74233
TY
6938static int ____netdev_has_upper_dev(struct net_device *upper_dev,
6939 struct netdev_nested_priv *priv)
f1170fd4 6940{
eff74233 6941 struct net_device *dev = (struct net_device *)priv->data;
f1170fd4
DA
6942
6943 return upper_dev == dev;
6944}
6945
9ff162a8
JP
6946/**
6947 * netdev_has_upper_dev - Check if device is linked to an upper device
6948 * @dev: device
6949 * @upper_dev: upper device to check
6950 *
6951 * Find out if a device is linked to specified upper device and return true
6952 * in case it is. Note that this checks only immediate upper device,
6953 * not through a complete stack of devices. The caller must hold the RTNL lock.
6954 */
6955bool netdev_has_upper_dev(struct net_device *dev,
6956 struct net_device *upper_dev)
6957{
eff74233
TY
6958 struct netdev_nested_priv priv = {
6959 .data = (void *)upper_dev,
6960 };
6961
9ff162a8
JP
6962 ASSERT_RTNL();
6963
32b6d34f 6964 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
eff74233 6965 &priv);
9ff162a8
JP
6966}
6967EXPORT_SYMBOL(netdev_has_upper_dev);
6968
1a3f060c 6969/**
c1639be9 6970 * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device
1a3f060c
DA
6971 * @dev: device
6972 * @upper_dev: upper device to check
6973 *
6974 * Find out if a device is linked to specified upper device and return true
6975 * in case it is. Note that this checks the entire upper device chain.
6976 * The caller must hold rcu lock.
6977 */
6978
1a3f060c
DA
6979bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6980 struct net_device *upper_dev)
6981{
eff74233
TY
6982 struct netdev_nested_priv priv = {
6983 .data = (void *)upper_dev,
6984 };
6985
32b6d34f 6986 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
eff74233 6987 &priv);
1a3f060c
DA
6988}
6989EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6990
9ff162a8
JP
6991/**
6992 * netdev_has_any_upper_dev - Check if device is linked to some device
6993 * @dev: device
6994 *
6995 * Find out if a device is linked to an upper device and return true in case
6996 * it is. The caller must hold the RTNL lock.
6997 */
25cc72a3 6998bool netdev_has_any_upper_dev(struct net_device *dev)
9ff162a8
JP
6999{
7000 ASSERT_RTNL();
7001
f1170fd4 7002 return !list_empty(&dev->adj_list.upper);
9ff162a8 7003}
25cc72a3 7004EXPORT_SYMBOL(netdev_has_any_upper_dev);
9ff162a8
JP
7005
7006/**
7007 * netdev_master_upper_dev_get - Get master upper device
7008 * @dev: device
7009 *
7010 * Find a master upper device and return pointer to it or NULL in case
7011 * it's not there. The caller must hold the RTNL lock.
7012 */
7013struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
7014{
aa9d8560 7015 struct netdev_adjacent *upper;
9ff162a8
JP
7016
7017 ASSERT_RTNL();
7018
2f268f12 7019 if (list_empty(&dev->adj_list.upper))
9ff162a8
JP
7020 return NULL;
7021
2f268f12 7022 upper = list_first_entry(&dev->adj_list.upper,
aa9d8560 7023 struct netdev_adjacent, list);
9ff162a8
JP
7024 if (likely(upper->master))
7025 return upper->dev;
7026 return NULL;
7027}
7028EXPORT_SYMBOL(netdev_master_upper_dev_get);
7029
32b6d34f
TY
7030static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
7031{
7032 struct netdev_adjacent *upper;
7033
7034 ASSERT_RTNL();
7035
7036 if (list_empty(&dev->adj_list.upper))
7037 return NULL;
7038
7039 upper = list_first_entry(&dev->adj_list.upper,
7040 struct netdev_adjacent, list);
7041 if (likely(upper->master) && !upper->ignore)
7042 return upper->dev;
7043 return NULL;
7044}
7045
0f524a80
DA
7046/**
7047 * netdev_has_any_lower_dev - Check if device is linked to some device
7048 * @dev: device
7049 *
7050 * Find out if a device is linked to a lower device and return true in case
7051 * it is. The caller must hold the RTNL lock.
7052 */
7053static bool netdev_has_any_lower_dev(struct net_device *dev)
7054{
7055 ASSERT_RTNL();
7056
7057 return !list_empty(&dev->adj_list.lower);
7058}
7059
b6ccba4c
VF
7060void *netdev_adjacent_get_private(struct list_head *adj_list)
7061{
7062 struct netdev_adjacent *adj;
7063
7064 adj = list_entry(adj_list, struct netdev_adjacent, list);
7065
7066 return adj->private;
7067}
7068EXPORT_SYMBOL(netdev_adjacent_get_private);
7069
44a40855
VY
7070/**
7071 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
7072 * @dev: device
7073 * @iter: list_head ** of the current position
7074 *
7075 * Gets the next device from the dev's upper list, starting from iter
7076 * position. The caller must hold RCU read lock.
7077 */
7078struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
7079 struct list_head **iter)
7080{
7081 struct netdev_adjacent *upper;
7082
7083 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7084
7085 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7086
7087 if (&upper->list == &dev->adj_list.upper)
7088 return NULL;
7089
7090 *iter = &upper->list;
7091
7092 return upper->dev;
7093}
7094EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
7095
32b6d34f
TY
7096static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
7097 struct list_head **iter,
7098 bool *ignore)
5343da4c
TY
7099{
7100 struct netdev_adjacent *upper;
7101
7102 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
7103
7104 if (&upper->list == &dev->adj_list.upper)
7105 return NULL;
7106
7107 *iter = &upper->list;
32b6d34f 7108 *ignore = upper->ignore;
5343da4c
TY
7109
7110 return upper->dev;
7111}
7112
1a3f060c
DA
7113static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
7114 struct list_head **iter)
7115{
7116 struct netdev_adjacent *upper;
7117
7118 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7119
7120 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7121
7122 if (&upper->list == &dev->adj_list.upper)
7123 return NULL;
7124
7125 *iter = &upper->list;
7126
7127 return upper->dev;
7128}
7129
32b6d34f
TY
7130static int __netdev_walk_all_upper_dev(struct net_device *dev,
7131 int (*fn)(struct net_device *dev,
eff74233
TY
7132 struct netdev_nested_priv *priv),
7133 struct netdev_nested_priv *priv)
5343da4c
TY
7134{
7135 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7136 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7137 int ret, cur = 0;
32b6d34f 7138 bool ignore;
5343da4c
TY
7139
7140 now = dev;
7141 iter = &dev->adj_list.upper;
7142
7143 while (1) {
7144 if (now != dev) {
eff74233 7145 ret = fn(now, priv);
5343da4c
TY
7146 if (ret)
7147 return ret;
7148 }
7149
7150 next = NULL;
7151 while (1) {
32b6d34f 7152 udev = __netdev_next_upper_dev(now, &iter, &ignore);
5343da4c
TY
7153 if (!udev)
7154 break;
32b6d34f
TY
7155 if (ignore)
7156 continue;
5343da4c
TY
7157
7158 next = udev;
7159 niter = &udev->adj_list.upper;
7160 dev_stack[cur] = now;
7161 iter_stack[cur++] = iter;
7162 break;
7163 }
7164
7165 if (!next) {
7166 if (!cur)
7167 return 0;
7168 next = dev_stack[--cur];
7169 niter = iter_stack[cur];
7170 }
7171
7172 now = next;
7173 iter = niter;
7174 }
7175
7176 return 0;
7177}
7178
1a3f060c
DA
7179int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
7180 int (*fn)(struct net_device *dev,
eff74233
TY
7181 struct netdev_nested_priv *priv),
7182 struct netdev_nested_priv *priv)
1a3f060c 7183{
5343da4c
TY
7184 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7185 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7186 int ret, cur = 0;
1a3f060c 7187
5343da4c
TY
7188 now = dev;
7189 iter = &dev->adj_list.upper;
1a3f060c 7190
5343da4c
TY
7191 while (1) {
7192 if (now != dev) {
eff74233 7193 ret = fn(now, priv);
5343da4c
TY
7194 if (ret)
7195 return ret;
7196 }
7197
7198 next = NULL;
7199 while (1) {
7200 udev = netdev_next_upper_dev_rcu(now, &iter);
7201 if (!udev)
7202 break;
7203
7204 next = udev;
7205 niter = &udev->adj_list.upper;
7206 dev_stack[cur] = now;
7207 iter_stack[cur++] = iter;
7208 break;
7209 }
7210
7211 if (!next) {
7212 if (!cur)
7213 return 0;
7214 next = dev_stack[--cur];
7215 niter = iter_stack[cur];
7216 }
7217
7218 now = next;
7219 iter = niter;
1a3f060c
DA
7220 }
7221
7222 return 0;
7223}
7224EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
7225
32b6d34f
TY
7226static bool __netdev_has_upper_dev(struct net_device *dev,
7227 struct net_device *upper_dev)
7228{
eff74233 7229 struct netdev_nested_priv priv = {
1fc70edb 7230 .flags = 0,
eff74233
TY
7231 .data = (void *)upper_dev,
7232 };
7233
32b6d34f
TY
7234 ASSERT_RTNL();
7235
7236 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
eff74233 7237 &priv);
32b6d34f
TY
7238}
7239
31088a11
VF
7240/**
7241 * netdev_lower_get_next_private - Get the next ->private from the
7242 * lower neighbour list
7243 * @dev: device
7244 * @iter: list_head ** of the current position
7245 *
7246 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7247 * list, starting from iter position. The caller must hold either hold the
7248 * RTNL lock or its own locking that guarantees that the neighbour lower
b469139e 7249 * list will remain unchanged.
31088a11
VF
7250 */
7251void *netdev_lower_get_next_private(struct net_device *dev,
7252 struct list_head **iter)
7253{
7254 struct netdev_adjacent *lower;
7255
7256 lower = list_entry(*iter, struct netdev_adjacent, list);
7257
7258 if (&lower->list == &dev->adj_list.lower)
7259 return NULL;
7260
6859e7df 7261 *iter = lower->list.next;
31088a11
VF
7262
7263 return lower->private;
7264}
7265EXPORT_SYMBOL(netdev_lower_get_next_private);
7266
7267/**
7268 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7269 * lower neighbour list, RCU
7270 * variant
7271 * @dev: device
7272 * @iter: list_head ** of the current position
7273 *
7274 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7275 * list, starting from iter position. The caller must hold RCU read lock.
7276 */
7277void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7278 struct list_head **iter)
7279{
7280 struct netdev_adjacent *lower;
7281
7282 WARN_ON_ONCE(!rcu_read_lock_held());
7283
7284 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7285
7286 if (&lower->list == &dev->adj_list.lower)
7287 return NULL;
7288
6859e7df 7289 *iter = &lower->list;
31088a11
VF
7290
7291 return lower->private;
7292}
7293EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7294
4085ebe8
VY
7295/**
7296 * netdev_lower_get_next - Get the next device from the lower neighbour
7297 * list
7298 * @dev: device
7299 * @iter: list_head ** of the current position
7300 *
7301 * Gets the next netdev_adjacent from the dev's lower neighbour
7302 * list, starting from iter position. The caller must hold RTNL lock or
7303 * its own locking that guarantees that the neighbour lower
b469139e 7304 * list will remain unchanged.
4085ebe8
VY
7305 */
7306void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7307{
7308 struct netdev_adjacent *lower;
7309
cfdd28be 7310 lower = list_entry(*iter, struct netdev_adjacent, list);
4085ebe8
VY
7311
7312 if (&lower->list == &dev->adj_list.lower)
7313 return NULL;
7314
cfdd28be 7315 *iter = lower->list.next;
4085ebe8
VY
7316
7317 return lower->dev;
7318}
7319EXPORT_SYMBOL(netdev_lower_get_next);
7320
1a3f060c
DA
7321static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7322 struct list_head **iter)
7323{
7324 struct netdev_adjacent *lower;
7325
46b5ab1a 7326 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
1a3f060c
DA
7327
7328 if (&lower->list == &dev->adj_list.lower)
7329 return NULL;
7330
46b5ab1a 7331 *iter = &lower->list;
1a3f060c
DA
7332
7333 return lower->dev;
7334}
7335
32b6d34f
TY
7336static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7337 struct list_head **iter,
7338 bool *ignore)
7339{
7340 struct netdev_adjacent *lower;
7341
7342 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7343
7344 if (&lower->list == &dev->adj_list.lower)
7345 return NULL;
7346
7347 *iter = &lower->list;
7348 *ignore = lower->ignore;
7349
7350 return lower->dev;
7351}
7352
1a3f060c
DA
7353int netdev_walk_all_lower_dev(struct net_device *dev,
7354 int (*fn)(struct net_device *dev,
eff74233
TY
7355 struct netdev_nested_priv *priv),
7356 struct netdev_nested_priv *priv)
1a3f060c 7357{
5343da4c
TY
7358 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7359 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7360 int ret, cur = 0;
1a3f060c 7361
5343da4c
TY
7362 now = dev;
7363 iter = &dev->adj_list.lower;
1a3f060c 7364
5343da4c
TY
7365 while (1) {
7366 if (now != dev) {
eff74233 7367 ret = fn(now, priv);
5343da4c
TY
7368 if (ret)
7369 return ret;
7370 }
7371
7372 next = NULL;
7373 while (1) {
7374 ldev = netdev_next_lower_dev(now, &iter);
7375 if (!ldev)
7376 break;
7377
7378 next = ldev;
7379 niter = &ldev->adj_list.lower;
7380 dev_stack[cur] = now;
7381 iter_stack[cur++] = iter;
7382 break;
7383 }
7384
7385 if (!next) {
7386 if (!cur)
7387 return 0;
7388 next = dev_stack[--cur];
7389 niter = iter_stack[cur];
7390 }
7391
7392 now = next;
7393 iter = niter;
1a3f060c
DA
7394 }
7395
7396 return 0;
7397}
7398EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7399
32b6d34f
TY
7400static int __netdev_walk_all_lower_dev(struct net_device *dev,
7401 int (*fn)(struct net_device *dev,
eff74233
TY
7402 struct netdev_nested_priv *priv),
7403 struct netdev_nested_priv *priv)
32b6d34f
TY
7404{
7405 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7406 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7407 int ret, cur = 0;
7408 bool ignore;
7409
7410 now = dev;
7411 iter = &dev->adj_list.lower;
7412
7413 while (1) {
7414 if (now != dev) {
eff74233 7415 ret = fn(now, priv);
32b6d34f
TY
7416 if (ret)
7417 return ret;
7418 }
7419
7420 next = NULL;
7421 while (1) {
7422 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7423 if (!ldev)
7424 break;
7425 if (ignore)
7426 continue;
7427
7428 next = ldev;
7429 niter = &ldev->adj_list.lower;
7430 dev_stack[cur] = now;
7431 iter_stack[cur++] = iter;
7432 break;
7433 }
7434
7435 if (!next) {
7436 if (!cur)
7437 return 0;
7438 next = dev_stack[--cur];
7439 niter = iter_stack[cur];
7440 }
7441
7442 now = next;
7443 iter = niter;
7444 }
7445
7446 return 0;
7447}
7448
7151affe
TY
7449struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7450 struct list_head **iter)
1a3f060c
DA
7451{
7452 struct netdev_adjacent *lower;
7453
7454 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7455 if (&lower->list == &dev->adj_list.lower)
7456 return NULL;
7457
7458 *iter = &lower->list;
7459
7460 return lower->dev;
7461}
7151affe 7462EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
1a3f060c 7463
5343da4c
TY
7464static u8 __netdev_upper_depth(struct net_device *dev)
7465{
7466 struct net_device *udev;
7467 struct list_head *iter;
7468 u8 max_depth = 0;
32b6d34f 7469 bool ignore;
5343da4c
TY
7470
7471 for (iter = &dev->adj_list.upper,
32b6d34f 7472 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
5343da4c 7473 udev;
32b6d34f
TY
7474 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7475 if (ignore)
7476 continue;
5343da4c
TY
7477 if (max_depth < udev->upper_level)
7478 max_depth = udev->upper_level;
7479 }
7480
7481 return max_depth;
7482}
7483
7484static u8 __netdev_lower_depth(struct net_device *dev)
1a3f060c
DA
7485{
7486 struct net_device *ldev;
7487 struct list_head *iter;
5343da4c 7488 u8 max_depth = 0;
32b6d34f 7489 bool ignore;
1a3f060c
DA
7490
7491 for (iter = &dev->adj_list.lower,
32b6d34f 7492 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
1a3f060c 7493 ldev;
32b6d34f
TY
7494 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7495 if (ignore)
7496 continue;
5343da4c
TY
7497 if (max_depth < ldev->lower_level)
7498 max_depth = ldev->lower_level;
7499 }
1a3f060c 7500
5343da4c
TY
7501 return max_depth;
7502}
7503
eff74233
TY
7504static int __netdev_update_upper_level(struct net_device *dev,
7505 struct netdev_nested_priv *__unused)
5343da4c
TY
7506{
7507 dev->upper_level = __netdev_upper_depth(dev) + 1;
7508 return 0;
7509}
7510
eff74233 7511static int __netdev_update_lower_level(struct net_device *dev,
1fc70edb 7512 struct netdev_nested_priv *priv)
5343da4c
TY
7513{
7514 dev->lower_level = __netdev_lower_depth(dev) + 1;
1fc70edb
TY
7515
7516#ifdef CONFIG_LOCKDEP
7517 if (!priv)
7518 return 0;
7519
7520 if (priv->flags & NESTED_SYNC_IMM)
7521 dev->nested_level = dev->lower_level - 1;
7522 if (priv->flags & NESTED_SYNC_TODO)
7523 net_unlink_todo(dev);
7524#endif
5343da4c
TY
7525 return 0;
7526}
7527
7528int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7529 int (*fn)(struct net_device *dev,
eff74233
TY
7530 struct netdev_nested_priv *priv),
7531 struct netdev_nested_priv *priv)
5343da4c
TY
7532{
7533 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7534 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7535 int ret, cur = 0;
7536
7537 now = dev;
7538 iter = &dev->adj_list.lower;
7539
7540 while (1) {
7541 if (now != dev) {
eff74233 7542 ret = fn(now, priv);
5343da4c
TY
7543 if (ret)
7544 return ret;
7545 }
7546
7547 next = NULL;
7548 while (1) {
7549 ldev = netdev_next_lower_dev_rcu(now, &iter);
7550 if (!ldev)
7551 break;
7552
7553 next = ldev;
7554 niter = &ldev->adj_list.lower;
7555 dev_stack[cur] = now;
7556 iter_stack[cur++] = iter;
7557 break;
7558 }
7559
7560 if (!next) {
7561 if (!cur)
7562 return 0;
7563 next = dev_stack[--cur];
7564 niter = iter_stack[cur];
7565 }
7566
7567 now = next;
7568 iter = niter;
1a3f060c
DA
7569 }
7570
7571 return 0;
7572}
7573EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7574
e001bfad 7575/**
7576 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7577 * lower neighbour list, RCU
7578 * variant
7579 * @dev: device
7580 *
7581 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7582 * list. The caller must hold RCU read lock.
7583 */
7584void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7585{
7586 struct netdev_adjacent *lower;
7587
7588 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7589 struct netdev_adjacent, list);
7590 if (lower)
7591 return lower->private;
7592 return NULL;
7593}
7594EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7595
9ff162a8
JP
7596/**
7597 * netdev_master_upper_dev_get_rcu - Get master upper device
7598 * @dev: device
7599 *
7600 * Find a master upper device and return pointer to it or NULL in case
7601 * it's not there. The caller must hold the RCU read lock.
7602 */
7603struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7604{
aa9d8560 7605 struct netdev_adjacent *upper;
9ff162a8 7606
2f268f12 7607 upper = list_first_or_null_rcu(&dev->adj_list.upper,
aa9d8560 7608 struct netdev_adjacent, list);
9ff162a8
JP
7609 if (upper && likely(upper->master))
7610 return upper->dev;
7611 return NULL;
7612}
7613EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7614
0a59f3a9 7615static int netdev_adjacent_sysfs_add(struct net_device *dev,
3ee32707
VF
7616 struct net_device *adj_dev,
7617 struct list_head *dev_list)
7618{
7619 char linkname[IFNAMSIZ+7];
f4563a75 7620
3ee32707
VF
7621 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7622 "upper_%s" : "lower_%s", adj_dev->name);
7623 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7624 linkname);
7625}
0a59f3a9 7626static void netdev_adjacent_sysfs_del(struct net_device *dev,
3ee32707
VF
7627 char *name,
7628 struct list_head *dev_list)
7629{
7630 char linkname[IFNAMSIZ+7];
f4563a75 7631
3ee32707
VF
7632 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7633 "upper_%s" : "lower_%s", name);
7634 sysfs_remove_link(&(dev->dev.kobj), linkname);
7635}
7636
7ce64c79
AF
7637static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7638 struct net_device *adj_dev,
7639 struct list_head *dev_list)
7640{
7641 return (dev_list == &dev->adj_list.upper ||
7642 dev_list == &dev->adj_list.lower) &&
7643 net_eq(dev_net(dev), dev_net(adj_dev));
7644}
3ee32707 7645
5d261913
VF
7646static int __netdev_adjacent_dev_insert(struct net_device *dev,
7647 struct net_device *adj_dev,
7863c054 7648 struct list_head *dev_list,
402dae96 7649 void *private, bool master)
5d261913
VF
7650{
7651 struct netdev_adjacent *adj;
842d67a7 7652 int ret;
5d261913 7653
6ea29da1 7654 adj = __netdev_find_adj(adj_dev, dev_list);
5d261913
VF
7655
7656 if (adj) {
790510d9 7657 adj->ref_nr += 1;
67b62f98
DA
7658 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7659 dev->name, adj_dev->name, adj->ref_nr);
7660
5d261913
VF
7661 return 0;
7662 }
7663
7664 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7665 if (!adj)
7666 return -ENOMEM;
7667
7668 adj->dev = adj_dev;
7669 adj->master = master;
790510d9 7670 adj->ref_nr = 1;
402dae96 7671 adj->private = private;
32b6d34f 7672 adj->ignore = false;
5d261913 7673 dev_hold(adj_dev);
2f268f12 7674
67b62f98
DA
7675 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7676 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
5d261913 7677
7ce64c79 7678 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
3ee32707 7679 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
5831d66e
VF
7680 if (ret)
7681 goto free_adj;
7682 }
7683
7863c054 7684 /* Ensure that master link is always the first item in list. */
842d67a7
VF
7685 if (master) {
7686 ret = sysfs_create_link(&(dev->dev.kobj),
7687 &(adj_dev->dev.kobj), "master");
7688 if (ret)
5831d66e 7689 goto remove_symlinks;
842d67a7 7690
7863c054 7691 list_add_rcu(&adj->list, dev_list);
842d67a7 7692 } else {
7863c054 7693 list_add_tail_rcu(&adj->list, dev_list);
842d67a7 7694 }
5d261913
VF
7695
7696 return 0;
842d67a7 7697
5831d66e 7698remove_symlinks:
7ce64c79 7699 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
3ee32707 7700 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
842d67a7
VF
7701free_adj:
7702 kfree(adj);
974daef7 7703 dev_put(adj_dev);
842d67a7
VF
7704
7705 return ret;
5d261913
VF
7706}
7707
1d143d9f 7708static void __netdev_adjacent_dev_remove(struct net_device *dev,
7709 struct net_device *adj_dev,
93409033 7710 u16 ref_nr,
1d143d9f 7711 struct list_head *dev_list)
5d261913
VF
7712{
7713 struct netdev_adjacent *adj;
7714
67b62f98
DA
7715 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7716 dev->name, adj_dev->name, ref_nr);
7717
6ea29da1 7718 adj = __netdev_find_adj(adj_dev, dev_list);
5d261913 7719
2f268f12 7720 if (!adj) {
67b62f98 7721 pr_err("Adjacency does not exist for device %s from %s\n",
2f268f12 7722 dev->name, adj_dev->name);
67b62f98
DA
7723 WARN_ON(1);
7724 return;
2f268f12 7725 }
5d261913 7726
93409033 7727 if (adj->ref_nr > ref_nr) {
67b62f98
DA
7728 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7729 dev->name, adj_dev->name, ref_nr,
7730 adj->ref_nr - ref_nr);
93409033 7731 adj->ref_nr -= ref_nr;
5d261913
VF
7732 return;
7733 }
7734
842d67a7
VF
7735 if (adj->master)
7736 sysfs_remove_link(&(dev->dev.kobj), "master");
7737
7ce64c79 7738 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
3ee32707 7739 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
5831d66e 7740
5d261913 7741 list_del_rcu(&adj->list);
67b62f98 7742 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
2f268f12 7743 adj_dev->name, dev->name, adj_dev->name);
5d261913
VF
7744 dev_put(adj_dev);
7745 kfree_rcu(adj, rcu);
7746}
7747
1d143d9f 7748static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7749 struct net_device *upper_dev,
7750 struct list_head *up_list,
7751 struct list_head *down_list,
7752 void *private, bool master)
5d261913
VF
7753{
7754 int ret;
7755
790510d9 7756 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
93409033 7757 private, master);
5d261913
VF
7758 if (ret)
7759 return ret;
7760
790510d9 7761 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
93409033 7762 private, false);
5d261913 7763 if (ret) {
790510d9 7764 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
5d261913
VF
7765 return ret;
7766 }
7767
7768 return 0;
7769}
7770
1d143d9f 7771static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7772 struct net_device *upper_dev,
93409033 7773 u16 ref_nr,
1d143d9f 7774 struct list_head *up_list,
7775 struct list_head *down_list)
5d261913 7776{
93409033
AC
7777 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7778 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
5d261913
VF
7779}
7780
1d143d9f 7781static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7782 struct net_device *upper_dev,
7783 void *private, bool master)
2f268f12 7784{
f1170fd4
DA
7785 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7786 &dev->adj_list.upper,
7787 &upper_dev->adj_list.lower,
7788 private, master);
5d261913
VF
7789}
7790
1d143d9f 7791static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7792 struct net_device *upper_dev)
2f268f12 7793{
93409033 7794 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
2f268f12
VF
7795 &dev->adj_list.upper,
7796 &upper_dev->adj_list.lower);
7797}
5d261913 7798
9ff162a8 7799static int __netdev_upper_dev_link(struct net_device *dev,
402dae96 7800 struct net_device *upper_dev, bool master,
42ab19ee 7801 void *upper_priv, void *upper_info,
1fc70edb 7802 struct netdev_nested_priv *priv,
42ab19ee 7803 struct netlink_ext_ack *extack)
9ff162a8 7804{
51d0c047
DA
7805 struct netdev_notifier_changeupper_info changeupper_info = {
7806 .info = {
7807 .dev = dev,
42ab19ee 7808 .extack = extack,
51d0c047
DA
7809 },
7810 .upper_dev = upper_dev,
7811 .master = master,
7812 .linking = true,
7813 .upper_info = upper_info,
7814 };
50d629e7 7815 struct net_device *master_dev;
5d261913 7816 int ret = 0;
9ff162a8
JP
7817
7818 ASSERT_RTNL();
7819
7820 if (dev == upper_dev)
7821 return -EBUSY;
7822
7823 /* To prevent loops, check if dev is not upper device to upper_dev. */
32b6d34f 7824 if (__netdev_has_upper_dev(upper_dev, dev))
9ff162a8
JP
7825 return -EBUSY;
7826
5343da4c
TY
7827 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7828 return -EMLINK;
7829
50d629e7 7830 if (!master) {
32b6d34f 7831 if (__netdev_has_upper_dev(dev, upper_dev))
50d629e7
MM
7832 return -EEXIST;
7833 } else {
32b6d34f 7834 master_dev = __netdev_master_upper_dev_get(dev);
50d629e7
MM
7835 if (master_dev)
7836 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7837 }
9ff162a8 7838
51d0c047 7839 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
573c7ba0
JP
7840 &changeupper_info.info);
7841 ret = notifier_to_errno(ret);
7842 if (ret)
7843 return ret;
7844
6dffb044 7845 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
402dae96 7846 master);
5d261913
VF
7847 if (ret)
7848 return ret;
9ff162a8 7849
51d0c047 7850 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
b03804e7
IS
7851 &changeupper_info.info);
7852 ret = notifier_to_errno(ret);
7853 if (ret)
f1170fd4 7854 goto rollback;
b03804e7 7855
5343da4c 7856 __netdev_update_upper_level(dev, NULL);
32b6d34f 7857 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
5343da4c 7858
1fc70edb 7859 __netdev_update_lower_level(upper_dev, priv);
32b6d34f 7860 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
1fc70edb 7861 priv);
5343da4c 7862
9ff162a8 7863 return 0;
5d261913 7864
f1170fd4 7865rollback:
2f268f12 7866 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
5d261913
VF
7867
7868 return ret;
9ff162a8
JP
7869}
7870
7871/**
7872 * netdev_upper_dev_link - Add a link to the upper device
7873 * @dev: device
7874 * @upper_dev: new upper device
7a006d59 7875 * @extack: netlink extended ack
9ff162a8
JP
7876 *
7877 * Adds a link to device which is upper to this one. The caller must hold
7878 * the RTNL lock. On a failure a negative errno code is returned.
7879 * On success the reference counts are adjusted and the function
7880 * returns zero.
7881 */
7882int netdev_upper_dev_link(struct net_device *dev,
42ab19ee
DA
7883 struct net_device *upper_dev,
7884 struct netlink_ext_ack *extack)
9ff162a8 7885{
1fc70edb
TY
7886 struct netdev_nested_priv priv = {
7887 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7888 .data = NULL,
7889 };
7890
42ab19ee 7891 return __netdev_upper_dev_link(dev, upper_dev, false,
1fc70edb 7892 NULL, NULL, &priv, extack);
9ff162a8
JP
7893}
7894EXPORT_SYMBOL(netdev_upper_dev_link);
7895
7896/**
7897 * netdev_master_upper_dev_link - Add a master link to the upper device
7898 * @dev: device
7899 * @upper_dev: new upper device
6dffb044 7900 * @upper_priv: upper device private
29bf24af 7901 * @upper_info: upper info to be passed down via notifier
7a006d59 7902 * @extack: netlink extended ack
9ff162a8
JP
7903 *
7904 * Adds a link to device which is upper to this one. In this case, only
7905 * one master upper device can be linked, although other non-master devices
7906 * might be linked as well. The caller must hold the RTNL lock.
7907 * On a failure a negative errno code is returned. On success the reference
7908 * counts are adjusted and the function returns zero.
7909 */
7910int netdev_master_upper_dev_link(struct net_device *dev,
6dffb044 7911 struct net_device *upper_dev,
42ab19ee
DA
7912 void *upper_priv, void *upper_info,
7913 struct netlink_ext_ack *extack)
9ff162a8 7914{
1fc70edb
TY
7915 struct netdev_nested_priv priv = {
7916 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
7917 .data = NULL,
7918 };
7919
29bf24af 7920 return __netdev_upper_dev_link(dev, upper_dev, true,
1fc70edb 7921 upper_priv, upper_info, &priv, extack);
9ff162a8
JP
7922}
7923EXPORT_SYMBOL(netdev_master_upper_dev_link);
7924
fe8300fd 7925static void __netdev_upper_dev_unlink(struct net_device *dev,
1fc70edb
TY
7926 struct net_device *upper_dev,
7927 struct netdev_nested_priv *priv)
9ff162a8 7928{
51d0c047
DA
7929 struct netdev_notifier_changeupper_info changeupper_info = {
7930 .info = {
7931 .dev = dev,
7932 },
7933 .upper_dev = upper_dev,
7934 .linking = false,
7935 };
f4563a75 7936
9ff162a8
JP
7937 ASSERT_RTNL();
7938
0e4ead9d 7939 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
0e4ead9d 7940
51d0c047 7941 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
573c7ba0
JP
7942 &changeupper_info.info);
7943
2f268f12 7944 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
5d261913 7945
51d0c047 7946 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
0e4ead9d 7947 &changeupper_info.info);
5343da4c
TY
7948
7949 __netdev_update_upper_level(dev, NULL);
32b6d34f 7950 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
5343da4c 7951
1fc70edb 7952 __netdev_update_lower_level(upper_dev, priv);
32b6d34f 7953 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
1fc70edb 7954 priv);
9ff162a8 7955}
fe8300fd
TY
7956
7957/**
7958 * netdev_upper_dev_unlink - Removes a link to upper device
7959 * @dev: device
7960 * @upper_dev: new upper device
7961 *
7962 * Removes a link to device which is upper to this one. The caller must hold
7963 * the RTNL lock.
7964 */
7965void netdev_upper_dev_unlink(struct net_device *dev,
7966 struct net_device *upper_dev)
7967{
1fc70edb
TY
7968 struct netdev_nested_priv priv = {
7969 .flags = NESTED_SYNC_TODO,
7970 .data = NULL,
7971 };
7972
7973 __netdev_upper_dev_unlink(dev, upper_dev, &priv);
9ff162a8
JP
7974}
7975EXPORT_SYMBOL(netdev_upper_dev_unlink);
7976
32b6d34f
TY
7977static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7978 struct net_device *lower_dev,
7979 bool val)
7980{
7981 struct netdev_adjacent *adj;
7982
7983 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7984 if (adj)
7985 adj->ignore = val;
7986
7987 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7988 if (adj)
7989 adj->ignore = val;
7990}
7991
7992static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7993 struct net_device *lower_dev)
7994{
7995 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7996}
7997
7998static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7999 struct net_device *lower_dev)
8000{
8001 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
8002}
8003
8004int netdev_adjacent_change_prepare(struct net_device *old_dev,
8005 struct net_device *new_dev,
8006 struct net_device *dev,
8007 struct netlink_ext_ack *extack)
8008{
1fc70edb
TY
8009 struct netdev_nested_priv priv = {
8010 .flags = 0,
8011 .data = NULL,
8012 };
32b6d34f
TY
8013 int err;
8014
8015 if (!new_dev)
8016 return 0;
8017
8018 if (old_dev && new_dev != old_dev)
8019 netdev_adjacent_dev_disable(dev, old_dev);
1fc70edb
TY
8020 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
8021 extack);
32b6d34f
TY
8022 if (err) {
8023 if (old_dev && new_dev != old_dev)
8024 netdev_adjacent_dev_enable(dev, old_dev);
8025 return err;
8026 }
8027
8028 return 0;
8029}
8030EXPORT_SYMBOL(netdev_adjacent_change_prepare);
8031
8032void netdev_adjacent_change_commit(struct net_device *old_dev,
8033 struct net_device *new_dev,
8034 struct net_device *dev)
8035{
1fc70edb
TY
8036 struct netdev_nested_priv priv = {
8037 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8038 .data = NULL,
8039 };
8040
32b6d34f
TY
8041 if (!new_dev || !old_dev)
8042 return;
8043
8044 if (new_dev == old_dev)
8045 return;
8046
8047 netdev_adjacent_dev_enable(dev, old_dev);
1fc70edb 8048 __netdev_upper_dev_unlink(old_dev, dev, &priv);
32b6d34f
TY
8049}
8050EXPORT_SYMBOL(netdev_adjacent_change_commit);
8051
8052void netdev_adjacent_change_abort(struct net_device *old_dev,
8053 struct net_device *new_dev,
8054 struct net_device *dev)
8055{
1fc70edb
TY
8056 struct netdev_nested_priv priv = {
8057 .flags = 0,
8058 .data = NULL,
8059 };
8060
32b6d34f
TY
8061 if (!new_dev)
8062 return;
8063
8064 if (old_dev && new_dev != old_dev)
8065 netdev_adjacent_dev_enable(dev, old_dev);
8066
1fc70edb 8067 __netdev_upper_dev_unlink(new_dev, dev, &priv);
32b6d34f
TY
8068}
8069EXPORT_SYMBOL(netdev_adjacent_change_abort);
8070
61bd3857
MS
8071/**
8072 * netdev_bonding_info_change - Dispatch event about slave change
8073 * @dev: device
4a26e453 8074 * @bonding_info: info to dispatch
61bd3857
MS
8075 *
8076 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
8077 * The caller must hold the RTNL lock.
8078 */
8079void netdev_bonding_info_change(struct net_device *dev,
8080 struct netdev_bonding_info *bonding_info)
8081{
51d0c047
DA
8082 struct netdev_notifier_bonding_info info = {
8083 .info.dev = dev,
8084 };
61bd3857
MS
8085
8086 memcpy(&info.bonding_info, bonding_info,
8087 sizeof(struct netdev_bonding_info));
51d0c047 8088 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
61bd3857
MS
8089 &info.info);
8090}
8091EXPORT_SYMBOL(netdev_bonding_info_change);
8092
cff9f12b
MG
8093/**
8094 * netdev_get_xmit_slave - Get the xmit slave of master device
8842500d 8095 * @dev: device
cff9f12b
MG
8096 * @skb: The packet
8097 * @all_slaves: assume all the slaves are active
8098 *
8099 * The reference counters are not incremented so the caller must be
8100 * careful with locks. The caller must hold RCU lock.
8101 * %NULL is returned if no slave is found.
8102 */
8103
8104struct net_device *netdev_get_xmit_slave(struct net_device *dev,
8105 struct sk_buff *skb,
8106 bool all_slaves)
8107{
8108 const struct net_device_ops *ops = dev->netdev_ops;
8109
8110 if (!ops->ndo_get_xmit_slave)
8111 return NULL;
8112 return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
8113}
8114EXPORT_SYMBOL(netdev_get_xmit_slave);
8115
719a402c
TT
8116static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev,
8117 struct sock *sk)
8118{
8119 const struct net_device_ops *ops = dev->netdev_ops;
8120
8121 if (!ops->ndo_sk_get_lower_dev)
8122 return NULL;
8123 return ops->ndo_sk_get_lower_dev(dev, sk);
8124}
8125
8126/**
8127 * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket
8128 * @dev: device
8129 * @sk: the socket
8130 *
8131 * %NULL is returned if no lower device is found.
8132 */
8133
8134struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
8135 struct sock *sk)
8136{
8137 struct net_device *lower;
8138
8139 lower = netdev_sk_get_lower_dev(dev, sk);
8140 while (lower) {
8141 dev = lower;
8142 lower = netdev_sk_get_lower_dev(dev, sk);
8143 }
8144
8145 return dev;
8146}
8147EXPORT_SYMBOL(netdev_sk_get_lowest_dev);
8148
2ce1ee17 8149static void netdev_adjacent_add_links(struct net_device *dev)
4c75431a
AF
8150{
8151 struct netdev_adjacent *iter;
8152
8153 struct net *net = dev_net(dev);
8154
8155 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 8156 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
8157 continue;
8158 netdev_adjacent_sysfs_add(iter->dev, dev,
8159 &iter->dev->adj_list.lower);
8160 netdev_adjacent_sysfs_add(dev, iter->dev,
8161 &dev->adj_list.upper);
8162 }
8163
8164 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 8165 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
8166 continue;
8167 netdev_adjacent_sysfs_add(iter->dev, dev,
8168 &iter->dev->adj_list.upper);
8169 netdev_adjacent_sysfs_add(dev, iter->dev,
8170 &dev->adj_list.lower);
8171 }
8172}
8173
2ce1ee17 8174static void netdev_adjacent_del_links(struct net_device *dev)
4c75431a
AF
8175{
8176 struct netdev_adjacent *iter;
8177
8178 struct net *net = dev_net(dev);
8179
8180 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 8181 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
8182 continue;
8183 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8184 &iter->dev->adj_list.lower);
8185 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8186 &dev->adj_list.upper);
8187 }
8188
8189 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 8190 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
8191 continue;
8192 netdev_adjacent_sysfs_del(iter->dev, dev->name,
8193 &iter->dev->adj_list.upper);
8194 netdev_adjacent_sysfs_del(dev, iter->dev->name,
8195 &dev->adj_list.lower);
8196 }
8197}
8198
5bb025fa 8199void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
402dae96 8200{
5bb025fa 8201 struct netdev_adjacent *iter;
402dae96 8202
4c75431a
AF
8203 struct net *net = dev_net(dev);
8204
5bb025fa 8205 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 8206 if (!net_eq(net, dev_net(iter->dev)))
4c75431a 8207 continue;
5bb025fa
VF
8208 netdev_adjacent_sysfs_del(iter->dev, oldname,
8209 &iter->dev->adj_list.lower);
8210 netdev_adjacent_sysfs_add(iter->dev, dev,
8211 &iter->dev->adj_list.lower);
8212 }
402dae96 8213
5bb025fa 8214 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 8215 if (!net_eq(net, dev_net(iter->dev)))
4c75431a 8216 continue;
5bb025fa
VF
8217 netdev_adjacent_sysfs_del(iter->dev, oldname,
8218 &iter->dev->adj_list.upper);
8219 netdev_adjacent_sysfs_add(iter->dev, dev,
8220 &iter->dev->adj_list.upper);
8221 }
402dae96 8222}
402dae96
VF
8223
8224void *netdev_lower_dev_get_private(struct net_device *dev,
8225 struct net_device *lower_dev)
8226{
8227 struct netdev_adjacent *lower;
8228
8229 if (!lower_dev)
8230 return NULL;
6ea29da1 8231 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
402dae96
VF
8232 if (!lower)
8233 return NULL;
8234
8235 return lower->private;
8236}
8237EXPORT_SYMBOL(netdev_lower_dev_get_private);
8238
4085ebe8 8239
04d48266 8240/**
c1639be9 8241 * netdev_lower_state_changed - Dispatch event about lower device state change
04d48266
JP
8242 * @lower_dev: device
8243 * @lower_state_info: state to dispatch
8244 *
8245 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
8246 * The caller must hold the RTNL lock.
8247 */
8248void netdev_lower_state_changed(struct net_device *lower_dev,
8249 void *lower_state_info)
8250{
51d0c047
DA
8251 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
8252 .info.dev = lower_dev,
8253 };
04d48266
JP
8254
8255 ASSERT_RTNL();
8256 changelowerstate_info.lower_state_info = lower_state_info;
51d0c047 8257 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
04d48266
JP
8258 &changelowerstate_info.info);
8259}
8260EXPORT_SYMBOL(netdev_lower_state_changed);
8261
b6c40d68
PM
8262static void dev_change_rx_flags(struct net_device *dev, int flags)
8263{
d314774c
SH
8264 const struct net_device_ops *ops = dev->netdev_ops;
8265
d2615bf4 8266 if (ops->ndo_change_rx_flags)
d314774c 8267 ops->ndo_change_rx_flags(dev, flags);
b6c40d68
PM
8268}
8269
991fb3f7 8270static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
1da177e4 8271{
b536db93 8272 unsigned int old_flags = dev->flags;
d04a48b0
EB
8273 kuid_t uid;
8274 kgid_t gid;
1da177e4 8275
24023451
PM
8276 ASSERT_RTNL();
8277
dad9b335
WC
8278 dev->flags |= IFF_PROMISC;
8279 dev->promiscuity += inc;
8280 if (dev->promiscuity == 0) {
8281 /*
8282 * Avoid overflow.
8283 * If inc causes overflow, untouch promisc and return error.
8284 */
8285 if (inc < 0)
8286 dev->flags &= ~IFF_PROMISC;
8287 else {
8288 dev->promiscuity -= inc;
7b6cd1ce
JP
8289 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
8290 dev->name);
dad9b335
WC
8291 return -EOVERFLOW;
8292 }
8293 }
52609c0b 8294 if (dev->flags != old_flags) {
7b6cd1ce
JP
8295 pr_info("device %s %s promiscuous mode\n",
8296 dev->name,
8297 dev->flags & IFF_PROMISC ? "entered" : "left");
8192b0c4
DH
8298 if (audit_enabled) {
8299 current_uid_gid(&uid, &gid);
cdfb6b34
RGB
8300 audit_log(audit_context(), GFP_ATOMIC,
8301 AUDIT_ANOM_PROMISCUOUS,
8302 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
8303 dev->name, (dev->flags & IFF_PROMISC),
8304 (old_flags & IFF_PROMISC),
8305 from_kuid(&init_user_ns, audit_get_loginuid(current)),
8306 from_kuid(&init_user_ns, uid),
8307 from_kgid(&init_user_ns, gid),
8308 audit_get_sessionid(current));
8192b0c4 8309 }
24023451 8310
b6c40d68 8311 dev_change_rx_flags(dev, IFF_PROMISC);
1da177e4 8312 }
991fb3f7
ND
8313 if (notify)
8314 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
dad9b335 8315 return 0;
1da177e4
LT
8316}
8317
4417da66
PM
8318/**
8319 * dev_set_promiscuity - update promiscuity count on a device
8320 * @dev: device
8321 * @inc: modifier
8322 *
8323 * Add or remove promiscuity from a device. While the count in the device
8324 * remains above zero the interface remains promiscuous. Once it hits zero
8325 * the device reverts back to normal filtering operation. A negative inc
8326 * value is used to drop promiscuity on the device.
dad9b335 8327 * Return 0 if successful or a negative errno code on error.
4417da66 8328 */
dad9b335 8329int dev_set_promiscuity(struct net_device *dev, int inc)
4417da66 8330{
b536db93 8331 unsigned int old_flags = dev->flags;
dad9b335 8332 int err;
4417da66 8333
991fb3f7 8334 err = __dev_set_promiscuity(dev, inc, true);
4b5a698e 8335 if (err < 0)
dad9b335 8336 return err;
4417da66
PM
8337 if (dev->flags != old_flags)
8338 dev_set_rx_mode(dev);
dad9b335 8339 return err;
4417da66 8340}
d1b19dff 8341EXPORT_SYMBOL(dev_set_promiscuity);
4417da66 8342
991fb3f7 8343static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
1da177e4 8344{
991fb3f7 8345 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
1da177e4 8346
24023451
PM
8347 ASSERT_RTNL();
8348
1da177e4 8349 dev->flags |= IFF_ALLMULTI;
dad9b335
WC
8350 dev->allmulti += inc;
8351 if (dev->allmulti == 0) {
8352 /*
8353 * Avoid overflow.
8354 * If inc causes overflow, untouch allmulti and return error.
8355 */
8356 if (inc < 0)
8357 dev->flags &= ~IFF_ALLMULTI;
8358 else {
8359 dev->allmulti -= inc;
7b6cd1ce
JP
8360 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8361 dev->name);
dad9b335
WC
8362 return -EOVERFLOW;
8363 }
8364 }
24023451 8365 if (dev->flags ^ old_flags) {
b6c40d68 8366 dev_change_rx_flags(dev, IFF_ALLMULTI);
4417da66 8367 dev_set_rx_mode(dev);
991fb3f7
ND
8368 if (notify)
8369 __dev_notify_flags(dev, old_flags,
8370 dev->gflags ^ old_gflags);
24023451 8371 }
dad9b335 8372 return 0;
4417da66 8373}
991fb3f7
ND
8374
8375/**
8376 * dev_set_allmulti - update allmulti count on a device
8377 * @dev: device
8378 * @inc: modifier
8379 *
8380 * Add or remove reception of all multicast frames to a device. While the
8381 * count in the device remains above zero the interface remains listening
8382 * to all interfaces. Once it hits zero the device reverts back to normal
8383 * filtering operation. A negative @inc value is used to drop the counter
8384 * when releasing a resource needing all multicasts.
8385 * Return 0 if successful or a negative errno code on error.
8386 */
8387
8388int dev_set_allmulti(struct net_device *dev, int inc)
8389{
8390 return __dev_set_allmulti(dev, inc, true);
8391}
d1b19dff 8392EXPORT_SYMBOL(dev_set_allmulti);
4417da66
PM
8393
8394/*
8395 * Upload unicast and multicast address lists to device and
8396 * configure RX filtering. When the device doesn't support unicast
53ccaae1 8397 * filtering it is put in promiscuous mode while unicast addresses
4417da66
PM
8398 * are present.
8399 */
8400void __dev_set_rx_mode(struct net_device *dev)
8401{
d314774c
SH
8402 const struct net_device_ops *ops = dev->netdev_ops;
8403
4417da66
PM
8404 /* dev_open will call this function so the list will stay sane. */
8405 if (!(dev->flags&IFF_UP))
8406 return;
8407
8408 if (!netif_device_present(dev))
40b77c94 8409 return;
4417da66 8410
01789349 8411 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
4417da66
PM
8412 /* Unicast addresses changes may only happen under the rtnl,
8413 * therefore calling __dev_set_promiscuity here is safe.
8414 */
32e7bfc4 8415 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
991fb3f7 8416 __dev_set_promiscuity(dev, 1, false);
2d348d1f 8417 dev->uc_promisc = true;
32e7bfc4 8418 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
991fb3f7 8419 __dev_set_promiscuity(dev, -1, false);
2d348d1f 8420 dev->uc_promisc = false;
4417da66 8421 }
4417da66 8422 }
01789349
JP
8423
8424 if (ops->ndo_set_rx_mode)
8425 ops->ndo_set_rx_mode(dev);
4417da66
PM
8426}
8427
8428void dev_set_rx_mode(struct net_device *dev)
8429{
b9e40857 8430 netif_addr_lock_bh(dev);
4417da66 8431 __dev_set_rx_mode(dev);
b9e40857 8432 netif_addr_unlock_bh(dev);
1da177e4
LT
8433}
8434
f0db275a
SH
8435/**
8436 * dev_get_flags - get flags reported to userspace
8437 * @dev: device
8438 *
8439 * Get the combination of flag bits exported through APIs to userspace.
8440 */
95c96174 8441unsigned int dev_get_flags(const struct net_device *dev)
1da177e4 8442{
95c96174 8443 unsigned int flags;
1da177e4
LT
8444
8445 flags = (dev->flags & ~(IFF_PROMISC |
8446 IFF_ALLMULTI |
b00055aa
SR
8447 IFF_RUNNING |
8448 IFF_LOWER_UP |
8449 IFF_DORMANT)) |
1da177e4
LT
8450 (dev->gflags & (IFF_PROMISC |
8451 IFF_ALLMULTI));
8452
b00055aa
SR
8453 if (netif_running(dev)) {
8454 if (netif_oper_up(dev))
8455 flags |= IFF_RUNNING;
8456 if (netif_carrier_ok(dev))
8457 flags |= IFF_LOWER_UP;
8458 if (netif_dormant(dev))
8459 flags |= IFF_DORMANT;
8460 }
1da177e4
LT
8461
8462 return flags;
8463}
d1b19dff 8464EXPORT_SYMBOL(dev_get_flags);
1da177e4 8465
6d040321
PM
8466int __dev_change_flags(struct net_device *dev, unsigned int flags,
8467 struct netlink_ext_ack *extack)
1da177e4 8468{
b536db93 8469 unsigned int old_flags = dev->flags;
bd380811 8470 int ret;
1da177e4 8471
24023451
PM
8472 ASSERT_RTNL();
8473
1da177e4
LT
8474 /*
8475 * Set the flags on our device.
8476 */
8477
8478 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8479 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8480 IFF_AUTOMEDIA)) |
8481 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8482 IFF_ALLMULTI));
8483
8484 /*
8485 * Load in the correct multicast list now the flags have changed.
8486 */
8487
b6c40d68
PM
8488 if ((old_flags ^ flags) & IFF_MULTICAST)
8489 dev_change_rx_flags(dev, IFF_MULTICAST);
24023451 8490
4417da66 8491 dev_set_rx_mode(dev);
1da177e4
LT
8492
8493 /*
8494 * Have we downed the interface. We handle IFF_UP ourselves
8495 * according to user attempts to set it, rather than blindly
8496 * setting it.
8497 */
8498
8499 ret = 0;
7051b88a 8500 if ((old_flags ^ flags) & IFF_UP) {
8501 if (old_flags & IFF_UP)
8502 __dev_close(dev);
8503 else
40c900aa 8504 ret = __dev_open(dev, extack);
7051b88a 8505 }
1da177e4 8506
1da177e4 8507 if ((flags ^ dev->gflags) & IFF_PROMISC) {
d1b19dff 8508 int inc = (flags & IFF_PROMISC) ? 1 : -1;
991fb3f7 8509 unsigned int old_flags = dev->flags;
d1b19dff 8510
1da177e4 8511 dev->gflags ^= IFF_PROMISC;
991fb3f7
ND
8512
8513 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8514 if (dev->flags != old_flags)
8515 dev_set_rx_mode(dev);
1da177e4
LT
8516 }
8517
8518 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
eb13da1a 8519 * is important. Some (broken) drivers set IFF_PROMISC, when
8520 * IFF_ALLMULTI is requested not asking us and not reporting.
1da177e4
LT
8521 */
8522 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
d1b19dff
ED
8523 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8524
1da177e4 8525 dev->gflags ^= IFF_ALLMULTI;
991fb3f7 8526 __dev_set_allmulti(dev, inc, false);
1da177e4
LT
8527 }
8528
bd380811
PM
8529 return ret;
8530}
8531
a528c219
ND
8532void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8533 unsigned int gchanges)
bd380811
PM
8534{
8535 unsigned int changes = dev->flags ^ old_flags;
8536
a528c219 8537 if (gchanges)
7f294054 8538 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
a528c219 8539
bd380811
PM
8540 if (changes & IFF_UP) {
8541 if (dev->flags & IFF_UP)
8542 call_netdevice_notifiers(NETDEV_UP, dev);
8543 else
8544 call_netdevice_notifiers(NETDEV_DOWN, dev);
8545 }
8546
8547 if (dev->flags & IFF_UP &&
be9efd36 8548 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
51d0c047
DA
8549 struct netdev_notifier_change_info change_info = {
8550 .info = {
8551 .dev = dev,
8552 },
8553 .flags_changed = changes,
8554 };
be9efd36 8555
51d0c047 8556 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
be9efd36 8557 }
bd380811
PM
8558}
8559
8560/**
8561 * dev_change_flags - change device settings
8562 * @dev: device
8563 * @flags: device state flags
567c5e13 8564 * @extack: netlink extended ack
bd380811
PM
8565 *
8566 * Change settings on device based state flags. The flags are
8567 * in the userspace exported format.
8568 */
567c5e13
PM
8569int dev_change_flags(struct net_device *dev, unsigned int flags,
8570 struct netlink_ext_ack *extack)
bd380811 8571{
b536db93 8572 int ret;
991fb3f7 8573 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
bd380811 8574
6d040321 8575 ret = __dev_change_flags(dev, flags, extack);
bd380811
PM
8576 if (ret < 0)
8577 return ret;
8578
991fb3f7 8579 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
a528c219 8580 __dev_notify_flags(dev, old_flags, changes);
1da177e4
LT
8581 return ret;
8582}
d1b19dff 8583EXPORT_SYMBOL(dev_change_flags);
1da177e4 8584
f51048c3 8585int __dev_set_mtu(struct net_device *dev, int new_mtu)
2315dc91
VF
8586{
8587 const struct net_device_ops *ops = dev->netdev_ops;
8588
8589 if (ops->ndo_change_mtu)
8590 return ops->ndo_change_mtu(dev, new_mtu);
8591
501a90c9
ED
8592 /* Pairs with all the lockless reads of dev->mtu in the stack */
8593 WRITE_ONCE(dev->mtu, new_mtu);
2315dc91
VF
8594 return 0;
8595}
f51048c3 8596EXPORT_SYMBOL(__dev_set_mtu);
2315dc91 8597
d836f5c6
ED
8598int dev_validate_mtu(struct net_device *dev, int new_mtu,
8599 struct netlink_ext_ack *extack)
8600{
8601 /* MTU must be positive, and in range */
8602 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8603 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8604 return -EINVAL;
8605 }
8606
8607 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8608 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8609 return -EINVAL;
8610 }
8611 return 0;
8612}
8613
f0db275a 8614/**
7a4c53be 8615 * dev_set_mtu_ext - Change maximum transfer unit
f0db275a
SH
8616 * @dev: device
8617 * @new_mtu: new transfer unit
7a4c53be 8618 * @extack: netlink extended ack
f0db275a
SH
8619 *
8620 * Change the maximum transfer size of the network device.
8621 */
7a4c53be
SH
8622int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8623 struct netlink_ext_ack *extack)
1da177e4 8624{
2315dc91 8625 int err, orig_mtu;
1da177e4
LT
8626
8627 if (new_mtu == dev->mtu)
8628 return 0;
8629
d836f5c6
ED
8630 err = dev_validate_mtu(dev, new_mtu, extack);
8631 if (err)
8632 return err;
1da177e4
LT
8633
8634 if (!netif_device_present(dev))
8635 return -ENODEV;
8636
1d486bfb
VF
8637 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8638 err = notifier_to_errno(err);
8639 if (err)
8640 return err;
d314774c 8641
2315dc91
VF
8642 orig_mtu = dev->mtu;
8643 err = __dev_set_mtu(dev, new_mtu);
d314774c 8644
2315dc91 8645 if (!err) {
af7d6cce
SD
8646 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8647 orig_mtu);
2315dc91
VF
8648 err = notifier_to_errno(err);
8649 if (err) {
8650 /* setting mtu back and notifying everyone again,
8651 * so that they have a chance to revert changes.
8652 */
8653 __dev_set_mtu(dev, orig_mtu);
af7d6cce
SD
8654 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8655 new_mtu);
2315dc91
VF
8656 }
8657 }
1da177e4
LT
8658 return err;
8659}
7a4c53be
SH
8660
8661int dev_set_mtu(struct net_device *dev, int new_mtu)
8662{
8663 struct netlink_ext_ack extack;
8664 int err;
8665
a6bcfc89 8666 memset(&extack, 0, sizeof(extack));
7a4c53be 8667 err = dev_set_mtu_ext(dev, new_mtu, &extack);
a6bcfc89 8668 if (err && extack._msg)
7a4c53be
SH
8669 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8670 return err;
8671}
d1b19dff 8672EXPORT_SYMBOL(dev_set_mtu);
1da177e4 8673
6a643ddb
CW
8674/**
8675 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8676 * @dev: device
8677 * @new_len: new tx queue length
8678 */
8679int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8680{
8681 unsigned int orig_len = dev->tx_queue_len;
8682 int res;
8683
8684 if (new_len != (unsigned int)new_len)
8685 return -ERANGE;
8686
8687 if (new_len != orig_len) {
8688 dev->tx_queue_len = new_len;
8689 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8690 res = notifier_to_errno(res);
7effaf06
TT
8691 if (res)
8692 goto err_rollback;
8693 res = dev_qdisc_change_tx_queue_len(dev);
8694 if (res)
8695 goto err_rollback;
6a643ddb
CW
8696 }
8697
8698 return 0;
7effaf06
TT
8699
8700err_rollback:
8701 netdev_err(dev, "refused to change device tx_queue_len\n");
8702 dev->tx_queue_len = orig_len;
8703 return res;
6a643ddb
CW
8704}
8705
cbda10fa
VD
8706/**
8707 * dev_set_group - Change group this device belongs to
8708 * @dev: device
8709 * @new_group: group this device should belong to
8710 */
8711void dev_set_group(struct net_device *dev, int new_group)
8712{
8713 dev->group = new_group;
8714}
8715EXPORT_SYMBOL(dev_set_group);
8716
d59cdf94
PM
8717/**
8718 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8719 * @dev: device
8720 * @addr: new address
8721 * @extack: netlink extended ack
8722 */
8723int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8724 struct netlink_ext_ack *extack)
8725{
8726 struct netdev_notifier_pre_changeaddr_info info = {
8727 .info.dev = dev,
8728 .info.extack = extack,
8729 .dev_addr = addr,
8730 };
8731 int rc;
8732
8733 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8734 return notifier_to_errno(rc);
8735}
8736EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8737
f0db275a
SH
8738/**
8739 * dev_set_mac_address - Change Media Access Control Address
8740 * @dev: device
8741 * @sa: new address
3a37a963 8742 * @extack: netlink extended ack
f0db275a
SH
8743 *
8744 * Change the hardware (MAC) address of the device
8745 */
3a37a963
PM
8746int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8747 struct netlink_ext_ack *extack)
1da177e4 8748{
d314774c 8749 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
8750 int err;
8751
d314774c 8752 if (!ops->ndo_set_mac_address)
1da177e4
LT
8753 return -EOPNOTSUPP;
8754 if (sa->sa_family != dev->type)
8755 return -EINVAL;
8756 if (!netif_device_present(dev))
8757 return -ENODEV;
d59cdf94
PM
8758 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8759 if (err)
8760 return err;
d314774c 8761 err = ops->ndo_set_mac_address(dev, sa);
f6521516
JP
8762 if (err)
8763 return err;
fbdeca2d 8764 dev->addr_assign_type = NET_ADDR_SET;
f6521516 8765 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
7bf23575 8766 add_device_randomness(dev->dev_addr, dev->addr_len);
f6521516 8767 return 0;
1da177e4 8768}
d1b19dff 8769EXPORT_SYMBOL(dev_set_mac_address);
1da177e4 8770
4bf84c35
JP
8771/**
8772 * dev_change_carrier - Change device carrier
8773 * @dev: device
691b3b7e 8774 * @new_carrier: new value
4bf84c35
JP
8775 *
8776 * Change device carrier
8777 */
8778int dev_change_carrier(struct net_device *dev, bool new_carrier)
8779{
8780 const struct net_device_ops *ops = dev->netdev_ops;
8781
8782 if (!ops->ndo_change_carrier)
8783 return -EOPNOTSUPP;
8784 if (!netif_device_present(dev))
8785 return -ENODEV;
8786 return ops->ndo_change_carrier(dev, new_carrier);
8787}
8788EXPORT_SYMBOL(dev_change_carrier);
8789
66b52b0d
JP
8790/**
8791 * dev_get_phys_port_id - Get device physical port ID
8792 * @dev: device
8793 * @ppid: port ID
8794 *
8795 * Get device physical port ID
8796 */
8797int dev_get_phys_port_id(struct net_device *dev,
02637fce 8798 struct netdev_phys_item_id *ppid)
66b52b0d
JP
8799{
8800 const struct net_device_ops *ops = dev->netdev_ops;
8801
8802 if (!ops->ndo_get_phys_port_id)
8803 return -EOPNOTSUPP;
8804 return ops->ndo_get_phys_port_id(dev, ppid);
8805}
8806EXPORT_SYMBOL(dev_get_phys_port_id);
8807
db24a904
DA
8808/**
8809 * dev_get_phys_port_name - Get device physical port name
8810 * @dev: device
8811 * @name: port name
ed49e650 8812 * @len: limit of bytes to copy to name
db24a904
DA
8813 *
8814 * Get device physical port name
8815 */
8816int dev_get_phys_port_name(struct net_device *dev,
8817 char *name, size_t len)
8818{
8819 const struct net_device_ops *ops = dev->netdev_ops;
af3836df 8820 int err;
db24a904 8821
af3836df
JP
8822 if (ops->ndo_get_phys_port_name) {
8823 err = ops->ndo_get_phys_port_name(dev, name, len);
8824 if (err != -EOPNOTSUPP)
8825 return err;
8826 }
8827 return devlink_compat_phys_port_name_get(dev, name, len);
db24a904
DA
8828}
8829EXPORT_SYMBOL(dev_get_phys_port_name);
8830
d6abc596
FF
8831/**
8832 * dev_get_port_parent_id - Get the device's port parent identifier
8833 * @dev: network device
8834 * @ppid: pointer to a storage for the port's parent identifier
8835 * @recurse: allow/disallow recursion to lower devices
8836 *
8837 * Get the devices's port parent identifier
8838 */
8839int dev_get_port_parent_id(struct net_device *dev,
8840 struct netdev_phys_item_id *ppid,
8841 bool recurse)
8842{
8843 const struct net_device_ops *ops = dev->netdev_ops;
8844 struct netdev_phys_item_id first = { };
8845 struct net_device *lower_dev;
8846 struct list_head *iter;
7e1146e8
JP
8847 int err;
8848
8849 if (ops->ndo_get_port_parent_id) {
8850 err = ops->ndo_get_port_parent_id(dev, ppid);
8851 if (err != -EOPNOTSUPP)
8852 return err;
8853 }
d6abc596 8854
7e1146e8
JP
8855 err = devlink_compat_switch_id_get(dev, ppid);
8856 if (!err || err != -EOPNOTSUPP)
8857 return err;
d6abc596
FF
8858
8859 if (!recurse)
7e1146e8 8860 return -EOPNOTSUPP;
d6abc596
FF
8861
8862 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8863 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8864 if (err)
8865 break;
8866 if (!first.id_len)
8867 first = *ppid;
8868 else if (memcmp(&first, ppid, sizeof(*ppid)))
e1b9efe6 8869 return -EOPNOTSUPP;
d6abc596
FF
8870 }
8871
8872 return err;
8873}
8874EXPORT_SYMBOL(dev_get_port_parent_id);
8875
8876/**
8877 * netdev_port_same_parent_id - Indicate if two network devices have
8878 * the same port parent identifier
8879 * @a: first network device
8880 * @b: second network device
8881 */
8882bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8883{
8884 struct netdev_phys_item_id a_id = { };
8885 struct netdev_phys_item_id b_id = { };
8886
8887 if (dev_get_port_parent_id(a, &a_id, true) ||
8888 dev_get_port_parent_id(b, &b_id, true))
8889 return false;
8890
8891 return netdev_phys_item_id_same(&a_id, &b_id);
8892}
8893EXPORT_SYMBOL(netdev_port_same_parent_id);
8894
d746d707
AK
8895/**
8896 * dev_change_proto_down - update protocol port state information
8897 * @dev: device
8898 * @proto_down: new value
8899 *
8900 * This info can be used by switch drivers to set the phys state of the
8901 * port.
8902 */
8903int dev_change_proto_down(struct net_device *dev, bool proto_down)
8904{
8905 const struct net_device_ops *ops = dev->netdev_ops;
8906
8907 if (!ops->ndo_change_proto_down)
8908 return -EOPNOTSUPP;
8909 if (!netif_device_present(dev))
8910 return -ENODEV;
8911 return ops->ndo_change_proto_down(dev, proto_down);
8912}
8913EXPORT_SYMBOL(dev_change_proto_down);
8914
b5899679
AR
8915/**
8916 * dev_change_proto_down_generic - generic implementation for
8917 * ndo_change_proto_down that sets carrier according to
8918 * proto_down.
8919 *
8920 * @dev: device
8921 * @proto_down: new value
8922 */
8923int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8924{
8925 if (proto_down)
8926 netif_carrier_off(dev);
8927 else
8928 netif_carrier_on(dev);
8929 dev->proto_down = proto_down;
8930 return 0;
8931}
8932EXPORT_SYMBOL(dev_change_proto_down_generic);
8933
829eb208
RP
8934/**
8935 * dev_change_proto_down_reason - proto down reason
8936 *
8937 * @dev: device
8938 * @mask: proto down mask
8939 * @value: proto down value
8940 */
8941void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
8942 u32 value)
8943{
8944 int b;
8945
8946 if (!mask) {
8947 dev->proto_down_reason = value;
8948 } else {
8949 for_each_set_bit(b, &mask, 32) {
8950 if (value & (1 << b))
8951 dev->proto_down_reason |= BIT(b);
8952 else
8953 dev->proto_down_reason &= ~BIT(b);
8954 }
8955 }
8956}
8957EXPORT_SYMBOL(dev_change_proto_down_reason);
8958
aa8d3a71
AN
8959struct bpf_xdp_link {
8960 struct bpf_link link;
8961 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
8962 int flags;
8963};
8964
c8a36f19 8965static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
d67b9cd2 8966{
7f0a8382
AN
8967 if (flags & XDP_FLAGS_HW_MODE)
8968 return XDP_MODE_HW;
8969 if (flags & XDP_FLAGS_DRV_MODE)
8970 return XDP_MODE_DRV;
c8a36f19
AN
8971 if (flags & XDP_FLAGS_SKB_MODE)
8972 return XDP_MODE_SKB;
8973 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
7f0a8382 8974}
d67b9cd2 8975
7f0a8382
AN
8976static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
8977{
8978 switch (mode) {
8979 case XDP_MODE_SKB:
8980 return generic_xdp_install;
8981 case XDP_MODE_DRV:
8982 case XDP_MODE_HW:
8983 return dev->netdev_ops->ndo_bpf;
8984 default:
8985 return NULL;
5d867245 8986 }
7f0a8382 8987}
118b4aa2 8988
aa8d3a71
AN
8989static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
8990 enum bpf_xdp_mode mode)
8991{
8992 return dev->xdp_state[mode].link;
8993}
8994
7f0a8382
AN
8995static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
8996 enum bpf_xdp_mode mode)
8997{
aa8d3a71
AN
8998 struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
8999
9000 if (link)
9001 return link->link.prog;
7f0a8382
AN
9002 return dev->xdp_state[mode].prog;
9003}
9004
998f1729
THJ
9005static u8 dev_xdp_prog_count(struct net_device *dev)
9006{
9007 u8 count = 0;
9008 int i;
9009
9010 for (i = 0; i < __MAX_XDP_MODE; i++)
9011 if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
9012 count++;
9013 return count;
9014}
9015
7f0a8382
AN
9016u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
9017{
9018 struct bpf_prog *prog = dev_xdp_prog(dev, mode);
118b4aa2 9019
7f0a8382
AN
9020 return prog ? prog->aux->id : 0;
9021}
58038695 9022
aa8d3a71
AN
9023static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
9024 struct bpf_xdp_link *link)
9025{
9026 dev->xdp_state[mode].link = link;
9027 dev->xdp_state[mode].prog = NULL;
d67b9cd2
DB
9028}
9029
7f0a8382
AN
9030static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
9031 struct bpf_prog *prog)
9032{
aa8d3a71 9033 dev->xdp_state[mode].link = NULL;
7f0a8382 9034 dev->xdp_state[mode].prog = prog;
d67b9cd2
DB
9035}
9036
7f0a8382
AN
9037static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
9038 bpf_op_t bpf_op, struct netlink_ext_ack *extack,
9039 u32 flags, struct bpf_prog *prog)
d67b9cd2 9040{
f4e63525 9041 struct netdev_bpf xdp;
7e6897f9
BT
9042 int err;
9043
d67b9cd2 9044 memset(&xdp, 0, sizeof(xdp));
7f0a8382 9045 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
d67b9cd2 9046 xdp.extack = extack;
32d60277 9047 xdp.flags = flags;
d67b9cd2
DB
9048 xdp.prog = prog;
9049
7f0a8382
AN
9050 /* Drivers assume refcnt is already incremented (i.e, prog pointer is
9051 * "moved" into driver), so they don't increment it on their own, but
9052 * they do decrement refcnt when program is detached or replaced.
9053 * Given net_device also owns link/prog, we need to bump refcnt here
9054 * to prevent drivers from underflowing it.
9055 */
9056 if (prog)
9057 bpf_prog_inc(prog);
7e6897f9 9058 err = bpf_op(dev, &xdp);
7f0a8382
AN
9059 if (err) {
9060 if (prog)
9061 bpf_prog_put(prog);
9062 return err;
9063 }
7e6897f9 9064
7f0a8382
AN
9065 if (mode != XDP_MODE_HW)
9066 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
7e6897f9 9067
7f0a8382 9068 return 0;
d67b9cd2
DB
9069}
9070
bd0b2e7f
JK
9071static void dev_xdp_uninstall(struct net_device *dev)
9072{
aa8d3a71 9073 struct bpf_xdp_link *link;
7f0a8382
AN
9074 struct bpf_prog *prog;
9075 enum bpf_xdp_mode mode;
9076 bpf_op_t bpf_op;
bd0b2e7f 9077
7f0a8382 9078 ASSERT_RTNL();
bd0b2e7f 9079
7f0a8382
AN
9080 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
9081 prog = dev_xdp_prog(dev, mode);
9082 if (!prog)
9083 continue;
bd0b2e7f 9084
7f0a8382
AN
9085 bpf_op = dev_xdp_bpf_op(dev, mode);
9086 if (!bpf_op)
9087 continue;
bd0b2e7f 9088
7f0a8382
AN
9089 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9090
aa8d3a71
AN
9091 /* auto-detach link from net device */
9092 link = dev_xdp_link(dev, mode);
9093 if (link)
9094 link->dev = NULL;
9095 else
9096 bpf_prog_put(prog);
9097
9098 dev_xdp_set_link(dev, mode, NULL);
7f0a8382 9099 }
bd0b2e7f
JK
9100}
9101
d4baa936 9102static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
aa8d3a71
AN
9103 struct bpf_xdp_link *link, struct bpf_prog *new_prog,
9104 struct bpf_prog *old_prog, u32 flags)
a7862b45 9105{
998f1729 9106 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
d4baa936
AN
9107 struct bpf_prog *cur_prog;
9108 enum bpf_xdp_mode mode;
7f0a8382 9109 bpf_op_t bpf_op;
a7862b45
BB
9110 int err;
9111
85de8576
DB
9112 ASSERT_RTNL();
9113
aa8d3a71
AN
9114 /* either link or prog attachment, never both */
9115 if (link && (new_prog || old_prog))
9116 return -EINVAL;
9117 /* link supports only XDP mode flags */
9118 if (link && (flags & ~XDP_FLAGS_MODES)) {
9119 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
9120 return -EINVAL;
9121 }
998f1729
THJ
9122 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */
9123 if (num_modes > 1) {
d4baa936
AN
9124 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
9125 return -EINVAL;
9126 }
998f1729
THJ
9127 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */
9128 if (!num_modes && dev_xdp_prog_count(dev) > 1) {
9129 NL_SET_ERR_MSG(extack,
9130 "More than one program loaded, unset mode is ambiguous");
9131 return -EINVAL;
9132 }
d4baa936
AN
9133 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
9134 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
9135 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
9136 return -EINVAL;
01dde20c 9137 }
a25717d2 9138
c8a36f19 9139 mode = dev_xdp_mode(dev, flags);
aa8d3a71
AN
9140 /* can't replace attached link */
9141 if (dev_xdp_link(dev, mode)) {
9142 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
9143 return -EBUSY;
01dde20c 9144 }
c14a9f63 9145
d4baa936 9146 cur_prog = dev_xdp_prog(dev, mode);
aa8d3a71
AN
9147 /* can't replace attached prog with link */
9148 if (link && cur_prog) {
9149 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
9150 return -EBUSY;
9151 }
d4baa936
AN
9152 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
9153 NL_SET_ERR_MSG(extack, "Active program does not match expected");
9154 return -EEXIST;
92234c8f 9155 }
c14a9f63 9156
aa8d3a71
AN
9157 /* put effective new program into new_prog */
9158 if (link)
9159 new_prog = link->link.prog;
85de8576 9160
d4baa936
AN
9161 if (new_prog) {
9162 bool offload = mode == XDP_MODE_HW;
7f0a8382
AN
9163 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
9164 ? XDP_MODE_DRV : XDP_MODE_SKB;
441a3303 9165
068d9d1e
AN
9166 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
9167 NL_SET_ERR_MSG(extack, "XDP program already attached");
9168 return -EBUSY;
9169 }
d4baa936 9170 if (!offload && dev_xdp_prog(dev, other_mode)) {
7f0a8382 9171 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
d67b9cd2 9172 return -EEXIST;
01dde20c 9173 }
d4baa936 9174 if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
7f0a8382 9175 NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
441a3303
JK
9176 return -EINVAL;
9177 }
d4baa936 9178 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
fbee97fe 9179 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
fbee97fe
DA
9180 return -EINVAL;
9181 }
d4baa936
AN
9182 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
9183 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
92164774
LB
9184 return -EINVAL;
9185 }
d4baa936 9186 }
92164774 9187
d4baa936
AN
9188 /* don't call drivers if the effective program didn't change */
9189 if (new_prog != cur_prog) {
9190 bpf_op = dev_xdp_bpf_op(dev, mode);
9191 if (!bpf_op) {
9192 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
9193 return -EOPNOTSUPP;
c14a9f63 9194 }
a7862b45 9195
d4baa936
AN
9196 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
9197 if (err)
9198 return err;
7f0a8382 9199 }
d4baa936 9200
aa8d3a71
AN
9201 if (link)
9202 dev_xdp_set_link(dev, mode, link);
9203 else
9204 dev_xdp_set_prog(dev, mode, new_prog);
d4baa936
AN
9205 if (cur_prog)
9206 bpf_prog_put(cur_prog);
a7862b45 9207
7f0a8382 9208 return 0;
a7862b45 9209}
a7862b45 9210
aa8d3a71
AN
9211static int dev_xdp_attach_link(struct net_device *dev,
9212 struct netlink_ext_ack *extack,
9213 struct bpf_xdp_link *link)
9214{
9215 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
9216}
9217
9218static int dev_xdp_detach_link(struct net_device *dev,
9219 struct netlink_ext_ack *extack,
9220 struct bpf_xdp_link *link)
9221{
9222 enum bpf_xdp_mode mode;
9223 bpf_op_t bpf_op;
9224
9225 ASSERT_RTNL();
9226
c8a36f19 9227 mode = dev_xdp_mode(dev, link->flags);
aa8d3a71
AN
9228 if (dev_xdp_link(dev, mode) != link)
9229 return -EINVAL;
9230
9231 bpf_op = dev_xdp_bpf_op(dev, mode);
9232 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9233 dev_xdp_set_link(dev, mode, NULL);
9234 return 0;
9235}
9236
9237static void bpf_xdp_link_release(struct bpf_link *link)
9238{
9239 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9240
9241 rtnl_lock();
9242
9243 /* if racing with net_device's tear down, xdp_link->dev might be
9244 * already NULL, in which case link was already auto-detached
9245 */
73b11c2a 9246 if (xdp_link->dev) {
aa8d3a71 9247 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
73b11c2a
AN
9248 xdp_link->dev = NULL;
9249 }
aa8d3a71
AN
9250
9251 rtnl_unlock();
9252}
9253
73b11c2a
AN
9254static int bpf_xdp_link_detach(struct bpf_link *link)
9255{
9256 bpf_xdp_link_release(link);
9257 return 0;
9258}
9259
aa8d3a71
AN
9260static void bpf_xdp_link_dealloc(struct bpf_link *link)
9261{
9262 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9263
9264 kfree(xdp_link);
9265}
9266
c1931c97
AN
9267static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
9268 struct seq_file *seq)
9269{
9270 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9271 u32 ifindex = 0;
9272
9273 rtnl_lock();
9274 if (xdp_link->dev)
9275 ifindex = xdp_link->dev->ifindex;
9276 rtnl_unlock();
9277
9278 seq_printf(seq, "ifindex:\t%u\n", ifindex);
9279}
9280
9281static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
9282 struct bpf_link_info *info)
9283{
9284 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9285 u32 ifindex = 0;
9286
9287 rtnl_lock();
9288 if (xdp_link->dev)
9289 ifindex = xdp_link->dev->ifindex;
9290 rtnl_unlock();
9291
9292 info->xdp.ifindex = ifindex;
9293 return 0;
9294}
9295
026a4c28
AN
9296static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9297 struct bpf_prog *old_prog)
9298{
9299 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9300 enum bpf_xdp_mode mode;
9301 bpf_op_t bpf_op;
9302 int err = 0;
9303
9304 rtnl_lock();
9305
9306 /* link might have been auto-released already, so fail */
9307 if (!xdp_link->dev) {
9308 err = -ENOLINK;
9309 goto out_unlock;
9310 }
9311
9312 if (old_prog && link->prog != old_prog) {
9313 err = -EPERM;
9314 goto out_unlock;
9315 }
9316 old_prog = link->prog;
9317 if (old_prog == new_prog) {
9318 /* no-op, don't disturb drivers */
9319 bpf_prog_put(new_prog);
9320 goto out_unlock;
9321 }
9322
c8a36f19 9323 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
026a4c28
AN
9324 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9325 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9326 xdp_link->flags, new_prog);
9327 if (err)
9328 goto out_unlock;
9329
9330 old_prog = xchg(&link->prog, new_prog);
9331 bpf_prog_put(old_prog);
9332
9333out_unlock:
9334 rtnl_unlock();
9335 return err;
9336}
9337
aa8d3a71
AN
9338static const struct bpf_link_ops bpf_xdp_link_lops = {
9339 .release = bpf_xdp_link_release,
9340 .dealloc = bpf_xdp_link_dealloc,
73b11c2a 9341 .detach = bpf_xdp_link_detach,
c1931c97
AN
9342 .show_fdinfo = bpf_xdp_link_show_fdinfo,
9343 .fill_link_info = bpf_xdp_link_fill_link_info,
026a4c28 9344 .update_prog = bpf_xdp_link_update,
aa8d3a71
AN
9345};
9346
9347int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9348{
9349 struct net *net = current->nsproxy->net_ns;
9350 struct bpf_link_primer link_primer;
9351 struct bpf_xdp_link *link;
9352 struct net_device *dev;
9353 int err, fd;
9354
9355 dev = dev_get_by_index(net, attr->link_create.target_ifindex);
9356 if (!dev)
9357 return -EINVAL;
9358
9359 link = kzalloc(sizeof(*link), GFP_USER);
9360 if (!link) {
9361 err = -ENOMEM;
9362 goto out_put_dev;
9363 }
9364
9365 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9366 link->dev = dev;
9367 link->flags = attr->link_create.flags;
9368
9369 err = bpf_link_prime(&link->link, &link_primer);
9370 if (err) {
9371 kfree(link);
9372 goto out_put_dev;
9373 }
9374
9375 rtnl_lock();
9376 err = dev_xdp_attach_link(dev, NULL, link);
9377 rtnl_unlock();
9378
9379 if (err) {
9380 bpf_link_cleanup(&link_primer);
9381 goto out_put_dev;
9382 }
9383
9384 fd = bpf_link_settle(&link_primer);
9385 /* link itself doesn't hold dev's refcnt to not complicate shutdown */
9386 dev_put(dev);
9387 return fd;
9388
9389out_put_dev:
9390 dev_put(dev);
9391 return err;
9392}
9393
d4baa936
AN
9394/**
9395 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
9396 * @dev: device
9397 * @extack: netlink extended ack
9398 * @fd: new program fd or negative value to clear
9399 * @expected_fd: old program fd that userspace expects to replace or clear
9400 * @flags: xdp-related flags
9401 *
9402 * Set or clear a bpf program for a device
9403 */
9404int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
9405 int fd, int expected_fd, u32 flags)
9406{
c8a36f19 9407 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
d4baa936
AN
9408 struct bpf_prog *new_prog = NULL, *old_prog = NULL;
9409 int err;
9410
9411 ASSERT_RTNL();
9412
9413 if (fd >= 0) {
9414 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9415 mode != XDP_MODE_SKB);
9416 if (IS_ERR(new_prog))
9417 return PTR_ERR(new_prog);
9418 }
9419
9420 if (expected_fd >= 0) {
9421 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9422 mode != XDP_MODE_SKB);
9423 if (IS_ERR(old_prog)) {
9424 err = PTR_ERR(old_prog);
9425 old_prog = NULL;
9426 goto err_out;
c14a9f63 9427 }
a7862b45
BB
9428 }
9429
aa8d3a71 9430 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
a7862b45 9431
d4baa936
AN
9432err_out:
9433 if (err && new_prog)
9434 bpf_prog_put(new_prog);
9435 if (old_prog)
9436 bpf_prog_put(old_prog);
a7862b45
BB
9437 return err;
9438}
a7862b45 9439
1da177e4
LT
9440/**
9441 * dev_new_index - allocate an ifindex
c4ea43c5 9442 * @net: the applicable net namespace
1da177e4
LT
9443 *
9444 * Returns a suitable unique value for a new device interface
9445 * number. The caller must hold the rtnl semaphore or the
9446 * dev_base_lock to be sure it remains unique.
9447 */
881d966b 9448static int dev_new_index(struct net *net)
1da177e4 9449{
aa79e66e 9450 int ifindex = net->ifindex;
f4563a75 9451
1da177e4
LT
9452 for (;;) {
9453 if (++ifindex <= 0)
9454 ifindex = 1;
881d966b 9455 if (!__dev_get_by_index(net, ifindex))
aa79e66e 9456 return net->ifindex = ifindex;
1da177e4
LT
9457 }
9458}
9459
1da177e4 9460/* Delayed registration/unregisteration */
3b5b34fd 9461static LIST_HEAD(net_todo_list);
200b916f 9462DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
1da177e4 9463
6f05f629 9464static void net_set_todo(struct net_device *dev)
1da177e4 9465{
1da177e4 9466 list_add_tail(&dev->todo_list, &net_todo_list);
50624c93 9467 dev_net(dev)->dev_unreg_count++;
1da177e4
LT
9468}
9469
fd867d51
JW
9470static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
9471 struct net_device *upper, netdev_features_t features)
9472{
9473 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9474 netdev_features_t feature;
5ba3f7d6 9475 int feature_bit;
fd867d51 9476
3b89ea9c 9477 for_each_netdev_feature(upper_disables, feature_bit) {
5ba3f7d6 9478 feature = __NETIF_F_BIT(feature_bit);
fd867d51
JW
9479 if (!(upper->wanted_features & feature)
9480 && (features & feature)) {
9481 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
9482 &feature, upper->name);
9483 features &= ~feature;
9484 }
9485 }
9486
9487 return features;
9488}
9489
9490static void netdev_sync_lower_features(struct net_device *upper,
9491 struct net_device *lower, netdev_features_t features)
9492{
9493 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
9494 netdev_features_t feature;
5ba3f7d6 9495 int feature_bit;
fd867d51 9496
3b89ea9c 9497 for_each_netdev_feature(upper_disables, feature_bit) {
5ba3f7d6 9498 feature = __NETIF_F_BIT(feature_bit);
fd867d51
JW
9499 if (!(features & feature) && (lower->features & feature)) {
9500 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
9501 &feature, lower->name);
9502 lower->wanted_features &= ~feature;
dd912306 9503 __netdev_update_features(lower);
fd867d51
JW
9504
9505 if (unlikely(lower->features & feature))
9506 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
9507 &feature, lower->name);
dd912306
CW
9508 else
9509 netdev_features_change(lower);
fd867d51
JW
9510 }
9511 }
9512}
9513
c8f44aff
MM
9514static netdev_features_t netdev_fix_features(struct net_device *dev,
9515 netdev_features_t features)
b63365a2 9516{
57422dc5
MM
9517 /* Fix illegal checksum combinations */
9518 if ((features & NETIF_F_HW_CSUM) &&
9519 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
6f404e44 9520 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
57422dc5
MM
9521 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
9522 }
9523
b63365a2 9524 /* TSO requires that SG is present as well. */
ea2d3688 9525 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
6f404e44 9526 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
ea2d3688 9527 features &= ~NETIF_F_ALL_TSO;
b63365a2
HX
9528 }
9529
ec5f0615
PS
9530 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
9531 !(features & NETIF_F_IP_CSUM)) {
9532 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
9533 features &= ~NETIF_F_TSO;
9534 features &= ~NETIF_F_TSO_ECN;
9535 }
9536
9537 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
9538 !(features & NETIF_F_IPV6_CSUM)) {
9539 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
9540 features &= ~NETIF_F_TSO6;
9541 }
9542
b1dc497b
AD
9543 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
9544 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
9545 features &= ~NETIF_F_TSO_MANGLEID;
9546
31d8b9e0
BH
9547 /* TSO ECN requires that TSO is present as well. */
9548 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
9549 features &= ~NETIF_F_TSO_ECN;
9550
212b573f
MM
9551 /* Software GSO depends on SG. */
9552 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
6f404e44 9553 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
212b573f
MM
9554 features &= ~NETIF_F_GSO;
9555 }
9556
802ab55a
AD
9557 /* GSO partial features require GSO partial be set */
9558 if ((features & dev->gso_partial_features) &&
9559 !(features & NETIF_F_GSO_PARTIAL)) {
9560 netdev_dbg(dev,
9561 "Dropping partially supported GSO features since no GSO partial.\n");
9562 features &= ~dev->gso_partial_features;
9563 }
9564
fb1f5f79
MC
9565 if (!(features & NETIF_F_RXCSUM)) {
9566 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
9567 * successfully merged by hardware must also have the
9568 * checksum verified by hardware. If the user does not
9569 * want to enable RXCSUM, logically, we should disable GRO_HW.
9570 */
9571 if (features & NETIF_F_GRO_HW) {
9572 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
9573 features &= ~NETIF_F_GRO_HW;
9574 }
9575 }
9576
de8d5ab2
GP
9577 /* LRO/HW-GRO features cannot be combined with RX-FCS */
9578 if (features & NETIF_F_RXFCS) {
9579 if (features & NETIF_F_LRO) {
9580 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
9581 features &= ~NETIF_F_LRO;
9582 }
9583
9584 if (features & NETIF_F_GRO_HW) {
9585 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
9586 features &= ~NETIF_F_GRO_HW;
9587 }
e6c6a929
GP
9588 }
9589
25537d71
TT
9590 if (features & NETIF_F_HW_TLS_TX) {
9591 bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) ==
9592 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
9593 bool hw_csum = features & NETIF_F_HW_CSUM;
9594
9595 if (!ip_csum && !hw_csum) {
9596 netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
9597 features &= ~NETIF_F_HW_TLS_TX;
9598 }
ae0b04b2
TT
9599 }
9600
a3eb4e9d
TT
9601 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
9602 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
9603 features &= ~NETIF_F_HW_TLS_RX;
9604 }
9605
b63365a2
HX
9606 return features;
9607}
b63365a2 9608
6cb6a27c 9609int __netdev_update_features(struct net_device *dev)
5455c699 9610{
fd867d51 9611 struct net_device *upper, *lower;
c8f44aff 9612 netdev_features_t features;
fd867d51 9613 struct list_head *iter;
e7868a85 9614 int err = -1;
5455c699 9615
87267485
MM
9616 ASSERT_RTNL();
9617
5455c699
MM
9618 features = netdev_get_wanted_features(dev);
9619
9620 if (dev->netdev_ops->ndo_fix_features)
9621 features = dev->netdev_ops->ndo_fix_features(dev, features);
9622
9623 /* driver might be less strict about feature dependencies */
9624 features = netdev_fix_features(dev, features);
9625
4250b75b 9626 /* some features can't be enabled if they're off on an upper device */
fd867d51
JW
9627 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9628 features = netdev_sync_upper_features(dev, upper, features);
9629
5455c699 9630 if (dev->features == features)
e7868a85 9631 goto sync_lower;
5455c699 9632
c8f44aff
MM
9633 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9634 &dev->features, &features);
5455c699
MM
9635
9636 if (dev->netdev_ops->ndo_set_features)
9637 err = dev->netdev_ops->ndo_set_features(dev, features);
5f8dc33e
NA
9638 else
9639 err = 0;
5455c699 9640
6cb6a27c 9641 if (unlikely(err < 0)) {
5455c699 9642 netdev_err(dev,
c8f44aff
MM
9643 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9644 err, &features, &dev->features);
17b85d29
NA
9645 /* return non-0 since some features might have changed and
9646 * it's better to fire a spurious notification than miss it
9647 */
9648 return -1;
6cb6a27c
MM
9649 }
9650
e7868a85 9651sync_lower:
fd867d51
JW
9652 /* some features must be disabled on lower devices when disabled
9653 * on an upper device (think: bonding master or bridge)
9654 */
9655 netdev_for_each_lower_dev(dev, lower, iter)
9656 netdev_sync_lower_features(dev, lower, features);
9657
ae847f40
SD
9658 if (!err) {
9659 netdev_features_t diff = features ^ dev->features;
9660
9661 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9662 /* udp_tunnel_{get,drop}_rx_info both need
9663 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9664 * device, or they won't do anything.
9665 * Thus we need to update dev->features
9666 * *before* calling udp_tunnel_get_rx_info,
9667 * but *after* calling udp_tunnel_drop_rx_info.
9668 */
9669 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9670 dev->features = features;
9671 udp_tunnel_get_rx_info(dev);
9672 } else {
9673 udp_tunnel_drop_rx_info(dev);
9674 }
9675 }
9676
9daae9bd
GP
9677 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9678 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9679 dev->features = features;
9680 err |= vlan_get_rx_ctag_filter_info(dev);
9681 } else {
9682 vlan_drop_rx_ctag_filter_info(dev);
9683 }
9684 }
9685
9686 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9687 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9688 dev->features = features;
9689 err |= vlan_get_rx_stag_filter_info(dev);
9690 } else {
9691 vlan_drop_rx_stag_filter_info(dev);
9692 }
9693 }
9694
6cb6a27c 9695 dev->features = features;
ae847f40 9696 }
6cb6a27c 9697
e7868a85 9698 return err < 0 ? 0 : 1;
6cb6a27c
MM
9699}
9700
afe12cc8
MM
9701/**
9702 * netdev_update_features - recalculate device features
9703 * @dev: the device to check
9704 *
9705 * Recalculate dev->features set and send notifications if it
9706 * has changed. Should be called after driver or hardware dependent
9707 * conditions might have changed that influence the features.
9708 */
6cb6a27c
MM
9709void netdev_update_features(struct net_device *dev)
9710{
9711 if (__netdev_update_features(dev))
9712 netdev_features_change(dev);
5455c699
MM
9713}
9714EXPORT_SYMBOL(netdev_update_features);
9715
afe12cc8
MM
9716/**
9717 * netdev_change_features - recalculate device features
9718 * @dev: the device to check
9719 *
9720 * Recalculate dev->features set and send notifications even
9721 * if they have not changed. Should be called instead of
9722 * netdev_update_features() if also dev->vlan_features might
9723 * have changed to allow the changes to be propagated to stacked
9724 * VLAN devices.
9725 */
9726void netdev_change_features(struct net_device *dev)
9727{
9728 __netdev_update_features(dev);
9729 netdev_features_change(dev);
9730}
9731EXPORT_SYMBOL(netdev_change_features);
9732
fc4a7489
PM
9733/**
9734 * netif_stacked_transfer_operstate - transfer operstate
9735 * @rootdev: the root or lower level device to transfer state from
9736 * @dev: the device to transfer operstate to
9737 *
9738 * Transfer operational state from root to device. This is normally
9739 * called when a stacking relationship exists between the root
9740 * device and the device(a leaf device).
9741 */
9742void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9743 struct net_device *dev)
9744{
9745 if (rootdev->operstate == IF_OPER_DORMANT)
9746 netif_dormant_on(dev);
9747 else
9748 netif_dormant_off(dev);
9749
eec517cd
AL
9750 if (rootdev->operstate == IF_OPER_TESTING)
9751 netif_testing_on(dev);
9752 else
9753 netif_testing_off(dev);
9754
0575c86b
ZS
9755 if (netif_carrier_ok(rootdev))
9756 netif_carrier_on(dev);
9757 else
9758 netif_carrier_off(dev);
fc4a7489
PM
9759}
9760EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9761
1b4bf461
ED
9762static int netif_alloc_rx_queues(struct net_device *dev)
9763{
1b4bf461 9764 unsigned int i, count = dev->num_rx_queues;
bd25fa7b 9765 struct netdev_rx_queue *rx;
10595902 9766 size_t sz = count * sizeof(*rx);
e817f856 9767 int err = 0;
1b4bf461 9768
bd25fa7b 9769 BUG_ON(count < 1);
1b4bf461 9770
dcda9b04 9771 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
da6bc57a
MH
9772 if (!rx)
9773 return -ENOMEM;
9774
bd25fa7b
TH
9775 dev->_rx = rx;
9776
e817f856 9777 for (i = 0; i < count; i++) {
fe822240 9778 rx[i].dev = dev;
e817f856
JDB
9779
9780 /* XDP RX-queue setup */
b02e5a0e 9781 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
e817f856
JDB
9782 if (err < 0)
9783 goto err_rxq_info;
9784 }
1b4bf461 9785 return 0;
e817f856
JDB
9786
9787err_rxq_info:
9788 /* Rollback successful reg's and free other resources */
9789 while (i--)
9790 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
141b52a9 9791 kvfree(dev->_rx);
e817f856
JDB
9792 dev->_rx = NULL;
9793 return err;
9794}
9795
9796static void netif_free_rx_queues(struct net_device *dev)
9797{
9798 unsigned int i, count = dev->num_rx_queues;
e817f856
JDB
9799
9800 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9801 if (!dev->_rx)
9802 return;
9803
e817f856 9804 for (i = 0; i < count; i++)
82aaff2f
JK
9805 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9806
9807 kvfree(dev->_rx);
1b4bf461
ED
9808}
9809
aa942104
CG
9810static void netdev_init_one_queue(struct net_device *dev,
9811 struct netdev_queue *queue, void *_unused)
9812{
9813 /* Initialize queue lock */
9814 spin_lock_init(&queue->_xmit_lock);
1a33e10e 9815 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
aa942104 9816 queue->xmit_lock_owner = -1;
b236da69 9817 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
aa942104 9818 queue->dev = dev;
114cf580
TH
9819#ifdef CONFIG_BQL
9820 dql_init(&queue->dql, HZ);
9821#endif
aa942104
CG
9822}
9823
60877a32
ED
9824static void netif_free_tx_queues(struct net_device *dev)
9825{
4cb28970 9826 kvfree(dev->_tx);
60877a32
ED
9827}
9828
e6484930
TH
9829static int netif_alloc_netdev_queues(struct net_device *dev)
9830{
9831 unsigned int count = dev->num_tx_queues;
9832 struct netdev_queue *tx;
60877a32 9833 size_t sz = count * sizeof(*tx);
e6484930 9834
d339727c
ED
9835 if (count < 1 || count > 0xffff)
9836 return -EINVAL;
62b5942a 9837
dcda9b04 9838 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
da6bc57a
MH
9839 if (!tx)
9840 return -ENOMEM;
9841
e6484930 9842 dev->_tx = tx;
1d24eb48 9843
e6484930
TH
9844 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9845 spin_lock_init(&dev->tx_global_lock);
aa942104
CG
9846
9847 return 0;
e6484930
TH
9848}
9849
a2029240
DV
9850void netif_tx_stop_all_queues(struct net_device *dev)
9851{
9852 unsigned int i;
9853
9854 for (i = 0; i < dev->num_tx_queues; i++) {
9855 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
f4563a75 9856
a2029240
DV
9857 netif_tx_stop_queue(txq);
9858 }
9859}
9860EXPORT_SYMBOL(netif_tx_stop_all_queues);
9861
1da177e4
LT
9862/**
9863 * register_netdevice - register a network device
9864 * @dev: device to register
9865 *
9866 * Take a completed network device structure and add it to the kernel
9867 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9868 * chain. 0 is returned on success. A negative errno code is returned
9869 * on a failure to set up the device, or if the name is a duplicate.
9870 *
9871 * Callers must hold the rtnl semaphore. You may want
9872 * register_netdev() instead of this.
9873 *
9874 * BUGS:
9875 * The locking appears insufficient to guarantee two parallel registers
9876 * will not get the same name.
9877 */
9878
9879int register_netdevice(struct net_device *dev)
9880{
1da177e4 9881 int ret;
d314774c 9882 struct net *net = dev_net(dev);
1da177e4 9883
e283de3a
FF
9884 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9885 NETDEV_FEATURE_COUNT);
1da177e4
LT
9886 BUG_ON(dev_boot_phase);
9887 ASSERT_RTNL();
9888
b17a7c17
SH
9889 might_sleep();
9890
1da177e4
LT
9891 /* When net_device's are persistent, this will be fatal. */
9892 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
d314774c 9893 BUG_ON(!net);
1da177e4 9894
9000edb7
JK
9895 ret = ethtool_check_ops(dev->ethtool_ops);
9896 if (ret)
9897 return ret;
9898
f1f28aa3 9899 spin_lock_init(&dev->addr_list_lock);
845e0ebb 9900 netdev_set_addr_lockdep_class(dev);
1da177e4 9901
828de4f6 9902 ret = dev_get_valid_name(net, dev, dev->name);
0696c3a8
PP
9903 if (ret < 0)
9904 goto out;
9905
9077f052 9906 ret = -ENOMEM;
ff927412
JP
9907 dev->name_node = netdev_name_node_head_alloc(dev);
9908 if (!dev->name_node)
9909 goto out;
9910
1da177e4 9911 /* Init, if this function is available */
d314774c
SH
9912 if (dev->netdev_ops->ndo_init) {
9913 ret = dev->netdev_ops->ndo_init(dev);
1da177e4
LT
9914 if (ret) {
9915 if (ret > 0)
9916 ret = -EIO;
42c17fa6 9917 goto err_free_name;
1da177e4
LT
9918 }
9919 }
4ec93edb 9920
f646968f
PM
9921 if (((dev->hw_features | dev->features) &
9922 NETIF_F_HW_VLAN_CTAG_FILTER) &&
d2ed273d
MM
9923 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9924 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9925 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9926 ret = -EINVAL;
9927 goto err_uninit;
9928 }
9929
9c7dafbf
PE
9930 ret = -EBUSY;
9931 if (!dev->ifindex)
9932 dev->ifindex = dev_new_index(net);
9933 else if (__dev_get_by_index(net, dev->ifindex))
9934 goto err_uninit;
9935
5455c699
MM
9936 /* Transfer changeable features to wanted_features and enable
9937 * software offloads (GSO and GRO).
9938 */
1a3c998f 9939 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
14d1232f 9940 dev->features |= NETIF_F_SOFT_FEATURES;
d764a122 9941
876c4384 9942 if (dev->udp_tunnel_nic_info) {
d764a122
SD
9943 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9944 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9945 }
9946
14d1232f 9947 dev->wanted_features = dev->features & dev->hw_features;
1da177e4 9948
cbc53e08 9949 if (!(dev->flags & IFF_LOOPBACK))
34324dc2 9950 dev->hw_features |= NETIF_F_NOCACHE_COPY;
cbc53e08 9951
7f348a60
AD
9952 /* If IPv4 TCP segmentation offload is supported we should also
9953 * allow the device to enable segmenting the frame with the option
9954 * of ignoring a static IP ID value. This doesn't enable the
9955 * feature itself but allows the user to enable it later.
9956 */
cbc53e08
AD
9957 if (dev->hw_features & NETIF_F_TSO)
9958 dev->hw_features |= NETIF_F_TSO_MANGLEID;
7f348a60
AD
9959 if (dev->vlan_features & NETIF_F_TSO)
9960 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9961 if (dev->mpls_features & NETIF_F_TSO)
9962 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9963 if (dev->hw_enc_features & NETIF_F_TSO)
9964 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
c6e1a0d1 9965
1180e7d6 9966 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
16c3ea78 9967 */
1180e7d6 9968 dev->vlan_features |= NETIF_F_HIGHDMA;
16c3ea78 9969
ee579677
PS
9970 /* Make NETIF_F_SG inheritable to tunnel devices.
9971 */
802ab55a 9972 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
ee579677 9973
0d89d203
SH
9974 /* Make NETIF_F_SG inheritable to MPLS.
9975 */
9976 dev->mpls_features |= NETIF_F_SG;
9977
7ffbe3fd
JB
9978 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9979 ret = notifier_to_errno(ret);
9980 if (ret)
9981 goto err_uninit;
9982
8b41d188 9983 ret = netdev_register_kobject(dev);
cb626bf5
JH
9984 if (ret) {
9985 dev->reg_state = NETREG_UNREGISTERED;
7ce1b0ed 9986 goto err_uninit;
cb626bf5 9987 }
b17a7c17
SH
9988 dev->reg_state = NETREG_REGISTERED;
9989
6cb6a27c 9990 __netdev_update_features(dev);
8e9b59b2 9991
1da177e4
LT
9992 /*
9993 * Default initial state at registry is that the
9994 * device is present.
9995 */
9996
9997 set_bit(__LINK_STATE_PRESENT, &dev->state);
9998
8f4cccbb
BH
9999 linkwatch_init_dev(dev);
10000
1da177e4 10001 dev_init_scheduler(dev);
1da177e4 10002 dev_hold(dev);
ce286d32 10003 list_netdevice(dev);
7bf23575 10004 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 10005
948b337e
JP
10006 /* If the device has permanent device address, driver should
10007 * set dev_addr and also addr_assign_type should be set to
10008 * NET_ADDR_PERM (default value).
10009 */
10010 if (dev->addr_assign_type == NET_ADDR_PERM)
10011 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
10012
1da177e4 10013 /* Notify protocols, that a new device appeared. */
056925ab 10014 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
fcc5a03a 10015 ret = notifier_to_errno(ret);
93ee31f1 10016 if (ret) {
766b0515
JK
10017 /* Expect explicit free_netdev() on failure */
10018 dev->needs_free_netdev = false;
037e56bd 10019 unregister_netdevice_queue(dev, NULL);
766b0515 10020 goto out;
93ee31f1 10021 }
d90a909e
EB
10022 /*
10023 * Prevent userspace races by waiting until the network
10024 * device is fully setup before sending notifications.
10025 */
a2835763
PM
10026 if (!dev->rtnl_link_ops ||
10027 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
7f294054 10028 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
1da177e4
LT
10029
10030out:
10031 return ret;
7ce1b0ed
HX
10032
10033err_uninit:
d314774c
SH
10034 if (dev->netdev_ops->ndo_uninit)
10035 dev->netdev_ops->ndo_uninit(dev);
cf124db5
DM
10036 if (dev->priv_destructor)
10037 dev->priv_destructor(dev);
42c17fa6
DC
10038err_free_name:
10039 netdev_name_node_free(dev->name_node);
7ce1b0ed 10040 goto out;
1da177e4 10041}
d1b19dff 10042EXPORT_SYMBOL(register_netdevice);
1da177e4 10043
937f1ba5
BH
10044/**
10045 * init_dummy_netdev - init a dummy network device for NAPI
10046 * @dev: device to init
10047 *
10048 * This takes a network device structure and initialize the minimum
10049 * amount of fields so it can be used to schedule NAPI polls without
10050 * registering a full blown interface. This is to be used by drivers
10051 * that need to tie several hardware interfaces to a single NAPI
10052 * poll scheduler due to HW limitations.
10053 */
10054int init_dummy_netdev(struct net_device *dev)
10055{
10056 /* Clear everything. Note we don't initialize spinlocks
10057 * are they aren't supposed to be taken by any of the
10058 * NAPI code and this dummy netdev is supposed to be
10059 * only ever used for NAPI polls
10060 */
10061 memset(dev, 0, sizeof(struct net_device));
10062
10063 /* make sure we BUG if trying to hit standard
10064 * register/unregister code path
10065 */
10066 dev->reg_state = NETREG_DUMMY;
10067
937f1ba5
BH
10068 /* NAPI wants this */
10069 INIT_LIST_HEAD(&dev->napi_list);
10070
10071 /* a dummy interface is started by default */
10072 set_bit(__LINK_STATE_PRESENT, &dev->state);
10073 set_bit(__LINK_STATE_START, &dev->state);
10074
35edfdc7
JE
10075 /* napi_busy_loop stats accounting wants this */
10076 dev_net_set(dev, &init_net);
10077
29b4433d
ED
10078 /* Note : We dont allocate pcpu_refcnt for dummy devices,
10079 * because users of this 'device' dont need to change
10080 * its refcount.
10081 */
10082
937f1ba5
BH
10083 return 0;
10084}
10085EXPORT_SYMBOL_GPL(init_dummy_netdev);
10086
10087
1da177e4
LT
10088/**
10089 * register_netdev - register a network device
10090 * @dev: device to register
10091 *
10092 * Take a completed network device structure and add it to the kernel
10093 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10094 * chain. 0 is returned on success. A negative errno code is returned
10095 * on a failure to set up the device, or if the name is a duplicate.
10096 *
38b4da38 10097 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
10098 * and expands the device name if you passed a format string to
10099 * alloc_netdev.
10100 */
10101int register_netdev(struct net_device *dev)
10102{
10103 int err;
10104
b0f3debc
KT
10105 if (rtnl_lock_killable())
10106 return -EINTR;
1da177e4 10107 err = register_netdevice(dev);
1da177e4
LT
10108 rtnl_unlock();
10109 return err;
10110}
10111EXPORT_SYMBOL(register_netdev);
10112
29b4433d
ED
10113int netdev_refcnt_read(const struct net_device *dev)
10114{
10115 int i, refcnt = 0;
10116
10117 for_each_possible_cpu(i)
10118 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
10119 return refcnt;
10120}
10121EXPORT_SYMBOL(netdev_refcnt_read);
10122
de2b541b
MCC
10123#define WAIT_REFS_MIN_MSECS 1
10124#define WAIT_REFS_MAX_MSECS 250
2c53040f 10125/**
1da177e4 10126 * netdev_wait_allrefs - wait until all references are gone.
3de7a37b 10127 * @dev: target net_device
1da177e4
LT
10128 *
10129 * This is called when unregistering network devices.
10130 *
10131 * Any protocol or device that holds a reference should register
10132 * for netdevice notification, and cleanup and put back the
10133 * reference if they receive an UNREGISTER event.
10134 * We can get stuck here if buggy protocols don't correctly
4ec93edb 10135 * call dev_put.
1da177e4
LT
10136 */
10137static void netdev_wait_allrefs(struct net_device *dev)
10138{
10139 unsigned long rebroadcast_time, warning_time;
0e4be9e5 10140 int wait = 0, refcnt;
1da177e4 10141
e014debe
ED
10142 linkwatch_forget_dev(dev);
10143
1da177e4 10144 rebroadcast_time = warning_time = jiffies;
29b4433d
ED
10145 refcnt = netdev_refcnt_read(dev);
10146
10147 while (refcnt != 0) {
1da177e4 10148 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 10149 rtnl_lock();
1da177e4
LT
10150
10151 /* Rebroadcast unregister notification */
056925ab 10152 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
1da177e4 10153
748e2d93 10154 __rtnl_unlock();
0115e8e3 10155 rcu_barrier();
748e2d93
ED
10156 rtnl_lock();
10157
1da177e4
LT
10158 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
10159 &dev->state)) {
10160 /* We must not have linkwatch events
10161 * pending on unregister. If this
10162 * happens, we simply run the queue
10163 * unscheduled, resulting in a noop
10164 * for this device.
10165 */
10166 linkwatch_run_queue();
10167 }
10168
6756ae4b 10169 __rtnl_unlock();
1da177e4
LT
10170
10171 rebroadcast_time = jiffies;
10172 }
10173
0e4be9e5
FR
10174 if (!wait) {
10175 rcu_barrier();
10176 wait = WAIT_REFS_MIN_MSECS;
10177 } else {
10178 msleep(wait);
10179 wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
10180 }
1da177e4 10181
29b4433d
ED
10182 refcnt = netdev_refcnt_read(dev);
10183
d7c04b05 10184 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
7b6cd1ce
JP
10185 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
10186 dev->name, refcnt);
1da177e4
LT
10187 warning_time = jiffies;
10188 }
10189 }
10190}
10191
10192/* The sequence is:
10193 *
10194 * rtnl_lock();
10195 * ...
10196 * register_netdevice(x1);
10197 * register_netdevice(x2);
10198 * ...
10199 * unregister_netdevice(y1);
10200 * unregister_netdevice(y2);
10201 * ...
10202 * rtnl_unlock();
10203 * free_netdev(y1);
10204 * free_netdev(y2);
10205 *
58ec3b4d 10206 * We are invoked by rtnl_unlock().
1da177e4 10207 * This allows us to deal with problems:
b17a7c17 10208 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
10209 * without deadlocking with linkwatch via keventd.
10210 * 2) Since we run with the RTNL semaphore not held, we can sleep
10211 * safely in order to wait for the netdev refcnt to drop to zero.
58ec3b4d
HX
10212 *
10213 * We must not return until all unregister events added during
10214 * the interval the lock was held have been completed.
1da177e4 10215 */
1da177e4
LT
10216void netdev_run_todo(void)
10217{
626ab0e6 10218 struct list_head list;
1fc70edb
TY
10219#ifdef CONFIG_LOCKDEP
10220 struct list_head unlink_list;
10221
10222 list_replace_init(&net_unlink_list, &unlink_list);
10223
10224 while (!list_empty(&unlink_list)) {
10225 struct net_device *dev = list_first_entry(&unlink_list,
10226 struct net_device,
10227 unlink_list);
0e8b8d6a 10228 list_del_init(&dev->unlink_list);
1fc70edb
TY
10229 dev->nested_level = dev->lower_level - 1;
10230 }
10231#endif
1da177e4 10232
1da177e4 10233 /* Snapshot list, allow later requests */
626ab0e6 10234 list_replace_init(&net_todo_list, &list);
58ec3b4d
HX
10235
10236 __rtnl_unlock();
626ab0e6 10237
0115e8e3
ED
10238
10239 /* Wait for rcu callbacks to finish before next phase */
850a545b
EB
10240 if (!list_empty(&list))
10241 rcu_barrier();
10242
1da177e4
LT
10243 while (!list_empty(&list)) {
10244 struct net_device *dev
e5e26d75 10245 = list_first_entry(&list, struct net_device, todo_list);
1da177e4
LT
10246 list_del(&dev->todo_list);
10247
b17a7c17 10248 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
7b6cd1ce 10249 pr_err("network todo '%s' but state %d\n",
b17a7c17
SH
10250 dev->name, dev->reg_state);
10251 dump_stack();
10252 continue;
10253 }
1da177e4 10254
b17a7c17 10255 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 10256
b17a7c17 10257 netdev_wait_allrefs(dev);
1da177e4 10258
b17a7c17 10259 /* paranoia */
29b4433d 10260 BUG_ON(netdev_refcnt_read(dev));
7866a621
SN
10261 BUG_ON(!list_empty(&dev->ptype_all));
10262 BUG_ON(!list_empty(&dev->ptype_specific));
33d480ce
ED
10263 WARN_ON(rcu_access_pointer(dev->ip_ptr));
10264 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
330c7272 10265#if IS_ENABLED(CONFIG_DECNET)
547b792c 10266 WARN_ON(dev->dn_ptr);
330c7272 10267#endif
cf124db5
DM
10268 if (dev->priv_destructor)
10269 dev->priv_destructor(dev);
10270 if (dev->needs_free_netdev)
10271 free_netdev(dev);
9093bbb2 10272
50624c93
EB
10273 /* Report a network device has been unregistered */
10274 rtnl_lock();
10275 dev_net(dev)->dev_unreg_count--;
10276 __rtnl_unlock();
10277 wake_up(&netdev_unregistering_wq);
10278
9093bbb2
SH
10279 /* Free network device */
10280 kobject_put(&dev->dev.kobj);
1da177e4 10281 }
1da177e4
LT
10282}
10283
9256645a
JW
10284/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
10285 * all the same fields in the same order as net_device_stats, with only
10286 * the type differing, but rtnl_link_stats64 may have additional fields
10287 * at the end for newer counters.
3cfde79c 10288 */
77a1abf5
ED
10289void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
10290 const struct net_device_stats *netdev_stats)
3cfde79c
BH
10291{
10292#if BITS_PER_LONG == 64
9256645a 10293 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
9af9959e 10294 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
9256645a
JW
10295 /* zero out counters that only exist in rtnl_link_stats64 */
10296 memset((char *)stats64 + sizeof(*netdev_stats), 0,
10297 sizeof(*stats64) - sizeof(*netdev_stats));
3cfde79c 10298#else
9256645a 10299 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
3cfde79c
BH
10300 const unsigned long *src = (const unsigned long *)netdev_stats;
10301 u64 *dst = (u64 *)stats64;
10302
9256645a 10303 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
3cfde79c
BH
10304 for (i = 0; i < n; i++)
10305 dst[i] = src[i];
9256645a
JW
10306 /* zero out counters that only exist in rtnl_link_stats64 */
10307 memset((char *)stats64 + n * sizeof(u64), 0,
10308 sizeof(*stats64) - n * sizeof(u64));
3cfde79c
BH
10309#endif
10310}
77a1abf5 10311EXPORT_SYMBOL(netdev_stats_to_stats64);
3cfde79c 10312
eeda3fd6
SH
10313/**
10314 * dev_get_stats - get network device statistics
10315 * @dev: device to get statistics from
28172739 10316 * @storage: place to store stats
eeda3fd6 10317 *
d7753516
BH
10318 * Get network statistics from device. Return @storage.
10319 * The device driver may provide its own method by setting
10320 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
10321 * otherwise the internal statistics structure is used.
eeda3fd6 10322 */
d7753516
BH
10323struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
10324 struct rtnl_link_stats64 *storage)
7004bf25 10325{
eeda3fd6
SH
10326 const struct net_device_ops *ops = dev->netdev_ops;
10327
28172739
ED
10328 if (ops->ndo_get_stats64) {
10329 memset(storage, 0, sizeof(*storage));
caf586e5
ED
10330 ops->ndo_get_stats64(dev, storage);
10331 } else if (ops->ndo_get_stats) {
3cfde79c 10332 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
caf586e5
ED
10333 } else {
10334 netdev_stats_to_stats64(storage, &dev->stats);
28172739 10335 }
6f64ec74
ED
10336 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
10337 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
10338 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
28172739 10339 return storage;
c45d286e 10340}
eeda3fd6 10341EXPORT_SYMBOL(dev_get_stats);
c45d286e 10342
44fa32f0
HK
10343/**
10344 * dev_fetch_sw_netstats - get per-cpu network device statistics
10345 * @s: place to store stats
10346 * @netstats: per-cpu network stats to read from
10347 *
10348 * Read per-cpu network statistics and populate the related fields in @s.
10349 */
10350void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
10351 const struct pcpu_sw_netstats __percpu *netstats)
10352{
10353 int cpu;
10354
10355 for_each_possible_cpu(cpu) {
10356 const struct pcpu_sw_netstats *stats;
10357 struct pcpu_sw_netstats tmp;
10358 unsigned int start;
10359
10360 stats = per_cpu_ptr(netstats, cpu);
10361 do {
10362 start = u64_stats_fetch_begin_irq(&stats->syncp);
10363 tmp.rx_packets = stats->rx_packets;
10364 tmp.rx_bytes = stats->rx_bytes;
10365 tmp.tx_packets = stats->tx_packets;
10366 tmp.tx_bytes = stats->tx_bytes;
10367 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
10368
10369 s->rx_packets += tmp.rx_packets;
10370 s->rx_bytes += tmp.rx_bytes;
10371 s->tx_packets += tmp.tx_packets;
10372 s->tx_bytes += tmp.tx_bytes;
10373 }
10374}
10375EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
10376
a1839426
HK
10377/**
10378 * dev_get_tstats64 - ndo_get_stats64 implementation
10379 * @dev: device to get statistics from
10380 * @s: place to store stats
10381 *
10382 * Populate @s from dev->stats and dev->tstats. Can be used as
10383 * ndo_get_stats64() callback.
10384 */
10385void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
10386{
10387 netdev_stats_to_stats64(s, &dev->stats);
10388 dev_fetch_sw_netstats(s, dev->tstats);
10389}
10390EXPORT_SYMBOL_GPL(dev_get_tstats64);
10391
24824a09 10392struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
dc2b4847 10393{
24824a09 10394 struct netdev_queue *queue = dev_ingress_queue(dev);
dc2b4847 10395
24824a09
ED
10396#ifdef CONFIG_NET_CLS_ACT
10397 if (queue)
10398 return queue;
10399 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
10400 if (!queue)
10401 return NULL;
10402 netdev_init_one_queue(dev, queue, NULL);
2ce1ee17 10403 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
24824a09
ED
10404 queue->qdisc_sleeping = &noop_qdisc;
10405 rcu_assign_pointer(dev->ingress_queue, queue);
10406#endif
10407 return queue;
bb949fbd
DM
10408}
10409
2c60db03
ED
10410static const struct ethtool_ops default_ethtool_ops;
10411
d07d7507
SG
10412void netdev_set_default_ethtool_ops(struct net_device *dev,
10413 const struct ethtool_ops *ops)
10414{
10415 if (dev->ethtool_ops == &default_ethtool_ops)
10416 dev->ethtool_ops = ops;
10417}
10418EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
10419
74d332c1
ED
10420void netdev_freemem(struct net_device *dev)
10421{
10422 char *addr = (char *)dev - dev->padded;
10423
4cb28970 10424 kvfree(addr);
74d332c1
ED
10425}
10426
1da177e4 10427/**
722c9a0c 10428 * alloc_netdev_mqs - allocate network device
10429 * @sizeof_priv: size of private data to allocate space for
10430 * @name: device name format string
10431 * @name_assign_type: origin of device name
10432 * @setup: callback to initialize device
10433 * @txqs: the number of TX subqueues to allocate
10434 * @rxqs: the number of RX subqueues to allocate
10435 *
10436 * Allocates a struct net_device with private data area for driver use
10437 * and performs basic initialization. Also allocates subqueue structs
10438 * for each queue on the device.
1da177e4 10439 */
36909ea4 10440struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
c835a677 10441 unsigned char name_assign_type,
36909ea4
TH
10442 void (*setup)(struct net_device *),
10443 unsigned int txqs, unsigned int rxqs)
1da177e4 10444{
1da177e4 10445 struct net_device *dev;
52a59bd5 10446 unsigned int alloc_size;
1ce8e7b5 10447 struct net_device *p;
1da177e4 10448
b6fe17d6
SH
10449 BUG_ON(strlen(name) >= sizeof(dev->name));
10450
36909ea4 10451 if (txqs < 1) {
7b6cd1ce 10452 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
55513fb4
TH
10453 return NULL;
10454 }
10455
36909ea4 10456 if (rxqs < 1) {
7b6cd1ce 10457 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
36909ea4
TH
10458 return NULL;
10459 }
36909ea4 10460
fd2ea0a7 10461 alloc_size = sizeof(struct net_device);
d1643d24
AD
10462 if (sizeof_priv) {
10463 /* ensure 32-byte alignment of private area */
1ce8e7b5 10464 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
d1643d24
AD
10465 alloc_size += sizeof_priv;
10466 }
10467 /* ensure 32-byte alignment of whole construct */
1ce8e7b5 10468 alloc_size += NETDEV_ALIGN - 1;
1da177e4 10469
dcda9b04 10470 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
62b5942a 10471 if (!p)
1da177e4 10472 return NULL;
1da177e4 10473
1ce8e7b5 10474 dev = PTR_ALIGN(p, NETDEV_ALIGN);
1da177e4 10475 dev->padded = (char *)dev - (char *)p;
ab9c73cc 10476
29b4433d
ED
10477 dev->pcpu_refcnt = alloc_percpu(int);
10478 if (!dev->pcpu_refcnt)
74d332c1 10479 goto free_dev;
ab9c73cc 10480
ab9c73cc 10481 if (dev_addr_init(dev))
29b4433d 10482 goto free_pcpu;
ab9c73cc 10483
22bedad3 10484 dev_mc_init(dev);
a748ee24 10485 dev_uc_init(dev);
ccffad25 10486
c346dca1 10487 dev_net_set(dev, &init_net);
1da177e4 10488
8d3bdbd5 10489 dev->gso_max_size = GSO_MAX_SIZE;
30b678d8 10490 dev->gso_max_segs = GSO_MAX_SEGS;
5343da4c
TY
10491 dev->upper_level = 1;
10492 dev->lower_level = 1;
1fc70edb
TY
10493#ifdef CONFIG_LOCKDEP
10494 dev->nested_level = 0;
10495 INIT_LIST_HEAD(&dev->unlink_list);
10496#endif
8d3bdbd5 10497
8d3bdbd5
DM
10498 INIT_LIST_HEAD(&dev->napi_list);
10499 INIT_LIST_HEAD(&dev->unreg_list);
5cde2829 10500 INIT_LIST_HEAD(&dev->close_list);
8d3bdbd5 10501 INIT_LIST_HEAD(&dev->link_watch_list);
2f268f12
VF
10502 INIT_LIST_HEAD(&dev->adj_list.upper);
10503 INIT_LIST_HEAD(&dev->adj_list.lower);
7866a621
SN
10504 INIT_LIST_HEAD(&dev->ptype_all);
10505 INIT_LIST_HEAD(&dev->ptype_specific);
93642e14 10506 INIT_LIST_HEAD(&dev->net_notifier_list);
59cc1f61
JK
10507#ifdef CONFIG_NET_SCHED
10508 hash_init(dev->qdisc_hash);
10509#endif
02875878 10510 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
8d3bdbd5
DM
10511 setup(dev);
10512
a813104d 10513 if (!dev->tx_queue_len) {
f84bb1ea 10514 dev->priv_flags |= IFF_NO_QUEUE;
11597084 10515 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
a813104d 10516 }
906470c1 10517
36909ea4
TH
10518 dev->num_tx_queues = txqs;
10519 dev->real_num_tx_queues = txqs;
ed9af2e8 10520 if (netif_alloc_netdev_queues(dev))
8d3bdbd5 10521 goto free_all;
e8a0464c 10522
36909ea4
TH
10523 dev->num_rx_queues = rxqs;
10524 dev->real_num_rx_queues = rxqs;
fe822240 10525 if (netif_alloc_rx_queues(dev))
8d3bdbd5 10526 goto free_all;
0a9627f2 10527
1da177e4 10528 strcpy(dev->name, name);
c835a677 10529 dev->name_assign_type = name_assign_type;
cbda10fa 10530 dev->group = INIT_NETDEV_GROUP;
2c60db03
ED
10531 if (!dev->ethtool_ops)
10532 dev->ethtool_ops = &default_ethtool_ops;
e687ad60 10533
357b6cc5 10534 nf_hook_ingress_init(dev);
e687ad60 10535
1da177e4 10536 return dev;
ab9c73cc 10537
8d3bdbd5
DM
10538free_all:
10539 free_netdev(dev);
10540 return NULL;
10541
29b4433d
ED
10542free_pcpu:
10543 free_percpu(dev->pcpu_refcnt);
74d332c1
ED
10544free_dev:
10545 netdev_freemem(dev);
ab9c73cc 10546 return NULL;
1da177e4 10547}
36909ea4 10548EXPORT_SYMBOL(alloc_netdev_mqs);
1da177e4
LT
10549
10550/**
722c9a0c 10551 * free_netdev - free network device
10552 * @dev: device
1da177e4 10553 *
722c9a0c 10554 * This function does the last stage of destroying an allocated device
10555 * interface. The reference to the device object is released. If this
10556 * is the last reference then it will be freed.Must be called in process
10557 * context.
1da177e4
LT
10558 */
10559void free_netdev(struct net_device *dev)
10560{
d565b0a1
HX
10561 struct napi_struct *p, *n;
10562
93d05d4a 10563 might_sleep();
c269a24c
JK
10564
10565 /* When called immediately after register_netdevice() failed the unwind
10566 * handling may still be dismantling the device. Handle that case by
10567 * deferring the free.
10568 */
10569 if (dev->reg_state == NETREG_UNREGISTERING) {
10570 ASSERT_RTNL();
10571 dev->needs_free_netdev = true;
10572 return;
10573 }
10574
60877a32 10575 netif_free_tx_queues(dev);
e817f856 10576 netif_free_rx_queues(dev);
e8a0464c 10577
33d480ce 10578 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
24824a09 10579
f001fde5
JP
10580 /* Flush device addresses */
10581 dev_addr_flush(dev);
10582
d565b0a1
HX
10583 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
10584 netif_napi_del(p);
10585
29b4433d
ED
10586 free_percpu(dev->pcpu_refcnt);
10587 dev->pcpu_refcnt = NULL;
75ccae62
THJ
10588 free_percpu(dev->xdp_bulkq);
10589 dev->xdp_bulkq = NULL;
29b4433d 10590
3041a069 10591 /* Compatibility with error handling in drivers */
1da177e4 10592 if (dev->reg_state == NETREG_UNINITIALIZED) {
74d332c1 10593 netdev_freemem(dev);
1da177e4
LT
10594 return;
10595 }
10596
10597 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
10598 dev->reg_state = NETREG_RELEASED;
10599
43cb76d9
GKH
10600 /* will free via device release */
10601 put_device(&dev->dev);
1da177e4 10602}
d1b19dff 10603EXPORT_SYMBOL(free_netdev);
4ec93edb 10604
f0db275a
SH
10605/**
10606 * synchronize_net - Synchronize with packet receive processing
10607 *
10608 * Wait for packets currently being received to be done.
10609 * Does not block later packets from starting.
10610 */
4ec93edb 10611void synchronize_net(void)
1da177e4
LT
10612{
10613 might_sleep();
be3fc413
ED
10614 if (rtnl_is_locked())
10615 synchronize_rcu_expedited();
10616 else
10617 synchronize_rcu();
1da177e4 10618}
d1b19dff 10619EXPORT_SYMBOL(synchronize_net);
1da177e4
LT
10620
10621/**
44a0873d 10622 * unregister_netdevice_queue - remove device from the kernel
1da177e4 10623 * @dev: device
44a0873d 10624 * @head: list
6ebfbc06 10625 *
1da177e4 10626 * This function shuts down a device interface and removes it
d59b54b1 10627 * from the kernel tables.
44a0873d 10628 * If head not NULL, device is queued to be unregistered later.
1da177e4
LT
10629 *
10630 * Callers must hold the rtnl semaphore. You may want
10631 * unregister_netdev() instead of this.
10632 */
10633
44a0873d 10634void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
1da177e4 10635{
a6620712
HX
10636 ASSERT_RTNL();
10637
44a0873d 10638 if (head) {
9fdce099 10639 list_move_tail(&dev->unreg_list, head);
44a0873d 10640 } else {
037e56bd
JK
10641 LIST_HEAD(single);
10642
10643 list_add(&dev->unreg_list, &single);
0cbe1e57 10644 unregister_netdevice_many(&single);
44a0873d 10645 }
1da177e4 10646}
44a0873d 10647EXPORT_SYMBOL(unregister_netdevice_queue);
1da177e4 10648
9b5e383c
ED
10649/**
10650 * unregister_netdevice_many - unregister many devices
10651 * @head: list of devices
87757a91
ED
10652 *
10653 * Note: As most callers use a stack allocated list_head,
10654 * we force a list_del() to make sure stack wont be corrupted later.
9b5e383c
ED
10655 */
10656void unregister_netdevice_many(struct list_head *head)
bcfe2f1a
JK
10657{
10658 struct net_device *dev, *tmp;
10659 LIST_HEAD(close_head);
10660
10661 BUG_ON(dev_boot_phase);
10662 ASSERT_RTNL();
10663
0cbe1e57
JK
10664 if (list_empty(head))
10665 return;
10666
bcfe2f1a
JK
10667 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
10668 /* Some devices call without registering
10669 * for initialization unwind. Remove those
10670 * devices and proceed with the remaining.
10671 */
10672 if (dev->reg_state == NETREG_UNINITIALIZED) {
10673 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
10674 dev->name, dev);
10675
10676 WARN_ON(1);
10677 list_del(&dev->unreg_list);
10678 continue;
10679 }
10680 dev->dismantle = true;
10681 BUG_ON(dev->reg_state != NETREG_REGISTERED);
10682 }
10683
10684 /* If device is running, close it first. */
10685 list_for_each_entry(dev, head, unreg_list)
10686 list_add_tail(&dev->close_list, &close_head);
10687 dev_close_many(&close_head, true);
10688
10689 list_for_each_entry(dev, head, unreg_list) {
10690 /* And unlink it from device chain. */
10691 unlist_netdevice(dev);
10692
10693 dev->reg_state = NETREG_UNREGISTERING;
10694 }
10695 flush_all_backlogs();
10696
10697 synchronize_net();
10698
10699 list_for_each_entry(dev, head, unreg_list) {
10700 struct sk_buff *skb = NULL;
10701
10702 /* Shutdown queueing discipline. */
10703 dev_shutdown(dev);
10704
10705 dev_xdp_uninstall(dev);
10706
10707 /* Notify protocols, that we are about to destroy
10708 * this device. They should clean all the things.
10709 */
10710 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
10711
10712 if (!dev->rtnl_link_ops ||
10713 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
10714 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
10715 GFP_KERNEL, NULL, 0);
10716
10717 /*
10718 * Flush the unicast and multicast chains
10719 */
10720 dev_uc_flush(dev);
10721 dev_mc_flush(dev);
10722
10723 netdev_name_node_alt_flush(dev);
10724 netdev_name_node_free(dev->name_node);
10725
10726 if (dev->netdev_ops->ndo_uninit)
10727 dev->netdev_ops->ndo_uninit(dev);
10728
10729 if (skb)
10730 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
10731
10732 /* Notifier chain MUST detach us all upper devices. */
10733 WARN_ON(netdev_has_any_upper_dev(dev));
10734 WARN_ON(netdev_has_any_lower_dev(dev));
10735
10736 /* Remove entries from kobject tree */
10737 netdev_unregister_kobject(dev);
10738#ifdef CONFIG_XPS
10739 /* Remove XPS queueing entries */
10740 netif_reset_xps_queues_gt(dev, 0);
10741#endif
10742 }
10743
10744 synchronize_net();
10745
10746 list_for_each_entry(dev, head, unreg_list) {
10747 dev_put(dev);
10748 net_set_todo(dev);
10749 }
0cbe1e57
JK
10750
10751 list_del(head);
bcfe2f1a 10752}
0cbe1e57 10753EXPORT_SYMBOL(unregister_netdevice_many);
bcfe2f1a 10754
1da177e4
LT
10755/**
10756 * unregister_netdev - remove device from the kernel
10757 * @dev: device
10758 *
10759 * This function shuts down a device interface and removes it
d59b54b1 10760 * from the kernel tables.
1da177e4
LT
10761 *
10762 * This is just a wrapper for unregister_netdevice that takes
10763 * the rtnl semaphore. In general you want to use this and not
10764 * unregister_netdevice.
10765 */
10766void unregister_netdev(struct net_device *dev)
10767{
10768 rtnl_lock();
10769 unregister_netdevice(dev);
10770 rtnl_unlock();
10771}
1da177e4
LT
10772EXPORT_SYMBOL(unregister_netdev);
10773
ce286d32
EB
10774/**
10775 * dev_change_net_namespace - move device to different nethost namespace
10776 * @dev: device
10777 * @net: network namespace
10778 * @pat: If not NULL name pattern to try if the current device name
10779 * is already taken in the destination network namespace.
10780 *
10781 * This function shuts down a device interface and moves it
10782 * to a new network namespace. On success 0 is returned, on
10783 * a failure a netagive errno code is returned.
10784 *
10785 * Callers must hold the rtnl semaphore.
10786 */
10787
10788int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10789{
ef6a4c88 10790 struct net *net_old = dev_net(dev);
38e01b30 10791 int err, new_nsid, new_ifindex;
ce286d32
EB
10792
10793 ASSERT_RTNL();
10794
10795 /* Don't allow namespace local devices to be moved. */
10796 err = -EINVAL;
10797 if (dev->features & NETIF_F_NETNS_LOCAL)
10798 goto out;
10799
10800 /* Ensure the device has been registrered */
ce286d32
EB
10801 if (dev->reg_state != NETREG_REGISTERED)
10802 goto out;
10803
10804 /* Get out if there is nothing todo */
10805 err = 0;
ef6a4c88 10806 if (net_eq(net_old, net))
ce286d32
EB
10807 goto out;
10808
10809 /* Pick the destination device name, and ensure
10810 * we can use it in the destination network namespace.
10811 */
10812 err = -EEXIST;
d9031024 10813 if (__dev_get_by_name(net, dev->name)) {
ce286d32
EB
10814 /* We get here if we can't use the current device name */
10815 if (!pat)
10816 goto out;
7892bd08
LR
10817 err = dev_get_valid_name(net, dev, pat);
10818 if (err < 0)
ce286d32
EB
10819 goto out;
10820 }
10821
10822 /*
10823 * And now a mini version of register_netdevice unregister_netdevice.
10824 */
10825
10826 /* If device is running close it first. */
9b772652 10827 dev_close(dev);
ce286d32
EB
10828
10829 /* And unlink it from device chain */
ce286d32
EB
10830 unlist_netdevice(dev);
10831
10832 synchronize_net();
10833
10834 /* Shutdown queueing discipline. */
10835 dev_shutdown(dev);
10836
10837 /* Notify protocols, that we are about to destroy
eb13da1a 10838 * this device. They should clean all the things.
10839 *
10840 * Note that dev->reg_state stays at NETREG_REGISTERED.
10841 * This is wanted because this way 8021q and macvlan know
10842 * the device is just moving and can keep their slaves up.
10843 */
ce286d32 10844 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6549dd43 10845 rcu_barrier();
38e01b30 10846
d4e4fdf9 10847 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
38e01b30
ND
10848 /* If there is an ifindex conflict assign a new one */
10849 if (__dev_get_by_index(net, dev->ifindex))
10850 new_ifindex = dev_new_index(net);
10851 else
10852 new_ifindex = dev->ifindex;
10853
10854 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10855 new_ifindex);
ce286d32
EB
10856
10857 /*
10858 * Flush the unicast and multicast chains
10859 */
a748ee24 10860 dev_uc_flush(dev);
22bedad3 10861 dev_mc_flush(dev);
ce286d32 10862
4e66ae2e
SH
10863 /* Send a netdev-removed uevent to the old namespace */
10864 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
4c75431a 10865 netdev_adjacent_del_links(dev);
4e66ae2e 10866
93642e14
JP
10867 /* Move per-net netdevice notifiers that are following the netdevice */
10868 move_netdevice_notifiers_dev_net(dev, net);
10869
ce286d32 10870 /* Actually switch the network namespace */
c346dca1 10871 dev_net_set(dev, net);
38e01b30 10872 dev->ifindex = new_ifindex;
ce286d32 10873
4e66ae2e
SH
10874 /* Send a netdev-add uevent to the new namespace */
10875 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
4c75431a 10876 netdev_adjacent_add_links(dev);
4e66ae2e 10877
8b41d188 10878 /* Fixup kobjects */
a1b3f594 10879 err = device_rename(&dev->dev, dev->name);
8b41d188 10880 WARN_ON(err);
ce286d32 10881
ef6a4c88
CB
10882 /* Adapt owner in case owning user namespace of target network
10883 * namespace is different from the original one.
10884 */
10885 err = netdev_change_owner(dev, net_old, net);
10886 WARN_ON(err);
10887
ce286d32
EB
10888 /* Add the device back in the hashes */
10889 list_netdevice(dev);
10890
10891 /* Notify protocols, that a new device appeared. */
10892 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10893
d90a909e
EB
10894 /*
10895 * Prevent userspace races by waiting until the network
10896 * device is fully setup before sending notifications.
10897 */
7f294054 10898 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
d90a909e 10899
ce286d32
EB
10900 synchronize_net();
10901 err = 0;
10902out:
10903 return err;
10904}
463d0183 10905EXPORT_SYMBOL_GPL(dev_change_net_namespace);
ce286d32 10906
f0bf90de 10907static int dev_cpu_dead(unsigned int oldcpu)
1da177e4
LT
10908{
10909 struct sk_buff **list_skb;
1da177e4 10910 struct sk_buff *skb;
f0bf90de 10911 unsigned int cpu;
97d8b6e3 10912 struct softnet_data *sd, *oldsd, *remsd = NULL;
1da177e4 10913
1da177e4
LT
10914 local_irq_disable();
10915 cpu = smp_processor_id();
10916 sd = &per_cpu(softnet_data, cpu);
10917 oldsd = &per_cpu(softnet_data, oldcpu);
10918
10919 /* Find end of our completion_queue. */
10920 list_skb = &sd->completion_queue;
10921 while (*list_skb)
10922 list_skb = &(*list_skb)->next;
10923 /* Append completion queue from offline CPU. */
10924 *list_skb = oldsd->completion_queue;
10925 oldsd->completion_queue = NULL;
10926
1da177e4 10927 /* Append output queue from offline CPU. */
a9cbd588
CG
10928 if (oldsd->output_queue) {
10929 *sd->output_queue_tailp = oldsd->output_queue;
10930 sd->output_queue_tailp = oldsd->output_queue_tailp;
10931 oldsd->output_queue = NULL;
10932 oldsd->output_queue_tailp = &oldsd->output_queue;
10933 }
ac64da0b
ED
10934 /* Append NAPI poll list from offline CPU, with one exception :
10935 * process_backlog() must be called by cpu owning percpu backlog.
10936 * We properly handle process_queue & input_pkt_queue later.
10937 */
10938 while (!list_empty(&oldsd->poll_list)) {
10939 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10940 struct napi_struct,
10941 poll_list);
10942
10943 list_del_init(&napi->poll_list);
10944 if (napi->poll == process_backlog)
10945 napi->state = 0;
10946 else
10947 ____napi_schedule(sd, napi);
264524d5 10948 }
1da177e4
LT
10949
10950 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10951 local_irq_enable();
10952
773fc8f6 10953#ifdef CONFIG_RPS
10954 remsd = oldsd->rps_ipi_list;
10955 oldsd->rps_ipi_list = NULL;
10956#endif
10957 /* send out pending IPI's on offline CPU */
10958 net_rps_send_ipi(remsd);
10959
1da177e4 10960 /* Process offline CPU's input_pkt_queue */
76cc8b13 10961 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
91e83133 10962 netif_rx_ni(skb);
76cc8b13 10963 input_queue_head_incr(oldsd);
fec5e652 10964 }
ac64da0b 10965 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
91e83133 10966 netif_rx_ni(skb);
76cc8b13
TH
10967 input_queue_head_incr(oldsd);
10968 }
1da177e4 10969
f0bf90de 10970 return 0;
1da177e4 10971}
1da177e4 10972
7f353bf2 10973/**
b63365a2
HX
10974 * netdev_increment_features - increment feature set by one
10975 * @all: current feature set
10976 * @one: new feature set
10977 * @mask: mask feature set
7f353bf2
HX
10978 *
10979 * Computes a new feature set after adding a device with feature set
b63365a2
HX
10980 * @one to the master device with current feature set @all. Will not
10981 * enable anything that is off in @mask. Returns the new feature set.
7f353bf2 10982 */
c8f44aff
MM
10983netdev_features_t netdev_increment_features(netdev_features_t all,
10984 netdev_features_t one, netdev_features_t mask)
b63365a2 10985{
c8cd0989 10986 if (mask & NETIF_F_HW_CSUM)
a188222b 10987 mask |= NETIF_F_CSUM_MASK;
1742f183 10988 mask |= NETIF_F_VLAN_CHALLENGED;
7f353bf2 10989
a188222b 10990 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
1742f183 10991 all &= one | ~NETIF_F_ALL_FOR_ALL;
c6e1a0d1 10992
1742f183 10993 /* If one device supports hw checksumming, set for all. */
c8cd0989
TH
10994 if (all & NETIF_F_HW_CSUM)
10995 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
7f353bf2
HX
10996
10997 return all;
10998}
b63365a2 10999EXPORT_SYMBOL(netdev_increment_features);
7f353bf2 11000
430f03cd 11001static struct hlist_head * __net_init netdev_create_hash(void)
30d97d35
PE
11002{
11003 int i;
11004 struct hlist_head *hash;
11005
6da2ec56 11006 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
30d97d35
PE
11007 if (hash != NULL)
11008 for (i = 0; i < NETDEV_HASHENTRIES; i++)
11009 INIT_HLIST_HEAD(&hash[i]);
11010
11011 return hash;
11012}
11013
881d966b 11014/* Initialize per network namespace state */
4665079c 11015static int __net_init netdev_init(struct net *net)
881d966b 11016{
d9f37d01 11017 BUILD_BUG_ON(GRO_HASH_BUCKETS >
c593642c 11018 8 * sizeof_field(struct napi_struct, gro_bitmask));
d9f37d01 11019
734b6541
RM
11020 if (net != &init_net)
11021 INIT_LIST_HEAD(&net->dev_base_head);
881d966b 11022
30d97d35
PE
11023 net->dev_name_head = netdev_create_hash();
11024 if (net->dev_name_head == NULL)
11025 goto err_name;
881d966b 11026
30d97d35
PE
11027 net->dev_index_head = netdev_create_hash();
11028 if (net->dev_index_head == NULL)
11029 goto err_idx;
881d966b 11030
a30c7b42
JP
11031 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
11032
881d966b 11033 return 0;
30d97d35
PE
11034
11035err_idx:
11036 kfree(net->dev_name_head);
11037err_name:
11038 return -ENOMEM;
881d966b
EB
11039}
11040
f0db275a
SH
11041/**
11042 * netdev_drivername - network driver for the device
11043 * @dev: network device
f0db275a
SH
11044 *
11045 * Determine network driver for device.
11046 */
3019de12 11047const char *netdev_drivername(const struct net_device *dev)
6579e57b 11048{
cf04a4c7
SH
11049 const struct device_driver *driver;
11050 const struct device *parent;
3019de12 11051 const char *empty = "";
6579e57b
AV
11052
11053 parent = dev->dev.parent;
6579e57b 11054 if (!parent)
3019de12 11055 return empty;
6579e57b
AV
11056
11057 driver = parent->driver;
11058 if (driver && driver->name)
3019de12
DM
11059 return driver->name;
11060 return empty;
6579e57b
AV
11061}
11062
6ea754eb
JP
11063static void __netdev_printk(const char *level, const struct net_device *dev,
11064 struct va_format *vaf)
256df2f3 11065{
b004ff49 11066 if (dev && dev->dev.parent) {
6ea754eb
JP
11067 dev_printk_emit(level[1] - '0',
11068 dev->dev.parent,
11069 "%s %s %s%s: %pV",
11070 dev_driver_string(dev->dev.parent),
11071 dev_name(dev->dev.parent),
11072 netdev_name(dev), netdev_reg_state(dev),
11073 vaf);
b004ff49 11074 } else if (dev) {
6ea754eb
JP
11075 printk("%s%s%s: %pV",
11076 level, netdev_name(dev), netdev_reg_state(dev), vaf);
b004ff49 11077 } else {
6ea754eb 11078 printk("%s(NULL net_device): %pV", level, vaf);
b004ff49 11079 }
256df2f3
JP
11080}
11081
6ea754eb
JP
11082void netdev_printk(const char *level, const struct net_device *dev,
11083 const char *format, ...)
256df2f3
JP
11084{
11085 struct va_format vaf;
11086 va_list args;
256df2f3
JP
11087
11088 va_start(args, format);
11089
11090 vaf.fmt = format;
11091 vaf.va = &args;
11092
6ea754eb 11093 __netdev_printk(level, dev, &vaf);
b004ff49 11094
256df2f3 11095 va_end(args);
256df2f3
JP
11096}
11097EXPORT_SYMBOL(netdev_printk);
11098
11099#define define_netdev_printk_level(func, level) \
6ea754eb 11100void func(const struct net_device *dev, const char *fmt, ...) \
256df2f3 11101{ \
256df2f3
JP
11102 struct va_format vaf; \
11103 va_list args; \
11104 \
11105 va_start(args, fmt); \
11106 \
11107 vaf.fmt = fmt; \
11108 vaf.va = &args; \
11109 \
6ea754eb 11110 __netdev_printk(level, dev, &vaf); \
b004ff49 11111 \
256df2f3 11112 va_end(args); \
256df2f3
JP
11113} \
11114EXPORT_SYMBOL(func);
11115
11116define_netdev_printk_level(netdev_emerg, KERN_EMERG);
11117define_netdev_printk_level(netdev_alert, KERN_ALERT);
11118define_netdev_printk_level(netdev_crit, KERN_CRIT);
11119define_netdev_printk_level(netdev_err, KERN_ERR);
11120define_netdev_printk_level(netdev_warn, KERN_WARNING);
11121define_netdev_printk_level(netdev_notice, KERN_NOTICE);
11122define_netdev_printk_level(netdev_info, KERN_INFO);
11123
4665079c 11124static void __net_exit netdev_exit(struct net *net)
881d966b
EB
11125{
11126 kfree(net->dev_name_head);
11127 kfree(net->dev_index_head);
ee21b18b
VA
11128 if (net != &init_net)
11129 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
881d966b
EB
11130}
11131
022cbae6 11132static struct pernet_operations __net_initdata netdev_net_ops = {
881d966b
EB
11133 .init = netdev_init,
11134 .exit = netdev_exit,
11135};
11136
4665079c 11137static void __net_exit default_device_exit(struct net *net)
ce286d32 11138{
e008b5fc 11139 struct net_device *dev, *aux;
ce286d32 11140 /*
e008b5fc 11141 * Push all migratable network devices back to the
ce286d32
EB
11142 * initial network namespace
11143 */
11144 rtnl_lock();
e008b5fc 11145 for_each_netdev_safe(net, dev, aux) {
ce286d32 11146 int err;
aca51397 11147 char fb_name[IFNAMSIZ];
ce286d32
EB
11148
11149 /* Ignore unmoveable devices (i.e. loopback) */
11150 if (dev->features & NETIF_F_NETNS_LOCAL)
11151 continue;
11152
e008b5fc
EB
11153 /* Leave virtual devices for the generic cleanup */
11154 if (dev->rtnl_link_ops)
11155 continue;
d0c082ce 11156
25985edc 11157 /* Push remaining network devices to init_net */
aca51397 11158 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
55b40dbf
JP
11159 if (__dev_get_by_name(&init_net, fb_name))
11160 snprintf(fb_name, IFNAMSIZ, "dev%%d");
aca51397 11161 err = dev_change_net_namespace(dev, &init_net, fb_name);
ce286d32 11162 if (err) {
7b6cd1ce
JP
11163 pr_emerg("%s: failed to move %s to init_net: %d\n",
11164 __func__, dev->name, err);
aca51397 11165 BUG();
ce286d32
EB
11166 }
11167 }
11168 rtnl_unlock();
11169}
11170
50624c93
EB
11171static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
11172{
11173 /* Return with the rtnl_lock held when there are no network
11174 * devices unregistering in any network namespace in net_list.
11175 */
11176 struct net *net;
11177 bool unregistering;
ff960a73 11178 DEFINE_WAIT_FUNC(wait, woken_wake_function);
50624c93 11179
ff960a73 11180 add_wait_queue(&netdev_unregistering_wq, &wait);
50624c93 11181 for (;;) {
50624c93
EB
11182 unregistering = false;
11183 rtnl_lock();
11184 list_for_each_entry(net, net_list, exit_list) {
11185 if (net->dev_unreg_count > 0) {
11186 unregistering = true;
11187 break;
11188 }
11189 }
11190 if (!unregistering)
11191 break;
11192 __rtnl_unlock();
ff960a73
PZ
11193
11194 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
50624c93 11195 }
ff960a73 11196 remove_wait_queue(&netdev_unregistering_wq, &wait);
50624c93
EB
11197}
11198
04dc7f6b
EB
11199static void __net_exit default_device_exit_batch(struct list_head *net_list)
11200{
11201 /* At exit all network devices most be removed from a network
b595076a 11202 * namespace. Do this in the reverse order of registration.
04dc7f6b
EB
11203 * Do this across as many network namespaces as possible to
11204 * improve batching efficiency.
11205 */
11206 struct net_device *dev;
11207 struct net *net;
11208 LIST_HEAD(dev_kill_list);
11209
50624c93
EB
11210 /* To prevent network device cleanup code from dereferencing
11211 * loopback devices or network devices that have been freed
11212 * wait here for all pending unregistrations to complete,
11213 * before unregistring the loopback device and allowing the
11214 * network namespace be freed.
11215 *
11216 * The netdev todo list containing all network devices
11217 * unregistrations that happen in default_device_exit_batch
11218 * will run in the rtnl_unlock() at the end of
11219 * default_device_exit_batch.
11220 */
11221 rtnl_lock_unregistering(net_list);
04dc7f6b
EB
11222 list_for_each_entry(net, net_list, exit_list) {
11223 for_each_netdev_reverse(net, dev) {
b0ab2fab 11224 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
04dc7f6b
EB
11225 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
11226 else
11227 unregister_netdevice_queue(dev, &dev_kill_list);
11228 }
11229 }
11230 unregister_netdevice_many(&dev_kill_list);
11231 rtnl_unlock();
11232}
11233
022cbae6 11234static struct pernet_operations __net_initdata default_device_ops = {
ce286d32 11235 .exit = default_device_exit,
04dc7f6b 11236 .exit_batch = default_device_exit_batch,
ce286d32
EB
11237};
11238
1da177e4
LT
11239/*
11240 * Initialize the DEV module. At boot time this walks the device list and
11241 * unhooks any devices that fail to initialise (normally hardware not
11242 * present) and leaves us with a valid list of present and active devices.
11243 *
11244 */
11245
11246/*
11247 * This is called single threaded during boot, so no need
11248 * to take the rtnl semaphore.
11249 */
11250static int __init net_dev_init(void)
11251{
11252 int i, rc = -ENOMEM;
11253
11254 BUG_ON(!dev_boot_phase);
11255
1da177e4
LT
11256 if (dev_proc_init())
11257 goto out;
11258
8b41d188 11259 if (netdev_kobject_init())
1da177e4
LT
11260 goto out;
11261
11262 INIT_LIST_HEAD(&ptype_all);
82d8a867 11263 for (i = 0; i < PTYPE_HASH_SIZE; i++)
1da177e4
LT
11264 INIT_LIST_HEAD(&ptype_base[i]);
11265
62532da9
VY
11266 INIT_LIST_HEAD(&offload_base);
11267
881d966b
EB
11268 if (register_pernet_subsys(&netdev_net_ops))
11269 goto out;
1da177e4
LT
11270
11271 /*
11272 * Initialise the packet receive queues.
11273 */
11274
6f912042 11275 for_each_possible_cpu(i) {
41852497 11276 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
e36fa2f7 11277 struct softnet_data *sd = &per_cpu(softnet_data, i);
1da177e4 11278
41852497
ED
11279 INIT_WORK(flush, flush_backlog);
11280
e36fa2f7 11281 skb_queue_head_init(&sd->input_pkt_queue);
6e7676c1 11282 skb_queue_head_init(&sd->process_queue);
f53c7239
SK
11283#ifdef CONFIG_XFRM_OFFLOAD
11284 skb_queue_head_init(&sd->xfrm_backlog);
11285#endif
e36fa2f7 11286 INIT_LIST_HEAD(&sd->poll_list);
a9cbd588 11287 sd->output_queue_tailp = &sd->output_queue;
df334545 11288#ifdef CONFIG_RPS
545b8c8d 11289 INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
e36fa2f7 11290 sd->cpu = i;
1e94d72f 11291#endif
0a9627f2 11292
7c4ec749 11293 init_gro_hash(&sd->backlog);
e36fa2f7
ED
11294 sd->backlog.poll = process_backlog;
11295 sd->backlog.weight = weight_p;
1da177e4
LT
11296 }
11297
1da177e4
LT
11298 dev_boot_phase = 0;
11299
505d4f73
EB
11300 /* The loopback device is special if any other network devices
11301 * is present in a network namespace the loopback device must
11302 * be present. Since we now dynamically allocate and free the
11303 * loopback device ensure this invariant is maintained by
11304 * keeping the loopback device as the first device on the
11305 * list of network devices. Ensuring the loopback devices
11306 * is the first device that appears and the last network device
11307 * that disappears.
11308 */
11309 if (register_pernet_device(&loopback_net_ops))
11310 goto out;
11311
11312 if (register_pernet_device(&default_device_ops))
11313 goto out;
11314
962cf36c
CM
11315 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
11316 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
1da177e4 11317
f0bf90de
SAS
11318 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
11319 NULL, dev_cpu_dead);
11320 WARN_ON(rc < 0);
1da177e4
LT
11321 rc = 0;
11322out:
11323 return rc;
11324}
11325
11326subsys_initcall(net_dev_init);