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