]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/core/dev.c
net: use skb->csum_not_inet to identify packets needing crc32c
[mirror_ubuntu-hirsute-kernel.git] / net / core / dev.c
CommitLineData
1da177e4 1/*
722c9a0c 2 * NET3 Protocol independent device support routines.
1da177e4
LT
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
722c9a0c 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
21 *
22 * Changes:
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
722c9a0c 24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
1da177e4
LT
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
34 * drivers
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
722c9a0c 39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
1da177e4
LT
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
44 * call a packet.
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
722c9a0c 49 * Alan Cox : Fixed nasty side effect of device close
1da177e4
LT
50 * changes.
51 * Rudi Cilibrasi : Pass the right thing to
52 * set_mac_address()
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
58 * 1 device.
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
66 * the backlog queue.
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
722c9a0c 70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
1da177e4
LT
72 * - netif_rx() feedback
73 */
74
7c0f6ba6 75#include <linux/uaccess.h>
1da177e4 76#include <linux/bitops.h>
4fc268d2 77#include <linux/capability.h>
1da177e4
LT
78#include <linux/cpu.h>
79#include <linux/types.h>
80#include <linux/kernel.h>
08e9897d 81#include <linux/hash.h>
5a0e3ad6 82#include <linux/slab.h>
1da177e4 83#include <linux/sched.h>
f1083048 84#include <linux/sched/mm.h>
4a3e2f71 85#include <linux/mutex.h>
1da177e4
LT
86#include <linux/string.h>
87#include <linux/mm.h>
88#include <linux/socket.h>
89#include <linux/sockios.h>
90#include <linux/errno.h>
91#include <linux/interrupt.h>
92#include <linux/if_ether.h>
93#include <linux/netdevice.h>
94#include <linux/etherdevice.h>
0187bdfb 95#include <linux/ethtool.h>
1da177e4
LT
96#include <linux/notifier.h>
97#include <linux/skbuff.h>
a7862b45 98#include <linux/bpf.h>
b5cdae32 99#include <linux/bpf_trace.h>
457c4cbc 100#include <net/net_namespace.h>
1da177e4 101#include <net/sock.h>
02d62e86 102#include <net/busy_poll.h>
1da177e4 103#include <linux/rtnetlink.h>
1da177e4 104#include <linux/stat.h>
1da177e4 105#include <net/dst.h>
fc4099f1 106#include <net/dst_metadata.h>
1da177e4 107#include <net/pkt_sched.h>
87d83093 108#include <net/pkt_cls.h>
1da177e4 109#include <net/checksum.h>
44540960 110#include <net/xfrm.h>
1da177e4
LT
111#include <linux/highmem.h>
112#include <linux/init.h>
1da177e4 113#include <linux/module.h>
1da177e4
LT
114#include <linux/netpoll.h>
115#include <linux/rcupdate.h>
116#include <linux/delay.h>
1da177e4 117#include <net/iw_handler.h>
1da177e4 118#include <asm/current.h>
5bdb9886 119#include <linux/audit.h>
db217334 120#include <linux/dmaengine.h>
f6a78bfc 121#include <linux/err.h>
c7fa9d18 122#include <linux/ctype.h>
723e98b7 123#include <linux/if_arp.h>
6de329e2 124#include <linux/if_vlan.h>
8f0f2223 125#include <linux/ip.h>
ad55dcaf 126#include <net/ip.h>
25cd9ba0 127#include <net/mpls.h>
8f0f2223
DM
128#include <linux/ipv6.h>
129#include <linux/in.h>
b6b2fed1
DM
130#include <linux/jhash.h>
131#include <linux/random.h>
9cbc1cb8 132#include <trace/events/napi.h>
cf66ba58 133#include <trace/events/net.h>
07dc22e7 134#include <trace/events/skb.h>
5acbbd42 135#include <linux/pci.h>
caeda9b9 136#include <linux/inetdevice.h>
c445477d 137#include <linux/cpu_rmap.h>
c5905afb 138#include <linux/static_key.h>
af12fa6e 139#include <linux/hashtable.h>
60877a32 140#include <linux/vmalloc.h>
529d0489 141#include <linux/if_macvlan.h>
e7fd2885 142#include <linux/errqueue.h>
3b47d303 143#include <linux/hrtimer.h>
e687ad60 144#include <linux/netfilter_ingress.h>
40e4e713 145#include <linux/crash_dump.h>
b72b5bf6 146#include <linux/sctp.h>
1da177e4 147
342709ef
PE
148#include "net-sysfs.h"
149
d565b0a1
HX
150/* Instead of increasing this, you should create a hash table. */
151#define MAX_GRO_SKBS 8
152
5d38a079
HX
153/* This should be increased if a protocol with a bigger head is added. */
154#define GRO_MAX_HEAD (MAX_HEADER + 128)
155
1da177e4 156static DEFINE_SPINLOCK(ptype_lock);
62532da9 157static DEFINE_SPINLOCK(offload_lock);
900ff8c6
CW
158struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
159struct list_head ptype_all __read_mostly; /* Taps */
62532da9 160static struct list_head offload_base __read_mostly;
1da177e4 161
ae78dbfa 162static int netif_rx_internal(struct sk_buff *skb);
54951194
LP
163static int call_netdevice_notifiers_info(unsigned long val,
164 struct net_device *dev,
165 struct netdev_notifier_info *info);
ae78dbfa 166
1da177e4 167/*
7562f876 168 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
169 * semaphore.
170 *
c6d14c84 171 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
1da177e4
LT
172 *
173 * Writers must hold the rtnl semaphore while they loop through the
7562f876 174 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
175 * actual updates. This allows pure readers to access the list even
176 * while a writer is preparing to update it.
177 *
178 * To put it another way, dev_base_lock is held for writing only to
179 * protect against pure readers; the rtnl semaphore provides the
180 * protection against other writers.
181 *
182 * See, for example usages, register_netdevice() and
183 * unregister_netdevice(), which must be called with the rtnl
184 * semaphore held.
185 */
1da177e4 186DEFINE_RWLOCK(dev_base_lock);
1da177e4
LT
187EXPORT_SYMBOL(dev_base_lock);
188
af12fa6e
ET
189/* protects napi_hash addition/deletion and napi_gen_id */
190static DEFINE_SPINLOCK(napi_hash_lock);
191
52bd2d62 192static unsigned int napi_gen_id = NR_CPUS;
6180d9de 193static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
af12fa6e 194
18afa4b0 195static seqcount_t devnet_rename_seq;
c91f6df2 196
4e985ada
TG
197static inline void dev_base_seq_inc(struct net *net)
198{
643aa9cb 199 while (++net->dev_base_seq == 0)
200 ;
4e985ada
TG
201}
202
881d966b 203static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
1da177e4 204{
8387ff25 205 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
95c96174 206
08e9897d 207 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
1da177e4
LT
208}
209
881d966b 210static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
1da177e4 211{
7c28bd0b 212 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
1da177e4
LT
213}
214
e36fa2f7 215static inline void rps_lock(struct softnet_data *sd)
152102c7
CG
216{
217#ifdef CONFIG_RPS
e36fa2f7 218 spin_lock(&sd->input_pkt_queue.lock);
152102c7
CG
219#endif
220}
221
e36fa2f7 222static inline void rps_unlock(struct softnet_data *sd)
152102c7
CG
223{
224#ifdef CONFIG_RPS
e36fa2f7 225 spin_unlock(&sd->input_pkt_queue.lock);
152102c7
CG
226#endif
227}
228
ce286d32 229/* Device list insertion */
53759be9 230static void list_netdevice(struct net_device *dev)
ce286d32 231{
c346dca1 232 struct net *net = dev_net(dev);
ce286d32
EB
233
234 ASSERT_RTNL();
235
236 write_lock_bh(&dev_base_lock);
c6d14c84 237 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
72c9528b 238 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
fb699dfd
ED
239 hlist_add_head_rcu(&dev->index_hlist,
240 dev_index_hash(net, dev->ifindex));
ce286d32 241 write_unlock_bh(&dev_base_lock);
4e985ada
TG
242
243 dev_base_seq_inc(net);
ce286d32
EB
244}
245
fb699dfd
ED
246/* Device list removal
247 * caller must respect a RCU grace period before freeing/reusing dev
248 */
ce286d32
EB
249static void unlist_netdevice(struct net_device *dev)
250{
251 ASSERT_RTNL();
252
253 /* Unlink dev from the device chain */
254 write_lock_bh(&dev_base_lock);
c6d14c84 255 list_del_rcu(&dev->dev_list);
72c9528b 256 hlist_del_rcu(&dev->name_hlist);
fb699dfd 257 hlist_del_rcu(&dev->index_hlist);
ce286d32 258 write_unlock_bh(&dev_base_lock);
4e985ada
TG
259
260 dev_base_seq_inc(dev_net(dev));
ce286d32
EB
261}
262
1da177e4
LT
263/*
264 * Our notifier list
265 */
266
f07d5b94 267static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
268
269/*
270 * Device drivers call our routines to queue packets here. We empty the
271 * queue in the local softnet handler.
272 */
bea3348e 273
9958da05 274DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
d1b19dff 275EXPORT_PER_CPU_SYMBOL(softnet_data);
1da177e4 276
cf508b12 277#ifdef CONFIG_LOCKDEP
723e98b7 278/*
c773e847 279 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
723e98b7
JP
280 * according to dev->type
281 */
643aa9cb 282static const unsigned short netdev_lock_type[] = {
283 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
723e98b7
JP
284 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
285 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
286 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
287 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
288 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
289 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
290 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
291 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
292 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
293 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
294 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
211ed865
PG
295 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
296 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
297 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
723e98b7 298
643aa9cb 299static const char *const netdev_lock_name[] = {
300 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
301 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
302 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
303 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
304 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
305 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
306 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
307 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
308 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
309 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
310 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
311 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
312 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
313 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
314 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
723e98b7
JP
315
316static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
cf508b12 317static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
723e98b7
JP
318
319static inline unsigned short netdev_lock_pos(unsigned short dev_type)
320{
321 int i;
322
323 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
324 if (netdev_lock_type[i] == dev_type)
325 return i;
326 /* the last key is used by default */
327 return ARRAY_SIZE(netdev_lock_type) - 1;
328}
329
cf508b12
DM
330static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
331 unsigned short dev_type)
723e98b7
JP
332{
333 int i;
334
335 i = netdev_lock_pos(dev_type);
336 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
337 netdev_lock_name[i]);
338}
cf508b12
DM
339
340static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
341{
342 int i;
343
344 i = netdev_lock_pos(dev->type);
345 lockdep_set_class_and_name(&dev->addr_list_lock,
346 &netdev_addr_lock_key[i],
347 netdev_lock_name[i]);
348}
723e98b7 349#else
cf508b12
DM
350static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
351 unsigned short dev_type)
352{
353}
354static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
723e98b7
JP
355{
356}
357#endif
1da177e4
LT
358
359/*******************************************************************************
eb13da1a 360 *
361 * Protocol management and registration routines
362 *
363 *******************************************************************************/
1da177e4 364
1da177e4 365
1da177e4
LT
366/*
367 * Add a protocol ID to the list. Now that the input handler is
368 * smarter we can dispense with all the messy stuff that used to be
369 * here.
370 *
371 * BEWARE!!! Protocol handlers, mangling input packets,
372 * MUST BE last in hash buckets and checking protocol handlers
373 * MUST start from promiscuous ptype_all chain in net_bh.
374 * It is true now, do not change it.
375 * Explanation follows: if protocol handler, mangling packet, will
376 * be the first on list, it is not able to sense, that packet
377 * is cloned and should be copied-on-write, so that it will
378 * change it and subsequent readers will get broken packet.
379 * --ANK (980803)
380 */
381
c07b68e8
ED
382static inline struct list_head *ptype_head(const struct packet_type *pt)
383{
384 if (pt->type == htons(ETH_P_ALL))
7866a621 385 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
c07b68e8 386 else
7866a621
SN
387 return pt->dev ? &pt->dev->ptype_specific :
388 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
c07b68e8
ED
389}
390
1da177e4
LT
391/**
392 * dev_add_pack - add packet handler
393 * @pt: packet type declaration
394 *
395 * Add a protocol handler to the networking stack. The passed &packet_type
396 * is linked into kernel lists and may not be freed until it has been
397 * removed from the kernel lists.
398 *
4ec93edb 399 * This call does not sleep therefore it can not
1da177e4
LT
400 * guarantee all CPU's that are in middle of receiving packets
401 * will see the new packet type (until the next received packet).
402 */
403
404void dev_add_pack(struct packet_type *pt)
405{
c07b68e8 406 struct list_head *head = ptype_head(pt);
1da177e4 407
c07b68e8
ED
408 spin_lock(&ptype_lock);
409 list_add_rcu(&pt->list, head);
410 spin_unlock(&ptype_lock);
1da177e4 411}
d1b19dff 412EXPORT_SYMBOL(dev_add_pack);
1da177e4 413
1da177e4
LT
414/**
415 * __dev_remove_pack - remove packet handler
416 * @pt: packet type declaration
417 *
418 * Remove a protocol handler that was previously added to the kernel
419 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
420 * from the kernel lists and can be freed or reused once this function
4ec93edb 421 * returns.
1da177e4
LT
422 *
423 * The packet type might still be in use by receivers
424 * and must not be freed until after all the CPU's have gone
425 * through a quiescent state.
426 */
427void __dev_remove_pack(struct packet_type *pt)
428{
c07b68e8 429 struct list_head *head = ptype_head(pt);
1da177e4
LT
430 struct packet_type *pt1;
431
c07b68e8 432 spin_lock(&ptype_lock);
1da177e4
LT
433
434 list_for_each_entry(pt1, head, list) {
435 if (pt == pt1) {
436 list_del_rcu(&pt->list);
437 goto out;
438 }
439 }
440
7b6cd1ce 441 pr_warn("dev_remove_pack: %p not found\n", pt);
1da177e4 442out:
c07b68e8 443 spin_unlock(&ptype_lock);
1da177e4 444}
d1b19dff
ED
445EXPORT_SYMBOL(__dev_remove_pack);
446
1da177e4
LT
447/**
448 * dev_remove_pack - remove packet handler
449 * @pt: packet type declaration
450 *
451 * Remove a protocol handler that was previously added to the kernel
452 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
453 * from the kernel lists and can be freed or reused once this function
454 * returns.
455 *
456 * This call sleeps to guarantee that no CPU is looking at the packet
457 * type after return.
458 */
459void dev_remove_pack(struct packet_type *pt)
460{
461 __dev_remove_pack(pt);
4ec93edb 462
1da177e4
LT
463 synchronize_net();
464}
d1b19dff 465EXPORT_SYMBOL(dev_remove_pack);
1da177e4 466
62532da9
VY
467
468/**
469 * dev_add_offload - register offload handlers
470 * @po: protocol offload declaration
471 *
472 * Add protocol offload handlers to the networking stack. The passed
473 * &proto_offload is linked into kernel lists and may not be freed until
474 * it has been removed from the kernel lists.
475 *
476 * This call does not sleep therefore it can not
477 * guarantee all CPU's that are in middle of receiving packets
478 * will see the new offload handlers (until the next received packet).
479 */
480void dev_add_offload(struct packet_offload *po)
481{
bdef7de4 482 struct packet_offload *elem;
62532da9
VY
483
484 spin_lock(&offload_lock);
bdef7de4
DM
485 list_for_each_entry(elem, &offload_base, list) {
486 if (po->priority < elem->priority)
487 break;
488 }
489 list_add_rcu(&po->list, elem->list.prev);
62532da9
VY
490 spin_unlock(&offload_lock);
491}
492EXPORT_SYMBOL(dev_add_offload);
493
494/**
495 * __dev_remove_offload - remove offload handler
496 * @po: packet offload declaration
497 *
498 * Remove a protocol offload handler that was previously added to the
499 * kernel offload handlers by dev_add_offload(). The passed &offload_type
500 * is removed from the kernel lists and can be freed or reused once this
501 * function returns.
502 *
503 * The packet type might still be in use by receivers
504 * and must not be freed until after all the CPU's have gone
505 * through a quiescent state.
506 */
1d143d9f 507static void __dev_remove_offload(struct packet_offload *po)
62532da9
VY
508{
509 struct list_head *head = &offload_base;
510 struct packet_offload *po1;
511
c53aa505 512 spin_lock(&offload_lock);
62532da9
VY
513
514 list_for_each_entry(po1, head, list) {
515 if (po == po1) {
516 list_del_rcu(&po->list);
517 goto out;
518 }
519 }
520
521 pr_warn("dev_remove_offload: %p not found\n", po);
522out:
c53aa505 523 spin_unlock(&offload_lock);
62532da9 524}
62532da9
VY
525
526/**
527 * dev_remove_offload - remove packet offload handler
528 * @po: packet offload declaration
529 *
530 * Remove a packet offload handler that was previously added to the kernel
531 * offload handlers by dev_add_offload(). The passed &offload_type is
532 * removed from the kernel lists and can be freed or reused once this
533 * function returns.
534 *
535 * This call sleeps to guarantee that no CPU is looking at the packet
536 * type after return.
537 */
538void dev_remove_offload(struct packet_offload *po)
539{
540 __dev_remove_offload(po);
541
542 synchronize_net();
543}
544EXPORT_SYMBOL(dev_remove_offload);
545
1da177e4 546/******************************************************************************
eb13da1a 547 *
548 * Device Boot-time Settings Routines
549 *
550 ******************************************************************************/
1da177e4
LT
551
552/* Boot time configuration table */
553static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
554
555/**
556 * netdev_boot_setup_add - add new setup entry
557 * @name: name of the device
558 * @map: configured settings for the device
559 *
560 * Adds new setup entry to the dev_boot_setup list. The function
561 * returns 0 on error and 1 on success. This is a generic routine to
562 * all netdevices.
563 */
564static int netdev_boot_setup_add(char *name, struct ifmap *map)
565{
566 struct netdev_boot_setup *s;
567 int i;
568
569 s = dev_boot_setup;
570 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
571 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
572 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 573 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
574 memcpy(&s[i].map, map, sizeof(s[i].map));
575 break;
576 }
577 }
578
579 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
580}
581
582/**
722c9a0c 583 * netdev_boot_setup_check - check boot time settings
584 * @dev: the netdevice
1da177e4 585 *
722c9a0c 586 * Check boot time settings for the device.
587 * The found settings are set for the device to be used
588 * later in the device probing.
589 * Returns 0 if no settings found, 1 if they are.
1da177e4
LT
590 */
591int netdev_boot_setup_check(struct net_device *dev)
592{
593 struct netdev_boot_setup *s = dev_boot_setup;
594 int i;
595
596 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
597 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 598 !strcmp(dev->name, s[i].name)) {
722c9a0c 599 dev->irq = s[i].map.irq;
600 dev->base_addr = s[i].map.base_addr;
601 dev->mem_start = s[i].map.mem_start;
602 dev->mem_end = s[i].map.mem_end;
1da177e4
LT
603 return 1;
604 }
605 }
606 return 0;
607}
d1b19dff 608EXPORT_SYMBOL(netdev_boot_setup_check);
1da177e4
LT
609
610
611/**
722c9a0c 612 * netdev_boot_base - get address from boot time settings
613 * @prefix: prefix for network device
614 * @unit: id for network device
615 *
616 * Check boot time settings for the base address of device.
617 * The found settings are set for the device to be used
618 * later in the device probing.
619 * Returns 0 if no settings found.
1da177e4
LT
620 */
621unsigned long netdev_boot_base(const char *prefix, int unit)
622{
623 const struct netdev_boot_setup *s = dev_boot_setup;
624 char name[IFNAMSIZ];
625 int i;
626
627 sprintf(name, "%s%d", prefix, unit);
628
629 /*
630 * If device already registered then return base of 1
631 * to indicate not to probe for this interface
632 */
881d966b 633 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
634 return 1;
635
636 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
637 if (!strcmp(name, s[i].name))
638 return s[i].map.base_addr;
639 return 0;
640}
641
642/*
643 * Saves at boot time configured settings for any netdevice.
644 */
645int __init netdev_boot_setup(char *str)
646{
647 int ints[5];
648 struct ifmap map;
649
650 str = get_options(str, ARRAY_SIZE(ints), ints);
651 if (!str || !*str)
652 return 0;
653
654 /* Save settings */
655 memset(&map, 0, sizeof(map));
656 if (ints[0] > 0)
657 map.irq = ints[1];
658 if (ints[0] > 1)
659 map.base_addr = ints[2];
660 if (ints[0] > 2)
661 map.mem_start = ints[3];
662 if (ints[0] > 3)
663 map.mem_end = ints[4];
664
665 /* Add new entry to the list */
666 return netdev_boot_setup_add(str, &map);
667}
668
669__setup("netdev=", netdev_boot_setup);
670
671/*******************************************************************************
eb13da1a 672 *
673 * Device Interface Subroutines
674 *
675 *******************************************************************************/
1da177e4 676
a54acb3a
ND
677/**
678 * dev_get_iflink - get 'iflink' value of a interface
679 * @dev: targeted interface
680 *
681 * Indicates the ifindex the interface is linked to.
682 * Physical interfaces have the same 'ifindex' and 'iflink' values.
683 */
684
685int dev_get_iflink(const struct net_device *dev)
686{
687 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
688 return dev->netdev_ops->ndo_get_iflink(dev);
689
7a66bbc9 690 return dev->ifindex;
a54acb3a
ND
691}
692EXPORT_SYMBOL(dev_get_iflink);
693
fc4099f1
PS
694/**
695 * dev_fill_metadata_dst - Retrieve tunnel egress information.
696 * @dev: targeted interface
697 * @skb: The packet.
698 *
699 * For better visibility of tunnel traffic OVS needs to retrieve
700 * egress tunnel information for a packet. Following API allows
701 * user to get this info.
702 */
703int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
704{
705 struct ip_tunnel_info *info;
706
707 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
708 return -EINVAL;
709
710 info = skb_tunnel_info_unclone(skb);
711 if (!info)
712 return -ENOMEM;
713 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
714 return -EINVAL;
715
716 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
717}
718EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
719
1da177e4
LT
720/**
721 * __dev_get_by_name - find a device by its name
c4ea43c5 722 * @net: the applicable net namespace
1da177e4
LT
723 * @name: name to find
724 *
725 * Find an interface by name. Must be called under RTNL semaphore
726 * or @dev_base_lock. If the name is found a pointer to the device
727 * is returned. If the name is not found then %NULL is returned. The
728 * reference counters are not incremented so the caller must be
729 * careful with locks.
730 */
731
881d966b 732struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4 733{
0bd8d536
ED
734 struct net_device *dev;
735 struct hlist_head *head = dev_name_hash(net, name);
1da177e4 736
b67bfe0d 737 hlist_for_each_entry(dev, head, name_hlist)
1da177e4
LT
738 if (!strncmp(dev->name, name, IFNAMSIZ))
739 return dev;
0bd8d536 740
1da177e4
LT
741 return NULL;
742}
d1b19dff 743EXPORT_SYMBOL(__dev_get_by_name);
1da177e4 744
72c9528b 745/**
722c9a0c 746 * dev_get_by_name_rcu - find a device by its name
747 * @net: the applicable net namespace
748 * @name: name to find
749 *
750 * Find an interface by name.
751 * If the name is found a pointer to the device is returned.
752 * If the name is not found then %NULL is returned.
753 * The reference counters are not incremented so the caller must be
754 * careful with locks. The caller must hold RCU lock.
72c9528b
ED
755 */
756
757struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
758{
72c9528b
ED
759 struct net_device *dev;
760 struct hlist_head *head = dev_name_hash(net, name);
761
b67bfe0d 762 hlist_for_each_entry_rcu(dev, head, name_hlist)
72c9528b
ED
763 if (!strncmp(dev->name, name, IFNAMSIZ))
764 return dev;
765
766 return NULL;
767}
768EXPORT_SYMBOL(dev_get_by_name_rcu);
769
1da177e4
LT
770/**
771 * dev_get_by_name - find a device by its name
c4ea43c5 772 * @net: the applicable net namespace
1da177e4
LT
773 * @name: name to find
774 *
775 * Find an interface by name. This can be called from any
776 * context and does its own locking. The returned handle has
777 * the usage count incremented and the caller must use dev_put() to
778 * release it when it is no longer needed. %NULL is returned if no
779 * matching device is found.
780 */
781
881d966b 782struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
783{
784 struct net_device *dev;
785
72c9528b
ED
786 rcu_read_lock();
787 dev = dev_get_by_name_rcu(net, name);
1da177e4
LT
788 if (dev)
789 dev_hold(dev);
72c9528b 790 rcu_read_unlock();
1da177e4
LT
791 return dev;
792}
d1b19dff 793EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
794
795/**
796 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 797 * @net: the applicable net namespace
1da177e4
LT
798 * @ifindex: index of device
799 *
800 * Search for an interface by index. Returns %NULL if the device
801 * is not found or a pointer to the device. The device has not
802 * had its reference counter increased so the caller must be careful
803 * about locking. The caller must hold either the RTNL semaphore
804 * or @dev_base_lock.
805 */
806
881d966b 807struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4 808{
0bd8d536
ED
809 struct net_device *dev;
810 struct hlist_head *head = dev_index_hash(net, ifindex);
1da177e4 811
b67bfe0d 812 hlist_for_each_entry(dev, head, index_hlist)
1da177e4
LT
813 if (dev->ifindex == ifindex)
814 return dev;
0bd8d536 815
1da177e4
LT
816 return NULL;
817}
d1b19dff 818EXPORT_SYMBOL(__dev_get_by_index);
1da177e4 819
fb699dfd
ED
820/**
821 * dev_get_by_index_rcu - find a device by its ifindex
822 * @net: the applicable net namespace
823 * @ifindex: index of device
824 *
825 * Search for an interface by index. Returns %NULL if the device
826 * is not found or a pointer to the device. The device has not
827 * had its reference counter increased so the caller must be careful
828 * about locking. The caller must hold RCU lock.
829 */
830
831struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
832{
fb699dfd
ED
833 struct net_device *dev;
834 struct hlist_head *head = dev_index_hash(net, ifindex);
835
b67bfe0d 836 hlist_for_each_entry_rcu(dev, head, index_hlist)
fb699dfd
ED
837 if (dev->ifindex == ifindex)
838 return dev;
839
840 return NULL;
841}
842EXPORT_SYMBOL(dev_get_by_index_rcu);
843
1da177e4
LT
844
845/**
846 * dev_get_by_index - find a device by its ifindex
c4ea43c5 847 * @net: the applicable net namespace
1da177e4
LT
848 * @ifindex: index of device
849 *
850 * Search for an interface by index. Returns NULL if the device
851 * is not found or a pointer to the device. The device returned has
852 * had a reference added and the pointer is safe until the user calls
853 * dev_put to indicate they have finished with it.
854 */
855
881d966b 856struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
857{
858 struct net_device *dev;
859
fb699dfd
ED
860 rcu_read_lock();
861 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
862 if (dev)
863 dev_hold(dev);
fb699dfd 864 rcu_read_unlock();
1da177e4
LT
865 return dev;
866}
d1b19dff 867EXPORT_SYMBOL(dev_get_by_index);
1da177e4 868
5dbe7c17
NS
869/**
870 * netdev_get_name - get a netdevice name, knowing its ifindex.
871 * @net: network namespace
872 * @name: a pointer to the buffer where the name will be stored.
873 * @ifindex: the ifindex of the interface to get the name from.
874 *
875 * The use of raw_seqcount_begin() and cond_resched() before
876 * retrying is required as we want to give the writers a chance
877 * to complete when CONFIG_PREEMPT is not set.
878 */
879int netdev_get_name(struct net *net, char *name, int ifindex)
880{
881 struct net_device *dev;
882 unsigned int seq;
883
884retry:
885 seq = raw_seqcount_begin(&devnet_rename_seq);
886 rcu_read_lock();
887 dev = dev_get_by_index_rcu(net, ifindex);
888 if (!dev) {
889 rcu_read_unlock();
890 return -ENODEV;
891 }
892
893 strcpy(name, dev->name);
894 rcu_read_unlock();
895 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
896 cond_resched();
897 goto retry;
898 }
899
900 return 0;
901}
902
1da177e4 903/**
941666c2 904 * dev_getbyhwaddr_rcu - find a device by its hardware address
c4ea43c5 905 * @net: the applicable net namespace
1da177e4
LT
906 * @type: media type of device
907 * @ha: hardware address
908 *
909 * Search for an interface by MAC address. Returns NULL if the device
c506653d
ED
910 * is not found or a pointer to the device.
911 * The caller must hold RCU or RTNL.
941666c2 912 * The returned device has not had its ref count increased
1da177e4
LT
913 * and the caller must therefore be careful about locking
914 *
1da177e4
LT
915 */
916
941666c2
ED
917struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
918 const char *ha)
1da177e4
LT
919{
920 struct net_device *dev;
921
941666c2 922 for_each_netdev_rcu(net, dev)
1da177e4
LT
923 if (dev->type == type &&
924 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
925 return dev;
926
927 return NULL;
1da177e4 928}
941666c2 929EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
cf309e3f 930
881d966b 931struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1da177e4
LT
932{
933 struct net_device *dev;
934
4e9cac2b 935 ASSERT_RTNL();
881d966b 936 for_each_netdev(net, dev)
4e9cac2b 937 if (dev->type == type)
7562f876
PE
938 return dev;
939
940 return NULL;
4e9cac2b 941}
4e9cac2b
PM
942EXPORT_SYMBOL(__dev_getfirstbyhwtype);
943
881d966b 944struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b 945{
99fe3c39 946 struct net_device *dev, *ret = NULL;
4e9cac2b 947
99fe3c39
ED
948 rcu_read_lock();
949 for_each_netdev_rcu(net, dev)
950 if (dev->type == type) {
951 dev_hold(dev);
952 ret = dev;
953 break;
954 }
955 rcu_read_unlock();
956 return ret;
1da177e4 957}
1da177e4
LT
958EXPORT_SYMBOL(dev_getfirstbyhwtype);
959
960/**
6c555490 961 * __dev_get_by_flags - find any device with given flags
c4ea43c5 962 * @net: the applicable net namespace
1da177e4
LT
963 * @if_flags: IFF_* values
964 * @mask: bitmask of bits in if_flags to check
965 *
966 * Search for any interface with the given flags. Returns NULL if a device
bb69ae04 967 * is not found or a pointer to the device. Must be called inside
6c555490 968 * rtnl_lock(), and result refcount is unchanged.
1da177e4
LT
969 */
970
6c555490
WC
971struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
972 unsigned short mask)
1da177e4 973{
7562f876 974 struct net_device *dev, *ret;
1da177e4 975
6c555490
WC
976 ASSERT_RTNL();
977
7562f876 978 ret = NULL;
6c555490 979 for_each_netdev(net, dev) {
1da177e4 980 if (((dev->flags ^ if_flags) & mask) == 0) {
7562f876 981 ret = dev;
1da177e4
LT
982 break;
983 }
984 }
7562f876 985 return ret;
1da177e4 986}
6c555490 987EXPORT_SYMBOL(__dev_get_by_flags);
1da177e4
LT
988
989/**
990 * dev_valid_name - check if name is okay for network device
991 * @name: name string
992 *
993 * Network device names need to be valid file names to
c7fa9d18
DM
994 * to allow sysfs to work. We also disallow any kind of
995 * whitespace.
1da177e4 996 */
95f050bf 997bool dev_valid_name(const char *name)
1da177e4 998{
c7fa9d18 999 if (*name == '\0')
95f050bf 1000 return false;
b6fe17d6 1001 if (strlen(name) >= IFNAMSIZ)
95f050bf 1002 return false;
c7fa9d18 1003 if (!strcmp(name, ".") || !strcmp(name, ".."))
95f050bf 1004 return false;
c7fa9d18
DM
1005
1006 while (*name) {
a4176a93 1007 if (*name == '/' || *name == ':' || isspace(*name))
95f050bf 1008 return false;
c7fa9d18
DM
1009 name++;
1010 }
95f050bf 1011 return true;
1da177e4 1012}
d1b19dff 1013EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
1014
1015/**
b267b179
EB
1016 * __dev_alloc_name - allocate a name for a device
1017 * @net: network namespace to allocate the device name in
1da177e4 1018 * @name: name format string
b267b179 1019 * @buf: scratch buffer and result name string
1da177e4
LT
1020 *
1021 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
1022 * id. It scans list of devices to build up a free map, then chooses
1023 * the first empty slot. The caller must hold the dev_base or rtnl lock
1024 * while allocating the name and adding the device in order to avoid
1025 * duplicates.
1026 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1027 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
1028 */
1029
b267b179 1030static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
1031{
1032 int i = 0;
1da177e4
LT
1033 const char *p;
1034 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 1035 unsigned long *inuse;
1da177e4
LT
1036 struct net_device *d;
1037
1038 p = strnchr(name, IFNAMSIZ-1, '%');
1039 if (p) {
1040 /*
1041 * Verify the string as this thing may have come from
1042 * the user. There must be either one "%d" and no other "%"
1043 * characters.
1044 */
1045 if (p[1] != 'd' || strchr(p + 2, '%'))
1046 return -EINVAL;
1047
1048 /* Use one page as a bit array of possible slots */
cfcabdcc 1049 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
1050 if (!inuse)
1051 return -ENOMEM;
1052
881d966b 1053 for_each_netdev(net, d) {
1da177e4
LT
1054 if (!sscanf(d->name, name, &i))
1055 continue;
1056 if (i < 0 || i >= max_netdevices)
1057 continue;
1058
1059 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 1060 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
1061 if (!strncmp(buf, d->name, IFNAMSIZ))
1062 set_bit(i, inuse);
1063 }
1064
1065 i = find_first_zero_bit(inuse, max_netdevices);
1066 free_page((unsigned long) inuse);
1067 }
1068
d9031024
OP
1069 if (buf != name)
1070 snprintf(buf, IFNAMSIZ, name, i);
b267b179 1071 if (!__dev_get_by_name(net, buf))
1da177e4 1072 return i;
1da177e4
LT
1073
1074 /* It is possible to run out of possible slots
1075 * when the name is long and there isn't enough space left
1076 * for the digits, or if all bits are used.
1077 */
1078 return -ENFILE;
1079}
1080
b267b179
EB
1081/**
1082 * dev_alloc_name - allocate a name for a device
1083 * @dev: device
1084 * @name: name format string
1085 *
1086 * Passed a format string - eg "lt%d" it will try and find a suitable
1087 * id. It scans list of devices to build up a free map, then chooses
1088 * the first empty slot. The caller must hold the dev_base or rtnl lock
1089 * while allocating the name and adding the device in order to avoid
1090 * duplicates.
1091 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1092 * Returns the number of the unit assigned or a negative errno code.
1093 */
1094
1095int dev_alloc_name(struct net_device *dev, const char *name)
1096{
1097 char buf[IFNAMSIZ];
1098 struct net *net;
1099 int ret;
1100
c346dca1
YH
1101 BUG_ON(!dev_net(dev));
1102 net = dev_net(dev);
b267b179
EB
1103 ret = __dev_alloc_name(net, name, buf);
1104 if (ret >= 0)
1105 strlcpy(dev->name, buf, IFNAMSIZ);
1106 return ret;
1107}
d1b19dff 1108EXPORT_SYMBOL(dev_alloc_name);
b267b179 1109
828de4f6
G
1110static int dev_alloc_name_ns(struct net *net,
1111 struct net_device *dev,
1112 const char *name)
d9031024 1113{
828de4f6
G
1114 char buf[IFNAMSIZ];
1115 int ret;
8ce6cebc 1116
828de4f6
G
1117 ret = __dev_alloc_name(net, name, buf);
1118 if (ret >= 0)
1119 strlcpy(dev->name, buf, IFNAMSIZ);
1120 return ret;
1121}
1122
1123static int dev_get_valid_name(struct net *net,
1124 struct net_device *dev,
1125 const char *name)
1126{
1127 BUG_ON(!net);
8ce6cebc 1128
d9031024
OP
1129 if (!dev_valid_name(name))
1130 return -EINVAL;
1131
1c5cae81 1132 if (strchr(name, '%'))
828de4f6 1133 return dev_alloc_name_ns(net, dev, name);
d9031024
OP
1134 else if (__dev_get_by_name(net, name))
1135 return -EEXIST;
8ce6cebc
DL
1136 else if (dev->name != name)
1137 strlcpy(dev->name, name, IFNAMSIZ);
d9031024
OP
1138
1139 return 0;
1140}
1da177e4
LT
1141
1142/**
1143 * dev_change_name - change name of a device
1144 * @dev: device
1145 * @newname: name (or format string) must be at least IFNAMSIZ
1146 *
1147 * Change name of a device, can pass format strings "eth%d".
1148 * for wildcarding.
1149 */
cf04a4c7 1150int dev_change_name(struct net_device *dev, const char *newname)
1da177e4 1151{
238fa362 1152 unsigned char old_assign_type;
fcc5a03a 1153 char oldname[IFNAMSIZ];
1da177e4 1154 int err = 0;
fcc5a03a 1155 int ret;
881d966b 1156 struct net *net;
1da177e4
LT
1157
1158 ASSERT_RTNL();
c346dca1 1159 BUG_ON(!dev_net(dev));
1da177e4 1160
c346dca1 1161 net = dev_net(dev);
1da177e4
LT
1162 if (dev->flags & IFF_UP)
1163 return -EBUSY;
1164
30e6c9fa 1165 write_seqcount_begin(&devnet_rename_seq);
c91f6df2
BH
1166
1167 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
30e6c9fa 1168 write_seqcount_end(&devnet_rename_seq);
c8d90dca 1169 return 0;
c91f6df2 1170 }
c8d90dca 1171
fcc5a03a
HX
1172 memcpy(oldname, dev->name, IFNAMSIZ);
1173
828de4f6 1174 err = dev_get_valid_name(net, dev, newname);
c91f6df2 1175 if (err < 0) {
30e6c9fa 1176 write_seqcount_end(&devnet_rename_seq);
d9031024 1177 return err;
c91f6df2 1178 }
1da177e4 1179
6fe82a39
VF
1180 if (oldname[0] && !strchr(oldname, '%'))
1181 netdev_info(dev, "renamed from %s\n", oldname);
1182
238fa362
TG
1183 old_assign_type = dev->name_assign_type;
1184 dev->name_assign_type = NET_NAME_RENAMED;
1185
fcc5a03a 1186rollback:
a1b3f594
EB
1187 ret = device_rename(&dev->dev, dev->name);
1188 if (ret) {
1189 memcpy(dev->name, oldname, IFNAMSIZ);
238fa362 1190 dev->name_assign_type = old_assign_type;
30e6c9fa 1191 write_seqcount_end(&devnet_rename_seq);
a1b3f594 1192 return ret;
dcc99773 1193 }
7f988eab 1194
30e6c9fa 1195 write_seqcount_end(&devnet_rename_seq);
c91f6df2 1196
5bb025fa
VF
1197 netdev_adjacent_rename_links(dev, oldname);
1198
7f988eab 1199 write_lock_bh(&dev_base_lock);
372b2312 1200 hlist_del_rcu(&dev->name_hlist);
72c9528b
ED
1201 write_unlock_bh(&dev_base_lock);
1202
1203 synchronize_rcu();
1204
1205 write_lock_bh(&dev_base_lock);
1206 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
7f988eab
HX
1207 write_unlock_bh(&dev_base_lock);
1208
056925ab 1209 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
1210 ret = notifier_to_errno(ret);
1211
1212 if (ret) {
91e9c07b
ED
1213 /* err >= 0 after dev_alloc_name() or stores the first errno */
1214 if (err >= 0) {
fcc5a03a 1215 err = ret;
30e6c9fa 1216 write_seqcount_begin(&devnet_rename_seq);
fcc5a03a 1217 memcpy(dev->name, oldname, IFNAMSIZ);
5bb025fa 1218 memcpy(oldname, newname, IFNAMSIZ);
238fa362
TG
1219 dev->name_assign_type = old_assign_type;
1220 old_assign_type = NET_NAME_RENAMED;
fcc5a03a 1221 goto rollback;
91e9c07b 1222 } else {
7b6cd1ce 1223 pr_err("%s: name change rollback failed: %d\n",
91e9c07b 1224 dev->name, ret);
fcc5a03a
HX
1225 }
1226 }
1da177e4
LT
1227
1228 return err;
1229}
1230
0b815a1a
SH
1231/**
1232 * dev_set_alias - change ifalias of a device
1233 * @dev: device
1234 * @alias: name up to IFALIASZ
f0db275a 1235 * @len: limit of bytes to copy from info
0b815a1a
SH
1236 *
1237 * Set ifalias for a device,
1238 */
1239int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1240{
7364e445
AK
1241 char *new_ifalias;
1242
0b815a1a
SH
1243 ASSERT_RTNL();
1244
1245 if (len >= IFALIASZ)
1246 return -EINVAL;
1247
96ca4a2c 1248 if (!len) {
388dfc2d
SK
1249 kfree(dev->ifalias);
1250 dev->ifalias = NULL;
96ca4a2c
OH
1251 return 0;
1252 }
1253
7364e445
AK
1254 new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1255 if (!new_ifalias)
0b815a1a 1256 return -ENOMEM;
7364e445 1257 dev->ifalias = new_ifalias;
0b815a1a
SH
1258
1259 strlcpy(dev->ifalias, alias, len+1);
1260 return len;
1261}
1262
1263
d8a33ac4 1264/**
3041a069 1265 * netdev_features_change - device changes features
d8a33ac4
SH
1266 * @dev: device to cause notification
1267 *
1268 * Called to indicate a device has changed features.
1269 */
1270void netdev_features_change(struct net_device *dev)
1271{
056925ab 1272 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
1273}
1274EXPORT_SYMBOL(netdev_features_change);
1275
1da177e4
LT
1276/**
1277 * netdev_state_change - device changes state
1278 * @dev: device to cause notification
1279 *
1280 * Called to indicate a device has changed state. This function calls
1281 * the notifier chains for netdev_chain and sends a NEWLINK message
1282 * to the routing socket.
1283 */
1284void netdev_state_change(struct net_device *dev)
1285{
1286 if (dev->flags & IFF_UP) {
54951194
LP
1287 struct netdev_notifier_change_info change_info;
1288
1289 change_info.flags_changed = 0;
1290 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
1291 &change_info.info);
7f294054 1292 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1da177e4
LT
1293 }
1294}
d1b19dff 1295EXPORT_SYMBOL(netdev_state_change);
1da177e4 1296
ee89bab1 1297/**
722c9a0c 1298 * netdev_notify_peers - notify network peers about existence of @dev
1299 * @dev: network device
ee89bab1
AW
1300 *
1301 * Generate traffic such that interested network peers are aware of
1302 * @dev, such as by generating a gratuitous ARP. This may be used when
1303 * a device wants to inform the rest of the network about some sort of
1304 * reconfiguration such as a failover event or virtual machine
1305 * migration.
1306 */
1307void netdev_notify_peers(struct net_device *dev)
c1da4ac7 1308{
ee89bab1
AW
1309 rtnl_lock();
1310 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
37c343b4 1311 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
ee89bab1 1312 rtnl_unlock();
c1da4ac7 1313}
ee89bab1 1314EXPORT_SYMBOL(netdev_notify_peers);
c1da4ac7 1315
bd380811 1316static int __dev_open(struct net_device *dev)
1da177e4 1317{
d314774c 1318 const struct net_device_ops *ops = dev->netdev_ops;
3b8bcfd5 1319 int ret;
1da177e4 1320
e46b66bc
BH
1321 ASSERT_RTNL();
1322
1da177e4
LT
1323 if (!netif_device_present(dev))
1324 return -ENODEV;
1325
ca99ca14
NH
1326 /* Block netpoll from trying to do any rx path servicing.
1327 * If we don't do this there is a chance ndo_poll_controller
1328 * or ndo_poll may be running while we open the device
1329 */
66b5552f 1330 netpoll_poll_disable(dev);
ca99ca14 1331
3b8bcfd5
JB
1332 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1333 ret = notifier_to_errno(ret);
1334 if (ret)
1335 return ret;
1336
1da177e4 1337 set_bit(__LINK_STATE_START, &dev->state);
bada339b 1338
d314774c
SH
1339 if (ops->ndo_validate_addr)
1340 ret = ops->ndo_validate_addr(dev);
bada339b 1341
d314774c
SH
1342 if (!ret && ops->ndo_open)
1343 ret = ops->ndo_open(dev);
1da177e4 1344
66b5552f 1345 netpoll_poll_enable(dev);
ca99ca14 1346
bada339b
JG
1347 if (ret)
1348 clear_bit(__LINK_STATE_START, &dev->state);
1349 else {
1da177e4 1350 dev->flags |= IFF_UP;
4417da66 1351 dev_set_rx_mode(dev);
1da177e4 1352 dev_activate(dev);
7bf23575 1353 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 1354 }
bada339b 1355
1da177e4
LT
1356 return ret;
1357}
1358
1359/**
bd380811
PM
1360 * dev_open - prepare an interface for use.
1361 * @dev: device to open
1da177e4 1362 *
bd380811
PM
1363 * Takes a device from down to up state. The device's private open
1364 * function is invoked and then the multicast lists are loaded. Finally
1365 * the device is moved into the up state and a %NETDEV_UP message is
1366 * sent to the netdev notifier chain.
1367 *
1368 * Calling this function on an active interface is a nop. On a failure
1369 * a negative errno code is returned.
1da177e4 1370 */
bd380811
PM
1371int dev_open(struct net_device *dev)
1372{
1373 int ret;
1374
bd380811
PM
1375 if (dev->flags & IFF_UP)
1376 return 0;
1377
bd380811
PM
1378 ret = __dev_open(dev);
1379 if (ret < 0)
1380 return ret;
1381
7f294054 1382 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
bd380811
PM
1383 call_netdevice_notifiers(NETDEV_UP, dev);
1384
1385 return ret;
1386}
1387EXPORT_SYMBOL(dev_open);
1388
44345724 1389static int __dev_close_many(struct list_head *head)
1da177e4 1390{
44345724 1391 struct net_device *dev;
e46b66bc 1392
bd380811 1393 ASSERT_RTNL();
9d5010db
DM
1394 might_sleep();
1395
5cde2829 1396 list_for_each_entry(dev, head, close_list) {
3f4df206 1397 /* Temporarily disable netpoll until the interface is down */
66b5552f 1398 netpoll_poll_disable(dev);
3f4df206 1399
44345724 1400 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1401
44345724 1402 clear_bit(__LINK_STATE_START, &dev->state);
1da177e4 1403
44345724
OP
1404 /* Synchronize to scheduled poll. We cannot touch poll list, it
1405 * can be even on different cpu. So just clear netif_running().
1406 *
1407 * dev->stop() will invoke napi_disable() on all of it's
1408 * napi_struct instances on this device.
1409 */
4e857c58 1410 smp_mb__after_atomic(); /* Commit netif_running(). */
44345724 1411 }
1da177e4 1412
44345724 1413 dev_deactivate_many(head);
d8b2a4d2 1414
5cde2829 1415 list_for_each_entry(dev, head, close_list) {
44345724 1416 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4 1417
44345724
OP
1418 /*
1419 * Call the device specific close. This cannot fail.
1420 * Only if device is UP
1421 *
1422 * We allow it to be called even after a DETACH hot-plug
1423 * event.
1424 */
1425 if (ops->ndo_stop)
1426 ops->ndo_stop(dev);
1427
44345724 1428 dev->flags &= ~IFF_UP;
66b5552f 1429 netpoll_poll_enable(dev);
44345724
OP
1430 }
1431
1432 return 0;
1433}
1434
1435static int __dev_close(struct net_device *dev)
1436{
f87e6f47 1437 int retval;
44345724
OP
1438 LIST_HEAD(single);
1439
5cde2829 1440 list_add(&dev->close_list, &single);
f87e6f47
LT
1441 retval = __dev_close_many(&single);
1442 list_del(&single);
ca99ca14 1443
f87e6f47 1444 return retval;
44345724
OP
1445}
1446
99c4a26a 1447int dev_close_many(struct list_head *head, bool unlink)
44345724
OP
1448{
1449 struct net_device *dev, *tmp;
1da177e4 1450
5cde2829
EB
1451 /* Remove the devices that don't need to be closed */
1452 list_for_each_entry_safe(dev, tmp, head, close_list)
44345724 1453 if (!(dev->flags & IFF_UP))
5cde2829 1454 list_del_init(&dev->close_list);
44345724
OP
1455
1456 __dev_close_many(head);
1da177e4 1457
5cde2829 1458 list_for_each_entry_safe(dev, tmp, head, close_list) {
7f294054 1459 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
44345724 1460 call_netdevice_notifiers(NETDEV_DOWN, dev);
99c4a26a
DM
1461 if (unlink)
1462 list_del_init(&dev->close_list);
44345724 1463 }
bd380811
PM
1464
1465 return 0;
1466}
99c4a26a 1467EXPORT_SYMBOL(dev_close_many);
bd380811
PM
1468
1469/**
1470 * dev_close - shutdown an interface.
1471 * @dev: device to shutdown
1472 *
1473 * This function moves an active device into down state. A
1474 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1475 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1476 * chain.
1477 */
1478int dev_close(struct net_device *dev)
1479{
e14a5993
ED
1480 if (dev->flags & IFF_UP) {
1481 LIST_HEAD(single);
1da177e4 1482
5cde2829 1483 list_add(&dev->close_list, &single);
99c4a26a 1484 dev_close_many(&single, true);
e14a5993
ED
1485 list_del(&single);
1486 }
da6e378b 1487 return 0;
1da177e4 1488}
d1b19dff 1489EXPORT_SYMBOL(dev_close);
1da177e4
LT
1490
1491
0187bdfb
BH
1492/**
1493 * dev_disable_lro - disable Large Receive Offload on a device
1494 * @dev: device
1495 *
1496 * Disable Large Receive Offload (LRO) on a net device. Must be
1497 * called under RTNL. This is needed if received packets may be
1498 * forwarded to another interface.
1499 */
1500void dev_disable_lro(struct net_device *dev)
1501{
fbe168ba
MK
1502 struct net_device *lower_dev;
1503 struct list_head *iter;
529d0489 1504
bc5787c6
MM
1505 dev->wanted_features &= ~NETIF_F_LRO;
1506 netdev_update_features(dev);
27660515 1507
22d5969f
MM
1508 if (unlikely(dev->features & NETIF_F_LRO))
1509 netdev_WARN(dev, "failed to disable LRO!\n");
fbe168ba
MK
1510
1511 netdev_for_each_lower_dev(dev, lower_dev, iter)
1512 dev_disable_lro(lower_dev);
0187bdfb
BH
1513}
1514EXPORT_SYMBOL(dev_disable_lro);
1515
351638e7
JP
1516static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1517 struct net_device *dev)
1518{
1519 struct netdev_notifier_info info;
1520
1521 netdev_notifier_info_init(&info, dev);
1522 return nb->notifier_call(nb, val, &info);
1523}
0187bdfb 1524
881d966b
EB
1525static int dev_boot_phase = 1;
1526
1da177e4 1527/**
722c9a0c 1528 * register_netdevice_notifier - register a network notifier block
1529 * @nb: notifier
1da177e4 1530 *
722c9a0c 1531 * Register a notifier to be called when network device events occur.
1532 * The notifier passed is linked into the kernel structures and must
1533 * not be reused until it has been unregistered. A negative errno code
1534 * is returned on a failure.
1da177e4 1535 *
722c9a0c 1536 * When registered all registration and up events are replayed
1537 * to the new notifier to allow device to have a race free
1538 * view of the network device list.
1da177e4
LT
1539 */
1540
1541int register_netdevice_notifier(struct notifier_block *nb)
1542{
1543 struct net_device *dev;
fcc5a03a 1544 struct net_device *last;
881d966b 1545 struct net *net;
1da177e4
LT
1546 int err;
1547
1548 rtnl_lock();
f07d5b94 1549 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1550 if (err)
1551 goto unlock;
881d966b
EB
1552 if (dev_boot_phase)
1553 goto unlock;
1554 for_each_net(net) {
1555 for_each_netdev(net, dev) {
351638e7 1556 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
881d966b
EB
1557 err = notifier_to_errno(err);
1558 if (err)
1559 goto rollback;
1560
1561 if (!(dev->flags & IFF_UP))
1562 continue;
1da177e4 1563
351638e7 1564 call_netdevice_notifier(nb, NETDEV_UP, dev);
881d966b 1565 }
1da177e4 1566 }
fcc5a03a
HX
1567
1568unlock:
1da177e4
LT
1569 rtnl_unlock();
1570 return err;
fcc5a03a
HX
1571
1572rollback:
1573 last = dev;
881d966b
EB
1574 for_each_net(net) {
1575 for_each_netdev(net, dev) {
1576 if (dev == last)
8f891489 1577 goto outroll;
fcc5a03a 1578
881d966b 1579 if (dev->flags & IFF_UP) {
351638e7
JP
1580 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1581 dev);
1582 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
881d966b 1583 }
351638e7 1584 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
fcc5a03a 1585 }
fcc5a03a 1586 }
c67625a1 1587
8f891489 1588outroll:
c67625a1 1589 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1590 goto unlock;
1da177e4 1591}
d1b19dff 1592EXPORT_SYMBOL(register_netdevice_notifier);
1da177e4
LT
1593
1594/**
722c9a0c 1595 * unregister_netdevice_notifier - unregister a network notifier block
1596 * @nb: notifier
1da177e4 1597 *
722c9a0c 1598 * Unregister a notifier previously registered by
1599 * register_netdevice_notifier(). The notifier is unlinked into the
1600 * kernel structures and may then be reused. A negative errno code
1601 * is returned on a failure.
7d3d43da 1602 *
722c9a0c 1603 * After unregistering unregister and down device events are synthesized
1604 * for all devices on the device list to the removed notifier to remove
1605 * the need for special case cleanup code.
1da177e4
LT
1606 */
1607
1608int unregister_netdevice_notifier(struct notifier_block *nb)
1609{
7d3d43da
EB
1610 struct net_device *dev;
1611 struct net *net;
9f514950
HX
1612 int err;
1613
1614 rtnl_lock();
f07d5b94 1615 err = raw_notifier_chain_unregister(&netdev_chain, nb);
7d3d43da
EB
1616 if (err)
1617 goto unlock;
1618
1619 for_each_net(net) {
1620 for_each_netdev(net, dev) {
1621 if (dev->flags & IFF_UP) {
351638e7
JP
1622 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1623 dev);
1624 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
7d3d43da 1625 }
351638e7 1626 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
7d3d43da
EB
1627 }
1628 }
1629unlock:
9f514950
HX
1630 rtnl_unlock();
1631 return err;
1da177e4 1632}
d1b19dff 1633EXPORT_SYMBOL(unregister_netdevice_notifier);
1da177e4 1634
351638e7
JP
1635/**
1636 * call_netdevice_notifiers_info - call all network notifier blocks
1637 * @val: value passed unmodified to notifier function
1638 * @dev: net_device pointer passed unmodified to notifier function
1639 * @info: notifier information data
1640 *
1641 * Call all network notifier blocks. Parameters and return value
1642 * are as for raw_notifier_call_chain().
1643 */
1644
1d143d9f 1645static int call_netdevice_notifiers_info(unsigned long val,
1646 struct net_device *dev,
1647 struct netdev_notifier_info *info)
351638e7
JP
1648{
1649 ASSERT_RTNL();
1650 netdev_notifier_info_init(info, dev);
1651 return raw_notifier_call_chain(&netdev_chain, val, info);
1652}
351638e7 1653
1da177e4
LT
1654/**
1655 * call_netdevice_notifiers - call all network notifier blocks
1656 * @val: value passed unmodified to notifier function
c4ea43c5 1657 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
1658 *
1659 * Call all network notifier blocks. Parameters and return value
f07d5b94 1660 * are as for raw_notifier_call_chain().
1da177e4
LT
1661 */
1662
ad7379d4 1663int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 1664{
351638e7
JP
1665 struct netdev_notifier_info info;
1666
1667 return call_netdevice_notifiers_info(val, dev, &info);
1da177e4 1668}
edf947f1 1669EXPORT_SYMBOL(call_netdevice_notifiers);
1da177e4 1670
1cf51900 1671#ifdef CONFIG_NET_INGRESS
4577139b
DB
1672static struct static_key ingress_needed __read_mostly;
1673
1674void net_inc_ingress_queue(void)
1675{
1676 static_key_slow_inc(&ingress_needed);
1677}
1678EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1679
1680void net_dec_ingress_queue(void)
1681{
1682 static_key_slow_dec(&ingress_needed);
1683}
1684EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1685#endif
1686
1f211a1b
DB
1687#ifdef CONFIG_NET_EGRESS
1688static struct static_key egress_needed __read_mostly;
1689
1690void net_inc_egress_queue(void)
1691{
1692 static_key_slow_inc(&egress_needed);
1693}
1694EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1695
1696void net_dec_egress_queue(void)
1697{
1698 static_key_slow_dec(&egress_needed);
1699}
1700EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1701#endif
1702
c5905afb 1703static struct static_key netstamp_needed __read_mostly;
b90e5794 1704#ifdef HAVE_JUMP_LABEL
b90e5794 1705static atomic_t netstamp_needed_deferred;
13baa00a 1706static atomic_t netstamp_wanted;
5fa8bbda 1707static void netstamp_clear(struct work_struct *work)
1da177e4 1708{
b90e5794 1709 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
13baa00a 1710 int wanted;
b90e5794 1711
13baa00a
ED
1712 wanted = atomic_add_return(deferred, &netstamp_wanted);
1713 if (wanted > 0)
1714 static_key_enable(&netstamp_needed);
1715 else
1716 static_key_disable(&netstamp_needed);
5fa8bbda
ED
1717}
1718static DECLARE_WORK(netstamp_work, netstamp_clear);
b90e5794 1719#endif
5fa8bbda
ED
1720
1721void net_enable_timestamp(void)
1722{
13baa00a
ED
1723#ifdef HAVE_JUMP_LABEL
1724 int wanted;
1725
1726 while (1) {
1727 wanted = atomic_read(&netstamp_wanted);
1728 if (wanted <= 0)
1729 break;
1730 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1731 return;
1732 }
1733 atomic_inc(&netstamp_needed_deferred);
1734 schedule_work(&netstamp_work);
1735#else
c5905afb 1736 static_key_slow_inc(&netstamp_needed);
13baa00a 1737#endif
1da177e4 1738}
d1b19dff 1739EXPORT_SYMBOL(net_enable_timestamp);
1da177e4
LT
1740
1741void net_disable_timestamp(void)
1742{
b90e5794 1743#ifdef HAVE_JUMP_LABEL
13baa00a
ED
1744 int wanted;
1745
1746 while (1) {
1747 wanted = atomic_read(&netstamp_wanted);
1748 if (wanted <= 1)
1749 break;
1750 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1751 return;
1752 }
1753 atomic_dec(&netstamp_needed_deferred);
5fa8bbda
ED
1754 schedule_work(&netstamp_work);
1755#else
c5905afb 1756 static_key_slow_dec(&netstamp_needed);
5fa8bbda 1757#endif
1da177e4 1758}
d1b19dff 1759EXPORT_SYMBOL(net_disable_timestamp);
1da177e4 1760
3b098e2d 1761static inline void net_timestamp_set(struct sk_buff *skb)
1da177e4 1762{
2456e855 1763 skb->tstamp = 0;
c5905afb 1764 if (static_key_false(&netstamp_needed))
a61bbcf2 1765 __net_timestamp(skb);
1da177e4
LT
1766}
1767
588f0330 1768#define net_timestamp_check(COND, SKB) \
c5905afb 1769 if (static_key_false(&netstamp_needed)) { \
2456e855 1770 if ((COND) && !(SKB)->tstamp) \
588f0330
ED
1771 __net_timestamp(SKB); \
1772 } \
3b098e2d 1773
f4b05d27 1774bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
79b569f0
DL
1775{
1776 unsigned int len;
1777
1778 if (!(dev->flags & IFF_UP))
1779 return false;
1780
1781 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1782 if (skb->len <= len)
1783 return true;
1784
1785 /* if TSO is enabled, we don't care about the length as the packet
1786 * could be forwarded without being segmented before
1787 */
1788 if (skb_is_gso(skb))
1789 return true;
1790
1791 return false;
1792}
1ee481fb 1793EXPORT_SYMBOL_GPL(is_skb_forwardable);
79b569f0 1794
a0265d28
HX
1795int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1796{
4e3264d2 1797 int ret = ____dev_forward_skb(dev, skb);
a0265d28 1798
4e3264d2
MKL
1799 if (likely(!ret)) {
1800 skb->protocol = eth_type_trans(skb, dev);
1801 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1802 }
a0265d28 1803
4e3264d2 1804 return ret;
a0265d28
HX
1805}
1806EXPORT_SYMBOL_GPL(__dev_forward_skb);
1807
44540960
AB
1808/**
1809 * dev_forward_skb - loopback an skb to another netif
1810 *
1811 * @dev: destination network device
1812 * @skb: buffer to forward
1813 *
1814 * return values:
1815 * NET_RX_SUCCESS (no congestion)
6ec82562 1816 * NET_RX_DROP (packet was dropped, but freed)
44540960
AB
1817 *
1818 * dev_forward_skb can be used for injecting an skb from the
1819 * start_xmit function of one device into the receive queue
1820 * of another device.
1821 *
1822 * The receiving device may be in another namespace, so
1823 * we have to clear all information in the skb that could
1824 * impact namespace isolation.
1825 */
1826int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1827{
a0265d28 1828 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
44540960
AB
1829}
1830EXPORT_SYMBOL_GPL(dev_forward_skb);
1831
71d9dec2
CG
1832static inline int deliver_skb(struct sk_buff *skb,
1833 struct packet_type *pt_prev,
1834 struct net_device *orig_dev)
1835{
1080e512
MT
1836 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
1837 return -ENOMEM;
71d9dec2
CG
1838 atomic_inc(&skb->users);
1839 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1840}
1841
7866a621
SN
1842static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1843 struct packet_type **pt,
fbcb2170
JP
1844 struct net_device *orig_dev,
1845 __be16 type,
7866a621
SN
1846 struct list_head *ptype_list)
1847{
1848 struct packet_type *ptype, *pt_prev = *pt;
1849
1850 list_for_each_entry_rcu(ptype, ptype_list, list) {
1851 if (ptype->type != type)
1852 continue;
1853 if (pt_prev)
fbcb2170 1854 deliver_skb(skb, pt_prev, orig_dev);
7866a621
SN
1855 pt_prev = ptype;
1856 }
1857 *pt = pt_prev;
1858}
1859
c0de08d0
EL
1860static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1861{
a3d744e9 1862 if (!ptype->af_packet_priv || !skb->sk)
c0de08d0
EL
1863 return false;
1864
1865 if (ptype->id_match)
1866 return ptype->id_match(ptype, skb->sk);
1867 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1868 return true;
1869
1870 return false;
1871}
1872
1da177e4
LT
1873/*
1874 * Support routine. Sends outgoing frames to any network
1875 * taps currently in use.
1876 */
1877
74b20582 1878void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
1879{
1880 struct packet_type *ptype;
71d9dec2
CG
1881 struct sk_buff *skb2 = NULL;
1882 struct packet_type *pt_prev = NULL;
7866a621 1883 struct list_head *ptype_list = &ptype_all;
a61bbcf2 1884
1da177e4 1885 rcu_read_lock();
7866a621
SN
1886again:
1887 list_for_each_entry_rcu(ptype, ptype_list, list) {
1da177e4
LT
1888 /* Never send packets back to the socket
1889 * they originated from - MvS (miquels@drinkel.ow.org)
1890 */
7866a621
SN
1891 if (skb_loop_sk(ptype, skb))
1892 continue;
71d9dec2 1893
7866a621
SN
1894 if (pt_prev) {
1895 deliver_skb(skb2, pt_prev, skb->dev);
1896 pt_prev = ptype;
1897 continue;
1898 }
1da177e4 1899
7866a621
SN
1900 /* need to clone skb, done only once */
1901 skb2 = skb_clone(skb, GFP_ATOMIC);
1902 if (!skb2)
1903 goto out_unlock;
70978182 1904
7866a621 1905 net_timestamp_set(skb2);
1da177e4 1906
7866a621
SN
1907 /* skb->nh should be correctly
1908 * set by sender, so that the second statement is
1909 * just protection against buggy protocols.
1910 */
1911 skb_reset_mac_header(skb2);
1912
1913 if (skb_network_header(skb2) < skb2->data ||
1914 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
1915 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1916 ntohs(skb2->protocol),
1917 dev->name);
1918 skb_reset_network_header(skb2);
1da177e4 1919 }
7866a621
SN
1920
1921 skb2->transport_header = skb2->network_header;
1922 skb2->pkt_type = PACKET_OUTGOING;
1923 pt_prev = ptype;
1924 }
1925
1926 if (ptype_list == &ptype_all) {
1927 ptype_list = &dev->ptype_all;
1928 goto again;
1da177e4 1929 }
7866a621 1930out_unlock:
71d9dec2
CG
1931 if (pt_prev)
1932 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1da177e4
LT
1933 rcu_read_unlock();
1934}
74b20582 1935EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
1da177e4 1936
2c53040f
BH
1937/**
1938 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
4f57c087
JF
1939 * @dev: Network device
1940 * @txq: number of queues available
1941 *
1942 * If real_num_tx_queues is changed the tc mappings may no longer be
1943 * valid. To resolve this verify the tc mapping remains valid and if
1944 * not NULL the mapping. With no priorities mapping to this
1945 * offset/count pair it will no longer be used. In the worst case TC0
1946 * is invalid nothing can be done so disable priority mappings. If is
1947 * expected that drivers will fix this mapping if they can before
1948 * calling netif_set_real_num_tx_queues.
1949 */
bb134d22 1950static void netif_setup_tc(struct net_device *dev, unsigned int txq)
4f57c087
JF
1951{
1952 int i;
1953 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1954
1955 /* If TC0 is invalidated disable TC mapping */
1956 if (tc->offset + tc->count > txq) {
7b6cd1ce 1957 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
4f57c087
JF
1958 dev->num_tc = 0;
1959 return;
1960 }
1961
1962 /* Invalidated prio to tc mappings set to TC0 */
1963 for (i = 1; i < TC_BITMASK + 1; i++) {
1964 int q = netdev_get_prio_tc_map(dev, i);
1965
1966 tc = &dev->tc_to_txq[q];
1967 if (tc->offset + tc->count > txq) {
7b6cd1ce
JP
1968 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
1969 i, q);
4f57c087
JF
1970 netdev_set_prio_tc_map(dev, i, 0);
1971 }
1972 }
1973}
1974
8d059b0f
AD
1975int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
1976{
1977 if (dev->num_tc) {
1978 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1979 int i;
1980
1981 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
1982 if ((txq - tc->offset) < tc->count)
1983 return i;
1984 }
1985
1986 return -1;
1987 }
1988
1989 return 0;
1990}
1991
537c00de
AD
1992#ifdef CONFIG_XPS
1993static DEFINE_MUTEX(xps_map_mutex);
1994#define xmap_dereference(P) \
1995 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1996
6234f874
AD
1997static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
1998 int tci, u16 index)
537c00de 1999{
10cdc3f3
AD
2000 struct xps_map *map = NULL;
2001 int pos;
537c00de 2002
10cdc3f3 2003 if (dev_maps)
6234f874
AD
2004 map = xmap_dereference(dev_maps->cpu_map[tci]);
2005 if (!map)
2006 return false;
537c00de 2007
6234f874
AD
2008 for (pos = map->len; pos--;) {
2009 if (map->queues[pos] != index)
2010 continue;
2011
2012 if (map->len > 1) {
2013 map->queues[pos] = map->queues[--map->len];
10cdc3f3 2014 break;
537c00de 2015 }
6234f874
AD
2016
2017 RCU_INIT_POINTER(dev_maps->cpu_map[tci], NULL);
2018 kfree_rcu(map, rcu);
2019 return false;
537c00de
AD
2020 }
2021
6234f874 2022 return true;
10cdc3f3
AD
2023}
2024
6234f874
AD
2025static bool remove_xps_queue_cpu(struct net_device *dev,
2026 struct xps_dev_maps *dev_maps,
2027 int cpu, u16 offset, u16 count)
2028{
184c449f
AD
2029 int num_tc = dev->num_tc ? : 1;
2030 bool active = false;
2031 int tci;
6234f874 2032
184c449f
AD
2033 for (tci = cpu * num_tc; num_tc--; tci++) {
2034 int i, j;
2035
2036 for (i = count, j = offset; i--; j++) {
2037 if (!remove_xps_queue(dev_maps, cpu, j))
2038 break;
2039 }
2040
2041 active |= i < 0;
6234f874
AD
2042 }
2043
184c449f 2044 return active;
6234f874
AD
2045}
2046
2047static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2048 u16 count)
10cdc3f3
AD
2049{
2050 struct xps_dev_maps *dev_maps;
024e9679 2051 int cpu, i;
10cdc3f3
AD
2052 bool active = false;
2053
2054 mutex_lock(&xps_map_mutex);
2055 dev_maps = xmap_dereference(dev->xps_maps);
2056
2057 if (!dev_maps)
2058 goto out_no_maps;
2059
6234f874
AD
2060 for_each_possible_cpu(cpu)
2061 active |= remove_xps_queue_cpu(dev, dev_maps, cpu,
2062 offset, count);
10cdc3f3
AD
2063
2064 if (!active) {
537c00de
AD
2065 RCU_INIT_POINTER(dev->xps_maps, NULL);
2066 kfree_rcu(dev_maps, rcu);
2067 }
2068
6234f874 2069 for (i = offset + (count - 1); count--; i--)
024e9679
AD
2070 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
2071 NUMA_NO_NODE);
2072
537c00de
AD
2073out_no_maps:
2074 mutex_unlock(&xps_map_mutex);
2075}
2076
6234f874
AD
2077static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2078{
2079 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2080}
2081
01c5f864
AD
2082static struct xps_map *expand_xps_map(struct xps_map *map,
2083 int cpu, u16 index)
2084{
2085 struct xps_map *new_map;
2086 int alloc_len = XPS_MIN_MAP_ALLOC;
2087 int i, pos;
2088
2089 for (pos = 0; map && pos < map->len; pos++) {
2090 if (map->queues[pos] != index)
2091 continue;
2092 return map;
2093 }
2094
2095 /* Need to add queue to this CPU's existing map */
2096 if (map) {
2097 if (pos < map->alloc_len)
2098 return map;
2099
2100 alloc_len = map->alloc_len * 2;
2101 }
2102
2103 /* Need to allocate new map to store queue on this CPU's map */
2104 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2105 cpu_to_node(cpu));
2106 if (!new_map)
2107 return NULL;
2108
2109 for (i = 0; i < pos; i++)
2110 new_map->queues[i] = map->queues[i];
2111 new_map->alloc_len = alloc_len;
2112 new_map->len = pos;
2113
2114 return new_map;
2115}
2116
3573540c
MT
2117int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2118 u16 index)
537c00de 2119{
01c5f864 2120 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
184c449f
AD
2121 int i, cpu, tci, numa_node_id = -2;
2122 int maps_sz, num_tc = 1, tc = 0;
537c00de 2123 struct xps_map *map, *new_map;
01c5f864 2124 bool active = false;
537c00de 2125
184c449f
AD
2126 if (dev->num_tc) {
2127 num_tc = dev->num_tc;
2128 tc = netdev_txq_to_tc(dev, index);
2129 if (tc < 0)
2130 return -EINVAL;
2131 }
2132
2133 maps_sz = XPS_DEV_MAPS_SIZE(num_tc);
2134 if (maps_sz < L1_CACHE_BYTES)
2135 maps_sz = L1_CACHE_BYTES;
2136
537c00de
AD
2137 mutex_lock(&xps_map_mutex);
2138
2139 dev_maps = xmap_dereference(dev->xps_maps);
2140
01c5f864 2141 /* allocate memory for queue storage */
184c449f 2142 for_each_cpu_and(cpu, cpu_online_mask, mask) {
01c5f864
AD
2143 if (!new_dev_maps)
2144 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2bb60cb9
AD
2145 if (!new_dev_maps) {
2146 mutex_unlock(&xps_map_mutex);
01c5f864 2147 return -ENOMEM;
2bb60cb9 2148 }
01c5f864 2149
184c449f
AD
2150 tci = cpu * num_tc + tc;
2151 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[tci]) :
01c5f864
AD
2152 NULL;
2153
2154 map = expand_xps_map(map, cpu, index);
2155 if (!map)
2156 goto error;
2157
184c449f 2158 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
01c5f864
AD
2159 }
2160
2161 if (!new_dev_maps)
2162 goto out_no_new_maps;
2163
537c00de 2164 for_each_possible_cpu(cpu) {
184c449f
AD
2165 /* copy maps belonging to foreign traffic classes */
2166 for (i = tc, tci = cpu * num_tc; dev_maps && i--; tci++) {
2167 /* fill in the new device map from the old device map */
2168 map = xmap_dereference(dev_maps->cpu_map[tci]);
2169 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2170 }
2171
2172 /* We need to explicitly update tci as prevous loop
2173 * could break out early if dev_maps is NULL.
2174 */
2175 tci = cpu * num_tc + tc;
2176
01c5f864
AD
2177 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
2178 /* add queue to CPU maps */
2179 int pos = 0;
2180
184c449f 2181 map = xmap_dereference(new_dev_maps->cpu_map[tci]);
01c5f864
AD
2182 while ((pos < map->len) && (map->queues[pos] != index))
2183 pos++;
2184
2185 if (pos == map->len)
2186 map->queues[map->len++] = index;
537c00de 2187#ifdef CONFIG_NUMA
537c00de
AD
2188 if (numa_node_id == -2)
2189 numa_node_id = cpu_to_node(cpu);
2190 else if (numa_node_id != cpu_to_node(cpu))
2191 numa_node_id = -1;
537c00de 2192#endif
01c5f864
AD
2193 } else if (dev_maps) {
2194 /* fill in the new device map from the old device map */
184c449f
AD
2195 map = xmap_dereference(dev_maps->cpu_map[tci]);
2196 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
537c00de 2197 }
01c5f864 2198
184c449f
AD
2199 /* copy maps belonging to foreign traffic classes */
2200 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2201 /* fill in the new device map from the old device map */
2202 map = xmap_dereference(dev_maps->cpu_map[tci]);
2203 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2204 }
537c00de
AD
2205 }
2206
01c5f864
AD
2207 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2208
537c00de 2209 /* Cleanup old maps */
184c449f
AD
2210 if (!dev_maps)
2211 goto out_no_old_maps;
2212
2213 for_each_possible_cpu(cpu) {
2214 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2215 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2216 map = xmap_dereference(dev_maps->cpu_map[tci]);
01c5f864
AD
2217 if (map && map != new_map)
2218 kfree_rcu(map, rcu);
2219 }
537c00de
AD
2220 }
2221
184c449f
AD
2222 kfree_rcu(dev_maps, rcu);
2223
2224out_no_old_maps:
01c5f864
AD
2225 dev_maps = new_dev_maps;
2226 active = true;
537c00de 2227
01c5f864
AD
2228out_no_new_maps:
2229 /* update Tx queue numa node */
537c00de
AD
2230 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2231 (numa_node_id >= 0) ? numa_node_id :
2232 NUMA_NO_NODE);
2233
01c5f864
AD
2234 if (!dev_maps)
2235 goto out_no_maps;
2236
2237 /* removes queue from unused CPUs */
2238 for_each_possible_cpu(cpu) {
184c449f
AD
2239 for (i = tc, tci = cpu * num_tc; i--; tci++)
2240 active |= remove_xps_queue(dev_maps, tci, index);
2241 if (!cpumask_test_cpu(cpu, mask) || !cpu_online(cpu))
2242 active |= remove_xps_queue(dev_maps, tci, index);
2243 for (i = num_tc - tc, tci++; --i; tci++)
2244 active |= remove_xps_queue(dev_maps, tci, index);
01c5f864
AD
2245 }
2246
2247 /* free map if not active */
2248 if (!active) {
2249 RCU_INIT_POINTER(dev->xps_maps, NULL);
2250 kfree_rcu(dev_maps, rcu);
2251 }
2252
2253out_no_maps:
537c00de
AD
2254 mutex_unlock(&xps_map_mutex);
2255
2256 return 0;
2257error:
01c5f864
AD
2258 /* remove any maps that we added */
2259 for_each_possible_cpu(cpu) {
184c449f
AD
2260 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2261 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2262 map = dev_maps ?
2263 xmap_dereference(dev_maps->cpu_map[tci]) :
2264 NULL;
2265 if (new_map && new_map != map)
2266 kfree(new_map);
2267 }
01c5f864
AD
2268 }
2269
537c00de
AD
2270 mutex_unlock(&xps_map_mutex);
2271
537c00de
AD
2272 kfree(new_dev_maps);
2273 return -ENOMEM;
2274}
2275EXPORT_SYMBOL(netif_set_xps_queue);
2276
2277#endif
9cf1f6a8
AD
2278void netdev_reset_tc(struct net_device *dev)
2279{
6234f874
AD
2280#ifdef CONFIG_XPS
2281 netif_reset_xps_queues_gt(dev, 0);
2282#endif
9cf1f6a8
AD
2283 dev->num_tc = 0;
2284 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2285 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2286}
2287EXPORT_SYMBOL(netdev_reset_tc);
2288
2289int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2290{
2291 if (tc >= dev->num_tc)
2292 return -EINVAL;
2293
6234f874
AD
2294#ifdef CONFIG_XPS
2295 netif_reset_xps_queues(dev, offset, count);
2296#endif
9cf1f6a8
AD
2297 dev->tc_to_txq[tc].count = count;
2298 dev->tc_to_txq[tc].offset = offset;
2299 return 0;
2300}
2301EXPORT_SYMBOL(netdev_set_tc_queue);
2302
2303int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2304{
2305 if (num_tc > TC_MAX_QUEUE)
2306 return -EINVAL;
2307
6234f874
AD
2308#ifdef CONFIG_XPS
2309 netif_reset_xps_queues_gt(dev, 0);
2310#endif
9cf1f6a8
AD
2311 dev->num_tc = num_tc;
2312 return 0;
2313}
2314EXPORT_SYMBOL(netdev_set_num_tc);
2315
f0796d5c
JF
2316/*
2317 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2318 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2319 */
e6484930 2320int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
f0796d5c 2321{
1d24eb48
TH
2322 int rc;
2323
e6484930
TH
2324 if (txq < 1 || txq > dev->num_tx_queues)
2325 return -EINVAL;
f0796d5c 2326
5c56580b
BH
2327 if (dev->reg_state == NETREG_REGISTERED ||
2328 dev->reg_state == NETREG_UNREGISTERING) {
e6484930
TH
2329 ASSERT_RTNL();
2330
1d24eb48
TH
2331 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2332 txq);
bf264145
TH
2333 if (rc)
2334 return rc;
2335
4f57c087
JF
2336 if (dev->num_tc)
2337 netif_setup_tc(dev, txq);
2338
024e9679 2339 if (txq < dev->real_num_tx_queues) {
e6484930 2340 qdisc_reset_all_tx_gt(dev, txq);
024e9679
AD
2341#ifdef CONFIG_XPS
2342 netif_reset_xps_queues_gt(dev, txq);
2343#endif
2344 }
f0796d5c 2345 }
e6484930
TH
2346
2347 dev->real_num_tx_queues = txq;
2348 return 0;
f0796d5c
JF
2349}
2350EXPORT_SYMBOL(netif_set_real_num_tx_queues);
56079431 2351
a953be53 2352#ifdef CONFIG_SYSFS
62fe0b40
BH
2353/**
2354 * netif_set_real_num_rx_queues - set actual number of RX queues used
2355 * @dev: Network device
2356 * @rxq: Actual number of RX queues
2357 *
2358 * This must be called either with the rtnl_lock held or before
2359 * registration of the net device. Returns 0 on success, or a
4e7f7951
BH
2360 * negative error code. If called before registration, it always
2361 * succeeds.
62fe0b40
BH
2362 */
2363int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2364{
2365 int rc;
2366
bd25fa7b
TH
2367 if (rxq < 1 || rxq > dev->num_rx_queues)
2368 return -EINVAL;
2369
62fe0b40
BH
2370 if (dev->reg_state == NETREG_REGISTERED) {
2371 ASSERT_RTNL();
2372
62fe0b40
BH
2373 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2374 rxq);
2375 if (rc)
2376 return rc;
62fe0b40
BH
2377 }
2378
2379 dev->real_num_rx_queues = rxq;
2380 return 0;
2381}
2382EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2383#endif
2384
2c53040f
BH
2385/**
2386 * netif_get_num_default_rss_queues - default number of RSS queues
16917b87
YM
2387 *
2388 * This routine should set an upper limit on the number of RSS queues
2389 * used by default by multiqueue devices.
2390 */
a55b138b 2391int netif_get_num_default_rss_queues(void)
16917b87 2392{
40e4e713
HS
2393 return is_kdump_kernel() ?
2394 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
16917b87
YM
2395}
2396EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2397
3bcb846c 2398static void __netif_reschedule(struct Qdisc *q)
56079431 2399{
def82a1d
JP
2400 struct softnet_data *sd;
2401 unsigned long flags;
56079431 2402
def82a1d 2403 local_irq_save(flags);
903ceff7 2404 sd = this_cpu_ptr(&softnet_data);
a9cbd588
CG
2405 q->next_sched = NULL;
2406 *sd->output_queue_tailp = q;
2407 sd->output_queue_tailp = &q->next_sched;
def82a1d
JP
2408 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2409 local_irq_restore(flags);
2410}
2411
2412void __netif_schedule(struct Qdisc *q)
2413{
2414 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2415 __netif_reschedule(q);
56079431
DV
2416}
2417EXPORT_SYMBOL(__netif_schedule);
2418
e6247027
ED
2419struct dev_kfree_skb_cb {
2420 enum skb_free_reason reason;
2421};
2422
2423static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
56079431 2424{
e6247027
ED
2425 return (struct dev_kfree_skb_cb *)skb->cb;
2426}
2427
46e5da40
JF
2428void netif_schedule_queue(struct netdev_queue *txq)
2429{
2430 rcu_read_lock();
2431 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2432 struct Qdisc *q = rcu_dereference(txq->qdisc);
2433
2434 __netif_schedule(q);
2435 }
2436 rcu_read_unlock();
2437}
2438EXPORT_SYMBOL(netif_schedule_queue);
2439
46e5da40
JF
2440void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2441{
2442 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2443 struct Qdisc *q;
2444
2445 rcu_read_lock();
2446 q = rcu_dereference(dev_queue->qdisc);
2447 __netif_schedule(q);
2448 rcu_read_unlock();
2449 }
2450}
2451EXPORT_SYMBOL(netif_tx_wake_queue);
2452
e6247027 2453void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
56079431 2454{
e6247027 2455 unsigned long flags;
56079431 2456
9899886d
MJ
2457 if (unlikely(!skb))
2458 return;
2459
e6247027
ED
2460 if (likely(atomic_read(&skb->users) == 1)) {
2461 smp_rmb();
2462 atomic_set(&skb->users, 0);
2463 } else if (likely(!atomic_dec_and_test(&skb->users))) {
2464 return;
bea3348e 2465 }
e6247027
ED
2466 get_kfree_skb_cb(skb)->reason = reason;
2467 local_irq_save(flags);
2468 skb->next = __this_cpu_read(softnet_data.completion_queue);
2469 __this_cpu_write(softnet_data.completion_queue, skb);
2470 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2471 local_irq_restore(flags);
56079431 2472}
e6247027 2473EXPORT_SYMBOL(__dev_kfree_skb_irq);
56079431 2474
e6247027 2475void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
56079431
DV
2476{
2477 if (in_irq() || irqs_disabled())
e6247027 2478 __dev_kfree_skb_irq(skb, reason);
56079431
DV
2479 else
2480 dev_kfree_skb(skb);
2481}
e6247027 2482EXPORT_SYMBOL(__dev_kfree_skb_any);
56079431
DV
2483
2484
bea3348e
SH
2485/**
2486 * netif_device_detach - mark device as removed
2487 * @dev: network device
2488 *
2489 * Mark device as removed from system and therefore no longer available.
2490 */
56079431
DV
2491void netif_device_detach(struct net_device *dev)
2492{
2493 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2494 netif_running(dev)) {
d543103a 2495 netif_tx_stop_all_queues(dev);
56079431
DV
2496 }
2497}
2498EXPORT_SYMBOL(netif_device_detach);
2499
bea3348e
SH
2500/**
2501 * netif_device_attach - mark device as attached
2502 * @dev: network device
2503 *
2504 * Mark device as attached from system and restart if needed.
2505 */
56079431
DV
2506void netif_device_attach(struct net_device *dev)
2507{
2508 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2509 netif_running(dev)) {
d543103a 2510 netif_tx_wake_all_queues(dev);
4ec93edb 2511 __netdev_watchdog_up(dev);
56079431
DV
2512 }
2513}
2514EXPORT_SYMBOL(netif_device_attach);
2515
5605c762
JP
2516/*
2517 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2518 * to be used as a distribution range.
2519 */
2520u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
2521 unsigned int num_tx_queues)
2522{
2523 u32 hash;
2524 u16 qoffset = 0;
2525 u16 qcount = num_tx_queues;
2526
2527 if (skb_rx_queue_recorded(skb)) {
2528 hash = skb_get_rx_queue(skb);
2529 while (unlikely(hash >= num_tx_queues))
2530 hash -= num_tx_queues;
2531 return hash;
2532 }
2533
2534 if (dev->num_tc) {
2535 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
f4563a75 2536
5605c762
JP
2537 qoffset = dev->tc_to_txq[tc].offset;
2538 qcount = dev->tc_to_txq[tc].count;
2539 }
2540
2541 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2542}
2543EXPORT_SYMBOL(__skb_tx_hash);
2544
36c92474
BH
2545static void skb_warn_bad_offload(const struct sk_buff *skb)
2546{
84d15ae5 2547 static const netdev_features_t null_features;
36c92474 2548 struct net_device *dev = skb->dev;
88ad4175 2549 const char *name = "";
36c92474 2550
c846ad9b
BG
2551 if (!net_ratelimit())
2552 return;
2553
88ad4175
BM
2554 if (dev) {
2555 if (dev->dev.parent)
2556 name = dev_driver_string(dev->dev.parent);
2557 else
2558 name = netdev_name(dev);
2559 }
36c92474
BH
2560 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2561 "gso_type=%d ip_summed=%d\n",
88ad4175 2562 name, dev ? &dev->features : &null_features,
65e9d2fa 2563 skb->sk ? &skb->sk->sk_route_caps : &null_features,
36c92474
BH
2564 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2565 skb_shinfo(skb)->gso_type, skb->ip_summed);
2566}
2567
1da177e4
LT
2568/*
2569 * Invalidate hardware checksum when packet is to be mangled, and
2570 * complete checksum manually on outgoing path.
2571 */
84fa7933 2572int skb_checksum_help(struct sk_buff *skb)
1da177e4 2573{
d3bc23e7 2574 __wsum csum;
663ead3b 2575 int ret = 0, offset;
1da177e4 2576
84fa7933 2577 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
2578 goto out_set_summed;
2579
2580 if (unlikely(skb_shinfo(skb)->gso_size)) {
36c92474
BH
2581 skb_warn_bad_offload(skb);
2582 return -EINVAL;
1da177e4
LT
2583 }
2584
cef401de
ED
2585 /* Before computing a checksum, we should make sure no frag could
2586 * be modified by an external entity : checksum could be wrong.
2587 */
2588 if (skb_has_shared_frag(skb)) {
2589 ret = __skb_linearize(skb);
2590 if (ret)
2591 goto out;
2592 }
2593
55508d60 2594 offset = skb_checksum_start_offset(skb);
a030847e
HX
2595 BUG_ON(offset >= skb_headlen(skb));
2596 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2597
2598 offset += skb->csum_offset;
2599 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2600
2601 if (skb_cloned(skb) &&
2602 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
1da177e4
LT
2603 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2604 if (ret)
2605 goto out;
2606 }
2607
4f2e4ad5 2608 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
a430a43d 2609out_set_summed:
1da177e4 2610 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 2611out:
1da177e4
LT
2612 return ret;
2613}
d1b19dff 2614EXPORT_SYMBOL(skb_checksum_help);
1da177e4 2615
b72b5bf6
DC
2616int skb_crc32c_csum_help(struct sk_buff *skb)
2617{
2618 __le32 crc32c_csum;
2619 int ret = 0, offset, start;
2620
2621 if (skb->ip_summed != CHECKSUM_PARTIAL)
2622 goto out;
2623
2624 if (unlikely(skb_is_gso(skb)))
2625 goto out;
2626
2627 /* Before computing a checksum, we should make sure no frag could
2628 * be modified by an external entity : checksum could be wrong.
2629 */
2630 if (unlikely(skb_has_shared_frag(skb))) {
2631 ret = __skb_linearize(skb);
2632 if (ret)
2633 goto out;
2634 }
2635 start = skb_checksum_start_offset(skb);
2636 offset = start + offsetof(struct sctphdr, checksum);
2637 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
2638 ret = -EINVAL;
2639 goto out;
2640 }
2641 if (skb_cloned(skb) &&
2642 !skb_clone_writable(skb, offset + sizeof(__le32))) {
2643 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2644 if (ret)
2645 goto out;
2646 }
2647 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
2648 skb->len - start, ~(__u32)0,
2649 crc32c_csum_stub));
2650 *(__le32 *)(skb->data + offset) = crc32c_csum;
2651 skb->ip_summed = CHECKSUM_NONE;
dba00306 2652 skb->csum_not_inet = 0;
b72b5bf6
DC
2653out:
2654 return ret;
2655}
2656
53d6471c 2657__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
f6a78bfc 2658{
252e3346 2659 __be16 type = skb->protocol;
f6a78bfc 2660
19acc327
PS
2661 /* Tunnel gso handlers can set protocol to ethernet. */
2662 if (type == htons(ETH_P_TEB)) {
2663 struct ethhdr *eth;
2664
2665 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2666 return 0;
2667
2668 eth = (struct ethhdr *)skb_mac_header(skb);
2669 type = eth->h_proto;
2670 }
2671
d4bcef3f 2672 return __vlan_get_protocol(skb, type, depth);
ec5f0615
PS
2673}
2674
2675/**
2676 * skb_mac_gso_segment - mac layer segmentation handler.
2677 * @skb: buffer to segment
2678 * @features: features for the output path (see dev->features)
2679 */
2680struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2681 netdev_features_t features)
2682{
2683 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2684 struct packet_offload *ptype;
53d6471c
VY
2685 int vlan_depth = skb->mac_len;
2686 __be16 type = skb_network_protocol(skb, &vlan_depth);
ec5f0615
PS
2687
2688 if (unlikely(!type))
2689 return ERR_PTR(-EINVAL);
2690
53d6471c 2691 __skb_pull(skb, vlan_depth);
f6a78bfc
HX
2692
2693 rcu_read_lock();
22061d80 2694 list_for_each_entry_rcu(ptype, &offload_base, list) {
f191a1d1 2695 if (ptype->type == type && ptype->callbacks.gso_segment) {
f191a1d1 2696 segs = ptype->callbacks.gso_segment(skb, features);
f6a78bfc
HX
2697 break;
2698 }
2699 }
2700 rcu_read_unlock();
2701
98e399f8 2702 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 2703
f6a78bfc
HX
2704 return segs;
2705}
05e8ef4a
PS
2706EXPORT_SYMBOL(skb_mac_gso_segment);
2707
2708
2709/* openvswitch calls this on rx path, so we need a different check.
2710 */
2711static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
2712{
2713 if (tx_path)
6e7bc478
ED
2714 return skb->ip_summed != CHECKSUM_PARTIAL &&
2715 skb->ip_summed != CHECKSUM_NONE;
2716
2717 return skb->ip_summed == CHECKSUM_NONE;
05e8ef4a
PS
2718}
2719
2720/**
2721 * __skb_gso_segment - Perform segmentation on skb.
2722 * @skb: buffer to segment
2723 * @features: features for the output path (see dev->features)
2724 * @tx_path: whether it is called in TX path
2725 *
2726 * This function segments the given skb and returns a list of segments.
2727 *
2728 * It may return NULL if the skb requires no segmentation. This is
2729 * only possible when GSO is used for verifying header integrity.
9207f9d4
KK
2730 *
2731 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
05e8ef4a
PS
2732 */
2733struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2734 netdev_features_t features, bool tx_path)
2735{
b2504a5d
ED
2736 struct sk_buff *segs;
2737
05e8ef4a
PS
2738 if (unlikely(skb_needs_check(skb, tx_path))) {
2739 int err;
2740
b2504a5d 2741 /* We're going to init ->check field in TCP or UDP header */
a40e0a66 2742 err = skb_cow_head(skb, 0);
2743 if (err < 0)
05e8ef4a
PS
2744 return ERR_PTR(err);
2745 }
2746
802ab55a
AD
2747 /* Only report GSO partial support if it will enable us to
2748 * support segmentation on this frame without needing additional
2749 * work.
2750 */
2751 if (features & NETIF_F_GSO_PARTIAL) {
2752 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
2753 struct net_device *dev = skb->dev;
2754
2755 partial_features |= dev->features & dev->gso_partial_features;
2756 if (!skb_gso_ok(skb, features | partial_features))
2757 features &= ~NETIF_F_GSO_PARTIAL;
2758 }
2759
9207f9d4
KK
2760 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
2761 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
2762
68c33163 2763 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
3347c960
ED
2764 SKB_GSO_CB(skb)->encap_level = 0;
2765
05e8ef4a
PS
2766 skb_reset_mac_header(skb);
2767 skb_reset_mac_len(skb);
2768
b2504a5d
ED
2769 segs = skb_mac_gso_segment(skb, features);
2770
2771 if (unlikely(skb_needs_check(skb, tx_path)))
2772 skb_warn_bad_offload(skb);
2773
2774 return segs;
05e8ef4a 2775}
12b0004d 2776EXPORT_SYMBOL(__skb_gso_segment);
f6a78bfc 2777
fb286bb2
HX
2778/* Take action when hardware reception checksum errors are detected. */
2779#ifdef CONFIG_BUG
2780void netdev_rx_csum_fault(struct net_device *dev)
2781{
2782 if (net_ratelimit()) {
7b6cd1ce 2783 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
fb286bb2
HX
2784 dump_stack();
2785 }
2786}
2787EXPORT_SYMBOL(netdev_rx_csum_fault);
2788#endif
2789
1da177e4
LT
2790/* Actually, we should eliminate this check as soon as we know, that:
2791 * 1. IOMMU is present and allows to map all the memory.
2792 * 2. No high memory really exists on this machine.
2793 */
2794
c1e756bf 2795static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1da177e4 2796{
3d3a8533 2797#ifdef CONFIG_HIGHMEM
1da177e4 2798 int i;
f4563a75 2799
5acbbd42 2800 if (!(dev->features & NETIF_F_HIGHDMA)) {
ea2ab693
IC
2801 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2802 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
f4563a75 2803
ea2ab693 2804 if (PageHighMem(skb_frag_page(frag)))
5acbbd42 2805 return 1;
ea2ab693 2806 }
5acbbd42 2807 }
1da177e4 2808
5acbbd42
FT
2809 if (PCI_DMA_BUS_IS_PHYS) {
2810 struct device *pdev = dev->dev.parent;
1da177e4 2811
9092c658
ED
2812 if (!pdev)
2813 return 0;
5acbbd42 2814 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
ea2ab693
IC
2815 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2816 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
f4563a75 2817
5acbbd42
FT
2818 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2819 return 1;
2820 }
2821 }
3d3a8533 2822#endif
1da177e4
LT
2823 return 0;
2824}
1da177e4 2825
3b392ddb
SH
2826/* If MPLS offload request, verify we are testing hardware MPLS features
2827 * instead of standard features for the netdev.
2828 */
d0edc7bf 2829#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3b392ddb
SH
2830static netdev_features_t net_mpls_features(struct sk_buff *skb,
2831 netdev_features_t features,
2832 __be16 type)
2833{
25cd9ba0 2834 if (eth_p_mpls(type))
3b392ddb
SH
2835 features &= skb->dev->mpls_features;
2836
2837 return features;
2838}
2839#else
2840static netdev_features_t net_mpls_features(struct sk_buff *skb,
2841 netdev_features_t features,
2842 __be16 type)
2843{
2844 return features;
2845}
2846#endif
2847
c8f44aff 2848static netdev_features_t harmonize_features(struct sk_buff *skb,
c1e756bf 2849 netdev_features_t features)
f01a5236 2850{
53d6471c 2851 int tmp;
3b392ddb
SH
2852 __be16 type;
2853
2854 type = skb_network_protocol(skb, &tmp);
2855 features = net_mpls_features(skb, features, type);
53d6471c 2856
c0d680e5 2857 if (skb->ip_summed != CHECKSUM_NONE &&
3b392ddb 2858 !can_checksum_protocol(features, type)) {
996e8021 2859 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
f01a5236 2860 }
7be2c82c
ED
2861 if (illegal_highdma(skb->dev, skb))
2862 features &= ~NETIF_F_SG;
f01a5236
JG
2863
2864 return features;
2865}
2866
e38f3025
TM
2867netdev_features_t passthru_features_check(struct sk_buff *skb,
2868 struct net_device *dev,
2869 netdev_features_t features)
2870{
2871 return features;
2872}
2873EXPORT_SYMBOL(passthru_features_check);
2874
8cb65d00
TM
2875static netdev_features_t dflt_features_check(const struct sk_buff *skb,
2876 struct net_device *dev,
2877 netdev_features_t features)
2878{
2879 return vlan_features_check(skb, features);
2880}
2881
cbc53e08
AD
2882static netdev_features_t gso_features_check(const struct sk_buff *skb,
2883 struct net_device *dev,
2884 netdev_features_t features)
2885{
2886 u16 gso_segs = skb_shinfo(skb)->gso_segs;
2887
2888 if (gso_segs > dev->gso_max_segs)
2889 return features & ~NETIF_F_GSO_MASK;
2890
802ab55a
AD
2891 /* Support for GSO partial features requires software
2892 * intervention before we can actually process the packets
2893 * so we need to strip support for any partial features now
2894 * and we can pull them back in after we have partially
2895 * segmented the frame.
2896 */
2897 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
2898 features &= ~dev->gso_partial_features;
2899
2900 /* Make sure to clear the IPv4 ID mangling feature if the
2901 * IPv4 header has the potential to be fragmented.
cbc53e08
AD
2902 */
2903 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2904 struct iphdr *iph = skb->encapsulation ?
2905 inner_ip_hdr(skb) : ip_hdr(skb);
2906
2907 if (!(iph->frag_off & htons(IP_DF)))
2908 features &= ~NETIF_F_TSO_MANGLEID;
2909 }
2910
2911 return features;
2912}
2913
c1e756bf 2914netdev_features_t netif_skb_features(struct sk_buff *skb)
58e998c6 2915{
5f35227e 2916 struct net_device *dev = skb->dev;
fcbeb976 2917 netdev_features_t features = dev->features;
58e998c6 2918
cbc53e08
AD
2919 if (skb_is_gso(skb))
2920 features = gso_features_check(skb, dev, features);
30b678d8 2921
5f35227e
JG
2922 /* If encapsulation offload request, verify we are testing
2923 * hardware encapsulation features instead of standard
2924 * features for the netdev
2925 */
2926 if (skb->encapsulation)
2927 features &= dev->hw_enc_features;
2928
f5a7fb88
TM
2929 if (skb_vlan_tagged(skb))
2930 features = netdev_intersect_features(features,
2931 dev->vlan_features |
2932 NETIF_F_HW_VLAN_CTAG_TX |
2933 NETIF_F_HW_VLAN_STAG_TX);
f01a5236 2934
5f35227e
JG
2935 if (dev->netdev_ops->ndo_features_check)
2936 features &= dev->netdev_ops->ndo_features_check(skb, dev,
2937 features);
8cb65d00
TM
2938 else
2939 features &= dflt_features_check(skb, dev, features);
5f35227e 2940
c1e756bf 2941 return harmonize_features(skb, features);
58e998c6 2942}
c1e756bf 2943EXPORT_SYMBOL(netif_skb_features);
58e998c6 2944
2ea25513 2945static int xmit_one(struct sk_buff *skb, struct net_device *dev,
95f6b3dd 2946 struct netdev_queue *txq, bool more)
f6a78bfc 2947{
2ea25513
DM
2948 unsigned int len;
2949 int rc;
00829823 2950
7866a621 2951 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
2ea25513 2952 dev_queue_xmit_nit(skb, dev);
fc741216 2953
2ea25513
DM
2954 len = skb->len;
2955 trace_net_dev_start_xmit(skb, dev);
95f6b3dd 2956 rc = netdev_start_xmit(skb, dev, txq, more);
2ea25513 2957 trace_net_dev_xmit(skb, rc, dev, len);
adf30907 2958
2ea25513
DM
2959 return rc;
2960}
7b9c6090 2961
8dcda22a
DM
2962struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
2963 struct netdev_queue *txq, int *ret)
7f2e870f
DM
2964{
2965 struct sk_buff *skb = first;
2966 int rc = NETDEV_TX_OK;
7b9c6090 2967
7f2e870f
DM
2968 while (skb) {
2969 struct sk_buff *next = skb->next;
fc70fb64 2970
7f2e870f 2971 skb->next = NULL;
95f6b3dd 2972 rc = xmit_one(skb, dev, txq, next != NULL);
7f2e870f
DM
2973 if (unlikely(!dev_xmit_complete(rc))) {
2974 skb->next = next;
2975 goto out;
2976 }
6afff0ca 2977
7f2e870f
DM
2978 skb = next;
2979 if (netif_xmit_stopped(txq) && skb) {
2980 rc = NETDEV_TX_BUSY;
2981 break;
9ccb8975 2982 }
7f2e870f 2983 }
9ccb8975 2984
7f2e870f
DM
2985out:
2986 *ret = rc;
2987 return skb;
2988}
b40863c6 2989
1ff0dc94
ED
2990static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
2991 netdev_features_t features)
f6a78bfc 2992{
df8a39de 2993 if (skb_vlan_tag_present(skb) &&
5968250c
JP
2994 !vlan_hw_offload_capable(features, skb->vlan_proto))
2995 skb = __vlan_hwaccel_push_inside(skb);
eae3f88e
DM
2996 return skb;
2997}
f6a78bfc 2998
55a93b3e 2999static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev)
eae3f88e
DM
3000{
3001 netdev_features_t features;
f6a78bfc 3002
eae3f88e
DM
3003 features = netif_skb_features(skb);
3004 skb = validate_xmit_vlan(skb, features);
3005 if (unlikely(!skb))
3006 goto out_null;
7b9c6090 3007
8b86a61d 3008 if (netif_needs_gso(skb, features)) {
ce93718f
DM
3009 struct sk_buff *segs;
3010
3011 segs = skb_gso_segment(skb, features);
cecda693 3012 if (IS_ERR(segs)) {
af6dabc9 3013 goto out_kfree_skb;
cecda693
JW
3014 } else if (segs) {
3015 consume_skb(skb);
3016 skb = segs;
f6a78bfc 3017 }
eae3f88e
DM
3018 } else {
3019 if (skb_needs_linearize(skb, features) &&
3020 __skb_linearize(skb))
3021 goto out_kfree_skb;
4ec93edb 3022
f6e27114
SK
3023 if (validate_xmit_xfrm(skb, features))
3024 goto out_kfree_skb;
3025
eae3f88e
DM
3026 /* If packet is not checksummed and device does not
3027 * support checksumming for this protocol, complete
3028 * checksumming here.
3029 */
3030 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3031 if (skb->encapsulation)
3032 skb_set_inner_transport_header(skb,
3033 skb_checksum_start_offset(skb));
3034 else
3035 skb_set_transport_header(skb,
3036 skb_checksum_start_offset(skb));
a188222b 3037 if (!(features & NETIF_F_CSUM_MASK) &&
eae3f88e
DM
3038 skb_checksum_help(skb))
3039 goto out_kfree_skb;
7b9c6090 3040 }
0c772159 3041 }
7b9c6090 3042
eae3f88e 3043 return skb;
fc70fb64 3044
f6a78bfc
HX
3045out_kfree_skb:
3046 kfree_skb(skb);
eae3f88e 3047out_null:
d21fd63e 3048 atomic_long_inc(&dev->tx_dropped);
eae3f88e
DM
3049 return NULL;
3050}
6afff0ca 3051
55a93b3e
ED
3052struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev)
3053{
3054 struct sk_buff *next, *head = NULL, *tail;
3055
bec3cfdc 3056 for (; skb != NULL; skb = next) {
55a93b3e
ED
3057 next = skb->next;
3058 skb->next = NULL;
bec3cfdc
ED
3059
3060 /* in case skb wont be segmented, point to itself */
3061 skb->prev = skb;
3062
55a93b3e 3063 skb = validate_xmit_skb(skb, dev);
bec3cfdc
ED
3064 if (!skb)
3065 continue;
55a93b3e 3066
bec3cfdc
ED
3067 if (!head)
3068 head = skb;
3069 else
3070 tail->next = skb;
3071 /* If skb was segmented, skb->prev points to
3072 * the last segment. If not, it still contains skb.
3073 */
3074 tail = skb->prev;
55a93b3e
ED
3075 }
3076 return head;
f6a78bfc 3077}
104ba78c 3078EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
f6a78bfc 3079
1def9238
ED
3080static void qdisc_pkt_len_init(struct sk_buff *skb)
3081{
3082 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3083
3084 qdisc_skb_cb(skb)->pkt_len = skb->len;
3085
3086 /* To get more precise estimation of bytes sent on wire,
3087 * we add to pkt_len the headers size of all segments
3088 */
3089 if (shinfo->gso_size) {
757b8b1d 3090 unsigned int hdr_len;
15e5a030 3091 u16 gso_segs = shinfo->gso_segs;
1def9238 3092
757b8b1d
ED
3093 /* mac layer + network layer */
3094 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3095
3096 /* + transport layer */
1def9238
ED
3097 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3098 hdr_len += tcp_hdrlen(skb);
3099 else
3100 hdr_len += sizeof(struct udphdr);
15e5a030
JW
3101
3102 if (shinfo->gso_type & SKB_GSO_DODGY)
3103 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3104 shinfo->gso_size);
3105
3106 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
1def9238
ED
3107 }
3108}
3109
bbd8a0d3
KK
3110static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3111 struct net_device *dev,
3112 struct netdev_queue *txq)
3113{
3114 spinlock_t *root_lock = qdisc_lock(q);
520ac30f 3115 struct sk_buff *to_free = NULL;
a2da570d 3116 bool contended;
bbd8a0d3
KK
3117 int rc;
3118
a2da570d 3119 qdisc_calculate_pkt_len(skb, q);
79640a4c
ED
3120 /*
3121 * Heuristic to force contended enqueues to serialize on a
3122 * separate lock before trying to get qdisc main lock.
f9eb8aea 3123 * This permits qdisc->running owner to get the lock more
9bf2b8c2 3124 * often and dequeue packets faster.
79640a4c 3125 */
a2da570d 3126 contended = qdisc_is_running(q);
79640a4c
ED
3127 if (unlikely(contended))
3128 spin_lock(&q->busylock);
3129
bbd8a0d3
KK
3130 spin_lock(root_lock);
3131 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
520ac30f 3132 __qdisc_drop(skb, &to_free);
bbd8a0d3
KK
3133 rc = NET_XMIT_DROP;
3134 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
bc135b23 3135 qdisc_run_begin(q)) {
bbd8a0d3
KK
3136 /*
3137 * This is a work-conserving queue; there are no old skbs
3138 * waiting to be sent out; and the qdisc is not running -
3139 * xmit the skb directly.
3140 */
bfe0d029 3141
bfe0d029
ED
3142 qdisc_bstats_update(q, skb);
3143
55a93b3e 3144 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
79640a4c
ED
3145 if (unlikely(contended)) {
3146 spin_unlock(&q->busylock);
3147 contended = false;
3148 }
bbd8a0d3 3149 __qdisc_run(q);
79640a4c 3150 } else
bc135b23 3151 qdisc_run_end(q);
bbd8a0d3
KK
3152
3153 rc = NET_XMIT_SUCCESS;
3154 } else {
520ac30f 3155 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
79640a4c
ED
3156 if (qdisc_run_begin(q)) {
3157 if (unlikely(contended)) {
3158 spin_unlock(&q->busylock);
3159 contended = false;
3160 }
3161 __qdisc_run(q);
3162 }
bbd8a0d3
KK
3163 }
3164 spin_unlock(root_lock);
520ac30f
ED
3165 if (unlikely(to_free))
3166 kfree_skb_list(to_free);
79640a4c
ED
3167 if (unlikely(contended))
3168 spin_unlock(&q->busylock);
bbd8a0d3
KK
3169 return rc;
3170}
3171
86f8515f 3172#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
5bc1421e
NH
3173static void skb_update_prio(struct sk_buff *skb)
3174{
6977a79d 3175 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
5bc1421e 3176
91c68ce2 3177 if (!skb->priority && skb->sk && map) {
2a56a1fe
TH
3178 unsigned int prioidx =
3179 sock_cgroup_prioidx(&skb->sk->sk_cgrp_data);
91c68ce2
ED
3180
3181 if (prioidx < map->priomap_len)
3182 skb->priority = map->priomap[prioidx];
3183 }
5bc1421e
NH
3184}
3185#else
3186#define skb_update_prio(skb)
3187#endif
3188
f60e5990 3189DEFINE_PER_CPU(int, xmit_recursion);
3190EXPORT_SYMBOL(xmit_recursion);
3191
95603e22
MM
3192/**
3193 * dev_loopback_xmit - loop back @skb
0c4b51f0
EB
3194 * @net: network namespace this loopback is happening in
3195 * @sk: sk needed to be a netfilter okfn
95603e22
MM
3196 * @skb: buffer to transmit
3197 */
0c4b51f0 3198int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
95603e22
MM
3199{
3200 skb_reset_mac_header(skb);
3201 __skb_pull(skb, skb_network_offset(skb));
3202 skb->pkt_type = PACKET_LOOPBACK;
3203 skb->ip_summed = CHECKSUM_UNNECESSARY;
3204 WARN_ON(!skb_dst(skb));
3205 skb_dst_force(skb);
3206 netif_rx_ni(skb);
3207 return 0;
3208}
3209EXPORT_SYMBOL(dev_loopback_xmit);
3210
1f211a1b
DB
3211#ifdef CONFIG_NET_EGRESS
3212static struct sk_buff *
3213sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3214{
3215 struct tcf_proto *cl = rcu_dereference_bh(dev->egress_cl_list);
3216 struct tcf_result cl_res;
3217
3218 if (!cl)
3219 return skb;
3220
8dc07fdb 3221 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
1f211a1b
DB
3222 qdisc_bstats_cpu_update(cl->q, skb);
3223
87d83093 3224 switch (tcf_classify(skb, cl, &cl_res, false)) {
1f211a1b
DB
3225 case TC_ACT_OK:
3226 case TC_ACT_RECLASSIFY:
3227 skb->tc_index = TC_H_MIN(cl_res.classid);
3228 break;
3229 case TC_ACT_SHOT:
3230 qdisc_qstats_cpu_drop(cl->q);
3231 *ret = NET_XMIT_DROP;
7e2c3aea
DB
3232 kfree_skb(skb);
3233 return NULL;
1f211a1b
DB
3234 case TC_ACT_STOLEN:
3235 case TC_ACT_QUEUED:
3236 *ret = NET_XMIT_SUCCESS;
7e2c3aea 3237 consume_skb(skb);
1f211a1b
DB
3238 return NULL;
3239 case TC_ACT_REDIRECT:
3240 /* No need to push/pop skb's mac_header here on egress! */
3241 skb_do_redirect(skb);
3242 *ret = NET_XMIT_SUCCESS;
3243 return NULL;
3244 default:
3245 break;
3246 }
3247
3248 return skb;
3249}
3250#endif /* CONFIG_NET_EGRESS */
3251
638b2a69
JP
3252static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
3253{
3254#ifdef CONFIG_XPS
3255 struct xps_dev_maps *dev_maps;
3256 struct xps_map *map;
3257 int queue_index = -1;
3258
3259 rcu_read_lock();
3260 dev_maps = rcu_dereference(dev->xps_maps);
3261 if (dev_maps) {
184c449f
AD
3262 unsigned int tci = skb->sender_cpu - 1;
3263
3264 if (dev->num_tc) {
3265 tci *= dev->num_tc;
3266 tci += netdev_get_prio_tc_map(dev, skb->priority);
3267 }
3268
3269 map = rcu_dereference(dev_maps->cpu_map[tci]);
638b2a69
JP
3270 if (map) {
3271 if (map->len == 1)
3272 queue_index = map->queues[0];
3273 else
3274 queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
3275 map->len)];
3276 if (unlikely(queue_index >= dev->real_num_tx_queues))
3277 queue_index = -1;
3278 }
3279 }
3280 rcu_read_unlock();
3281
3282 return queue_index;
3283#else
3284 return -1;
3285#endif
3286}
3287
3288static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
3289{
3290 struct sock *sk = skb->sk;
3291 int queue_index = sk_tx_queue_get(sk);
3292
3293 if (queue_index < 0 || skb->ooo_okay ||
3294 queue_index >= dev->real_num_tx_queues) {
3295 int new_index = get_xps_queue(dev, skb);
f4563a75 3296
638b2a69
JP
3297 if (new_index < 0)
3298 new_index = skb_tx_hash(dev, skb);
3299
3300 if (queue_index != new_index && sk &&
004a5d01 3301 sk_fullsock(sk) &&
638b2a69
JP
3302 rcu_access_pointer(sk->sk_dst_cache))
3303 sk_tx_queue_set(sk, new_index);
3304
3305 queue_index = new_index;
3306 }
3307
3308 return queue_index;
3309}
3310
3311struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3312 struct sk_buff *skb,
3313 void *accel_priv)
3314{
3315 int queue_index = 0;
3316
3317#ifdef CONFIG_XPS
52bd2d62
ED
3318 u32 sender_cpu = skb->sender_cpu - 1;
3319
3320 if (sender_cpu >= (u32)NR_CPUS)
638b2a69
JP
3321 skb->sender_cpu = raw_smp_processor_id() + 1;
3322#endif
3323
3324 if (dev->real_num_tx_queues != 1) {
3325 const struct net_device_ops *ops = dev->netdev_ops;
f4563a75 3326
638b2a69
JP
3327 if (ops->ndo_select_queue)
3328 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
3329 __netdev_pick_tx);
3330 else
3331 queue_index = __netdev_pick_tx(dev, skb);
3332
3333 if (!accel_priv)
3334 queue_index = netdev_cap_txqueue(dev, queue_index);
3335 }
3336
3337 skb_set_queue_mapping(skb, queue_index);
3338 return netdev_get_tx_queue(dev, queue_index);
3339}
3340
d29f749e 3341/**
9d08dd3d 3342 * __dev_queue_xmit - transmit a buffer
d29f749e 3343 * @skb: buffer to transmit
9d08dd3d 3344 * @accel_priv: private data used for L2 forwarding offload
d29f749e
DJ
3345 *
3346 * Queue a buffer for transmission to a network device. The caller must
3347 * have set the device and priority and built the buffer before calling
3348 * this function. The function can be called from an interrupt.
3349 *
3350 * A negative errno code is returned on a failure. A success does not
3351 * guarantee the frame will be transmitted as it may be dropped due
3352 * to congestion or traffic shaping.
3353 *
3354 * -----------------------------------------------------------------------------------
3355 * I notice this method can also return errors from the queue disciplines,
3356 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3357 * be positive.
3358 *
3359 * Regardless of the return value, the skb is consumed, so it is currently
3360 * difficult to retry a send to this method. (You can bump the ref count
3361 * before sending to hold a reference for retry if you are careful.)
3362 *
3363 * When calling this method, interrupts MUST be enabled. This is because
3364 * the BH enable code must have IRQs enabled so that it will not deadlock.
3365 * --BLG
3366 */
0a59f3a9 3367static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
1da177e4
LT
3368{
3369 struct net_device *dev = skb->dev;
dc2b4847 3370 struct netdev_queue *txq;
1da177e4
LT
3371 struct Qdisc *q;
3372 int rc = -ENOMEM;
3373
6d1ccff6
ED
3374 skb_reset_mac_header(skb);
3375
e7fd2885
WB
3376 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3377 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3378
4ec93edb
YH
3379 /* Disable soft irqs for various locks below. Also
3380 * stops preemption for RCU.
1da177e4 3381 */
4ec93edb 3382 rcu_read_lock_bh();
1da177e4 3383
5bc1421e
NH
3384 skb_update_prio(skb);
3385
1f211a1b
DB
3386 qdisc_pkt_len_init(skb);
3387#ifdef CONFIG_NET_CLS_ACT
8dc07fdb 3388 skb->tc_at_ingress = 0;
1f211a1b
DB
3389# ifdef CONFIG_NET_EGRESS
3390 if (static_key_false(&egress_needed)) {
3391 skb = sch_handle_egress(skb, &rc, dev);
3392 if (!skb)
3393 goto out;
3394 }
3395# endif
3396#endif
02875878
ED
3397 /* If device/qdisc don't need skb->dst, release it right now while
3398 * its hot in this cpu cache.
3399 */
3400 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3401 skb_dst_drop(skb);
3402 else
3403 skb_dst_force(skb);
3404
f663dd9a 3405 txq = netdev_pick_tx(dev, skb, accel_priv);
a898def2 3406 q = rcu_dereference_bh(txq->qdisc);
37437bb2 3407
cf66ba58 3408 trace_net_dev_queue(skb);
1da177e4 3409 if (q->enqueue) {
bbd8a0d3 3410 rc = __dev_xmit_skb(skb, q, dev, txq);
37437bb2 3411 goto out;
1da177e4
LT
3412 }
3413
3414 /* The device has no queue. Common case for software devices:
eb13da1a 3415 * loopback, all the sorts of tunnels...
1da177e4 3416
eb13da1a 3417 * Really, it is unlikely that netif_tx_lock protection is necessary
3418 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3419 * counters.)
3420 * However, it is possible, that they rely on protection
3421 * made by us here.
1da177e4 3422
eb13da1a 3423 * Check this and shot the lock. It is not prone from deadlocks.
3424 *Either shot noqueue qdisc, it is even simpler 8)
1da177e4
LT
3425 */
3426 if (dev->flags & IFF_UP) {
3427 int cpu = smp_processor_id(); /* ok because BHs are off */
3428
c773e847 3429 if (txq->xmit_lock_owner != cpu) {
a70b506e
DB
3430 if (unlikely(__this_cpu_read(xmit_recursion) >
3431 XMIT_RECURSION_LIMIT))
745e20f1
ED
3432 goto recursion_alert;
3433
1f59533f
JDB
3434 skb = validate_xmit_skb(skb, dev);
3435 if (!skb)
d21fd63e 3436 goto out;
1f59533f 3437
c773e847 3438 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 3439
73466498 3440 if (!netif_xmit_stopped(txq)) {
745e20f1 3441 __this_cpu_inc(xmit_recursion);
ce93718f 3442 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
745e20f1 3443 __this_cpu_dec(xmit_recursion);
572a9d7b 3444 if (dev_xmit_complete(rc)) {
c773e847 3445 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
3446 goto out;
3447 }
3448 }
c773e847 3449 HARD_TX_UNLOCK(dev, txq);
e87cc472
JP
3450 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3451 dev->name);
1da177e4
LT
3452 } else {
3453 /* Recursion is detected! It is possible,
745e20f1
ED
3454 * unfortunately
3455 */
3456recursion_alert:
e87cc472
JP
3457 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3458 dev->name);
1da177e4
LT
3459 }
3460 }
3461
3462 rc = -ENETDOWN;
d4828d85 3463 rcu_read_unlock_bh();
1da177e4 3464
015f0688 3465 atomic_long_inc(&dev->tx_dropped);
1f59533f 3466 kfree_skb_list(skb);
1da177e4
LT
3467 return rc;
3468out:
d4828d85 3469 rcu_read_unlock_bh();
1da177e4
LT
3470 return rc;
3471}
f663dd9a 3472
2b4aa3ce 3473int dev_queue_xmit(struct sk_buff *skb)
f663dd9a
JW
3474{
3475 return __dev_queue_xmit(skb, NULL);
3476}
2b4aa3ce 3477EXPORT_SYMBOL(dev_queue_xmit);
1da177e4 3478
f663dd9a
JW
3479int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
3480{
3481 return __dev_queue_xmit(skb, accel_priv);
3482}
3483EXPORT_SYMBOL(dev_queue_xmit_accel);
3484
1da177e4 3485
eb13da1a 3486/*************************************************************************
3487 * Receiver routines
3488 *************************************************************************/
1da177e4 3489
6b2bedc3 3490int netdev_max_backlog __read_mostly = 1000;
c9e6bc64
ED
3491EXPORT_SYMBOL(netdev_max_backlog);
3492
3b098e2d 3493int netdev_tstamp_prequeue __read_mostly = 1;
6b2bedc3 3494int netdev_budget __read_mostly = 300;
7acf8a1e 3495unsigned int __read_mostly netdev_budget_usecs = 2000;
3d48b53f
MT
3496int weight_p __read_mostly = 64; /* old backlog weight */
3497int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
3498int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
3499int dev_rx_weight __read_mostly = 64;
3500int dev_tx_weight __read_mostly = 64;
1da177e4 3501
eecfd7c4
ED
3502/* Called with irq disabled */
3503static inline void ____napi_schedule(struct softnet_data *sd,
3504 struct napi_struct *napi)
3505{
3506 list_add_tail(&napi->poll_list, &sd->poll_list);
3507 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3508}
3509
bfb564e7
KK
3510#ifdef CONFIG_RPS
3511
3512/* One global table that all flow-based protocols share. */
6e3f7faf 3513struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
bfb564e7 3514EXPORT_SYMBOL(rps_sock_flow_table);
567e4b79
ED
3515u32 rps_cpu_mask __read_mostly;
3516EXPORT_SYMBOL(rps_cpu_mask);
bfb564e7 3517
c5905afb 3518struct static_key rps_needed __read_mostly;
3df97ba8 3519EXPORT_SYMBOL(rps_needed);
13bfff25
ED
3520struct static_key rfs_needed __read_mostly;
3521EXPORT_SYMBOL(rfs_needed);
adc9300e 3522
c445477d
BH
3523static struct rps_dev_flow *
3524set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3525 struct rps_dev_flow *rflow, u16 next_cpu)
3526{
a31196b0 3527 if (next_cpu < nr_cpu_ids) {
c445477d
BH
3528#ifdef CONFIG_RFS_ACCEL
3529 struct netdev_rx_queue *rxqueue;
3530 struct rps_dev_flow_table *flow_table;
3531 struct rps_dev_flow *old_rflow;
3532 u32 flow_id;
3533 u16 rxq_index;
3534 int rc;
3535
3536 /* Should we steer this flow to a different hardware queue? */
69a19ee6
BH
3537 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3538 !(dev->features & NETIF_F_NTUPLE))
c445477d
BH
3539 goto out;
3540 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3541 if (rxq_index == skb_get_rx_queue(skb))
3542 goto out;
3543
3544 rxqueue = dev->_rx + rxq_index;
3545 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3546 if (!flow_table)
3547 goto out;
61b905da 3548 flow_id = skb_get_hash(skb) & flow_table->mask;
c445477d
BH
3549 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3550 rxq_index, flow_id);
3551 if (rc < 0)
3552 goto out;
3553 old_rflow = rflow;
3554 rflow = &flow_table->flows[flow_id];
c445477d
BH
3555 rflow->filter = rc;
3556 if (old_rflow->filter == rflow->filter)
3557 old_rflow->filter = RPS_NO_FILTER;
3558 out:
3559#endif
3560 rflow->last_qtail =
09994d1b 3561 per_cpu(softnet_data, next_cpu).input_queue_head;
c445477d
BH
3562 }
3563
09994d1b 3564 rflow->cpu = next_cpu;
c445477d
BH
3565 return rflow;
3566}
3567
bfb564e7
KK
3568/*
3569 * get_rps_cpu is called from netif_receive_skb and returns the target
3570 * CPU from the RPS map of the receiving queue for a given skb.
3571 * rcu_read_lock must be held on entry.
3572 */
3573static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3574 struct rps_dev_flow **rflowp)
3575{
567e4b79
ED
3576 const struct rps_sock_flow_table *sock_flow_table;
3577 struct netdev_rx_queue *rxqueue = dev->_rx;
bfb564e7 3578 struct rps_dev_flow_table *flow_table;
567e4b79 3579 struct rps_map *map;
bfb564e7 3580 int cpu = -1;
567e4b79 3581 u32 tcpu;
61b905da 3582 u32 hash;
bfb564e7
KK
3583
3584 if (skb_rx_queue_recorded(skb)) {
3585 u16 index = skb_get_rx_queue(skb);
567e4b79 3586
62fe0b40
BH
3587 if (unlikely(index >= dev->real_num_rx_queues)) {
3588 WARN_ONCE(dev->real_num_rx_queues > 1,
3589 "%s received packet on queue %u, but number "
3590 "of RX queues is %u\n",
3591 dev->name, index, dev->real_num_rx_queues);
bfb564e7
KK
3592 goto done;
3593 }
567e4b79
ED
3594 rxqueue += index;
3595 }
bfb564e7 3596
567e4b79
ED
3597 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
3598
3599 flow_table = rcu_dereference(rxqueue->rps_flow_table);
6e3f7faf 3600 map = rcu_dereference(rxqueue->rps_map);
567e4b79 3601 if (!flow_table && !map)
bfb564e7
KK
3602 goto done;
3603
2d47b459 3604 skb_reset_network_header(skb);
61b905da
TH
3605 hash = skb_get_hash(skb);
3606 if (!hash)
bfb564e7
KK
3607 goto done;
3608
fec5e652
TH
3609 sock_flow_table = rcu_dereference(rps_sock_flow_table);
3610 if (flow_table && sock_flow_table) {
fec5e652 3611 struct rps_dev_flow *rflow;
567e4b79
ED
3612 u32 next_cpu;
3613 u32 ident;
3614
3615 /* First check into global flow table if there is a match */
3616 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
3617 if ((ident ^ hash) & ~rps_cpu_mask)
3618 goto try_rps;
fec5e652 3619
567e4b79
ED
3620 next_cpu = ident & rps_cpu_mask;
3621
3622 /* OK, now we know there is a match,
3623 * we can look at the local (per receive queue) flow table
3624 */
61b905da 3625 rflow = &flow_table->flows[hash & flow_table->mask];
fec5e652
TH
3626 tcpu = rflow->cpu;
3627
fec5e652
TH
3628 /*
3629 * If the desired CPU (where last recvmsg was done) is
3630 * different from current CPU (one in the rx-queue flow
3631 * table entry), switch if one of the following holds:
a31196b0 3632 * - Current CPU is unset (>= nr_cpu_ids).
fec5e652
TH
3633 * - Current CPU is offline.
3634 * - The current CPU's queue tail has advanced beyond the
3635 * last packet that was enqueued using this table entry.
3636 * This guarantees that all previous packets for the flow
3637 * have been dequeued, thus preserving in order delivery.
3638 */
3639 if (unlikely(tcpu != next_cpu) &&
a31196b0 3640 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
fec5e652 3641 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
baefa31d
TH
3642 rflow->last_qtail)) >= 0)) {
3643 tcpu = next_cpu;
c445477d 3644 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
baefa31d 3645 }
c445477d 3646
a31196b0 3647 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
fec5e652
TH
3648 *rflowp = rflow;
3649 cpu = tcpu;
3650 goto done;
3651 }
3652 }
3653
567e4b79
ED
3654try_rps:
3655
0a9627f2 3656 if (map) {
8fc54f68 3657 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
0a9627f2
TH
3658 if (cpu_online(tcpu)) {
3659 cpu = tcpu;
3660 goto done;
3661 }
3662 }
3663
3664done:
0a9627f2
TH
3665 return cpu;
3666}
3667
c445477d
BH
3668#ifdef CONFIG_RFS_ACCEL
3669
3670/**
3671 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3672 * @dev: Device on which the filter was set
3673 * @rxq_index: RX queue index
3674 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3675 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3676 *
3677 * Drivers that implement ndo_rx_flow_steer() should periodically call
3678 * this function for each installed filter and remove the filters for
3679 * which it returns %true.
3680 */
3681bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3682 u32 flow_id, u16 filter_id)
3683{
3684 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3685 struct rps_dev_flow_table *flow_table;
3686 struct rps_dev_flow *rflow;
3687 bool expire = true;
a31196b0 3688 unsigned int cpu;
c445477d
BH
3689
3690 rcu_read_lock();
3691 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3692 if (flow_table && flow_id <= flow_table->mask) {
3693 rflow = &flow_table->flows[flow_id];
3694 cpu = ACCESS_ONCE(rflow->cpu);
a31196b0 3695 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
c445477d
BH
3696 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3697 rflow->last_qtail) <
3698 (int)(10 * flow_table->mask)))
3699 expire = false;
3700 }
3701 rcu_read_unlock();
3702 return expire;
3703}
3704EXPORT_SYMBOL(rps_may_expire_flow);
3705
3706#endif /* CONFIG_RFS_ACCEL */
3707
0a9627f2 3708/* Called from hardirq (IPI) context */
e36fa2f7 3709static void rps_trigger_softirq(void *data)
0a9627f2 3710{
e36fa2f7
ED
3711 struct softnet_data *sd = data;
3712
eecfd7c4 3713 ____napi_schedule(sd, &sd->backlog);
dee42870 3714 sd->received_rps++;
0a9627f2 3715}
e36fa2f7 3716
fec5e652 3717#endif /* CONFIG_RPS */
0a9627f2 3718
e36fa2f7
ED
3719/*
3720 * Check if this softnet_data structure is another cpu one
3721 * If yes, queue it to our IPI list and return 1
3722 * If no, return 0
3723 */
3724static int rps_ipi_queued(struct softnet_data *sd)
3725{
3726#ifdef CONFIG_RPS
903ceff7 3727 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
e36fa2f7
ED
3728
3729 if (sd != mysd) {
3730 sd->rps_ipi_next = mysd->rps_ipi_list;
3731 mysd->rps_ipi_list = sd;
3732
3733 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3734 return 1;
3735 }
3736#endif /* CONFIG_RPS */
3737 return 0;
3738}
3739
99bbc707
WB
3740#ifdef CONFIG_NET_FLOW_LIMIT
3741int netdev_flow_limit_table_len __read_mostly = (1 << 12);
3742#endif
3743
3744static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
3745{
3746#ifdef CONFIG_NET_FLOW_LIMIT
3747 struct sd_flow_limit *fl;
3748 struct softnet_data *sd;
3749 unsigned int old_flow, new_flow;
3750
3751 if (qlen < (netdev_max_backlog >> 1))
3752 return false;
3753
903ceff7 3754 sd = this_cpu_ptr(&softnet_data);
99bbc707
WB
3755
3756 rcu_read_lock();
3757 fl = rcu_dereference(sd->flow_limit);
3758 if (fl) {
3958afa1 3759 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
99bbc707
WB
3760 old_flow = fl->history[fl->history_head];
3761 fl->history[fl->history_head] = new_flow;
3762
3763 fl->history_head++;
3764 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
3765
3766 if (likely(fl->buckets[old_flow]))
3767 fl->buckets[old_flow]--;
3768
3769 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
3770 fl->count++;
3771 rcu_read_unlock();
3772 return true;
3773 }
3774 }
3775 rcu_read_unlock();
3776#endif
3777 return false;
3778}
3779
0a9627f2
TH
3780/*
3781 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3782 * queue (may be a remote CPU queue).
3783 */
fec5e652
TH
3784static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3785 unsigned int *qtail)
0a9627f2 3786{
e36fa2f7 3787 struct softnet_data *sd;
0a9627f2 3788 unsigned long flags;
99bbc707 3789 unsigned int qlen;
0a9627f2 3790
e36fa2f7 3791 sd = &per_cpu(softnet_data, cpu);
0a9627f2
TH
3792
3793 local_irq_save(flags);
0a9627f2 3794
e36fa2f7 3795 rps_lock(sd);
e9e4dd32
JA
3796 if (!netif_running(skb->dev))
3797 goto drop;
99bbc707
WB
3798 qlen = skb_queue_len(&sd->input_pkt_queue);
3799 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
e008f3f0 3800 if (qlen) {
0a9627f2 3801enqueue:
e36fa2f7 3802 __skb_queue_tail(&sd->input_pkt_queue, skb);
76cc8b13 3803 input_queue_tail_incr_save(sd, qtail);
e36fa2f7 3804 rps_unlock(sd);
152102c7 3805 local_irq_restore(flags);
0a9627f2
TH
3806 return NET_RX_SUCCESS;
3807 }
3808
ebda37c2
ED
3809 /* Schedule NAPI for backlog device
3810 * We can use non atomic operation since we own the queue lock
3811 */
3812 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
e36fa2f7 3813 if (!rps_ipi_queued(sd))
eecfd7c4 3814 ____napi_schedule(sd, &sd->backlog);
0a9627f2
TH
3815 }
3816 goto enqueue;
3817 }
3818
e9e4dd32 3819drop:
dee42870 3820 sd->dropped++;
e36fa2f7 3821 rps_unlock(sd);
0a9627f2 3822
0a9627f2
TH
3823 local_irq_restore(flags);
3824
caf586e5 3825 atomic_long_inc(&skb->dev->rx_dropped);
0a9627f2
TH
3826 kfree_skb(skb);
3827 return NET_RX_DROP;
3828}
1da177e4 3829
ae78dbfa 3830static int netif_rx_internal(struct sk_buff *skb)
1da177e4 3831{
b0e28f1e 3832 int ret;
1da177e4 3833
588f0330 3834 net_timestamp_check(netdev_tstamp_prequeue, skb);
1da177e4 3835
cf66ba58 3836 trace_netif_rx(skb);
df334545 3837#ifdef CONFIG_RPS
c5905afb 3838 if (static_key_false(&rps_needed)) {
fec5e652 3839 struct rps_dev_flow voidflow, *rflow = &voidflow;
b0e28f1e
ED
3840 int cpu;
3841
cece1945 3842 preempt_disable();
b0e28f1e 3843 rcu_read_lock();
fec5e652
TH
3844
3845 cpu = get_rps_cpu(skb->dev, skb, &rflow);
b0e28f1e
ED
3846 if (cpu < 0)
3847 cpu = smp_processor_id();
fec5e652
TH
3848
3849 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3850
b0e28f1e 3851 rcu_read_unlock();
cece1945 3852 preempt_enable();
adc9300e
ED
3853 } else
3854#endif
fec5e652
TH
3855 {
3856 unsigned int qtail;
f4563a75 3857
fec5e652
TH
3858 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
3859 put_cpu();
3860 }
b0e28f1e 3861 return ret;
1da177e4 3862}
ae78dbfa
BH
3863
3864/**
3865 * netif_rx - post buffer to the network code
3866 * @skb: buffer to post
3867 *
3868 * This function receives a packet from a device driver and queues it for
3869 * the upper (protocol) levels to process. It always succeeds. The buffer
3870 * may be dropped during processing for congestion control or by the
3871 * protocol layers.
3872 *
3873 * return values:
3874 * NET_RX_SUCCESS (no congestion)
3875 * NET_RX_DROP (packet was dropped)
3876 *
3877 */
3878
3879int netif_rx(struct sk_buff *skb)
3880{
3881 trace_netif_rx_entry(skb);
3882
3883 return netif_rx_internal(skb);
3884}
d1b19dff 3885EXPORT_SYMBOL(netif_rx);
1da177e4
LT
3886
3887int netif_rx_ni(struct sk_buff *skb)
3888{
3889 int err;
3890
ae78dbfa
BH
3891 trace_netif_rx_ni_entry(skb);
3892
1da177e4 3893 preempt_disable();
ae78dbfa 3894 err = netif_rx_internal(skb);
1da177e4
LT
3895 if (local_softirq_pending())
3896 do_softirq();
3897 preempt_enable();
3898
3899 return err;
3900}
1da177e4
LT
3901EXPORT_SYMBOL(netif_rx_ni);
3902
0766f788 3903static __latent_entropy void net_tx_action(struct softirq_action *h)
1da177e4 3904{
903ceff7 3905 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
1da177e4
LT
3906
3907 if (sd->completion_queue) {
3908 struct sk_buff *clist;
3909
3910 local_irq_disable();
3911 clist = sd->completion_queue;
3912 sd->completion_queue = NULL;
3913 local_irq_enable();
3914
3915 while (clist) {
3916 struct sk_buff *skb = clist;
f4563a75 3917
1da177e4
LT
3918 clist = clist->next;
3919
547b792c 3920 WARN_ON(atomic_read(&skb->users));
e6247027
ED
3921 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
3922 trace_consume_skb(skb);
3923 else
3924 trace_kfree_skb(skb, net_tx_action);
15fad714
JDB
3925
3926 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
3927 __kfree_skb(skb);
3928 else
3929 __kfree_skb_defer(skb);
1da177e4 3930 }
15fad714
JDB
3931
3932 __kfree_skb_flush();
1da177e4
LT
3933 }
3934
3935 if (sd->output_queue) {
37437bb2 3936 struct Qdisc *head;
1da177e4
LT
3937
3938 local_irq_disable();
3939 head = sd->output_queue;
3940 sd->output_queue = NULL;
a9cbd588 3941 sd->output_queue_tailp = &sd->output_queue;
1da177e4
LT
3942 local_irq_enable();
3943
3944 while (head) {
37437bb2
DM
3945 struct Qdisc *q = head;
3946 spinlock_t *root_lock;
3947
1da177e4
LT
3948 head = head->next_sched;
3949
5fb66229 3950 root_lock = qdisc_lock(q);
3bcb846c
ED
3951 spin_lock(root_lock);
3952 /* We need to make sure head->next_sched is read
3953 * before clearing __QDISC_STATE_SCHED
3954 */
3955 smp_mb__before_atomic();
3956 clear_bit(__QDISC_STATE_SCHED, &q->state);
3957 qdisc_run(q);
3958 spin_unlock(root_lock);
1da177e4
LT
3959 }
3960 }
3961}
3962
181402a5 3963#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
da678292
MM
3964/* This hook is defined here for ATM LANE */
3965int (*br_fdb_test_addr_hook)(struct net_device *dev,
3966 unsigned char *addr) __read_mostly;
4fb019a0 3967EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
da678292 3968#endif
1da177e4 3969
1f211a1b
DB
3970static inline struct sk_buff *
3971sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
3972 struct net_device *orig_dev)
f697c3e8 3973{
e7582bab 3974#ifdef CONFIG_NET_CLS_ACT
d2788d34
DB
3975 struct tcf_proto *cl = rcu_dereference_bh(skb->dev->ingress_cl_list);
3976 struct tcf_result cl_res;
24824a09 3977
c9e99fd0
DB
3978 /* If there's at least one ingress present somewhere (so
3979 * we get here via enabled static key), remaining devices
3980 * that are not configured with an ingress qdisc will bail
d2788d34 3981 * out here.
c9e99fd0 3982 */
d2788d34 3983 if (!cl)
4577139b 3984 return skb;
f697c3e8
HX
3985 if (*pt_prev) {
3986 *ret = deliver_skb(skb, *pt_prev, orig_dev);
3987 *pt_prev = NULL;
1da177e4
LT
3988 }
3989
3365495c 3990 qdisc_skb_cb(skb)->pkt_len = skb->len;
8dc07fdb 3991 skb->tc_at_ingress = 1;
24ea591d 3992 qdisc_bstats_cpu_update(cl->q, skb);
c9e99fd0 3993
87d83093 3994 switch (tcf_classify(skb, cl, &cl_res, false)) {
d2788d34
DB
3995 case TC_ACT_OK:
3996 case TC_ACT_RECLASSIFY:
3997 skb->tc_index = TC_H_MIN(cl_res.classid);
3998 break;
3999 case TC_ACT_SHOT:
24ea591d 4000 qdisc_qstats_cpu_drop(cl->q);
8a3a4c6e
ED
4001 kfree_skb(skb);
4002 return NULL;
d2788d34
DB
4003 case TC_ACT_STOLEN:
4004 case TC_ACT_QUEUED:
8a3a4c6e 4005 consume_skb(skb);
d2788d34 4006 return NULL;
27b29f63
AS
4007 case TC_ACT_REDIRECT:
4008 /* skb_mac_header check was done by cls/act_bpf, so
4009 * we can safely push the L2 header back before
4010 * redirecting to another netdev
4011 */
4012 __skb_push(skb, skb->mac_len);
4013 skb_do_redirect(skb);
4014 return NULL;
d2788d34
DB
4015 default:
4016 break;
f697c3e8 4017 }
e7582bab 4018#endif /* CONFIG_NET_CLS_ACT */
e687ad60
PN
4019 return skb;
4020}
1da177e4 4021
24b27fc4
MB
4022/**
4023 * netdev_is_rx_handler_busy - check if receive handler is registered
4024 * @dev: device to check
4025 *
4026 * Check if a receive handler is already registered for a given device.
4027 * Return true if there one.
4028 *
4029 * The caller must hold the rtnl_mutex.
4030 */
4031bool netdev_is_rx_handler_busy(struct net_device *dev)
4032{
4033 ASSERT_RTNL();
4034 return dev && rtnl_dereference(dev->rx_handler);
4035}
4036EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4037
ab95bfe0
JP
4038/**
4039 * netdev_rx_handler_register - register receive handler
4040 * @dev: device to register a handler for
4041 * @rx_handler: receive handler to register
93e2c32b 4042 * @rx_handler_data: data pointer that is used by rx handler
ab95bfe0 4043 *
e227867f 4044 * Register a receive handler for a device. This handler will then be
ab95bfe0
JP
4045 * called from __netif_receive_skb. A negative errno code is returned
4046 * on a failure.
4047 *
4048 * The caller must hold the rtnl_mutex.
8a4eb573
JP
4049 *
4050 * For a general description of rx_handler, see enum rx_handler_result.
ab95bfe0
JP
4051 */
4052int netdev_rx_handler_register(struct net_device *dev,
93e2c32b
JP
4053 rx_handler_func_t *rx_handler,
4054 void *rx_handler_data)
ab95bfe0 4055{
1b7cd004 4056 if (netdev_is_rx_handler_busy(dev))
ab95bfe0
JP
4057 return -EBUSY;
4058
00cfec37 4059 /* Note: rx_handler_data must be set before rx_handler */
93e2c32b 4060 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
ab95bfe0
JP
4061 rcu_assign_pointer(dev->rx_handler, rx_handler);
4062
4063 return 0;
4064}
4065EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4066
4067/**
4068 * netdev_rx_handler_unregister - unregister receive handler
4069 * @dev: device to unregister a handler from
4070 *
166ec369 4071 * Unregister a receive handler from a device.
ab95bfe0
JP
4072 *
4073 * The caller must hold the rtnl_mutex.
4074 */
4075void netdev_rx_handler_unregister(struct net_device *dev)
4076{
4077
4078 ASSERT_RTNL();
a9b3cd7f 4079 RCU_INIT_POINTER(dev->rx_handler, NULL);
00cfec37
ED
4080 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4081 * section has a guarantee to see a non NULL rx_handler_data
4082 * as well.
4083 */
4084 synchronize_net();
a9b3cd7f 4085 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
ab95bfe0
JP
4086}
4087EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4088
b4b9e355
MG
4089/*
4090 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4091 * the special handling of PFMEMALLOC skbs.
4092 */
4093static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4094{
4095 switch (skb->protocol) {
2b8837ae
JP
4096 case htons(ETH_P_ARP):
4097 case htons(ETH_P_IP):
4098 case htons(ETH_P_IPV6):
4099 case htons(ETH_P_8021Q):
4100 case htons(ETH_P_8021AD):
b4b9e355
MG
4101 return true;
4102 default:
4103 return false;
4104 }
4105}
4106
e687ad60
PN
4107static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4108 int *ret, struct net_device *orig_dev)
4109{
e7582bab 4110#ifdef CONFIG_NETFILTER_INGRESS
e687ad60 4111 if (nf_hook_ingress_active(skb)) {
2c1e2703
AC
4112 int ingress_retval;
4113
e687ad60
PN
4114 if (*pt_prev) {
4115 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4116 *pt_prev = NULL;
4117 }
4118
2c1e2703
AC
4119 rcu_read_lock();
4120 ingress_retval = nf_hook_ingress(skb);
4121 rcu_read_unlock();
4122 return ingress_retval;
e687ad60 4123 }
e7582bab 4124#endif /* CONFIG_NETFILTER_INGRESS */
e687ad60
PN
4125 return 0;
4126}
e687ad60 4127
9754e293 4128static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
1da177e4
LT
4129{
4130 struct packet_type *ptype, *pt_prev;
ab95bfe0 4131 rx_handler_func_t *rx_handler;
f2ccd8fa 4132 struct net_device *orig_dev;
8a4eb573 4133 bool deliver_exact = false;
1da177e4 4134 int ret = NET_RX_DROP;
252e3346 4135 __be16 type;
1da177e4 4136
588f0330 4137 net_timestamp_check(!netdev_tstamp_prequeue, skb);
81bbb3d4 4138
cf66ba58 4139 trace_netif_receive_skb(skb);
9b22ea56 4140
cc9bd5ce 4141 orig_dev = skb->dev;
8f903c70 4142
c1d2bbe1 4143 skb_reset_network_header(skb);
fda55eca
ED
4144 if (!skb_transport_header_was_set(skb))
4145 skb_reset_transport_header(skb);
0b5c9db1 4146 skb_reset_mac_len(skb);
1da177e4
LT
4147
4148 pt_prev = NULL;
4149
63d8ea7f 4150another_round:
b6858177 4151 skb->skb_iif = skb->dev->ifindex;
63d8ea7f
DM
4152
4153 __this_cpu_inc(softnet_data.processed);
4154
8ad227ff
PM
4155 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
4156 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
0d5501c1 4157 skb = skb_vlan_untag(skb);
bcc6d479 4158 if (unlikely(!skb))
2c17d27c 4159 goto out;
bcc6d479
JP
4160 }
4161
e7246e12
WB
4162 if (skb_skip_tc_classify(skb))
4163 goto skip_classify;
1da177e4 4164
9754e293 4165 if (pfmemalloc)
b4b9e355
MG
4166 goto skip_taps;
4167
1da177e4 4168 list_for_each_entry_rcu(ptype, &ptype_all, list) {
7866a621
SN
4169 if (pt_prev)
4170 ret = deliver_skb(skb, pt_prev, orig_dev);
4171 pt_prev = ptype;
4172 }
4173
4174 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
4175 if (pt_prev)
4176 ret = deliver_skb(skb, pt_prev, orig_dev);
4177 pt_prev = ptype;
1da177e4
LT
4178 }
4179
b4b9e355 4180skip_taps:
1cf51900 4181#ifdef CONFIG_NET_INGRESS
4577139b 4182 if (static_key_false(&ingress_needed)) {
1f211a1b 4183 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
4577139b 4184 if (!skb)
2c17d27c 4185 goto out;
e687ad60
PN
4186
4187 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
2c17d27c 4188 goto out;
4577139b 4189 }
1cf51900 4190#endif
a5135bcf 4191 skb_reset_tc(skb);
e7246e12 4192skip_classify:
9754e293 4193 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
b4b9e355
MG
4194 goto drop;
4195
df8a39de 4196 if (skb_vlan_tag_present(skb)) {
2425717b
JF
4197 if (pt_prev) {
4198 ret = deliver_skb(skb, pt_prev, orig_dev);
4199 pt_prev = NULL;
4200 }
48cc32d3 4201 if (vlan_do_receive(&skb))
2425717b
JF
4202 goto another_round;
4203 else if (unlikely(!skb))
2c17d27c 4204 goto out;
2425717b
JF
4205 }
4206
48cc32d3 4207 rx_handler = rcu_dereference(skb->dev->rx_handler);
ab95bfe0
JP
4208 if (rx_handler) {
4209 if (pt_prev) {
4210 ret = deliver_skb(skb, pt_prev, orig_dev);
4211 pt_prev = NULL;
4212 }
8a4eb573
JP
4213 switch (rx_handler(&skb)) {
4214 case RX_HANDLER_CONSUMED:
3bc1b1ad 4215 ret = NET_RX_SUCCESS;
2c17d27c 4216 goto out;
8a4eb573 4217 case RX_HANDLER_ANOTHER:
63d8ea7f 4218 goto another_round;
8a4eb573
JP
4219 case RX_HANDLER_EXACT:
4220 deliver_exact = true;
4221 case RX_HANDLER_PASS:
4222 break;
4223 default:
4224 BUG();
4225 }
ab95bfe0 4226 }
1da177e4 4227
df8a39de
JP
4228 if (unlikely(skb_vlan_tag_present(skb))) {
4229 if (skb_vlan_tag_get_id(skb))
d4b812de
ED
4230 skb->pkt_type = PACKET_OTHERHOST;
4231 /* Note: we might in the future use prio bits
4232 * and set skb->priority like in vlan_do_receive()
4233 * For the time being, just ignore Priority Code Point
4234 */
4235 skb->vlan_tci = 0;
4236 }
48cc32d3 4237
7866a621
SN
4238 type = skb->protocol;
4239
63d8ea7f 4240 /* deliver only exact match when indicated */
7866a621
SN
4241 if (likely(!deliver_exact)) {
4242 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4243 &ptype_base[ntohs(type) &
4244 PTYPE_HASH_MASK]);
4245 }
1f3c8804 4246
7866a621
SN
4247 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4248 &orig_dev->ptype_specific);
4249
4250 if (unlikely(skb->dev != orig_dev)) {
4251 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
4252 &skb->dev->ptype_specific);
1da177e4
LT
4253 }
4254
4255 if (pt_prev) {
1080e512 4256 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
0e698bf6 4257 goto drop;
1080e512
MT
4258 else
4259 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1da177e4 4260 } else {
b4b9e355 4261drop:
6e7333d3
JW
4262 if (!deliver_exact)
4263 atomic_long_inc(&skb->dev->rx_dropped);
4264 else
4265 atomic_long_inc(&skb->dev->rx_nohandler);
1da177e4
LT
4266 kfree_skb(skb);
4267 /* Jamal, now you will not able to escape explaining
4268 * me how you were going to use this. :-)
4269 */
4270 ret = NET_RX_DROP;
4271 }
4272
2c17d27c 4273out:
9754e293
DM
4274 return ret;
4275}
4276
4277static int __netif_receive_skb(struct sk_buff *skb)
4278{
4279 int ret;
4280
4281 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
f1083048 4282 unsigned int noreclaim_flag;
9754e293
DM
4283
4284 /*
4285 * PFMEMALLOC skbs are special, they should
4286 * - be delivered to SOCK_MEMALLOC sockets only
4287 * - stay away from userspace
4288 * - have bounded memory usage
4289 *
4290 * Use PF_MEMALLOC as this saves us from propagating the allocation
4291 * context down to all allocation sites.
4292 */
f1083048 4293 noreclaim_flag = memalloc_noreclaim_save();
9754e293 4294 ret = __netif_receive_skb_core(skb, true);
f1083048 4295 memalloc_noreclaim_restore(noreclaim_flag);
9754e293
DM
4296 } else
4297 ret = __netif_receive_skb_core(skb, false);
4298
1da177e4
LT
4299 return ret;
4300}
0a9627f2 4301
b5cdae32
DM
4302static struct static_key generic_xdp_needed __read_mostly;
4303
4304static int generic_xdp_install(struct net_device *dev, struct netdev_xdp *xdp)
4305{
4306 struct bpf_prog *new = xdp->prog;
4307 int ret = 0;
4308
4309 switch (xdp->command) {
4310 case XDP_SETUP_PROG: {
4311 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
4312
4313 rcu_assign_pointer(dev->xdp_prog, new);
4314 if (old)
4315 bpf_prog_put(old);
4316
4317 if (old && !new) {
4318 static_key_slow_dec(&generic_xdp_needed);
4319 } else if (new && !old) {
4320 static_key_slow_inc(&generic_xdp_needed);
4321 dev_disable_lro(dev);
4322 }
4323 break;
4324 }
4325
4326 case XDP_QUERY_PROG:
4327 xdp->prog_attached = !!rcu_access_pointer(dev->xdp_prog);
4328 break;
4329
4330 default:
4331 ret = -EINVAL;
4332 break;
4333 }
4334
4335 return ret;
4336}
4337
4338static u32 netif_receive_generic_xdp(struct sk_buff *skb,
4339 struct bpf_prog *xdp_prog)
4340{
4341 struct xdp_buff xdp;
4342 u32 act = XDP_DROP;
4343 void *orig_data;
4344 int hlen, off;
4345 u32 mac_len;
4346
4347 /* Reinjected packets coming from act_mirred or similar should
4348 * not get XDP generic processing.
4349 */
4350 if (skb_cloned(skb))
4351 return XDP_PASS;
4352
4353 if (skb_linearize(skb))
4354 goto do_drop;
4355
4356 /* The XDP program wants to see the packet starting at the MAC
4357 * header.
4358 */
4359 mac_len = skb->data - skb_mac_header(skb);
4360 hlen = skb_headlen(skb) + mac_len;
4361 xdp.data = skb->data - mac_len;
4362 xdp.data_end = xdp.data + hlen;
4363 xdp.data_hard_start = skb->data - skb_headroom(skb);
4364 orig_data = xdp.data;
4365
4366 act = bpf_prog_run_xdp(xdp_prog, &xdp);
4367
4368 off = xdp.data - orig_data;
4369 if (off > 0)
4370 __skb_pull(skb, off);
4371 else if (off < 0)
4372 __skb_push(skb, -off);
4373
4374 switch (act) {
4375 case XDP_TX:
4376 __skb_push(skb, mac_len);
4377 /* fall through */
4378 case XDP_PASS:
4379 break;
4380
4381 default:
4382 bpf_warn_invalid_xdp_action(act);
4383 /* fall through */
4384 case XDP_ABORTED:
4385 trace_xdp_exception(skb->dev, xdp_prog, act);
4386 /* fall through */
4387 case XDP_DROP:
4388 do_drop:
4389 kfree_skb(skb);
4390 break;
4391 }
4392
4393 return act;
4394}
4395
4396/* When doing generic XDP we have to bypass the qdisc layer and the
4397 * network taps in order to match in-driver-XDP behavior.
4398 */
4399static void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
4400{
4401 struct net_device *dev = skb->dev;
4402 struct netdev_queue *txq;
4403 bool free_skb = true;
4404 int cpu, rc;
4405
4406 txq = netdev_pick_tx(dev, skb, NULL);
4407 cpu = smp_processor_id();
4408 HARD_TX_LOCK(dev, txq, cpu);
4409 if (!netif_xmit_stopped(txq)) {
4410 rc = netdev_start_xmit(skb, dev, txq, 0);
4411 if (dev_xmit_complete(rc))
4412 free_skb = false;
4413 }
4414 HARD_TX_UNLOCK(dev, txq);
4415 if (free_skb) {
4416 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4417 kfree_skb(skb);
4418 }
4419}
4420
ae78dbfa 4421static int netif_receive_skb_internal(struct sk_buff *skb)
0a9627f2 4422{
2c17d27c
JA
4423 int ret;
4424
588f0330 4425 net_timestamp_check(netdev_tstamp_prequeue, skb);
3b098e2d 4426
c1f19b51
RC
4427 if (skb_defer_rx_timestamp(skb))
4428 return NET_RX_SUCCESS;
4429
2c17d27c
JA
4430 rcu_read_lock();
4431
b5cdae32
DM
4432 if (static_key_false(&generic_xdp_needed)) {
4433 struct bpf_prog *xdp_prog = rcu_dereference(skb->dev->xdp_prog);
4434
4435 if (xdp_prog) {
4436 u32 act = netif_receive_generic_xdp(skb, xdp_prog);
4437
4438 if (act != XDP_PASS) {
4439 rcu_read_unlock();
4440 if (act == XDP_TX)
4441 generic_xdp_tx(skb, xdp_prog);
4442 return NET_RX_DROP;
4443 }
4444 }
4445 }
4446
df334545 4447#ifdef CONFIG_RPS
c5905afb 4448 if (static_key_false(&rps_needed)) {
3b098e2d 4449 struct rps_dev_flow voidflow, *rflow = &voidflow;
2c17d27c 4450 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
0a9627f2 4451
3b098e2d
ED
4452 if (cpu >= 0) {
4453 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4454 rcu_read_unlock();
adc9300e 4455 return ret;
3b098e2d 4456 }
fec5e652 4457 }
1e94d72f 4458#endif
2c17d27c
JA
4459 ret = __netif_receive_skb(skb);
4460 rcu_read_unlock();
4461 return ret;
0a9627f2 4462}
ae78dbfa
BH
4463
4464/**
4465 * netif_receive_skb - process receive buffer from network
4466 * @skb: buffer to process
4467 *
4468 * netif_receive_skb() is the main receive data processing function.
4469 * It always succeeds. The buffer may be dropped during processing
4470 * for congestion control or by the protocol layers.
4471 *
4472 * This function may only be called from softirq context and interrupts
4473 * should be enabled.
4474 *
4475 * Return values (usually ignored):
4476 * NET_RX_SUCCESS: no congestion
4477 * NET_RX_DROP: packet was dropped
4478 */
04eb4489 4479int netif_receive_skb(struct sk_buff *skb)
ae78dbfa
BH
4480{
4481 trace_netif_receive_skb_entry(skb);
4482
4483 return netif_receive_skb_internal(skb);
4484}
04eb4489 4485EXPORT_SYMBOL(netif_receive_skb);
1da177e4 4486
41852497 4487DEFINE_PER_CPU(struct work_struct, flush_works);
145dd5f9
PA
4488
4489/* Network device is going away, flush any packets still pending */
4490static void flush_backlog(struct work_struct *work)
6e583ce5 4491{
6e583ce5 4492 struct sk_buff *skb, *tmp;
145dd5f9
PA
4493 struct softnet_data *sd;
4494
4495 local_bh_disable();
4496 sd = this_cpu_ptr(&softnet_data);
6e583ce5 4497
145dd5f9 4498 local_irq_disable();
e36fa2f7 4499 rps_lock(sd);
6e7676c1 4500 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
41852497 4501 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
e36fa2f7 4502 __skb_unlink(skb, &sd->input_pkt_queue);
6e583ce5 4503 kfree_skb(skb);
76cc8b13 4504 input_queue_head_incr(sd);
6e583ce5 4505 }
6e7676c1 4506 }
e36fa2f7 4507 rps_unlock(sd);
145dd5f9 4508 local_irq_enable();
6e7676c1
CG
4509
4510 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
41852497 4511 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
6e7676c1
CG
4512 __skb_unlink(skb, &sd->process_queue);
4513 kfree_skb(skb);
76cc8b13 4514 input_queue_head_incr(sd);
6e7676c1
CG
4515 }
4516 }
145dd5f9
PA
4517 local_bh_enable();
4518}
4519
41852497 4520static void flush_all_backlogs(void)
145dd5f9
PA
4521{
4522 unsigned int cpu;
4523
4524 get_online_cpus();
4525
41852497
ED
4526 for_each_online_cpu(cpu)
4527 queue_work_on(cpu, system_highpri_wq,
4528 per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
4529
4530 for_each_online_cpu(cpu)
41852497 4531 flush_work(per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
4532
4533 put_online_cpus();
6e583ce5
SH
4534}
4535
d565b0a1
HX
4536static int napi_gro_complete(struct sk_buff *skb)
4537{
22061d80 4538 struct packet_offload *ptype;
d565b0a1 4539 __be16 type = skb->protocol;
22061d80 4540 struct list_head *head = &offload_base;
d565b0a1
HX
4541 int err = -ENOENT;
4542
c3c7c254
ED
4543 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
4544
fc59f9a3
HX
4545 if (NAPI_GRO_CB(skb)->count == 1) {
4546 skb_shinfo(skb)->gso_size = 0;
d565b0a1 4547 goto out;
fc59f9a3 4548 }
d565b0a1
HX
4549
4550 rcu_read_lock();
4551 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 4552 if (ptype->type != type || !ptype->callbacks.gro_complete)
d565b0a1
HX
4553 continue;
4554
299603e8 4555 err = ptype->callbacks.gro_complete(skb, 0);
d565b0a1
HX
4556 break;
4557 }
4558 rcu_read_unlock();
4559
4560 if (err) {
4561 WARN_ON(&ptype->list == head);
4562 kfree_skb(skb);
4563 return NET_RX_SUCCESS;
4564 }
4565
4566out:
ae78dbfa 4567 return netif_receive_skb_internal(skb);
d565b0a1
HX
4568}
4569
2e71a6f8
ED
4570/* napi->gro_list contains packets ordered by age.
4571 * youngest packets at the head of it.
4572 * Complete skbs in reverse order to reduce latencies.
4573 */
4574void napi_gro_flush(struct napi_struct *napi, bool flush_old)
d565b0a1 4575{
2e71a6f8 4576 struct sk_buff *skb, *prev = NULL;
d565b0a1 4577
2e71a6f8
ED
4578 /* scan list and build reverse chain */
4579 for (skb = napi->gro_list; skb != NULL; skb = skb->next) {
4580 skb->prev = prev;
4581 prev = skb;
4582 }
4583
4584 for (skb = prev; skb; skb = prev) {
d565b0a1 4585 skb->next = NULL;
2e71a6f8
ED
4586
4587 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
4588 return;
4589
4590 prev = skb->prev;
d565b0a1 4591 napi_gro_complete(skb);
2e71a6f8 4592 napi->gro_count--;
d565b0a1
HX
4593 }
4594
4595 napi->gro_list = NULL;
4596}
86cac58b 4597EXPORT_SYMBOL(napi_gro_flush);
d565b0a1 4598
89c5fa33
ED
4599static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb)
4600{
4601 struct sk_buff *p;
4602 unsigned int maclen = skb->dev->hard_header_len;
0b4cec8c 4603 u32 hash = skb_get_hash_raw(skb);
89c5fa33
ED
4604
4605 for (p = napi->gro_list; p; p = p->next) {
4606 unsigned long diffs;
4607
0b4cec8c
TH
4608 NAPI_GRO_CB(p)->flush = 0;
4609
4610 if (hash != skb_get_hash_raw(p)) {
4611 NAPI_GRO_CB(p)->same_flow = 0;
4612 continue;
4613 }
4614
89c5fa33
ED
4615 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
4616 diffs |= p->vlan_tci ^ skb->vlan_tci;
ce87fc6c 4617 diffs |= skb_metadata_dst_cmp(p, skb);
89c5fa33
ED
4618 if (maclen == ETH_HLEN)
4619 diffs |= compare_ether_header(skb_mac_header(p),
a50e233c 4620 skb_mac_header(skb));
89c5fa33
ED
4621 else if (!diffs)
4622 diffs = memcmp(skb_mac_header(p),
a50e233c 4623 skb_mac_header(skb),
89c5fa33
ED
4624 maclen);
4625 NAPI_GRO_CB(p)->same_flow = !diffs;
89c5fa33
ED
4626 }
4627}
4628
299603e8
JC
4629static void skb_gro_reset_offset(struct sk_buff *skb)
4630{
4631 const struct skb_shared_info *pinfo = skb_shinfo(skb);
4632 const skb_frag_t *frag0 = &pinfo->frags[0];
4633
4634 NAPI_GRO_CB(skb)->data_offset = 0;
4635 NAPI_GRO_CB(skb)->frag0 = NULL;
4636 NAPI_GRO_CB(skb)->frag0_len = 0;
4637
4638 if (skb_mac_header(skb) == skb_tail_pointer(skb) &&
4639 pinfo->nr_frags &&
4640 !PageHighMem(skb_frag_page(frag0))) {
4641 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
7cfd5fd5
ED
4642 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
4643 skb_frag_size(frag0),
4644 skb->end - skb->tail);
89c5fa33
ED
4645 }
4646}
4647
a50e233c
ED
4648static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
4649{
4650 struct skb_shared_info *pinfo = skb_shinfo(skb);
4651
4652 BUG_ON(skb->end - skb->tail < grow);
4653
4654 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
4655
4656 skb->data_len -= grow;
4657 skb->tail += grow;
4658
4659 pinfo->frags[0].page_offset += grow;
4660 skb_frag_size_sub(&pinfo->frags[0], grow);
4661
4662 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
4663 skb_frag_unref(skb, 0);
4664 memmove(pinfo->frags, pinfo->frags + 1,
4665 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
4666 }
4667}
4668
bb728820 4669static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1
HX
4670{
4671 struct sk_buff **pp = NULL;
22061d80 4672 struct packet_offload *ptype;
d565b0a1 4673 __be16 type = skb->protocol;
22061d80 4674 struct list_head *head = &offload_base;
0da2afd5 4675 int same_flow;
5b252f0c 4676 enum gro_result ret;
a50e233c 4677 int grow;
d565b0a1 4678
b5cdae32 4679 if (netif_elide_gro(skb->dev))
d565b0a1
HX
4680 goto normal;
4681
89c5fa33
ED
4682 gro_list_prepare(napi, skb);
4683
d565b0a1
HX
4684 rcu_read_lock();
4685 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 4686 if (ptype->type != type || !ptype->callbacks.gro_receive)
d565b0a1
HX
4687 continue;
4688
86911732 4689 skb_set_network_header(skb, skb_gro_offset(skb));
efd9450e 4690 skb_reset_mac_len(skb);
d565b0a1 4691 NAPI_GRO_CB(skb)->same_flow = 0;
d61d072e 4692 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
5d38a079 4693 NAPI_GRO_CB(skb)->free = 0;
fac8e0f5 4694 NAPI_GRO_CB(skb)->encap_mark = 0;
fcd91dd4 4695 NAPI_GRO_CB(skb)->recursion_counter = 0;
a0ca153f 4696 NAPI_GRO_CB(skb)->is_fou = 0;
1530545e 4697 NAPI_GRO_CB(skb)->is_atomic = 1;
15e2396d 4698 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
d565b0a1 4699
662880f4
TH
4700 /* Setup for GRO checksum validation */
4701 switch (skb->ip_summed) {
4702 case CHECKSUM_COMPLETE:
4703 NAPI_GRO_CB(skb)->csum = skb->csum;
4704 NAPI_GRO_CB(skb)->csum_valid = 1;
4705 NAPI_GRO_CB(skb)->csum_cnt = 0;
4706 break;
4707 case CHECKSUM_UNNECESSARY:
4708 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
4709 NAPI_GRO_CB(skb)->csum_valid = 0;
4710 break;
4711 default:
4712 NAPI_GRO_CB(skb)->csum_cnt = 0;
4713 NAPI_GRO_CB(skb)->csum_valid = 0;
4714 }
d565b0a1 4715
f191a1d1 4716 pp = ptype->callbacks.gro_receive(&napi->gro_list, skb);
d565b0a1
HX
4717 break;
4718 }
4719 rcu_read_unlock();
4720
4721 if (&ptype->list == head)
4722 goto normal;
4723
25393d3f
SK
4724 if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) {
4725 ret = GRO_CONSUMED;
4726 goto ok;
4727 }
4728
0da2afd5 4729 same_flow = NAPI_GRO_CB(skb)->same_flow;
5d0d9be8 4730 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
0da2afd5 4731
d565b0a1
HX
4732 if (pp) {
4733 struct sk_buff *nskb = *pp;
4734
4735 *pp = nskb->next;
4736 nskb->next = NULL;
4737 napi_gro_complete(nskb);
4ae5544f 4738 napi->gro_count--;
d565b0a1
HX
4739 }
4740
0da2afd5 4741 if (same_flow)
d565b0a1
HX
4742 goto ok;
4743
600adc18 4744 if (NAPI_GRO_CB(skb)->flush)
d565b0a1 4745 goto normal;
d565b0a1 4746
600adc18
ED
4747 if (unlikely(napi->gro_count >= MAX_GRO_SKBS)) {
4748 struct sk_buff *nskb = napi->gro_list;
4749
4750 /* locate the end of the list to select the 'oldest' flow */
4751 while (nskb->next) {
4752 pp = &nskb->next;
4753 nskb = *pp;
4754 }
4755 *pp = NULL;
4756 nskb->next = NULL;
4757 napi_gro_complete(nskb);
4758 } else {
4759 napi->gro_count++;
4760 }
d565b0a1 4761 NAPI_GRO_CB(skb)->count = 1;
2e71a6f8 4762 NAPI_GRO_CB(skb)->age = jiffies;
29e98242 4763 NAPI_GRO_CB(skb)->last = skb;
86911732 4764 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
d565b0a1
HX
4765 skb->next = napi->gro_list;
4766 napi->gro_list = skb;
5d0d9be8 4767 ret = GRO_HELD;
d565b0a1 4768
ad0f9904 4769pull:
a50e233c
ED
4770 grow = skb_gro_offset(skb) - skb_headlen(skb);
4771 if (grow > 0)
4772 gro_pull_from_frag0(skb, grow);
d565b0a1 4773ok:
5d0d9be8 4774 return ret;
d565b0a1
HX
4775
4776normal:
ad0f9904
HX
4777 ret = GRO_NORMAL;
4778 goto pull;
5d38a079 4779}
96e93eab 4780
bf5a755f
JC
4781struct packet_offload *gro_find_receive_by_type(__be16 type)
4782{
4783 struct list_head *offload_head = &offload_base;
4784 struct packet_offload *ptype;
4785
4786 list_for_each_entry_rcu(ptype, offload_head, list) {
4787 if (ptype->type != type || !ptype->callbacks.gro_receive)
4788 continue;
4789 return ptype;
4790 }
4791 return NULL;
4792}
e27a2f83 4793EXPORT_SYMBOL(gro_find_receive_by_type);
bf5a755f
JC
4794
4795struct packet_offload *gro_find_complete_by_type(__be16 type)
4796{
4797 struct list_head *offload_head = &offload_base;
4798 struct packet_offload *ptype;
4799
4800 list_for_each_entry_rcu(ptype, offload_head, list) {
4801 if (ptype->type != type || !ptype->callbacks.gro_complete)
4802 continue;
4803 return ptype;
4804 }
4805 return NULL;
4806}
e27a2f83 4807EXPORT_SYMBOL(gro_find_complete_by_type);
5d38a079 4808
bb728820 4809static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
5d38a079 4810{
5d0d9be8
HX
4811 switch (ret) {
4812 case GRO_NORMAL:
ae78dbfa 4813 if (netif_receive_skb_internal(skb))
c7c4b3b6
BH
4814 ret = GRO_DROP;
4815 break;
5d38a079 4816
5d0d9be8 4817 case GRO_DROP:
5d38a079
HX
4818 kfree_skb(skb);
4819 break;
5b252f0c 4820
daa86548 4821 case GRO_MERGED_FREE:
ce87fc6c
JG
4822 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) {
4823 skb_dst_drop(skb);
f991bb9d 4824 secpath_reset(skb);
d7e8883c 4825 kmem_cache_free(skbuff_head_cache, skb);
ce87fc6c 4826 } else {
d7e8883c 4827 __kfree_skb(skb);
ce87fc6c 4828 }
daa86548
ED
4829 break;
4830
5b252f0c
BH
4831 case GRO_HELD:
4832 case GRO_MERGED:
25393d3f 4833 case GRO_CONSUMED:
5b252f0c 4834 break;
5d38a079
HX
4835 }
4836
c7c4b3b6 4837 return ret;
5d0d9be8 4838}
5d0d9be8 4839
c7c4b3b6 4840gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
5d0d9be8 4841{
93f93a44 4842 skb_mark_napi_id(skb, napi);
ae78dbfa 4843 trace_napi_gro_receive_entry(skb);
86911732 4844
a50e233c
ED
4845 skb_gro_reset_offset(skb);
4846
89c5fa33 4847 return napi_skb_finish(dev_gro_receive(napi, skb), skb);
d565b0a1
HX
4848}
4849EXPORT_SYMBOL(napi_gro_receive);
4850
d0c2b0d2 4851static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
96e93eab 4852{
93a35f59
ED
4853 if (unlikely(skb->pfmemalloc)) {
4854 consume_skb(skb);
4855 return;
4856 }
96e93eab 4857 __skb_pull(skb, skb_headlen(skb));
2a2a459e
ED
4858 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
4859 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
3701e513 4860 skb->vlan_tci = 0;
66c46d74 4861 skb->dev = napi->dev;
6d152e23 4862 skb->skb_iif = 0;
c3caf119
JC
4863 skb->encapsulation = 0;
4864 skb_shinfo(skb)->gso_type = 0;
e33d0ba8 4865 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
f991bb9d 4866 secpath_reset(skb);
96e93eab
HX
4867
4868 napi->skb = skb;
4869}
96e93eab 4870
76620aaf 4871struct sk_buff *napi_get_frags(struct napi_struct *napi)
5d38a079 4872{
5d38a079 4873 struct sk_buff *skb = napi->skb;
5d38a079
HX
4874
4875 if (!skb) {
fd11a83d 4876 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
e2f9dc3b
ED
4877 if (skb) {
4878 napi->skb = skb;
4879 skb_mark_napi_id(skb, napi);
4880 }
80595d59 4881 }
96e93eab
HX
4882 return skb;
4883}
76620aaf 4884EXPORT_SYMBOL(napi_get_frags);
96e93eab 4885
a50e233c
ED
4886static gro_result_t napi_frags_finish(struct napi_struct *napi,
4887 struct sk_buff *skb,
4888 gro_result_t ret)
96e93eab 4889{
5d0d9be8
HX
4890 switch (ret) {
4891 case GRO_NORMAL:
a50e233c
ED
4892 case GRO_HELD:
4893 __skb_push(skb, ETH_HLEN);
4894 skb->protocol = eth_type_trans(skb, skb->dev);
4895 if (ret == GRO_NORMAL && netif_receive_skb_internal(skb))
c7c4b3b6 4896 ret = GRO_DROP;
86911732 4897 break;
5d38a079 4898
5d0d9be8 4899 case GRO_DROP:
5d0d9be8
HX
4900 case GRO_MERGED_FREE:
4901 napi_reuse_skb(napi, skb);
4902 break;
5b252f0c
BH
4903
4904 case GRO_MERGED:
25393d3f 4905 case GRO_CONSUMED:
5b252f0c 4906 break;
5d0d9be8 4907 }
5d38a079 4908
c7c4b3b6 4909 return ret;
5d38a079 4910}
5d0d9be8 4911
a50e233c
ED
4912/* Upper GRO stack assumes network header starts at gro_offset=0
4913 * Drivers could call both napi_gro_frags() and napi_gro_receive()
4914 * We copy ethernet header into skb->data to have a common layout.
4915 */
4adb9c4a 4916static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
76620aaf
HX
4917{
4918 struct sk_buff *skb = napi->skb;
a50e233c
ED
4919 const struct ethhdr *eth;
4920 unsigned int hlen = sizeof(*eth);
76620aaf
HX
4921
4922 napi->skb = NULL;
4923
a50e233c
ED
4924 skb_reset_mac_header(skb);
4925 skb_gro_reset_offset(skb);
4926
4927 eth = skb_gro_header_fast(skb, 0);
4928 if (unlikely(skb_gro_header_hard(skb, hlen))) {
4929 eth = skb_gro_header_slow(skb, hlen, 0);
4930 if (unlikely(!eth)) {
4da46ceb
AC
4931 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
4932 __func__, napi->dev->name);
a50e233c
ED
4933 napi_reuse_skb(napi, skb);
4934 return NULL;
4935 }
4936 } else {
4937 gro_pull_from_frag0(skb, hlen);
4938 NAPI_GRO_CB(skb)->frag0 += hlen;
4939 NAPI_GRO_CB(skb)->frag0_len -= hlen;
76620aaf 4940 }
a50e233c
ED
4941 __skb_pull(skb, hlen);
4942
4943 /*
4944 * This works because the only protocols we care about don't require
4945 * special handling.
4946 * We'll fix it up properly in napi_frags_finish()
4947 */
4948 skb->protocol = eth->h_proto;
76620aaf 4949
76620aaf
HX
4950 return skb;
4951}
76620aaf 4952
c7c4b3b6 4953gro_result_t napi_gro_frags(struct napi_struct *napi)
5d0d9be8 4954{
76620aaf 4955 struct sk_buff *skb = napi_frags_skb(napi);
5d0d9be8
HX
4956
4957 if (!skb)
c7c4b3b6 4958 return GRO_DROP;
5d0d9be8 4959
ae78dbfa
BH
4960 trace_napi_gro_frags_entry(skb);
4961
89c5fa33 4962 return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
5d0d9be8 4963}
5d38a079
HX
4964EXPORT_SYMBOL(napi_gro_frags);
4965
573e8fca
TH
4966/* Compute the checksum from gro_offset and return the folded value
4967 * after adding in any pseudo checksum.
4968 */
4969__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
4970{
4971 __wsum wsum;
4972 __sum16 sum;
4973
4974 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
4975
4976 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
4977 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
4978 if (likely(!sum)) {
4979 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
4980 !skb->csum_complete_sw)
4981 netdev_rx_csum_fault(skb->dev);
4982 }
4983
4984 NAPI_GRO_CB(skb)->csum = wsum;
4985 NAPI_GRO_CB(skb)->csum_valid = 1;
4986
4987 return sum;
4988}
4989EXPORT_SYMBOL(__skb_gro_checksum_complete);
4990
e326bed2 4991/*
855abcf0 4992 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
e326bed2
ED
4993 * Note: called with local irq disabled, but exits with local irq enabled.
4994 */
4995static void net_rps_action_and_irq_enable(struct softnet_data *sd)
4996{
4997#ifdef CONFIG_RPS
4998 struct softnet_data *remsd = sd->rps_ipi_list;
4999
5000 if (remsd) {
5001 sd->rps_ipi_list = NULL;
5002
5003 local_irq_enable();
5004
5005 /* Send pending IPI's to kick RPS processing on remote cpus. */
5006 while (remsd) {
5007 struct softnet_data *next = remsd->rps_ipi_next;
5008
5009 if (cpu_online(remsd->cpu))
c46fff2a 5010 smp_call_function_single_async(remsd->cpu,
fce8ad15 5011 &remsd->csd);
e326bed2
ED
5012 remsd = next;
5013 }
5014 } else
5015#endif
5016 local_irq_enable();
5017}
5018
d75b1ade
ED
5019static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
5020{
5021#ifdef CONFIG_RPS
5022 return sd->rps_ipi_list != NULL;
5023#else
5024 return false;
5025#endif
5026}
5027
bea3348e 5028static int process_backlog(struct napi_struct *napi, int quota)
1da177e4 5029{
eecfd7c4 5030 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
145dd5f9
PA
5031 bool again = true;
5032 int work = 0;
1da177e4 5033
e326bed2
ED
5034 /* Check if we have pending ipi, its better to send them now,
5035 * not waiting net_rx_action() end.
5036 */
d75b1ade 5037 if (sd_has_rps_ipi_waiting(sd)) {
e326bed2
ED
5038 local_irq_disable();
5039 net_rps_action_and_irq_enable(sd);
5040 }
d75b1ade 5041
3d48b53f 5042 napi->weight = dev_rx_weight;
145dd5f9 5043 while (again) {
1da177e4 5044 struct sk_buff *skb;
6e7676c1
CG
5045
5046 while ((skb = __skb_dequeue(&sd->process_queue))) {
2c17d27c 5047 rcu_read_lock();
6e7676c1 5048 __netif_receive_skb(skb);
2c17d27c 5049 rcu_read_unlock();
76cc8b13 5050 input_queue_head_incr(sd);
145dd5f9 5051 if (++work >= quota)
76cc8b13 5052 return work;
145dd5f9 5053
6e7676c1 5054 }
1da177e4 5055
145dd5f9 5056 local_irq_disable();
e36fa2f7 5057 rps_lock(sd);
11ef7a89 5058 if (skb_queue_empty(&sd->input_pkt_queue)) {
eecfd7c4
ED
5059 /*
5060 * Inline a custom version of __napi_complete().
5061 * only current cpu owns and manipulates this napi,
11ef7a89
TH
5062 * and NAPI_STATE_SCHED is the only possible flag set
5063 * on backlog.
5064 * We can use a plain write instead of clear_bit(),
eecfd7c4
ED
5065 * and we dont need an smp_mb() memory barrier.
5066 */
eecfd7c4 5067 napi->state = 0;
145dd5f9
PA
5068 again = false;
5069 } else {
5070 skb_queue_splice_tail_init(&sd->input_pkt_queue,
5071 &sd->process_queue);
bea3348e 5072 }
e36fa2f7 5073 rps_unlock(sd);
145dd5f9 5074 local_irq_enable();
6e7676c1 5075 }
1da177e4 5076
bea3348e
SH
5077 return work;
5078}
1da177e4 5079
bea3348e
SH
5080/**
5081 * __napi_schedule - schedule for receive
c4ea43c5 5082 * @n: entry to schedule
bea3348e 5083 *
bc9ad166
ED
5084 * The entry's receive function will be scheduled to run.
5085 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
bea3348e 5086 */
b5606c2d 5087void __napi_schedule(struct napi_struct *n)
bea3348e
SH
5088{
5089 unsigned long flags;
1da177e4 5090
bea3348e 5091 local_irq_save(flags);
903ceff7 5092 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
bea3348e 5093 local_irq_restore(flags);
1da177e4 5094}
bea3348e
SH
5095EXPORT_SYMBOL(__napi_schedule);
5096
39e6c820
ED
5097/**
5098 * napi_schedule_prep - check if napi can be scheduled
5099 * @n: napi context
5100 *
5101 * Test if NAPI routine is already running, and if not mark
5102 * it as running. This is used as a condition variable
5103 * insure only one NAPI poll instance runs. We also make
5104 * sure there is no pending NAPI disable.
5105 */
5106bool napi_schedule_prep(struct napi_struct *n)
5107{
5108 unsigned long val, new;
5109
5110 do {
5111 val = READ_ONCE(n->state);
5112 if (unlikely(val & NAPIF_STATE_DISABLE))
5113 return false;
5114 new = val | NAPIF_STATE_SCHED;
5115
5116 /* Sets STATE_MISSED bit if STATE_SCHED was already set
5117 * This was suggested by Alexander Duyck, as compiler
5118 * emits better code than :
5119 * if (val & NAPIF_STATE_SCHED)
5120 * new |= NAPIF_STATE_MISSED;
5121 */
5122 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
5123 NAPIF_STATE_MISSED;
5124 } while (cmpxchg(&n->state, val, new) != val);
5125
5126 return !(val & NAPIF_STATE_SCHED);
5127}
5128EXPORT_SYMBOL(napi_schedule_prep);
5129
bc9ad166
ED
5130/**
5131 * __napi_schedule_irqoff - schedule for receive
5132 * @n: entry to schedule
5133 *
5134 * Variant of __napi_schedule() assuming hard irqs are masked
5135 */
5136void __napi_schedule_irqoff(struct napi_struct *n)
5137{
5138 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
5139}
5140EXPORT_SYMBOL(__napi_schedule_irqoff);
5141
364b6055 5142bool napi_complete_done(struct napi_struct *n, int work_done)
d565b0a1 5143{
39e6c820 5144 unsigned long flags, val, new;
d565b0a1
HX
5145
5146 /*
217f6974
ED
5147 * 1) Don't let napi dequeue from the cpu poll list
5148 * just in case its running on a different cpu.
5149 * 2) If we are busy polling, do nothing here, we have
5150 * the guarantee we will be called later.
d565b0a1 5151 */
217f6974
ED
5152 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
5153 NAPIF_STATE_IN_BUSY_POLL)))
364b6055 5154 return false;
d565b0a1 5155
3b47d303
ED
5156 if (n->gro_list) {
5157 unsigned long timeout = 0;
d75b1ade 5158
3b47d303
ED
5159 if (work_done)
5160 timeout = n->dev->gro_flush_timeout;
5161
5162 if (timeout)
5163 hrtimer_start(&n->timer, ns_to_ktime(timeout),
5164 HRTIMER_MODE_REL_PINNED);
5165 else
5166 napi_gro_flush(n, false);
5167 }
02c1602e 5168 if (unlikely(!list_empty(&n->poll_list))) {
d75b1ade
ED
5169 /* If n->poll_list is not empty, we need to mask irqs */
5170 local_irq_save(flags);
02c1602e 5171 list_del_init(&n->poll_list);
d75b1ade
ED
5172 local_irq_restore(flags);
5173 }
39e6c820
ED
5174
5175 do {
5176 val = READ_ONCE(n->state);
5177
5178 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
5179
5180 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
5181
5182 /* If STATE_MISSED was set, leave STATE_SCHED set,
5183 * because we will call napi->poll() one more time.
5184 * This C code was suggested by Alexander Duyck to help gcc.
5185 */
5186 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
5187 NAPIF_STATE_SCHED;
5188 } while (cmpxchg(&n->state, val, new) != val);
5189
5190 if (unlikely(val & NAPIF_STATE_MISSED)) {
5191 __napi_schedule(n);
5192 return false;
5193 }
5194
364b6055 5195 return true;
d565b0a1 5196}
3b47d303 5197EXPORT_SYMBOL(napi_complete_done);
d565b0a1 5198
af12fa6e 5199/* must be called under rcu_read_lock(), as we dont take a reference */
02d62e86 5200static struct napi_struct *napi_by_id(unsigned int napi_id)
af12fa6e
ET
5201{
5202 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
5203 struct napi_struct *napi;
5204
5205 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
5206 if (napi->napi_id == napi_id)
5207 return napi;
5208
5209 return NULL;
5210}
02d62e86
ED
5211
5212#if defined(CONFIG_NET_RX_BUSY_POLL)
217f6974 5213
ce6aea93 5214#define BUSY_POLL_BUDGET 8
217f6974
ED
5215
5216static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
5217{
5218 int rc;
5219
39e6c820
ED
5220 /* Busy polling means there is a high chance device driver hard irq
5221 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
5222 * set in napi_schedule_prep().
5223 * Since we are about to call napi->poll() once more, we can safely
5224 * clear NAPI_STATE_MISSED.
5225 *
5226 * Note: x86 could use a single "lock and ..." instruction
5227 * to perform these two clear_bit()
5228 */
5229 clear_bit(NAPI_STATE_MISSED, &napi->state);
217f6974
ED
5230 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
5231
5232 local_bh_disable();
5233
5234 /* All we really want here is to re-enable device interrupts.
5235 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
5236 */
5237 rc = napi->poll(napi, BUSY_POLL_BUDGET);
5238 netpoll_poll_unlock(have_poll_lock);
5239 if (rc == BUSY_POLL_BUDGET)
5240 __napi_schedule(napi);
5241 local_bh_enable();
5242 if (local_softirq_pending())
5243 do_softirq();
5244}
5245
7db6b048
SS
5246void napi_busy_loop(unsigned int napi_id,
5247 bool (*loop_end)(void *, unsigned long),
5248 void *loop_end_arg)
02d62e86 5249{
7db6b048 5250 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
217f6974 5251 int (*napi_poll)(struct napi_struct *napi, int budget);
217f6974 5252 void *have_poll_lock = NULL;
02d62e86 5253 struct napi_struct *napi;
217f6974
ED
5254
5255restart:
217f6974 5256 napi_poll = NULL;
02d62e86 5257
2a028ecb 5258 rcu_read_lock();
02d62e86 5259
545cd5e5 5260 napi = napi_by_id(napi_id);
02d62e86
ED
5261 if (!napi)
5262 goto out;
5263
217f6974
ED
5264 preempt_disable();
5265 for (;;) {
2b5cd0df
AD
5266 int work = 0;
5267
2a028ecb 5268 local_bh_disable();
217f6974
ED
5269 if (!napi_poll) {
5270 unsigned long val = READ_ONCE(napi->state);
5271
5272 /* If multiple threads are competing for this napi,
5273 * we avoid dirtying napi->state as much as we can.
5274 */
5275 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
5276 NAPIF_STATE_IN_BUSY_POLL))
5277 goto count;
5278 if (cmpxchg(&napi->state, val,
5279 val | NAPIF_STATE_IN_BUSY_POLL |
5280 NAPIF_STATE_SCHED) != val)
5281 goto count;
5282 have_poll_lock = netpoll_poll_lock(napi);
5283 napi_poll = napi->poll;
5284 }
2b5cd0df
AD
5285 work = napi_poll(napi, BUSY_POLL_BUDGET);
5286 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
217f6974 5287count:
2b5cd0df 5288 if (work > 0)
7db6b048 5289 __NET_ADD_STATS(dev_net(napi->dev),
2b5cd0df 5290 LINUX_MIB_BUSYPOLLRXPACKETS, work);
2a028ecb 5291 local_bh_enable();
02d62e86 5292
7db6b048 5293 if (!loop_end || loop_end(loop_end_arg, start_time))
217f6974 5294 break;
02d62e86 5295
217f6974
ED
5296 if (unlikely(need_resched())) {
5297 if (napi_poll)
5298 busy_poll_stop(napi, have_poll_lock);
5299 preempt_enable();
5300 rcu_read_unlock();
5301 cond_resched();
7db6b048 5302 if (loop_end(loop_end_arg, start_time))
2b5cd0df 5303 return;
217f6974
ED
5304 goto restart;
5305 }
6cdf89b1 5306 cpu_relax();
217f6974
ED
5307 }
5308 if (napi_poll)
5309 busy_poll_stop(napi, have_poll_lock);
5310 preempt_enable();
02d62e86 5311out:
2a028ecb 5312 rcu_read_unlock();
02d62e86 5313}
7db6b048 5314EXPORT_SYMBOL(napi_busy_loop);
02d62e86
ED
5315
5316#endif /* CONFIG_NET_RX_BUSY_POLL */
af12fa6e 5317
149d6ad8 5318static void napi_hash_add(struct napi_struct *napi)
af12fa6e 5319{
d64b5e85
ED
5320 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
5321 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
52bd2d62 5322 return;
af12fa6e 5323
52bd2d62 5324 spin_lock(&napi_hash_lock);
af12fa6e 5325
545cd5e5 5326 /* 0..NR_CPUS range is reserved for sender_cpu use */
52bd2d62 5327 do {
545cd5e5
AD
5328 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
5329 napi_gen_id = MIN_NAPI_ID;
52bd2d62
ED
5330 } while (napi_by_id(napi_gen_id));
5331 napi->napi_id = napi_gen_id;
af12fa6e 5332
52bd2d62
ED
5333 hlist_add_head_rcu(&napi->napi_hash_node,
5334 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
af12fa6e 5335
52bd2d62 5336 spin_unlock(&napi_hash_lock);
af12fa6e 5337}
af12fa6e
ET
5338
5339/* Warning : caller is responsible to make sure rcu grace period
5340 * is respected before freeing memory containing @napi
5341 */
34cbe27e 5342bool napi_hash_del(struct napi_struct *napi)
af12fa6e 5343{
34cbe27e
ED
5344 bool rcu_sync_needed = false;
5345
af12fa6e
ET
5346 spin_lock(&napi_hash_lock);
5347
34cbe27e
ED
5348 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
5349 rcu_sync_needed = true;
af12fa6e 5350 hlist_del_rcu(&napi->napi_hash_node);
34cbe27e 5351 }
af12fa6e 5352 spin_unlock(&napi_hash_lock);
34cbe27e 5353 return rcu_sync_needed;
af12fa6e
ET
5354}
5355EXPORT_SYMBOL_GPL(napi_hash_del);
5356
3b47d303
ED
5357static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
5358{
5359 struct napi_struct *napi;
5360
5361 napi = container_of(timer, struct napi_struct, timer);
39e6c820
ED
5362
5363 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
5364 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
5365 */
5366 if (napi->gro_list && !napi_disable_pending(napi) &&
5367 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
5368 __napi_schedule_irqoff(napi);
3b47d303
ED
5369
5370 return HRTIMER_NORESTART;
5371}
5372
d565b0a1
HX
5373void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
5374 int (*poll)(struct napi_struct *, int), int weight)
5375{
5376 INIT_LIST_HEAD(&napi->poll_list);
3b47d303
ED
5377 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
5378 napi->timer.function = napi_watchdog;
4ae5544f 5379 napi->gro_count = 0;
d565b0a1 5380 napi->gro_list = NULL;
5d38a079 5381 napi->skb = NULL;
d565b0a1 5382 napi->poll = poll;
82dc3c63
ED
5383 if (weight > NAPI_POLL_WEIGHT)
5384 pr_err_once("netif_napi_add() called with weight %d on device %s\n",
5385 weight, dev->name);
d565b0a1
HX
5386 napi->weight = weight;
5387 list_add(&napi->dev_list, &dev->napi_list);
d565b0a1 5388 napi->dev = dev;
5d38a079 5389#ifdef CONFIG_NETPOLL
d565b0a1
HX
5390 napi->poll_owner = -1;
5391#endif
5392 set_bit(NAPI_STATE_SCHED, &napi->state);
93d05d4a 5393 napi_hash_add(napi);
d565b0a1
HX
5394}
5395EXPORT_SYMBOL(netif_napi_add);
5396
3b47d303
ED
5397void napi_disable(struct napi_struct *n)
5398{
5399 might_sleep();
5400 set_bit(NAPI_STATE_DISABLE, &n->state);
5401
5402 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
5403 msleep(1);
2d8bff12
NH
5404 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
5405 msleep(1);
3b47d303
ED
5406
5407 hrtimer_cancel(&n->timer);
5408
5409 clear_bit(NAPI_STATE_DISABLE, &n->state);
5410}
5411EXPORT_SYMBOL(napi_disable);
5412
93d05d4a 5413/* Must be called in process context */
d565b0a1
HX
5414void netif_napi_del(struct napi_struct *napi)
5415{
93d05d4a
ED
5416 might_sleep();
5417 if (napi_hash_del(napi))
5418 synchronize_net();
d7b06636 5419 list_del_init(&napi->dev_list);
76620aaf 5420 napi_free_frags(napi);
d565b0a1 5421
289dccbe 5422 kfree_skb_list(napi->gro_list);
d565b0a1 5423 napi->gro_list = NULL;
4ae5544f 5424 napi->gro_count = 0;
d565b0a1
HX
5425}
5426EXPORT_SYMBOL(netif_napi_del);
5427
726ce70e
HX
5428static int napi_poll(struct napi_struct *n, struct list_head *repoll)
5429{
5430 void *have;
5431 int work, weight;
5432
5433 list_del_init(&n->poll_list);
5434
5435 have = netpoll_poll_lock(n);
5436
5437 weight = n->weight;
5438
5439 /* This NAPI_STATE_SCHED test is for avoiding a race
5440 * with netpoll's poll_napi(). Only the entity which
5441 * obtains the lock and sees NAPI_STATE_SCHED set will
5442 * actually make the ->poll() call. Therefore we avoid
5443 * accidentally calling ->poll() when NAPI is not scheduled.
5444 */
5445 work = 0;
5446 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
5447 work = n->poll(n, weight);
1db19db7 5448 trace_napi_poll(n, work, weight);
726ce70e
HX
5449 }
5450
5451 WARN_ON_ONCE(work > weight);
5452
5453 if (likely(work < weight))
5454 goto out_unlock;
5455
5456 /* Drivers must not modify the NAPI state if they
5457 * consume the entire weight. In such cases this code
5458 * still "owns" the NAPI instance and therefore can
5459 * move the instance around on the list at-will.
5460 */
5461 if (unlikely(napi_disable_pending(n))) {
5462 napi_complete(n);
5463 goto out_unlock;
5464 }
5465
5466 if (n->gro_list) {
5467 /* flush too old packets
5468 * If HZ < 1000, flush all packets.
5469 */
5470 napi_gro_flush(n, HZ >= 1000);
5471 }
5472
001ce546
HX
5473 /* Some drivers may have called napi_schedule
5474 * prior to exhausting their budget.
5475 */
5476 if (unlikely(!list_empty(&n->poll_list))) {
5477 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
5478 n->dev ? n->dev->name : "backlog");
5479 goto out_unlock;
5480 }
5481
726ce70e
HX
5482 list_add_tail(&n->poll_list, repoll);
5483
5484out_unlock:
5485 netpoll_poll_unlock(have);
5486
5487 return work;
5488}
5489
0766f788 5490static __latent_entropy void net_rx_action(struct softirq_action *h)
1da177e4 5491{
903ceff7 5492 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
7acf8a1e
MW
5493 unsigned long time_limit = jiffies +
5494 usecs_to_jiffies(netdev_budget_usecs);
51b0bded 5495 int budget = netdev_budget;
d75b1ade
ED
5496 LIST_HEAD(list);
5497 LIST_HEAD(repoll);
53fb95d3 5498
1da177e4 5499 local_irq_disable();
d75b1ade
ED
5500 list_splice_init(&sd->poll_list, &list);
5501 local_irq_enable();
1da177e4 5502
ceb8d5bf 5503 for (;;) {
bea3348e 5504 struct napi_struct *n;
1da177e4 5505
ceb8d5bf
HX
5506 if (list_empty(&list)) {
5507 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
f52dffe0 5508 goto out;
ceb8d5bf
HX
5509 break;
5510 }
5511
6bd373eb
HX
5512 n = list_first_entry(&list, struct napi_struct, poll_list);
5513 budget -= napi_poll(n, &repoll);
5514
d75b1ade 5515 /* If softirq window is exhausted then punt.
24f8b238
SH
5516 * Allow this to run for 2 jiffies since which will allow
5517 * an average latency of 1.5/HZ.
bea3348e 5518 */
ceb8d5bf
HX
5519 if (unlikely(budget <= 0 ||
5520 time_after_eq(jiffies, time_limit))) {
5521 sd->time_squeeze++;
5522 break;
5523 }
1da177e4 5524 }
d75b1ade 5525
d75b1ade
ED
5526 local_irq_disable();
5527
5528 list_splice_tail_init(&sd->poll_list, &list);
5529 list_splice_tail(&repoll, &list);
5530 list_splice(&list, &sd->poll_list);
5531 if (!list_empty(&sd->poll_list))
5532 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
5533
e326bed2 5534 net_rps_action_and_irq_enable(sd);
f52dffe0
ED
5535out:
5536 __kfree_skb_flush();
1da177e4
LT
5537}
5538
aa9d8560 5539struct netdev_adjacent {
9ff162a8 5540 struct net_device *dev;
5d261913
VF
5541
5542 /* upper master flag, there can only be one master device per list */
9ff162a8 5543 bool master;
5d261913 5544
5d261913
VF
5545 /* counter for the number of times this device was added to us */
5546 u16 ref_nr;
5547
402dae96
VF
5548 /* private field for the users */
5549 void *private;
5550
9ff162a8
JP
5551 struct list_head list;
5552 struct rcu_head rcu;
9ff162a8
JP
5553};
5554
6ea29da1 5555static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
2f268f12 5556 struct list_head *adj_list)
9ff162a8 5557{
5d261913 5558 struct netdev_adjacent *adj;
5d261913 5559
2f268f12 5560 list_for_each_entry(adj, adj_list, list) {
5d261913
VF
5561 if (adj->dev == adj_dev)
5562 return adj;
9ff162a8
JP
5563 }
5564 return NULL;
5565}
5566
f1170fd4
DA
5567static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data)
5568{
5569 struct net_device *dev = data;
5570
5571 return upper_dev == dev;
5572}
5573
9ff162a8
JP
5574/**
5575 * netdev_has_upper_dev - Check if device is linked to an upper device
5576 * @dev: device
5577 * @upper_dev: upper device to check
5578 *
5579 * Find out if a device is linked to specified upper device and return true
5580 * in case it is. Note that this checks only immediate upper device,
5581 * not through a complete stack of devices. The caller must hold the RTNL lock.
5582 */
5583bool netdev_has_upper_dev(struct net_device *dev,
5584 struct net_device *upper_dev)
5585{
5586 ASSERT_RTNL();
5587
f1170fd4
DA
5588 return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
5589 upper_dev);
9ff162a8
JP
5590}
5591EXPORT_SYMBOL(netdev_has_upper_dev);
5592
1a3f060c
DA
5593/**
5594 * netdev_has_upper_dev_all - Check if device is linked to an upper device
5595 * @dev: device
5596 * @upper_dev: upper device to check
5597 *
5598 * Find out if a device is linked to specified upper device and return true
5599 * in case it is. Note that this checks the entire upper device chain.
5600 * The caller must hold rcu lock.
5601 */
5602
1a3f060c
DA
5603bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
5604 struct net_device *upper_dev)
5605{
5606 return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev,
5607 upper_dev);
5608}
5609EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
5610
9ff162a8
JP
5611/**
5612 * netdev_has_any_upper_dev - Check if device is linked to some device
5613 * @dev: device
5614 *
5615 * Find out if a device is linked to an upper device and return true in case
5616 * it is. The caller must hold the RTNL lock.
5617 */
1d143d9f 5618static bool netdev_has_any_upper_dev(struct net_device *dev)
9ff162a8
JP
5619{
5620 ASSERT_RTNL();
5621
f1170fd4 5622 return !list_empty(&dev->adj_list.upper);
9ff162a8 5623}
9ff162a8
JP
5624
5625/**
5626 * netdev_master_upper_dev_get - Get master upper device
5627 * @dev: device
5628 *
5629 * Find a master upper device and return pointer to it or NULL in case
5630 * it's not there. The caller must hold the RTNL lock.
5631 */
5632struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
5633{
aa9d8560 5634 struct netdev_adjacent *upper;
9ff162a8
JP
5635
5636 ASSERT_RTNL();
5637
2f268f12 5638 if (list_empty(&dev->adj_list.upper))
9ff162a8
JP
5639 return NULL;
5640
2f268f12 5641 upper = list_first_entry(&dev->adj_list.upper,
aa9d8560 5642 struct netdev_adjacent, list);
9ff162a8
JP
5643 if (likely(upper->master))
5644 return upper->dev;
5645 return NULL;
5646}
5647EXPORT_SYMBOL(netdev_master_upper_dev_get);
5648
0f524a80
DA
5649/**
5650 * netdev_has_any_lower_dev - Check if device is linked to some device
5651 * @dev: device
5652 *
5653 * Find out if a device is linked to a lower device and return true in case
5654 * it is. The caller must hold the RTNL lock.
5655 */
5656static bool netdev_has_any_lower_dev(struct net_device *dev)
5657{
5658 ASSERT_RTNL();
5659
5660 return !list_empty(&dev->adj_list.lower);
5661}
5662
b6ccba4c
VF
5663void *netdev_adjacent_get_private(struct list_head *adj_list)
5664{
5665 struct netdev_adjacent *adj;
5666
5667 adj = list_entry(adj_list, struct netdev_adjacent, list);
5668
5669 return adj->private;
5670}
5671EXPORT_SYMBOL(netdev_adjacent_get_private);
5672
44a40855
VY
5673/**
5674 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
5675 * @dev: device
5676 * @iter: list_head ** of the current position
5677 *
5678 * Gets the next device from the dev's upper list, starting from iter
5679 * position. The caller must hold RCU read lock.
5680 */
5681struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
5682 struct list_head **iter)
5683{
5684 struct netdev_adjacent *upper;
5685
5686 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5687
5688 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5689
5690 if (&upper->list == &dev->adj_list.upper)
5691 return NULL;
5692
5693 *iter = &upper->list;
5694
5695 return upper->dev;
5696}
5697EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
5698
1a3f060c
DA
5699static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
5700 struct list_head **iter)
5701{
5702 struct netdev_adjacent *upper;
5703
5704 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
5705
5706 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5707
5708 if (&upper->list == &dev->adj_list.upper)
5709 return NULL;
5710
5711 *iter = &upper->list;
5712
5713 return upper->dev;
5714}
5715
5716int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
5717 int (*fn)(struct net_device *dev,
5718 void *data),
5719 void *data)
5720{
5721 struct net_device *udev;
5722 struct list_head *iter;
5723 int ret;
5724
5725 for (iter = &dev->adj_list.upper,
5726 udev = netdev_next_upper_dev_rcu(dev, &iter);
5727 udev;
5728 udev = netdev_next_upper_dev_rcu(dev, &iter)) {
5729 /* first is the upper device itself */
5730 ret = fn(udev, data);
5731 if (ret)
5732 return ret;
5733
5734 /* then look at all of its upper devices */
5735 ret = netdev_walk_all_upper_dev_rcu(udev, fn, data);
5736 if (ret)
5737 return ret;
5738 }
5739
5740 return 0;
5741}
5742EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
5743
31088a11
VF
5744/**
5745 * netdev_lower_get_next_private - Get the next ->private from the
5746 * lower neighbour list
5747 * @dev: device
5748 * @iter: list_head ** of the current position
5749 *
5750 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5751 * list, starting from iter position. The caller must hold either hold the
5752 * RTNL lock or its own locking that guarantees that the neighbour lower
b469139e 5753 * list will remain unchanged.
31088a11
VF
5754 */
5755void *netdev_lower_get_next_private(struct net_device *dev,
5756 struct list_head **iter)
5757{
5758 struct netdev_adjacent *lower;
5759
5760 lower = list_entry(*iter, struct netdev_adjacent, list);
5761
5762 if (&lower->list == &dev->adj_list.lower)
5763 return NULL;
5764
6859e7df 5765 *iter = lower->list.next;
31088a11
VF
5766
5767 return lower->private;
5768}
5769EXPORT_SYMBOL(netdev_lower_get_next_private);
5770
5771/**
5772 * netdev_lower_get_next_private_rcu - Get the next ->private from the
5773 * lower neighbour list, RCU
5774 * variant
5775 * @dev: device
5776 * @iter: list_head ** of the current position
5777 *
5778 * Gets the next netdev_adjacent->private from the dev's lower neighbour
5779 * list, starting from iter position. The caller must hold RCU read lock.
5780 */
5781void *netdev_lower_get_next_private_rcu(struct net_device *dev,
5782 struct list_head **iter)
5783{
5784 struct netdev_adjacent *lower;
5785
5786 WARN_ON_ONCE(!rcu_read_lock_held());
5787
5788 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5789
5790 if (&lower->list == &dev->adj_list.lower)
5791 return NULL;
5792
6859e7df 5793 *iter = &lower->list;
31088a11
VF
5794
5795 return lower->private;
5796}
5797EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
5798
4085ebe8
VY
5799/**
5800 * netdev_lower_get_next - Get the next device from the lower neighbour
5801 * list
5802 * @dev: device
5803 * @iter: list_head ** of the current position
5804 *
5805 * Gets the next netdev_adjacent from the dev's lower neighbour
5806 * list, starting from iter position. The caller must hold RTNL lock or
5807 * its own locking that guarantees that the neighbour lower
b469139e 5808 * list will remain unchanged.
4085ebe8
VY
5809 */
5810void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
5811{
5812 struct netdev_adjacent *lower;
5813
cfdd28be 5814 lower = list_entry(*iter, struct netdev_adjacent, list);
4085ebe8
VY
5815
5816 if (&lower->list == &dev->adj_list.lower)
5817 return NULL;
5818
cfdd28be 5819 *iter = lower->list.next;
4085ebe8
VY
5820
5821 return lower->dev;
5822}
5823EXPORT_SYMBOL(netdev_lower_get_next);
5824
1a3f060c
DA
5825static struct net_device *netdev_next_lower_dev(struct net_device *dev,
5826 struct list_head **iter)
5827{
5828 struct netdev_adjacent *lower;
5829
46b5ab1a 5830 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
1a3f060c
DA
5831
5832 if (&lower->list == &dev->adj_list.lower)
5833 return NULL;
5834
46b5ab1a 5835 *iter = &lower->list;
1a3f060c
DA
5836
5837 return lower->dev;
5838}
5839
5840int netdev_walk_all_lower_dev(struct net_device *dev,
5841 int (*fn)(struct net_device *dev,
5842 void *data),
5843 void *data)
5844{
5845 struct net_device *ldev;
5846 struct list_head *iter;
5847 int ret;
5848
5849 for (iter = &dev->adj_list.lower,
5850 ldev = netdev_next_lower_dev(dev, &iter);
5851 ldev;
5852 ldev = netdev_next_lower_dev(dev, &iter)) {
5853 /* first is the lower device itself */
5854 ret = fn(ldev, data);
5855 if (ret)
5856 return ret;
5857
5858 /* then look at all of its lower devices */
5859 ret = netdev_walk_all_lower_dev(ldev, fn, data);
5860 if (ret)
5861 return ret;
5862 }
5863
5864 return 0;
5865}
5866EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
5867
1a3f060c
DA
5868static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
5869 struct list_head **iter)
5870{
5871 struct netdev_adjacent *lower;
5872
5873 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
5874 if (&lower->list == &dev->adj_list.lower)
5875 return NULL;
5876
5877 *iter = &lower->list;
5878
5879 return lower->dev;
5880}
5881
5882int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
5883 int (*fn)(struct net_device *dev,
5884 void *data),
5885 void *data)
5886{
5887 struct net_device *ldev;
5888 struct list_head *iter;
5889 int ret;
5890
5891 for (iter = &dev->adj_list.lower,
5892 ldev = netdev_next_lower_dev_rcu(dev, &iter);
5893 ldev;
5894 ldev = netdev_next_lower_dev_rcu(dev, &iter)) {
5895 /* first is the lower device itself */
5896 ret = fn(ldev, data);
5897 if (ret)
5898 return ret;
5899
5900 /* then look at all of its lower devices */
5901 ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data);
5902 if (ret)
5903 return ret;
5904 }
5905
5906 return 0;
5907}
5908EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
5909
e001bfad 5910/**
5911 * netdev_lower_get_first_private_rcu - Get the first ->private from the
5912 * lower neighbour list, RCU
5913 * variant
5914 * @dev: device
5915 *
5916 * Gets the first netdev_adjacent->private from the dev's lower neighbour
5917 * list. The caller must hold RCU read lock.
5918 */
5919void *netdev_lower_get_first_private_rcu(struct net_device *dev)
5920{
5921 struct netdev_adjacent *lower;
5922
5923 lower = list_first_or_null_rcu(&dev->adj_list.lower,
5924 struct netdev_adjacent, list);
5925 if (lower)
5926 return lower->private;
5927 return NULL;
5928}
5929EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
5930
9ff162a8
JP
5931/**
5932 * netdev_master_upper_dev_get_rcu - Get master upper device
5933 * @dev: device
5934 *
5935 * Find a master upper device and return pointer to it or NULL in case
5936 * it's not there. The caller must hold the RCU read lock.
5937 */
5938struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
5939{
aa9d8560 5940 struct netdev_adjacent *upper;
9ff162a8 5941
2f268f12 5942 upper = list_first_or_null_rcu(&dev->adj_list.upper,
aa9d8560 5943 struct netdev_adjacent, list);
9ff162a8
JP
5944 if (upper && likely(upper->master))
5945 return upper->dev;
5946 return NULL;
5947}
5948EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
5949
0a59f3a9 5950static int netdev_adjacent_sysfs_add(struct net_device *dev,
3ee32707
VF
5951 struct net_device *adj_dev,
5952 struct list_head *dev_list)
5953{
5954 char linkname[IFNAMSIZ+7];
f4563a75 5955
3ee32707
VF
5956 sprintf(linkname, dev_list == &dev->adj_list.upper ?
5957 "upper_%s" : "lower_%s", adj_dev->name);
5958 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
5959 linkname);
5960}
0a59f3a9 5961static void netdev_adjacent_sysfs_del(struct net_device *dev,
3ee32707
VF
5962 char *name,
5963 struct list_head *dev_list)
5964{
5965 char linkname[IFNAMSIZ+7];
f4563a75 5966
3ee32707
VF
5967 sprintf(linkname, dev_list == &dev->adj_list.upper ?
5968 "upper_%s" : "lower_%s", name);
5969 sysfs_remove_link(&(dev->dev.kobj), linkname);
5970}
5971
7ce64c79
AF
5972static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
5973 struct net_device *adj_dev,
5974 struct list_head *dev_list)
5975{
5976 return (dev_list == &dev->adj_list.upper ||
5977 dev_list == &dev->adj_list.lower) &&
5978 net_eq(dev_net(dev), dev_net(adj_dev));
5979}
3ee32707 5980
5d261913
VF
5981static int __netdev_adjacent_dev_insert(struct net_device *dev,
5982 struct net_device *adj_dev,
7863c054 5983 struct list_head *dev_list,
402dae96 5984 void *private, bool master)
5d261913
VF
5985{
5986 struct netdev_adjacent *adj;
842d67a7 5987 int ret;
5d261913 5988
6ea29da1 5989 adj = __netdev_find_adj(adj_dev, dev_list);
5d261913
VF
5990
5991 if (adj) {
790510d9 5992 adj->ref_nr += 1;
67b62f98
DA
5993 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
5994 dev->name, adj_dev->name, adj->ref_nr);
5995
5d261913
VF
5996 return 0;
5997 }
5998
5999 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
6000 if (!adj)
6001 return -ENOMEM;
6002
6003 adj->dev = adj_dev;
6004 adj->master = master;
790510d9 6005 adj->ref_nr = 1;
402dae96 6006 adj->private = private;
5d261913 6007 dev_hold(adj_dev);
2f268f12 6008
67b62f98
DA
6009 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
6010 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
5d261913 6011
7ce64c79 6012 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
3ee32707 6013 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
5831d66e
VF
6014 if (ret)
6015 goto free_adj;
6016 }
6017
7863c054 6018 /* Ensure that master link is always the first item in list. */
842d67a7
VF
6019 if (master) {
6020 ret = sysfs_create_link(&(dev->dev.kobj),
6021 &(adj_dev->dev.kobj), "master");
6022 if (ret)
5831d66e 6023 goto remove_symlinks;
842d67a7 6024
7863c054 6025 list_add_rcu(&adj->list, dev_list);
842d67a7 6026 } else {
7863c054 6027 list_add_tail_rcu(&adj->list, dev_list);
842d67a7 6028 }
5d261913
VF
6029
6030 return 0;
842d67a7 6031
5831d66e 6032remove_symlinks:
7ce64c79 6033 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
3ee32707 6034 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
842d67a7
VF
6035free_adj:
6036 kfree(adj);
974daef7 6037 dev_put(adj_dev);
842d67a7
VF
6038
6039 return ret;
5d261913
VF
6040}
6041
1d143d9f 6042static void __netdev_adjacent_dev_remove(struct net_device *dev,
6043 struct net_device *adj_dev,
93409033 6044 u16 ref_nr,
1d143d9f 6045 struct list_head *dev_list)
5d261913
VF
6046{
6047 struct netdev_adjacent *adj;
6048
67b62f98
DA
6049 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
6050 dev->name, adj_dev->name, ref_nr);
6051
6ea29da1 6052 adj = __netdev_find_adj(adj_dev, dev_list);
5d261913 6053
2f268f12 6054 if (!adj) {
67b62f98 6055 pr_err("Adjacency does not exist for device %s from %s\n",
2f268f12 6056 dev->name, adj_dev->name);
67b62f98
DA
6057 WARN_ON(1);
6058 return;
2f268f12 6059 }
5d261913 6060
93409033 6061 if (adj->ref_nr > ref_nr) {
67b62f98
DA
6062 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
6063 dev->name, adj_dev->name, ref_nr,
6064 adj->ref_nr - ref_nr);
93409033 6065 adj->ref_nr -= ref_nr;
5d261913
VF
6066 return;
6067 }
6068
842d67a7
VF
6069 if (adj->master)
6070 sysfs_remove_link(&(dev->dev.kobj), "master");
6071
7ce64c79 6072 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
3ee32707 6073 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
5831d66e 6074
5d261913 6075 list_del_rcu(&adj->list);
67b62f98 6076 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
2f268f12 6077 adj_dev->name, dev->name, adj_dev->name);
5d261913
VF
6078 dev_put(adj_dev);
6079 kfree_rcu(adj, rcu);
6080}
6081
1d143d9f 6082static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
6083 struct net_device *upper_dev,
6084 struct list_head *up_list,
6085 struct list_head *down_list,
6086 void *private, bool master)
5d261913
VF
6087{
6088 int ret;
6089
790510d9 6090 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
93409033 6091 private, master);
5d261913
VF
6092 if (ret)
6093 return ret;
6094
790510d9 6095 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
93409033 6096 private, false);
5d261913 6097 if (ret) {
790510d9 6098 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
5d261913
VF
6099 return ret;
6100 }
6101
6102 return 0;
6103}
6104
1d143d9f 6105static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
6106 struct net_device *upper_dev,
93409033 6107 u16 ref_nr,
1d143d9f 6108 struct list_head *up_list,
6109 struct list_head *down_list)
5d261913 6110{
93409033
AC
6111 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
6112 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
5d261913
VF
6113}
6114
1d143d9f 6115static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
6116 struct net_device *upper_dev,
6117 void *private, bool master)
2f268f12 6118{
f1170fd4
DA
6119 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
6120 &dev->adj_list.upper,
6121 &upper_dev->adj_list.lower,
6122 private, master);
5d261913
VF
6123}
6124
1d143d9f 6125static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
6126 struct net_device *upper_dev)
2f268f12 6127{
93409033 6128 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
2f268f12
VF
6129 &dev->adj_list.upper,
6130 &upper_dev->adj_list.lower);
6131}
5d261913 6132
9ff162a8 6133static int __netdev_upper_dev_link(struct net_device *dev,
402dae96 6134 struct net_device *upper_dev, bool master,
29bf24af 6135 void *upper_priv, void *upper_info)
9ff162a8 6136{
0e4ead9d 6137 struct netdev_notifier_changeupper_info changeupper_info;
5d261913 6138 int ret = 0;
9ff162a8
JP
6139
6140 ASSERT_RTNL();
6141
6142 if (dev == upper_dev)
6143 return -EBUSY;
6144
6145 /* To prevent loops, check if dev is not upper device to upper_dev. */
f1170fd4 6146 if (netdev_has_upper_dev(upper_dev, dev))
9ff162a8
JP
6147 return -EBUSY;
6148
f1170fd4 6149 if (netdev_has_upper_dev(dev, upper_dev))
9ff162a8
JP
6150 return -EEXIST;
6151
6152 if (master && netdev_master_upper_dev_get(dev))
6153 return -EBUSY;
6154
0e4ead9d
JP
6155 changeupper_info.upper_dev = upper_dev;
6156 changeupper_info.master = master;
6157 changeupper_info.linking = true;
29bf24af 6158 changeupper_info.upper_info = upper_info;
0e4ead9d 6159
573c7ba0
JP
6160 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, dev,
6161 &changeupper_info.info);
6162 ret = notifier_to_errno(ret);
6163 if (ret)
6164 return ret;
6165
6dffb044 6166 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
402dae96 6167 master);
5d261913
VF
6168 if (ret)
6169 return ret;
9ff162a8 6170
b03804e7
IS
6171 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
6172 &changeupper_info.info);
6173 ret = notifier_to_errno(ret);
6174 if (ret)
f1170fd4 6175 goto rollback;
b03804e7 6176
9ff162a8 6177 return 0;
5d261913 6178
f1170fd4 6179rollback:
2f268f12 6180 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
5d261913
VF
6181
6182 return ret;
9ff162a8
JP
6183}
6184
6185/**
6186 * netdev_upper_dev_link - Add a link to the upper device
6187 * @dev: device
6188 * @upper_dev: new upper device
6189 *
6190 * Adds a link to device which is upper to this one. The caller must hold
6191 * the RTNL lock. On a failure a negative errno code is returned.
6192 * On success the reference counts are adjusted and the function
6193 * returns zero.
6194 */
6195int netdev_upper_dev_link(struct net_device *dev,
6196 struct net_device *upper_dev)
6197{
29bf24af 6198 return __netdev_upper_dev_link(dev, upper_dev, false, NULL, NULL);
9ff162a8
JP
6199}
6200EXPORT_SYMBOL(netdev_upper_dev_link);
6201
6202/**
6203 * netdev_master_upper_dev_link - Add a master link to the upper device
6204 * @dev: device
6205 * @upper_dev: new upper device
6dffb044 6206 * @upper_priv: upper device private
29bf24af 6207 * @upper_info: upper info to be passed down via notifier
9ff162a8
JP
6208 *
6209 * Adds a link to device which is upper to this one. In this case, only
6210 * one master upper device can be linked, although other non-master devices
6211 * might be linked as well. The caller must hold the RTNL lock.
6212 * On a failure a negative errno code is returned. On success the reference
6213 * counts are adjusted and the function returns zero.
6214 */
6215int netdev_master_upper_dev_link(struct net_device *dev,
6dffb044 6216 struct net_device *upper_dev,
29bf24af 6217 void *upper_priv, void *upper_info)
9ff162a8 6218{
29bf24af
JP
6219 return __netdev_upper_dev_link(dev, upper_dev, true,
6220 upper_priv, upper_info);
9ff162a8
JP
6221}
6222EXPORT_SYMBOL(netdev_master_upper_dev_link);
6223
6224/**
6225 * netdev_upper_dev_unlink - Removes a link to upper device
6226 * @dev: device
6227 * @upper_dev: new upper device
6228 *
6229 * Removes a link to device which is upper to this one. The caller must hold
6230 * the RTNL lock.
6231 */
6232void netdev_upper_dev_unlink(struct net_device *dev,
6233 struct net_device *upper_dev)
6234{
0e4ead9d 6235 struct netdev_notifier_changeupper_info changeupper_info;
f4563a75 6236
9ff162a8
JP
6237 ASSERT_RTNL();
6238
0e4ead9d
JP
6239 changeupper_info.upper_dev = upper_dev;
6240 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
6241 changeupper_info.linking = false;
6242
573c7ba0
JP
6243 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, dev,
6244 &changeupper_info.info);
6245
2f268f12 6246 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
5d261913 6247
0e4ead9d
JP
6248 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, dev,
6249 &changeupper_info.info);
9ff162a8
JP
6250}
6251EXPORT_SYMBOL(netdev_upper_dev_unlink);
6252
61bd3857
MS
6253/**
6254 * netdev_bonding_info_change - Dispatch event about slave change
6255 * @dev: device
4a26e453 6256 * @bonding_info: info to dispatch
61bd3857
MS
6257 *
6258 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
6259 * The caller must hold the RTNL lock.
6260 */
6261void netdev_bonding_info_change(struct net_device *dev,
6262 struct netdev_bonding_info *bonding_info)
6263{
6264 struct netdev_notifier_bonding_info info;
6265
6266 memcpy(&info.bonding_info, bonding_info,
6267 sizeof(struct netdev_bonding_info));
6268 call_netdevice_notifiers_info(NETDEV_BONDING_INFO, dev,
6269 &info.info);
6270}
6271EXPORT_SYMBOL(netdev_bonding_info_change);
6272
2ce1ee17 6273static void netdev_adjacent_add_links(struct net_device *dev)
4c75431a
AF
6274{
6275 struct netdev_adjacent *iter;
6276
6277 struct net *net = dev_net(dev);
6278
6279 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 6280 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
6281 continue;
6282 netdev_adjacent_sysfs_add(iter->dev, dev,
6283 &iter->dev->adj_list.lower);
6284 netdev_adjacent_sysfs_add(dev, iter->dev,
6285 &dev->adj_list.upper);
6286 }
6287
6288 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 6289 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
6290 continue;
6291 netdev_adjacent_sysfs_add(iter->dev, dev,
6292 &iter->dev->adj_list.upper);
6293 netdev_adjacent_sysfs_add(dev, iter->dev,
6294 &dev->adj_list.lower);
6295 }
6296}
6297
2ce1ee17 6298static void netdev_adjacent_del_links(struct net_device *dev)
4c75431a
AF
6299{
6300 struct netdev_adjacent *iter;
6301
6302 struct net *net = dev_net(dev);
6303
6304 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 6305 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
6306 continue;
6307 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6308 &iter->dev->adj_list.lower);
6309 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6310 &dev->adj_list.upper);
6311 }
6312
6313 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 6314 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
6315 continue;
6316 netdev_adjacent_sysfs_del(iter->dev, dev->name,
6317 &iter->dev->adj_list.upper);
6318 netdev_adjacent_sysfs_del(dev, iter->dev->name,
6319 &dev->adj_list.lower);
6320 }
6321}
6322
5bb025fa 6323void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
402dae96 6324{
5bb025fa 6325 struct netdev_adjacent *iter;
402dae96 6326
4c75431a
AF
6327 struct net *net = dev_net(dev);
6328
5bb025fa 6329 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 6330 if (!net_eq(net, dev_net(iter->dev)))
4c75431a 6331 continue;
5bb025fa
VF
6332 netdev_adjacent_sysfs_del(iter->dev, oldname,
6333 &iter->dev->adj_list.lower);
6334 netdev_adjacent_sysfs_add(iter->dev, dev,
6335 &iter->dev->adj_list.lower);
6336 }
402dae96 6337
5bb025fa 6338 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 6339 if (!net_eq(net, dev_net(iter->dev)))
4c75431a 6340 continue;
5bb025fa
VF
6341 netdev_adjacent_sysfs_del(iter->dev, oldname,
6342 &iter->dev->adj_list.upper);
6343 netdev_adjacent_sysfs_add(iter->dev, dev,
6344 &iter->dev->adj_list.upper);
6345 }
402dae96 6346}
402dae96
VF
6347
6348void *netdev_lower_dev_get_private(struct net_device *dev,
6349 struct net_device *lower_dev)
6350{
6351 struct netdev_adjacent *lower;
6352
6353 if (!lower_dev)
6354 return NULL;
6ea29da1 6355 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
402dae96
VF
6356 if (!lower)
6357 return NULL;
6358
6359 return lower->private;
6360}
6361EXPORT_SYMBOL(netdev_lower_dev_get_private);
6362
4085ebe8 6363
952fcfd0 6364int dev_get_nest_level(struct net_device *dev)
4085ebe8
VY
6365{
6366 struct net_device *lower = NULL;
6367 struct list_head *iter;
6368 int max_nest = -1;
6369 int nest;
6370
6371 ASSERT_RTNL();
6372
6373 netdev_for_each_lower_dev(dev, lower, iter) {
952fcfd0 6374 nest = dev_get_nest_level(lower);
4085ebe8
VY
6375 if (max_nest < nest)
6376 max_nest = nest;
6377 }
6378
952fcfd0 6379 return max_nest + 1;
4085ebe8
VY
6380}
6381EXPORT_SYMBOL(dev_get_nest_level);
6382
04d48266
JP
6383/**
6384 * netdev_lower_change - Dispatch event about lower device state change
6385 * @lower_dev: device
6386 * @lower_state_info: state to dispatch
6387 *
6388 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
6389 * The caller must hold the RTNL lock.
6390 */
6391void netdev_lower_state_changed(struct net_device *lower_dev,
6392 void *lower_state_info)
6393{
6394 struct netdev_notifier_changelowerstate_info changelowerstate_info;
6395
6396 ASSERT_RTNL();
6397 changelowerstate_info.lower_state_info = lower_state_info;
6398 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, lower_dev,
6399 &changelowerstate_info.info);
6400}
6401EXPORT_SYMBOL(netdev_lower_state_changed);
6402
b6c40d68
PM
6403static void dev_change_rx_flags(struct net_device *dev, int flags)
6404{
d314774c
SH
6405 const struct net_device_ops *ops = dev->netdev_ops;
6406
d2615bf4 6407 if (ops->ndo_change_rx_flags)
d314774c 6408 ops->ndo_change_rx_flags(dev, flags);
b6c40d68
PM
6409}
6410
991fb3f7 6411static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
1da177e4 6412{
b536db93 6413 unsigned int old_flags = dev->flags;
d04a48b0
EB
6414 kuid_t uid;
6415 kgid_t gid;
1da177e4 6416
24023451
PM
6417 ASSERT_RTNL();
6418
dad9b335
WC
6419 dev->flags |= IFF_PROMISC;
6420 dev->promiscuity += inc;
6421 if (dev->promiscuity == 0) {
6422 /*
6423 * Avoid overflow.
6424 * If inc causes overflow, untouch promisc and return error.
6425 */
6426 if (inc < 0)
6427 dev->flags &= ~IFF_PROMISC;
6428 else {
6429 dev->promiscuity -= inc;
7b6cd1ce
JP
6430 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
6431 dev->name);
dad9b335
WC
6432 return -EOVERFLOW;
6433 }
6434 }
52609c0b 6435 if (dev->flags != old_flags) {
7b6cd1ce
JP
6436 pr_info("device %s %s promiscuous mode\n",
6437 dev->name,
6438 dev->flags & IFF_PROMISC ? "entered" : "left");
8192b0c4
DH
6439 if (audit_enabled) {
6440 current_uid_gid(&uid, &gid);
7759db82
KHK
6441 audit_log(current->audit_context, GFP_ATOMIC,
6442 AUDIT_ANOM_PROMISCUOUS,
6443 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
6444 dev->name, (dev->flags & IFF_PROMISC),
6445 (old_flags & IFF_PROMISC),
e1760bd5 6446 from_kuid(&init_user_ns, audit_get_loginuid(current)),
d04a48b0
EB
6447 from_kuid(&init_user_ns, uid),
6448 from_kgid(&init_user_ns, gid),
7759db82 6449 audit_get_sessionid(current));
8192b0c4 6450 }
24023451 6451
b6c40d68 6452 dev_change_rx_flags(dev, IFF_PROMISC);
1da177e4 6453 }
991fb3f7
ND
6454 if (notify)
6455 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
dad9b335 6456 return 0;
1da177e4
LT
6457}
6458
4417da66
PM
6459/**
6460 * dev_set_promiscuity - update promiscuity count on a device
6461 * @dev: device
6462 * @inc: modifier
6463 *
6464 * Add or remove promiscuity from a device. While the count in the device
6465 * remains above zero the interface remains promiscuous. Once it hits zero
6466 * the device reverts back to normal filtering operation. A negative inc
6467 * value is used to drop promiscuity on the device.
dad9b335 6468 * Return 0 if successful or a negative errno code on error.
4417da66 6469 */
dad9b335 6470int dev_set_promiscuity(struct net_device *dev, int inc)
4417da66 6471{
b536db93 6472 unsigned int old_flags = dev->flags;
dad9b335 6473 int err;
4417da66 6474
991fb3f7 6475 err = __dev_set_promiscuity(dev, inc, true);
4b5a698e 6476 if (err < 0)
dad9b335 6477 return err;
4417da66
PM
6478 if (dev->flags != old_flags)
6479 dev_set_rx_mode(dev);
dad9b335 6480 return err;
4417da66 6481}
d1b19dff 6482EXPORT_SYMBOL(dev_set_promiscuity);
4417da66 6483
991fb3f7 6484static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
1da177e4 6485{
991fb3f7 6486 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
1da177e4 6487
24023451
PM
6488 ASSERT_RTNL();
6489
1da177e4 6490 dev->flags |= IFF_ALLMULTI;
dad9b335
WC
6491 dev->allmulti += inc;
6492 if (dev->allmulti == 0) {
6493 /*
6494 * Avoid overflow.
6495 * If inc causes overflow, untouch allmulti and return error.
6496 */
6497 if (inc < 0)
6498 dev->flags &= ~IFF_ALLMULTI;
6499 else {
6500 dev->allmulti -= inc;
7b6cd1ce
JP
6501 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
6502 dev->name);
dad9b335
WC
6503 return -EOVERFLOW;
6504 }
6505 }
24023451 6506 if (dev->flags ^ old_flags) {
b6c40d68 6507 dev_change_rx_flags(dev, IFF_ALLMULTI);
4417da66 6508 dev_set_rx_mode(dev);
991fb3f7
ND
6509 if (notify)
6510 __dev_notify_flags(dev, old_flags,
6511 dev->gflags ^ old_gflags);
24023451 6512 }
dad9b335 6513 return 0;
4417da66 6514}
991fb3f7
ND
6515
6516/**
6517 * dev_set_allmulti - update allmulti count on a device
6518 * @dev: device
6519 * @inc: modifier
6520 *
6521 * Add or remove reception of all multicast frames to a device. While the
6522 * count in the device remains above zero the interface remains listening
6523 * to all interfaces. Once it hits zero the device reverts back to normal
6524 * filtering operation. A negative @inc value is used to drop the counter
6525 * when releasing a resource needing all multicasts.
6526 * Return 0 if successful or a negative errno code on error.
6527 */
6528
6529int dev_set_allmulti(struct net_device *dev, int inc)
6530{
6531 return __dev_set_allmulti(dev, inc, true);
6532}
d1b19dff 6533EXPORT_SYMBOL(dev_set_allmulti);
4417da66
PM
6534
6535/*
6536 * Upload unicast and multicast address lists to device and
6537 * configure RX filtering. When the device doesn't support unicast
53ccaae1 6538 * filtering it is put in promiscuous mode while unicast addresses
4417da66
PM
6539 * are present.
6540 */
6541void __dev_set_rx_mode(struct net_device *dev)
6542{
d314774c
SH
6543 const struct net_device_ops *ops = dev->netdev_ops;
6544
4417da66
PM
6545 /* dev_open will call this function so the list will stay sane. */
6546 if (!(dev->flags&IFF_UP))
6547 return;
6548
6549 if (!netif_device_present(dev))
40b77c94 6550 return;
4417da66 6551
01789349 6552 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
4417da66
PM
6553 /* Unicast addresses changes may only happen under the rtnl,
6554 * therefore calling __dev_set_promiscuity here is safe.
6555 */
32e7bfc4 6556 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
991fb3f7 6557 __dev_set_promiscuity(dev, 1, false);
2d348d1f 6558 dev->uc_promisc = true;
32e7bfc4 6559 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
991fb3f7 6560 __dev_set_promiscuity(dev, -1, false);
2d348d1f 6561 dev->uc_promisc = false;
4417da66 6562 }
4417da66 6563 }
01789349
JP
6564
6565 if (ops->ndo_set_rx_mode)
6566 ops->ndo_set_rx_mode(dev);
4417da66
PM
6567}
6568
6569void dev_set_rx_mode(struct net_device *dev)
6570{
b9e40857 6571 netif_addr_lock_bh(dev);
4417da66 6572 __dev_set_rx_mode(dev);
b9e40857 6573 netif_addr_unlock_bh(dev);
1da177e4
LT
6574}
6575
f0db275a
SH
6576/**
6577 * dev_get_flags - get flags reported to userspace
6578 * @dev: device
6579 *
6580 * Get the combination of flag bits exported through APIs to userspace.
6581 */
95c96174 6582unsigned int dev_get_flags(const struct net_device *dev)
1da177e4 6583{
95c96174 6584 unsigned int flags;
1da177e4
LT
6585
6586 flags = (dev->flags & ~(IFF_PROMISC |
6587 IFF_ALLMULTI |
b00055aa
SR
6588 IFF_RUNNING |
6589 IFF_LOWER_UP |
6590 IFF_DORMANT)) |
1da177e4
LT
6591 (dev->gflags & (IFF_PROMISC |
6592 IFF_ALLMULTI));
6593
b00055aa
SR
6594 if (netif_running(dev)) {
6595 if (netif_oper_up(dev))
6596 flags |= IFF_RUNNING;
6597 if (netif_carrier_ok(dev))
6598 flags |= IFF_LOWER_UP;
6599 if (netif_dormant(dev))
6600 flags |= IFF_DORMANT;
6601 }
1da177e4
LT
6602
6603 return flags;
6604}
d1b19dff 6605EXPORT_SYMBOL(dev_get_flags);
1da177e4 6606
bd380811 6607int __dev_change_flags(struct net_device *dev, unsigned int flags)
1da177e4 6608{
b536db93 6609 unsigned int old_flags = dev->flags;
bd380811 6610 int ret;
1da177e4 6611
24023451
PM
6612 ASSERT_RTNL();
6613
1da177e4
LT
6614 /*
6615 * Set the flags on our device.
6616 */
6617
6618 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
6619 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
6620 IFF_AUTOMEDIA)) |
6621 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
6622 IFF_ALLMULTI));
6623
6624 /*
6625 * Load in the correct multicast list now the flags have changed.
6626 */
6627
b6c40d68
PM
6628 if ((old_flags ^ flags) & IFF_MULTICAST)
6629 dev_change_rx_flags(dev, IFF_MULTICAST);
24023451 6630
4417da66 6631 dev_set_rx_mode(dev);
1da177e4
LT
6632
6633 /*
6634 * Have we downed the interface. We handle IFF_UP ourselves
6635 * according to user attempts to set it, rather than blindly
6636 * setting it.
6637 */
6638
6639 ret = 0;
d215d10f 6640 if ((old_flags ^ flags) & IFF_UP)
bd380811 6641 ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev);
1da177e4 6642
1da177e4 6643 if ((flags ^ dev->gflags) & IFF_PROMISC) {
d1b19dff 6644 int inc = (flags & IFF_PROMISC) ? 1 : -1;
991fb3f7 6645 unsigned int old_flags = dev->flags;
d1b19dff 6646
1da177e4 6647 dev->gflags ^= IFF_PROMISC;
991fb3f7
ND
6648
6649 if (__dev_set_promiscuity(dev, inc, false) >= 0)
6650 if (dev->flags != old_flags)
6651 dev_set_rx_mode(dev);
1da177e4
LT
6652 }
6653
6654 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
eb13da1a 6655 * is important. Some (broken) drivers set IFF_PROMISC, when
6656 * IFF_ALLMULTI is requested not asking us and not reporting.
1da177e4
LT
6657 */
6658 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
d1b19dff
ED
6659 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
6660
1da177e4 6661 dev->gflags ^= IFF_ALLMULTI;
991fb3f7 6662 __dev_set_allmulti(dev, inc, false);
1da177e4
LT
6663 }
6664
bd380811
PM
6665 return ret;
6666}
6667
a528c219
ND
6668void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
6669 unsigned int gchanges)
bd380811
PM
6670{
6671 unsigned int changes = dev->flags ^ old_flags;
6672
a528c219 6673 if (gchanges)
7f294054 6674 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
a528c219 6675
bd380811
PM
6676 if (changes & IFF_UP) {
6677 if (dev->flags & IFF_UP)
6678 call_netdevice_notifiers(NETDEV_UP, dev);
6679 else
6680 call_netdevice_notifiers(NETDEV_DOWN, dev);
6681 }
6682
6683 if (dev->flags & IFF_UP &&
be9efd36
JP
6684 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
6685 struct netdev_notifier_change_info change_info;
6686
6687 change_info.flags_changed = changes;
6688 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
6689 &change_info.info);
6690 }
bd380811
PM
6691}
6692
6693/**
6694 * dev_change_flags - change device settings
6695 * @dev: device
6696 * @flags: device state flags
6697 *
6698 * Change settings on device based state flags. The flags are
6699 * in the userspace exported format.
6700 */
b536db93 6701int dev_change_flags(struct net_device *dev, unsigned int flags)
bd380811 6702{
b536db93 6703 int ret;
991fb3f7 6704 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
bd380811
PM
6705
6706 ret = __dev_change_flags(dev, flags);
6707 if (ret < 0)
6708 return ret;
6709
991fb3f7 6710 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
a528c219 6711 __dev_notify_flags(dev, old_flags, changes);
1da177e4
LT
6712 return ret;
6713}
d1b19dff 6714EXPORT_SYMBOL(dev_change_flags);
1da177e4 6715
2315dc91
VF
6716static int __dev_set_mtu(struct net_device *dev, int new_mtu)
6717{
6718 const struct net_device_ops *ops = dev->netdev_ops;
6719
6720 if (ops->ndo_change_mtu)
6721 return ops->ndo_change_mtu(dev, new_mtu);
6722
6723 dev->mtu = new_mtu;
6724 return 0;
6725}
6726
f0db275a
SH
6727/**
6728 * dev_set_mtu - Change maximum transfer unit
6729 * @dev: device
6730 * @new_mtu: new transfer unit
6731 *
6732 * Change the maximum transfer size of the network device.
6733 */
1da177e4
LT
6734int dev_set_mtu(struct net_device *dev, int new_mtu)
6735{
2315dc91 6736 int err, orig_mtu;
1da177e4
LT
6737
6738 if (new_mtu == dev->mtu)
6739 return 0;
6740
61e84623
JW
6741 /* MTU must be positive, and in range */
6742 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
6743 net_err_ratelimited("%s: Invalid MTU %d requested, hw min %d\n",
6744 dev->name, new_mtu, dev->min_mtu);
1da177e4 6745 return -EINVAL;
61e84623
JW
6746 }
6747
6748 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
6749 net_err_ratelimited("%s: Invalid MTU %d requested, hw max %d\n",
a0e65de7 6750 dev->name, new_mtu, dev->max_mtu);
61e84623
JW
6751 return -EINVAL;
6752 }
1da177e4
LT
6753
6754 if (!netif_device_present(dev))
6755 return -ENODEV;
6756
1d486bfb
VF
6757 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
6758 err = notifier_to_errno(err);
6759 if (err)
6760 return err;
d314774c 6761
2315dc91
VF
6762 orig_mtu = dev->mtu;
6763 err = __dev_set_mtu(dev, new_mtu);
d314774c 6764
2315dc91
VF
6765 if (!err) {
6766 err = call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
6767 err = notifier_to_errno(err);
6768 if (err) {
6769 /* setting mtu back and notifying everyone again,
6770 * so that they have a chance to revert changes.
6771 */
6772 __dev_set_mtu(dev, orig_mtu);
6773 call_netdevice_notifiers(NETDEV_CHANGEMTU, dev);
6774 }
6775 }
1da177e4
LT
6776 return err;
6777}
d1b19dff 6778EXPORT_SYMBOL(dev_set_mtu);
1da177e4 6779
cbda10fa
VD
6780/**
6781 * dev_set_group - Change group this device belongs to
6782 * @dev: device
6783 * @new_group: group this device should belong to
6784 */
6785void dev_set_group(struct net_device *dev, int new_group)
6786{
6787 dev->group = new_group;
6788}
6789EXPORT_SYMBOL(dev_set_group);
6790
f0db275a
SH
6791/**
6792 * dev_set_mac_address - Change Media Access Control Address
6793 * @dev: device
6794 * @sa: new address
6795 *
6796 * Change the hardware (MAC) address of the device
6797 */
1da177e4
LT
6798int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
6799{
d314774c 6800 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
6801 int err;
6802
d314774c 6803 if (!ops->ndo_set_mac_address)
1da177e4
LT
6804 return -EOPNOTSUPP;
6805 if (sa->sa_family != dev->type)
6806 return -EINVAL;
6807 if (!netif_device_present(dev))
6808 return -ENODEV;
d314774c 6809 err = ops->ndo_set_mac_address(dev, sa);
f6521516
JP
6810 if (err)
6811 return err;
fbdeca2d 6812 dev->addr_assign_type = NET_ADDR_SET;
f6521516 6813 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
7bf23575 6814 add_device_randomness(dev->dev_addr, dev->addr_len);
f6521516 6815 return 0;
1da177e4 6816}
d1b19dff 6817EXPORT_SYMBOL(dev_set_mac_address);
1da177e4 6818
4bf84c35
JP
6819/**
6820 * dev_change_carrier - Change device carrier
6821 * @dev: device
691b3b7e 6822 * @new_carrier: new value
4bf84c35
JP
6823 *
6824 * Change device carrier
6825 */
6826int dev_change_carrier(struct net_device *dev, bool new_carrier)
6827{
6828 const struct net_device_ops *ops = dev->netdev_ops;
6829
6830 if (!ops->ndo_change_carrier)
6831 return -EOPNOTSUPP;
6832 if (!netif_device_present(dev))
6833 return -ENODEV;
6834 return ops->ndo_change_carrier(dev, new_carrier);
6835}
6836EXPORT_SYMBOL(dev_change_carrier);
6837
66b52b0d
JP
6838/**
6839 * dev_get_phys_port_id - Get device physical port ID
6840 * @dev: device
6841 * @ppid: port ID
6842 *
6843 * Get device physical port ID
6844 */
6845int dev_get_phys_port_id(struct net_device *dev,
02637fce 6846 struct netdev_phys_item_id *ppid)
66b52b0d
JP
6847{
6848 const struct net_device_ops *ops = dev->netdev_ops;
6849
6850 if (!ops->ndo_get_phys_port_id)
6851 return -EOPNOTSUPP;
6852 return ops->ndo_get_phys_port_id(dev, ppid);
6853}
6854EXPORT_SYMBOL(dev_get_phys_port_id);
6855
db24a904
DA
6856/**
6857 * dev_get_phys_port_name - Get device physical port name
6858 * @dev: device
6859 * @name: port name
ed49e650 6860 * @len: limit of bytes to copy to name
db24a904
DA
6861 *
6862 * Get device physical port name
6863 */
6864int dev_get_phys_port_name(struct net_device *dev,
6865 char *name, size_t len)
6866{
6867 const struct net_device_ops *ops = dev->netdev_ops;
6868
6869 if (!ops->ndo_get_phys_port_name)
6870 return -EOPNOTSUPP;
6871 return ops->ndo_get_phys_port_name(dev, name, len);
6872}
6873EXPORT_SYMBOL(dev_get_phys_port_name);
6874
d746d707
AK
6875/**
6876 * dev_change_proto_down - update protocol port state information
6877 * @dev: device
6878 * @proto_down: new value
6879 *
6880 * This info can be used by switch drivers to set the phys state of the
6881 * port.
6882 */
6883int dev_change_proto_down(struct net_device *dev, bool proto_down)
6884{
6885 const struct net_device_ops *ops = dev->netdev_ops;
6886
6887 if (!ops->ndo_change_proto_down)
6888 return -EOPNOTSUPP;
6889 if (!netif_device_present(dev))
6890 return -ENODEV;
6891 return ops->ndo_change_proto_down(dev, proto_down);
6892}
6893EXPORT_SYMBOL(dev_change_proto_down);
6894
d67b9cd2
DB
6895bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
6896{
6897 struct netdev_xdp xdp;
6898
6899 memset(&xdp, 0, sizeof(xdp));
6900 xdp.command = XDP_QUERY_PROG;
6901
6902 /* Query must always succeed. */
6903 WARN_ON(xdp_op(dev, &xdp) < 0);
6904 return xdp.prog_attached;
6905}
6906
6907static int dev_xdp_install(struct net_device *dev, xdp_op_t xdp_op,
6908 struct netlink_ext_ack *extack,
6909 struct bpf_prog *prog)
6910{
6911 struct netdev_xdp xdp;
6912
6913 memset(&xdp, 0, sizeof(xdp));
6914 xdp.command = XDP_SETUP_PROG;
6915 xdp.extack = extack;
6916 xdp.prog = prog;
6917
6918 return xdp_op(dev, &xdp);
6919}
6920
a7862b45
BB
6921/**
6922 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
6923 * @dev: device
b5d60989 6924 * @extack: netlink extended ack
a7862b45 6925 * @fd: new program fd or negative value to clear
85de8576 6926 * @flags: xdp-related flags
a7862b45
BB
6927 *
6928 * Set or clear a bpf program for a device
6929 */
ddf9f970
JK
6930int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
6931 int fd, u32 flags)
a7862b45
BB
6932{
6933 const struct net_device_ops *ops = dev->netdev_ops;
6934 struct bpf_prog *prog = NULL;
d67b9cd2 6935 xdp_op_t xdp_op, xdp_chk;
a7862b45
BB
6936 int err;
6937
85de8576
DB
6938 ASSERT_RTNL();
6939
d67b9cd2 6940 xdp_op = xdp_chk = ops->ndo_xdp;
0489df9a
DB
6941 if (!xdp_op && (flags & XDP_FLAGS_DRV_MODE))
6942 return -EOPNOTSUPP;
b5cdae32
DM
6943 if (!xdp_op || (flags & XDP_FLAGS_SKB_MODE))
6944 xdp_op = generic_xdp_install;
d67b9cd2
DB
6945 if (xdp_op == xdp_chk)
6946 xdp_chk = generic_xdp_install;
b5cdae32 6947
a7862b45 6948 if (fd >= 0) {
d67b9cd2
DB
6949 if (xdp_chk && __dev_xdp_attached(dev, xdp_chk))
6950 return -EEXIST;
6951 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
6952 __dev_xdp_attached(dev, xdp_op))
6953 return -EBUSY;
85de8576 6954
a7862b45
BB
6955 prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
6956 if (IS_ERR(prog))
6957 return PTR_ERR(prog);
6958 }
6959
d67b9cd2 6960 err = dev_xdp_install(dev, xdp_op, extack, prog);
a7862b45
BB
6961 if (err < 0 && prog)
6962 bpf_prog_put(prog);
6963
6964 return err;
6965}
a7862b45 6966
1da177e4
LT
6967/**
6968 * dev_new_index - allocate an ifindex
c4ea43c5 6969 * @net: the applicable net namespace
1da177e4
LT
6970 *
6971 * Returns a suitable unique value for a new device interface
6972 * number. The caller must hold the rtnl semaphore or the
6973 * dev_base_lock to be sure it remains unique.
6974 */
881d966b 6975static int dev_new_index(struct net *net)
1da177e4 6976{
aa79e66e 6977 int ifindex = net->ifindex;
f4563a75 6978
1da177e4
LT
6979 for (;;) {
6980 if (++ifindex <= 0)
6981 ifindex = 1;
881d966b 6982 if (!__dev_get_by_index(net, ifindex))
aa79e66e 6983 return net->ifindex = ifindex;
1da177e4
LT
6984 }
6985}
6986
1da177e4 6987/* Delayed registration/unregisteration */
3b5b34fd 6988static LIST_HEAD(net_todo_list);
200b916f 6989DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
1da177e4 6990
6f05f629 6991static void net_set_todo(struct net_device *dev)
1da177e4 6992{
1da177e4 6993 list_add_tail(&dev->todo_list, &net_todo_list);
50624c93 6994 dev_net(dev)->dev_unreg_count++;
1da177e4
LT
6995}
6996
9b5e383c 6997static void rollback_registered_many(struct list_head *head)
93ee31f1 6998{
e93737b0 6999 struct net_device *dev, *tmp;
5cde2829 7000 LIST_HEAD(close_head);
9b5e383c 7001
93ee31f1
DL
7002 BUG_ON(dev_boot_phase);
7003 ASSERT_RTNL();
7004
e93737b0 7005 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
9b5e383c 7006 /* Some devices call without registering
e93737b0
KK
7007 * for initialization unwind. Remove those
7008 * devices and proceed with the remaining.
9b5e383c
ED
7009 */
7010 if (dev->reg_state == NETREG_UNINITIALIZED) {
7b6cd1ce
JP
7011 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
7012 dev->name, dev);
93ee31f1 7013
9b5e383c 7014 WARN_ON(1);
e93737b0
KK
7015 list_del(&dev->unreg_list);
7016 continue;
9b5e383c 7017 }
449f4544 7018 dev->dismantle = true;
9b5e383c 7019 BUG_ON(dev->reg_state != NETREG_REGISTERED);
44345724 7020 }
93ee31f1 7021
44345724 7022 /* If device is running, close it first. */
5cde2829
EB
7023 list_for_each_entry(dev, head, unreg_list)
7024 list_add_tail(&dev->close_list, &close_head);
99c4a26a 7025 dev_close_many(&close_head, true);
93ee31f1 7026
44345724 7027 list_for_each_entry(dev, head, unreg_list) {
9b5e383c
ED
7028 /* And unlink it from device chain. */
7029 unlist_netdevice(dev);
93ee31f1 7030
9b5e383c
ED
7031 dev->reg_state = NETREG_UNREGISTERING;
7032 }
41852497 7033 flush_all_backlogs();
93ee31f1
DL
7034
7035 synchronize_net();
7036
9b5e383c 7037 list_for_each_entry(dev, head, unreg_list) {
395eea6c
MB
7038 struct sk_buff *skb = NULL;
7039
9b5e383c
ED
7040 /* Shutdown queueing discipline. */
7041 dev_shutdown(dev);
93ee31f1
DL
7042
7043
9b5e383c 7044 /* Notify protocols, that we are about to destroy
eb13da1a 7045 * this device. They should clean all the things.
7046 */
9b5e383c 7047 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
93ee31f1 7048
395eea6c
MB
7049 if (!dev->rtnl_link_ops ||
7050 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
7051 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U,
7052 GFP_KERNEL);
7053
9b5e383c
ED
7054 /*
7055 * Flush the unicast and multicast chains
7056 */
a748ee24 7057 dev_uc_flush(dev);
22bedad3 7058 dev_mc_flush(dev);
93ee31f1 7059
9b5e383c
ED
7060 if (dev->netdev_ops->ndo_uninit)
7061 dev->netdev_ops->ndo_uninit(dev);
93ee31f1 7062
395eea6c
MB
7063 if (skb)
7064 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
56bfa7ee 7065
9ff162a8
JP
7066 /* Notifier chain MUST detach us all upper devices. */
7067 WARN_ON(netdev_has_any_upper_dev(dev));
0f524a80 7068 WARN_ON(netdev_has_any_lower_dev(dev));
93ee31f1 7069
9b5e383c
ED
7070 /* Remove entries from kobject tree */
7071 netdev_unregister_kobject(dev);
024e9679
AD
7072#ifdef CONFIG_XPS
7073 /* Remove XPS queueing entries */
7074 netif_reset_xps_queues_gt(dev, 0);
7075#endif
9b5e383c 7076 }
93ee31f1 7077
850a545b 7078 synchronize_net();
395264d5 7079
a5ee1551 7080 list_for_each_entry(dev, head, unreg_list)
9b5e383c
ED
7081 dev_put(dev);
7082}
7083
7084static void rollback_registered(struct net_device *dev)
7085{
7086 LIST_HEAD(single);
7087
7088 list_add(&dev->unreg_list, &single);
7089 rollback_registered_many(&single);
ceaaec98 7090 list_del(&single);
93ee31f1
DL
7091}
7092
fd867d51
JW
7093static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
7094 struct net_device *upper, netdev_features_t features)
7095{
7096 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
7097 netdev_features_t feature;
5ba3f7d6 7098 int feature_bit;
fd867d51 7099
5ba3f7d6
JW
7100 for_each_netdev_feature(&upper_disables, feature_bit) {
7101 feature = __NETIF_F_BIT(feature_bit);
fd867d51
JW
7102 if (!(upper->wanted_features & feature)
7103 && (features & feature)) {
7104 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
7105 &feature, upper->name);
7106 features &= ~feature;
7107 }
7108 }
7109
7110 return features;
7111}
7112
7113static void netdev_sync_lower_features(struct net_device *upper,
7114 struct net_device *lower, netdev_features_t features)
7115{
7116 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
7117 netdev_features_t feature;
5ba3f7d6 7118 int feature_bit;
fd867d51 7119
5ba3f7d6
JW
7120 for_each_netdev_feature(&upper_disables, feature_bit) {
7121 feature = __NETIF_F_BIT(feature_bit);
fd867d51
JW
7122 if (!(features & feature) && (lower->features & feature)) {
7123 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
7124 &feature, lower->name);
7125 lower->wanted_features &= ~feature;
7126 netdev_update_features(lower);
7127
7128 if (unlikely(lower->features & feature))
7129 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
7130 &feature, lower->name);
7131 }
7132 }
7133}
7134
c8f44aff
MM
7135static netdev_features_t netdev_fix_features(struct net_device *dev,
7136 netdev_features_t features)
b63365a2 7137{
57422dc5
MM
7138 /* Fix illegal checksum combinations */
7139 if ((features & NETIF_F_HW_CSUM) &&
7140 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
6f404e44 7141 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
57422dc5
MM
7142 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
7143 }
7144
b63365a2 7145 /* TSO requires that SG is present as well. */
ea2d3688 7146 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
6f404e44 7147 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
ea2d3688 7148 features &= ~NETIF_F_ALL_TSO;
b63365a2
HX
7149 }
7150
ec5f0615
PS
7151 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
7152 !(features & NETIF_F_IP_CSUM)) {
7153 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
7154 features &= ~NETIF_F_TSO;
7155 features &= ~NETIF_F_TSO_ECN;
7156 }
7157
7158 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
7159 !(features & NETIF_F_IPV6_CSUM)) {
7160 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
7161 features &= ~NETIF_F_TSO6;
7162 }
7163
b1dc497b
AD
7164 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
7165 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
7166 features &= ~NETIF_F_TSO_MANGLEID;
7167
31d8b9e0
BH
7168 /* TSO ECN requires that TSO is present as well. */
7169 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
7170 features &= ~NETIF_F_TSO_ECN;
7171
212b573f
MM
7172 /* Software GSO depends on SG. */
7173 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
6f404e44 7174 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
212b573f
MM
7175 features &= ~NETIF_F_GSO;
7176 }
7177
acd1130e 7178 /* UFO needs SG and checksumming */
b63365a2 7179 if (features & NETIF_F_UFO) {
79032644 7180 /* maybe split UFO into V4 and V6? */
c8cd0989
TH
7181 if (!(features & NETIF_F_HW_CSUM) &&
7182 ((features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) !=
7183 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))) {
6f404e44 7184 netdev_dbg(dev,
acd1130e 7185 "Dropping NETIF_F_UFO since no checksum offload features.\n");
b63365a2
HX
7186 features &= ~NETIF_F_UFO;
7187 }
7188
7189 if (!(features & NETIF_F_SG)) {
6f404e44 7190 netdev_dbg(dev,
acd1130e 7191 "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
b63365a2
HX
7192 features &= ~NETIF_F_UFO;
7193 }
7194 }
7195
802ab55a
AD
7196 /* GSO partial features require GSO partial be set */
7197 if ((features & dev->gso_partial_features) &&
7198 !(features & NETIF_F_GSO_PARTIAL)) {
7199 netdev_dbg(dev,
7200 "Dropping partially supported GSO features since no GSO partial.\n");
7201 features &= ~dev->gso_partial_features;
7202 }
7203
b63365a2
HX
7204 return features;
7205}
b63365a2 7206
6cb6a27c 7207int __netdev_update_features(struct net_device *dev)
5455c699 7208{
fd867d51 7209 struct net_device *upper, *lower;
c8f44aff 7210 netdev_features_t features;
fd867d51 7211 struct list_head *iter;
e7868a85 7212 int err = -1;
5455c699 7213
87267485
MM
7214 ASSERT_RTNL();
7215
5455c699
MM
7216 features = netdev_get_wanted_features(dev);
7217
7218 if (dev->netdev_ops->ndo_fix_features)
7219 features = dev->netdev_ops->ndo_fix_features(dev, features);
7220
7221 /* driver might be less strict about feature dependencies */
7222 features = netdev_fix_features(dev, features);
7223
fd867d51
JW
7224 /* some features can't be enabled if they're off an an upper device */
7225 netdev_for_each_upper_dev_rcu(dev, upper, iter)
7226 features = netdev_sync_upper_features(dev, upper, features);
7227
5455c699 7228 if (dev->features == features)
e7868a85 7229 goto sync_lower;
5455c699 7230
c8f44aff
MM
7231 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
7232 &dev->features, &features);
5455c699
MM
7233
7234 if (dev->netdev_ops->ndo_set_features)
7235 err = dev->netdev_ops->ndo_set_features(dev, features);
5f8dc33e
NA
7236 else
7237 err = 0;
5455c699 7238
6cb6a27c 7239 if (unlikely(err < 0)) {
5455c699 7240 netdev_err(dev,
c8f44aff
MM
7241 "set_features() failed (%d); wanted %pNF, left %pNF\n",
7242 err, &features, &dev->features);
17b85d29
NA
7243 /* return non-0 since some features might have changed and
7244 * it's better to fire a spurious notification than miss it
7245 */
7246 return -1;
6cb6a27c
MM
7247 }
7248
e7868a85 7249sync_lower:
fd867d51
JW
7250 /* some features must be disabled on lower devices when disabled
7251 * on an upper device (think: bonding master or bridge)
7252 */
7253 netdev_for_each_lower_dev(dev, lower, iter)
7254 netdev_sync_lower_features(dev, lower, features);
7255
6cb6a27c
MM
7256 if (!err)
7257 dev->features = features;
7258
e7868a85 7259 return err < 0 ? 0 : 1;
6cb6a27c
MM
7260}
7261
afe12cc8
MM
7262/**
7263 * netdev_update_features - recalculate device features
7264 * @dev: the device to check
7265 *
7266 * Recalculate dev->features set and send notifications if it
7267 * has changed. Should be called after driver or hardware dependent
7268 * conditions might have changed that influence the features.
7269 */
6cb6a27c
MM
7270void netdev_update_features(struct net_device *dev)
7271{
7272 if (__netdev_update_features(dev))
7273 netdev_features_change(dev);
5455c699
MM
7274}
7275EXPORT_SYMBOL(netdev_update_features);
7276
afe12cc8
MM
7277/**
7278 * netdev_change_features - recalculate device features
7279 * @dev: the device to check
7280 *
7281 * Recalculate dev->features set and send notifications even
7282 * if they have not changed. Should be called instead of
7283 * netdev_update_features() if also dev->vlan_features might
7284 * have changed to allow the changes to be propagated to stacked
7285 * VLAN devices.
7286 */
7287void netdev_change_features(struct net_device *dev)
7288{
7289 __netdev_update_features(dev);
7290 netdev_features_change(dev);
7291}
7292EXPORT_SYMBOL(netdev_change_features);
7293
fc4a7489
PM
7294/**
7295 * netif_stacked_transfer_operstate - transfer operstate
7296 * @rootdev: the root or lower level device to transfer state from
7297 * @dev: the device to transfer operstate to
7298 *
7299 * Transfer operational state from root to device. This is normally
7300 * called when a stacking relationship exists between the root
7301 * device and the device(a leaf device).
7302 */
7303void netif_stacked_transfer_operstate(const struct net_device *rootdev,
7304 struct net_device *dev)
7305{
7306 if (rootdev->operstate == IF_OPER_DORMANT)
7307 netif_dormant_on(dev);
7308 else
7309 netif_dormant_off(dev);
7310
0575c86b
ZS
7311 if (netif_carrier_ok(rootdev))
7312 netif_carrier_on(dev);
7313 else
7314 netif_carrier_off(dev);
fc4a7489
PM
7315}
7316EXPORT_SYMBOL(netif_stacked_transfer_operstate);
7317
a953be53 7318#ifdef CONFIG_SYSFS
1b4bf461
ED
7319static int netif_alloc_rx_queues(struct net_device *dev)
7320{
1b4bf461 7321 unsigned int i, count = dev->num_rx_queues;
bd25fa7b 7322 struct netdev_rx_queue *rx;
10595902 7323 size_t sz = count * sizeof(*rx);
1b4bf461 7324
bd25fa7b 7325 BUG_ON(count < 1);
1b4bf461 7326
da6bc57a
MH
7327 rx = kvzalloc(sz, GFP_KERNEL | __GFP_REPEAT);
7328 if (!rx)
7329 return -ENOMEM;
7330
bd25fa7b
TH
7331 dev->_rx = rx;
7332
bd25fa7b 7333 for (i = 0; i < count; i++)
fe822240 7334 rx[i].dev = dev;
1b4bf461
ED
7335 return 0;
7336}
bf264145 7337#endif
1b4bf461 7338
aa942104
CG
7339static void netdev_init_one_queue(struct net_device *dev,
7340 struct netdev_queue *queue, void *_unused)
7341{
7342 /* Initialize queue lock */
7343 spin_lock_init(&queue->_xmit_lock);
7344 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
7345 queue->xmit_lock_owner = -1;
b236da69 7346 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
aa942104 7347 queue->dev = dev;
114cf580
TH
7348#ifdef CONFIG_BQL
7349 dql_init(&queue->dql, HZ);
7350#endif
aa942104
CG
7351}
7352
60877a32
ED
7353static void netif_free_tx_queues(struct net_device *dev)
7354{
4cb28970 7355 kvfree(dev->_tx);
60877a32
ED
7356}
7357
e6484930
TH
7358static int netif_alloc_netdev_queues(struct net_device *dev)
7359{
7360 unsigned int count = dev->num_tx_queues;
7361 struct netdev_queue *tx;
60877a32 7362 size_t sz = count * sizeof(*tx);
e6484930 7363
d339727c
ED
7364 if (count < 1 || count > 0xffff)
7365 return -EINVAL;
62b5942a 7366
da6bc57a
MH
7367 tx = kvzalloc(sz, GFP_KERNEL | __GFP_REPEAT);
7368 if (!tx)
7369 return -ENOMEM;
7370
e6484930 7371 dev->_tx = tx;
1d24eb48 7372
e6484930
TH
7373 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
7374 spin_lock_init(&dev->tx_global_lock);
aa942104
CG
7375
7376 return 0;
e6484930
TH
7377}
7378
a2029240
DV
7379void netif_tx_stop_all_queues(struct net_device *dev)
7380{
7381 unsigned int i;
7382
7383 for (i = 0; i < dev->num_tx_queues; i++) {
7384 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
f4563a75 7385
a2029240
DV
7386 netif_tx_stop_queue(txq);
7387 }
7388}
7389EXPORT_SYMBOL(netif_tx_stop_all_queues);
7390
1da177e4
LT
7391/**
7392 * register_netdevice - register a network device
7393 * @dev: device to register
7394 *
7395 * Take a completed network device structure and add it to the kernel
7396 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7397 * chain. 0 is returned on success. A negative errno code is returned
7398 * on a failure to set up the device, or if the name is a duplicate.
7399 *
7400 * Callers must hold the rtnl semaphore. You may want
7401 * register_netdev() instead of this.
7402 *
7403 * BUGS:
7404 * The locking appears insufficient to guarantee two parallel registers
7405 * will not get the same name.
7406 */
7407
7408int register_netdevice(struct net_device *dev)
7409{
1da177e4 7410 int ret;
d314774c 7411 struct net *net = dev_net(dev);
1da177e4
LT
7412
7413 BUG_ON(dev_boot_phase);
7414 ASSERT_RTNL();
7415
b17a7c17
SH
7416 might_sleep();
7417
1da177e4
LT
7418 /* When net_device's are persistent, this will be fatal. */
7419 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
d314774c 7420 BUG_ON(!net);
1da177e4 7421
f1f28aa3 7422 spin_lock_init(&dev->addr_list_lock);
cf508b12 7423 netdev_set_addr_lockdep_class(dev);
1da177e4 7424
828de4f6 7425 ret = dev_get_valid_name(net, dev, dev->name);
0696c3a8
PP
7426 if (ret < 0)
7427 goto out;
7428
1da177e4 7429 /* Init, if this function is available */
d314774c
SH
7430 if (dev->netdev_ops->ndo_init) {
7431 ret = dev->netdev_ops->ndo_init(dev);
1da177e4
LT
7432 if (ret) {
7433 if (ret > 0)
7434 ret = -EIO;
90833aa4 7435 goto out;
1da177e4
LT
7436 }
7437 }
4ec93edb 7438
f646968f
PM
7439 if (((dev->hw_features | dev->features) &
7440 NETIF_F_HW_VLAN_CTAG_FILTER) &&
d2ed273d
MM
7441 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
7442 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
7443 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
7444 ret = -EINVAL;
7445 goto err_uninit;
7446 }
7447
9c7dafbf
PE
7448 ret = -EBUSY;
7449 if (!dev->ifindex)
7450 dev->ifindex = dev_new_index(net);
7451 else if (__dev_get_by_index(net, dev->ifindex))
7452 goto err_uninit;
7453
5455c699
MM
7454 /* Transfer changeable features to wanted_features and enable
7455 * software offloads (GSO and GRO).
7456 */
7457 dev->hw_features |= NETIF_F_SOFT_FEATURES;
14d1232f
MM
7458 dev->features |= NETIF_F_SOFT_FEATURES;
7459 dev->wanted_features = dev->features & dev->hw_features;
1da177e4 7460
cbc53e08 7461 if (!(dev->flags & IFF_LOOPBACK))
34324dc2 7462 dev->hw_features |= NETIF_F_NOCACHE_COPY;
cbc53e08 7463
7f348a60
AD
7464 /* If IPv4 TCP segmentation offload is supported we should also
7465 * allow the device to enable segmenting the frame with the option
7466 * of ignoring a static IP ID value. This doesn't enable the
7467 * feature itself but allows the user to enable it later.
7468 */
cbc53e08
AD
7469 if (dev->hw_features & NETIF_F_TSO)
7470 dev->hw_features |= NETIF_F_TSO_MANGLEID;
7f348a60
AD
7471 if (dev->vlan_features & NETIF_F_TSO)
7472 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
7473 if (dev->mpls_features & NETIF_F_TSO)
7474 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
7475 if (dev->hw_enc_features & NETIF_F_TSO)
7476 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
c6e1a0d1 7477
1180e7d6 7478 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
16c3ea78 7479 */
1180e7d6 7480 dev->vlan_features |= NETIF_F_HIGHDMA;
16c3ea78 7481
ee579677
PS
7482 /* Make NETIF_F_SG inheritable to tunnel devices.
7483 */
802ab55a 7484 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
ee579677 7485
0d89d203
SH
7486 /* Make NETIF_F_SG inheritable to MPLS.
7487 */
7488 dev->mpls_features |= NETIF_F_SG;
7489
7ffbe3fd
JB
7490 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
7491 ret = notifier_to_errno(ret);
7492 if (ret)
7493 goto err_uninit;
7494
8b41d188 7495 ret = netdev_register_kobject(dev);
b17a7c17 7496 if (ret)
7ce1b0ed 7497 goto err_uninit;
b17a7c17
SH
7498 dev->reg_state = NETREG_REGISTERED;
7499
6cb6a27c 7500 __netdev_update_features(dev);
8e9b59b2 7501
1da177e4
LT
7502 /*
7503 * Default initial state at registry is that the
7504 * device is present.
7505 */
7506
7507 set_bit(__LINK_STATE_PRESENT, &dev->state);
7508
8f4cccbb
BH
7509 linkwatch_init_dev(dev);
7510
1da177e4 7511 dev_init_scheduler(dev);
1da177e4 7512 dev_hold(dev);
ce286d32 7513 list_netdevice(dev);
7bf23575 7514 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 7515
948b337e
JP
7516 /* If the device has permanent device address, driver should
7517 * set dev_addr and also addr_assign_type should be set to
7518 * NET_ADDR_PERM (default value).
7519 */
7520 if (dev->addr_assign_type == NET_ADDR_PERM)
7521 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
7522
1da177e4 7523 /* Notify protocols, that a new device appeared. */
056925ab 7524 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
fcc5a03a 7525 ret = notifier_to_errno(ret);
93ee31f1
DL
7526 if (ret) {
7527 rollback_registered(dev);
7528 dev->reg_state = NETREG_UNREGISTERED;
7529 }
d90a909e
EB
7530 /*
7531 * Prevent userspace races by waiting until the network
7532 * device is fully setup before sending notifications.
7533 */
a2835763
PM
7534 if (!dev->rtnl_link_ops ||
7535 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
7f294054 7536 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
1da177e4
LT
7537
7538out:
7539 return ret;
7ce1b0ed
HX
7540
7541err_uninit:
d314774c
SH
7542 if (dev->netdev_ops->ndo_uninit)
7543 dev->netdev_ops->ndo_uninit(dev);
7ce1b0ed 7544 goto out;
1da177e4 7545}
d1b19dff 7546EXPORT_SYMBOL(register_netdevice);
1da177e4 7547
937f1ba5
BH
7548/**
7549 * init_dummy_netdev - init a dummy network device for NAPI
7550 * @dev: device to init
7551 *
7552 * This takes a network device structure and initialize the minimum
7553 * amount of fields so it can be used to schedule NAPI polls without
7554 * registering a full blown interface. This is to be used by drivers
7555 * that need to tie several hardware interfaces to a single NAPI
7556 * poll scheduler due to HW limitations.
7557 */
7558int init_dummy_netdev(struct net_device *dev)
7559{
7560 /* Clear everything. Note we don't initialize spinlocks
7561 * are they aren't supposed to be taken by any of the
7562 * NAPI code and this dummy netdev is supposed to be
7563 * only ever used for NAPI polls
7564 */
7565 memset(dev, 0, sizeof(struct net_device));
7566
7567 /* make sure we BUG if trying to hit standard
7568 * register/unregister code path
7569 */
7570 dev->reg_state = NETREG_DUMMY;
7571
937f1ba5
BH
7572 /* NAPI wants this */
7573 INIT_LIST_HEAD(&dev->napi_list);
7574
7575 /* a dummy interface is started by default */
7576 set_bit(__LINK_STATE_PRESENT, &dev->state);
7577 set_bit(__LINK_STATE_START, &dev->state);
7578
29b4433d
ED
7579 /* Note : We dont allocate pcpu_refcnt for dummy devices,
7580 * because users of this 'device' dont need to change
7581 * its refcount.
7582 */
7583
937f1ba5
BH
7584 return 0;
7585}
7586EXPORT_SYMBOL_GPL(init_dummy_netdev);
7587
7588
1da177e4
LT
7589/**
7590 * register_netdev - register a network device
7591 * @dev: device to register
7592 *
7593 * Take a completed network device structure and add it to the kernel
7594 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
7595 * chain. 0 is returned on success. A negative errno code is returned
7596 * on a failure to set up the device, or if the name is a duplicate.
7597 *
38b4da38 7598 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
7599 * and expands the device name if you passed a format string to
7600 * alloc_netdev.
7601 */
7602int register_netdev(struct net_device *dev)
7603{
7604 int err;
7605
7606 rtnl_lock();
1da177e4 7607 err = register_netdevice(dev);
1da177e4
LT
7608 rtnl_unlock();
7609 return err;
7610}
7611EXPORT_SYMBOL(register_netdev);
7612
29b4433d
ED
7613int netdev_refcnt_read(const struct net_device *dev)
7614{
7615 int i, refcnt = 0;
7616
7617 for_each_possible_cpu(i)
7618 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
7619 return refcnt;
7620}
7621EXPORT_SYMBOL(netdev_refcnt_read);
7622
2c53040f 7623/**
1da177e4 7624 * netdev_wait_allrefs - wait until all references are gone.
3de7a37b 7625 * @dev: target net_device
1da177e4
LT
7626 *
7627 * This is called when unregistering network devices.
7628 *
7629 * Any protocol or device that holds a reference should register
7630 * for netdevice notification, and cleanup and put back the
7631 * reference if they receive an UNREGISTER event.
7632 * We can get stuck here if buggy protocols don't correctly
4ec93edb 7633 * call dev_put.
1da177e4
LT
7634 */
7635static void netdev_wait_allrefs(struct net_device *dev)
7636{
7637 unsigned long rebroadcast_time, warning_time;
29b4433d 7638 int refcnt;
1da177e4 7639
e014debe
ED
7640 linkwatch_forget_dev(dev);
7641
1da177e4 7642 rebroadcast_time = warning_time = jiffies;
29b4433d
ED
7643 refcnt = netdev_refcnt_read(dev);
7644
7645 while (refcnt != 0) {
1da177e4 7646 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 7647 rtnl_lock();
1da177e4
LT
7648
7649 /* Rebroadcast unregister notification */
056925ab 7650 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
1da177e4 7651
748e2d93 7652 __rtnl_unlock();
0115e8e3 7653 rcu_barrier();
748e2d93
ED
7654 rtnl_lock();
7655
0115e8e3 7656 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
1da177e4
LT
7657 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
7658 &dev->state)) {
7659 /* We must not have linkwatch events
7660 * pending on unregister. If this
7661 * happens, we simply run the queue
7662 * unscheduled, resulting in a noop
7663 * for this device.
7664 */
7665 linkwatch_run_queue();
7666 }
7667
6756ae4b 7668 __rtnl_unlock();
1da177e4
LT
7669
7670 rebroadcast_time = jiffies;
7671 }
7672
7673 msleep(250);
7674
29b4433d
ED
7675 refcnt = netdev_refcnt_read(dev);
7676
1da177e4 7677 if (time_after(jiffies, warning_time + 10 * HZ)) {
7b6cd1ce
JP
7678 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
7679 dev->name, refcnt);
1da177e4
LT
7680 warning_time = jiffies;
7681 }
7682 }
7683}
7684
7685/* The sequence is:
7686 *
7687 * rtnl_lock();
7688 * ...
7689 * register_netdevice(x1);
7690 * register_netdevice(x2);
7691 * ...
7692 * unregister_netdevice(y1);
7693 * unregister_netdevice(y2);
7694 * ...
7695 * rtnl_unlock();
7696 * free_netdev(y1);
7697 * free_netdev(y2);
7698 *
58ec3b4d 7699 * We are invoked by rtnl_unlock().
1da177e4 7700 * This allows us to deal with problems:
b17a7c17 7701 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
7702 * without deadlocking with linkwatch via keventd.
7703 * 2) Since we run with the RTNL semaphore not held, we can sleep
7704 * safely in order to wait for the netdev refcnt to drop to zero.
58ec3b4d
HX
7705 *
7706 * We must not return until all unregister events added during
7707 * the interval the lock was held have been completed.
1da177e4 7708 */
1da177e4
LT
7709void netdev_run_todo(void)
7710{
626ab0e6 7711 struct list_head list;
1da177e4 7712
1da177e4 7713 /* Snapshot list, allow later requests */
626ab0e6 7714 list_replace_init(&net_todo_list, &list);
58ec3b4d
HX
7715
7716 __rtnl_unlock();
626ab0e6 7717
0115e8e3
ED
7718
7719 /* Wait for rcu callbacks to finish before next phase */
850a545b
EB
7720 if (!list_empty(&list))
7721 rcu_barrier();
7722
1da177e4
LT
7723 while (!list_empty(&list)) {
7724 struct net_device *dev
e5e26d75 7725 = list_first_entry(&list, struct net_device, todo_list);
1da177e4
LT
7726 list_del(&dev->todo_list);
7727
748e2d93 7728 rtnl_lock();
0115e8e3 7729 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
748e2d93 7730 __rtnl_unlock();
0115e8e3 7731
b17a7c17 7732 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
7b6cd1ce 7733 pr_err("network todo '%s' but state %d\n",
b17a7c17
SH
7734 dev->name, dev->reg_state);
7735 dump_stack();
7736 continue;
7737 }
1da177e4 7738
b17a7c17 7739 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 7740
b17a7c17 7741 netdev_wait_allrefs(dev);
1da177e4 7742
b17a7c17 7743 /* paranoia */
29b4433d 7744 BUG_ON(netdev_refcnt_read(dev));
7866a621
SN
7745 BUG_ON(!list_empty(&dev->ptype_all));
7746 BUG_ON(!list_empty(&dev->ptype_specific));
33d480ce
ED
7747 WARN_ON(rcu_access_pointer(dev->ip_ptr));
7748 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
547b792c 7749 WARN_ON(dev->dn_ptr);
1da177e4 7750
b17a7c17
SH
7751 if (dev->destructor)
7752 dev->destructor(dev);
9093bbb2 7753
50624c93
EB
7754 /* Report a network device has been unregistered */
7755 rtnl_lock();
7756 dev_net(dev)->dev_unreg_count--;
7757 __rtnl_unlock();
7758 wake_up(&netdev_unregistering_wq);
7759
9093bbb2
SH
7760 /* Free network device */
7761 kobject_put(&dev->dev.kobj);
1da177e4 7762 }
1da177e4
LT
7763}
7764
9256645a
JW
7765/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
7766 * all the same fields in the same order as net_device_stats, with only
7767 * the type differing, but rtnl_link_stats64 may have additional fields
7768 * at the end for newer counters.
3cfde79c 7769 */
77a1abf5
ED
7770void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
7771 const struct net_device_stats *netdev_stats)
3cfde79c
BH
7772{
7773#if BITS_PER_LONG == 64
9256645a 7774 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
77a1abf5 7775 memcpy(stats64, netdev_stats, sizeof(*stats64));
9256645a
JW
7776 /* zero out counters that only exist in rtnl_link_stats64 */
7777 memset((char *)stats64 + sizeof(*netdev_stats), 0,
7778 sizeof(*stats64) - sizeof(*netdev_stats));
3cfde79c 7779#else
9256645a 7780 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
3cfde79c
BH
7781 const unsigned long *src = (const unsigned long *)netdev_stats;
7782 u64 *dst = (u64 *)stats64;
7783
9256645a 7784 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
3cfde79c
BH
7785 for (i = 0; i < n; i++)
7786 dst[i] = src[i];
9256645a
JW
7787 /* zero out counters that only exist in rtnl_link_stats64 */
7788 memset((char *)stats64 + n * sizeof(u64), 0,
7789 sizeof(*stats64) - n * sizeof(u64));
3cfde79c
BH
7790#endif
7791}
77a1abf5 7792EXPORT_SYMBOL(netdev_stats_to_stats64);
3cfde79c 7793
eeda3fd6
SH
7794/**
7795 * dev_get_stats - get network device statistics
7796 * @dev: device to get statistics from
28172739 7797 * @storage: place to store stats
eeda3fd6 7798 *
d7753516
BH
7799 * Get network statistics from device. Return @storage.
7800 * The device driver may provide its own method by setting
7801 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
7802 * otherwise the internal statistics structure is used.
eeda3fd6 7803 */
d7753516
BH
7804struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
7805 struct rtnl_link_stats64 *storage)
7004bf25 7806{
eeda3fd6
SH
7807 const struct net_device_ops *ops = dev->netdev_ops;
7808
28172739
ED
7809 if (ops->ndo_get_stats64) {
7810 memset(storage, 0, sizeof(*storage));
caf586e5
ED
7811 ops->ndo_get_stats64(dev, storage);
7812 } else if (ops->ndo_get_stats) {
3cfde79c 7813 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
caf586e5
ED
7814 } else {
7815 netdev_stats_to_stats64(storage, &dev->stats);
28172739 7816 }
caf586e5 7817 storage->rx_dropped += atomic_long_read(&dev->rx_dropped);
015f0688 7818 storage->tx_dropped += atomic_long_read(&dev->tx_dropped);
6e7333d3 7819 storage->rx_nohandler += atomic_long_read(&dev->rx_nohandler);
28172739 7820 return storage;
c45d286e 7821}
eeda3fd6 7822EXPORT_SYMBOL(dev_get_stats);
c45d286e 7823
24824a09 7824struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
dc2b4847 7825{
24824a09 7826 struct netdev_queue *queue = dev_ingress_queue(dev);
dc2b4847 7827
24824a09
ED
7828#ifdef CONFIG_NET_CLS_ACT
7829 if (queue)
7830 return queue;
7831 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
7832 if (!queue)
7833 return NULL;
7834 netdev_init_one_queue(dev, queue, NULL);
2ce1ee17 7835 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
24824a09
ED
7836 queue->qdisc_sleeping = &noop_qdisc;
7837 rcu_assign_pointer(dev->ingress_queue, queue);
7838#endif
7839 return queue;
bb949fbd
DM
7840}
7841
2c60db03
ED
7842static const struct ethtool_ops default_ethtool_ops;
7843
d07d7507
SG
7844void netdev_set_default_ethtool_ops(struct net_device *dev,
7845 const struct ethtool_ops *ops)
7846{
7847 if (dev->ethtool_ops == &default_ethtool_ops)
7848 dev->ethtool_ops = ops;
7849}
7850EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
7851
74d332c1
ED
7852void netdev_freemem(struct net_device *dev)
7853{
7854 char *addr = (char *)dev - dev->padded;
7855
4cb28970 7856 kvfree(addr);
74d332c1
ED
7857}
7858
1da177e4 7859/**
722c9a0c 7860 * alloc_netdev_mqs - allocate network device
7861 * @sizeof_priv: size of private data to allocate space for
7862 * @name: device name format string
7863 * @name_assign_type: origin of device name
7864 * @setup: callback to initialize device
7865 * @txqs: the number of TX subqueues to allocate
7866 * @rxqs: the number of RX subqueues to allocate
7867 *
7868 * Allocates a struct net_device with private data area for driver use
7869 * and performs basic initialization. Also allocates subqueue structs
7870 * for each queue on the device.
1da177e4 7871 */
36909ea4 7872struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
c835a677 7873 unsigned char name_assign_type,
36909ea4
TH
7874 void (*setup)(struct net_device *),
7875 unsigned int txqs, unsigned int rxqs)
1da177e4 7876{
1da177e4 7877 struct net_device *dev;
7943986c 7878 size_t alloc_size;
1ce8e7b5 7879 struct net_device *p;
1da177e4 7880
b6fe17d6
SH
7881 BUG_ON(strlen(name) >= sizeof(dev->name));
7882
36909ea4 7883 if (txqs < 1) {
7b6cd1ce 7884 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
55513fb4
TH
7885 return NULL;
7886 }
7887
a953be53 7888#ifdef CONFIG_SYSFS
36909ea4 7889 if (rxqs < 1) {
7b6cd1ce 7890 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
36909ea4
TH
7891 return NULL;
7892 }
7893#endif
7894
fd2ea0a7 7895 alloc_size = sizeof(struct net_device);
d1643d24
AD
7896 if (sizeof_priv) {
7897 /* ensure 32-byte alignment of private area */
1ce8e7b5 7898 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
d1643d24
AD
7899 alloc_size += sizeof_priv;
7900 }
7901 /* ensure 32-byte alignment of whole construct */
1ce8e7b5 7902 alloc_size += NETDEV_ALIGN - 1;
1da177e4 7903
da6bc57a 7904 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_REPEAT);
62b5942a 7905 if (!p)
1da177e4 7906 return NULL;
1da177e4 7907
1ce8e7b5 7908 dev = PTR_ALIGN(p, NETDEV_ALIGN);
1da177e4 7909 dev->padded = (char *)dev - (char *)p;
ab9c73cc 7910
29b4433d
ED
7911 dev->pcpu_refcnt = alloc_percpu(int);
7912 if (!dev->pcpu_refcnt)
74d332c1 7913 goto free_dev;
ab9c73cc 7914
ab9c73cc 7915 if (dev_addr_init(dev))
29b4433d 7916 goto free_pcpu;
ab9c73cc 7917
22bedad3 7918 dev_mc_init(dev);
a748ee24 7919 dev_uc_init(dev);
ccffad25 7920
c346dca1 7921 dev_net_set(dev, &init_net);
1da177e4 7922
8d3bdbd5 7923 dev->gso_max_size = GSO_MAX_SIZE;
30b678d8 7924 dev->gso_max_segs = GSO_MAX_SEGS;
8d3bdbd5 7925
8d3bdbd5
DM
7926 INIT_LIST_HEAD(&dev->napi_list);
7927 INIT_LIST_HEAD(&dev->unreg_list);
5cde2829 7928 INIT_LIST_HEAD(&dev->close_list);
8d3bdbd5 7929 INIT_LIST_HEAD(&dev->link_watch_list);
2f268f12
VF
7930 INIT_LIST_HEAD(&dev->adj_list.upper);
7931 INIT_LIST_HEAD(&dev->adj_list.lower);
7866a621
SN
7932 INIT_LIST_HEAD(&dev->ptype_all);
7933 INIT_LIST_HEAD(&dev->ptype_specific);
59cc1f61
JK
7934#ifdef CONFIG_NET_SCHED
7935 hash_init(dev->qdisc_hash);
7936#endif
02875878 7937 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
8d3bdbd5
DM
7938 setup(dev);
7939
a813104d 7940 if (!dev->tx_queue_len) {
f84bb1ea 7941 dev->priv_flags |= IFF_NO_QUEUE;
11597084 7942 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
a813104d 7943 }
906470c1 7944
36909ea4
TH
7945 dev->num_tx_queues = txqs;
7946 dev->real_num_tx_queues = txqs;
ed9af2e8 7947 if (netif_alloc_netdev_queues(dev))
8d3bdbd5 7948 goto free_all;
e8a0464c 7949
a953be53 7950#ifdef CONFIG_SYSFS
36909ea4
TH
7951 dev->num_rx_queues = rxqs;
7952 dev->real_num_rx_queues = rxqs;
fe822240 7953 if (netif_alloc_rx_queues(dev))
8d3bdbd5 7954 goto free_all;
df334545 7955#endif
0a9627f2 7956
1da177e4 7957 strcpy(dev->name, name);
c835a677 7958 dev->name_assign_type = name_assign_type;
cbda10fa 7959 dev->group = INIT_NETDEV_GROUP;
2c60db03
ED
7960 if (!dev->ethtool_ops)
7961 dev->ethtool_ops = &default_ethtool_ops;
e687ad60
PN
7962
7963 nf_hook_ingress_init(dev);
7964
1da177e4 7965 return dev;
ab9c73cc 7966
8d3bdbd5
DM
7967free_all:
7968 free_netdev(dev);
7969 return NULL;
7970
29b4433d
ED
7971free_pcpu:
7972 free_percpu(dev->pcpu_refcnt);
74d332c1
ED
7973free_dev:
7974 netdev_freemem(dev);
ab9c73cc 7975 return NULL;
1da177e4 7976}
36909ea4 7977EXPORT_SYMBOL(alloc_netdev_mqs);
1da177e4
LT
7978
7979/**
722c9a0c 7980 * free_netdev - free network device
7981 * @dev: device
1da177e4 7982 *
722c9a0c 7983 * This function does the last stage of destroying an allocated device
7984 * interface. The reference to the device object is released. If this
7985 * is the last reference then it will be freed.Must be called in process
7986 * context.
1da177e4
LT
7987 */
7988void free_netdev(struct net_device *dev)
7989{
d565b0a1 7990 struct napi_struct *p, *n;
b5cdae32 7991 struct bpf_prog *prog;
d565b0a1 7992
93d05d4a 7993 might_sleep();
60877a32 7994 netif_free_tx_queues(dev);
a953be53 7995#ifdef CONFIG_SYSFS
10595902 7996 kvfree(dev->_rx);
fe822240 7997#endif
e8a0464c 7998
33d480ce 7999 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
24824a09 8000
f001fde5
JP
8001 /* Flush device addresses */
8002 dev_addr_flush(dev);
8003
d565b0a1
HX
8004 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
8005 netif_napi_del(p);
8006
29b4433d
ED
8007 free_percpu(dev->pcpu_refcnt);
8008 dev->pcpu_refcnt = NULL;
8009
b5cdae32
DM
8010 prog = rcu_dereference_protected(dev->xdp_prog, 1);
8011 if (prog) {
8012 bpf_prog_put(prog);
8013 static_key_slow_dec(&generic_xdp_needed);
8014 }
8015
3041a069 8016 /* Compatibility with error handling in drivers */
1da177e4 8017 if (dev->reg_state == NETREG_UNINITIALIZED) {
74d332c1 8018 netdev_freemem(dev);
1da177e4
LT
8019 return;
8020 }
8021
8022 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
8023 dev->reg_state = NETREG_RELEASED;
8024
43cb76d9
GKH
8025 /* will free via device release */
8026 put_device(&dev->dev);
1da177e4 8027}
d1b19dff 8028EXPORT_SYMBOL(free_netdev);
4ec93edb 8029
f0db275a
SH
8030/**
8031 * synchronize_net - Synchronize with packet receive processing
8032 *
8033 * Wait for packets currently being received to be done.
8034 * Does not block later packets from starting.
8035 */
4ec93edb 8036void synchronize_net(void)
1da177e4
LT
8037{
8038 might_sleep();
be3fc413
ED
8039 if (rtnl_is_locked())
8040 synchronize_rcu_expedited();
8041 else
8042 synchronize_rcu();
1da177e4 8043}
d1b19dff 8044EXPORT_SYMBOL(synchronize_net);
1da177e4
LT
8045
8046/**
44a0873d 8047 * unregister_netdevice_queue - remove device from the kernel
1da177e4 8048 * @dev: device
44a0873d 8049 * @head: list
6ebfbc06 8050 *
1da177e4 8051 * This function shuts down a device interface and removes it
d59b54b1 8052 * from the kernel tables.
44a0873d 8053 * If head not NULL, device is queued to be unregistered later.
1da177e4
LT
8054 *
8055 * Callers must hold the rtnl semaphore. You may want
8056 * unregister_netdev() instead of this.
8057 */
8058
44a0873d 8059void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
1da177e4 8060{
a6620712
HX
8061 ASSERT_RTNL();
8062
44a0873d 8063 if (head) {
9fdce099 8064 list_move_tail(&dev->unreg_list, head);
44a0873d
ED
8065 } else {
8066 rollback_registered(dev);
8067 /* Finish processing unregister after unlock */
8068 net_set_todo(dev);
8069 }
1da177e4 8070}
44a0873d 8071EXPORT_SYMBOL(unregister_netdevice_queue);
1da177e4 8072
9b5e383c
ED
8073/**
8074 * unregister_netdevice_many - unregister many devices
8075 * @head: list of devices
87757a91
ED
8076 *
8077 * Note: As most callers use a stack allocated list_head,
8078 * we force a list_del() to make sure stack wont be corrupted later.
9b5e383c
ED
8079 */
8080void unregister_netdevice_many(struct list_head *head)
8081{
8082 struct net_device *dev;
8083
8084 if (!list_empty(head)) {
8085 rollback_registered_many(head);
8086 list_for_each_entry(dev, head, unreg_list)
8087 net_set_todo(dev);
87757a91 8088 list_del(head);
9b5e383c
ED
8089 }
8090}
63c8099d 8091EXPORT_SYMBOL(unregister_netdevice_many);
9b5e383c 8092
1da177e4
LT
8093/**
8094 * unregister_netdev - remove device from the kernel
8095 * @dev: device
8096 *
8097 * This function shuts down a device interface and removes it
d59b54b1 8098 * from the kernel tables.
1da177e4
LT
8099 *
8100 * This is just a wrapper for unregister_netdevice that takes
8101 * the rtnl semaphore. In general you want to use this and not
8102 * unregister_netdevice.
8103 */
8104void unregister_netdev(struct net_device *dev)
8105{
8106 rtnl_lock();
8107 unregister_netdevice(dev);
8108 rtnl_unlock();
8109}
1da177e4
LT
8110EXPORT_SYMBOL(unregister_netdev);
8111
ce286d32
EB
8112/**
8113 * dev_change_net_namespace - move device to different nethost namespace
8114 * @dev: device
8115 * @net: network namespace
8116 * @pat: If not NULL name pattern to try if the current device name
8117 * is already taken in the destination network namespace.
8118 *
8119 * This function shuts down a device interface and moves it
8120 * to a new network namespace. On success 0 is returned, on
8121 * a failure a netagive errno code is returned.
8122 *
8123 * Callers must hold the rtnl semaphore.
8124 */
8125
8126int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
8127{
ce286d32
EB
8128 int err;
8129
8130 ASSERT_RTNL();
8131
8132 /* Don't allow namespace local devices to be moved. */
8133 err = -EINVAL;
8134 if (dev->features & NETIF_F_NETNS_LOCAL)
8135 goto out;
8136
8137 /* Ensure the device has been registrered */
ce286d32
EB
8138 if (dev->reg_state != NETREG_REGISTERED)
8139 goto out;
8140
8141 /* Get out if there is nothing todo */
8142 err = 0;
878628fb 8143 if (net_eq(dev_net(dev), net))
ce286d32
EB
8144 goto out;
8145
8146 /* Pick the destination device name, and ensure
8147 * we can use it in the destination network namespace.
8148 */
8149 err = -EEXIST;
d9031024 8150 if (__dev_get_by_name(net, dev->name)) {
ce286d32
EB
8151 /* We get here if we can't use the current device name */
8152 if (!pat)
8153 goto out;
828de4f6 8154 if (dev_get_valid_name(net, dev, pat) < 0)
ce286d32
EB
8155 goto out;
8156 }
8157
8158 /*
8159 * And now a mini version of register_netdevice unregister_netdevice.
8160 */
8161
8162 /* If device is running close it first. */
9b772652 8163 dev_close(dev);
ce286d32
EB
8164
8165 /* And unlink it from device chain */
8166 err = -ENODEV;
8167 unlist_netdevice(dev);
8168
8169 synchronize_net();
8170
8171 /* Shutdown queueing discipline. */
8172 dev_shutdown(dev);
8173
8174 /* Notify protocols, that we are about to destroy
eb13da1a 8175 * this device. They should clean all the things.
8176 *
8177 * Note that dev->reg_state stays at NETREG_REGISTERED.
8178 * This is wanted because this way 8021q and macvlan know
8179 * the device is just moving and can keep their slaves up.
8180 */
ce286d32 8181 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6549dd43
G
8182 rcu_barrier();
8183 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
7f294054 8184 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U, GFP_KERNEL);
ce286d32
EB
8185
8186 /*
8187 * Flush the unicast and multicast chains
8188 */
a748ee24 8189 dev_uc_flush(dev);
22bedad3 8190 dev_mc_flush(dev);
ce286d32 8191
4e66ae2e
SH
8192 /* Send a netdev-removed uevent to the old namespace */
8193 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
4c75431a 8194 netdev_adjacent_del_links(dev);
4e66ae2e 8195
ce286d32 8196 /* Actually switch the network namespace */
c346dca1 8197 dev_net_set(dev, net);
ce286d32 8198
ce286d32 8199 /* If there is an ifindex conflict assign a new one */
7a66bbc9 8200 if (__dev_get_by_index(net, dev->ifindex))
ce286d32 8201 dev->ifindex = dev_new_index(net);
ce286d32 8202
4e66ae2e
SH
8203 /* Send a netdev-add uevent to the new namespace */
8204 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
4c75431a 8205 netdev_adjacent_add_links(dev);
4e66ae2e 8206
8b41d188 8207 /* Fixup kobjects */
a1b3f594 8208 err = device_rename(&dev->dev, dev->name);
8b41d188 8209 WARN_ON(err);
ce286d32
EB
8210
8211 /* Add the device back in the hashes */
8212 list_netdevice(dev);
8213
8214 /* Notify protocols, that a new device appeared. */
8215 call_netdevice_notifiers(NETDEV_REGISTER, dev);
8216
d90a909e
EB
8217 /*
8218 * Prevent userspace races by waiting until the network
8219 * device is fully setup before sending notifications.
8220 */
7f294054 8221 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
d90a909e 8222
ce286d32
EB
8223 synchronize_net();
8224 err = 0;
8225out:
8226 return err;
8227}
463d0183 8228EXPORT_SYMBOL_GPL(dev_change_net_namespace);
ce286d32 8229
f0bf90de 8230static int dev_cpu_dead(unsigned int oldcpu)
1da177e4
LT
8231{
8232 struct sk_buff **list_skb;
1da177e4 8233 struct sk_buff *skb;
f0bf90de 8234 unsigned int cpu;
1da177e4
LT
8235 struct softnet_data *sd, *oldsd;
8236
1da177e4
LT
8237 local_irq_disable();
8238 cpu = smp_processor_id();
8239 sd = &per_cpu(softnet_data, cpu);
8240 oldsd = &per_cpu(softnet_data, oldcpu);
8241
8242 /* Find end of our completion_queue. */
8243 list_skb = &sd->completion_queue;
8244 while (*list_skb)
8245 list_skb = &(*list_skb)->next;
8246 /* Append completion queue from offline CPU. */
8247 *list_skb = oldsd->completion_queue;
8248 oldsd->completion_queue = NULL;
8249
1da177e4 8250 /* Append output queue from offline CPU. */
a9cbd588
CG
8251 if (oldsd->output_queue) {
8252 *sd->output_queue_tailp = oldsd->output_queue;
8253 sd->output_queue_tailp = oldsd->output_queue_tailp;
8254 oldsd->output_queue = NULL;
8255 oldsd->output_queue_tailp = &oldsd->output_queue;
8256 }
ac64da0b
ED
8257 /* Append NAPI poll list from offline CPU, with one exception :
8258 * process_backlog() must be called by cpu owning percpu backlog.
8259 * We properly handle process_queue & input_pkt_queue later.
8260 */
8261 while (!list_empty(&oldsd->poll_list)) {
8262 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
8263 struct napi_struct,
8264 poll_list);
8265
8266 list_del_init(&napi->poll_list);
8267 if (napi->poll == process_backlog)
8268 napi->state = 0;
8269 else
8270 ____napi_schedule(sd, napi);
264524d5 8271 }
1da177e4
LT
8272
8273 raise_softirq_irqoff(NET_TX_SOFTIRQ);
8274 local_irq_enable();
8275
8276 /* Process offline CPU's input_pkt_queue */
76cc8b13 8277 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
91e83133 8278 netif_rx_ni(skb);
76cc8b13 8279 input_queue_head_incr(oldsd);
fec5e652 8280 }
ac64da0b 8281 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
91e83133 8282 netif_rx_ni(skb);
76cc8b13
TH
8283 input_queue_head_incr(oldsd);
8284 }
1da177e4 8285
f0bf90de 8286 return 0;
1da177e4 8287}
1da177e4 8288
7f353bf2 8289/**
b63365a2
HX
8290 * netdev_increment_features - increment feature set by one
8291 * @all: current feature set
8292 * @one: new feature set
8293 * @mask: mask feature set
7f353bf2
HX
8294 *
8295 * Computes a new feature set after adding a device with feature set
b63365a2
HX
8296 * @one to the master device with current feature set @all. Will not
8297 * enable anything that is off in @mask. Returns the new feature set.
7f353bf2 8298 */
c8f44aff
MM
8299netdev_features_t netdev_increment_features(netdev_features_t all,
8300 netdev_features_t one, netdev_features_t mask)
b63365a2 8301{
c8cd0989 8302 if (mask & NETIF_F_HW_CSUM)
a188222b 8303 mask |= NETIF_F_CSUM_MASK;
1742f183 8304 mask |= NETIF_F_VLAN_CHALLENGED;
7f353bf2 8305
a188222b 8306 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
1742f183 8307 all &= one | ~NETIF_F_ALL_FOR_ALL;
c6e1a0d1 8308
1742f183 8309 /* If one device supports hw checksumming, set for all. */
c8cd0989
TH
8310 if (all & NETIF_F_HW_CSUM)
8311 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
7f353bf2
HX
8312
8313 return all;
8314}
b63365a2 8315EXPORT_SYMBOL(netdev_increment_features);
7f353bf2 8316
430f03cd 8317static struct hlist_head * __net_init netdev_create_hash(void)
30d97d35
PE
8318{
8319 int i;
8320 struct hlist_head *hash;
8321
8322 hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL);
8323 if (hash != NULL)
8324 for (i = 0; i < NETDEV_HASHENTRIES; i++)
8325 INIT_HLIST_HEAD(&hash[i]);
8326
8327 return hash;
8328}
8329
881d966b 8330/* Initialize per network namespace state */
4665079c 8331static int __net_init netdev_init(struct net *net)
881d966b 8332{
734b6541
RM
8333 if (net != &init_net)
8334 INIT_LIST_HEAD(&net->dev_base_head);
881d966b 8335
30d97d35
PE
8336 net->dev_name_head = netdev_create_hash();
8337 if (net->dev_name_head == NULL)
8338 goto err_name;
881d966b 8339
30d97d35
PE
8340 net->dev_index_head = netdev_create_hash();
8341 if (net->dev_index_head == NULL)
8342 goto err_idx;
881d966b
EB
8343
8344 return 0;
30d97d35
PE
8345
8346err_idx:
8347 kfree(net->dev_name_head);
8348err_name:
8349 return -ENOMEM;
881d966b
EB
8350}
8351
f0db275a
SH
8352/**
8353 * netdev_drivername - network driver for the device
8354 * @dev: network device
f0db275a
SH
8355 *
8356 * Determine network driver for device.
8357 */
3019de12 8358const char *netdev_drivername(const struct net_device *dev)
6579e57b 8359{
cf04a4c7
SH
8360 const struct device_driver *driver;
8361 const struct device *parent;
3019de12 8362 const char *empty = "";
6579e57b
AV
8363
8364 parent = dev->dev.parent;
6579e57b 8365 if (!parent)
3019de12 8366 return empty;
6579e57b
AV
8367
8368 driver = parent->driver;
8369 if (driver && driver->name)
3019de12
DM
8370 return driver->name;
8371 return empty;
6579e57b
AV
8372}
8373
6ea754eb
JP
8374static void __netdev_printk(const char *level, const struct net_device *dev,
8375 struct va_format *vaf)
256df2f3 8376{
b004ff49 8377 if (dev && dev->dev.parent) {
6ea754eb
JP
8378 dev_printk_emit(level[1] - '0',
8379 dev->dev.parent,
8380 "%s %s %s%s: %pV",
8381 dev_driver_string(dev->dev.parent),
8382 dev_name(dev->dev.parent),
8383 netdev_name(dev), netdev_reg_state(dev),
8384 vaf);
b004ff49 8385 } else if (dev) {
6ea754eb
JP
8386 printk("%s%s%s: %pV",
8387 level, netdev_name(dev), netdev_reg_state(dev), vaf);
b004ff49 8388 } else {
6ea754eb 8389 printk("%s(NULL net_device): %pV", level, vaf);
b004ff49 8390 }
256df2f3
JP
8391}
8392
6ea754eb
JP
8393void netdev_printk(const char *level, const struct net_device *dev,
8394 const char *format, ...)
256df2f3
JP
8395{
8396 struct va_format vaf;
8397 va_list args;
256df2f3
JP
8398
8399 va_start(args, format);
8400
8401 vaf.fmt = format;
8402 vaf.va = &args;
8403
6ea754eb 8404 __netdev_printk(level, dev, &vaf);
b004ff49 8405
256df2f3 8406 va_end(args);
256df2f3
JP
8407}
8408EXPORT_SYMBOL(netdev_printk);
8409
8410#define define_netdev_printk_level(func, level) \
6ea754eb 8411void func(const struct net_device *dev, const char *fmt, ...) \
256df2f3 8412{ \
256df2f3
JP
8413 struct va_format vaf; \
8414 va_list args; \
8415 \
8416 va_start(args, fmt); \
8417 \
8418 vaf.fmt = fmt; \
8419 vaf.va = &args; \
8420 \
6ea754eb 8421 __netdev_printk(level, dev, &vaf); \
b004ff49 8422 \
256df2f3 8423 va_end(args); \
256df2f3
JP
8424} \
8425EXPORT_SYMBOL(func);
8426
8427define_netdev_printk_level(netdev_emerg, KERN_EMERG);
8428define_netdev_printk_level(netdev_alert, KERN_ALERT);
8429define_netdev_printk_level(netdev_crit, KERN_CRIT);
8430define_netdev_printk_level(netdev_err, KERN_ERR);
8431define_netdev_printk_level(netdev_warn, KERN_WARNING);
8432define_netdev_printk_level(netdev_notice, KERN_NOTICE);
8433define_netdev_printk_level(netdev_info, KERN_INFO);
8434
4665079c 8435static void __net_exit netdev_exit(struct net *net)
881d966b
EB
8436{
8437 kfree(net->dev_name_head);
8438 kfree(net->dev_index_head);
8439}
8440
022cbae6 8441static struct pernet_operations __net_initdata netdev_net_ops = {
881d966b
EB
8442 .init = netdev_init,
8443 .exit = netdev_exit,
8444};
8445
4665079c 8446static void __net_exit default_device_exit(struct net *net)
ce286d32 8447{
e008b5fc 8448 struct net_device *dev, *aux;
ce286d32 8449 /*
e008b5fc 8450 * Push all migratable network devices back to the
ce286d32
EB
8451 * initial network namespace
8452 */
8453 rtnl_lock();
e008b5fc 8454 for_each_netdev_safe(net, dev, aux) {
ce286d32 8455 int err;
aca51397 8456 char fb_name[IFNAMSIZ];
ce286d32
EB
8457
8458 /* Ignore unmoveable devices (i.e. loopback) */
8459 if (dev->features & NETIF_F_NETNS_LOCAL)
8460 continue;
8461
e008b5fc
EB
8462 /* Leave virtual devices for the generic cleanup */
8463 if (dev->rtnl_link_ops)
8464 continue;
d0c082ce 8465
25985edc 8466 /* Push remaining network devices to init_net */
aca51397
PE
8467 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
8468 err = dev_change_net_namespace(dev, &init_net, fb_name);
ce286d32 8469 if (err) {
7b6cd1ce
JP
8470 pr_emerg("%s: failed to move %s to init_net: %d\n",
8471 __func__, dev->name, err);
aca51397 8472 BUG();
ce286d32
EB
8473 }
8474 }
8475 rtnl_unlock();
8476}
8477
50624c93
EB
8478static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
8479{
8480 /* Return with the rtnl_lock held when there are no network
8481 * devices unregistering in any network namespace in net_list.
8482 */
8483 struct net *net;
8484 bool unregistering;
ff960a73 8485 DEFINE_WAIT_FUNC(wait, woken_wake_function);
50624c93 8486
ff960a73 8487 add_wait_queue(&netdev_unregistering_wq, &wait);
50624c93 8488 for (;;) {
50624c93
EB
8489 unregistering = false;
8490 rtnl_lock();
8491 list_for_each_entry(net, net_list, exit_list) {
8492 if (net->dev_unreg_count > 0) {
8493 unregistering = true;
8494 break;
8495 }
8496 }
8497 if (!unregistering)
8498 break;
8499 __rtnl_unlock();
ff960a73
PZ
8500
8501 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
50624c93 8502 }
ff960a73 8503 remove_wait_queue(&netdev_unregistering_wq, &wait);
50624c93
EB
8504}
8505
04dc7f6b
EB
8506static void __net_exit default_device_exit_batch(struct list_head *net_list)
8507{
8508 /* At exit all network devices most be removed from a network
b595076a 8509 * namespace. Do this in the reverse order of registration.
04dc7f6b
EB
8510 * Do this across as many network namespaces as possible to
8511 * improve batching efficiency.
8512 */
8513 struct net_device *dev;
8514 struct net *net;
8515 LIST_HEAD(dev_kill_list);
8516
50624c93
EB
8517 /* To prevent network device cleanup code from dereferencing
8518 * loopback devices or network devices that have been freed
8519 * wait here for all pending unregistrations to complete,
8520 * before unregistring the loopback device and allowing the
8521 * network namespace be freed.
8522 *
8523 * The netdev todo list containing all network devices
8524 * unregistrations that happen in default_device_exit_batch
8525 * will run in the rtnl_unlock() at the end of
8526 * default_device_exit_batch.
8527 */
8528 rtnl_lock_unregistering(net_list);
04dc7f6b
EB
8529 list_for_each_entry(net, net_list, exit_list) {
8530 for_each_netdev_reverse(net, dev) {
b0ab2fab 8531 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
04dc7f6b
EB
8532 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
8533 else
8534 unregister_netdevice_queue(dev, &dev_kill_list);
8535 }
8536 }
8537 unregister_netdevice_many(&dev_kill_list);
8538 rtnl_unlock();
8539}
8540
022cbae6 8541static struct pernet_operations __net_initdata default_device_ops = {
ce286d32 8542 .exit = default_device_exit,
04dc7f6b 8543 .exit_batch = default_device_exit_batch,
ce286d32
EB
8544};
8545
1da177e4
LT
8546/*
8547 * Initialize the DEV module. At boot time this walks the device list and
8548 * unhooks any devices that fail to initialise (normally hardware not
8549 * present) and leaves us with a valid list of present and active devices.
8550 *
8551 */
8552
8553/*
8554 * This is called single threaded during boot, so no need
8555 * to take the rtnl semaphore.
8556 */
8557static int __init net_dev_init(void)
8558{
8559 int i, rc = -ENOMEM;
8560
8561 BUG_ON(!dev_boot_phase);
8562
1da177e4
LT
8563 if (dev_proc_init())
8564 goto out;
8565
8b41d188 8566 if (netdev_kobject_init())
1da177e4
LT
8567 goto out;
8568
8569 INIT_LIST_HEAD(&ptype_all);
82d8a867 8570 for (i = 0; i < PTYPE_HASH_SIZE; i++)
1da177e4
LT
8571 INIT_LIST_HEAD(&ptype_base[i]);
8572
62532da9
VY
8573 INIT_LIST_HEAD(&offload_base);
8574
881d966b
EB
8575 if (register_pernet_subsys(&netdev_net_ops))
8576 goto out;
1da177e4
LT
8577
8578 /*
8579 * Initialise the packet receive queues.
8580 */
8581
6f912042 8582 for_each_possible_cpu(i) {
41852497 8583 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
e36fa2f7 8584 struct softnet_data *sd = &per_cpu(softnet_data, i);
1da177e4 8585
41852497
ED
8586 INIT_WORK(flush, flush_backlog);
8587
e36fa2f7 8588 skb_queue_head_init(&sd->input_pkt_queue);
6e7676c1 8589 skb_queue_head_init(&sd->process_queue);
e36fa2f7 8590 INIT_LIST_HEAD(&sd->poll_list);
a9cbd588 8591 sd->output_queue_tailp = &sd->output_queue;
df334545 8592#ifdef CONFIG_RPS
e36fa2f7
ED
8593 sd->csd.func = rps_trigger_softirq;
8594 sd->csd.info = sd;
e36fa2f7 8595 sd->cpu = i;
1e94d72f 8596#endif
0a9627f2 8597
e36fa2f7
ED
8598 sd->backlog.poll = process_backlog;
8599 sd->backlog.weight = weight_p;
1da177e4
LT
8600 }
8601
1da177e4
LT
8602 dev_boot_phase = 0;
8603
505d4f73
EB
8604 /* The loopback device is special if any other network devices
8605 * is present in a network namespace the loopback device must
8606 * be present. Since we now dynamically allocate and free the
8607 * loopback device ensure this invariant is maintained by
8608 * keeping the loopback device as the first device on the
8609 * list of network devices. Ensuring the loopback devices
8610 * is the first device that appears and the last network device
8611 * that disappears.
8612 */
8613 if (register_pernet_device(&loopback_net_ops))
8614 goto out;
8615
8616 if (register_pernet_device(&default_device_ops))
8617 goto out;
8618
962cf36c
CM
8619 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
8620 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
1da177e4 8621
f0bf90de
SAS
8622 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
8623 NULL, dev_cpu_dead);
8624 WARN_ON(rc < 0);
f38a9eb1 8625 dst_subsys_init();
1da177e4
LT
8626 rc = 0;
8627out:
8628 return rc;
8629}
8630
8631subsys_initcall(net_dev_init);