Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
722c9a0c | 3 | * NET3 Protocol independent device support routines. |
1da177e4 | 4 | * |
1da177e4 | 5 | * Derived from the non IP parts of dev.c 1.0.19 |
722c9a0c | 6 | * Authors: Ross Biro |
1da177e4 LT |
7 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
8 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
9 | * | |
10 | * Additional Authors: | |
11 | * Florian la Roche <rzsfl@rz.uni-sb.de> | |
12 | * Alan Cox <gw4pts@gw4pts.ampr.org> | |
13 | * David Hinds <dahinds@users.sourceforge.net> | |
14 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
15 | * Adam Sulmicki <adam@cfar.umd.edu> | |
16 | * Pekka Riikonen <priikone@poesidon.pspt.fi> | |
17 | * | |
18 | * Changes: | |
19 | * D.J. Barrow : Fixed bug where dev->refcnt gets set | |
722c9a0c | 20 | * to 2 if register_netdev gets called |
21 | * before net_dev_init & also removed a | |
22 | * few lines of code in the process. | |
1da177e4 LT |
23 | * Alan Cox : device private ioctl copies fields back. |
24 | * Alan Cox : Transmit queue code does relevant | |
25 | * stunts to keep the queue safe. | |
26 | * Alan Cox : Fixed double lock. | |
27 | * Alan Cox : Fixed promisc NULL pointer trap | |
28 | * ???????? : Support the full private ioctl range | |
29 | * Alan Cox : Moved ioctl permission check into | |
30 | * drivers | |
31 | * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI | |
32 | * Alan Cox : 100 backlog just doesn't cut it when | |
33 | * you start doing multicast video 8) | |
34 | * Alan Cox : Rewrote net_bh and list manager. | |
722c9a0c | 35 | * Alan Cox : Fix ETH_P_ALL echoback lengths. |
1da177e4 LT |
36 | * Alan Cox : Took out transmit every packet pass |
37 | * Saved a few bytes in the ioctl handler | |
38 | * Alan Cox : Network driver sets packet type before | |
39 | * calling netif_rx. Saves a function | |
40 | * call a packet. | |
41 | * Alan Cox : Hashed net_bh() | |
42 | * Richard Kooijman: Timestamp fixes. | |
43 | * Alan Cox : Wrong field in SIOCGIFDSTADDR | |
44 | * Alan Cox : Device lock protection. | |
722c9a0c | 45 | * Alan Cox : Fixed nasty side effect of device close |
1da177e4 LT |
46 | * changes. |
47 | * Rudi Cilibrasi : Pass the right thing to | |
48 | * set_mac_address() | |
49 | * Dave Miller : 32bit quantity for the device lock to | |
50 | * make it work out on a Sparc. | |
51 | * Bjorn Ekwall : Added KERNELD hack. | |
52 | * Alan Cox : Cleaned up the backlog initialise. | |
53 | * Craig Metz : SIOCGIFCONF fix if space for under | |
54 | * 1 device. | |
55 | * Thomas Bogendoerfer : Return ENODEV for dev_open, if there | |
56 | * is no device open function. | |
57 | * Andi Kleen : Fix error reporting for SIOCGIFCONF | |
58 | * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF | |
59 | * Cyrus Durgin : Cleaned for KMOD | |
60 | * Adam Sulmicki : Bug Fix : Network Device Unload | |
61 | * A network device unload needs to purge | |
62 | * the backlog queue. | |
63 | * Paul Rusty Russell : SIOCSIFNAME | |
64 | * Pekka Riikonen : Netdev boot-time settings code | |
65 | * Andrew Morton : Make unregister_netdevice wait | |
722c9a0c | 66 | * indefinitely on dev->refcnt |
67 | * J Hadi Salim : - Backlog queue sampling | |
1da177e4 LT |
68 | * - netif_rx() feedback |
69 | */ | |
70 | ||
7c0f6ba6 | 71 | #include <linux/uaccess.h> |
1da177e4 | 72 | #include <linux/bitops.h> |
4fc268d2 | 73 | #include <linux/capability.h> |
1da177e4 LT |
74 | #include <linux/cpu.h> |
75 | #include <linux/types.h> | |
76 | #include <linux/kernel.h> | |
08e9897d | 77 | #include <linux/hash.h> |
5a0e3ad6 | 78 | #include <linux/slab.h> |
1da177e4 | 79 | #include <linux/sched.h> |
f1083048 | 80 | #include <linux/sched/mm.h> |
4a3e2f71 | 81 | #include <linux/mutex.h> |
11d6011c | 82 | #include <linux/rwsem.h> |
1da177e4 LT |
83 | #include <linux/string.h> |
84 | #include <linux/mm.h> | |
85 | #include <linux/socket.h> | |
86 | #include <linux/sockios.h> | |
87 | #include <linux/errno.h> | |
88 | #include <linux/interrupt.h> | |
89 | #include <linux/if_ether.h> | |
90 | #include <linux/netdevice.h> | |
91 | #include <linux/etherdevice.h> | |
0187bdfb | 92 | #include <linux/ethtool.h> |
1da177e4 | 93 | #include <linux/skbuff.h> |
a7862b45 | 94 | #include <linux/bpf.h> |
b5cdae32 | 95 | #include <linux/bpf_trace.h> |
457c4cbc | 96 | #include <net/net_namespace.h> |
1da177e4 | 97 | #include <net/sock.h> |
02d62e86 | 98 | #include <net/busy_poll.h> |
1da177e4 | 99 | #include <linux/rtnetlink.h> |
1da177e4 | 100 | #include <linux/stat.h> |
b14a9fc4 | 101 | #include <net/dsa.h> |
1da177e4 | 102 | #include <net/dst.h> |
fc4099f1 | 103 | #include <net/dst_metadata.h> |
1da177e4 | 104 | #include <net/pkt_sched.h> |
87d83093 | 105 | #include <net/pkt_cls.h> |
1da177e4 | 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> |
caeda9b9 | 132 | #include <linux/inetdevice.h> |
c445477d | 133 | #include <linux/cpu_rmap.h> |
c5905afb | 134 | #include <linux/static_key.h> |
af12fa6e | 135 | #include <linux/hashtable.h> |
60877a32 | 136 | #include <linux/vmalloc.h> |
529d0489 | 137 | #include <linux/if_macvlan.h> |
e7fd2885 | 138 | #include <linux/errqueue.h> |
3b47d303 | 139 | #include <linux/hrtimer.h> |
357b6cc5 | 140 | #include <linux/netfilter_ingress.h> |
40e4e713 | 141 | #include <linux/crash_dump.h> |
b72b5bf6 | 142 | #include <linux/sctp.h> |
ae847f40 | 143 | #include <net/udp_tunnel.h> |
6621dd29 | 144 | #include <linux/net_namespace.h> |
aaa5d90b | 145 | #include <linux/indirect_call_wrapper.h> |
af3836df | 146 | #include <net/devlink.h> |
bd869245 | 147 | #include <linux/pm_runtime.h> |
1da177e4 | 148 | |
342709ef PE |
149 | #include "net-sysfs.h" |
150 | ||
d565b0a1 HX |
151 | #define MAX_GRO_SKBS 8 |
152 | ||
5d38a079 HX |
153 | /* This should be increased if a protocol with a bigger head is added. */ |
154 | #define GRO_MAX_HEAD (MAX_HEADER + 128) | |
155 | ||
1da177e4 | 156 | static DEFINE_SPINLOCK(ptype_lock); |
62532da9 | 157 | static DEFINE_SPINLOCK(offload_lock); |
900ff8c6 CW |
158 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
159 | struct list_head ptype_all __read_mostly; /* Taps */ | |
62532da9 | 160 | static struct list_head offload_base __read_mostly; |
1da177e4 | 161 | |
ae78dbfa | 162 | static int netif_rx_internal(struct sk_buff *skb); |
54951194 | 163 | static int call_netdevice_notifiers_info(unsigned long val, |
54951194 | 164 | struct netdev_notifier_info *info); |
26372605 PM |
165 | static int call_netdevice_notifiers_extack(unsigned long val, |
166 | struct net_device *dev, | |
167 | struct netlink_ext_ack *extack); | |
90b602f8 | 168 | static struct napi_struct *napi_by_id(unsigned int napi_id); |
ae78dbfa | 169 | |
1da177e4 | 170 | /* |
7562f876 | 171 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
1da177e4 LT |
172 | * semaphore. |
173 | * | |
c6d14c84 | 174 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
1da177e4 LT |
175 | * |
176 | * Writers must hold the rtnl semaphore while they loop through the | |
7562f876 | 177 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
1da177e4 LT |
178 | * actual updates. This allows pure readers to access the list even |
179 | * while a writer is preparing to update it. | |
180 | * | |
181 | * To put it another way, dev_base_lock is held for writing only to | |
182 | * protect against pure readers; the rtnl semaphore provides the | |
183 | * protection against other writers. | |
184 | * | |
185 | * See, for example usages, register_netdevice() and | |
186 | * unregister_netdevice(), which must be called with the rtnl | |
187 | * semaphore held. | |
188 | */ | |
1da177e4 | 189 | DEFINE_RWLOCK(dev_base_lock); |
1da177e4 LT |
190 | EXPORT_SYMBOL(dev_base_lock); |
191 | ||
6c557001 FW |
192 | static DEFINE_MUTEX(ifalias_mutex); |
193 | ||
af12fa6e ET |
194 | /* protects napi_hash addition/deletion and napi_gen_id */ |
195 | static DEFINE_SPINLOCK(napi_hash_lock); | |
196 | ||
52bd2d62 | 197 | static unsigned int napi_gen_id = NR_CPUS; |
6180d9de | 198 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
af12fa6e | 199 | |
11d6011c | 200 | static DECLARE_RWSEM(devnet_rename_sem); |
c91f6df2 | 201 | |
4e985ada TG |
202 | static inline void dev_base_seq_inc(struct net *net) |
203 | { | |
643aa9cb | 204 | while (++net->dev_base_seq == 0) |
205 | ; | |
4e985ada TG |
206 | } |
207 | ||
881d966b | 208 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 209 | { |
8387ff25 | 210 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
95c96174 | 211 | |
08e9897d | 212 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
213 | } |
214 | ||
881d966b | 215 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 216 | { |
7c28bd0b | 217 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
218 | } |
219 | ||
e36fa2f7 | 220 | static inline void rps_lock(struct softnet_data *sd) |
152102c7 CG |
221 | { |
222 | #ifdef CONFIG_RPS | |
e36fa2f7 | 223 | spin_lock(&sd->input_pkt_queue.lock); |
152102c7 CG |
224 | #endif |
225 | } | |
226 | ||
e36fa2f7 | 227 | static inline void rps_unlock(struct softnet_data *sd) |
152102c7 CG |
228 | { |
229 | #ifdef CONFIG_RPS | |
e36fa2f7 | 230 | spin_unlock(&sd->input_pkt_queue.lock); |
152102c7 CG |
231 | #endif |
232 | } | |
233 | ||
ff927412 JP |
234 | static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, |
235 | const char *name) | |
236 | { | |
237 | struct netdev_name_node *name_node; | |
238 | ||
239 | name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); | |
240 | if (!name_node) | |
241 | return NULL; | |
242 | INIT_HLIST_NODE(&name_node->hlist); | |
243 | name_node->dev = dev; | |
244 | name_node->name = name; | |
245 | return name_node; | |
246 | } | |
247 | ||
248 | static struct netdev_name_node * | |
249 | netdev_name_node_head_alloc(struct net_device *dev) | |
250 | { | |
36fbf1e5 JP |
251 | struct netdev_name_node *name_node; |
252 | ||
253 | name_node = netdev_name_node_alloc(dev, dev->name); | |
254 | if (!name_node) | |
255 | return NULL; | |
256 | INIT_LIST_HEAD(&name_node->list); | |
257 | return name_node; | |
ff927412 JP |
258 | } |
259 | ||
260 | static void netdev_name_node_free(struct netdev_name_node *name_node) | |
261 | { | |
262 | kfree(name_node); | |
263 | } | |
264 | ||
265 | static void netdev_name_node_add(struct net *net, | |
266 | struct netdev_name_node *name_node) | |
267 | { | |
268 | hlist_add_head_rcu(&name_node->hlist, | |
269 | dev_name_hash(net, name_node->name)); | |
270 | } | |
271 | ||
272 | static void netdev_name_node_del(struct netdev_name_node *name_node) | |
273 | { | |
274 | hlist_del_rcu(&name_node->hlist); | |
275 | } | |
276 | ||
277 | static struct netdev_name_node *netdev_name_node_lookup(struct net *net, | |
278 | const char *name) | |
279 | { | |
280 | struct hlist_head *head = dev_name_hash(net, name); | |
281 | struct netdev_name_node *name_node; | |
282 | ||
283 | hlist_for_each_entry(name_node, head, hlist) | |
284 | if (!strcmp(name_node->name, name)) | |
285 | return name_node; | |
286 | return NULL; | |
287 | } | |
288 | ||
289 | static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, | |
290 | const char *name) | |
291 | { | |
292 | struct hlist_head *head = dev_name_hash(net, name); | |
293 | struct netdev_name_node *name_node; | |
294 | ||
295 | hlist_for_each_entry_rcu(name_node, head, hlist) | |
296 | if (!strcmp(name_node->name, name)) | |
297 | return name_node; | |
298 | return NULL; | |
299 | } | |
300 | ||
36fbf1e5 JP |
301 | int netdev_name_node_alt_create(struct net_device *dev, const char *name) |
302 | { | |
303 | struct netdev_name_node *name_node; | |
304 | struct net *net = dev_net(dev); | |
305 | ||
306 | name_node = netdev_name_node_lookup(net, name); | |
307 | if (name_node) | |
308 | return -EEXIST; | |
309 | name_node = netdev_name_node_alloc(dev, name); | |
310 | if (!name_node) | |
311 | return -ENOMEM; | |
312 | netdev_name_node_add(net, name_node); | |
313 | /* The node that holds dev->name acts as a head of per-device list. */ | |
314 | list_add_tail(&name_node->list, &dev->name_node->list); | |
315 | ||
316 | return 0; | |
317 | } | |
318 | EXPORT_SYMBOL(netdev_name_node_alt_create); | |
319 | ||
320 | static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node) | |
321 | { | |
322 | list_del(&name_node->list); | |
323 | netdev_name_node_del(name_node); | |
324 | kfree(name_node->name); | |
325 | netdev_name_node_free(name_node); | |
326 | } | |
327 | ||
328 | int netdev_name_node_alt_destroy(struct net_device *dev, const char *name) | |
329 | { | |
330 | struct netdev_name_node *name_node; | |
331 | struct net *net = dev_net(dev); | |
332 | ||
333 | name_node = netdev_name_node_lookup(net, name); | |
334 | if (!name_node) | |
335 | return -ENOENT; | |
e08ad805 ED |
336 | /* lookup might have found our primary name or a name belonging |
337 | * to another device. | |
338 | */ | |
339 | if (name_node == dev->name_node || name_node->dev != dev) | |
340 | return -EINVAL; | |
341 | ||
36fbf1e5 JP |
342 | __netdev_name_node_alt_destroy(name_node); |
343 | ||
344 | return 0; | |
345 | } | |
346 | EXPORT_SYMBOL(netdev_name_node_alt_destroy); | |
347 | ||
348 | static void netdev_name_node_alt_flush(struct net_device *dev) | |
349 | { | |
350 | struct netdev_name_node *name_node, *tmp; | |
351 | ||
352 | list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) | |
353 | __netdev_name_node_alt_destroy(name_node); | |
354 | } | |
355 | ||
ce286d32 | 356 | /* Device list insertion */ |
53759be9 | 357 | static void list_netdevice(struct net_device *dev) |
ce286d32 | 358 | { |
c346dca1 | 359 | struct net *net = dev_net(dev); |
ce286d32 EB |
360 | |
361 | ASSERT_RTNL(); | |
362 | ||
363 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 364 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
ff927412 | 365 | netdev_name_node_add(net, dev->name_node); |
fb699dfd ED |
366 | hlist_add_head_rcu(&dev->index_hlist, |
367 | dev_index_hash(net, dev->ifindex)); | |
ce286d32 | 368 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
369 | |
370 | dev_base_seq_inc(net); | |
ce286d32 EB |
371 | } |
372 | ||
fb699dfd ED |
373 | /* Device list removal |
374 | * caller must respect a RCU grace period before freeing/reusing dev | |
375 | */ | |
ce286d32 EB |
376 | static void unlist_netdevice(struct net_device *dev) |
377 | { | |
378 | ASSERT_RTNL(); | |
379 | ||
380 | /* Unlink dev from the device chain */ | |
381 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 382 | list_del_rcu(&dev->dev_list); |
ff927412 | 383 | netdev_name_node_del(dev->name_node); |
fb699dfd | 384 | hlist_del_rcu(&dev->index_hlist); |
ce286d32 | 385 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
386 | |
387 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
388 | } |
389 | ||
1da177e4 LT |
390 | /* |
391 | * Our notifier list | |
392 | */ | |
393 | ||
f07d5b94 | 394 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
395 | |
396 | /* | |
397 | * Device drivers call our routines to queue packets here. We empty the | |
398 | * queue in the local softnet handler. | |
399 | */ | |
bea3348e | 400 | |
9958da05 | 401 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 402 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 403 | |
1a33e10e CW |
404 | #ifdef CONFIG_LOCKDEP |
405 | /* | |
406 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class | |
407 | * according to dev->type | |
408 | */ | |
409 | static const unsigned short netdev_lock_type[] = { | |
410 | ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, | |
411 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, | |
412 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, | |
413 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, | |
414 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, | |
415 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, | |
416 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, | |
417 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, | |
418 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, | |
419 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, | |
420 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, | |
421 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, | |
422 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, | |
423 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, | |
424 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; | |
425 | ||
426 | static const char *const netdev_lock_name[] = { | |
427 | "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", | |
428 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", | |
429 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", | |
430 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", | |
431 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", | |
432 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", | |
433 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", | |
434 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", | |
435 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", | |
436 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", | |
437 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", | |
438 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", | |
439 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", | |
440 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", | |
441 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; | |
442 | ||
443 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; | |
845e0ebb | 444 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
1a33e10e CW |
445 | |
446 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) | |
447 | { | |
448 | int i; | |
449 | ||
450 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) | |
451 | if (netdev_lock_type[i] == dev_type) | |
452 | return i; | |
453 | /* the last key is used by default */ | |
454 | return ARRAY_SIZE(netdev_lock_type) - 1; | |
455 | } | |
456 | ||
457 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
458 | unsigned short dev_type) | |
459 | { | |
460 | int i; | |
461 | ||
462 | i = netdev_lock_pos(dev_type); | |
463 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], | |
464 | netdev_lock_name[i]); | |
465 | } | |
845e0ebb CW |
466 | |
467 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
468 | { | |
469 | int i; | |
470 | ||
471 | i = netdev_lock_pos(dev->type); | |
472 | lockdep_set_class_and_name(&dev->addr_list_lock, | |
473 | &netdev_addr_lock_key[i], | |
474 | netdev_lock_name[i]); | |
475 | } | |
1a33e10e CW |
476 | #else |
477 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
478 | unsigned short dev_type) | |
479 | { | |
480 | } | |
845e0ebb CW |
481 | |
482 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
483 | { | |
484 | } | |
1a33e10e CW |
485 | #endif |
486 | ||
1da177e4 | 487 | /******************************************************************************* |
eb13da1a | 488 | * |
489 | * Protocol management and registration routines | |
490 | * | |
491 | *******************************************************************************/ | |
1da177e4 | 492 | |
1da177e4 | 493 | |
1da177e4 LT |
494 | /* |
495 | * Add a protocol ID to the list. Now that the input handler is | |
496 | * smarter we can dispense with all the messy stuff that used to be | |
497 | * here. | |
498 | * | |
499 | * BEWARE!!! Protocol handlers, mangling input packets, | |
500 | * MUST BE last in hash buckets and checking protocol handlers | |
501 | * MUST start from promiscuous ptype_all chain in net_bh. | |
502 | * It is true now, do not change it. | |
503 | * Explanation follows: if protocol handler, mangling packet, will | |
504 | * be the first on list, it is not able to sense, that packet | |
505 | * is cloned and should be copied-on-write, so that it will | |
506 | * change it and subsequent readers will get broken packet. | |
507 | * --ANK (980803) | |
508 | */ | |
509 | ||
c07b68e8 ED |
510 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
511 | { | |
512 | if (pt->type == htons(ETH_P_ALL)) | |
7866a621 | 513 | return pt->dev ? &pt->dev->ptype_all : &ptype_all; |
c07b68e8 | 514 | else |
7866a621 SN |
515 | return pt->dev ? &pt->dev->ptype_specific : |
516 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
c07b68e8 ED |
517 | } |
518 | ||
1da177e4 LT |
519 | /** |
520 | * dev_add_pack - add packet handler | |
521 | * @pt: packet type declaration | |
522 | * | |
523 | * Add a protocol handler to the networking stack. The passed &packet_type | |
524 | * is linked into kernel lists and may not be freed until it has been | |
525 | * removed from the kernel lists. | |
526 | * | |
4ec93edb | 527 | * This call does not sleep therefore it can not |
1da177e4 LT |
528 | * guarantee all CPU's that are in middle of receiving packets |
529 | * will see the new packet type (until the next received packet). | |
530 | */ | |
531 | ||
532 | void dev_add_pack(struct packet_type *pt) | |
533 | { | |
c07b68e8 | 534 | struct list_head *head = ptype_head(pt); |
1da177e4 | 535 | |
c07b68e8 ED |
536 | spin_lock(&ptype_lock); |
537 | list_add_rcu(&pt->list, head); | |
538 | spin_unlock(&ptype_lock); | |
1da177e4 | 539 | } |
d1b19dff | 540 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 541 | |
1da177e4 LT |
542 | /** |
543 | * __dev_remove_pack - remove packet handler | |
544 | * @pt: packet type declaration | |
545 | * | |
546 | * Remove a protocol handler that was previously added to the kernel | |
547 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
548 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 549 | * returns. |
1da177e4 LT |
550 | * |
551 | * The packet type might still be in use by receivers | |
552 | * and must not be freed until after all the CPU's have gone | |
553 | * through a quiescent state. | |
554 | */ | |
555 | void __dev_remove_pack(struct packet_type *pt) | |
556 | { | |
c07b68e8 | 557 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
558 | struct packet_type *pt1; |
559 | ||
c07b68e8 | 560 | spin_lock(&ptype_lock); |
1da177e4 LT |
561 | |
562 | list_for_each_entry(pt1, head, list) { | |
563 | if (pt == pt1) { | |
564 | list_del_rcu(&pt->list); | |
565 | goto out; | |
566 | } | |
567 | } | |
568 | ||
7b6cd1ce | 569 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 570 | out: |
c07b68e8 | 571 | spin_unlock(&ptype_lock); |
1da177e4 | 572 | } |
d1b19dff ED |
573 | EXPORT_SYMBOL(__dev_remove_pack); |
574 | ||
1da177e4 LT |
575 | /** |
576 | * dev_remove_pack - remove packet handler | |
577 | * @pt: packet type declaration | |
578 | * | |
579 | * Remove a protocol handler that was previously added to the kernel | |
580 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
581 | * from the kernel lists and can be freed or reused once this function | |
582 | * returns. | |
583 | * | |
584 | * This call sleeps to guarantee that no CPU is looking at the packet | |
585 | * type after return. | |
586 | */ | |
587 | void dev_remove_pack(struct packet_type *pt) | |
588 | { | |
589 | __dev_remove_pack(pt); | |
4ec93edb | 590 | |
1da177e4 LT |
591 | synchronize_net(); |
592 | } | |
d1b19dff | 593 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 594 | |
62532da9 VY |
595 | |
596 | /** | |
597 | * dev_add_offload - register offload handlers | |
598 | * @po: protocol offload declaration | |
599 | * | |
600 | * Add protocol offload handlers to the networking stack. The passed | |
601 | * &proto_offload is linked into kernel lists and may not be freed until | |
602 | * it has been removed from the kernel lists. | |
603 | * | |
604 | * This call does not sleep therefore it can not | |
605 | * guarantee all CPU's that are in middle of receiving packets | |
606 | * will see the new offload handlers (until the next received packet). | |
607 | */ | |
608 | void dev_add_offload(struct packet_offload *po) | |
609 | { | |
bdef7de4 | 610 | struct packet_offload *elem; |
62532da9 VY |
611 | |
612 | spin_lock(&offload_lock); | |
bdef7de4 DM |
613 | list_for_each_entry(elem, &offload_base, list) { |
614 | if (po->priority < elem->priority) | |
615 | break; | |
616 | } | |
617 | list_add_rcu(&po->list, elem->list.prev); | |
62532da9 VY |
618 | spin_unlock(&offload_lock); |
619 | } | |
620 | EXPORT_SYMBOL(dev_add_offload); | |
621 | ||
622 | /** | |
623 | * __dev_remove_offload - remove offload handler | |
624 | * @po: packet offload declaration | |
625 | * | |
626 | * Remove a protocol offload handler that was previously added to the | |
627 | * kernel offload handlers by dev_add_offload(). The passed &offload_type | |
628 | * is removed from the kernel lists and can be freed or reused once this | |
629 | * function returns. | |
630 | * | |
631 | * The packet type might still be in use by receivers | |
632 | * and must not be freed until after all the CPU's have gone | |
633 | * through a quiescent state. | |
634 | */ | |
1d143d9f | 635 | static void __dev_remove_offload(struct packet_offload *po) |
62532da9 VY |
636 | { |
637 | struct list_head *head = &offload_base; | |
638 | struct packet_offload *po1; | |
639 | ||
c53aa505 | 640 | spin_lock(&offload_lock); |
62532da9 VY |
641 | |
642 | list_for_each_entry(po1, head, list) { | |
643 | if (po == po1) { | |
644 | list_del_rcu(&po->list); | |
645 | goto out; | |
646 | } | |
647 | } | |
648 | ||
649 | pr_warn("dev_remove_offload: %p not found\n", po); | |
650 | out: | |
c53aa505 | 651 | spin_unlock(&offload_lock); |
62532da9 | 652 | } |
62532da9 VY |
653 | |
654 | /** | |
655 | * dev_remove_offload - remove packet offload handler | |
656 | * @po: packet offload declaration | |
657 | * | |
658 | * Remove a packet offload handler that was previously added to the kernel | |
659 | * offload handlers by dev_add_offload(). The passed &offload_type is | |
660 | * removed from the kernel lists and can be freed or reused once this | |
661 | * function returns. | |
662 | * | |
663 | * This call sleeps to guarantee that no CPU is looking at the packet | |
664 | * type after return. | |
665 | */ | |
666 | void dev_remove_offload(struct packet_offload *po) | |
667 | { | |
668 | __dev_remove_offload(po); | |
669 | ||
670 | synchronize_net(); | |
671 | } | |
672 | EXPORT_SYMBOL(dev_remove_offload); | |
673 | ||
1da177e4 | 674 | /****************************************************************************** |
eb13da1a | 675 | * |
676 | * Device Boot-time Settings Routines | |
677 | * | |
678 | ******************************************************************************/ | |
1da177e4 LT |
679 | |
680 | /* Boot time configuration table */ | |
681 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; | |
682 | ||
683 | /** | |
684 | * netdev_boot_setup_add - add new setup entry | |
685 | * @name: name of the device | |
686 | * @map: configured settings for the device | |
687 | * | |
688 | * Adds new setup entry to the dev_boot_setup list. The function | |
689 | * returns 0 on error and 1 on success. This is a generic routine to | |
690 | * all netdevices. | |
691 | */ | |
692 | static int netdev_boot_setup_add(char *name, struct ifmap *map) | |
693 | { | |
694 | struct netdev_boot_setup *s; | |
695 | int i; | |
696 | ||
697 | s = dev_boot_setup; | |
698 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
699 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { | |
700 | memset(s[i].name, 0, sizeof(s[i].name)); | |
93b3cff9 | 701 | strlcpy(s[i].name, name, IFNAMSIZ); |
1da177e4 LT |
702 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
703 | break; | |
704 | } | |
705 | } | |
706 | ||
707 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; | |
708 | } | |
709 | ||
710 | /** | |
722c9a0c | 711 | * netdev_boot_setup_check - check boot time settings |
712 | * @dev: the netdevice | |
1da177e4 | 713 | * |
722c9a0c | 714 | * Check boot time settings for the device. |
715 | * The found settings are set for the device to be used | |
716 | * later in the device probing. | |
717 | * Returns 0 if no settings found, 1 if they are. | |
1da177e4 LT |
718 | */ |
719 | int netdev_boot_setup_check(struct net_device *dev) | |
720 | { | |
721 | struct netdev_boot_setup *s = dev_boot_setup; | |
722 | int i; | |
723 | ||
724 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
725 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && | |
93b3cff9 | 726 | !strcmp(dev->name, s[i].name)) { |
722c9a0c | 727 | dev->irq = s[i].map.irq; |
728 | dev->base_addr = s[i].map.base_addr; | |
729 | dev->mem_start = s[i].map.mem_start; | |
730 | dev->mem_end = s[i].map.mem_end; | |
1da177e4 LT |
731 | return 1; |
732 | } | |
733 | } | |
734 | return 0; | |
735 | } | |
d1b19dff | 736 | EXPORT_SYMBOL(netdev_boot_setup_check); |
1da177e4 LT |
737 | |
738 | ||
739 | /** | |
722c9a0c | 740 | * netdev_boot_base - get address from boot time settings |
741 | * @prefix: prefix for network device | |
742 | * @unit: id for network device | |
743 | * | |
744 | * Check boot time settings for the base address of device. | |
745 | * The found settings are set for the device to be used | |
746 | * later in the device probing. | |
747 | * Returns 0 if no settings found. | |
1da177e4 LT |
748 | */ |
749 | unsigned long netdev_boot_base(const char *prefix, int unit) | |
750 | { | |
751 | const struct netdev_boot_setup *s = dev_boot_setup; | |
752 | char name[IFNAMSIZ]; | |
753 | int i; | |
754 | ||
755 | sprintf(name, "%s%d", prefix, unit); | |
756 | ||
757 | /* | |
758 | * If device already registered then return base of 1 | |
759 | * to indicate not to probe for this interface | |
760 | */ | |
881d966b | 761 | if (__dev_get_by_name(&init_net, name)) |
1da177e4 LT |
762 | return 1; |
763 | ||
764 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) | |
765 | if (!strcmp(name, s[i].name)) | |
766 | return s[i].map.base_addr; | |
767 | return 0; | |
768 | } | |
769 | ||
770 | /* | |
771 | * Saves at boot time configured settings for any netdevice. | |
772 | */ | |
773 | int __init netdev_boot_setup(char *str) | |
774 | { | |
775 | int ints[5]; | |
776 | struct ifmap map; | |
777 | ||
778 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
779 | if (!str || !*str) | |
780 | return 0; | |
781 | ||
782 | /* Save settings */ | |
783 | memset(&map, 0, sizeof(map)); | |
784 | if (ints[0] > 0) | |
785 | map.irq = ints[1]; | |
786 | if (ints[0] > 1) | |
787 | map.base_addr = ints[2]; | |
788 | if (ints[0] > 2) | |
789 | map.mem_start = ints[3]; | |
790 | if (ints[0] > 3) | |
791 | map.mem_end = ints[4]; | |
792 | ||
793 | /* Add new entry to the list */ | |
794 | return netdev_boot_setup_add(str, &map); | |
795 | } | |
796 | ||
797 | __setup("netdev=", netdev_boot_setup); | |
798 | ||
799 | /******************************************************************************* | |
eb13da1a | 800 | * |
801 | * Device Interface Subroutines | |
802 | * | |
803 | *******************************************************************************/ | |
1da177e4 | 804 | |
a54acb3a ND |
805 | /** |
806 | * dev_get_iflink - get 'iflink' value of a interface | |
807 | * @dev: targeted interface | |
808 | * | |
809 | * Indicates the ifindex the interface is linked to. | |
810 | * Physical interfaces have the same 'ifindex' and 'iflink' values. | |
811 | */ | |
812 | ||
813 | int dev_get_iflink(const struct net_device *dev) | |
814 | { | |
815 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) | |
816 | return dev->netdev_ops->ndo_get_iflink(dev); | |
817 | ||
7a66bbc9 | 818 | return dev->ifindex; |
a54acb3a ND |
819 | } |
820 | EXPORT_SYMBOL(dev_get_iflink); | |
821 | ||
fc4099f1 PS |
822 | /** |
823 | * dev_fill_metadata_dst - Retrieve tunnel egress information. | |
824 | * @dev: targeted interface | |
825 | * @skb: The packet. | |
826 | * | |
827 | * For better visibility of tunnel traffic OVS needs to retrieve | |
828 | * egress tunnel information for a packet. Following API allows | |
829 | * user to get this info. | |
830 | */ | |
831 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) | |
832 | { | |
833 | struct ip_tunnel_info *info; | |
834 | ||
835 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) | |
836 | return -EINVAL; | |
837 | ||
838 | info = skb_tunnel_info_unclone(skb); | |
839 | if (!info) | |
840 | return -ENOMEM; | |
841 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) | |
842 | return -EINVAL; | |
843 | ||
844 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); | |
845 | } | |
846 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); | |
847 | ||
1da177e4 LT |
848 | /** |
849 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 850 | * @net: the applicable net namespace |
1da177e4 LT |
851 | * @name: name to find |
852 | * | |
853 | * Find an interface by name. Must be called under RTNL semaphore | |
854 | * or @dev_base_lock. If the name is found a pointer to the device | |
855 | * is returned. If the name is not found then %NULL is returned. The | |
856 | * reference counters are not incremented so the caller must be | |
857 | * careful with locks. | |
858 | */ | |
859 | ||
881d966b | 860 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 | 861 | { |
ff927412 | 862 | struct netdev_name_node *node_name; |
1da177e4 | 863 | |
ff927412 JP |
864 | node_name = netdev_name_node_lookup(net, name); |
865 | return node_name ? node_name->dev : NULL; | |
1da177e4 | 866 | } |
d1b19dff | 867 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 868 | |
72c9528b | 869 | /** |
722c9a0c | 870 | * dev_get_by_name_rcu - find a device by its name |
871 | * @net: the applicable net namespace | |
872 | * @name: name to find | |
873 | * | |
874 | * Find an interface by name. | |
875 | * If the name is found a pointer to the device is returned. | |
876 | * If the name is not found then %NULL is returned. | |
877 | * The reference counters are not incremented so the caller must be | |
878 | * careful with locks. The caller must hold RCU lock. | |
72c9528b ED |
879 | */ |
880 | ||
881 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
882 | { | |
ff927412 | 883 | struct netdev_name_node *node_name; |
72c9528b | 884 | |
ff927412 JP |
885 | node_name = netdev_name_node_lookup_rcu(net, name); |
886 | return node_name ? node_name->dev : NULL; | |
72c9528b ED |
887 | } |
888 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
889 | ||
1da177e4 LT |
890 | /** |
891 | * dev_get_by_name - find a device by its name | |
c4ea43c5 | 892 | * @net: the applicable net namespace |
1da177e4 LT |
893 | * @name: name to find |
894 | * | |
895 | * Find an interface by name. This can be called from any | |
896 | * context and does its own locking. The returned handle has | |
897 | * the usage count incremented and the caller must use dev_put() to | |
898 | * release it when it is no longer needed. %NULL is returned if no | |
899 | * matching device is found. | |
900 | */ | |
901 | ||
881d966b | 902 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
903 | { |
904 | struct net_device *dev; | |
905 | ||
72c9528b ED |
906 | rcu_read_lock(); |
907 | dev = dev_get_by_name_rcu(net, name); | |
1da177e4 LT |
908 | if (dev) |
909 | dev_hold(dev); | |
72c9528b | 910 | rcu_read_unlock(); |
1da177e4 LT |
911 | return dev; |
912 | } | |
d1b19dff | 913 | EXPORT_SYMBOL(dev_get_by_name); |
1da177e4 LT |
914 | |
915 | /** | |
916 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 917 | * @net: the applicable net namespace |
1da177e4 LT |
918 | * @ifindex: index of device |
919 | * | |
920 | * Search for an interface by index. Returns %NULL if the device | |
921 | * is not found or a pointer to the device. The device has not | |
922 | * had its reference counter increased so the caller must be careful | |
923 | * about locking. The caller must hold either the RTNL semaphore | |
924 | * or @dev_base_lock. | |
925 | */ | |
926 | ||
881d966b | 927 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 | 928 | { |
0bd8d536 ED |
929 | struct net_device *dev; |
930 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 931 | |
b67bfe0d | 932 | hlist_for_each_entry(dev, head, index_hlist) |
1da177e4 LT |
933 | if (dev->ifindex == ifindex) |
934 | return dev; | |
0bd8d536 | 935 | |
1da177e4 LT |
936 | return NULL; |
937 | } | |
d1b19dff | 938 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 939 | |
fb699dfd ED |
940 | /** |
941 | * dev_get_by_index_rcu - find a device by its ifindex | |
942 | * @net: the applicable net namespace | |
943 | * @ifindex: index of device | |
944 | * | |
945 | * Search for an interface by index. Returns %NULL if the device | |
946 | * is not found or a pointer to the device. The device has not | |
947 | * had its reference counter increased so the caller must be careful | |
948 | * about locking. The caller must hold RCU lock. | |
949 | */ | |
950 | ||
951 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
952 | { | |
fb699dfd ED |
953 | struct net_device *dev; |
954 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
955 | ||
b67bfe0d | 956 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
fb699dfd ED |
957 | if (dev->ifindex == ifindex) |
958 | return dev; | |
959 | ||
960 | return NULL; | |
961 | } | |
962 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
963 | ||
1da177e4 LT |
964 | |
965 | /** | |
966 | * dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 967 | * @net: the applicable net namespace |
1da177e4 LT |
968 | * @ifindex: index of device |
969 | * | |
970 | * Search for an interface by index. Returns NULL if the device | |
971 | * is not found or a pointer to the device. The device returned has | |
972 | * had a reference added and the pointer is safe until the user calls | |
973 | * dev_put to indicate they have finished with it. | |
974 | */ | |
975 | ||
881d966b | 976 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
977 | { |
978 | struct net_device *dev; | |
979 | ||
fb699dfd ED |
980 | rcu_read_lock(); |
981 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 LT |
982 | if (dev) |
983 | dev_hold(dev); | |
fb699dfd | 984 | rcu_read_unlock(); |
1da177e4 LT |
985 | return dev; |
986 | } | |
d1b19dff | 987 | EXPORT_SYMBOL(dev_get_by_index); |
1da177e4 | 988 | |
90b602f8 ML |
989 | /** |
990 | * dev_get_by_napi_id - find a device by napi_id | |
991 | * @napi_id: ID of the NAPI struct | |
992 | * | |
993 | * Search for an interface by NAPI ID. Returns %NULL if the device | |
994 | * is not found or a pointer to the device. The device has not had | |
995 | * its reference counter increased so the caller must be careful | |
996 | * about locking. The caller must hold RCU lock. | |
997 | */ | |
998 | ||
999 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) | |
1000 | { | |
1001 | struct napi_struct *napi; | |
1002 | ||
1003 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
1004 | ||
1005 | if (napi_id < MIN_NAPI_ID) | |
1006 | return NULL; | |
1007 | ||
1008 | napi = napi_by_id(napi_id); | |
1009 | ||
1010 | return napi ? napi->dev : NULL; | |
1011 | } | |
1012 | EXPORT_SYMBOL(dev_get_by_napi_id); | |
1013 | ||
5dbe7c17 NS |
1014 | /** |
1015 | * netdev_get_name - get a netdevice name, knowing its ifindex. | |
1016 | * @net: network namespace | |
1017 | * @name: a pointer to the buffer where the name will be stored. | |
1018 | * @ifindex: the ifindex of the interface to get the name from. | |
5dbe7c17 NS |
1019 | */ |
1020 | int netdev_get_name(struct net *net, char *name, int ifindex) | |
1021 | { | |
1022 | struct net_device *dev; | |
11d6011c | 1023 | int ret; |
5dbe7c17 | 1024 | |
11d6011c | 1025 | down_read(&devnet_rename_sem); |
5dbe7c17 | 1026 | rcu_read_lock(); |
11d6011c | 1027 | |
5dbe7c17 NS |
1028 | dev = dev_get_by_index_rcu(net, ifindex); |
1029 | if (!dev) { | |
11d6011c AD |
1030 | ret = -ENODEV; |
1031 | goto out; | |
5dbe7c17 NS |
1032 | } |
1033 | ||
1034 | strcpy(name, dev->name); | |
5dbe7c17 | 1035 | |
11d6011c AD |
1036 | ret = 0; |
1037 | out: | |
1038 | rcu_read_unlock(); | |
1039 | up_read(&devnet_rename_sem); | |
1040 | return ret; | |
5dbe7c17 NS |
1041 | } |
1042 | ||
1da177e4 | 1043 | /** |
941666c2 | 1044 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 1045 | * @net: the applicable net namespace |
1da177e4 LT |
1046 | * @type: media type of device |
1047 | * @ha: hardware address | |
1048 | * | |
1049 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
1050 | * is not found or a pointer to the device. |
1051 | * The caller must hold RCU or RTNL. | |
941666c2 | 1052 | * The returned device has not had its ref count increased |
1da177e4 LT |
1053 | * and the caller must therefore be careful about locking |
1054 | * | |
1da177e4 LT |
1055 | */ |
1056 | ||
941666c2 ED |
1057 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
1058 | const char *ha) | |
1da177e4 LT |
1059 | { |
1060 | struct net_device *dev; | |
1061 | ||
941666c2 | 1062 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
1063 | if (dev->type == type && |
1064 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
1065 | return dev; |
1066 | ||
1067 | return NULL; | |
1da177e4 | 1068 | } |
941666c2 | 1069 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 1070 | |
881d966b | 1071 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
1da177e4 LT |
1072 | { |
1073 | struct net_device *dev; | |
1074 | ||
4e9cac2b | 1075 | ASSERT_RTNL(); |
881d966b | 1076 | for_each_netdev(net, dev) |
4e9cac2b | 1077 | if (dev->type == type) |
7562f876 PE |
1078 | return dev; |
1079 | ||
1080 | return NULL; | |
4e9cac2b | 1081 | } |
4e9cac2b PM |
1082 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
1083 | ||
881d966b | 1084 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 1085 | { |
99fe3c39 | 1086 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 1087 | |
99fe3c39 ED |
1088 | rcu_read_lock(); |
1089 | for_each_netdev_rcu(net, dev) | |
1090 | if (dev->type == type) { | |
1091 | dev_hold(dev); | |
1092 | ret = dev; | |
1093 | break; | |
1094 | } | |
1095 | rcu_read_unlock(); | |
1096 | return ret; | |
1da177e4 | 1097 | } |
1da177e4 LT |
1098 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
1099 | ||
1100 | /** | |
6c555490 | 1101 | * __dev_get_by_flags - find any device with given flags |
c4ea43c5 | 1102 | * @net: the applicable net namespace |
1da177e4 LT |
1103 | * @if_flags: IFF_* values |
1104 | * @mask: bitmask of bits in if_flags to check | |
1105 | * | |
1106 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 | 1107 | * is not found or a pointer to the device. Must be called inside |
6c555490 | 1108 | * rtnl_lock(), and result refcount is unchanged. |
1da177e4 LT |
1109 | */ |
1110 | ||
6c555490 WC |
1111 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
1112 | unsigned short mask) | |
1da177e4 | 1113 | { |
7562f876 | 1114 | struct net_device *dev, *ret; |
1da177e4 | 1115 | |
6c555490 WC |
1116 | ASSERT_RTNL(); |
1117 | ||
7562f876 | 1118 | ret = NULL; |
6c555490 | 1119 | for_each_netdev(net, dev) { |
1da177e4 | 1120 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 1121 | ret = dev; |
1da177e4 LT |
1122 | break; |
1123 | } | |
1124 | } | |
7562f876 | 1125 | return ret; |
1da177e4 | 1126 | } |
6c555490 | 1127 | EXPORT_SYMBOL(__dev_get_by_flags); |
1da177e4 LT |
1128 | |
1129 | /** | |
1130 | * dev_valid_name - check if name is okay for network device | |
1131 | * @name: name string | |
1132 | * | |
1133 | * Network device names need to be valid file names to | |
c7fa9d18 DM |
1134 | * to allow sysfs to work. We also disallow any kind of |
1135 | * whitespace. | |
1da177e4 | 1136 | */ |
95f050bf | 1137 | bool dev_valid_name(const char *name) |
1da177e4 | 1138 | { |
c7fa9d18 | 1139 | if (*name == '\0') |
95f050bf | 1140 | return false; |
a9d48205 | 1141 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
95f050bf | 1142 | return false; |
c7fa9d18 | 1143 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 1144 | return false; |
c7fa9d18 DM |
1145 | |
1146 | while (*name) { | |
a4176a93 | 1147 | if (*name == '/' || *name == ':' || isspace(*name)) |
95f050bf | 1148 | return false; |
c7fa9d18 DM |
1149 | name++; |
1150 | } | |
95f050bf | 1151 | return true; |
1da177e4 | 1152 | } |
d1b19dff | 1153 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
1154 | |
1155 | /** | |
b267b179 EB |
1156 | * __dev_alloc_name - allocate a name for a device |
1157 | * @net: network namespace to allocate the device name in | |
1da177e4 | 1158 | * @name: name format string |
b267b179 | 1159 | * @buf: scratch buffer and result name string |
1da177e4 LT |
1160 | * |
1161 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
1162 | * id. It scans list of devices to build up a free map, then chooses |
1163 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1164 | * while allocating the name and adding the device in order to avoid | |
1165 | * duplicates. | |
1166 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1167 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
1168 | */ |
1169 | ||
b267b179 | 1170 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
1da177e4 LT |
1171 | { |
1172 | int i = 0; | |
1da177e4 LT |
1173 | const char *p; |
1174 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 1175 | unsigned long *inuse; |
1da177e4 LT |
1176 | struct net_device *d; |
1177 | ||
93809105 RV |
1178 | if (!dev_valid_name(name)) |
1179 | return -EINVAL; | |
1180 | ||
51f299dd | 1181 | p = strchr(name, '%'); |
1da177e4 LT |
1182 | if (p) { |
1183 | /* | |
1184 | * Verify the string as this thing may have come from | |
1185 | * the user. There must be either one "%d" and no other "%" | |
1186 | * characters. | |
1187 | */ | |
1188 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
1189 | return -EINVAL; | |
1190 | ||
1191 | /* Use one page as a bit array of possible slots */ | |
cfcabdcc | 1192 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
1193 | if (!inuse) |
1194 | return -ENOMEM; | |
1195 | ||
881d966b | 1196 | for_each_netdev(net, d) { |
1da177e4 LT |
1197 | if (!sscanf(d->name, name, &i)) |
1198 | continue; | |
1199 | if (i < 0 || i >= max_netdevices) | |
1200 | continue; | |
1201 | ||
1202 | /* avoid cases where sscanf is not exact inverse of printf */ | |
b267b179 | 1203 | snprintf(buf, IFNAMSIZ, name, i); |
1da177e4 LT |
1204 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
1205 | set_bit(i, inuse); | |
1206 | } | |
1207 | ||
1208 | i = find_first_zero_bit(inuse, max_netdevices); | |
1209 | free_page((unsigned long) inuse); | |
1210 | } | |
1211 | ||
6224abda | 1212 | snprintf(buf, IFNAMSIZ, name, i); |
b267b179 | 1213 | if (!__dev_get_by_name(net, buf)) |
1da177e4 | 1214 | return i; |
1da177e4 LT |
1215 | |
1216 | /* It is possible to run out of possible slots | |
1217 | * when the name is long and there isn't enough space left | |
1218 | * for the digits, or if all bits are used. | |
1219 | */ | |
029b6d14 | 1220 | return -ENFILE; |
1da177e4 LT |
1221 | } |
1222 | ||
2c88b855 RV |
1223 | static int dev_alloc_name_ns(struct net *net, |
1224 | struct net_device *dev, | |
1225 | const char *name) | |
1226 | { | |
1227 | char buf[IFNAMSIZ]; | |
1228 | int ret; | |
1229 | ||
c46d7642 | 1230 | BUG_ON(!net); |
2c88b855 RV |
1231 | ret = __dev_alloc_name(net, name, buf); |
1232 | if (ret >= 0) | |
1233 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1234 | return ret; | |
1da177e4 LT |
1235 | } |
1236 | ||
b267b179 EB |
1237 | /** |
1238 | * dev_alloc_name - allocate a name for a device | |
1239 | * @dev: device | |
1240 | * @name: name format string | |
1241 | * | |
1242 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1243 | * id. It scans list of devices to build up a free map, then chooses | |
1244 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1245 | * while allocating the name and adding the device in order to avoid | |
1246 | * duplicates. | |
1247 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1248 | * Returns the number of the unit assigned or a negative errno code. | |
1249 | */ | |
1250 | ||
1251 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1252 | { | |
c46d7642 | 1253 | return dev_alloc_name_ns(dev_net(dev), dev, name); |
b267b179 | 1254 | } |
d1b19dff | 1255 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1256 | |
bacb7e18 ED |
1257 | static int dev_get_valid_name(struct net *net, struct net_device *dev, |
1258 | const char *name) | |
828de4f6 | 1259 | { |
55a5ec9b DM |
1260 | BUG_ON(!net); |
1261 | ||
1262 | if (!dev_valid_name(name)) | |
1263 | return -EINVAL; | |
1264 | ||
1265 | if (strchr(name, '%')) | |
1266 | return dev_alloc_name_ns(net, dev, name); | |
1267 | else if (__dev_get_by_name(net, name)) | |
1268 | return -EEXIST; | |
1269 | else if (dev->name != name) | |
1270 | strlcpy(dev->name, name, IFNAMSIZ); | |
1271 | ||
1272 | return 0; | |
d9031024 | 1273 | } |
1da177e4 LT |
1274 | |
1275 | /** | |
1276 | * dev_change_name - change name of a device | |
1277 | * @dev: device | |
1278 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1279 | * | |
1280 | * Change name of a device, can pass format strings "eth%d". | |
1281 | * for wildcarding. | |
1282 | */ | |
cf04a4c7 | 1283 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1284 | { |
238fa362 | 1285 | unsigned char old_assign_type; |
fcc5a03a | 1286 | char oldname[IFNAMSIZ]; |
1da177e4 | 1287 | int err = 0; |
fcc5a03a | 1288 | int ret; |
881d966b | 1289 | struct net *net; |
1da177e4 LT |
1290 | |
1291 | ASSERT_RTNL(); | |
c346dca1 | 1292 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1293 | |
c346dca1 | 1294 | net = dev_net(dev); |
8065a779 SWL |
1295 | |
1296 | /* Some auto-enslaved devices e.g. failover slaves are | |
1297 | * special, as userspace might rename the device after | |
1298 | * the interface had been brought up and running since | |
1299 | * the point kernel initiated auto-enslavement. Allow | |
1300 | * live name change even when these slave devices are | |
1301 | * up and running. | |
1302 | * | |
1303 | * Typically, users of these auto-enslaving devices | |
1304 | * don't actually care about slave name change, as | |
1305 | * they are supposed to operate on master interface | |
1306 | * directly. | |
1307 | */ | |
1308 | if (dev->flags & IFF_UP && | |
1309 | likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK))) | |
1da177e4 LT |
1310 | return -EBUSY; |
1311 | ||
11d6011c | 1312 | down_write(&devnet_rename_sem); |
c91f6df2 BH |
1313 | |
1314 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
11d6011c | 1315 | up_write(&devnet_rename_sem); |
c8d90dca | 1316 | return 0; |
c91f6df2 | 1317 | } |
c8d90dca | 1318 | |
fcc5a03a HX |
1319 | memcpy(oldname, dev->name, IFNAMSIZ); |
1320 | ||
828de4f6 | 1321 | err = dev_get_valid_name(net, dev, newname); |
c91f6df2 | 1322 | if (err < 0) { |
11d6011c | 1323 | up_write(&devnet_rename_sem); |
d9031024 | 1324 | return err; |
c91f6df2 | 1325 | } |
1da177e4 | 1326 | |
6fe82a39 VF |
1327 | if (oldname[0] && !strchr(oldname, '%')) |
1328 | netdev_info(dev, "renamed from %s\n", oldname); | |
1329 | ||
238fa362 TG |
1330 | old_assign_type = dev->name_assign_type; |
1331 | dev->name_assign_type = NET_NAME_RENAMED; | |
1332 | ||
fcc5a03a | 1333 | rollback: |
a1b3f594 EB |
1334 | ret = device_rename(&dev->dev, dev->name); |
1335 | if (ret) { | |
1336 | memcpy(dev->name, oldname, IFNAMSIZ); | |
238fa362 | 1337 | dev->name_assign_type = old_assign_type; |
11d6011c | 1338 | up_write(&devnet_rename_sem); |
a1b3f594 | 1339 | return ret; |
dcc99773 | 1340 | } |
7f988eab | 1341 | |
11d6011c | 1342 | up_write(&devnet_rename_sem); |
c91f6df2 | 1343 | |
5bb025fa VF |
1344 | netdev_adjacent_rename_links(dev, oldname); |
1345 | ||
7f988eab | 1346 | write_lock_bh(&dev_base_lock); |
ff927412 | 1347 | netdev_name_node_del(dev->name_node); |
72c9528b ED |
1348 | write_unlock_bh(&dev_base_lock); |
1349 | ||
1350 | synchronize_rcu(); | |
1351 | ||
1352 | write_lock_bh(&dev_base_lock); | |
ff927412 | 1353 | netdev_name_node_add(net, dev->name_node); |
7f988eab HX |
1354 | write_unlock_bh(&dev_base_lock); |
1355 | ||
056925ab | 1356 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1357 | ret = notifier_to_errno(ret); |
1358 | ||
1359 | if (ret) { | |
91e9c07b ED |
1360 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1361 | if (err >= 0) { | |
fcc5a03a | 1362 | err = ret; |
11d6011c | 1363 | down_write(&devnet_rename_sem); |
fcc5a03a | 1364 | memcpy(dev->name, oldname, IFNAMSIZ); |
5bb025fa | 1365 | memcpy(oldname, newname, IFNAMSIZ); |
238fa362 TG |
1366 | dev->name_assign_type = old_assign_type; |
1367 | old_assign_type = NET_NAME_RENAMED; | |
fcc5a03a | 1368 | goto rollback; |
91e9c07b | 1369 | } else { |
7b6cd1ce | 1370 | pr_err("%s: name change rollback failed: %d\n", |
91e9c07b | 1371 | dev->name, ret); |
fcc5a03a HX |
1372 | } |
1373 | } | |
1da177e4 LT |
1374 | |
1375 | return err; | |
1376 | } | |
1377 | ||
0b815a1a SH |
1378 | /** |
1379 | * dev_set_alias - change ifalias of a device | |
1380 | * @dev: device | |
1381 | * @alias: name up to IFALIASZ | |
f0db275a | 1382 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1383 | * |
1384 | * Set ifalias for a device, | |
1385 | */ | |
1386 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1387 | { | |
6c557001 | 1388 | struct dev_ifalias *new_alias = NULL; |
0b815a1a SH |
1389 | |
1390 | if (len >= IFALIASZ) | |
1391 | return -EINVAL; | |
1392 | ||
6c557001 FW |
1393 | if (len) { |
1394 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); | |
1395 | if (!new_alias) | |
1396 | return -ENOMEM; | |
1397 | ||
1398 | memcpy(new_alias->ifalias, alias, len); | |
1399 | new_alias->ifalias[len] = 0; | |
96ca4a2c OH |
1400 | } |
1401 | ||
6c557001 | 1402 | mutex_lock(&ifalias_mutex); |
e3f0d761 PM |
1403 | new_alias = rcu_replace_pointer(dev->ifalias, new_alias, |
1404 | mutex_is_locked(&ifalias_mutex)); | |
6c557001 FW |
1405 | mutex_unlock(&ifalias_mutex); |
1406 | ||
1407 | if (new_alias) | |
1408 | kfree_rcu(new_alias, rcuhead); | |
0b815a1a | 1409 | |
0b815a1a SH |
1410 | return len; |
1411 | } | |
0fe554a4 | 1412 | EXPORT_SYMBOL(dev_set_alias); |
0b815a1a | 1413 | |
6c557001 FW |
1414 | /** |
1415 | * dev_get_alias - get ifalias of a device | |
1416 | * @dev: device | |
20e88320 | 1417 | * @name: buffer to store name of ifalias |
6c557001 FW |
1418 | * @len: size of buffer |
1419 | * | |
1420 | * get ifalias for a device. Caller must make sure dev cannot go | |
1421 | * away, e.g. rcu read lock or own a reference count to device. | |
1422 | */ | |
1423 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) | |
1424 | { | |
1425 | const struct dev_ifalias *alias; | |
1426 | int ret = 0; | |
1427 | ||
1428 | rcu_read_lock(); | |
1429 | alias = rcu_dereference(dev->ifalias); | |
1430 | if (alias) | |
1431 | ret = snprintf(name, len, "%s", alias->ifalias); | |
1432 | rcu_read_unlock(); | |
1433 | ||
1434 | return ret; | |
1435 | } | |
0b815a1a | 1436 | |
d8a33ac4 | 1437 | /** |
3041a069 | 1438 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1439 | * @dev: device to cause notification |
1440 | * | |
1441 | * Called to indicate a device has changed features. | |
1442 | */ | |
1443 | void netdev_features_change(struct net_device *dev) | |
1444 | { | |
056925ab | 1445 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1446 | } |
1447 | EXPORT_SYMBOL(netdev_features_change); | |
1448 | ||
1da177e4 LT |
1449 | /** |
1450 | * netdev_state_change - device changes state | |
1451 | * @dev: device to cause notification | |
1452 | * | |
1453 | * Called to indicate a device has changed state. This function calls | |
1454 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1455 | * to the routing socket. | |
1456 | */ | |
1457 | void netdev_state_change(struct net_device *dev) | |
1458 | { | |
1459 | if (dev->flags & IFF_UP) { | |
51d0c047 DA |
1460 | struct netdev_notifier_change_info change_info = { |
1461 | .info.dev = dev, | |
1462 | }; | |
54951194 | 1463 | |
51d0c047 | 1464 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
54951194 | 1465 | &change_info.info); |
7f294054 | 1466 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
1da177e4 LT |
1467 | } |
1468 | } | |
d1b19dff | 1469 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1470 | |
ee89bab1 | 1471 | /** |
722c9a0c | 1472 | * netdev_notify_peers - notify network peers about existence of @dev |
1473 | * @dev: network device | |
ee89bab1 AW |
1474 | * |
1475 | * Generate traffic such that interested network peers are aware of | |
1476 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1477 | * a device wants to inform the rest of the network about some sort of | |
1478 | * reconfiguration such as a failover event or virtual machine | |
1479 | * migration. | |
1480 | */ | |
1481 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1482 | { |
ee89bab1 AW |
1483 | rtnl_lock(); |
1484 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
37c343b4 | 1485 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); |
ee89bab1 | 1486 | rtnl_unlock(); |
c1da4ac7 | 1487 | } |
ee89bab1 | 1488 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1489 | |
40c900aa | 1490 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
1da177e4 | 1491 | { |
d314774c | 1492 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1493 | int ret; |
1da177e4 | 1494 | |
e46b66bc BH |
1495 | ASSERT_RTNL(); |
1496 | ||
bd869245 HK |
1497 | if (!netif_device_present(dev)) { |
1498 | /* may be detached because parent is runtime-suspended */ | |
1499 | if (dev->dev.parent) | |
1500 | pm_runtime_resume(dev->dev.parent); | |
1501 | if (!netif_device_present(dev)) | |
1502 | return -ENODEV; | |
1503 | } | |
1da177e4 | 1504 | |
ca99ca14 NH |
1505 | /* Block netpoll from trying to do any rx path servicing. |
1506 | * If we don't do this there is a chance ndo_poll_controller | |
1507 | * or ndo_poll may be running while we open the device | |
1508 | */ | |
66b5552f | 1509 | netpoll_poll_disable(dev); |
ca99ca14 | 1510 | |
40c900aa | 1511 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
3b8bcfd5 JB |
1512 | ret = notifier_to_errno(ret); |
1513 | if (ret) | |
1514 | return ret; | |
1515 | ||
1da177e4 | 1516 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1517 | |
d314774c SH |
1518 | if (ops->ndo_validate_addr) |
1519 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1520 | |
d314774c SH |
1521 | if (!ret && ops->ndo_open) |
1522 | ret = ops->ndo_open(dev); | |
1da177e4 | 1523 | |
66b5552f | 1524 | netpoll_poll_enable(dev); |
ca99ca14 | 1525 | |
bada339b JG |
1526 | if (ret) |
1527 | clear_bit(__LINK_STATE_START, &dev->state); | |
1528 | else { | |
1da177e4 | 1529 | dev->flags |= IFF_UP; |
4417da66 | 1530 | dev_set_rx_mode(dev); |
1da177e4 | 1531 | dev_activate(dev); |
7bf23575 | 1532 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1533 | } |
bada339b | 1534 | |
1da177e4 LT |
1535 | return ret; |
1536 | } | |
1537 | ||
1538 | /** | |
bd380811 | 1539 | * dev_open - prepare an interface for use. |
00f54e68 PM |
1540 | * @dev: device to open |
1541 | * @extack: netlink extended ack | |
1da177e4 | 1542 | * |
bd380811 PM |
1543 | * Takes a device from down to up state. The device's private open |
1544 | * function is invoked and then the multicast lists are loaded. Finally | |
1545 | * the device is moved into the up state and a %NETDEV_UP message is | |
1546 | * sent to the netdev notifier chain. | |
1547 | * | |
1548 | * Calling this function on an active interface is a nop. On a failure | |
1549 | * a negative errno code is returned. | |
1da177e4 | 1550 | */ |
00f54e68 | 1551 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
bd380811 PM |
1552 | { |
1553 | int ret; | |
1554 | ||
bd380811 PM |
1555 | if (dev->flags & IFF_UP) |
1556 | return 0; | |
1557 | ||
40c900aa | 1558 | ret = __dev_open(dev, extack); |
bd380811 PM |
1559 | if (ret < 0) |
1560 | return ret; | |
1561 | ||
7f294054 | 1562 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
bd380811 PM |
1563 | call_netdevice_notifiers(NETDEV_UP, dev); |
1564 | ||
1565 | return ret; | |
1566 | } | |
1567 | EXPORT_SYMBOL(dev_open); | |
1568 | ||
7051b88a | 1569 | static void __dev_close_many(struct list_head *head) |
1da177e4 | 1570 | { |
44345724 | 1571 | struct net_device *dev; |
e46b66bc | 1572 | |
bd380811 | 1573 | ASSERT_RTNL(); |
9d5010db DM |
1574 | might_sleep(); |
1575 | ||
5cde2829 | 1576 | list_for_each_entry(dev, head, close_list) { |
3f4df206 | 1577 | /* Temporarily disable netpoll until the interface is down */ |
66b5552f | 1578 | netpoll_poll_disable(dev); |
3f4df206 | 1579 | |
44345724 | 1580 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1581 | |
44345724 | 1582 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1583 | |
44345724 OP |
1584 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1585 | * can be even on different cpu. So just clear netif_running(). | |
1586 | * | |
1587 | * dev->stop() will invoke napi_disable() on all of it's | |
1588 | * napi_struct instances on this device. | |
1589 | */ | |
4e857c58 | 1590 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
44345724 | 1591 | } |
1da177e4 | 1592 | |
44345724 | 1593 | dev_deactivate_many(head); |
d8b2a4d2 | 1594 | |
5cde2829 | 1595 | list_for_each_entry(dev, head, close_list) { |
44345724 | 1596 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 1597 | |
44345724 OP |
1598 | /* |
1599 | * Call the device specific close. This cannot fail. | |
1600 | * Only if device is UP | |
1601 | * | |
1602 | * We allow it to be called even after a DETACH hot-plug | |
1603 | * event. | |
1604 | */ | |
1605 | if (ops->ndo_stop) | |
1606 | ops->ndo_stop(dev); | |
1607 | ||
44345724 | 1608 | dev->flags &= ~IFF_UP; |
66b5552f | 1609 | netpoll_poll_enable(dev); |
44345724 | 1610 | } |
44345724 OP |
1611 | } |
1612 | ||
7051b88a | 1613 | static void __dev_close(struct net_device *dev) |
44345724 OP |
1614 | { |
1615 | LIST_HEAD(single); | |
1616 | ||
5cde2829 | 1617 | list_add(&dev->close_list, &single); |
7051b88a | 1618 | __dev_close_many(&single); |
f87e6f47 | 1619 | list_del(&single); |
44345724 OP |
1620 | } |
1621 | ||
7051b88a | 1622 | void dev_close_many(struct list_head *head, bool unlink) |
44345724 OP |
1623 | { |
1624 | struct net_device *dev, *tmp; | |
1da177e4 | 1625 | |
5cde2829 EB |
1626 | /* Remove the devices that don't need to be closed */ |
1627 | list_for_each_entry_safe(dev, tmp, head, close_list) | |
44345724 | 1628 | if (!(dev->flags & IFF_UP)) |
5cde2829 | 1629 | list_del_init(&dev->close_list); |
44345724 OP |
1630 | |
1631 | __dev_close_many(head); | |
1da177e4 | 1632 | |
5cde2829 | 1633 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
7f294054 | 1634 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
44345724 | 1635 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
99c4a26a DM |
1636 | if (unlink) |
1637 | list_del_init(&dev->close_list); | |
44345724 | 1638 | } |
bd380811 | 1639 | } |
99c4a26a | 1640 | EXPORT_SYMBOL(dev_close_many); |
bd380811 PM |
1641 | |
1642 | /** | |
1643 | * dev_close - shutdown an interface. | |
1644 | * @dev: device to shutdown | |
1645 | * | |
1646 | * This function moves an active device into down state. A | |
1647 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1648 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1649 | * chain. | |
1650 | */ | |
7051b88a | 1651 | void dev_close(struct net_device *dev) |
bd380811 | 1652 | { |
e14a5993 ED |
1653 | if (dev->flags & IFF_UP) { |
1654 | LIST_HEAD(single); | |
1da177e4 | 1655 | |
5cde2829 | 1656 | list_add(&dev->close_list, &single); |
99c4a26a | 1657 | dev_close_many(&single, true); |
e14a5993 ED |
1658 | list_del(&single); |
1659 | } | |
1da177e4 | 1660 | } |
d1b19dff | 1661 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1662 | |
1663 | ||
0187bdfb BH |
1664 | /** |
1665 | * dev_disable_lro - disable Large Receive Offload on a device | |
1666 | * @dev: device | |
1667 | * | |
1668 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1669 | * called under RTNL. This is needed if received packets may be | |
1670 | * forwarded to another interface. | |
1671 | */ | |
1672 | void dev_disable_lro(struct net_device *dev) | |
1673 | { | |
fbe168ba MK |
1674 | struct net_device *lower_dev; |
1675 | struct list_head *iter; | |
529d0489 | 1676 | |
bc5787c6 MM |
1677 | dev->wanted_features &= ~NETIF_F_LRO; |
1678 | netdev_update_features(dev); | |
27660515 | 1679 | |
22d5969f MM |
1680 | if (unlikely(dev->features & NETIF_F_LRO)) |
1681 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
fbe168ba MK |
1682 | |
1683 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
1684 | dev_disable_lro(lower_dev); | |
0187bdfb BH |
1685 | } |
1686 | EXPORT_SYMBOL(dev_disable_lro); | |
1687 | ||
56f5aa77 MC |
1688 | /** |
1689 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device | |
1690 | * @dev: device | |
1691 | * | |
1692 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be | |
1693 | * called under RTNL. This is needed if Generic XDP is installed on | |
1694 | * the device. | |
1695 | */ | |
1696 | static void dev_disable_gro_hw(struct net_device *dev) | |
1697 | { | |
1698 | dev->wanted_features &= ~NETIF_F_GRO_HW; | |
1699 | netdev_update_features(dev); | |
1700 | ||
1701 | if (unlikely(dev->features & NETIF_F_GRO_HW)) | |
1702 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); | |
1703 | } | |
1704 | ||
ede2762d KT |
1705 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
1706 | { | |
1707 | #define N(val) \ | |
1708 | case NETDEV_##val: \ | |
1709 | return "NETDEV_" __stringify(val); | |
1710 | switch (cmd) { | |
1711 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) | |
1712 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) | |
1713 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) | |
1714 | N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER) | |
1715 | N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO) | |
1716 | N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO) | |
1717 | N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) | |
9daae9bd GP |
1718 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
1719 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) | |
1570415f | 1720 | N(PRE_CHANGEADDR) |
3f5ecd8a | 1721 | } |
ede2762d KT |
1722 | #undef N |
1723 | return "UNKNOWN_NETDEV_EVENT"; | |
1724 | } | |
1725 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); | |
1726 | ||
351638e7 JP |
1727 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
1728 | struct net_device *dev) | |
1729 | { | |
51d0c047 DA |
1730 | struct netdev_notifier_info info = { |
1731 | .dev = dev, | |
1732 | }; | |
351638e7 | 1733 | |
351638e7 JP |
1734 | return nb->notifier_call(nb, val, &info); |
1735 | } | |
0187bdfb | 1736 | |
afa0df59 JP |
1737 | static int call_netdevice_register_notifiers(struct notifier_block *nb, |
1738 | struct net_device *dev) | |
1739 | { | |
1740 | int err; | |
1741 | ||
1742 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); | |
1743 | err = notifier_to_errno(err); | |
1744 | if (err) | |
1745 | return err; | |
1746 | ||
1747 | if (!(dev->flags & IFF_UP)) | |
1748 | return 0; | |
1749 | ||
1750 | call_netdevice_notifier(nb, NETDEV_UP, dev); | |
1751 | return 0; | |
1752 | } | |
1753 | ||
1754 | static void call_netdevice_unregister_notifiers(struct notifier_block *nb, | |
1755 | struct net_device *dev) | |
1756 | { | |
1757 | if (dev->flags & IFF_UP) { | |
1758 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, | |
1759 | dev); | |
1760 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
1761 | } | |
1762 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); | |
1763 | } | |
1764 | ||
1765 | static int call_netdevice_register_net_notifiers(struct notifier_block *nb, | |
1766 | struct net *net) | |
1767 | { | |
1768 | struct net_device *dev; | |
1769 | int err; | |
1770 | ||
1771 | for_each_netdev(net, dev) { | |
1772 | err = call_netdevice_register_notifiers(nb, dev); | |
1773 | if (err) | |
1774 | goto rollback; | |
1775 | } | |
1776 | return 0; | |
1777 | ||
1778 | rollback: | |
1779 | for_each_netdev_continue_reverse(net, dev) | |
1780 | call_netdevice_unregister_notifiers(nb, dev); | |
1781 | return err; | |
1782 | } | |
1783 | ||
1784 | static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb, | |
1785 | struct net *net) | |
1786 | { | |
1787 | struct net_device *dev; | |
1788 | ||
1789 | for_each_netdev(net, dev) | |
1790 | call_netdevice_unregister_notifiers(nb, dev); | |
1791 | } | |
1792 | ||
881d966b EB |
1793 | static int dev_boot_phase = 1; |
1794 | ||
1da177e4 | 1795 | /** |
722c9a0c | 1796 | * register_netdevice_notifier - register a network notifier block |
1797 | * @nb: notifier | |
1da177e4 | 1798 | * |
722c9a0c | 1799 | * Register a notifier to be called when network device events occur. |
1800 | * The notifier passed is linked into the kernel structures and must | |
1801 | * not be reused until it has been unregistered. A negative errno code | |
1802 | * is returned on a failure. | |
1da177e4 | 1803 | * |
722c9a0c | 1804 | * When registered all registration and up events are replayed |
1805 | * to the new notifier to allow device to have a race free | |
1806 | * view of the network device list. | |
1da177e4 LT |
1807 | */ |
1808 | ||
1809 | int register_netdevice_notifier(struct notifier_block *nb) | |
1810 | { | |
881d966b | 1811 | struct net *net; |
1da177e4 LT |
1812 | int err; |
1813 | ||
328fbe74 KT |
1814 | /* Close race with setup_net() and cleanup_net() */ |
1815 | down_write(&pernet_ops_rwsem); | |
1da177e4 | 1816 | rtnl_lock(); |
f07d5b94 | 1817 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1818 | if (err) |
1819 | goto unlock; | |
881d966b EB |
1820 | if (dev_boot_phase) |
1821 | goto unlock; | |
1822 | for_each_net(net) { | |
afa0df59 JP |
1823 | err = call_netdevice_register_net_notifiers(nb, net); |
1824 | if (err) | |
1825 | goto rollback; | |
1da177e4 | 1826 | } |
fcc5a03a HX |
1827 | |
1828 | unlock: | |
1da177e4 | 1829 | rtnl_unlock(); |
328fbe74 | 1830 | up_write(&pernet_ops_rwsem); |
1da177e4 | 1831 | return err; |
fcc5a03a HX |
1832 | |
1833 | rollback: | |
afa0df59 JP |
1834 | for_each_net_continue_reverse(net) |
1835 | call_netdevice_unregister_net_notifiers(nb, net); | |
c67625a1 PE |
1836 | |
1837 | raw_notifier_chain_unregister(&netdev_chain, nb); | |
fcc5a03a | 1838 | goto unlock; |
1da177e4 | 1839 | } |
d1b19dff | 1840 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1841 | |
1842 | /** | |
722c9a0c | 1843 | * unregister_netdevice_notifier - unregister a network notifier block |
1844 | * @nb: notifier | |
1da177e4 | 1845 | * |
722c9a0c | 1846 | * Unregister a notifier previously registered by |
1847 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1848 | * kernel structures and may then be reused. A negative errno code | |
1849 | * is returned on a failure. | |
7d3d43da | 1850 | * |
722c9a0c | 1851 | * After unregistering unregister and down device events are synthesized |
1852 | * for all devices on the device list to the removed notifier to remove | |
1853 | * the need for special case cleanup code. | |
1da177e4 LT |
1854 | */ |
1855 | ||
1856 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1857 | { | |
7d3d43da | 1858 | struct net *net; |
9f514950 HX |
1859 | int err; |
1860 | ||
328fbe74 KT |
1861 | /* Close race with setup_net() and cleanup_net() */ |
1862 | down_write(&pernet_ops_rwsem); | |
9f514950 | 1863 | rtnl_lock(); |
f07d5b94 | 1864 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1865 | if (err) |
1866 | goto unlock; | |
1867 | ||
48b3a137 JP |
1868 | for_each_net(net) |
1869 | call_netdevice_unregister_net_notifiers(nb, net); | |
1870 | ||
7d3d43da | 1871 | unlock: |
9f514950 | 1872 | rtnl_unlock(); |
328fbe74 | 1873 | up_write(&pernet_ops_rwsem); |
9f514950 | 1874 | return err; |
1da177e4 | 1875 | } |
d1b19dff | 1876 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 | 1877 | |
1f637703 JP |
1878 | static int __register_netdevice_notifier_net(struct net *net, |
1879 | struct notifier_block *nb, | |
1880 | bool ignore_call_fail) | |
1881 | { | |
1882 | int err; | |
1883 | ||
1884 | err = raw_notifier_chain_register(&net->netdev_chain, nb); | |
1885 | if (err) | |
1886 | return err; | |
1887 | if (dev_boot_phase) | |
1888 | return 0; | |
1889 | ||
1890 | err = call_netdevice_register_net_notifiers(nb, net); | |
1891 | if (err && !ignore_call_fail) | |
1892 | goto chain_unregister; | |
1893 | ||
1894 | return 0; | |
1895 | ||
1896 | chain_unregister: | |
1897 | raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1898 | return err; | |
1899 | } | |
1900 | ||
1901 | static int __unregister_netdevice_notifier_net(struct net *net, | |
1902 | struct notifier_block *nb) | |
1903 | { | |
1904 | int err; | |
1905 | ||
1906 | err = raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1907 | if (err) | |
1908 | return err; | |
1909 | ||
1910 | call_netdevice_unregister_net_notifiers(nb, net); | |
1911 | return 0; | |
1912 | } | |
1913 | ||
a30c7b42 JP |
1914 | /** |
1915 | * register_netdevice_notifier_net - register a per-netns network notifier block | |
1916 | * @net: network namespace | |
1917 | * @nb: notifier | |
1918 | * | |
1919 | * Register a notifier to be called when network device events occur. | |
1920 | * The notifier passed is linked into the kernel structures and must | |
1921 | * not be reused until it has been unregistered. A negative errno code | |
1922 | * is returned on a failure. | |
1923 | * | |
1924 | * When registered all registration and up events are replayed | |
1925 | * to the new notifier to allow device to have a race free | |
1926 | * view of the network device list. | |
1927 | */ | |
1928 | ||
1929 | int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb) | |
1930 | { | |
1931 | int err; | |
1932 | ||
1933 | rtnl_lock(); | |
1f637703 | 1934 | err = __register_netdevice_notifier_net(net, nb, false); |
a30c7b42 JP |
1935 | rtnl_unlock(); |
1936 | return err; | |
a30c7b42 JP |
1937 | } |
1938 | EXPORT_SYMBOL(register_netdevice_notifier_net); | |
1939 | ||
1940 | /** | |
1941 | * unregister_netdevice_notifier_net - unregister a per-netns | |
1942 | * network notifier block | |
1943 | * @net: network namespace | |
1944 | * @nb: notifier | |
1945 | * | |
1946 | * Unregister a notifier previously registered by | |
1947 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1948 | * kernel structures and may then be reused. A negative errno code | |
1949 | * is returned on a failure. | |
1950 | * | |
1951 | * After unregistering unregister and down device events are synthesized | |
1952 | * for all devices on the device list to the removed notifier to remove | |
1953 | * the need for special case cleanup code. | |
1954 | */ | |
1955 | ||
1956 | int unregister_netdevice_notifier_net(struct net *net, | |
1957 | struct notifier_block *nb) | |
1958 | { | |
1959 | int err; | |
1960 | ||
1961 | rtnl_lock(); | |
1f637703 | 1962 | err = __unregister_netdevice_notifier_net(net, nb); |
a30c7b42 JP |
1963 | rtnl_unlock(); |
1964 | return err; | |
1965 | } | |
1966 | EXPORT_SYMBOL(unregister_netdevice_notifier_net); | |
a30c7b42 | 1967 | |
93642e14 JP |
1968 | int register_netdevice_notifier_dev_net(struct net_device *dev, |
1969 | struct notifier_block *nb, | |
1970 | struct netdev_net_notifier *nn) | |
1971 | { | |
1972 | int err; | |
a30c7b42 | 1973 | |
93642e14 JP |
1974 | rtnl_lock(); |
1975 | err = __register_netdevice_notifier_net(dev_net(dev), nb, false); | |
1976 | if (!err) { | |
1977 | nn->nb = nb; | |
1978 | list_add(&nn->list, &dev->net_notifier_list); | |
1979 | } | |
a30c7b42 JP |
1980 | rtnl_unlock(); |
1981 | return err; | |
1982 | } | |
93642e14 JP |
1983 | EXPORT_SYMBOL(register_netdevice_notifier_dev_net); |
1984 | ||
1985 | int unregister_netdevice_notifier_dev_net(struct net_device *dev, | |
1986 | struct notifier_block *nb, | |
1987 | struct netdev_net_notifier *nn) | |
1988 | { | |
1989 | int err; | |
1990 | ||
1991 | rtnl_lock(); | |
1992 | list_del(&nn->list); | |
1993 | err = __unregister_netdevice_notifier_net(dev_net(dev), nb); | |
1994 | rtnl_unlock(); | |
1995 | return err; | |
1996 | } | |
1997 | EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net); | |
1998 | ||
1999 | static void move_netdevice_notifiers_dev_net(struct net_device *dev, | |
2000 | struct net *net) | |
2001 | { | |
2002 | struct netdev_net_notifier *nn; | |
2003 | ||
2004 | list_for_each_entry(nn, &dev->net_notifier_list, list) { | |
2005 | __unregister_netdevice_notifier_net(dev_net(dev), nn->nb); | |
2006 | __register_netdevice_notifier_net(net, nn->nb, true); | |
2007 | } | |
2008 | } | |
a30c7b42 | 2009 | |
351638e7 JP |
2010 | /** |
2011 | * call_netdevice_notifiers_info - call all network notifier blocks | |
2012 | * @val: value passed unmodified to notifier function | |
351638e7 JP |
2013 | * @info: notifier information data |
2014 | * | |
2015 | * Call all network notifier blocks. Parameters and return value | |
2016 | * are as for raw_notifier_call_chain(). | |
2017 | */ | |
2018 | ||
1d143d9f | 2019 | static int call_netdevice_notifiers_info(unsigned long val, |
1d143d9f | 2020 | struct netdev_notifier_info *info) |
351638e7 | 2021 | { |
a30c7b42 JP |
2022 | struct net *net = dev_net(info->dev); |
2023 | int ret; | |
2024 | ||
351638e7 | 2025 | ASSERT_RTNL(); |
a30c7b42 JP |
2026 | |
2027 | /* Run per-netns notifier block chain first, then run the global one. | |
2028 | * Hopefully, one day, the global one is going to be removed after | |
2029 | * all notifier block registrators get converted to be per-netns. | |
2030 | */ | |
2031 | ret = raw_notifier_call_chain(&net->netdev_chain, val, info); | |
2032 | if (ret & NOTIFY_STOP_MASK) | |
2033 | return ret; | |
351638e7 JP |
2034 | return raw_notifier_call_chain(&netdev_chain, val, info); |
2035 | } | |
351638e7 | 2036 | |
26372605 PM |
2037 | static int call_netdevice_notifiers_extack(unsigned long val, |
2038 | struct net_device *dev, | |
2039 | struct netlink_ext_ack *extack) | |
2040 | { | |
2041 | struct netdev_notifier_info info = { | |
2042 | .dev = dev, | |
2043 | .extack = extack, | |
2044 | }; | |
2045 | ||
2046 | return call_netdevice_notifiers_info(val, &info); | |
2047 | } | |
2048 | ||
1da177e4 LT |
2049 | /** |
2050 | * call_netdevice_notifiers - call all network notifier blocks | |
2051 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 2052 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
2053 | * |
2054 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 2055 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
2056 | */ |
2057 | ||
ad7379d4 | 2058 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 2059 | { |
26372605 | 2060 | return call_netdevice_notifiers_extack(val, dev, NULL); |
1da177e4 | 2061 | } |
edf947f1 | 2062 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 2063 | |
af7d6cce SD |
2064 | /** |
2065 | * call_netdevice_notifiers_mtu - call all network notifier blocks | |
2066 | * @val: value passed unmodified to notifier function | |
2067 | * @dev: net_device pointer passed unmodified to notifier function | |
2068 | * @arg: additional u32 argument passed to the notifier function | |
2069 | * | |
2070 | * Call all network notifier blocks. Parameters and return value | |
2071 | * are as for raw_notifier_call_chain(). | |
2072 | */ | |
2073 | static int call_netdevice_notifiers_mtu(unsigned long val, | |
2074 | struct net_device *dev, u32 arg) | |
2075 | { | |
2076 | struct netdev_notifier_info_ext info = { | |
2077 | .info.dev = dev, | |
2078 | .ext.mtu = arg, | |
2079 | }; | |
2080 | ||
2081 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); | |
2082 | ||
2083 | return call_netdevice_notifiers_info(val, &info.info); | |
2084 | } | |
2085 | ||
1cf51900 | 2086 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 2087 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
4577139b DB |
2088 | |
2089 | void net_inc_ingress_queue(void) | |
2090 | { | |
aabf6772 | 2091 | static_branch_inc(&ingress_needed_key); |
4577139b DB |
2092 | } |
2093 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); | |
2094 | ||
2095 | void net_dec_ingress_queue(void) | |
2096 | { | |
aabf6772 | 2097 | static_branch_dec(&ingress_needed_key); |
4577139b DB |
2098 | } |
2099 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); | |
2100 | #endif | |
2101 | ||
1f211a1b | 2102 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 2103 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
1f211a1b DB |
2104 | |
2105 | void net_inc_egress_queue(void) | |
2106 | { | |
aabf6772 | 2107 | static_branch_inc(&egress_needed_key); |
1f211a1b DB |
2108 | } |
2109 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); | |
2110 | ||
2111 | void net_dec_egress_queue(void) | |
2112 | { | |
aabf6772 | 2113 | static_branch_dec(&egress_needed_key); |
1f211a1b DB |
2114 | } |
2115 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); | |
2116 | #endif | |
2117 | ||
39e83922 | 2118 | static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
e9666d10 | 2119 | #ifdef CONFIG_JUMP_LABEL |
b90e5794 | 2120 | static atomic_t netstamp_needed_deferred; |
13baa00a | 2121 | static atomic_t netstamp_wanted; |
5fa8bbda | 2122 | static void netstamp_clear(struct work_struct *work) |
1da177e4 | 2123 | { |
b90e5794 | 2124 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
13baa00a | 2125 | int wanted; |
b90e5794 | 2126 | |
13baa00a ED |
2127 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
2128 | if (wanted > 0) | |
39e83922 | 2129 | static_branch_enable(&netstamp_needed_key); |
13baa00a | 2130 | else |
39e83922 | 2131 | static_branch_disable(&netstamp_needed_key); |
5fa8bbda ED |
2132 | } |
2133 | static DECLARE_WORK(netstamp_work, netstamp_clear); | |
b90e5794 | 2134 | #endif |
5fa8bbda ED |
2135 | |
2136 | void net_enable_timestamp(void) | |
2137 | { | |
e9666d10 | 2138 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2139 | int wanted; |
2140 | ||
2141 | while (1) { | |
2142 | wanted = atomic_read(&netstamp_wanted); | |
2143 | if (wanted <= 0) | |
2144 | break; | |
2145 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted) | |
2146 | return; | |
2147 | } | |
2148 | atomic_inc(&netstamp_needed_deferred); | |
2149 | schedule_work(&netstamp_work); | |
2150 | #else | |
39e83922 | 2151 | static_branch_inc(&netstamp_needed_key); |
13baa00a | 2152 | #endif |
1da177e4 | 2153 | } |
d1b19dff | 2154 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
2155 | |
2156 | void net_disable_timestamp(void) | |
2157 | { | |
e9666d10 | 2158 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2159 | int wanted; |
2160 | ||
2161 | while (1) { | |
2162 | wanted = atomic_read(&netstamp_wanted); | |
2163 | if (wanted <= 1) | |
2164 | break; | |
2165 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted) | |
2166 | return; | |
2167 | } | |
2168 | atomic_dec(&netstamp_needed_deferred); | |
5fa8bbda ED |
2169 | schedule_work(&netstamp_work); |
2170 | #else | |
39e83922 | 2171 | static_branch_dec(&netstamp_needed_key); |
5fa8bbda | 2172 | #endif |
1da177e4 | 2173 | } |
d1b19dff | 2174 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 2175 | |
3b098e2d | 2176 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 2177 | { |
2456e855 | 2178 | skb->tstamp = 0; |
39e83922 | 2179 | if (static_branch_unlikely(&netstamp_needed_key)) |
a61bbcf2 | 2180 | __net_timestamp(skb); |
1da177e4 LT |
2181 | } |
2182 | ||
39e83922 DB |
2183 | #define net_timestamp_check(COND, SKB) \ |
2184 | if (static_branch_unlikely(&netstamp_needed_key)) { \ | |
2185 | if ((COND) && !(SKB)->tstamp) \ | |
2186 | __net_timestamp(SKB); \ | |
2187 | } \ | |
3b098e2d | 2188 | |
f4b05d27 | 2189 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
79b569f0 DL |
2190 | { |
2191 | unsigned int len; | |
2192 | ||
2193 | if (!(dev->flags & IFF_UP)) | |
2194 | return false; | |
2195 | ||
2196 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; | |
2197 | if (skb->len <= len) | |
2198 | return true; | |
2199 | ||
2200 | /* if TSO is enabled, we don't care about the length as the packet | |
2201 | * could be forwarded without being segmented before | |
2202 | */ | |
2203 | if (skb_is_gso(skb)) | |
2204 | return true; | |
2205 | ||
2206 | return false; | |
2207 | } | |
1ee481fb | 2208 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
79b569f0 | 2209 | |
a0265d28 HX |
2210 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
2211 | { | |
4e3264d2 | 2212 | int ret = ____dev_forward_skb(dev, skb); |
a0265d28 | 2213 | |
4e3264d2 MKL |
2214 | if (likely(!ret)) { |
2215 | skb->protocol = eth_type_trans(skb, dev); | |
2216 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
2217 | } | |
a0265d28 | 2218 | |
4e3264d2 | 2219 | return ret; |
a0265d28 HX |
2220 | } |
2221 | EXPORT_SYMBOL_GPL(__dev_forward_skb); | |
2222 | ||
44540960 AB |
2223 | /** |
2224 | * dev_forward_skb - loopback an skb to another netif | |
2225 | * | |
2226 | * @dev: destination network device | |
2227 | * @skb: buffer to forward | |
2228 | * | |
2229 | * return values: | |
2230 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 2231 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
2232 | * |
2233 | * dev_forward_skb can be used for injecting an skb from the | |
2234 | * start_xmit function of one device into the receive queue | |
2235 | * of another device. | |
2236 | * | |
2237 | * The receiving device may be in another namespace, so | |
2238 | * we have to clear all information in the skb that could | |
2239 | * impact namespace isolation. | |
2240 | */ | |
2241 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2242 | { | |
a0265d28 | 2243 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
44540960 AB |
2244 | } |
2245 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
2246 | ||
71d9dec2 CG |
2247 | static inline int deliver_skb(struct sk_buff *skb, |
2248 | struct packet_type *pt_prev, | |
2249 | struct net_device *orig_dev) | |
2250 | { | |
1f8b977a | 2251 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
1080e512 | 2252 | return -ENOMEM; |
63354797 | 2253 | refcount_inc(&skb->users); |
71d9dec2 CG |
2254 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
2255 | } | |
2256 | ||
7866a621 SN |
2257 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
2258 | struct packet_type **pt, | |
fbcb2170 JP |
2259 | struct net_device *orig_dev, |
2260 | __be16 type, | |
7866a621 SN |
2261 | struct list_head *ptype_list) |
2262 | { | |
2263 | struct packet_type *ptype, *pt_prev = *pt; | |
2264 | ||
2265 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
2266 | if (ptype->type != type) | |
2267 | continue; | |
2268 | if (pt_prev) | |
fbcb2170 | 2269 | deliver_skb(skb, pt_prev, orig_dev); |
7866a621 SN |
2270 | pt_prev = ptype; |
2271 | } | |
2272 | *pt = pt_prev; | |
2273 | } | |
2274 | ||
c0de08d0 EL |
2275 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
2276 | { | |
a3d744e9 | 2277 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
2278 | return false; |
2279 | ||
2280 | if (ptype->id_match) | |
2281 | return ptype->id_match(ptype, skb->sk); | |
2282 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
2283 | return true; | |
2284 | ||
2285 | return false; | |
2286 | } | |
2287 | ||
9f9a742d MR |
2288 | /** |
2289 | * dev_nit_active - return true if any network interface taps are in use | |
2290 | * | |
2291 | * @dev: network device to check for the presence of taps | |
2292 | */ | |
2293 | bool dev_nit_active(struct net_device *dev) | |
2294 | { | |
2295 | return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all); | |
2296 | } | |
2297 | EXPORT_SYMBOL_GPL(dev_nit_active); | |
2298 | ||
1da177e4 LT |
2299 | /* |
2300 | * Support routine. Sends outgoing frames to any network | |
2301 | * taps currently in use. | |
2302 | */ | |
2303 | ||
74b20582 | 2304 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
2305 | { |
2306 | struct packet_type *ptype; | |
71d9dec2 CG |
2307 | struct sk_buff *skb2 = NULL; |
2308 | struct packet_type *pt_prev = NULL; | |
7866a621 | 2309 | struct list_head *ptype_list = &ptype_all; |
a61bbcf2 | 2310 | |
1da177e4 | 2311 | rcu_read_lock(); |
7866a621 SN |
2312 | again: |
2313 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
fa788d98 VW |
2314 | if (ptype->ignore_outgoing) |
2315 | continue; | |
2316 | ||
1da177e4 LT |
2317 | /* Never send packets back to the socket |
2318 | * they originated from - MvS (miquels@drinkel.ow.org) | |
2319 | */ | |
7866a621 SN |
2320 | if (skb_loop_sk(ptype, skb)) |
2321 | continue; | |
71d9dec2 | 2322 | |
7866a621 SN |
2323 | if (pt_prev) { |
2324 | deliver_skb(skb2, pt_prev, skb->dev); | |
2325 | pt_prev = ptype; | |
2326 | continue; | |
2327 | } | |
1da177e4 | 2328 | |
7866a621 SN |
2329 | /* need to clone skb, done only once */ |
2330 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
2331 | if (!skb2) | |
2332 | goto out_unlock; | |
70978182 | 2333 | |
7866a621 | 2334 | net_timestamp_set(skb2); |
1da177e4 | 2335 | |
7866a621 SN |
2336 | /* skb->nh should be correctly |
2337 | * set by sender, so that the second statement is | |
2338 | * just protection against buggy protocols. | |
2339 | */ | |
2340 | skb_reset_mac_header(skb2); | |
2341 | ||
2342 | if (skb_network_header(skb2) < skb2->data || | |
2343 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { | |
2344 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", | |
2345 | ntohs(skb2->protocol), | |
2346 | dev->name); | |
2347 | skb_reset_network_header(skb2); | |
1da177e4 | 2348 | } |
7866a621 SN |
2349 | |
2350 | skb2->transport_header = skb2->network_header; | |
2351 | skb2->pkt_type = PACKET_OUTGOING; | |
2352 | pt_prev = ptype; | |
2353 | } | |
2354 | ||
2355 | if (ptype_list == &ptype_all) { | |
2356 | ptype_list = &dev->ptype_all; | |
2357 | goto again; | |
1da177e4 | 2358 | } |
7866a621 | 2359 | out_unlock: |
581fe0ea WB |
2360 | if (pt_prev) { |
2361 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) | |
2362 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
2363 | else | |
2364 | kfree_skb(skb2); | |
2365 | } | |
1da177e4 LT |
2366 | rcu_read_unlock(); |
2367 | } | |
74b20582 | 2368 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
1da177e4 | 2369 | |
2c53040f BH |
2370 | /** |
2371 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
2372 | * @dev: Network device |
2373 | * @txq: number of queues available | |
2374 | * | |
2375 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
2376 | * valid. To resolve this verify the tc mapping remains valid and if | |
2377 | * not NULL the mapping. With no priorities mapping to this | |
2378 | * offset/count pair it will no longer be used. In the worst case TC0 | |
2379 | * is invalid nothing can be done so disable priority mappings. If is | |
2380 | * expected that drivers will fix this mapping if they can before | |
2381 | * calling netif_set_real_num_tx_queues. | |
2382 | */ | |
bb134d22 | 2383 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
2384 | { |
2385 | int i; | |
2386 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2387 | ||
2388 | /* If TC0 is invalidated disable TC mapping */ | |
2389 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce | 2390 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
2391 | dev->num_tc = 0; |
2392 | return; | |
2393 | } | |
2394 | ||
2395 | /* Invalidated prio to tc mappings set to TC0 */ | |
2396 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
2397 | int q = netdev_get_prio_tc_map(dev, i); | |
2398 | ||
2399 | tc = &dev->tc_to_txq[q]; | |
2400 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce JP |
2401 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
2402 | i, q); | |
4f57c087 JF |
2403 | netdev_set_prio_tc_map(dev, i, 0); |
2404 | } | |
2405 | } | |
2406 | } | |
2407 | ||
8d059b0f AD |
2408 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
2409 | { | |
2410 | if (dev->num_tc) { | |
2411 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2412 | int i; | |
2413 | ||
ffcfe25b | 2414 | /* walk through the TCs and see if it falls into any of them */ |
8d059b0f AD |
2415 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
2416 | if ((txq - tc->offset) < tc->count) | |
2417 | return i; | |
2418 | } | |
2419 | ||
ffcfe25b | 2420 | /* didn't find it, just return -1 to indicate no match */ |
8d059b0f AD |
2421 | return -1; |
2422 | } | |
2423 | ||
2424 | return 0; | |
2425 | } | |
8a5f2166 | 2426 | EXPORT_SYMBOL(netdev_txq_to_tc); |
8d059b0f | 2427 | |
537c00de | 2428 | #ifdef CONFIG_XPS |
04157469 AN |
2429 | struct static_key xps_needed __read_mostly; |
2430 | EXPORT_SYMBOL(xps_needed); | |
2431 | struct static_key xps_rxqs_needed __read_mostly; | |
2432 | EXPORT_SYMBOL(xps_rxqs_needed); | |
537c00de AD |
2433 | static DEFINE_MUTEX(xps_map_mutex); |
2434 | #define xmap_dereference(P) \ | |
2435 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
2436 | ||
6234f874 AD |
2437 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
2438 | int tci, u16 index) | |
537c00de | 2439 | { |
10cdc3f3 AD |
2440 | struct xps_map *map = NULL; |
2441 | int pos; | |
537c00de | 2442 | |
10cdc3f3 | 2443 | if (dev_maps) |
80d19669 | 2444 | map = xmap_dereference(dev_maps->attr_map[tci]); |
6234f874 AD |
2445 | if (!map) |
2446 | return false; | |
537c00de | 2447 | |
6234f874 AD |
2448 | for (pos = map->len; pos--;) { |
2449 | if (map->queues[pos] != index) | |
2450 | continue; | |
2451 | ||
2452 | if (map->len > 1) { | |
2453 | map->queues[pos] = map->queues[--map->len]; | |
10cdc3f3 | 2454 | break; |
537c00de | 2455 | } |
6234f874 | 2456 | |
80d19669 | 2457 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
6234f874 AD |
2458 | kfree_rcu(map, rcu); |
2459 | return false; | |
537c00de AD |
2460 | } |
2461 | ||
6234f874 | 2462 | return true; |
10cdc3f3 AD |
2463 | } |
2464 | ||
6234f874 AD |
2465 | static bool remove_xps_queue_cpu(struct net_device *dev, |
2466 | struct xps_dev_maps *dev_maps, | |
2467 | int cpu, u16 offset, u16 count) | |
2468 | { | |
184c449f AD |
2469 | int num_tc = dev->num_tc ? : 1; |
2470 | bool active = false; | |
2471 | int tci; | |
6234f874 | 2472 | |
184c449f AD |
2473 | for (tci = cpu * num_tc; num_tc--; tci++) { |
2474 | int i, j; | |
2475 | ||
2476 | for (i = count, j = offset; i--; j++) { | |
6358d49a | 2477 | if (!remove_xps_queue(dev_maps, tci, j)) |
184c449f AD |
2478 | break; |
2479 | } | |
2480 | ||
2481 | active |= i < 0; | |
6234f874 AD |
2482 | } |
2483 | ||
184c449f | 2484 | return active; |
6234f874 AD |
2485 | } |
2486 | ||
867d0ad4 SD |
2487 | static void reset_xps_maps(struct net_device *dev, |
2488 | struct xps_dev_maps *dev_maps, | |
2489 | bool is_rxqs_map) | |
2490 | { | |
2491 | if (is_rxqs_map) { | |
2492 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); | |
2493 | RCU_INIT_POINTER(dev->xps_rxqs_map, NULL); | |
2494 | } else { | |
2495 | RCU_INIT_POINTER(dev->xps_cpus_map, NULL); | |
2496 | } | |
2497 | static_key_slow_dec_cpuslocked(&xps_needed); | |
2498 | kfree_rcu(dev_maps, rcu); | |
2499 | } | |
2500 | ||
80d19669 AN |
2501 | static void clean_xps_maps(struct net_device *dev, const unsigned long *mask, |
2502 | struct xps_dev_maps *dev_maps, unsigned int nr_ids, | |
2503 | u16 offset, u16 count, bool is_rxqs_map) | |
2504 | { | |
2505 | bool active = false; | |
2506 | int i, j; | |
2507 | ||
2508 | for (j = -1; j = netif_attrmask_next(j, mask, nr_ids), | |
2509 | j < nr_ids;) | |
2510 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, | |
2511 | count); | |
867d0ad4 SD |
2512 | if (!active) |
2513 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
80d19669 | 2514 | |
f28c020f SD |
2515 | if (!is_rxqs_map) { |
2516 | for (i = offset + (count - 1); count--; i--) { | |
2517 | netdev_queue_numa_node_write( | |
2518 | netdev_get_tx_queue(dev, i), | |
2519 | NUMA_NO_NODE); | |
80d19669 | 2520 | } |
80d19669 AN |
2521 | } |
2522 | } | |
2523 | ||
6234f874 AD |
2524 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
2525 | u16 count) | |
10cdc3f3 | 2526 | { |
80d19669 | 2527 | const unsigned long *possible_mask = NULL; |
10cdc3f3 | 2528 | struct xps_dev_maps *dev_maps; |
80d19669 | 2529 | unsigned int nr_ids; |
10cdc3f3 | 2530 | |
04157469 AN |
2531 | if (!static_key_false(&xps_needed)) |
2532 | return; | |
10cdc3f3 | 2533 | |
4d99f660 | 2534 | cpus_read_lock(); |
04157469 | 2535 | mutex_lock(&xps_map_mutex); |
10cdc3f3 | 2536 | |
04157469 AN |
2537 | if (static_key_false(&xps_rxqs_needed)) { |
2538 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2539 | if (dev_maps) { | |
2540 | nr_ids = dev->num_rx_queues; | |
2541 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, | |
2542 | offset, count, true); | |
2543 | } | |
537c00de AD |
2544 | } |
2545 | ||
80d19669 AN |
2546 | dev_maps = xmap_dereference(dev->xps_cpus_map); |
2547 | if (!dev_maps) | |
2548 | goto out_no_maps; | |
2549 | ||
2550 | if (num_possible_cpus() > 1) | |
2551 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2552 | nr_ids = nr_cpu_ids; | |
2553 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count, | |
2554 | false); | |
024e9679 | 2555 | |
537c00de AD |
2556 | out_no_maps: |
2557 | mutex_unlock(&xps_map_mutex); | |
4d99f660 | 2558 | cpus_read_unlock(); |
537c00de AD |
2559 | } |
2560 | ||
6234f874 AD |
2561 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
2562 | { | |
2563 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); | |
2564 | } | |
2565 | ||
80d19669 AN |
2566 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
2567 | u16 index, bool is_rxqs_map) | |
01c5f864 AD |
2568 | { |
2569 | struct xps_map *new_map; | |
2570 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
2571 | int i, pos; | |
2572 | ||
2573 | for (pos = 0; map && pos < map->len; pos++) { | |
2574 | if (map->queues[pos] != index) | |
2575 | continue; | |
2576 | return map; | |
2577 | } | |
2578 | ||
80d19669 | 2579 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
01c5f864 AD |
2580 | if (map) { |
2581 | if (pos < map->alloc_len) | |
2582 | return map; | |
2583 | ||
2584 | alloc_len = map->alloc_len * 2; | |
2585 | } | |
2586 | ||
80d19669 AN |
2587 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
2588 | * map | |
2589 | */ | |
2590 | if (is_rxqs_map) | |
2591 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); | |
2592 | else | |
2593 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
2594 | cpu_to_node(attr_index)); | |
01c5f864 AD |
2595 | if (!new_map) |
2596 | return NULL; | |
2597 | ||
2598 | for (i = 0; i < pos; i++) | |
2599 | new_map->queues[i] = map->queues[i]; | |
2600 | new_map->alloc_len = alloc_len; | |
2601 | new_map->len = pos; | |
2602 | ||
2603 | return new_map; | |
2604 | } | |
2605 | ||
4d99f660 | 2606 | /* Must be called under cpus_read_lock */ |
80d19669 AN |
2607 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
2608 | u16 index, bool is_rxqs_map) | |
537c00de | 2609 | { |
80d19669 | 2610 | const unsigned long *online_mask = NULL, *possible_mask = NULL; |
01c5f864 | 2611 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
80d19669 | 2612 | int i, j, tci, numa_node_id = -2; |
184c449f | 2613 | int maps_sz, num_tc = 1, tc = 0; |
537c00de | 2614 | struct xps_map *map, *new_map; |
01c5f864 | 2615 | bool active = false; |
80d19669 | 2616 | unsigned int nr_ids; |
537c00de | 2617 | |
184c449f | 2618 | if (dev->num_tc) { |
ffcfe25b | 2619 | /* Do not allow XPS on subordinate device directly */ |
184c449f | 2620 | num_tc = dev->num_tc; |
ffcfe25b AD |
2621 | if (num_tc < 0) |
2622 | return -EINVAL; | |
2623 | ||
2624 | /* If queue belongs to subordinate dev use its map */ | |
2625 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
2626 | ||
184c449f AD |
2627 | tc = netdev_txq_to_tc(dev, index); |
2628 | if (tc < 0) | |
2629 | return -EINVAL; | |
2630 | } | |
2631 | ||
537c00de | 2632 | mutex_lock(&xps_map_mutex); |
80d19669 AN |
2633 | if (is_rxqs_map) { |
2634 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); | |
2635 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2636 | nr_ids = dev->num_rx_queues; | |
2637 | } else { | |
2638 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); | |
2639 | if (num_possible_cpus() > 1) { | |
2640 | online_mask = cpumask_bits(cpu_online_mask); | |
2641 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2642 | } | |
2643 | dev_maps = xmap_dereference(dev->xps_cpus_map); | |
2644 | nr_ids = nr_cpu_ids; | |
2645 | } | |
537c00de | 2646 | |
80d19669 AN |
2647 | if (maps_sz < L1_CACHE_BYTES) |
2648 | maps_sz = L1_CACHE_BYTES; | |
537c00de | 2649 | |
01c5f864 | 2650 | /* allocate memory for queue storage */ |
80d19669 AN |
2651 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
2652 | j < nr_ids;) { | |
01c5f864 AD |
2653 | if (!new_dev_maps) |
2654 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); | |
2bb60cb9 AD |
2655 | if (!new_dev_maps) { |
2656 | mutex_unlock(&xps_map_mutex); | |
01c5f864 | 2657 | return -ENOMEM; |
2bb60cb9 | 2658 | } |
01c5f864 | 2659 | |
80d19669 AN |
2660 | tci = j * num_tc + tc; |
2661 | map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) : | |
01c5f864 AD |
2662 | NULL; |
2663 | ||
80d19669 | 2664 | map = expand_xps_map(map, j, index, is_rxqs_map); |
01c5f864 AD |
2665 | if (!map) |
2666 | goto error; | |
2667 | ||
80d19669 | 2668 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
01c5f864 AD |
2669 | } |
2670 | ||
2671 | if (!new_dev_maps) | |
2672 | goto out_no_new_maps; | |
2673 | ||
867d0ad4 SD |
2674 | if (!dev_maps) { |
2675 | /* Increment static keys at most once per type */ | |
2676 | static_key_slow_inc_cpuslocked(&xps_needed); | |
2677 | if (is_rxqs_map) | |
2678 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); | |
2679 | } | |
04157469 | 2680 | |
80d19669 AN |
2681 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2682 | j < nr_ids;) { | |
184c449f | 2683 | /* copy maps belonging to foreign traffic classes */ |
80d19669 | 2684 | for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) { |
184c449f | 2685 | /* fill in the new device map from the old device map */ |
80d19669 AN |
2686 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2687 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f AD |
2688 | } |
2689 | ||
2690 | /* We need to explicitly update tci as prevous loop | |
2691 | * could break out early if dev_maps is NULL. | |
2692 | */ | |
80d19669 | 2693 | tci = j * num_tc + tc; |
184c449f | 2694 | |
80d19669 AN |
2695 | if (netif_attr_test_mask(j, mask, nr_ids) && |
2696 | netif_attr_test_online(j, online_mask, nr_ids)) { | |
2697 | /* add tx-queue to CPU/rx-queue maps */ | |
01c5f864 AD |
2698 | int pos = 0; |
2699 | ||
80d19669 | 2700 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
01c5f864 AD |
2701 | while ((pos < map->len) && (map->queues[pos] != index)) |
2702 | pos++; | |
2703 | ||
2704 | if (pos == map->len) | |
2705 | map->queues[map->len++] = index; | |
537c00de | 2706 | #ifdef CONFIG_NUMA |
80d19669 AN |
2707 | if (!is_rxqs_map) { |
2708 | if (numa_node_id == -2) | |
2709 | numa_node_id = cpu_to_node(j); | |
2710 | else if (numa_node_id != cpu_to_node(j)) | |
2711 | numa_node_id = -1; | |
2712 | } | |
537c00de | 2713 | #endif |
01c5f864 AD |
2714 | } else if (dev_maps) { |
2715 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2716 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2717 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
537c00de | 2718 | } |
01c5f864 | 2719 | |
184c449f AD |
2720 | /* copy maps belonging to foreign traffic classes */ |
2721 | for (i = num_tc - tc, tci++; dev_maps && --i; tci++) { | |
2722 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2723 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2724 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f | 2725 | } |
537c00de AD |
2726 | } |
2727 | ||
80d19669 AN |
2728 | if (is_rxqs_map) |
2729 | rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps); | |
2730 | else | |
2731 | rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps); | |
01c5f864 | 2732 | |
537c00de | 2733 | /* Cleanup old maps */ |
184c449f AD |
2734 | if (!dev_maps) |
2735 | goto out_no_old_maps; | |
2736 | ||
80d19669 AN |
2737 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2738 | j < nr_ids;) { | |
2739 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2740 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
2741 | map = xmap_dereference(dev_maps->attr_map[tci]); | |
01c5f864 AD |
2742 | if (map && map != new_map) |
2743 | kfree_rcu(map, rcu); | |
2744 | } | |
537c00de AD |
2745 | } |
2746 | ||
184c449f AD |
2747 | kfree_rcu(dev_maps, rcu); |
2748 | ||
2749 | out_no_old_maps: | |
01c5f864 AD |
2750 | dev_maps = new_dev_maps; |
2751 | active = true; | |
537c00de | 2752 | |
01c5f864 | 2753 | out_no_new_maps: |
80d19669 AN |
2754 | if (!is_rxqs_map) { |
2755 | /* update Tx queue numa node */ | |
2756 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), | |
2757 | (numa_node_id >= 0) ? | |
2758 | numa_node_id : NUMA_NO_NODE); | |
2759 | } | |
537c00de | 2760 | |
01c5f864 AD |
2761 | if (!dev_maps) |
2762 | goto out_no_maps; | |
2763 | ||
80d19669 AN |
2764 | /* removes tx-queue from unused CPUs/rx-queues */ |
2765 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), | |
2766 | j < nr_ids;) { | |
2767 | for (i = tc, tci = j * num_tc; i--; tci++) | |
184c449f | 2768 | active |= remove_xps_queue(dev_maps, tci, index); |
80d19669 AN |
2769 | if (!netif_attr_test_mask(j, mask, nr_ids) || |
2770 | !netif_attr_test_online(j, online_mask, nr_ids)) | |
184c449f AD |
2771 | active |= remove_xps_queue(dev_maps, tci, index); |
2772 | for (i = num_tc - tc, tci++; --i; tci++) | |
2773 | active |= remove_xps_queue(dev_maps, tci, index); | |
01c5f864 AD |
2774 | } |
2775 | ||
2776 | /* free map if not active */ | |
867d0ad4 SD |
2777 | if (!active) |
2778 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
01c5f864 AD |
2779 | |
2780 | out_no_maps: | |
537c00de AD |
2781 | mutex_unlock(&xps_map_mutex); |
2782 | ||
2783 | return 0; | |
2784 | error: | |
01c5f864 | 2785 | /* remove any maps that we added */ |
80d19669 AN |
2786 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2787 | j < nr_ids;) { | |
2788 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2789 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
184c449f | 2790 | map = dev_maps ? |
80d19669 | 2791 | xmap_dereference(dev_maps->attr_map[tci]) : |
184c449f AD |
2792 | NULL; |
2793 | if (new_map && new_map != map) | |
2794 | kfree(new_map); | |
2795 | } | |
01c5f864 AD |
2796 | } |
2797 | ||
537c00de AD |
2798 | mutex_unlock(&xps_map_mutex); |
2799 | ||
537c00de AD |
2800 | kfree(new_dev_maps); |
2801 | return -ENOMEM; | |
2802 | } | |
4d99f660 | 2803 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
80d19669 AN |
2804 | |
2805 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, | |
2806 | u16 index) | |
2807 | { | |
4d99f660 AV |
2808 | int ret; |
2809 | ||
2810 | cpus_read_lock(); | |
2811 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false); | |
2812 | cpus_read_unlock(); | |
2813 | ||
2814 | return ret; | |
80d19669 | 2815 | } |
537c00de AD |
2816 | EXPORT_SYMBOL(netif_set_xps_queue); |
2817 | ||
2818 | #endif | |
ffcfe25b AD |
2819 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
2820 | { | |
2821 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2822 | ||
2823 | /* Unbind any subordinate channels */ | |
2824 | while (txq-- != &dev->_tx[0]) { | |
2825 | if (txq->sb_dev) | |
2826 | netdev_unbind_sb_channel(dev, txq->sb_dev); | |
2827 | } | |
2828 | } | |
2829 | ||
9cf1f6a8 AD |
2830 | void netdev_reset_tc(struct net_device *dev) |
2831 | { | |
6234f874 AD |
2832 | #ifdef CONFIG_XPS |
2833 | netif_reset_xps_queues_gt(dev, 0); | |
2834 | #endif | |
ffcfe25b AD |
2835 | netdev_unbind_all_sb_channels(dev); |
2836 | ||
2837 | /* Reset TC configuration of device */ | |
9cf1f6a8 AD |
2838 | dev->num_tc = 0; |
2839 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); | |
2840 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); | |
2841 | } | |
2842 | EXPORT_SYMBOL(netdev_reset_tc); | |
2843 | ||
2844 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) | |
2845 | { | |
2846 | if (tc >= dev->num_tc) | |
2847 | return -EINVAL; | |
2848 | ||
6234f874 AD |
2849 | #ifdef CONFIG_XPS |
2850 | netif_reset_xps_queues(dev, offset, count); | |
2851 | #endif | |
9cf1f6a8 AD |
2852 | dev->tc_to_txq[tc].count = count; |
2853 | dev->tc_to_txq[tc].offset = offset; | |
2854 | return 0; | |
2855 | } | |
2856 | EXPORT_SYMBOL(netdev_set_tc_queue); | |
2857 | ||
2858 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) | |
2859 | { | |
2860 | if (num_tc > TC_MAX_QUEUE) | |
2861 | return -EINVAL; | |
2862 | ||
6234f874 AD |
2863 | #ifdef CONFIG_XPS |
2864 | netif_reset_xps_queues_gt(dev, 0); | |
2865 | #endif | |
ffcfe25b AD |
2866 | netdev_unbind_all_sb_channels(dev); |
2867 | ||
9cf1f6a8 AD |
2868 | dev->num_tc = num_tc; |
2869 | return 0; | |
2870 | } | |
2871 | EXPORT_SYMBOL(netdev_set_num_tc); | |
2872 | ||
ffcfe25b AD |
2873 | void netdev_unbind_sb_channel(struct net_device *dev, |
2874 | struct net_device *sb_dev) | |
2875 | { | |
2876 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2877 | ||
2878 | #ifdef CONFIG_XPS | |
2879 | netif_reset_xps_queues_gt(sb_dev, 0); | |
2880 | #endif | |
2881 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); | |
2882 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); | |
2883 | ||
2884 | while (txq-- != &dev->_tx[0]) { | |
2885 | if (txq->sb_dev == sb_dev) | |
2886 | txq->sb_dev = NULL; | |
2887 | } | |
2888 | } | |
2889 | EXPORT_SYMBOL(netdev_unbind_sb_channel); | |
2890 | ||
2891 | int netdev_bind_sb_channel_queue(struct net_device *dev, | |
2892 | struct net_device *sb_dev, | |
2893 | u8 tc, u16 count, u16 offset) | |
2894 | { | |
2895 | /* Make certain the sb_dev and dev are already configured */ | |
2896 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) | |
2897 | return -EINVAL; | |
2898 | ||
2899 | /* We cannot hand out queues we don't have */ | |
2900 | if ((offset + count) > dev->real_num_tx_queues) | |
2901 | return -EINVAL; | |
2902 | ||
2903 | /* Record the mapping */ | |
2904 | sb_dev->tc_to_txq[tc].count = count; | |
2905 | sb_dev->tc_to_txq[tc].offset = offset; | |
2906 | ||
2907 | /* Provide a way for Tx queue to find the tc_to_txq map or | |
2908 | * XPS map for itself. | |
2909 | */ | |
2910 | while (count--) | |
2911 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; | |
2912 | ||
2913 | return 0; | |
2914 | } | |
2915 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); | |
2916 | ||
2917 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) | |
2918 | { | |
2919 | /* Do not use a multiqueue device to represent a subordinate channel */ | |
2920 | if (netif_is_multiqueue(dev)) | |
2921 | return -ENODEV; | |
2922 | ||
2923 | /* We allow channels 1 - 32767 to be used for subordinate channels. | |
2924 | * Channel 0 is meant to be "native" mode and used only to represent | |
2925 | * the main root device. We allow writing 0 to reset the device back | |
2926 | * to normal mode after being used as a subordinate channel. | |
2927 | */ | |
2928 | if (channel > S16_MAX) | |
2929 | return -EINVAL; | |
2930 | ||
2931 | dev->num_tc = -channel; | |
2932 | ||
2933 | return 0; | |
2934 | } | |
2935 | EXPORT_SYMBOL(netdev_set_sb_channel); | |
2936 | ||
f0796d5c JF |
2937 | /* |
2938 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
3a053b1a | 2939 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
f0796d5c | 2940 | */ |
e6484930 | 2941 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2942 | { |
ac5b7019 | 2943 | bool disabling; |
1d24eb48 TH |
2944 | int rc; |
2945 | ||
ac5b7019 JK |
2946 | disabling = txq < dev->real_num_tx_queues; |
2947 | ||
e6484930 TH |
2948 | if (txq < 1 || txq > dev->num_tx_queues) |
2949 | return -EINVAL; | |
f0796d5c | 2950 | |
5c56580b BH |
2951 | if (dev->reg_state == NETREG_REGISTERED || |
2952 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2953 | ASSERT_RTNL(); |
2954 | ||
1d24eb48 TH |
2955 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2956 | txq); | |
bf264145 TH |
2957 | if (rc) |
2958 | return rc; | |
2959 | ||
4f57c087 JF |
2960 | if (dev->num_tc) |
2961 | netif_setup_tc(dev, txq); | |
2962 | ||
ac5b7019 JK |
2963 | dev->real_num_tx_queues = txq; |
2964 | ||
2965 | if (disabling) { | |
2966 | synchronize_net(); | |
e6484930 | 2967 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2968 | #ifdef CONFIG_XPS |
2969 | netif_reset_xps_queues_gt(dev, txq); | |
2970 | #endif | |
2971 | } | |
ac5b7019 JK |
2972 | } else { |
2973 | dev->real_num_tx_queues = txq; | |
f0796d5c | 2974 | } |
e6484930 | 2975 | |
e6484930 | 2976 | return 0; |
f0796d5c JF |
2977 | } |
2978 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2979 | |
a953be53 | 2980 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
2981 | /** |
2982 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2983 | * @dev: Network device | |
2984 | * @rxq: Actual number of RX queues | |
2985 | * | |
2986 | * This must be called either with the rtnl_lock held or before | |
2987 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2988 | * negative error code. If called before registration, it always |
2989 | * succeeds. | |
62fe0b40 BH |
2990 | */ |
2991 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2992 | { | |
2993 | int rc; | |
2994 | ||
bd25fa7b TH |
2995 | if (rxq < 1 || rxq > dev->num_rx_queues) |
2996 | return -EINVAL; | |
2997 | ||
62fe0b40 BH |
2998 | if (dev->reg_state == NETREG_REGISTERED) { |
2999 | ASSERT_RTNL(); | |
3000 | ||
62fe0b40 BH |
3001 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
3002 | rxq); | |
3003 | if (rc) | |
3004 | return rc; | |
62fe0b40 BH |
3005 | } |
3006 | ||
3007 | dev->real_num_rx_queues = rxq; | |
3008 | return 0; | |
3009 | } | |
3010 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
3011 | #endif | |
3012 | ||
2c53040f BH |
3013 | /** |
3014 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 YM |
3015 | * |
3016 | * This routine should set an upper limit on the number of RSS queues | |
3017 | * used by default by multiqueue devices. | |
3018 | */ | |
a55b138b | 3019 | int netif_get_num_default_rss_queues(void) |
16917b87 | 3020 | { |
40e4e713 HS |
3021 | return is_kdump_kernel() ? |
3022 | 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); | |
16917b87 YM |
3023 | } |
3024 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
3025 | ||
3bcb846c | 3026 | static void __netif_reschedule(struct Qdisc *q) |
56079431 | 3027 | { |
def82a1d JP |
3028 | struct softnet_data *sd; |
3029 | unsigned long flags; | |
56079431 | 3030 | |
def82a1d | 3031 | local_irq_save(flags); |
903ceff7 | 3032 | sd = this_cpu_ptr(&softnet_data); |
a9cbd588 CG |
3033 | q->next_sched = NULL; |
3034 | *sd->output_queue_tailp = q; | |
3035 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
3036 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
3037 | local_irq_restore(flags); | |
3038 | } | |
3039 | ||
3040 | void __netif_schedule(struct Qdisc *q) | |
3041 | { | |
3042 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
3043 | __netif_reschedule(q); | |
56079431 DV |
3044 | } |
3045 | EXPORT_SYMBOL(__netif_schedule); | |
3046 | ||
e6247027 ED |
3047 | struct dev_kfree_skb_cb { |
3048 | enum skb_free_reason reason; | |
3049 | }; | |
3050 | ||
3051 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) | |
56079431 | 3052 | { |
e6247027 ED |
3053 | return (struct dev_kfree_skb_cb *)skb->cb; |
3054 | } | |
3055 | ||
46e5da40 JF |
3056 | void netif_schedule_queue(struct netdev_queue *txq) |
3057 | { | |
3058 | rcu_read_lock(); | |
5be5515a | 3059 | if (!netif_xmit_stopped(txq)) { |
46e5da40 JF |
3060 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
3061 | ||
3062 | __netif_schedule(q); | |
3063 | } | |
3064 | rcu_read_unlock(); | |
3065 | } | |
3066 | EXPORT_SYMBOL(netif_schedule_queue); | |
3067 | ||
46e5da40 JF |
3068 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
3069 | { | |
3070 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { | |
3071 | struct Qdisc *q; | |
3072 | ||
3073 | rcu_read_lock(); | |
3074 | q = rcu_dereference(dev_queue->qdisc); | |
3075 | __netif_schedule(q); | |
3076 | rcu_read_unlock(); | |
3077 | } | |
3078 | } | |
3079 | EXPORT_SYMBOL(netif_tx_wake_queue); | |
3080 | ||
e6247027 | 3081 | void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 | 3082 | { |
e6247027 | 3083 | unsigned long flags; |
56079431 | 3084 | |
9899886d MJ |
3085 | if (unlikely(!skb)) |
3086 | return; | |
3087 | ||
63354797 | 3088 | if (likely(refcount_read(&skb->users) == 1)) { |
e6247027 | 3089 | smp_rmb(); |
63354797 RE |
3090 | refcount_set(&skb->users, 0); |
3091 | } else if (likely(!refcount_dec_and_test(&skb->users))) { | |
e6247027 | 3092 | return; |
bea3348e | 3093 | } |
e6247027 ED |
3094 | get_kfree_skb_cb(skb)->reason = reason; |
3095 | local_irq_save(flags); | |
3096 | skb->next = __this_cpu_read(softnet_data.completion_queue); | |
3097 | __this_cpu_write(softnet_data.completion_queue, skb); | |
3098 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
3099 | local_irq_restore(flags); | |
56079431 | 3100 | } |
e6247027 | 3101 | EXPORT_SYMBOL(__dev_kfree_skb_irq); |
56079431 | 3102 | |
e6247027 | 3103 | void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 DV |
3104 | { |
3105 | if (in_irq() || irqs_disabled()) | |
e6247027 | 3106 | __dev_kfree_skb_irq(skb, reason); |
56079431 DV |
3107 | else |
3108 | dev_kfree_skb(skb); | |
3109 | } | |
e6247027 | 3110 | EXPORT_SYMBOL(__dev_kfree_skb_any); |
56079431 DV |
3111 | |
3112 | ||
bea3348e SH |
3113 | /** |
3114 | * netif_device_detach - mark device as removed | |
3115 | * @dev: network device | |
3116 | * | |
3117 | * Mark device as removed from system and therefore no longer available. | |
3118 | */ | |
56079431 DV |
3119 | void netif_device_detach(struct net_device *dev) |
3120 | { | |
3121 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3122 | netif_running(dev)) { | |
d543103a | 3123 | netif_tx_stop_all_queues(dev); |
56079431 DV |
3124 | } |
3125 | } | |
3126 | EXPORT_SYMBOL(netif_device_detach); | |
3127 | ||
bea3348e SH |
3128 | /** |
3129 | * netif_device_attach - mark device as attached | |
3130 | * @dev: network device | |
3131 | * | |
3132 | * Mark device as attached from system and restart if needed. | |
3133 | */ | |
56079431 DV |
3134 | void netif_device_attach(struct net_device *dev) |
3135 | { | |
3136 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3137 | netif_running(dev)) { | |
d543103a | 3138 | netif_tx_wake_all_queues(dev); |
4ec93edb | 3139 | __netdev_watchdog_up(dev); |
56079431 DV |
3140 | } |
3141 | } | |
3142 | EXPORT_SYMBOL(netif_device_attach); | |
3143 | ||
5605c762 JP |
3144 | /* |
3145 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
3146 | * to be used as a distribution range. | |
3147 | */ | |
eadec877 AD |
3148 | static u16 skb_tx_hash(const struct net_device *dev, |
3149 | const struct net_device *sb_dev, | |
3150 | struct sk_buff *skb) | |
5605c762 JP |
3151 | { |
3152 | u32 hash; | |
3153 | u16 qoffset = 0; | |
1b837d48 | 3154 | u16 qcount = dev->real_num_tx_queues; |
5605c762 | 3155 | |
eadec877 AD |
3156 | if (dev->num_tc) { |
3157 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
3158 | ||
3159 | qoffset = sb_dev->tc_to_txq[tc].offset; | |
3160 | qcount = sb_dev->tc_to_txq[tc].count; | |
3161 | } | |
3162 | ||
5605c762 JP |
3163 | if (skb_rx_queue_recorded(skb)) { |
3164 | hash = skb_get_rx_queue(skb); | |
6e11d157 AN |
3165 | if (hash >= qoffset) |
3166 | hash -= qoffset; | |
1b837d48 AD |
3167 | while (unlikely(hash >= qcount)) |
3168 | hash -= qcount; | |
eadec877 | 3169 | return hash + qoffset; |
5605c762 JP |
3170 | } |
3171 | ||
3172 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; | |
3173 | } | |
5605c762 | 3174 | |
36c92474 BH |
3175 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
3176 | { | |
84d15ae5 | 3177 | static const netdev_features_t null_features; |
36c92474 | 3178 | struct net_device *dev = skb->dev; |
88ad4175 | 3179 | const char *name = ""; |
36c92474 | 3180 | |
c846ad9b BG |
3181 | if (!net_ratelimit()) |
3182 | return; | |
3183 | ||
88ad4175 BM |
3184 | if (dev) { |
3185 | if (dev->dev.parent) | |
3186 | name = dev_driver_string(dev->dev.parent); | |
3187 | else | |
3188 | name = netdev_name(dev); | |
3189 | } | |
6413139d WB |
3190 | skb_dump(KERN_WARNING, skb, false); |
3191 | WARN(1, "%s: caps=(%pNF, %pNF)\n", | |
88ad4175 | 3192 | name, dev ? &dev->features : &null_features, |
6413139d | 3193 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
36c92474 BH |
3194 | } |
3195 | ||
1da177e4 LT |
3196 | /* |
3197 | * Invalidate hardware checksum when packet is to be mangled, and | |
3198 | * complete checksum manually on outgoing path. | |
3199 | */ | |
84fa7933 | 3200 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 3201 | { |
d3bc23e7 | 3202 | __wsum csum; |
663ead3b | 3203 | int ret = 0, offset; |
1da177e4 | 3204 | |
84fa7933 | 3205 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
3206 | goto out_set_summed; |
3207 | ||
3208 | if (unlikely(skb_shinfo(skb)->gso_size)) { | |
36c92474 BH |
3209 | skb_warn_bad_offload(skb); |
3210 | return -EINVAL; | |
1da177e4 LT |
3211 | } |
3212 | ||
cef401de ED |
3213 | /* Before computing a checksum, we should make sure no frag could |
3214 | * be modified by an external entity : checksum could be wrong. | |
3215 | */ | |
3216 | if (skb_has_shared_frag(skb)) { | |
3217 | ret = __skb_linearize(skb); | |
3218 | if (ret) | |
3219 | goto out; | |
3220 | } | |
3221 | ||
55508d60 | 3222 | offset = skb_checksum_start_offset(skb); |
a030847e HX |
3223 | BUG_ON(offset >= skb_headlen(skb)); |
3224 | csum = skb_checksum(skb, offset, skb->len - offset, 0); | |
3225 | ||
3226 | offset += skb->csum_offset; | |
3227 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); | |
3228 | ||
8211fbfa HK |
3229 | ret = skb_ensure_writable(skb, offset + sizeof(__sum16)); |
3230 | if (ret) | |
3231 | goto out; | |
1da177e4 | 3232 | |
4f2e4ad5 | 3233 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
a430a43d | 3234 | out_set_summed: |
1da177e4 | 3235 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 3236 | out: |
1da177e4 LT |
3237 | return ret; |
3238 | } | |
d1b19dff | 3239 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 3240 | |
b72b5bf6 DC |
3241 | int skb_crc32c_csum_help(struct sk_buff *skb) |
3242 | { | |
3243 | __le32 crc32c_csum; | |
3244 | int ret = 0, offset, start; | |
3245 | ||
3246 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
3247 | goto out; | |
3248 | ||
3249 | if (unlikely(skb_is_gso(skb))) | |
3250 | goto out; | |
3251 | ||
3252 | /* Before computing a checksum, we should make sure no frag could | |
3253 | * be modified by an external entity : checksum could be wrong. | |
3254 | */ | |
3255 | if (unlikely(skb_has_shared_frag(skb))) { | |
3256 | ret = __skb_linearize(skb); | |
3257 | if (ret) | |
3258 | goto out; | |
3259 | } | |
3260 | start = skb_checksum_start_offset(skb); | |
3261 | offset = start + offsetof(struct sctphdr, checksum); | |
3262 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { | |
3263 | ret = -EINVAL; | |
3264 | goto out; | |
3265 | } | |
8211fbfa HK |
3266 | |
3267 | ret = skb_ensure_writable(skb, offset + sizeof(__le32)); | |
3268 | if (ret) | |
3269 | goto out; | |
3270 | ||
b72b5bf6 DC |
3271 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, |
3272 | skb->len - start, ~(__u32)0, | |
3273 | crc32c_csum_stub)); | |
3274 | *(__le32 *)(skb->data + offset) = crc32c_csum; | |
3275 | skb->ip_summed = CHECKSUM_NONE; | |
dba00306 | 3276 | skb->csum_not_inet = 0; |
b72b5bf6 DC |
3277 | out: |
3278 | return ret; | |
3279 | } | |
3280 | ||
53d6471c | 3281 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
f6a78bfc | 3282 | { |
252e3346 | 3283 | __be16 type = skb->protocol; |
f6a78bfc | 3284 | |
19acc327 PS |
3285 | /* Tunnel gso handlers can set protocol to ethernet. */ |
3286 | if (type == htons(ETH_P_TEB)) { | |
3287 | struct ethhdr *eth; | |
3288 | ||
3289 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) | |
3290 | return 0; | |
3291 | ||
1dfe82eb | 3292 | eth = (struct ethhdr *)skb->data; |
19acc327 PS |
3293 | type = eth->h_proto; |
3294 | } | |
3295 | ||
d4bcef3f | 3296 | return __vlan_get_protocol(skb, type, depth); |
ec5f0615 PS |
3297 | } |
3298 | ||
3299 | /** | |
3300 | * skb_mac_gso_segment - mac layer segmentation handler. | |
3301 | * @skb: buffer to segment | |
3302 | * @features: features for the output path (see dev->features) | |
3303 | */ | |
3304 | struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb, | |
3305 | netdev_features_t features) | |
3306 | { | |
3307 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); | |
3308 | struct packet_offload *ptype; | |
53d6471c VY |
3309 | int vlan_depth = skb->mac_len; |
3310 | __be16 type = skb_network_protocol(skb, &vlan_depth); | |
ec5f0615 PS |
3311 | |
3312 | if (unlikely(!type)) | |
3313 | return ERR_PTR(-EINVAL); | |
3314 | ||
53d6471c | 3315 | __skb_pull(skb, vlan_depth); |
f6a78bfc HX |
3316 | |
3317 | rcu_read_lock(); | |
22061d80 | 3318 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
f191a1d1 | 3319 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
f191a1d1 | 3320 | segs = ptype->callbacks.gso_segment(skb, features); |
f6a78bfc HX |
3321 | break; |
3322 | } | |
3323 | } | |
3324 | rcu_read_unlock(); | |
3325 | ||
98e399f8 | 3326 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
576a30eb | 3327 | |
f6a78bfc HX |
3328 | return segs; |
3329 | } | |
05e8ef4a PS |
3330 | EXPORT_SYMBOL(skb_mac_gso_segment); |
3331 | ||
3332 | ||
3333 | /* openvswitch calls this on rx path, so we need a different check. | |
3334 | */ | |
3335 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) | |
3336 | { | |
3337 | if (tx_path) | |
0c19f846 WB |
3338 | return skb->ip_summed != CHECKSUM_PARTIAL && |
3339 | skb->ip_summed != CHECKSUM_UNNECESSARY; | |
6e7bc478 ED |
3340 | |
3341 | return skb->ip_summed == CHECKSUM_NONE; | |
05e8ef4a PS |
3342 | } |
3343 | ||
3344 | /** | |
3345 | * __skb_gso_segment - Perform segmentation on skb. | |
3346 | * @skb: buffer to segment | |
3347 | * @features: features for the output path (see dev->features) | |
3348 | * @tx_path: whether it is called in TX path | |
3349 | * | |
3350 | * This function segments the given skb and returns a list of segments. | |
3351 | * | |
3352 | * It may return NULL if the skb requires no segmentation. This is | |
3353 | * only possible when GSO is used for verifying header integrity. | |
9207f9d4 | 3354 | * |
a08e7fd9 | 3355 | * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb. |
05e8ef4a PS |
3356 | */ |
3357 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, | |
3358 | netdev_features_t features, bool tx_path) | |
3359 | { | |
b2504a5d ED |
3360 | struct sk_buff *segs; |
3361 | ||
05e8ef4a PS |
3362 | if (unlikely(skb_needs_check(skb, tx_path))) { |
3363 | int err; | |
3364 | ||
b2504a5d | 3365 | /* We're going to init ->check field in TCP or UDP header */ |
a40e0a66 | 3366 | err = skb_cow_head(skb, 0); |
3367 | if (err < 0) | |
05e8ef4a PS |
3368 | return ERR_PTR(err); |
3369 | } | |
3370 | ||
802ab55a AD |
3371 | /* Only report GSO partial support if it will enable us to |
3372 | * support segmentation on this frame without needing additional | |
3373 | * work. | |
3374 | */ | |
3375 | if (features & NETIF_F_GSO_PARTIAL) { | |
3376 | netdev_features_t partial_features = NETIF_F_GSO_ROBUST; | |
3377 | struct net_device *dev = skb->dev; | |
3378 | ||
3379 | partial_features |= dev->features & dev->gso_partial_features; | |
3380 | if (!skb_gso_ok(skb, features | partial_features)) | |
3381 | features &= ~NETIF_F_GSO_PARTIAL; | |
3382 | } | |
3383 | ||
a08e7fd9 | 3384 | BUILD_BUG_ON(SKB_GSO_CB_OFFSET + |
9207f9d4 KK |
3385 | sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb)); |
3386 | ||
68c33163 | 3387 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
3347c960 ED |
3388 | SKB_GSO_CB(skb)->encap_level = 0; |
3389 | ||
05e8ef4a PS |
3390 | skb_reset_mac_header(skb); |
3391 | skb_reset_mac_len(skb); | |
3392 | ||
b2504a5d ED |
3393 | segs = skb_mac_gso_segment(skb, features); |
3394 | ||
3a1296a3 | 3395 | if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs))) |
b2504a5d ED |
3396 | skb_warn_bad_offload(skb); |
3397 | ||
3398 | return segs; | |
05e8ef4a | 3399 | } |
12b0004d | 3400 | EXPORT_SYMBOL(__skb_gso_segment); |
f6a78bfc | 3401 | |
fb286bb2 HX |
3402 | /* Take action when hardware reception checksum errors are detected. */ |
3403 | #ifdef CONFIG_BUG | |
7fe50ac8 | 3404 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
fb286bb2 HX |
3405 | { |
3406 | if (net_ratelimit()) { | |
7b6cd1ce | 3407 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
6413139d | 3408 | skb_dump(KERN_ERR, skb, true); |
fb286bb2 HX |
3409 | dump_stack(); |
3410 | } | |
3411 | } | |
3412 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
3413 | #endif | |
3414 | ||
ab74cfeb | 3415 | /* XXX: check that highmem exists at all on the given machine. */ |
c1e756bf | 3416 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 3417 | { |
3d3a8533 | 3418 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 3419 | int i; |
f4563a75 | 3420 | |
5acbbd42 | 3421 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
3422 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
3423 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
f4563a75 | 3424 | |
ea2ab693 | 3425 | if (PageHighMem(skb_frag_page(frag))) |
5acbbd42 | 3426 | return 1; |
ea2ab693 | 3427 | } |
5acbbd42 | 3428 | } |
3d3a8533 | 3429 | #endif |
1da177e4 LT |
3430 | return 0; |
3431 | } | |
1da177e4 | 3432 | |
3b392ddb SH |
3433 | /* If MPLS offload request, verify we are testing hardware MPLS features |
3434 | * instead of standard features for the netdev. | |
3435 | */ | |
d0edc7bf | 3436 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
3b392ddb SH |
3437 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
3438 | netdev_features_t features, | |
3439 | __be16 type) | |
3440 | { | |
25cd9ba0 | 3441 | if (eth_p_mpls(type)) |
3b392ddb SH |
3442 | features &= skb->dev->mpls_features; |
3443 | ||
3444 | return features; | |
3445 | } | |
3446 | #else | |
3447 | static netdev_features_t net_mpls_features(struct sk_buff *skb, | |
3448 | netdev_features_t features, | |
3449 | __be16 type) | |
3450 | { | |
3451 | return features; | |
3452 | } | |
3453 | #endif | |
3454 | ||
c8f44aff | 3455 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
c1e756bf | 3456 | netdev_features_t features) |
f01a5236 | 3457 | { |
3b392ddb SH |
3458 | __be16 type; |
3459 | ||
9fc95f50 | 3460 | type = skb_network_protocol(skb, NULL); |
3b392ddb | 3461 | features = net_mpls_features(skb, features, type); |
53d6471c | 3462 | |
c0d680e5 | 3463 | if (skb->ip_summed != CHECKSUM_NONE && |
3b392ddb | 3464 | !can_checksum_protocol(features, type)) { |
996e8021 | 3465 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
f01a5236 | 3466 | } |
7be2c82c ED |
3467 | if (illegal_highdma(skb->dev, skb)) |
3468 | features &= ~NETIF_F_SG; | |
f01a5236 JG |
3469 | |
3470 | return features; | |
3471 | } | |
3472 | ||
e38f3025 TM |
3473 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
3474 | struct net_device *dev, | |
3475 | netdev_features_t features) | |
3476 | { | |
3477 | return features; | |
3478 | } | |
3479 | EXPORT_SYMBOL(passthru_features_check); | |
3480 | ||
7ce23672 | 3481 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
8cb65d00 TM |
3482 | struct net_device *dev, |
3483 | netdev_features_t features) | |
3484 | { | |
3485 | return vlan_features_check(skb, features); | |
3486 | } | |
3487 | ||
cbc53e08 AD |
3488 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
3489 | struct net_device *dev, | |
3490 | netdev_features_t features) | |
3491 | { | |
3492 | u16 gso_segs = skb_shinfo(skb)->gso_segs; | |
3493 | ||
3494 | if (gso_segs > dev->gso_max_segs) | |
3495 | return features & ~NETIF_F_GSO_MASK; | |
3496 | ||
802ab55a AD |
3497 | /* Support for GSO partial features requires software |
3498 | * intervention before we can actually process the packets | |
3499 | * so we need to strip support for any partial features now | |
3500 | * and we can pull them back in after we have partially | |
3501 | * segmented the frame. | |
3502 | */ | |
3503 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) | |
3504 | features &= ~dev->gso_partial_features; | |
3505 | ||
3506 | /* Make sure to clear the IPv4 ID mangling feature if the | |
3507 | * IPv4 header has the potential to be fragmented. | |
cbc53e08 AD |
3508 | */ |
3509 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
3510 | struct iphdr *iph = skb->encapsulation ? | |
3511 | inner_ip_hdr(skb) : ip_hdr(skb); | |
3512 | ||
3513 | if (!(iph->frag_off & htons(IP_DF))) | |
3514 | features &= ~NETIF_F_TSO_MANGLEID; | |
3515 | } | |
3516 | ||
3517 | return features; | |
3518 | } | |
3519 | ||
c1e756bf | 3520 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 | 3521 | { |
5f35227e | 3522 | struct net_device *dev = skb->dev; |
fcbeb976 | 3523 | netdev_features_t features = dev->features; |
58e998c6 | 3524 | |
cbc53e08 AD |
3525 | if (skb_is_gso(skb)) |
3526 | features = gso_features_check(skb, dev, features); | |
30b678d8 | 3527 | |
5f35227e JG |
3528 | /* If encapsulation offload request, verify we are testing |
3529 | * hardware encapsulation features instead of standard | |
3530 | * features for the netdev | |
3531 | */ | |
3532 | if (skb->encapsulation) | |
3533 | features &= dev->hw_enc_features; | |
3534 | ||
f5a7fb88 TM |
3535 | if (skb_vlan_tagged(skb)) |
3536 | features = netdev_intersect_features(features, | |
3537 | dev->vlan_features | | |
3538 | NETIF_F_HW_VLAN_CTAG_TX | | |
3539 | NETIF_F_HW_VLAN_STAG_TX); | |
f01a5236 | 3540 | |
5f35227e JG |
3541 | if (dev->netdev_ops->ndo_features_check) |
3542 | features &= dev->netdev_ops->ndo_features_check(skb, dev, | |
3543 | features); | |
8cb65d00 TM |
3544 | else |
3545 | features &= dflt_features_check(skb, dev, features); | |
5f35227e | 3546 | |
c1e756bf | 3547 | return harmonize_features(skb, features); |
58e998c6 | 3548 | } |
c1e756bf | 3549 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 3550 | |
2ea25513 | 3551 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
95f6b3dd | 3552 | struct netdev_queue *txq, bool more) |
f6a78bfc | 3553 | { |
2ea25513 DM |
3554 | unsigned int len; |
3555 | int rc; | |
00829823 | 3556 | |
9f9a742d | 3557 | if (dev_nit_active(dev)) |
2ea25513 | 3558 | dev_queue_xmit_nit(skb, dev); |
fc741216 | 3559 | |
2ea25513 DM |
3560 | len = skb->len; |
3561 | trace_net_dev_start_xmit(skb, dev); | |
95f6b3dd | 3562 | rc = netdev_start_xmit(skb, dev, txq, more); |
2ea25513 | 3563 | trace_net_dev_xmit(skb, rc, dev, len); |
adf30907 | 3564 | |
2ea25513 DM |
3565 | return rc; |
3566 | } | |
7b9c6090 | 3567 | |
8dcda22a DM |
3568 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
3569 | struct netdev_queue *txq, int *ret) | |
7f2e870f DM |
3570 | { |
3571 | struct sk_buff *skb = first; | |
3572 | int rc = NETDEV_TX_OK; | |
7b9c6090 | 3573 | |
7f2e870f DM |
3574 | while (skb) { |
3575 | struct sk_buff *next = skb->next; | |
fc70fb64 | 3576 | |
a8305bff | 3577 | skb_mark_not_on_list(skb); |
95f6b3dd | 3578 | rc = xmit_one(skb, dev, txq, next != NULL); |
7f2e870f DM |
3579 | if (unlikely(!dev_xmit_complete(rc))) { |
3580 | skb->next = next; | |
3581 | goto out; | |
3582 | } | |
6afff0ca | 3583 | |
7f2e870f | 3584 | skb = next; |
fe60faa5 | 3585 | if (netif_tx_queue_stopped(txq) && skb) { |
7f2e870f DM |
3586 | rc = NETDEV_TX_BUSY; |
3587 | break; | |
9ccb8975 | 3588 | } |
7f2e870f | 3589 | } |
9ccb8975 | 3590 | |
7f2e870f DM |
3591 | out: |
3592 | *ret = rc; | |
3593 | return skb; | |
3594 | } | |
b40863c6 | 3595 | |
1ff0dc94 ED |
3596 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
3597 | netdev_features_t features) | |
f6a78bfc | 3598 | { |
df8a39de | 3599 | if (skb_vlan_tag_present(skb) && |
5968250c JP |
3600 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
3601 | skb = __vlan_hwaccel_push_inside(skb); | |
eae3f88e DM |
3602 | return skb; |
3603 | } | |
f6a78bfc | 3604 | |
43c26a1a DC |
3605 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
3606 | const netdev_features_t features) | |
3607 | { | |
3608 | if (unlikely(skb->csum_not_inet)) | |
3609 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : | |
3610 | skb_crc32c_csum_help(skb); | |
3611 | ||
3612 | return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb); | |
3613 | } | |
3614 | EXPORT_SYMBOL(skb_csum_hwoffload_help); | |
3615 | ||
f53c7239 | 3616 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
eae3f88e DM |
3617 | { |
3618 | netdev_features_t features; | |
f6a78bfc | 3619 | |
eae3f88e DM |
3620 | features = netif_skb_features(skb); |
3621 | skb = validate_xmit_vlan(skb, features); | |
3622 | if (unlikely(!skb)) | |
3623 | goto out_null; | |
7b9c6090 | 3624 | |
ebf4e808 IL |
3625 | skb = sk_validate_xmit_skb(skb, dev); |
3626 | if (unlikely(!skb)) | |
3627 | goto out_null; | |
3628 | ||
8b86a61d | 3629 | if (netif_needs_gso(skb, features)) { |
ce93718f DM |
3630 | struct sk_buff *segs; |
3631 | ||
3632 | segs = skb_gso_segment(skb, features); | |
cecda693 | 3633 | if (IS_ERR(segs)) { |
af6dabc9 | 3634 | goto out_kfree_skb; |
cecda693 JW |
3635 | } else if (segs) { |
3636 | consume_skb(skb); | |
3637 | skb = segs; | |
f6a78bfc | 3638 | } |
eae3f88e DM |
3639 | } else { |
3640 | if (skb_needs_linearize(skb, features) && | |
3641 | __skb_linearize(skb)) | |
3642 | goto out_kfree_skb; | |
4ec93edb | 3643 | |
eae3f88e DM |
3644 | /* If packet is not checksummed and device does not |
3645 | * support checksumming for this protocol, complete | |
3646 | * checksumming here. | |
3647 | */ | |
3648 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
3649 | if (skb->encapsulation) | |
3650 | skb_set_inner_transport_header(skb, | |
3651 | skb_checksum_start_offset(skb)); | |
3652 | else | |
3653 | skb_set_transport_header(skb, | |
3654 | skb_checksum_start_offset(skb)); | |
43c26a1a | 3655 | if (skb_csum_hwoffload_help(skb, features)) |
eae3f88e | 3656 | goto out_kfree_skb; |
7b9c6090 | 3657 | } |
0c772159 | 3658 | } |
7b9c6090 | 3659 | |
f53c7239 | 3660 | skb = validate_xmit_xfrm(skb, features, again); |
3dca3f38 | 3661 | |
eae3f88e | 3662 | return skb; |
fc70fb64 | 3663 | |
f6a78bfc HX |
3664 | out_kfree_skb: |
3665 | kfree_skb(skb); | |
eae3f88e | 3666 | out_null: |
d21fd63e | 3667 | atomic_long_inc(&dev->tx_dropped); |
eae3f88e DM |
3668 | return NULL; |
3669 | } | |
6afff0ca | 3670 | |
f53c7239 | 3671 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
55a93b3e ED |
3672 | { |
3673 | struct sk_buff *next, *head = NULL, *tail; | |
3674 | ||
bec3cfdc | 3675 | for (; skb != NULL; skb = next) { |
55a93b3e | 3676 | next = skb->next; |
a8305bff | 3677 | skb_mark_not_on_list(skb); |
bec3cfdc ED |
3678 | |
3679 | /* in case skb wont be segmented, point to itself */ | |
3680 | skb->prev = skb; | |
3681 | ||
f53c7239 | 3682 | skb = validate_xmit_skb(skb, dev, again); |
bec3cfdc ED |
3683 | if (!skb) |
3684 | continue; | |
55a93b3e | 3685 | |
bec3cfdc ED |
3686 | if (!head) |
3687 | head = skb; | |
3688 | else | |
3689 | tail->next = skb; | |
3690 | /* If skb was segmented, skb->prev points to | |
3691 | * the last segment. If not, it still contains skb. | |
3692 | */ | |
3693 | tail = skb->prev; | |
55a93b3e ED |
3694 | } |
3695 | return head; | |
f6a78bfc | 3696 | } |
104ba78c | 3697 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
f6a78bfc | 3698 | |
1def9238 ED |
3699 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
3700 | { | |
3701 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
3702 | ||
3703 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
3704 | ||
3705 | /* To get more precise estimation of bytes sent on wire, | |
3706 | * we add to pkt_len the headers size of all segments | |
3707 | */ | |
a0dce875 | 3708 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
757b8b1d | 3709 | unsigned int hdr_len; |
15e5a030 | 3710 | u16 gso_segs = shinfo->gso_segs; |
1def9238 | 3711 | |
757b8b1d ED |
3712 | /* mac layer + network layer */ |
3713 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); | |
3714 | ||
3715 | /* + transport layer */ | |
7c68d1a6 ED |
3716 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
3717 | const struct tcphdr *th; | |
3718 | struct tcphdr _tcphdr; | |
3719 | ||
3720 | th = skb_header_pointer(skb, skb_transport_offset(skb), | |
3721 | sizeof(_tcphdr), &_tcphdr); | |
3722 | if (likely(th)) | |
3723 | hdr_len += __tcp_hdrlen(th); | |
3724 | } else { | |
3725 | struct udphdr _udphdr; | |
3726 | ||
3727 | if (skb_header_pointer(skb, skb_transport_offset(skb), | |
3728 | sizeof(_udphdr), &_udphdr)) | |
3729 | hdr_len += sizeof(struct udphdr); | |
3730 | } | |
15e5a030 JW |
3731 | |
3732 | if (shinfo->gso_type & SKB_GSO_DODGY) | |
3733 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, | |
3734 | shinfo->gso_size); | |
3735 | ||
3736 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; | |
1def9238 ED |
3737 | } |
3738 | } | |
3739 | ||
bbd8a0d3 KK |
3740 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
3741 | struct net_device *dev, | |
3742 | struct netdev_queue *txq) | |
3743 | { | |
3744 | spinlock_t *root_lock = qdisc_lock(q); | |
520ac30f | 3745 | struct sk_buff *to_free = NULL; |
a2da570d | 3746 | bool contended; |
bbd8a0d3 KK |
3747 | int rc; |
3748 | ||
a2da570d | 3749 | qdisc_calculate_pkt_len(skb, q); |
6b3ba914 JF |
3750 | |
3751 | if (q->flags & TCQ_F_NOLOCK) { | |
ac5c66f2 | 3752 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
379349e9 | 3753 | qdisc_run(q); |
6b3ba914 JF |
3754 | |
3755 | if (unlikely(to_free)) | |
3756 | kfree_skb_list(to_free); | |
3757 | return rc; | |
3758 | } | |
3759 | ||
79640a4c ED |
3760 | /* |
3761 | * Heuristic to force contended enqueues to serialize on a | |
3762 | * separate lock before trying to get qdisc main lock. | |
f9eb8aea | 3763 | * This permits qdisc->running owner to get the lock more |
9bf2b8c2 | 3764 | * often and dequeue packets faster. |
79640a4c | 3765 | */ |
a2da570d | 3766 | contended = qdisc_is_running(q); |
79640a4c ED |
3767 | if (unlikely(contended)) |
3768 | spin_lock(&q->busylock); | |
3769 | ||
bbd8a0d3 KK |
3770 | spin_lock(root_lock); |
3771 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
520ac30f | 3772 | __qdisc_drop(skb, &to_free); |
bbd8a0d3 KK |
3773 | rc = NET_XMIT_DROP; |
3774 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 3775 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
3776 | /* |
3777 | * This is a work-conserving queue; there are no old skbs | |
3778 | * waiting to be sent out; and the qdisc is not running - | |
3779 | * xmit the skb directly. | |
3780 | */ | |
bfe0d029 | 3781 | |
bfe0d029 ED |
3782 | qdisc_bstats_update(q, skb); |
3783 | ||
55a93b3e | 3784 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
79640a4c ED |
3785 | if (unlikely(contended)) { |
3786 | spin_unlock(&q->busylock); | |
3787 | contended = false; | |
3788 | } | |
bbd8a0d3 | 3789 | __qdisc_run(q); |
6c148184 | 3790 | } |
bbd8a0d3 | 3791 | |
6c148184 | 3792 | qdisc_run_end(q); |
bbd8a0d3 KK |
3793 | rc = NET_XMIT_SUCCESS; |
3794 | } else { | |
ac5c66f2 | 3795 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
79640a4c ED |
3796 | if (qdisc_run_begin(q)) { |
3797 | if (unlikely(contended)) { | |
3798 | spin_unlock(&q->busylock); | |
3799 | contended = false; | |
3800 | } | |
3801 | __qdisc_run(q); | |
6c148184 | 3802 | qdisc_run_end(q); |
79640a4c | 3803 | } |
bbd8a0d3 KK |
3804 | } |
3805 | spin_unlock(root_lock); | |
520ac30f ED |
3806 | if (unlikely(to_free)) |
3807 | kfree_skb_list(to_free); | |
79640a4c ED |
3808 | if (unlikely(contended)) |
3809 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
3810 | return rc; |
3811 | } | |
3812 | ||
86f8515f | 3813 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
5bc1421e NH |
3814 | static void skb_update_prio(struct sk_buff *skb) |
3815 | { | |
4dcb31d4 ED |
3816 | const struct netprio_map *map; |
3817 | const struct sock *sk; | |
3818 | unsigned int prioidx; | |
5bc1421e | 3819 | |
4dcb31d4 ED |
3820 | if (skb->priority) |
3821 | return; | |
3822 | map = rcu_dereference_bh(skb->dev->priomap); | |
3823 | if (!map) | |
3824 | return; | |
3825 | sk = skb_to_full_sk(skb); | |
3826 | if (!sk) | |
3827 | return; | |
91c68ce2 | 3828 | |
4dcb31d4 ED |
3829 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
3830 | ||
3831 | if (prioidx < map->priomap_len) | |
3832 | skb->priority = map->priomap[prioidx]; | |
5bc1421e NH |
3833 | } |
3834 | #else | |
3835 | #define skb_update_prio(skb) | |
3836 | #endif | |
3837 | ||
95603e22 MM |
3838 | /** |
3839 | * dev_loopback_xmit - loop back @skb | |
0c4b51f0 EB |
3840 | * @net: network namespace this loopback is happening in |
3841 | * @sk: sk needed to be a netfilter okfn | |
95603e22 MM |
3842 | * @skb: buffer to transmit |
3843 | */ | |
0c4b51f0 | 3844 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
95603e22 MM |
3845 | { |
3846 | skb_reset_mac_header(skb); | |
3847 | __skb_pull(skb, skb_network_offset(skb)); | |
3848 | skb->pkt_type = PACKET_LOOPBACK; | |
3849 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
3850 | WARN_ON(!skb_dst(skb)); | |
3851 | skb_dst_force(skb); | |
3852 | netif_rx_ni(skb); | |
3853 | return 0; | |
3854 | } | |
3855 | EXPORT_SYMBOL(dev_loopback_xmit); | |
3856 | ||
1f211a1b DB |
3857 | #ifdef CONFIG_NET_EGRESS |
3858 | static struct sk_buff * | |
3859 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
3860 | { | |
46209401 | 3861 | struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress); |
1f211a1b DB |
3862 | struct tcf_result cl_res; |
3863 | ||
46209401 | 3864 | if (!miniq) |
1f211a1b DB |
3865 | return skb; |
3866 | ||
8dc07fdb | 3867 | /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ |
46209401 | 3868 | mini_qdisc_bstats_cpu_update(miniq, skb); |
1f211a1b | 3869 | |
46209401 | 3870 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
1f211a1b DB |
3871 | case TC_ACT_OK: |
3872 | case TC_ACT_RECLASSIFY: | |
3873 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
3874 | break; | |
3875 | case TC_ACT_SHOT: | |
46209401 | 3876 | mini_qdisc_qstats_cpu_drop(miniq); |
1f211a1b | 3877 | *ret = NET_XMIT_DROP; |
7e2c3aea DB |
3878 | kfree_skb(skb); |
3879 | return NULL; | |
1f211a1b DB |
3880 | case TC_ACT_STOLEN: |
3881 | case TC_ACT_QUEUED: | |
e25ea21f | 3882 | case TC_ACT_TRAP: |
1f211a1b | 3883 | *ret = NET_XMIT_SUCCESS; |
7e2c3aea | 3884 | consume_skb(skb); |
1f211a1b DB |
3885 | return NULL; |
3886 | case TC_ACT_REDIRECT: | |
3887 | /* No need to push/pop skb's mac_header here on egress! */ | |
3888 | skb_do_redirect(skb); | |
3889 | *ret = NET_XMIT_SUCCESS; | |
3890 | return NULL; | |
3891 | default: | |
3892 | break; | |
3893 | } | |
357b6cc5 | 3894 | |
1f211a1b DB |
3895 | return skb; |
3896 | } | |
3897 | #endif /* CONFIG_NET_EGRESS */ | |
3898 | ||
fc9bab24 AN |
3899 | #ifdef CONFIG_XPS |
3900 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, | |
3901 | struct xps_dev_maps *dev_maps, unsigned int tci) | |
3902 | { | |
3903 | struct xps_map *map; | |
3904 | int queue_index = -1; | |
3905 | ||
3906 | if (dev->num_tc) { | |
3907 | tci *= dev->num_tc; | |
3908 | tci += netdev_get_prio_tc_map(dev, skb->priority); | |
3909 | } | |
3910 | ||
3911 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
3912 | if (map) { | |
3913 | if (map->len == 1) | |
3914 | queue_index = map->queues[0]; | |
3915 | else | |
3916 | queue_index = map->queues[reciprocal_scale( | |
3917 | skb_get_hash(skb), map->len)]; | |
3918 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
3919 | queue_index = -1; | |
3920 | } | |
3921 | return queue_index; | |
3922 | } | |
3923 | #endif | |
3924 | ||
eadec877 AD |
3925 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
3926 | struct sk_buff *skb) | |
638b2a69 JP |
3927 | { |
3928 | #ifdef CONFIG_XPS | |
3929 | struct xps_dev_maps *dev_maps; | |
fc9bab24 | 3930 | struct sock *sk = skb->sk; |
638b2a69 JP |
3931 | int queue_index = -1; |
3932 | ||
04157469 AN |
3933 | if (!static_key_false(&xps_needed)) |
3934 | return -1; | |
3935 | ||
638b2a69 | 3936 | rcu_read_lock(); |
fc9bab24 AN |
3937 | if (!static_key_false(&xps_rxqs_needed)) |
3938 | goto get_cpus_map; | |
3939 | ||
eadec877 | 3940 | dev_maps = rcu_dereference(sb_dev->xps_rxqs_map); |
638b2a69 | 3941 | if (dev_maps) { |
fc9bab24 | 3942 | int tci = sk_rx_queue_get(sk); |
184c449f | 3943 | |
fc9bab24 AN |
3944 | if (tci >= 0 && tci < dev->num_rx_queues) |
3945 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3946 | tci); | |
3947 | } | |
184c449f | 3948 | |
fc9bab24 AN |
3949 | get_cpus_map: |
3950 | if (queue_index < 0) { | |
eadec877 | 3951 | dev_maps = rcu_dereference(sb_dev->xps_cpus_map); |
fc9bab24 AN |
3952 | if (dev_maps) { |
3953 | unsigned int tci = skb->sender_cpu - 1; | |
3954 | ||
3955 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3956 | tci); | |
638b2a69 JP |
3957 | } |
3958 | } | |
3959 | rcu_read_unlock(); | |
3960 | ||
3961 | return queue_index; | |
3962 | #else | |
3963 | return -1; | |
3964 | #endif | |
3965 | } | |
3966 | ||
a4ea8a3d | 3967 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 3968 | struct net_device *sb_dev) |
a4ea8a3d AD |
3969 | { |
3970 | return 0; | |
3971 | } | |
3972 | EXPORT_SYMBOL(dev_pick_tx_zero); | |
3973 | ||
3974 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, | |
a350ecce | 3975 | struct net_device *sb_dev) |
a4ea8a3d AD |
3976 | { |
3977 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; | |
3978 | } | |
3979 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); | |
3980 | ||
b71b5837 PA |
3981 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
3982 | struct net_device *sb_dev) | |
638b2a69 JP |
3983 | { |
3984 | struct sock *sk = skb->sk; | |
3985 | int queue_index = sk_tx_queue_get(sk); | |
3986 | ||
eadec877 AD |
3987 | sb_dev = sb_dev ? : dev; |
3988 | ||
638b2a69 JP |
3989 | if (queue_index < 0 || skb->ooo_okay || |
3990 | queue_index >= dev->real_num_tx_queues) { | |
eadec877 | 3991 | int new_index = get_xps_queue(dev, sb_dev, skb); |
f4563a75 | 3992 | |
638b2a69 | 3993 | if (new_index < 0) |
eadec877 | 3994 | new_index = skb_tx_hash(dev, sb_dev, skb); |
638b2a69 JP |
3995 | |
3996 | if (queue_index != new_index && sk && | |
004a5d01 | 3997 | sk_fullsock(sk) && |
638b2a69 JP |
3998 | rcu_access_pointer(sk->sk_dst_cache)) |
3999 | sk_tx_queue_set(sk, new_index); | |
4000 | ||
4001 | queue_index = new_index; | |
4002 | } | |
4003 | ||
4004 | return queue_index; | |
4005 | } | |
b71b5837 | 4006 | EXPORT_SYMBOL(netdev_pick_tx); |
638b2a69 | 4007 | |
4bd97d51 PA |
4008 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
4009 | struct sk_buff *skb, | |
4010 | struct net_device *sb_dev) | |
638b2a69 JP |
4011 | { |
4012 | int queue_index = 0; | |
4013 | ||
4014 | #ifdef CONFIG_XPS | |
52bd2d62 ED |
4015 | u32 sender_cpu = skb->sender_cpu - 1; |
4016 | ||
4017 | if (sender_cpu >= (u32)NR_CPUS) | |
638b2a69 JP |
4018 | skb->sender_cpu = raw_smp_processor_id() + 1; |
4019 | #endif | |
4020 | ||
4021 | if (dev->real_num_tx_queues != 1) { | |
4022 | const struct net_device_ops *ops = dev->netdev_ops; | |
f4563a75 | 4023 | |
638b2a69 | 4024 | if (ops->ndo_select_queue) |
a350ecce | 4025 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
638b2a69 | 4026 | else |
4bd97d51 | 4027 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
638b2a69 | 4028 | |
d584527c | 4029 | queue_index = netdev_cap_txqueue(dev, queue_index); |
638b2a69 JP |
4030 | } |
4031 | ||
4032 | skb_set_queue_mapping(skb, queue_index); | |
4033 | return netdev_get_tx_queue(dev, queue_index); | |
4034 | } | |
4035 | ||
d29f749e | 4036 | /** |
9d08dd3d | 4037 | * __dev_queue_xmit - transmit a buffer |
d29f749e | 4038 | * @skb: buffer to transmit |
eadec877 | 4039 | * @sb_dev: suboordinate device used for L2 forwarding offload |
d29f749e DJ |
4040 | * |
4041 | * Queue a buffer for transmission to a network device. The caller must | |
4042 | * have set the device and priority and built the buffer before calling | |
4043 | * this function. The function can be called from an interrupt. | |
4044 | * | |
4045 | * A negative errno code is returned on a failure. A success does not | |
4046 | * guarantee the frame will be transmitted as it may be dropped due | |
4047 | * to congestion or traffic shaping. | |
4048 | * | |
4049 | * ----------------------------------------------------------------------------------- | |
4050 | * I notice this method can also return errors from the queue disciplines, | |
4051 | * including NET_XMIT_DROP, which is a positive value. So, errors can also | |
4052 | * be positive. | |
4053 | * | |
4054 | * Regardless of the return value, the skb is consumed, so it is currently | |
4055 | * difficult to retry a send to this method. (You can bump the ref count | |
4056 | * before sending to hold a reference for retry if you are careful.) | |
4057 | * | |
4058 | * When calling this method, interrupts MUST be enabled. This is because | |
4059 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
4060 | * --BLG | |
4061 | */ | |
eadec877 | 4062 | static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
1da177e4 LT |
4063 | { |
4064 | struct net_device *dev = skb->dev; | |
dc2b4847 | 4065 | struct netdev_queue *txq; |
1da177e4 LT |
4066 | struct Qdisc *q; |
4067 | int rc = -ENOMEM; | |
f53c7239 | 4068 | bool again = false; |
1da177e4 | 4069 | |
6d1ccff6 ED |
4070 | skb_reset_mac_header(skb); |
4071 | ||
e7fd2885 WB |
4072 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
4073 | __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED); | |
4074 | ||
4ec93edb YH |
4075 | /* Disable soft irqs for various locks below. Also |
4076 | * stops preemption for RCU. | |
1da177e4 | 4077 | */ |
4ec93edb | 4078 | rcu_read_lock_bh(); |
1da177e4 | 4079 | |
5bc1421e NH |
4080 | skb_update_prio(skb); |
4081 | ||
1f211a1b DB |
4082 | qdisc_pkt_len_init(skb); |
4083 | #ifdef CONFIG_NET_CLS_ACT | |
8dc07fdb | 4084 | skb->tc_at_ingress = 0; |
357b6cc5 | 4085 | # ifdef CONFIG_NET_EGRESS |
aabf6772 | 4086 | if (static_branch_unlikely(&egress_needed_key)) { |
1f211a1b DB |
4087 | skb = sch_handle_egress(skb, &rc, dev); |
4088 | if (!skb) | |
4089 | goto out; | |
4090 | } | |
357b6cc5 | 4091 | # endif |
1f211a1b | 4092 | #endif |
02875878 ED |
4093 | /* If device/qdisc don't need skb->dst, release it right now while |
4094 | * its hot in this cpu cache. | |
4095 | */ | |
4096 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
4097 | skb_dst_drop(skb); | |
4098 | else | |
4099 | skb_dst_force(skb); | |
4100 | ||
4bd97d51 | 4101 | txq = netdev_core_pick_tx(dev, skb, sb_dev); |
a898def2 | 4102 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 4103 | |
cf66ba58 | 4104 | trace_net_dev_queue(skb); |
1da177e4 | 4105 | if (q->enqueue) { |
bbd8a0d3 | 4106 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 4107 | goto out; |
1da177e4 LT |
4108 | } |
4109 | ||
4110 | /* The device has no queue. Common case for software devices: | |
eb13da1a | 4111 | * loopback, all the sorts of tunnels... |
1da177e4 | 4112 | |
eb13da1a | 4113 | * Really, it is unlikely that netif_tx_lock protection is necessary |
4114 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
4115 | * counters.) | |
4116 | * However, it is possible, that they rely on protection | |
4117 | * made by us here. | |
1da177e4 | 4118 | |
eb13da1a | 4119 | * Check this and shot the lock. It is not prone from deadlocks. |
4120 | *Either shot noqueue qdisc, it is even simpler 8) | |
1da177e4 LT |
4121 | */ |
4122 | if (dev->flags & IFF_UP) { | |
4123 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
4124 | ||
c773e847 | 4125 | if (txq->xmit_lock_owner != cpu) { |
97cdcf37 | 4126 | if (dev_xmit_recursion()) |
745e20f1 ED |
4127 | goto recursion_alert; |
4128 | ||
f53c7239 | 4129 | skb = validate_xmit_skb(skb, dev, &again); |
1f59533f | 4130 | if (!skb) |
d21fd63e | 4131 | goto out; |
1f59533f | 4132 | |
c773e847 | 4133 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 4134 | |
73466498 | 4135 | if (!netif_xmit_stopped(txq)) { |
97cdcf37 | 4136 | dev_xmit_recursion_inc(); |
ce93718f | 4137 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
97cdcf37 | 4138 | dev_xmit_recursion_dec(); |
572a9d7b | 4139 | if (dev_xmit_complete(rc)) { |
c773e847 | 4140 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
4141 | goto out; |
4142 | } | |
4143 | } | |
c773e847 | 4144 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
4145 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
4146 | dev->name); | |
1da177e4 LT |
4147 | } else { |
4148 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
4149 | * unfortunately |
4150 | */ | |
4151 | recursion_alert: | |
e87cc472 JP |
4152 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
4153 | dev->name); | |
1da177e4 LT |
4154 | } |
4155 | } | |
4156 | ||
4157 | rc = -ENETDOWN; | |
d4828d85 | 4158 | rcu_read_unlock_bh(); |
1da177e4 | 4159 | |
015f0688 | 4160 | atomic_long_inc(&dev->tx_dropped); |
1f59533f | 4161 | kfree_skb_list(skb); |
1da177e4 LT |
4162 | return rc; |
4163 | out: | |
d4828d85 | 4164 | rcu_read_unlock_bh(); |
1da177e4 LT |
4165 | return rc; |
4166 | } | |
f663dd9a | 4167 | |
2b4aa3ce | 4168 | int dev_queue_xmit(struct sk_buff *skb) |
f663dd9a JW |
4169 | { |
4170 | return __dev_queue_xmit(skb, NULL); | |
4171 | } | |
2b4aa3ce | 4172 | EXPORT_SYMBOL(dev_queue_xmit); |
1da177e4 | 4173 | |
eadec877 | 4174 | int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev) |
f663dd9a | 4175 | { |
eadec877 | 4176 | return __dev_queue_xmit(skb, sb_dev); |
f663dd9a JW |
4177 | } |
4178 | EXPORT_SYMBOL(dev_queue_xmit_accel); | |
4179 | ||
865b03f2 MK |
4180 | int dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
4181 | { | |
4182 | struct net_device *dev = skb->dev; | |
4183 | struct sk_buff *orig_skb = skb; | |
4184 | struct netdev_queue *txq; | |
4185 | int ret = NETDEV_TX_BUSY; | |
4186 | bool again = false; | |
4187 | ||
4188 | if (unlikely(!netif_running(dev) || | |
4189 | !netif_carrier_ok(dev))) | |
4190 | goto drop; | |
4191 | ||
4192 | skb = validate_xmit_skb_list(skb, dev, &again); | |
4193 | if (skb != orig_skb) | |
4194 | goto drop; | |
4195 | ||
4196 | skb_set_queue_mapping(skb, queue_id); | |
4197 | txq = skb_get_tx_queue(dev, skb); | |
4198 | ||
4199 | local_bh_disable(); | |
4200 | ||
0ad6f6e7 | 4201 | dev_xmit_recursion_inc(); |
865b03f2 MK |
4202 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
4203 | if (!netif_xmit_frozen_or_drv_stopped(txq)) | |
4204 | ret = netdev_start_xmit(skb, dev, txq, false); | |
4205 | HARD_TX_UNLOCK(dev, txq); | |
0ad6f6e7 | 4206 | dev_xmit_recursion_dec(); |
865b03f2 MK |
4207 | |
4208 | local_bh_enable(); | |
4209 | ||
4210 | if (!dev_xmit_complete(ret)) | |
4211 | kfree_skb(skb); | |
4212 | ||
4213 | return ret; | |
4214 | drop: | |
4215 | atomic_long_inc(&dev->tx_dropped); | |
4216 | kfree_skb_list(skb); | |
4217 | return NET_XMIT_DROP; | |
4218 | } | |
4219 | EXPORT_SYMBOL(dev_direct_xmit); | |
1da177e4 | 4220 | |
eb13da1a | 4221 | /************************************************************************* |
4222 | * Receiver routines | |
4223 | *************************************************************************/ | |
1da177e4 | 4224 | |
6b2bedc3 | 4225 | int netdev_max_backlog __read_mostly = 1000; |
c9e6bc64 ED |
4226 | EXPORT_SYMBOL(netdev_max_backlog); |
4227 | ||
3b098e2d | 4228 | int netdev_tstamp_prequeue __read_mostly = 1; |
6b2bedc3 | 4229 | int netdev_budget __read_mostly = 300; |
a4837980 KK |
4230 | /* Must be at least 2 jiffes to guarantee 1 jiffy timeout */ |
4231 | unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ; | |
3d48b53f MT |
4232 | int weight_p __read_mostly = 64; /* old backlog weight */ |
4233 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ | |
4234 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ | |
4235 | int dev_rx_weight __read_mostly = 64; | |
4236 | int dev_tx_weight __read_mostly = 64; | |
323ebb61 EC |
4237 | /* Maximum number of GRO_NORMAL skbs to batch up for list-RX */ |
4238 | int gro_normal_batch __read_mostly = 8; | |
1da177e4 | 4239 | |
eecfd7c4 ED |
4240 | /* Called with irq disabled */ |
4241 | static inline void ____napi_schedule(struct softnet_data *sd, | |
4242 | struct napi_struct *napi) | |
4243 | { | |
4244 | list_add_tail(&napi->poll_list, &sd->poll_list); | |
4245 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4246 | } | |
4247 | ||
bfb564e7 KK |
4248 | #ifdef CONFIG_RPS |
4249 | ||
4250 | /* One global table that all flow-based protocols share. */ | |
6e3f7faf | 4251 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
bfb564e7 | 4252 | EXPORT_SYMBOL(rps_sock_flow_table); |
567e4b79 ED |
4253 | u32 rps_cpu_mask __read_mostly; |
4254 | EXPORT_SYMBOL(rps_cpu_mask); | |
bfb564e7 | 4255 | |
dc05360f | 4256 | struct static_key_false rps_needed __read_mostly; |
3df97ba8 | 4257 | EXPORT_SYMBOL(rps_needed); |
dc05360f | 4258 | struct static_key_false rfs_needed __read_mostly; |
13bfff25 | 4259 | EXPORT_SYMBOL(rfs_needed); |
adc9300e | 4260 | |
c445477d BH |
4261 | static struct rps_dev_flow * |
4262 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4263 | struct rps_dev_flow *rflow, u16 next_cpu) | |
4264 | { | |
a31196b0 | 4265 | if (next_cpu < nr_cpu_ids) { |
c445477d BH |
4266 | #ifdef CONFIG_RFS_ACCEL |
4267 | struct netdev_rx_queue *rxqueue; | |
4268 | struct rps_dev_flow_table *flow_table; | |
4269 | struct rps_dev_flow *old_rflow; | |
4270 | u32 flow_id; | |
4271 | u16 rxq_index; | |
4272 | int rc; | |
4273 | ||
4274 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
4275 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
4276 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
4277 | goto out; |
4278 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
4279 | if (rxq_index == skb_get_rx_queue(skb)) | |
4280 | goto out; | |
4281 | ||
4282 | rxqueue = dev->_rx + rxq_index; | |
4283 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4284 | if (!flow_table) | |
4285 | goto out; | |
61b905da | 4286 | flow_id = skb_get_hash(skb) & flow_table->mask; |
c445477d BH |
4287 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
4288 | rxq_index, flow_id); | |
4289 | if (rc < 0) | |
4290 | goto out; | |
4291 | old_rflow = rflow; | |
4292 | rflow = &flow_table->flows[flow_id]; | |
c445477d BH |
4293 | rflow->filter = rc; |
4294 | if (old_rflow->filter == rflow->filter) | |
4295 | old_rflow->filter = RPS_NO_FILTER; | |
4296 | out: | |
4297 | #endif | |
4298 | rflow->last_qtail = | |
09994d1b | 4299 | per_cpu(softnet_data, next_cpu).input_queue_head; |
c445477d BH |
4300 | } |
4301 | ||
09994d1b | 4302 | rflow->cpu = next_cpu; |
c445477d BH |
4303 | return rflow; |
4304 | } | |
4305 | ||
bfb564e7 KK |
4306 | /* |
4307 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
4308 | * CPU from the RPS map of the receiving queue for a given skb. | |
4309 | * rcu_read_lock must be held on entry. | |
4310 | */ | |
4311 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4312 | struct rps_dev_flow **rflowp) | |
4313 | { | |
567e4b79 ED |
4314 | const struct rps_sock_flow_table *sock_flow_table; |
4315 | struct netdev_rx_queue *rxqueue = dev->_rx; | |
bfb564e7 | 4316 | struct rps_dev_flow_table *flow_table; |
567e4b79 | 4317 | struct rps_map *map; |
bfb564e7 | 4318 | int cpu = -1; |
567e4b79 | 4319 | u32 tcpu; |
61b905da | 4320 | u32 hash; |
bfb564e7 KK |
4321 | |
4322 | if (skb_rx_queue_recorded(skb)) { | |
4323 | u16 index = skb_get_rx_queue(skb); | |
567e4b79 | 4324 | |
62fe0b40 BH |
4325 | if (unlikely(index >= dev->real_num_rx_queues)) { |
4326 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4327 | "%s received packet on queue %u, but number " | |
4328 | "of RX queues is %u\n", | |
4329 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
4330 | goto done; |
4331 | } | |
567e4b79 ED |
4332 | rxqueue += index; |
4333 | } | |
bfb564e7 | 4334 | |
567e4b79 ED |
4335 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
4336 | ||
4337 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
6e3f7faf | 4338 | map = rcu_dereference(rxqueue->rps_map); |
567e4b79 | 4339 | if (!flow_table && !map) |
bfb564e7 KK |
4340 | goto done; |
4341 | ||
2d47b459 | 4342 | skb_reset_network_header(skb); |
61b905da TH |
4343 | hash = skb_get_hash(skb); |
4344 | if (!hash) | |
bfb564e7 KK |
4345 | goto done; |
4346 | ||
fec5e652 TH |
4347 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
4348 | if (flow_table && sock_flow_table) { | |
fec5e652 | 4349 | struct rps_dev_flow *rflow; |
567e4b79 ED |
4350 | u32 next_cpu; |
4351 | u32 ident; | |
4352 | ||
4353 | /* First check into global flow table if there is a match */ | |
4354 | ident = sock_flow_table->ents[hash & sock_flow_table->mask]; | |
4355 | if ((ident ^ hash) & ~rps_cpu_mask) | |
4356 | goto try_rps; | |
fec5e652 | 4357 | |
567e4b79 ED |
4358 | next_cpu = ident & rps_cpu_mask; |
4359 | ||
4360 | /* OK, now we know there is a match, | |
4361 | * we can look at the local (per receive queue) flow table | |
4362 | */ | |
61b905da | 4363 | rflow = &flow_table->flows[hash & flow_table->mask]; |
fec5e652 TH |
4364 | tcpu = rflow->cpu; |
4365 | ||
fec5e652 TH |
4366 | /* |
4367 | * If the desired CPU (where last recvmsg was done) is | |
4368 | * different from current CPU (one in the rx-queue flow | |
4369 | * table entry), switch if one of the following holds: | |
a31196b0 | 4370 | * - Current CPU is unset (>= nr_cpu_ids). |
fec5e652 TH |
4371 | * - Current CPU is offline. |
4372 | * - The current CPU's queue tail has advanced beyond the | |
4373 | * last packet that was enqueued using this table entry. | |
4374 | * This guarantees that all previous packets for the flow | |
4375 | * have been dequeued, thus preserving in order delivery. | |
4376 | */ | |
4377 | if (unlikely(tcpu != next_cpu) && | |
a31196b0 | 4378 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
fec5e652 | 4379 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
baefa31d TH |
4380 | rflow->last_qtail)) >= 0)) { |
4381 | tcpu = next_cpu; | |
c445477d | 4382 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 4383 | } |
c445477d | 4384 | |
a31196b0 | 4385 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
fec5e652 TH |
4386 | *rflowp = rflow; |
4387 | cpu = tcpu; | |
4388 | goto done; | |
4389 | } | |
4390 | } | |
4391 | ||
567e4b79 ED |
4392 | try_rps: |
4393 | ||
0a9627f2 | 4394 | if (map) { |
8fc54f68 | 4395 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
0a9627f2 TH |
4396 | if (cpu_online(tcpu)) { |
4397 | cpu = tcpu; | |
4398 | goto done; | |
4399 | } | |
4400 | } | |
4401 | ||
4402 | done: | |
0a9627f2 TH |
4403 | return cpu; |
4404 | } | |
4405 | ||
c445477d BH |
4406 | #ifdef CONFIG_RFS_ACCEL |
4407 | ||
4408 | /** | |
4409 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
4410 | * @dev: Device on which the filter was set | |
4411 | * @rxq_index: RX queue index | |
4412 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
4413 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
4414 | * | |
4415 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
4416 | * this function for each installed filter and remove the filters for | |
4417 | * which it returns %true. | |
4418 | */ | |
4419 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
4420 | u32 flow_id, u16 filter_id) | |
4421 | { | |
4422 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
4423 | struct rps_dev_flow_table *flow_table; | |
4424 | struct rps_dev_flow *rflow; | |
4425 | bool expire = true; | |
a31196b0 | 4426 | unsigned int cpu; |
c445477d BH |
4427 | |
4428 | rcu_read_lock(); | |
4429 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4430 | if (flow_table && flow_id <= flow_table->mask) { | |
4431 | rflow = &flow_table->flows[flow_id]; | |
6aa7de05 | 4432 | cpu = READ_ONCE(rflow->cpu); |
a31196b0 | 4433 | if (rflow->filter == filter_id && cpu < nr_cpu_ids && |
c445477d BH |
4434 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
4435 | rflow->last_qtail) < | |
4436 | (int)(10 * flow_table->mask))) | |
4437 | expire = false; | |
4438 | } | |
4439 | rcu_read_unlock(); | |
4440 | return expire; | |
4441 | } | |
4442 | EXPORT_SYMBOL(rps_may_expire_flow); | |
4443 | ||
4444 | #endif /* CONFIG_RFS_ACCEL */ | |
4445 | ||
0a9627f2 | 4446 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 4447 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 4448 | { |
e36fa2f7 ED |
4449 | struct softnet_data *sd = data; |
4450 | ||
eecfd7c4 | 4451 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 4452 | sd->received_rps++; |
0a9627f2 | 4453 | } |
e36fa2f7 | 4454 | |
fec5e652 | 4455 | #endif /* CONFIG_RPS */ |
0a9627f2 | 4456 | |
e36fa2f7 ED |
4457 | /* |
4458 | * Check if this softnet_data structure is another cpu one | |
4459 | * If yes, queue it to our IPI list and return 1 | |
4460 | * If no, return 0 | |
4461 | */ | |
4462 | static int rps_ipi_queued(struct softnet_data *sd) | |
4463 | { | |
4464 | #ifdef CONFIG_RPS | |
903ceff7 | 4465 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
e36fa2f7 ED |
4466 | |
4467 | if (sd != mysd) { | |
4468 | sd->rps_ipi_next = mysd->rps_ipi_list; | |
4469 | mysd->rps_ipi_list = sd; | |
4470 | ||
4471 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4472 | return 1; | |
4473 | } | |
4474 | #endif /* CONFIG_RPS */ | |
4475 | return 0; | |
4476 | } | |
4477 | ||
99bbc707 WB |
4478 | #ifdef CONFIG_NET_FLOW_LIMIT |
4479 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); | |
4480 | #endif | |
4481 | ||
4482 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) | |
4483 | { | |
4484 | #ifdef CONFIG_NET_FLOW_LIMIT | |
4485 | struct sd_flow_limit *fl; | |
4486 | struct softnet_data *sd; | |
4487 | unsigned int old_flow, new_flow; | |
4488 | ||
4489 | if (qlen < (netdev_max_backlog >> 1)) | |
4490 | return false; | |
4491 | ||
903ceff7 | 4492 | sd = this_cpu_ptr(&softnet_data); |
99bbc707 WB |
4493 | |
4494 | rcu_read_lock(); | |
4495 | fl = rcu_dereference(sd->flow_limit); | |
4496 | if (fl) { | |
3958afa1 | 4497 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
99bbc707 WB |
4498 | old_flow = fl->history[fl->history_head]; |
4499 | fl->history[fl->history_head] = new_flow; | |
4500 | ||
4501 | fl->history_head++; | |
4502 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; | |
4503 | ||
4504 | if (likely(fl->buckets[old_flow])) | |
4505 | fl->buckets[old_flow]--; | |
4506 | ||
4507 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { | |
4508 | fl->count++; | |
4509 | rcu_read_unlock(); | |
4510 | return true; | |
4511 | } | |
4512 | } | |
4513 | rcu_read_unlock(); | |
4514 | #endif | |
4515 | return false; | |
4516 | } | |
4517 | ||
0a9627f2 TH |
4518 | /* |
4519 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
4520 | * queue (may be a remote CPU queue). | |
4521 | */ | |
fec5e652 TH |
4522 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
4523 | unsigned int *qtail) | |
0a9627f2 | 4524 | { |
e36fa2f7 | 4525 | struct softnet_data *sd; |
0a9627f2 | 4526 | unsigned long flags; |
99bbc707 | 4527 | unsigned int qlen; |
0a9627f2 | 4528 | |
e36fa2f7 | 4529 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 TH |
4530 | |
4531 | local_irq_save(flags); | |
0a9627f2 | 4532 | |
e36fa2f7 | 4533 | rps_lock(sd); |
e9e4dd32 JA |
4534 | if (!netif_running(skb->dev)) |
4535 | goto drop; | |
99bbc707 WB |
4536 | qlen = skb_queue_len(&sd->input_pkt_queue); |
4537 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { | |
e008f3f0 | 4538 | if (qlen) { |
0a9627f2 | 4539 | enqueue: |
e36fa2f7 | 4540 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
76cc8b13 | 4541 | input_queue_tail_incr_save(sd, qtail); |
e36fa2f7 | 4542 | rps_unlock(sd); |
152102c7 | 4543 | local_irq_restore(flags); |
0a9627f2 TH |
4544 | return NET_RX_SUCCESS; |
4545 | } | |
4546 | ||
ebda37c2 ED |
4547 | /* Schedule NAPI for backlog device |
4548 | * We can use non atomic operation since we own the queue lock | |
4549 | */ | |
4550 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { | |
e36fa2f7 | 4551 | if (!rps_ipi_queued(sd)) |
eecfd7c4 | 4552 | ____napi_schedule(sd, &sd->backlog); |
0a9627f2 TH |
4553 | } |
4554 | goto enqueue; | |
4555 | } | |
4556 | ||
e9e4dd32 | 4557 | drop: |
dee42870 | 4558 | sd->dropped++; |
e36fa2f7 | 4559 | rps_unlock(sd); |
0a9627f2 | 4560 | |
0a9627f2 TH |
4561 | local_irq_restore(flags); |
4562 | ||
caf586e5 | 4563 | atomic_long_inc(&skb->dev->rx_dropped); |
0a9627f2 TH |
4564 | kfree_skb(skb); |
4565 | return NET_RX_DROP; | |
4566 | } | |
1da177e4 | 4567 | |
e817f856 JDB |
4568 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
4569 | { | |
4570 | struct net_device *dev = skb->dev; | |
4571 | struct netdev_rx_queue *rxqueue; | |
4572 | ||
4573 | rxqueue = dev->_rx; | |
4574 | ||
4575 | if (skb_rx_queue_recorded(skb)) { | |
4576 | u16 index = skb_get_rx_queue(skb); | |
4577 | ||
4578 | if (unlikely(index >= dev->real_num_rx_queues)) { | |
4579 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4580 | "%s received packet on queue %u, but number " | |
4581 | "of RX queues is %u\n", | |
4582 | dev->name, index, dev->real_num_rx_queues); | |
4583 | ||
4584 | return rxqueue; /* Return first rxqueue */ | |
4585 | } | |
4586 | rxqueue += index; | |
4587 | } | |
4588 | return rxqueue; | |
4589 | } | |
4590 | ||
d4455169 | 4591 | static u32 netif_receive_generic_xdp(struct sk_buff *skb, |
02671e23 | 4592 | struct xdp_buff *xdp, |
d4455169 JF |
4593 | struct bpf_prog *xdp_prog) |
4594 | { | |
e817f856 | 4595 | struct netdev_rx_queue *rxqueue; |
198d83bb | 4596 | void *orig_data, *orig_data_end; |
de8f3a83 | 4597 | u32 metalen, act = XDP_DROP; |
29724956 JDB |
4598 | __be16 orig_eth_type; |
4599 | struct ethhdr *eth; | |
4600 | bool orig_bcast; | |
d4455169 JF |
4601 | int hlen, off; |
4602 | u32 mac_len; | |
4603 | ||
4604 | /* Reinjected packets coming from act_mirred or similar should | |
4605 | * not get XDP generic processing. | |
4606 | */ | |
2c64605b | 4607 | if (skb_is_redirected(skb)) |
d4455169 JF |
4608 | return XDP_PASS; |
4609 | ||
de8f3a83 DB |
4610 | /* XDP packets must be linear and must have sufficient headroom |
4611 | * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also | |
4612 | * native XDP provides, thus we need to do it here as well. | |
4613 | */ | |
ad1e03b2 | 4614 | if (skb_cloned(skb) || skb_is_nonlinear(skb) || |
de8f3a83 DB |
4615 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { |
4616 | int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); | |
4617 | int troom = skb->tail + skb->data_len - skb->end; | |
4618 | ||
4619 | /* In case we have to go down the path and also linearize, | |
4620 | * then lets do the pskb_expand_head() work just once here. | |
4621 | */ | |
4622 | if (pskb_expand_head(skb, | |
4623 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, | |
4624 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC)) | |
4625 | goto do_drop; | |
2d17d8d7 | 4626 | if (skb_linearize(skb)) |
de8f3a83 DB |
4627 | goto do_drop; |
4628 | } | |
d4455169 JF |
4629 | |
4630 | /* The XDP program wants to see the packet starting at the MAC | |
4631 | * header. | |
4632 | */ | |
4633 | mac_len = skb->data - skb_mac_header(skb); | |
4634 | hlen = skb_headlen(skb) + mac_len; | |
02671e23 BT |
4635 | xdp->data = skb->data - mac_len; |
4636 | xdp->data_meta = xdp->data; | |
4637 | xdp->data_end = xdp->data + hlen; | |
4638 | xdp->data_hard_start = skb->data - skb_headroom(skb); | |
a075767b JDB |
4639 | |
4640 | /* SKB "head" area always have tailroom for skb_shared_info */ | |
4641 | xdp->frame_sz = (void *)skb_end_pointer(skb) - xdp->data_hard_start; | |
4642 | xdp->frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
4643 | ||
02671e23 BT |
4644 | orig_data_end = xdp->data_end; |
4645 | orig_data = xdp->data; | |
29724956 JDB |
4646 | eth = (struct ethhdr *)xdp->data; |
4647 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); | |
4648 | orig_eth_type = eth->h_proto; | |
d4455169 | 4649 | |
e817f856 | 4650 | rxqueue = netif_get_rxqueue(skb); |
02671e23 | 4651 | xdp->rxq = &rxqueue->xdp_rxq; |
e817f856 | 4652 | |
02671e23 | 4653 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
d4455169 | 4654 | |
065af355 | 4655 | /* check if bpf_xdp_adjust_head was used */ |
02671e23 | 4656 | off = xdp->data - orig_data; |
065af355 JDB |
4657 | if (off) { |
4658 | if (off > 0) | |
4659 | __skb_pull(skb, off); | |
4660 | else if (off < 0) | |
4661 | __skb_push(skb, -off); | |
4662 | ||
4663 | skb->mac_header += off; | |
4664 | skb_reset_network_header(skb); | |
4665 | } | |
d4455169 | 4666 | |
a075767b JDB |
4667 | /* check if bpf_xdp_adjust_tail was used */ |
4668 | off = xdp->data_end - orig_data_end; | |
f7613120 | 4669 | if (off != 0) { |
02671e23 | 4670 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
a075767b | 4671 | skb->len += off; /* positive on grow, negative on shrink */ |
f7613120 | 4672 | } |
198d83bb | 4673 | |
29724956 JDB |
4674 | /* check if XDP changed eth hdr such SKB needs update */ |
4675 | eth = (struct ethhdr *)xdp->data; | |
4676 | if ((orig_eth_type != eth->h_proto) || | |
4677 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { | |
4678 | __skb_push(skb, ETH_HLEN); | |
4679 | skb->protocol = eth_type_trans(skb, skb->dev); | |
4680 | } | |
4681 | ||
d4455169 | 4682 | switch (act) { |
6103aa96 | 4683 | case XDP_REDIRECT: |
d4455169 JF |
4684 | case XDP_TX: |
4685 | __skb_push(skb, mac_len); | |
de8f3a83 | 4686 | break; |
d4455169 | 4687 | case XDP_PASS: |
02671e23 | 4688 | metalen = xdp->data - xdp->data_meta; |
de8f3a83 DB |
4689 | if (metalen) |
4690 | skb_metadata_set(skb, metalen); | |
d4455169 | 4691 | break; |
d4455169 JF |
4692 | default: |
4693 | bpf_warn_invalid_xdp_action(act); | |
df561f66 | 4694 | fallthrough; |
d4455169 JF |
4695 | case XDP_ABORTED: |
4696 | trace_xdp_exception(skb->dev, xdp_prog, act); | |
df561f66 | 4697 | fallthrough; |
d4455169 JF |
4698 | case XDP_DROP: |
4699 | do_drop: | |
4700 | kfree_skb(skb); | |
4701 | break; | |
4702 | } | |
4703 | ||
4704 | return act; | |
4705 | } | |
4706 | ||
4707 | /* When doing generic XDP we have to bypass the qdisc layer and the | |
4708 | * network taps in order to match in-driver-XDP behavior. | |
4709 | */ | |
7c497478 | 4710 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
d4455169 JF |
4711 | { |
4712 | struct net_device *dev = skb->dev; | |
4713 | struct netdev_queue *txq; | |
4714 | bool free_skb = true; | |
4715 | int cpu, rc; | |
4716 | ||
4bd97d51 | 4717 | txq = netdev_core_pick_tx(dev, skb, NULL); |
d4455169 JF |
4718 | cpu = smp_processor_id(); |
4719 | HARD_TX_LOCK(dev, txq, cpu); | |
4720 | if (!netif_xmit_stopped(txq)) { | |
4721 | rc = netdev_start_xmit(skb, dev, txq, 0); | |
4722 | if (dev_xmit_complete(rc)) | |
4723 | free_skb = false; | |
4724 | } | |
4725 | HARD_TX_UNLOCK(dev, txq); | |
4726 | if (free_skb) { | |
4727 | trace_xdp_exception(dev, xdp_prog, XDP_TX); | |
4728 | kfree_skb(skb); | |
4729 | } | |
4730 | } | |
4731 | ||
02786475 | 4732 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
d4455169 | 4733 | |
7c497478 | 4734 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb) |
d4455169 | 4735 | { |
d4455169 | 4736 | if (xdp_prog) { |
02671e23 BT |
4737 | struct xdp_buff xdp; |
4738 | u32 act; | |
6103aa96 | 4739 | int err; |
d4455169 | 4740 | |
02671e23 | 4741 | act = netif_receive_generic_xdp(skb, &xdp, xdp_prog); |
d4455169 | 4742 | if (act != XDP_PASS) { |
6103aa96 JF |
4743 | switch (act) { |
4744 | case XDP_REDIRECT: | |
2facaad6 | 4745 | err = xdp_do_generic_redirect(skb->dev, skb, |
02671e23 | 4746 | &xdp, xdp_prog); |
6103aa96 JF |
4747 | if (err) |
4748 | goto out_redir; | |
02671e23 | 4749 | break; |
6103aa96 | 4750 | case XDP_TX: |
d4455169 | 4751 | generic_xdp_tx(skb, xdp_prog); |
6103aa96 JF |
4752 | break; |
4753 | } | |
d4455169 JF |
4754 | return XDP_DROP; |
4755 | } | |
4756 | } | |
4757 | return XDP_PASS; | |
6103aa96 | 4758 | out_redir: |
6103aa96 JF |
4759 | kfree_skb(skb); |
4760 | return XDP_DROP; | |
d4455169 | 4761 | } |
7c497478 | 4762 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
d4455169 | 4763 | |
ae78dbfa | 4764 | static int netif_rx_internal(struct sk_buff *skb) |
1da177e4 | 4765 | { |
b0e28f1e | 4766 | int ret; |
1da177e4 | 4767 | |
588f0330 | 4768 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
1da177e4 | 4769 | |
cf66ba58 | 4770 | trace_netif_rx(skb); |
d4455169 | 4771 | |
df334545 | 4772 | #ifdef CONFIG_RPS |
dc05360f | 4773 | if (static_branch_unlikely(&rps_needed)) { |
fec5e652 | 4774 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
4775 | int cpu; |
4776 | ||
cece1945 | 4777 | preempt_disable(); |
b0e28f1e | 4778 | rcu_read_lock(); |
fec5e652 TH |
4779 | |
4780 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
4781 | if (cpu < 0) |
4782 | cpu = smp_processor_id(); | |
fec5e652 TH |
4783 | |
4784 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
4785 | ||
b0e28f1e | 4786 | rcu_read_unlock(); |
cece1945 | 4787 | preempt_enable(); |
adc9300e ED |
4788 | } else |
4789 | #endif | |
fec5e652 TH |
4790 | { |
4791 | unsigned int qtail; | |
f4563a75 | 4792 | |
fec5e652 TH |
4793 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); |
4794 | put_cpu(); | |
4795 | } | |
b0e28f1e | 4796 | return ret; |
1da177e4 | 4797 | } |
ae78dbfa BH |
4798 | |
4799 | /** | |
4800 | * netif_rx - post buffer to the network code | |
4801 | * @skb: buffer to post | |
4802 | * | |
4803 | * This function receives a packet from a device driver and queues it for | |
4804 | * the upper (protocol) levels to process. It always succeeds. The buffer | |
4805 | * may be dropped during processing for congestion control or by the | |
4806 | * protocol layers. | |
4807 | * | |
4808 | * return values: | |
4809 | * NET_RX_SUCCESS (no congestion) | |
4810 | * NET_RX_DROP (packet was dropped) | |
4811 | * | |
4812 | */ | |
4813 | ||
4814 | int netif_rx(struct sk_buff *skb) | |
4815 | { | |
b0e3f1bd GB |
4816 | int ret; |
4817 | ||
ae78dbfa BH |
4818 | trace_netif_rx_entry(skb); |
4819 | ||
b0e3f1bd GB |
4820 | ret = netif_rx_internal(skb); |
4821 | trace_netif_rx_exit(ret); | |
4822 | ||
4823 | return ret; | |
ae78dbfa | 4824 | } |
d1b19dff | 4825 | EXPORT_SYMBOL(netif_rx); |
1da177e4 LT |
4826 | |
4827 | int netif_rx_ni(struct sk_buff *skb) | |
4828 | { | |
4829 | int err; | |
4830 | ||
ae78dbfa BH |
4831 | trace_netif_rx_ni_entry(skb); |
4832 | ||
1da177e4 | 4833 | preempt_disable(); |
ae78dbfa | 4834 | err = netif_rx_internal(skb); |
1da177e4 LT |
4835 | if (local_softirq_pending()) |
4836 | do_softirq(); | |
4837 | preempt_enable(); | |
b0e3f1bd | 4838 | trace_netif_rx_ni_exit(err); |
1da177e4 LT |
4839 | |
4840 | return err; | |
4841 | } | |
1da177e4 LT |
4842 | EXPORT_SYMBOL(netif_rx_ni); |
4843 | ||
0766f788 | 4844 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
1da177e4 | 4845 | { |
903ceff7 | 4846 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
1da177e4 LT |
4847 | |
4848 | if (sd->completion_queue) { | |
4849 | struct sk_buff *clist; | |
4850 | ||
4851 | local_irq_disable(); | |
4852 | clist = sd->completion_queue; | |
4853 | sd->completion_queue = NULL; | |
4854 | local_irq_enable(); | |
4855 | ||
4856 | while (clist) { | |
4857 | struct sk_buff *skb = clist; | |
f4563a75 | 4858 | |
1da177e4 LT |
4859 | clist = clist->next; |
4860 | ||
63354797 | 4861 | WARN_ON(refcount_read(&skb->users)); |
e6247027 ED |
4862 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED)) |
4863 | trace_consume_skb(skb); | |
4864 | else | |
4865 | trace_kfree_skb(skb, net_tx_action); | |
15fad714 JDB |
4866 | |
4867 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) | |
4868 | __kfree_skb(skb); | |
4869 | else | |
4870 | __kfree_skb_defer(skb); | |
1da177e4 | 4871 | } |
15fad714 JDB |
4872 | |
4873 | __kfree_skb_flush(); | |
1da177e4 LT |
4874 | } |
4875 | ||
4876 | if (sd->output_queue) { | |
37437bb2 | 4877 | struct Qdisc *head; |
1da177e4 LT |
4878 | |
4879 | local_irq_disable(); | |
4880 | head = sd->output_queue; | |
4881 | sd->output_queue = NULL; | |
a9cbd588 | 4882 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
4883 | local_irq_enable(); |
4884 | ||
4885 | while (head) { | |
37437bb2 | 4886 | struct Qdisc *q = head; |
6b3ba914 | 4887 | spinlock_t *root_lock = NULL; |
37437bb2 | 4888 | |
1da177e4 LT |
4889 | head = head->next_sched; |
4890 | ||
6b3ba914 JF |
4891 | if (!(q->flags & TCQ_F_NOLOCK)) { |
4892 | root_lock = qdisc_lock(q); | |
4893 | spin_lock(root_lock); | |
4894 | } | |
3bcb846c ED |
4895 | /* We need to make sure head->next_sched is read |
4896 | * before clearing __QDISC_STATE_SCHED | |
4897 | */ | |
4898 | smp_mb__before_atomic(); | |
4899 | clear_bit(__QDISC_STATE_SCHED, &q->state); | |
4900 | qdisc_run(q); | |
6b3ba914 JF |
4901 | if (root_lock) |
4902 | spin_unlock(root_lock); | |
1da177e4 LT |
4903 | } |
4904 | } | |
f53c7239 SK |
4905 | |
4906 | xfrm_dev_backlog(sd); | |
1da177e4 LT |
4907 | } |
4908 | ||
181402a5 | 4909 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
4910 | /* This hook is defined here for ATM LANE */ |
4911 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
4912 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 4913 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 4914 | #endif |
1da177e4 | 4915 | |
1f211a1b DB |
4916 | static inline struct sk_buff * |
4917 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
4918 | struct net_device *orig_dev) | |
f697c3e8 | 4919 | { |
e7582bab | 4920 | #ifdef CONFIG_NET_CLS_ACT |
46209401 | 4921 | struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress); |
d2788d34 | 4922 | struct tcf_result cl_res; |
24824a09 | 4923 | |
c9e99fd0 DB |
4924 | /* If there's at least one ingress present somewhere (so |
4925 | * we get here via enabled static key), remaining devices | |
4926 | * that are not configured with an ingress qdisc will bail | |
d2788d34 | 4927 | * out here. |
c9e99fd0 | 4928 | */ |
46209401 | 4929 | if (!miniq) |
4577139b | 4930 | return skb; |
46209401 | 4931 | |
f697c3e8 HX |
4932 | if (*pt_prev) { |
4933 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4934 | *pt_prev = NULL; | |
1da177e4 LT |
4935 | } |
4936 | ||
3365495c | 4937 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
8dc07fdb | 4938 | skb->tc_at_ingress = 1; |
46209401 | 4939 | mini_qdisc_bstats_cpu_update(miniq, skb); |
c9e99fd0 | 4940 | |
7d17c544 PB |
4941 | switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list, |
4942 | &cl_res, false)) { | |
d2788d34 DB |
4943 | case TC_ACT_OK: |
4944 | case TC_ACT_RECLASSIFY: | |
4945 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
4946 | break; | |
4947 | case TC_ACT_SHOT: | |
46209401 | 4948 | mini_qdisc_qstats_cpu_drop(miniq); |
8a3a4c6e ED |
4949 | kfree_skb(skb); |
4950 | return NULL; | |
d2788d34 DB |
4951 | case TC_ACT_STOLEN: |
4952 | case TC_ACT_QUEUED: | |
e25ea21f | 4953 | case TC_ACT_TRAP: |
8a3a4c6e | 4954 | consume_skb(skb); |
d2788d34 | 4955 | return NULL; |
27b29f63 AS |
4956 | case TC_ACT_REDIRECT: |
4957 | /* skb_mac_header check was done by cls/act_bpf, so | |
4958 | * we can safely push the L2 header back before | |
4959 | * redirecting to another netdev | |
4960 | */ | |
4961 | __skb_push(skb, skb->mac_len); | |
4962 | skb_do_redirect(skb); | |
4963 | return NULL; | |
720f22fe | 4964 | case TC_ACT_CONSUMED: |
cd11b164 | 4965 | return NULL; |
d2788d34 DB |
4966 | default: |
4967 | break; | |
f697c3e8 | 4968 | } |
e7582bab | 4969 | #endif /* CONFIG_NET_CLS_ACT */ |
e687ad60 PN |
4970 | return skb; |
4971 | } | |
1da177e4 | 4972 | |
24b27fc4 MB |
4973 | /** |
4974 | * netdev_is_rx_handler_busy - check if receive handler is registered | |
4975 | * @dev: device to check | |
4976 | * | |
4977 | * Check if a receive handler is already registered for a given device. | |
4978 | * Return true if there one. | |
4979 | * | |
4980 | * The caller must hold the rtnl_mutex. | |
4981 | */ | |
4982 | bool netdev_is_rx_handler_busy(struct net_device *dev) | |
4983 | { | |
4984 | ASSERT_RTNL(); | |
4985 | return dev && rtnl_dereference(dev->rx_handler); | |
4986 | } | |
4987 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); | |
4988 | ||
ab95bfe0 JP |
4989 | /** |
4990 | * netdev_rx_handler_register - register receive handler | |
4991 | * @dev: device to register a handler for | |
4992 | * @rx_handler: receive handler to register | |
93e2c32b | 4993 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 | 4994 | * |
e227867f | 4995 | * Register a receive handler for a device. This handler will then be |
ab95bfe0 JP |
4996 | * called from __netif_receive_skb. A negative errno code is returned |
4997 | * on a failure. | |
4998 | * | |
4999 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
5000 | * |
5001 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
5002 | */ |
5003 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
5004 | rx_handler_func_t *rx_handler, |
5005 | void *rx_handler_data) | |
ab95bfe0 | 5006 | { |
1b7cd004 | 5007 | if (netdev_is_rx_handler_busy(dev)) |
ab95bfe0 JP |
5008 | return -EBUSY; |
5009 | ||
f5426250 PA |
5010 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
5011 | return -EINVAL; | |
5012 | ||
00cfec37 | 5013 | /* Note: rx_handler_data must be set before rx_handler */ |
93e2c32b | 5014 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
5015 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
5016 | ||
5017 | return 0; | |
5018 | } | |
5019 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
5020 | ||
5021 | /** | |
5022 | * netdev_rx_handler_unregister - unregister receive handler | |
5023 | * @dev: device to unregister a handler from | |
5024 | * | |
166ec369 | 5025 | * Unregister a receive handler from a device. |
ab95bfe0 JP |
5026 | * |
5027 | * The caller must hold the rtnl_mutex. | |
5028 | */ | |
5029 | void netdev_rx_handler_unregister(struct net_device *dev) | |
5030 | { | |
5031 | ||
5032 | ASSERT_RTNL(); | |
a9b3cd7f | 5033 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
00cfec37 ED |
5034 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
5035 | * section has a guarantee to see a non NULL rx_handler_data | |
5036 | * as well. | |
5037 | */ | |
5038 | synchronize_net(); | |
a9b3cd7f | 5039 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
ab95bfe0 JP |
5040 | } |
5041 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
5042 | ||
b4b9e355 MG |
5043 | /* |
5044 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
5045 | * the special handling of PFMEMALLOC skbs. | |
5046 | */ | |
5047 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
5048 | { | |
5049 | switch (skb->protocol) { | |
2b8837ae JP |
5050 | case htons(ETH_P_ARP): |
5051 | case htons(ETH_P_IP): | |
5052 | case htons(ETH_P_IPV6): | |
5053 | case htons(ETH_P_8021Q): | |
5054 | case htons(ETH_P_8021AD): | |
b4b9e355 MG |
5055 | return true; |
5056 | default: | |
5057 | return false; | |
5058 | } | |
5059 | } | |
5060 | ||
e687ad60 PN |
5061 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
5062 | int *ret, struct net_device *orig_dev) | |
5063 | { | |
5064 | if (nf_hook_ingress_active(skb)) { | |
2c1e2703 AC |
5065 | int ingress_retval; |
5066 | ||
e687ad60 PN |
5067 | if (*pt_prev) { |
5068 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
5069 | *pt_prev = NULL; | |
5070 | } | |
5071 | ||
2c1e2703 AC |
5072 | rcu_read_lock(); |
5073 | ingress_retval = nf_hook_ingress(skb); | |
5074 | rcu_read_unlock(); | |
5075 | return ingress_retval; | |
e687ad60 PN |
5076 | } |
5077 | return 0; | |
5078 | } | |
e687ad60 | 5079 | |
c0bbbdc3 | 5080 | static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, |
88eb1944 | 5081 | struct packet_type **ppt_prev) |
1da177e4 LT |
5082 | { |
5083 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 5084 | rx_handler_func_t *rx_handler; |
c0bbbdc3 | 5085 | struct sk_buff *skb = *pskb; |
f2ccd8fa | 5086 | struct net_device *orig_dev; |
8a4eb573 | 5087 | bool deliver_exact = false; |
1da177e4 | 5088 | int ret = NET_RX_DROP; |
252e3346 | 5089 | __be16 type; |
1da177e4 | 5090 | |
588f0330 | 5091 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
81bbb3d4 | 5092 | |
cf66ba58 | 5093 | trace_netif_receive_skb(skb); |
9b22ea56 | 5094 | |
cc9bd5ce | 5095 | orig_dev = skb->dev; |
8f903c70 | 5096 | |
c1d2bbe1 | 5097 | skb_reset_network_header(skb); |
fda55eca ED |
5098 | if (!skb_transport_header_was_set(skb)) |
5099 | skb_reset_transport_header(skb); | |
0b5c9db1 | 5100 | skb_reset_mac_len(skb); |
1da177e4 LT |
5101 | |
5102 | pt_prev = NULL; | |
5103 | ||
63d8ea7f | 5104 | another_round: |
b6858177 | 5105 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
5106 | |
5107 | __this_cpu_inc(softnet_data.processed); | |
5108 | ||
458bf2f2 SH |
5109 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
5110 | int ret2; | |
5111 | ||
5112 | preempt_disable(); | |
5113 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb); | |
5114 | preempt_enable(); | |
5115 | ||
c0bbbdc3 BS |
5116 | if (ret2 != XDP_PASS) { |
5117 | ret = NET_RX_DROP; | |
5118 | goto out; | |
5119 | } | |
458bf2f2 SH |
5120 | skb_reset_mac_len(skb); |
5121 | } | |
5122 | ||
8ad227ff PM |
5123 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
5124 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { | |
0d5501c1 | 5125 | skb = skb_vlan_untag(skb); |
bcc6d479 | 5126 | if (unlikely(!skb)) |
2c17d27c | 5127 | goto out; |
bcc6d479 JP |
5128 | } |
5129 | ||
e7246e12 WB |
5130 | if (skb_skip_tc_classify(skb)) |
5131 | goto skip_classify; | |
1da177e4 | 5132 | |
9754e293 | 5133 | if (pfmemalloc) |
b4b9e355 MG |
5134 | goto skip_taps; |
5135 | ||
1da177e4 | 5136 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
7866a621 SN |
5137 | if (pt_prev) |
5138 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5139 | pt_prev = ptype; | |
5140 | } | |
5141 | ||
5142 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { | |
5143 | if (pt_prev) | |
5144 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5145 | pt_prev = ptype; | |
1da177e4 LT |
5146 | } |
5147 | ||
b4b9e355 | 5148 | skip_taps: |
1cf51900 | 5149 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 5150 | if (static_branch_unlikely(&ingress_needed_key)) { |
1f211a1b | 5151 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev); |
4577139b | 5152 | if (!skb) |
2c17d27c | 5153 | goto out; |
e687ad60 PN |
5154 | |
5155 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) | |
2c17d27c | 5156 | goto out; |
4577139b | 5157 | } |
1cf51900 | 5158 | #endif |
2c64605b | 5159 | skb_reset_redirect(skb); |
e7246e12 | 5160 | skip_classify: |
9754e293 | 5161 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
b4b9e355 MG |
5162 | goto drop; |
5163 | ||
df8a39de | 5164 | if (skb_vlan_tag_present(skb)) { |
2425717b JF |
5165 | if (pt_prev) { |
5166 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5167 | pt_prev = NULL; | |
5168 | } | |
48cc32d3 | 5169 | if (vlan_do_receive(&skb)) |
2425717b JF |
5170 | goto another_round; |
5171 | else if (unlikely(!skb)) | |
2c17d27c | 5172 | goto out; |
2425717b JF |
5173 | } |
5174 | ||
48cc32d3 | 5175 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
5176 | if (rx_handler) { |
5177 | if (pt_prev) { | |
5178 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5179 | pt_prev = NULL; | |
5180 | } | |
8a4eb573 JP |
5181 | switch (rx_handler(&skb)) { |
5182 | case RX_HANDLER_CONSUMED: | |
3bc1b1ad | 5183 | ret = NET_RX_SUCCESS; |
2c17d27c | 5184 | goto out; |
8a4eb573 | 5185 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 5186 | goto another_round; |
8a4eb573 JP |
5187 | case RX_HANDLER_EXACT: |
5188 | deliver_exact = true; | |
5189 | case RX_HANDLER_PASS: | |
5190 | break; | |
5191 | default: | |
5192 | BUG(); | |
5193 | } | |
ab95bfe0 | 5194 | } |
1da177e4 | 5195 | |
b14a9fc4 | 5196 | if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) { |
36b2f61a GV |
5197 | check_vlan_id: |
5198 | if (skb_vlan_tag_get_id(skb)) { | |
5199 | /* Vlan id is non 0 and vlan_do_receive() above couldn't | |
5200 | * find vlan device. | |
5201 | */ | |
d4b812de | 5202 | skb->pkt_type = PACKET_OTHERHOST; |
36b2f61a GV |
5203 | } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
5204 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { | |
5205 | /* Outer header is 802.1P with vlan 0, inner header is | |
5206 | * 802.1Q or 802.1AD and vlan_do_receive() above could | |
5207 | * not find vlan dev for vlan id 0. | |
5208 | */ | |
5209 | __vlan_hwaccel_clear_tag(skb); | |
5210 | skb = skb_vlan_untag(skb); | |
5211 | if (unlikely(!skb)) | |
5212 | goto out; | |
5213 | if (vlan_do_receive(&skb)) | |
5214 | /* After stripping off 802.1P header with vlan 0 | |
5215 | * vlan dev is found for inner header. | |
5216 | */ | |
5217 | goto another_round; | |
5218 | else if (unlikely(!skb)) | |
5219 | goto out; | |
5220 | else | |
5221 | /* We have stripped outer 802.1P vlan 0 header. | |
5222 | * But could not find vlan dev. | |
5223 | * check again for vlan id to set OTHERHOST. | |
5224 | */ | |
5225 | goto check_vlan_id; | |
5226 | } | |
d4b812de ED |
5227 | /* Note: we might in the future use prio bits |
5228 | * and set skb->priority like in vlan_do_receive() | |
5229 | * For the time being, just ignore Priority Code Point | |
5230 | */ | |
b1817524 | 5231 | __vlan_hwaccel_clear_tag(skb); |
d4b812de | 5232 | } |
48cc32d3 | 5233 | |
7866a621 SN |
5234 | type = skb->protocol; |
5235 | ||
63d8ea7f | 5236 | /* deliver only exact match when indicated */ |
7866a621 SN |
5237 | if (likely(!deliver_exact)) { |
5238 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5239 | &ptype_base[ntohs(type) & | |
5240 | PTYPE_HASH_MASK]); | |
5241 | } | |
1f3c8804 | 5242 | |
7866a621 SN |
5243 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
5244 | &orig_dev->ptype_specific); | |
5245 | ||
5246 | if (unlikely(skb->dev != orig_dev)) { | |
5247 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5248 | &skb->dev->ptype_specific); | |
1da177e4 LT |
5249 | } |
5250 | ||
5251 | if (pt_prev) { | |
1f8b977a | 5252 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
0e698bf6 | 5253 | goto drop; |
88eb1944 | 5254 | *ppt_prev = pt_prev; |
1da177e4 | 5255 | } else { |
b4b9e355 | 5256 | drop: |
6e7333d3 JW |
5257 | if (!deliver_exact) |
5258 | atomic_long_inc(&skb->dev->rx_dropped); | |
5259 | else | |
5260 | atomic_long_inc(&skb->dev->rx_nohandler); | |
1da177e4 LT |
5261 | kfree_skb(skb); |
5262 | /* Jamal, now you will not able to escape explaining | |
5263 | * me how you were going to use this. :-) | |
5264 | */ | |
5265 | ret = NET_RX_DROP; | |
5266 | } | |
5267 | ||
2c17d27c | 5268 | out: |
c0bbbdc3 BS |
5269 | /* The invariant here is that if *ppt_prev is not NULL |
5270 | * then skb should also be non-NULL. | |
5271 | * | |
5272 | * Apparently *ppt_prev assignment above holds this invariant due to | |
5273 | * skb dereferencing near it. | |
5274 | */ | |
5275 | *pskb = skb; | |
9754e293 DM |
5276 | return ret; |
5277 | } | |
5278 | ||
88eb1944 EC |
5279 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
5280 | { | |
5281 | struct net_device *orig_dev = skb->dev; | |
5282 | struct packet_type *pt_prev = NULL; | |
5283 | int ret; | |
5284 | ||
c0bbbdc3 | 5285 | ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
88eb1944 | 5286 | if (pt_prev) |
f5737cba PA |
5287 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
5288 | skb->dev, pt_prev, orig_dev); | |
88eb1944 EC |
5289 | return ret; |
5290 | } | |
5291 | ||
1c601d82 JDB |
5292 | /** |
5293 | * netif_receive_skb_core - special purpose version of netif_receive_skb | |
5294 | * @skb: buffer to process | |
5295 | * | |
5296 | * More direct receive version of netif_receive_skb(). It should | |
5297 | * only be used by callers that have a need to skip RPS and Generic XDP. | |
2de9780f | 5298 | * Caller must also take care of handling if ``(page_is_)pfmemalloc``. |
1c601d82 JDB |
5299 | * |
5300 | * This function may only be called from softirq context and interrupts | |
5301 | * should be enabled. | |
5302 | * | |
5303 | * Return values (usually ignored): | |
5304 | * NET_RX_SUCCESS: no congestion | |
5305 | * NET_RX_DROP: packet was dropped | |
5306 | */ | |
5307 | int netif_receive_skb_core(struct sk_buff *skb) | |
5308 | { | |
5309 | int ret; | |
5310 | ||
5311 | rcu_read_lock(); | |
88eb1944 | 5312 | ret = __netif_receive_skb_one_core(skb, false); |
1c601d82 JDB |
5313 | rcu_read_unlock(); |
5314 | ||
5315 | return ret; | |
5316 | } | |
5317 | EXPORT_SYMBOL(netif_receive_skb_core); | |
5318 | ||
88eb1944 EC |
5319 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
5320 | struct packet_type *pt_prev, | |
5321 | struct net_device *orig_dev) | |
4ce0017a EC |
5322 | { |
5323 | struct sk_buff *skb, *next; | |
5324 | ||
88eb1944 EC |
5325 | if (!pt_prev) |
5326 | return; | |
5327 | if (list_empty(head)) | |
5328 | return; | |
17266ee9 | 5329 | if (pt_prev->list_func != NULL) |
fdf71426 PA |
5330 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
5331 | ip_list_rcv, head, pt_prev, orig_dev); | |
17266ee9 | 5332 | else |
9a5a90d1 AL |
5333 | list_for_each_entry_safe(skb, next, head, list) { |
5334 | skb_list_del_init(skb); | |
fdf71426 | 5335 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
9a5a90d1 | 5336 | } |
88eb1944 EC |
5337 | } |
5338 | ||
5339 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) | |
5340 | { | |
5341 | /* Fast-path assumptions: | |
5342 | * - There is no RX handler. | |
5343 | * - Only one packet_type matches. | |
5344 | * If either of these fails, we will end up doing some per-packet | |
5345 | * processing in-line, then handling the 'last ptype' for the whole | |
5346 | * sublist. This can't cause out-of-order delivery to any single ptype, | |
5347 | * because the 'last ptype' must be constant across the sublist, and all | |
5348 | * other ptypes are handled per-packet. | |
5349 | */ | |
5350 | /* Current (common) ptype of sublist */ | |
5351 | struct packet_type *pt_curr = NULL; | |
5352 | /* Current (common) orig_dev of sublist */ | |
5353 | struct net_device *od_curr = NULL; | |
5354 | struct list_head sublist; | |
5355 | struct sk_buff *skb, *next; | |
5356 | ||
9af86f93 | 5357 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5358 | list_for_each_entry_safe(skb, next, head, list) { |
5359 | struct net_device *orig_dev = skb->dev; | |
5360 | struct packet_type *pt_prev = NULL; | |
5361 | ||
22f6bbb7 | 5362 | skb_list_del_init(skb); |
c0bbbdc3 | 5363 | __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
9af86f93 EC |
5364 | if (!pt_prev) |
5365 | continue; | |
88eb1944 EC |
5366 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
5367 | /* dispatch old sublist */ | |
88eb1944 EC |
5368 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
5369 | /* start new sublist */ | |
9af86f93 | 5370 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5371 | pt_curr = pt_prev; |
5372 | od_curr = orig_dev; | |
5373 | } | |
9af86f93 | 5374 | list_add_tail(&skb->list, &sublist); |
88eb1944 EC |
5375 | } |
5376 | ||
5377 | /* dispatch final sublist */ | |
9af86f93 | 5378 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
4ce0017a EC |
5379 | } |
5380 | ||
9754e293 DM |
5381 | static int __netif_receive_skb(struct sk_buff *skb) |
5382 | { | |
5383 | int ret; | |
5384 | ||
5385 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { | |
f1083048 | 5386 | unsigned int noreclaim_flag; |
9754e293 DM |
5387 | |
5388 | /* | |
5389 | * PFMEMALLOC skbs are special, they should | |
5390 | * - be delivered to SOCK_MEMALLOC sockets only | |
5391 | * - stay away from userspace | |
5392 | * - have bounded memory usage | |
5393 | * | |
5394 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
5395 | * context down to all allocation sites. | |
5396 | */ | |
f1083048 | 5397 | noreclaim_flag = memalloc_noreclaim_save(); |
88eb1944 | 5398 | ret = __netif_receive_skb_one_core(skb, true); |
f1083048 | 5399 | memalloc_noreclaim_restore(noreclaim_flag); |
9754e293 | 5400 | } else |
88eb1944 | 5401 | ret = __netif_receive_skb_one_core(skb, false); |
9754e293 | 5402 | |
1da177e4 LT |
5403 | return ret; |
5404 | } | |
0a9627f2 | 5405 | |
4ce0017a EC |
5406 | static void __netif_receive_skb_list(struct list_head *head) |
5407 | { | |
5408 | unsigned long noreclaim_flag = 0; | |
5409 | struct sk_buff *skb, *next; | |
5410 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ | |
5411 | ||
5412 | list_for_each_entry_safe(skb, next, head, list) { | |
5413 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { | |
5414 | struct list_head sublist; | |
5415 | ||
5416 | /* Handle the previous sublist */ | |
5417 | list_cut_before(&sublist, head, &skb->list); | |
b9f463d6 EC |
5418 | if (!list_empty(&sublist)) |
5419 | __netif_receive_skb_list_core(&sublist, pfmemalloc); | |
4ce0017a EC |
5420 | pfmemalloc = !pfmemalloc; |
5421 | /* See comments in __netif_receive_skb */ | |
5422 | if (pfmemalloc) | |
5423 | noreclaim_flag = memalloc_noreclaim_save(); | |
5424 | else | |
5425 | memalloc_noreclaim_restore(noreclaim_flag); | |
5426 | } | |
5427 | } | |
5428 | /* Handle the remaining sublist */ | |
b9f463d6 EC |
5429 | if (!list_empty(head)) |
5430 | __netif_receive_skb_list_core(head, pfmemalloc); | |
4ce0017a EC |
5431 | /* Restore pflags */ |
5432 | if (pfmemalloc) | |
5433 | memalloc_noreclaim_restore(noreclaim_flag); | |
5434 | } | |
5435 | ||
f4e63525 | 5436 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
b5cdae32 | 5437 | { |
58038695 | 5438 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
b5cdae32 DM |
5439 | struct bpf_prog *new = xdp->prog; |
5440 | int ret = 0; | |
5441 | ||
fbee97fe DA |
5442 | if (new) { |
5443 | u32 i; | |
5444 | ||
5445 | /* generic XDP does not work with DEVMAPs that can | |
5446 | * have a bpf_prog installed on an entry | |
5447 | */ | |
5448 | for (i = 0; i < new->aux->used_map_cnt; i++) { | |
5449 | if (dev_map_can_have_prog(new->aux->used_maps[i])) | |
5450 | return -EINVAL; | |
92164774 LB |
5451 | if (cpu_map_prog_allowed(new->aux->used_maps[i])) |
5452 | return -EINVAL; | |
fbee97fe DA |
5453 | } |
5454 | } | |
5455 | ||
b5cdae32 | 5456 | switch (xdp->command) { |
58038695 | 5457 | case XDP_SETUP_PROG: |
b5cdae32 DM |
5458 | rcu_assign_pointer(dev->xdp_prog, new); |
5459 | if (old) | |
5460 | bpf_prog_put(old); | |
5461 | ||
5462 | if (old && !new) { | |
02786475 | 5463 | static_branch_dec(&generic_xdp_needed_key); |
b5cdae32 | 5464 | } else if (new && !old) { |
02786475 | 5465 | static_branch_inc(&generic_xdp_needed_key); |
b5cdae32 | 5466 | dev_disable_lro(dev); |
56f5aa77 | 5467 | dev_disable_gro_hw(dev); |
b5cdae32 DM |
5468 | } |
5469 | break; | |
b5cdae32 | 5470 | |
b5cdae32 DM |
5471 | default: |
5472 | ret = -EINVAL; | |
5473 | break; | |
5474 | } | |
5475 | ||
5476 | return ret; | |
5477 | } | |
5478 | ||
ae78dbfa | 5479 | static int netif_receive_skb_internal(struct sk_buff *skb) |
0a9627f2 | 5480 | { |
2c17d27c JA |
5481 | int ret; |
5482 | ||
588f0330 | 5483 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
3b098e2d | 5484 | |
c1f19b51 RC |
5485 | if (skb_defer_rx_timestamp(skb)) |
5486 | return NET_RX_SUCCESS; | |
5487 | ||
bbbe211c | 5488 | rcu_read_lock(); |
df334545 | 5489 | #ifdef CONFIG_RPS |
dc05360f | 5490 | if (static_branch_unlikely(&rps_needed)) { |
3b098e2d | 5491 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
2c17d27c | 5492 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
0a9627f2 | 5493 | |
3b098e2d ED |
5494 | if (cpu >= 0) { |
5495 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5496 | rcu_read_unlock(); | |
adc9300e | 5497 | return ret; |
3b098e2d | 5498 | } |
fec5e652 | 5499 | } |
1e94d72f | 5500 | #endif |
2c17d27c JA |
5501 | ret = __netif_receive_skb(skb); |
5502 | rcu_read_unlock(); | |
5503 | return ret; | |
0a9627f2 | 5504 | } |
ae78dbfa | 5505 | |
7da517a3 EC |
5506 | static void netif_receive_skb_list_internal(struct list_head *head) |
5507 | { | |
7da517a3 | 5508 | struct sk_buff *skb, *next; |
8c057efa | 5509 | struct list_head sublist; |
7da517a3 | 5510 | |
8c057efa | 5511 | INIT_LIST_HEAD(&sublist); |
7da517a3 EC |
5512 | list_for_each_entry_safe(skb, next, head, list) { |
5513 | net_timestamp_check(netdev_tstamp_prequeue, skb); | |
22f6bbb7 | 5514 | skb_list_del_init(skb); |
8c057efa EC |
5515 | if (!skb_defer_rx_timestamp(skb)) |
5516 | list_add_tail(&skb->list, &sublist); | |
7da517a3 | 5517 | } |
8c057efa | 5518 | list_splice_init(&sublist, head); |
7da517a3 | 5519 | |
7da517a3 EC |
5520 | rcu_read_lock(); |
5521 | #ifdef CONFIG_RPS | |
dc05360f | 5522 | if (static_branch_unlikely(&rps_needed)) { |
7da517a3 EC |
5523 | list_for_each_entry_safe(skb, next, head, list) { |
5524 | struct rps_dev_flow voidflow, *rflow = &voidflow; | |
5525 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
5526 | ||
5527 | if (cpu >= 0) { | |
8c057efa | 5528 | /* Will be handled, remove from list */ |
22f6bbb7 | 5529 | skb_list_del_init(skb); |
8c057efa | 5530 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
7da517a3 EC |
5531 | } |
5532 | } | |
5533 | } | |
5534 | #endif | |
5535 | __netif_receive_skb_list(head); | |
5536 | rcu_read_unlock(); | |
5537 | } | |
5538 | ||
ae78dbfa BH |
5539 | /** |
5540 | * netif_receive_skb - process receive buffer from network | |
5541 | * @skb: buffer to process | |
5542 | * | |
5543 | * netif_receive_skb() is the main receive data processing function. | |
5544 | * It always succeeds. The buffer may be dropped during processing | |
5545 | * for congestion control or by the protocol layers. | |
5546 | * | |
5547 | * This function may only be called from softirq context and interrupts | |
5548 | * should be enabled. | |
5549 | * | |
5550 | * Return values (usually ignored): | |
5551 | * NET_RX_SUCCESS: no congestion | |
5552 | * NET_RX_DROP: packet was dropped | |
5553 | */ | |
04eb4489 | 5554 | int netif_receive_skb(struct sk_buff *skb) |
ae78dbfa | 5555 | { |
b0e3f1bd GB |
5556 | int ret; |
5557 | ||
ae78dbfa BH |
5558 | trace_netif_receive_skb_entry(skb); |
5559 | ||
b0e3f1bd GB |
5560 | ret = netif_receive_skb_internal(skb); |
5561 | trace_netif_receive_skb_exit(ret); | |
5562 | ||
5563 | return ret; | |
ae78dbfa | 5564 | } |
04eb4489 | 5565 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 5566 | |
f6ad8c1b EC |
5567 | /** |
5568 | * netif_receive_skb_list - process many receive buffers from network | |
5569 | * @head: list of skbs to process. | |
5570 | * | |
7da517a3 EC |
5571 | * Since return value of netif_receive_skb() is normally ignored, and |
5572 | * wouldn't be meaningful for a list, this function returns void. | |
f6ad8c1b EC |
5573 | * |
5574 | * This function may only be called from softirq context and interrupts | |
5575 | * should be enabled. | |
5576 | */ | |
5577 | void netif_receive_skb_list(struct list_head *head) | |
5578 | { | |
7da517a3 | 5579 | struct sk_buff *skb; |
f6ad8c1b | 5580 | |
b9f463d6 EC |
5581 | if (list_empty(head)) |
5582 | return; | |
b0e3f1bd GB |
5583 | if (trace_netif_receive_skb_list_entry_enabled()) { |
5584 | list_for_each_entry(skb, head, list) | |
5585 | trace_netif_receive_skb_list_entry(skb); | |
5586 | } | |
7da517a3 | 5587 | netif_receive_skb_list_internal(head); |
b0e3f1bd | 5588 | trace_netif_receive_skb_list_exit(0); |
f6ad8c1b EC |
5589 | } |
5590 | EXPORT_SYMBOL(netif_receive_skb_list); | |
5591 | ||
ce1e2a77 | 5592 | static DEFINE_PER_CPU(struct work_struct, flush_works); |
145dd5f9 PA |
5593 | |
5594 | /* Network device is going away, flush any packets still pending */ | |
5595 | static void flush_backlog(struct work_struct *work) | |
6e583ce5 | 5596 | { |
6e583ce5 | 5597 | struct sk_buff *skb, *tmp; |
145dd5f9 PA |
5598 | struct softnet_data *sd; |
5599 | ||
5600 | local_bh_disable(); | |
5601 | sd = this_cpu_ptr(&softnet_data); | |
6e583ce5 | 5602 | |
145dd5f9 | 5603 | local_irq_disable(); |
e36fa2f7 | 5604 | rps_lock(sd); |
6e7676c1 | 5605 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
41852497 | 5606 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
e36fa2f7 | 5607 | __skb_unlink(skb, &sd->input_pkt_queue); |
7df5cb75 | 5608 | dev_kfree_skb_irq(skb); |
76cc8b13 | 5609 | input_queue_head_incr(sd); |
6e583ce5 | 5610 | } |
6e7676c1 | 5611 | } |
e36fa2f7 | 5612 | rps_unlock(sd); |
145dd5f9 | 5613 | local_irq_enable(); |
6e7676c1 CG |
5614 | |
5615 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
41852497 | 5616 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
6e7676c1 CG |
5617 | __skb_unlink(skb, &sd->process_queue); |
5618 | kfree_skb(skb); | |
76cc8b13 | 5619 | input_queue_head_incr(sd); |
6e7676c1 CG |
5620 | } |
5621 | } | |
145dd5f9 PA |
5622 | local_bh_enable(); |
5623 | } | |
5624 | ||
2de79ee2 PA |
5625 | static bool flush_required(int cpu) |
5626 | { | |
5627 | #if IS_ENABLED(CONFIG_RPS) | |
5628 | struct softnet_data *sd = &per_cpu(softnet_data, cpu); | |
5629 | bool do_flush; | |
5630 | ||
5631 | local_irq_disable(); | |
5632 | rps_lock(sd); | |
5633 | ||
5634 | /* as insertion into process_queue happens with the rps lock held, | |
5635 | * process_queue access may race only with dequeue | |
5636 | */ | |
5637 | do_flush = !skb_queue_empty(&sd->input_pkt_queue) || | |
5638 | !skb_queue_empty_lockless(&sd->process_queue); | |
5639 | rps_unlock(sd); | |
5640 | local_irq_enable(); | |
5641 | ||
5642 | return do_flush; | |
5643 | #endif | |
5644 | /* without RPS we can't safely check input_pkt_queue: during a | |
5645 | * concurrent remote skb_queue_splice() we can detect as empty both | |
5646 | * input_pkt_queue and process_queue even if the latter could end-up | |
5647 | * containing a lot of packets. | |
5648 | */ | |
5649 | return true; | |
5650 | } | |
5651 | ||
41852497 | 5652 | static void flush_all_backlogs(void) |
145dd5f9 | 5653 | { |
2de79ee2 | 5654 | static cpumask_t flush_cpus; |
145dd5f9 PA |
5655 | unsigned int cpu; |
5656 | ||
2de79ee2 PA |
5657 | /* since we are under rtnl lock protection we can use static data |
5658 | * for the cpumask and avoid allocating on stack the possibly | |
5659 | * large mask | |
5660 | */ | |
5661 | ASSERT_RTNL(); | |
5662 | ||
145dd5f9 PA |
5663 | get_online_cpus(); |
5664 | ||
2de79ee2 PA |
5665 | cpumask_clear(&flush_cpus); |
5666 | for_each_online_cpu(cpu) { | |
5667 | if (flush_required(cpu)) { | |
5668 | queue_work_on(cpu, system_highpri_wq, | |
5669 | per_cpu_ptr(&flush_works, cpu)); | |
5670 | cpumask_set_cpu(cpu, &flush_cpus); | |
5671 | } | |
5672 | } | |
145dd5f9 | 5673 | |
2de79ee2 PA |
5674 | /* we can have in flight packet[s] on the cpus we are not flushing, |
5675 | * synchronize_net() in rollback_registered_many() will take care of | |
5676 | * them | |
5677 | */ | |
5678 | for_each_cpu(cpu, &flush_cpus) | |
41852497 | 5679 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
145dd5f9 PA |
5680 | |
5681 | put_online_cpus(); | |
6e583ce5 SH |
5682 | } |
5683 | ||
c8079432 MM |
5684 | /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */ |
5685 | static void gro_normal_list(struct napi_struct *napi) | |
5686 | { | |
5687 | if (!napi->rx_count) | |
5688 | return; | |
5689 | netif_receive_skb_list_internal(&napi->rx_list); | |
5690 | INIT_LIST_HEAD(&napi->rx_list); | |
5691 | napi->rx_count = 0; | |
5692 | } | |
5693 | ||
5694 | /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded, | |
5695 | * pass the whole batch up to the stack. | |
5696 | */ | |
5697 | static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb) | |
5698 | { | |
5699 | list_add_tail(&skb->list, &napi->rx_list); | |
5700 | if (++napi->rx_count >= gro_normal_batch) | |
5701 | gro_normal_list(napi); | |
5702 | } | |
5703 | ||
aaa5d90b PA |
5704 | INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int)); |
5705 | INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int)); | |
c8079432 | 5706 | static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5707 | { |
22061d80 | 5708 | struct packet_offload *ptype; |
d565b0a1 | 5709 | __be16 type = skb->protocol; |
22061d80 | 5710 | struct list_head *head = &offload_base; |
d565b0a1 HX |
5711 | int err = -ENOENT; |
5712 | ||
c3c7c254 ED |
5713 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
5714 | ||
fc59f9a3 HX |
5715 | if (NAPI_GRO_CB(skb)->count == 1) { |
5716 | skb_shinfo(skb)->gso_size = 0; | |
d565b0a1 | 5717 | goto out; |
fc59f9a3 | 5718 | } |
d565b0a1 HX |
5719 | |
5720 | rcu_read_lock(); | |
5721 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5722 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
d565b0a1 HX |
5723 | continue; |
5724 | ||
aaa5d90b PA |
5725 | err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete, |
5726 | ipv6_gro_complete, inet_gro_complete, | |
5727 | skb, 0); | |
d565b0a1 HX |
5728 | break; |
5729 | } | |
5730 | rcu_read_unlock(); | |
5731 | ||
5732 | if (err) { | |
5733 | WARN_ON(&ptype->list == head); | |
5734 | kfree_skb(skb); | |
5735 | return NET_RX_SUCCESS; | |
5736 | } | |
5737 | ||
5738 | out: | |
c8079432 MM |
5739 | gro_normal_one(napi, skb); |
5740 | return NET_RX_SUCCESS; | |
d565b0a1 HX |
5741 | } |
5742 | ||
6312fe77 | 5743 | static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index, |
07d78363 | 5744 | bool flush_old) |
d565b0a1 | 5745 | { |
6312fe77 | 5746 | struct list_head *head = &napi->gro_hash[index].list; |
d4546c25 | 5747 | struct sk_buff *skb, *p; |
2e71a6f8 | 5748 | |
07d78363 | 5749 | list_for_each_entry_safe_reverse(skb, p, head, list) { |
2e71a6f8 ED |
5750 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) |
5751 | return; | |
992cba7e | 5752 | skb_list_del_init(skb); |
c8079432 | 5753 | napi_gro_complete(napi, skb); |
6312fe77 | 5754 | napi->gro_hash[index].count--; |
d565b0a1 | 5755 | } |
d9f37d01 LR |
5756 | |
5757 | if (!napi->gro_hash[index].count) | |
5758 | __clear_bit(index, &napi->gro_bitmask); | |
d565b0a1 | 5759 | } |
07d78363 | 5760 | |
6312fe77 | 5761 | /* napi->gro_hash[].list contains packets ordered by age. |
07d78363 DM |
5762 | * youngest packets at the head of it. |
5763 | * Complete skbs in reverse order to reduce latencies. | |
5764 | */ | |
5765 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) | |
5766 | { | |
42519ede ED |
5767 | unsigned long bitmask = napi->gro_bitmask; |
5768 | unsigned int i, base = ~0U; | |
07d78363 | 5769 | |
42519ede ED |
5770 | while ((i = ffs(bitmask)) != 0) { |
5771 | bitmask >>= i; | |
5772 | base += i; | |
5773 | __napi_gro_flush_chain(napi, base, flush_old); | |
d9f37d01 | 5774 | } |
07d78363 | 5775 | } |
86cac58b | 5776 | EXPORT_SYMBOL(napi_gro_flush); |
d565b0a1 | 5777 | |
07d78363 DM |
5778 | static struct list_head *gro_list_prepare(struct napi_struct *napi, |
5779 | struct sk_buff *skb) | |
89c5fa33 | 5780 | { |
89c5fa33 | 5781 | unsigned int maclen = skb->dev->hard_header_len; |
0b4cec8c | 5782 | u32 hash = skb_get_hash_raw(skb); |
07d78363 | 5783 | struct list_head *head; |
d4546c25 | 5784 | struct sk_buff *p; |
89c5fa33 | 5785 | |
6312fe77 | 5786 | head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list; |
07d78363 | 5787 | list_for_each_entry(p, head, list) { |
89c5fa33 ED |
5788 | unsigned long diffs; |
5789 | ||
0b4cec8c TH |
5790 | NAPI_GRO_CB(p)->flush = 0; |
5791 | ||
5792 | if (hash != skb_get_hash_raw(p)) { | |
5793 | NAPI_GRO_CB(p)->same_flow = 0; | |
5794 | continue; | |
5795 | } | |
5796 | ||
89c5fa33 | 5797 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; |
b1817524 MM |
5798 | diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb); |
5799 | if (skb_vlan_tag_present(p)) | |
fc5141cb | 5800 | diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb); |
ce87fc6c | 5801 | diffs |= skb_metadata_dst_cmp(p, skb); |
de8f3a83 | 5802 | diffs |= skb_metadata_differs(p, skb); |
89c5fa33 ED |
5803 | if (maclen == ETH_HLEN) |
5804 | diffs |= compare_ether_header(skb_mac_header(p), | |
a50e233c | 5805 | skb_mac_header(skb)); |
89c5fa33 ED |
5806 | else if (!diffs) |
5807 | diffs = memcmp(skb_mac_header(p), | |
a50e233c | 5808 | skb_mac_header(skb), |
89c5fa33 ED |
5809 | maclen); |
5810 | NAPI_GRO_CB(p)->same_flow = !diffs; | |
89c5fa33 | 5811 | } |
07d78363 DM |
5812 | |
5813 | return head; | |
89c5fa33 ED |
5814 | } |
5815 | ||
299603e8 JC |
5816 | static void skb_gro_reset_offset(struct sk_buff *skb) |
5817 | { | |
5818 | const struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5819 | const skb_frag_t *frag0 = &pinfo->frags[0]; | |
5820 | ||
5821 | NAPI_GRO_CB(skb)->data_offset = 0; | |
5822 | NAPI_GRO_CB(skb)->frag0 = NULL; | |
5823 | NAPI_GRO_CB(skb)->frag0_len = 0; | |
5824 | ||
8aef998d | 5825 | if (!skb_headlen(skb) && pinfo->nr_frags && |
299603e8 JC |
5826 | !PageHighMem(skb_frag_page(frag0))) { |
5827 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); | |
7cfd5fd5 ED |
5828 | NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int, |
5829 | skb_frag_size(frag0), | |
5830 | skb->end - skb->tail); | |
89c5fa33 ED |
5831 | } |
5832 | } | |
5833 | ||
a50e233c ED |
5834 | static void gro_pull_from_frag0(struct sk_buff *skb, int grow) |
5835 | { | |
5836 | struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5837 | ||
5838 | BUG_ON(skb->end - skb->tail < grow); | |
5839 | ||
5840 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); | |
5841 | ||
5842 | skb->data_len -= grow; | |
5843 | skb->tail += grow; | |
5844 | ||
b54c9d5b | 5845 | skb_frag_off_add(&pinfo->frags[0], grow); |
a50e233c ED |
5846 | skb_frag_size_sub(&pinfo->frags[0], grow); |
5847 | ||
5848 | if (unlikely(!skb_frag_size(&pinfo->frags[0]))) { | |
5849 | skb_frag_unref(skb, 0); | |
5850 | memmove(pinfo->frags, pinfo->frags + 1, | |
5851 | --pinfo->nr_frags * sizeof(pinfo->frags[0])); | |
5852 | } | |
5853 | } | |
5854 | ||
c8079432 | 5855 | static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head) |
07d78363 | 5856 | { |
6312fe77 | 5857 | struct sk_buff *oldest; |
07d78363 | 5858 | |
6312fe77 | 5859 | oldest = list_last_entry(head, struct sk_buff, list); |
07d78363 | 5860 | |
6312fe77 | 5861 | /* We are called with head length >= MAX_GRO_SKBS, so this is |
07d78363 DM |
5862 | * impossible. |
5863 | */ | |
5864 | if (WARN_ON_ONCE(!oldest)) | |
5865 | return; | |
5866 | ||
d9f37d01 LR |
5867 | /* Do not adjust napi->gro_hash[].count, caller is adding a new |
5868 | * SKB to the chain. | |
07d78363 | 5869 | */ |
ece23711 | 5870 | skb_list_del_init(oldest); |
c8079432 | 5871 | napi_gro_complete(napi, oldest); |
07d78363 DM |
5872 | } |
5873 | ||
aaa5d90b PA |
5874 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *, |
5875 | struct sk_buff *)); | |
5876 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *, | |
5877 | struct sk_buff *)); | |
bb728820 | 5878 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5879 | { |
6312fe77 | 5880 | u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1); |
d4546c25 | 5881 | struct list_head *head = &offload_base; |
22061d80 | 5882 | struct packet_offload *ptype; |
d565b0a1 | 5883 | __be16 type = skb->protocol; |
07d78363 | 5884 | struct list_head *gro_head; |
d4546c25 | 5885 | struct sk_buff *pp = NULL; |
5b252f0c | 5886 | enum gro_result ret; |
d4546c25 | 5887 | int same_flow; |
a50e233c | 5888 | int grow; |
d565b0a1 | 5889 | |
b5cdae32 | 5890 | if (netif_elide_gro(skb->dev)) |
d565b0a1 HX |
5891 | goto normal; |
5892 | ||
07d78363 | 5893 | gro_head = gro_list_prepare(napi, skb); |
89c5fa33 | 5894 | |
d565b0a1 HX |
5895 | rcu_read_lock(); |
5896 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5897 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
d565b0a1 HX |
5898 | continue; |
5899 | ||
86911732 | 5900 | skb_set_network_header(skb, skb_gro_offset(skb)); |
efd9450e | 5901 | skb_reset_mac_len(skb); |
d565b0a1 | 5902 | NAPI_GRO_CB(skb)->same_flow = 0; |
d61d072e | 5903 | NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb); |
5d38a079 | 5904 | NAPI_GRO_CB(skb)->free = 0; |
fac8e0f5 | 5905 | NAPI_GRO_CB(skb)->encap_mark = 0; |
fcd91dd4 | 5906 | NAPI_GRO_CB(skb)->recursion_counter = 0; |
a0ca153f | 5907 | NAPI_GRO_CB(skb)->is_fou = 0; |
1530545e | 5908 | NAPI_GRO_CB(skb)->is_atomic = 1; |
15e2396d | 5909 | NAPI_GRO_CB(skb)->gro_remcsum_start = 0; |
d565b0a1 | 5910 | |
662880f4 TH |
5911 | /* Setup for GRO checksum validation */ |
5912 | switch (skb->ip_summed) { | |
5913 | case CHECKSUM_COMPLETE: | |
5914 | NAPI_GRO_CB(skb)->csum = skb->csum; | |
5915 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
5916 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5917 | break; | |
5918 | case CHECKSUM_UNNECESSARY: | |
5919 | NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1; | |
5920 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5921 | break; | |
5922 | default: | |
5923 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5924 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5925 | } | |
d565b0a1 | 5926 | |
aaa5d90b PA |
5927 | pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive, |
5928 | ipv6_gro_receive, inet_gro_receive, | |
5929 | gro_head, skb); | |
d565b0a1 HX |
5930 | break; |
5931 | } | |
5932 | rcu_read_unlock(); | |
5933 | ||
5934 | if (&ptype->list == head) | |
5935 | goto normal; | |
5936 | ||
45586c70 | 5937 | if (PTR_ERR(pp) == -EINPROGRESS) { |
25393d3f SK |
5938 | ret = GRO_CONSUMED; |
5939 | goto ok; | |
5940 | } | |
5941 | ||
0da2afd5 | 5942 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
5d0d9be8 | 5943 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
0da2afd5 | 5944 | |
d565b0a1 | 5945 | if (pp) { |
992cba7e | 5946 | skb_list_del_init(pp); |
c8079432 | 5947 | napi_gro_complete(napi, pp); |
6312fe77 | 5948 | napi->gro_hash[hash].count--; |
d565b0a1 HX |
5949 | } |
5950 | ||
0da2afd5 | 5951 | if (same_flow) |
d565b0a1 HX |
5952 | goto ok; |
5953 | ||
600adc18 | 5954 | if (NAPI_GRO_CB(skb)->flush) |
d565b0a1 | 5955 | goto normal; |
d565b0a1 | 5956 | |
6312fe77 | 5957 | if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) { |
c8079432 | 5958 | gro_flush_oldest(napi, gro_head); |
600adc18 | 5959 | } else { |
6312fe77 | 5960 | napi->gro_hash[hash].count++; |
600adc18 | 5961 | } |
d565b0a1 | 5962 | NAPI_GRO_CB(skb)->count = 1; |
2e71a6f8 | 5963 | NAPI_GRO_CB(skb)->age = jiffies; |
29e98242 | 5964 | NAPI_GRO_CB(skb)->last = skb; |
86911732 | 5965 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
07d78363 | 5966 | list_add(&skb->list, gro_head); |
5d0d9be8 | 5967 | ret = GRO_HELD; |
d565b0a1 | 5968 | |
ad0f9904 | 5969 | pull: |
a50e233c ED |
5970 | grow = skb_gro_offset(skb) - skb_headlen(skb); |
5971 | if (grow > 0) | |
5972 | gro_pull_from_frag0(skb, grow); | |
d565b0a1 | 5973 | ok: |
d9f37d01 LR |
5974 | if (napi->gro_hash[hash].count) { |
5975 | if (!test_bit(hash, &napi->gro_bitmask)) | |
5976 | __set_bit(hash, &napi->gro_bitmask); | |
5977 | } else if (test_bit(hash, &napi->gro_bitmask)) { | |
5978 | __clear_bit(hash, &napi->gro_bitmask); | |
5979 | } | |
5980 | ||
5d0d9be8 | 5981 | return ret; |
d565b0a1 HX |
5982 | |
5983 | normal: | |
ad0f9904 HX |
5984 | ret = GRO_NORMAL; |
5985 | goto pull; | |
5d38a079 | 5986 | } |
96e93eab | 5987 | |
bf5a755f JC |
5988 | struct packet_offload *gro_find_receive_by_type(__be16 type) |
5989 | { | |
5990 | struct list_head *offload_head = &offload_base; | |
5991 | struct packet_offload *ptype; | |
5992 | ||
5993 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
5994 | if (ptype->type != type || !ptype->callbacks.gro_receive) | |
5995 | continue; | |
5996 | return ptype; | |
5997 | } | |
5998 | return NULL; | |
5999 | } | |
e27a2f83 | 6000 | EXPORT_SYMBOL(gro_find_receive_by_type); |
bf5a755f JC |
6001 | |
6002 | struct packet_offload *gro_find_complete_by_type(__be16 type) | |
6003 | { | |
6004 | struct list_head *offload_head = &offload_base; | |
6005 | struct packet_offload *ptype; | |
6006 | ||
6007 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
6008 | if (ptype->type != type || !ptype->callbacks.gro_complete) | |
6009 | continue; | |
6010 | return ptype; | |
6011 | } | |
6012 | return NULL; | |
6013 | } | |
e27a2f83 | 6014 | EXPORT_SYMBOL(gro_find_complete_by_type); |
5d38a079 | 6015 | |
e44699d2 MK |
6016 | static void napi_skb_free_stolen_head(struct sk_buff *skb) |
6017 | { | |
6018 | skb_dst_drop(skb); | |
174e2381 | 6019 | skb_ext_put(skb); |
e44699d2 MK |
6020 | kmem_cache_free(skbuff_head_cache, skb); |
6021 | } | |
6022 | ||
6570bc79 AL |
6023 | static gro_result_t napi_skb_finish(struct napi_struct *napi, |
6024 | struct sk_buff *skb, | |
6025 | gro_result_t ret) | |
5d38a079 | 6026 | { |
5d0d9be8 HX |
6027 | switch (ret) { |
6028 | case GRO_NORMAL: | |
6570bc79 | 6029 | gro_normal_one(napi, skb); |
c7c4b3b6 | 6030 | break; |
5d38a079 | 6031 | |
5d0d9be8 | 6032 | case GRO_DROP: |
5d38a079 HX |
6033 | kfree_skb(skb); |
6034 | break; | |
5b252f0c | 6035 | |
daa86548 | 6036 | case GRO_MERGED_FREE: |
e44699d2 MK |
6037 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
6038 | napi_skb_free_stolen_head(skb); | |
6039 | else | |
d7e8883c | 6040 | __kfree_skb(skb); |
daa86548 ED |
6041 | break; |
6042 | ||
5b252f0c BH |
6043 | case GRO_HELD: |
6044 | case GRO_MERGED: | |
25393d3f | 6045 | case GRO_CONSUMED: |
5b252f0c | 6046 | break; |
5d38a079 HX |
6047 | } |
6048 | ||
c7c4b3b6 | 6049 | return ret; |
5d0d9be8 | 6050 | } |
5d0d9be8 | 6051 | |
c7c4b3b6 | 6052 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
5d0d9be8 | 6053 | { |
b0e3f1bd GB |
6054 | gro_result_t ret; |
6055 | ||
93f93a44 | 6056 | skb_mark_napi_id(skb, napi); |
ae78dbfa | 6057 | trace_napi_gro_receive_entry(skb); |
86911732 | 6058 | |
a50e233c ED |
6059 | skb_gro_reset_offset(skb); |
6060 | ||
6570bc79 | 6061 | ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb)); |
b0e3f1bd GB |
6062 | trace_napi_gro_receive_exit(ret); |
6063 | ||
6064 | return ret; | |
d565b0a1 HX |
6065 | } |
6066 | EXPORT_SYMBOL(napi_gro_receive); | |
6067 | ||
d0c2b0d2 | 6068 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
96e93eab | 6069 | { |
93a35f59 ED |
6070 | if (unlikely(skb->pfmemalloc)) { |
6071 | consume_skb(skb); | |
6072 | return; | |
6073 | } | |
96e93eab | 6074 | __skb_pull(skb, skb_headlen(skb)); |
2a2a459e ED |
6075 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
6076 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); | |
b1817524 | 6077 | __vlan_hwaccel_clear_tag(skb); |
66c46d74 | 6078 | skb->dev = napi->dev; |
6d152e23 | 6079 | skb->skb_iif = 0; |
33d9a2c7 ED |
6080 | |
6081 | /* eth_type_trans() assumes pkt_type is PACKET_HOST */ | |
6082 | skb->pkt_type = PACKET_HOST; | |
6083 | ||
c3caf119 JC |
6084 | skb->encapsulation = 0; |
6085 | skb_shinfo(skb)->gso_type = 0; | |
e33d0ba8 | 6086 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
174e2381 | 6087 | skb_ext_reset(skb); |
96e93eab HX |
6088 | |
6089 | napi->skb = skb; | |
6090 | } | |
96e93eab | 6091 | |
76620aaf | 6092 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
5d38a079 | 6093 | { |
5d38a079 | 6094 | struct sk_buff *skb = napi->skb; |
5d38a079 HX |
6095 | |
6096 | if (!skb) { | |
fd11a83d | 6097 | skb = napi_alloc_skb(napi, GRO_MAX_HEAD); |
e2f9dc3b ED |
6098 | if (skb) { |
6099 | napi->skb = skb; | |
6100 | skb_mark_napi_id(skb, napi); | |
6101 | } | |
80595d59 | 6102 | } |
96e93eab HX |
6103 | return skb; |
6104 | } | |
76620aaf | 6105 | EXPORT_SYMBOL(napi_get_frags); |
96e93eab | 6106 | |
a50e233c ED |
6107 | static gro_result_t napi_frags_finish(struct napi_struct *napi, |
6108 | struct sk_buff *skb, | |
6109 | gro_result_t ret) | |
96e93eab | 6110 | { |
5d0d9be8 HX |
6111 | switch (ret) { |
6112 | case GRO_NORMAL: | |
a50e233c ED |
6113 | case GRO_HELD: |
6114 | __skb_push(skb, ETH_HLEN); | |
6115 | skb->protocol = eth_type_trans(skb, skb->dev); | |
323ebb61 EC |
6116 | if (ret == GRO_NORMAL) |
6117 | gro_normal_one(napi, skb); | |
86911732 | 6118 | break; |
5d38a079 | 6119 | |
5d0d9be8 | 6120 | case GRO_DROP: |
5d0d9be8 HX |
6121 | napi_reuse_skb(napi, skb); |
6122 | break; | |
5b252f0c | 6123 | |
e44699d2 MK |
6124 | case GRO_MERGED_FREE: |
6125 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) | |
6126 | napi_skb_free_stolen_head(skb); | |
6127 | else | |
6128 | napi_reuse_skb(napi, skb); | |
6129 | break; | |
6130 | ||
5b252f0c | 6131 | case GRO_MERGED: |
25393d3f | 6132 | case GRO_CONSUMED: |
5b252f0c | 6133 | break; |
5d0d9be8 | 6134 | } |
5d38a079 | 6135 | |
c7c4b3b6 | 6136 | return ret; |
5d38a079 | 6137 | } |
5d0d9be8 | 6138 | |
a50e233c ED |
6139 | /* Upper GRO stack assumes network header starts at gro_offset=0 |
6140 | * Drivers could call both napi_gro_frags() and napi_gro_receive() | |
6141 | * We copy ethernet header into skb->data to have a common layout. | |
6142 | */ | |
4adb9c4a | 6143 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
76620aaf HX |
6144 | { |
6145 | struct sk_buff *skb = napi->skb; | |
a50e233c ED |
6146 | const struct ethhdr *eth; |
6147 | unsigned int hlen = sizeof(*eth); | |
76620aaf HX |
6148 | |
6149 | napi->skb = NULL; | |
6150 | ||
a50e233c ED |
6151 | skb_reset_mac_header(skb); |
6152 | skb_gro_reset_offset(skb); | |
6153 | ||
a50e233c ED |
6154 | if (unlikely(skb_gro_header_hard(skb, hlen))) { |
6155 | eth = skb_gro_header_slow(skb, hlen, 0); | |
6156 | if (unlikely(!eth)) { | |
4da46ceb AC |
6157 | net_warn_ratelimited("%s: dropping impossible skb from %s\n", |
6158 | __func__, napi->dev->name); | |
a50e233c ED |
6159 | napi_reuse_skb(napi, skb); |
6160 | return NULL; | |
6161 | } | |
6162 | } else { | |
a4270d67 | 6163 | eth = (const struct ethhdr *)skb->data; |
a50e233c ED |
6164 | gro_pull_from_frag0(skb, hlen); |
6165 | NAPI_GRO_CB(skb)->frag0 += hlen; | |
6166 | NAPI_GRO_CB(skb)->frag0_len -= hlen; | |
76620aaf | 6167 | } |
a50e233c ED |
6168 | __skb_pull(skb, hlen); |
6169 | ||
6170 | /* | |
6171 | * This works because the only protocols we care about don't require | |
6172 | * special handling. | |
6173 | * We'll fix it up properly in napi_frags_finish() | |
6174 | */ | |
6175 | skb->protocol = eth->h_proto; | |
76620aaf | 6176 | |
76620aaf HX |
6177 | return skb; |
6178 | } | |
76620aaf | 6179 | |
c7c4b3b6 | 6180 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
5d0d9be8 | 6181 | { |
b0e3f1bd | 6182 | gro_result_t ret; |
76620aaf | 6183 | struct sk_buff *skb = napi_frags_skb(napi); |
5d0d9be8 HX |
6184 | |
6185 | if (!skb) | |
c7c4b3b6 | 6186 | return GRO_DROP; |
5d0d9be8 | 6187 | |
ae78dbfa BH |
6188 | trace_napi_gro_frags_entry(skb); |
6189 | ||
b0e3f1bd GB |
6190 | ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
6191 | trace_napi_gro_frags_exit(ret); | |
6192 | ||
6193 | return ret; | |
5d0d9be8 | 6194 | } |
5d38a079 HX |
6195 | EXPORT_SYMBOL(napi_gro_frags); |
6196 | ||
573e8fca TH |
6197 | /* Compute the checksum from gro_offset and return the folded value |
6198 | * after adding in any pseudo checksum. | |
6199 | */ | |
6200 | __sum16 __skb_gro_checksum_complete(struct sk_buff *skb) | |
6201 | { | |
6202 | __wsum wsum; | |
6203 | __sum16 sum; | |
6204 | ||
6205 | wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0); | |
6206 | ||
6207 | /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */ | |
6208 | sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum)); | |
14641931 | 6209 | /* See comments in __skb_checksum_complete(). */ |
573e8fca TH |
6210 | if (likely(!sum)) { |
6211 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && | |
6212 | !skb->csum_complete_sw) | |
7fe50ac8 | 6213 | netdev_rx_csum_fault(skb->dev, skb); |
573e8fca TH |
6214 | } |
6215 | ||
6216 | NAPI_GRO_CB(skb)->csum = wsum; | |
6217 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
6218 | ||
6219 | return sum; | |
6220 | } | |
6221 | EXPORT_SYMBOL(__skb_gro_checksum_complete); | |
6222 | ||
773fc8f6 | 6223 | static void net_rps_send_ipi(struct softnet_data *remsd) |
6224 | { | |
6225 | #ifdef CONFIG_RPS | |
6226 | while (remsd) { | |
6227 | struct softnet_data *next = remsd->rps_ipi_next; | |
6228 | ||
6229 | if (cpu_online(remsd->cpu)) | |
6230 | smp_call_function_single_async(remsd->cpu, &remsd->csd); | |
6231 | remsd = next; | |
6232 | } | |
6233 | #endif | |
6234 | } | |
6235 | ||
e326bed2 | 6236 | /* |
855abcf0 | 6237 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
e326bed2 ED |
6238 | * Note: called with local irq disabled, but exits with local irq enabled. |
6239 | */ | |
6240 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
6241 | { | |
6242 | #ifdef CONFIG_RPS | |
6243 | struct softnet_data *remsd = sd->rps_ipi_list; | |
6244 | ||
6245 | if (remsd) { | |
6246 | sd->rps_ipi_list = NULL; | |
6247 | ||
6248 | local_irq_enable(); | |
6249 | ||
6250 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
773fc8f6 | 6251 | net_rps_send_ipi(remsd); |
e326bed2 ED |
6252 | } else |
6253 | #endif | |
6254 | local_irq_enable(); | |
6255 | } | |
6256 | ||
d75b1ade ED |
6257 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
6258 | { | |
6259 | #ifdef CONFIG_RPS | |
6260 | return sd->rps_ipi_list != NULL; | |
6261 | #else | |
6262 | return false; | |
6263 | #endif | |
6264 | } | |
6265 | ||
bea3348e | 6266 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 | 6267 | { |
eecfd7c4 | 6268 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
145dd5f9 PA |
6269 | bool again = true; |
6270 | int work = 0; | |
1da177e4 | 6271 | |
e326bed2 ED |
6272 | /* Check if we have pending ipi, its better to send them now, |
6273 | * not waiting net_rx_action() end. | |
6274 | */ | |
d75b1ade | 6275 | if (sd_has_rps_ipi_waiting(sd)) { |
e326bed2 ED |
6276 | local_irq_disable(); |
6277 | net_rps_action_and_irq_enable(sd); | |
6278 | } | |
d75b1ade | 6279 | |
3d48b53f | 6280 | napi->weight = dev_rx_weight; |
145dd5f9 | 6281 | while (again) { |
1da177e4 | 6282 | struct sk_buff *skb; |
6e7676c1 CG |
6283 | |
6284 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
2c17d27c | 6285 | rcu_read_lock(); |
6e7676c1 | 6286 | __netif_receive_skb(skb); |
2c17d27c | 6287 | rcu_read_unlock(); |
76cc8b13 | 6288 | input_queue_head_incr(sd); |
145dd5f9 | 6289 | if (++work >= quota) |
76cc8b13 | 6290 | return work; |
145dd5f9 | 6291 | |
6e7676c1 | 6292 | } |
1da177e4 | 6293 | |
145dd5f9 | 6294 | local_irq_disable(); |
e36fa2f7 | 6295 | rps_lock(sd); |
11ef7a89 | 6296 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
eecfd7c4 ED |
6297 | /* |
6298 | * Inline a custom version of __napi_complete(). | |
6299 | * only current cpu owns and manipulates this napi, | |
11ef7a89 TH |
6300 | * and NAPI_STATE_SCHED is the only possible flag set |
6301 | * on backlog. | |
6302 | * We can use a plain write instead of clear_bit(), | |
eecfd7c4 ED |
6303 | * and we dont need an smp_mb() memory barrier. |
6304 | */ | |
eecfd7c4 | 6305 | napi->state = 0; |
145dd5f9 PA |
6306 | again = false; |
6307 | } else { | |
6308 | skb_queue_splice_tail_init(&sd->input_pkt_queue, | |
6309 | &sd->process_queue); | |
bea3348e | 6310 | } |
e36fa2f7 | 6311 | rps_unlock(sd); |
145dd5f9 | 6312 | local_irq_enable(); |
6e7676c1 | 6313 | } |
1da177e4 | 6314 | |
bea3348e SH |
6315 | return work; |
6316 | } | |
1da177e4 | 6317 | |
bea3348e SH |
6318 | /** |
6319 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 6320 | * @n: entry to schedule |
bea3348e | 6321 | * |
bc9ad166 ED |
6322 | * The entry's receive function will be scheduled to run. |
6323 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. | |
bea3348e | 6324 | */ |
b5606c2d | 6325 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
6326 | { |
6327 | unsigned long flags; | |
1da177e4 | 6328 | |
bea3348e | 6329 | local_irq_save(flags); |
903ceff7 | 6330 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
bea3348e | 6331 | local_irq_restore(flags); |
1da177e4 | 6332 | } |
bea3348e SH |
6333 | EXPORT_SYMBOL(__napi_schedule); |
6334 | ||
39e6c820 ED |
6335 | /** |
6336 | * napi_schedule_prep - check if napi can be scheduled | |
6337 | * @n: napi context | |
6338 | * | |
6339 | * Test if NAPI routine is already running, and if not mark | |
ee1a4c84 | 6340 | * it as running. This is used as a condition variable to |
39e6c820 ED |
6341 | * insure only one NAPI poll instance runs. We also make |
6342 | * sure there is no pending NAPI disable. | |
6343 | */ | |
6344 | bool napi_schedule_prep(struct napi_struct *n) | |
6345 | { | |
6346 | unsigned long val, new; | |
6347 | ||
6348 | do { | |
6349 | val = READ_ONCE(n->state); | |
6350 | if (unlikely(val & NAPIF_STATE_DISABLE)) | |
6351 | return false; | |
6352 | new = val | NAPIF_STATE_SCHED; | |
6353 | ||
6354 | /* Sets STATE_MISSED bit if STATE_SCHED was already set | |
6355 | * This was suggested by Alexander Duyck, as compiler | |
6356 | * emits better code than : | |
6357 | * if (val & NAPIF_STATE_SCHED) | |
6358 | * new |= NAPIF_STATE_MISSED; | |
6359 | */ | |
6360 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * | |
6361 | NAPIF_STATE_MISSED; | |
6362 | } while (cmpxchg(&n->state, val, new) != val); | |
6363 | ||
6364 | return !(val & NAPIF_STATE_SCHED); | |
6365 | } | |
6366 | EXPORT_SYMBOL(napi_schedule_prep); | |
6367 | ||
bc9ad166 ED |
6368 | /** |
6369 | * __napi_schedule_irqoff - schedule for receive | |
6370 | * @n: entry to schedule | |
6371 | * | |
6372 | * Variant of __napi_schedule() assuming hard irqs are masked | |
6373 | */ | |
6374 | void __napi_schedule_irqoff(struct napi_struct *n) | |
6375 | { | |
6376 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); | |
6377 | } | |
6378 | EXPORT_SYMBOL(__napi_schedule_irqoff); | |
6379 | ||
364b6055 | 6380 | bool napi_complete_done(struct napi_struct *n, int work_done) |
d565b0a1 | 6381 | { |
6f8b12d6 ED |
6382 | unsigned long flags, val, new, timeout = 0; |
6383 | bool ret = true; | |
d565b0a1 HX |
6384 | |
6385 | /* | |
217f6974 ED |
6386 | * 1) Don't let napi dequeue from the cpu poll list |
6387 | * just in case its running on a different cpu. | |
6388 | * 2) If we are busy polling, do nothing here, we have | |
6389 | * the guarantee we will be called later. | |
d565b0a1 | 6390 | */ |
217f6974 ED |
6391 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
6392 | NAPIF_STATE_IN_BUSY_POLL))) | |
364b6055 | 6393 | return false; |
d565b0a1 | 6394 | |
6f8b12d6 ED |
6395 | if (work_done) { |
6396 | if (n->gro_bitmask) | |
7e417a66 ED |
6397 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6398 | n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs); | |
6f8b12d6 ED |
6399 | } |
6400 | if (n->defer_hard_irqs_count > 0) { | |
6401 | n->defer_hard_irqs_count--; | |
7e417a66 | 6402 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6f8b12d6 ED |
6403 | if (timeout) |
6404 | ret = false; | |
6405 | } | |
6406 | if (n->gro_bitmask) { | |
605108ac PA |
6407 | /* When the NAPI instance uses a timeout and keeps postponing |
6408 | * it, we need to bound somehow the time packets are kept in | |
6409 | * the GRO layer | |
6410 | */ | |
6411 | napi_gro_flush(n, !!timeout); | |
3b47d303 | 6412 | } |
c8079432 MM |
6413 | |
6414 | gro_normal_list(n); | |
6415 | ||
02c1602e | 6416 | if (unlikely(!list_empty(&n->poll_list))) { |
d75b1ade ED |
6417 | /* If n->poll_list is not empty, we need to mask irqs */ |
6418 | local_irq_save(flags); | |
02c1602e | 6419 | list_del_init(&n->poll_list); |
d75b1ade ED |
6420 | local_irq_restore(flags); |
6421 | } | |
39e6c820 ED |
6422 | |
6423 | do { | |
6424 | val = READ_ONCE(n->state); | |
6425 | ||
6426 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); | |
6427 | ||
6428 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED); | |
6429 | ||
6430 | /* If STATE_MISSED was set, leave STATE_SCHED set, | |
6431 | * because we will call napi->poll() one more time. | |
6432 | * This C code was suggested by Alexander Duyck to help gcc. | |
6433 | */ | |
6434 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * | |
6435 | NAPIF_STATE_SCHED; | |
6436 | } while (cmpxchg(&n->state, val, new) != val); | |
6437 | ||
6438 | if (unlikely(val & NAPIF_STATE_MISSED)) { | |
6439 | __napi_schedule(n); | |
6440 | return false; | |
6441 | } | |
6442 | ||
6f8b12d6 ED |
6443 | if (timeout) |
6444 | hrtimer_start(&n->timer, ns_to_ktime(timeout), | |
6445 | HRTIMER_MODE_REL_PINNED); | |
6446 | return ret; | |
d565b0a1 | 6447 | } |
3b47d303 | 6448 | EXPORT_SYMBOL(napi_complete_done); |
d565b0a1 | 6449 | |
af12fa6e | 6450 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
02d62e86 | 6451 | static struct napi_struct *napi_by_id(unsigned int napi_id) |
af12fa6e ET |
6452 | { |
6453 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); | |
6454 | struct napi_struct *napi; | |
6455 | ||
6456 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) | |
6457 | if (napi->napi_id == napi_id) | |
6458 | return napi; | |
6459 | ||
6460 | return NULL; | |
6461 | } | |
02d62e86 ED |
6462 | |
6463 | #if defined(CONFIG_NET_RX_BUSY_POLL) | |
217f6974 | 6464 | |
ce6aea93 | 6465 | #define BUSY_POLL_BUDGET 8 |
217f6974 ED |
6466 | |
6467 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock) | |
6468 | { | |
6469 | int rc; | |
6470 | ||
39e6c820 ED |
6471 | /* Busy polling means there is a high chance device driver hard irq |
6472 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was | |
6473 | * set in napi_schedule_prep(). | |
6474 | * Since we are about to call napi->poll() once more, we can safely | |
6475 | * clear NAPI_STATE_MISSED. | |
6476 | * | |
6477 | * Note: x86 could use a single "lock and ..." instruction | |
6478 | * to perform these two clear_bit() | |
6479 | */ | |
6480 | clear_bit(NAPI_STATE_MISSED, &napi->state); | |
217f6974 ED |
6481 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
6482 | ||
6483 | local_bh_disable(); | |
6484 | ||
6485 | /* All we really want here is to re-enable device interrupts. | |
6486 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. | |
6487 | */ | |
6488 | rc = napi->poll(napi, BUSY_POLL_BUDGET); | |
323ebb61 EC |
6489 | /* We can't gro_normal_list() here, because napi->poll() might have |
6490 | * rearmed the napi (napi_complete_done()) in which case it could | |
6491 | * already be running on another CPU. | |
6492 | */ | |
1e22391e | 6493 | trace_napi_poll(napi, rc, BUSY_POLL_BUDGET); |
217f6974 | 6494 | netpoll_poll_unlock(have_poll_lock); |
323ebb61 EC |
6495 | if (rc == BUSY_POLL_BUDGET) { |
6496 | /* As the whole budget was spent, we still own the napi so can | |
6497 | * safely handle the rx_list. | |
6498 | */ | |
6499 | gro_normal_list(napi); | |
217f6974 | 6500 | __napi_schedule(napi); |
323ebb61 | 6501 | } |
217f6974 | 6502 | local_bh_enable(); |
217f6974 ED |
6503 | } |
6504 | ||
7db6b048 SS |
6505 | void napi_busy_loop(unsigned int napi_id, |
6506 | bool (*loop_end)(void *, unsigned long), | |
6507 | void *loop_end_arg) | |
02d62e86 | 6508 | { |
7db6b048 | 6509 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
217f6974 | 6510 | int (*napi_poll)(struct napi_struct *napi, int budget); |
217f6974 | 6511 | void *have_poll_lock = NULL; |
02d62e86 | 6512 | struct napi_struct *napi; |
217f6974 ED |
6513 | |
6514 | restart: | |
217f6974 | 6515 | napi_poll = NULL; |
02d62e86 | 6516 | |
2a028ecb | 6517 | rcu_read_lock(); |
02d62e86 | 6518 | |
545cd5e5 | 6519 | napi = napi_by_id(napi_id); |
02d62e86 ED |
6520 | if (!napi) |
6521 | goto out; | |
6522 | ||
217f6974 ED |
6523 | preempt_disable(); |
6524 | for (;;) { | |
2b5cd0df AD |
6525 | int work = 0; |
6526 | ||
2a028ecb | 6527 | local_bh_disable(); |
217f6974 ED |
6528 | if (!napi_poll) { |
6529 | unsigned long val = READ_ONCE(napi->state); | |
6530 | ||
6531 | /* If multiple threads are competing for this napi, | |
6532 | * we avoid dirtying napi->state as much as we can. | |
6533 | */ | |
6534 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | | |
6535 | NAPIF_STATE_IN_BUSY_POLL)) | |
6536 | goto count; | |
6537 | if (cmpxchg(&napi->state, val, | |
6538 | val | NAPIF_STATE_IN_BUSY_POLL | | |
6539 | NAPIF_STATE_SCHED) != val) | |
6540 | goto count; | |
6541 | have_poll_lock = netpoll_poll_lock(napi); | |
6542 | napi_poll = napi->poll; | |
6543 | } | |
2b5cd0df AD |
6544 | work = napi_poll(napi, BUSY_POLL_BUDGET); |
6545 | trace_napi_poll(napi, work, BUSY_POLL_BUDGET); | |
323ebb61 | 6546 | gro_normal_list(napi); |
217f6974 | 6547 | count: |
2b5cd0df | 6548 | if (work > 0) |
7db6b048 | 6549 | __NET_ADD_STATS(dev_net(napi->dev), |
2b5cd0df | 6550 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
2a028ecb | 6551 | local_bh_enable(); |
02d62e86 | 6552 | |
7db6b048 | 6553 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
217f6974 | 6554 | break; |
02d62e86 | 6555 | |
217f6974 ED |
6556 | if (unlikely(need_resched())) { |
6557 | if (napi_poll) | |
6558 | busy_poll_stop(napi, have_poll_lock); | |
6559 | preempt_enable(); | |
6560 | rcu_read_unlock(); | |
6561 | cond_resched(); | |
7db6b048 | 6562 | if (loop_end(loop_end_arg, start_time)) |
2b5cd0df | 6563 | return; |
217f6974 ED |
6564 | goto restart; |
6565 | } | |
6cdf89b1 | 6566 | cpu_relax(); |
217f6974 ED |
6567 | } |
6568 | if (napi_poll) | |
6569 | busy_poll_stop(napi, have_poll_lock); | |
6570 | preempt_enable(); | |
02d62e86 | 6571 | out: |
2a028ecb | 6572 | rcu_read_unlock(); |
02d62e86 | 6573 | } |
7db6b048 | 6574 | EXPORT_SYMBOL(napi_busy_loop); |
02d62e86 ED |
6575 | |
6576 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
af12fa6e | 6577 | |
149d6ad8 | 6578 | static void napi_hash_add(struct napi_struct *napi) |
af12fa6e | 6579 | { |
4d092dd2 | 6580 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state)) |
52bd2d62 | 6581 | return; |
af12fa6e | 6582 | |
52bd2d62 | 6583 | spin_lock(&napi_hash_lock); |
af12fa6e | 6584 | |
545cd5e5 | 6585 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
52bd2d62 | 6586 | do { |
545cd5e5 AD |
6587 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
6588 | napi_gen_id = MIN_NAPI_ID; | |
52bd2d62 ED |
6589 | } while (napi_by_id(napi_gen_id)); |
6590 | napi->napi_id = napi_gen_id; | |
af12fa6e | 6591 | |
52bd2d62 ED |
6592 | hlist_add_head_rcu(&napi->napi_hash_node, |
6593 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); | |
af12fa6e | 6594 | |
52bd2d62 | 6595 | spin_unlock(&napi_hash_lock); |
af12fa6e | 6596 | } |
af12fa6e ET |
6597 | |
6598 | /* Warning : caller is responsible to make sure rcu grace period | |
6599 | * is respected before freeing memory containing @napi | |
6600 | */ | |
5198d545 | 6601 | static void napi_hash_del(struct napi_struct *napi) |
af12fa6e ET |
6602 | { |
6603 | spin_lock(&napi_hash_lock); | |
6604 | ||
4d092dd2 | 6605 | hlist_del_init_rcu(&napi->napi_hash_node); |
5198d545 | 6606 | |
af12fa6e ET |
6607 | spin_unlock(&napi_hash_lock); |
6608 | } | |
af12fa6e | 6609 | |
3b47d303 ED |
6610 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
6611 | { | |
6612 | struct napi_struct *napi; | |
6613 | ||
6614 | napi = container_of(timer, struct napi_struct, timer); | |
39e6c820 ED |
6615 | |
6616 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting | |
6617 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. | |
6618 | */ | |
6f8b12d6 | 6619 | if (!napi_disable_pending(napi) && |
39e6c820 ED |
6620 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) |
6621 | __napi_schedule_irqoff(napi); | |
3b47d303 ED |
6622 | |
6623 | return HRTIMER_NORESTART; | |
6624 | } | |
6625 | ||
7c4ec749 | 6626 | static void init_gro_hash(struct napi_struct *napi) |
d565b0a1 | 6627 | { |
07d78363 DM |
6628 | int i; |
6629 | ||
6312fe77 LR |
6630 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6631 | INIT_LIST_HEAD(&napi->gro_hash[i].list); | |
6632 | napi->gro_hash[i].count = 0; | |
6633 | } | |
7c4ec749 DM |
6634 | napi->gro_bitmask = 0; |
6635 | } | |
6636 | ||
6637 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, | |
6638 | int (*poll)(struct napi_struct *, int), int weight) | |
6639 | { | |
4d092dd2 JK |
6640 | if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state))) |
6641 | return; | |
6642 | ||
7c4ec749 | 6643 | INIT_LIST_HEAD(&napi->poll_list); |
4d092dd2 | 6644 | INIT_HLIST_NODE(&napi->napi_hash_node); |
7c4ec749 DM |
6645 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
6646 | napi->timer.function = napi_watchdog; | |
6647 | init_gro_hash(napi); | |
5d38a079 | 6648 | napi->skb = NULL; |
323ebb61 EC |
6649 | INIT_LIST_HEAD(&napi->rx_list); |
6650 | napi->rx_count = 0; | |
d565b0a1 | 6651 | napi->poll = poll; |
82dc3c63 | 6652 | if (weight > NAPI_POLL_WEIGHT) |
bf29e9e9 QC |
6653 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
6654 | weight); | |
d565b0a1 | 6655 | napi->weight = weight; |
d565b0a1 | 6656 | napi->dev = dev; |
5d38a079 | 6657 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
6658 | napi->poll_owner = -1; |
6659 | #endif | |
6660 | set_bit(NAPI_STATE_SCHED, &napi->state); | |
96e97bc0 JK |
6661 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
6662 | list_add_rcu(&napi->dev_list, &dev->napi_list); | |
93d05d4a | 6663 | napi_hash_add(napi); |
d565b0a1 HX |
6664 | } |
6665 | EXPORT_SYMBOL(netif_napi_add); | |
6666 | ||
3b47d303 ED |
6667 | void napi_disable(struct napi_struct *n) |
6668 | { | |
6669 | might_sleep(); | |
6670 | set_bit(NAPI_STATE_DISABLE, &n->state); | |
6671 | ||
6672 | while (test_and_set_bit(NAPI_STATE_SCHED, &n->state)) | |
6673 | msleep(1); | |
2d8bff12 NH |
6674 | while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state)) |
6675 | msleep(1); | |
3b47d303 ED |
6676 | |
6677 | hrtimer_cancel(&n->timer); | |
6678 | ||
6679 | clear_bit(NAPI_STATE_DISABLE, &n->state); | |
6680 | } | |
6681 | EXPORT_SYMBOL(napi_disable); | |
6682 | ||
07d78363 | 6683 | static void flush_gro_hash(struct napi_struct *napi) |
d4546c25 | 6684 | { |
07d78363 | 6685 | int i; |
d4546c25 | 6686 | |
07d78363 DM |
6687 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6688 | struct sk_buff *skb, *n; | |
6689 | ||
6312fe77 | 6690 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
07d78363 | 6691 | kfree_skb(skb); |
6312fe77 | 6692 | napi->gro_hash[i].count = 0; |
07d78363 | 6693 | } |
d4546c25 DM |
6694 | } |
6695 | ||
93d05d4a | 6696 | /* Must be called in process context */ |
5198d545 | 6697 | void __netif_napi_del(struct napi_struct *napi) |
d565b0a1 | 6698 | { |
4d092dd2 JK |
6699 | if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state)) |
6700 | return; | |
6701 | ||
5198d545 | 6702 | napi_hash_del(napi); |
5251ef82 | 6703 | list_del_rcu(&napi->dev_list); |
76620aaf | 6704 | napi_free_frags(napi); |
d565b0a1 | 6705 | |
07d78363 | 6706 | flush_gro_hash(napi); |
d9f37d01 | 6707 | napi->gro_bitmask = 0; |
d565b0a1 | 6708 | } |
5198d545 | 6709 | EXPORT_SYMBOL(__netif_napi_del); |
d565b0a1 | 6710 | |
726ce70e HX |
6711 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) |
6712 | { | |
6713 | void *have; | |
6714 | int work, weight; | |
6715 | ||
6716 | list_del_init(&n->poll_list); | |
6717 | ||
6718 | have = netpoll_poll_lock(n); | |
6719 | ||
6720 | weight = n->weight; | |
6721 | ||
6722 | /* This NAPI_STATE_SCHED test is for avoiding a race | |
6723 | * with netpoll's poll_napi(). Only the entity which | |
6724 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
6725 | * actually make the ->poll() call. Therefore we avoid | |
6726 | * accidentally calling ->poll() when NAPI is not scheduled. | |
6727 | */ | |
6728 | work = 0; | |
6729 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { | |
6730 | work = n->poll(n, weight); | |
1db19db7 | 6731 | trace_napi_poll(n, work, weight); |
726ce70e HX |
6732 | } |
6733 | ||
427d5838 ED |
6734 | if (unlikely(work > weight)) |
6735 | pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n", | |
6736 | n->poll, work, weight); | |
726ce70e HX |
6737 | |
6738 | if (likely(work < weight)) | |
6739 | goto out_unlock; | |
6740 | ||
6741 | /* Drivers must not modify the NAPI state if they | |
6742 | * consume the entire weight. In such cases this code | |
6743 | * still "owns" the NAPI instance and therefore can | |
6744 | * move the instance around on the list at-will. | |
6745 | */ | |
6746 | if (unlikely(napi_disable_pending(n))) { | |
6747 | napi_complete(n); | |
6748 | goto out_unlock; | |
6749 | } | |
6750 | ||
d9f37d01 | 6751 | if (n->gro_bitmask) { |
726ce70e HX |
6752 | /* flush too old packets |
6753 | * If HZ < 1000, flush all packets. | |
6754 | */ | |
6755 | napi_gro_flush(n, HZ >= 1000); | |
6756 | } | |
6757 | ||
c8079432 MM |
6758 | gro_normal_list(n); |
6759 | ||
001ce546 HX |
6760 | /* Some drivers may have called napi_schedule |
6761 | * prior to exhausting their budget. | |
6762 | */ | |
6763 | if (unlikely(!list_empty(&n->poll_list))) { | |
6764 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", | |
6765 | n->dev ? n->dev->name : "backlog"); | |
6766 | goto out_unlock; | |
6767 | } | |
6768 | ||
726ce70e HX |
6769 | list_add_tail(&n->poll_list, repoll); |
6770 | ||
6771 | out_unlock: | |
6772 | netpoll_poll_unlock(have); | |
6773 | ||
6774 | return work; | |
6775 | } | |
6776 | ||
0766f788 | 6777 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
1da177e4 | 6778 | { |
903ceff7 | 6779 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
7acf8a1e MW |
6780 | unsigned long time_limit = jiffies + |
6781 | usecs_to_jiffies(netdev_budget_usecs); | |
51b0bded | 6782 | int budget = netdev_budget; |
d75b1ade ED |
6783 | LIST_HEAD(list); |
6784 | LIST_HEAD(repoll); | |
53fb95d3 | 6785 | |
1da177e4 | 6786 | local_irq_disable(); |
d75b1ade ED |
6787 | list_splice_init(&sd->poll_list, &list); |
6788 | local_irq_enable(); | |
1da177e4 | 6789 | |
ceb8d5bf | 6790 | for (;;) { |
bea3348e | 6791 | struct napi_struct *n; |
1da177e4 | 6792 | |
ceb8d5bf HX |
6793 | if (list_empty(&list)) { |
6794 | if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll)) | |
f52dffe0 | 6795 | goto out; |
ceb8d5bf HX |
6796 | break; |
6797 | } | |
6798 | ||
6bd373eb HX |
6799 | n = list_first_entry(&list, struct napi_struct, poll_list); |
6800 | budget -= napi_poll(n, &repoll); | |
6801 | ||
d75b1ade | 6802 | /* If softirq window is exhausted then punt. |
24f8b238 SH |
6803 | * Allow this to run for 2 jiffies since which will allow |
6804 | * an average latency of 1.5/HZ. | |
bea3348e | 6805 | */ |
ceb8d5bf HX |
6806 | if (unlikely(budget <= 0 || |
6807 | time_after_eq(jiffies, time_limit))) { | |
6808 | sd->time_squeeze++; | |
6809 | break; | |
6810 | } | |
1da177e4 | 6811 | } |
d75b1ade | 6812 | |
d75b1ade ED |
6813 | local_irq_disable(); |
6814 | ||
6815 | list_splice_tail_init(&sd->poll_list, &list); | |
6816 | list_splice_tail(&repoll, &list); | |
6817 | list_splice(&list, &sd->poll_list); | |
6818 | if (!list_empty(&sd->poll_list)) | |
6819 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
6820 | ||
e326bed2 | 6821 | net_rps_action_and_irq_enable(sd); |
f52dffe0 ED |
6822 | out: |
6823 | __kfree_skb_flush(); | |
1da177e4 LT |
6824 | } |
6825 | ||
aa9d8560 | 6826 | struct netdev_adjacent { |
9ff162a8 | 6827 | struct net_device *dev; |
5d261913 VF |
6828 | |
6829 | /* upper master flag, there can only be one master device per list */ | |
9ff162a8 | 6830 | bool master; |
5d261913 | 6831 | |
32b6d34f TY |
6832 | /* lookup ignore flag */ |
6833 | bool ignore; | |
6834 | ||
5d261913 VF |
6835 | /* counter for the number of times this device was added to us */ |
6836 | u16 ref_nr; | |
6837 | ||
402dae96 VF |
6838 | /* private field for the users */ |
6839 | void *private; | |
6840 | ||
9ff162a8 JP |
6841 | struct list_head list; |
6842 | struct rcu_head rcu; | |
9ff162a8 JP |
6843 | }; |
6844 | ||
6ea29da1 | 6845 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
2f268f12 | 6846 | struct list_head *adj_list) |
9ff162a8 | 6847 | { |
5d261913 | 6848 | struct netdev_adjacent *adj; |
5d261913 | 6849 | |
2f268f12 | 6850 | list_for_each_entry(adj, adj_list, list) { |
5d261913 VF |
6851 | if (adj->dev == adj_dev) |
6852 | return adj; | |
9ff162a8 JP |
6853 | } |
6854 | return NULL; | |
6855 | } | |
6856 | ||
32b6d34f | 6857 | static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data) |
f1170fd4 DA |
6858 | { |
6859 | struct net_device *dev = data; | |
6860 | ||
6861 | return upper_dev == dev; | |
6862 | } | |
6863 | ||
9ff162a8 JP |
6864 | /** |
6865 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
6866 | * @dev: device | |
6867 | * @upper_dev: upper device to check | |
6868 | * | |
6869 | * Find out if a device is linked to specified upper device and return true | |
6870 | * in case it is. Note that this checks only immediate upper device, | |
6871 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
6872 | */ | |
6873 | bool netdev_has_upper_dev(struct net_device *dev, | |
6874 | struct net_device *upper_dev) | |
6875 | { | |
6876 | ASSERT_RTNL(); | |
6877 | ||
32b6d34f | 6878 | return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
f1170fd4 | 6879 | upper_dev); |
9ff162a8 JP |
6880 | } |
6881 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
6882 | ||
1a3f060c DA |
6883 | /** |
6884 | * netdev_has_upper_dev_all - Check if device is linked to an upper device | |
6885 | * @dev: device | |
6886 | * @upper_dev: upper device to check | |
6887 | * | |
6888 | * Find out if a device is linked to specified upper device and return true | |
6889 | * in case it is. Note that this checks the entire upper device chain. | |
6890 | * The caller must hold rcu lock. | |
6891 | */ | |
6892 | ||
1a3f060c DA |
6893 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
6894 | struct net_device *upper_dev) | |
6895 | { | |
32b6d34f | 6896 | return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
1a3f060c DA |
6897 | upper_dev); |
6898 | } | |
6899 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); | |
6900 | ||
9ff162a8 JP |
6901 | /** |
6902 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
6903 | * @dev: device | |
6904 | * | |
6905 | * Find out if a device is linked to an upper device and return true in case | |
6906 | * it is. The caller must hold the RTNL lock. | |
6907 | */ | |
25cc72a3 | 6908 | bool netdev_has_any_upper_dev(struct net_device *dev) |
9ff162a8 JP |
6909 | { |
6910 | ASSERT_RTNL(); | |
6911 | ||
f1170fd4 | 6912 | return !list_empty(&dev->adj_list.upper); |
9ff162a8 | 6913 | } |
25cc72a3 | 6914 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
9ff162a8 JP |
6915 | |
6916 | /** | |
6917 | * netdev_master_upper_dev_get - Get master upper device | |
6918 | * @dev: device | |
6919 | * | |
6920 | * Find a master upper device and return pointer to it or NULL in case | |
6921 | * it's not there. The caller must hold the RTNL lock. | |
6922 | */ | |
6923 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
6924 | { | |
aa9d8560 | 6925 | struct netdev_adjacent *upper; |
9ff162a8 JP |
6926 | |
6927 | ASSERT_RTNL(); | |
6928 | ||
2f268f12 | 6929 | if (list_empty(&dev->adj_list.upper)) |
9ff162a8 JP |
6930 | return NULL; |
6931 | ||
2f268f12 | 6932 | upper = list_first_entry(&dev->adj_list.upper, |
aa9d8560 | 6933 | struct netdev_adjacent, list); |
9ff162a8 JP |
6934 | if (likely(upper->master)) |
6935 | return upper->dev; | |
6936 | return NULL; | |
6937 | } | |
6938 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
6939 | ||
32b6d34f TY |
6940 | static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) |
6941 | { | |
6942 | struct netdev_adjacent *upper; | |
6943 | ||
6944 | ASSERT_RTNL(); | |
6945 | ||
6946 | if (list_empty(&dev->adj_list.upper)) | |
6947 | return NULL; | |
6948 | ||
6949 | upper = list_first_entry(&dev->adj_list.upper, | |
6950 | struct netdev_adjacent, list); | |
6951 | if (likely(upper->master) && !upper->ignore) | |
6952 | return upper->dev; | |
6953 | return NULL; | |
6954 | } | |
6955 | ||
0f524a80 DA |
6956 | /** |
6957 | * netdev_has_any_lower_dev - Check if device is linked to some device | |
6958 | * @dev: device | |
6959 | * | |
6960 | * Find out if a device is linked to a lower device and return true in case | |
6961 | * it is. The caller must hold the RTNL lock. | |
6962 | */ | |
6963 | static bool netdev_has_any_lower_dev(struct net_device *dev) | |
6964 | { | |
6965 | ASSERT_RTNL(); | |
6966 | ||
6967 | return !list_empty(&dev->adj_list.lower); | |
6968 | } | |
6969 | ||
b6ccba4c VF |
6970 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
6971 | { | |
6972 | struct netdev_adjacent *adj; | |
6973 | ||
6974 | adj = list_entry(adj_list, struct netdev_adjacent, list); | |
6975 | ||
6976 | return adj->private; | |
6977 | } | |
6978 | EXPORT_SYMBOL(netdev_adjacent_get_private); | |
6979 | ||
44a40855 VY |
6980 | /** |
6981 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list | |
6982 | * @dev: device | |
6983 | * @iter: list_head ** of the current position | |
6984 | * | |
6985 | * Gets the next device from the dev's upper list, starting from iter | |
6986 | * position. The caller must hold RCU read lock. | |
6987 | */ | |
6988 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, | |
6989 | struct list_head **iter) | |
6990 | { | |
6991 | struct netdev_adjacent *upper; | |
6992 | ||
6993 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6994 | ||
6995 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6996 | ||
6997 | if (&upper->list == &dev->adj_list.upper) | |
6998 | return NULL; | |
6999 | ||
7000 | *iter = &upper->list; | |
7001 | ||
7002 | return upper->dev; | |
7003 | } | |
7004 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); | |
7005 | ||
32b6d34f TY |
7006 | static struct net_device *__netdev_next_upper_dev(struct net_device *dev, |
7007 | struct list_head **iter, | |
7008 | bool *ignore) | |
5343da4c TY |
7009 | { |
7010 | struct netdev_adjacent *upper; | |
7011 | ||
7012 | upper = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7013 | ||
7014 | if (&upper->list == &dev->adj_list.upper) | |
7015 | return NULL; | |
7016 | ||
7017 | *iter = &upper->list; | |
32b6d34f | 7018 | *ignore = upper->ignore; |
5343da4c TY |
7019 | |
7020 | return upper->dev; | |
7021 | } | |
7022 | ||
1a3f060c DA |
7023 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
7024 | struct list_head **iter) | |
7025 | { | |
7026 | struct netdev_adjacent *upper; | |
7027 | ||
7028 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
7029 | ||
7030 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7031 | ||
7032 | if (&upper->list == &dev->adj_list.upper) | |
7033 | return NULL; | |
7034 | ||
7035 | *iter = &upper->list; | |
7036 | ||
7037 | return upper->dev; | |
7038 | } | |
7039 | ||
32b6d34f TY |
7040 | static int __netdev_walk_all_upper_dev(struct net_device *dev, |
7041 | int (*fn)(struct net_device *dev, | |
7042 | void *data), | |
7043 | void *data) | |
5343da4c TY |
7044 | { |
7045 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7046 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7047 | int ret, cur = 0; | |
32b6d34f | 7048 | bool ignore; |
5343da4c TY |
7049 | |
7050 | now = dev; | |
7051 | iter = &dev->adj_list.upper; | |
7052 | ||
7053 | while (1) { | |
7054 | if (now != dev) { | |
7055 | ret = fn(now, data); | |
7056 | if (ret) | |
7057 | return ret; | |
7058 | } | |
7059 | ||
7060 | next = NULL; | |
7061 | while (1) { | |
32b6d34f | 7062 | udev = __netdev_next_upper_dev(now, &iter, &ignore); |
5343da4c TY |
7063 | if (!udev) |
7064 | break; | |
32b6d34f TY |
7065 | if (ignore) |
7066 | continue; | |
5343da4c TY |
7067 | |
7068 | next = udev; | |
7069 | niter = &udev->adj_list.upper; | |
7070 | dev_stack[cur] = now; | |
7071 | iter_stack[cur++] = iter; | |
7072 | break; | |
7073 | } | |
7074 | ||
7075 | if (!next) { | |
7076 | if (!cur) | |
7077 | return 0; | |
7078 | next = dev_stack[--cur]; | |
7079 | niter = iter_stack[cur]; | |
7080 | } | |
7081 | ||
7082 | now = next; | |
7083 | iter = niter; | |
7084 | } | |
7085 | ||
7086 | return 0; | |
7087 | } | |
7088 | ||
1a3f060c DA |
7089 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, |
7090 | int (*fn)(struct net_device *dev, | |
7091 | void *data), | |
7092 | void *data) | |
7093 | { | |
5343da4c TY |
7094 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7095 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7096 | int ret, cur = 0; | |
1a3f060c | 7097 | |
5343da4c TY |
7098 | now = dev; |
7099 | iter = &dev->adj_list.upper; | |
1a3f060c | 7100 | |
5343da4c TY |
7101 | while (1) { |
7102 | if (now != dev) { | |
7103 | ret = fn(now, data); | |
7104 | if (ret) | |
7105 | return ret; | |
7106 | } | |
7107 | ||
7108 | next = NULL; | |
7109 | while (1) { | |
7110 | udev = netdev_next_upper_dev_rcu(now, &iter); | |
7111 | if (!udev) | |
7112 | break; | |
7113 | ||
7114 | next = udev; | |
7115 | niter = &udev->adj_list.upper; | |
7116 | dev_stack[cur] = now; | |
7117 | iter_stack[cur++] = iter; | |
7118 | break; | |
7119 | } | |
7120 | ||
7121 | if (!next) { | |
7122 | if (!cur) | |
7123 | return 0; | |
7124 | next = dev_stack[--cur]; | |
7125 | niter = iter_stack[cur]; | |
7126 | } | |
7127 | ||
7128 | now = next; | |
7129 | iter = niter; | |
1a3f060c DA |
7130 | } |
7131 | ||
7132 | return 0; | |
7133 | } | |
7134 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); | |
7135 | ||
32b6d34f TY |
7136 | static bool __netdev_has_upper_dev(struct net_device *dev, |
7137 | struct net_device *upper_dev) | |
7138 | { | |
7139 | ASSERT_RTNL(); | |
7140 | ||
7141 | return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, | |
7142 | upper_dev); | |
7143 | } | |
7144 | ||
31088a11 VF |
7145 | /** |
7146 | * netdev_lower_get_next_private - Get the next ->private from the | |
7147 | * lower neighbour list | |
7148 | * @dev: device | |
7149 | * @iter: list_head ** of the current position | |
7150 | * | |
7151 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7152 | * list, starting from iter position. The caller must hold either hold the | |
7153 | * RTNL lock or its own locking that guarantees that the neighbour lower | |
b469139e | 7154 | * list will remain unchanged. |
31088a11 VF |
7155 | */ |
7156 | void *netdev_lower_get_next_private(struct net_device *dev, | |
7157 | struct list_head **iter) | |
7158 | { | |
7159 | struct netdev_adjacent *lower; | |
7160 | ||
7161 | lower = list_entry(*iter, struct netdev_adjacent, list); | |
7162 | ||
7163 | if (&lower->list == &dev->adj_list.lower) | |
7164 | return NULL; | |
7165 | ||
6859e7df | 7166 | *iter = lower->list.next; |
31088a11 VF |
7167 | |
7168 | return lower->private; | |
7169 | } | |
7170 | EXPORT_SYMBOL(netdev_lower_get_next_private); | |
7171 | ||
7172 | /** | |
7173 | * netdev_lower_get_next_private_rcu - Get the next ->private from the | |
7174 | * lower neighbour list, RCU | |
7175 | * variant | |
7176 | * @dev: device | |
7177 | * @iter: list_head ** of the current position | |
7178 | * | |
7179 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7180 | * list, starting from iter position. The caller must hold RCU read lock. | |
7181 | */ | |
7182 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, | |
7183 | struct list_head **iter) | |
7184 | { | |
7185 | struct netdev_adjacent *lower; | |
7186 | ||
7187 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
7188 | ||
7189 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7190 | ||
7191 | if (&lower->list == &dev->adj_list.lower) | |
7192 | return NULL; | |
7193 | ||
6859e7df | 7194 | *iter = &lower->list; |
31088a11 VF |
7195 | |
7196 | return lower->private; | |
7197 | } | |
7198 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); | |
7199 | ||
4085ebe8 VY |
7200 | /** |
7201 | * netdev_lower_get_next - Get the next device from the lower neighbour | |
7202 | * list | |
7203 | * @dev: device | |
7204 | * @iter: list_head ** of the current position | |
7205 | * | |
7206 | * Gets the next netdev_adjacent from the dev's lower neighbour | |
7207 | * list, starting from iter position. The caller must hold RTNL lock or | |
7208 | * its own locking that guarantees that the neighbour lower | |
b469139e | 7209 | * list will remain unchanged. |
4085ebe8 VY |
7210 | */ |
7211 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) | |
7212 | { | |
7213 | struct netdev_adjacent *lower; | |
7214 | ||
cfdd28be | 7215 | lower = list_entry(*iter, struct netdev_adjacent, list); |
4085ebe8 VY |
7216 | |
7217 | if (&lower->list == &dev->adj_list.lower) | |
7218 | return NULL; | |
7219 | ||
cfdd28be | 7220 | *iter = lower->list.next; |
4085ebe8 VY |
7221 | |
7222 | return lower->dev; | |
7223 | } | |
7224 | EXPORT_SYMBOL(netdev_lower_get_next); | |
7225 | ||
1a3f060c DA |
7226 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
7227 | struct list_head **iter) | |
7228 | { | |
7229 | struct netdev_adjacent *lower; | |
7230 | ||
46b5ab1a | 7231 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
1a3f060c DA |
7232 | |
7233 | if (&lower->list == &dev->adj_list.lower) | |
7234 | return NULL; | |
7235 | ||
46b5ab1a | 7236 | *iter = &lower->list; |
1a3f060c DA |
7237 | |
7238 | return lower->dev; | |
7239 | } | |
7240 | ||
32b6d34f TY |
7241 | static struct net_device *__netdev_next_lower_dev(struct net_device *dev, |
7242 | struct list_head **iter, | |
7243 | bool *ignore) | |
7244 | { | |
7245 | struct netdev_adjacent *lower; | |
7246 | ||
7247 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7248 | ||
7249 | if (&lower->list == &dev->adj_list.lower) | |
7250 | return NULL; | |
7251 | ||
7252 | *iter = &lower->list; | |
7253 | *ignore = lower->ignore; | |
7254 | ||
7255 | return lower->dev; | |
7256 | } | |
7257 | ||
1a3f060c DA |
7258 | int netdev_walk_all_lower_dev(struct net_device *dev, |
7259 | int (*fn)(struct net_device *dev, | |
7260 | void *data), | |
7261 | void *data) | |
7262 | { | |
5343da4c TY |
7263 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7264 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7265 | int ret, cur = 0; | |
1a3f060c | 7266 | |
5343da4c TY |
7267 | now = dev; |
7268 | iter = &dev->adj_list.lower; | |
1a3f060c | 7269 | |
5343da4c TY |
7270 | while (1) { |
7271 | if (now != dev) { | |
7272 | ret = fn(now, data); | |
7273 | if (ret) | |
7274 | return ret; | |
7275 | } | |
7276 | ||
7277 | next = NULL; | |
7278 | while (1) { | |
7279 | ldev = netdev_next_lower_dev(now, &iter); | |
7280 | if (!ldev) | |
7281 | break; | |
7282 | ||
7283 | next = ldev; | |
7284 | niter = &ldev->adj_list.lower; | |
7285 | dev_stack[cur] = now; | |
7286 | iter_stack[cur++] = iter; | |
7287 | break; | |
7288 | } | |
7289 | ||
7290 | if (!next) { | |
7291 | if (!cur) | |
7292 | return 0; | |
7293 | next = dev_stack[--cur]; | |
7294 | niter = iter_stack[cur]; | |
7295 | } | |
7296 | ||
7297 | now = next; | |
7298 | iter = niter; | |
1a3f060c DA |
7299 | } |
7300 | ||
7301 | return 0; | |
7302 | } | |
7303 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); | |
7304 | ||
32b6d34f TY |
7305 | static int __netdev_walk_all_lower_dev(struct net_device *dev, |
7306 | int (*fn)(struct net_device *dev, | |
7307 | void *data), | |
7308 | void *data) | |
7309 | { | |
7310 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7311 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7312 | int ret, cur = 0; | |
7313 | bool ignore; | |
7314 | ||
7315 | now = dev; | |
7316 | iter = &dev->adj_list.lower; | |
7317 | ||
7318 | while (1) { | |
7319 | if (now != dev) { | |
7320 | ret = fn(now, data); | |
7321 | if (ret) | |
7322 | return ret; | |
7323 | } | |
7324 | ||
7325 | next = NULL; | |
7326 | while (1) { | |
7327 | ldev = __netdev_next_lower_dev(now, &iter, &ignore); | |
7328 | if (!ldev) | |
7329 | break; | |
7330 | if (ignore) | |
7331 | continue; | |
7332 | ||
7333 | next = ldev; | |
7334 | niter = &ldev->adj_list.lower; | |
7335 | dev_stack[cur] = now; | |
7336 | iter_stack[cur++] = iter; | |
7337 | break; | |
7338 | } | |
7339 | ||
7340 | if (!next) { | |
7341 | if (!cur) | |
7342 | return 0; | |
7343 | next = dev_stack[--cur]; | |
7344 | niter = iter_stack[cur]; | |
7345 | } | |
7346 | ||
7347 | now = next; | |
7348 | iter = niter; | |
7349 | } | |
7350 | ||
7351 | return 0; | |
7352 | } | |
7353 | ||
7151affe TY |
7354 | struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
7355 | struct list_head **iter) | |
1a3f060c DA |
7356 | { |
7357 | struct netdev_adjacent *lower; | |
7358 | ||
7359 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7360 | if (&lower->list == &dev->adj_list.lower) | |
7361 | return NULL; | |
7362 | ||
7363 | *iter = &lower->list; | |
7364 | ||
7365 | return lower->dev; | |
7366 | } | |
7151affe | 7367 | EXPORT_SYMBOL(netdev_next_lower_dev_rcu); |
1a3f060c | 7368 | |
5343da4c TY |
7369 | static u8 __netdev_upper_depth(struct net_device *dev) |
7370 | { | |
7371 | struct net_device *udev; | |
7372 | struct list_head *iter; | |
7373 | u8 max_depth = 0; | |
32b6d34f | 7374 | bool ignore; |
5343da4c TY |
7375 | |
7376 | for (iter = &dev->adj_list.upper, | |
32b6d34f | 7377 | udev = __netdev_next_upper_dev(dev, &iter, &ignore); |
5343da4c | 7378 | udev; |
32b6d34f TY |
7379 | udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { |
7380 | if (ignore) | |
7381 | continue; | |
5343da4c TY |
7382 | if (max_depth < udev->upper_level) |
7383 | max_depth = udev->upper_level; | |
7384 | } | |
7385 | ||
7386 | return max_depth; | |
7387 | } | |
7388 | ||
7389 | static u8 __netdev_lower_depth(struct net_device *dev) | |
1a3f060c DA |
7390 | { |
7391 | struct net_device *ldev; | |
7392 | struct list_head *iter; | |
5343da4c | 7393 | u8 max_depth = 0; |
32b6d34f | 7394 | bool ignore; |
1a3f060c DA |
7395 | |
7396 | for (iter = &dev->adj_list.lower, | |
32b6d34f | 7397 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore); |
1a3f060c | 7398 | ldev; |
32b6d34f TY |
7399 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { |
7400 | if (ignore) | |
7401 | continue; | |
5343da4c TY |
7402 | if (max_depth < ldev->lower_level) |
7403 | max_depth = ldev->lower_level; | |
7404 | } | |
1a3f060c | 7405 | |
5343da4c TY |
7406 | return max_depth; |
7407 | } | |
7408 | ||
7409 | static int __netdev_update_upper_level(struct net_device *dev, void *data) | |
7410 | { | |
7411 | dev->upper_level = __netdev_upper_depth(dev) + 1; | |
7412 | return 0; | |
7413 | } | |
7414 | ||
7415 | static int __netdev_update_lower_level(struct net_device *dev, void *data) | |
7416 | { | |
7417 | dev->lower_level = __netdev_lower_depth(dev) + 1; | |
7418 | return 0; | |
7419 | } | |
7420 | ||
7421 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, | |
7422 | int (*fn)(struct net_device *dev, | |
7423 | void *data), | |
7424 | void *data) | |
7425 | { | |
7426 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7427 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7428 | int ret, cur = 0; | |
7429 | ||
7430 | now = dev; | |
7431 | iter = &dev->adj_list.lower; | |
7432 | ||
7433 | while (1) { | |
7434 | if (now != dev) { | |
7435 | ret = fn(now, data); | |
7436 | if (ret) | |
7437 | return ret; | |
7438 | } | |
7439 | ||
7440 | next = NULL; | |
7441 | while (1) { | |
7442 | ldev = netdev_next_lower_dev_rcu(now, &iter); | |
7443 | if (!ldev) | |
7444 | break; | |
7445 | ||
7446 | next = ldev; | |
7447 | niter = &ldev->adj_list.lower; | |
7448 | dev_stack[cur] = now; | |
7449 | iter_stack[cur++] = iter; | |
7450 | break; | |
7451 | } | |
7452 | ||
7453 | if (!next) { | |
7454 | if (!cur) | |
7455 | return 0; | |
7456 | next = dev_stack[--cur]; | |
7457 | niter = iter_stack[cur]; | |
7458 | } | |
7459 | ||
7460 | now = next; | |
7461 | iter = niter; | |
1a3f060c DA |
7462 | } |
7463 | ||
7464 | return 0; | |
7465 | } | |
7466 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); | |
7467 | ||
e001bfad | 7468 | /** |
7469 | * netdev_lower_get_first_private_rcu - Get the first ->private from the | |
7470 | * lower neighbour list, RCU | |
7471 | * variant | |
7472 | * @dev: device | |
7473 | * | |
7474 | * Gets the first netdev_adjacent->private from the dev's lower neighbour | |
7475 | * list. The caller must hold RCU read lock. | |
7476 | */ | |
7477 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) | |
7478 | { | |
7479 | struct netdev_adjacent *lower; | |
7480 | ||
7481 | lower = list_first_or_null_rcu(&dev->adj_list.lower, | |
7482 | struct netdev_adjacent, list); | |
7483 | if (lower) | |
7484 | return lower->private; | |
7485 | return NULL; | |
7486 | } | |
7487 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); | |
7488 | ||
9ff162a8 JP |
7489 | /** |
7490 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
7491 | * @dev: device | |
7492 | * | |
7493 | * Find a master upper device and return pointer to it or NULL in case | |
7494 | * it's not there. The caller must hold the RCU read lock. | |
7495 | */ | |
7496 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
7497 | { | |
aa9d8560 | 7498 | struct netdev_adjacent *upper; |
9ff162a8 | 7499 | |
2f268f12 | 7500 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
aa9d8560 | 7501 | struct netdev_adjacent, list); |
9ff162a8 JP |
7502 | if (upper && likely(upper->master)) |
7503 | return upper->dev; | |
7504 | return NULL; | |
7505 | } | |
7506 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
7507 | ||
0a59f3a9 | 7508 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
3ee32707 VF |
7509 | struct net_device *adj_dev, |
7510 | struct list_head *dev_list) | |
7511 | { | |
7512 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7513 | |
3ee32707 VF |
7514 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7515 | "upper_%s" : "lower_%s", adj_dev->name); | |
7516 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), | |
7517 | linkname); | |
7518 | } | |
0a59f3a9 | 7519 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
3ee32707 VF |
7520 | char *name, |
7521 | struct list_head *dev_list) | |
7522 | { | |
7523 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7524 | |
3ee32707 VF |
7525 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7526 | "upper_%s" : "lower_%s", name); | |
7527 | sysfs_remove_link(&(dev->dev.kobj), linkname); | |
7528 | } | |
7529 | ||
7ce64c79 AF |
7530 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
7531 | struct net_device *adj_dev, | |
7532 | struct list_head *dev_list) | |
7533 | { | |
7534 | return (dev_list == &dev->adj_list.upper || | |
7535 | dev_list == &dev->adj_list.lower) && | |
7536 | net_eq(dev_net(dev), dev_net(adj_dev)); | |
7537 | } | |
3ee32707 | 7538 | |
5d261913 VF |
7539 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
7540 | struct net_device *adj_dev, | |
7863c054 | 7541 | struct list_head *dev_list, |
402dae96 | 7542 | void *private, bool master) |
5d261913 VF |
7543 | { |
7544 | struct netdev_adjacent *adj; | |
842d67a7 | 7545 | int ret; |
5d261913 | 7546 | |
6ea29da1 | 7547 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 VF |
7548 | |
7549 | if (adj) { | |
790510d9 | 7550 | adj->ref_nr += 1; |
67b62f98 DA |
7551 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
7552 | dev->name, adj_dev->name, adj->ref_nr); | |
7553 | ||
5d261913 VF |
7554 | return 0; |
7555 | } | |
7556 | ||
7557 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); | |
7558 | if (!adj) | |
7559 | return -ENOMEM; | |
7560 | ||
7561 | adj->dev = adj_dev; | |
7562 | adj->master = master; | |
790510d9 | 7563 | adj->ref_nr = 1; |
402dae96 | 7564 | adj->private = private; |
32b6d34f | 7565 | adj->ignore = false; |
5d261913 | 7566 | dev_hold(adj_dev); |
2f268f12 | 7567 | |
67b62f98 DA |
7568 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
7569 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); | |
5d261913 | 7570 | |
7ce64c79 | 7571 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
3ee32707 | 7572 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
5831d66e VF |
7573 | if (ret) |
7574 | goto free_adj; | |
7575 | } | |
7576 | ||
7863c054 | 7577 | /* Ensure that master link is always the first item in list. */ |
842d67a7 VF |
7578 | if (master) { |
7579 | ret = sysfs_create_link(&(dev->dev.kobj), | |
7580 | &(adj_dev->dev.kobj), "master"); | |
7581 | if (ret) | |
5831d66e | 7582 | goto remove_symlinks; |
842d67a7 | 7583 | |
7863c054 | 7584 | list_add_rcu(&adj->list, dev_list); |
842d67a7 | 7585 | } else { |
7863c054 | 7586 | list_add_tail_rcu(&adj->list, dev_list); |
842d67a7 | 7587 | } |
5d261913 VF |
7588 | |
7589 | return 0; | |
842d67a7 | 7590 | |
5831d66e | 7591 | remove_symlinks: |
7ce64c79 | 7592 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7593 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
842d67a7 VF |
7594 | free_adj: |
7595 | kfree(adj); | |
974daef7 | 7596 | dev_put(adj_dev); |
842d67a7 VF |
7597 | |
7598 | return ret; | |
5d261913 VF |
7599 | } |
7600 | ||
1d143d9f | 7601 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
7602 | struct net_device *adj_dev, | |
93409033 | 7603 | u16 ref_nr, |
1d143d9f | 7604 | struct list_head *dev_list) |
5d261913 VF |
7605 | { |
7606 | struct netdev_adjacent *adj; | |
7607 | ||
67b62f98 DA |
7608 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
7609 | dev->name, adj_dev->name, ref_nr); | |
7610 | ||
6ea29da1 | 7611 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 | 7612 | |
2f268f12 | 7613 | if (!adj) { |
67b62f98 | 7614 | pr_err("Adjacency does not exist for device %s from %s\n", |
2f268f12 | 7615 | dev->name, adj_dev->name); |
67b62f98 DA |
7616 | WARN_ON(1); |
7617 | return; | |
2f268f12 | 7618 | } |
5d261913 | 7619 | |
93409033 | 7620 | if (adj->ref_nr > ref_nr) { |
67b62f98 DA |
7621 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
7622 | dev->name, adj_dev->name, ref_nr, | |
7623 | adj->ref_nr - ref_nr); | |
93409033 | 7624 | adj->ref_nr -= ref_nr; |
5d261913 VF |
7625 | return; |
7626 | } | |
7627 | ||
842d67a7 VF |
7628 | if (adj->master) |
7629 | sysfs_remove_link(&(dev->dev.kobj), "master"); | |
7630 | ||
7ce64c79 | 7631 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7632 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
5831d66e | 7633 | |
5d261913 | 7634 | list_del_rcu(&adj->list); |
67b62f98 | 7635 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
2f268f12 | 7636 | adj_dev->name, dev->name, adj_dev->name); |
5d261913 VF |
7637 | dev_put(adj_dev); |
7638 | kfree_rcu(adj, rcu); | |
7639 | } | |
7640 | ||
1d143d9f | 7641 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
7642 | struct net_device *upper_dev, | |
7643 | struct list_head *up_list, | |
7644 | struct list_head *down_list, | |
7645 | void *private, bool master) | |
5d261913 VF |
7646 | { |
7647 | int ret; | |
7648 | ||
790510d9 | 7649 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
93409033 | 7650 | private, master); |
5d261913 VF |
7651 | if (ret) |
7652 | return ret; | |
7653 | ||
790510d9 | 7654 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
93409033 | 7655 | private, false); |
5d261913 | 7656 | if (ret) { |
790510d9 | 7657 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
5d261913 VF |
7658 | return ret; |
7659 | } | |
7660 | ||
7661 | return 0; | |
7662 | } | |
7663 | ||
1d143d9f | 7664 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
7665 | struct net_device *upper_dev, | |
93409033 | 7666 | u16 ref_nr, |
1d143d9f | 7667 | struct list_head *up_list, |
7668 | struct list_head *down_list) | |
5d261913 | 7669 | { |
93409033 AC |
7670 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
7671 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); | |
5d261913 VF |
7672 | } |
7673 | ||
1d143d9f | 7674 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
7675 | struct net_device *upper_dev, | |
7676 | void *private, bool master) | |
2f268f12 | 7677 | { |
f1170fd4 DA |
7678 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
7679 | &dev->adj_list.upper, | |
7680 | &upper_dev->adj_list.lower, | |
7681 | private, master); | |
5d261913 VF |
7682 | } |
7683 | ||
1d143d9f | 7684 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
7685 | struct net_device *upper_dev) | |
2f268f12 | 7686 | { |
93409033 | 7687 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
2f268f12 VF |
7688 | &dev->adj_list.upper, |
7689 | &upper_dev->adj_list.lower); | |
7690 | } | |
5d261913 | 7691 | |
9ff162a8 | 7692 | static int __netdev_upper_dev_link(struct net_device *dev, |
402dae96 | 7693 | struct net_device *upper_dev, bool master, |
42ab19ee DA |
7694 | void *upper_priv, void *upper_info, |
7695 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7696 | { |
51d0c047 DA |
7697 | struct netdev_notifier_changeupper_info changeupper_info = { |
7698 | .info = { | |
7699 | .dev = dev, | |
42ab19ee | 7700 | .extack = extack, |
51d0c047 DA |
7701 | }, |
7702 | .upper_dev = upper_dev, | |
7703 | .master = master, | |
7704 | .linking = true, | |
7705 | .upper_info = upper_info, | |
7706 | }; | |
50d629e7 | 7707 | struct net_device *master_dev; |
5d261913 | 7708 | int ret = 0; |
9ff162a8 JP |
7709 | |
7710 | ASSERT_RTNL(); | |
7711 | ||
7712 | if (dev == upper_dev) | |
7713 | return -EBUSY; | |
7714 | ||
7715 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
32b6d34f | 7716 | if (__netdev_has_upper_dev(upper_dev, dev)) |
9ff162a8 JP |
7717 | return -EBUSY; |
7718 | ||
5343da4c TY |
7719 | if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) |
7720 | return -EMLINK; | |
7721 | ||
50d629e7 | 7722 | if (!master) { |
32b6d34f | 7723 | if (__netdev_has_upper_dev(dev, upper_dev)) |
50d629e7 MM |
7724 | return -EEXIST; |
7725 | } else { | |
32b6d34f | 7726 | master_dev = __netdev_master_upper_dev_get(dev); |
50d629e7 MM |
7727 | if (master_dev) |
7728 | return master_dev == upper_dev ? -EEXIST : -EBUSY; | |
7729 | } | |
9ff162a8 | 7730 | |
51d0c047 | 7731 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7732 | &changeupper_info.info); |
7733 | ret = notifier_to_errno(ret); | |
7734 | if (ret) | |
7735 | return ret; | |
7736 | ||
6dffb044 | 7737 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
402dae96 | 7738 | master); |
5d261913 VF |
7739 | if (ret) |
7740 | return ret; | |
9ff162a8 | 7741 | |
51d0c047 | 7742 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
b03804e7 IS |
7743 | &changeupper_info.info); |
7744 | ret = notifier_to_errno(ret); | |
7745 | if (ret) | |
f1170fd4 | 7746 | goto rollback; |
b03804e7 | 7747 | |
5343da4c | 7748 | __netdev_update_upper_level(dev, NULL); |
32b6d34f | 7749 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c TY |
7750 | |
7751 | __netdev_update_lower_level(upper_dev, NULL); | |
32b6d34f TY |
7752 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
7753 | NULL); | |
5343da4c | 7754 | |
9ff162a8 | 7755 | return 0; |
5d261913 | 7756 | |
f1170fd4 | 7757 | rollback: |
2f268f12 | 7758 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 VF |
7759 | |
7760 | return ret; | |
9ff162a8 JP |
7761 | } |
7762 | ||
7763 | /** | |
7764 | * netdev_upper_dev_link - Add a link to the upper device | |
7765 | * @dev: device | |
7766 | * @upper_dev: new upper device | |
7a006d59 | 7767 | * @extack: netlink extended ack |
9ff162a8 JP |
7768 | * |
7769 | * Adds a link to device which is upper to this one. The caller must hold | |
7770 | * the RTNL lock. On a failure a negative errno code is returned. | |
7771 | * On success the reference counts are adjusted and the function | |
7772 | * returns zero. | |
7773 | */ | |
7774 | int netdev_upper_dev_link(struct net_device *dev, | |
42ab19ee DA |
7775 | struct net_device *upper_dev, |
7776 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7777 | { |
42ab19ee DA |
7778 | return __netdev_upper_dev_link(dev, upper_dev, false, |
7779 | NULL, NULL, extack); | |
9ff162a8 JP |
7780 | } |
7781 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
7782 | ||
7783 | /** | |
7784 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
7785 | * @dev: device | |
7786 | * @upper_dev: new upper device | |
6dffb044 | 7787 | * @upper_priv: upper device private |
29bf24af | 7788 | * @upper_info: upper info to be passed down via notifier |
7a006d59 | 7789 | * @extack: netlink extended ack |
9ff162a8 JP |
7790 | * |
7791 | * Adds a link to device which is upper to this one. In this case, only | |
7792 | * one master upper device can be linked, although other non-master devices | |
7793 | * might be linked as well. The caller must hold the RTNL lock. | |
7794 | * On a failure a negative errno code is returned. On success the reference | |
7795 | * counts are adjusted and the function returns zero. | |
7796 | */ | |
7797 | int netdev_master_upper_dev_link(struct net_device *dev, | |
6dffb044 | 7798 | struct net_device *upper_dev, |
42ab19ee DA |
7799 | void *upper_priv, void *upper_info, |
7800 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7801 | { |
29bf24af | 7802 | return __netdev_upper_dev_link(dev, upper_dev, true, |
42ab19ee | 7803 | upper_priv, upper_info, extack); |
9ff162a8 JP |
7804 | } |
7805 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
7806 | ||
7807 | /** | |
7808 | * netdev_upper_dev_unlink - Removes a link to upper device | |
7809 | * @dev: device | |
7810 | * @upper_dev: new upper device | |
7811 | * | |
7812 | * Removes a link to device which is upper to this one. The caller must hold | |
7813 | * the RTNL lock. | |
7814 | */ | |
7815 | void netdev_upper_dev_unlink(struct net_device *dev, | |
7816 | struct net_device *upper_dev) | |
7817 | { | |
51d0c047 DA |
7818 | struct netdev_notifier_changeupper_info changeupper_info = { |
7819 | .info = { | |
7820 | .dev = dev, | |
7821 | }, | |
7822 | .upper_dev = upper_dev, | |
7823 | .linking = false, | |
7824 | }; | |
f4563a75 | 7825 | |
9ff162a8 JP |
7826 | ASSERT_RTNL(); |
7827 | ||
0e4ead9d | 7828 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
0e4ead9d | 7829 | |
51d0c047 | 7830 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7831 | &changeupper_info.info); |
7832 | ||
2f268f12 | 7833 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 | 7834 | |
51d0c047 | 7835 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
0e4ead9d | 7836 | &changeupper_info.info); |
5343da4c TY |
7837 | |
7838 | __netdev_update_upper_level(dev, NULL); | |
32b6d34f | 7839 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c TY |
7840 | |
7841 | __netdev_update_lower_level(upper_dev, NULL); | |
32b6d34f TY |
7842 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
7843 | NULL); | |
9ff162a8 JP |
7844 | } |
7845 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
7846 | ||
32b6d34f TY |
7847 | static void __netdev_adjacent_dev_set(struct net_device *upper_dev, |
7848 | struct net_device *lower_dev, | |
7849 | bool val) | |
7850 | { | |
7851 | struct netdev_adjacent *adj; | |
7852 | ||
7853 | adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); | |
7854 | if (adj) | |
7855 | adj->ignore = val; | |
7856 | ||
7857 | adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); | |
7858 | if (adj) | |
7859 | adj->ignore = val; | |
7860 | } | |
7861 | ||
7862 | static void netdev_adjacent_dev_disable(struct net_device *upper_dev, | |
7863 | struct net_device *lower_dev) | |
7864 | { | |
7865 | __netdev_adjacent_dev_set(upper_dev, lower_dev, true); | |
7866 | } | |
7867 | ||
7868 | static void netdev_adjacent_dev_enable(struct net_device *upper_dev, | |
7869 | struct net_device *lower_dev) | |
7870 | { | |
7871 | __netdev_adjacent_dev_set(upper_dev, lower_dev, false); | |
7872 | } | |
7873 | ||
7874 | int netdev_adjacent_change_prepare(struct net_device *old_dev, | |
7875 | struct net_device *new_dev, | |
7876 | struct net_device *dev, | |
7877 | struct netlink_ext_ack *extack) | |
7878 | { | |
7879 | int err; | |
7880 | ||
7881 | if (!new_dev) | |
7882 | return 0; | |
7883 | ||
7884 | if (old_dev && new_dev != old_dev) | |
7885 | netdev_adjacent_dev_disable(dev, old_dev); | |
7886 | ||
7887 | err = netdev_upper_dev_link(new_dev, dev, extack); | |
7888 | if (err) { | |
7889 | if (old_dev && new_dev != old_dev) | |
7890 | netdev_adjacent_dev_enable(dev, old_dev); | |
7891 | return err; | |
7892 | } | |
7893 | ||
7894 | return 0; | |
7895 | } | |
7896 | EXPORT_SYMBOL(netdev_adjacent_change_prepare); | |
7897 | ||
7898 | void netdev_adjacent_change_commit(struct net_device *old_dev, | |
7899 | struct net_device *new_dev, | |
7900 | struct net_device *dev) | |
7901 | { | |
7902 | if (!new_dev || !old_dev) | |
7903 | return; | |
7904 | ||
7905 | if (new_dev == old_dev) | |
7906 | return; | |
7907 | ||
7908 | netdev_adjacent_dev_enable(dev, old_dev); | |
7909 | netdev_upper_dev_unlink(old_dev, dev); | |
7910 | } | |
7911 | EXPORT_SYMBOL(netdev_adjacent_change_commit); | |
7912 | ||
7913 | void netdev_adjacent_change_abort(struct net_device *old_dev, | |
7914 | struct net_device *new_dev, | |
7915 | struct net_device *dev) | |
7916 | { | |
7917 | if (!new_dev) | |
7918 | return; | |
7919 | ||
7920 | if (old_dev && new_dev != old_dev) | |
7921 | netdev_adjacent_dev_enable(dev, old_dev); | |
7922 | ||
7923 | netdev_upper_dev_unlink(new_dev, dev); | |
7924 | } | |
7925 | EXPORT_SYMBOL(netdev_adjacent_change_abort); | |
7926 | ||
61bd3857 MS |
7927 | /** |
7928 | * netdev_bonding_info_change - Dispatch event about slave change | |
7929 | * @dev: device | |
4a26e453 | 7930 | * @bonding_info: info to dispatch |
61bd3857 MS |
7931 | * |
7932 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. | |
7933 | * The caller must hold the RTNL lock. | |
7934 | */ | |
7935 | void netdev_bonding_info_change(struct net_device *dev, | |
7936 | struct netdev_bonding_info *bonding_info) | |
7937 | { | |
51d0c047 DA |
7938 | struct netdev_notifier_bonding_info info = { |
7939 | .info.dev = dev, | |
7940 | }; | |
61bd3857 MS |
7941 | |
7942 | memcpy(&info.bonding_info, bonding_info, | |
7943 | sizeof(struct netdev_bonding_info)); | |
51d0c047 | 7944 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
61bd3857 MS |
7945 | &info.info); |
7946 | } | |
7947 | EXPORT_SYMBOL(netdev_bonding_info_change); | |
7948 | ||
cff9f12b MG |
7949 | /** |
7950 | * netdev_get_xmit_slave - Get the xmit slave of master device | |
8842500d | 7951 | * @dev: device |
cff9f12b MG |
7952 | * @skb: The packet |
7953 | * @all_slaves: assume all the slaves are active | |
7954 | * | |
7955 | * The reference counters are not incremented so the caller must be | |
7956 | * careful with locks. The caller must hold RCU lock. | |
7957 | * %NULL is returned if no slave is found. | |
7958 | */ | |
7959 | ||
7960 | struct net_device *netdev_get_xmit_slave(struct net_device *dev, | |
7961 | struct sk_buff *skb, | |
7962 | bool all_slaves) | |
7963 | { | |
7964 | const struct net_device_ops *ops = dev->netdev_ops; | |
7965 | ||
7966 | if (!ops->ndo_get_xmit_slave) | |
7967 | return NULL; | |
7968 | return ops->ndo_get_xmit_slave(dev, skb, all_slaves); | |
7969 | } | |
7970 | EXPORT_SYMBOL(netdev_get_xmit_slave); | |
7971 | ||
2ce1ee17 | 7972 | static void netdev_adjacent_add_links(struct net_device *dev) |
4c75431a AF |
7973 | { |
7974 | struct netdev_adjacent *iter; | |
7975 | ||
7976 | struct net *net = dev_net(dev); | |
7977 | ||
7978 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 7979 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7980 | continue; |
7981 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7982 | &iter->dev->adj_list.lower); | |
7983 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
7984 | &dev->adj_list.upper); | |
7985 | } | |
7986 | ||
7987 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 7988 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7989 | continue; |
7990 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7991 | &iter->dev->adj_list.upper); | |
7992 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
7993 | &dev->adj_list.lower); | |
7994 | } | |
7995 | } | |
7996 | ||
2ce1ee17 | 7997 | static void netdev_adjacent_del_links(struct net_device *dev) |
4c75431a AF |
7998 | { |
7999 | struct netdev_adjacent *iter; | |
8000 | ||
8001 | struct net *net = dev_net(dev); | |
8002 | ||
8003 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8004 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8005 | continue; |
8006 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8007 | &iter->dev->adj_list.lower); | |
8008 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8009 | &dev->adj_list.upper); | |
8010 | } | |
8011 | ||
8012 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8013 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8014 | continue; |
8015 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8016 | &iter->dev->adj_list.upper); | |
8017 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8018 | &dev->adj_list.lower); | |
8019 | } | |
8020 | } | |
8021 | ||
5bb025fa | 8022 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
402dae96 | 8023 | { |
5bb025fa | 8024 | struct netdev_adjacent *iter; |
402dae96 | 8025 | |
4c75431a AF |
8026 | struct net *net = dev_net(dev); |
8027 | ||
5bb025fa | 8028 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
be4da0e3 | 8029 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8030 | continue; |
5bb025fa VF |
8031 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8032 | &iter->dev->adj_list.lower); | |
8033 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8034 | &iter->dev->adj_list.lower); | |
8035 | } | |
402dae96 | 8036 | |
5bb025fa | 8037 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
be4da0e3 | 8038 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8039 | continue; |
5bb025fa VF |
8040 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8041 | &iter->dev->adj_list.upper); | |
8042 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8043 | &iter->dev->adj_list.upper); | |
8044 | } | |
402dae96 | 8045 | } |
402dae96 VF |
8046 | |
8047 | void *netdev_lower_dev_get_private(struct net_device *dev, | |
8048 | struct net_device *lower_dev) | |
8049 | { | |
8050 | struct netdev_adjacent *lower; | |
8051 | ||
8052 | if (!lower_dev) | |
8053 | return NULL; | |
6ea29da1 | 8054 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
402dae96 VF |
8055 | if (!lower) |
8056 | return NULL; | |
8057 | ||
8058 | return lower->private; | |
8059 | } | |
8060 | EXPORT_SYMBOL(netdev_lower_dev_get_private); | |
8061 | ||
4085ebe8 | 8062 | |
04d48266 JP |
8063 | /** |
8064 | * netdev_lower_change - Dispatch event about lower device state change | |
8065 | * @lower_dev: device | |
8066 | * @lower_state_info: state to dispatch | |
8067 | * | |
8068 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. | |
8069 | * The caller must hold the RTNL lock. | |
8070 | */ | |
8071 | void netdev_lower_state_changed(struct net_device *lower_dev, | |
8072 | void *lower_state_info) | |
8073 | { | |
51d0c047 DA |
8074 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
8075 | .info.dev = lower_dev, | |
8076 | }; | |
04d48266 JP |
8077 | |
8078 | ASSERT_RTNL(); | |
8079 | changelowerstate_info.lower_state_info = lower_state_info; | |
51d0c047 | 8080 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
04d48266 JP |
8081 | &changelowerstate_info.info); |
8082 | } | |
8083 | EXPORT_SYMBOL(netdev_lower_state_changed); | |
8084 | ||
b6c40d68 PM |
8085 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
8086 | { | |
d314774c SH |
8087 | const struct net_device_ops *ops = dev->netdev_ops; |
8088 | ||
d2615bf4 | 8089 | if (ops->ndo_change_rx_flags) |
d314774c | 8090 | ops->ndo_change_rx_flags(dev, flags); |
b6c40d68 PM |
8091 | } |
8092 | ||
991fb3f7 | 8093 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8094 | { |
b536db93 | 8095 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
8096 | kuid_t uid; |
8097 | kgid_t gid; | |
1da177e4 | 8098 | |
24023451 PM |
8099 | ASSERT_RTNL(); |
8100 | ||
dad9b335 WC |
8101 | dev->flags |= IFF_PROMISC; |
8102 | dev->promiscuity += inc; | |
8103 | if (dev->promiscuity == 0) { | |
8104 | /* | |
8105 | * Avoid overflow. | |
8106 | * If inc causes overflow, untouch promisc and return error. | |
8107 | */ | |
8108 | if (inc < 0) | |
8109 | dev->flags &= ~IFF_PROMISC; | |
8110 | else { | |
8111 | dev->promiscuity -= inc; | |
7b6cd1ce JP |
8112 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
8113 | dev->name); | |
dad9b335 WC |
8114 | return -EOVERFLOW; |
8115 | } | |
8116 | } | |
52609c0b | 8117 | if (dev->flags != old_flags) { |
7b6cd1ce JP |
8118 | pr_info("device %s %s promiscuous mode\n", |
8119 | dev->name, | |
8120 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
8121 | if (audit_enabled) { |
8122 | current_uid_gid(&uid, &gid); | |
cdfb6b34 RGB |
8123 | audit_log(audit_context(), GFP_ATOMIC, |
8124 | AUDIT_ANOM_PROMISCUOUS, | |
8125 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
8126 | dev->name, (dev->flags & IFF_PROMISC), | |
8127 | (old_flags & IFF_PROMISC), | |
8128 | from_kuid(&init_user_ns, audit_get_loginuid(current)), | |
8129 | from_kuid(&init_user_ns, uid), | |
8130 | from_kgid(&init_user_ns, gid), | |
8131 | audit_get_sessionid(current)); | |
8192b0c4 | 8132 | } |
24023451 | 8133 | |
b6c40d68 | 8134 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 8135 | } |
991fb3f7 ND |
8136 | if (notify) |
8137 | __dev_notify_flags(dev, old_flags, IFF_PROMISC); | |
dad9b335 | 8138 | return 0; |
1da177e4 LT |
8139 | } |
8140 | ||
4417da66 PM |
8141 | /** |
8142 | * dev_set_promiscuity - update promiscuity count on a device | |
8143 | * @dev: device | |
8144 | * @inc: modifier | |
8145 | * | |
8146 | * Add or remove promiscuity from a device. While the count in the device | |
8147 | * remains above zero the interface remains promiscuous. Once it hits zero | |
8148 | * the device reverts back to normal filtering operation. A negative inc | |
8149 | * value is used to drop promiscuity on the device. | |
dad9b335 | 8150 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 8151 | */ |
dad9b335 | 8152 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 8153 | { |
b536db93 | 8154 | unsigned int old_flags = dev->flags; |
dad9b335 | 8155 | int err; |
4417da66 | 8156 | |
991fb3f7 | 8157 | err = __dev_set_promiscuity(dev, inc, true); |
4b5a698e | 8158 | if (err < 0) |
dad9b335 | 8159 | return err; |
4417da66 PM |
8160 | if (dev->flags != old_flags) |
8161 | dev_set_rx_mode(dev); | |
dad9b335 | 8162 | return err; |
4417da66 | 8163 | } |
d1b19dff | 8164 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 8165 | |
991fb3f7 | 8166 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8167 | { |
991fb3f7 | 8168 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
1da177e4 | 8169 | |
24023451 PM |
8170 | ASSERT_RTNL(); |
8171 | ||
1da177e4 | 8172 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
8173 | dev->allmulti += inc; |
8174 | if (dev->allmulti == 0) { | |
8175 | /* | |
8176 | * Avoid overflow. | |
8177 | * If inc causes overflow, untouch allmulti and return error. | |
8178 | */ | |
8179 | if (inc < 0) | |
8180 | dev->flags &= ~IFF_ALLMULTI; | |
8181 | else { | |
8182 | dev->allmulti -= inc; | |
7b6cd1ce JP |
8183 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
8184 | dev->name); | |
dad9b335 WC |
8185 | return -EOVERFLOW; |
8186 | } | |
8187 | } | |
24023451 | 8188 | if (dev->flags ^ old_flags) { |
b6c40d68 | 8189 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 8190 | dev_set_rx_mode(dev); |
991fb3f7 ND |
8191 | if (notify) |
8192 | __dev_notify_flags(dev, old_flags, | |
8193 | dev->gflags ^ old_gflags); | |
24023451 | 8194 | } |
dad9b335 | 8195 | return 0; |
4417da66 | 8196 | } |
991fb3f7 ND |
8197 | |
8198 | /** | |
8199 | * dev_set_allmulti - update allmulti count on a device | |
8200 | * @dev: device | |
8201 | * @inc: modifier | |
8202 | * | |
8203 | * Add or remove reception of all multicast frames to a device. While the | |
8204 | * count in the device remains above zero the interface remains listening | |
8205 | * to all interfaces. Once it hits zero the device reverts back to normal | |
8206 | * filtering operation. A negative @inc value is used to drop the counter | |
8207 | * when releasing a resource needing all multicasts. | |
8208 | * Return 0 if successful or a negative errno code on error. | |
8209 | */ | |
8210 | ||
8211 | int dev_set_allmulti(struct net_device *dev, int inc) | |
8212 | { | |
8213 | return __dev_set_allmulti(dev, inc, true); | |
8214 | } | |
d1b19dff | 8215 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
8216 | |
8217 | /* | |
8218 | * Upload unicast and multicast address lists to device and | |
8219 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 8220 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
8221 | * are present. |
8222 | */ | |
8223 | void __dev_set_rx_mode(struct net_device *dev) | |
8224 | { | |
d314774c SH |
8225 | const struct net_device_ops *ops = dev->netdev_ops; |
8226 | ||
4417da66 PM |
8227 | /* dev_open will call this function so the list will stay sane. */ |
8228 | if (!(dev->flags&IFF_UP)) | |
8229 | return; | |
8230 | ||
8231 | if (!netif_device_present(dev)) | |
40b77c94 | 8232 | return; |
4417da66 | 8233 | |
01789349 | 8234 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
8235 | /* Unicast addresses changes may only happen under the rtnl, |
8236 | * therefore calling __dev_set_promiscuity here is safe. | |
8237 | */ | |
32e7bfc4 | 8238 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
991fb3f7 | 8239 | __dev_set_promiscuity(dev, 1, false); |
2d348d1f | 8240 | dev->uc_promisc = true; |
32e7bfc4 | 8241 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
991fb3f7 | 8242 | __dev_set_promiscuity(dev, -1, false); |
2d348d1f | 8243 | dev->uc_promisc = false; |
4417da66 | 8244 | } |
4417da66 | 8245 | } |
01789349 JP |
8246 | |
8247 | if (ops->ndo_set_rx_mode) | |
8248 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
8249 | } |
8250 | ||
8251 | void dev_set_rx_mode(struct net_device *dev) | |
8252 | { | |
b9e40857 | 8253 | netif_addr_lock_bh(dev); |
4417da66 | 8254 | __dev_set_rx_mode(dev); |
b9e40857 | 8255 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
8256 | } |
8257 | ||
f0db275a SH |
8258 | /** |
8259 | * dev_get_flags - get flags reported to userspace | |
8260 | * @dev: device | |
8261 | * | |
8262 | * Get the combination of flag bits exported through APIs to userspace. | |
8263 | */ | |
95c96174 | 8264 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 8265 | { |
95c96174 | 8266 | unsigned int flags; |
1da177e4 LT |
8267 | |
8268 | flags = (dev->flags & ~(IFF_PROMISC | | |
8269 | IFF_ALLMULTI | | |
b00055aa SR |
8270 | IFF_RUNNING | |
8271 | IFF_LOWER_UP | | |
8272 | IFF_DORMANT)) | | |
1da177e4 LT |
8273 | (dev->gflags & (IFF_PROMISC | |
8274 | IFF_ALLMULTI)); | |
8275 | ||
b00055aa SR |
8276 | if (netif_running(dev)) { |
8277 | if (netif_oper_up(dev)) | |
8278 | flags |= IFF_RUNNING; | |
8279 | if (netif_carrier_ok(dev)) | |
8280 | flags |= IFF_LOWER_UP; | |
8281 | if (netif_dormant(dev)) | |
8282 | flags |= IFF_DORMANT; | |
8283 | } | |
1da177e4 LT |
8284 | |
8285 | return flags; | |
8286 | } | |
d1b19dff | 8287 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 8288 | |
6d040321 PM |
8289 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
8290 | struct netlink_ext_ack *extack) | |
1da177e4 | 8291 | { |
b536db93 | 8292 | unsigned int old_flags = dev->flags; |
bd380811 | 8293 | int ret; |
1da177e4 | 8294 | |
24023451 PM |
8295 | ASSERT_RTNL(); |
8296 | ||
1da177e4 LT |
8297 | /* |
8298 | * Set the flags on our device. | |
8299 | */ | |
8300 | ||
8301 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
8302 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
8303 | IFF_AUTOMEDIA)) | | |
8304 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
8305 | IFF_ALLMULTI)); | |
8306 | ||
8307 | /* | |
8308 | * Load in the correct multicast list now the flags have changed. | |
8309 | */ | |
8310 | ||
b6c40d68 PM |
8311 | if ((old_flags ^ flags) & IFF_MULTICAST) |
8312 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 8313 | |
4417da66 | 8314 | dev_set_rx_mode(dev); |
1da177e4 LT |
8315 | |
8316 | /* | |
8317 | * Have we downed the interface. We handle IFF_UP ourselves | |
8318 | * according to user attempts to set it, rather than blindly | |
8319 | * setting it. | |
8320 | */ | |
8321 | ||
8322 | ret = 0; | |
7051b88a | 8323 | if ((old_flags ^ flags) & IFF_UP) { |
8324 | if (old_flags & IFF_UP) | |
8325 | __dev_close(dev); | |
8326 | else | |
40c900aa | 8327 | ret = __dev_open(dev, extack); |
7051b88a | 8328 | } |
1da177e4 | 8329 | |
1da177e4 | 8330 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff | 8331 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
991fb3f7 | 8332 | unsigned int old_flags = dev->flags; |
d1b19dff | 8333 | |
1da177e4 | 8334 | dev->gflags ^= IFF_PROMISC; |
991fb3f7 ND |
8335 | |
8336 | if (__dev_set_promiscuity(dev, inc, false) >= 0) | |
8337 | if (dev->flags != old_flags) | |
8338 | dev_set_rx_mode(dev); | |
1da177e4 LT |
8339 | } |
8340 | ||
8341 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
eb13da1a | 8342 | * is important. Some (broken) drivers set IFF_PROMISC, when |
8343 | * IFF_ALLMULTI is requested not asking us and not reporting. | |
1da177e4 LT |
8344 | */ |
8345 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
8346 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
8347 | ||
1da177e4 | 8348 | dev->gflags ^= IFF_ALLMULTI; |
991fb3f7 | 8349 | __dev_set_allmulti(dev, inc, false); |
1da177e4 LT |
8350 | } |
8351 | ||
bd380811 PM |
8352 | return ret; |
8353 | } | |
8354 | ||
a528c219 ND |
8355 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
8356 | unsigned int gchanges) | |
bd380811 PM |
8357 | { |
8358 | unsigned int changes = dev->flags ^ old_flags; | |
8359 | ||
a528c219 | 8360 | if (gchanges) |
7f294054 | 8361 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC); |
a528c219 | 8362 | |
bd380811 PM |
8363 | if (changes & IFF_UP) { |
8364 | if (dev->flags & IFF_UP) | |
8365 | call_netdevice_notifiers(NETDEV_UP, dev); | |
8366 | else | |
8367 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
8368 | } | |
8369 | ||
8370 | if (dev->flags & IFF_UP && | |
be9efd36 | 8371 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
51d0c047 DA |
8372 | struct netdev_notifier_change_info change_info = { |
8373 | .info = { | |
8374 | .dev = dev, | |
8375 | }, | |
8376 | .flags_changed = changes, | |
8377 | }; | |
be9efd36 | 8378 | |
51d0c047 | 8379 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
be9efd36 | 8380 | } |
bd380811 PM |
8381 | } |
8382 | ||
8383 | /** | |
8384 | * dev_change_flags - change device settings | |
8385 | * @dev: device | |
8386 | * @flags: device state flags | |
567c5e13 | 8387 | * @extack: netlink extended ack |
bd380811 PM |
8388 | * |
8389 | * Change settings on device based state flags. The flags are | |
8390 | * in the userspace exported format. | |
8391 | */ | |
567c5e13 PM |
8392 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
8393 | struct netlink_ext_ack *extack) | |
bd380811 | 8394 | { |
b536db93 | 8395 | int ret; |
991fb3f7 | 8396 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
bd380811 | 8397 | |
6d040321 | 8398 | ret = __dev_change_flags(dev, flags, extack); |
bd380811 PM |
8399 | if (ret < 0) |
8400 | return ret; | |
8401 | ||
991fb3f7 | 8402 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
a528c219 | 8403 | __dev_notify_flags(dev, old_flags, changes); |
1da177e4 LT |
8404 | return ret; |
8405 | } | |
d1b19dff | 8406 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 8407 | |
f51048c3 | 8408 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
2315dc91 VF |
8409 | { |
8410 | const struct net_device_ops *ops = dev->netdev_ops; | |
8411 | ||
8412 | if (ops->ndo_change_mtu) | |
8413 | return ops->ndo_change_mtu(dev, new_mtu); | |
8414 | ||
501a90c9 ED |
8415 | /* Pairs with all the lockless reads of dev->mtu in the stack */ |
8416 | WRITE_ONCE(dev->mtu, new_mtu); | |
2315dc91 VF |
8417 | return 0; |
8418 | } | |
f51048c3 | 8419 | EXPORT_SYMBOL(__dev_set_mtu); |
2315dc91 | 8420 | |
d836f5c6 ED |
8421 | int dev_validate_mtu(struct net_device *dev, int new_mtu, |
8422 | struct netlink_ext_ack *extack) | |
8423 | { | |
8424 | /* MTU must be positive, and in range */ | |
8425 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { | |
8426 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); | |
8427 | return -EINVAL; | |
8428 | } | |
8429 | ||
8430 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { | |
8431 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); | |
8432 | return -EINVAL; | |
8433 | } | |
8434 | return 0; | |
8435 | } | |
8436 | ||
f0db275a | 8437 | /** |
7a4c53be | 8438 | * dev_set_mtu_ext - Change maximum transfer unit |
f0db275a SH |
8439 | * @dev: device |
8440 | * @new_mtu: new transfer unit | |
7a4c53be | 8441 | * @extack: netlink extended ack |
f0db275a SH |
8442 | * |
8443 | * Change the maximum transfer size of the network device. | |
8444 | */ | |
7a4c53be SH |
8445 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
8446 | struct netlink_ext_ack *extack) | |
1da177e4 | 8447 | { |
2315dc91 | 8448 | int err, orig_mtu; |
1da177e4 LT |
8449 | |
8450 | if (new_mtu == dev->mtu) | |
8451 | return 0; | |
8452 | ||
d836f5c6 ED |
8453 | err = dev_validate_mtu(dev, new_mtu, extack); |
8454 | if (err) | |
8455 | return err; | |
1da177e4 LT |
8456 | |
8457 | if (!netif_device_present(dev)) | |
8458 | return -ENODEV; | |
8459 | ||
1d486bfb VF |
8460 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
8461 | err = notifier_to_errno(err); | |
8462 | if (err) | |
8463 | return err; | |
d314774c | 8464 | |
2315dc91 VF |
8465 | orig_mtu = dev->mtu; |
8466 | err = __dev_set_mtu(dev, new_mtu); | |
d314774c | 8467 | |
2315dc91 | 8468 | if (!err) { |
af7d6cce SD |
8469 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8470 | orig_mtu); | |
2315dc91 VF |
8471 | err = notifier_to_errno(err); |
8472 | if (err) { | |
8473 | /* setting mtu back and notifying everyone again, | |
8474 | * so that they have a chance to revert changes. | |
8475 | */ | |
8476 | __dev_set_mtu(dev, orig_mtu); | |
af7d6cce SD |
8477 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8478 | new_mtu); | |
2315dc91 VF |
8479 | } |
8480 | } | |
1da177e4 LT |
8481 | return err; |
8482 | } | |
7a4c53be SH |
8483 | |
8484 | int dev_set_mtu(struct net_device *dev, int new_mtu) | |
8485 | { | |
8486 | struct netlink_ext_ack extack; | |
8487 | int err; | |
8488 | ||
a6bcfc89 | 8489 | memset(&extack, 0, sizeof(extack)); |
7a4c53be | 8490 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
a6bcfc89 | 8491 | if (err && extack._msg) |
7a4c53be SH |
8492 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
8493 | return err; | |
8494 | } | |
d1b19dff | 8495 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 8496 | |
6a643ddb CW |
8497 | /** |
8498 | * dev_change_tx_queue_len - Change TX queue length of a netdevice | |
8499 | * @dev: device | |
8500 | * @new_len: new tx queue length | |
8501 | */ | |
8502 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) | |
8503 | { | |
8504 | unsigned int orig_len = dev->tx_queue_len; | |
8505 | int res; | |
8506 | ||
8507 | if (new_len != (unsigned int)new_len) | |
8508 | return -ERANGE; | |
8509 | ||
8510 | if (new_len != orig_len) { | |
8511 | dev->tx_queue_len = new_len; | |
8512 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
8513 | res = notifier_to_errno(res); | |
7effaf06 TT |
8514 | if (res) |
8515 | goto err_rollback; | |
8516 | res = dev_qdisc_change_tx_queue_len(dev); | |
8517 | if (res) | |
8518 | goto err_rollback; | |
6a643ddb CW |
8519 | } |
8520 | ||
8521 | return 0; | |
7effaf06 TT |
8522 | |
8523 | err_rollback: | |
8524 | netdev_err(dev, "refused to change device tx_queue_len\n"); | |
8525 | dev->tx_queue_len = orig_len; | |
8526 | return res; | |
6a643ddb CW |
8527 | } |
8528 | ||
cbda10fa VD |
8529 | /** |
8530 | * dev_set_group - Change group this device belongs to | |
8531 | * @dev: device | |
8532 | * @new_group: group this device should belong to | |
8533 | */ | |
8534 | void dev_set_group(struct net_device *dev, int new_group) | |
8535 | { | |
8536 | dev->group = new_group; | |
8537 | } | |
8538 | EXPORT_SYMBOL(dev_set_group); | |
8539 | ||
d59cdf94 PM |
8540 | /** |
8541 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. | |
8542 | * @dev: device | |
8543 | * @addr: new address | |
8544 | * @extack: netlink extended ack | |
8545 | */ | |
8546 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, | |
8547 | struct netlink_ext_ack *extack) | |
8548 | { | |
8549 | struct netdev_notifier_pre_changeaddr_info info = { | |
8550 | .info.dev = dev, | |
8551 | .info.extack = extack, | |
8552 | .dev_addr = addr, | |
8553 | }; | |
8554 | int rc; | |
8555 | ||
8556 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); | |
8557 | return notifier_to_errno(rc); | |
8558 | } | |
8559 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); | |
8560 | ||
f0db275a SH |
8561 | /** |
8562 | * dev_set_mac_address - Change Media Access Control Address | |
8563 | * @dev: device | |
8564 | * @sa: new address | |
3a37a963 | 8565 | * @extack: netlink extended ack |
f0db275a SH |
8566 | * |
8567 | * Change the hardware (MAC) address of the device | |
8568 | */ | |
3a37a963 PM |
8569 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
8570 | struct netlink_ext_ack *extack) | |
1da177e4 | 8571 | { |
d314774c | 8572 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
8573 | int err; |
8574 | ||
d314774c | 8575 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
8576 | return -EOPNOTSUPP; |
8577 | if (sa->sa_family != dev->type) | |
8578 | return -EINVAL; | |
8579 | if (!netif_device_present(dev)) | |
8580 | return -ENODEV; | |
d59cdf94 PM |
8581 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
8582 | if (err) | |
8583 | return err; | |
d314774c | 8584 | err = ops->ndo_set_mac_address(dev, sa); |
f6521516 JP |
8585 | if (err) |
8586 | return err; | |
fbdeca2d | 8587 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 8588 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 8589 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 8590 | return 0; |
1da177e4 | 8591 | } |
d1b19dff | 8592 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 8593 | |
4bf84c35 JP |
8594 | /** |
8595 | * dev_change_carrier - Change device carrier | |
8596 | * @dev: device | |
691b3b7e | 8597 | * @new_carrier: new value |
4bf84c35 JP |
8598 | * |
8599 | * Change device carrier | |
8600 | */ | |
8601 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
8602 | { | |
8603 | const struct net_device_ops *ops = dev->netdev_ops; | |
8604 | ||
8605 | if (!ops->ndo_change_carrier) | |
8606 | return -EOPNOTSUPP; | |
8607 | if (!netif_device_present(dev)) | |
8608 | return -ENODEV; | |
8609 | return ops->ndo_change_carrier(dev, new_carrier); | |
8610 | } | |
8611 | EXPORT_SYMBOL(dev_change_carrier); | |
8612 | ||
66b52b0d JP |
8613 | /** |
8614 | * dev_get_phys_port_id - Get device physical port ID | |
8615 | * @dev: device | |
8616 | * @ppid: port ID | |
8617 | * | |
8618 | * Get device physical port ID | |
8619 | */ | |
8620 | int dev_get_phys_port_id(struct net_device *dev, | |
02637fce | 8621 | struct netdev_phys_item_id *ppid) |
66b52b0d JP |
8622 | { |
8623 | const struct net_device_ops *ops = dev->netdev_ops; | |
8624 | ||
8625 | if (!ops->ndo_get_phys_port_id) | |
8626 | return -EOPNOTSUPP; | |
8627 | return ops->ndo_get_phys_port_id(dev, ppid); | |
8628 | } | |
8629 | EXPORT_SYMBOL(dev_get_phys_port_id); | |
8630 | ||
db24a904 DA |
8631 | /** |
8632 | * dev_get_phys_port_name - Get device physical port name | |
8633 | * @dev: device | |
8634 | * @name: port name | |
ed49e650 | 8635 | * @len: limit of bytes to copy to name |
db24a904 DA |
8636 | * |
8637 | * Get device physical port name | |
8638 | */ | |
8639 | int dev_get_phys_port_name(struct net_device *dev, | |
8640 | char *name, size_t len) | |
8641 | { | |
8642 | const struct net_device_ops *ops = dev->netdev_ops; | |
af3836df | 8643 | int err; |
db24a904 | 8644 | |
af3836df JP |
8645 | if (ops->ndo_get_phys_port_name) { |
8646 | err = ops->ndo_get_phys_port_name(dev, name, len); | |
8647 | if (err != -EOPNOTSUPP) | |
8648 | return err; | |
8649 | } | |
8650 | return devlink_compat_phys_port_name_get(dev, name, len); | |
db24a904 DA |
8651 | } |
8652 | EXPORT_SYMBOL(dev_get_phys_port_name); | |
8653 | ||
d6abc596 FF |
8654 | /** |
8655 | * dev_get_port_parent_id - Get the device's port parent identifier | |
8656 | * @dev: network device | |
8657 | * @ppid: pointer to a storage for the port's parent identifier | |
8658 | * @recurse: allow/disallow recursion to lower devices | |
8659 | * | |
8660 | * Get the devices's port parent identifier | |
8661 | */ | |
8662 | int dev_get_port_parent_id(struct net_device *dev, | |
8663 | struct netdev_phys_item_id *ppid, | |
8664 | bool recurse) | |
8665 | { | |
8666 | const struct net_device_ops *ops = dev->netdev_ops; | |
8667 | struct netdev_phys_item_id first = { }; | |
8668 | struct net_device *lower_dev; | |
8669 | struct list_head *iter; | |
7e1146e8 JP |
8670 | int err; |
8671 | ||
8672 | if (ops->ndo_get_port_parent_id) { | |
8673 | err = ops->ndo_get_port_parent_id(dev, ppid); | |
8674 | if (err != -EOPNOTSUPP) | |
8675 | return err; | |
8676 | } | |
d6abc596 | 8677 | |
7e1146e8 JP |
8678 | err = devlink_compat_switch_id_get(dev, ppid); |
8679 | if (!err || err != -EOPNOTSUPP) | |
8680 | return err; | |
d6abc596 FF |
8681 | |
8682 | if (!recurse) | |
7e1146e8 | 8683 | return -EOPNOTSUPP; |
d6abc596 FF |
8684 | |
8685 | netdev_for_each_lower_dev(dev, lower_dev, iter) { | |
8686 | err = dev_get_port_parent_id(lower_dev, ppid, recurse); | |
8687 | if (err) | |
8688 | break; | |
8689 | if (!first.id_len) | |
8690 | first = *ppid; | |
8691 | else if (memcmp(&first, ppid, sizeof(*ppid))) | |
8692 | return -ENODATA; | |
8693 | } | |
8694 | ||
8695 | return err; | |
8696 | } | |
8697 | EXPORT_SYMBOL(dev_get_port_parent_id); | |
8698 | ||
8699 | /** | |
8700 | * netdev_port_same_parent_id - Indicate if two network devices have | |
8701 | * the same port parent identifier | |
8702 | * @a: first network device | |
8703 | * @b: second network device | |
8704 | */ | |
8705 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) | |
8706 | { | |
8707 | struct netdev_phys_item_id a_id = { }; | |
8708 | struct netdev_phys_item_id b_id = { }; | |
8709 | ||
8710 | if (dev_get_port_parent_id(a, &a_id, true) || | |
8711 | dev_get_port_parent_id(b, &b_id, true)) | |
8712 | return false; | |
8713 | ||
8714 | return netdev_phys_item_id_same(&a_id, &b_id); | |
8715 | } | |
8716 | EXPORT_SYMBOL(netdev_port_same_parent_id); | |
8717 | ||
d746d707 AK |
8718 | /** |
8719 | * dev_change_proto_down - update protocol port state information | |
8720 | * @dev: device | |
8721 | * @proto_down: new value | |
8722 | * | |
8723 | * This info can be used by switch drivers to set the phys state of the | |
8724 | * port. | |
8725 | */ | |
8726 | int dev_change_proto_down(struct net_device *dev, bool proto_down) | |
8727 | { | |
8728 | const struct net_device_ops *ops = dev->netdev_ops; | |
8729 | ||
8730 | if (!ops->ndo_change_proto_down) | |
8731 | return -EOPNOTSUPP; | |
8732 | if (!netif_device_present(dev)) | |
8733 | return -ENODEV; | |
8734 | return ops->ndo_change_proto_down(dev, proto_down); | |
8735 | } | |
8736 | EXPORT_SYMBOL(dev_change_proto_down); | |
8737 | ||
b5899679 AR |
8738 | /** |
8739 | * dev_change_proto_down_generic - generic implementation for | |
8740 | * ndo_change_proto_down that sets carrier according to | |
8741 | * proto_down. | |
8742 | * | |
8743 | * @dev: device | |
8744 | * @proto_down: new value | |
8745 | */ | |
8746 | int dev_change_proto_down_generic(struct net_device *dev, bool proto_down) | |
8747 | { | |
8748 | if (proto_down) | |
8749 | netif_carrier_off(dev); | |
8750 | else | |
8751 | netif_carrier_on(dev); | |
8752 | dev->proto_down = proto_down; | |
8753 | return 0; | |
8754 | } | |
8755 | EXPORT_SYMBOL(dev_change_proto_down_generic); | |
8756 | ||
829eb208 RP |
8757 | /** |
8758 | * dev_change_proto_down_reason - proto down reason | |
8759 | * | |
8760 | * @dev: device | |
8761 | * @mask: proto down mask | |
8762 | * @value: proto down value | |
8763 | */ | |
8764 | void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask, | |
8765 | u32 value) | |
8766 | { | |
8767 | int b; | |
8768 | ||
8769 | if (!mask) { | |
8770 | dev->proto_down_reason = value; | |
8771 | } else { | |
8772 | for_each_set_bit(b, &mask, 32) { | |
8773 | if (value & (1 << b)) | |
8774 | dev->proto_down_reason |= BIT(b); | |
8775 | else | |
8776 | dev->proto_down_reason &= ~BIT(b); | |
8777 | } | |
8778 | } | |
8779 | } | |
8780 | EXPORT_SYMBOL(dev_change_proto_down_reason); | |
8781 | ||
aa8d3a71 AN |
8782 | struct bpf_xdp_link { |
8783 | struct bpf_link link; | |
8784 | struct net_device *dev; /* protected by rtnl_lock, no refcnt held */ | |
8785 | int flags; | |
8786 | }; | |
8787 | ||
c8a36f19 | 8788 | static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags) |
d67b9cd2 | 8789 | { |
7f0a8382 AN |
8790 | if (flags & XDP_FLAGS_HW_MODE) |
8791 | return XDP_MODE_HW; | |
8792 | if (flags & XDP_FLAGS_DRV_MODE) | |
8793 | return XDP_MODE_DRV; | |
c8a36f19 AN |
8794 | if (flags & XDP_FLAGS_SKB_MODE) |
8795 | return XDP_MODE_SKB; | |
8796 | return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB; | |
7f0a8382 | 8797 | } |
d67b9cd2 | 8798 | |
7f0a8382 AN |
8799 | static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode) |
8800 | { | |
8801 | switch (mode) { | |
8802 | case XDP_MODE_SKB: | |
8803 | return generic_xdp_install; | |
8804 | case XDP_MODE_DRV: | |
8805 | case XDP_MODE_HW: | |
8806 | return dev->netdev_ops->ndo_bpf; | |
8807 | default: | |
8808 | return NULL; | |
8809 | }; | |
8810 | } | |
118b4aa2 | 8811 | |
aa8d3a71 AN |
8812 | static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev, |
8813 | enum bpf_xdp_mode mode) | |
8814 | { | |
8815 | return dev->xdp_state[mode].link; | |
8816 | } | |
8817 | ||
7f0a8382 AN |
8818 | static struct bpf_prog *dev_xdp_prog(struct net_device *dev, |
8819 | enum bpf_xdp_mode mode) | |
8820 | { | |
aa8d3a71 AN |
8821 | struct bpf_xdp_link *link = dev_xdp_link(dev, mode); |
8822 | ||
8823 | if (link) | |
8824 | return link->link.prog; | |
7f0a8382 AN |
8825 | return dev->xdp_state[mode].prog; |
8826 | } | |
8827 | ||
8828 | u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode) | |
8829 | { | |
8830 | struct bpf_prog *prog = dev_xdp_prog(dev, mode); | |
118b4aa2 | 8831 | |
7f0a8382 AN |
8832 | return prog ? prog->aux->id : 0; |
8833 | } | |
58038695 | 8834 | |
aa8d3a71 AN |
8835 | static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode, |
8836 | struct bpf_xdp_link *link) | |
8837 | { | |
8838 | dev->xdp_state[mode].link = link; | |
8839 | dev->xdp_state[mode].prog = NULL; | |
d67b9cd2 DB |
8840 | } |
8841 | ||
7f0a8382 AN |
8842 | static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode, |
8843 | struct bpf_prog *prog) | |
8844 | { | |
aa8d3a71 | 8845 | dev->xdp_state[mode].link = NULL; |
7f0a8382 | 8846 | dev->xdp_state[mode].prog = prog; |
d67b9cd2 DB |
8847 | } |
8848 | ||
7f0a8382 AN |
8849 | static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, |
8850 | bpf_op_t bpf_op, struct netlink_ext_ack *extack, | |
8851 | u32 flags, struct bpf_prog *prog) | |
d67b9cd2 | 8852 | { |
f4e63525 | 8853 | struct netdev_bpf xdp; |
7e6897f9 BT |
8854 | int err; |
8855 | ||
d67b9cd2 | 8856 | memset(&xdp, 0, sizeof(xdp)); |
7f0a8382 | 8857 | xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG; |
d67b9cd2 | 8858 | xdp.extack = extack; |
32d60277 | 8859 | xdp.flags = flags; |
d67b9cd2 DB |
8860 | xdp.prog = prog; |
8861 | ||
7f0a8382 AN |
8862 | /* Drivers assume refcnt is already incremented (i.e, prog pointer is |
8863 | * "moved" into driver), so they don't increment it on their own, but | |
8864 | * they do decrement refcnt when program is detached or replaced. | |
8865 | * Given net_device also owns link/prog, we need to bump refcnt here | |
8866 | * to prevent drivers from underflowing it. | |
8867 | */ | |
8868 | if (prog) | |
8869 | bpf_prog_inc(prog); | |
7e6897f9 | 8870 | err = bpf_op(dev, &xdp); |
7f0a8382 AN |
8871 | if (err) { |
8872 | if (prog) | |
8873 | bpf_prog_put(prog); | |
8874 | return err; | |
8875 | } | |
7e6897f9 | 8876 | |
7f0a8382 AN |
8877 | if (mode != XDP_MODE_HW) |
8878 | bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog); | |
7e6897f9 | 8879 | |
7f0a8382 | 8880 | return 0; |
d67b9cd2 DB |
8881 | } |
8882 | ||
bd0b2e7f JK |
8883 | static void dev_xdp_uninstall(struct net_device *dev) |
8884 | { | |
aa8d3a71 | 8885 | struct bpf_xdp_link *link; |
7f0a8382 AN |
8886 | struct bpf_prog *prog; |
8887 | enum bpf_xdp_mode mode; | |
8888 | bpf_op_t bpf_op; | |
bd0b2e7f | 8889 | |
7f0a8382 | 8890 | ASSERT_RTNL(); |
bd0b2e7f | 8891 | |
7f0a8382 AN |
8892 | for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) { |
8893 | prog = dev_xdp_prog(dev, mode); | |
8894 | if (!prog) | |
8895 | continue; | |
bd0b2e7f | 8896 | |
7f0a8382 AN |
8897 | bpf_op = dev_xdp_bpf_op(dev, mode); |
8898 | if (!bpf_op) | |
8899 | continue; | |
bd0b2e7f | 8900 | |
7f0a8382 AN |
8901 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); |
8902 | ||
aa8d3a71 AN |
8903 | /* auto-detach link from net device */ |
8904 | link = dev_xdp_link(dev, mode); | |
8905 | if (link) | |
8906 | link->dev = NULL; | |
8907 | else | |
8908 | bpf_prog_put(prog); | |
8909 | ||
8910 | dev_xdp_set_link(dev, mode, NULL); | |
7f0a8382 | 8911 | } |
bd0b2e7f JK |
8912 | } |
8913 | ||
d4baa936 | 8914 | static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack, |
aa8d3a71 AN |
8915 | struct bpf_xdp_link *link, struct bpf_prog *new_prog, |
8916 | struct bpf_prog *old_prog, u32 flags) | |
a7862b45 | 8917 | { |
d4baa936 AN |
8918 | struct bpf_prog *cur_prog; |
8919 | enum bpf_xdp_mode mode; | |
7f0a8382 | 8920 | bpf_op_t bpf_op; |
a7862b45 BB |
8921 | int err; |
8922 | ||
85de8576 DB |
8923 | ASSERT_RTNL(); |
8924 | ||
aa8d3a71 AN |
8925 | /* either link or prog attachment, never both */ |
8926 | if (link && (new_prog || old_prog)) | |
8927 | return -EINVAL; | |
8928 | /* link supports only XDP mode flags */ | |
8929 | if (link && (flags & ~XDP_FLAGS_MODES)) { | |
8930 | NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment"); | |
8931 | return -EINVAL; | |
8932 | } | |
d4baa936 AN |
8933 | /* just one XDP mode bit should be set, zero defaults to SKB mode */ |
8934 | if (hweight32(flags & XDP_FLAGS_MODES) > 1) { | |
8935 | NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set"); | |
8936 | return -EINVAL; | |
8937 | } | |
8938 | /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */ | |
8939 | if (old_prog && !(flags & XDP_FLAGS_REPLACE)) { | |
8940 | NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified"); | |
8941 | return -EINVAL; | |
01dde20c | 8942 | } |
a25717d2 | 8943 | |
c8a36f19 | 8944 | mode = dev_xdp_mode(dev, flags); |
aa8d3a71 AN |
8945 | /* can't replace attached link */ |
8946 | if (dev_xdp_link(dev, mode)) { | |
8947 | NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link"); | |
8948 | return -EBUSY; | |
01dde20c | 8949 | } |
c14a9f63 | 8950 | |
d4baa936 | 8951 | cur_prog = dev_xdp_prog(dev, mode); |
aa8d3a71 AN |
8952 | /* can't replace attached prog with link */ |
8953 | if (link && cur_prog) { | |
8954 | NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link"); | |
8955 | return -EBUSY; | |
8956 | } | |
d4baa936 AN |
8957 | if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) { |
8958 | NL_SET_ERR_MSG(extack, "Active program does not match expected"); | |
8959 | return -EEXIST; | |
92234c8f | 8960 | } |
c14a9f63 | 8961 | |
aa8d3a71 AN |
8962 | /* put effective new program into new_prog */ |
8963 | if (link) | |
8964 | new_prog = link->link.prog; | |
85de8576 | 8965 | |
d4baa936 AN |
8966 | if (new_prog) { |
8967 | bool offload = mode == XDP_MODE_HW; | |
7f0a8382 AN |
8968 | enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB |
8969 | ? XDP_MODE_DRV : XDP_MODE_SKB; | |
441a3303 | 8970 | |
068d9d1e AN |
8971 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) { |
8972 | NL_SET_ERR_MSG(extack, "XDP program already attached"); | |
8973 | return -EBUSY; | |
8974 | } | |
d4baa936 | 8975 | if (!offload && dev_xdp_prog(dev, other_mode)) { |
7f0a8382 | 8976 | NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time"); |
d67b9cd2 | 8977 | return -EEXIST; |
01dde20c | 8978 | } |
d4baa936 | 8979 | if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) { |
7f0a8382 | 8980 | NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported"); |
441a3303 JK |
8981 | return -EINVAL; |
8982 | } | |
d4baa936 | 8983 | if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) { |
fbee97fe | 8984 | NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device"); |
fbee97fe DA |
8985 | return -EINVAL; |
8986 | } | |
d4baa936 AN |
8987 | if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) { |
8988 | NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device"); | |
92164774 LB |
8989 | return -EINVAL; |
8990 | } | |
d4baa936 | 8991 | } |
92164774 | 8992 | |
d4baa936 AN |
8993 | /* don't call drivers if the effective program didn't change */ |
8994 | if (new_prog != cur_prog) { | |
8995 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
8996 | if (!bpf_op) { | |
8997 | NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode"); | |
8998 | return -EOPNOTSUPP; | |
c14a9f63 | 8999 | } |
a7862b45 | 9000 | |
d4baa936 AN |
9001 | err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog); |
9002 | if (err) | |
9003 | return err; | |
7f0a8382 | 9004 | } |
d4baa936 | 9005 | |
aa8d3a71 AN |
9006 | if (link) |
9007 | dev_xdp_set_link(dev, mode, link); | |
9008 | else | |
9009 | dev_xdp_set_prog(dev, mode, new_prog); | |
d4baa936 AN |
9010 | if (cur_prog) |
9011 | bpf_prog_put(cur_prog); | |
a7862b45 | 9012 | |
7f0a8382 | 9013 | return 0; |
a7862b45 | 9014 | } |
a7862b45 | 9015 | |
aa8d3a71 AN |
9016 | static int dev_xdp_attach_link(struct net_device *dev, |
9017 | struct netlink_ext_ack *extack, | |
9018 | struct bpf_xdp_link *link) | |
9019 | { | |
9020 | return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags); | |
9021 | } | |
9022 | ||
9023 | static int dev_xdp_detach_link(struct net_device *dev, | |
9024 | struct netlink_ext_ack *extack, | |
9025 | struct bpf_xdp_link *link) | |
9026 | { | |
9027 | enum bpf_xdp_mode mode; | |
9028 | bpf_op_t bpf_op; | |
9029 | ||
9030 | ASSERT_RTNL(); | |
9031 | ||
c8a36f19 | 9032 | mode = dev_xdp_mode(dev, link->flags); |
aa8d3a71 AN |
9033 | if (dev_xdp_link(dev, mode) != link) |
9034 | return -EINVAL; | |
9035 | ||
9036 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9037 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); | |
9038 | dev_xdp_set_link(dev, mode, NULL); | |
9039 | return 0; | |
9040 | } | |
9041 | ||
9042 | static void bpf_xdp_link_release(struct bpf_link *link) | |
9043 | { | |
9044 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9045 | ||
9046 | rtnl_lock(); | |
9047 | ||
9048 | /* if racing with net_device's tear down, xdp_link->dev might be | |
9049 | * already NULL, in which case link was already auto-detached | |
9050 | */ | |
73b11c2a | 9051 | if (xdp_link->dev) { |
aa8d3a71 | 9052 | WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link)); |
73b11c2a AN |
9053 | xdp_link->dev = NULL; |
9054 | } | |
aa8d3a71 AN |
9055 | |
9056 | rtnl_unlock(); | |
9057 | } | |
9058 | ||
73b11c2a AN |
9059 | static int bpf_xdp_link_detach(struct bpf_link *link) |
9060 | { | |
9061 | bpf_xdp_link_release(link); | |
9062 | return 0; | |
9063 | } | |
9064 | ||
aa8d3a71 AN |
9065 | static void bpf_xdp_link_dealloc(struct bpf_link *link) |
9066 | { | |
9067 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9068 | ||
9069 | kfree(xdp_link); | |
9070 | } | |
9071 | ||
c1931c97 AN |
9072 | static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link, |
9073 | struct seq_file *seq) | |
9074 | { | |
9075 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9076 | u32 ifindex = 0; | |
9077 | ||
9078 | rtnl_lock(); | |
9079 | if (xdp_link->dev) | |
9080 | ifindex = xdp_link->dev->ifindex; | |
9081 | rtnl_unlock(); | |
9082 | ||
9083 | seq_printf(seq, "ifindex:\t%u\n", ifindex); | |
9084 | } | |
9085 | ||
9086 | static int bpf_xdp_link_fill_link_info(const struct bpf_link *link, | |
9087 | struct bpf_link_info *info) | |
9088 | { | |
9089 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9090 | u32 ifindex = 0; | |
9091 | ||
9092 | rtnl_lock(); | |
9093 | if (xdp_link->dev) | |
9094 | ifindex = xdp_link->dev->ifindex; | |
9095 | rtnl_unlock(); | |
9096 | ||
9097 | info->xdp.ifindex = ifindex; | |
9098 | return 0; | |
9099 | } | |
9100 | ||
026a4c28 AN |
9101 | static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, |
9102 | struct bpf_prog *old_prog) | |
9103 | { | |
9104 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9105 | enum bpf_xdp_mode mode; | |
9106 | bpf_op_t bpf_op; | |
9107 | int err = 0; | |
9108 | ||
9109 | rtnl_lock(); | |
9110 | ||
9111 | /* link might have been auto-released already, so fail */ | |
9112 | if (!xdp_link->dev) { | |
9113 | err = -ENOLINK; | |
9114 | goto out_unlock; | |
9115 | } | |
9116 | ||
9117 | if (old_prog && link->prog != old_prog) { | |
9118 | err = -EPERM; | |
9119 | goto out_unlock; | |
9120 | } | |
9121 | old_prog = link->prog; | |
9122 | if (old_prog == new_prog) { | |
9123 | /* no-op, don't disturb drivers */ | |
9124 | bpf_prog_put(new_prog); | |
9125 | goto out_unlock; | |
9126 | } | |
9127 | ||
c8a36f19 | 9128 | mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags); |
026a4c28 AN |
9129 | bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode); |
9130 | err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL, | |
9131 | xdp_link->flags, new_prog); | |
9132 | if (err) | |
9133 | goto out_unlock; | |
9134 | ||
9135 | old_prog = xchg(&link->prog, new_prog); | |
9136 | bpf_prog_put(old_prog); | |
9137 | ||
9138 | out_unlock: | |
9139 | rtnl_unlock(); | |
9140 | return err; | |
9141 | } | |
9142 | ||
aa8d3a71 AN |
9143 | static const struct bpf_link_ops bpf_xdp_link_lops = { |
9144 | .release = bpf_xdp_link_release, | |
9145 | .dealloc = bpf_xdp_link_dealloc, | |
73b11c2a | 9146 | .detach = bpf_xdp_link_detach, |
c1931c97 AN |
9147 | .show_fdinfo = bpf_xdp_link_show_fdinfo, |
9148 | .fill_link_info = bpf_xdp_link_fill_link_info, | |
026a4c28 | 9149 | .update_prog = bpf_xdp_link_update, |
aa8d3a71 AN |
9150 | }; |
9151 | ||
9152 | int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
9153 | { | |
9154 | struct net *net = current->nsproxy->net_ns; | |
9155 | struct bpf_link_primer link_primer; | |
9156 | struct bpf_xdp_link *link; | |
9157 | struct net_device *dev; | |
9158 | int err, fd; | |
9159 | ||
9160 | dev = dev_get_by_index(net, attr->link_create.target_ifindex); | |
9161 | if (!dev) | |
9162 | return -EINVAL; | |
9163 | ||
9164 | link = kzalloc(sizeof(*link), GFP_USER); | |
9165 | if (!link) { | |
9166 | err = -ENOMEM; | |
9167 | goto out_put_dev; | |
9168 | } | |
9169 | ||
9170 | bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog); | |
9171 | link->dev = dev; | |
9172 | link->flags = attr->link_create.flags; | |
9173 | ||
9174 | err = bpf_link_prime(&link->link, &link_primer); | |
9175 | if (err) { | |
9176 | kfree(link); | |
9177 | goto out_put_dev; | |
9178 | } | |
9179 | ||
9180 | rtnl_lock(); | |
9181 | err = dev_xdp_attach_link(dev, NULL, link); | |
9182 | rtnl_unlock(); | |
9183 | ||
9184 | if (err) { | |
9185 | bpf_link_cleanup(&link_primer); | |
9186 | goto out_put_dev; | |
9187 | } | |
9188 | ||
9189 | fd = bpf_link_settle(&link_primer); | |
9190 | /* link itself doesn't hold dev's refcnt to not complicate shutdown */ | |
9191 | dev_put(dev); | |
9192 | return fd; | |
9193 | ||
9194 | out_put_dev: | |
9195 | dev_put(dev); | |
9196 | return err; | |
9197 | } | |
9198 | ||
d4baa936 AN |
9199 | /** |
9200 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path | |
9201 | * @dev: device | |
9202 | * @extack: netlink extended ack | |
9203 | * @fd: new program fd or negative value to clear | |
9204 | * @expected_fd: old program fd that userspace expects to replace or clear | |
9205 | * @flags: xdp-related flags | |
9206 | * | |
9207 | * Set or clear a bpf program for a device | |
9208 | */ | |
9209 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, | |
9210 | int fd, int expected_fd, u32 flags) | |
9211 | { | |
c8a36f19 | 9212 | enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags); |
d4baa936 AN |
9213 | struct bpf_prog *new_prog = NULL, *old_prog = NULL; |
9214 | int err; | |
9215 | ||
9216 | ASSERT_RTNL(); | |
9217 | ||
9218 | if (fd >= 0) { | |
9219 | new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, | |
9220 | mode != XDP_MODE_SKB); | |
9221 | if (IS_ERR(new_prog)) | |
9222 | return PTR_ERR(new_prog); | |
9223 | } | |
9224 | ||
9225 | if (expected_fd >= 0) { | |
9226 | old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, | |
9227 | mode != XDP_MODE_SKB); | |
9228 | if (IS_ERR(old_prog)) { | |
9229 | err = PTR_ERR(old_prog); | |
9230 | old_prog = NULL; | |
9231 | goto err_out; | |
c14a9f63 | 9232 | } |
a7862b45 BB |
9233 | } |
9234 | ||
aa8d3a71 | 9235 | err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags); |
a7862b45 | 9236 | |
d4baa936 AN |
9237 | err_out: |
9238 | if (err && new_prog) | |
9239 | bpf_prog_put(new_prog); | |
9240 | if (old_prog) | |
9241 | bpf_prog_put(old_prog); | |
a7862b45 BB |
9242 | return err; |
9243 | } | |
a7862b45 | 9244 | |
1da177e4 LT |
9245 | /** |
9246 | * dev_new_index - allocate an ifindex | |
c4ea43c5 | 9247 | * @net: the applicable net namespace |
1da177e4 LT |
9248 | * |
9249 | * Returns a suitable unique value for a new device interface | |
9250 | * number. The caller must hold the rtnl semaphore or the | |
9251 | * dev_base_lock to be sure it remains unique. | |
9252 | */ | |
881d966b | 9253 | static int dev_new_index(struct net *net) |
1da177e4 | 9254 | { |
aa79e66e | 9255 | int ifindex = net->ifindex; |
f4563a75 | 9256 | |
1da177e4 LT |
9257 | for (;;) { |
9258 | if (++ifindex <= 0) | |
9259 | ifindex = 1; | |
881d966b | 9260 | if (!__dev_get_by_index(net, ifindex)) |
aa79e66e | 9261 | return net->ifindex = ifindex; |
1da177e4 LT |
9262 | } |
9263 | } | |
9264 | ||
1da177e4 | 9265 | /* Delayed registration/unregisteration */ |
3b5b34fd | 9266 | static LIST_HEAD(net_todo_list); |
200b916f | 9267 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
1da177e4 | 9268 | |
6f05f629 | 9269 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 9270 | { |
1da177e4 | 9271 | list_add_tail(&dev->todo_list, &net_todo_list); |
50624c93 | 9272 | dev_net(dev)->dev_unreg_count++; |
1da177e4 LT |
9273 | } |
9274 | ||
9b5e383c | 9275 | static void rollback_registered_many(struct list_head *head) |
93ee31f1 | 9276 | { |
e93737b0 | 9277 | struct net_device *dev, *tmp; |
5cde2829 | 9278 | LIST_HEAD(close_head); |
9b5e383c | 9279 | |
93ee31f1 DL |
9280 | BUG_ON(dev_boot_phase); |
9281 | ASSERT_RTNL(); | |
9282 | ||
e93737b0 | 9283 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
9b5e383c | 9284 | /* Some devices call without registering |
e93737b0 KK |
9285 | * for initialization unwind. Remove those |
9286 | * devices and proceed with the remaining. | |
9b5e383c ED |
9287 | */ |
9288 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
7b6cd1ce JP |
9289 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", |
9290 | dev->name, dev); | |
93ee31f1 | 9291 | |
9b5e383c | 9292 | WARN_ON(1); |
e93737b0 KK |
9293 | list_del(&dev->unreg_list); |
9294 | continue; | |
9b5e383c | 9295 | } |
449f4544 | 9296 | dev->dismantle = true; |
9b5e383c | 9297 | BUG_ON(dev->reg_state != NETREG_REGISTERED); |
44345724 | 9298 | } |
93ee31f1 | 9299 | |
44345724 | 9300 | /* If device is running, close it first. */ |
5cde2829 EB |
9301 | list_for_each_entry(dev, head, unreg_list) |
9302 | list_add_tail(&dev->close_list, &close_head); | |
99c4a26a | 9303 | dev_close_many(&close_head, true); |
93ee31f1 | 9304 | |
44345724 | 9305 | list_for_each_entry(dev, head, unreg_list) { |
9b5e383c ED |
9306 | /* And unlink it from device chain. */ |
9307 | unlist_netdevice(dev); | |
93ee31f1 | 9308 | |
9b5e383c ED |
9309 | dev->reg_state = NETREG_UNREGISTERING; |
9310 | } | |
41852497 | 9311 | flush_all_backlogs(); |
93ee31f1 DL |
9312 | |
9313 | synchronize_net(); | |
9314 | ||
9b5e383c | 9315 | list_for_each_entry(dev, head, unreg_list) { |
395eea6c MB |
9316 | struct sk_buff *skb = NULL; |
9317 | ||
9b5e383c ED |
9318 | /* Shutdown queueing discipline. */ |
9319 | dev_shutdown(dev); | |
93ee31f1 | 9320 | |
bd0b2e7f | 9321 | dev_xdp_uninstall(dev); |
93ee31f1 | 9322 | |
9b5e383c | 9323 | /* Notify protocols, that we are about to destroy |
eb13da1a | 9324 | * this device. They should clean all the things. |
9325 | */ | |
9b5e383c | 9326 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
93ee31f1 | 9327 | |
395eea6c MB |
9328 | if (!dev->rtnl_link_ops || |
9329 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
3d3ea5af | 9330 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, |
38e01b30 | 9331 | GFP_KERNEL, NULL, 0); |
395eea6c | 9332 | |
9b5e383c ED |
9333 | /* |
9334 | * Flush the unicast and multicast chains | |
9335 | */ | |
a748ee24 | 9336 | dev_uc_flush(dev); |
22bedad3 | 9337 | dev_mc_flush(dev); |
93ee31f1 | 9338 | |
36fbf1e5 | 9339 | netdev_name_node_alt_flush(dev); |
ff927412 JP |
9340 | netdev_name_node_free(dev->name_node); |
9341 | ||
9b5e383c ED |
9342 | if (dev->netdev_ops->ndo_uninit) |
9343 | dev->netdev_ops->ndo_uninit(dev); | |
93ee31f1 | 9344 | |
395eea6c MB |
9345 | if (skb) |
9346 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL); | |
56bfa7ee | 9347 | |
9ff162a8 JP |
9348 | /* Notifier chain MUST detach us all upper devices. */ |
9349 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
0f524a80 | 9350 | WARN_ON(netdev_has_any_lower_dev(dev)); |
93ee31f1 | 9351 | |
9b5e383c ED |
9352 | /* Remove entries from kobject tree */ |
9353 | netdev_unregister_kobject(dev); | |
024e9679 AD |
9354 | #ifdef CONFIG_XPS |
9355 | /* Remove XPS queueing entries */ | |
9356 | netif_reset_xps_queues_gt(dev, 0); | |
9357 | #endif | |
9b5e383c | 9358 | } |
93ee31f1 | 9359 | |
850a545b | 9360 | synchronize_net(); |
395264d5 | 9361 | |
a5ee1551 | 9362 | list_for_each_entry(dev, head, unreg_list) |
9b5e383c ED |
9363 | dev_put(dev); |
9364 | } | |
9365 | ||
9366 | static void rollback_registered(struct net_device *dev) | |
9367 | { | |
9368 | LIST_HEAD(single); | |
9369 | ||
9370 | list_add(&dev->unreg_list, &single); | |
9371 | rollback_registered_many(&single); | |
ceaaec98 | 9372 | list_del(&single); |
93ee31f1 DL |
9373 | } |
9374 | ||
fd867d51 JW |
9375 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
9376 | struct net_device *upper, netdev_features_t features) | |
9377 | { | |
9378 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9379 | netdev_features_t feature; | |
5ba3f7d6 | 9380 | int feature_bit; |
fd867d51 | 9381 | |
3b89ea9c | 9382 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9383 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9384 | if (!(upper->wanted_features & feature) |
9385 | && (features & feature)) { | |
9386 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", | |
9387 | &feature, upper->name); | |
9388 | features &= ~feature; | |
9389 | } | |
9390 | } | |
9391 | ||
9392 | return features; | |
9393 | } | |
9394 | ||
9395 | static void netdev_sync_lower_features(struct net_device *upper, | |
9396 | struct net_device *lower, netdev_features_t features) | |
9397 | { | |
9398 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9399 | netdev_features_t feature; | |
5ba3f7d6 | 9400 | int feature_bit; |
fd867d51 | 9401 | |
3b89ea9c | 9402 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9403 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9404 | if (!(features & feature) && (lower->features & feature)) { |
9405 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", | |
9406 | &feature, lower->name); | |
9407 | lower->wanted_features &= ~feature; | |
dd912306 | 9408 | __netdev_update_features(lower); |
fd867d51 JW |
9409 | |
9410 | if (unlikely(lower->features & feature)) | |
9411 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", | |
9412 | &feature, lower->name); | |
dd912306 CW |
9413 | else |
9414 | netdev_features_change(lower); | |
fd867d51 JW |
9415 | } |
9416 | } | |
9417 | } | |
9418 | ||
c8f44aff MM |
9419 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
9420 | netdev_features_t features) | |
b63365a2 | 9421 | { |
57422dc5 MM |
9422 | /* Fix illegal checksum combinations */ |
9423 | if ((features & NETIF_F_HW_CSUM) && | |
9424 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 9425 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
9426 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
9427 | } | |
9428 | ||
b63365a2 | 9429 | /* TSO requires that SG is present as well. */ |
ea2d3688 | 9430 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 9431 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 9432 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
9433 | } |
9434 | ||
ec5f0615 PS |
9435 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
9436 | !(features & NETIF_F_IP_CSUM)) { | |
9437 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); | |
9438 | features &= ~NETIF_F_TSO; | |
9439 | features &= ~NETIF_F_TSO_ECN; | |
9440 | } | |
9441 | ||
9442 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && | |
9443 | !(features & NETIF_F_IPV6_CSUM)) { | |
9444 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); | |
9445 | features &= ~NETIF_F_TSO6; | |
9446 | } | |
9447 | ||
b1dc497b AD |
9448 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
9449 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) | |
9450 | features &= ~NETIF_F_TSO_MANGLEID; | |
9451 | ||
31d8b9e0 BH |
9452 | /* TSO ECN requires that TSO is present as well. */ |
9453 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
9454 | features &= ~NETIF_F_TSO_ECN; | |
9455 | ||
212b573f MM |
9456 | /* Software GSO depends on SG. */ |
9457 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 9458 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
9459 | features &= ~NETIF_F_GSO; |
9460 | } | |
9461 | ||
802ab55a AD |
9462 | /* GSO partial features require GSO partial be set */ |
9463 | if ((features & dev->gso_partial_features) && | |
9464 | !(features & NETIF_F_GSO_PARTIAL)) { | |
9465 | netdev_dbg(dev, | |
9466 | "Dropping partially supported GSO features since no GSO partial.\n"); | |
9467 | features &= ~dev->gso_partial_features; | |
9468 | } | |
9469 | ||
fb1f5f79 MC |
9470 | if (!(features & NETIF_F_RXCSUM)) { |
9471 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet | |
9472 | * successfully merged by hardware must also have the | |
9473 | * checksum verified by hardware. If the user does not | |
9474 | * want to enable RXCSUM, logically, we should disable GRO_HW. | |
9475 | */ | |
9476 | if (features & NETIF_F_GRO_HW) { | |
9477 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); | |
9478 | features &= ~NETIF_F_GRO_HW; | |
9479 | } | |
9480 | } | |
9481 | ||
de8d5ab2 GP |
9482 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
9483 | if (features & NETIF_F_RXFCS) { | |
9484 | if (features & NETIF_F_LRO) { | |
9485 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); | |
9486 | features &= ~NETIF_F_LRO; | |
9487 | } | |
9488 | ||
9489 | if (features & NETIF_F_GRO_HW) { | |
9490 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); | |
9491 | features &= ~NETIF_F_GRO_HW; | |
9492 | } | |
e6c6a929 GP |
9493 | } |
9494 | ||
b63365a2 HX |
9495 | return features; |
9496 | } | |
b63365a2 | 9497 | |
6cb6a27c | 9498 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 9499 | { |
fd867d51 | 9500 | struct net_device *upper, *lower; |
c8f44aff | 9501 | netdev_features_t features; |
fd867d51 | 9502 | struct list_head *iter; |
e7868a85 | 9503 | int err = -1; |
5455c699 | 9504 | |
87267485 MM |
9505 | ASSERT_RTNL(); |
9506 | ||
5455c699 MM |
9507 | features = netdev_get_wanted_features(dev); |
9508 | ||
9509 | if (dev->netdev_ops->ndo_fix_features) | |
9510 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
9511 | ||
9512 | /* driver might be less strict about feature dependencies */ | |
9513 | features = netdev_fix_features(dev, features); | |
9514 | ||
fd867d51 JW |
9515 | /* some features can't be enabled if they're off an an upper device */ |
9516 | netdev_for_each_upper_dev_rcu(dev, upper, iter) | |
9517 | features = netdev_sync_upper_features(dev, upper, features); | |
9518 | ||
5455c699 | 9519 | if (dev->features == features) |
e7868a85 | 9520 | goto sync_lower; |
5455c699 | 9521 | |
c8f44aff MM |
9522 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
9523 | &dev->features, &features); | |
5455c699 MM |
9524 | |
9525 | if (dev->netdev_ops->ndo_set_features) | |
9526 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
5f8dc33e NA |
9527 | else |
9528 | err = 0; | |
5455c699 | 9529 | |
6cb6a27c | 9530 | if (unlikely(err < 0)) { |
5455c699 | 9531 | netdev_err(dev, |
c8f44aff MM |
9532 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
9533 | err, &features, &dev->features); | |
17b85d29 NA |
9534 | /* return non-0 since some features might have changed and |
9535 | * it's better to fire a spurious notification than miss it | |
9536 | */ | |
9537 | return -1; | |
6cb6a27c MM |
9538 | } |
9539 | ||
e7868a85 | 9540 | sync_lower: |
fd867d51 JW |
9541 | /* some features must be disabled on lower devices when disabled |
9542 | * on an upper device (think: bonding master or bridge) | |
9543 | */ | |
9544 | netdev_for_each_lower_dev(dev, lower, iter) | |
9545 | netdev_sync_lower_features(dev, lower, features); | |
9546 | ||
ae847f40 SD |
9547 | if (!err) { |
9548 | netdev_features_t diff = features ^ dev->features; | |
9549 | ||
9550 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9551 | /* udp_tunnel_{get,drop}_rx_info both need | |
9552 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the | |
9553 | * device, or they won't do anything. | |
9554 | * Thus we need to update dev->features | |
9555 | * *before* calling udp_tunnel_get_rx_info, | |
9556 | * but *after* calling udp_tunnel_drop_rx_info. | |
9557 | */ | |
9558 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9559 | dev->features = features; | |
9560 | udp_tunnel_get_rx_info(dev); | |
9561 | } else { | |
9562 | udp_tunnel_drop_rx_info(dev); | |
9563 | } | |
9564 | } | |
9565 | ||
9daae9bd GP |
9566 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
9567 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { | |
9568 | dev->features = features; | |
9569 | err |= vlan_get_rx_ctag_filter_info(dev); | |
9570 | } else { | |
9571 | vlan_drop_rx_ctag_filter_info(dev); | |
9572 | } | |
9573 | } | |
9574 | ||
9575 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9576 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9577 | dev->features = features; | |
9578 | err |= vlan_get_rx_stag_filter_info(dev); | |
9579 | } else { | |
9580 | vlan_drop_rx_stag_filter_info(dev); | |
9581 | } | |
9582 | } | |
9583 | ||
6cb6a27c | 9584 | dev->features = features; |
ae847f40 | 9585 | } |
6cb6a27c | 9586 | |
e7868a85 | 9587 | return err < 0 ? 0 : 1; |
6cb6a27c MM |
9588 | } |
9589 | ||
afe12cc8 MM |
9590 | /** |
9591 | * netdev_update_features - recalculate device features | |
9592 | * @dev: the device to check | |
9593 | * | |
9594 | * Recalculate dev->features set and send notifications if it | |
9595 | * has changed. Should be called after driver or hardware dependent | |
9596 | * conditions might have changed that influence the features. | |
9597 | */ | |
6cb6a27c MM |
9598 | void netdev_update_features(struct net_device *dev) |
9599 | { | |
9600 | if (__netdev_update_features(dev)) | |
9601 | netdev_features_change(dev); | |
5455c699 MM |
9602 | } |
9603 | EXPORT_SYMBOL(netdev_update_features); | |
9604 | ||
afe12cc8 MM |
9605 | /** |
9606 | * netdev_change_features - recalculate device features | |
9607 | * @dev: the device to check | |
9608 | * | |
9609 | * Recalculate dev->features set and send notifications even | |
9610 | * if they have not changed. Should be called instead of | |
9611 | * netdev_update_features() if also dev->vlan_features might | |
9612 | * have changed to allow the changes to be propagated to stacked | |
9613 | * VLAN devices. | |
9614 | */ | |
9615 | void netdev_change_features(struct net_device *dev) | |
9616 | { | |
9617 | __netdev_update_features(dev); | |
9618 | netdev_features_change(dev); | |
9619 | } | |
9620 | EXPORT_SYMBOL(netdev_change_features); | |
9621 | ||
fc4a7489 PM |
9622 | /** |
9623 | * netif_stacked_transfer_operstate - transfer operstate | |
9624 | * @rootdev: the root or lower level device to transfer state from | |
9625 | * @dev: the device to transfer operstate to | |
9626 | * | |
9627 | * Transfer operational state from root to device. This is normally | |
9628 | * called when a stacking relationship exists between the root | |
9629 | * device and the device(a leaf device). | |
9630 | */ | |
9631 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
9632 | struct net_device *dev) | |
9633 | { | |
9634 | if (rootdev->operstate == IF_OPER_DORMANT) | |
9635 | netif_dormant_on(dev); | |
9636 | else | |
9637 | netif_dormant_off(dev); | |
9638 | ||
eec517cd AL |
9639 | if (rootdev->operstate == IF_OPER_TESTING) |
9640 | netif_testing_on(dev); | |
9641 | else | |
9642 | netif_testing_off(dev); | |
9643 | ||
0575c86b ZS |
9644 | if (netif_carrier_ok(rootdev)) |
9645 | netif_carrier_on(dev); | |
9646 | else | |
9647 | netif_carrier_off(dev); | |
fc4a7489 PM |
9648 | } |
9649 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
9650 | ||
1b4bf461 ED |
9651 | static int netif_alloc_rx_queues(struct net_device *dev) |
9652 | { | |
1b4bf461 | 9653 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 9654 | struct netdev_rx_queue *rx; |
10595902 | 9655 | size_t sz = count * sizeof(*rx); |
e817f856 | 9656 | int err = 0; |
1b4bf461 | 9657 | |
bd25fa7b | 9658 | BUG_ON(count < 1); |
1b4bf461 | 9659 | |
dcda9b04 | 9660 | rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9661 | if (!rx) |
9662 | return -ENOMEM; | |
9663 | ||
bd25fa7b TH |
9664 | dev->_rx = rx; |
9665 | ||
e817f856 | 9666 | for (i = 0; i < count; i++) { |
fe822240 | 9667 | rx[i].dev = dev; |
e817f856 JDB |
9668 | |
9669 | /* XDP RX-queue setup */ | |
9670 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i); | |
9671 | if (err < 0) | |
9672 | goto err_rxq_info; | |
9673 | } | |
1b4bf461 | 9674 | return 0; |
e817f856 JDB |
9675 | |
9676 | err_rxq_info: | |
9677 | /* Rollback successful reg's and free other resources */ | |
9678 | while (i--) | |
9679 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); | |
141b52a9 | 9680 | kvfree(dev->_rx); |
e817f856 JDB |
9681 | dev->_rx = NULL; |
9682 | return err; | |
9683 | } | |
9684 | ||
9685 | static void netif_free_rx_queues(struct net_device *dev) | |
9686 | { | |
9687 | unsigned int i, count = dev->num_rx_queues; | |
e817f856 JDB |
9688 | |
9689 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ | |
9690 | if (!dev->_rx) | |
9691 | return; | |
9692 | ||
e817f856 | 9693 | for (i = 0; i < count; i++) |
82aaff2f JK |
9694 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
9695 | ||
9696 | kvfree(dev->_rx); | |
1b4bf461 ED |
9697 | } |
9698 | ||
aa942104 CG |
9699 | static void netdev_init_one_queue(struct net_device *dev, |
9700 | struct netdev_queue *queue, void *_unused) | |
9701 | { | |
9702 | /* Initialize queue lock */ | |
9703 | spin_lock_init(&queue->_xmit_lock); | |
1a33e10e | 9704 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); |
aa942104 | 9705 | queue->xmit_lock_owner = -1; |
b236da69 | 9706 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 9707 | queue->dev = dev; |
114cf580 TH |
9708 | #ifdef CONFIG_BQL |
9709 | dql_init(&queue->dql, HZ); | |
9710 | #endif | |
aa942104 CG |
9711 | } |
9712 | ||
60877a32 ED |
9713 | static void netif_free_tx_queues(struct net_device *dev) |
9714 | { | |
4cb28970 | 9715 | kvfree(dev->_tx); |
60877a32 ED |
9716 | } |
9717 | ||
e6484930 TH |
9718 | static int netif_alloc_netdev_queues(struct net_device *dev) |
9719 | { | |
9720 | unsigned int count = dev->num_tx_queues; | |
9721 | struct netdev_queue *tx; | |
60877a32 | 9722 | size_t sz = count * sizeof(*tx); |
e6484930 | 9723 | |
d339727c ED |
9724 | if (count < 1 || count > 0xffff) |
9725 | return -EINVAL; | |
62b5942a | 9726 | |
dcda9b04 | 9727 | tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9728 | if (!tx) |
9729 | return -ENOMEM; | |
9730 | ||
e6484930 | 9731 | dev->_tx = tx; |
1d24eb48 | 9732 | |
e6484930 TH |
9733 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
9734 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
9735 | |
9736 | return 0; | |
e6484930 TH |
9737 | } |
9738 | ||
a2029240 DV |
9739 | void netif_tx_stop_all_queues(struct net_device *dev) |
9740 | { | |
9741 | unsigned int i; | |
9742 | ||
9743 | for (i = 0; i < dev->num_tx_queues; i++) { | |
9744 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
f4563a75 | 9745 | |
a2029240 DV |
9746 | netif_tx_stop_queue(txq); |
9747 | } | |
9748 | } | |
9749 | EXPORT_SYMBOL(netif_tx_stop_all_queues); | |
9750 | ||
1da177e4 LT |
9751 | /** |
9752 | * register_netdevice - register a network device | |
9753 | * @dev: device to register | |
9754 | * | |
9755 | * Take a completed network device structure and add it to the kernel | |
9756 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
9757 | * chain. 0 is returned on success. A negative errno code is returned | |
9758 | * on a failure to set up the device, or if the name is a duplicate. | |
9759 | * | |
9760 | * Callers must hold the rtnl semaphore. You may want | |
9761 | * register_netdev() instead of this. | |
9762 | * | |
9763 | * BUGS: | |
9764 | * The locking appears insufficient to guarantee two parallel registers | |
9765 | * will not get the same name. | |
9766 | */ | |
9767 | ||
9768 | int register_netdevice(struct net_device *dev) | |
9769 | { | |
1da177e4 | 9770 | int ret; |
d314774c | 9771 | struct net *net = dev_net(dev); |
1da177e4 | 9772 | |
e283de3a FF |
9773 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
9774 | NETDEV_FEATURE_COUNT); | |
1da177e4 LT |
9775 | BUG_ON(dev_boot_phase); |
9776 | ASSERT_RTNL(); | |
9777 | ||
b17a7c17 SH |
9778 | might_sleep(); |
9779 | ||
1da177e4 LT |
9780 | /* When net_device's are persistent, this will be fatal. */ |
9781 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 9782 | BUG_ON(!net); |
1da177e4 | 9783 | |
9000edb7 JK |
9784 | ret = ethtool_check_ops(dev->ethtool_ops); |
9785 | if (ret) | |
9786 | return ret; | |
9787 | ||
f1f28aa3 | 9788 | spin_lock_init(&dev->addr_list_lock); |
845e0ebb | 9789 | netdev_set_addr_lockdep_class(dev); |
1da177e4 | 9790 | |
828de4f6 | 9791 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
9792 | if (ret < 0) |
9793 | goto out; | |
9794 | ||
9077f052 | 9795 | ret = -ENOMEM; |
ff927412 JP |
9796 | dev->name_node = netdev_name_node_head_alloc(dev); |
9797 | if (!dev->name_node) | |
9798 | goto out; | |
9799 | ||
1da177e4 | 9800 | /* Init, if this function is available */ |
d314774c SH |
9801 | if (dev->netdev_ops->ndo_init) { |
9802 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
9803 | if (ret) { |
9804 | if (ret > 0) | |
9805 | ret = -EIO; | |
42c17fa6 | 9806 | goto err_free_name; |
1da177e4 LT |
9807 | } |
9808 | } | |
4ec93edb | 9809 | |
f646968f PM |
9810 | if (((dev->hw_features | dev->features) & |
9811 | NETIF_F_HW_VLAN_CTAG_FILTER) && | |
d2ed273d MM |
9812 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
9813 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { | |
9814 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); | |
9815 | ret = -EINVAL; | |
9816 | goto err_uninit; | |
9817 | } | |
9818 | ||
9c7dafbf PE |
9819 | ret = -EBUSY; |
9820 | if (!dev->ifindex) | |
9821 | dev->ifindex = dev_new_index(net); | |
9822 | else if (__dev_get_by_index(net, dev->ifindex)) | |
9823 | goto err_uninit; | |
9824 | ||
5455c699 MM |
9825 | /* Transfer changeable features to wanted_features and enable |
9826 | * software offloads (GSO and GRO). | |
9827 | */ | |
1a3c998f | 9828 | dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); |
14d1232f | 9829 | dev->features |= NETIF_F_SOFT_FEATURES; |
d764a122 SD |
9830 | |
9831 | if (dev->netdev_ops->ndo_udp_tunnel_add) { | |
9832 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
9833 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
9834 | } | |
9835 | ||
14d1232f | 9836 | dev->wanted_features = dev->features & dev->hw_features; |
1da177e4 | 9837 | |
cbc53e08 | 9838 | if (!(dev->flags & IFF_LOOPBACK)) |
34324dc2 | 9839 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
cbc53e08 | 9840 | |
7f348a60 AD |
9841 | /* If IPv4 TCP segmentation offload is supported we should also |
9842 | * allow the device to enable segmenting the frame with the option | |
9843 | * of ignoring a static IP ID value. This doesn't enable the | |
9844 | * feature itself but allows the user to enable it later. | |
9845 | */ | |
cbc53e08 AD |
9846 | if (dev->hw_features & NETIF_F_TSO) |
9847 | dev->hw_features |= NETIF_F_TSO_MANGLEID; | |
7f348a60 AD |
9848 | if (dev->vlan_features & NETIF_F_TSO) |
9849 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; | |
9850 | if (dev->mpls_features & NETIF_F_TSO) | |
9851 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; | |
9852 | if (dev->hw_enc_features & NETIF_F_TSO) | |
9853 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; | |
c6e1a0d1 | 9854 | |
1180e7d6 | 9855 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 9856 | */ |
1180e7d6 | 9857 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 9858 | |
ee579677 PS |
9859 | /* Make NETIF_F_SG inheritable to tunnel devices. |
9860 | */ | |
802ab55a | 9861 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
ee579677 | 9862 | |
0d89d203 SH |
9863 | /* Make NETIF_F_SG inheritable to MPLS. |
9864 | */ | |
9865 | dev->mpls_features |= NETIF_F_SG; | |
9866 | ||
7ffbe3fd JB |
9867 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
9868 | ret = notifier_to_errno(ret); | |
9869 | if (ret) | |
9870 | goto err_uninit; | |
9871 | ||
8b41d188 | 9872 | ret = netdev_register_kobject(dev); |
cb626bf5 JH |
9873 | if (ret) { |
9874 | dev->reg_state = NETREG_UNREGISTERED; | |
7ce1b0ed | 9875 | goto err_uninit; |
cb626bf5 | 9876 | } |
b17a7c17 SH |
9877 | dev->reg_state = NETREG_REGISTERED; |
9878 | ||
6cb6a27c | 9879 | __netdev_update_features(dev); |
8e9b59b2 | 9880 | |
1da177e4 LT |
9881 | /* |
9882 | * Default initial state at registry is that the | |
9883 | * device is present. | |
9884 | */ | |
9885 | ||
9886 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
9887 | ||
8f4cccbb BH |
9888 | linkwatch_init_dev(dev); |
9889 | ||
1da177e4 | 9890 | dev_init_scheduler(dev); |
1da177e4 | 9891 | dev_hold(dev); |
ce286d32 | 9892 | list_netdevice(dev); |
7bf23575 | 9893 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 9894 | |
948b337e JP |
9895 | /* If the device has permanent device address, driver should |
9896 | * set dev_addr and also addr_assign_type should be set to | |
9897 | * NET_ADDR_PERM (default value). | |
9898 | */ | |
9899 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
9900 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
9901 | ||
1da177e4 | 9902 | /* Notify protocols, that a new device appeared. */ |
056925ab | 9903 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 9904 | ret = notifier_to_errno(ret); |
93ee31f1 DL |
9905 | if (ret) { |
9906 | rollback_registered(dev); | |
10cc514f SAK |
9907 | rcu_barrier(); |
9908 | ||
93ee31f1 | 9909 | dev->reg_state = NETREG_UNREGISTERED; |
814152a8 YY |
9910 | /* We should put the kobject that hold in |
9911 | * netdev_unregister_kobject(), otherwise | |
9912 | * the net device cannot be freed when | |
9913 | * driver calls free_netdev(), because the | |
9914 | * kobject is being hold. | |
9915 | */ | |
9916 | kobject_put(&dev->dev.kobj); | |
93ee31f1 | 9917 | } |
d90a909e EB |
9918 | /* |
9919 | * Prevent userspace races by waiting until the network | |
9920 | * device is fully setup before sending notifications. | |
9921 | */ | |
a2835763 PM |
9922 | if (!dev->rtnl_link_ops || |
9923 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
7f294054 | 9924 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
1da177e4 LT |
9925 | |
9926 | out: | |
9927 | return ret; | |
7ce1b0ed HX |
9928 | |
9929 | err_uninit: | |
d314774c SH |
9930 | if (dev->netdev_ops->ndo_uninit) |
9931 | dev->netdev_ops->ndo_uninit(dev); | |
cf124db5 DM |
9932 | if (dev->priv_destructor) |
9933 | dev->priv_destructor(dev); | |
42c17fa6 DC |
9934 | err_free_name: |
9935 | netdev_name_node_free(dev->name_node); | |
7ce1b0ed | 9936 | goto out; |
1da177e4 | 9937 | } |
d1b19dff | 9938 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 9939 | |
937f1ba5 BH |
9940 | /** |
9941 | * init_dummy_netdev - init a dummy network device for NAPI | |
9942 | * @dev: device to init | |
9943 | * | |
9944 | * This takes a network device structure and initialize the minimum | |
9945 | * amount of fields so it can be used to schedule NAPI polls without | |
9946 | * registering a full blown interface. This is to be used by drivers | |
9947 | * that need to tie several hardware interfaces to a single NAPI | |
9948 | * poll scheduler due to HW limitations. | |
9949 | */ | |
9950 | int init_dummy_netdev(struct net_device *dev) | |
9951 | { | |
9952 | /* Clear everything. Note we don't initialize spinlocks | |
9953 | * are they aren't supposed to be taken by any of the | |
9954 | * NAPI code and this dummy netdev is supposed to be | |
9955 | * only ever used for NAPI polls | |
9956 | */ | |
9957 | memset(dev, 0, sizeof(struct net_device)); | |
9958 | ||
9959 | /* make sure we BUG if trying to hit standard | |
9960 | * register/unregister code path | |
9961 | */ | |
9962 | dev->reg_state = NETREG_DUMMY; | |
9963 | ||
937f1ba5 BH |
9964 | /* NAPI wants this */ |
9965 | INIT_LIST_HEAD(&dev->napi_list); | |
9966 | ||
9967 | /* a dummy interface is started by default */ | |
9968 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
9969 | set_bit(__LINK_STATE_START, &dev->state); | |
9970 | ||
35edfdc7 JE |
9971 | /* napi_busy_loop stats accounting wants this */ |
9972 | dev_net_set(dev, &init_net); | |
9973 | ||
29b4433d ED |
9974 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
9975 | * because users of this 'device' dont need to change | |
9976 | * its refcount. | |
9977 | */ | |
9978 | ||
937f1ba5 BH |
9979 | return 0; |
9980 | } | |
9981 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
9982 | ||
9983 | ||
1da177e4 LT |
9984 | /** |
9985 | * register_netdev - register a network device | |
9986 | * @dev: device to register | |
9987 | * | |
9988 | * Take a completed network device structure and add it to the kernel | |
9989 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
9990 | * chain. 0 is returned on success. A negative errno code is returned | |
9991 | * on a failure to set up the device, or if the name is a duplicate. | |
9992 | * | |
38b4da38 | 9993 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
9994 | * and expands the device name if you passed a format string to |
9995 | * alloc_netdev. | |
9996 | */ | |
9997 | int register_netdev(struct net_device *dev) | |
9998 | { | |
9999 | int err; | |
10000 | ||
b0f3debc KT |
10001 | if (rtnl_lock_killable()) |
10002 | return -EINTR; | |
1da177e4 | 10003 | err = register_netdevice(dev); |
1da177e4 LT |
10004 | rtnl_unlock(); |
10005 | return err; | |
10006 | } | |
10007 | EXPORT_SYMBOL(register_netdev); | |
10008 | ||
29b4433d ED |
10009 | int netdev_refcnt_read(const struct net_device *dev) |
10010 | { | |
10011 | int i, refcnt = 0; | |
10012 | ||
10013 | for_each_possible_cpu(i) | |
10014 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
10015 | return refcnt; | |
10016 | } | |
10017 | EXPORT_SYMBOL(netdev_refcnt_read); | |
10018 | ||
2c53040f | 10019 | /** |
1da177e4 | 10020 | * netdev_wait_allrefs - wait until all references are gone. |
3de7a37b | 10021 | * @dev: target net_device |
1da177e4 LT |
10022 | * |
10023 | * This is called when unregistering network devices. | |
10024 | * | |
10025 | * Any protocol or device that holds a reference should register | |
10026 | * for netdevice notification, and cleanup and put back the | |
10027 | * reference if they receive an UNREGISTER event. | |
10028 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 10029 | * call dev_put. |
1da177e4 | 10030 | */ |
0e4be9e5 FR |
10031 | #define WAIT_REFS_MIN_MSECS 1 |
10032 | #define WAIT_REFS_MAX_MSECS 250 | |
1da177e4 LT |
10033 | static void netdev_wait_allrefs(struct net_device *dev) |
10034 | { | |
10035 | unsigned long rebroadcast_time, warning_time; | |
0e4be9e5 | 10036 | int wait = 0, refcnt; |
1da177e4 | 10037 | |
e014debe ED |
10038 | linkwatch_forget_dev(dev); |
10039 | ||
1da177e4 | 10040 | rebroadcast_time = warning_time = jiffies; |
29b4433d ED |
10041 | refcnt = netdev_refcnt_read(dev); |
10042 | ||
10043 | while (refcnt != 0) { | |
1da177e4 | 10044 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 10045 | rtnl_lock(); |
1da177e4 LT |
10046 | |
10047 | /* Rebroadcast unregister notification */ | |
056925ab | 10048 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
1da177e4 | 10049 | |
748e2d93 | 10050 | __rtnl_unlock(); |
0115e8e3 | 10051 | rcu_barrier(); |
748e2d93 ED |
10052 | rtnl_lock(); |
10053 | ||
1da177e4 LT |
10054 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
10055 | &dev->state)) { | |
10056 | /* We must not have linkwatch events | |
10057 | * pending on unregister. If this | |
10058 | * happens, we simply run the queue | |
10059 | * unscheduled, resulting in a noop | |
10060 | * for this device. | |
10061 | */ | |
10062 | linkwatch_run_queue(); | |
10063 | } | |
10064 | ||
6756ae4b | 10065 | __rtnl_unlock(); |
1da177e4 LT |
10066 | |
10067 | rebroadcast_time = jiffies; | |
10068 | } | |
10069 | ||
0e4be9e5 FR |
10070 | if (!wait) { |
10071 | rcu_barrier(); | |
10072 | wait = WAIT_REFS_MIN_MSECS; | |
10073 | } else { | |
10074 | msleep(wait); | |
10075 | wait = min(wait << 1, WAIT_REFS_MAX_MSECS); | |
10076 | } | |
1da177e4 | 10077 | |
29b4433d ED |
10078 | refcnt = netdev_refcnt_read(dev); |
10079 | ||
d7c04b05 | 10080 | if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) { |
7b6cd1ce JP |
10081 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
10082 | dev->name, refcnt); | |
1da177e4 LT |
10083 | warning_time = jiffies; |
10084 | } | |
10085 | } | |
10086 | } | |
10087 | ||
10088 | /* The sequence is: | |
10089 | * | |
10090 | * rtnl_lock(); | |
10091 | * ... | |
10092 | * register_netdevice(x1); | |
10093 | * register_netdevice(x2); | |
10094 | * ... | |
10095 | * unregister_netdevice(y1); | |
10096 | * unregister_netdevice(y2); | |
10097 | * ... | |
10098 | * rtnl_unlock(); | |
10099 | * free_netdev(y1); | |
10100 | * free_netdev(y2); | |
10101 | * | |
58ec3b4d | 10102 | * We are invoked by rtnl_unlock(). |
1da177e4 | 10103 | * This allows us to deal with problems: |
b17a7c17 | 10104 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
10105 | * without deadlocking with linkwatch via keventd. |
10106 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
10107 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
10108 | * |
10109 | * We must not return until all unregister events added during | |
10110 | * the interval the lock was held have been completed. | |
1da177e4 | 10111 | */ |
1da177e4 LT |
10112 | void netdev_run_todo(void) |
10113 | { | |
626ab0e6 | 10114 | struct list_head list; |
1da177e4 | 10115 | |
1da177e4 | 10116 | /* Snapshot list, allow later requests */ |
626ab0e6 | 10117 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
10118 | |
10119 | __rtnl_unlock(); | |
626ab0e6 | 10120 | |
0115e8e3 ED |
10121 | |
10122 | /* Wait for rcu callbacks to finish before next phase */ | |
850a545b EB |
10123 | if (!list_empty(&list)) |
10124 | rcu_barrier(); | |
10125 | ||
1da177e4 LT |
10126 | while (!list_empty(&list)) { |
10127 | struct net_device *dev | |
e5e26d75 | 10128 | = list_first_entry(&list, struct net_device, todo_list); |
1da177e4 LT |
10129 | list_del(&dev->todo_list); |
10130 | ||
b17a7c17 | 10131 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
7b6cd1ce | 10132 | pr_err("network todo '%s' but state %d\n", |
b17a7c17 SH |
10133 | dev->name, dev->reg_state); |
10134 | dump_stack(); | |
10135 | continue; | |
10136 | } | |
1da177e4 | 10137 | |
b17a7c17 | 10138 | dev->reg_state = NETREG_UNREGISTERED; |
1da177e4 | 10139 | |
b17a7c17 | 10140 | netdev_wait_allrefs(dev); |
1da177e4 | 10141 | |
b17a7c17 | 10142 | /* paranoia */ |
29b4433d | 10143 | BUG_ON(netdev_refcnt_read(dev)); |
7866a621 SN |
10144 | BUG_ON(!list_empty(&dev->ptype_all)); |
10145 | BUG_ON(!list_empty(&dev->ptype_specific)); | |
33d480ce ED |
10146 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
10147 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
330c7272 | 10148 | #if IS_ENABLED(CONFIG_DECNET) |
547b792c | 10149 | WARN_ON(dev->dn_ptr); |
330c7272 | 10150 | #endif |
cf124db5 DM |
10151 | if (dev->priv_destructor) |
10152 | dev->priv_destructor(dev); | |
10153 | if (dev->needs_free_netdev) | |
10154 | free_netdev(dev); | |
9093bbb2 | 10155 | |
50624c93 EB |
10156 | /* Report a network device has been unregistered */ |
10157 | rtnl_lock(); | |
10158 | dev_net(dev)->dev_unreg_count--; | |
10159 | __rtnl_unlock(); | |
10160 | wake_up(&netdev_unregistering_wq); | |
10161 | ||
9093bbb2 SH |
10162 | /* Free network device */ |
10163 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 10164 | } |
1da177e4 LT |
10165 | } |
10166 | ||
9256645a JW |
10167 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
10168 | * all the same fields in the same order as net_device_stats, with only | |
10169 | * the type differing, but rtnl_link_stats64 may have additional fields | |
10170 | * at the end for newer counters. | |
3cfde79c | 10171 | */ |
77a1abf5 ED |
10172 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
10173 | const struct net_device_stats *netdev_stats) | |
3cfde79c BH |
10174 | { |
10175 | #if BITS_PER_LONG == 64 | |
9256645a | 10176 | BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats)); |
9af9959e | 10177 | memcpy(stats64, netdev_stats, sizeof(*netdev_stats)); |
9256645a JW |
10178 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10179 | memset((char *)stats64 + sizeof(*netdev_stats), 0, | |
10180 | sizeof(*stats64) - sizeof(*netdev_stats)); | |
3cfde79c | 10181 | #else |
9256645a | 10182 | size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long); |
3cfde79c BH |
10183 | const unsigned long *src = (const unsigned long *)netdev_stats; |
10184 | u64 *dst = (u64 *)stats64; | |
10185 | ||
9256645a | 10186 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
3cfde79c BH |
10187 | for (i = 0; i < n; i++) |
10188 | dst[i] = src[i]; | |
9256645a JW |
10189 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10190 | memset((char *)stats64 + n * sizeof(u64), 0, | |
10191 | sizeof(*stats64) - n * sizeof(u64)); | |
3cfde79c BH |
10192 | #endif |
10193 | } | |
77a1abf5 | 10194 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 10195 | |
eeda3fd6 SH |
10196 | /** |
10197 | * dev_get_stats - get network device statistics | |
10198 | * @dev: device to get statistics from | |
28172739 | 10199 | * @storage: place to store stats |
eeda3fd6 | 10200 | * |
d7753516 BH |
10201 | * Get network statistics from device. Return @storage. |
10202 | * The device driver may provide its own method by setting | |
10203 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
10204 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 10205 | */ |
d7753516 BH |
10206 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
10207 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 10208 | { |
eeda3fd6 SH |
10209 | const struct net_device_ops *ops = dev->netdev_ops; |
10210 | ||
28172739 ED |
10211 | if (ops->ndo_get_stats64) { |
10212 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
10213 | ops->ndo_get_stats64(dev, storage); |
10214 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 10215 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
caf586e5 ED |
10216 | } else { |
10217 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 10218 | } |
6f64ec74 ED |
10219 | storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped); |
10220 | storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped); | |
10221 | storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler); | |
28172739 | 10222 | return storage; |
c45d286e | 10223 | } |
eeda3fd6 | 10224 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 10225 | |
24824a09 | 10226 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 10227 | { |
24824a09 | 10228 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 10229 | |
24824a09 ED |
10230 | #ifdef CONFIG_NET_CLS_ACT |
10231 | if (queue) | |
10232 | return queue; | |
10233 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
10234 | if (!queue) | |
10235 | return NULL; | |
10236 | netdev_init_one_queue(dev, queue, NULL); | |
2ce1ee17 | 10237 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
24824a09 ED |
10238 | queue->qdisc_sleeping = &noop_qdisc; |
10239 | rcu_assign_pointer(dev->ingress_queue, queue); | |
10240 | #endif | |
10241 | return queue; | |
bb949fbd DM |
10242 | } |
10243 | ||
2c60db03 ED |
10244 | static const struct ethtool_ops default_ethtool_ops; |
10245 | ||
d07d7507 SG |
10246 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
10247 | const struct ethtool_ops *ops) | |
10248 | { | |
10249 | if (dev->ethtool_ops == &default_ethtool_ops) | |
10250 | dev->ethtool_ops = ops; | |
10251 | } | |
10252 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
10253 | ||
74d332c1 ED |
10254 | void netdev_freemem(struct net_device *dev) |
10255 | { | |
10256 | char *addr = (char *)dev - dev->padded; | |
10257 | ||
4cb28970 | 10258 | kvfree(addr); |
74d332c1 ED |
10259 | } |
10260 | ||
1da177e4 | 10261 | /** |
722c9a0c | 10262 | * alloc_netdev_mqs - allocate network device |
10263 | * @sizeof_priv: size of private data to allocate space for | |
10264 | * @name: device name format string | |
10265 | * @name_assign_type: origin of device name | |
10266 | * @setup: callback to initialize device | |
10267 | * @txqs: the number of TX subqueues to allocate | |
10268 | * @rxqs: the number of RX subqueues to allocate | |
10269 | * | |
10270 | * Allocates a struct net_device with private data area for driver use | |
10271 | * and performs basic initialization. Also allocates subqueue structs | |
10272 | * for each queue on the device. | |
1da177e4 | 10273 | */ |
36909ea4 | 10274 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
c835a677 | 10275 | unsigned char name_assign_type, |
36909ea4 TH |
10276 | void (*setup)(struct net_device *), |
10277 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 10278 | { |
1da177e4 | 10279 | struct net_device *dev; |
52a59bd5 | 10280 | unsigned int alloc_size; |
1ce8e7b5 | 10281 | struct net_device *p; |
1da177e4 | 10282 | |
b6fe17d6 SH |
10283 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
10284 | ||
36909ea4 | 10285 | if (txqs < 1) { |
7b6cd1ce | 10286 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
10287 | return NULL; |
10288 | } | |
10289 | ||
36909ea4 | 10290 | if (rxqs < 1) { |
7b6cd1ce | 10291 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
10292 | return NULL; |
10293 | } | |
36909ea4 | 10294 | |
fd2ea0a7 | 10295 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
10296 | if (sizeof_priv) { |
10297 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 10298 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
10299 | alloc_size += sizeof_priv; |
10300 | } | |
10301 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 10302 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 10303 | |
dcda9b04 | 10304 | p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
62b5942a | 10305 | if (!p) |
1da177e4 | 10306 | return NULL; |
1da177e4 | 10307 | |
1ce8e7b5 | 10308 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 10309 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 10310 | |
29b4433d ED |
10311 | dev->pcpu_refcnt = alloc_percpu(int); |
10312 | if (!dev->pcpu_refcnt) | |
74d332c1 | 10313 | goto free_dev; |
ab9c73cc | 10314 | |
ab9c73cc | 10315 | if (dev_addr_init(dev)) |
29b4433d | 10316 | goto free_pcpu; |
ab9c73cc | 10317 | |
22bedad3 | 10318 | dev_mc_init(dev); |
a748ee24 | 10319 | dev_uc_init(dev); |
ccffad25 | 10320 | |
c346dca1 | 10321 | dev_net_set(dev, &init_net); |
1da177e4 | 10322 | |
8d3bdbd5 | 10323 | dev->gso_max_size = GSO_MAX_SIZE; |
30b678d8 | 10324 | dev->gso_max_segs = GSO_MAX_SEGS; |
5343da4c TY |
10325 | dev->upper_level = 1; |
10326 | dev->lower_level = 1; | |
8d3bdbd5 | 10327 | |
8d3bdbd5 DM |
10328 | INIT_LIST_HEAD(&dev->napi_list); |
10329 | INIT_LIST_HEAD(&dev->unreg_list); | |
5cde2829 | 10330 | INIT_LIST_HEAD(&dev->close_list); |
8d3bdbd5 | 10331 | INIT_LIST_HEAD(&dev->link_watch_list); |
2f268f12 VF |
10332 | INIT_LIST_HEAD(&dev->adj_list.upper); |
10333 | INIT_LIST_HEAD(&dev->adj_list.lower); | |
7866a621 SN |
10334 | INIT_LIST_HEAD(&dev->ptype_all); |
10335 | INIT_LIST_HEAD(&dev->ptype_specific); | |
93642e14 | 10336 | INIT_LIST_HEAD(&dev->net_notifier_list); |
59cc1f61 JK |
10337 | #ifdef CONFIG_NET_SCHED |
10338 | hash_init(dev->qdisc_hash); | |
10339 | #endif | |
02875878 | 10340 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
8d3bdbd5 DM |
10341 | setup(dev); |
10342 | ||
a813104d | 10343 | if (!dev->tx_queue_len) { |
f84bb1ea | 10344 | dev->priv_flags |= IFF_NO_QUEUE; |
11597084 | 10345 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
a813104d | 10346 | } |
906470c1 | 10347 | |
36909ea4 TH |
10348 | dev->num_tx_queues = txqs; |
10349 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 10350 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 10351 | goto free_all; |
e8a0464c | 10352 | |
36909ea4 TH |
10353 | dev->num_rx_queues = rxqs; |
10354 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 10355 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 10356 | goto free_all; |
0a9627f2 | 10357 | |
1da177e4 | 10358 | strcpy(dev->name, name); |
c835a677 | 10359 | dev->name_assign_type = name_assign_type; |
cbda10fa | 10360 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
10361 | if (!dev->ethtool_ops) |
10362 | dev->ethtool_ops = &default_ethtool_ops; | |
e687ad60 | 10363 | |
357b6cc5 | 10364 | nf_hook_ingress_init(dev); |
e687ad60 | 10365 | |
1da177e4 | 10366 | return dev; |
ab9c73cc | 10367 | |
8d3bdbd5 DM |
10368 | free_all: |
10369 | free_netdev(dev); | |
10370 | return NULL; | |
10371 | ||
29b4433d ED |
10372 | free_pcpu: |
10373 | free_percpu(dev->pcpu_refcnt); | |
74d332c1 ED |
10374 | free_dev: |
10375 | netdev_freemem(dev); | |
ab9c73cc | 10376 | return NULL; |
1da177e4 | 10377 | } |
36909ea4 | 10378 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
10379 | |
10380 | /** | |
722c9a0c | 10381 | * free_netdev - free network device |
10382 | * @dev: device | |
1da177e4 | 10383 | * |
722c9a0c | 10384 | * This function does the last stage of destroying an allocated device |
10385 | * interface. The reference to the device object is released. If this | |
10386 | * is the last reference then it will be freed.Must be called in process | |
10387 | * context. | |
1da177e4 LT |
10388 | */ |
10389 | void free_netdev(struct net_device *dev) | |
10390 | { | |
d565b0a1 HX |
10391 | struct napi_struct *p, *n; |
10392 | ||
93d05d4a | 10393 | might_sleep(); |
60877a32 | 10394 | netif_free_tx_queues(dev); |
e817f856 | 10395 | netif_free_rx_queues(dev); |
e8a0464c | 10396 | |
33d480ce | 10397 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 10398 | |
f001fde5 JP |
10399 | /* Flush device addresses */ |
10400 | dev_addr_flush(dev); | |
10401 | ||
d565b0a1 HX |
10402 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
10403 | netif_napi_del(p); | |
10404 | ||
29b4433d ED |
10405 | free_percpu(dev->pcpu_refcnt); |
10406 | dev->pcpu_refcnt = NULL; | |
75ccae62 THJ |
10407 | free_percpu(dev->xdp_bulkq); |
10408 | dev->xdp_bulkq = NULL; | |
29b4433d | 10409 | |
3041a069 | 10410 | /* Compatibility with error handling in drivers */ |
1da177e4 | 10411 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
74d332c1 | 10412 | netdev_freemem(dev); |
1da177e4 LT |
10413 | return; |
10414 | } | |
10415 | ||
10416 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
10417 | dev->reg_state = NETREG_RELEASED; | |
10418 | ||
43cb76d9 GKH |
10419 | /* will free via device release */ |
10420 | put_device(&dev->dev); | |
1da177e4 | 10421 | } |
d1b19dff | 10422 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 10423 | |
f0db275a SH |
10424 | /** |
10425 | * synchronize_net - Synchronize with packet receive processing | |
10426 | * | |
10427 | * Wait for packets currently being received to be done. | |
10428 | * Does not block later packets from starting. | |
10429 | */ | |
4ec93edb | 10430 | void synchronize_net(void) |
1da177e4 LT |
10431 | { |
10432 | might_sleep(); | |
be3fc413 ED |
10433 | if (rtnl_is_locked()) |
10434 | synchronize_rcu_expedited(); | |
10435 | else | |
10436 | synchronize_rcu(); | |
1da177e4 | 10437 | } |
d1b19dff | 10438 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
10439 | |
10440 | /** | |
44a0873d | 10441 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 10442 | * @dev: device |
44a0873d | 10443 | * @head: list |
6ebfbc06 | 10444 | * |
1da177e4 | 10445 | * This function shuts down a device interface and removes it |
d59b54b1 | 10446 | * from the kernel tables. |
44a0873d | 10447 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
10448 | * |
10449 | * Callers must hold the rtnl semaphore. You may want | |
10450 | * unregister_netdev() instead of this. | |
10451 | */ | |
10452 | ||
44a0873d | 10453 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 10454 | { |
a6620712 HX |
10455 | ASSERT_RTNL(); |
10456 | ||
44a0873d | 10457 | if (head) { |
9fdce099 | 10458 | list_move_tail(&dev->unreg_list, head); |
44a0873d ED |
10459 | } else { |
10460 | rollback_registered(dev); | |
10461 | /* Finish processing unregister after unlock */ | |
10462 | net_set_todo(dev); | |
10463 | } | |
1da177e4 | 10464 | } |
44a0873d | 10465 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 10466 | |
9b5e383c ED |
10467 | /** |
10468 | * unregister_netdevice_many - unregister many devices | |
10469 | * @head: list of devices | |
87757a91 ED |
10470 | * |
10471 | * Note: As most callers use a stack allocated list_head, | |
10472 | * we force a list_del() to make sure stack wont be corrupted later. | |
9b5e383c ED |
10473 | */ |
10474 | void unregister_netdevice_many(struct list_head *head) | |
10475 | { | |
10476 | struct net_device *dev; | |
10477 | ||
10478 | if (!list_empty(head)) { | |
10479 | rollback_registered_many(head); | |
10480 | list_for_each_entry(dev, head, unreg_list) | |
10481 | net_set_todo(dev); | |
87757a91 | 10482 | list_del(head); |
9b5e383c ED |
10483 | } |
10484 | } | |
63c8099d | 10485 | EXPORT_SYMBOL(unregister_netdevice_many); |
9b5e383c | 10486 | |
1da177e4 LT |
10487 | /** |
10488 | * unregister_netdev - remove device from the kernel | |
10489 | * @dev: device | |
10490 | * | |
10491 | * This function shuts down a device interface and removes it | |
d59b54b1 | 10492 | * from the kernel tables. |
1da177e4 LT |
10493 | * |
10494 | * This is just a wrapper for unregister_netdevice that takes | |
10495 | * the rtnl semaphore. In general you want to use this and not | |
10496 | * unregister_netdevice. | |
10497 | */ | |
10498 | void unregister_netdev(struct net_device *dev) | |
10499 | { | |
10500 | rtnl_lock(); | |
10501 | unregister_netdevice(dev); | |
10502 | rtnl_unlock(); | |
10503 | } | |
1da177e4 LT |
10504 | EXPORT_SYMBOL(unregister_netdev); |
10505 | ||
ce286d32 EB |
10506 | /** |
10507 | * dev_change_net_namespace - move device to different nethost namespace | |
10508 | * @dev: device | |
10509 | * @net: network namespace | |
10510 | * @pat: If not NULL name pattern to try if the current device name | |
10511 | * is already taken in the destination network namespace. | |
10512 | * | |
10513 | * This function shuts down a device interface and moves it | |
10514 | * to a new network namespace. On success 0 is returned, on | |
10515 | * a failure a netagive errno code is returned. | |
10516 | * | |
10517 | * Callers must hold the rtnl semaphore. | |
10518 | */ | |
10519 | ||
10520 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) | |
10521 | { | |
ef6a4c88 | 10522 | struct net *net_old = dev_net(dev); |
38e01b30 | 10523 | int err, new_nsid, new_ifindex; |
ce286d32 EB |
10524 | |
10525 | ASSERT_RTNL(); | |
10526 | ||
10527 | /* Don't allow namespace local devices to be moved. */ | |
10528 | err = -EINVAL; | |
10529 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
10530 | goto out; | |
10531 | ||
10532 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
10533 | if (dev->reg_state != NETREG_REGISTERED) |
10534 | goto out; | |
10535 | ||
10536 | /* Get out if there is nothing todo */ | |
10537 | err = 0; | |
ef6a4c88 | 10538 | if (net_eq(net_old, net)) |
ce286d32 EB |
10539 | goto out; |
10540 | ||
10541 | /* Pick the destination device name, and ensure | |
10542 | * we can use it in the destination network namespace. | |
10543 | */ | |
10544 | err = -EEXIST; | |
d9031024 | 10545 | if (__dev_get_by_name(net, dev->name)) { |
ce286d32 EB |
10546 | /* We get here if we can't use the current device name */ |
10547 | if (!pat) | |
10548 | goto out; | |
7892bd08 LR |
10549 | err = dev_get_valid_name(net, dev, pat); |
10550 | if (err < 0) | |
ce286d32 EB |
10551 | goto out; |
10552 | } | |
10553 | ||
10554 | /* | |
10555 | * And now a mini version of register_netdevice unregister_netdevice. | |
10556 | */ | |
10557 | ||
10558 | /* If device is running close it first. */ | |
9b772652 | 10559 | dev_close(dev); |
ce286d32 EB |
10560 | |
10561 | /* And unlink it from device chain */ | |
ce286d32 EB |
10562 | unlist_netdevice(dev); |
10563 | ||
10564 | synchronize_net(); | |
10565 | ||
10566 | /* Shutdown queueing discipline. */ | |
10567 | dev_shutdown(dev); | |
10568 | ||
10569 | /* Notify protocols, that we are about to destroy | |
eb13da1a | 10570 | * this device. They should clean all the things. |
10571 | * | |
10572 | * Note that dev->reg_state stays at NETREG_REGISTERED. | |
10573 | * This is wanted because this way 8021q and macvlan know | |
10574 | * the device is just moving and can keep their slaves up. | |
10575 | */ | |
ce286d32 | 10576 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
6549dd43 | 10577 | rcu_barrier(); |
38e01b30 | 10578 | |
d4e4fdf9 | 10579 | new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); |
38e01b30 ND |
10580 | /* If there is an ifindex conflict assign a new one */ |
10581 | if (__dev_get_by_index(net, dev->ifindex)) | |
10582 | new_ifindex = dev_new_index(net); | |
10583 | else | |
10584 | new_ifindex = dev->ifindex; | |
10585 | ||
10586 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, | |
10587 | new_ifindex); | |
ce286d32 EB |
10588 | |
10589 | /* | |
10590 | * Flush the unicast and multicast chains | |
10591 | */ | |
a748ee24 | 10592 | dev_uc_flush(dev); |
22bedad3 | 10593 | dev_mc_flush(dev); |
ce286d32 | 10594 | |
4e66ae2e SH |
10595 | /* Send a netdev-removed uevent to the old namespace */ |
10596 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
4c75431a | 10597 | netdev_adjacent_del_links(dev); |
4e66ae2e | 10598 | |
93642e14 JP |
10599 | /* Move per-net netdevice notifiers that are following the netdevice */ |
10600 | move_netdevice_notifiers_dev_net(dev, net); | |
10601 | ||
ce286d32 | 10602 | /* Actually switch the network namespace */ |
c346dca1 | 10603 | dev_net_set(dev, net); |
38e01b30 | 10604 | dev->ifindex = new_ifindex; |
ce286d32 | 10605 | |
4e66ae2e SH |
10606 | /* Send a netdev-add uevent to the new namespace */ |
10607 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
4c75431a | 10608 | netdev_adjacent_add_links(dev); |
4e66ae2e | 10609 | |
8b41d188 | 10610 | /* Fixup kobjects */ |
a1b3f594 | 10611 | err = device_rename(&dev->dev, dev->name); |
8b41d188 | 10612 | WARN_ON(err); |
ce286d32 | 10613 | |
ef6a4c88 CB |
10614 | /* Adapt owner in case owning user namespace of target network |
10615 | * namespace is different from the original one. | |
10616 | */ | |
10617 | err = netdev_change_owner(dev, net_old, net); | |
10618 | WARN_ON(err); | |
10619 | ||
ce286d32 EB |
10620 | /* Add the device back in the hashes */ |
10621 | list_netdevice(dev); | |
10622 | ||
10623 | /* Notify protocols, that a new device appeared. */ | |
10624 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
10625 | ||
d90a909e EB |
10626 | /* |
10627 | * Prevent userspace races by waiting until the network | |
10628 | * device is fully setup before sending notifications. | |
10629 | */ | |
7f294054 | 10630 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
d90a909e | 10631 | |
ce286d32 EB |
10632 | synchronize_net(); |
10633 | err = 0; | |
10634 | out: | |
10635 | return err; | |
10636 | } | |
463d0183 | 10637 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
ce286d32 | 10638 | |
f0bf90de | 10639 | static int dev_cpu_dead(unsigned int oldcpu) |
1da177e4 LT |
10640 | { |
10641 | struct sk_buff **list_skb; | |
1da177e4 | 10642 | struct sk_buff *skb; |
f0bf90de | 10643 | unsigned int cpu; |
97d8b6e3 | 10644 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
1da177e4 | 10645 | |
1da177e4 LT |
10646 | local_irq_disable(); |
10647 | cpu = smp_processor_id(); | |
10648 | sd = &per_cpu(softnet_data, cpu); | |
10649 | oldsd = &per_cpu(softnet_data, oldcpu); | |
10650 | ||
10651 | /* Find end of our completion_queue. */ | |
10652 | list_skb = &sd->completion_queue; | |
10653 | while (*list_skb) | |
10654 | list_skb = &(*list_skb)->next; | |
10655 | /* Append completion queue from offline CPU. */ | |
10656 | *list_skb = oldsd->completion_queue; | |
10657 | oldsd->completion_queue = NULL; | |
10658 | ||
1da177e4 | 10659 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
10660 | if (oldsd->output_queue) { |
10661 | *sd->output_queue_tailp = oldsd->output_queue; | |
10662 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
10663 | oldsd->output_queue = NULL; | |
10664 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
10665 | } | |
ac64da0b ED |
10666 | /* Append NAPI poll list from offline CPU, with one exception : |
10667 | * process_backlog() must be called by cpu owning percpu backlog. | |
10668 | * We properly handle process_queue & input_pkt_queue later. | |
10669 | */ | |
10670 | while (!list_empty(&oldsd->poll_list)) { | |
10671 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, | |
10672 | struct napi_struct, | |
10673 | poll_list); | |
10674 | ||
10675 | list_del_init(&napi->poll_list); | |
10676 | if (napi->poll == process_backlog) | |
10677 | napi->state = 0; | |
10678 | else | |
10679 | ____napi_schedule(sd, napi); | |
264524d5 | 10680 | } |
1da177e4 LT |
10681 | |
10682 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
10683 | local_irq_enable(); | |
10684 | ||
773fc8f6 | 10685 | #ifdef CONFIG_RPS |
10686 | remsd = oldsd->rps_ipi_list; | |
10687 | oldsd->rps_ipi_list = NULL; | |
10688 | #endif | |
10689 | /* send out pending IPI's on offline CPU */ | |
10690 | net_rps_send_ipi(remsd); | |
10691 | ||
1da177e4 | 10692 | /* Process offline CPU's input_pkt_queue */ |
76cc8b13 | 10693 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
91e83133 | 10694 | netif_rx_ni(skb); |
76cc8b13 | 10695 | input_queue_head_incr(oldsd); |
fec5e652 | 10696 | } |
ac64da0b | 10697 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
91e83133 | 10698 | netif_rx_ni(skb); |
76cc8b13 TH |
10699 | input_queue_head_incr(oldsd); |
10700 | } | |
1da177e4 | 10701 | |
f0bf90de | 10702 | return 0; |
1da177e4 | 10703 | } |
1da177e4 | 10704 | |
7f353bf2 | 10705 | /** |
b63365a2 HX |
10706 | * netdev_increment_features - increment feature set by one |
10707 | * @all: current feature set | |
10708 | * @one: new feature set | |
10709 | * @mask: mask feature set | |
7f353bf2 HX |
10710 | * |
10711 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
10712 | * @one to the master device with current feature set @all. Will not |
10713 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 10714 | */ |
c8f44aff MM |
10715 | netdev_features_t netdev_increment_features(netdev_features_t all, |
10716 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 10717 | { |
c8cd0989 | 10718 | if (mask & NETIF_F_HW_CSUM) |
a188222b | 10719 | mask |= NETIF_F_CSUM_MASK; |
1742f183 | 10720 | mask |= NETIF_F_VLAN_CHALLENGED; |
7f353bf2 | 10721 | |
a188222b | 10722 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
1742f183 | 10723 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
c6e1a0d1 | 10724 | |
1742f183 | 10725 | /* If one device supports hw checksumming, set for all. */ |
c8cd0989 TH |
10726 | if (all & NETIF_F_HW_CSUM) |
10727 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); | |
7f353bf2 HX |
10728 | |
10729 | return all; | |
10730 | } | |
b63365a2 | 10731 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 10732 | |
430f03cd | 10733 | static struct hlist_head * __net_init netdev_create_hash(void) |
30d97d35 PE |
10734 | { |
10735 | int i; | |
10736 | struct hlist_head *hash; | |
10737 | ||
6da2ec56 | 10738 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
30d97d35 PE |
10739 | if (hash != NULL) |
10740 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
10741 | INIT_HLIST_HEAD(&hash[i]); | |
10742 | ||
10743 | return hash; | |
10744 | } | |
10745 | ||
881d966b | 10746 | /* Initialize per network namespace state */ |
4665079c | 10747 | static int __net_init netdev_init(struct net *net) |
881d966b | 10748 | { |
d9f37d01 | 10749 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
c593642c | 10750 | 8 * sizeof_field(struct napi_struct, gro_bitmask)); |
d9f37d01 | 10751 | |
734b6541 RM |
10752 | if (net != &init_net) |
10753 | INIT_LIST_HEAD(&net->dev_base_head); | |
881d966b | 10754 | |
30d97d35 PE |
10755 | net->dev_name_head = netdev_create_hash(); |
10756 | if (net->dev_name_head == NULL) | |
10757 | goto err_name; | |
881d966b | 10758 | |
30d97d35 PE |
10759 | net->dev_index_head = netdev_create_hash(); |
10760 | if (net->dev_index_head == NULL) | |
10761 | goto err_idx; | |
881d966b | 10762 | |
a30c7b42 JP |
10763 | RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain); |
10764 | ||
881d966b | 10765 | return 0; |
30d97d35 PE |
10766 | |
10767 | err_idx: | |
10768 | kfree(net->dev_name_head); | |
10769 | err_name: | |
10770 | return -ENOMEM; | |
881d966b EB |
10771 | } |
10772 | ||
f0db275a SH |
10773 | /** |
10774 | * netdev_drivername - network driver for the device | |
10775 | * @dev: network device | |
f0db275a SH |
10776 | * |
10777 | * Determine network driver for device. | |
10778 | */ | |
3019de12 | 10779 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 10780 | { |
cf04a4c7 SH |
10781 | const struct device_driver *driver; |
10782 | const struct device *parent; | |
3019de12 | 10783 | const char *empty = ""; |
6579e57b AV |
10784 | |
10785 | parent = dev->dev.parent; | |
6579e57b | 10786 | if (!parent) |
3019de12 | 10787 | return empty; |
6579e57b AV |
10788 | |
10789 | driver = parent->driver; | |
10790 | if (driver && driver->name) | |
3019de12 DM |
10791 | return driver->name; |
10792 | return empty; | |
6579e57b AV |
10793 | } |
10794 | ||
6ea754eb JP |
10795 | static void __netdev_printk(const char *level, const struct net_device *dev, |
10796 | struct va_format *vaf) | |
256df2f3 | 10797 | { |
b004ff49 | 10798 | if (dev && dev->dev.parent) { |
6ea754eb JP |
10799 | dev_printk_emit(level[1] - '0', |
10800 | dev->dev.parent, | |
10801 | "%s %s %s%s: %pV", | |
10802 | dev_driver_string(dev->dev.parent), | |
10803 | dev_name(dev->dev.parent), | |
10804 | netdev_name(dev), netdev_reg_state(dev), | |
10805 | vaf); | |
b004ff49 | 10806 | } else if (dev) { |
6ea754eb JP |
10807 | printk("%s%s%s: %pV", |
10808 | level, netdev_name(dev), netdev_reg_state(dev), vaf); | |
b004ff49 | 10809 | } else { |
6ea754eb | 10810 | printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 10811 | } |
256df2f3 JP |
10812 | } |
10813 | ||
6ea754eb JP |
10814 | void netdev_printk(const char *level, const struct net_device *dev, |
10815 | const char *format, ...) | |
256df2f3 JP |
10816 | { |
10817 | struct va_format vaf; | |
10818 | va_list args; | |
256df2f3 JP |
10819 | |
10820 | va_start(args, format); | |
10821 | ||
10822 | vaf.fmt = format; | |
10823 | vaf.va = &args; | |
10824 | ||
6ea754eb | 10825 | __netdev_printk(level, dev, &vaf); |
b004ff49 | 10826 | |
256df2f3 | 10827 | va_end(args); |
256df2f3 JP |
10828 | } |
10829 | EXPORT_SYMBOL(netdev_printk); | |
10830 | ||
10831 | #define define_netdev_printk_level(func, level) \ | |
6ea754eb | 10832 | void func(const struct net_device *dev, const char *fmt, ...) \ |
256df2f3 | 10833 | { \ |
256df2f3 JP |
10834 | struct va_format vaf; \ |
10835 | va_list args; \ | |
10836 | \ | |
10837 | va_start(args, fmt); \ | |
10838 | \ | |
10839 | vaf.fmt = fmt; \ | |
10840 | vaf.va = &args; \ | |
10841 | \ | |
6ea754eb | 10842 | __netdev_printk(level, dev, &vaf); \ |
b004ff49 | 10843 | \ |
256df2f3 | 10844 | va_end(args); \ |
256df2f3 JP |
10845 | } \ |
10846 | EXPORT_SYMBOL(func); | |
10847 | ||
10848 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
10849 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
10850 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
10851 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
10852 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
10853 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
10854 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
10855 | ||
4665079c | 10856 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
10857 | { |
10858 | kfree(net->dev_name_head); | |
10859 | kfree(net->dev_index_head); | |
ee21b18b VA |
10860 | if (net != &init_net) |
10861 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); | |
881d966b EB |
10862 | } |
10863 | ||
022cbae6 | 10864 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
10865 | .init = netdev_init, |
10866 | .exit = netdev_exit, | |
10867 | }; | |
10868 | ||
4665079c | 10869 | static void __net_exit default_device_exit(struct net *net) |
ce286d32 | 10870 | { |
e008b5fc | 10871 | struct net_device *dev, *aux; |
ce286d32 | 10872 | /* |
e008b5fc | 10873 | * Push all migratable network devices back to the |
ce286d32 EB |
10874 | * initial network namespace |
10875 | */ | |
10876 | rtnl_lock(); | |
e008b5fc | 10877 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 10878 | int err; |
aca51397 | 10879 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
10880 | |
10881 | /* Ignore unmoveable devices (i.e. loopback) */ | |
10882 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
10883 | continue; | |
10884 | ||
e008b5fc EB |
10885 | /* Leave virtual devices for the generic cleanup */ |
10886 | if (dev->rtnl_link_ops) | |
10887 | continue; | |
d0c082ce | 10888 | |
25985edc | 10889 | /* Push remaining network devices to init_net */ |
aca51397 | 10890 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
55b40dbf JP |
10891 | if (__dev_get_by_name(&init_net, fb_name)) |
10892 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); | |
aca51397 | 10893 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
ce286d32 | 10894 | if (err) { |
7b6cd1ce JP |
10895 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
10896 | __func__, dev->name, err); | |
aca51397 | 10897 | BUG(); |
ce286d32 EB |
10898 | } |
10899 | } | |
10900 | rtnl_unlock(); | |
10901 | } | |
10902 | ||
50624c93 EB |
10903 | static void __net_exit rtnl_lock_unregistering(struct list_head *net_list) |
10904 | { | |
10905 | /* Return with the rtnl_lock held when there are no network | |
10906 | * devices unregistering in any network namespace in net_list. | |
10907 | */ | |
10908 | struct net *net; | |
10909 | bool unregistering; | |
ff960a73 | 10910 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
50624c93 | 10911 | |
ff960a73 | 10912 | add_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 | 10913 | for (;;) { |
50624c93 EB |
10914 | unregistering = false; |
10915 | rtnl_lock(); | |
10916 | list_for_each_entry(net, net_list, exit_list) { | |
10917 | if (net->dev_unreg_count > 0) { | |
10918 | unregistering = true; | |
10919 | break; | |
10920 | } | |
10921 | } | |
10922 | if (!unregistering) | |
10923 | break; | |
10924 | __rtnl_unlock(); | |
ff960a73 PZ |
10925 | |
10926 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
50624c93 | 10927 | } |
ff960a73 | 10928 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 EB |
10929 | } |
10930 | ||
04dc7f6b EB |
10931 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
10932 | { | |
10933 | /* At exit all network devices most be removed from a network | |
b595076a | 10934 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
10935 | * Do this across as many network namespaces as possible to |
10936 | * improve batching efficiency. | |
10937 | */ | |
10938 | struct net_device *dev; | |
10939 | struct net *net; | |
10940 | LIST_HEAD(dev_kill_list); | |
10941 | ||
50624c93 EB |
10942 | /* To prevent network device cleanup code from dereferencing |
10943 | * loopback devices or network devices that have been freed | |
10944 | * wait here for all pending unregistrations to complete, | |
10945 | * before unregistring the loopback device and allowing the | |
10946 | * network namespace be freed. | |
10947 | * | |
10948 | * The netdev todo list containing all network devices | |
10949 | * unregistrations that happen in default_device_exit_batch | |
10950 | * will run in the rtnl_unlock() at the end of | |
10951 | * default_device_exit_batch. | |
10952 | */ | |
10953 | rtnl_lock_unregistering(net_list); | |
04dc7f6b EB |
10954 | list_for_each_entry(net, net_list, exit_list) { |
10955 | for_each_netdev_reverse(net, dev) { | |
b0ab2fab | 10956 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
04dc7f6b EB |
10957 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
10958 | else | |
10959 | unregister_netdevice_queue(dev, &dev_kill_list); | |
10960 | } | |
10961 | } | |
10962 | unregister_netdevice_many(&dev_kill_list); | |
10963 | rtnl_unlock(); | |
10964 | } | |
10965 | ||
022cbae6 | 10966 | static struct pernet_operations __net_initdata default_device_ops = { |
ce286d32 | 10967 | .exit = default_device_exit, |
04dc7f6b | 10968 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
10969 | }; |
10970 | ||
1da177e4 LT |
10971 | /* |
10972 | * Initialize the DEV module. At boot time this walks the device list and | |
10973 | * unhooks any devices that fail to initialise (normally hardware not | |
10974 | * present) and leaves us with a valid list of present and active devices. | |
10975 | * | |
10976 | */ | |
10977 | ||
10978 | /* | |
10979 | * This is called single threaded during boot, so no need | |
10980 | * to take the rtnl semaphore. | |
10981 | */ | |
10982 | static int __init net_dev_init(void) | |
10983 | { | |
10984 | int i, rc = -ENOMEM; | |
10985 | ||
10986 | BUG_ON(!dev_boot_phase); | |
10987 | ||
1da177e4 LT |
10988 | if (dev_proc_init()) |
10989 | goto out; | |
10990 | ||
8b41d188 | 10991 | if (netdev_kobject_init()) |
1da177e4 LT |
10992 | goto out; |
10993 | ||
10994 | INIT_LIST_HEAD(&ptype_all); | |
82d8a867 | 10995 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
10996 | INIT_LIST_HEAD(&ptype_base[i]); |
10997 | ||
62532da9 VY |
10998 | INIT_LIST_HEAD(&offload_base); |
10999 | ||
881d966b EB |
11000 | if (register_pernet_subsys(&netdev_net_ops)) |
11001 | goto out; | |
1da177e4 LT |
11002 | |
11003 | /* | |
11004 | * Initialise the packet receive queues. | |
11005 | */ | |
11006 | ||
6f912042 | 11007 | for_each_possible_cpu(i) { |
41852497 | 11008 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
e36fa2f7 | 11009 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 11010 | |
41852497 ED |
11011 | INIT_WORK(flush, flush_backlog); |
11012 | ||
e36fa2f7 | 11013 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 11014 | skb_queue_head_init(&sd->process_queue); |
f53c7239 SK |
11015 | #ifdef CONFIG_XFRM_OFFLOAD |
11016 | skb_queue_head_init(&sd->xfrm_backlog); | |
11017 | #endif | |
e36fa2f7 | 11018 | INIT_LIST_HEAD(&sd->poll_list); |
a9cbd588 | 11019 | sd->output_queue_tailp = &sd->output_queue; |
df334545 | 11020 | #ifdef CONFIG_RPS |
e36fa2f7 ED |
11021 | sd->csd.func = rps_trigger_softirq; |
11022 | sd->csd.info = sd; | |
e36fa2f7 | 11023 | sd->cpu = i; |
1e94d72f | 11024 | #endif |
0a9627f2 | 11025 | |
7c4ec749 | 11026 | init_gro_hash(&sd->backlog); |
e36fa2f7 ED |
11027 | sd->backlog.poll = process_backlog; |
11028 | sd->backlog.weight = weight_p; | |
1da177e4 LT |
11029 | } |
11030 | ||
1da177e4 LT |
11031 | dev_boot_phase = 0; |
11032 | ||
505d4f73 EB |
11033 | /* The loopback device is special if any other network devices |
11034 | * is present in a network namespace the loopback device must | |
11035 | * be present. Since we now dynamically allocate and free the | |
11036 | * loopback device ensure this invariant is maintained by | |
11037 | * keeping the loopback device as the first device on the | |
11038 | * list of network devices. Ensuring the loopback devices | |
11039 | * is the first device that appears and the last network device | |
11040 | * that disappears. | |
11041 | */ | |
11042 | if (register_pernet_device(&loopback_net_ops)) | |
11043 | goto out; | |
11044 | ||
11045 | if (register_pernet_device(&default_device_ops)) | |
11046 | goto out; | |
11047 | ||
962cf36c CM |
11048 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
11049 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 | 11050 | |
f0bf90de SAS |
11051 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
11052 | NULL, dev_cpu_dead); | |
11053 | WARN_ON(rc < 0); | |
1da177e4 LT |
11054 | rc = 0; |
11055 | out: | |
11056 | return rc; | |
11057 | } | |
11058 | ||
11059 | subsys_initcall(net_dev_init); |