]>
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, <[email protected]> |
8 | * Mark Evans, <[email protected]> | |
9 | * | |
10 | * Additional Authors: | |
11 | * Florian la Roche <[email protected]> | |
12 | * Alan Cox <[email protected]> | |
13 | * David Hinds <[email protected]> | |
14 | * Alexey Kuznetsov <[email protected]> | |
15 | * Adam Sulmicki <[email protected]> | |
16 | * Pekka Riikonen <[email protected]> | |
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> |
29863d41 | 94 | #include <linux/kthread.h> |
a7862b45 | 95 | #include <linux/bpf.h> |
b5cdae32 | 96 | #include <linux/bpf_trace.h> |
457c4cbc | 97 | #include <net/net_namespace.h> |
1da177e4 | 98 | #include <net/sock.h> |
02d62e86 | 99 | #include <net/busy_poll.h> |
1da177e4 | 100 | #include <linux/rtnetlink.h> |
1da177e4 | 101 | #include <linux/stat.h> |
b14a9fc4 | 102 | #include <net/dsa.h> |
1da177e4 | 103 | #include <net/dst.h> |
fc4099f1 | 104 | #include <net/dst_metadata.h> |
04f00ab2 | 105 | #include <net/gro.h> |
1da177e4 | 106 | #include <net/pkt_sched.h> |
87d83093 | 107 | #include <net/pkt_cls.h> |
1da177e4 | 108 | #include <net/checksum.h> |
44540960 | 109 | #include <net/xfrm.h> |
1da177e4 LT |
110 | #include <linux/highmem.h> |
111 | #include <linux/init.h> | |
1da177e4 | 112 | #include <linux/module.h> |
1da177e4 LT |
113 | #include <linux/netpoll.h> |
114 | #include <linux/rcupdate.h> | |
115 | #include <linux/delay.h> | |
1da177e4 | 116 | #include <net/iw_handler.h> |
1da177e4 | 117 | #include <asm/current.h> |
5bdb9886 | 118 | #include <linux/audit.h> |
db217334 | 119 | #include <linux/dmaengine.h> |
f6a78bfc | 120 | #include <linux/err.h> |
c7fa9d18 | 121 | #include <linux/ctype.h> |
723e98b7 | 122 | #include <linux/if_arp.h> |
6de329e2 | 123 | #include <linux/if_vlan.h> |
8f0f2223 | 124 | #include <linux/ip.h> |
ad55dcaf | 125 | #include <net/ip.h> |
25cd9ba0 | 126 | #include <net/mpls.h> |
8f0f2223 DM |
127 | #include <linux/ipv6.h> |
128 | #include <linux/in.h> | |
b6b2fed1 DM |
129 | #include <linux/jhash.h> |
130 | #include <linux/random.h> | |
9cbc1cb8 | 131 | #include <trace/events/napi.h> |
cf66ba58 | 132 | #include <trace/events/net.h> |
07dc22e7 | 133 | #include <trace/events/skb.h> |
70713ddd | 134 | #include <trace/events/qdisc.h> |
caeda9b9 | 135 | #include <linux/inetdevice.h> |
c445477d | 136 | #include <linux/cpu_rmap.h> |
c5905afb | 137 | #include <linux/static_key.h> |
af12fa6e | 138 | #include <linux/hashtable.h> |
60877a32 | 139 | #include <linux/vmalloc.h> |
529d0489 | 140 | #include <linux/if_macvlan.h> |
e7fd2885 | 141 | #include <linux/errqueue.h> |
3b47d303 | 142 | #include <linux/hrtimer.h> |
7463acfb | 143 | #include <linux/netfilter_netdev.h> |
40e4e713 | 144 | #include <linux/crash_dump.h> |
b72b5bf6 | 145 | #include <linux/sctp.h> |
ae847f40 | 146 | #include <net/udp_tunnel.h> |
6621dd29 | 147 | #include <linux/net_namespace.h> |
aaa5d90b | 148 | #include <linux/indirect_call_wrapper.h> |
af3836df | 149 | #include <net/devlink.h> |
bd869245 | 150 | #include <linux/pm_runtime.h> |
3744741a | 151 | #include <linux/prandom.h> |
127d7355 | 152 | #include <linux/once_lite.h> |
1da177e4 | 153 | |
342709ef PE |
154 | #include "net-sysfs.h" |
155 | ||
5d38a079 | 156 | |
1da177e4 | 157 | static DEFINE_SPINLOCK(ptype_lock); |
900ff8c6 CW |
158 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
159 | struct list_head ptype_all __read_mostly; /* Taps */ | |
1da177e4 | 160 | |
ae78dbfa | 161 | static int netif_rx_internal(struct sk_buff *skb); |
54951194 | 162 | static int call_netdevice_notifiers_info(unsigned long val, |
54951194 | 163 | struct netdev_notifier_info *info); |
26372605 PM |
164 | static int call_netdevice_notifiers_extack(unsigned long val, |
165 | struct net_device *dev, | |
166 | struct netlink_ext_ack *extack); | |
90b602f8 | 167 | static struct napi_struct *napi_by_id(unsigned int napi_id); |
ae78dbfa | 168 | |
1da177e4 | 169 | /* |
7562f876 | 170 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
1da177e4 LT |
171 | * semaphore. |
172 | * | |
c6d14c84 | 173 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
1da177e4 LT |
174 | * |
175 | * Writers must hold the rtnl semaphore while they loop through the | |
7562f876 | 176 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
1da177e4 LT |
177 | * actual updates. This allows pure readers to access the list even |
178 | * while a writer is preparing to update it. | |
179 | * | |
180 | * To put it another way, dev_base_lock is held for writing only to | |
181 | * protect against pure readers; the rtnl semaphore provides the | |
182 | * protection against other writers. | |
183 | * | |
184 | * See, for example usages, register_netdevice() and | |
185 | * unregister_netdevice(), which must be called with the rtnl | |
186 | * semaphore held. | |
187 | */ | |
1da177e4 | 188 | DEFINE_RWLOCK(dev_base_lock); |
1da177e4 LT |
189 | EXPORT_SYMBOL(dev_base_lock); |
190 | ||
6c557001 FW |
191 | static DEFINE_MUTEX(ifalias_mutex); |
192 | ||
af12fa6e ET |
193 | /* protects napi_hash addition/deletion and napi_gen_id */ |
194 | static DEFINE_SPINLOCK(napi_hash_lock); | |
195 | ||
52bd2d62 | 196 | static unsigned int napi_gen_id = NR_CPUS; |
6180d9de | 197 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
af12fa6e | 198 | |
11d6011c | 199 | static DECLARE_RWSEM(devnet_rename_sem); |
c91f6df2 | 200 | |
4e985ada TG |
201 | static inline void dev_base_seq_inc(struct net *net) |
202 | { | |
643aa9cb | 203 | while (++net->dev_base_seq == 0) |
204 | ; | |
4e985ada TG |
205 | } |
206 | ||
881d966b | 207 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 208 | { |
8387ff25 | 209 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
95c96174 | 210 | |
08e9897d | 211 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
212 | } |
213 | ||
881d966b | 214 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 215 | { |
7c28bd0b | 216 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
217 | } |
218 | ||
e722db8d SAS |
219 | static inline void rps_lock_irqsave(struct softnet_data *sd, |
220 | unsigned long *flags) | |
152102c7 | 221 | { |
e722db8d SAS |
222 | if (IS_ENABLED(CONFIG_RPS)) |
223 | spin_lock_irqsave(&sd->input_pkt_queue.lock, *flags); | |
224 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
225 | local_irq_save(*flags); | |
152102c7 CG |
226 | } |
227 | ||
e722db8d | 228 | static inline void rps_lock_irq_disable(struct softnet_data *sd) |
152102c7 | 229 | { |
e722db8d SAS |
230 | if (IS_ENABLED(CONFIG_RPS)) |
231 | spin_lock_irq(&sd->input_pkt_queue.lock); | |
232 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
233 | local_irq_disable(); | |
234 | } | |
235 | ||
236 | static inline void rps_unlock_irq_restore(struct softnet_data *sd, | |
237 | unsigned long *flags) | |
238 | { | |
239 | if (IS_ENABLED(CONFIG_RPS)) | |
240 | spin_unlock_irqrestore(&sd->input_pkt_queue.lock, *flags); | |
241 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
242 | local_irq_restore(*flags); | |
243 | } | |
244 | ||
245 | static inline void rps_unlock_irq_enable(struct softnet_data *sd) | |
246 | { | |
247 | if (IS_ENABLED(CONFIG_RPS)) | |
248 | spin_unlock_irq(&sd->input_pkt_queue.lock); | |
249 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
250 | local_irq_enable(); | |
152102c7 CG |
251 | } |
252 | ||
ff927412 JP |
253 | static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, |
254 | const char *name) | |
255 | { | |
256 | struct netdev_name_node *name_node; | |
257 | ||
258 | name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); | |
259 | if (!name_node) | |
260 | return NULL; | |
261 | INIT_HLIST_NODE(&name_node->hlist); | |
262 | name_node->dev = dev; | |
263 | name_node->name = name; | |
264 | return name_node; | |
265 | } | |
266 | ||
267 | static struct netdev_name_node * | |
268 | netdev_name_node_head_alloc(struct net_device *dev) | |
269 | { | |
36fbf1e5 JP |
270 | struct netdev_name_node *name_node; |
271 | ||
272 | name_node = netdev_name_node_alloc(dev, dev->name); | |
273 | if (!name_node) | |
274 | return NULL; | |
275 | INIT_LIST_HEAD(&name_node->list); | |
276 | return name_node; | |
ff927412 JP |
277 | } |
278 | ||
279 | static void netdev_name_node_free(struct netdev_name_node *name_node) | |
280 | { | |
281 | kfree(name_node); | |
282 | } | |
283 | ||
284 | static void netdev_name_node_add(struct net *net, | |
285 | struct netdev_name_node *name_node) | |
286 | { | |
287 | hlist_add_head_rcu(&name_node->hlist, | |
288 | dev_name_hash(net, name_node->name)); | |
289 | } | |
290 | ||
291 | static void netdev_name_node_del(struct netdev_name_node *name_node) | |
292 | { | |
293 | hlist_del_rcu(&name_node->hlist); | |
294 | } | |
295 | ||
296 | static struct netdev_name_node *netdev_name_node_lookup(struct net *net, | |
297 | const char *name) | |
298 | { | |
299 | struct hlist_head *head = dev_name_hash(net, name); | |
300 | struct netdev_name_node *name_node; | |
301 | ||
302 | hlist_for_each_entry(name_node, head, hlist) | |
303 | if (!strcmp(name_node->name, name)) | |
304 | return name_node; | |
305 | return NULL; | |
306 | } | |
307 | ||
308 | static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, | |
309 | const char *name) | |
310 | { | |
311 | struct hlist_head *head = dev_name_hash(net, name); | |
312 | struct netdev_name_node *name_node; | |
313 | ||
314 | hlist_for_each_entry_rcu(name_node, head, hlist) | |
315 | if (!strcmp(name_node->name, name)) | |
316 | return name_node; | |
317 | return NULL; | |
318 | } | |
319 | ||
75ea27d0 AT |
320 | bool netdev_name_in_use(struct net *net, const char *name) |
321 | { | |
322 | return netdev_name_node_lookup(net, name); | |
323 | } | |
324 | EXPORT_SYMBOL(netdev_name_in_use); | |
325 | ||
36fbf1e5 JP |
326 | int netdev_name_node_alt_create(struct net_device *dev, const char *name) |
327 | { | |
328 | struct netdev_name_node *name_node; | |
329 | struct net *net = dev_net(dev); | |
330 | ||
331 | name_node = netdev_name_node_lookup(net, name); | |
332 | if (name_node) | |
333 | return -EEXIST; | |
334 | name_node = netdev_name_node_alloc(dev, name); | |
335 | if (!name_node) | |
336 | return -ENOMEM; | |
337 | netdev_name_node_add(net, name_node); | |
338 | /* The node that holds dev->name acts as a head of per-device list. */ | |
339 | list_add_tail(&name_node->list, &dev->name_node->list); | |
340 | ||
341 | return 0; | |
342 | } | |
36fbf1e5 JP |
343 | |
344 | static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node) | |
345 | { | |
346 | list_del(&name_node->list); | |
347 | netdev_name_node_del(name_node); | |
348 | kfree(name_node->name); | |
349 | netdev_name_node_free(name_node); | |
350 | } | |
351 | ||
352 | int netdev_name_node_alt_destroy(struct net_device *dev, const char *name) | |
353 | { | |
354 | struct netdev_name_node *name_node; | |
355 | struct net *net = dev_net(dev); | |
356 | ||
357 | name_node = netdev_name_node_lookup(net, name); | |
358 | if (!name_node) | |
359 | return -ENOENT; | |
e08ad805 ED |
360 | /* lookup might have found our primary name or a name belonging |
361 | * to another device. | |
362 | */ | |
363 | if (name_node == dev->name_node || name_node->dev != dev) | |
364 | return -EINVAL; | |
365 | ||
36fbf1e5 JP |
366 | __netdev_name_node_alt_destroy(name_node); |
367 | ||
368 | return 0; | |
369 | } | |
36fbf1e5 JP |
370 | |
371 | static void netdev_name_node_alt_flush(struct net_device *dev) | |
372 | { | |
373 | struct netdev_name_node *name_node, *tmp; | |
374 | ||
375 | list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) | |
376 | __netdev_name_node_alt_destroy(name_node); | |
377 | } | |
378 | ||
ce286d32 | 379 | /* Device list insertion */ |
53759be9 | 380 | static void list_netdevice(struct net_device *dev) |
ce286d32 | 381 | { |
c346dca1 | 382 | struct net *net = dev_net(dev); |
ce286d32 EB |
383 | |
384 | ASSERT_RTNL(); | |
385 | ||
fd888e85 | 386 | write_lock(&dev_base_lock); |
c6d14c84 | 387 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
ff927412 | 388 | netdev_name_node_add(net, dev->name_node); |
fb699dfd ED |
389 | hlist_add_head_rcu(&dev->index_hlist, |
390 | dev_index_hash(net, dev->ifindex)); | |
fd888e85 | 391 | write_unlock(&dev_base_lock); |
4e985ada TG |
392 | |
393 | dev_base_seq_inc(net); | |
ce286d32 EB |
394 | } |
395 | ||
fb699dfd ED |
396 | /* Device list removal |
397 | * caller must respect a RCU grace period before freeing/reusing dev | |
398 | */ | |
ce286d32 EB |
399 | static void unlist_netdevice(struct net_device *dev) |
400 | { | |
401 | ASSERT_RTNL(); | |
402 | ||
403 | /* Unlink dev from the device chain */ | |
fd888e85 | 404 | write_lock(&dev_base_lock); |
c6d14c84 | 405 | list_del_rcu(&dev->dev_list); |
ff927412 | 406 | netdev_name_node_del(dev->name_node); |
fb699dfd | 407 | hlist_del_rcu(&dev->index_hlist); |
fd888e85 | 408 | write_unlock(&dev_base_lock); |
4e985ada TG |
409 | |
410 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
411 | } |
412 | ||
1da177e4 LT |
413 | /* |
414 | * Our notifier list | |
415 | */ | |
416 | ||
f07d5b94 | 417 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
418 | |
419 | /* | |
420 | * Device drivers call our routines to queue packets here. We empty the | |
421 | * queue in the local softnet handler. | |
422 | */ | |
bea3348e | 423 | |
9958da05 | 424 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 425 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 426 | |
1a33e10e CW |
427 | #ifdef CONFIG_LOCKDEP |
428 | /* | |
429 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class | |
430 | * according to dev->type | |
431 | */ | |
432 | static const unsigned short netdev_lock_type[] = { | |
433 | ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, | |
434 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, | |
435 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, | |
436 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, | |
437 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, | |
438 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, | |
439 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, | |
440 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, | |
441 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, | |
442 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, | |
443 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, | |
444 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, | |
445 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, | |
446 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, | |
447 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; | |
448 | ||
449 | static const char *const netdev_lock_name[] = { | |
450 | "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", | |
451 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", | |
452 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", | |
453 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", | |
454 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", | |
455 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", | |
456 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", | |
457 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", | |
458 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", | |
459 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", | |
460 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", | |
461 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", | |
462 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", | |
463 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", | |
464 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; | |
465 | ||
466 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; | |
845e0ebb | 467 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
1a33e10e CW |
468 | |
469 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) | |
470 | { | |
471 | int i; | |
472 | ||
473 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) | |
474 | if (netdev_lock_type[i] == dev_type) | |
475 | return i; | |
476 | /* the last key is used by default */ | |
477 | return ARRAY_SIZE(netdev_lock_type) - 1; | |
478 | } | |
479 | ||
480 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
481 | unsigned short dev_type) | |
482 | { | |
483 | int i; | |
484 | ||
485 | i = netdev_lock_pos(dev_type); | |
486 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], | |
487 | netdev_lock_name[i]); | |
488 | } | |
845e0ebb CW |
489 | |
490 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
491 | { | |
492 | int i; | |
493 | ||
494 | i = netdev_lock_pos(dev->type); | |
495 | lockdep_set_class_and_name(&dev->addr_list_lock, | |
496 | &netdev_addr_lock_key[i], | |
497 | netdev_lock_name[i]); | |
498 | } | |
1a33e10e CW |
499 | #else |
500 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
501 | unsigned short dev_type) | |
502 | { | |
503 | } | |
845e0ebb CW |
504 | |
505 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
506 | { | |
507 | } | |
1a33e10e CW |
508 | #endif |
509 | ||
1da177e4 | 510 | /******************************************************************************* |
eb13da1a | 511 | * |
512 | * Protocol management and registration routines | |
513 | * | |
514 | *******************************************************************************/ | |
1da177e4 | 515 | |
1da177e4 | 516 | |
1da177e4 LT |
517 | /* |
518 | * Add a protocol ID to the list. Now that the input handler is | |
519 | * smarter we can dispense with all the messy stuff that used to be | |
520 | * here. | |
521 | * | |
522 | * BEWARE!!! Protocol handlers, mangling input packets, | |
523 | * MUST BE last in hash buckets and checking protocol handlers | |
524 | * MUST start from promiscuous ptype_all chain in net_bh. | |
525 | * It is true now, do not change it. | |
526 | * Explanation follows: if protocol handler, mangling packet, will | |
527 | * be the first on list, it is not able to sense, that packet | |
528 | * is cloned and should be copied-on-write, so that it will | |
529 | * change it and subsequent readers will get broken packet. | |
530 | * --ANK (980803) | |
531 | */ | |
532 | ||
c07b68e8 ED |
533 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
534 | { | |
535 | if (pt->type == htons(ETH_P_ALL)) | |
7866a621 | 536 | return pt->dev ? &pt->dev->ptype_all : &ptype_all; |
c07b68e8 | 537 | else |
7866a621 SN |
538 | return pt->dev ? &pt->dev->ptype_specific : |
539 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
c07b68e8 ED |
540 | } |
541 | ||
1da177e4 LT |
542 | /** |
543 | * dev_add_pack - add packet handler | |
544 | * @pt: packet type declaration | |
545 | * | |
546 | * Add a protocol handler to the networking stack. The passed &packet_type | |
547 | * is linked into kernel lists and may not be freed until it has been | |
548 | * removed from the kernel lists. | |
549 | * | |
4ec93edb | 550 | * This call does not sleep therefore it can not |
1da177e4 LT |
551 | * guarantee all CPU's that are in middle of receiving packets |
552 | * will see the new packet type (until the next received packet). | |
553 | */ | |
554 | ||
555 | void dev_add_pack(struct packet_type *pt) | |
556 | { | |
c07b68e8 | 557 | struct list_head *head = ptype_head(pt); |
1da177e4 | 558 | |
c07b68e8 ED |
559 | spin_lock(&ptype_lock); |
560 | list_add_rcu(&pt->list, head); | |
561 | spin_unlock(&ptype_lock); | |
1da177e4 | 562 | } |
d1b19dff | 563 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 564 | |
1da177e4 LT |
565 | /** |
566 | * __dev_remove_pack - remove packet handler | |
567 | * @pt: packet type declaration | |
568 | * | |
569 | * Remove a protocol handler that was previously added to the kernel | |
570 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
571 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 572 | * returns. |
1da177e4 LT |
573 | * |
574 | * The packet type might still be in use by receivers | |
575 | * and must not be freed until after all the CPU's have gone | |
576 | * through a quiescent state. | |
577 | */ | |
578 | void __dev_remove_pack(struct packet_type *pt) | |
579 | { | |
c07b68e8 | 580 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
581 | struct packet_type *pt1; |
582 | ||
c07b68e8 | 583 | spin_lock(&ptype_lock); |
1da177e4 LT |
584 | |
585 | list_for_each_entry(pt1, head, list) { | |
586 | if (pt == pt1) { | |
587 | list_del_rcu(&pt->list); | |
588 | goto out; | |
589 | } | |
590 | } | |
591 | ||
7b6cd1ce | 592 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 593 | out: |
c07b68e8 | 594 | spin_unlock(&ptype_lock); |
1da177e4 | 595 | } |
d1b19dff ED |
596 | EXPORT_SYMBOL(__dev_remove_pack); |
597 | ||
1da177e4 LT |
598 | /** |
599 | * dev_remove_pack - remove packet handler | |
600 | * @pt: packet type declaration | |
601 | * | |
602 | * Remove a protocol handler that was previously added to the kernel | |
603 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
604 | * from the kernel lists and can be freed or reused once this function | |
605 | * returns. | |
606 | * | |
607 | * This call sleeps to guarantee that no CPU is looking at the packet | |
608 | * type after return. | |
609 | */ | |
610 | void dev_remove_pack(struct packet_type *pt) | |
611 | { | |
612 | __dev_remove_pack(pt); | |
4ec93edb | 613 | |
1da177e4 LT |
614 | synchronize_net(); |
615 | } | |
d1b19dff | 616 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 617 | |
62532da9 | 618 | |
1da177e4 | 619 | /******************************************************************************* |
eb13da1a | 620 | * |
621 | * Device Interface Subroutines | |
622 | * | |
623 | *******************************************************************************/ | |
1da177e4 | 624 | |
a54acb3a ND |
625 | /** |
626 | * dev_get_iflink - get 'iflink' value of a interface | |
627 | * @dev: targeted interface | |
628 | * | |
629 | * Indicates the ifindex the interface is linked to. | |
630 | * Physical interfaces have the same 'ifindex' and 'iflink' values. | |
631 | */ | |
632 | ||
633 | int dev_get_iflink(const struct net_device *dev) | |
634 | { | |
635 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) | |
636 | return dev->netdev_ops->ndo_get_iflink(dev); | |
637 | ||
7a66bbc9 | 638 | return dev->ifindex; |
a54acb3a ND |
639 | } |
640 | EXPORT_SYMBOL(dev_get_iflink); | |
641 | ||
fc4099f1 PS |
642 | /** |
643 | * dev_fill_metadata_dst - Retrieve tunnel egress information. | |
644 | * @dev: targeted interface | |
645 | * @skb: The packet. | |
646 | * | |
647 | * For better visibility of tunnel traffic OVS needs to retrieve | |
648 | * egress tunnel information for a packet. Following API allows | |
649 | * user to get this info. | |
650 | */ | |
651 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) | |
652 | { | |
653 | struct ip_tunnel_info *info; | |
654 | ||
655 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) | |
656 | return -EINVAL; | |
657 | ||
658 | info = skb_tunnel_info_unclone(skb); | |
659 | if (!info) | |
660 | return -ENOMEM; | |
661 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) | |
662 | return -EINVAL; | |
663 | ||
664 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); | |
665 | } | |
666 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); | |
667 | ||
ddb94eaf PNA |
668 | static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack) |
669 | { | |
670 | int k = stack->num_paths++; | |
671 | ||
672 | if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX)) | |
673 | return NULL; | |
674 | ||
675 | return &stack->path[k]; | |
676 | } | |
677 | ||
678 | int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr, | |
679 | struct net_device_path_stack *stack) | |
680 | { | |
681 | const struct net_device *last_dev; | |
682 | struct net_device_path_ctx ctx = { | |
683 | .dev = dev, | |
684 | .daddr = daddr, | |
685 | }; | |
686 | struct net_device_path *path; | |
687 | int ret = 0; | |
688 | ||
689 | stack->num_paths = 0; | |
690 | while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) { | |
691 | last_dev = ctx.dev; | |
692 | path = dev_fwd_path(stack); | |
693 | if (!path) | |
694 | return -1; | |
695 | ||
696 | memset(path, 0, sizeof(struct net_device_path)); | |
697 | ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path); | |
698 | if (ret < 0) | |
699 | return -1; | |
700 | ||
701 | if (WARN_ON_ONCE(last_dev == ctx.dev)) | |
702 | return -1; | |
703 | } | |
a333215e FF |
704 | |
705 | if (!ctx.dev) | |
706 | return ret; | |
707 | ||
ddb94eaf PNA |
708 | path = dev_fwd_path(stack); |
709 | if (!path) | |
710 | return -1; | |
711 | path->type = DEV_PATH_ETHERNET; | |
712 | path->dev = ctx.dev; | |
713 | ||
714 | return ret; | |
715 | } | |
716 | EXPORT_SYMBOL_GPL(dev_fill_forward_path); | |
717 | ||
1da177e4 LT |
718 | /** |
719 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 720 | * @net: the applicable net namespace |
1da177e4 LT |
721 | * @name: name to find |
722 | * | |
723 | * Find an interface by name. Must be called under RTNL semaphore | |
724 | * or @dev_base_lock. If the name is found a pointer to the device | |
725 | * is returned. If the name is not found then %NULL is returned. The | |
726 | * reference counters are not incremented so the caller must be | |
727 | * careful with locks. | |
728 | */ | |
729 | ||
881d966b | 730 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 | 731 | { |
ff927412 | 732 | struct netdev_name_node *node_name; |
1da177e4 | 733 | |
ff927412 JP |
734 | node_name = netdev_name_node_lookup(net, name); |
735 | return node_name ? node_name->dev : NULL; | |
1da177e4 | 736 | } |
d1b19dff | 737 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 738 | |
72c9528b | 739 | /** |
722c9a0c | 740 | * dev_get_by_name_rcu - find a device by its name |
741 | * @net: the applicable net namespace | |
742 | * @name: name to find | |
743 | * | |
744 | * Find an interface by name. | |
745 | * If the name is found a pointer to the device is returned. | |
746 | * If the name is not found then %NULL is returned. | |
747 | * The reference counters are not incremented so the caller must be | |
748 | * careful with locks. The caller must hold RCU lock. | |
72c9528b ED |
749 | */ |
750 | ||
751 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
752 | { | |
ff927412 | 753 | struct netdev_name_node *node_name; |
72c9528b | 754 | |
ff927412 JP |
755 | node_name = netdev_name_node_lookup_rcu(net, name); |
756 | return node_name ? node_name->dev : NULL; | |
72c9528b ED |
757 | } |
758 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
759 | ||
1da177e4 LT |
760 | /** |
761 | * dev_get_by_name - find a device by its name | |
c4ea43c5 | 762 | * @net: the applicable net namespace |
1da177e4 LT |
763 | * @name: name to find |
764 | * | |
765 | * Find an interface by name. This can be called from any | |
766 | * context and does its own locking. The returned handle has | |
767 | * the usage count incremented and the caller must use dev_put() to | |
768 | * release it when it is no longer needed. %NULL is returned if no | |
769 | * matching device is found. | |
770 | */ | |
771 | ||
881d966b | 772 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
773 | { |
774 | struct net_device *dev; | |
775 | ||
72c9528b ED |
776 | rcu_read_lock(); |
777 | dev = dev_get_by_name_rcu(net, name); | |
1160dfa1 | 778 | dev_hold(dev); |
72c9528b | 779 | rcu_read_unlock(); |
1da177e4 LT |
780 | return dev; |
781 | } | |
d1b19dff | 782 | EXPORT_SYMBOL(dev_get_by_name); |
1da177e4 LT |
783 | |
784 | /** | |
785 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 786 | * @net: the applicable net namespace |
1da177e4 LT |
787 | * @ifindex: index of device |
788 | * | |
789 | * Search for an interface by index. Returns %NULL if the device | |
790 | * is not found or a pointer to the device. The device has not | |
791 | * had its reference counter increased so the caller must be careful | |
792 | * about locking. The caller must hold either the RTNL semaphore | |
793 | * or @dev_base_lock. | |
794 | */ | |
795 | ||
881d966b | 796 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 | 797 | { |
0bd8d536 ED |
798 | struct net_device *dev; |
799 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 800 | |
b67bfe0d | 801 | hlist_for_each_entry(dev, head, index_hlist) |
1da177e4 LT |
802 | if (dev->ifindex == ifindex) |
803 | return dev; | |
0bd8d536 | 804 | |
1da177e4 LT |
805 | return NULL; |
806 | } | |
d1b19dff | 807 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 808 | |
fb699dfd ED |
809 | /** |
810 | * dev_get_by_index_rcu - find a device by its ifindex | |
811 | * @net: the applicable net namespace | |
812 | * @ifindex: index of device | |
813 | * | |
814 | * Search for an interface by index. Returns %NULL if the device | |
815 | * is not found or a pointer to the device. The device has not | |
816 | * had its reference counter increased so the caller must be careful | |
817 | * about locking. The caller must hold RCU lock. | |
818 | */ | |
819 | ||
820 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
821 | { | |
fb699dfd ED |
822 | struct net_device *dev; |
823 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
824 | ||
b67bfe0d | 825 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
fb699dfd ED |
826 | if (dev->ifindex == ifindex) |
827 | return dev; | |
828 | ||
829 | return NULL; | |
830 | } | |
831 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
832 | ||
1da177e4 LT |
833 | |
834 | /** | |
835 | * dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 836 | * @net: the applicable net namespace |
1da177e4 LT |
837 | * @ifindex: index of device |
838 | * | |
839 | * Search for an interface by index. Returns NULL if the device | |
840 | * is not found or a pointer to the device. The device returned has | |
841 | * had a reference added and the pointer is safe until the user calls | |
842 | * dev_put to indicate they have finished with it. | |
843 | */ | |
844 | ||
881d966b | 845 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
846 | { |
847 | struct net_device *dev; | |
848 | ||
fb699dfd ED |
849 | rcu_read_lock(); |
850 | dev = dev_get_by_index_rcu(net, ifindex); | |
1160dfa1 | 851 | dev_hold(dev); |
fb699dfd | 852 | rcu_read_unlock(); |
1da177e4 LT |
853 | return dev; |
854 | } | |
d1b19dff | 855 | EXPORT_SYMBOL(dev_get_by_index); |
1da177e4 | 856 | |
90b602f8 ML |
857 | /** |
858 | * dev_get_by_napi_id - find a device by napi_id | |
859 | * @napi_id: ID of the NAPI struct | |
860 | * | |
861 | * Search for an interface by NAPI ID. Returns %NULL if the device | |
862 | * is not found or a pointer to the device. The device has not had | |
863 | * its reference counter increased so the caller must be careful | |
864 | * about locking. The caller must hold RCU lock. | |
865 | */ | |
866 | ||
867 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) | |
868 | { | |
869 | struct napi_struct *napi; | |
870 | ||
871 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
872 | ||
873 | if (napi_id < MIN_NAPI_ID) | |
874 | return NULL; | |
875 | ||
876 | napi = napi_by_id(napi_id); | |
877 | ||
878 | return napi ? napi->dev : NULL; | |
879 | } | |
880 | EXPORT_SYMBOL(dev_get_by_napi_id); | |
881 | ||
5dbe7c17 NS |
882 | /** |
883 | * netdev_get_name - get a netdevice name, knowing its ifindex. | |
884 | * @net: network namespace | |
885 | * @name: a pointer to the buffer where the name will be stored. | |
886 | * @ifindex: the ifindex of the interface to get the name from. | |
5dbe7c17 NS |
887 | */ |
888 | int netdev_get_name(struct net *net, char *name, int ifindex) | |
889 | { | |
890 | struct net_device *dev; | |
11d6011c | 891 | int ret; |
5dbe7c17 | 892 | |
11d6011c | 893 | down_read(&devnet_rename_sem); |
5dbe7c17 | 894 | rcu_read_lock(); |
11d6011c | 895 | |
5dbe7c17 NS |
896 | dev = dev_get_by_index_rcu(net, ifindex); |
897 | if (!dev) { | |
11d6011c AD |
898 | ret = -ENODEV; |
899 | goto out; | |
5dbe7c17 NS |
900 | } |
901 | ||
902 | strcpy(name, dev->name); | |
5dbe7c17 | 903 | |
11d6011c AD |
904 | ret = 0; |
905 | out: | |
906 | rcu_read_unlock(); | |
907 | up_read(&devnet_rename_sem); | |
908 | return ret; | |
5dbe7c17 NS |
909 | } |
910 | ||
1da177e4 | 911 | /** |
941666c2 | 912 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 913 | * @net: the applicable net namespace |
1da177e4 LT |
914 | * @type: media type of device |
915 | * @ha: hardware address | |
916 | * | |
917 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
918 | * is not found or a pointer to the device. |
919 | * The caller must hold RCU or RTNL. | |
941666c2 | 920 | * The returned device has not had its ref count increased |
1da177e4 LT |
921 | * and the caller must therefore be careful about locking |
922 | * | |
1da177e4 LT |
923 | */ |
924 | ||
941666c2 ED |
925 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
926 | const char *ha) | |
1da177e4 LT |
927 | { |
928 | struct net_device *dev; | |
929 | ||
941666c2 | 930 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
931 | if (dev->type == type && |
932 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
933 | return dev; |
934 | ||
935 | return NULL; | |
1da177e4 | 936 | } |
941666c2 | 937 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 938 | |
881d966b | 939 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 940 | { |
99fe3c39 | 941 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 942 | |
99fe3c39 ED |
943 | rcu_read_lock(); |
944 | for_each_netdev_rcu(net, dev) | |
945 | if (dev->type == type) { | |
946 | dev_hold(dev); | |
947 | ret = dev; | |
948 | break; | |
949 | } | |
950 | rcu_read_unlock(); | |
951 | return ret; | |
1da177e4 | 952 | } |
1da177e4 LT |
953 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
954 | ||
955 | /** | |
6c555490 | 956 | * __dev_get_by_flags - find any device with given flags |
c4ea43c5 | 957 | * @net: the applicable net namespace |
1da177e4 LT |
958 | * @if_flags: IFF_* values |
959 | * @mask: bitmask of bits in if_flags to check | |
960 | * | |
961 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 | 962 | * is not found or a pointer to the device. Must be called inside |
6c555490 | 963 | * rtnl_lock(), and result refcount is unchanged. |
1da177e4 LT |
964 | */ |
965 | ||
6c555490 WC |
966 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
967 | unsigned short mask) | |
1da177e4 | 968 | { |
7562f876 | 969 | struct net_device *dev, *ret; |
1da177e4 | 970 | |
6c555490 WC |
971 | ASSERT_RTNL(); |
972 | ||
7562f876 | 973 | ret = NULL; |
6c555490 | 974 | for_each_netdev(net, dev) { |
1da177e4 | 975 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 976 | ret = dev; |
1da177e4 LT |
977 | break; |
978 | } | |
979 | } | |
7562f876 | 980 | return ret; |
1da177e4 | 981 | } |
6c555490 | 982 | EXPORT_SYMBOL(__dev_get_by_flags); |
1da177e4 LT |
983 | |
984 | /** | |
985 | * dev_valid_name - check if name is okay for network device | |
986 | * @name: name string | |
987 | * | |
988 | * Network device names need to be valid file names to | |
4250b75b | 989 | * allow sysfs to work. We also disallow any kind of |
c7fa9d18 | 990 | * whitespace. |
1da177e4 | 991 | */ |
95f050bf | 992 | bool dev_valid_name(const char *name) |
1da177e4 | 993 | { |
c7fa9d18 | 994 | if (*name == '\0') |
95f050bf | 995 | return false; |
a9d48205 | 996 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
95f050bf | 997 | return false; |
c7fa9d18 | 998 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 999 | return false; |
c7fa9d18 DM |
1000 | |
1001 | while (*name) { | |
a4176a93 | 1002 | if (*name == '/' || *name == ':' || isspace(*name)) |
95f050bf | 1003 | return false; |
c7fa9d18 DM |
1004 | name++; |
1005 | } | |
95f050bf | 1006 | return true; |
1da177e4 | 1007 | } |
d1b19dff | 1008 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
1009 | |
1010 | /** | |
b267b179 EB |
1011 | * __dev_alloc_name - allocate a name for a device |
1012 | * @net: network namespace to allocate the device name in | |
1da177e4 | 1013 | * @name: name format string |
b267b179 | 1014 | * @buf: scratch buffer and result name string |
1da177e4 LT |
1015 | * |
1016 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
1017 | * id. It scans list of devices to build up a free map, then chooses |
1018 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1019 | * while allocating the name and adding the device in order to avoid | |
1020 | * duplicates. | |
1021 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1022 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
1023 | */ |
1024 | ||
b267b179 | 1025 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
1da177e4 LT |
1026 | { |
1027 | int i = 0; | |
1da177e4 LT |
1028 | const char *p; |
1029 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 1030 | unsigned long *inuse; |
1da177e4 LT |
1031 | struct net_device *d; |
1032 | ||
93809105 RV |
1033 | if (!dev_valid_name(name)) |
1034 | return -EINVAL; | |
1035 | ||
51f299dd | 1036 | p = strchr(name, '%'); |
1da177e4 LT |
1037 | if (p) { |
1038 | /* | |
1039 | * Verify the string as this thing may have come from | |
1040 | * the user. There must be either one "%d" and no other "%" | |
1041 | * characters. | |
1042 | */ | |
1043 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
1044 | return -EINVAL; | |
1045 | ||
1046 | /* Use one page as a bit array of possible slots */ | |
cfcabdcc | 1047 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
1048 | if (!inuse) |
1049 | return -ENOMEM; | |
1050 | ||
881d966b | 1051 | for_each_netdev(net, d) { |
6c015a22 JB |
1052 | struct netdev_name_node *name_node; |
1053 | list_for_each_entry(name_node, &d->name_node->list, list) { | |
1054 | if (!sscanf(name_node->name, name, &i)) | |
1055 | continue; | |
1056 | if (i < 0 || i >= max_netdevices) | |
1057 | continue; | |
1058 | ||
1059 | /* avoid cases where sscanf is not exact inverse of printf */ | |
1060 | snprintf(buf, IFNAMSIZ, name, i); | |
1061 | if (!strncmp(buf, name_node->name, IFNAMSIZ)) | |
25ee1660 | 1062 | __set_bit(i, inuse); |
6c015a22 | 1063 | } |
1da177e4 LT |
1064 | if (!sscanf(d->name, name, &i)) |
1065 | continue; | |
1066 | if (i < 0 || i >= max_netdevices) | |
1067 | continue; | |
1068 | ||
1069 | /* avoid cases where sscanf is not exact inverse of printf */ | |
b267b179 | 1070 | snprintf(buf, IFNAMSIZ, name, i); |
1da177e4 | 1071 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
25ee1660 | 1072 | __set_bit(i, inuse); |
1da177e4 LT |
1073 | } |
1074 | ||
1075 | i = find_first_zero_bit(inuse, max_netdevices); | |
1076 | free_page((unsigned long) inuse); | |
1077 | } | |
1078 | ||
6224abda | 1079 | snprintf(buf, IFNAMSIZ, name, i); |
75ea27d0 | 1080 | if (!netdev_name_in_use(net, buf)) |
1da177e4 | 1081 | return i; |
1da177e4 LT |
1082 | |
1083 | /* It is possible to run out of possible slots | |
1084 | * when the name is long and there isn't enough space left | |
1085 | * for the digits, or if all bits are used. | |
1086 | */ | |
029b6d14 | 1087 | return -ENFILE; |
1da177e4 LT |
1088 | } |
1089 | ||
2c88b855 RV |
1090 | static int dev_alloc_name_ns(struct net *net, |
1091 | struct net_device *dev, | |
1092 | const char *name) | |
1093 | { | |
1094 | char buf[IFNAMSIZ]; | |
1095 | int ret; | |
1096 | ||
c46d7642 | 1097 | BUG_ON(!net); |
2c88b855 RV |
1098 | ret = __dev_alloc_name(net, name, buf); |
1099 | if (ret >= 0) | |
1100 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1101 | return ret; | |
1da177e4 LT |
1102 | } |
1103 | ||
b267b179 EB |
1104 | /** |
1105 | * dev_alloc_name - allocate a name for a device | |
1106 | * @dev: device | |
1107 | * @name: name format string | |
1108 | * | |
1109 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1110 | * id. It scans list of devices to build up a free map, then chooses | |
1111 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1112 | * while allocating the name and adding the device in order to avoid | |
1113 | * duplicates. | |
1114 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1115 | * Returns the number of the unit assigned or a negative errno code. | |
1116 | */ | |
1117 | ||
1118 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1119 | { | |
c46d7642 | 1120 | return dev_alloc_name_ns(dev_net(dev), dev, name); |
b267b179 | 1121 | } |
d1b19dff | 1122 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1123 | |
bacb7e18 ED |
1124 | static int dev_get_valid_name(struct net *net, struct net_device *dev, |
1125 | const char *name) | |
828de4f6 | 1126 | { |
55a5ec9b DM |
1127 | BUG_ON(!net); |
1128 | ||
1129 | if (!dev_valid_name(name)) | |
1130 | return -EINVAL; | |
1131 | ||
1132 | if (strchr(name, '%')) | |
1133 | return dev_alloc_name_ns(net, dev, name); | |
75ea27d0 | 1134 | else if (netdev_name_in_use(net, name)) |
55a5ec9b DM |
1135 | return -EEXIST; |
1136 | else if (dev->name != name) | |
1137 | strlcpy(dev->name, name, IFNAMSIZ); | |
1138 | ||
1139 | return 0; | |
d9031024 | 1140 | } |
1da177e4 LT |
1141 | |
1142 | /** | |
1143 | * dev_change_name - change name of a device | |
1144 | * @dev: device | |
1145 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1146 | * | |
1147 | * Change name of a device, can pass format strings "eth%d". | |
1148 | * for wildcarding. | |
1149 | */ | |
cf04a4c7 | 1150 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1151 | { |
238fa362 | 1152 | unsigned char old_assign_type; |
fcc5a03a | 1153 | char oldname[IFNAMSIZ]; |
1da177e4 | 1154 | int err = 0; |
fcc5a03a | 1155 | int ret; |
881d966b | 1156 | struct net *net; |
1da177e4 LT |
1157 | |
1158 | ASSERT_RTNL(); | |
c346dca1 | 1159 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1160 | |
c346dca1 | 1161 | net = dev_net(dev); |
8065a779 SWL |
1162 | |
1163 | /* Some auto-enslaved devices e.g. failover slaves are | |
1164 | * special, as userspace might rename the device after | |
1165 | * the interface had been brought up and running since | |
1166 | * the point kernel initiated auto-enslavement. Allow | |
1167 | * live name change even when these slave devices are | |
1168 | * up and running. | |
1169 | * | |
1170 | * Typically, users of these auto-enslaving devices | |
1171 | * don't actually care about slave name change, as | |
1172 | * they are supposed to operate on master interface | |
1173 | * directly. | |
1174 | */ | |
1175 | if (dev->flags & IFF_UP && | |
1176 | likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK))) | |
1da177e4 LT |
1177 | return -EBUSY; |
1178 | ||
11d6011c | 1179 | down_write(&devnet_rename_sem); |
c91f6df2 BH |
1180 | |
1181 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
11d6011c | 1182 | up_write(&devnet_rename_sem); |
c8d90dca | 1183 | return 0; |
c91f6df2 | 1184 | } |
c8d90dca | 1185 | |
fcc5a03a HX |
1186 | memcpy(oldname, dev->name, IFNAMSIZ); |
1187 | ||
828de4f6 | 1188 | err = dev_get_valid_name(net, dev, newname); |
c91f6df2 | 1189 | if (err < 0) { |
11d6011c | 1190 | up_write(&devnet_rename_sem); |
d9031024 | 1191 | return err; |
c91f6df2 | 1192 | } |
1da177e4 | 1193 | |
6fe82a39 VF |
1194 | if (oldname[0] && !strchr(oldname, '%')) |
1195 | netdev_info(dev, "renamed from %s\n", oldname); | |
1196 | ||
238fa362 TG |
1197 | old_assign_type = dev->name_assign_type; |
1198 | dev->name_assign_type = NET_NAME_RENAMED; | |
1199 | ||
fcc5a03a | 1200 | rollback: |
a1b3f594 EB |
1201 | ret = device_rename(&dev->dev, dev->name); |
1202 | if (ret) { | |
1203 | memcpy(dev->name, oldname, IFNAMSIZ); | |
238fa362 | 1204 | dev->name_assign_type = old_assign_type; |
11d6011c | 1205 | up_write(&devnet_rename_sem); |
a1b3f594 | 1206 | return ret; |
dcc99773 | 1207 | } |
7f988eab | 1208 | |
11d6011c | 1209 | up_write(&devnet_rename_sem); |
c91f6df2 | 1210 | |
5bb025fa VF |
1211 | netdev_adjacent_rename_links(dev, oldname); |
1212 | ||
fd888e85 | 1213 | write_lock(&dev_base_lock); |
ff927412 | 1214 | netdev_name_node_del(dev->name_node); |
fd888e85 | 1215 | write_unlock(&dev_base_lock); |
72c9528b ED |
1216 | |
1217 | synchronize_rcu(); | |
1218 | ||
fd888e85 | 1219 | write_lock(&dev_base_lock); |
ff927412 | 1220 | netdev_name_node_add(net, dev->name_node); |
fd888e85 | 1221 | write_unlock(&dev_base_lock); |
7f988eab | 1222 | |
056925ab | 1223 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1224 | ret = notifier_to_errno(ret); |
1225 | ||
1226 | if (ret) { | |
91e9c07b ED |
1227 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1228 | if (err >= 0) { | |
fcc5a03a | 1229 | err = ret; |
11d6011c | 1230 | down_write(&devnet_rename_sem); |
fcc5a03a | 1231 | memcpy(dev->name, oldname, IFNAMSIZ); |
5bb025fa | 1232 | memcpy(oldname, newname, IFNAMSIZ); |
238fa362 TG |
1233 | dev->name_assign_type = old_assign_type; |
1234 | old_assign_type = NET_NAME_RENAMED; | |
fcc5a03a | 1235 | goto rollback; |
91e9c07b | 1236 | } else { |
5b92be64 JB |
1237 | netdev_err(dev, "name change rollback failed: %d\n", |
1238 | ret); | |
fcc5a03a HX |
1239 | } |
1240 | } | |
1da177e4 LT |
1241 | |
1242 | return err; | |
1243 | } | |
1244 | ||
0b815a1a SH |
1245 | /** |
1246 | * dev_set_alias - change ifalias of a device | |
1247 | * @dev: device | |
1248 | * @alias: name up to IFALIASZ | |
f0db275a | 1249 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1250 | * |
1251 | * Set ifalias for a device, | |
1252 | */ | |
1253 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1254 | { | |
6c557001 | 1255 | struct dev_ifalias *new_alias = NULL; |
0b815a1a SH |
1256 | |
1257 | if (len >= IFALIASZ) | |
1258 | return -EINVAL; | |
1259 | ||
6c557001 FW |
1260 | if (len) { |
1261 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); | |
1262 | if (!new_alias) | |
1263 | return -ENOMEM; | |
1264 | ||
1265 | memcpy(new_alias->ifalias, alias, len); | |
1266 | new_alias->ifalias[len] = 0; | |
96ca4a2c OH |
1267 | } |
1268 | ||
6c557001 | 1269 | mutex_lock(&ifalias_mutex); |
e3f0d761 PM |
1270 | new_alias = rcu_replace_pointer(dev->ifalias, new_alias, |
1271 | mutex_is_locked(&ifalias_mutex)); | |
6c557001 FW |
1272 | mutex_unlock(&ifalias_mutex); |
1273 | ||
1274 | if (new_alias) | |
1275 | kfree_rcu(new_alias, rcuhead); | |
0b815a1a | 1276 | |
0b815a1a SH |
1277 | return len; |
1278 | } | |
0fe554a4 | 1279 | EXPORT_SYMBOL(dev_set_alias); |
0b815a1a | 1280 | |
6c557001 FW |
1281 | /** |
1282 | * dev_get_alias - get ifalias of a device | |
1283 | * @dev: device | |
20e88320 | 1284 | * @name: buffer to store name of ifalias |
6c557001 FW |
1285 | * @len: size of buffer |
1286 | * | |
1287 | * get ifalias for a device. Caller must make sure dev cannot go | |
1288 | * away, e.g. rcu read lock or own a reference count to device. | |
1289 | */ | |
1290 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) | |
1291 | { | |
1292 | const struct dev_ifalias *alias; | |
1293 | int ret = 0; | |
1294 | ||
1295 | rcu_read_lock(); | |
1296 | alias = rcu_dereference(dev->ifalias); | |
1297 | if (alias) | |
1298 | ret = snprintf(name, len, "%s", alias->ifalias); | |
1299 | rcu_read_unlock(); | |
1300 | ||
1301 | return ret; | |
1302 | } | |
0b815a1a | 1303 | |
d8a33ac4 | 1304 | /** |
3041a069 | 1305 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1306 | * @dev: device to cause notification |
1307 | * | |
1308 | * Called to indicate a device has changed features. | |
1309 | */ | |
1310 | void netdev_features_change(struct net_device *dev) | |
1311 | { | |
056925ab | 1312 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1313 | } |
1314 | EXPORT_SYMBOL(netdev_features_change); | |
1315 | ||
1da177e4 LT |
1316 | /** |
1317 | * netdev_state_change - device changes state | |
1318 | * @dev: device to cause notification | |
1319 | * | |
1320 | * Called to indicate a device has changed state. This function calls | |
1321 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1322 | * to the routing socket. | |
1323 | */ | |
1324 | void netdev_state_change(struct net_device *dev) | |
1325 | { | |
1326 | if (dev->flags & IFF_UP) { | |
51d0c047 DA |
1327 | struct netdev_notifier_change_info change_info = { |
1328 | .info.dev = dev, | |
1329 | }; | |
54951194 | 1330 | |
51d0c047 | 1331 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
54951194 | 1332 | &change_info.info); |
7f294054 | 1333 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
1da177e4 LT |
1334 | } |
1335 | } | |
d1b19dff | 1336 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1337 | |
7061eb8c LP |
1338 | /** |
1339 | * __netdev_notify_peers - notify network peers about existence of @dev, | |
1340 | * to be called when rtnl lock is already held. | |
1341 | * @dev: network device | |
1342 | * | |
1343 | * Generate traffic such that interested network peers are aware of | |
1344 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1345 | * a device wants to inform the rest of the network about some sort of | |
1346 | * reconfiguration such as a failover event or virtual machine | |
1347 | * migration. | |
1348 | */ | |
1349 | void __netdev_notify_peers(struct net_device *dev) | |
1350 | { | |
1351 | ASSERT_RTNL(); | |
1352 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
1353 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); | |
1354 | } | |
1355 | EXPORT_SYMBOL(__netdev_notify_peers); | |
1356 | ||
ee89bab1 | 1357 | /** |
722c9a0c | 1358 | * netdev_notify_peers - notify network peers about existence of @dev |
1359 | * @dev: network device | |
ee89bab1 AW |
1360 | * |
1361 | * Generate traffic such that interested network peers are aware of | |
1362 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1363 | * a device wants to inform the rest of the network about some sort of | |
1364 | * reconfiguration such as a failover event or virtual machine | |
1365 | * migration. | |
1366 | */ | |
1367 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1368 | { |
ee89bab1 | 1369 | rtnl_lock(); |
7061eb8c | 1370 | __netdev_notify_peers(dev); |
ee89bab1 | 1371 | rtnl_unlock(); |
c1da4ac7 | 1372 | } |
ee89bab1 | 1373 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1374 | |
29863d41 WW |
1375 | static int napi_threaded_poll(void *data); |
1376 | ||
1377 | static int napi_kthread_create(struct napi_struct *n) | |
1378 | { | |
1379 | int err = 0; | |
1380 | ||
1381 | /* Create and wake up the kthread once to put it in | |
1382 | * TASK_INTERRUPTIBLE mode to avoid the blocked task | |
1383 | * warning and work with loadavg. | |
1384 | */ | |
1385 | n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d", | |
1386 | n->dev->name, n->napi_id); | |
1387 | if (IS_ERR(n->thread)) { | |
1388 | err = PTR_ERR(n->thread); | |
1389 | pr_err("kthread_run failed with err %d\n", err); | |
1390 | n->thread = NULL; | |
1391 | } | |
1392 | ||
1393 | return err; | |
1394 | } | |
1395 | ||
40c900aa | 1396 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
1da177e4 | 1397 | { |
d314774c | 1398 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1399 | int ret; |
1da177e4 | 1400 | |
e46b66bc | 1401 | ASSERT_RTNL(); |
d07b26f5 | 1402 | dev_addr_check(dev); |
e46b66bc | 1403 | |
bd869245 HK |
1404 | if (!netif_device_present(dev)) { |
1405 | /* may be detached because parent is runtime-suspended */ | |
1406 | if (dev->dev.parent) | |
1407 | pm_runtime_resume(dev->dev.parent); | |
1408 | if (!netif_device_present(dev)) | |
1409 | return -ENODEV; | |
1410 | } | |
1da177e4 | 1411 | |
ca99ca14 NH |
1412 | /* Block netpoll from trying to do any rx path servicing. |
1413 | * If we don't do this there is a chance ndo_poll_controller | |
1414 | * or ndo_poll may be running while we open the device | |
1415 | */ | |
66b5552f | 1416 | netpoll_poll_disable(dev); |
ca99ca14 | 1417 | |
40c900aa | 1418 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
3b8bcfd5 JB |
1419 | ret = notifier_to_errno(ret); |
1420 | if (ret) | |
1421 | return ret; | |
1422 | ||
1da177e4 | 1423 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1424 | |
d314774c SH |
1425 | if (ops->ndo_validate_addr) |
1426 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1427 | |
d314774c SH |
1428 | if (!ret && ops->ndo_open) |
1429 | ret = ops->ndo_open(dev); | |
1da177e4 | 1430 | |
66b5552f | 1431 | netpoll_poll_enable(dev); |
ca99ca14 | 1432 | |
bada339b JG |
1433 | if (ret) |
1434 | clear_bit(__LINK_STATE_START, &dev->state); | |
1435 | else { | |
1da177e4 | 1436 | dev->flags |= IFF_UP; |
4417da66 | 1437 | dev_set_rx_mode(dev); |
1da177e4 | 1438 | dev_activate(dev); |
7bf23575 | 1439 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1440 | } |
bada339b | 1441 | |
1da177e4 LT |
1442 | return ret; |
1443 | } | |
1444 | ||
1445 | /** | |
bd380811 | 1446 | * dev_open - prepare an interface for use. |
00f54e68 PM |
1447 | * @dev: device to open |
1448 | * @extack: netlink extended ack | |
1da177e4 | 1449 | * |
bd380811 PM |
1450 | * Takes a device from down to up state. The device's private open |
1451 | * function is invoked and then the multicast lists are loaded. Finally | |
1452 | * the device is moved into the up state and a %NETDEV_UP message is | |
1453 | * sent to the netdev notifier chain. | |
1454 | * | |
1455 | * Calling this function on an active interface is a nop. On a failure | |
1456 | * a negative errno code is returned. | |
1da177e4 | 1457 | */ |
00f54e68 | 1458 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
bd380811 PM |
1459 | { |
1460 | int ret; | |
1461 | ||
bd380811 PM |
1462 | if (dev->flags & IFF_UP) |
1463 | return 0; | |
1464 | ||
40c900aa | 1465 | ret = __dev_open(dev, extack); |
bd380811 PM |
1466 | if (ret < 0) |
1467 | return ret; | |
1468 | ||
7f294054 | 1469 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
bd380811 PM |
1470 | call_netdevice_notifiers(NETDEV_UP, dev); |
1471 | ||
1472 | return ret; | |
1473 | } | |
1474 | EXPORT_SYMBOL(dev_open); | |
1475 | ||
7051b88a | 1476 | static void __dev_close_many(struct list_head *head) |
1da177e4 | 1477 | { |
44345724 | 1478 | struct net_device *dev; |
e46b66bc | 1479 | |
bd380811 | 1480 | ASSERT_RTNL(); |
9d5010db DM |
1481 | might_sleep(); |
1482 | ||
5cde2829 | 1483 | list_for_each_entry(dev, head, close_list) { |
3f4df206 | 1484 | /* Temporarily disable netpoll until the interface is down */ |
66b5552f | 1485 | netpoll_poll_disable(dev); |
3f4df206 | 1486 | |
44345724 | 1487 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1488 | |
44345724 | 1489 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1490 | |
44345724 OP |
1491 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1492 | * can be even on different cpu. So just clear netif_running(). | |
1493 | * | |
1494 | * dev->stop() will invoke napi_disable() on all of it's | |
1495 | * napi_struct instances on this device. | |
1496 | */ | |
4e857c58 | 1497 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
44345724 | 1498 | } |
1da177e4 | 1499 | |
44345724 | 1500 | dev_deactivate_many(head); |
d8b2a4d2 | 1501 | |
5cde2829 | 1502 | list_for_each_entry(dev, head, close_list) { |
44345724 | 1503 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 1504 | |
44345724 OP |
1505 | /* |
1506 | * Call the device specific close. This cannot fail. | |
1507 | * Only if device is UP | |
1508 | * | |
1509 | * We allow it to be called even after a DETACH hot-plug | |
1510 | * event. | |
1511 | */ | |
1512 | if (ops->ndo_stop) | |
1513 | ops->ndo_stop(dev); | |
1514 | ||
44345724 | 1515 | dev->flags &= ~IFF_UP; |
66b5552f | 1516 | netpoll_poll_enable(dev); |
44345724 | 1517 | } |
44345724 OP |
1518 | } |
1519 | ||
7051b88a | 1520 | static void __dev_close(struct net_device *dev) |
44345724 OP |
1521 | { |
1522 | LIST_HEAD(single); | |
1523 | ||
5cde2829 | 1524 | list_add(&dev->close_list, &single); |
7051b88a | 1525 | __dev_close_many(&single); |
f87e6f47 | 1526 | list_del(&single); |
44345724 OP |
1527 | } |
1528 | ||
7051b88a | 1529 | void dev_close_many(struct list_head *head, bool unlink) |
44345724 OP |
1530 | { |
1531 | struct net_device *dev, *tmp; | |
1da177e4 | 1532 | |
5cde2829 EB |
1533 | /* Remove the devices that don't need to be closed */ |
1534 | list_for_each_entry_safe(dev, tmp, head, close_list) | |
44345724 | 1535 | if (!(dev->flags & IFF_UP)) |
5cde2829 | 1536 | list_del_init(&dev->close_list); |
44345724 OP |
1537 | |
1538 | __dev_close_many(head); | |
1da177e4 | 1539 | |
5cde2829 | 1540 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
7f294054 | 1541 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
44345724 | 1542 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
99c4a26a DM |
1543 | if (unlink) |
1544 | list_del_init(&dev->close_list); | |
44345724 | 1545 | } |
bd380811 | 1546 | } |
99c4a26a | 1547 | EXPORT_SYMBOL(dev_close_many); |
bd380811 PM |
1548 | |
1549 | /** | |
1550 | * dev_close - shutdown an interface. | |
1551 | * @dev: device to shutdown | |
1552 | * | |
1553 | * This function moves an active device into down state. A | |
1554 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1555 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1556 | * chain. | |
1557 | */ | |
7051b88a | 1558 | void dev_close(struct net_device *dev) |
bd380811 | 1559 | { |
e14a5993 ED |
1560 | if (dev->flags & IFF_UP) { |
1561 | LIST_HEAD(single); | |
1da177e4 | 1562 | |
5cde2829 | 1563 | list_add(&dev->close_list, &single); |
99c4a26a | 1564 | dev_close_many(&single, true); |
e14a5993 ED |
1565 | list_del(&single); |
1566 | } | |
1da177e4 | 1567 | } |
d1b19dff | 1568 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1569 | |
1570 | ||
0187bdfb BH |
1571 | /** |
1572 | * dev_disable_lro - disable Large Receive Offload on a device | |
1573 | * @dev: device | |
1574 | * | |
1575 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1576 | * called under RTNL. This is needed if received packets may be | |
1577 | * forwarded to another interface. | |
1578 | */ | |
1579 | void dev_disable_lro(struct net_device *dev) | |
1580 | { | |
fbe168ba MK |
1581 | struct net_device *lower_dev; |
1582 | struct list_head *iter; | |
529d0489 | 1583 | |
bc5787c6 MM |
1584 | dev->wanted_features &= ~NETIF_F_LRO; |
1585 | netdev_update_features(dev); | |
27660515 | 1586 | |
22d5969f MM |
1587 | if (unlikely(dev->features & NETIF_F_LRO)) |
1588 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
fbe168ba MK |
1589 | |
1590 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
1591 | dev_disable_lro(lower_dev); | |
0187bdfb BH |
1592 | } |
1593 | EXPORT_SYMBOL(dev_disable_lro); | |
1594 | ||
56f5aa77 MC |
1595 | /** |
1596 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device | |
1597 | * @dev: device | |
1598 | * | |
1599 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be | |
1600 | * called under RTNL. This is needed if Generic XDP is installed on | |
1601 | * the device. | |
1602 | */ | |
1603 | static void dev_disable_gro_hw(struct net_device *dev) | |
1604 | { | |
1605 | dev->wanted_features &= ~NETIF_F_GRO_HW; | |
1606 | netdev_update_features(dev); | |
1607 | ||
1608 | if (unlikely(dev->features & NETIF_F_GRO_HW)) | |
1609 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); | |
1610 | } | |
1611 | ||
ede2762d KT |
1612 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
1613 | { | |
1614 | #define N(val) \ | |
1615 | case NETDEV_##val: \ | |
1616 | return "NETDEV_" __stringify(val); | |
1617 | switch (cmd) { | |
1618 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) | |
1619 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) | |
1620 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) | |
1621 | N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER) | |
1622 | N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO) | |
1623 | N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO) | |
1624 | N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) | |
9daae9bd GP |
1625 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
1626 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) | |
9309f97a PM |
1627 | N(PRE_CHANGEADDR) N(OFFLOAD_XSTATS_ENABLE) N(OFFLOAD_XSTATS_DISABLE) |
1628 | N(OFFLOAD_XSTATS_REPORT_USED) N(OFFLOAD_XSTATS_REPORT_DELTA) | |
3f5ecd8a | 1629 | } |
ede2762d KT |
1630 | #undef N |
1631 | return "UNKNOWN_NETDEV_EVENT"; | |
1632 | } | |
1633 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); | |
1634 | ||
351638e7 JP |
1635 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
1636 | struct net_device *dev) | |
1637 | { | |
51d0c047 DA |
1638 | struct netdev_notifier_info info = { |
1639 | .dev = dev, | |
1640 | }; | |
351638e7 | 1641 | |
351638e7 JP |
1642 | return nb->notifier_call(nb, val, &info); |
1643 | } | |
0187bdfb | 1644 | |
afa0df59 JP |
1645 | static int call_netdevice_register_notifiers(struct notifier_block *nb, |
1646 | struct net_device *dev) | |
1647 | { | |
1648 | int err; | |
1649 | ||
1650 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); | |
1651 | err = notifier_to_errno(err); | |
1652 | if (err) | |
1653 | return err; | |
1654 | ||
1655 | if (!(dev->flags & IFF_UP)) | |
1656 | return 0; | |
1657 | ||
1658 | call_netdevice_notifier(nb, NETDEV_UP, dev); | |
1659 | return 0; | |
1660 | } | |
1661 | ||
1662 | static void call_netdevice_unregister_notifiers(struct notifier_block *nb, | |
1663 | struct net_device *dev) | |
1664 | { | |
1665 | if (dev->flags & IFF_UP) { | |
1666 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, | |
1667 | dev); | |
1668 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
1669 | } | |
1670 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); | |
1671 | } | |
1672 | ||
1673 | static int call_netdevice_register_net_notifiers(struct notifier_block *nb, | |
1674 | struct net *net) | |
1675 | { | |
1676 | struct net_device *dev; | |
1677 | int err; | |
1678 | ||
1679 | for_each_netdev(net, dev) { | |
1680 | err = call_netdevice_register_notifiers(nb, dev); | |
1681 | if (err) | |
1682 | goto rollback; | |
1683 | } | |
1684 | return 0; | |
1685 | ||
1686 | rollback: | |
1687 | for_each_netdev_continue_reverse(net, dev) | |
1688 | call_netdevice_unregister_notifiers(nb, dev); | |
1689 | return err; | |
1690 | } | |
1691 | ||
1692 | static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb, | |
1693 | struct net *net) | |
1694 | { | |
1695 | struct net_device *dev; | |
1696 | ||
1697 | for_each_netdev(net, dev) | |
1698 | call_netdevice_unregister_notifiers(nb, dev); | |
1699 | } | |
1700 | ||
881d966b EB |
1701 | static int dev_boot_phase = 1; |
1702 | ||
1da177e4 | 1703 | /** |
722c9a0c | 1704 | * register_netdevice_notifier - register a network notifier block |
1705 | * @nb: notifier | |
1da177e4 | 1706 | * |
722c9a0c | 1707 | * Register a notifier to be called when network device events occur. |
1708 | * The notifier passed is linked into the kernel structures and must | |
1709 | * not be reused until it has been unregistered. A negative errno code | |
1710 | * is returned on a failure. | |
1da177e4 | 1711 | * |
722c9a0c | 1712 | * When registered all registration and up events are replayed |
1713 | * to the new notifier to allow device to have a race free | |
1714 | * view of the network device list. | |
1da177e4 LT |
1715 | */ |
1716 | ||
1717 | int register_netdevice_notifier(struct notifier_block *nb) | |
1718 | { | |
881d966b | 1719 | struct net *net; |
1da177e4 LT |
1720 | int err; |
1721 | ||
328fbe74 KT |
1722 | /* Close race with setup_net() and cleanup_net() */ |
1723 | down_write(&pernet_ops_rwsem); | |
1da177e4 | 1724 | rtnl_lock(); |
f07d5b94 | 1725 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1726 | if (err) |
1727 | goto unlock; | |
881d966b EB |
1728 | if (dev_boot_phase) |
1729 | goto unlock; | |
1730 | for_each_net(net) { | |
afa0df59 JP |
1731 | err = call_netdevice_register_net_notifiers(nb, net); |
1732 | if (err) | |
1733 | goto rollback; | |
1da177e4 | 1734 | } |
fcc5a03a HX |
1735 | |
1736 | unlock: | |
1da177e4 | 1737 | rtnl_unlock(); |
328fbe74 | 1738 | up_write(&pernet_ops_rwsem); |
1da177e4 | 1739 | return err; |
fcc5a03a HX |
1740 | |
1741 | rollback: | |
afa0df59 JP |
1742 | for_each_net_continue_reverse(net) |
1743 | call_netdevice_unregister_net_notifiers(nb, net); | |
c67625a1 PE |
1744 | |
1745 | raw_notifier_chain_unregister(&netdev_chain, nb); | |
fcc5a03a | 1746 | goto unlock; |
1da177e4 | 1747 | } |
d1b19dff | 1748 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1749 | |
1750 | /** | |
722c9a0c | 1751 | * unregister_netdevice_notifier - unregister a network notifier block |
1752 | * @nb: notifier | |
1da177e4 | 1753 | * |
722c9a0c | 1754 | * Unregister a notifier previously registered by |
1755 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1756 | * kernel structures and may then be reused. A negative errno code | |
1757 | * is returned on a failure. | |
7d3d43da | 1758 | * |
722c9a0c | 1759 | * After unregistering unregister and down device events are synthesized |
1760 | * for all devices on the device list to the removed notifier to remove | |
1761 | * the need for special case cleanup code. | |
1da177e4 LT |
1762 | */ |
1763 | ||
1764 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1765 | { | |
7d3d43da | 1766 | struct net *net; |
9f514950 HX |
1767 | int err; |
1768 | ||
328fbe74 KT |
1769 | /* Close race with setup_net() and cleanup_net() */ |
1770 | down_write(&pernet_ops_rwsem); | |
9f514950 | 1771 | rtnl_lock(); |
f07d5b94 | 1772 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1773 | if (err) |
1774 | goto unlock; | |
1775 | ||
48b3a137 JP |
1776 | for_each_net(net) |
1777 | call_netdevice_unregister_net_notifiers(nb, net); | |
1778 | ||
7d3d43da | 1779 | unlock: |
9f514950 | 1780 | rtnl_unlock(); |
328fbe74 | 1781 | up_write(&pernet_ops_rwsem); |
9f514950 | 1782 | return err; |
1da177e4 | 1783 | } |
d1b19dff | 1784 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 | 1785 | |
1f637703 JP |
1786 | static int __register_netdevice_notifier_net(struct net *net, |
1787 | struct notifier_block *nb, | |
1788 | bool ignore_call_fail) | |
1789 | { | |
1790 | int err; | |
1791 | ||
1792 | err = raw_notifier_chain_register(&net->netdev_chain, nb); | |
1793 | if (err) | |
1794 | return err; | |
1795 | if (dev_boot_phase) | |
1796 | return 0; | |
1797 | ||
1798 | err = call_netdevice_register_net_notifiers(nb, net); | |
1799 | if (err && !ignore_call_fail) | |
1800 | goto chain_unregister; | |
1801 | ||
1802 | return 0; | |
1803 | ||
1804 | chain_unregister: | |
1805 | raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1806 | return err; | |
1807 | } | |
1808 | ||
1809 | static int __unregister_netdevice_notifier_net(struct net *net, | |
1810 | struct notifier_block *nb) | |
1811 | { | |
1812 | int err; | |
1813 | ||
1814 | err = raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1815 | if (err) | |
1816 | return err; | |
1817 | ||
1818 | call_netdevice_unregister_net_notifiers(nb, net); | |
1819 | return 0; | |
1820 | } | |
1821 | ||
a30c7b42 JP |
1822 | /** |
1823 | * register_netdevice_notifier_net - register a per-netns network notifier block | |
1824 | * @net: network namespace | |
1825 | * @nb: notifier | |
1826 | * | |
1827 | * Register a notifier to be called when network device events occur. | |
1828 | * The notifier passed is linked into the kernel structures and must | |
1829 | * not be reused until it has been unregistered. A negative errno code | |
1830 | * is returned on a failure. | |
1831 | * | |
1832 | * When registered all registration and up events are replayed | |
1833 | * to the new notifier to allow device to have a race free | |
1834 | * view of the network device list. | |
1835 | */ | |
1836 | ||
1837 | int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb) | |
1838 | { | |
1839 | int err; | |
1840 | ||
1841 | rtnl_lock(); | |
1f637703 | 1842 | err = __register_netdevice_notifier_net(net, nb, false); |
a30c7b42 JP |
1843 | rtnl_unlock(); |
1844 | return err; | |
a30c7b42 JP |
1845 | } |
1846 | EXPORT_SYMBOL(register_netdevice_notifier_net); | |
1847 | ||
1848 | /** | |
1849 | * unregister_netdevice_notifier_net - unregister a per-netns | |
1850 | * network notifier block | |
1851 | * @net: network namespace | |
1852 | * @nb: notifier | |
1853 | * | |
1854 | * Unregister a notifier previously registered by | |
1855 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1856 | * kernel structures and may then be reused. A negative errno code | |
1857 | * is returned on a failure. | |
1858 | * | |
1859 | * After unregistering unregister and down device events are synthesized | |
1860 | * for all devices on the device list to the removed notifier to remove | |
1861 | * the need for special case cleanup code. | |
1862 | */ | |
1863 | ||
1864 | int unregister_netdevice_notifier_net(struct net *net, | |
1865 | struct notifier_block *nb) | |
1866 | { | |
1867 | int err; | |
1868 | ||
1869 | rtnl_lock(); | |
1f637703 | 1870 | err = __unregister_netdevice_notifier_net(net, nb); |
a30c7b42 JP |
1871 | rtnl_unlock(); |
1872 | return err; | |
1873 | } | |
1874 | EXPORT_SYMBOL(unregister_netdevice_notifier_net); | |
a30c7b42 | 1875 | |
93642e14 JP |
1876 | int register_netdevice_notifier_dev_net(struct net_device *dev, |
1877 | struct notifier_block *nb, | |
1878 | struct netdev_net_notifier *nn) | |
1879 | { | |
1880 | int err; | |
a30c7b42 | 1881 | |
93642e14 JP |
1882 | rtnl_lock(); |
1883 | err = __register_netdevice_notifier_net(dev_net(dev), nb, false); | |
1884 | if (!err) { | |
1885 | nn->nb = nb; | |
1886 | list_add(&nn->list, &dev->net_notifier_list); | |
1887 | } | |
a30c7b42 JP |
1888 | rtnl_unlock(); |
1889 | return err; | |
1890 | } | |
93642e14 JP |
1891 | EXPORT_SYMBOL(register_netdevice_notifier_dev_net); |
1892 | ||
1893 | int unregister_netdevice_notifier_dev_net(struct net_device *dev, | |
1894 | struct notifier_block *nb, | |
1895 | struct netdev_net_notifier *nn) | |
1896 | { | |
1897 | int err; | |
1898 | ||
1899 | rtnl_lock(); | |
1900 | list_del(&nn->list); | |
1901 | err = __unregister_netdevice_notifier_net(dev_net(dev), nb); | |
1902 | rtnl_unlock(); | |
1903 | return err; | |
1904 | } | |
1905 | EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net); | |
1906 | ||
1907 | static void move_netdevice_notifiers_dev_net(struct net_device *dev, | |
1908 | struct net *net) | |
1909 | { | |
1910 | struct netdev_net_notifier *nn; | |
1911 | ||
1912 | list_for_each_entry(nn, &dev->net_notifier_list, list) { | |
1913 | __unregister_netdevice_notifier_net(dev_net(dev), nn->nb); | |
1914 | __register_netdevice_notifier_net(net, nn->nb, true); | |
1915 | } | |
1916 | } | |
a30c7b42 | 1917 | |
351638e7 JP |
1918 | /** |
1919 | * call_netdevice_notifiers_info - call all network notifier blocks | |
1920 | * @val: value passed unmodified to notifier function | |
351638e7 JP |
1921 | * @info: notifier information data |
1922 | * | |
1923 | * Call all network notifier blocks. Parameters and return value | |
1924 | * are as for raw_notifier_call_chain(). | |
1925 | */ | |
1926 | ||
1d143d9f | 1927 | static int call_netdevice_notifiers_info(unsigned long val, |
1d143d9f | 1928 | struct netdev_notifier_info *info) |
351638e7 | 1929 | { |
a30c7b42 JP |
1930 | struct net *net = dev_net(info->dev); |
1931 | int ret; | |
1932 | ||
351638e7 | 1933 | ASSERT_RTNL(); |
a30c7b42 JP |
1934 | |
1935 | /* Run per-netns notifier block chain first, then run the global one. | |
1936 | * Hopefully, one day, the global one is going to be removed after | |
1937 | * all notifier block registrators get converted to be per-netns. | |
1938 | */ | |
1939 | ret = raw_notifier_call_chain(&net->netdev_chain, val, info); | |
1940 | if (ret & NOTIFY_STOP_MASK) | |
1941 | return ret; | |
351638e7 JP |
1942 | return raw_notifier_call_chain(&netdev_chain, val, info); |
1943 | } | |
351638e7 | 1944 | |
9309f97a PM |
1945 | /** |
1946 | * call_netdevice_notifiers_info_robust - call per-netns notifier blocks | |
1947 | * for and rollback on error | |
1948 | * @val_up: value passed unmodified to notifier function | |
1949 | * @val_down: value passed unmodified to the notifier function when | |
1950 | * recovering from an error on @val_up | |
1951 | * @info: notifier information data | |
1952 | * | |
1953 | * Call all per-netns network notifier blocks, but not notifier blocks on | |
1954 | * the global notifier chain. Parameters and return value are as for | |
1955 | * raw_notifier_call_chain_robust(). | |
1956 | */ | |
1957 | ||
1958 | static int | |
1959 | call_netdevice_notifiers_info_robust(unsigned long val_up, | |
1960 | unsigned long val_down, | |
1961 | struct netdev_notifier_info *info) | |
1962 | { | |
1963 | struct net *net = dev_net(info->dev); | |
1964 | ||
1965 | ASSERT_RTNL(); | |
1966 | ||
1967 | return raw_notifier_call_chain_robust(&net->netdev_chain, | |
1968 | val_up, val_down, info); | |
1969 | } | |
1970 | ||
26372605 PM |
1971 | static int call_netdevice_notifiers_extack(unsigned long val, |
1972 | struct net_device *dev, | |
1973 | struct netlink_ext_ack *extack) | |
1974 | { | |
1975 | struct netdev_notifier_info info = { | |
1976 | .dev = dev, | |
1977 | .extack = extack, | |
1978 | }; | |
1979 | ||
1980 | return call_netdevice_notifiers_info(val, &info); | |
1981 | } | |
1982 | ||
1da177e4 LT |
1983 | /** |
1984 | * call_netdevice_notifiers - call all network notifier blocks | |
1985 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 1986 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
1987 | * |
1988 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 1989 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
1990 | */ |
1991 | ||
ad7379d4 | 1992 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 1993 | { |
26372605 | 1994 | return call_netdevice_notifiers_extack(val, dev, NULL); |
1da177e4 | 1995 | } |
edf947f1 | 1996 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 1997 | |
af7d6cce SD |
1998 | /** |
1999 | * call_netdevice_notifiers_mtu - call all network notifier blocks | |
2000 | * @val: value passed unmodified to notifier function | |
2001 | * @dev: net_device pointer passed unmodified to notifier function | |
2002 | * @arg: additional u32 argument passed to the notifier function | |
2003 | * | |
2004 | * Call all network notifier blocks. Parameters and return value | |
2005 | * are as for raw_notifier_call_chain(). | |
2006 | */ | |
2007 | static int call_netdevice_notifiers_mtu(unsigned long val, | |
2008 | struct net_device *dev, u32 arg) | |
2009 | { | |
2010 | struct netdev_notifier_info_ext info = { | |
2011 | .info.dev = dev, | |
2012 | .ext.mtu = arg, | |
2013 | }; | |
2014 | ||
2015 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); | |
2016 | ||
2017 | return call_netdevice_notifiers_info(val, &info.info); | |
2018 | } | |
2019 | ||
1cf51900 | 2020 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 2021 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
4577139b DB |
2022 | |
2023 | void net_inc_ingress_queue(void) | |
2024 | { | |
aabf6772 | 2025 | static_branch_inc(&ingress_needed_key); |
4577139b DB |
2026 | } |
2027 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); | |
2028 | ||
2029 | void net_dec_ingress_queue(void) | |
2030 | { | |
aabf6772 | 2031 | static_branch_dec(&ingress_needed_key); |
4577139b DB |
2032 | } |
2033 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); | |
2034 | #endif | |
2035 | ||
1f211a1b | 2036 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 2037 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
1f211a1b DB |
2038 | |
2039 | void net_inc_egress_queue(void) | |
2040 | { | |
aabf6772 | 2041 | static_branch_inc(&egress_needed_key); |
1f211a1b DB |
2042 | } |
2043 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); | |
2044 | ||
2045 | void net_dec_egress_queue(void) | |
2046 | { | |
aabf6772 | 2047 | static_branch_dec(&egress_needed_key); |
1f211a1b DB |
2048 | } |
2049 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); | |
2050 | #endif | |
2051 | ||
27942a15 MKL |
2052 | DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
2053 | EXPORT_SYMBOL(netstamp_needed_key); | |
e9666d10 | 2054 | #ifdef CONFIG_JUMP_LABEL |
b90e5794 | 2055 | static atomic_t netstamp_needed_deferred; |
13baa00a | 2056 | static atomic_t netstamp_wanted; |
5fa8bbda | 2057 | static void netstamp_clear(struct work_struct *work) |
1da177e4 | 2058 | { |
b90e5794 | 2059 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
13baa00a | 2060 | int wanted; |
b90e5794 | 2061 | |
13baa00a ED |
2062 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
2063 | if (wanted > 0) | |
39e83922 | 2064 | static_branch_enable(&netstamp_needed_key); |
13baa00a | 2065 | else |
39e83922 | 2066 | static_branch_disable(&netstamp_needed_key); |
5fa8bbda ED |
2067 | } |
2068 | static DECLARE_WORK(netstamp_work, netstamp_clear); | |
b90e5794 | 2069 | #endif |
5fa8bbda ED |
2070 | |
2071 | void net_enable_timestamp(void) | |
2072 | { | |
e9666d10 | 2073 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2074 | int wanted; |
2075 | ||
2076 | while (1) { | |
2077 | wanted = atomic_read(&netstamp_wanted); | |
2078 | if (wanted <= 0) | |
2079 | break; | |
2080 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted) | |
2081 | return; | |
2082 | } | |
2083 | atomic_inc(&netstamp_needed_deferred); | |
2084 | schedule_work(&netstamp_work); | |
2085 | #else | |
39e83922 | 2086 | static_branch_inc(&netstamp_needed_key); |
13baa00a | 2087 | #endif |
1da177e4 | 2088 | } |
d1b19dff | 2089 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
2090 | |
2091 | void net_disable_timestamp(void) | |
2092 | { | |
e9666d10 | 2093 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2094 | int wanted; |
2095 | ||
2096 | while (1) { | |
2097 | wanted = atomic_read(&netstamp_wanted); | |
2098 | if (wanted <= 1) | |
2099 | break; | |
2100 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted) | |
2101 | return; | |
2102 | } | |
2103 | atomic_dec(&netstamp_needed_deferred); | |
5fa8bbda ED |
2104 | schedule_work(&netstamp_work); |
2105 | #else | |
39e83922 | 2106 | static_branch_dec(&netstamp_needed_key); |
5fa8bbda | 2107 | #endif |
1da177e4 | 2108 | } |
d1b19dff | 2109 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 2110 | |
3b098e2d | 2111 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 2112 | { |
2456e855 | 2113 | skb->tstamp = 0; |
27942a15 | 2114 | skb->mono_delivery_time = 0; |
39e83922 | 2115 | if (static_branch_unlikely(&netstamp_needed_key)) |
d93376f5 | 2116 | skb->tstamp = ktime_get_real(); |
1da177e4 LT |
2117 | } |
2118 | ||
39e83922 DB |
2119 | #define net_timestamp_check(COND, SKB) \ |
2120 | if (static_branch_unlikely(&netstamp_needed_key)) { \ | |
2121 | if ((COND) && !(SKB)->tstamp) \ | |
d93376f5 | 2122 | (SKB)->tstamp = ktime_get_real(); \ |
39e83922 | 2123 | } \ |
3b098e2d | 2124 | |
f4b05d27 | 2125 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
79b569f0 | 2126 | { |
5f7d5728 | 2127 | return __is_skb_forwardable(dev, skb, true); |
79b569f0 | 2128 | } |
1ee481fb | 2129 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
79b569f0 | 2130 | |
5f7d5728 JDB |
2131 | static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb, |
2132 | bool check_mtu) | |
a0265d28 | 2133 | { |
5f7d5728 | 2134 | int ret = ____dev_forward_skb(dev, skb, check_mtu); |
a0265d28 | 2135 | |
4e3264d2 MKL |
2136 | if (likely(!ret)) { |
2137 | skb->protocol = eth_type_trans(skb, dev); | |
2138 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
2139 | } | |
a0265d28 | 2140 | |
4e3264d2 | 2141 | return ret; |
a0265d28 | 2142 | } |
5f7d5728 JDB |
2143 | |
2144 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2145 | { | |
2146 | return __dev_forward_skb2(dev, skb, true); | |
2147 | } | |
a0265d28 HX |
2148 | EXPORT_SYMBOL_GPL(__dev_forward_skb); |
2149 | ||
44540960 AB |
2150 | /** |
2151 | * dev_forward_skb - loopback an skb to another netif | |
2152 | * | |
2153 | * @dev: destination network device | |
2154 | * @skb: buffer to forward | |
2155 | * | |
2156 | * return values: | |
2157 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 2158 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
2159 | * |
2160 | * dev_forward_skb can be used for injecting an skb from the | |
2161 | * start_xmit function of one device into the receive queue | |
2162 | * of another device. | |
2163 | * | |
2164 | * The receiving device may be in another namespace, so | |
2165 | * we have to clear all information in the skb that could | |
2166 | * impact namespace isolation. | |
2167 | */ | |
2168 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2169 | { | |
a0265d28 | 2170 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
44540960 AB |
2171 | } |
2172 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
2173 | ||
5f7d5728 JDB |
2174 | int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb) |
2175 | { | |
2176 | return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb); | |
2177 | } | |
2178 | ||
71d9dec2 CG |
2179 | static inline int deliver_skb(struct sk_buff *skb, |
2180 | struct packet_type *pt_prev, | |
2181 | struct net_device *orig_dev) | |
2182 | { | |
1f8b977a | 2183 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
1080e512 | 2184 | return -ENOMEM; |
63354797 | 2185 | refcount_inc(&skb->users); |
71d9dec2 CG |
2186 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
2187 | } | |
2188 | ||
7866a621 SN |
2189 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
2190 | struct packet_type **pt, | |
fbcb2170 JP |
2191 | struct net_device *orig_dev, |
2192 | __be16 type, | |
7866a621 SN |
2193 | struct list_head *ptype_list) |
2194 | { | |
2195 | struct packet_type *ptype, *pt_prev = *pt; | |
2196 | ||
2197 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
2198 | if (ptype->type != type) | |
2199 | continue; | |
2200 | if (pt_prev) | |
fbcb2170 | 2201 | deliver_skb(skb, pt_prev, orig_dev); |
7866a621 SN |
2202 | pt_prev = ptype; |
2203 | } | |
2204 | *pt = pt_prev; | |
2205 | } | |
2206 | ||
c0de08d0 EL |
2207 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
2208 | { | |
a3d744e9 | 2209 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
2210 | return false; |
2211 | ||
2212 | if (ptype->id_match) | |
2213 | return ptype->id_match(ptype, skb->sk); | |
2214 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
2215 | return true; | |
2216 | ||
2217 | return false; | |
2218 | } | |
2219 | ||
9f9a742d MR |
2220 | /** |
2221 | * dev_nit_active - return true if any network interface taps are in use | |
2222 | * | |
2223 | * @dev: network device to check for the presence of taps | |
2224 | */ | |
2225 | bool dev_nit_active(struct net_device *dev) | |
2226 | { | |
2227 | return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all); | |
2228 | } | |
2229 | EXPORT_SYMBOL_GPL(dev_nit_active); | |
2230 | ||
1da177e4 LT |
2231 | /* |
2232 | * Support routine. Sends outgoing frames to any network | |
2233 | * taps currently in use. | |
2234 | */ | |
2235 | ||
74b20582 | 2236 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
2237 | { |
2238 | struct packet_type *ptype; | |
71d9dec2 CG |
2239 | struct sk_buff *skb2 = NULL; |
2240 | struct packet_type *pt_prev = NULL; | |
7866a621 | 2241 | struct list_head *ptype_list = &ptype_all; |
a61bbcf2 | 2242 | |
1da177e4 | 2243 | rcu_read_lock(); |
7866a621 SN |
2244 | again: |
2245 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
fa788d98 VW |
2246 | if (ptype->ignore_outgoing) |
2247 | continue; | |
2248 | ||
1da177e4 LT |
2249 | /* Never send packets back to the socket |
2250 | * they originated from - MvS ([email protected]) | |
2251 | */ | |
7866a621 SN |
2252 | if (skb_loop_sk(ptype, skb)) |
2253 | continue; | |
71d9dec2 | 2254 | |
7866a621 SN |
2255 | if (pt_prev) { |
2256 | deliver_skb(skb2, pt_prev, skb->dev); | |
2257 | pt_prev = ptype; | |
2258 | continue; | |
2259 | } | |
1da177e4 | 2260 | |
7866a621 SN |
2261 | /* need to clone skb, done only once */ |
2262 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
2263 | if (!skb2) | |
2264 | goto out_unlock; | |
70978182 | 2265 | |
7866a621 | 2266 | net_timestamp_set(skb2); |
1da177e4 | 2267 | |
7866a621 SN |
2268 | /* skb->nh should be correctly |
2269 | * set by sender, so that the second statement is | |
2270 | * just protection against buggy protocols. | |
2271 | */ | |
2272 | skb_reset_mac_header(skb2); | |
2273 | ||
2274 | if (skb_network_header(skb2) < skb2->data || | |
2275 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { | |
2276 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", | |
2277 | ntohs(skb2->protocol), | |
2278 | dev->name); | |
2279 | skb_reset_network_header(skb2); | |
1da177e4 | 2280 | } |
7866a621 SN |
2281 | |
2282 | skb2->transport_header = skb2->network_header; | |
2283 | skb2->pkt_type = PACKET_OUTGOING; | |
2284 | pt_prev = ptype; | |
2285 | } | |
2286 | ||
2287 | if (ptype_list == &ptype_all) { | |
2288 | ptype_list = &dev->ptype_all; | |
2289 | goto again; | |
1da177e4 | 2290 | } |
7866a621 | 2291 | out_unlock: |
581fe0ea WB |
2292 | if (pt_prev) { |
2293 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) | |
2294 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
2295 | else | |
2296 | kfree_skb(skb2); | |
2297 | } | |
1da177e4 LT |
2298 | rcu_read_unlock(); |
2299 | } | |
74b20582 | 2300 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
1da177e4 | 2301 | |
2c53040f BH |
2302 | /** |
2303 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
2304 | * @dev: Network device |
2305 | * @txq: number of queues available | |
2306 | * | |
2307 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
2308 | * valid. To resolve this verify the tc mapping remains valid and if | |
2309 | * not NULL the mapping. With no priorities mapping to this | |
2310 | * offset/count pair it will no longer be used. In the worst case TC0 | |
2311 | * is invalid nothing can be done so disable priority mappings. If is | |
2312 | * expected that drivers will fix this mapping if they can before | |
2313 | * calling netif_set_real_num_tx_queues. | |
2314 | */ | |
bb134d22 | 2315 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
2316 | { |
2317 | int i; | |
2318 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2319 | ||
2320 | /* If TC0 is invalidated disable TC mapping */ | |
2321 | if (tc->offset + tc->count > txq) { | |
5b92be64 | 2322 | netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
2323 | dev->num_tc = 0; |
2324 | return; | |
2325 | } | |
2326 | ||
2327 | /* Invalidated prio to tc mappings set to TC0 */ | |
2328 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
2329 | int q = netdev_get_prio_tc_map(dev, i); | |
2330 | ||
2331 | tc = &dev->tc_to_txq[q]; | |
2332 | if (tc->offset + tc->count > txq) { | |
5b92be64 JB |
2333 | netdev_warn(dev, "Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
2334 | i, q); | |
4f57c087 JF |
2335 | netdev_set_prio_tc_map(dev, i, 0); |
2336 | } | |
2337 | } | |
2338 | } | |
2339 | ||
8d059b0f AD |
2340 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
2341 | { | |
2342 | if (dev->num_tc) { | |
2343 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2344 | int i; | |
2345 | ||
ffcfe25b | 2346 | /* walk through the TCs and see if it falls into any of them */ |
8d059b0f AD |
2347 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
2348 | if ((txq - tc->offset) < tc->count) | |
2349 | return i; | |
2350 | } | |
2351 | ||
ffcfe25b | 2352 | /* didn't find it, just return -1 to indicate no match */ |
8d059b0f AD |
2353 | return -1; |
2354 | } | |
2355 | ||
2356 | return 0; | |
2357 | } | |
8a5f2166 | 2358 | EXPORT_SYMBOL(netdev_txq_to_tc); |
8d059b0f | 2359 | |
537c00de | 2360 | #ifdef CONFIG_XPS |
5da9ace3 VO |
2361 | static struct static_key xps_needed __read_mostly; |
2362 | static struct static_key xps_rxqs_needed __read_mostly; | |
537c00de AD |
2363 | static DEFINE_MUTEX(xps_map_mutex); |
2364 | #define xmap_dereference(P) \ | |
2365 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
2366 | ||
6234f874 | 2367 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
2d05bf01 | 2368 | struct xps_dev_maps *old_maps, int tci, u16 index) |
537c00de | 2369 | { |
10cdc3f3 AD |
2370 | struct xps_map *map = NULL; |
2371 | int pos; | |
537c00de | 2372 | |
10cdc3f3 | 2373 | if (dev_maps) |
80d19669 | 2374 | map = xmap_dereference(dev_maps->attr_map[tci]); |
6234f874 AD |
2375 | if (!map) |
2376 | return false; | |
537c00de | 2377 | |
6234f874 AD |
2378 | for (pos = map->len; pos--;) { |
2379 | if (map->queues[pos] != index) | |
2380 | continue; | |
2381 | ||
2382 | if (map->len > 1) { | |
2383 | map->queues[pos] = map->queues[--map->len]; | |
10cdc3f3 | 2384 | break; |
537c00de | 2385 | } |
6234f874 | 2386 | |
2d05bf01 AT |
2387 | if (old_maps) |
2388 | RCU_INIT_POINTER(old_maps->attr_map[tci], NULL); | |
80d19669 | 2389 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
6234f874 AD |
2390 | kfree_rcu(map, rcu); |
2391 | return false; | |
537c00de AD |
2392 | } |
2393 | ||
6234f874 | 2394 | return true; |
10cdc3f3 AD |
2395 | } |
2396 | ||
6234f874 AD |
2397 | static bool remove_xps_queue_cpu(struct net_device *dev, |
2398 | struct xps_dev_maps *dev_maps, | |
2399 | int cpu, u16 offset, u16 count) | |
2400 | { | |
255c04a8 | 2401 | int num_tc = dev_maps->num_tc; |
184c449f AD |
2402 | bool active = false; |
2403 | int tci; | |
6234f874 | 2404 | |
184c449f AD |
2405 | for (tci = cpu * num_tc; num_tc--; tci++) { |
2406 | int i, j; | |
2407 | ||
2408 | for (i = count, j = offset; i--; j++) { | |
2d05bf01 | 2409 | if (!remove_xps_queue(dev_maps, NULL, tci, j)) |
184c449f AD |
2410 | break; |
2411 | } | |
2412 | ||
2413 | active |= i < 0; | |
6234f874 AD |
2414 | } |
2415 | ||
184c449f | 2416 | return active; |
6234f874 AD |
2417 | } |
2418 | ||
867d0ad4 SD |
2419 | static void reset_xps_maps(struct net_device *dev, |
2420 | struct xps_dev_maps *dev_maps, | |
044ab86d | 2421 | enum xps_map_type type) |
867d0ad4 | 2422 | { |
867d0ad4 | 2423 | static_key_slow_dec_cpuslocked(&xps_needed); |
044ab86d AT |
2424 | if (type == XPS_RXQS) |
2425 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); | |
2426 | ||
2427 | RCU_INIT_POINTER(dev->xps_maps[type], NULL); | |
2428 | ||
867d0ad4 SD |
2429 | kfree_rcu(dev_maps, rcu); |
2430 | } | |
2431 | ||
044ab86d AT |
2432 | static void clean_xps_maps(struct net_device *dev, enum xps_map_type type, |
2433 | u16 offset, u16 count) | |
80d19669 | 2434 | { |
044ab86d | 2435 | struct xps_dev_maps *dev_maps; |
80d19669 AN |
2436 | bool active = false; |
2437 | int i, j; | |
2438 | ||
044ab86d AT |
2439 | dev_maps = xmap_dereference(dev->xps_maps[type]); |
2440 | if (!dev_maps) | |
2441 | return; | |
2442 | ||
6f36158e AT |
2443 | for (j = 0; j < dev_maps->nr_ids; j++) |
2444 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count); | |
867d0ad4 | 2445 | if (!active) |
044ab86d | 2446 | reset_xps_maps(dev, dev_maps, type); |
80d19669 | 2447 | |
044ab86d | 2448 | if (type == XPS_CPUS) { |
6f36158e | 2449 | for (i = offset + (count - 1); count--; i--) |
f28c020f | 2450 | netdev_queue_numa_node_write( |
6f36158e | 2451 | netdev_get_tx_queue(dev, i), NUMA_NO_NODE); |
80d19669 AN |
2452 | } |
2453 | } | |
2454 | ||
6234f874 AD |
2455 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
2456 | u16 count) | |
10cdc3f3 | 2457 | { |
04157469 AN |
2458 | if (!static_key_false(&xps_needed)) |
2459 | return; | |
10cdc3f3 | 2460 | |
4d99f660 | 2461 | cpus_read_lock(); |
04157469 | 2462 | mutex_lock(&xps_map_mutex); |
10cdc3f3 | 2463 | |
044ab86d AT |
2464 | if (static_key_false(&xps_rxqs_needed)) |
2465 | clean_xps_maps(dev, XPS_RXQS, offset, count); | |
80d19669 | 2466 | |
044ab86d | 2467 | clean_xps_maps(dev, XPS_CPUS, offset, count); |
024e9679 | 2468 | |
537c00de | 2469 | mutex_unlock(&xps_map_mutex); |
4d99f660 | 2470 | cpus_read_unlock(); |
537c00de AD |
2471 | } |
2472 | ||
6234f874 AD |
2473 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
2474 | { | |
2475 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); | |
2476 | } | |
2477 | ||
80d19669 AN |
2478 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
2479 | u16 index, bool is_rxqs_map) | |
01c5f864 AD |
2480 | { |
2481 | struct xps_map *new_map; | |
2482 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
2483 | int i, pos; | |
2484 | ||
2485 | for (pos = 0; map && pos < map->len; pos++) { | |
2486 | if (map->queues[pos] != index) | |
2487 | continue; | |
2488 | return map; | |
2489 | } | |
2490 | ||
80d19669 | 2491 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
01c5f864 AD |
2492 | if (map) { |
2493 | if (pos < map->alloc_len) | |
2494 | return map; | |
2495 | ||
2496 | alloc_len = map->alloc_len * 2; | |
2497 | } | |
2498 | ||
80d19669 AN |
2499 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
2500 | * map | |
2501 | */ | |
2502 | if (is_rxqs_map) | |
2503 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); | |
2504 | else | |
2505 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
2506 | cpu_to_node(attr_index)); | |
01c5f864 AD |
2507 | if (!new_map) |
2508 | return NULL; | |
2509 | ||
2510 | for (i = 0; i < pos; i++) | |
2511 | new_map->queues[i] = map->queues[i]; | |
2512 | new_map->alloc_len = alloc_len; | |
2513 | new_map->len = pos; | |
2514 | ||
2515 | return new_map; | |
2516 | } | |
2517 | ||
402fbb99 AT |
2518 | /* Copy xps maps at a given index */ |
2519 | static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps, | |
2520 | struct xps_dev_maps *new_dev_maps, int index, | |
2521 | int tc, bool skip_tc) | |
2522 | { | |
2523 | int i, tci = index * dev_maps->num_tc; | |
2524 | struct xps_map *map; | |
2525 | ||
2526 | /* copy maps belonging to foreign traffic classes */ | |
2527 | for (i = 0; i < dev_maps->num_tc; i++, tci++) { | |
2528 | if (i == tc && skip_tc) | |
2529 | continue; | |
2530 | ||
2531 | /* fill in the new device map from the old device map */ | |
2532 | map = xmap_dereference(dev_maps->attr_map[tci]); | |
2533 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
2534 | } | |
2535 | } | |
2536 | ||
4d99f660 | 2537 | /* Must be called under cpus_read_lock */ |
80d19669 | 2538 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
044ab86d | 2539 | u16 index, enum xps_map_type type) |
537c00de | 2540 | { |
2d05bf01 | 2541 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL; |
6f36158e | 2542 | const unsigned long *online_mask = NULL; |
255c04a8 | 2543 | bool active = false, copy = false; |
80d19669 | 2544 | int i, j, tci, numa_node_id = -2; |
184c449f | 2545 | int maps_sz, num_tc = 1, tc = 0; |
537c00de | 2546 | struct xps_map *map, *new_map; |
80d19669 | 2547 | unsigned int nr_ids; |
537c00de | 2548 | |
184c449f | 2549 | if (dev->num_tc) { |
ffcfe25b | 2550 | /* Do not allow XPS on subordinate device directly */ |
184c449f | 2551 | num_tc = dev->num_tc; |
ffcfe25b AD |
2552 | if (num_tc < 0) |
2553 | return -EINVAL; | |
2554 | ||
2555 | /* If queue belongs to subordinate dev use its map */ | |
2556 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
2557 | ||
184c449f AD |
2558 | tc = netdev_txq_to_tc(dev, index); |
2559 | if (tc < 0) | |
2560 | return -EINVAL; | |
2561 | } | |
2562 | ||
537c00de | 2563 | mutex_lock(&xps_map_mutex); |
044ab86d AT |
2564 | |
2565 | dev_maps = xmap_dereference(dev->xps_maps[type]); | |
2566 | if (type == XPS_RXQS) { | |
80d19669 | 2567 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); |
80d19669 AN |
2568 | nr_ids = dev->num_rx_queues; |
2569 | } else { | |
2570 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); | |
6f36158e | 2571 | if (num_possible_cpus() > 1) |
80d19669 | 2572 | online_mask = cpumask_bits(cpu_online_mask); |
80d19669 AN |
2573 | nr_ids = nr_cpu_ids; |
2574 | } | |
537c00de | 2575 | |
80d19669 AN |
2576 | if (maps_sz < L1_CACHE_BYTES) |
2577 | maps_sz = L1_CACHE_BYTES; | |
537c00de | 2578 | |
255c04a8 | 2579 | /* The old dev_maps could be larger or smaller than the one we're |
5478fcd0 AT |
2580 | * setting up now, as dev->num_tc or nr_ids could have been updated in |
2581 | * between. We could try to be smart, but let's be safe instead and only | |
2582 | * copy foreign traffic classes if the two map sizes match. | |
255c04a8 | 2583 | */ |
5478fcd0 AT |
2584 | if (dev_maps && |
2585 | dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids) | |
255c04a8 AT |
2586 | copy = true; |
2587 | ||
01c5f864 | 2588 | /* allocate memory for queue storage */ |
80d19669 AN |
2589 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
2590 | j < nr_ids;) { | |
2bb60cb9 | 2591 | if (!new_dev_maps) { |
255c04a8 AT |
2592 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); |
2593 | if (!new_dev_maps) { | |
2594 | mutex_unlock(&xps_map_mutex); | |
2595 | return -ENOMEM; | |
2596 | } | |
2597 | ||
5478fcd0 | 2598 | new_dev_maps->nr_ids = nr_ids; |
255c04a8 | 2599 | new_dev_maps->num_tc = num_tc; |
2bb60cb9 | 2600 | } |
01c5f864 | 2601 | |
80d19669 | 2602 | tci = j * num_tc + tc; |
255c04a8 | 2603 | map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL; |
01c5f864 | 2604 | |
044ab86d | 2605 | map = expand_xps_map(map, j, index, type == XPS_RXQS); |
01c5f864 AD |
2606 | if (!map) |
2607 | goto error; | |
2608 | ||
80d19669 | 2609 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
01c5f864 AD |
2610 | } |
2611 | ||
2612 | if (!new_dev_maps) | |
2613 | goto out_no_new_maps; | |
2614 | ||
867d0ad4 SD |
2615 | if (!dev_maps) { |
2616 | /* Increment static keys at most once per type */ | |
2617 | static_key_slow_inc_cpuslocked(&xps_needed); | |
044ab86d | 2618 | if (type == XPS_RXQS) |
867d0ad4 SD |
2619 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); |
2620 | } | |
04157469 | 2621 | |
6f36158e | 2622 | for (j = 0; j < nr_ids; j++) { |
402fbb99 | 2623 | bool skip_tc = false; |
184c449f | 2624 | |
80d19669 | 2625 | tci = j * num_tc + tc; |
80d19669 AN |
2626 | if (netif_attr_test_mask(j, mask, nr_ids) && |
2627 | netif_attr_test_online(j, online_mask, nr_ids)) { | |
2628 | /* add tx-queue to CPU/rx-queue maps */ | |
01c5f864 AD |
2629 | int pos = 0; |
2630 | ||
402fbb99 AT |
2631 | skip_tc = true; |
2632 | ||
80d19669 | 2633 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
01c5f864 AD |
2634 | while ((pos < map->len) && (map->queues[pos] != index)) |
2635 | pos++; | |
2636 | ||
2637 | if (pos == map->len) | |
2638 | map->queues[map->len++] = index; | |
537c00de | 2639 | #ifdef CONFIG_NUMA |
044ab86d | 2640 | if (type == XPS_CPUS) { |
80d19669 AN |
2641 | if (numa_node_id == -2) |
2642 | numa_node_id = cpu_to_node(j); | |
2643 | else if (numa_node_id != cpu_to_node(j)) | |
2644 | numa_node_id = -1; | |
2645 | } | |
537c00de | 2646 | #endif |
537c00de | 2647 | } |
01c5f864 | 2648 | |
402fbb99 AT |
2649 | if (copy) |
2650 | xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc, | |
2651 | skip_tc); | |
537c00de AD |
2652 | } |
2653 | ||
044ab86d | 2654 | rcu_assign_pointer(dev->xps_maps[type], new_dev_maps); |
01c5f864 | 2655 | |
537c00de | 2656 | /* Cleanup old maps */ |
184c449f AD |
2657 | if (!dev_maps) |
2658 | goto out_no_old_maps; | |
2659 | ||
6f36158e | 2660 | for (j = 0; j < dev_maps->nr_ids; j++) { |
255c04a8 | 2661 | for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) { |
80d19669 | 2662 | map = xmap_dereference(dev_maps->attr_map[tci]); |
255c04a8 AT |
2663 | if (!map) |
2664 | continue; | |
2665 | ||
2666 | if (copy) { | |
2667 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
2668 | if (map == new_map) | |
2669 | continue; | |
2670 | } | |
2671 | ||
75b2758a | 2672 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
255c04a8 | 2673 | kfree_rcu(map, rcu); |
01c5f864 | 2674 | } |
537c00de AD |
2675 | } |
2676 | ||
2d05bf01 | 2677 | old_dev_maps = dev_maps; |
184c449f AD |
2678 | |
2679 | out_no_old_maps: | |
01c5f864 AD |
2680 | dev_maps = new_dev_maps; |
2681 | active = true; | |
537c00de | 2682 | |
01c5f864 | 2683 | out_no_new_maps: |
044ab86d | 2684 | if (type == XPS_CPUS) |
80d19669 AN |
2685 | /* update Tx queue numa node */ |
2686 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), | |
2687 | (numa_node_id >= 0) ? | |
2688 | numa_node_id : NUMA_NO_NODE); | |
537c00de | 2689 | |
01c5f864 AD |
2690 | if (!dev_maps) |
2691 | goto out_no_maps; | |
2692 | ||
80d19669 | 2693 | /* removes tx-queue from unused CPUs/rx-queues */ |
6f36158e | 2694 | for (j = 0; j < dev_maps->nr_ids; j++) { |
132f743b AT |
2695 | tci = j * dev_maps->num_tc; |
2696 | ||
2697 | for (i = 0; i < dev_maps->num_tc; i++, tci++) { | |
2698 | if (i == tc && | |
2699 | netif_attr_test_mask(j, mask, dev_maps->nr_ids) && | |
2700 | netif_attr_test_online(j, online_mask, dev_maps->nr_ids)) | |
2701 | continue; | |
2702 | ||
2d05bf01 AT |
2703 | active |= remove_xps_queue(dev_maps, |
2704 | copy ? old_dev_maps : NULL, | |
2705 | tci, index); | |
132f743b | 2706 | } |
01c5f864 AD |
2707 | } |
2708 | ||
2d05bf01 AT |
2709 | if (old_dev_maps) |
2710 | kfree_rcu(old_dev_maps, rcu); | |
2711 | ||
01c5f864 | 2712 | /* free map if not active */ |
867d0ad4 | 2713 | if (!active) |
044ab86d | 2714 | reset_xps_maps(dev, dev_maps, type); |
01c5f864 AD |
2715 | |
2716 | out_no_maps: | |
537c00de AD |
2717 | mutex_unlock(&xps_map_mutex); |
2718 | ||
2719 | return 0; | |
2720 | error: | |
01c5f864 | 2721 | /* remove any maps that we added */ |
6f36158e | 2722 | for (j = 0; j < nr_ids; j++) { |
80d19669 AN |
2723 | for (i = num_tc, tci = j * num_tc; i--; tci++) { |
2724 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
255c04a8 | 2725 | map = copy ? |
80d19669 | 2726 | xmap_dereference(dev_maps->attr_map[tci]) : |
184c449f AD |
2727 | NULL; |
2728 | if (new_map && new_map != map) | |
2729 | kfree(new_map); | |
2730 | } | |
01c5f864 AD |
2731 | } |
2732 | ||
537c00de AD |
2733 | mutex_unlock(&xps_map_mutex); |
2734 | ||
537c00de AD |
2735 | kfree(new_dev_maps); |
2736 | return -ENOMEM; | |
2737 | } | |
4d99f660 | 2738 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
80d19669 AN |
2739 | |
2740 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, | |
2741 | u16 index) | |
2742 | { | |
4d99f660 AV |
2743 | int ret; |
2744 | ||
2745 | cpus_read_lock(); | |
044ab86d | 2746 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS); |
4d99f660 AV |
2747 | cpus_read_unlock(); |
2748 | ||
2749 | return ret; | |
80d19669 | 2750 | } |
537c00de AD |
2751 | EXPORT_SYMBOL(netif_set_xps_queue); |
2752 | ||
2753 | #endif | |
ffcfe25b AD |
2754 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
2755 | { | |
2756 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2757 | ||
2758 | /* Unbind any subordinate channels */ | |
2759 | while (txq-- != &dev->_tx[0]) { | |
2760 | if (txq->sb_dev) | |
2761 | netdev_unbind_sb_channel(dev, txq->sb_dev); | |
2762 | } | |
2763 | } | |
2764 | ||
9cf1f6a8 AD |
2765 | void netdev_reset_tc(struct net_device *dev) |
2766 | { | |
6234f874 AD |
2767 | #ifdef CONFIG_XPS |
2768 | netif_reset_xps_queues_gt(dev, 0); | |
2769 | #endif | |
ffcfe25b AD |
2770 | netdev_unbind_all_sb_channels(dev); |
2771 | ||
2772 | /* Reset TC configuration of device */ | |
9cf1f6a8 AD |
2773 | dev->num_tc = 0; |
2774 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); | |
2775 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); | |
2776 | } | |
2777 | EXPORT_SYMBOL(netdev_reset_tc); | |
2778 | ||
2779 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) | |
2780 | { | |
2781 | if (tc >= dev->num_tc) | |
2782 | return -EINVAL; | |
2783 | ||
6234f874 AD |
2784 | #ifdef CONFIG_XPS |
2785 | netif_reset_xps_queues(dev, offset, count); | |
2786 | #endif | |
9cf1f6a8 AD |
2787 | dev->tc_to_txq[tc].count = count; |
2788 | dev->tc_to_txq[tc].offset = offset; | |
2789 | return 0; | |
2790 | } | |
2791 | EXPORT_SYMBOL(netdev_set_tc_queue); | |
2792 | ||
2793 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) | |
2794 | { | |
2795 | if (num_tc > TC_MAX_QUEUE) | |
2796 | return -EINVAL; | |
2797 | ||
6234f874 AD |
2798 | #ifdef CONFIG_XPS |
2799 | netif_reset_xps_queues_gt(dev, 0); | |
2800 | #endif | |
ffcfe25b AD |
2801 | netdev_unbind_all_sb_channels(dev); |
2802 | ||
9cf1f6a8 AD |
2803 | dev->num_tc = num_tc; |
2804 | return 0; | |
2805 | } | |
2806 | EXPORT_SYMBOL(netdev_set_num_tc); | |
2807 | ||
ffcfe25b AD |
2808 | void netdev_unbind_sb_channel(struct net_device *dev, |
2809 | struct net_device *sb_dev) | |
2810 | { | |
2811 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2812 | ||
2813 | #ifdef CONFIG_XPS | |
2814 | netif_reset_xps_queues_gt(sb_dev, 0); | |
2815 | #endif | |
2816 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); | |
2817 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); | |
2818 | ||
2819 | while (txq-- != &dev->_tx[0]) { | |
2820 | if (txq->sb_dev == sb_dev) | |
2821 | txq->sb_dev = NULL; | |
2822 | } | |
2823 | } | |
2824 | EXPORT_SYMBOL(netdev_unbind_sb_channel); | |
2825 | ||
2826 | int netdev_bind_sb_channel_queue(struct net_device *dev, | |
2827 | struct net_device *sb_dev, | |
2828 | u8 tc, u16 count, u16 offset) | |
2829 | { | |
2830 | /* Make certain the sb_dev and dev are already configured */ | |
2831 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) | |
2832 | return -EINVAL; | |
2833 | ||
2834 | /* We cannot hand out queues we don't have */ | |
2835 | if ((offset + count) > dev->real_num_tx_queues) | |
2836 | return -EINVAL; | |
2837 | ||
2838 | /* Record the mapping */ | |
2839 | sb_dev->tc_to_txq[tc].count = count; | |
2840 | sb_dev->tc_to_txq[tc].offset = offset; | |
2841 | ||
2842 | /* Provide a way for Tx queue to find the tc_to_txq map or | |
2843 | * XPS map for itself. | |
2844 | */ | |
2845 | while (count--) | |
2846 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; | |
2847 | ||
2848 | return 0; | |
2849 | } | |
2850 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); | |
2851 | ||
2852 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) | |
2853 | { | |
2854 | /* Do not use a multiqueue device to represent a subordinate channel */ | |
2855 | if (netif_is_multiqueue(dev)) | |
2856 | return -ENODEV; | |
2857 | ||
2858 | /* We allow channels 1 - 32767 to be used for subordinate channels. | |
2859 | * Channel 0 is meant to be "native" mode and used only to represent | |
2860 | * the main root device. We allow writing 0 to reset the device back | |
2861 | * to normal mode after being used as a subordinate channel. | |
2862 | */ | |
2863 | if (channel > S16_MAX) | |
2864 | return -EINVAL; | |
2865 | ||
2866 | dev->num_tc = -channel; | |
2867 | ||
2868 | return 0; | |
2869 | } | |
2870 | EXPORT_SYMBOL(netdev_set_sb_channel); | |
2871 | ||
f0796d5c JF |
2872 | /* |
2873 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
3a053b1a | 2874 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
f0796d5c | 2875 | */ |
e6484930 | 2876 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2877 | { |
ac5b7019 | 2878 | bool disabling; |
1d24eb48 TH |
2879 | int rc; |
2880 | ||
ac5b7019 JK |
2881 | disabling = txq < dev->real_num_tx_queues; |
2882 | ||
e6484930 TH |
2883 | if (txq < 1 || txq > dev->num_tx_queues) |
2884 | return -EINVAL; | |
f0796d5c | 2885 | |
5c56580b BH |
2886 | if (dev->reg_state == NETREG_REGISTERED || |
2887 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2888 | ASSERT_RTNL(); |
2889 | ||
1d24eb48 TH |
2890 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2891 | txq); | |
bf264145 TH |
2892 | if (rc) |
2893 | return rc; | |
2894 | ||
4f57c087 JF |
2895 | if (dev->num_tc) |
2896 | netif_setup_tc(dev, txq); | |
2897 | ||
1e080f17 JK |
2898 | dev_qdisc_change_real_num_tx(dev, txq); |
2899 | ||
ac5b7019 JK |
2900 | dev->real_num_tx_queues = txq; |
2901 | ||
2902 | if (disabling) { | |
2903 | synchronize_net(); | |
e6484930 | 2904 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2905 | #ifdef CONFIG_XPS |
2906 | netif_reset_xps_queues_gt(dev, txq); | |
2907 | #endif | |
2908 | } | |
ac5b7019 JK |
2909 | } else { |
2910 | dev->real_num_tx_queues = txq; | |
f0796d5c | 2911 | } |
e6484930 | 2912 | |
e6484930 | 2913 | return 0; |
f0796d5c JF |
2914 | } |
2915 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2916 | |
a953be53 | 2917 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
2918 | /** |
2919 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2920 | * @dev: Network device | |
2921 | * @rxq: Actual number of RX queues | |
2922 | * | |
2923 | * This must be called either with the rtnl_lock held or before | |
2924 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2925 | * negative error code. If called before registration, it always |
2926 | * succeeds. | |
62fe0b40 BH |
2927 | */ |
2928 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2929 | { | |
2930 | int rc; | |
2931 | ||
bd25fa7b TH |
2932 | if (rxq < 1 || rxq > dev->num_rx_queues) |
2933 | return -EINVAL; | |
2934 | ||
62fe0b40 BH |
2935 | if (dev->reg_state == NETREG_REGISTERED) { |
2936 | ASSERT_RTNL(); | |
2937 | ||
62fe0b40 BH |
2938 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
2939 | rxq); | |
2940 | if (rc) | |
2941 | return rc; | |
62fe0b40 BH |
2942 | } |
2943 | ||
2944 | dev->real_num_rx_queues = rxq; | |
2945 | return 0; | |
2946 | } | |
2947 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
2948 | #endif | |
2949 | ||
271e5b7d JK |
2950 | /** |
2951 | * netif_set_real_num_queues - set actual number of RX and TX queues used | |
2952 | * @dev: Network device | |
2953 | * @txq: Actual number of TX queues | |
2954 | * @rxq: Actual number of RX queues | |
2955 | * | |
2956 | * Set the real number of both TX and RX queues. | |
2957 | * Does nothing if the number of queues is already correct. | |
2958 | */ | |
2959 | int netif_set_real_num_queues(struct net_device *dev, | |
2960 | unsigned int txq, unsigned int rxq) | |
2961 | { | |
2962 | unsigned int old_rxq = dev->real_num_rx_queues; | |
2963 | int err; | |
2964 | ||
2965 | if (txq < 1 || txq > dev->num_tx_queues || | |
2966 | rxq < 1 || rxq > dev->num_rx_queues) | |
2967 | return -EINVAL; | |
2968 | ||
2969 | /* Start from increases, so the error path only does decreases - | |
2970 | * decreases can't fail. | |
2971 | */ | |
2972 | if (rxq > dev->real_num_rx_queues) { | |
2973 | err = netif_set_real_num_rx_queues(dev, rxq); | |
2974 | if (err) | |
2975 | return err; | |
2976 | } | |
2977 | if (txq > dev->real_num_tx_queues) { | |
2978 | err = netif_set_real_num_tx_queues(dev, txq); | |
2979 | if (err) | |
2980 | goto undo_rx; | |
2981 | } | |
2982 | if (rxq < dev->real_num_rx_queues) | |
2983 | WARN_ON(netif_set_real_num_rx_queues(dev, rxq)); | |
2984 | if (txq < dev->real_num_tx_queues) | |
2985 | WARN_ON(netif_set_real_num_tx_queues(dev, txq)); | |
2986 | ||
2987 | return 0; | |
2988 | undo_rx: | |
2989 | WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq)); | |
2990 | return err; | |
2991 | } | |
2992 | EXPORT_SYMBOL(netif_set_real_num_queues); | |
2993 | ||
2c53040f BH |
2994 | /** |
2995 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 | 2996 | * |
046e1537 ÍH |
2997 | * Default value is the number of physical cores if there are only 1 or 2, or |
2998 | * divided by 2 if there are more. | |
16917b87 | 2999 | */ |
a55b138b | 3000 | int netif_get_num_default_rss_queues(void) |
16917b87 | 3001 | { |
046e1537 ÍH |
3002 | cpumask_var_t cpus; |
3003 | int cpu, count = 0; | |
3004 | ||
3005 | if (unlikely(is_kdump_kernel() || !zalloc_cpumask_var(&cpus, GFP_KERNEL))) | |
3006 | return 1; | |
3007 | ||
3008 | cpumask_copy(cpus, cpu_online_mask); | |
3009 | for_each_cpu(cpu, cpus) { | |
3010 | ++count; | |
3011 | cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu)); | |
3012 | } | |
3013 | free_cpumask_var(cpus); | |
3014 | ||
3015 | return count > 2 ? DIV_ROUND_UP(count, 2) : count; | |
16917b87 YM |
3016 | } |
3017 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
3018 | ||
3bcb846c | 3019 | static void __netif_reschedule(struct Qdisc *q) |
56079431 | 3020 | { |
def82a1d JP |
3021 | struct softnet_data *sd; |
3022 | unsigned long flags; | |
56079431 | 3023 | |
def82a1d | 3024 | local_irq_save(flags); |
903ceff7 | 3025 | sd = this_cpu_ptr(&softnet_data); |
a9cbd588 CG |
3026 | q->next_sched = NULL; |
3027 | *sd->output_queue_tailp = q; | |
3028 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
3029 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
3030 | local_irq_restore(flags); | |
3031 | } | |
3032 | ||
3033 | void __netif_schedule(struct Qdisc *q) | |
3034 | { | |
3035 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
3036 | __netif_reschedule(q); | |
56079431 DV |
3037 | } |
3038 | EXPORT_SYMBOL(__netif_schedule); | |
3039 | ||
e6247027 ED |
3040 | struct dev_kfree_skb_cb { |
3041 | enum skb_free_reason reason; | |
3042 | }; | |
3043 | ||
3044 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) | |
56079431 | 3045 | { |
e6247027 ED |
3046 | return (struct dev_kfree_skb_cb *)skb->cb; |
3047 | } | |
3048 | ||
46e5da40 JF |
3049 | void netif_schedule_queue(struct netdev_queue *txq) |
3050 | { | |
3051 | rcu_read_lock(); | |
5be5515a | 3052 | if (!netif_xmit_stopped(txq)) { |
46e5da40 JF |
3053 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
3054 | ||
3055 | __netif_schedule(q); | |
3056 | } | |
3057 | rcu_read_unlock(); | |
3058 | } | |
3059 | EXPORT_SYMBOL(netif_schedule_queue); | |
3060 | ||
46e5da40 JF |
3061 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
3062 | { | |
3063 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { | |
3064 | struct Qdisc *q; | |
3065 | ||
3066 | rcu_read_lock(); | |
3067 | q = rcu_dereference(dev_queue->qdisc); | |
3068 | __netif_schedule(q); | |
3069 | rcu_read_unlock(); | |
3070 | } | |
3071 | } | |
3072 | EXPORT_SYMBOL(netif_tx_wake_queue); | |
3073 | ||
e6247027 | 3074 | void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 | 3075 | { |
e6247027 | 3076 | unsigned long flags; |
56079431 | 3077 | |
9899886d MJ |
3078 | if (unlikely(!skb)) |
3079 | return; | |
3080 | ||
63354797 | 3081 | if (likely(refcount_read(&skb->users) == 1)) { |
e6247027 | 3082 | smp_rmb(); |
63354797 RE |
3083 | refcount_set(&skb->users, 0); |
3084 | } else if (likely(!refcount_dec_and_test(&skb->users))) { | |
e6247027 | 3085 | return; |
bea3348e | 3086 | } |
e6247027 ED |
3087 | get_kfree_skb_cb(skb)->reason = reason; |
3088 | local_irq_save(flags); | |
3089 | skb->next = __this_cpu_read(softnet_data.completion_queue); | |
3090 | __this_cpu_write(softnet_data.completion_queue, skb); | |
3091 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
3092 | local_irq_restore(flags); | |
56079431 | 3093 | } |
e6247027 | 3094 | EXPORT_SYMBOL(__dev_kfree_skb_irq); |
56079431 | 3095 | |
e6247027 | 3096 | void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 | 3097 | { |
afa79d08 | 3098 | if (in_hardirq() || irqs_disabled()) |
e6247027 | 3099 | __dev_kfree_skb_irq(skb, reason); |
56079431 DV |
3100 | else |
3101 | dev_kfree_skb(skb); | |
3102 | } | |
e6247027 | 3103 | EXPORT_SYMBOL(__dev_kfree_skb_any); |
56079431 DV |
3104 | |
3105 | ||
bea3348e SH |
3106 | /** |
3107 | * netif_device_detach - mark device as removed | |
3108 | * @dev: network device | |
3109 | * | |
3110 | * Mark device as removed from system and therefore no longer available. | |
3111 | */ | |
56079431 DV |
3112 | void netif_device_detach(struct net_device *dev) |
3113 | { | |
3114 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3115 | netif_running(dev)) { | |
d543103a | 3116 | netif_tx_stop_all_queues(dev); |
56079431 DV |
3117 | } |
3118 | } | |
3119 | EXPORT_SYMBOL(netif_device_detach); | |
3120 | ||
bea3348e SH |
3121 | /** |
3122 | * netif_device_attach - mark device as attached | |
3123 | * @dev: network device | |
3124 | * | |
3125 | * Mark device as attached from system and restart if needed. | |
3126 | */ | |
56079431 DV |
3127 | void netif_device_attach(struct net_device *dev) |
3128 | { | |
3129 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3130 | netif_running(dev)) { | |
d543103a | 3131 | netif_tx_wake_all_queues(dev); |
4ec93edb | 3132 | __netdev_watchdog_up(dev); |
56079431 DV |
3133 | } |
3134 | } | |
3135 | EXPORT_SYMBOL(netif_device_attach); | |
3136 | ||
5605c762 JP |
3137 | /* |
3138 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
3139 | * to be used as a distribution range. | |
3140 | */ | |
eadec877 AD |
3141 | static u16 skb_tx_hash(const struct net_device *dev, |
3142 | const struct net_device *sb_dev, | |
3143 | struct sk_buff *skb) | |
5605c762 JP |
3144 | { |
3145 | u32 hash; | |
3146 | u16 qoffset = 0; | |
1b837d48 | 3147 | u16 qcount = dev->real_num_tx_queues; |
5605c762 | 3148 | |
eadec877 AD |
3149 | if (dev->num_tc) { |
3150 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
3151 | ||
3152 | qoffset = sb_dev->tc_to_txq[tc].offset; | |
3153 | qcount = sb_dev->tc_to_txq[tc].count; | |
0c57eeec MC |
3154 | if (unlikely(!qcount)) { |
3155 | net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n", | |
3156 | sb_dev->name, qoffset, tc); | |
3157 | qoffset = 0; | |
3158 | qcount = dev->real_num_tx_queues; | |
3159 | } | |
eadec877 AD |
3160 | } |
3161 | ||
5605c762 JP |
3162 | if (skb_rx_queue_recorded(skb)) { |
3163 | hash = skb_get_rx_queue(skb); | |
6e11d157 AN |
3164 | if (hash >= qoffset) |
3165 | hash -= qoffset; | |
1b837d48 AD |
3166 | while (unlikely(hash >= qcount)) |
3167 | hash -= qcount; | |
eadec877 | 3168 | return hash + qoffset; |
5605c762 JP |
3169 | } |
3170 | ||
3171 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; | |
3172 | } | |
5605c762 | 3173 | |
36c92474 BH |
3174 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
3175 | { | |
84d15ae5 | 3176 | static const netdev_features_t null_features; |
36c92474 | 3177 | struct net_device *dev = skb->dev; |
88ad4175 | 3178 | const char *name = ""; |
36c92474 | 3179 | |
c846ad9b BG |
3180 | if (!net_ratelimit()) |
3181 | return; | |
3182 | ||
88ad4175 BM |
3183 | if (dev) { |
3184 | if (dev->dev.parent) | |
3185 | name = dev_driver_string(dev->dev.parent); | |
3186 | else | |
3187 | name = netdev_name(dev); | |
3188 | } | |
6413139d WB |
3189 | skb_dump(KERN_WARNING, skb, false); |
3190 | WARN(1, "%s: caps=(%pNF, %pNF)\n", | |
88ad4175 | 3191 | name, dev ? &dev->features : &null_features, |
6413139d | 3192 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
36c92474 BH |
3193 | } |
3194 | ||
1da177e4 LT |
3195 | /* |
3196 | * Invalidate hardware checksum when packet is to be mangled, and | |
3197 | * complete checksum manually on outgoing path. | |
3198 | */ | |
84fa7933 | 3199 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 3200 | { |
d3bc23e7 | 3201 | __wsum csum; |
663ead3b | 3202 | int ret = 0, offset; |
1da177e4 | 3203 | |
84fa7933 | 3204 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
3205 | goto out_set_summed; |
3206 | ||
3aefd7d6 | 3207 | if (unlikely(skb_is_gso(skb))) { |
36c92474 BH |
3208 | skb_warn_bad_offload(skb); |
3209 | return -EINVAL; | |
1da177e4 LT |
3210 | } |
3211 | ||
cef401de ED |
3212 | /* Before computing a checksum, we should make sure no frag could |
3213 | * be modified by an external entity : checksum could be wrong. | |
3214 | */ | |
3215 | if (skb_has_shared_frag(skb)) { | |
3216 | ret = __skb_linearize(skb); | |
3217 | if (ret) | |
3218 | goto out; | |
3219 | } | |
3220 | ||
55508d60 | 3221 | offset = skb_checksum_start_offset(skb); |
a030847e HX |
3222 | BUG_ON(offset >= skb_headlen(skb)); |
3223 | csum = skb_checksum(skb, offset, skb->len - offset, 0); | |
3224 | ||
3225 | offset += skb->csum_offset; | |
3226 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); | |
3227 | ||
8211fbfa HK |
3228 | ret = skb_ensure_writable(skb, offset + sizeof(__sum16)); |
3229 | if (ret) | |
3230 | goto out; | |
1da177e4 | 3231 | |
4f2e4ad5 | 3232 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
a430a43d | 3233 | out_set_summed: |
1da177e4 | 3234 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 3235 | out: |
1da177e4 LT |
3236 | return ret; |
3237 | } | |
d1b19dff | 3238 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 3239 | |
b72b5bf6 DC |
3240 | int skb_crc32c_csum_help(struct sk_buff *skb) |
3241 | { | |
3242 | __le32 crc32c_csum; | |
3243 | int ret = 0, offset, start; | |
3244 | ||
3245 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
3246 | goto out; | |
3247 | ||
3248 | if (unlikely(skb_is_gso(skb))) | |
3249 | goto out; | |
3250 | ||
3251 | /* Before computing a checksum, we should make sure no frag could | |
3252 | * be modified by an external entity : checksum could be wrong. | |
3253 | */ | |
3254 | if (unlikely(skb_has_shared_frag(skb))) { | |
3255 | ret = __skb_linearize(skb); | |
3256 | if (ret) | |
3257 | goto out; | |
3258 | } | |
3259 | start = skb_checksum_start_offset(skb); | |
3260 | offset = start + offsetof(struct sctphdr, checksum); | |
3261 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { | |
3262 | ret = -EINVAL; | |
3263 | goto out; | |
3264 | } | |
8211fbfa HK |
3265 | |
3266 | ret = skb_ensure_writable(skb, offset + sizeof(__le32)); | |
3267 | if (ret) | |
3268 | goto out; | |
3269 | ||
b72b5bf6 DC |
3270 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, |
3271 | skb->len - start, ~(__u32)0, | |
3272 | crc32c_csum_stub)); | |
3273 | *(__le32 *)(skb->data + offset) = crc32c_csum; | |
3274 | skb->ip_summed = CHECKSUM_NONE; | |
dba00306 | 3275 | skb->csum_not_inet = 0; |
b72b5bf6 DC |
3276 | out: |
3277 | return ret; | |
3278 | } | |
3279 | ||
53d6471c | 3280 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
f6a78bfc | 3281 | { |
252e3346 | 3282 | __be16 type = skb->protocol; |
f6a78bfc | 3283 | |
19acc327 PS |
3284 | /* Tunnel gso handlers can set protocol to ethernet. */ |
3285 | if (type == htons(ETH_P_TEB)) { | |
3286 | struct ethhdr *eth; | |
3287 | ||
3288 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) | |
3289 | return 0; | |
3290 | ||
1dfe82eb | 3291 | eth = (struct ethhdr *)skb->data; |
19acc327 PS |
3292 | type = eth->h_proto; |
3293 | } | |
3294 | ||
d4bcef3f | 3295 | return __vlan_get_protocol(skb, type, depth); |
ec5f0615 PS |
3296 | } |
3297 | ||
05e8ef4a PS |
3298 | /* openvswitch calls this on rx path, so we need a different check. |
3299 | */ | |
3300 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) | |
3301 | { | |
3302 | if (tx_path) | |
0c19f846 WB |
3303 | return skb->ip_summed != CHECKSUM_PARTIAL && |
3304 | skb->ip_summed != CHECKSUM_UNNECESSARY; | |
6e7bc478 ED |
3305 | |
3306 | return skb->ip_summed == CHECKSUM_NONE; | |
05e8ef4a PS |
3307 | } |
3308 | ||
3309 | /** | |
3310 | * __skb_gso_segment - Perform segmentation on skb. | |
3311 | * @skb: buffer to segment | |
3312 | * @features: features for the output path (see dev->features) | |
3313 | * @tx_path: whether it is called in TX path | |
3314 | * | |
3315 | * This function segments the given skb and returns a list of segments. | |
3316 | * | |
3317 | * It may return NULL if the skb requires no segmentation. This is | |
3318 | * only possible when GSO is used for verifying header integrity. | |
9207f9d4 | 3319 | * |
a08e7fd9 | 3320 | * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb. |
05e8ef4a PS |
3321 | */ |
3322 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, | |
3323 | netdev_features_t features, bool tx_path) | |
3324 | { | |
b2504a5d ED |
3325 | struct sk_buff *segs; |
3326 | ||
05e8ef4a PS |
3327 | if (unlikely(skb_needs_check(skb, tx_path))) { |
3328 | int err; | |
3329 | ||
b2504a5d | 3330 | /* We're going to init ->check field in TCP or UDP header */ |
a40e0a66 | 3331 | err = skb_cow_head(skb, 0); |
3332 | if (err < 0) | |
05e8ef4a PS |
3333 | return ERR_PTR(err); |
3334 | } | |
3335 | ||
802ab55a AD |
3336 | /* Only report GSO partial support if it will enable us to |
3337 | * support segmentation on this frame without needing additional | |
3338 | * work. | |
3339 | */ | |
3340 | if (features & NETIF_F_GSO_PARTIAL) { | |
3341 | netdev_features_t partial_features = NETIF_F_GSO_ROBUST; | |
3342 | struct net_device *dev = skb->dev; | |
3343 | ||
3344 | partial_features |= dev->features & dev->gso_partial_features; | |
3345 | if (!skb_gso_ok(skb, features | partial_features)) | |
3346 | features &= ~NETIF_F_GSO_PARTIAL; | |
3347 | } | |
3348 | ||
a08e7fd9 | 3349 | BUILD_BUG_ON(SKB_GSO_CB_OFFSET + |
9207f9d4 KK |
3350 | sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb)); |
3351 | ||
68c33163 | 3352 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
3347c960 ED |
3353 | SKB_GSO_CB(skb)->encap_level = 0; |
3354 | ||
05e8ef4a PS |
3355 | skb_reset_mac_header(skb); |
3356 | skb_reset_mac_len(skb); | |
3357 | ||
b2504a5d ED |
3358 | segs = skb_mac_gso_segment(skb, features); |
3359 | ||
3a1296a3 | 3360 | if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs))) |
b2504a5d ED |
3361 | skb_warn_bad_offload(skb); |
3362 | ||
3363 | return segs; | |
05e8ef4a | 3364 | } |
12b0004d | 3365 | EXPORT_SYMBOL(__skb_gso_segment); |
f6a78bfc | 3366 | |
fb286bb2 HX |
3367 | /* Take action when hardware reception checksum errors are detected. */ |
3368 | #ifdef CONFIG_BUG | |
127d7355 TL |
3369 | static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
3370 | { | |
5b92be64 | 3371 | netdev_err(dev, "hw csum failure\n"); |
127d7355 TL |
3372 | skb_dump(KERN_ERR, skb, true); |
3373 | dump_stack(); | |
3374 | } | |
3375 | ||
7fe50ac8 | 3376 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
fb286bb2 | 3377 | { |
127d7355 | 3378 | DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb); |
fb286bb2 HX |
3379 | } |
3380 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
3381 | #endif | |
3382 | ||
ab74cfeb | 3383 | /* XXX: check that highmem exists at all on the given machine. */ |
c1e756bf | 3384 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 3385 | { |
3d3a8533 | 3386 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 3387 | int i; |
f4563a75 | 3388 | |
5acbbd42 | 3389 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
3390 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
3391 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
f4563a75 | 3392 | |
ea2ab693 | 3393 | if (PageHighMem(skb_frag_page(frag))) |
5acbbd42 | 3394 | return 1; |
ea2ab693 | 3395 | } |
5acbbd42 | 3396 | } |
3d3a8533 | 3397 | #endif |
1da177e4 LT |
3398 | return 0; |
3399 | } | |
1da177e4 | 3400 | |
3b392ddb SH |
3401 | /* If MPLS offload request, verify we are testing hardware MPLS features |
3402 | * instead of standard features for the netdev. | |
3403 | */ | |
d0edc7bf | 3404 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
3b392ddb SH |
3405 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
3406 | netdev_features_t features, | |
3407 | __be16 type) | |
3408 | { | |
25cd9ba0 | 3409 | if (eth_p_mpls(type)) |
3b392ddb SH |
3410 | features &= skb->dev->mpls_features; |
3411 | ||
3412 | return features; | |
3413 | } | |
3414 | #else | |
3415 | static netdev_features_t net_mpls_features(struct sk_buff *skb, | |
3416 | netdev_features_t features, | |
3417 | __be16 type) | |
3418 | { | |
3419 | return features; | |
3420 | } | |
3421 | #endif | |
3422 | ||
c8f44aff | 3423 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
c1e756bf | 3424 | netdev_features_t features) |
f01a5236 | 3425 | { |
3b392ddb SH |
3426 | __be16 type; |
3427 | ||
9fc95f50 | 3428 | type = skb_network_protocol(skb, NULL); |
3b392ddb | 3429 | features = net_mpls_features(skb, features, type); |
53d6471c | 3430 | |
c0d680e5 | 3431 | if (skb->ip_summed != CHECKSUM_NONE && |
3b392ddb | 3432 | !can_checksum_protocol(features, type)) { |
996e8021 | 3433 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
f01a5236 | 3434 | } |
7be2c82c ED |
3435 | if (illegal_highdma(skb->dev, skb)) |
3436 | features &= ~NETIF_F_SG; | |
f01a5236 JG |
3437 | |
3438 | return features; | |
3439 | } | |
3440 | ||
e38f3025 TM |
3441 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
3442 | struct net_device *dev, | |
3443 | netdev_features_t features) | |
3444 | { | |
3445 | return features; | |
3446 | } | |
3447 | EXPORT_SYMBOL(passthru_features_check); | |
3448 | ||
7ce23672 | 3449 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
8cb65d00 TM |
3450 | struct net_device *dev, |
3451 | netdev_features_t features) | |
3452 | { | |
3453 | return vlan_features_check(skb, features); | |
3454 | } | |
3455 | ||
cbc53e08 AD |
3456 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
3457 | struct net_device *dev, | |
3458 | netdev_features_t features) | |
3459 | { | |
3460 | u16 gso_segs = skb_shinfo(skb)->gso_segs; | |
3461 | ||
6d872df3 | 3462 | if (gso_segs > READ_ONCE(dev->gso_max_segs)) |
cbc53e08 AD |
3463 | return features & ~NETIF_F_GSO_MASK; |
3464 | ||
1d155dfd HK |
3465 | if (!skb_shinfo(skb)->gso_type) { |
3466 | skb_warn_bad_offload(skb); | |
3467 | return features & ~NETIF_F_GSO_MASK; | |
3468 | } | |
3469 | ||
802ab55a AD |
3470 | /* Support for GSO partial features requires software |
3471 | * intervention before we can actually process the packets | |
3472 | * so we need to strip support for any partial features now | |
3473 | * and we can pull them back in after we have partially | |
3474 | * segmented the frame. | |
3475 | */ | |
3476 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) | |
3477 | features &= ~dev->gso_partial_features; | |
3478 | ||
3479 | /* Make sure to clear the IPv4 ID mangling feature if the | |
3480 | * IPv4 header has the potential to be fragmented. | |
cbc53e08 AD |
3481 | */ |
3482 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
3483 | struct iphdr *iph = skb->encapsulation ? | |
3484 | inner_ip_hdr(skb) : ip_hdr(skb); | |
3485 | ||
3486 | if (!(iph->frag_off & htons(IP_DF))) | |
3487 | features &= ~NETIF_F_TSO_MANGLEID; | |
3488 | } | |
3489 | ||
3490 | return features; | |
3491 | } | |
3492 | ||
c1e756bf | 3493 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 | 3494 | { |
5f35227e | 3495 | struct net_device *dev = skb->dev; |
fcbeb976 | 3496 | netdev_features_t features = dev->features; |
58e998c6 | 3497 | |
cbc53e08 AD |
3498 | if (skb_is_gso(skb)) |
3499 | features = gso_features_check(skb, dev, features); | |
30b678d8 | 3500 | |
5f35227e JG |
3501 | /* If encapsulation offload request, verify we are testing |
3502 | * hardware encapsulation features instead of standard | |
3503 | * features for the netdev | |
3504 | */ | |
3505 | if (skb->encapsulation) | |
3506 | features &= dev->hw_enc_features; | |
3507 | ||
f5a7fb88 TM |
3508 | if (skb_vlan_tagged(skb)) |
3509 | features = netdev_intersect_features(features, | |
3510 | dev->vlan_features | | |
3511 | NETIF_F_HW_VLAN_CTAG_TX | | |
3512 | NETIF_F_HW_VLAN_STAG_TX); | |
f01a5236 | 3513 | |
5f35227e JG |
3514 | if (dev->netdev_ops->ndo_features_check) |
3515 | features &= dev->netdev_ops->ndo_features_check(skb, dev, | |
3516 | features); | |
8cb65d00 TM |
3517 | else |
3518 | features &= dflt_features_check(skb, dev, features); | |
5f35227e | 3519 | |
c1e756bf | 3520 | return harmonize_features(skb, features); |
58e998c6 | 3521 | } |
c1e756bf | 3522 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 3523 | |
2ea25513 | 3524 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
95f6b3dd | 3525 | struct netdev_queue *txq, bool more) |
f6a78bfc | 3526 | { |
2ea25513 DM |
3527 | unsigned int len; |
3528 | int rc; | |
00829823 | 3529 | |
9f9a742d | 3530 | if (dev_nit_active(dev)) |
2ea25513 | 3531 | dev_queue_xmit_nit(skb, dev); |
fc741216 | 3532 | |
2ea25513 | 3533 | len = skb->len; |
3744741a | 3534 | PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies); |
2ea25513 | 3535 | trace_net_dev_start_xmit(skb, dev); |
95f6b3dd | 3536 | rc = netdev_start_xmit(skb, dev, txq, more); |
2ea25513 | 3537 | trace_net_dev_xmit(skb, rc, dev, len); |
adf30907 | 3538 | |
2ea25513 DM |
3539 | return rc; |
3540 | } | |
7b9c6090 | 3541 | |
8dcda22a DM |
3542 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
3543 | struct netdev_queue *txq, int *ret) | |
7f2e870f DM |
3544 | { |
3545 | struct sk_buff *skb = first; | |
3546 | int rc = NETDEV_TX_OK; | |
7b9c6090 | 3547 | |
7f2e870f DM |
3548 | while (skb) { |
3549 | struct sk_buff *next = skb->next; | |
fc70fb64 | 3550 | |
a8305bff | 3551 | skb_mark_not_on_list(skb); |
95f6b3dd | 3552 | rc = xmit_one(skb, dev, txq, next != NULL); |
7f2e870f DM |
3553 | if (unlikely(!dev_xmit_complete(rc))) { |
3554 | skb->next = next; | |
3555 | goto out; | |
3556 | } | |
6afff0ca | 3557 | |
7f2e870f | 3558 | skb = next; |
fe60faa5 | 3559 | if (netif_tx_queue_stopped(txq) && skb) { |
7f2e870f DM |
3560 | rc = NETDEV_TX_BUSY; |
3561 | break; | |
9ccb8975 | 3562 | } |
7f2e870f | 3563 | } |
9ccb8975 | 3564 | |
7f2e870f DM |
3565 | out: |
3566 | *ret = rc; | |
3567 | return skb; | |
3568 | } | |
b40863c6 | 3569 | |
1ff0dc94 ED |
3570 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
3571 | netdev_features_t features) | |
f6a78bfc | 3572 | { |
df8a39de | 3573 | if (skb_vlan_tag_present(skb) && |
5968250c JP |
3574 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
3575 | skb = __vlan_hwaccel_push_inside(skb); | |
eae3f88e DM |
3576 | return skb; |
3577 | } | |
f6a78bfc | 3578 | |
43c26a1a DC |
3579 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
3580 | const netdev_features_t features) | |
3581 | { | |
fa821170 | 3582 | if (unlikely(skb_csum_is_sctp(skb))) |
43c26a1a DC |
3583 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : |
3584 | skb_crc32c_csum_help(skb); | |
3585 | ||
62fafcd6 XL |
3586 | if (features & NETIF_F_HW_CSUM) |
3587 | return 0; | |
3588 | ||
3589 | if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { | |
3590 | switch (skb->csum_offset) { | |
3591 | case offsetof(struct tcphdr, check): | |
3592 | case offsetof(struct udphdr, check): | |
3593 | return 0; | |
3594 | } | |
3595 | } | |
3596 | ||
3597 | return skb_checksum_help(skb); | |
43c26a1a DC |
3598 | } |
3599 | EXPORT_SYMBOL(skb_csum_hwoffload_help); | |
3600 | ||
f53c7239 | 3601 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
eae3f88e DM |
3602 | { |
3603 | netdev_features_t features; | |
f6a78bfc | 3604 | |
eae3f88e DM |
3605 | features = netif_skb_features(skb); |
3606 | skb = validate_xmit_vlan(skb, features); | |
3607 | if (unlikely(!skb)) | |
3608 | goto out_null; | |
7b9c6090 | 3609 | |
ebf4e808 IL |
3610 | skb = sk_validate_xmit_skb(skb, dev); |
3611 | if (unlikely(!skb)) | |
3612 | goto out_null; | |
3613 | ||
8b86a61d | 3614 | if (netif_needs_gso(skb, features)) { |
ce93718f DM |
3615 | struct sk_buff *segs; |
3616 | ||
3617 | segs = skb_gso_segment(skb, features); | |
cecda693 | 3618 | if (IS_ERR(segs)) { |
af6dabc9 | 3619 | goto out_kfree_skb; |
cecda693 JW |
3620 | } else if (segs) { |
3621 | consume_skb(skb); | |
3622 | skb = segs; | |
f6a78bfc | 3623 | } |
eae3f88e DM |
3624 | } else { |
3625 | if (skb_needs_linearize(skb, features) && | |
3626 | __skb_linearize(skb)) | |
3627 | goto out_kfree_skb; | |
4ec93edb | 3628 | |
eae3f88e DM |
3629 | /* If packet is not checksummed and device does not |
3630 | * support checksumming for this protocol, complete | |
3631 | * checksumming here. | |
3632 | */ | |
3633 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
3634 | if (skb->encapsulation) | |
3635 | skb_set_inner_transport_header(skb, | |
3636 | skb_checksum_start_offset(skb)); | |
3637 | else | |
3638 | skb_set_transport_header(skb, | |
3639 | skb_checksum_start_offset(skb)); | |
43c26a1a | 3640 | if (skb_csum_hwoffload_help(skb, features)) |
eae3f88e | 3641 | goto out_kfree_skb; |
7b9c6090 | 3642 | } |
0c772159 | 3643 | } |
7b9c6090 | 3644 | |
f53c7239 | 3645 | skb = validate_xmit_xfrm(skb, features, again); |
3dca3f38 | 3646 | |
eae3f88e | 3647 | return skb; |
fc70fb64 | 3648 | |
f6a78bfc HX |
3649 | out_kfree_skb: |
3650 | kfree_skb(skb); | |
eae3f88e | 3651 | out_null: |
625788b5 | 3652 | dev_core_stats_tx_dropped_inc(dev); |
eae3f88e DM |
3653 | return NULL; |
3654 | } | |
6afff0ca | 3655 | |
f53c7239 | 3656 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
55a93b3e ED |
3657 | { |
3658 | struct sk_buff *next, *head = NULL, *tail; | |
3659 | ||
bec3cfdc | 3660 | for (; skb != NULL; skb = next) { |
55a93b3e | 3661 | next = skb->next; |
a8305bff | 3662 | skb_mark_not_on_list(skb); |
bec3cfdc ED |
3663 | |
3664 | /* in case skb wont be segmented, point to itself */ | |
3665 | skb->prev = skb; | |
3666 | ||
f53c7239 | 3667 | skb = validate_xmit_skb(skb, dev, again); |
bec3cfdc ED |
3668 | if (!skb) |
3669 | continue; | |
55a93b3e | 3670 | |
bec3cfdc ED |
3671 | if (!head) |
3672 | head = skb; | |
3673 | else | |
3674 | tail->next = skb; | |
3675 | /* If skb was segmented, skb->prev points to | |
3676 | * the last segment. If not, it still contains skb. | |
3677 | */ | |
3678 | tail = skb->prev; | |
55a93b3e ED |
3679 | } |
3680 | return head; | |
f6a78bfc | 3681 | } |
104ba78c | 3682 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
f6a78bfc | 3683 | |
1def9238 ED |
3684 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
3685 | { | |
3686 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
3687 | ||
3688 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
3689 | ||
3690 | /* To get more precise estimation of bytes sent on wire, | |
3691 | * we add to pkt_len the headers size of all segments | |
3692 | */ | |
a0dce875 | 3693 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
757b8b1d | 3694 | unsigned int hdr_len; |
15e5a030 | 3695 | u16 gso_segs = shinfo->gso_segs; |
1def9238 | 3696 | |
757b8b1d ED |
3697 | /* mac layer + network layer */ |
3698 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); | |
3699 | ||
3700 | /* + transport layer */ | |
7c68d1a6 ED |
3701 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
3702 | const struct tcphdr *th; | |
3703 | struct tcphdr _tcphdr; | |
3704 | ||
3705 | th = skb_header_pointer(skb, skb_transport_offset(skb), | |
3706 | sizeof(_tcphdr), &_tcphdr); | |
3707 | if (likely(th)) | |
3708 | hdr_len += __tcp_hdrlen(th); | |
3709 | } else { | |
3710 | struct udphdr _udphdr; | |
3711 | ||
3712 | if (skb_header_pointer(skb, skb_transport_offset(skb), | |
3713 | sizeof(_udphdr), &_udphdr)) | |
3714 | hdr_len += sizeof(struct udphdr); | |
3715 | } | |
15e5a030 JW |
3716 | |
3717 | if (shinfo->gso_type & SKB_GSO_DODGY) | |
3718 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, | |
3719 | shinfo->gso_size); | |
3720 | ||
3721 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; | |
1def9238 ED |
3722 | } |
3723 | } | |
3724 | ||
70713ddd QX |
3725 | static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q, |
3726 | struct sk_buff **to_free, | |
3727 | struct netdev_queue *txq) | |
3728 | { | |
3729 | int rc; | |
3730 | ||
3731 | rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK; | |
3732 | if (rc == NET_XMIT_SUCCESS) | |
3733 | trace_qdisc_enqueue(q, txq, skb); | |
3734 | return rc; | |
3735 | } | |
3736 | ||
bbd8a0d3 KK |
3737 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
3738 | struct net_device *dev, | |
3739 | struct netdev_queue *txq) | |
3740 | { | |
3741 | spinlock_t *root_lock = qdisc_lock(q); | |
520ac30f | 3742 | struct sk_buff *to_free = NULL; |
a2da570d | 3743 | bool contended; |
bbd8a0d3 KK |
3744 | int rc; |
3745 | ||
a2da570d | 3746 | qdisc_calculate_pkt_len(skb, q); |
6b3ba914 JF |
3747 | |
3748 | if (q->flags & TCQ_F_NOLOCK) { | |
c4fef01b YL |
3749 | if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) && |
3750 | qdisc_run_begin(q)) { | |
3751 | /* Retest nolock_qdisc_is_empty() within the protection | |
3752 | * of q->seqlock to protect from racing with requeuing. | |
3753 | */ | |
3754 | if (unlikely(!nolock_qdisc_is_empty(q))) { | |
70713ddd | 3755 | rc = dev_qdisc_enqueue(skb, q, &to_free, txq); |
c4fef01b YL |
3756 | __qdisc_run(q); |
3757 | qdisc_run_end(q); | |
3758 | ||
3759 | goto no_lock_out; | |
3760 | } | |
3761 | ||
3762 | qdisc_bstats_cpu_update(q, skb); | |
3763 | if (sch_direct_xmit(skb, q, dev, txq, NULL, true) && | |
3764 | !nolock_qdisc_is_empty(q)) | |
3765 | __qdisc_run(q); | |
3766 | ||
3767 | qdisc_run_end(q); | |
3768 | return NET_XMIT_SUCCESS; | |
3769 | } | |
3770 | ||
70713ddd | 3771 | rc = dev_qdisc_enqueue(skb, q, &to_free, txq); |
c4fef01b | 3772 | qdisc_run(q); |
6b3ba914 | 3773 | |
c4fef01b | 3774 | no_lock_out: |
6b3ba914 | 3775 | if (unlikely(to_free)) |
7faef054 MD |
3776 | kfree_skb_list_reason(to_free, |
3777 | SKB_DROP_REASON_QDISC_DROP); | |
6b3ba914 JF |
3778 | return rc; |
3779 | } | |
3780 | ||
79640a4c ED |
3781 | /* |
3782 | * Heuristic to force contended enqueues to serialize on a | |
3783 | * separate lock before trying to get qdisc main lock. | |
f9eb8aea | 3784 | * This permits qdisc->running owner to get the lock more |
9bf2b8c2 | 3785 | * often and dequeue packets faster. |
64445dda SAS |
3786 | * On PREEMPT_RT it is possible to preempt the qdisc owner during xmit |
3787 | * and then other tasks will only enqueue packets. The packets will be | |
3788 | * sent after the qdisc owner is scheduled again. To prevent this | |
3789 | * scenario the task always serialize on the lock. | |
79640a4c | 3790 | */ |
a9aa5e33 | 3791 | contended = qdisc_is_running(q) || IS_ENABLED(CONFIG_PREEMPT_RT); |
79640a4c ED |
3792 | if (unlikely(contended)) |
3793 | spin_lock(&q->busylock); | |
3794 | ||
bbd8a0d3 KK |
3795 | spin_lock(root_lock); |
3796 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
520ac30f | 3797 | __qdisc_drop(skb, &to_free); |
bbd8a0d3 KK |
3798 | rc = NET_XMIT_DROP; |
3799 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 3800 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
3801 | /* |
3802 | * This is a work-conserving queue; there are no old skbs | |
3803 | * waiting to be sent out; and the qdisc is not running - | |
3804 | * xmit the skb directly. | |
3805 | */ | |
bfe0d029 | 3806 | |
bfe0d029 ED |
3807 | qdisc_bstats_update(q, skb); |
3808 | ||
55a93b3e | 3809 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
79640a4c ED |
3810 | if (unlikely(contended)) { |
3811 | spin_unlock(&q->busylock); | |
3812 | contended = false; | |
3813 | } | |
bbd8a0d3 | 3814 | __qdisc_run(q); |
6c148184 | 3815 | } |
bbd8a0d3 | 3816 | |
6c148184 | 3817 | qdisc_run_end(q); |
bbd8a0d3 KK |
3818 | rc = NET_XMIT_SUCCESS; |
3819 | } else { | |
70713ddd | 3820 | rc = dev_qdisc_enqueue(skb, q, &to_free, txq); |
79640a4c ED |
3821 | if (qdisc_run_begin(q)) { |
3822 | if (unlikely(contended)) { | |
3823 | spin_unlock(&q->busylock); | |
3824 | contended = false; | |
3825 | } | |
3826 | __qdisc_run(q); | |
6c148184 | 3827 | qdisc_run_end(q); |
79640a4c | 3828 | } |
bbd8a0d3 KK |
3829 | } |
3830 | spin_unlock(root_lock); | |
520ac30f | 3831 | if (unlikely(to_free)) |
7faef054 | 3832 | kfree_skb_list_reason(to_free, SKB_DROP_REASON_QDISC_DROP); |
79640a4c ED |
3833 | if (unlikely(contended)) |
3834 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
3835 | return rc; |
3836 | } | |
3837 | ||
86f8515f | 3838 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
5bc1421e NH |
3839 | static void skb_update_prio(struct sk_buff *skb) |
3840 | { | |
4dcb31d4 ED |
3841 | const struct netprio_map *map; |
3842 | const struct sock *sk; | |
3843 | unsigned int prioidx; | |
5bc1421e | 3844 | |
4dcb31d4 ED |
3845 | if (skb->priority) |
3846 | return; | |
3847 | map = rcu_dereference_bh(skb->dev->priomap); | |
3848 | if (!map) | |
3849 | return; | |
3850 | sk = skb_to_full_sk(skb); | |
3851 | if (!sk) | |
3852 | return; | |
91c68ce2 | 3853 | |
4dcb31d4 ED |
3854 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
3855 | ||
3856 | if (prioidx < map->priomap_len) | |
3857 | skb->priority = map->priomap[prioidx]; | |
5bc1421e NH |
3858 | } |
3859 | #else | |
3860 | #define skb_update_prio(skb) | |
3861 | #endif | |
3862 | ||
95603e22 MM |
3863 | /** |
3864 | * dev_loopback_xmit - loop back @skb | |
0c4b51f0 EB |
3865 | * @net: network namespace this loopback is happening in |
3866 | * @sk: sk needed to be a netfilter okfn | |
95603e22 MM |
3867 | * @skb: buffer to transmit |
3868 | */ | |
0c4b51f0 | 3869 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
95603e22 MM |
3870 | { |
3871 | skb_reset_mac_header(skb); | |
3872 | __skb_pull(skb, skb_network_offset(skb)); | |
3873 | skb->pkt_type = PACKET_LOOPBACK; | |
9122a70a CS |
3874 | if (skb->ip_summed == CHECKSUM_NONE) |
3875 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
95603e22 MM |
3876 | WARN_ON(!skb_dst(skb)); |
3877 | skb_dst_force(skb); | |
ad0a043f | 3878 | netif_rx(skb); |
95603e22 MM |
3879 | return 0; |
3880 | } | |
3881 | EXPORT_SYMBOL(dev_loopback_xmit); | |
3882 | ||
1f211a1b DB |
3883 | #ifdef CONFIG_NET_EGRESS |
3884 | static struct sk_buff * | |
3885 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
3886 | { | |
42df6e1d | 3887 | #ifdef CONFIG_NET_CLS_ACT |
46209401 | 3888 | struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress); |
1f211a1b DB |
3889 | struct tcf_result cl_res; |
3890 | ||
46209401 | 3891 | if (!miniq) |
1f211a1b DB |
3892 | return skb; |
3893 | ||
8dc07fdb | 3894 | /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ |
ec624fe7 PB |
3895 | tc_skb_cb(skb)->mru = 0; |
3896 | tc_skb_cb(skb)->post_ct = false; | |
46209401 | 3897 | mini_qdisc_bstats_cpu_update(miniq, skb); |
1f211a1b | 3898 | |
3aa26055 | 3899 | switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) { |
1f211a1b DB |
3900 | case TC_ACT_OK: |
3901 | case TC_ACT_RECLASSIFY: | |
3902 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
3903 | break; | |
3904 | case TC_ACT_SHOT: | |
46209401 | 3905 | mini_qdisc_qstats_cpu_drop(miniq); |
1f211a1b | 3906 | *ret = NET_XMIT_DROP; |
98b4d7a4 | 3907 | kfree_skb_reason(skb, SKB_DROP_REASON_TC_EGRESS); |
7e2c3aea | 3908 | return NULL; |
1f211a1b DB |
3909 | case TC_ACT_STOLEN: |
3910 | case TC_ACT_QUEUED: | |
e25ea21f | 3911 | case TC_ACT_TRAP: |
1f211a1b | 3912 | *ret = NET_XMIT_SUCCESS; |
7e2c3aea | 3913 | consume_skb(skb); |
1f211a1b DB |
3914 | return NULL; |
3915 | case TC_ACT_REDIRECT: | |
3916 | /* No need to push/pop skb's mac_header here on egress! */ | |
3917 | skb_do_redirect(skb); | |
3918 | *ret = NET_XMIT_SUCCESS; | |
3919 | return NULL; | |
3920 | default: | |
3921 | break; | |
3922 | } | |
42df6e1d | 3923 | #endif /* CONFIG_NET_CLS_ACT */ |
357b6cc5 | 3924 | |
1f211a1b DB |
3925 | return skb; |
3926 | } | |
3927 | #endif /* CONFIG_NET_EGRESS */ | |
3928 | ||
fc9bab24 AN |
3929 | #ifdef CONFIG_XPS |
3930 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, | |
3931 | struct xps_dev_maps *dev_maps, unsigned int tci) | |
3932 | { | |
255c04a8 | 3933 | int tc = netdev_get_prio_tc_map(dev, skb->priority); |
fc9bab24 AN |
3934 | struct xps_map *map; |
3935 | int queue_index = -1; | |
3936 | ||
5478fcd0 | 3937 | if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids) |
255c04a8 AT |
3938 | return queue_index; |
3939 | ||
3940 | tci *= dev_maps->num_tc; | |
3941 | tci += tc; | |
fc9bab24 AN |
3942 | |
3943 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
3944 | if (map) { | |
3945 | if (map->len == 1) | |
3946 | queue_index = map->queues[0]; | |
3947 | else | |
3948 | queue_index = map->queues[reciprocal_scale( | |
3949 | skb_get_hash(skb), map->len)]; | |
3950 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
3951 | queue_index = -1; | |
3952 | } | |
3953 | return queue_index; | |
3954 | } | |
3955 | #endif | |
3956 | ||
eadec877 AD |
3957 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
3958 | struct sk_buff *skb) | |
638b2a69 JP |
3959 | { |
3960 | #ifdef CONFIG_XPS | |
3961 | struct xps_dev_maps *dev_maps; | |
fc9bab24 | 3962 | struct sock *sk = skb->sk; |
638b2a69 JP |
3963 | int queue_index = -1; |
3964 | ||
04157469 AN |
3965 | if (!static_key_false(&xps_needed)) |
3966 | return -1; | |
3967 | ||
638b2a69 | 3968 | rcu_read_lock(); |
fc9bab24 AN |
3969 | if (!static_key_false(&xps_rxqs_needed)) |
3970 | goto get_cpus_map; | |
3971 | ||
044ab86d | 3972 | dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]); |
638b2a69 | 3973 | if (dev_maps) { |
fc9bab24 | 3974 | int tci = sk_rx_queue_get(sk); |
184c449f | 3975 | |
5478fcd0 | 3976 | if (tci >= 0) |
fc9bab24 AN |
3977 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, |
3978 | tci); | |
3979 | } | |
184c449f | 3980 | |
fc9bab24 AN |
3981 | get_cpus_map: |
3982 | if (queue_index < 0) { | |
044ab86d | 3983 | dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]); |
fc9bab24 AN |
3984 | if (dev_maps) { |
3985 | unsigned int tci = skb->sender_cpu - 1; | |
3986 | ||
3987 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3988 | tci); | |
638b2a69 JP |
3989 | } |
3990 | } | |
3991 | rcu_read_unlock(); | |
3992 | ||
3993 | return queue_index; | |
3994 | #else | |
3995 | return -1; | |
3996 | #endif | |
3997 | } | |
3998 | ||
a4ea8a3d | 3999 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 4000 | struct net_device *sb_dev) |
a4ea8a3d AD |
4001 | { |
4002 | return 0; | |
4003 | } | |
4004 | EXPORT_SYMBOL(dev_pick_tx_zero); | |
4005 | ||
4006 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, | |
a350ecce | 4007 | struct net_device *sb_dev) |
a4ea8a3d AD |
4008 | { |
4009 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; | |
4010 | } | |
4011 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); | |
4012 | ||
b71b5837 PA |
4013 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
4014 | struct net_device *sb_dev) | |
638b2a69 JP |
4015 | { |
4016 | struct sock *sk = skb->sk; | |
4017 | int queue_index = sk_tx_queue_get(sk); | |
4018 | ||
eadec877 AD |
4019 | sb_dev = sb_dev ? : dev; |
4020 | ||
638b2a69 JP |
4021 | if (queue_index < 0 || skb->ooo_okay || |
4022 | queue_index >= dev->real_num_tx_queues) { | |
eadec877 | 4023 | int new_index = get_xps_queue(dev, sb_dev, skb); |
f4563a75 | 4024 | |
638b2a69 | 4025 | if (new_index < 0) |
eadec877 | 4026 | new_index = skb_tx_hash(dev, sb_dev, skb); |
638b2a69 JP |
4027 | |
4028 | if (queue_index != new_index && sk && | |
004a5d01 | 4029 | sk_fullsock(sk) && |
638b2a69 JP |
4030 | rcu_access_pointer(sk->sk_dst_cache)) |
4031 | sk_tx_queue_set(sk, new_index); | |
4032 | ||
4033 | queue_index = new_index; | |
4034 | } | |
4035 | ||
4036 | return queue_index; | |
4037 | } | |
b71b5837 | 4038 | EXPORT_SYMBOL(netdev_pick_tx); |
638b2a69 | 4039 | |
4bd97d51 PA |
4040 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
4041 | struct sk_buff *skb, | |
4042 | struct net_device *sb_dev) | |
638b2a69 JP |
4043 | { |
4044 | int queue_index = 0; | |
4045 | ||
4046 | #ifdef CONFIG_XPS | |
52bd2d62 ED |
4047 | u32 sender_cpu = skb->sender_cpu - 1; |
4048 | ||
4049 | if (sender_cpu >= (u32)NR_CPUS) | |
638b2a69 JP |
4050 | skb->sender_cpu = raw_smp_processor_id() + 1; |
4051 | #endif | |
4052 | ||
4053 | if (dev->real_num_tx_queues != 1) { | |
4054 | const struct net_device_ops *ops = dev->netdev_ops; | |
f4563a75 | 4055 | |
638b2a69 | 4056 | if (ops->ndo_select_queue) |
a350ecce | 4057 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
638b2a69 | 4058 | else |
4bd97d51 | 4059 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
638b2a69 | 4060 | |
d584527c | 4061 | queue_index = netdev_cap_txqueue(dev, queue_index); |
638b2a69 JP |
4062 | } |
4063 | ||
4064 | skb_set_queue_mapping(skb, queue_index); | |
4065 | return netdev_get_tx_queue(dev, queue_index); | |
4066 | } | |
4067 | ||
d29f749e | 4068 | /** |
9d08dd3d | 4069 | * __dev_queue_xmit - transmit a buffer |
d29f749e | 4070 | * @skb: buffer to transmit |
eadec877 | 4071 | * @sb_dev: suboordinate device used for L2 forwarding offload |
d29f749e DJ |
4072 | * |
4073 | * Queue a buffer for transmission to a network device. The caller must | |
4074 | * have set the device and priority and built the buffer before calling | |
4075 | * this function. The function can be called from an interrupt. | |
4076 | * | |
4077 | * A negative errno code is returned on a failure. A success does not | |
4078 | * guarantee the frame will be transmitted as it may be dropped due | |
4079 | * to congestion or traffic shaping. | |
4080 | * | |
4081 | * ----------------------------------------------------------------------------------- | |
4082 | * I notice this method can also return errors from the queue disciplines, | |
4083 | * including NET_XMIT_DROP, which is a positive value. So, errors can also | |
4084 | * be positive. | |
4085 | * | |
4086 | * Regardless of the return value, the skb is consumed, so it is currently | |
4087 | * difficult to retry a send to this method. (You can bump the ref count | |
4088 | * before sending to hold a reference for retry if you are careful.) | |
4089 | * | |
4090 | * When calling this method, interrupts MUST be enabled. This is because | |
4091 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
4092 | * --BLG | |
4093 | */ | |
eadec877 | 4094 | static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
1da177e4 LT |
4095 | { |
4096 | struct net_device *dev = skb->dev; | |
dc2b4847 | 4097 | struct netdev_queue *txq; |
1da177e4 LT |
4098 | struct Qdisc *q; |
4099 | int rc = -ENOMEM; | |
f53c7239 | 4100 | bool again = false; |
1da177e4 | 4101 | |
6d1ccff6 ED |
4102 | skb_reset_mac_header(skb); |
4103 | ||
e7fd2885 | 4104 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
e7ed11ee | 4105 | __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED); |
e7fd2885 | 4106 | |
4ec93edb YH |
4107 | /* Disable soft irqs for various locks below. Also |
4108 | * stops preemption for RCU. | |
1da177e4 | 4109 | */ |
4ec93edb | 4110 | rcu_read_lock_bh(); |
1da177e4 | 4111 | |
5bc1421e NH |
4112 | skb_update_prio(skb); |
4113 | ||
1f211a1b DB |
4114 | qdisc_pkt_len_init(skb); |
4115 | #ifdef CONFIG_NET_CLS_ACT | |
8dc07fdb | 4116 | skb->tc_at_ingress = 0; |
42df6e1d LW |
4117 | #endif |
4118 | #ifdef CONFIG_NET_EGRESS | |
aabf6772 | 4119 | if (static_branch_unlikely(&egress_needed_key)) { |
42df6e1d LW |
4120 | if (nf_hook_egress_active()) { |
4121 | skb = nf_hook_egress(skb, &rc, dev); | |
4122 | if (!skb) | |
4123 | goto out; | |
4124 | } | |
4125 | nf_skip_egress(skb, true); | |
1f211a1b DB |
4126 | skb = sch_handle_egress(skb, &rc, dev); |
4127 | if (!skb) | |
4128 | goto out; | |
42df6e1d | 4129 | nf_skip_egress(skb, false); |
1f211a1b | 4130 | } |
1f211a1b | 4131 | #endif |
02875878 ED |
4132 | /* If device/qdisc don't need skb->dst, release it right now while |
4133 | * its hot in this cpu cache. | |
4134 | */ | |
4135 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
4136 | skb_dst_drop(skb); | |
4137 | else | |
4138 | skb_dst_force(skb); | |
4139 | ||
4bd97d51 | 4140 | txq = netdev_core_pick_tx(dev, skb, sb_dev); |
a898def2 | 4141 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 4142 | |
cf66ba58 | 4143 | trace_net_dev_queue(skb); |
1da177e4 | 4144 | if (q->enqueue) { |
bbd8a0d3 | 4145 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 4146 | goto out; |
1da177e4 LT |
4147 | } |
4148 | ||
4149 | /* The device has no queue. Common case for software devices: | |
eb13da1a | 4150 | * loopback, all the sorts of tunnels... |
1da177e4 | 4151 | |
eb13da1a | 4152 | * Really, it is unlikely that netif_tx_lock protection is necessary |
4153 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
4154 | * counters.) | |
4155 | * However, it is possible, that they rely on protection | |
4156 | * made by us here. | |
1da177e4 | 4157 | |
eb13da1a | 4158 | * Check this and shot the lock. It is not prone from deadlocks. |
4159 | *Either shot noqueue qdisc, it is even simpler 8) | |
1da177e4 LT |
4160 | */ |
4161 | if (dev->flags & IFF_UP) { | |
4162 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
4163 | ||
7a10d8c8 ED |
4164 | /* Other cpus might concurrently change txq->xmit_lock_owner |
4165 | * to -1 or to their cpu id, but not to our id. | |
4166 | */ | |
4167 | if (READ_ONCE(txq->xmit_lock_owner) != cpu) { | |
97cdcf37 | 4168 | if (dev_xmit_recursion()) |
745e20f1 ED |
4169 | goto recursion_alert; |
4170 | ||
f53c7239 | 4171 | skb = validate_xmit_skb(skb, dev, &again); |
1f59533f | 4172 | if (!skb) |
d21fd63e | 4173 | goto out; |
1f59533f | 4174 | |
3744741a | 4175 | PRANDOM_ADD_NOISE(skb, dev, txq, jiffies); |
c773e847 | 4176 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 4177 | |
73466498 | 4178 | if (!netif_xmit_stopped(txq)) { |
97cdcf37 | 4179 | dev_xmit_recursion_inc(); |
ce93718f | 4180 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
97cdcf37 | 4181 | dev_xmit_recursion_dec(); |
572a9d7b | 4182 | if (dev_xmit_complete(rc)) { |
c773e847 | 4183 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
4184 | goto out; |
4185 | } | |
4186 | } | |
c773e847 | 4187 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
4188 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
4189 | dev->name); | |
1da177e4 LT |
4190 | } else { |
4191 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
4192 | * unfortunately |
4193 | */ | |
4194 | recursion_alert: | |
e87cc472 JP |
4195 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
4196 | dev->name); | |
1da177e4 LT |
4197 | } |
4198 | } | |
4199 | ||
4200 | rc = -ENETDOWN; | |
d4828d85 | 4201 | rcu_read_unlock_bh(); |
1da177e4 | 4202 | |
625788b5 | 4203 | dev_core_stats_tx_dropped_inc(dev); |
1f59533f | 4204 | kfree_skb_list(skb); |
1da177e4 LT |
4205 | return rc; |
4206 | out: | |
d4828d85 | 4207 | rcu_read_unlock_bh(); |
1da177e4 LT |
4208 | return rc; |
4209 | } | |
f663dd9a | 4210 | |
2b4aa3ce | 4211 | int dev_queue_xmit(struct sk_buff *skb) |
f663dd9a JW |
4212 | { |
4213 | return __dev_queue_xmit(skb, NULL); | |
4214 | } | |
2b4aa3ce | 4215 | EXPORT_SYMBOL(dev_queue_xmit); |
1da177e4 | 4216 | |
eadec877 | 4217 | int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev) |
f663dd9a | 4218 | { |
eadec877 | 4219 | return __dev_queue_xmit(skb, sb_dev); |
f663dd9a JW |
4220 | } |
4221 | EXPORT_SYMBOL(dev_queue_xmit_accel); | |
4222 | ||
36ccdf85 | 4223 | int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
865b03f2 MK |
4224 | { |
4225 | struct net_device *dev = skb->dev; | |
4226 | struct sk_buff *orig_skb = skb; | |
4227 | struct netdev_queue *txq; | |
4228 | int ret = NETDEV_TX_BUSY; | |
4229 | bool again = false; | |
4230 | ||
4231 | if (unlikely(!netif_running(dev) || | |
4232 | !netif_carrier_ok(dev))) | |
4233 | goto drop; | |
4234 | ||
4235 | skb = validate_xmit_skb_list(skb, dev, &again); | |
4236 | if (skb != orig_skb) | |
4237 | goto drop; | |
4238 | ||
4239 | skb_set_queue_mapping(skb, queue_id); | |
4240 | txq = skb_get_tx_queue(dev, skb); | |
3744741a | 4241 | PRANDOM_ADD_NOISE(skb, dev, txq, jiffies); |
865b03f2 MK |
4242 | |
4243 | local_bh_disable(); | |
4244 | ||
0ad6f6e7 | 4245 | dev_xmit_recursion_inc(); |
865b03f2 MK |
4246 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
4247 | if (!netif_xmit_frozen_or_drv_stopped(txq)) | |
4248 | ret = netdev_start_xmit(skb, dev, txq, false); | |
4249 | HARD_TX_UNLOCK(dev, txq); | |
0ad6f6e7 | 4250 | dev_xmit_recursion_dec(); |
865b03f2 MK |
4251 | |
4252 | local_bh_enable(); | |
865b03f2 MK |
4253 | return ret; |
4254 | drop: | |
625788b5 | 4255 | dev_core_stats_tx_dropped_inc(dev); |
865b03f2 MK |
4256 | kfree_skb_list(skb); |
4257 | return NET_XMIT_DROP; | |
4258 | } | |
36ccdf85 | 4259 | EXPORT_SYMBOL(__dev_direct_xmit); |
1da177e4 | 4260 | |
eb13da1a | 4261 | /************************************************************************* |
4262 | * Receiver routines | |
4263 | *************************************************************************/ | |
1da177e4 | 4264 | |
6b2bedc3 | 4265 | int netdev_max_backlog __read_mostly = 1000; |
c9e6bc64 ED |
4266 | EXPORT_SYMBOL(netdev_max_backlog); |
4267 | ||
3b098e2d | 4268 | int netdev_tstamp_prequeue __read_mostly = 1; |
6b2bedc3 | 4269 | int netdev_budget __read_mostly = 300; |
a4837980 KK |
4270 | /* Must be at least 2 jiffes to guarantee 1 jiffy timeout */ |
4271 | unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ; | |
3d48b53f MT |
4272 | int weight_p __read_mostly = 64; /* old backlog weight */ |
4273 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ | |
4274 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ | |
4275 | int dev_rx_weight __read_mostly = 64; | |
4276 | int dev_tx_weight __read_mostly = 64; | |
1da177e4 | 4277 | |
eecfd7c4 ED |
4278 | /* Called with irq disabled */ |
4279 | static inline void ____napi_schedule(struct softnet_data *sd, | |
4280 | struct napi_struct *napi) | |
4281 | { | |
29863d41 WW |
4282 | struct task_struct *thread; |
4283 | ||
fbd9a2ce SAS |
4284 | lockdep_assert_irqs_disabled(); |
4285 | ||
29863d41 WW |
4286 | if (test_bit(NAPI_STATE_THREADED, &napi->state)) { |
4287 | /* Paired with smp_mb__before_atomic() in | |
5fdd2f0e WW |
4288 | * napi_enable()/dev_set_threaded(). |
4289 | * Use READ_ONCE() to guarantee a complete | |
4290 | * read on napi->thread. Only call | |
29863d41 WW |
4291 | * wake_up_process() when it's not NULL. |
4292 | */ | |
4293 | thread = READ_ONCE(napi->thread); | |
4294 | if (thread) { | |
cb038357 WW |
4295 | /* Avoid doing set_bit() if the thread is in |
4296 | * INTERRUPTIBLE state, cause napi_thread_wait() | |
4297 | * makes sure to proceed with napi polling | |
4298 | * if the thread is explicitly woken from here. | |
4299 | */ | |
2f064a59 | 4300 | if (READ_ONCE(thread->__state) != TASK_INTERRUPTIBLE) |
cb038357 | 4301 | set_bit(NAPI_STATE_SCHED_THREADED, &napi->state); |
29863d41 WW |
4302 | wake_up_process(thread); |
4303 | return; | |
4304 | } | |
4305 | } | |
4306 | ||
eecfd7c4 ED |
4307 | list_add_tail(&napi->poll_list, &sd->poll_list); |
4308 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4309 | } | |
4310 | ||
bfb564e7 KK |
4311 | #ifdef CONFIG_RPS |
4312 | ||
4313 | /* One global table that all flow-based protocols share. */ | |
6e3f7faf | 4314 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
bfb564e7 | 4315 | EXPORT_SYMBOL(rps_sock_flow_table); |
567e4b79 ED |
4316 | u32 rps_cpu_mask __read_mostly; |
4317 | EXPORT_SYMBOL(rps_cpu_mask); | |
bfb564e7 | 4318 | |
dc05360f | 4319 | struct static_key_false rps_needed __read_mostly; |
3df97ba8 | 4320 | EXPORT_SYMBOL(rps_needed); |
dc05360f | 4321 | struct static_key_false rfs_needed __read_mostly; |
13bfff25 | 4322 | EXPORT_SYMBOL(rfs_needed); |
adc9300e | 4323 | |
c445477d BH |
4324 | static struct rps_dev_flow * |
4325 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4326 | struct rps_dev_flow *rflow, u16 next_cpu) | |
4327 | { | |
a31196b0 | 4328 | if (next_cpu < nr_cpu_ids) { |
c445477d BH |
4329 | #ifdef CONFIG_RFS_ACCEL |
4330 | struct netdev_rx_queue *rxqueue; | |
4331 | struct rps_dev_flow_table *flow_table; | |
4332 | struct rps_dev_flow *old_rflow; | |
4333 | u32 flow_id; | |
4334 | u16 rxq_index; | |
4335 | int rc; | |
4336 | ||
4337 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
4338 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
4339 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
4340 | goto out; |
4341 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
4342 | if (rxq_index == skb_get_rx_queue(skb)) | |
4343 | goto out; | |
4344 | ||
4345 | rxqueue = dev->_rx + rxq_index; | |
4346 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4347 | if (!flow_table) | |
4348 | goto out; | |
61b905da | 4349 | flow_id = skb_get_hash(skb) & flow_table->mask; |
c445477d BH |
4350 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
4351 | rxq_index, flow_id); | |
4352 | if (rc < 0) | |
4353 | goto out; | |
4354 | old_rflow = rflow; | |
4355 | rflow = &flow_table->flows[flow_id]; | |
c445477d BH |
4356 | rflow->filter = rc; |
4357 | if (old_rflow->filter == rflow->filter) | |
4358 | old_rflow->filter = RPS_NO_FILTER; | |
4359 | out: | |
4360 | #endif | |
4361 | rflow->last_qtail = | |
09994d1b | 4362 | per_cpu(softnet_data, next_cpu).input_queue_head; |
c445477d BH |
4363 | } |
4364 | ||
09994d1b | 4365 | rflow->cpu = next_cpu; |
c445477d BH |
4366 | return rflow; |
4367 | } | |
4368 | ||
bfb564e7 KK |
4369 | /* |
4370 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
4371 | * CPU from the RPS map of the receiving queue for a given skb. | |
4372 | * rcu_read_lock must be held on entry. | |
4373 | */ | |
4374 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4375 | struct rps_dev_flow **rflowp) | |
4376 | { | |
567e4b79 ED |
4377 | const struct rps_sock_flow_table *sock_flow_table; |
4378 | struct netdev_rx_queue *rxqueue = dev->_rx; | |
bfb564e7 | 4379 | struct rps_dev_flow_table *flow_table; |
567e4b79 | 4380 | struct rps_map *map; |
bfb564e7 | 4381 | int cpu = -1; |
567e4b79 | 4382 | u32 tcpu; |
61b905da | 4383 | u32 hash; |
bfb564e7 KK |
4384 | |
4385 | if (skb_rx_queue_recorded(skb)) { | |
4386 | u16 index = skb_get_rx_queue(skb); | |
567e4b79 | 4387 | |
62fe0b40 BH |
4388 | if (unlikely(index >= dev->real_num_rx_queues)) { |
4389 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4390 | "%s received packet on queue %u, but number " | |
4391 | "of RX queues is %u\n", | |
4392 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
4393 | goto done; |
4394 | } | |
567e4b79 ED |
4395 | rxqueue += index; |
4396 | } | |
bfb564e7 | 4397 | |
567e4b79 ED |
4398 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
4399 | ||
4400 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
6e3f7faf | 4401 | map = rcu_dereference(rxqueue->rps_map); |
567e4b79 | 4402 | if (!flow_table && !map) |
bfb564e7 KK |
4403 | goto done; |
4404 | ||
2d47b459 | 4405 | skb_reset_network_header(skb); |
61b905da TH |
4406 | hash = skb_get_hash(skb); |
4407 | if (!hash) | |
bfb564e7 KK |
4408 | goto done; |
4409 | ||
fec5e652 TH |
4410 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
4411 | if (flow_table && sock_flow_table) { | |
fec5e652 | 4412 | struct rps_dev_flow *rflow; |
567e4b79 ED |
4413 | u32 next_cpu; |
4414 | u32 ident; | |
4415 | ||
4416 | /* First check into global flow table if there is a match */ | |
4417 | ident = sock_flow_table->ents[hash & sock_flow_table->mask]; | |
4418 | if ((ident ^ hash) & ~rps_cpu_mask) | |
4419 | goto try_rps; | |
fec5e652 | 4420 | |
567e4b79 ED |
4421 | next_cpu = ident & rps_cpu_mask; |
4422 | ||
4423 | /* OK, now we know there is a match, | |
4424 | * we can look at the local (per receive queue) flow table | |
4425 | */ | |
61b905da | 4426 | rflow = &flow_table->flows[hash & flow_table->mask]; |
fec5e652 TH |
4427 | tcpu = rflow->cpu; |
4428 | ||
fec5e652 TH |
4429 | /* |
4430 | * If the desired CPU (where last recvmsg was done) is | |
4431 | * different from current CPU (one in the rx-queue flow | |
4432 | * table entry), switch if one of the following holds: | |
a31196b0 | 4433 | * - Current CPU is unset (>= nr_cpu_ids). |
fec5e652 TH |
4434 | * - Current CPU is offline. |
4435 | * - The current CPU's queue tail has advanced beyond the | |
4436 | * last packet that was enqueued using this table entry. | |
4437 | * This guarantees that all previous packets for the flow | |
4438 | * have been dequeued, thus preserving in order delivery. | |
4439 | */ | |
4440 | if (unlikely(tcpu != next_cpu) && | |
a31196b0 | 4441 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
fec5e652 | 4442 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
baefa31d TH |
4443 | rflow->last_qtail)) >= 0)) { |
4444 | tcpu = next_cpu; | |
c445477d | 4445 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 4446 | } |
c445477d | 4447 | |
a31196b0 | 4448 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
fec5e652 TH |
4449 | *rflowp = rflow; |
4450 | cpu = tcpu; | |
4451 | goto done; | |
4452 | } | |
4453 | } | |
4454 | ||
567e4b79 ED |
4455 | try_rps: |
4456 | ||
0a9627f2 | 4457 | if (map) { |
8fc54f68 | 4458 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
0a9627f2 TH |
4459 | if (cpu_online(tcpu)) { |
4460 | cpu = tcpu; | |
4461 | goto done; | |
4462 | } | |
4463 | } | |
4464 | ||
4465 | done: | |
0a9627f2 TH |
4466 | return cpu; |
4467 | } | |
4468 | ||
c445477d BH |
4469 | #ifdef CONFIG_RFS_ACCEL |
4470 | ||
4471 | /** | |
4472 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
4473 | * @dev: Device on which the filter was set | |
4474 | * @rxq_index: RX queue index | |
4475 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
4476 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
4477 | * | |
4478 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
4479 | * this function for each installed filter and remove the filters for | |
4480 | * which it returns %true. | |
4481 | */ | |
4482 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
4483 | u32 flow_id, u16 filter_id) | |
4484 | { | |
4485 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
4486 | struct rps_dev_flow_table *flow_table; | |
4487 | struct rps_dev_flow *rflow; | |
4488 | bool expire = true; | |
a31196b0 | 4489 | unsigned int cpu; |
c445477d BH |
4490 | |
4491 | rcu_read_lock(); | |
4492 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4493 | if (flow_table && flow_id <= flow_table->mask) { | |
4494 | rflow = &flow_table->flows[flow_id]; | |
6aa7de05 | 4495 | cpu = READ_ONCE(rflow->cpu); |
a31196b0 | 4496 | if (rflow->filter == filter_id && cpu < nr_cpu_ids && |
c445477d BH |
4497 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
4498 | rflow->last_qtail) < | |
4499 | (int)(10 * flow_table->mask))) | |
4500 | expire = false; | |
4501 | } | |
4502 | rcu_read_unlock(); | |
4503 | return expire; | |
4504 | } | |
4505 | EXPORT_SYMBOL(rps_may_expire_flow); | |
4506 | ||
4507 | #endif /* CONFIG_RFS_ACCEL */ | |
4508 | ||
0a9627f2 | 4509 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 4510 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 4511 | { |
e36fa2f7 ED |
4512 | struct softnet_data *sd = data; |
4513 | ||
eecfd7c4 | 4514 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 4515 | sd->received_rps++; |
0a9627f2 | 4516 | } |
e36fa2f7 | 4517 | |
fec5e652 | 4518 | #endif /* CONFIG_RPS */ |
0a9627f2 | 4519 | |
e36fa2f7 ED |
4520 | /* |
4521 | * Check if this softnet_data structure is another cpu one | |
4522 | * If yes, queue it to our IPI list and return 1 | |
4523 | * If no, return 0 | |
4524 | */ | |
e722db8d | 4525 | static int napi_schedule_rps(struct softnet_data *sd) |
e36fa2f7 | 4526 | { |
903ceff7 | 4527 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
e36fa2f7 | 4528 | |
e722db8d | 4529 | #ifdef CONFIG_RPS |
e36fa2f7 ED |
4530 | if (sd != mysd) { |
4531 | sd->rps_ipi_next = mysd->rps_ipi_list; | |
4532 | mysd->rps_ipi_list = sd; | |
4533 | ||
4534 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4535 | return 1; | |
4536 | } | |
4537 | #endif /* CONFIG_RPS */ | |
e722db8d | 4538 | __napi_schedule_irqoff(&mysd->backlog); |
e36fa2f7 ED |
4539 | return 0; |
4540 | } | |
4541 | ||
99bbc707 WB |
4542 | #ifdef CONFIG_NET_FLOW_LIMIT |
4543 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); | |
4544 | #endif | |
4545 | ||
4546 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) | |
4547 | { | |
4548 | #ifdef CONFIG_NET_FLOW_LIMIT | |
4549 | struct sd_flow_limit *fl; | |
4550 | struct softnet_data *sd; | |
4551 | unsigned int old_flow, new_flow; | |
4552 | ||
4553 | if (qlen < (netdev_max_backlog >> 1)) | |
4554 | return false; | |
4555 | ||
903ceff7 | 4556 | sd = this_cpu_ptr(&softnet_data); |
99bbc707 WB |
4557 | |
4558 | rcu_read_lock(); | |
4559 | fl = rcu_dereference(sd->flow_limit); | |
4560 | if (fl) { | |
3958afa1 | 4561 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
99bbc707 WB |
4562 | old_flow = fl->history[fl->history_head]; |
4563 | fl->history[fl->history_head] = new_flow; | |
4564 | ||
4565 | fl->history_head++; | |
4566 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; | |
4567 | ||
4568 | if (likely(fl->buckets[old_flow])) | |
4569 | fl->buckets[old_flow]--; | |
4570 | ||
4571 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { | |
4572 | fl->count++; | |
4573 | rcu_read_unlock(); | |
4574 | return true; | |
4575 | } | |
4576 | } | |
4577 | rcu_read_unlock(); | |
4578 | #endif | |
4579 | return false; | |
4580 | } | |
4581 | ||
0a9627f2 TH |
4582 | /* |
4583 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
4584 | * queue (may be a remote CPU queue). | |
4585 | */ | |
fec5e652 TH |
4586 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
4587 | unsigned int *qtail) | |
0a9627f2 | 4588 | { |
44f0bd40 | 4589 | enum skb_drop_reason reason; |
e36fa2f7 | 4590 | struct softnet_data *sd; |
0a9627f2 | 4591 | unsigned long flags; |
99bbc707 | 4592 | unsigned int qlen; |
0a9627f2 | 4593 | |
44f0bd40 | 4594 | reason = SKB_DROP_REASON_NOT_SPECIFIED; |
e36fa2f7 | 4595 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 | 4596 | |
e722db8d | 4597 | rps_lock_irqsave(sd, &flags); |
e9e4dd32 JA |
4598 | if (!netif_running(skb->dev)) |
4599 | goto drop; | |
99bbc707 WB |
4600 | qlen = skb_queue_len(&sd->input_pkt_queue); |
4601 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { | |
e008f3f0 | 4602 | if (qlen) { |
0a9627f2 | 4603 | enqueue: |
e36fa2f7 | 4604 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
76cc8b13 | 4605 | input_queue_tail_incr_save(sd, qtail); |
e722db8d | 4606 | rps_unlock_irq_restore(sd, &flags); |
0a9627f2 TH |
4607 | return NET_RX_SUCCESS; |
4608 | } | |
4609 | ||
ebda37c2 ED |
4610 | /* Schedule NAPI for backlog device |
4611 | * We can use non atomic operation since we own the queue lock | |
4612 | */ | |
e722db8d SAS |
4613 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) |
4614 | napi_schedule_rps(sd); | |
0a9627f2 TH |
4615 | goto enqueue; |
4616 | } | |
44f0bd40 | 4617 | reason = SKB_DROP_REASON_CPU_BACKLOG; |
0a9627f2 | 4618 | |
e9e4dd32 | 4619 | drop: |
dee42870 | 4620 | sd->dropped++; |
e722db8d | 4621 | rps_unlock_irq_restore(sd, &flags); |
0a9627f2 | 4622 | |
625788b5 | 4623 | dev_core_stats_rx_dropped_inc(skb->dev); |
44f0bd40 | 4624 | kfree_skb_reason(skb, reason); |
0a9627f2 TH |
4625 | return NET_RX_DROP; |
4626 | } | |
1da177e4 | 4627 | |
e817f856 JDB |
4628 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
4629 | { | |
4630 | struct net_device *dev = skb->dev; | |
4631 | struct netdev_rx_queue *rxqueue; | |
4632 | ||
4633 | rxqueue = dev->_rx; | |
4634 | ||
4635 | if (skb_rx_queue_recorded(skb)) { | |
4636 | u16 index = skb_get_rx_queue(skb); | |
4637 | ||
4638 | if (unlikely(index >= dev->real_num_rx_queues)) { | |
4639 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4640 | "%s received packet on queue %u, but number " | |
4641 | "of RX queues is %u\n", | |
4642 | dev->name, index, dev->real_num_rx_queues); | |
4643 | ||
4644 | return rxqueue; /* Return first rxqueue */ | |
4645 | } | |
4646 | rxqueue += index; | |
4647 | } | |
4648 | return rxqueue; | |
4649 | } | |
4650 | ||
fe21cb91 KKD |
4651 | u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp, |
4652 | struct bpf_prog *xdp_prog) | |
d4455169 | 4653 | { |
be9df4af | 4654 | void *orig_data, *orig_data_end, *hard_start; |
e817f856 | 4655 | struct netdev_rx_queue *rxqueue; |
22b60343 | 4656 | bool orig_bcast, orig_host; |
43b5169d | 4657 | u32 mac_len, frame_sz; |
29724956 JDB |
4658 | __be16 orig_eth_type; |
4659 | struct ethhdr *eth; | |
fe21cb91 | 4660 | u32 metalen, act; |
be9df4af | 4661 | int off; |
d4455169 | 4662 | |
d4455169 JF |
4663 | /* The XDP program wants to see the packet starting at the MAC |
4664 | * header. | |
4665 | */ | |
4666 | mac_len = skb->data - skb_mac_header(skb); | |
be9df4af | 4667 | hard_start = skb->data - skb_headroom(skb); |
a075767b JDB |
4668 | |
4669 | /* SKB "head" area always have tailroom for skb_shared_info */ | |
be9df4af | 4670 | frame_sz = (void *)skb_end_pointer(skb) - hard_start; |
43b5169d | 4671 | frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
a075767b | 4672 | |
be9df4af LB |
4673 | rxqueue = netif_get_rxqueue(skb); |
4674 | xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq); | |
4675 | xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len, | |
4676 | skb_headlen(skb) + mac_len, true); | |
a075767b | 4677 | |
02671e23 BT |
4678 | orig_data_end = xdp->data_end; |
4679 | orig_data = xdp->data; | |
29724956 | 4680 | eth = (struct ethhdr *)xdp->data; |
22b60343 | 4681 | orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr); |
29724956 JDB |
4682 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); |
4683 | orig_eth_type = eth->h_proto; | |
d4455169 | 4684 | |
02671e23 | 4685 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
d4455169 | 4686 | |
065af355 | 4687 | /* check if bpf_xdp_adjust_head was used */ |
02671e23 | 4688 | off = xdp->data - orig_data; |
065af355 JDB |
4689 | if (off) { |
4690 | if (off > 0) | |
4691 | __skb_pull(skb, off); | |
4692 | else if (off < 0) | |
4693 | __skb_push(skb, -off); | |
4694 | ||
4695 | skb->mac_header += off; | |
4696 | skb_reset_network_header(skb); | |
4697 | } | |
d4455169 | 4698 | |
a075767b JDB |
4699 | /* check if bpf_xdp_adjust_tail was used */ |
4700 | off = xdp->data_end - orig_data_end; | |
f7613120 | 4701 | if (off != 0) { |
02671e23 | 4702 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
a075767b | 4703 | skb->len += off; /* positive on grow, negative on shrink */ |
f7613120 | 4704 | } |
198d83bb | 4705 | |
29724956 JDB |
4706 | /* check if XDP changed eth hdr such SKB needs update */ |
4707 | eth = (struct ethhdr *)xdp->data; | |
4708 | if ((orig_eth_type != eth->h_proto) || | |
22b60343 MW |
4709 | (orig_host != ether_addr_equal_64bits(eth->h_dest, |
4710 | skb->dev->dev_addr)) || | |
29724956 JDB |
4711 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { |
4712 | __skb_push(skb, ETH_HLEN); | |
22b60343 | 4713 | skb->pkt_type = PACKET_HOST; |
29724956 JDB |
4714 | skb->protocol = eth_type_trans(skb, skb->dev); |
4715 | } | |
4716 | ||
fe21cb91 KKD |
4717 | /* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull |
4718 | * before calling us again on redirect path. We do not call do_redirect | |
4719 | * as we leave that up to the caller. | |
4720 | * | |
4721 | * Caller is responsible for managing lifetime of skb (i.e. calling | |
4722 | * kfree_skb in response to actions it cannot handle/XDP_DROP). | |
4723 | */ | |
d4455169 | 4724 | switch (act) { |
6103aa96 | 4725 | case XDP_REDIRECT: |
d4455169 JF |
4726 | case XDP_TX: |
4727 | __skb_push(skb, mac_len); | |
de8f3a83 | 4728 | break; |
d4455169 | 4729 | case XDP_PASS: |
02671e23 | 4730 | metalen = xdp->data - xdp->data_meta; |
de8f3a83 DB |
4731 | if (metalen) |
4732 | skb_metadata_set(skb, metalen); | |
d4455169 | 4733 | break; |
fe21cb91 KKD |
4734 | } |
4735 | ||
4736 | return act; | |
4737 | } | |
4738 | ||
4739 | static u32 netif_receive_generic_xdp(struct sk_buff *skb, | |
4740 | struct xdp_buff *xdp, | |
4741 | struct bpf_prog *xdp_prog) | |
4742 | { | |
4743 | u32 act = XDP_DROP; | |
4744 | ||
4745 | /* Reinjected packets coming from act_mirred or similar should | |
4746 | * not get XDP generic processing. | |
4747 | */ | |
4748 | if (skb_is_redirected(skb)) | |
4749 | return XDP_PASS; | |
4750 | ||
4751 | /* XDP packets must be linear and must have sufficient headroom | |
4752 | * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also | |
4753 | * native XDP provides, thus we need to do it here as well. | |
4754 | */ | |
4755 | if (skb_cloned(skb) || skb_is_nonlinear(skb) || | |
4756 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { | |
4757 | int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); | |
4758 | int troom = skb->tail + skb->data_len - skb->end; | |
4759 | ||
4760 | /* In case we have to go down the path and also linearize, | |
4761 | * then lets do the pskb_expand_head() work just once here. | |
4762 | */ | |
4763 | if (pskb_expand_head(skb, | |
4764 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, | |
4765 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC)) | |
4766 | goto do_drop; | |
4767 | if (skb_linearize(skb)) | |
4768 | goto do_drop; | |
4769 | } | |
4770 | ||
4771 | act = bpf_prog_run_generic_xdp(skb, xdp, xdp_prog); | |
4772 | switch (act) { | |
4773 | case XDP_REDIRECT: | |
4774 | case XDP_TX: | |
4775 | case XDP_PASS: | |
4776 | break; | |
d4455169 | 4777 | default: |
c8064e5b | 4778 | bpf_warn_invalid_xdp_action(skb->dev, xdp_prog, act); |
df561f66 | 4779 | fallthrough; |
d4455169 JF |
4780 | case XDP_ABORTED: |
4781 | trace_xdp_exception(skb->dev, xdp_prog, act); | |
df561f66 | 4782 | fallthrough; |
d4455169 JF |
4783 | case XDP_DROP: |
4784 | do_drop: | |
4785 | kfree_skb(skb); | |
4786 | break; | |
4787 | } | |
4788 | ||
4789 | return act; | |
4790 | } | |
4791 | ||
4792 | /* When doing generic XDP we have to bypass the qdisc layer and the | |
4793 | * network taps in order to match in-driver-XDP behavior. | |
4794 | */ | |
7c497478 | 4795 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
d4455169 JF |
4796 | { |
4797 | struct net_device *dev = skb->dev; | |
4798 | struct netdev_queue *txq; | |
4799 | bool free_skb = true; | |
4800 | int cpu, rc; | |
4801 | ||
4bd97d51 | 4802 | txq = netdev_core_pick_tx(dev, skb, NULL); |
d4455169 JF |
4803 | cpu = smp_processor_id(); |
4804 | HARD_TX_LOCK(dev, txq, cpu); | |
4805 | if (!netif_xmit_stopped(txq)) { | |
4806 | rc = netdev_start_xmit(skb, dev, txq, 0); | |
4807 | if (dev_xmit_complete(rc)) | |
4808 | free_skb = false; | |
4809 | } | |
4810 | HARD_TX_UNLOCK(dev, txq); | |
4811 | if (free_skb) { | |
4812 | trace_xdp_exception(dev, xdp_prog, XDP_TX); | |
4813 | kfree_skb(skb); | |
4814 | } | |
4815 | } | |
4816 | ||
02786475 | 4817 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
d4455169 | 4818 | |
7c497478 | 4819 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb) |
d4455169 | 4820 | { |
d4455169 | 4821 | if (xdp_prog) { |
02671e23 BT |
4822 | struct xdp_buff xdp; |
4823 | u32 act; | |
6103aa96 | 4824 | int err; |
d4455169 | 4825 | |
02671e23 | 4826 | act = netif_receive_generic_xdp(skb, &xdp, xdp_prog); |
d4455169 | 4827 | if (act != XDP_PASS) { |
6103aa96 JF |
4828 | switch (act) { |
4829 | case XDP_REDIRECT: | |
2facaad6 | 4830 | err = xdp_do_generic_redirect(skb->dev, skb, |
02671e23 | 4831 | &xdp, xdp_prog); |
6103aa96 JF |
4832 | if (err) |
4833 | goto out_redir; | |
02671e23 | 4834 | break; |
6103aa96 | 4835 | case XDP_TX: |
d4455169 | 4836 | generic_xdp_tx(skb, xdp_prog); |
6103aa96 JF |
4837 | break; |
4838 | } | |
d4455169 JF |
4839 | return XDP_DROP; |
4840 | } | |
4841 | } | |
4842 | return XDP_PASS; | |
6103aa96 | 4843 | out_redir: |
7e726ed8 | 4844 | kfree_skb_reason(skb, SKB_DROP_REASON_XDP); |
6103aa96 | 4845 | return XDP_DROP; |
d4455169 | 4846 | } |
7c497478 | 4847 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
d4455169 | 4848 | |
ae78dbfa | 4849 | static int netif_rx_internal(struct sk_buff *skb) |
1da177e4 | 4850 | { |
b0e28f1e | 4851 | int ret; |
1da177e4 | 4852 | |
588f0330 | 4853 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
1da177e4 | 4854 | |
cf66ba58 | 4855 | trace_netif_rx(skb); |
d4455169 | 4856 | |
df334545 | 4857 | #ifdef CONFIG_RPS |
dc05360f | 4858 | if (static_branch_unlikely(&rps_needed)) { |
fec5e652 | 4859 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
4860 | int cpu; |
4861 | ||
4862 | rcu_read_lock(); | |
fec5e652 TH |
4863 | |
4864 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
4865 | if (cpu < 0) |
4866 | cpu = smp_processor_id(); | |
fec5e652 TH |
4867 | |
4868 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
4869 | ||
b0e28f1e | 4870 | rcu_read_unlock(); |
adc9300e ED |
4871 | } else |
4872 | #endif | |
fec5e652 TH |
4873 | { |
4874 | unsigned int qtail; | |
f4563a75 | 4875 | |
f234ae29 | 4876 | ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail); |
fec5e652 | 4877 | } |
b0e28f1e | 4878 | return ret; |
1da177e4 | 4879 | } |
ae78dbfa | 4880 | |
baebdf48 SAS |
4881 | /** |
4882 | * __netif_rx - Slightly optimized version of netif_rx | |
4883 | * @skb: buffer to post | |
4884 | * | |
4885 | * This behaves as netif_rx except that it does not disable bottom halves. | |
4886 | * As a result this function may only be invoked from the interrupt context | |
4887 | * (either hard or soft interrupt). | |
4888 | */ | |
4889 | int __netif_rx(struct sk_buff *skb) | |
4890 | { | |
4891 | int ret; | |
4892 | ||
351bdbb6 | 4893 | lockdep_assert_once(hardirq_count() | softirq_count()); |
baebdf48 SAS |
4894 | |
4895 | trace_netif_rx_entry(skb); | |
4896 | ret = netif_rx_internal(skb); | |
4897 | trace_netif_rx_exit(ret); | |
4898 | return ret; | |
4899 | } | |
4900 | EXPORT_SYMBOL(__netif_rx); | |
4901 | ||
ae78dbfa BH |
4902 | /** |
4903 | * netif_rx - post buffer to the network code | |
4904 | * @skb: buffer to post | |
4905 | * | |
4906 | * This function receives a packet from a device driver and queues it for | |
baebdf48 SAS |
4907 | * the upper (protocol) levels to process via the backlog NAPI device. It |
4908 | * always succeeds. The buffer may be dropped during processing for | |
4909 | * congestion control or by the protocol layers. | |
4910 | * The network buffer is passed via the backlog NAPI device. Modern NIC | |
4911 | * driver should use NAPI and GRO. | |
167053f8 SAS |
4912 | * This function can used from interrupt and from process context. The |
4913 | * caller from process context must not disable interrupts before invoking | |
4914 | * this function. | |
ae78dbfa BH |
4915 | * |
4916 | * return values: | |
4917 | * NET_RX_SUCCESS (no congestion) | |
4918 | * NET_RX_DROP (packet was dropped) | |
4919 | * | |
4920 | */ | |
ae78dbfa BH |
4921 | int netif_rx(struct sk_buff *skb) |
4922 | { | |
167053f8 | 4923 | bool need_bh_off = !(hardirq_count() | softirq_count()); |
b0e3f1bd GB |
4924 | int ret; |
4925 | ||
167053f8 SAS |
4926 | if (need_bh_off) |
4927 | local_bh_disable(); | |
ae78dbfa | 4928 | trace_netif_rx_entry(skb); |
b0e3f1bd GB |
4929 | ret = netif_rx_internal(skb); |
4930 | trace_netif_rx_exit(ret); | |
167053f8 SAS |
4931 | if (need_bh_off) |
4932 | local_bh_enable(); | |
b0e3f1bd | 4933 | return ret; |
ae78dbfa | 4934 | } |
d1b19dff | 4935 | EXPORT_SYMBOL(netif_rx); |
1da177e4 | 4936 | |
0766f788 | 4937 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
1da177e4 | 4938 | { |
903ceff7 | 4939 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
1da177e4 LT |
4940 | |
4941 | if (sd->completion_queue) { | |
4942 | struct sk_buff *clist; | |
4943 | ||
4944 | local_irq_disable(); | |
4945 | clist = sd->completion_queue; | |
4946 | sd->completion_queue = NULL; | |
4947 | local_irq_enable(); | |
4948 | ||
4949 | while (clist) { | |
4950 | struct sk_buff *skb = clist; | |
f4563a75 | 4951 | |
1da177e4 LT |
4952 | clist = clist->next; |
4953 | ||
63354797 | 4954 | WARN_ON(refcount_read(&skb->users)); |
e6247027 ED |
4955 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED)) |
4956 | trace_consume_skb(skb); | |
4957 | else | |
c504e5c2 MD |
4958 | trace_kfree_skb(skb, net_tx_action, |
4959 | SKB_DROP_REASON_NOT_SPECIFIED); | |
15fad714 JDB |
4960 | |
4961 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) | |
4962 | __kfree_skb(skb); | |
4963 | else | |
4964 | __kfree_skb_defer(skb); | |
1da177e4 LT |
4965 | } |
4966 | } | |
4967 | ||
4968 | if (sd->output_queue) { | |
37437bb2 | 4969 | struct Qdisc *head; |
1da177e4 LT |
4970 | |
4971 | local_irq_disable(); | |
4972 | head = sd->output_queue; | |
4973 | sd->output_queue = NULL; | |
a9cbd588 | 4974 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
4975 | local_irq_enable(); |
4976 | ||
102b55ee YL |
4977 | rcu_read_lock(); |
4978 | ||
1da177e4 | 4979 | while (head) { |
37437bb2 | 4980 | struct Qdisc *q = head; |
6b3ba914 | 4981 | spinlock_t *root_lock = NULL; |
37437bb2 | 4982 | |
1da177e4 LT |
4983 | head = head->next_sched; |
4984 | ||
3bcb846c ED |
4985 | /* We need to make sure head->next_sched is read |
4986 | * before clearing __QDISC_STATE_SCHED | |
4987 | */ | |
4988 | smp_mb__before_atomic(); | |
102b55ee YL |
4989 | |
4990 | if (!(q->flags & TCQ_F_NOLOCK)) { | |
4991 | root_lock = qdisc_lock(q); | |
4992 | spin_lock(root_lock); | |
4993 | } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, | |
4994 | &q->state))) { | |
4995 | /* There is a synchronize_net() between | |
4996 | * STATE_DEACTIVATED flag being set and | |
4997 | * qdisc_reset()/some_qdisc_is_busy() in | |
4998 | * dev_deactivate(), so we can safely bail out | |
4999 | * early here to avoid data race between | |
5000 | * qdisc_deactivate() and some_qdisc_is_busy() | |
5001 | * for lockless qdisc. | |
5002 | */ | |
5003 | clear_bit(__QDISC_STATE_SCHED, &q->state); | |
5004 | continue; | |
5005 | } | |
5006 | ||
3bcb846c ED |
5007 | clear_bit(__QDISC_STATE_SCHED, &q->state); |
5008 | qdisc_run(q); | |
6b3ba914 JF |
5009 | if (root_lock) |
5010 | spin_unlock(root_lock); | |
1da177e4 | 5011 | } |
102b55ee YL |
5012 | |
5013 | rcu_read_unlock(); | |
1da177e4 | 5014 | } |
f53c7239 SK |
5015 | |
5016 | xfrm_dev_backlog(sd); | |
1da177e4 LT |
5017 | } |
5018 | ||
181402a5 | 5019 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
5020 | /* This hook is defined here for ATM LANE */ |
5021 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
5022 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 5023 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 5024 | #endif |
1da177e4 | 5025 | |
1f211a1b DB |
5026 | static inline struct sk_buff * |
5027 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
9aa1206e | 5028 | struct net_device *orig_dev, bool *another) |
f697c3e8 | 5029 | { |
e7582bab | 5030 | #ifdef CONFIG_NET_CLS_ACT |
46209401 | 5031 | struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress); |
d2788d34 | 5032 | struct tcf_result cl_res; |
24824a09 | 5033 | |
c9e99fd0 DB |
5034 | /* If there's at least one ingress present somewhere (so |
5035 | * we get here via enabled static key), remaining devices | |
5036 | * that are not configured with an ingress qdisc will bail | |
d2788d34 | 5037 | * out here. |
c9e99fd0 | 5038 | */ |
46209401 | 5039 | if (!miniq) |
4577139b | 5040 | return skb; |
46209401 | 5041 | |
f697c3e8 HX |
5042 | if (*pt_prev) { |
5043 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
5044 | *pt_prev = NULL; | |
1da177e4 LT |
5045 | } |
5046 | ||
3365495c | 5047 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
ec624fe7 PB |
5048 | tc_skb_cb(skb)->mru = 0; |
5049 | tc_skb_cb(skb)->post_ct = false; | |
8dc07fdb | 5050 | skb->tc_at_ingress = 1; |
46209401 | 5051 | mini_qdisc_bstats_cpu_update(miniq, skb); |
c9e99fd0 | 5052 | |
3aa26055 | 5053 | switch (tcf_classify(skb, miniq->block, miniq->filter_list, &cl_res, false)) { |
d2788d34 DB |
5054 | case TC_ACT_OK: |
5055 | case TC_ACT_RECLASSIFY: | |
5056 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
5057 | break; | |
5058 | case TC_ACT_SHOT: | |
46209401 | 5059 | mini_qdisc_qstats_cpu_drop(miniq); |
a568aff2 | 5060 | kfree_skb_reason(skb, SKB_DROP_REASON_TC_INGRESS); |
8a3a4c6e | 5061 | return NULL; |
d2788d34 DB |
5062 | case TC_ACT_STOLEN: |
5063 | case TC_ACT_QUEUED: | |
e25ea21f | 5064 | case TC_ACT_TRAP: |
8a3a4c6e | 5065 | consume_skb(skb); |
d2788d34 | 5066 | return NULL; |
27b29f63 AS |
5067 | case TC_ACT_REDIRECT: |
5068 | /* skb_mac_header check was done by cls/act_bpf, so | |
5069 | * we can safely push the L2 header back before | |
5070 | * redirecting to another netdev | |
5071 | */ | |
5072 | __skb_push(skb, skb->mac_len); | |
9aa1206e DB |
5073 | if (skb_do_redirect(skb) == -EAGAIN) { |
5074 | __skb_pull(skb, skb->mac_len); | |
5075 | *another = true; | |
5076 | break; | |
5077 | } | |
27b29f63 | 5078 | return NULL; |
720f22fe | 5079 | case TC_ACT_CONSUMED: |
cd11b164 | 5080 | return NULL; |
d2788d34 DB |
5081 | default: |
5082 | break; | |
f697c3e8 | 5083 | } |
e7582bab | 5084 | #endif /* CONFIG_NET_CLS_ACT */ |
e687ad60 PN |
5085 | return skb; |
5086 | } | |
1da177e4 | 5087 | |
24b27fc4 MB |
5088 | /** |
5089 | * netdev_is_rx_handler_busy - check if receive handler is registered | |
5090 | * @dev: device to check | |
5091 | * | |
5092 | * Check if a receive handler is already registered for a given device. | |
5093 | * Return true if there one. | |
5094 | * | |
5095 | * The caller must hold the rtnl_mutex. | |
5096 | */ | |
5097 | bool netdev_is_rx_handler_busy(struct net_device *dev) | |
5098 | { | |
5099 | ASSERT_RTNL(); | |
5100 | return dev && rtnl_dereference(dev->rx_handler); | |
5101 | } | |
5102 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); | |
5103 | ||
ab95bfe0 JP |
5104 | /** |
5105 | * netdev_rx_handler_register - register receive handler | |
5106 | * @dev: device to register a handler for | |
5107 | * @rx_handler: receive handler to register | |
93e2c32b | 5108 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 | 5109 | * |
e227867f | 5110 | * Register a receive handler for a device. This handler will then be |
ab95bfe0 JP |
5111 | * called from __netif_receive_skb. A negative errno code is returned |
5112 | * on a failure. | |
5113 | * | |
5114 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
5115 | * |
5116 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
5117 | */ |
5118 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
5119 | rx_handler_func_t *rx_handler, |
5120 | void *rx_handler_data) | |
ab95bfe0 | 5121 | { |
1b7cd004 | 5122 | if (netdev_is_rx_handler_busy(dev)) |
ab95bfe0 JP |
5123 | return -EBUSY; |
5124 | ||
f5426250 PA |
5125 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
5126 | return -EINVAL; | |
5127 | ||
00cfec37 | 5128 | /* Note: rx_handler_data must be set before rx_handler */ |
93e2c32b | 5129 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
5130 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
5131 | ||
5132 | return 0; | |
5133 | } | |
5134 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
5135 | ||
5136 | /** | |
5137 | * netdev_rx_handler_unregister - unregister receive handler | |
5138 | * @dev: device to unregister a handler from | |
5139 | * | |
166ec369 | 5140 | * Unregister a receive handler from a device. |
ab95bfe0 JP |
5141 | * |
5142 | * The caller must hold the rtnl_mutex. | |
5143 | */ | |
5144 | void netdev_rx_handler_unregister(struct net_device *dev) | |
5145 | { | |
5146 | ||
5147 | ASSERT_RTNL(); | |
a9b3cd7f | 5148 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
00cfec37 ED |
5149 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
5150 | * section has a guarantee to see a non NULL rx_handler_data | |
5151 | * as well. | |
5152 | */ | |
5153 | synchronize_net(); | |
a9b3cd7f | 5154 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
ab95bfe0 JP |
5155 | } |
5156 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
5157 | ||
b4b9e355 MG |
5158 | /* |
5159 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
5160 | * the special handling of PFMEMALLOC skbs. | |
5161 | */ | |
5162 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
5163 | { | |
5164 | switch (skb->protocol) { | |
2b8837ae JP |
5165 | case htons(ETH_P_ARP): |
5166 | case htons(ETH_P_IP): | |
5167 | case htons(ETH_P_IPV6): | |
5168 | case htons(ETH_P_8021Q): | |
5169 | case htons(ETH_P_8021AD): | |
b4b9e355 MG |
5170 | return true; |
5171 | default: | |
5172 | return false; | |
5173 | } | |
5174 | } | |
5175 | ||
e687ad60 PN |
5176 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
5177 | int *ret, struct net_device *orig_dev) | |
5178 | { | |
5179 | if (nf_hook_ingress_active(skb)) { | |
2c1e2703 AC |
5180 | int ingress_retval; |
5181 | ||
e687ad60 PN |
5182 | if (*pt_prev) { |
5183 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
5184 | *pt_prev = NULL; | |
5185 | } | |
5186 | ||
2c1e2703 AC |
5187 | rcu_read_lock(); |
5188 | ingress_retval = nf_hook_ingress(skb); | |
5189 | rcu_read_unlock(); | |
5190 | return ingress_retval; | |
e687ad60 PN |
5191 | } |
5192 | return 0; | |
5193 | } | |
e687ad60 | 5194 | |
c0bbbdc3 | 5195 | static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, |
88eb1944 | 5196 | struct packet_type **ppt_prev) |
1da177e4 LT |
5197 | { |
5198 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 5199 | rx_handler_func_t *rx_handler; |
c0bbbdc3 | 5200 | struct sk_buff *skb = *pskb; |
f2ccd8fa | 5201 | struct net_device *orig_dev; |
8a4eb573 | 5202 | bool deliver_exact = false; |
1da177e4 | 5203 | int ret = NET_RX_DROP; |
252e3346 | 5204 | __be16 type; |
1da177e4 | 5205 | |
588f0330 | 5206 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
81bbb3d4 | 5207 | |
cf66ba58 | 5208 | trace_netif_receive_skb(skb); |
9b22ea56 | 5209 | |
cc9bd5ce | 5210 | orig_dev = skb->dev; |
8f903c70 | 5211 | |
c1d2bbe1 | 5212 | skb_reset_network_header(skb); |
fda55eca ED |
5213 | if (!skb_transport_header_was_set(skb)) |
5214 | skb_reset_transport_header(skb); | |
0b5c9db1 | 5215 | skb_reset_mac_len(skb); |
1da177e4 LT |
5216 | |
5217 | pt_prev = NULL; | |
5218 | ||
63d8ea7f | 5219 | another_round: |
b6858177 | 5220 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
5221 | |
5222 | __this_cpu_inc(softnet_data.processed); | |
5223 | ||
458bf2f2 SH |
5224 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
5225 | int ret2; | |
5226 | ||
2b4cd14f | 5227 | migrate_disable(); |
458bf2f2 | 5228 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb); |
2b4cd14f | 5229 | migrate_enable(); |
458bf2f2 | 5230 | |
c0bbbdc3 BS |
5231 | if (ret2 != XDP_PASS) { |
5232 | ret = NET_RX_DROP; | |
5233 | goto out; | |
5234 | } | |
458bf2f2 SH |
5235 | } |
5236 | ||
324cefaf | 5237 | if (eth_type_vlan(skb->protocol)) { |
0d5501c1 | 5238 | skb = skb_vlan_untag(skb); |
bcc6d479 | 5239 | if (unlikely(!skb)) |
2c17d27c | 5240 | goto out; |
bcc6d479 JP |
5241 | } |
5242 | ||
cd14e9b7 | 5243 | if (skb_skip_tc_classify(skb)) |
e7246e12 | 5244 | goto skip_classify; |
1da177e4 | 5245 | |
9754e293 | 5246 | if (pfmemalloc) |
b4b9e355 MG |
5247 | goto skip_taps; |
5248 | ||
1da177e4 | 5249 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
7866a621 SN |
5250 | if (pt_prev) |
5251 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5252 | pt_prev = ptype; | |
5253 | } | |
5254 | ||
5255 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { | |
5256 | if (pt_prev) | |
5257 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5258 | pt_prev = ptype; | |
1da177e4 LT |
5259 | } |
5260 | ||
b4b9e355 | 5261 | skip_taps: |
1cf51900 | 5262 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 5263 | if (static_branch_unlikely(&ingress_needed_key)) { |
9aa1206e DB |
5264 | bool another = false; |
5265 | ||
42df6e1d | 5266 | nf_skip_egress(skb, true); |
9aa1206e DB |
5267 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev, |
5268 | &another); | |
5269 | if (another) | |
5270 | goto another_round; | |
4577139b | 5271 | if (!skb) |
2c17d27c | 5272 | goto out; |
e687ad60 | 5273 | |
42df6e1d | 5274 | nf_skip_egress(skb, false); |
e687ad60 | 5275 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) |
2c17d27c | 5276 | goto out; |
cd14e9b7 | 5277 | } |
1cf51900 | 5278 | #endif |
2c64605b | 5279 | skb_reset_redirect(skb); |
e7246e12 | 5280 | skip_classify: |
9754e293 | 5281 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
b4b9e355 MG |
5282 | goto drop; |
5283 | ||
df8a39de | 5284 | if (skb_vlan_tag_present(skb)) { |
2425717b JF |
5285 | if (pt_prev) { |
5286 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5287 | pt_prev = NULL; | |
5288 | } | |
48cc32d3 | 5289 | if (vlan_do_receive(&skb)) |
2425717b JF |
5290 | goto another_round; |
5291 | else if (unlikely(!skb)) | |
2c17d27c | 5292 | goto out; |
2425717b JF |
5293 | } |
5294 | ||
48cc32d3 | 5295 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
5296 | if (rx_handler) { |
5297 | if (pt_prev) { | |
5298 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5299 | pt_prev = NULL; | |
5300 | } | |
8a4eb573 JP |
5301 | switch (rx_handler(&skb)) { |
5302 | case RX_HANDLER_CONSUMED: | |
3bc1b1ad | 5303 | ret = NET_RX_SUCCESS; |
2c17d27c | 5304 | goto out; |
8a4eb573 | 5305 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 5306 | goto another_round; |
8a4eb573 JP |
5307 | case RX_HANDLER_EXACT: |
5308 | deliver_exact = true; | |
b1866bff | 5309 | break; |
8a4eb573 JP |
5310 | case RX_HANDLER_PASS: |
5311 | break; | |
5312 | default: | |
5313 | BUG(); | |
5314 | } | |
ab95bfe0 | 5315 | } |
1da177e4 | 5316 | |
b14a9fc4 | 5317 | if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) { |
36b2f61a GV |
5318 | check_vlan_id: |
5319 | if (skb_vlan_tag_get_id(skb)) { | |
5320 | /* Vlan id is non 0 and vlan_do_receive() above couldn't | |
5321 | * find vlan device. | |
5322 | */ | |
d4b812de | 5323 | skb->pkt_type = PACKET_OTHERHOST; |
324cefaf | 5324 | } else if (eth_type_vlan(skb->protocol)) { |
36b2f61a GV |
5325 | /* Outer header is 802.1P with vlan 0, inner header is |
5326 | * 802.1Q or 802.1AD and vlan_do_receive() above could | |
5327 | * not find vlan dev for vlan id 0. | |
5328 | */ | |
5329 | __vlan_hwaccel_clear_tag(skb); | |
5330 | skb = skb_vlan_untag(skb); | |
5331 | if (unlikely(!skb)) | |
5332 | goto out; | |
5333 | if (vlan_do_receive(&skb)) | |
5334 | /* After stripping off 802.1P header with vlan 0 | |
5335 | * vlan dev is found for inner header. | |
5336 | */ | |
5337 | goto another_round; | |
5338 | else if (unlikely(!skb)) | |
5339 | goto out; | |
5340 | else | |
5341 | /* We have stripped outer 802.1P vlan 0 header. | |
5342 | * But could not find vlan dev. | |
5343 | * check again for vlan id to set OTHERHOST. | |
5344 | */ | |
5345 | goto check_vlan_id; | |
5346 | } | |
d4b812de ED |
5347 | /* Note: we might in the future use prio bits |
5348 | * and set skb->priority like in vlan_do_receive() | |
5349 | * For the time being, just ignore Priority Code Point | |
5350 | */ | |
b1817524 | 5351 | __vlan_hwaccel_clear_tag(skb); |
d4b812de | 5352 | } |
48cc32d3 | 5353 | |
7866a621 SN |
5354 | type = skb->protocol; |
5355 | ||
63d8ea7f | 5356 | /* deliver only exact match when indicated */ |
7866a621 SN |
5357 | if (likely(!deliver_exact)) { |
5358 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5359 | &ptype_base[ntohs(type) & | |
5360 | PTYPE_HASH_MASK]); | |
5361 | } | |
1f3c8804 | 5362 | |
7866a621 SN |
5363 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
5364 | &orig_dev->ptype_specific); | |
5365 | ||
5366 | if (unlikely(skb->dev != orig_dev)) { | |
5367 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5368 | &skb->dev->ptype_specific); | |
1da177e4 LT |
5369 | } |
5370 | ||
5371 | if (pt_prev) { | |
1f8b977a | 5372 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
0e698bf6 | 5373 | goto drop; |
88eb1944 | 5374 | *ppt_prev = pt_prev; |
1da177e4 | 5375 | } else { |
b4b9e355 | 5376 | drop: |
6c2728b7 | 5377 | if (!deliver_exact) { |
625788b5 | 5378 | dev_core_stats_rx_dropped_inc(skb->dev); |
6c2728b7 MD |
5379 | kfree_skb_reason(skb, SKB_DROP_REASON_PTYPE_ABSENT); |
5380 | } else { | |
625788b5 | 5381 | dev_core_stats_rx_nohandler_inc(skb->dev); |
6c2728b7 MD |
5382 | kfree_skb(skb); |
5383 | } | |
1da177e4 LT |
5384 | /* Jamal, now you will not able to escape explaining |
5385 | * me how you were going to use this. :-) | |
5386 | */ | |
5387 | ret = NET_RX_DROP; | |
5388 | } | |
5389 | ||
2c17d27c | 5390 | out: |
c0bbbdc3 BS |
5391 | /* The invariant here is that if *ppt_prev is not NULL |
5392 | * then skb should also be non-NULL. | |
5393 | * | |
5394 | * Apparently *ppt_prev assignment above holds this invariant due to | |
5395 | * skb dereferencing near it. | |
5396 | */ | |
5397 | *pskb = skb; | |
9754e293 DM |
5398 | return ret; |
5399 | } | |
5400 | ||
88eb1944 EC |
5401 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
5402 | { | |
5403 | struct net_device *orig_dev = skb->dev; | |
5404 | struct packet_type *pt_prev = NULL; | |
5405 | int ret; | |
5406 | ||
c0bbbdc3 | 5407 | ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
88eb1944 | 5408 | if (pt_prev) |
f5737cba PA |
5409 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
5410 | skb->dev, pt_prev, orig_dev); | |
88eb1944 EC |
5411 | return ret; |
5412 | } | |
5413 | ||
1c601d82 JDB |
5414 | /** |
5415 | * netif_receive_skb_core - special purpose version of netif_receive_skb | |
5416 | * @skb: buffer to process | |
5417 | * | |
5418 | * More direct receive version of netif_receive_skb(). It should | |
5419 | * only be used by callers that have a need to skip RPS and Generic XDP. | |
2de9780f | 5420 | * Caller must also take care of handling if ``(page_is_)pfmemalloc``. |
1c601d82 JDB |
5421 | * |
5422 | * This function may only be called from softirq context and interrupts | |
5423 | * should be enabled. | |
5424 | * | |
5425 | * Return values (usually ignored): | |
5426 | * NET_RX_SUCCESS: no congestion | |
5427 | * NET_RX_DROP: packet was dropped | |
5428 | */ | |
5429 | int netif_receive_skb_core(struct sk_buff *skb) | |
5430 | { | |
5431 | int ret; | |
5432 | ||
5433 | rcu_read_lock(); | |
88eb1944 | 5434 | ret = __netif_receive_skb_one_core(skb, false); |
1c601d82 JDB |
5435 | rcu_read_unlock(); |
5436 | ||
5437 | return ret; | |
5438 | } | |
5439 | EXPORT_SYMBOL(netif_receive_skb_core); | |
5440 | ||
88eb1944 EC |
5441 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
5442 | struct packet_type *pt_prev, | |
5443 | struct net_device *orig_dev) | |
4ce0017a EC |
5444 | { |
5445 | struct sk_buff *skb, *next; | |
5446 | ||
88eb1944 EC |
5447 | if (!pt_prev) |
5448 | return; | |
5449 | if (list_empty(head)) | |
5450 | return; | |
17266ee9 | 5451 | if (pt_prev->list_func != NULL) |
fdf71426 PA |
5452 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
5453 | ip_list_rcv, head, pt_prev, orig_dev); | |
17266ee9 | 5454 | else |
9a5a90d1 AL |
5455 | list_for_each_entry_safe(skb, next, head, list) { |
5456 | skb_list_del_init(skb); | |
fdf71426 | 5457 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
9a5a90d1 | 5458 | } |
88eb1944 EC |
5459 | } |
5460 | ||
5461 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) | |
5462 | { | |
5463 | /* Fast-path assumptions: | |
5464 | * - There is no RX handler. | |
5465 | * - Only one packet_type matches. | |
5466 | * If either of these fails, we will end up doing some per-packet | |
5467 | * processing in-line, then handling the 'last ptype' for the whole | |
5468 | * sublist. This can't cause out-of-order delivery to any single ptype, | |
5469 | * because the 'last ptype' must be constant across the sublist, and all | |
5470 | * other ptypes are handled per-packet. | |
5471 | */ | |
5472 | /* Current (common) ptype of sublist */ | |
5473 | struct packet_type *pt_curr = NULL; | |
5474 | /* Current (common) orig_dev of sublist */ | |
5475 | struct net_device *od_curr = NULL; | |
5476 | struct list_head sublist; | |
5477 | struct sk_buff *skb, *next; | |
5478 | ||
9af86f93 | 5479 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5480 | list_for_each_entry_safe(skb, next, head, list) { |
5481 | struct net_device *orig_dev = skb->dev; | |
5482 | struct packet_type *pt_prev = NULL; | |
5483 | ||
22f6bbb7 | 5484 | skb_list_del_init(skb); |
c0bbbdc3 | 5485 | __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
9af86f93 EC |
5486 | if (!pt_prev) |
5487 | continue; | |
88eb1944 EC |
5488 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
5489 | /* dispatch old sublist */ | |
88eb1944 EC |
5490 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
5491 | /* start new sublist */ | |
9af86f93 | 5492 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5493 | pt_curr = pt_prev; |
5494 | od_curr = orig_dev; | |
5495 | } | |
9af86f93 | 5496 | list_add_tail(&skb->list, &sublist); |
88eb1944 EC |
5497 | } |
5498 | ||
5499 | /* dispatch final sublist */ | |
9af86f93 | 5500 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
4ce0017a EC |
5501 | } |
5502 | ||
9754e293 DM |
5503 | static int __netif_receive_skb(struct sk_buff *skb) |
5504 | { | |
5505 | int ret; | |
5506 | ||
5507 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { | |
f1083048 | 5508 | unsigned int noreclaim_flag; |
9754e293 DM |
5509 | |
5510 | /* | |
5511 | * PFMEMALLOC skbs are special, they should | |
5512 | * - be delivered to SOCK_MEMALLOC sockets only | |
5513 | * - stay away from userspace | |
5514 | * - have bounded memory usage | |
5515 | * | |
5516 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
5517 | * context down to all allocation sites. | |
5518 | */ | |
f1083048 | 5519 | noreclaim_flag = memalloc_noreclaim_save(); |
88eb1944 | 5520 | ret = __netif_receive_skb_one_core(skb, true); |
f1083048 | 5521 | memalloc_noreclaim_restore(noreclaim_flag); |
9754e293 | 5522 | } else |
88eb1944 | 5523 | ret = __netif_receive_skb_one_core(skb, false); |
9754e293 | 5524 | |
1da177e4 LT |
5525 | return ret; |
5526 | } | |
0a9627f2 | 5527 | |
4ce0017a EC |
5528 | static void __netif_receive_skb_list(struct list_head *head) |
5529 | { | |
5530 | unsigned long noreclaim_flag = 0; | |
5531 | struct sk_buff *skb, *next; | |
5532 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ | |
5533 | ||
5534 | list_for_each_entry_safe(skb, next, head, list) { | |
5535 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { | |
5536 | struct list_head sublist; | |
5537 | ||
5538 | /* Handle the previous sublist */ | |
5539 | list_cut_before(&sublist, head, &skb->list); | |
b9f463d6 EC |
5540 | if (!list_empty(&sublist)) |
5541 | __netif_receive_skb_list_core(&sublist, pfmemalloc); | |
4ce0017a EC |
5542 | pfmemalloc = !pfmemalloc; |
5543 | /* See comments in __netif_receive_skb */ | |
5544 | if (pfmemalloc) | |
5545 | noreclaim_flag = memalloc_noreclaim_save(); | |
5546 | else | |
5547 | memalloc_noreclaim_restore(noreclaim_flag); | |
5548 | } | |
5549 | } | |
5550 | /* Handle the remaining sublist */ | |
b9f463d6 EC |
5551 | if (!list_empty(head)) |
5552 | __netif_receive_skb_list_core(head, pfmemalloc); | |
4ce0017a EC |
5553 | /* Restore pflags */ |
5554 | if (pfmemalloc) | |
5555 | memalloc_noreclaim_restore(noreclaim_flag); | |
5556 | } | |
5557 | ||
f4e63525 | 5558 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
b5cdae32 | 5559 | { |
58038695 | 5560 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
b5cdae32 DM |
5561 | struct bpf_prog *new = xdp->prog; |
5562 | int ret = 0; | |
5563 | ||
5564 | switch (xdp->command) { | |
58038695 | 5565 | case XDP_SETUP_PROG: |
b5cdae32 DM |
5566 | rcu_assign_pointer(dev->xdp_prog, new); |
5567 | if (old) | |
5568 | bpf_prog_put(old); | |
5569 | ||
5570 | if (old && !new) { | |
02786475 | 5571 | static_branch_dec(&generic_xdp_needed_key); |
b5cdae32 | 5572 | } else if (new && !old) { |
02786475 | 5573 | static_branch_inc(&generic_xdp_needed_key); |
b5cdae32 | 5574 | dev_disable_lro(dev); |
56f5aa77 | 5575 | dev_disable_gro_hw(dev); |
b5cdae32 DM |
5576 | } |
5577 | break; | |
b5cdae32 | 5578 | |
b5cdae32 DM |
5579 | default: |
5580 | ret = -EINVAL; | |
5581 | break; | |
5582 | } | |
5583 | ||
5584 | return ret; | |
5585 | } | |
5586 | ||
ae78dbfa | 5587 | static int netif_receive_skb_internal(struct sk_buff *skb) |
0a9627f2 | 5588 | { |
2c17d27c JA |
5589 | int ret; |
5590 | ||
588f0330 | 5591 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
3b098e2d | 5592 | |
c1f19b51 RC |
5593 | if (skb_defer_rx_timestamp(skb)) |
5594 | return NET_RX_SUCCESS; | |
5595 | ||
bbbe211c | 5596 | rcu_read_lock(); |
df334545 | 5597 | #ifdef CONFIG_RPS |
dc05360f | 5598 | if (static_branch_unlikely(&rps_needed)) { |
3b098e2d | 5599 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
2c17d27c | 5600 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
0a9627f2 | 5601 | |
3b098e2d ED |
5602 | if (cpu >= 0) { |
5603 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5604 | rcu_read_unlock(); | |
adc9300e | 5605 | return ret; |
3b098e2d | 5606 | } |
fec5e652 | 5607 | } |
1e94d72f | 5608 | #endif |
2c17d27c JA |
5609 | ret = __netif_receive_skb(skb); |
5610 | rcu_read_unlock(); | |
5611 | return ret; | |
0a9627f2 | 5612 | } |
ae78dbfa | 5613 | |
587652bb | 5614 | void netif_receive_skb_list_internal(struct list_head *head) |
7da517a3 | 5615 | { |
7da517a3 | 5616 | struct sk_buff *skb, *next; |
8c057efa | 5617 | struct list_head sublist; |
7da517a3 | 5618 | |
8c057efa | 5619 | INIT_LIST_HEAD(&sublist); |
7da517a3 EC |
5620 | list_for_each_entry_safe(skb, next, head, list) { |
5621 | net_timestamp_check(netdev_tstamp_prequeue, skb); | |
22f6bbb7 | 5622 | skb_list_del_init(skb); |
8c057efa EC |
5623 | if (!skb_defer_rx_timestamp(skb)) |
5624 | list_add_tail(&skb->list, &sublist); | |
7da517a3 | 5625 | } |
8c057efa | 5626 | list_splice_init(&sublist, head); |
7da517a3 | 5627 | |
7da517a3 EC |
5628 | rcu_read_lock(); |
5629 | #ifdef CONFIG_RPS | |
dc05360f | 5630 | if (static_branch_unlikely(&rps_needed)) { |
7da517a3 EC |
5631 | list_for_each_entry_safe(skb, next, head, list) { |
5632 | struct rps_dev_flow voidflow, *rflow = &voidflow; | |
5633 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
5634 | ||
5635 | if (cpu >= 0) { | |
8c057efa | 5636 | /* Will be handled, remove from list */ |
22f6bbb7 | 5637 | skb_list_del_init(skb); |
8c057efa | 5638 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
7da517a3 EC |
5639 | } |
5640 | } | |
5641 | } | |
5642 | #endif | |
5643 | __netif_receive_skb_list(head); | |
5644 | rcu_read_unlock(); | |
5645 | } | |
5646 | ||
ae78dbfa BH |
5647 | /** |
5648 | * netif_receive_skb - process receive buffer from network | |
5649 | * @skb: buffer to process | |
5650 | * | |
5651 | * netif_receive_skb() is the main receive data processing function. | |
5652 | * It always succeeds. The buffer may be dropped during processing | |
5653 | * for congestion control or by the protocol layers. | |
5654 | * | |
5655 | * This function may only be called from softirq context and interrupts | |
5656 | * should be enabled. | |
5657 | * | |
5658 | * Return values (usually ignored): | |
5659 | * NET_RX_SUCCESS: no congestion | |
5660 | * NET_RX_DROP: packet was dropped | |
5661 | */ | |
04eb4489 | 5662 | int netif_receive_skb(struct sk_buff *skb) |
ae78dbfa | 5663 | { |
b0e3f1bd GB |
5664 | int ret; |
5665 | ||
ae78dbfa BH |
5666 | trace_netif_receive_skb_entry(skb); |
5667 | ||
b0e3f1bd GB |
5668 | ret = netif_receive_skb_internal(skb); |
5669 | trace_netif_receive_skb_exit(ret); | |
5670 | ||
5671 | return ret; | |
ae78dbfa | 5672 | } |
04eb4489 | 5673 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 5674 | |
f6ad8c1b EC |
5675 | /** |
5676 | * netif_receive_skb_list - process many receive buffers from network | |
5677 | * @head: list of skbs to process. | |
5678 | * | |
7da517a3 EC |
5679 | * Since return value of netif_receive_skb() is normally ignored, and |
5680 | * wouldn't be meaningful for a list, this function returns void. | |
f6ad8c1b EC |
5681 | * |
5682 | * This function may only be called from softirq context and interrupts | |
5683 | * should be enabled. | |
5684 | */ | |
5685 | void netif_receive_skb_list(struct list_head *head) | |
5686 | { | |
7da517a3 | 5687 | struct sk_buff *skb; |
f6ad8c1b | 5688 | |
b9f463d6 EC |
5689 | if (list_empty(head)) |
5690 | return; | |
b0e3f1bd GB |
5691 | if (trace_netif_receive_skb_list_entry_enabled()) { |
5692 | list_for_each_entry(skb, head, list) | |
5693 | trace_netif_receive_skb_list_entry(skb); | |
5694 | } | |
7da517a3 | 5695 | netif_receive_skb_list_internal(head); |
b0e3f1bd | 5696 | trace_netif_receive_skb_list_exit(0); |
f6ad8c1b EC |
5697 | } |
5698 | EXPORT_SYMBOL(netif_receive_skb_list); | |
5699 | ||
ce1e2a77 | 5700 | static DEFINE_PER_CPU(struct work_struct, flush_works); |
145dd5f9 PA |
5701 | |
5702 | /* Network device is going away, flush any packets still pending */ | |
5703 | static void flush_backlog(struct work_struct *work) | |
6e583ce5 | 5704 | { |
6e583ce5 | 5705 | struct sk_buff *skb, *tmp; |
145dd5f9 PA |
5706 | struct softnet_data *sd; |
5707 | ||
5708 | local_bh_disable(); | |
5709 | sd = this_cpu_ptr(&softnet_data); | |
6e583ce5 | 5710 | |
e722db8d | 5711 | rps_lock_irq_disable(sd); |
6e7676c1 | 5712 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
41852497 | 5713 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
e36fa2f7 | 5714 | __skb_unlink(skb, &sd->input_pkt_queue); |
7df5cb75 | 5715 | dev_kfree_skb_irq(skb); |
76cc8b13 | 5716 | input_queue_head_incr(sd); |
6e583ce5 | 5717 | } |
6e7676c1 | 5718 | } |
e722db8d | 5719 | rps_unlock_irq_enable(sd); |
6e7676c1 CG |
5720 | |
5721 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
41852497 | 5722 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
6e7676c1 CG |
5723 | __skb_unlink(skb, &sd->process_queue); |
5724 | kfree_skb(skb); | |
76cc8b13 | 5725 | input_queue_head_incr(sd); |
6e7676c1 CG |
5726 | } |
5727 | } | |
145dd5f9 PA |
5728 | local_bh_enable(); |
5729 | } | |
5730 | ||
2de79ee2 PA |
5731 | static bool flush_required(int cpu) |
5732 | { | |
5733 | #if IS_ENABLED(CONFIG_RPS) | |
5734 | struct softnet_data *sd = &per_cpu(softnet_data, cpu); | |
5735 | bool do_flush; | |
5736 | ||
e722db8d | 5737 | rps_lock_irq_disable(sd); |
2de79ee2 PA |
5738 | |
5739 | /* as insertion into process_queue happens with the rps lock held, | |
5740 | * process_queue access may race only with dequeue | |
5741 | */ | |
5742 | do_flush = !skb_queue_empty(&sd->input_pkt_queue) || | |
5743 | !skb_queue_empty_lockless(&sd->process_queue); | |
e722db8d | 5744 | rps_unlock_irq_enable(sd); |
2de79ee2 PA |
5745 | |
5746 | return do_flush; | |
5747 | #endif | |
5748 | /* without RPS we can't safely check input_pkt_queue: during a | |
5749 | * concurrent remote skb_queue_splice() we can detect as empty both | |
5750 | * input_pkt_queue and process_queue even if the latter could end-up | |
5751 | * containing a lot of packets. | |
5752 | */ | |
5753 | return true; | |
5754 | } | |
5755 | ||
41852497 | 5756 | static void flush_all_backlogs(void) |
145dd5f9 | 5757 | { |
2de79ee2 | 5758 | static cpumask_t flush_cpus; |
145dd5f9 PA |
5759 | unsigned int cpu; |
5760 | ||
2de79ee2 PA |
5761 | /* since we are under rtnl lock protection we can use static data |
5762 | * for the cpumask and avoid allocating on stack the possibly | |
5763 | * large mask | |
5764 | */ | |
5765 | ASSERT_RTNL(); | |
5766 | ||
372bbdd5 | 5767 | cpus_read_lock(); |
145dd5f9 | 5768 | |
2de79ee2 PA |
5769 | cpumask_clear(&flush_cpus); |
5770 | for_each_online_cpu(cpu) { | |
5771 | if (flush_required(cpu)) { | |
5772 | queue_work_on(cpu, system_highpri_wq, | |
5773 | per_cpu_ptr(&flush_works, cpu)); | |
5774 | cpumask_set_cpu(cpu, &flush_cpus); | |
5775 | } | |
5776 | } | |
145dd5f9 | 5777 | |
2de79ee2 | 5778 | /* we can have in flight packet[s] on the cpus we are not flushing, |
0cbe1e57 | 5779 | * synchronize_net() in unregister_netdevice_many() will take care of |
2de79ee2 PA |
5780 | * them |
5781 | */ | |
5782 | for_each_cpu(cpu, &flush_cpus) | |
41852497 | 5783 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
145dd5f9 | 5784 | |
372bbdd5 | 5785 | cpus_read_unlock(); |
6e583ce5 SH |
5786 | } |
5787 | ||
773fc8f6 | 5788 | static void net_rps_send_ipi(struct softnet_data *remsd) |
5789 | { | |
5790 | #ifdef CONFIG_RPS | |
5791 | while (remsd) { | |
5792 | struct softnet_data *next = remsd->rps_ipi_next; | |
5793 | ||
5794 | if (cpu_online(remsd->cpu)) | |
5795 | smp_call_function_single_async(remsd->cpu, &remsd->csd); | |
5796 | remsd = next; | |
5797 | } | |
5798 | #endif | |
5799 | } | |
5800 | ||
e326bed2 | 5801 | /* |
855abcf0 | 5802 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
e326bed2 ED |
5803 | * Note: called with local irq disabled, but exits with local irq enabled. |
5804 | */ | |
5805 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
5806 | { | |
5807 | #ifdef CONFIG_RPS | |
5808 | struct softnet_data *remsd = sd->rps_ipi_list; | |
5809 | ||
5810 | if (remsd) { | |
5811 | sd->rps_ipi_list = NULL; | |
5812 | ||
5813 | local_irq_enable(); | |
5814 | ||
5815 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
773fc8f6 | 5816 | net_rps_send_ipi(remsd); |
e326bed2 ED |
5817 | } else |
5818 | #endif | |
5819 | local_irq_enable(); | |
5820 | } | |
5821 | ||
d75b1ade ED |
5822 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
5823 | { | |
5824 | #ifdef CONFIG_RPS | |
5825 | return sd->rps_ipi_list != NULL; | |
5826 | #else | |
5827 | return false; | |
5828 | #endif | |
5829 | } | |
5830 | ||
bea3348e | 5831 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 | 5832 | { |
eecfd7c4 | 5833 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
145dd5f9 PA |
5834 | bool again = true; |
5835 | int work = 0; | |
1da177e4 | 5836 | |
e326bed2 ED |
5837 | /* Check if we have pending ipi, its better to send them now, |
5838 | * not waiting net_rx_action() end. | |
5839 | */ | |
d75b1ade | 5840 | if (sd_has_rps_ipi_waiting(sd)) { |
e326bed2 ED |
5841 | local_irq_disable(); |
5842 | net_rps_action_and_irq_enable(sd); | |
5843 | } | |
d75b1ade | 5844 | |
3d48b53f | 5845 | napi->weight = dev_rx_weight; |
145dd5f9 | 5846 | while (again) { |
1da177e4 | 5847 | struct sk_buff *skb; |
6e7676c1 CG |
5848 | |
5849 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
2c17d27c | 5850 | rcu_read_lock(); |
6e7676c1 | 5851 | __netif_receive_skb(skb); |
2c17d27c | 5852 | rcu_read_unlock(); |
76cc8b13 | 5853 | input_queue_head_incr(sd); |
145dd5f9 | 5854 | if (++work >= quota) |
76cc8b13 | 5855 | return work; |
145dd5f9 | 5856 | |
6e7676c1 | 5857 | } |
1da177e4 | 5858 | |
e722db8d | 5859 | rps_lock_irq_disable(sd); |
11ef7a89 | 5860 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
eecfd7c4 ED |
5861 | /* |
5862 | * Inline a custom version of __napi_complete(). | |
5863 | * only current cpu owns and manipulates this napi, | |
11ef7a89 TH |
5864 | * and NAPI_STATE_SCHED is the only possible flag set |
5865 | * on backlog. | |
5866 | * We can use a plain write instead of clear_bit(), | |
eecfd7c4 ED |
5867 | * and we dont need an smp_mb() memory barrier. |
5868 | */ | |
eecfd7c4 | 5869 | napi->state = 0; |
145dd5f9 PA |
5870 | again = false; |
5871 | } else { | |
5872 | skb_queue_splice_tail_init(&sd->input_pkt_queue, | |
5873 | &sd->process_queue); | |
bea3348e | 5874 | } |
e722db8d | 5875 | rps_unlock_irq_enable(sd); |
6e7676c1 | 5876 | } |
1da177e4 | 5877 | |
bea3348e SH |
5878 | return work; |
5879 | } | |
1da177e4 | 5880 | |
bea3348e SH |
5881 | /** |
5882 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 5883 | * @n: entry to schedule |
bea3348e | 5884 | * |
bc9ad166 ED |
5885 | * The entry's receive function will be scheduled to run. |
5886 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. | |
bea3348e | 5887 | */ |
b5606c2d | 5888 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
5889 | { |
5890 | unsigned long flags; | |
1da177e4 | 5891 | |
bea3348e | 5892 | local_irq_save(flags); |
903ceff7 | 5893 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
bea3348e | 5894 | local_irq_restore(flags); |
1da177e4 | 5895 | } |
bea3348e SH |
5896 | EXPORT_SYMBOL(__napi_schedule); |
5897 | ||
39e6c820 ED |
5898 | /** |
5899 | * napi_schedule_prep - check if napi can be scheduled | |
5900 | * @n: napi context | |
5901 | * | |
5902 | * Test if NAPI routine is already running, and if not mark | |
ee1a4c84 | 5903 | * it as running. This is used as a condition variable to |
39e6c820 ED |
5904 | * insure only one NAPI poll instance runs. We also make |
5905 | * sure there is no pending NAPI disable. | |
5906 | */ | |
5907 | bool napi_schedule_prep(struct napi_struct *n) | |
5908 | { | |
5909 | unsigned long val, new; | |
5910 | ||
5911 | do { | |
5912 | val = READ_ONCE(n->state); | |
5913 | if (unlikely(val & NAPIF_STATE_DISABLE)) | |
5914 | return false; | |
5915 | new = val | NAPIF_STATE_SCHED; | |
5916 | ||
5917 | /* Sets STATE_MISSED bit if STATE_SCHED was already set | |
5918 | * This was suggested by Alexander Duyck, as compiler | |
5919 | * emits better code than : | |
5920 | * if (val & NAPIF_STATE_SCHED) | |
5921 | * new |= NAPIF_STATE_MISSED; | |
5922 | */ | |
5923 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * | |
5924 | NAPIF_STATE_MISSED; | |
5925 | } while (cmpxchg(&n->state, val, new) != val); | |
5926 | ||
5927 | return !(val & NAPIF_STATE_SCHED); | |
5928 | } | |
5929 | EXPORT_SYMBOL(napi_schedule_prep); | |
5930 | ||
bc9ad166 ED |
5931 | /** |
5932 | * __napi_schedule_irqoff - schedule for receive | |
5933 | * @n: entry to schedule | |
5934 | * | |
8380c81d SAS |
5935 | * Variant of __napi_schedule() assuming hard irqs are masked. |
5936 | * | |
5937 | * On PREEMPT_RT enabled kernels this maps to __napi_schedule() | |
5938 | * because the interrupt disabled assumption might not be true | |
5939 | * due to force-threaded interrupts and spinlock substitution. | |
bc9ad166 ED |
5940 | */ |
5941 | void __napi_schedule_irqoff(struct napi_struct *n) | |
5942 | { | |
8380c81d SAS |
5943 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
5944 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); | |
5945 | else | |
5946 | __napi_schedule(n); | |
bc9ad166 ED |
5947 | } |
5948 | EXPORT_SYMBOL(__napi_schedule_irqoff); | |
5949 | ||
364b6055 | 5950 | bool napi_complete_done(struct napi_struct *n, int work_done) |
d565b0a1 | 5951 | { |
6f8b12d6 ED |
5952 | unsigned long flags, val, new, timeout = 0; |
5953 | bool ret = true; | |
d565b0a1 HX |
5954 | |
5955 | /* | |
217f6974 ED |
5956 | * 1) Don't let napi dequeue from the cpu poll list |
5957 | * just in case its running on a different cpu. | |
5958 | * 2) If we are busy polling, do nothing here, we have | |
5959 | * the guarantee we will be called later. | |
d565b0a1 | 5960 | */ |
217f6974 ED |
5961 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
5962 | NAPIF_STATE_IN_BUSY_POLL))) | |
364b6055 | 5963 | return false; |
d565b0a1 | 5964 | |
6f8b12d6 ED |
5965 | if (work_done) { |
5966 | if (n->gro_bitmask) | |
7e417a66 ED |
5967 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
5968 | n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs); | |
6f8b12d6 ED |
5969 | } |
5970 | if (n->defer_hard_irqs_count > 0) { | |
5971 | n->defer_hard_irqs_count--; | |
7e417a66 | 5972 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6f8b12d6 ED |
5973 | if (timeout) |
5974 | ret = false; | |
5975 | } | |
5976 | if (n->gro_bitmask) { | |
605108ac PA |
5977 | /* When the NAPI instance uses a timeout and keeps postponing |
5978 | * it, we need to bound somehow the time packets are kept in | |
5979 | * the GRO layer | |
5980 | */ | |
5981 | napi_gro_flush(n, !!timeout); | |
3b47d303 | 5982 | } |
c8079432 MM |
5983 | |
5984 | gro_normal_list(n); | |
5985 | ||
02c1602e | 5986 | if (unlikely(!list_empty(&n->poll_list))) { |
d75b1ade ED |
5987 | /* If n->poll_list is not empty, we need to mask irqs */ |
5988 | local_irq_save(flags); | |
02c1602e | 5989 | list_del_init(&n->poll_list); |
d75b1ade ED |
5990 | local_irq_restore(flags); |
5991 | } | |
39e6c820 ED |
5992 | |
5993 | do { | |
5994 | val = READ_ONCE(n->state); | |
5995 | ||
5996 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); | |
5997 | ||
7fd3253a | 5998 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED | |
cb038357 | 5999 | NAPIF_STATE_SCHED_THREADED | |
7fd3253a | 6000 | NAPIF_STATE_PREFER_BUSY_POLL); |
39e6c820 ED |
6001 | |
6002 | /* If STATE_MISSED was set, leave STATE_SCHED set, | |
6003 | * because we will call napi->poll() one more time. | |
6004 | * This C code was suggested by Alexander Duyck to help gcc. | |
6005 | */ | |
6006 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * | |
6007 | NAPIF_STATE_SCHED; | |
6008 | } while (cmpxchg(&n->state, val, new) != val); | |
6009 | ||
6010 | if (unlikely(val & NAPIF_STATE_MISSED)) { | |
6011 | __napi_schedule(n); | |
6012 | return false; | |
6013 | } | |
6014 | ||
6f8b12d6 ED |
6015 | if (timeout) |
6016 | hrtimer_start(&n->timer, ns_to_ktime(timeout), | |
6017 | HRTIMER_MODE_REL_PINNED); | |
6018 | return ret; | |
d565b0a1 | 6019 | } |
3b47d303 | 6020 | EXPORT_SYMBOL(napi_complete_done); |
d565b0a1 | 6021 | |
af12fa6e | 6022 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
02d62e86 | 6023 | static struct napi_struct *napi_by_id(unsigned int napi_id) |
af12fa6e ET |
6024 | { |
6025 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); | |
6026 | struct napi_struct *napi; | |
6027 | ||
6028 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) | |
6029 | if (napi->napi_id == napi_id) | |
6030 | return napi; | |
6031 | ||
6032 | return NULL; | |
6033 | } | |
02d62e86 ED |
6034 | |
6035 | #if defined(CONFIG_NET_RX_BUSY_POLL) | |
217f6974 | 6036 | |
7fd3253a | 6037 | static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule) |
217f6974 | 6038 | { |
7fd3253a BT |
6039 | if (!skip_schedule) { |
6040 | gro_normal_list(napi); | |
6041 | __napi_schedule(napi); | |
6042 | return; | |
6043 | } | |
217f6974 | 6044 | |
7fd3253a BT |
6045 | if (napi->gro_bitmask) { |
6046 | /* flush too old packets | |
6047 | * If HZ < 1000, flush all packets. | |
6048 | */ | |
6049 | napi_gro_flush(napi, HZ >= 1000); | |
6050 | } | |
217f6974 | 6051 | |
7fd3253a BT |
6052 | gro_normal_list(napi); |
6053 | clear_bit(NAPI_STATE_SCHED, &napi->state); | |
6054 | } | |
6055 | ||
7c951caf BT |
6056 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll, |
6057 | u16 budget) | |
217f6974 | 6058 | { |
7fd3253a BT |
6059 | bool skip_schedule = false; |
6060 | unsigned long timeout; | |
217f6974 ED |
6061 | int rc; |
6062 | ||
39e6c820 ED |
6063 | /* Busy polling means there is a high chance device driver hard irq |
6064 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was | |
6065 | * set in napi_schedule_prep(). | |
6066 | * Since we are about to call napi->poll() once more, we can safely | |
6067 | * clear NAPI_STATE_MISSED. | |
6068 | * | |
6069 | * Note: x86 could use a single "lock and ..." instruction | |
6070 | * to perform these two clear_bit() | |
6071 | */ | |
6072 | clear_bit(NAPI_STATE_MISSED, &napi->state); | |
217f6974 ED |
6073 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
6074 | ||
6075 | local_bh_disable(); | |
6076 | ||
7fd3253a BT |
6077 | if (prefer_busy_poll) { |
6078 | napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs); | |
6079 | timeout = READ_ONCE(napi->dev->gro_flush_timeout); | |
6080 | if (napi->defer_hard_irqs_count && timeout) { | |
6081 | hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED); | |
6082 | skip_schedule = true; | |
6083 | } | |
6084 | } | |
6085 | ||
217f6974 ED |
6086 | /* All we really want here is to re-enable device interrupts. |
6087 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. | |
6088 | */ | |
7c951caf | 6089 | rc = napi->poll(napi, budget); |
323ebb61 EC |
6090 | /* We can't gro_normal_list() here, because napi->poll() might have |
6091 | * rearmed the napi (napi_complete_done()) in which case it could | |
6092 | * already be running on another CPU. | |
6093 | */ | |
7c951caf | 6094 | trace_napi_poll(napi, rc, budget); |
217f6974 | 6095 | netpoll_poll_unlock(have_poll_lock); |
7c951caf | 6096 | if (rc == budget) |
7fd3253a | 6097 | __busy_poll_stop(napi, skip_schedule); |
217f6974 | 6098 | local_bh_enable(); |
217f6974 ED |
6099 | } |
6100 | ||
7db6b048 SS |
6101 | void napi_busy_loop(unsigned int napi_id, |
6102 | bool (*loop_end)(void *, unsigned long), | |
7c951caf | 6103 | void *loop_end_arg, bool prefer_busy_poll, u16 budget) |
02d62e86 | 6104 | { |
7db6b048 | 6105 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
217f6974 | 6106 | int (*napi_poll)(struct napi_struct *napi, int budget); |
217f6974 | 6107 | void *have_poll_lock = NULL; |
02d62e86 | 6108 | struct napi_struct *napi; |
217f6974 ED |
6109 | |
6110 | restart: | |
217f6974 | 6111 | napi_poll = NULL; |
02d62e86 | 6112 | |
2a028ecb | 6113 | rcu_read_lock(); |
02d62e86 | 6114 | |
545cd5e5 | 6115 | napi = napi_by_id(napi_id); |
02d62e86 ED |
6116 | if (!napi) |
6117 | goto out; | |
6118 | ||
217f6974 ED |
6119 | preempt_disable(); |
6120 | for (;;) { | |
2b5cd0df AD |
6121 | int work = 0; |
6122 | ||
2a028ecb | 6123 | local_bh_disable(); |
217f6974 ED |
6124 | if (!napi_poll) { |
6125 | unsigned long val = READ_ONCE(napi->state); | |
6126 | ||
6127 | /* If multiple threads are competing for this napi, | |
6128 | * we avoid dirtying napi->state as much as we can. | |
6129 | */ | |
6130 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | | |
7fd3253a BT |
6131 | NAPIF_STATE_IN_BUSY_POLL)) { |
6132 | if (prefer_busy_poll) | |
6133 | set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
217f6974 | 6134 | goto count; |
7fd3253a | 6135 | } |
217f6974 ED |
6136 | if (cmpxchg(&napi->state, val, |
6137 | val | NAPIF_STATE_IN_BUSY_POLL | | |
7fd3253a BT |
6138 | NAPIF_STATE_SCHED) != val) { |
6139 | if (prefer_busy_poll) | |
6140 | set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
217f6974 | 6141 | goto count; |
7fd3253a | 6142 | } |
217f6974 ED |
6143 | have_poll_lock = netpoll_poll_lock(napi); |
6144 | napi_poll = napi->poll; | |
6145 | } | |
7c951caf BT |
6146 | work = napi_poll(napi, budget); |
6147 | trace_napi_poll(napi, work, budget); | |
323ebb61 | 6148 | gro_normal_list(napi); |
217f6974 | 6149 | count: |
2b5cd0df | 6150 | if (work > 0) |
7db6b048 | 6151 | __NET_ADD_STATS(dev_net(napi->dev), |
2b5cd0df | 6152 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
2a028ecb | 6153 | local_bh_enable(); |
02d62e86 | 6154 | |
7db6b048 | 6155 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
217f6974 | 6156 | break; |
02d62e86 | 6157 | |
217f6974 ED |
6158 | if (unlikely(need_resched())) { |
6159 | if (napi_poll) | |
7c951caf | 6160 | busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget); |
217f6974 ED |
6161 | preempt_enable(); |
6162 | rcu_read_unlock(); | |
6163 | cond_resched(); | |
7db6b048 | 6164 | if (loop_end(loop_end_arg, start_time)) |
2b5cd0df | 6165 | return; |
217f6974 ED |
6166 | goto restart; |
6167 | } | |
6cdf89b1 | 6168 | cpu_relax(); |
217f6974 ED |
6169 | } |
6170 | if (napi_poll) | |
7c951caf | 6171 | busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget); |
217f6974 | 6172 | preempt_enable(); |
02d62e86 | 6173 | out: |
2a028ecb | 6174 | rcu_read_unlock(); |
02d62e86 | 6175 | } |
7db6b048 | 6176 | EXPORT_SYMBOL(napi_busy_loop); |
02d62e86 ED |
6177 | |
6178 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
af12fa6e | 6179 | |
149d6ad8 | 6180 | static void napi_hash_add(struct napi_struct *napi) |
af12fa6e | 6181 | { |
4d092dd2 | 6182 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state)) |
52bd2d62 | 6183 | return; |
af12fa6e | 6184 | |
52bd2d62 | 6185 | spin_lock(&napi_hash_lock); |
af12fa6e | 6186 | |
545cd5e5 | 6187 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
52bd2d62 | 6188 | do { |
545cd5e5 AD |
6189 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
6190 | napi_gen_id = MIN_NAPI_ID; | |
52bd2d62 ED |
6191 | } while (napi_by_id(napi_gen_id)); |
6192 | napi->napi_id = napi_gen_id; | |
af12fa6e | 6193 | |
52bd2d62 ED |
6194 | hlist_add_head_rcu(&napi->napi_hash_node, |
6195 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); | |
af12fa6e | 6196 | |
52bd2d62 | 6197 | spin_unlock(&napi_hash_lock); |
af12fa6e | 6198 | } |
af12fa6e ET |
6199 | |
6200 | /* Warning : caller is responsible to make sure rcu grace period | |
6201 | * is respected before freeing memory containing @napi | |
6202 | */ | |
5198d545 | 6203 | static void napi_hash_del(struct napi_struct *napi) |
af12fa6e ET |
6204 | { |
6205 | spin_lock(&napi_hash_lock); | |
6206 | ||
4d092dd2 | 6207 | hlist_del_init_rcu(&napi->napi_hash_node); |
5198d545 | 6208 | |
af12fa6e ET |
6209 | spin_unlock(&napi_hash_lock); |
6210 | } | |
af12fa6e | 6211 | |
3b47d303 ED |
6212 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
6213 | { | |
6214 | struct napi_struct *napi; | |
6215 | ||
6216 | napi = container_of(timer, struct napi_struct, timer); | |
39e6c820 ED |
6217 | |
6218 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting | |
6219 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. | |
6220 | */ | |
6f8b12d6 | 6221 | if (!napi_disable_pending(napi) && |
7fd3253a BT |
6222 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) { |
6223 | clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
39e6c820 | 6224 | __napi_schedule_irqoff(napi); |
7fd3253a | 6225 | } |
3b47d303 ED |
6226 | |
6227 | return HRTIMER_NORESTART; | |
6228 | } | |
6229 | ||
7c4ec749 | 6230 | static void init_gro_hash(struct napi_struct *napi) |
d565b0a1 | 6231 | { |
07d78363 DM |
6232 | int i; |
6233 | ||
6312fe77 LR |
6234 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6235 | INIT_LIST_HEAD(&napi->gro_hash[i].list); | |
6236 | napi->gro_hash[i].count = 0; | |
6237 | } | |
7c4ec749 DM |
6238 | napi->gro_bitmask = 0; |
6239 | } | |
6240 | ||
5fdd2f0e WW |
6241 | int dev_set_threaded(struct net_device *dev, bool threaded) |
6242 | { | |
6243 | struct napi_struct *napi; | |
6244 | int err = 0; | |
6245 | ||
6246 | if (dev->threaded == threaded) | |
6247 | return 0; | |
6248 | ||
6249 | if (threaded) { | |
6250 | list_for_each_entry(napi, &dev->napi_list, dev_list) { | |
6251 | if (!napi->thread) { | |
6252 | err = napi_kthread_create(napi); | |
6253 | if (err) { | |
6254 | threaded = false; | |
6255 | break; | |
6256 | } | |
6257 | } | |
6258 | } | |
6259 | } | |
6260 | ||
6261 | dev->threaded = threaded; | |
6262 | ||
6263 | /* Make sure kthread is created before THREADED bit | |
6264 | * is set. | |
6265 | */ | |
6266 | smp_mb__before_atomic(); | |
6267 | ||
6268 | /* Setting/unsetting threaded mode on a napi might not immediately | |
6269 | * take effect, if the current napi instance is actively being | |
6270 | * polled. In this case, the switch between threaded mode and | |
6271 | * softirq mode will happen in the next round of napi_schedule(). | |
6272 | * This should not cause hiccups/stalls to the live traffic. | |
6273 | */ | |
6274 | list_for_each_entry(napi, &dev->napi_list, dev_list) { | |
6275 | if (threaded) | |
6276 | set_bit(NAPI_STATE_THREADED, &napi->state); | |
6277 | else | |
6278 | clear_bit(NAPI_STATE_THREADED, &napi->state); | |
6279 | } | |
6280 | ||
6281 | return err; | |
6282 | } | |
8f64860f | 6283 | EXPORT_SYMBOL(dev_set_threaded); |
5fdd2f0e | 6284 | |
7c4ec749 DM |
6285 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, |
6286 | int (*poll)(struct napi_struct *, int), int weight) | |
6287 | { | |
4d092dd2 JK |
6288 | if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state))) |
6289 | return; | |
6290 | ||
7c4ec749 | 6291 | INIT_LIST_HEAD(&napi->poll_list); |
4d092dd2 | 6292 | INIT_HLIST_NODE(&napi->napi_hash_node); |
7c4ec749 DM |
6293 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
6294 | napi->timer.function = napi_watchdog; | |
6295 | init_gro_hash(napi); | |
5d38a079 | 6296 | napi->skb = NULL; |
323ebb61 EC |
6297 | INIT_LIST_HEAD(&napi->rx_list); |
6298 | napi->rx_count = 0; | |
d565b0a1 | 6299 | napi->poll = poll; |
82dc3c63 | 6300 | if (weight > NAPI_POLL_WEIGHT) |
bf29e9e9 QC |
6301 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
6302 | weight); | |
d565b0a1 | 6303 | napi->weight = weight; |
d565b0a1 | 6304 | napi->dev = dev; |
5d38a079 | 6305 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
6306 | napi->poll_owner = -1; |
6307 | #endif | |
6308 | set_bit(NAPI_STATE_SCHED, &napi->state); | |
96e97bc0 JK |
6309 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
6310 | list_add_rcu(&napi->dev_list, &dev->napi_list); | |
93d05d4a | 6311 | napi_hash_add(napi); |
29863d41 WW |
6312 | /* Create kthread for this napi if dev->threaded is set. |
6313 | * Clear dev->threaded if kthread creation failed so that | |
6314 | * threaded mode will not be enabled in napi_enable(). | |
6315 | */ | |
6316 | if (dev->threaded && napi_kthread_create(napi)) | |
6317 | dev->threaded = 0; | |
d565b0a1 HX |
6318 | } |
6319 | EXPORT_SYMBOL(netif_napi_add); | |
6320 | ||
3b47d303 ED |
6321 | void napi_disable(struct napi_struct *n) |
6322 | { | |
719c5719 JK |
6323 | unsigned long val, new; |
6324 | ||
3b47d303 ED |
6325 | might_sleep(); |
6326 | set_bit(NAPI_STATE_DISABLE, &n->state); | |
6327 | ||
0315a075 | 6328 | for ( ; ; ) { |
719c5719 JK |
6329 | val = READ_ONCE(n->state); |
6330 | if (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) { | |
6331 | usleep_range(20, 200); | |
6332 | continue; | |
6333 | } | |
6334 | ||
6335 | new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC; | |
6336 | new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL); | |
0315a075 AL |
6337 | |
6338 | if (cmpxchg(&n->state, val, new) == val) | |
6339 | break; | |
6340 | } | |
3b47d303 ED |
6341 | |
6342 | hrtimer_cancel(&n->timer); | |
6343 | ||
6344 | clear_bit(NAPI_STATE_DISABLE, &n->state); | |
6345 | } | |
6346 | EXPORT_SYMBOL(napi_disable); | |
6347 | ||
29863d41 WW |
6348 | /** |
6349 | * napi_enable - enable NAPI scheduling | |
6350 | * @n: NAPI context | |
6351 | * | |
6352 | * Resume NAPI from being scheduled on this context. | |
6353 | * Must be paired with napi_disable. | |
6354 | */ | |
6355 | void napi_enable(struct napi_struct *n) | |
6356 | { | |
3765996e XZ |
6357 | unsigned long val, new; |
6358 | ||
6359 | do { | |
6360 | val = READ_ONCE(n->state); | |
6361 | BUG_ON(!test_bit(NAPI_STATE_SCHED, &val)); | |
6362 | ||
6363 | new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC); | |
6364 | if (n->dev->threaded && n->thread) | |
6365 | new |= NAPIF_STATE_THREADED; | |
6366 | } while (cmpxchg(&n->state, val, new) != val); | |
29863d41 WW |
6367 | } |
6368 | EXPORT_SYMBOL(napi_enable); | |
6369 | ||
07d78363 | 6370 | static void flush_gro_hash(struct napi_struct *napi) |
d4546c25 | 6371 | { |
07d78363 | 6372 | int i; |
d4546c25 | 6373 | |
07d78363 DM |
6374 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6375 | struct sk_buff *skb, *n; | |
6376 | ||
6312fe77 | 6377 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
07d78363 | 6378 | kfree_skb(skb); |
6312fe77 | 6379 | napi->gro_hash[i].count = 0; |
07d78363 | 6380 | } |
d4546c25 DM |
6381 | } |
6382 | ||
93d05d4a | 6383 | /* Must be called in process context */ |
5198d545 | 6384 | void __netif_napi_del(struct napi_struct *napi) |
d565b0a1 | 6385 | { |
4d092dd2 JK |
6386 | if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state)) |
6387 | return; | |
6388 | ||
5198d545 | 6389 | napi_hash_del(napi); |
5251ef82 | 6390 | list_del_rcu(&napi->dev_list); |
76620aaf | 6391 | napi_free_frags(napi); |
d565b0a1 | 6392 | |
07d78363 | 6393 | flush_gro_hash(napi); |
d9f37d01 | 6394 | napi->gro_bitmask = 0; |
29863d41 WW |
6395 | |
6396 | if (napi->thread) { | |
6397 | kthread_stop(napi->thread); | |
6398 | napi->thread = NULL; | |
6399 | } | |
d565b0a1 | 6400 | } |
5198d545 | 6401 | EXPORT_SYMBOL(__netif_napi_del); |
d565b0a1 | 6402 | |
898f8015 | 6403 | static int __napi_poll(struct napi_struct *n, bool *repoll) |
726ce70e | 6404 | { |
726ce70e HX |
6405 | int work, weight; |
6406 | ||
726ce70e HX |
6407 | weight = n->weight; |
6408 | ||
6409 | /* This NAPI_STATE_SCHED test is for avoiding a race | |
6410 | * with netpoll's poll_napi(). Only the entity which | |
6411 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
6412 | * actually make the ->poll() call. Therefore we avoid | |
6413 | * accidentally calling ->poll() when NAPI is not scheduled. | |
6414 | */ | |
6415 | work = 0; | |
6416 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { | |
6417 | work = n->poll(n, weight); | |
1db19db7 | 6418 | trace_napi_poll(n, work, weight); |
726ce70e HX |
6419 | } |
6420 | ||
427d5838 | 6421 | if (unlikely(work > weight)) |
5b92be64 JB |
6422 | netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n", |
6423 | n->poll, work, weight); | |
726ce70e HX |
6424 | |
6425 | if (likely(work < weight)) | |
898f8015 | 6426 | return work; |
726ce70e HX |
6427 | |
6428 | /* Drivers must not modify the NAPI state if they | |
6429 | * consume the entire weight. In such cases this code | |
6430 | * still "owns" the NAPI instance and therefore can | |
6431 | * move the instance around on the list at-will. | |
6432 | */ | |
6433 | if (unlikely(napi_disable_pending(n))) { | |
6434 | napi_complete(n); | |
898f8015 | 6435 | return work; |
726ce70e HX |
6436 | } |
6437 | ||
7fd3253a BT |
6438 | /* The NAPI context has more processing work, but busy-polling |
6439 | * is preferred. Exit early. | |
6440 | */ | |
6441 | if (napi_prefer_busy_poll(n)) { | |
6442 | if (napi_complete_done(n, work)) { | |
6443 | /* If timeout is not set, we need to make sure | |
6444 | * that the NAPI is re-scheduled. | |
6445 | */ | |
6446 | napi_schedule(n); | |
6447 | } | |
898f8015 | 6448 | return work; |
7fd3253a BT |
6449 | } |
6450 | ||
d9f37d01 | 6451 | if (n->gro_bitmask) { |
726ce70e HX |
6452 | /* flush too old packets |
6453 | * If HZ < 1000, flush all packets. | |
6454 | */ | |
6455 | napi_gro_flush(n, HZ >= 1000); | |
6456 | } | |
6457 | ||
c8079432 MM |
6458 | gro_normal_list(n); |
6459 | ||
001ce546 HX |
6460 | /* Some drivers may have called napi_schedule |
6461 | * prior to exhausting their budget. | |
6462 | */ | |
6463 | if (unlikely(!list_empty(&n->poll_list))) { | |
6464 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", | |
6465 | n->dev ? n->dev->name : "backlog"); | |
898f8015 | 6466 | return work; |
001ce546 HX |
6467 | } |
6468 | ||
898f8015 FF |
6469 | *repoll = true; |
6470 | ||
6471 | return work; | |
6472 | } | |
6473 | ||
6474 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) | |
6475 | { | |
6476 | bool do_repoll = false; | |
6477 | void *have; | |
6478 | int work; | |
6479 | ||
6480 | list_del_init(&n->poll_list); | |
6481 | ||
6482 | have = netpoll_poll_lock(n); | |
6483 | ||
6484 | work = __napi_poll(n, &do_repoll); | |
6485 | ||
6486 | if (do_repoll) | |
6487 | list_add_tail(&n->poll_list, repoll); | |
726ce70e | 6488 | |
726ce70e HX |
6489 | netpoll_poll_unlock(have); |
6490 | ||
6491 | return work; | |
6492 | } | |
6493 | ||
29863d41 WW |
6494 | static int napi_thread_wait(struct napi_struct *napi) |
6495 | { | |
cb038357 WW |
6496 | bool woken = false; |
6497 | ||
29863d41 WW |
6498 | set_current_state(TASK_INTERRUPTIBLE); |
6499 | ||
27f0ad71 | 6500 | while (!kthread_should_stop()) { |
cb038357 WW |
6501 | /* Testing SCHED_THREADED bit here to make sure the current |
6502 | * kthread owns this napi and could poll on this napi. | |
6503 | * Testing SCHED bit is not enough because SCHED bit might be | |
6504 | * set by some other busy poll thread or by napi_disable(). | |
6505 | */ | |
6506 | if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) { | |
29863d41 WW |
6507 | WARN_ON(!list_empty(&napi->poll_list)); |
6508 | __set_current_state(TASK_RUNNING); | |
6509 | return 0; | |
6510 | } | |
6511 | ||
6512 | schedule(); | |
cb038357 WW |
6513 | /* woken being true indicates this thread owns this napi. */ |
6514 | woken = true; | |
29863d41 WW |
6515 | set_current_state(TASK_INTERRUPTIBLE); |
6516 | } | |
6517 | __set_current_state(TASK_RUNNING); | |
27f0ad71 | 6518 | |
29863d41 WW |
6519 | return -1; |
6520 | } | |
6521 | ||
6522 | static int napi_threaded_poll(void *data) | |
6523 | { | |
6524 | struct napi_struct *napi = data; | |
6525 | void *have; | |
6526 | ||
6527 | while (!napi_thread_wait(napi)) { | |
6528 | for (;;) { | |
6529 | bool repoll = false; | |
6530 | ||
6531 | local_bh_disable(); | |
6532 | ||
6533 | have = netpoll_poll_lock(napi); | |
6534 | __napi_poll(napi, &repoll); | |
6535 | netpoll_poll_unlock(have); | |
6536 | ||
29863d41 WW |
6537 | local_bh_enable(); |
6538 | ||
6539 | if (!repoll) | |
6540 | break; | |
6541 | ||
6542 | cond_resched(); | |
6543 | } | |
6544 | } | |
6545 | return 0; | |
6546 | } | |
6547 | ||
0766f788 | 6548 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
1da177e4 | 6549 | { |
903ceff7 | 6550 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
7acf8a1e MW |
6551 | unsigned long time_limit = jiffies + |
6552 | usecs_to_jiffies(netdev_budget_usecs); | |
51b0bded | 6553 | int budget = netdev_budget; |
d75b1ade ED |
6554 | LIST_HEAD(list); |
6555 | LIST_HEAD(repoll); | |
53fb95d3 | 6556 | |
1da177e4 | 6557 | local_irq_disable(); |
d75b1ade ED |
6558 | list_splice_init(&sd->poll_list, &list); |
6559 | local_irq_enable(); | |
1da177e4 | 6560 | |
ceb8d5bf | 6561 | for (;;) { |
bea3348e | 6562 | struct napi_struct *n; |
1da177e4 | 6563 | |
ceb8d5bf HX |
6564 | if (list_empty(&list)) { |
6565 | if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll)) | |
fec6e49b | 6566 | return; |
ceb8d5bf HX |
6567 | break; |
6568 | } | |
6569 | ||
6bd373eb HX |
6570 | n = list_first_entry(&list, struct napi_struct, poll_list); |
6571 | budget -= napi_poll(n, &repoll); | |
6572 | ||
d75b1ade | 6573 | /* If softirq window is exhausted then punt. |
24f8b238 SH |
6574 | * Allow this to run for 2 jiffies since which will allow |
6575 | * an average latency of 1.5/HZ. | |
bea3348e | 6576 | */ |
ceb8d5bf HX |
6577 | if (unlikely(budget <= 0 || |
6578 | time_after_eq(jiffies, time_limit))) { | |
6579 | sd->time_squeeze++; | |
6580 | break; | |
6581 | } | |
1da177e4 | 6582 | } |
d75b1ade | 6583 | |
d75b1ade ED |
6584 | local_irq_disable(); |
6585 | ||
6586 | list_splice_tail_init(&sd->poll_list, &list); | |
6587 | list_splice_tail(&repoll, &list); | |
6588 | list_splice(&list, &sd->poll_list); | |
6589 | if (!list_empty(&sd->poll_list)) | |
6590 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
6591 | ||
e326bed2 | 6592 | net_rps_action_and_irq_enable(sd); |
1da177e4 LT |
6593 | } |
6594 | ||
aa9d8560 | 6595 | struct netdev_adjacent { |
9ff162a8 | 6596 | struct net_device *dev; |
f77159a3 | 6597 | netdevice_tracker dev_tracker; |
5d261913 VF |
6598 | |
6599 | /* upper master flag, there can only be one master device per list */ | |
9ff162a8 | 6600 | bool master; |
5d261913 | 6601 | |
32b6d34f TY |
6602 | /* lookup ignore flag */ |
6603 | bool ignore; | |
6604 | ||
5d261913 VF |
6605 | /* counter for the number of times this device was added to us */ |
6606 | u16 ref_nr; | |
6607 | ||
402dae96 VF |
6608 | /* private field for the users */ |
6609 | void *private; | |
6610 | ||
9ff162a8 JP |
6611 | struct list_head list; |
6612 | struct rcu_head rcu; | |
9ff162a8 JP |
6613 | }; |
6614 | ||
6ea29da1 | 6615 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
2f268f12 | 6616 | struct list_head *adj_list) |
9ff162a8 | 6617 | { |
5d261913 | 6618 | struct netdev_adjacent *adj; |
5d261913 | 6619 | |
2f268f12 | 6620 | list_for_each_entry(adj, adj_list, list) { |
5d261913 VF |
6621 | if (adj->dev == adj_dev) |
6622 | return adj; | |
9ff162a8 JP |
6623 | } |
6624 | return NULL; | |
6625 | } | |
6626 | ||
eff74233 TY |
6627 | static int ____netdev_has_upper_dev(struct net_device *upper_dev, |
6628 | struct netdev_nested_priv *priv) | |
f1170fd4 | 6629 | { |
eff74233 | 6630 | struct net_device *dev = (struct net_device *)priv->data; |
f1170fd4 DA |
6631 | |
6632 | return upper_dev == dev; | |
6633 | } | |
6634 | ||
9ff162a8 JP |
6635 | /** |
6636 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
6637 | * @dev: device | |
6638 | * @upper_dev: upper device to check | |
6639 | * | |
6640 | * Find out if a device is linked to specified upper device and return true | |
6641 | * in case it is. Note that this checks only immediate upper device, | |
6642 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
6643 | */ | |
6644 | bool netdev_has_upper_dev(struct net_device *dev, | |
6645 | struct net_device *upper_dev) | |
6646 | { | |
eff74233 TY |
6647 | struct netdev_nested_priv priv = { |
6648 | .data = (void *)upper_dev, | |
6649 | }; | |
6650 | ||
9ff162a8 JP |
6651 | ASSERT_RTNL(); |
6652 | ||
32b6d34f | 6653 | return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
eff74233 | 6654 | &priv); |
9ff162a8 JP |
6655 | } |
6656 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
6657 | ||
1a3f060c | 6658 | /** |
c1639be9 | 6659 | * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device |
1a3f060c DA |
6660 | * @dev: device |
6661 | * @upper_dev: upper device to check | |
6662 | * | |
6663 | * Find out if a device is linked to specified upper device and return true | |
6664 | * in case it is. Note that this checks the entire upper device chain. | |
6665 | * The caller must hold rcu lock. | |
6666 | */ | |
6667 | ||
1a3f060c DA |
6668 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
6669 | struct net_device *upper_dev) | |
6670 | { | |
eff74233 TY |
6671 | struct netdev_nested_priv priv = { |
6672 | .data = (void *)upper_dev, | |
6673 | }; | |
6674 | ||
32b6d34f | 6675 | return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
eff74233 | 6676 | &priv); |
1a3f060c DA |
6677 | } |
6678 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); | |
6679 | ||
9ff162a8 JP |
6680 | /** |
6681 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
6682 | * @dev: device | |
6683 | * | |
6684 | * Find out if a device is linked to an upper device and return true in case | |
6685 | * it is. The caller must hold the RTNL lock. | |
6686 | */ | |
25cc72a3 | 6687 | bool netdev_has_any_upper_dev(struct net_device *dev) |
9ff162a8 JP |
6688 | { |
6689 | ASSERT_RTNL(); | |
6690 | ||
f1170fd4 | 6691 | return !list_empty(&dev->adj_list.upper); |
9ff162a8 | 6692 | } |
25cc72a3 | 6693 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
9ff162a8 JP |
6694 | |
6695 | /** | |
6696 | * netdev_master_upper_dev_get - Get master upper device | |
6697 | * @dev: device | |
6698 | * | |
6699 | * Find a master upper device and return pointer to it or NULL in case | |
6700 | * it's not there. The caller must hold the RTNL lock. | |
6701 | */ | |
6702 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
6703 | { | |
aa9d8560 | 6704 | struct netdev_adjacent *upper; |
9ff162a8 JP |
6705 | |
6706 | ASSERT_RTNL(); | |
6707 | ||
2f268f12 | 6708 | if (list_empty(&dev->adj_list.upper)) |
9ff162a8 JP |
6709 | return NULL; |
6710 | ||
2f268f12 | 6711 | upper = list_first_entry(&dev->adj_list.upper, |
aa9d8560 | 6712 | struct netdev_adjacent, list); |
9ff162a8 JP |
6713 | if (likely(upper->master)) |
6714 | return upper->dev; | |
6715 | return NULL; | |
6716 | } | |
6717 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
6718 | ||
32b6d34f TY |
6719 | static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) |
6720 | { | |
6721 | struct netdev_adjacent *upper; | |
6722 | ||
6723 | ASSERT_RTNL(); | |
6724 | ||
6725 | if (list_empty(&dev->adj_list.upper)) | |
6726 | return NULL; | |
6727 | ||
6728 | upper = list_first_entry(&dev->adj_list.upper, | |
6729 | struct netdev_adjacent, list); | |
6730 | if (likely(upper->master) && !upper->ignore) | |
6731 | return upper->dev; | |
6732 | return NULL; | |
6733 | } | |
6734 | ||
0f524a80 DA |
6735 | /** |
6736 | * netdev_has_any_lower_dev - Check if device is linked to some device | |
6737 | * @dev: device | |
6738 | * | |
6739 | * Find out if a device is linked to a lower device and return true in case | |
6740 | * it is. The caller must hold the RTNL lock. | |
6741 | */ | |
6742 | static bool netdev_has_any_lower_dev(struct net_device *dev) | |
6743 | { | |
6744 | ASSERT_RTNL(); | |
6745 | ||
6746 | return !list_empty(&dev->adj_list.lower); | |
6747 | } | |
6748 | ||
b6ccba4c VF |
6749 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
6750 | { | |
6751 | struct netdev_adjacent *adj; | |
6752 | ||
6753 | adj = list_entry(adj_list, struct netdev_adjacent, list); | |
6754 | ||
6755 | return adj->private; | |
6756 | } | |
6757 | EXPORT_SYMBOL(netdev_adjacent_get_private); | |
6758 | ||
44a40855 VY |
6759 | /** |
6760 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list | |
6761 | * @dev: device | |
6762 | * @iter: list_head ** of the current position | |
6763 | * | |
6764 | * Gets the next device from the dev's upper list, starting from iter | |
6765 | * position. The caller must hold RCU read lock. | |
6766 | */ | |
6767 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, | |
6768 | struct list_head **iter) | |
6769 | { | |
6770 | struct netdev_adjacent *upper; | |
6771 | ||
6772 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6773 | ||
6774 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6775 | ||
6776 | if (&upper->list == &dev->adj_list.upper) | |
6777 | return NULL; | |
6778 | ||
6779 | *iter = &upper->list; | |
6780 | ||
6781 | return upper->dev; | |
6782 | } | |
6783 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); | |
6784 | ||
32b6d34f TY |
6785 | static struct net_device *__netdev_next_upper_dev(struct net_device *dev, |
6786 | struct list_head **iter, | |
6787 | bool *ignore) | |
5343da4c TY |
6788 | { |
6789 | struct netdev_adjacent *upper; | |
6790 | ||
6791 | upper = list_entry((*iter)->next, struct netdev_adjacent, list); | |
6792 | ||
6793 | if (&upper->list == &dev->adj_list.upper) | |
6794 | return NULL; | |
6795 | ||
6796 | *iter = &upper->list; | |
32b6d34f | 6797 | *ignore = upper->ignore; |
5343da4c TY |
6798 | |
6799 | return upper->dev; | |
6800 | } | |
6801 | ||
1a3f060c DA |
6802 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
6803 | struct list_head **iter) | |
6804 | { | |
6805 | struct netdev_adjacent *upper; | |
6806 | ||
6807 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6808 | ||
6809 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6810 | ||
6811 | if (&upper->list == &dev->adj_list.upper) | |
6812 | return NULL; | |
6813 | ||
6814 | *iter = &upper->list; | |
6815 | ||
6816 | return upper->dev; | |
6817 | } | |
6818 | ||
32b6d34f TY |
6819 | static int __netdev_walk_all_upper_dev(struct net_device *dev, |
6820 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
6821 | struct netdev_nested_priv *priv), |
6822 | struct netdev_nested_priv *priv) | |
5343da4c TY |
6823 | { |
6824 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
6825 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
6826 | int ret, cur = 0; | |
32b6d34f | 6827 | bool ignore; |
5343da4c TY |
6828 | |
6829 | now = dev; | |
6830 | iter = &dev->adj_list.upper; | |
6831 | ||
6832 | while (1) { | |
6833 | if (now != dev) { | |
eff74233 | 6834 | ret = fn(now, priv); |
5343da4c TY |
6835 | if (ret) |
6836 | return ret; | |
6837 | } | |
6838 | ||
6839 | next = NULL; | |
6840 | while (1) { | |
32b6d34f | 6841 | udev = __netdev_next_upper_dev(now, &iter, &ignore); |
5343da4c TY |
6842 | if (!udev) |
6843 | break; | |
32b6d34f TY |
6844 | if (ignore) |
6845 | continue; | |
5343da4c TY |
6846 | |
6847 | next = udev; | |
6848 | niter = &udev->adj_list.upper; | |
6849 | dev_stack[cur] = now; | |
6850 | iter_stack[cur++] = iter; | |
6851 | break; | |
6852 | } | |
6853 | ||
6854 | if (!next) { | |
6855 | if (!cur) | |
6856 | return 0; | |
6857 | next = dev_stack[--cur]; | |
6858 | niter = iter_stack[cur]; | |
6859 | } | |
6860 | ||
6861 | now = next; | |
6862 | iter = niter; | |
6863 | } | |
6864 | ||
6865 | return 0; | |
6866 | } | |
6867 | ||
1a3f060c DA |
6868 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, |
6869 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
6870 | struct netdev_nested_priv *priv), |
6871 | struct netdev_nested_priv *priv) | |
1a3f060c | 6872 | { |
5343da4c TY |
6873 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
6874 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
6875 | int ret, cur = 0; | |
1a3f060c | 6876 | |
5343da4c TY |
6877 | now = dev; |
6878 | iter = &dev->adj_list.upper; | |
1a3f060c | 6879 | |
5343da4c TY |
6880 | while (1) { |
6881 | if (now != dev) { | |
eff74233 | 6882 | ret = fn(now, priv); |
5343da4c TY |
6883 | if (ret) |
6884 | return ret; | |
6885 | } | |
6886 | ||
6887 | next = NULL; | |
6888 | while (1) { | |
6889 | udev = netdev_next_upper_dev_rcu(now, &iter); | |
6890 | if (!udev) | |
6891 | break; | |
6892 | ||
6893 | next = udev; | |
6894 | niter = &udev->adj_list.upper; | |
6895 | dev_stack[cur] = now; | |
6896 | iter_stack[cur++] = iter; | |
6897 | break; | |
6898 | } | |
6899 | ||
6900 | if (!next) { | |
6901 | if (!cur) | |
6902 | return 0; | |
6903 | next = dev_stack[--cur]; | |
6904 | niter = iter_stack[cur]; | |
6905 | } | |
6906 | ||
6907 | now = next; | |
6908 | iter = niter; | |
1a3f060c DA |
6909 | } |
6910 | ||
6911 | return 0; | |
6912 | } | |
6913 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); | |
6914 | ||
32b6d34f TY |
6915 | static bool __netdev_has_upper_dev(struct net_device *dev, |
6916 | struct net_device *upper_dev) | |
6917 | { | |
eff74233 | 6918 | struct netdev_nested_priv priv = { |
1fc70edb | 6919 | .flags = 0, |
eff74233 TY |
6920 | .data = (void *)upper_dev, |
6921 | }; | |
6922 | ||
32b6d34f TY |
6923 | ASSERT_RTNL(); |
6924 | ||
6925 | return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, | |
eff74233 | 6926 | &priv); |
32b6d34f TY |
6927 | } |
6928 | ||
31088a11 VF |
6929 | /** |
6930 | * netdev_lower_get_next_private - Get the next ->private from the | |
6931 | * lower neighbour list | |
6932 | * @dev: device | |
6933 | * @iter: list_head ** of the current position | |
6934 | * | |
6935 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
6936 | * list, starting from iter position. The caller must hold either hold the | |
6937 | * RTNL lock or its own locking that guarantees that the neighbour lower | |
b469139e | 6938 | * list will remain unchanged. |
31088a11 VF |
6939 | */ |
6940 | void *netdev_lower_get_next_private(struct net_device *dev, | |
6941 | struct list_head **iter) | |
6942 | { | |
6943 | struct netdev_adjacent *lower; | |
6944 | ||
6945 | lower = list_entry(*iter, struct netdev_adjacent, list); | |
6946 | ||
6947 | if (&lower->list == &dev->adj_list.lower) | |
6948 | return NULL; | |
6949 | ||
6859e7df | 6950 | *iter = lower->list.next; |
31088a11 VF |
6951 | |
6952 | return lower->private; | |
6953 | } | |
6954 | EXPORT_SYMBOL(netdev_lower_get_next_private); | |
6955 | ||
6956 | /** | |
6957 | * netdev_lower_get_next_private_rcu - Get the next ->private from the | |
6958 | * lower neighbour list, RCU | |
6959 | * variant | |
6960 | * @dev: device | |
6961 | * @iter: list_head ** of the current position | |
6962 | * | |
6963 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
6964 | * list, starting from iter position. The caller must hold RCU read lock. | |
6965 | */ | |
6966 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, | |
6967 | struct list_head **iter) | |
6968 | { | |
6969 | struct netdev_adjacent *lower; | |
6970 | ||
68918669 | 6971 | WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); |
31088a11 VF |
6972 | |
6973 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6974 | ||
6975 | if (&lower->list == &dev->adj_list.lower) | |
6976 | return NULL; | |
6977 | ||
6859e7df | 6978 | *iter = &lower->list; |
31088a11 VF |
6979 | |
6980 | return lower->private; | |
6981 | } | |
6982 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); | |
6983 | ||
4085ebe8 VY |
6984 | /** |
6985 | * netdev_lower_get_next - Get the next device from the lower neighbour | |
6986 | * list | |
6987 | * @dev: device | |
6988 | * @iter: list_head ** of the current position | |
6989 | * | |
6990 | * Gets the next netdev_adjacent from the dev's lower neighbour | |
6991 | * list, starting from iter position. The caller must hold RTNL lock or | |
6992 | * its own locking that guarantees that the neighbour lower | |
b469139e | 6993 | * list will remain unchanged. |
4085ebe8 VY |
6994 | */ |
6995 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) | |
6996 | { | |
6997 | struct netdev_adjacent *lower; | |
6998 | ||
cfdd28be | 6999 | lower = list_entry(*iter, struct netdev_adjacent, list); |
4085ebe8 VY |
7000 | |
7001 | if (&lower->list == &dev->adj_list.lower) | |
7002 | return NULL; | |
7003 | ||
cfdd28be | 7004 | *iter = lower->list.next; |
4085ebe8 VY |
7005 | |
7006 | return lower->dev; | |
7007 | } | |
7008 | EXPORT_SYMBOL(netdev_lower_get_next); | |
7009 | ||
1a3f060c DA |
7010 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
7011 | struct list_head **iter) | |
7012 | { | |
7013 | struct netdev_adjacent *lower; | |
7014 | ||
46b5ab1a | 7015 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
1a3f060c DA |
7016 | |
7017 | if (&lower->list == &dev->adj_list.lower) | |
7018 | return NULL; | |
7019 | ||
46b5ab1a | 7020 | *iter = &lower->list; |
1a3f060c DA |
7021 | |
7022 | return lower->dev; | |
7023 | } | |
7024 | ||
32b6d34f TY |
7025 | static struct net_device *__netdev_next_lower_dev(struct net_device *dev, |
7026 | struct list_head **iter, | |
7027 | bool *ignore) | |
7028 | { | |
7029 | struct netdev_adjacent *lower; | |
7030 | ||
7031 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7032 | ||
7033 | if (&lower->list == &dev->adj_list.lower) | |
7034 | return NULL; | |
7035 | ||
7036 | *iter = &lower->list; | |
7037 | *ignore = lower->ignore; | |
7038 | ||
7039 | return lower->dev; | |
7040 | } | |
7041 | ||
1a3f060c DA |
7042 | int netdev_walk_all_lower_dev(struct net_device *dev, |
7043 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7044 | struct netdev_nested_priv *priv), |
7045 | struct netdev_nested_priv *priv) | |
1a3f060c | 7046 | { |
5343da4c TY |
7047 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7048 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7049 | int ret, cur = 0; | |
1a3f060c | 7050 | |
5343da4c TY |
7051 | now = dev; |
7052 | iter = &dev->adj_list.lower; | |
1a3f060c | 7053 | |
5343da4c TY |
7054 | while (1) { |
7055 | if (now != dev) { | |
eff74233 | 7056 | ret = fn(now, priv); |
5343da4c TY |
7057 | if (ret) |
7058 | return ret; | |
7059 | } | |
7060 | ||
7061 | next = NULL; | |
7062 | while (1) { | |
7063 | ldev = netdev_next_lower_dev(now, &iter); | |
7064 | if (!ldev) | |
7065 | break; | |
7066 | ||
7067 | next = ldev; | |
7068 | niter = &ldev->adj_list.lower; | |
7069 | dev_stack[cur] = now; | |
7070 | iter_stack[cur++] = iter; | |
7071 | break; | |
7072 | } | |
7073 | ||
7074 | if (!next) { | |
7075 | if (!cur) | |
7076 | return 0; | |
7077 | next = dev_stack[--cur]; | |
7078 | niter = iter_stack[cur]; | |
7079 | } | |
7080 | ||
7081 | now = next; | |
7082 | iter = niter; | |
1a3f060c DA |
7083 | } |
7084 | ||
7085 | return 0; | |
7086 | } | |
7087 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); | |
7088 | ||
32b6d34f TY |
7089 | static int __netdev_walk_all_lower_dev(struct net_device *dev, |
7090 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7091 | struct netdev_nested_priv *priv), |
7092 | struct netdev_nested_priv *priv) | |
32b6d34f TY |
7093 | { |
7094 | struct net_device *ldev, *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; | |
7097 | bool ignore; | |
7098 | ||
7099 | now = dev; | |
7100 | iter = &dev->adj_list.lower; | |
7101 | ||
7102 | while (1) { | |
7103 | if (now != dev) { | |
eff74233 | 7104 | ret = fn(now, priv); |
32b6d34f TY |
7105 | if (ret) |
7106 | return ret; | |
7107 | } | |
7108 | ||
7109 | next = NULL; | |
7110 | while (1) { | |
7111 | ldev = __netdev_next_lower_dev(now, &iter, &ignore); | |
7112 | if (!ldev) | |
7113 | break; | |
7114 | if (ignore) | |
7115 | continue; | |
7116 | ||
7117 | next = ldev; | |
7118 | niter = &ldev->adj_list.lower; | |
7119 | dev_stack[cur] = now; | |
7120 | iter_stack[cur++] = iter; | |
7121 | break; | |
7122 | } | |
7123 | ||
7124 | if (!next) { | |
7125 | if (!cur) | |
7126 | return 0; | |
7127 | next = dev_stack[--cur]; | |
7128 | niter = iter_stack[cur]; | |
7129 | } | |
7130 | ||
7131 | now = next; | |
7132 | iter = niter; | |
7133 | } | |
7134 | ||
7135 | return 0; | |
7136 | } | |
7137 | ||
7151affe TY |
7138 | struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
7139 | struct list_head **iter) | |
1a3f060c DA |
7140 | { |
7141 | struct netdev_adjacent *lower; | |
7142 | ||
7143 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7144 | if (&lower->list == &dev->adj_list.lower) | |
7145 | return NULL; | |
7146 | ||
7147 | *iter = &lower->list; | |
7148 | ||
7149 | return lower->dev; | |
7150 | } | |
7151affe | 7151 | EXPORT_SYMBOL(netdev_next_lower_dev_rcu); |
1a3f060c | 7152 | |
5343da4c TY |
7153 | static u8 __netdev_upper_depth(struct net_device *dev) |
7154 | { | |
7155 | struct net_device *udev; | |
7156 | struct list_head *iter; | |
7157 | u8 max_depth = 0; | |
32b6d34f | 7158 | bool ignore; |
5343da4c TY |
7159 | |
7160 | for (iter = &dev->adj_list.upper, | |
32b6d34f | 7161 | udev = __netdev_next_upper_dev(dev, &iter, &ignore); |
5343da4c | 7162 | udev; |
32b6d34f TY |
7163 | udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { |
7164 | if (ignore) | |
7165 | continue; | |
5343da4c TY |
7166 | if (max_depth < udev->upper_level) |
7167 | max_depth = udev->upper_level; | |
7168 | } | |
7169 | ||
7170 | return max_depth; | |
7171 | } | |
7172 | ||
7173 | static u8 __netdev_lower_depth(struct net_device *dev) | |
1a3f060c DA |
7174 | { |
7175 | struct net_device *ldev; | |
7176 | struct list_head *iter; | |
5343da4c | 7177 | u8 max_depth = 0; |
32b6d34f | 7178 | bool ignore; |
1a3f060c DA |
7179 | |
7180 | for (iter = &dev->adj_list.lower, | |
32b6d34f | 7181 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore); |
1a3f060c | 7182 | ldev; |
32b6d34f TY |
7183 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { |
7184 | if (ignore) | |
7185 | continue; | |
5343da4c TY |
7186 | if (max_depth < ldev->lower_level) |
7187 | max_depth = ldev->lower_level; | |
7188 | } | |
1a3f060c | 7189 | |
5343da4c TY |
7190 | return max_depth; |
7191 | } | |
7192 | ||
eff74233 TY |
7193 | static int __netdev_update_upper_level(struct net_device *dev, |
7194 | struct netdev_nested_priv *__unused) | |
5343da4c TY |
7195 | { |
7196 | dev->upper_level = __netdev_upper_depth(dev) + 1; | |
7197 | return 0; | |
7198 | } | |
7199 | ||
f32404ae JB |
7200 | #ifdef CONFIG_LOCKDEP |
7201 | static LIST_HEAD(net_unlink_list); | |
7202 | ||
7203 | static void net_unlink_todo(struct net_device *dev) | |
7204 | { | |
7205 | if (list_empty(&dev->unlink_list)) | |
7206 | list_add_tail(&dev->unlink_list, &net_unlink_list); | |
7207 | } | |
7208 | #endif | |
7209 | ||
eff74233 | 7210 | static int __netdev_update_lower_level(struct net_device *dev, |
1fc70edb | 7211 | struct netdev_nested_priv *priv) |
5343da4c TY |
7212 | { |
7213 | dev->lower_level = __netdev_lower_depth(dev) + 1; | |
1fc70edb TY |
7214 | |
7215 | #ifdef CONFIG_LOCKDEP | |
7216 | if (!priv) | |
7217 | return 0; | |
7218 | ||
7219 | if (priv->flags & NESTED_SYNC_IMM) | |
7220 | dev->nested_level = dev->lower_level - 1; | |
7221 | if (priv->flags & NESTED_SYNC_TODO) | |
7222 | net_unlink_todo(dev); | |
7223 | #endif | |
5343da4c TY |
7224 | return 0; |
7225 | } | |
7226 | ||
7227 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, | |
7228 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7229 | struct netdev_nested_priv *priv), |
7230 | struct netdev_nested_priv *priv) | |
5343da4c TY |
7231 | { |
7232 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7233 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7234 | int ret, cur = 0; | |
7235 | ||
7236 | now = dev; | |
7237 | iter = &dev->adj_list.lower; | |
7238 | ||
7239 | while (1) { | |
7240 | if (now != dev) { | |
eff74233 | 7241 | ret = fn(now, priv); |
5343da4c TY |
7242 | if (ret) |
7243 | return ret; | |
7244 | } | |
7245 | ||
7246 | next = NULL; | |
7247 | while (1) { | |
7248 | ldev = netdev_next_lower_dev_rcu(now, &iter); | |
7249 | if (!ldev) | |
7250 | break; | |
7251 | ||
7252 | next = ldev; | |
7253 | niter = &ldev->adj_list.lower; | |
7254 | dev_stack[cur] = now; | |
7255 | iter_stack[cur++] = iter; | |
7256 | break; | |
7257 | } | |
7258 | ||
7259 | if (!next) { | |
7260 | if (!cur) | |
7261 | return 0; | |
7262 | next = dev_stack[--cur]; | |
7263 | niter = iter_stack[cur]; | |
7264 | } | |
7265 | ||
7266 | now = next; | |
7267 | iter = niter; | |
1a3f060c DA |
7268 | } |
7269 | ||
7270 | return 0; | |
7271 | } | |
7272 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); | |
7273 | ||
e001bfad | 7274 | /** |
7275 | * netdev_lower_get_first_private_rcu - Get the first ->private from the | |
7276 | * lower neighbour list, RCU | |
7277 | * variant | |
7278 | * @dev: device | |
7279 | * | |
7280 | * Gets the first netdev_adjacent->private from the dev's lower neighbour | |
7281 | * list. The caller must hold RCU read lock. | |
7282 | */ | |
7283 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) | |
7284 | { | |
7285 | struct netdev_adjacent *lower; | |
7286 | ||
7287 | lower = list_first_or_null_rcu(&dev->adj_list.lower, | |
7288 | struct netdev_adjacent, list); | |
7289 | if (lower) | |
7290 | return lower->private; | |
7291 | return NULL; | |
7292 | } | |
7293 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); | |
7294 | ||
9ff162a8 JP |
7295 | /** |
7296 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
7297 | * @dev: device | |
7298 | * | |
7299 | * Find a master upper device and return pointer to it or NULL in case | |
7300 | * it's not there. The caller must hold the RCU read lock. | |
7301 | */ | |
7302 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
7303 | { | |
aa9d8560 | 7304 | struct netdev_adjacent *upper; |
9ff162a8 | 7305 | |
2f268f12 | 7306 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
aa9d8560 | 7307 | struct netdev_adjacent, list); |
9ff162a8 JP |
7308 | if (upper && likely(upper->master)) |
7309 | return upper->dev; | |
7310 | return NULL; | |
7311 | } | |
7312 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
7313 | ||
0a59f3a9 | 7314 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
3ee32707 VF |
7315 | struct net_device *adj_dev, |
7316 | struct list_head *dev_list) | |
7317 | { | |
7318 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7319 | |
3ee32707 VF |
7320 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7321 | "upper_%s" : "lower_%s", adj_dev->name); | |
7322 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), | |
7323 | linkname); | |
7324 | } | |
0a59f3a9 | 7325 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
3ee32707 VF |
7326 | char *name, |
7327 | struct list_head *dev_list) | |
7328 | { | |
7329 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7330 | |
3ee32707 VF |
7331 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7332 | "upper_%s" : "lower_%s", name); | |
7333 | sysfs_remove_link(&(dev->dev.kobj), linkname); | |
7334 | } | |
7335 | ||
7ce64c79 AF |
7336 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
7337 | struct net_device *adj_dev, | |
7338 | struct list_head *dev_list) | |
7339 | { | |
7340 | return (dev_list == &dev->adj_list.upper || | |
7341 | dev_list == &dev->adj_list.lower) && | |
7342 | net_eq(dev_net(dev), dev_net(adj_dev)); | |
7343 | } | |
3ee32707 | 7344 | |
5d261913 VF |
7345 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
7346 | struct net_device *adj_dev, | |
7863c054 | 7347 | struct list_head *dev_list, |
402dae96 | 7348 | void *private, bool master) |
5d261913 VF |
7349 | { |
7350 | struct netdev_adjacent *adj; | |
842d67a7 | 7351 | int ret; |
5d261913 | 7352 | |
6ea29da1 | 7353 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 VF |
7354 | |
7355 | if (adj) { | |
790510d9 | 7356 | adj->ref_nr += 1; |
67b62f98 DA |
7357 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
7358 | dev->name, adj_dev->name, adj->ref_nr); | |
7359 | ||
5d261913 VF |
7360 | return 0; |
7361 | } | |
7362 | ||
7363 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); | |
7364 | if (!adj) | |
7365 | return -ENOMEM; | |
7366 | ||
7367 | adj->dev = adj_dev; | |
7368 | adj->master = master; | |
790510d9 | 7369 | adj->ref_nr = 1; |
402dae96 | 7370 | adj->private = private; |
32b6d34f | 7371 | adj->ignore = false; |
f77159a3 | 7372 | dev_hold_track(adj_dev, &adj->dev_tracker, GFP_KERNEL); |
2f268f12 | 7373 | |
67b62f98 DA |
7374 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
7375 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); | |
5d261913 | 7376 | |
7ce64c79 | 7377 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
3ee32707 | 7378 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
5831d66e VF |
7379 | if (ret) |
7380 | goto free_adj; | |
7381 | } | |
7382 | ||
7863c054 | 7383 | /* Ensure that master link is always the first item in list. */ |
842d67a7 VF |
7384 | if (master) { |
7385 | ret = sysfs_create_link(&(dev->dev.kobj), | |
7386 | &(adj_dev->dev.kobj), "master"); | |
7387 | if (ret) | |
5831d66e | 7388 | goto remove_symlinks; |
842d67a7 | 7389 | |
7863c054 | 7390 | list_add_rcu(&adj->list, dev_list); |
842d67a7 | 7391 | } else { |
7863c054 | 7392 | list_add_tail_rcu(&adj->list, dev_list); |
842d67a7 | 7393 | } |
5d261913 VF |
7394 | |
7395 | return 0; | |
842d67a7 | 7396 | |
5831d66e | 7397 | remove_symlinks: |
7ce64c79 | 7398 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7399 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
842d67a7 | 7400 | free_adj: |
f77159a3 | 7401 | dev_put_track(adj_dev, &adj->dev_tracker); |
842d67a7 VF |
7402 | kfree(adj); |
7403 | ||
7404 | return ret; | |
5d261913 VF |
7405 | } |
7406 | ||
1d143d9f | 7407 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
7408 | struct net_device *adj_dev, | |
93409033 | 7409 | u16 ref_nr, |
1d143d9f | 7410 | struct list_head *dev_list) |
5d261913 VF |
7411 | { |
7412 | struct netdev_adjacent *adj; | |
7413 | ||
67b62f98 DA |
7414 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
7415 | dev->name, adj_dev->name, ref_nr); | |
7416 | ||
6ea29da1 | 7417 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 | 7418 | |
2f268f12 | 7419 | if (!adj) { |
67b62f98 | 7420 | pr_err("Adjacency does not exist for device %s from %s\n", |
2f268f12 | 7421 | dev->name, adj_dev->name); |
67b62f98 DA |
7422 | WARN_ON(1); |
7423 | return; | |
2f268f12 | 7424 | } |
5d261913 | 7425 | |
93409033 | 7426 | if (adj->ref_nr > ref_nr) { |
67b62f98 DA |
7427 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
7428 | dev->name, adj_dev->name, ref_nr, | |
7429 | adj->ref_nr - ref_nr); | |
93409033 | 7430 | adj->ref_nr -= ref_nr; |
5d261913 VF |
7431 | return; |
7432 | } | |
7433 | ||
842d67a7 VF |
7434 | if (adj->master) |
7435 | sysfs_remove_link(&(dev->dev.kobj), "master"); | |
7436 | ||
7ce64c79 | 7437 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7438 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
5831d66e | 7439 | |
5d261913 | 7440 | list_del_rcu(&adj->list); |
67b62f98 | 7441 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
2f268f12 | 7442 | adj_dev->name, dev->name, adj_dev->name); |
f77159a3 | 7443 | dev_put_track(adj_dev, &adj->dev_tracker); |
5d261913 VF |
7444 | kfree_rcu(adj, rcu); |
7445 | } | |
7446 | ||
1d143d9f | 7447 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
7448 | struct net_device *upper_dev, | |
7449 | struct list_head *up_list, | |
7450 | struct list_head *down_list, | |
7451 | void *private, bool master) | |
5d261913 VF |
7452 | { |
7453 | int ret; | |
7454 | ||
790510d9 | 7455 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
93409033 | 7456 | private, master); |
5d261913 VF |
7457 | if (ret) |
7458 | return ret; | |
7459 | ||
790510d9 | 7460 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
93409033 | 7461 | private, false); |
5d261913 | 7462 | if (ret) { |
790510d9 | 7463 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
5d261913 VF |
7464 | return ret; |
7465 | } | |
7466 | ||
7467 | return 0; | |
7468 | } | |
7469 | ||
1d143d9f | 7470 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
7471 | struct net_device *upper_dev, | |
93409033 | 7472 | u16 ref_nr, |
1d143d9f | 7473 | struct list_head *up_list, |
7474 | struct list_head *down_list) | |
5d261913 | 7475 | { |
93409033 AC |
7476 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
7477 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); | |
5d261913 VF |
7478 | } |
7479 | ||
1d143d9f | 7480 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
7481 | struct net_device *upper_dev, | |
7482 | void *private, bool master) | |
2f268f12 | 7483 | { |
f1170fd4 DA |
7484 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
7485 | &dev->adj_list.upper, | |
7486 | &upper_dev->adj_list.lower, | |
7487 | private, master); | |
5d261913 VF |
7488 | } |
7489 | ||
1d143d9f | 7490 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
7491 | struct net_device *upper_dev) | |
2f268f12 | 7492 | { |
93409033 | 7493 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
2f268f12 VF |
7494 | &dev->adj_list.upper, |
7495 | &upper_dev->adj_list.lower); | |
7496 | } | |
5d261913 | 7497 | |
9ff162a8 | 7498 | static int __netdev_upper_dev_link(struct net_device *dev, |
402dae96 | 7499 | struct net_device *upper_dev, bool master, |
42ab19ee | 7500 | void *upper_priv, void *upper_info, |
1fc70edb | 7501 | struct netdev_nested_priv *priv, |
42ab19ee | 7502 | struct netlink_ext_ack *extack) |
9ff162a8 | 7503 | { |
51d0c047 DA |
7504 | struct netdev_notifier_changeupper_info changeupper_info = { |
7505 | .info = { | |
7506 | .dev = dev, | |
42ab19ee | 7507 | .extack = extack, |
51d0c047 DA |
7508 | }, |
7509 | .upper_dev = upper_dev, | |
7510 | .master = master, | |
7511 | .linking = true, | |
7512 | .upper_info = upper_info, | |
7513 | }; | |
50d629e7 | 7514 | struct net_device *master_dev; |
5d261913 | 7515 | int ret = 0; |
9ff162a8 JP |
7516 | |
7517 | ASSERT_RTNL(); | |
7518 | ||
7519 | if (dev == upper_dev) | |
7520 | return -EBUSY; | |
7521 | ||
7522 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
32b6d34f | 7523 | if (__netdev_has_upper_dev(upper_dev, dev)) |
9ff162a8 JP |
7524 | return -EBUSY; |
7525 | ||
5343da4c TY |
7526 | if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) |
7527 | return -EMLINK; | |
7528 | ||
50d629e7 | 7529 | if (!master) { |
32b6d34f | 7530 | if (__netdev_has_upper_dev(dev, upper_dev)) |
50d629e7 MM |
7531 | return -EEXIST; |
7532 | } else { | |
32b6d34f | 7533 | master_dev = __netdev_master_upper_dev_get(dev); |
50d629e7 MM |
7534 | if (master_dev) |
7535 | return master_dev == upper_dev ? -EEXIST : -EBUSY; | |
7536 | } | |
9ff162a8 | 7537 | |
51d0c047 | 7538 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7539 | &changeupper_info.info); |
7540 | ret = notifier_to_errno(ret); | |
7541 | if (ret) | |
7542 | return ret; | |
7543 | ||
6dffb044 | 7544 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
402dae96 | 7545 | master); |
5d261913 VF |
7546 | if (ret) |
7547 | return ret; | |
9ff162a8 | 7548 | |
51d0c047 | 7549 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
b03804e7 IS |
7550 | &changeupper_info.info); |
7551 | ret = notifier_to_errno(ret); | |
7552 | if (ret) | |
f1170fd4 | 7553 | goto rollback; |
b03804e7 | 7554 | |
5343da4c | 7555 | __netdev_update_upper_level(dev, NULL); |
32b6d34f | 7556 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c | 7557 | |
1fc70edb | 7558 | __netdev_update_lower_level(upper_dev, priv); |
32b6d34f | 7559 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
1fc70edb | 7560 | priv); |
5343da4c | 7561 | |
9ff162a8 | 7562 | return 0; |
5d261913 | 7563 | |
f1170fd4 | 7564 | rollback: |
2f268f12 | 7565 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 VF |
7566 | |
7567 | return ret; | |
9ff162a8 JP |
7568 | } |
7569 | ||
7570 | /** | |
7571 | * netdev_upper_dev_link - Add a link to the upper device | |
7572 | * @dev: device | |
7573 | * @upper_dev: new upper device | |
7a006d59 | 7574 | * @extack: netlink extended ack |
9ff162a8 JP |
7575 | * |
7576 | * Adds a link to device which is upper to this one. The caller must hold | |
7577 | * the RTNL lock. On a failure a negative errno code is returned. | |
7578 | * On success the reference counts are adjusted and the function | |
7579 | * returns zero. | |
7580 | */ | |
7581 | int netdev_upper_dev_link(struct net_device *dev, | |
42ab19ee DA |
7582 | struct net_device *upper_dev, |
7583 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7584 | { |
1fc70edb TY |
7585 | struct netdev_nested_priv priv = { |
7586 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7587 | .data = NULL, | |
7588 | }; | |
7589 | ||
42ab19ee | 7590 | return __netdev_upper_dev_link(dev, upper_dev, false, |
1fc70edb | 7591 | NULL, NULL, &priv, extack); |
9ff162a8 JP |
7592 | } |
7593 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
7594 | ||
7595 | /** | |
7596 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
7597 | * @dev: device | |
7598 | * @upper_dev: new upper device | |
6dffb044 | 7599 | * @upper_priv: upper device private |
29bf24af | 7600 | * @upper_info: upper info to be passed down via notifier |
7a006d59 | 7601 | * @extack: netlink extended ack |
9ff162a8 JP |
7602 | * |
7603 | * Adds a link to device which is upper to this one. In this case, only | |
7604 | * one master upper device can be linked, although other non-master devices | |
7605 | * might be linked as well. The caller must hold the RTNL lock. | |
7606 | * On a failure a negative errno code is returned. On success the reference | |
7607 | * counts are adjusted and the function returns zero. | |
7608 | */ | |
7609 | int netdev_master_upper_dev_link(struct net_device *dev, | |
6dffb044 | 7610 | struct net_device *upper_dev, |
42ab19ee DA |
7611 | void *upper_priv, void *upper_info, |
7612 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7613 | { |
1fc70edb TY |
7614 | struct netdev_nested_priv priv = { |
7615 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7616 | .data = NULL, | |
7617 | }; | |
7618 | ||
29bf24af | 7619 | return __netdev_upper_dev_link(dev, upper_dev, true, |
1fc70edb | 7620 | upper_priv, upper_info, &priv, extack); |
9ff162a8 JP |
7621 | } |
7622 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
7623 | ||
fe8300fd | 7624 | static void __netdev_upper_dev_unlink(struct net_device *dev, |
1fc70edb TY |
7625 | struct net_device *upper_dev, |
7626 | struct netdev_nested_priv *priv) | |
9ff162a8 | 7627 | { |
51d0c047 DA |
7628 | struct netdev_notifier_changeupper_info changeupper_info = { |
7629 | .info = { | |
7630 | .dev = dev, | |
7631 | }, | |
7632 | .upper_dev = upper_dev, | |
7633 | .linking = false, | |
7634 | }; | |
f4563a75 | 7635 | |
9ff162a8 JP |
7636 | ASSERT_RTNL(); |
7637 | ||
0e4ead9d | 7638 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
0e4ead9d | 7639 | |
51d0c047 | 7640 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7641 | &changeupper_info.info); |
7642 | ||
2f268f12 | 7643 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 | 7644 | |
51d0c047 | 7645 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
0e4ead9d | 7646 | &changeupper_info.info); |
5343da4c TY |
7647 | |
7648 | __netdev_update_upper_level(dev, NULL); | |
32b6d34f | 7649 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c | 7650 | |
1fc70edb | 7651 | __netdev_update_lower_level(upper_dev, priv); |
32b6d34f | 7652 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
1fc70edb | 7653 | priv); |
9ff162a8 | 7654 | } |
fe8300fd TY |
7655 | |
7656 | /** | |
7657 | * netdev_upper_dev_unlink - Removes a link to upper device | |
7658 | * @dev: device | |
7659 | * @upper_dev: new upper device | |
7660 | * | |
7661 | * Removes a link to device which is upper to this one. The caller must hold | |
7662 | * the RTNL lock. | |
7663 | */ | |
7664 | void netdev_upper_dev_unlink(struct net_device *dev, | |
7665 | struct net_device *upper_dev) | |
7666 | { | |
1fc70edb TY |
7667 | struct netdev_nested_priv priv = { |
7668 | .flags = NESTED_SYNC_TODO, | |
7669 | .data = NULL, | |
7670 | }; | |
7671 | ||
7672 | __netdev_upper_dev_unlink(dev, upper_dev, &priv); | |
9ff162a8 JP |
7673 | } |
7674 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
7675 | ||
32b6d34f TY |
7676 | static void __netdev_adjacent_dev_set(struct net_device *upper_dev, |
7677 | struct net_device *lower_dev, | |
7678 | bool val) | |
7679 | { | |
7680 | struct netdev_adjacent *adj; | |
7681 | ||
7682 | adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); | |
7683 | if (adj) | |
7684 | adj->ignore = val; | |
7685 | ||
7686 | adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); | |
7687 | if (adj) | |
7688 | adj->ignore = val; | |
7689 | } | |
7690 | ||
7691 | static void netdev_adjacent_dev_disable(struct net_device *upper_dev, | |
7692 | struct net_device *lower_dev) | |
7693 | { | |
7694 | __netdev_adjacent_dev_set(upper_dev, lower_dev, true); | |
7695 | } | |
7696 | ||
7697 | static void netdev_adjacent_dev_enable(struct net_device *upper_dev, | |
7698 | struct net_device *lower_dev) | |
7699 | { | |
7700 | __netdev_adjacent_dev_set(upper_dev, lower_dev, false); | |
7701 | } | |
7702 | ||
7703 | int netdev_adjacent_change_prepare(struct net_device *old_dev, | |
7704 | struct net_device *new_dev, | |
7705 | struct net_device *dev, | |
7706 | struct netlink_ext_ack *extack) | |
7707 | { | |
1fc70edb TY |
7708 | struct netdev_nested_priv priv = { |
7709 | .flags = 0, | |
7710 | .data = NULL, | |
7711 | }; | |
32b6d34f TY |
7712 | int err; |
7713 | ||
7714 | if (!new_dev) | |
7715 | return 0; | |
7716 | ||
7717 | if (old_dev && new_dev != old_dev) | |
7718 | netdev_adjacent_dev_disable(dev, old_dev); | |
1fc70edb TY |
7719 | err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv, |
7720 | extack); | |
32b6d34f TY |
7721 | if (err) { |
7722 | if (old_dev && new_dev != old_dev) | |
7723 | netdev_adjacent_dev_enable(dev, old_dev); | |
7724 | return err; | |
7725 | } | |
7726 | ||
7727 | return 0; | |
7728 | } | |
7729 | EXPORT_SYMBOL(netdev_adjacent_change_prepare); | |
7730 | ||
7731 | void netdev_adjacent_change_commit(struct net_device *old_dev, | |
7732 | struct net_device *new_dev, | |
7733 | struct net_device *dev) | |
7734 | { | |
1fc70edb TY |
7735 | struct netdev_nested_priv priv = { |
7736 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7737 | .data = NULL, | |
7738 | }; | |
7739 | ||
32b6d34f TY |
7740 | if (!new_dev || !old_dev) |
7741 | return; | |
7742 | ||
7743 | if (new_dev == old_dev) | |
7744 | return; | |
7745 | ||
7746 | netdev_adjacent_dev_enable(dev, old_dev); | |
1fc70edb | 7747 | __netdev_upper_dev_unlink(old_dev, dev, &priv); |
32b6d34f TY |
7748 | } |
7749 | EXPORT_SYMBOL(netdev_adjacent_change_commit); | |
7750 | ||
7751 | void netdev_adjacent_change_abort(struct net_device *old_dev, | |
7752 | struct net_device *new_dev, | |
7753 | struct net_device *dev) | |
7754 | { | |
1fc70edb TY |
7755 | struct netdev_nested_priv priv = { |
7756 | .flags = 0, | |
7757 | .data = NULL, | |
7758 | }; | |
7759 | ||
32b6d34f TY |
7760 | if (!new_dev) |
7761 | return; | |
7762 | ||
7763 | if (old_dev && new_dev != old_dev) | |
7764 | netdev_adjacent_dev_enable(dev, old_dev); | |
7765 | ||
1fc70edb | 7766 | __netdev_upper_dev_unlink(new_dev, dev, &priv); |
32b6d34f TY |
7767 | } |
7768 | EXPORT_SYMBOL(netdev_adjacent_change_abort); | |
7769 | ||
61bd3857 MS |
7770 | /** |
7771 | * netdev_bonding_info_change - Dispatch event about slave change | |
7772 | * @dev: device | |
4a26e453 | 7773 | * @bonding_info: info to dispatch |
61bd3857 MS |
7774 | * |
7775 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. | |
7776 | * The caller must hold the RTNL lock. | |
7777 | */ | |
7778 | void netdev_bonding_info_change(struct net_device *dev, | |
7779 | struct netdev_bonding_info *bonding_info) | |
7780 | { | |
51d0c047 DA |
7781 | struct netdev_notifier_bonding_info info = { |
7782 | .info.dev = dev, | |
7783 | }; | |
61bd3857 MS |
7784 | |
7785 | memcpy(&info.bonding_info, bonding_info, | |
7786 | sizeof(struct netdev_bonding_info)); | |
51d0c047 | 7787 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
61bd3857 MS |
7788 | &info.info); |
7789 | } | |
7790 | EXPORT_SYMBOL(netdev_bonding_info_change); | |
7791 | ||
9309f97a PM |
7792 | static int netdev_offload_xstats_enable_l3(struct net_device *dev, |
7793 | struct netlink_ext_ack *extack) | |
7794 | { | |
7795 | struct netdev_notifier_offload_xstats_info info = { | |
7796 | .info.dev = dev, | |
7797 | .info.extack = extack, | |
7798 | .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3, | |
7799 | }; | |
7800 | int err; | |
7801 | int rc; | |
7802 | ||
7803 | dev->offload_xstats_l3 = kzalloc(sizeof(*dev->offload_xstats_l3), | |
7804 | GFP_KERNEL); | |
7805 | if (!dev->offload_xstats_l3) | |
7806 | return -ENOMEM; | |
7807 | ||
7808 | rc = call_netdevice_notifiers_info_robust(NETDEV_OFFLOAD_XSTATS_ENABLE, | |
7809 | NETDEV_OFFLOAD_XSTATS_DISABLE, | |
7810 | &info.info); | |
7811 | err = notifier_to_errno(rc); | |
7812 | if (err) | |
7813 | goto free_stats; | |
7814 | ||
7815 | return 0; | |
7816 | ||
7817 | free_stats: | |
7818 | kfree(dev->offload_xstats_l3); | |
7819 | dev->offload_xstats_l3 = NULL; | |
7820 | return err; | |
7821 | } | |
7822 | ||
7823 | int netdev_offload_xstats_enable(struct net_device *dev, | |
7824 | enum netdev_offload_xstats_type type, | |
7825 | struct netlink_ext_ack *extack) | |
7826 | { | |
7827 | ASSERT_RTNL(); | |
7828 | ||
7829 | if (netdev_offload_xstats_enabled(dev, type)) | |
7830 | return -EALREADY; | |
7831 | ||
7832 | switch (type) { | |
7833 | case NETDEV_OFFLOAD_XSTATS_TYPE_L3: | |
7834 | return netdev_offload_xstats_enable_l3(dev, extack); | |
7835 | } | |
7836 | ||
7837 | WARN_ON(1); | |
7838 | return -EINVAL; | |
7839 | } | |
7840 | EXPORT_SYMBOL(netdev_offload_xstats_enable); | |
7841 | ||
7842 | static void netdev_offload_xstats_disable_l3(struct net_device *dev) | |
7843 | { | |
7844 | struct netdev_notifier_offload_xstats_info info = { | |
7845 | .info.dev = dev, | |
7846 | .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3, | |
7847 | }; | |
7848 | ||
7849 | call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_DISABLE, | |
7850 | &info.info); | |
7851 | kfree(dev->offload_xstats_l3); | |
7852 | dev->offload_xstats_l3 = NULL; | |
7853 | } | |
7854 | ||
7855 | int netdev_offload_xstats_disable(struct net_device *dev, | |
7856 | enum netdev_offload_xstats_type type) | |
7857 | { | |
7858 | ASSERT_RTNL(); | |
7859 | ||
7860 | if (!netdev_offload_xstats_enabled(dev, type)) | |
7861 | return -EALREADY; | |
7862 | ||
7863 | switch (type) { | |
7864 | case NETDEV_OFFLOAD_XSTATS_TYPE_L3: | |
7865 | netdev_offload_xstats_disable_l3(dev); | |
7866 | return 0; | |
7867 | } | |
7868 | ||
7869 | WARN_ON(1); | |
7870 | return -EINVAL; | |
7871 | } | |
7872 | EXPORT_SYMBOL(netdev_offload_xstats_disable); | |
7873 | ||
7874 | static void netdev_offload_xstats_disable_all(struct net_device *dev) | |
7875 | { | |
7876 | netdev_offload_xstats_disable(dev, NETDEV_OFFLOAD_XSTATS_TYPE_L3); | |
7877 | } | |
7878 | ||
7879 | static struct rtnl_hw_stats64 * | |
7880 | netdev_offload_xstats_get_ptr(const struct net_device *dev, | |
7881 | enum netdev_offload_xstats_type type) | |
7882 | { | |
7883 | switch (type) { | |
7884 | case NETDEV_OFFLOAD_XSTATS_TYPE_L3: | |
7885 | return dev->offload_xstats_l3; | |
7886 | } | |
7887 | ||
7888 | WARN_ON(1); | |
7889 | return NULL; | |
7890 | } | |
7891 | ||
7892 | bool netdev_offload_xstats_enabled(const struct net_device *dev, | |
7893 | enum netdev_offload_xstats_type type) | |
7894 | { | |
7895 | ASSERT_RTNL(); | |
7896 | ||
7897 | return netdev_offload_xstats_get_ptr(dev, type); | |
7898 | } | |
7899 | EXPORT_SYMBOL(netdev_offload_xstats_enabled); | |
7900 | ||
7901 | struct netdev_notifier_offload_xstats_ru { | |
7902 | bool used; | |
7903 | }; | |
7904 | ||
7905 | struct netdev_notifier_offload_xstats_rd { | |
7906 | struct rtnl_hw_stats64 stats; | |
7907 | bool used; | |
7908 | }; | |
7909 | ||
7910 | static void netdev_hw_stats64_add(struct rtnl_hw_stats64 *dest, | |
7911 | const struct rtnl_hw_stats64 *src) | |
7912 | { | |
7913 | dest->rx_packets += src->rx_packets; | |
7914 | dest->tx_packets += src->tx_packets; | |
7915 | dest->rx_bytes += src->rx_bytes; | |
7916 | dest->tx_bytes += src->tx_bytes; | |
7917 | dest->rx_errors += src->rx_errors; | |
7918 | dest->tx_errors += src->tx_errors; | |
7919 | dest->rx_dropped += src->rx_dropped; | |
7920 | dest->tx_dropped += src->tx_dropped; | |
7921 | dest->multicast += src->multicast; | |
7922 | } | |
7923 | ||
7924 | static int netdev_offload_xstats_get_used(struct net_device *dev, | |
7925 | enum netdev_offload_xstats_type type, | |
7926 | bool *p_used, | |
7927 | struct netlink_ext_ack *extack) | |
7928 | { | |
7929 | struct netdev_notifier_offload_xstats_ru report_used = {}; | |
7930 | struct netdev_notifier_offload_xstats_info info = { | |
7931 | .info.dev = dev, | |
7932 | .info.extack = extack, | |
7933 | .type = type, | |
7934 | .report_used = &report_used, | |
7935 | }; | |
7936 | int rc; | |
7937 | ||
7938 | WARN_ON(!netdev_offload_xstats_enabled(dev, type)); | |
7939 | rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_USED, | |
7940 | &info.info); | |
7941 | *p_used = report_used.used; | |
7942 | return notifier_to_errno(rc); | |
7943 | } | |
7944 | ||
7945 | static int netdev_offload_xstats_get_stats(struct net_device *dev, | |
7946 | enum netdev_offload_xstats_type type, | |
7947 | struct rtnl_hw_stats64 *p_stats, | |
7948 | bool *p_used, | |
7949 | struct netlink_ext_ack *extack) | |
7950 | { | |
7951 | struct netdev_notifier_offload_xstats_rd report_delta = {}; | |
7952 | struct netdev_notifier_offload_xstats_info info = { | |
7953 | .info.dev = dev, | |
7954 | .info.extack = extack, | |
7955 | .type = type, | |
7956 | .report_delta = &report_delta, | |
7957 | }; | |
7958 | struct rtnl_hw_stats64 *stats; | |
7959 | int rc; | |
7960 | ||
7961 | stats = netdev_offload_xstats_get_ptr(dev, type); | |
7962 | if (WARN_ON(!stats)) | |
7963 | return -EINVAL; | |
7964 | ||
7965 | rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_DELTA, | |
7966 | &info.info); | |
7967 | ||
7968 | /* Cache whatever we got, even if there was an error, otherwise the | |
7969 | * successful stats retrievals would get lost. | |
7970 | */ | |
7971 | netdev_hw_stats64_add(stats, &report_delta.stats); | |
7972 | ||
7973 | if (p_stats) | |
7974 | *p_stats = *stats; | |
7975 | *p_used = report_delta.used; | |
7976 | ||
7977 | return notifier_to_errno(rc); | |
7978 | } | |
7979 | ||
7980 | int netdev_offload_xstats_get(struct net_device *dev, | |
7981 | enum netdev_offload_xstats_type type, | |
7982 | struct rtnl_hw_stats64 *p_stats, bool *p_used, | |
7983 | struct netlink_ext_ack *extack) | |
7984 | { | |
7985 | ASSERT_RTNL(); | |
7986 | ||
7987 | if (p_stats) | |
7988 | return netdev_offload_xstats_get_stats(dev, type, p_stats, | |
7989 | p_used, extack); | |
7990 | else | |
7991 | return netdev_offload_xstats_get_used(dev, type, p_used, | |
7992 | extack); | |
7993 | } | |
7994 | EXPORT_SYMBOL(netdev_offload_xstats_get); | |
7995 | ||
7996 | void | |
7997 | netdev_offload_xstats_report_delta(struct netdev_notifier_offload_xstats_rd *report_delta, | |
7998 | const struct rtnl_hw_stats64 *stats) | |
7999 | { | |
8000 | report_delta->used = true; | |
8001 | netdev_hw_stats64_add(&report_delta->stats, stats); | |
8002 | } | |
8003 | EXPORT_SYMBOL(netdev_offload_xstats_report_delta); | |
8004 | ||
8005 | void | |
8006 | netdev_offload_xstats_report_used(struct netdev_notifier_offload_xstats_ru *report_used) | |
8007 | { | |
8008 | report_used->used = true; | |
8009 | } | |
8010 | EXPORT_SYMBOL(netdev_offload_xstats_report_used); | |
8011 | ||
8012 | void netdev_offload_xstats_push_delta(struct net_device *dev, | |
8013 | enum netdev_offload_xstats_type type, | |
8014 | const struct rtnl_hw_stats64 *p_stats) | |
8015 | { | |
8016 | struct rtnl_hw_stats64 *stats; | |
8017 | ||
8018 | ASSERT_RTNL(); | |
8019 | ||
8020 | stats = netdev_offload_xstats_get_ptr(dev, type); | |
8021 | if (WARN_ON(!stats)) | |
8022 | return; | |
8023 | ||
8024 | netdev_hw_stats64_add(stats, p_stats); | |
8025 | } | |
8026 | EXPORT_SYMBOL(netdev_offload_xstats_push_delta); | |
8027 | ||
cff9f12b MG |
8028 | /** |
8029 | * netdev_get_xmit_slave - Get the xmit slave of master device | |
8842500d | 8030 | * @dev: device |
cff9f12b MG |
8031 | * @skb: The packet |
8032 | * @all_slaves: assume all the slaves are active | |
8033 | * | |
8034 | * The reference counters are not incremented so the caller must be | |
8035 | * careful with locks. The caller must hold RCU lock. | |
8036 | * %NULL is returned if no slave is found. | |
8037 | */ | |
8038 | ||
8039 | struct net_device *netdev_get_xmit_slave(struct net_device *dev, | |
8040 | struct sk_buff *skb, | |
8041 | bool all_slaves) | |
8042 | { | |
8043 | const struct net_device_ops *ops = dev->netdev_ops; | |
8044 | ||
8045 | if (!ops->ndo_get_xmit_slave) | |
8046 | return NULL; | |
8047 | return ops->ndo_get_xmit_slave(dev, skb, all_slaves); | |
8048 | } | |
8049 | EXPORT_SYMBOL(netdev_get_xmit_slave); | |
8050 | ||
719a402c TT |
8051 | static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev, |
8052 | struct sock *sk) | |
8053 | { | |
8054 | const struct net_device_ops *ops = dev->netdev_ops; | |
8055 | ||
8056 | if (!ops->ndo_sk_get_lower_dev) | |
8057 | return NULL; | |
8058 | return ops->ndo_sk_get_lower_dev(dev, sk); | |
8059 | } | |
8060 | ||
8061 | /** | |
8062 | * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket | |
8063 | * @dev: device | |
8064 | * @sk: the socket | |
8065 | * | |
8066 | * %NULL is returned if no lower device is found. | |
8067 | */ | |
8068 | ||
8069 | struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev, | |
8070 | struct sock *sk) | |
8071 | { | |
8072 | struct net_device *lower; | |
8073 | ||
8074 | lower = netdev_sk_get_lower_dev(dev, sk); | |
8075 | while (lower) { | |
8076 | dev = lower; | |
8077 | lower = netdev_sk_get_lower_dev(dev, sk); | |
8078 | } | |
8079 | ||
8080 | return dev; | |
8081 | } | |
8082 | EXPORT_SYMBOL(netdev_sk_get_lowest_dev); | |
8083 | ||
2ce1ee17 | 8084 | static void netdev_adjacent_add_links(struct net_device *dev) |
4c75431a AF |
8085 | { |
8086 | struct netdev_adjacent *iter; | |
8087 | ||
8088 | struct net *net = dev_net(dev); | |
8089 | ||
8090 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8091 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8092 | continue; |
8093 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8094 | &iter->dev->adj_list.lower); | |
8095 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
8096 | &dev->adj_list.upper); | |
8097 | } | |
8098 | ||
8099 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8100 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8101 | continue; |
8102 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8103 | &iter->dev->adj_list.upper); | |
8104 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
8105 | &dev->adj_list.lower); | |
8106 | } | |
8107 | } | |
8108 | ||
2ce1ee17 | 8109 | static void netdev_adjacent_del_links(struct net_device *dev) |
4c75431a AF |
8110 | { |
8111 | struct netdev_adjacent *iter; | |
8112 | ||
8113 | struct net *net = dev_net(dev); | |
8114 | ||
8115 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8116 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8117 | continue; |
8118 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8119 | &iter->dev->adj_list.lower); | |
8120 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8121 | &dev->adj_list.upper); | |
8122 | } | |
8123 | ||
8124 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8125 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8126 | continue; |
8127 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8128 | &iter->dev->adj_list.upper); | |
8129 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8130 | &dev->adj_list.lower); | |
8131 | } | |
8132 | } | |
8133 | ||
5bb025fa | 8134 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
402dae96 | 8135 | { |
5bb025fa | 8136 | struct netdev_adjacent *iter; |
402dae96 | 8137 | |
4c75431a AF |
8138 | struct net *net = dev_net(dev); |
8139 | ||
5bb025fa | 8140 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
be4da0e3 | 8141 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8142 | continue; |
5bb025fa VF |
8143 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8144 | &iter->dev->adj_list.lower); | |
8145 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8146 | &iter->dev->adj_list.lower); | |
8147 | } | |
402dae96 | 8148 | |
5bb025fa | 8149 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
be4da0e3 | 8150 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8151 | continue; |
5bb025fa VF |
8152 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8153 | &iter->dev->adj_list.upper); | |
8154 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8155 | &iter->dev->adj_list.upper); | |
8156 | } | |
402dae96 | 8157 | } |
402dae96 VF |
8158 | |
8159 | void *netdev_lower_dev_get_private(struct net_device *dev, | |
8160 | struct net_device *lower_dev) | |
8161 | { | |
8162 | struct netdev_adjacent *lower; | |
8163 | ||
8164 | if (!lower_dev) | |
8165 | return NULL; | |
6ea29da1 | 8166 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
402dae96 VF |
8167 | if (!lower) |
8168 | return NULL; | |
8169 | ||
8170 | return lower->private; | |
8171 | } | |
8172 | EXPORT_SYMBOL(netdev_lower_dev_get_private); | |
8173 | ||
4085ebe8 | 8174 | |
04d48266 | 8175 | /** |
c1639be9 | 8176 | * netdev_lower_state_changed - Dispatch event about lower device state change |
04d48266 JP |
8177 | * @lower_dev: device |
8178 | * @lower_state_info: state to dispatch | |
8179 | * | |
8180 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. | |
8181 | * The caller must hold the RTNL lock. | |
8182 | */ | |
8183 | void netdev_lower_state_changed(struct net_device *lower_dev, | |
8184 | void *lower_state_info) | |
8185 | { | |
51d0c047 DA |
8186 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
8187 | .info.dev = lower_dev, | |
8188 | }; | |
04d48266 JP |
8189 | |
8190 | ASSERT_RTNL(); | |
8191 | changelowerstate_info.lower_state_info = lower_state_info; | |
51d0c047 | 8192 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
04d48266 JP |
8193 | &changelowerstate_info.info); |
8194 | } | |
8195 | EXPORT_SYMBOL(netdev_lower_state_changed); | |
8196 | ||
b6c40d68 PM |
8197 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
8198 | { | |
d314774c SH |
8199 | const struct net_device_ops *ops = dev->netdev_ops; |
8200 | ||
d2615bf4 | 8201 | if (ops->ndo_change_rx_flags) |
d314774c | 8202 | ops->ndo_change_rx_flags(dev, flags); |
b6c40d68 PM |
8203 | } |
8204 | ||
991fb3f7 | 8205 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8206 | { |
b536db93 | 8207 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
8208 | kuid_t uid; |
8209 | kgid_t gid; | |
1da177e4 | 8210 | |
24023451 PM |
8211 | ASSERT_RTNL(); |
8212 | ||
dad9b335 WC |
8213 | dev->flags |= IFF_PROMISC; |
8214 | dev->promiscuity += inc; | |
8215 | if (dev->promiscuity == 0) { | |
8216 | /* | |
8217 | * Avoid overflow. | |
8218 | * If inc causes overflow, untouch promisc and return error. | |
8219 | */ | |
8220 | if (inc < 0) | |
8221 | dev->flags &= ~IFF_PROMISC; | |
8222 | else { | |
8223 | dev->promiscuity -= inc; | |
5b92be64 | 8224 | netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n"); |
dad9b335 WC |
8225 | return -EOVERFLOW; |
8226 | } | |
8227 | } | |
52609c0b | 8228 | if (dev->flags != old_flags) { |
7b6cd1ce JP |
8229 | pr_info("device %s %s promiscuous mode\n", |
8230 | dev->name, | |
8231 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
8232 | if (audit_enabled) { |
8233 | current_uid_gid(&uid, &gid); | |
cdfb6b34 RGB |
8234 | audit_log(audit_context(), GFP_ATOMIC, |
8235 | AUDIT_ANOM_PROMISCUOUS, | |
8236 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
8237 | dev->name, (dev->flags & IFF_PROMISC), | |
8238 | (old_flags & IFF_PROMISC), | |
8239 | from_kuid(&init_user_ns, audit_get_loginuid(current)), | |
8240 | from_kuid(&init_user_ns, uid), | |
8241 | from_kgid(&init_user_ns, gid), | |
8242 | audit_get_sessionid(current)); | |
8192b0c4 | 8243 | } |
24023451 | 8244 | |
b6c40d68 | 8245 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 8246 | } |
991fb3f7 ND |
8247 | if (notify) |
8248 | __dev_notify_flags(dev, old_flags, IFF_PROMISC); | |
dad9b335 | 8249 | return 0; |
1da177e4 LT |
8250 | } |
8251 | ||
4417da66 PM |
8252 | /** |
8253 | * dev_set_promiscuity - update promiscuity count on a device | |
8254 | * @dev: device | |
8255 | * @inc: modifier | |
8256 | * | |
8257 | * Add or remove promiscuity from a device. While the count in the device | |
8258 | * remains above zero the interface remains promiscuous. Once it hits zero | |
8259 | * the device reverts back to normal filtering operation. A negative inc | |
8260 | * value is used to drop promiscuity on the device. | |
dad9b335 | 8261 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 8262 | */ |
dad9b335 | 8263 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 8264 | { |
b536db93 | 8265 | unsigned int old_flags = dev->flags; |
dad9b335 | 8266 | int err; |
4417da66 | 8267 | |
991fb3f7 | 8268 | err = __dev_set_promiscuity(dev, inc, true); |
4b5a698e | 8269 | if (err < 0) |
dad9b335 | 8270 | return err; |
4417da66 PM |
8271 | if (dev->flags != old_flags) |
8272 | dev_set_rx_mode(dev); | |
dad9b335 | 8273 | return err; |
4417da66 | 8274 | } |
d1b19dff | 8275 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 8276 | |
991fb3f7 | 8277 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8278 | { |
991fb3f7 | 8279 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
1da177e4 | 8280 | |
24023451 PM |
8281 | ASSERT_RTNL(); |
8282 | ||
1da177e4 | 8283 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
8284 | dev->allmulti += inc; |
8285 | if (dev->allmulti == 0) { | |
8286 | /* | |
8287 | * Avoid overflow. | |
8288 | * If inc causes overflow, untouch allmulti and return error. | |
8289 | */ | |
8290 | if (inc < 0) | |
8291 | dev->flags &= ~IFF_ALLMULTI; | |
8292 | else { | |
8293 | dev->allmulti -= inc; | |
5b92be64 | 8294 | netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n"); |
dad9b335 WC |
8295 | return -EOVERFLOW; |
8296 | } | |
8297 | } | |
24023451 | 8298 | if (dev->flags ^ old_flags) { |
b6c40d68 | 8299 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 8300 | dev_set_rx_mode(dev); |
991fb3f7 ND |
8301 | if (notify) |
8302 | __dev_notify_flags(dev, old_flags, | |
8303 | dev->gflags ^ old_gflags); | |
24023451 | 8304 | } |
dad9b335 | 8305 | return 0; |
4417da66 | 8306 | } |
991fb3f7 ND |
8307 | |
8308 | /** | |
8309 | * dev_set_allmulti - update allmulti count on a device | |
8310 | * @dev: device | |
8311 | * @inc: modifier | |
8312 | * | |
8313 | * Add or remove reception of all multicast frames to a device. While the | |
8314 | * count in the device remains above zero the interface remains listening | |
8315 | * to all interfaces. Once it hits zero the device reverts back to normal | |
8316 | * filtering operation. A negative @inc value is used to drop the counter | |
8317 | * when releasing a resource needing all multicasts. | |
8318 | * Return 0 if successful or a negative errno code on error. | |
8319 | */ | |
8320 | ||
8321 | int dev_set_allmulti(struct net_device *dev, int inc) | |
8322 | { | |
8323 | return __dev_set_allmulti(dev, inc, true); | |
8324 | } | |
d1b19dff | 8325 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
8326 | |
8327 | /* | |
8328 | * Upload unicast and multicast address lists to device and | |
8329 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 8330 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
8331 | * are present. |
8332 | */ | |
8333 | void __dev_set_rx_mode(struct net_device *dev) | |
8334 | { | |
d314774c SH |
8335 | const struct net_device_ops *ops = dev->netdev_ops; |
8336 | ||
4417da66 PM |
8337 | /* dev_open will call this function so the list will stay sane. */ |
8338 | if (!(dev->flags&IFF_UP)) | |
8339 | return; | |
8340 | ||
8341 | if (!netif_device_present(dev)) | |
40b77c94 | 8342 | return; |
4417da66 | 8343 | |
01789349 | 8344 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
8345 | /* Unicast addresses changes may only happen under the rtnl, |
8346 | * therefore calling __dev_set_promiscuity here is safe. | |
8347 | */ | |
32e7bfc4 | 8348 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
991fb3f7 | 8349 | __dev_set_promiscuity(dev, 1, false); |
2d348d1f | 8350 | dev->uc_promisc = true; |
32e7bfc4 | 8351 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
991fb3f7 | 8352 | __dev_set_promiscuity(dev, -1, false); |
2d348d1f | 8353 | dev->uc_promisc = false; |
4417da66 | 8354 | } |
4417da66 | 8355 | } |
01789349 JP |
8356 | |
8357 | if (ops->ndo_set_rx_mode) | |
8358 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
8359 | } |
8360 | ||
8361 | void dev_set_rx_mode(struct net_device *dev) | |
8362 | { | |
b9e40857 | 8363 | netif_addr_lock_bh(dev); |
4417da66 | 8364 | __dev_set_rx_mode(dev); |
b9e40857 | 8365 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
8366 | } |
8367 | ||
f0db275a SH |
8368 | /** |
8369 | * dev_get_flags - get flags reported to userspace | |
8370 | * @dev: device | |
8371 | * | |
8372 | * Get the combination of flag bits exported through APIs to userspace. | |
8373 | */ | |
95c96174 | 8374 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 8375 | { |
95c96174 | 8376 | unsigned int flags; |
1da177e4 LT |
8377 | |
8378 | flags = (dev->flags & ~(IFF_PROMISC | | |
8379 | IFF_ALLMULTI | | |
b00055aa SR |
8380 | IFF_RUNNING | |
8381 | IFF_LOWER_UP | | |
8382 | IFF_DORMANT)) | | |
1da177e4 LT |
8383 | (dev->gflags & (IFF_PROMISC | |
8384 | IFF_ALLMULTI)); | |
8385 | ||
b00055aa SR |
8386 | if (netif_running(dev)) { |
8387 | if (netif_oper_up(dev)) | |
8388 | flags |= IFF_RUNNING; | |
8389 | if (netif_carrier_ok(dev)) | |
8390 | flags |= IFF_LOWER_UP; | |
8391 | if (netif_dormant(dev)) | |
8392 | flags |= IFF_DORMANT; | |
8393 | } | |
1da177e4 LT |
8394 | |
8395 | return flags; | |
8396 | } | |
d1b19dff | 8397 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 8398 | |
6d040321 PM |
8399 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
8400 | struct netlink_ext_ack *extack) | |
1da177e4 | 8401 | { |
b536db93 | 8402 | unsigned int old_flags = dev->flags; |
bd380811 | 8403 | int ret; |
1da177e4 | 8404 | |
24023451 PM |
8405 | ASSERT_RTNL(); |
8406 | ||
1da177e4 LT |
8407 | /* |
8408 | * Set the flags on our device. | |
8409 | */ | |
8410 | ||
8411 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
8412 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
8413 | IFF_AUTOMEDIA)) | | |
8414 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
8415 | IFF_ALLMULTI)); | |
8416 | ||
8417 | /* | |
8418 | * Load in the correct multicast list now the flags have changed. | |
8419 | */ | |
8420 | ||
b6c40d68 PM |
8421 | if ((old_flags ^ flags) & IFF_MULTICAST) |
8422 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 8423 | |
4417da66 | 8424 | dev_set_rx_mode(dev); |
1da177e4 LT |
8425 | |
8426 | /* | |
8427 | * Have we downed the interface. We handle IFF_UP ourselves | |
8428 | * according to user attempts to set it, rather than blindly | |
8429 | * setting it. | |
8430 | */ | |
8431 | ||
8432 | ret = 0; | |
7051b88a | 8433 | if ((old_flags ^ flags) & IFF_UP) { |
8434 | if (old_flags & IFF_UP) | |
8435 | __dev_close(dev); | |
8436 | else | |
40c900aa | 8437 | ret = __dev_open(dev, extack); |
7051b88a | 8438 | } |
1da177e4 | 8439 | |
1da177e4 | 8440 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff | 8441 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
991fb3f7 | 8442 | unsigned int old_flags = dev->flags; |
d1b19dff | 8443 | |
1da177e4 | 8444 | dev->gflags ^= IFF_PROMISC; |
991fb3f7 ND |
8445 | |
8446 | if (__dev_set_promiscuity(dev, inc, false) >= 0) | |
8447 | if (dev->flags != old_flags) | |
8448 | dev_set_rx_mode(dev); | |
1da177e4 LT |
8449 | } |
8450 | ||
8451 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
eb13da1a | 8452 | * is important. Some (broken) drivers set IFF_PROMISC, when |
8453 | * IFF_ALLMULTI is requested not asking us and not reporting. | |
1da177e4 LT |
8454 | */ |
8455 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
8456 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
8457 | ||
1da177e4 | 8458 | dev->gflags ^= IFF_ALLMULTI; |
991fb3f7 | 8459 | __dev_set_allmulti(dev, inc, false); |
1da177e4 LT |
8460 | } |
8461 | ||
bd380811 PM |
8462 | return ret; |
8463 | } | |
8464 | ||
a528c219 ND |
8465 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
8466 | unsigned int gchanges) | |
bd380811 PM |
8467 | { |
8468 | unsigned int changes = dev->flags ^ old_flags; | |
8469 | ||
a528c219 | 8470 | if (gchanges) |
7f294054 | 8471 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC); |
a528c219 | 8472 | |
bd380811 PM |
8473 | if (changes & IFF_UP) { |
8474 | if (dev->flags & IFF_UP) | |
8475 | call_netdevice_notifiers(NETDEV_UP, dev); | |
8476 | else | |
8477 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
8478 | } | |
8479 | ||
8480 | if (dev->flags & IFF_UP && | |
be9efd36 | 8481 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
51d0c047 DA |
8482 | struct netdev_notifier_change_info change_info = { |
8483 | .info = { | |
8484 | .dev = dev, | |
8485 | }, | |
8486 | .flags_changed = changes, | |
8487 | }; | |
be9efd36 | 8488 | |
51d0c047 | 8489 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
be9efd36 | 8490 | } |
bd380811 PM |
8491 | } |
8492 | ||
8493 | /** | |
8494 | * dev_change_flags - change device settings | |
8495 | * @dev: device | |
8496 | * @flags: device state flags | |
567c5e13 | 8497 | * @extack: netlink extended ack |
bd380811 PM |
8498 | * |
8499 | * Change settings on device based state flags. The flags are | |
8500 | * in the userspace exported format. | |
8501 | */ | |
567c5e13 PM |
8502 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
8503 | struct netlink_ext_ack *extack) | |
bd380811 | 8504 | { |
b536db93 | 8505 | int ret; |
991fb3f7 | 8506 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
bd380811 | 8507 | |
6d040321 | 8508 | ret = __dev_change_flags(dev, flags, extack); |
bd380811 PM |
8509 | if (ret < 0) |
8510 | return ret; | |
8511 | ||
991fb3f7 | 8512 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
a528c219 | 8513 | __dev_notify_flags(dev, old_flags, changes); |
1da177e4 LT |
8514 | return ret; |
8515 | } | |
d1b19dff | 8516 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 8517 | |
f51048c3 | 8518 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
2315dc91 VF |
8519 | { |
8520 | const struct net_device_ops *ops = dev->netdev_ops; | |
8521 | ||
8522 | if (ops->ndo_change_mtu) | |
8523 | return ops->ndo_change_mtu(dev, new_mtu); | |
8524 | ||
501a90c9 ED |
8525 | /* Pairs with all the lockless reads of dev->mtu in the stack */ |
8526 | WRITE_ONCE(dev->mtu, new_mtu); | |
2315dc91 VF |
8527 | return 0; |
8528 | } | |
f51048c3 | 8529 | EXPORT_SYMBOL(__dev_set_mtu); |
2315dc91 | 8530 | |
d836f5c6 ED |
8531 | int dev_validate_mtu(struct net_device *dev, int new_mtu, |
8532 | struct netlink_ext_ack *extack) | |
8533 | { | |
8534 | /* MTU must be positive, and in range */ | |
8535 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { | |
8536 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); | |
8537 | return -EINVAL; | |
8538 | } | |
8539 | ||
8540 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { | |
8541 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); | |
8542 | return -EINVAL; | |
8543 | } | |
8544 | return 0; | |
8545 | } | |
8546 | ||
f0db275a | 8547 | /** |
7a4c53be | 8548 | * dev_set_mtu_ext - Change maximum transfer unit |
f0db275a SH |
8549 | * @dev: device |
8550 | * @new_mtu: new transfer unit | |
7a4c53be | 8551 | * @extack: netlink extended ack |
f0db275a SH |
8552 | * |
8553 | * Change the maximum transfer size of the network device. | |
8554 | */ | |
7a4c53be SH |
8555 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
8556 | struct netlink_ext_ack *extack) | |
1da177e4 | 8557 | { |
2315dc91 | 8558 | int err, orig_mtu; |
1da177e4 LT |
8559 | |
8560 | if (new_mtu == dev->mtu) | |
8561 | return 0; | |
8562 | ||
d836f5c6 ED |
8563 | err = dev_validate_mtu(dev, new_mtu, extack); |
8564 | if (err) | |
8565 | return err; | |
1da177e4 LT |
8566 | |
8567 | if (!netif_device_present(dev)) | |
8568 | return -ENODEV; | |
8569 | ||
1d486bfb VF |
8570 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
8571 | err = notifier_to_errno(err); | |
8572 | if (err) | |
8573 | return err; | |
d314774c | 8574 | |
2315dc91 VF |
8575 | orig_mtu = dev->mtu; |
8576 | err = __dev_set_mtu(dev, new_mtu); | |
d314774c | 8577 | |
2315dc91 | 8578 | if (!err) { |
af7d6cce SD |
8579 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8580 | orig_mtu); | |
2315dc91 VF |
8581 | err = notifier_to_errno(err); |
8582 | if (err) { | |
8583 | /* setting mtu back and notifying everyone again, | |
8584 | * so that they have a chance to revert changes. | |
8585 | */ | |
8586 | __dev_set_mtu(dev, orig_mtu); | |
af7d6cce SD |
8587 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8588 | new_mtu); | |
2315dc91 VF |
8589 | } |
8590 | } | |
1da177e4 LT |
8591 | return err; |
8592 | } | |
7a4c53be SH |
8593 | |
8594 | int dev_set_mtu(struct net_device *dev, int new_mtu) | |
8595 | { | |
8596 | struct netlink_ext_ack extack; | |
8597 | int err; | |
8598 | ||
a6bcfc89 | 8599 | memset(&extack, 0, sizeof(extack)); |
7a4c53be | 8600 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
a6bcfc89 | 8601 | if (err && extack._msg) |
7a4c53be SH |
8602 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
8603 | return err; | |
8604 | } | |
d1b19dff | 8605 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 8606 | |
6a643ddb CW |
8607 | /** |
8608 | * dev_change_tx_queue_len - Change TX queue length of a netdevice | |
8609 | * @dev: device | |
8610 | * @new_len: new tx queue length | |
8611 | */ | |
8612 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) | |
8613 | { | |
8614 | unsigned int orig_len = dev->tx_queue_len; | |
8615 | int res; | |
8616 | ||
8617 | if (new_len != (unsigned int)new_len) | |
8618 | return -ERANGE; | |
8619 | ||
8620 | if (new_len != orig_len) { | |
8621 | dev->tx_queue_len = new_len; | |
8622 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
8623 | res = notifier_to_errno(res); | |
7effaf06 TT |
8624 | if (res) |
8625 | goto err_rollback; | |
8626 | res = dev_qdisc_change_tx_queue_len(dev); | |
8627 | if (res) | |
8628 | goto err_rollback; | |
6a643ddb CW |
8629 | } |
8630 | ||
8631 | return 0; | |
7effaf06 TT |
8632 | |
8633 | err_rollback: | |
8634 | netdev_err(dev, "refused to change device tx_queue_len\n"); | |
8635 | dev->tx_queue_len = orig_len; | |
8636 | return res; | |
6a643ddb CW |
8637 | } |
8638 | ||
cbda10fa VD |
8639 | /** |
8640 | * dev_set_group - Change group this device belongs to | |
8641 | * @dev: device | |
8642 | * @new_group: group this device should belong to | |
8643 | */ | |
8644 | void dev_set_group(struct net_device *dev, int new_group) | |
8645 | { | |
8646 | dev->group = new_group; | |
8647 | } | |
cbda10fa | 8648 | |
d59cdf94 PM |
8649 | /** |
8650 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. | |
8651 | * @dev: device | |
8652 | * @addr: new address | |
8653 | * @extack: netlink extended ack | |
8654 | */ | |
8655 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, | |
8656 | struct netlink_ext_ack *extack) | |
8657 | { | |
8658 | struct netdev_notifier_pre_changeaddr_info info = { | |
8659 | .info.dev = dev, | |
8660 | .info.extack = extack, | |
8661 | .dev_addr = addr, | |
8662 | }; | |
8663 | int rc; | |
8664 | ||
8665 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); | |
8666 | return notifier_to_errno(rc); | |
8667 | } | |
8668 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); | |
8669 | ||
f0db275a SH |
8670 | /** |
8671 | * dev_set_mac_address - Change Media Access Control Address | |
8672 | * @dev: device | |
8673 | * @sa: new address | |
3a37a963 | 8674 | * @extack: netlink extended ack |
f0db275a SH |
8675 | * |
8676 | * Change the hardware (MAC) address of the device | |
8677 | */ | |
3a37a963 PM |
8678 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
8679 | struct netlink_ext_ack *extack) | |
1da177e4 | 8680 | { |
d314774c | 8681 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
8682 | int err; |
8683 | ||
d314774c | 8684 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
8685 | return -EOPNOTSUPP; |
8686 | if (sa->sa_family != dev->type) | |
8687 | return -EINVAL; | |
8688 | if (!netif_device_present(dev)) | |
8689 | return -ENODEV; | |
d59cdf94 PM |
8690 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
8691 | if (err) | |
8692 | return err; | |
d314774c | 8693 | err = ops->ndo_set_mac_address(dev, sa); |
f6521516 JP |
8694 | if (err) |
8695 | return err; | |
fbdeca2d | 8696 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 8697 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 8698 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 8699 | return 0; |
1da177e4 | 8700 | } |
d1b19dff | 8701 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 8702 | |
3b23a32a CW |
8703 | static DECLARE_RWSEM(dev_addr_sem); |
8704 | ||
8705 | int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa, | |
8706 | struct netlink_ext_ack *extack) | |
8707 | { | |
8708 | int ret; | |
8709 | ||
8710 | down_write(&dev_addr_sem); | |
8711 | ret = dev_set_mac_address(dev, sa, extack); | |
8712 | up_write(&dev_addr_sem); | |
8713 | return ret; | |
8714 | } | |
8715 | EXPORT_SYMBOL(dev_set_mac_address_user); | |
8716 | ||
8717 | int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name) | |
8718 | { | |
8719 | size_t size = sizeof(sa->sa_data); | |
8720 | struct net_device *dev; | |
8721 | int ret = 0; | |
8722 | ||
8723 | down_read(&dev_addr_sem); | |
8724 | rcu_read_lock(); | |
8725 | ||
8726 | dev = dev_get_by_name_rcu(net, dev_name); | |
8727 | if (!dev) { | |
8728 | ret = -ENODEV; | |
8729 | goto unlock; | |
8730 | } | |
8731 | if (!dev->addr_len) | |
8732 | memset(sa->sa_data, 0, size); | |
8733 | else | |
8734 | memcpy(sa->sa_data, dev->dev_addr, | |
8735 | min_t(size_t, size, dev->addr_len)); | |
8736 | sa->sa_family = dev->type; | |
8737 | ||
8738 | unlock: | |
8739 | rcu_read_unlock(); | |
8740 | up_read(&dev_addr_sem); | |
8741 | return ret; | |
8742 | } | |
8743 | EXPORT_SYMBOL(dev_get_mac_address); | |
8744 | ||
4bf84c35 JP |
8745 | /** |
8746 | * dev_change_carrier - Change device carrier | |
8747 | * @dev: device | |
691b3b7e | 8748 | * @new_carrier: new value |
4bf84c35 JP |
8749 | * |
8750 | * Change device carrier | |
8751 | */ | |
8752 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
8753 | { | |
8754 | const struct net_device_ops *ops = dev->netdev_ops; | |
8755 | ||
8756 | if (!ops->ndo_change_carrier) | |
8757 | return -EOPNOTSUPP; | |
8758 | if (!netif_device_present(dev)) | |
8759 | return -ENODEV; | |
8760 | return ops->ndo_change_carrier(dev, new_carrier); | |
8761 | } | |
4bf84c35 | 8762 | |
66b52b0d JP |
8763 | /** |
8764 | * dev_get_phys_port_id - Get device physical port ID | |
8765 | * @dev: device | |
8766 | * @ppid: port ID | |
8767 | * | |
8768 | * Get device physical port ID | |
8769 | */ | |
8770 | int dev_get_phys_port_id(struct net_device *dev, | |
02637fce | 8771 | struct netdev_phys_item_id *ppid) |
66b52b0d JP |
8772 | { |
8773 | const struct net_device_ops *ops = dev->netdev_ops; | |
8774 | ||
8775 | if (!ops->ndo_get_phys_port_id) | |
8776 | return -EOPNOTSUPP; | |
8777 | return ops->ndo_get_phys_port_id(dev, ppid); | |
8778 | } | |
66b52b0d | 8779 | |
db24a904 DA |
8780 | /** |
8781 | * dev_get_phys_port_name - Get device physical port name | |
8782 | * @dev: device | |
8783 | * @name: port name | |
ed49e650 | 8784 | * @len: limit of bytes to copy to name |
db24a904 DA |
8785 | * |
8786 | * Get device physical port name | |
8787 | */ | |
8788 | int dev_get_phys_port_name(struct net_device *dev, | |
8789 | char *name, size_t len) | |
8790 | { | |
8791 | const struct net_device_ops *ops = dev->netdev_ops; | |
af3836df | 8792 | int err; |
db24a904 | 8793 | |
af3836df JP |
8794 | if (ops->ndo_get_phys_port_name) { |
8795 | err = ops->ndo_get_phys_port_name(dev, name, len); | |
8796 | if (err != -EOPNOTSUPP) | |
8797 | return err; | |
8798 | } | |
8799 | return devlink_compat_phys_port_name_get(dev, name, len); | |
db24a904 | 8800 | } |
db24a904 | 8801 | |
d6abc596 FF |
8802 | /** |
8803 | * dev_get_port_parent_id - Get the device's port parent identifier | |
8804 | * @dev: network device | |
8805 | * @ppid: pointer to a storage for the port's parent identifier | |
8806 | * @recurse: allow/disallow recursion to lower devices | |
8807 | * | |
8808 | * Get the devices's port parent identifier | |
8809 | */ | |
8810 | int dev_get_port_parent_id(struct net_device *dev, | |
8811 | struct netdev_phys_item_id *ppid, | |
8812 | bool recurse) | |
8813 | { | |
8814 | const struct net_device_ops *ops = dev->netdev_ops; | |
8815 | struct netdev_phys_item_id first = { }; | |
8816 | struct net_device *lower_dev; | |
8817 | struct list_head *iter; | |
7e1146e8 JP |
8818 | int err; |
8819 | ||
8820 | if (ops->ndo_get_port_parent_id) { | |
8821 | err = ops->ndo_get_port_parent_id(dev, ppid); | |
8822 | if (err != -EOPNOTSUPP) | |
8823 | return err; | |
8824 | } | |
d6abc596 | 8825 | |
7e1146e8 | 8826 | err = devlink_compat_switch_id_get(dev, ppid); |
c0288ae8 | 8827 | if (!recurse || err != -EOPNOTSUPP) |
7e1146e8 | 8828 | return err; |
d6abc596 | 8829 | |
d6abc596 | 8830 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
c0288ae8 | 8831 | err = dev_get_port_parent_id(lower_dev, ppid, true); |
d6abc596 FF |
8832 | if (err) |
8833 | break; | |
8834 | if (!first.id_len) | |
8835 | first = *ppid; | |
8836 | else if (memcmp(&first, ppid, sizeof(*ppid))) | |
e1b9efe6 | 8837 | return -EOPNOTSUPP; |
d6abc596 FF |
8838 | } |
8839 | ||
8840 | return err; | |
8841 | } | |
8842 | EXPORT_SYMBOL(dev_get_port_parent_id); | |
8843 | ||
8844 | /** | |
8845 | * netdev_port_same_parent_id - Indicate if two network devices have | |
8846 | * the same port parent identifier | |
8847 | * @a: first network device | |
8848 | * @b: second network device | |
8849 | */ | |
8850 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) | |
8851 | { | |
8852 | struct netdev_phys_item_id a_id = { }; | |
8853 | struct netdev_phys_item_id b_id = { }; | |
8854 | ||
8855 | if (dev_get_port_parent_id(a, &a_id, true) || | |
8856 | dev_get_port_parent_id(b, &b_id, true)) | |
8857 | return false; | |
8858 | ||
8859 | return netdev_phys_item_id_same(&a_id, &b_id); | |
8860 | } | |
8861 | EXPORT_SYMBOL(netdev_port_same_parent_id); | |
8862 | ||
d746d707 | 8863 | /** |
2106efda JK |
8864 | * dev_change_proto_down - set carrier according to proto_down. |
8865 | * | |
d746d707 AK |
8866 | * @dev: device |
8867 | * @proto_down: new value | |
d746d707 AK |
8868 | */ |
8869 | int dev_change_proto_down(struct net_device *dev, bool proto_down) | |
8870 | { | |
2106efda | 8871 | if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) |
d746d707 AK |
8872 | return -EOPNOTSUPP; |
8873 | if (!netif_device_present(dev)) | |
8874 | return -ENODEV; | |
b5899679 AR |
8875 | if (proto_down) |
8876 | netif_carrier_off(dev); | |
8877 | else | |
8878 | netif_carrier_on(dev); | |
8879 | dev->proto_down = proto_down; | |
8880 | return 0; | |
8881 | } | |
b5899679 | 8882 | |
829eb208 RP |
8883 | /** |
8884 | * dev_change_proto_down_reason - proto down reason | |
8885 | * | |
8886 | * @dev: device | |
8887 | * @mask: proto down mask | |
8888 | * @value: proto down value | |
8889 | */ | |
8890 | void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask, | |
8891 | u32 value) | |
8892 | { | |
8893 | int b; | |
8894 | ||
8895 | if (!mask) { | |
8896 | dev->proto_down_reason = value; | |
8897 | } else { | |
8898 | for_each_set_bit(b, &mask, 32) { | |
8899 | if (value & (1 << b)) | |
8900 | dev->proto_down_reason |= BIT(b); | |
8901 | else | |
8902 | dev->proto_down_reason &= ~BIT(b); | |
8903 | } | |
8904 | } | |
8905 | } | |
829eb208 | 8906 | |
aa8d3a71 AN |
8907 | struct bpf_xdp_link { |
8908 | struct bpf_link link; | |
8909 | struct net_device *dev; /* protected by rtnl_lock, no refcnt held */ | |
8910 | int flags; | |
8911 | }; | |
8912 | ||
c8a36f19 | 8913 | static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags) |
d67b9cd2 | 8914 | { |
7f0a8382 AN |
8915 | if (flags & XDP_FLAGS_HW_MODE) |
8916 | return XDP_MODE_HW; | |
8917 | if (flags & XDP_FLAGS_DRV_MODE) | |
8918 | return XDP_MODE_DRV; | |
c8a36f19 AN |
8919 | if (flags & XDP_FLAGS_SKB_MODE) |
8920 | return XDP_MODE_SKB; | |
8921 | return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB; | |
7f0a8382 | 8922 | } |
d67b9cd2 | 8923 | |
7f0a8382 AN |
8924 | static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode) |
8925 | { | |
8926 | switch (mode) { | |
8927 | case XDP_MODE_SKB: | |
8928 | return generic_xdp_install; | |
8929 | case XDP_MODE_DRV: | |
8930 | case XDP_MODE_HW: | |
8931 | return dev->netdev_ops->ndo_bpf; | |
8932 | default: | |
8933 | return NULL; | |
5d867245 | 8934 | } |
7f0a8382 | 8935 | } |
118b4aa2 | 8936 | |
aa8d3a71 AN |
8937 | static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev, |
8938 | enum bpf_xdp_mode mode) | |
8939 | { | |
8940 | return dev->xdp_state[mode].link; | |
8941 | } | |
8942 | ||
7f0a8382 AN |
8943 | static struct bpf_prog *dev_xdp_prog(struct net_device *dev, |
8944 | enum bpf_xdp_mode mode) | |
8945 | { | |
aa8d3a71 AN |
8946 | struct bpf_xdp_link *link = dev_xdp_link(dev, mode); |
8947 | ||
8948 | if (link) | |
8949 | return link->link.prog; | |
7f0a8382 AN |
8950 | return dev->xdp_state[mode].prog; |
8951 | } | |
8952 | ||
879af96f | 8953 | u8 dev_xdp_prog_count(struct net_device *dev) |
998f1729 THJ |
8954 | { |
8955 | u8 count = 0; | |
8956 | int i; | |
8957 | ||
8958 | for (i = 0; i < __MAX_XDP_MODE; i++) | |
8959 | if (dev->xdp_state[i].prog || dev->xdp_state[i].link) | |
8960 | count++; | |
8961 | return count; | |
8962 | } | |
879af96f | 8963 | EXPORT_SYMBOL_GPL(dev_xdp_prog_count); |
998f1729 | 8964 | |
7f0a8382 AN |
8965 | u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode) |
8966 | { | |
8967 | struct bpf_prog *prog = dev_xdp_prog(dev, mode); | |
118b4aa2 | 8968 | |
7f0a8382 AN |
8969 | return prog ? prog->aux->id : 0; |
8970 | } | |
58038695 | 8971 | |
aa8d3a71 AN |
8972 | static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode, |
8973 | struct bpf_xdp_link *link) | |
8974 | { | |
8975 | dev->xdp_state[mode].link = link; | |
8976 | dev->xdp_state[mode].prog = NULL; | |
d67b9cd2 DB |
8977 | } |
8978 | ||
7f0a8382 AN |
8979 | static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode, |
8980 | struct bpf_prog *prog) | |
8981 | { | |
aa8d3a71 | 8982 | dev->xdp_state[mode].link = NULL; |
7f0a8382 | 8983 | dev->xdp_state[mode].prog = prog; |
d67b9cd2 DB |
8984 | } |
8985 | ||
7f0a8382 AN |
8986 | static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, |
8987 | bpf_op_t bpf_op, struct netlink_ext_ack *extack, | |
8988 | u32 flags, struct bpf_prog *prog) | |
d67b9cd2 | 8989 | { |
f4e63525 | 8990 | struct netdev_bpf xdp; |
7e6897f9 BT |
8991 | int err; |
8992 | ||
d67b9cd2 | 8993 | memset(&xdp, 0, sizeof(xdp)); |
7f0a8382 | 8994 | xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG; |
d67b9cd2 | 8995 | xdp.extack = extack; |
32d60277 | 8996 | xdp.flags = flags; |
d67b9cd2 DB |
8997 | xdp.prog = prog; |
8998 | ||
7f0a8382 AN |
8999 | /* Drivers assume refcnt is already incremented (i.e, prog pointer is |
9000 | * "moved" into driver), so they don't increment it on their own, but | |
9001 | * they do decrement refcnt when program is detached or replaced. | |
9002 | * Given net_device also owns link/prog, we need to bump refcnt here | |
9003 | * to prevent drivers from underflowing it. | |
9004 | */ | |
9005 | if (prog) | |
9006 | bpf_prog_inc(prog); | |
7e6897f9 | 9007 | err = bpf_op(dev, &xdp); |
7f0a8382 AN |
9008 | if (err) { |
9009 | if (prog) | |
9010 | bpf_prog_put(prog); | |
9011 | return err; | |
9012 | } | |
7e6897f9 | 9013 | |
7f0a8382 AN |
9014 | if (mode != XDP_MODE_HW) |
9015 | bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog); | |
7e6897f9 | 9016 | |
7f0a8382 | 9017 | return 0; |
d67b9cd2 DB |
9018 | } |
9019 | ||
bd0b2e7f JK |
9020 | static void dev_xdp_uninstall(struct net_device *dev) |
9021 | { | |
aa8d3a71 | 9022 | struct bpf_xdp_link *link; |
7f0a8382 AN |
9023 | struct bpf_prog *prog; |
9024 | enum bpf_xdp_mode mode; | |
9025 | bpf_op_t bpf_op; | |
bd0b2e7f | 9026 | |
7f0a8382 | 9027 | ASSERT_RTNL(); |
bd0b2e7f | 9028 | |
7f0a8382 AN |
9029 | for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) { |
9030 | prog = dev_xdp_prog(dev, mode); | |
9031 | if (!prog) | |
9032 | continue; | |
bd0b2e7f | 9033 | |
7f0a8382 AN |
9034 | bpf_op = dev_xdp_bpf_op(dev, mode); |
9035 | if (!bpf_op) | |
9036 | continue; | |
bd0b2e7f | 9037 | |
7f0a8382 AN |
9038 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); |
9039 | ||
aa8d3a71 AN |
9040 | /* auto-detach link from net device */ |
9041 | link = dev_xdp_link(dev, mode); | |
9042 | if (link) | |
9043 | link->dev = NULL; | |
9044 | else | |
9045 | bpf_prog_put(prog); | |
9046 | ||
9047 | dev_xdp_set_link(dev, mode, NULL); | |
7f0a8382 | 9048 | } |
bd0b2e7f JK |
9049 | } |
9050 | ||
d4baa936 | 9051 | static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack, |
aa8d3a71 AN |
9052 | struct bpf_xdp_link *link, struct bpf_prog *new_prog, |
9053 | struct bpf_prog *old_prog, u32 flags) | |
a7862b45 | 9054 | { |
998f1729 | 9055 | unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES); |
d4baa936 | 9056 | struct bpf_prog *cur_prog; |
879af96f JM |
9057 | struct net_device *upper; |
9058 | struct list_head *iter; | |
d4baa936 | 9059 | enum bpf_xdp_mode mode; |
7f0a8382 | 9060 | bpf_op_t bpf_op; |
a7862b45 BB |
9061 | int err; |
9062 | ||
85de8576 DB |
9063 | ASSERT_RTNL(); |
9064 | ||
aa8d3a71 AN |
9065 | /* either link or prog attachment, never both */ |
9066 | if (link && (new_prog || old_prog)) | |
9067 | return -EINVAL; | |
9068 | /* link supports only XDP mode flags */ | |
9069 | if (link && (flags & ~XDP_FLAGS_MODES)) { | |
9070 | NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment"); | |
9071 | return -EINVAL; | |
9072 | } | |
998f1729 THJ |
9073 | /* just one XDP mode bit should be set, zero defaults to drv/skb mode */ |
9074 | if (num_modes > 1) { | |
d4baa936 AN |
9075 | NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set"); |
9076 | return -EINVAL; | |
9077 | } | |
998f1729 THJ |
9078 | /* avoid ambiguity if offload + drv/skb mode progs are both loaded */ |
9079 | if (!num_modes && dev_xdp_prog_count(dev) > 1) { | |
9080 | NL_SET_ERR_MSG(extack, | |
9081 | "More than one program loaded, unset mode is ambiguous"); | |
9082 | return -EINVAL; | |
9083 | } | |
d4baa936 AN |
9084 | /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */ |
9085 | if (old_prog && !(flags & XDP_FLAGS_REPLACE)) { | |
9086 | NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified"); | |
9087 | return -EINVAL; | |
01dde20c | 9088 | } |
a25717d2 | 9089 | |
c8a36f19 | 9090 | mode = dev_xdp_mode(dev, flags); |
aa8d3a71 AN |
9091 | /* can't replace attached link */ |
9092 | if (dev_xdp_link(dev, mode)) { | |
9093 | NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link"); | |
9094 | return -EBUSY; | |
01dde20c | 9095 | } |
c14a9f63 | 9096 | |
879af96f JM |
9097 | /* don't allow if an upper device already has a program */ |
9098 | netdev_for_each_upper_dev_rcu(dev, upper, iter) { | |
9099 | if (dev_xdp_prog_count(upper) > 0) { | |
9100 | NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program"); | |
9101 | return -EEXIST; | |
9102 | } | |
9103 | } | |
9104 | ||
d4baa936 | 9105 | cur_prog = dev_xdp_prog(dev, mode); |
aa8d3a71 AN |
9106 | /* can't replace attached prog with link */ |
9107 | if (link && cur_prog) { | |
9108 | NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link"); | |
9109 | return -EBUSY; | |
9110 | } | |
d4baa936 AN |
9111 | if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) { |
9112 | NL_SET_ERR_MSG(extack, "Active program does not match expected"); | |
9113 | return -EEXIST; | |
92234c8f | 9114 | } |
c14a9f63 | 9115 | |
aa8d3a71 AN |
9116 | /* put effective new program into new_prog */ |
9117 | if (link) | |
9118 | new_prog = link->link.prog; | |
85de8576 | 9119 | |
d4baa936 AN |
9120 | if (new_prog) { |
9121 | bool offload = mode == XDP_MODE_HW; | |
7f0a8382 AN |
9122 | enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB |
9123 | ? XDP_MODE_DRV : XDP_MODE_SKB; | |
441a3303 | 9124 | |
068d9d1e AN |
9125 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) { |
9126 | NL_SET_ERR_MSG(extack, "XDP program already attached"); | |
9127 | return -EBUSY; | |
9128 | } | |
d4baa936 | 9129 | if (!offload && dev_xdp_prog(dev, other_mode)) { |
7f0a8382 | 9130 | NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time"); |
d67b9cd2 | 9131 | return -EEXIST; |
01dde20c | 9132 | } |
d4baa936 | 9133 | if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) { |
7f0a8382 | 9134 | NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported"); |
441a3303 JK |
9135 | return -EINVAL; |
9136 | } | |
d4baa936 | 9137 | if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) { |
fbee97fe | 9138 | NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device"); |
fbee97fe DA |
9139 | return -EINVAL; |
9140 | } | |
d4baa936 AN |
9141 | if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) { |
9142 | NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device"); | |
92164774 LB |
9143 | return -EINVAL; |
9144 | } | |
d4baa936 | 9145 | } |
92164774 | 9146 | |
d4baa936 AN |
9147 | /* don't call drivers if the effective program didn't change */ |
9148 | if (new_prog != cur_prog) { | |
9149 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9150 | if (!bpf_op) { | |
9151 | NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode"); | |
9152 | return -EOPNOTSUPP; | |
c14a9f63 | 9153 | } |
a7862b45 | 9154 | |
d4baa936 AN |
9155 | err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog); |
9156 | if (err) | |
9157 | return err; | |
7f0a8382 | 9158 | } |
d4baa936 | 9159 | |
aa8d3a71 AN |
9160 | if (link) |
9161 | dev_xdp_set_link(dev, mode, link); | |
9162 | else | |
9163 | dev_xdp_set_prog(dev, mode, new_prog); | |
d4baa936 AN |
9164 | if (cur_prog) |
9165 | bpf_prog_put(cur_prog); | |
a7862b45 | 9166 | |
7f0a8382 | 9167 | return 0; |
a7862b45 | 9168 | } |
a7862b45 | 9169 | |
aa8d3a71 AN |
9170 | static int dev_xdp_attach_link(struct net_device *dev, |
9171 | struct netlink_ext_ack *extack, | |
9172 | struct bpf_xdp_link *link) | |
9173 | { | |
9174 | return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags); | |
9175 | } | |
9176 | ||
9177 | static int dev_xdp_detach_link(struct net_device *dev, | |
9178 | struct netlink_ext_ack *extack, | |
9179 | struct bpf_xdp_link *link) | |
9180 | { | |
9181 | enum bpf_xdp_mode mode; | |
9182 | bpf_op_t bpf_op; | |
9183 | ||
9184 | ASSERT_RTNL(); | |
9185 | ||
c8a36f19 | 9186 | mode = dev_xdp_mode(dev, link->flags); |
aa8d3a71 AN |
9187 | if (dev_xdp_link(dev, mode) != link) |
9188 | return -EINVAL; | |
9189 | ||
9190 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9191 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); | |
9192 | dev_xdp_set_link(dev, mode, NULL); | |
9193 | return 0; | |
9194 | } | |
9195 | ||
9196 | static void bpf_xdp_link_release(struct bpf_link *link) | |
9197 | { | |
9198 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9199 | ||
9200 | rtnl_lock(); | |
9201 | ||
9202 | /* if racing with net_device's tear down, xdp_link->dev might be | |
9203 | * already NULL, in which case link was already auto-detached | |
9204 | */ | |
73b11c2a | 9205 | if (xdp_link->dev) { |
aa8d3a71 | 9206 | WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link)); |
73b11c2a AN |
9207 | xdp_link->dev = NULL; |
9208 | } | |
aa8d3a71 AN |
9209 | |
9210 | rtnl_unlock(); | |
9211 | } | |
9212 | ||
73b11c2a AN |
9213 | static int bpf_xdp_link_detach(struct bpf_link *link) |
9214 | { | |
9215 | bpf_xdp_link_release(link); | |
9216 | return 0; | |
9217 | } | |
9218 | ||
aa8d3a71 AN |
9219 | static void bpf_xdp_link_dealloc(struct bpf_link *link) |
9220 | { | |
9221 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9222 | ||
9223 | kfree(xdp_link); | |
9224 | } | |
9225 | ||
c1931c97 AN |
9226 | static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link, |
9227 | struct seq_file *seq) | |
9228 | { | |
9229 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9230 | u32 ifindex = 0; | |
9231 | ||
9232 | rtnl_lock(); | |
9233 | if (xdp_link->dev) | |
9234 | ifindex = xdp_link->dev->ifindex; | |
9235 | rtnl_unlock(); | |
9236 | ||
9237 | seq_printf(seq, "ifindex:\t%u\n", ifindex); | |
9238 | } | |
9239 | ||
9240 | static int bpf_xdp_link_fill_link_info(const struct bpf_link *link, | |
9241 | struct bpf_link_info *info) | |
9242 | { | |
9243 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9244 | u32 ifindex = 0; | |
9245 | ||
9246 | rtnl_lock(); | |
9247 | if (xdp_link->dev) | |
9248 | ifindex = xdp_link->dev->ifindex; | |
9249 | rtnl_unlock(); | |
9250 | ||
9251 | info->xdp.ifindex = ifindex; | |
9252 | return 0; | |
9253 | } | |
9254 | ||
026a4c28 AN |
9255 | static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, |
9256 | struct bpf_prog *old_prog) | |
9257 | { | |
9258 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9259 | enum bpf_xdp_mode mode; | |
9260 | bpf_op_t bpf_op; | |
9261 | int err = 0; | |
9262 | ||
9263 | rtnl_lock(); | |
9264 | ||
9265 | /* link might have been auto-released already, so fail */ | |
9266 | if (!xdp_link->dev) { | |
9267 | err = -ENOLINK; | |
9268 | goto out_unlock; | |
9269 | } | |
9270 | ||
9271 | if (old_prog && link->prog != old_prog) { | |
9272 | err = -EPERM; | |
9273 | goto out_unlock; | |
9274 | } | |
9275 | old_prog = link->prog; | |
382778ed THJ |
9276 | if (old_prog->type != new_prog->type || |
9277 | old_prog->expected_attach_type != new_prog->expected_attach_type) { | |
9278 | err = -EINVAL; | |
9279 | goto out_unlock; | |
9280 | } | |
9281 | ||
026a4c28 AN |
9282 | if (old_prog == new_prog) { |
9283 | /* no-op, don't disturb drivers */ | |
9284 | bpf_prog_put(new_prog); | |
9285 | goto out_unlock; | |
9286 | } | |
9287 | ||
c8a36f19 | 9288 | mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags); |
026a4c28 AN |
9289 | bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode); |
9290 | err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL, | |
9291 | xdp_link->flags, new_prog); | |
9292 | if (err) | |
9293 | goto out_unlock; | |
9294 | ||
9295 | old_prog = xchg(&link->prog, new_prog); | |
9296 | bpf_prog_put(old_prog); | |
9297 | ||
9298 | out_unlock: | |
9299 | rtnl_unlock(); | |
9300 | return err; | |
9301 | } | |
9302 | ||
aa8d3a71 AN |
9303 | static const struct bpf_link_ops bpf_xdp_link_lops = { |
9304 | .release = bpf_xdp_link_release, | |
9305 | .dealloc = bpf_xdp_link_dealloc, | |
73b11c2a | 9306 | .detach = bpf_xdp_link_detach, |
c1931c97 AN |
9307 | .show_fdinfo = bpf_xdp_link_show_fdinfo, |
9308 | .fill_link_info = bpf_xdp_link_fill_link_info, | |
026a4c28 | 9309 | .update_prog = bpf_xdp_link_update, |
aa8d3a71 AN |
9310 | }; |
9311 | ||
9312 | int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
9313 | { | |
9314 | struct net *net = current->nsproxy->net_ns; | |
9315 | struct bpf_link_primer link_primer; | |
9316 | struct bpf_xdp_link *link; | |
9317 | struct net_device *dev; | |
9318 | int err, fd; | |
9319 | ||
5acc7d3e | 9320 | rtnl_lock(); |
aa8d3a71 | 9321 | dev = dev_get_by_index(net, attr->link_create.target_ifindex); |
5acc7d3e XZ |
9322 | if (!dev) { |
9323 | rtnl_unlock(); | |
aa8d3a71 | 9324 | return -EINVAL; |
5acc7d3e | 9325 | } |
aa8d3a71 AN |
9326 | |
9327 | link = kzalloc(sizeof(*link), GFP_USER); | |
9328 | if (!link) { | |
9329 | err = -ENOMEM; | |
5acc7d3e | 9330 | goto unlock; |
aa8d3a71 AN |
9331 | } |
9332 | ||
9333 | bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog); | |
9334 | link->dev = dev; | |
9335 | link->flags = attr->link_create.flags; | |
9336 | ||
9337 | err = bpf_link_prime(&link->link, &link_primer); | |
9338 | if (err) { | |
9339 | kfree(link); | |
5acc7d3e | 9340 | goto unlock; |
aa8d3a71 AN |
9341 | } |
9342 | ||
aa8d3a71 AN |
9343 | err = dev_xdp_attach_link(dev, NULL, link); |
9344 | rtnl_unlock(); | |
9345 | ||
9346 | if (err) { | |
5acc7d3e | 9347 | link->dev = NULL; |
aa8d3a71 AN |
9348 | bpf_link_cleanup(&link_primer); |
9349 | goto out_put_dev; | |
9350 | } | |
9351 | ||
9352 | fd = bpf_link_settle(&link_primer); | |
9353 | /* link itself doesn't hold dev's refcnt to not complicate shutdown */ | |
9354 | dev_put(dev); | |
9355 | return fd; | |
9356 | ||
5acc7d3e XZ |
9357 | unlock: |
9358 | rtnl_unlock(); | |
9359 | ||
aa8d3a71 AN |
9360 | out_put_dev: |
9361 | dev_put(dev); | |
9362 | return err; | |
9363 | } | |
9364 | ||
d4baa936 AN |
9365 | /** |
9366 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path | |
9367 | * @dev: device | |
9368 | * @extack: netlink extended ack | |
9369 | * @fd: new program fd or negative value to clear | |
9370 | * @expected_fd: old program fd that userspace expects to replace or clear | |
9371 | * @flags: xdp-related flags | |
9372 | * | |
9373 | * Set or clear a bpf program for a device | |
9374 | */ | |
9375 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, | |
9376 | int fd, int expected_fd, u32 flags) | |
9377 | { | |
c8a36f19 | 9378 | enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags); |
d4baa936 AN |
9379 | struct bpf_prog *new_prog = NULL, *old_prog = NULL; |
9380 | int err; | |
9381 | ||
9382 | ASSERT_RTNL(); | |
9383 | ||
9384 | if (fd >= 0) { | |
9385 | new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, | |
9386 | mode != XDP_MODE_SKB); | |
9387 | if (IS_ERR(new_prog)) | |
9388 | return PTR_ERR(new_prog); | |
9389 | } | |
9390 | ||
9391 | if (expected_fd >= 0) { | |
9392 | old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, | |
9393 | mode != XDP_MODE_SKB); | |
9394 | if (IS_ERR(old_prog)) { | |
9395 | err = PTR_ERR(old_prog); | |
9396 | old_prog = NULL; | |
9397 | goto err_out; | |
c14a9f63 | 9398 | } |
a7862b45 BB |
9399 | } |
9400 | ||
aa8d3a71 | 9401 | err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags); |
a7862b45 | 9402 | |
d4baa936 AN |
9403 | err_out: |
9404 | if (err && new_prog) | |
9405 | bpf_prog_put(new_prog); | |
9406 | if (old_prog) | |
9407 | bpf_prog_put(old_prog); | |
a7862b45 BB |
9408 | return err; |
9409 | } | |
a7862b45 | 9410 | |
1da177e4 LT |
9411 | /** |
9412 | * dev_new_index - allocate an ifindex | |
c4ea43c5 | 9413 | * @net: the applicable net namespace |
1da177e4 LT |
9414 | * |
9415 | * Returns a suitable unique value for a new device interface | |
9416 | * number. The caller must hold the rtnl semaphore or the | |
9417 | * dev_base_lock to be sure it remains unique. | |
9418 | */ | |
881d966b | 9419 | static int dev_new_index(struct net *net) |
1da177e4 | 9420 | { |
aa79e66e | 9421 | int ifindex = net->ifindex; |
f4563a75 | 9422 | |
1da177e4 LT |
9423 | for (;;) { |
9424 | if (++ifindex <= 0) | |
9425 | ifindex = 1; | |
881d966b | 9426 | if (!__dev_get_by_index(net, ifindex)) |
aa79e66e | 9427 | return net->ifindex = ifindex; |
1da177e4 LT |
9428 | } |
9429 | } | |
9430 | ||
1da177e4 | 9431 | /* Delayed registration/unregisteration */ |
0b5c21bb | 9432 | LIST_HEAD(net_todo_list); |
200b916f | 9433 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
1da177e4 | 9434 | |
6f05f629 | 9435 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 9436 | { |
1da177e4 | 9437 | list_add_tail(&dev->todo_list, &net_todo_list); |
ede6c39c | 9438 | atomic_inc(&dev_net(dev)->dev_unreg_count); |
1da177e4 LT |
9439 | } |
9440 | ||
fd867d51 JW |
9441 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
9442 | struct net_device *upper, netdev_features_t features) | |
9443 | { | |
9444 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9445 | netdev_features_t feature; | |
5ba3f7d6 | 9446 | int feature_bit; |
fd867d51 | 9447 | |
3b89ea9c | 9448 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9449 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9450 | if (!(upper->wanted_features & feature) |
9451 | && (features & feature)) { | |
9452 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", | |
9453 | &feature, upper->name); | |
9454 | features &= ~feature; | |
9455 | } | |
9456 | } | |
9457 | ||
9458 | return features; | |
9459 | } | |
9460 | ||
9461 | static void netdev_sync_lower_features(struct net_device *upper, | |
9462 | struct net_device *lower, netdev_features_t features) | |
9463 | { | |
9464 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9465 | netdev_features_t feature; | |
5ba3f7d6 | 9466 | int feature_bit; |
fd867d51 | 9467 | |
3b89ea9c | 9468 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9469 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9470 | if (!(features & feature) && (lower->features & feature)) { |
9471 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", | |
9472 | &feature, lower->name); | |
9473 | lower->wanted_features &= ~feature; | |
dd912306 | 9474 | __netdev_update_features(lower); |
fd867d51 JW |
9475 | |
9476 | if (unlikely(lower->features & feature)) | |
9477 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", | |
9478 | &feature, lower->name); | |
dd912306 CW |
9479 | else |
9480 | netdev_features_change(lower); | |
fd867d51 JW |
9481 | } |
9482 | } | |
9483 | } | |
9484 | ||
c8f44aff MM |
9485 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
9486 | netdev_features_t features) | |
b63365a2 | 9487 | { |
57422dc5 MM |
9488 | /* Fix illegal checksum combinations */ |
9489 | if ((features & NETIF_F_HW_CSUM) && | |
9490 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 9491 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
9492 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
9493 | } | |
9494 | ||
b63365a2 | 9495 | /* TSO requires that SG is present as well. */ |
ea2d3688 | 9496 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 9497 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 9498 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
9499 | } |
9500 | ||
ec5f0615 PS |
9501 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
9502 | !(features & NETIF_F_IP_CSUM)) { | |
9503 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); | |
9504 | features &= ~NETIF_F_TSO; | |
9505 | features &= ~NETIF_F_TSO_ECN; | |
9506 | } | |
9507 | ||
9508 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && | |
9509 | !(features & NETIF_F_IPV6_CSUM)) { | |
9510 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); | |
9511 | features &= ~NETIF_F_TSO6; | |
9512 | } | |
9513 | ||
b1dc497b AD |
9514 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
9515 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) | |
9516 | features &= ~NETIF_F_TSO_MANGLEID; | |
9517 | ||
31d8b9e0 BH |
9518 | /* TSO ECN requires that TSO is present as well. */ |
9519 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
9520 | features &= ~NETIF_F_TSO_ECN; | |
9521 | ||
212b573f MM |
9522 | /* Software GSO depends on SG. */ |
9523 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 9524 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
9525 | features &= ~NETIF_F_GSO; |
9526 | } | |
9527 | ||
802ab55a AD |
9528 | /* GSO partial features require GSO partial be set */ |
9529 | if ((features & dev->gso_partial_features) && | |
9530 | !(features & NETIF_F_GSO_PARTIAL)) { | |
9531 | netdev_dbg(dev, | |
9532 | "Dropping partially supported GSO features since no GSO partial.\n"); | |
9533 | features &= ~dev->gso_partial_features; | |
9534 | } | |
9535 | ||
fb1f5f79 MC |
9536 | if (!(features & NETIF_F_RXCSUM)) { |
9537 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet | |
9538 | * successfully merged by hardware must also have the | |
9539 | * checksum verified by hardware. If the user does not | |
9540 | * want to enable RXCSUM, logically, we should disable GRO_HW. | |
9541 | */ | |
9542 | if (features & NETIF_F_GRO_HW) { | |
9543 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); | |
9544 | features &= ~NETIF_F_GRO_HW; | |
9545 | } | |
9546 | } | |
9547 | ||
de8d5ab2 GP |
9548 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
9549 | if (features & NETIF_F_RXFCS) { | |
9550 | if (features & NETIF_F_LRO) { | |
9551 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); | |
9552 | features &= ~NETIF_F_LRO; | |
9553 | } | |
9554 | ||
9555 | if (features & NETIF_F_GRO_HW) { | |
9556 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); | |
9557 | features &= ~NETIF_F_GRO_HW; | |
9558 | } | |
e6c6a929 GP |
9559 | } |
9560 | ||
54b2b3ec BB |
9561 | if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) { |
9562 | netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n"); | |
9563 | features &= ~NETIF_F_LRO; | |
9564 | } | |
9565 | ||
25537d71 TT |
9566 | if (features & NETIF_F_HW_TLS_TX) { |
9567 | bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) == | |
9568 | (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); | |
9569 | bool hw_csum = features & NETIF_F_HW_CSUM; | |
9570 | ||
9571 | if (!ip_csum && !hw_csum) { | |
9572 | netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n"); | |
9573 | features &= ~NETIF_F_HW_TLS_TX; | |
9574 | } | |
ae0b04b2 TT |
9575 | } |
9576 | ||
a3eb4e9d TT |
9577 | if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) { |
9578 | netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n"); | |
9579 | features &= ~NETIF_F_HW_TLS_RX; | |
9580 | } | |
9581 | ||
b63365a2 HX |
9582 | return features; |
9583 | } | |
b63365a2 | 9584 | |
6cb6a27c | 9585 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 9586 | { |
fd867d51 | 9587 | struct net_device *upper, *lower; |
c8f44aff | 9588 | netdev_features_t features; |
fd867d51 | 9589 | struct list_head *iter; |
e7868a85 | 9590 | int err = -1; |
5455c699 | 9591 | |
87267485 MM |
9592 | ASSERT_RTNL(); |
9593 | ||
5455c699 MM |
9594 | features = netdev_get_wanted_features(dev); |
9595 | ||
9596 | if (dev->netdev_ops->ndo_fix_features) | |
9597 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
9598 | ||
9599 | /* driver might be less strict about feature dependencies */ | |
9600 | features = netdev_fix_features(dev, features); | |
9601 | ||
4250b75b | 9602 | /* some features can't be enabled if they're off on an upper device */ |
fd867d51 JW |
9603 | netdev_for_each_upper_dev_rcu(dev, upper, iter) |
9604 | features = netdev_sync_upper_features(dev, upper, features); | |
9605 | ||
5455c699 | 9606 | if (dev->features == features) |
e7868a85 | 9607 | goto sync_lower; |
5455c699 | 9608 | |
c8f44aff MM |
9609 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
9610 | &dev->features, &features); | |
5455c699 MM |
9611 | |
9612 | if (dev->netdev_ops->ndo_set_features) | |
9613 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
5f8dc33e NA |
9614 | else |
9615 | err = 0; | |
5455c699 | 9616 | |
6cb6a27c | 9617 | if (unlikely(err < 0)) { |
5455c699 | 9618 | netdev_err(dev, |
c8f44aff MM |
9619 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
9620 | err, &features, &dev->features); | |
17b85d29 NA |
9621 | /* return non-0 since some features might have changed and |
9622 | * it's better to fire a spurious notification than miss it | |
9623 | */ | |
9624 | return -1; | |
6cb6a27c MM |
9625 | } |
9626 | ||
e7868a85 | 9627 | sync_lower: |
fd867d51 JW |
9628 | /* some features must be disabled on lower devices when disabled |
9629 | * on an upper device (think: bonding master or bridge) | |
9630 | */ | |
9631 | netdev_for_each_lower_dev(dev, lower, iter) | |
9632 | netdev_sync_lower_features(dev, lower, features); | |
9633 | ||
ae847f40 SD |
9634 | if (!err) { |
9635 | netdev_features_t diff = features ^ dev->features; | |
9636 | ||
9637 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9638 | /* udp_tunnel_{get,drop}_rx_info both need | |
9639 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the | |
9640 | * device, or they won't do anything. | |
9641 | * Thus we need to update dev->features | |
9642 | * *before* calling udp_tunnel_get_rx_info, | |
9643 | * but *after* calling udp_tunnel_drop_rx_info. | |
9644 | */ | |
9645 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9646 | dev->features = features; | |
9647 | udp_tunnel_get_rx_info(dev); | |
9648 | } else { | |
9649 | udp_tunnel_drop_rx_info(dev); | |
9650 | } | |
9651 | } | |
9652 | ||
9daae9bd GP |
9653 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
9654 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { | |
9655 | dev->features = features; | |
9656 | err |= vlan_get_rx_ctag_filter_info(dev); | |
9657 | } else { | |
9658 | vlan_drop_rx_ctag_filter_info(dev); | |
9659 | } | |
9660 | } | |
9661 | ||
9662 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9663 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9664 | dev->features = features; | |
9665 | err |= vlan_get_rx_stag_filter_info(dev); | |
9666 | } else { | |
9667 | vlan_drop_rx_stag_filter_info(dev); | |
9668 | } | |
9669 | } | |
9670 | ||
6cb6a27c | 9671 | dev->features = features; |
ae847f40 | 9672 | } |
6cb6a27c | 9673 | |
e7868a85 | 9674 | return err < 0 ? 0 : 1; |
6cb6a27c MM |
9675 | } |
9676 | ||
afe12cc8 MM |
9677 | /** |
9678 | * netdev_update_features - recalculate device features | |
9679 | * @dev: the device to check | |
9680 | * | |
9681 | * Recalculate dev->features set and send notifications if it | |
9682 | * has changed. Should be called after driver or hardware dependent | |
9683 | * conditions might have changed that influence the features. | |
9684 | */ | |
6cb6a27c MM |
9685 | void netdev_update_features(struct net_device *dev) |
9686 | { | |
9687 | if (__netdev_update_features(dev)) | |
9688 | netdev_features_change(dev); | |
5455c699 MM |
9689 | } |
9690 | EXPORT_SYMBOL(netdev_update_features); | |
9691 | ||
afe12cc8 MM |
9692 | /** |
9693 | * netdev_change_features - recalculate device features | |
9694 | * @dev: the device to check | |
9695 | * | |
9696 | * Recalculate dev->features set and send notifications even | |
9697 | * if they have not changed. Should be called instead of | |
9698 | * netdev_update_features() if also dev->vlan_features might | |
9699 | * have changed to allow the changes to be propagated to stacked | |
9700 | * VLAN devices. | |
9701 | */ | |
9702 | void netdev_change_features(struct net_device *dev) | |
9703 | { | |
9704 | __netdev_update_features(dev); | |
9705 | netdev_features_change(dev); | |
9706 | } | |
9707 | EXPORT_SYMBOL(netdev_change_features); | |
9708 | ||
fc4a7489 PM |
9709 | /** |
9710 | * netif_stacked_transfer_operstate - transfer operstate | |
9711 | * @rootdev: the root or lower level device to transfer state from | |
9712 | * @dev: the device to transfer operstate to | |
9713 | * | |
9714 | * Transfer operational state from root to device. This is normally | |
9715 | * called when a stacking relationship exists between the root | |
9716 | * device and the device(a leaf device). | |
9717 | */ | |
9718 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
9719 | struct net_device *dev) | |
9720 | { | |
9721 | if (rootdev->operstate == IF_OPER_DORMANT) | |
9722 | netif_dormant_on(dev); | |
9723 | else | |
9724 | netif_dormant_off(dev); | |
9725 | ||
eec517cd AL |
9726 | if (rootdev->operstate == IF_OPER_TESTING) |
9727 | netif_testing_on(dev); | |
9728 | else | |
9729 | netif_testing_off(dev); | |
9730 | ||
0575c86b ZS |
9731 | if (netif_carrier_ok(rootdev)) |
9732 | netif_carrier_on(dev); | |
9733 | else | |
9734 | netif_carrier_off(dev); | |
fc4a7489 PM |
9735 | } |
9736 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
9737 | ||
1b4bf461 ED |
9738 | static int netif_alloc_rx_queues(struct net_device *dev) |
9739 | { | |
1b4bf461 | 9740 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 9741 | struct netdev_rx_queue *rx; |
10595902 | 9742 | size_t sz = count * sizeof(*rx); |
e817f856 | 9743 | int err = 0; |
1b4bf461 | 9744 | |
bd25fa7b | 9745 | BUG_ON(count < 1); |
1b4bf461 | 9746 | |
c948f51c | 9747 | rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9748 | if (!rx) |
9749 | return -ENOMEM; | |
9750 | ||
bd25fa7b TH |
9751 | dev->_rx = rx; |
9752 | ||
e817f856 | 9753 | for (i = 0; i < count; i++) { |
fe822240 | 9754 | rx[i].dev = dev; |
e817f856 JDB |
9755 | |
9756 | /* XDP RX-queue setup */ | |
b02e5a0e | 9757 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0); |
e817f856 JDB |
9758 | if (err < 0) |
9759 | goto err_rxq_info; | |
9760 | } | |
1b4bf461 | 9761 | return 0; |
e817f856 JDB |
9762 | |
9763 | err_rxq_info: | |
9764 | /* Rollback successful reg's and free other resources */ | |
9765 | while (i--) | |
9766 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); | |
141b52a9 | 9767 | kvfree(dev->_rx); |
e817f856 JDB |
9768 | dev->_rx = NULL; |
9769 | return err; | |
9770 | } | |
9771 | ||
9772 | static void netif_free_rx_queues(struct net_device *dev) | |
9773 | { | |
9774 | unsigned int i, count = dev->num_rx_queues; | |
e817f856 JDB |
9775 | |
9776 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ | |
9777 | if (!dev->_rx) | |
9778 | return; | |
9779 | ||
e817f856 | 9780 | for (i = 0; i < count; i++) |
82aaff2f JK |
9781 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
9782 | ||
9783 | kvfree(dev->_rx); | |
1b4bf461 ED |
9784 | } |
9785 | ||
aa942104 CG |
9786 | static void netdev_init_one_queue(struct net_device *dev, |
9787 | struct netdev_queue *queue, void *_unused) | |
9788 | { | |
9789 | /* Initialize queue lock */ | |
9790 | spin_lock_init(&queue->_xmit_lock); | |
1a33e10e | 9791 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); |
aa942104 | 9792 | queue->xmit_lock_owner = -1; |
b236da69 | 9793 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 9794 | queue->dev = dev; |
114cf580 TH |
9795 | #ifdef CONFIG_BQL |
9796 | dql_init(&queue->dql, HZ); | |
9797 | #endif | |
aa942104 CG |
9798 | } |
9799 | ||
60877a32 ED |
9800 | static void netif_free_tx_queues(struct net_device *dev) |
9801 | { | |
4cb28970 | 9802 | kvfree(dev->_tx); |
60877a32 ED |
9803 | } |
9804 | ||
e6484930 TH |
9805 | static int netif_alloc_netdev_queues(struct net_device *dev) |
9806 | { | |
9807 | unsigned int count = dev->num_tx_queues; | |
9808 | struct netdev_queue *tx; | |
60877a32 | 9809 | size_t sz = count * sizeof(*tx); |
e6484930 | 9810 | |
d339727c ED |
9811 | if (count < 1 || count > 0xffff) |
9812 | return -EINVAL; | |
62b5942a | 9813 | |
c948f51c | 9814 | tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9815 | if (!tx) |
9816 | return -ENOMEM; | |
9817 | ||
e6484930 | 9818 | dev->_tx = tx; |
1d24eb48 | 9819 | |
e6484930 TH |
9820 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
9821 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
9822 | |
9823 | return 0; | |
e6484930 TH |
9824 | } |
9825 | ||
a2029240 DV |
9826 | void netif_tx_stop_all_queues(struct net_device *dev) |
9827 | { | |
9828 | unsigned int i; | |
9829 | ||
9830 | for (i = 0; i < dev->num_tx_queues; i++) { | |
9831 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
f4563a75 | 9832 | |
a2029240 DV |
9833 | netif_tx_stop_queue(txq); |
9834 | } | |
9835 | } | |
9836 | EXPORT_SYMBOL(netif_tx_stop_all_queues); | |
9837 | ||
1da177e4 LT |
9838 | /** |
9839 | * register_netdevice - register a network device | |
9840 | * @dev: device to register | |
9841 | * | |
9842 | * Take a completed network device structure and add it to the kernel | |
9843 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
9844 | * chain. 0 is returned on success. A negative errno code is returned | |
9845 | * on a failure to set up the device, or if the name is a duplicate. | |
9846 | * | |
9847 | * Callers must hold the rtnl semaphore. You may want | |
9848 | * register_netdev() instead of this. | |
9849 | * | |
9850 | * BUGS: | |
9851 | * The locking appears insufficient to guarantee two parallel registers | |
9852 | * will not get the same name. | |
9853 | */ | |
9854 | ||
9855 | int register_netdevice(struct net_device *dev) | |
9856 | { | |
1da177e4 | 9857 | int ret; |
d314774c | 9858 | struct net *net = dev_net(dev); |
1da177e4 | 9859 | |
e283de3a FF |
9860 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
9861 | NETDEV_FEATURE_COUNT); | |
1da177e4 LT |
9862 | BUG_ON(dev_boot_phase); |
9863 | ASSERT_RTNL(); | |
9864 | ||
b17a7c17 SH |
9865 | might_sleep(); |
9866 | ||
1da177e4 LT |
9867 | /* When net_device's are persistent, this will be fatal. */ |
9868 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 9869 | BUG_ON(!net); |
1da177e4 | 9870 | |
9000edb7 JK |
9871 | ret = ethtool_check_ops(dev->ethtool_ops); |
9872 | if (ret) | |
9873 | return ret; | |
9874 | ||
f1f28aa3 | 9875 | spin_lock_init(&dev->addr_list_lock); |
845e0ebb | 9876 | netdev_set_addr_lockdep_class(dev); |
1da177e4 | 9877 | |
828de4f6 | 9878 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
9879 | if (ret < 0) |
9880 | goto out; | |
9881 | ||
9077f052 | 9882 | ret = -ENOMEM; |
ff927412 JP |
9883 | dev->name_node = netdev_name_node_head_alloc(dev); |
9884 | if (!dev->name_node) | |
9885 | goto out; | |
9886 | ||
1da177e4 | 9887 | /* Init, if this function is available */ |
d314774c SH |
9888 | if (dev->netdev_ops->ndo_init) { |
9889 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
9890 | if (ret) { |
9891 | if (ret > 0) | |
9892 | ret = -EIO; | |
42c17fa6 | 9893 | goto err_free_name; |
1da177e4 LT |
9894 | } |
9895 | } | |
4ec93edb | 9896 | |
f646968f PM |
9897 | if (((dev->hw_features | dev->features) & |
9898 | NETIF_F_HW_VLAN_CTAG_FILTER) && | |
d2ed273d MM |
9899 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
9900 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { | |
9901 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); | |
9902 | ret = -EINVAL; | |
9903 | goto err_uninit; | |
9904 | } | |
9905 | ||
9c7dafbf PE |
9906 | ret = -EBUSY; |
9907 | if (!dev->ifindex) | |
9908 | dev->ifindex = dev_new_index(net); | |
9909 | else if (__dev_get_by_index(net, dev->ifindex)) | |
9910 | goto err_uninit; | |
9911 | ||
5455c699 MM |
9912 | /* Transfer changeable features to wanted_features and enable |
9913 | * software offloads (GSO and GRO). | |
9914 | */ | |
1a3c998f | 9915 | dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); |
14d1232f | 9916 | dev->features |= NETIF_F_SOFT_FEATURES; |
d764a122 | 9917 | |
876c4384 | 9918 | if (dev->udp_tunnel_nic_info) { |
d764a122 SD |
9919 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; |
9920 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
9921 | } | |
9922 | ||
14d1232f | 9923 | dev->wanted_features = dev->features & dev->hw_features; |
1da177e4 | 9924 | |
cbc53e08 | 9925 | if (!(dev->flags & IFF_LOOPBACK)) |
34324dc2 | 9926 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
cbc53e08 | 9927 | |
7f348a60 AD |
9928 | /* If IPv4 TCP segmentation offload is supported we should also |
9929 | * allow the device to enable segmenting the frame with the option | |
9930 | * of ignoring a static IP ID value. This doesn't enable the | |
9931 | * feature itself but allows the user to enable it later. | |
9932 | */ | |
cbc53e08 AD |
9933 | if (dev->hw_features & NETIF_F_TSO) |
9934 | dev->hw_features |= NETIF_F_TSO_MANGLEID; | |
7f348a60 AD |
9935 | if (dev->vlan_features & NETIF_F_TSO) |
9936 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; | |
9937 | if (dev->mpls_features & NETIF_F_TSO) | |
9938 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; | |
9939 | if (dev->hw_enc_features & NETIF_F_TSO) | |
9940 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; | |
c6e1a0d1 | 9941 | |
1180e7d6 | 9942 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 9943 | */ |
1180e7d6 | 9944 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 9945 | |
ee579677 PS |
9946 | /* Make NETIF_F_SG inheritable to tunnel devices. |
9947 | */ | |
802ab55a | 9948 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
ee579677 | 9949 | |
0d89d203 SH |
9950 | /* Make NETIF_F_SG inheritable to MPLS. |
9951 | */ | |
9952 | dev->mpls_features |= NETIF_F_SG; | |
9953 | ||
7ffbe3fd JB |
9954 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
9955 | ret = notifier_to_errno(ret); | |
9956 | if (ret) | |
9957 | goto err_uninit; | |
9958 | ||
8b41d188 | 9959 | ret = netdev_register_kobject(dev); |
cb626bf5 JH |
9960 | if (ret) { |
9961 | dev->reg_state = NETREG_UNREGISTERED; | |
7ce1b0ed | 9962 | goto err_uninit; |
cb626bf5 | 9963 | } |
b17a7c17 SH |
9964 | dev->reg_state = NETREG_REGISTERED; |
9965 | ||
6cb6a27c | 9966 | __netdev_update_features(dev); |
8e9b59b2 | 9967 | |
1da177e4 LT |
9968 | /* |
9969 | * Default initial state at registry is that the | |
9970 | * device is present. | |
9971 | */ | |
9972 | ||
9973 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
9974 | ||
8f4cccbb BH |
9975 | linkwatch_init_dev(dev); |
9976 | ||
1da177e4 | 9977 | dev_init_scheduler(dev); |
b2309a71 ED |
9978 | |
9979 | dev_hold_track(dev, &dev->dev_registered_tracker, GFP_KERNEL); | |
ce286d32 | 9980 | list_netdevice(dev); |
b2309a71 | 9981 | |
7bf23575 | 9982 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 9983 | |
948b337e JP |
9984 | /* If the device has permanent device address, driver should |
9985 | * set dev_addr and also addr_assign_type should be set to | |
9986 | * NET_ADDR_PERM (default value). | |
9987 | */ | |
9988 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
9989 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
9990 | ||
1da177e4 | 9991 | /* Notify protocols, that a new device appeared. */ |
056925ab | 9992 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 9993 | ret = notifier_to_errno(ret); |
93ee31f1 | 9994 | if (ret) { |
766b0515 JK |
9995 | /* Expect explicit free_netdev() on failure */ |
9996 | dev->needs_free_netdev = false; | |
037e56bd | 9997 | unregister_netdevice_queue(dev, NULL); |
766b0515 | 9998 | goto out; |
93ee31f1 | 9999 | } |
d90a909e EB |
10000 | /* |
10001 | * Prevent userspace races by waiting until the network | |
10002 | * device is fully setup before sending notifications. | |
10003 | */ | |
a2835763 PM |
10004 | if (!dev->rtnl_link_ops || |
10005 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
7f294054 | 10006 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
1da177e4 LT |
10007 | |
10008 | out: | |
10009 | return ret; | |
7ce1b0ed HX |
10010 | |
10011 | err_uninit: | |
d314774c SH |
10012 | if (dev->netdev_ops->ndo_uninit) |
10013 | dev->netdev_ops->ndo_uninit(dev); | |
cf124db5 DM |
10014 | if (dev->priv_destructor) |
10015 | dev->priv_destructor(dev); | |
42c17fa6 DC |
10016 | err_free_name: |
10017 | netdev_name_node_free(dev->name_node); | |
7ce1b0ed | 10018 | goto out; |
1da177e4 | 10019 | } |
d1b19dff | 10020 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 10021 | |
937f1ba5 BH |
10022 | /** |
10023 | * init_dummy_netdev - init a dummy network device for NAPI | |
10024 | * @dev: device to init | |
10025 | * | |
10026 | * This takes a network device structure and initialize the minimum | |
10027 | * amount of fields so it can be used to schedule NAPI polls without | |
10028 | * registering a full blown interface. This is to be used by drivers | |
10029 | * that need to tie several hardware interfaces to a single NAPI | |
10030 | * poll scheduler due to HW limitations. | |
10031 | */ | |
10032 | int init_dummy_netdev(struct net_device *dev) | |
10033 | { | |
10034 | /* Clear everything. Note we don't initialize spinlocks | |
10035 | * are they aren't supposed to be taken by any of the | |
10036 | * NAPI code and this dummy netdev is supposed to be | |
10037 | * only ever used for NAPI polls | |
10038 | */ | |
10039 | memset(dev, 0, sizeof(struct net_device)); | |
10040 | ||
10041 | /* make sure we BUG if trying to hit standard | |
10042 | * register/unregister code path | |
10043 | */ | |
10044 | dev->reg_state = NETREG_DUMMY; | |
10045 | ||
937f1ba5 BH |
10046 | /* NAPI wants this */ |
10047 | INIT_LIST_HEAD(&dev->napi_list); | |
10048 | ||
10049 | /* a dummy interface is started by default */ | |
10050 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
10051 | set_bit(__LINK_STATE_START, &dev->state); | |
10052 | ||
35edfdc7 JE |
10053 | /* napi_busy_loop stats accounting wants this */ |
10054 | dev_net_set(dev, &init_net); | |
10055 | ||
29b4433d ED |
10056 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
10057 | * because users of this 'device' dont need to change | |
10058 | * its refcount. | |
10059 | */ | |
10060 | ||
937f1ba5 BH |
10061 | return 0; |
10062 | } | |
10063 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
10064 | ||
10065 | ||
1da177e4 LT |
10066 | /** |
10067 | * register_netdev - register a network device | |
10068 | * @dev: device to register | |
10069 | * | |
10070 | * Take a completed network device structure and add it to the kernel | |
10071 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
10072 | * chain. 0 is returned on success. A negative errno code is returned | |
10073 | * on a failure to set up the device, or if the name is a duplicate. | |
10074 | * | |
38b4da38 | 10075 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
10076 | * and expands the device name if you passed a format string to |
10077 | * alloc_netdev. | |
10078 | */ | |
10079 | int register_netdev(struct net_device *dev) | |
10080 | { | |
10081 | int err; | |
10082 | ||
b0f3debc KT |
10083 | if (rtnl_lock_killable()) |
10084 | return -EINTR; | |
1da177e4 | 10085 | err = register_netdevice(dev); |
1da177e4 LT |
10086 | rtnl_unlock(); |
10087 | return err; | |
10088 | } | |
10089 | EXPORT_SYMBOL(register_netdev); | |
10090 | ||
29b4433d ED |
10091 | int netdev_refcnt_read(const struct net_device *dev) |
10092 | { | |
919067cc | 10093 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d ED |
10094 | int i, refcnt = 0; |
10095 | ||
10096 | for_each_possible_cpu(i) | |
10097 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
10098 | return refcnt; | |
919067cc ED |
10099 | #else |
10100 | return refcount_read(&dev->dev_refcnt); | |
10101 | #endif | |
29b4433d ED |
10102 | } |
10103 | EXPORT_SYMBOL(netdev_refcnt_read); | |
10104 | ||
5aa3afe1 DV |
10105 | int netdev_unregister_timeout_secs __read_mostly = 10; |
10106 | ||
de2b541b MCC |
10107 | #define WAIT_REFS_MIN_MSECS 1 |
10108 | #define WAIT_REFS_MAX_MSECS 250 | |
2c53040f | 10109 | /** |
faab39f6 JK |
10110 | * netdev_wait_allrefs_any - wait until all references are gone. |
10111 | * @list: list of net_devices to wait on | |
1da177e4 LT |
10112 | * |
10113 | * This is called when unregistering network devices. | |
10114 | * | |
10115 | * Any protocol or device that holds a reference should register | |
10116 | * for netdevice notification, and cleanup and put back the | |
10117 | * reference if they receive an UNREGISTER event. | |
10118 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 10119 | * call dev_put. |
1da177e4 | 10120 | */ |
faab39f6 | 10121 | static struct net_device *netdev_wait_allrefs_any(struct list_head *list) |
1da177e4 LT |
10122 | { |
10123 | unsigned long rebroadcast_time, warning_time; | |
faab39f6 JK |
10124 | struct net_device *dev; |
10125 | int wait = 0; | |
1da177e4 LT |
10126 | |
10127 | rebroadcast_time = warning_time = jiffies; | |
29b4433d | 10128 | |
faab39f6 JK |
10129 | list_for_each_entry(dev, list, todo_list) |
10130 | if (netdev_refcnt_read(dev) == 1) | |
10131 | return dev; | |
10132 | ||
10133 | while (true) { | |
1da177e4 | 10134 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 10135 | rtnl_lock(); |
1da177e4 LT |
10136 | |
10137 | /* Rebroadcast unregister notification */ | |
faab39f6 JK |
10138 | list_for_each_entry(dev, list, todo_list) |
10139 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
1da177e4 | 10140 | |
748e2d93 | 10141 | __rtnl_unlock(); |
0115e8e3 | 10142 | rcu_barrier(); |
748e2d93 ED |
10143 | rtnl_lock(); |
10144 | ||
faab39f6 JK |
10145 | list_for_each_entry(dev, list, todo_list) |
10146 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, | |
10147 | &dev->state)) { | |
10148 | /* We must not have linkwatch events | |
10149 | * pending on unregister. If this | |
10150 | * happens, we simply run the queue | |
10151 | * unscheduled, resulting in a noop | |
10152 | * for this device. | |
10153 | */ | |
10154 | linkwatch_run_queue(); | |
10155 | break; | |
10156 | } | |
1da177e4 | 10157 | |
6756ae4b | 10158 | __rtnl_unlock(); |
1da177e4 LT |
10159 | |
10160 | rebroadcast_time = jiffies; | |
10161 | } | |
10162 | ||
0e4be9e5 FR |
10163 | if (!wait) { |
10164 | rcu_barrier(); | |
10165 | wait = WAIT_REFS_MIN_MSECS; | |
10166 | } else { | |
10167 | msleep(wait); | |
10168 | wait = min(wait << 1, WAIT_REFS_MAX_MSECS); | |
10169 | } | |
1da177e4 | 10170 | |
faab39f6 JK |
10171 | list_for_each_entry(dev, list, todo_list) |
10172 | if (netdev_refcnt_read(dev) == 1) | |
10173 | return dev; | |
29b4433d | 10174 | |
faab39f6 | 10175 | if (time_after(jiffies, warning_time + |
5aa3afe1 | 10176 | netdev_unregister_timeout_secs * HZ)) { |
faab39f6 JK |
10177 | list_for_each_entry(dev, list, todo_list) { |
10178 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", | |
10179 | dev->name, netdev_refcnt_read(dev)); | |
10180 | ref_tracker_dir_print(&dev->refcnt_tracker, 10); | |
10181 | } | |
10182 | ||
1da177e4 LT |
10183 | warning_time = jiffies; |
10184 | } | |
10185 | } | |
10186 | } | |
10187 | ||
10188 | /* The sequence is: | |
10189 | * | |
10190 | * rtnl_lock(); | |
10191 | * ... | |
10192 | * register_netdevice(x1); | |
10193 | * register_netdevice(x2); | |
10194 | * ... | |
10195 | * unregister_netdevice(y1); | |
10196 | * unregister_netdevice(y2); | |
10197 | * ... | |
10198 | * rtnl_unlock(); | |
10199 | * free_netdev(y1); | |
10200 | * free_netdev(y2); | |
10201 | * | |
58ec3b4d | 10202 | * We are invoked by rtnl_unlock(). |
1da177e4 | 10203 | * This allows us to deal with problems: |
b17a7c17 | 10204 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
10205 | * without deadlocking with linkwatch via keventd. |
10206 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
10207 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
10208 | * |
10209 | * We must not return until all unregister events added during | |
10210 | * the interval the lock was held have been completed. | |
1da177e4 | 10211 | */ |
1da177e4 LT |
10212 | void netdev_run_todo(void) |
10213 | { | |
ae68db14 | 10214 | struct net_device *dev, *tmp; |
626ab0e6 | 10215 | struct list_head list; |
1fc70edb TY |
10216 | #ifdef CONFIG_LOCKDEP |
10217 | struct list_head unlink_list; | |
10218 | ||
10219 | list_replace_init(&net_unlink_list, &unlink_list); | |
10220 | ||
10221 | while (!list_empty(&unlink_list)) { | |
10222 | struct net_device *dev = list_first_entry(&unlink_list, | |
10223 | struct net_device, | |
10224 | unlink_list); | |
0e8b8d6a | 10225 | list_del_init(&dev->unlink_list); |
1fc70edb TY |
10226 | dev->nested_level = dev->lower_level - 1; |
10227 | } | |
10228 | #endif | |
1da177e4 | 10229 | |
1da177e4 | 10230 | /* Snapshot list, allow later requests */ |
626ab0e6 | 10231 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
10232 | |
10233 | __rtnl_unlock(); | |
626ab0e6 | 10234 | |
0115e8e3 | 10235 | /* Wait for rcu callbacks to finish before next phase */ |
850a545b EB |
10236 | if (!list_empty(&list)) |
10237 | rcu_barrier(); | |
10238 | ||
ae68db14 | 10239 | list_for_each_entry_safe(dev, tmp, &list, todo_list) { |
b17a7c17 | 10240 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
ae68db14 JK |
10241 | netdev_WARN(dev, "run_todo but not unregistering\n"); |
10242 | list_del(&dev->todo_list); | |
b17a7c17 SH |
10243 | continue; |
10244 | } | |
1da177e4 | 10245 | |
b17a7c17 | 10246 | dev->reg_state = NETREG_UNREGISTERED; |
86213f80 | 10247 | linkwatch_forget_dev(dev); |
ae68db14 JK |
10248 | } |
10249 | ||
10250 | while (!list_empty(&list)) { | |
faab39f6 | 10251 | dev = netdev_wait_allrefs_any(&list); |
ae68db14 | 10252 | list_del(&dev->todo_list); |
1da177e4 | 10253 | |
b17a7c17 | 10254 | /* paranoia */ |
add2d736 | 10255 | BUG_ON(netdev_refcnt_read(dev) != 1); |
7866a621 SN |
10256 | BUG_ON(!list_empty(&dev->ptype_all)); |
10257 | BUG_ON(!list_empty(&dev->ptype_specific)); | |
33d480ce ED |
10258 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
10259 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
330c7272 | 10260 | #if IS_ENABLED(CONFIG_DECNET) |
547b792c | 10261 | WARN_ON(dev->dn_ptr); |
330c7272 | 10262 | #endif |
cf124db5 DM |
10263 | if (dev->priv_destructor) |
10264 | dev->priv_destructor(dev); | |
10265 | if (dev->needs_free_netdev) | |
10266 | free_netdev(dev); | |
9093bbb2 | 10267 | |
ede6c39c ED |
10268 | if (atomic_dec_and_test(&dev_net(dev)->dev_unreg_count)) |
10269 | wake_up(&netdev_unregistering_wq); | |
50624c93 | 10270 | |
9093bbb2 SH |
10271 | /* Free network device */ |
10272 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 10273 | } |
1da177e4 LT |
10274 | } |
10275 | ||
9256645a JW |
10276 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
10277 | * all the same fields in the same order as net_device_stats, with only | |
10278 | * the type differing, but rtnl_link_stats64 may have additional fields | |
10279 | * at the end for newer counters. | |
3cfde79c | 10280 | */ |
77a1abf5 ED |
10281 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
10282 | const struct net_device_stats *netdev_stats) | |
3cfde79c BH |
10283 | { |
10284 | #if BITS_PER_LONG == 64 | |
9256645a | 10285 | BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats)); |
9af9959e | 10286 | memcpy(stats64, netdev_stats, sizeof(*netdev_stats)); |
9256645a JW |
10287 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10288 | memset((char *)stats64 + sizeof(*netdev_stats), 0, | |
10289 | sizeof(*stats64) - sizeof(*netdev_stats)); | |
3cfde79c | 10290 | #else |
9256645a | 10291 | size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long); |
3cfde79c BH |
10292 | const unsigned long *src = (const unsigned long *)netdev_stats; |
10293 | u64 *dst = (u64 *)stats64; | |
10294 | ||
9256645a | 10295 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
3cfde79c BH |
10296 | for (i = 0; i < n; i++) |
10297 | dst[i] = src[i]; | |
9256645a JW |
10298 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10299 | memset((char *)stats64 + n * sizeof(u64), 0, | |
10300 | sizeof(*stats64) - n * sizeof(u64)); | |
3cfde79c BH |
10301 | #endif |
10302 | } | |
77a1abf5 | 10303 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 10304 | |
625788b5 ED |
10305 | struct net_device_core_stats *netdev_core_stats_alloc(struct net_device *dev) |
10306 | { | |
10307 | struct net_device_core_stats __percpu *p; | |
10308 | ||
10309 | p = alloc_percpu_gfp(struct net_device_core_stats, | |
10310 | GFP_ATOMIC | __GFP_NOWARN); | |
10311 | ||
10312 | if (p && cmpxchg(&dev->core_stats, NULL, p)) | |
10313 | free_percpu(p); | |
10314 | ||
10315 | /* This READ_ONCE() pairs with the cmpxchg() above */ | |
10316 | p = READ_ONCE(dev->core_stats); | |
10317 | if (!p) | |
10318 | return NULL; | |
10319 | ||
10320 | return this_cpu_ptr(p); | |
10321 | } | |
10322 | EXPORT_SYMBOL(netdev_core_stats_alloc); | |
10323 | ||
eeda3fd6 SH |
10324 | /** |
10325 | * dev_get_stats - get network device statistics | |
10326 | * @dev: device to get statistics from | |
28172739 | 10327 | * @storage: place to store stats |
eeda3fd6 | 10328 | * |
d7753516 BH |
10329 | * Get network statistics from device. Return @storage. |
10330 | * The device driver may provide its own method by setting | |
10331 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
10332 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 10333 | */ |
d7753516 BH |
10334 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
10335 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 10336 | { |
eeda3fd6 | 10337 | const struct net_device_ops *ops = dev->netdev_ops; |
625788b5 | 10338 | const struct net_device_core_stats __percpu *p; |
eeda3fd6 | 10339 | |
28172739 ED |
10340 | if (ops->ndo_get_stats64) { |
10341 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
10342 | ops->ndo_get_stats64(dev, storage); |
10343 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 10344 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
caf586e5 ED |
10345 | } else { |
10346 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 10347 | } |
625788b5 ED |
10348 | |
10349 | /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */ | |
10350 | p = READ_ONCE(dev->core_stats); | |
10351 | if (p) { | |
10352 | const struct net_device_core_stats *core_stats; | |
10353 | int i; | |
10354 | ||
10355 | for_each_possible_cpu(i) { | |
10356 | core_stats = per_cpu_ptr(p, i); | |
10357 | storage->rx_dropped += local_read(&core_stats->rx_dropped); | |
10358 | storage->tx_dropped += local_read(&core_stats->tx_dropped); | |
10359 | storage->rx_nohandler += local_read(&core_stats->rx_nohandler); | |
10360 | } | |
10361 | } | |
28172739 | 10362 | return storage; |
c45d286e | 10363 | } |
eeda3fd6 | 10364 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 10365 | |
44fa32f0 HK |
10366 | /** |
10367 | * dev_fetch_sw_netstats - get per-cpu network device statistics | |
10368 | * @s: place to store stats | |
10369 | * @netstats: per-cpu network stats to read from | |
10370 | * | |
10371 | * Read per-cpu network statistics and populate the related fields in @s. | |
10372 | */ | |
10373 | void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s, | |
10374 | const struct pcpu_sw_netstats __percpu *netstats) | |
10375 | { | |
10376 | int cpu; | |
10377 | ||
10378 | for_each_possible_cpu(cpu) { | |
10379 | const struct pcpu_sw_netstats *stats; | |
10380 | struct pcpu_sw_netstats tmp; | |
10381 | unsigned int start; | |
10382 | ||
10383 | stats = per_cpu_ptr(netstats, cpu); | |
10384 | do { | |
10385 | start = u64_stats_fetch_begin_irq(&stats->syncp); | |
10386 | tmp.rx_packets = stats->rx_packets; | |
10387 | tmp.rx_bytes = stats->rx_bytes; | |
10388 | tmp.tx_packets = stats->tx_packets; | |
10389 | tmp.tx_bytes = stats->tx_bytes; | |
10390 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
10391 | ||
10392 | s->rx_packets += tmp.rx_packets; | |
10393 | s->rx_bytes += tmp.rx_bytes; | |
10394 | s->tx_packets += tmp.tx_packets; | |
10395 | s->tx_bytes += tmp.tx_bytes; | |
10396 | } | |
10397 | } | |
10398 | EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats); | |
10399 | ||
a1839426 HK |
10400 | /** |
10401 | * dev_get_tstats64 - ndo_get_stats64 implementation | |
10402 | * @dev: device to get statistics from | |
10403 | * @s: place to store stats | |
10404 | * | |
10405 | * Populate @s from dev->stats and dev->tstats. Can be used as | |
10406 | * ndo_get_stats64() callback. | |
10407 | */ | |
10408 | void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s) | |
10409 | { | |
10410 | netdev_stats_to_stats64(s, &dev->stats); | |
10411 | dev_fetch_sw_netstats(s, dev->tstats); | |
10412 | } | |
10413 | EXPORT_SYMBOL_GPL(dev_get_tstats64); | |
10414 | ||
24824a09 | 10415 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 10416 | { |
24824a09 | 10417 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 10418 | |
24824a09 ED |
10419 | #ifdef CONFIG_NET_CLS_ACT |
10420 | if (queue) | |
10421 | return queue; | |
10422 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
10423 | if (!queue) | |
10424 | return NULL; | |
10425 | netdev_init_one_queue(dev, queue, NULL); | |
2ce1ee17 | 10426 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
24824a09 ED |
10427 | queue->qdisc_sleeping = &noop_qdisc; |
10428 | rcu_assign_pointer(dev->ingress_queue, queue); | |
10429 | #endif | |
10430 | return queue; | |
bb949fbd DM |
10431 | } |
10432 | ||
2c60db03 ED |
10433 | static const struct ethtool_ops default_ethtool_ops; |
10434 | ||
d07d7507 SG |
10435 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
10436 | const struct ethtool_ops *ops) | |
10437 | { | |
10438 | if (dev->ethtool_ops == &default_ethtool_ops) | |
10439 | dev->ethtool_ops = ops; | |
10440 | } | |
10441 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
10442 | ||
74d332c1 ED |
10443 | void netdev_freemem(struct net_device *dev) |
10444 | { | |
10445 | char *addr = (char *)dev - dev->padded; | |
10446 | ||
4cb28970 | 10447 | kvfree(addr); |
74d332c1 ED |
10448 | } |
10449 | ||
1da177e4 | 10450 | /** |
722c9a0c | 10451 | * alloc_netdev_mqs - allocate network device |
10452 | * @sizeof_priv: size of private data to allocate space for | |
10453 | * @name: device name format string | |
10454 | * @name_assign_type: origin of device name | |
10455 | * @setup: callback to initialize device | |
10456 | * @txqs: the number of TX subqueues to allocate | |
10457 | * @rxqs: the number of RX subqueues to allocate | |
10458 | * | |
10459 | * Allocates a struct net_device with private data area for driver use | |
10460 | * and performs basic initialization. Also allocates subqueue structs | |
10461 | * for each queue on the device. | |
1da177e4 | 10462 | */ |
36909ea4 | 10463 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
c835a677 | 10464 | unsigned char name_assign_type, |
36909ea4 TH |
10465 | void (*setup)(struct net_device *), |
10466 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 10467 | { |
1da177e4 | 10468 | struct net_device *dev; |
52a59bd5 | 10469 | unsigned int alloc_size; |
1ce8e7b5 | 10470 | struct net_device *p; |
1da177e4 | 10471 | |
b6fe17d6 SH |
10472 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
10473 | ||
36909ea4 | 10474 | if (txqs < 1) { |
7b6cd1ce | 10475 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
10476 | return NULL; |
10477 | } | |
10478 | ||
36909ea4 | 10479 | if (rxqs < 1) { |
7b6cd1ce | 10480 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
10481 | return NULL; |
10482 | } | |
36909ea4 | 10483 | |
fd2ea0a7 | 10484 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
10485 | if (sizeof_priv) { |
10486 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 10487 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
10488 | alloc_size += sizeof_priv; |
10489 | } | |
10490 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 10491 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 10492 | |
c948f51c | 10493 | p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); |
62b5942a | 10494 | if (!p) |
1da177e4 | 10495 | return NULL; |
1da177e4 | 10496 | |
1ce8e7b5 | 10497 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 10498 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 10499 | |
4d92b95f | 10500 | ref_tracker_dir_init(&dev->refcnt_tracker, 128); |
919067cc | 10501 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d ED |
10502 | dev->pcpu_refcnt = alloc_percpu(int); |
10503 | if (!dev->pcpu_refcnt) | |
74d332c1 | 10504 | goto free_dev; |
4c6c11ea | 10505 | __dev_hold(dev); |
add2d736 ED |
10506 | #else |
10507 | refcount_set(&dev->dev_refcnt, 1); | |
919067cc | 10508 | #endif |
ab9c73cc | 10509 | |
ab9c73cc | 10510 | if (dev_addr_init(dev)) |
29b4433d | 10511 | goto free_pcpu; |
ab9c73cc | 10512 | |
22bedad3 | 10513 | dev_mc_init(dev); |
a748ee24 | 10514 | dev_uc_init(dev); |
ccffad25 | 10515 | |
c346dca1 | 10516 | dev_net_set(dev, &init_net); |
1da177e4 | 10517 | |
8d3bdbd5 | 10518 | dev->gso_max_size = GSO_MAX_SIZE; |
30b678d8 | 10519 | dev->gso_max_segs = GSO_MAX_SEGS; |
eac1b93c | 10520 | dev->gro_max_size = GRO_MAX_SIZE; |
5343da4c TY |
10521 | dev->upper_level = 1; |
10522 | dev->lower_level = 1; | |
1fc70edb TY |
10523 | #ifdef CONFIG_LOCKDEP |
10524 | dev->nested_level = 0; | |
10525 | INIT_LIST_HEAD(&dev->unlink_list); | |
10526 | #endif | |
8d3bdbd5 | 10527 | |
8d3bdbd5 DM |
10528 | INIT_LIST_HEAD(&dev->napi_list); |
10529 | INIT_LIST_HEAD(&dev->unreg_list); | |
5cde2829 | 10530 | INIT_LIST_HEAD(&dev->close_list); |
8d3bdbd5 | 10531 | INIT_LIST_HEAD(&dev->link_watch_list); |
2f268f12 VF |
10532 | INIT_LIST_HEAD(&dev->adj_list.upper); |
10533 | INIT_LIST_HEAD(&dev->adj_list.lower); | |
7866a621 SN |
10534 | INIT_LIST_HEAD(&dev->ptype_all); |
10535 | INIT_LIST_HEAD(&dev->ptype_specific); | |
93642e14 | 10536 | INIT_LIST_HEAD(&dev->net_notifier_list); |
59cc1f61 JK |
10537 | #ifdef CONFIG_NET_SCHED |
10538 | hash_init(dev->qdisc_hash); | |
10539 | #endif | |
02875878 | 10540 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
8d3bdbd5 DM |
10541 | setup(dev); |
10542 | ||
a813104d | 10543 | if (!dev->tx_queue_len) { |
f84bb1ea | 10544 | dev->priv_flags |= IFF_NO_QUEUE; |
11597084 | 10545 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
a813104d | 10546 | } |
906470c1 | 10547 | |
36909ea4 TH |
10548 | dev->num_tx_queues = txqs; |
10549 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 10550 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 10551 | goto free_all; |
e8a0464c | 10552 | |
36909ea4 TH |
10553 | dev->num_rx_queues = rxqs; |
10554 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 10555 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 10556 | goto free_all; |
0a9627f2 | 10557 | |
1da177e4 | 10558 | strcpy(dev->name, name); |
c835a677 | 10559 | dev->name_assign_type = name_assign_type; |
cbda10fa | 10560 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
10561 | if (!dev->ethtool_ops) |
10562 | dev->ethtool_ops = &default_ethtool_ops; | |
e687ad60 | 10563 | |
17d20784 | 10564 | nf_hook_netdev_init(dev); |
e687ad60 | 10565 | |
1da177e4 | 10566 | return dev; |
ab9c73cc | 10567 | |
8d3bdbd5 DM |
10568 | free_all: |
10569 | free_netdev(dev); | |
10570 | return NULL; | |
10571 | ||
29b4433d | 10572 | free_pcpu: |
919067cc | 10573 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d | 10574 | free_percpu(dev->pcpu_refcnt); |
74d332c1 | 10575 | free_dev: |
919067cc | 10576 | #endif |
74d332c1 | 10577 | netdev_freemem(dev); |
ab9c73cc | 10578 | return NULL; |
1da177e4 | 10579 | } |
36909ea4 | 10580 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
10581 | |
10582 | /** | |
722c9a0c | 10583 | * free_netdev - free network device |
10584 | * @dev: device | |
1da177e4 | 10585 | * |
722c9a0c | 10586 | * This function does the last stage of destroying an allocated device |
10587 | * interface. The reference to the device object is released. If this | |
10588 | * is the last reference then it will be freed.Must be called in process | |
10589 | * context. | |
1da177e4 LT |
10590 | */ |
10591 | void free_netdev(struct net_device *dev) | |
10592 | { | |
d565b0a1 HX |
10593 | struct napi_struct *p, *n; |
10594 | ||
93d05d4a | 10595 | might_sleep(); |
c269a24c JK |
10596 | |
10597 | /* When called immediately after register_netdevice() failed the unwind | |
10598 | * handling may still be dismantling the device. Handle that case by | |
10599 | * deferring the free. | |
10600 | */ | |
10601 | if (dev->reg_state == NETREG_UNREGISTERING) { | |
10602 | ASSERT_RTNL(); | |
10603 | dev->needs_free_netdev = true; | |
10604 | return; | |
10605 | } | |
10606 | ||
60877a32 | 10607 | netif_free_tx_queues(dev); |
e817f856 | 10608 | netif_free_rx_queues(dev); |
e8a0464c | 10609 | |
33d480ce | 10610 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 10611 | |
f001fde5 JP |
10612 | /* Flush device addresses */ |
10613 | dev_addr_flush(dev); | |
10614 | ||
d565b0a1 HX |
10615 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
10616 | netif_napi_del(p); | |
10617 | ||
4d92b95f | 10618 | ref_tracker_dir_exit(&dev->refcnt_tracker); |
919067cc | 10619 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d ED |
10620 | free_percpu(dev->pcpu_refcnt); |
10621 | dev->pcpu_refcnt = NULL; | |
919067cc | 10622 | #endif |
625788b5 ED |
10623 | free_percpu(dev->core_stats); |
10624 | dev->core_stats = NULL; | |
75ccae62 THJ |
10625 | free_percpu(dev->xdp_bulkq); |
10626 | dev->xdp_bulkq = NULL; | |
29b4433d | 10627 | |
3041a069 | 10628 | /* Compatibility with error handling in drivers */ |
1da177e4 | 10629 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
74d332c1 | 10630 | netdev_freemem(dev); |
1da177e4 LT |
10631 | return; |
10632 | } | |
10633 | ||
10634 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
10635 | dev->reg_state = NETREG_RELEASED; | |
10636 | ||
43cb76d9 GKH |
10637 | /* will free via device release */ |
10638 | put_device(&dev->dev); | |
1da177e4 | 10639 | } |
d1b19dff | 10640 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 10641 | |
f0db275a SH |
10642 | /** |
10643 | * synchronize_net - Synchronize with packet receive processing | |
10644 | * | |
10645 | * Wait for packets currently being received to be done. | |
10646 | * Does not block later packets from starting. | |
10647 | */ | |
4ec93edb | 10648 | void synchronize_net(void) |
1da177e4 LT |
10649 | { |
10650 | might_sleep(); | |
be3fc413 ED |
10651 | if (rtnl_is_locked()) |
10652 | synchronize_rcu_expedited(); | |
10653 | else | |
10654 | synchronize_rcu(); | |
1da177e4 | 10655 | } |
d1b19dff | 10656 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
10657 | |
10658 | /** | |
44a0873d | 10659 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 10660 | * @dev: device |
44a0873d | 10661 | * @head: list |
6ebfbc06 | 10662 | * |
1da177e4 | 10663 | * This function shuts down a device interface and removes it |
d59b54b1 | 10664 | * from the kernel tables. |
44a0873d | 10665 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
10666 | * |
10667 | * Callers must hold the rtnl semaphore. You may want | |
10668 | * unregister_netdev() instead of this. | |
10669 | */ | |
10670 | ||
44a0873d | 10671 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 10672 | { |
a6620712 HX |
10673 | ASSERT_RTNL(); |
10674 | ||
44a0873d | 10675 | if (head) { |
9fdce099 | 10676 | list_move_tail(&dev->unreg_list, head); |
44a0873d | 10677 | } else { |
037e56bd JK |
10678 | LIST_HEAD(single); |
10679 | ||
10680 | list_add(&dev->unreg_list, &single); | |
0cbe1e57 | 10681 | unregister_netdevice_many(&single); |
44a0873d | 10682 | } |
1da177e4 | 10683 | } |
44a0873d | 10684 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 10685 | |
9b5e383c ED |
10686 | /** |
10687 | * unregister_netdevice_many - unregister many devices | |
10688 | * @head: list of devices | |
87757a91 ED |
10689 | * |
10690 | * Note: As most callers use a stack allocated list_head, | |
10691 | * we force a list_del() to make sure stack wont be corrupted later. | |
9b5e383c ED |
10692 | */ |
10693 | void unregister_netdevice_many(struct list_head *head) | |
bcfe2f1a JK |
10694 | { |
10695 | struct net_device *dev, *tmp; | |
10696 | LIST_HEAD(close_head); | |
10697 | ||
10698 | BUG_ON(dev_boot_phase); | |
10699 | ASSERT_RTNL(); | |
10700 | ||
0cbe1e57 JK |
10701 | if (list_empty(head)) |
10702 | return; | |
10703 | ||
bcfe2f1a JK |
10704 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
10705 | /* Some devices call without registering | |
10706 | * for initialization unwind. Remove those | |
10707 | * devices and proceed with the remaining. | |
10708 | */ | |
10709 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
10710 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", | |
10711 | dev->name, dev); | |
10712 | ||
10713 | WARN_ON(1); | |
10714 | list_del(&dev->unreg_list); | |
10715 | continue; | |
10716 | } | |
10717 | dev->dismantle = true; | |
10718 | BUG_ON(dev->reg_state != NETREG_REGISTERED); | |
10719 | } | |
10720 | ||
10721 | /* If device is running, close it first. */ | |
10722 | list_for_each_entry(dev, head, unreg_list) | |
10723 | list_add_tail(&dev->close_list, &close_head); | |
10724 | dev_close_many(&close_head, true); | |
10725 | ||
10726 | list_for_each_entry(dev, head, unreg_list) { | |
10727 | /* And unlink it from device chain. */ | |
10728 | unlist_netdevice(dev); | |
10729 | ||
10730 | dev->reg_state = NETREG_UNREGISTERING; | |
10731 | } | |
10732 | flush_all_backlogs(); | |
10733 | ||
10734 | synchronize_net(); | |
10735 | ||
10736 | list_for_each_entry(dev, head, unreg_list) { | |
10737 | struct sk_buff *skb = NULL; | |
10738 | ||
10739 | /* Shutdown queueing discipline. */ | |
10740 | dev_shutdown(dev); | |
10741 | ||
10742 | dev_xdp_uninstall(dev); | |
10743 | ||
9309f97a PM |
10744 | netdev_offload_xstats_disable_all(dev); |
10745 | ||
bcfe2f1a JK |
10746 | /* Notify protocols, that we are about to destroy |
10747 | * this device. They should clean all the things. | |
10748 | */ | |
10749 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
10750 | ||
10751 | if (!dev->rtnl_link_ops || | |
10752 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
10753 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, | |
10754 | GFP_KERNEL, NULL, 0); | |
10755 | ||
10756 | /* | |
10757 | * Flush the unicast and multicast chains | |
10758 | */ | |
10759 | dev_uc_flush(dev); | |
10760 | dev_mc_flush(dev); | |
10761 | ||
10762 | netdev_name_node_alt_flush(dev); | |
10763 | netdev_name_node_free(dev->name_node); | |
10764 | ||
10765 | if (dev->netdev_ops->ndo_uninit) | |
10766 | dev->netdev_ops->ndo_uninit(dev); | |
10767 | ||
10768 | if (skb) | |
10769 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL); | |
10770 | ||
10771 | /* Notifier chain MUST detach us all upper devices. */ | |
10772 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
10773 | WARN_ON(netdev_has_any_lower_dev(dev)); | |
10774 | ||
10775 | /* Remove entries from kobject tree */ | |
10776 | netdev_unregister_kobject(dev); | |
10777 | #ifdef CONFIG_XPS | |
10778 | /* Remove XPS queueing entries */ | |
10779 | netif_reset_xps_queues_gt(dev, 0); | |
10780 | #endif | |
10781 | } | |
10782 | ||
10783 | synchronize_net(); | |
10784 | ||
10785 | list_for_each_entry(dev, head, unreg_list) { | |
b2309a71 | 10786 | dev_put_track(dev, &dev->dev_registered_tracker); |
bcfe2f1a JK |
10787 | net_set_todo(dev); |
10788 | } | |
0cbe1e57 JK |
10789 | |
10790 | list_del(head); | |
bcfe2f1a | 10791 | } |
0cbe1e57 | 10792 | EXPORT_SYMBOL(unregister_netdevice_many); |
bcfe2f1a | 10793 | |
1da177e4 LT |
10794 | /** |
10795 | * unregister_netdev - remove device from the kernel | |
10796 | * @dev: device | |
10797 | * | |
10798 | * This function shuts down a device interface and removes it | |
d59b54b1 | 10799 | * from the kernel tables. |
1da177e4 LT |
10800 | * |
10801 | * This is just a wrapper for unregister_netdevice that takes | |
10802 | * the rtnl semaphore. In general you want to use this and not | |
10803 | * unregister_netdevice. | |
10804 | */ | |
10805 | void unregister_netdev(struct net_device *dev) | |
10806 | { | |
10807 | rtnl_lock(); | |
10808 | unregister_netdevice(dev); | |
10809 | rtnl_unlock(); | |
10810 | } | |
1da177e4 LT |
10811 | EXPORT_SYMBOL(unregister_netdev); |
10812 | ||
ce286d32 | 10813 | /** |
0854fa82 | 10814 | * __dev_change_net_namespace - move device to different nethost namespace |
ce286d32 EB |
10815 | * @dev: device |
10816 | * @net: network namespace | |
10817 | * @pat: If not NULL name pattern to try if the current device name | |
10818 | * is already taken in the destination network namespace. | |
eeb85a14 AV |
10819 | * @new_ifindex: If not zero, specifies device index in the target |
10820 | * namespace. | |
ce286d32 EB |
10821 | * |
10822 | * This function shuts down a device interface and moves it | |
10823 | * to a new network namespace. On success 0 is returned, on | |
10824 | * a failure a netagive errno code is returned. | |
10825 | * | |
10826 | * Callers must hold the rtnl semaphore. | |
10827 | */ | |
10828 | ||
0854fa82 AV |
10829 | int __dev_change_net_namespace(struct net_device *dev, struct net *net, |
10830 | const char *pat, int new_ifindex) | |
ce286d32 | 10831 | { |
ef6a4c88 | 10832 | struct net *net_old = dev_net(dev); |
eeb85a14 | 10833 | int err, new_nsid; |
ce286d32 EB |
10834 | |
10835 | ASSERT_RTNL(); | |
10836 | ||
10837 | /* Don't allow namespace local devices to be moved. */ | |
10838 | err = -EINVAL; | |
10839 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
10840 | goto out; | |
10841 | ||
10842 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
10843 | if (dev->reg_state != NETREG_REGISTERED) |
10844 | goto out; | |
10845 | ||
10846 | /* Get out if there is nothing todo */ | |
10847 | err = 0; | |
ef6a4c88 | 10848 | if (net_eq(net_old, net)) |
ce286d32 EB |
10849 | goto out; |
10850 | ||
10851 | /* Pick the destination device name, and ensure | |
10852 | * we can use it in the destination network namespace. | |
10853 | */ | |
10854 | err = -EEXIST; | |
75ea27d0 | 10855 | if (netdev_name_in_use(net, dev->name)) { |
ce286d32 EB |
10856 | /* We get here if we can't use the current device name */ |
10857 | if (!pat) | |
10858 | goto out; | |
7892bd08 LR |
10859 | err = dev_get_valid_name(net, dev, pat); |
10860 | if (err < 0) | |
ce286d32 EB |
10861 | goto out; |
10862 | } | |
10863 | ||
eeb85a14 AV |
10864 | /* Check that new_ifindex isn't used yet. */ |
10865 | err = -EBUSY; | |
10866 | if (new_ifindex && __dev_get_by_index(net, new_ifindex)) | |
10867 | goto out; | |
10868 | ||
ce286d32 EB |
10869 | /* |
10870 | * And now a mini version of register_netdevice unregister_netdevice. | |
10871 | */ | |
10872 | ||
10873 | /* If device is running close it first. */ | |
9b772652 | 10874 | dev_close(dev); |
ce286d32 EB |
10875 | |
10876 | /* And unlink it from device chain */ | |
ce286d32 EB |
10877 | unlist_netdevice(dev); |
10878 | ||
10879 | synchronize_net(); | |
10880 | ||
10881 | /* Shutdown queueing discipline. */ | |
10882 | dev_shutdown(dev); | |
10883 | ||
10884 | /* Notify protocols, that we are about to destroy | |
eb13da1a | 10885 | * this device. They should clean all the things. |
10886 | * | |
10887 | * Note that dev->reg_state stays at NETREG_REGISTERED. | |
10888 | * This is wanted because this way 8021q and macvlan know | |
10889 | * the device is just moving and can keep their slaves up. | |
10890 | */ | |
ce286d32 | 10891 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
6549dd43 | 10892 | rcu_barrier(); |
38e01b30 | 10893 | |
d4e4fdf9 | 10894 | new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); |
38e01b30 | 10895 | /* If there is an ifindex conflict assign a new one */ |
eeb85a14 AV |
10896 | if (!new_ifindex) { |
10897 | if (__dev_get_by_index(net, dev->ifindex)) | |
10898 | new_ifindex = dev_new_index(net); | |
10899 | else | |
10900 | new_ifindex = dev->ifindex; | |
10901 | } | |
38e01b30 ND |
10902 | |
10903 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, | |
10904 | new_ifindex); | |
ce286d32 EB |
10905 | |
10906 | /* | |
10907 | * Flush the unicast and multicast chains | |
10908 | */ | |
a748ee24 | 10909 | dev_uc_flush(dev); |
22bedad3 | 10910 | dev_mc_flush(dev); |
ce286d32 | 10911 | |
4e66ae2e SH |
10912 | /* Send a netdev-removed uevent to the old namespace */ |
10913 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
4c75431a | 10914 | netdev_adjacent_del_links(dev); |
4e66ae2e | 10915 | |
93642e14 JP |
10916 | /* Move per-net netdevice notifiers that are following the netdevice */ |
10917 | move_netdevice_notifiers_dev_net(dev, net); | |
10918 | ||
ce286d32 | 10919 | /* Actually switch the network namespace */ |
c346dca1 | 10920 | dev_net_set(dev, net); |
38e01b30 | 10921 | dev->ifindex = new_ifindex; |
ce286d32 | 10922 | |
4e66ae2e SH |
10923 | /* Send a netdev-add uevent to the new namespace */ |
10924 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
4c75431a | 10925 | netdev_adjacent_add_links(dev); |
4e66ae2e | 10926 | |
8b41d188 | 10927 | /* Fixup kobjects */ |
a1b3f594 | 10928 | err = device_rename(&dev->dev, dev->name); |
8b41d188 | 10929 | WARN_ON(err); |
ce286d32 | 10930 | |
ef6a4c88 CB |
10931 | /* Adapt owner in case owning user namespace of target network |
10932 | * namespace is different from the original one. | |
10933 | */ | |
10934 | err = netdev_change_owner(dev, net_old, net); | |
10935 | WARN_ON(err); | |
10936 | ||
ce286d32 EB |
10937 | /* Add the device back in the hashes */ |
10938 | list_netdevice(dev); | |
10939 | ||
10940 | /* Notify protocols, that a new device appeared. */ | |
10941 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
10942 | ||
d90a909e EB |
10943 | /* |
10944 | * Prevent userspace races by waiting until the network | |
10945 | * device is fully setup before sending notifications. | |
10946 | */ | |
7f294054 | 10947 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
d90a909e | 10948 | |
ce286d32 EB |
10949 | synchronize_net(); |
10950 | err = 0; | |
10951 | out: | |
10952 | return err; | |
10953 | } | |
0854fa82 | 10954 | EXPORT_SYMBOL_GPL(__dev_change_net_namespace); |
ce286d32 | 10955 | |
f0bf90de | 10956 | static int dev_cpu_dead(unsigned int oldcpu) |
1da177e4 LT |
10957 | { |
10958 | struct sk_buff **list_skb; | |
1da177e4 | 10959 | struct sk_buff *skb; |
f0bf90de | 10960 | unsigned int cpu; |
97d8b6e3 | 10961 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
1da177e4 | 10962 | |
1da177e4 LT |
10963 | local_irq_disable(); |
10964 | cpu = smp_processor_id(); | |
10965 | sd = &per_cpu(softnet_data, cpu); | |
10966 | oldsd = &per_cpu(softnet_data, oldcpu); | |
10967 | ||
10968 | /* Find end of our completion_queue. */ | |
10969 | list_skb = &sd->completion_queue; | |
10970 | while (*list_skb) | |
10971 | list_skb = &(*list_skb)->next; | |
10972 | /* Append completion queue from offline CPU. */ | |
10973 | *list_skb = oldsd->completion_queue; | |
10974 | oldsd->completion_queue = NULL; | |
10975 | ||
1da177e4 | 10976 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
10977 | if (oldsd->output_queue) { |
10978 | *sd->output_queue_tailp = oldsd->output_queue; | |
10979 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
10980 | oldsd->output_queue = NULL; | |
10981 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
10982 | } | |
ac64da0b ED |
10983 | /* Append NAPI poll list from offline CPU, with one exception : |
10984 | * process_backlog() must be called by cpu owning percpu backlog. | |
10985 | * We properly handle process_queue & input_pkt_queue later. | |
10986 | */ | |
10987 | while (!list_empty(&oldsd->poll_list)) { | |
10988 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, | |
10989 | struct napi_struct, | |
10990 | poll_list); | |
10991 | ||
10992 | list_del_init(&napi->poll_list); | |
10993 | if (napi->poll == process_backlog) | |
10994 | napi->state = 0; | |
10995 | else | |
10996 | ____napi_schedule(sd, napi); | |
264524d5 | 10997 | } |
1da177e4 LT |
10998 | |
10999 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
11000 | local_irq_enable(); | |
11001 | ||
773fc8f6 | 11002 | #ifdef CONFIG_RPS |
11003 | remsd = oldsd->rps_ipi_list; | |
11004 | oldsd->rps_ipi_list = NULL; | |
11005 | #endif | |
11006 | /* send out pending IPI's on offline CPU */ | |
11007 | net_rps_send_ipi(remsd); | |
11008 | ||
1da177e4 | 11009 | /* Process offline CPU's input_pkt_queue */ |
76cc8b13 | 11010 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
ad0a043f | 11011 | netif_rx(skb); |
76cc8b13 | 11012 | input_queue_head_incr(oldsd); |
fec5e652 | 11013 | } |
ac64da0b | 11014 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
ad0a043f | 11015 | netif_rx(skb); |
76cc8b13 TH |
11016 | input_queue_head_incr(oldsd); |
11017 | } | |
1da177e4 | 11018 | |
f0bf90de | 11019 | return 0; |
1da177e4 | 11020 | } |
1da177e4 | 11021 | |
7f353bf2 | 11022 | /** |
b63365a2 HX |
11023 | * netdev_increment_features - increment feature set by one |
11024 | * @all: current feature set | |
11025 | * @one: new feature set | |
11026 | * @mask: mask feature set | |
7f353bf2 HX |
11027 | * |
11028 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
11029 | * @one to the master device with current feature set @all. Will not |
11030 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 11031 | */ |
c8f44aff MM |
11032 | netdev_features_t netdev_increment_features(netdev_features_t all, |
11033 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 11034 | { |
c8cd0989 | 11035 | if (mask & NETIF_F_HW_CSUM) |
a188222b | 11036 | mask |= NETIF_F_CSUM_MASK; |
1742f183 | 11037 | mask |= NETIF_F_VLAN_CHALLENGED; |
7f353bf2 | 11038 | |
a188222b | 11039 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
1742f183 | 11040 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
c6e1a0d1 | 11041 | |
1742f183 | 11042 | /* If one device supports hw checksumming, set for all. */ |
c8cd0989 TH |
11043 | if (all & NETIF_F_HW_CSUM) |
11044 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); | |
7f353bf2 HX |
11045 | |
11046 | return all; | |
11047 | } | |
b63365a2 | 11048 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 11049 | |
430f03cd | 11050 | static struct hlist_head * __net_init netdev_create_hash(void) |
30d97d35 PE |
11051 | { |
11052 | int i; | |
11053 | struct hlist_head *hash; | |
11054 | ||
6da2ec56 | 11055 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
30d97d35 PE |
11056 | if (hash != NULL) |
11057 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
11058 | INIT_HLIST_HEAD(&hash[i]); | |
11059 | ||
11060 | return hash; | |
11061 | } | |
11062 | ||
881d966b | 11063 | /* Initialize per network namespace state */ |
4665079c | 11064 | static int __net_init netdev_init(struct net *net) |
881d966b | 11065 | { |
d9f37d01 | 11066 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
c593642c | 11067 | 8 * sizeof_field(struct napi_struct, gro_bitmask)); |
d9f37d01 | 11068 | |
9c1be193 | 11069 | INIT_LIST_HEAD(&net->dev_base_head); |
881d966b | 11070 | |
30d97d35 PE |
11071 | net->dev_name_head = netdev_create_hash(); |
11072 | if (net->dev_name_head == NULL) | |
11073 | goto err_name; | |
881d966b | 11074 | |
30d97d35 PE |
11075 | net->dev_index_head = netdev_create_hash(); |
11076 | if (net->dev_index_head == NULL) | |
11077 | goto err_idx; | |
881d966b | 11078 | |
a30c7b42 JP |
11079 | RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain); |
11080 | ||
881d966b | 11081 | return 0; |
30d97d35 PE |
11082 | |
11083 | err_idx: | |
11084 | kfree(net->dev_name_head); | |
11085 | err_name: | |
11086 | return -ENOMEM; | |
881d966b EB |
11087 | } |
11088 | ||
f0db275a SH |
11089 | /** |
11090 | * netdev_drivername - network driver for the device | |
11091 | * @dev: network device | |
f0db275a SH |
11092 | * |
11093 | * Determine network driver for device. | |
11094 | */ | |
3019de12 | 11095 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 11096 | { |
cf04a4c7 SH |
11097 | const struct device_driver *driver; |
11098 | const struct device *parent; | |
3019de12 | 11099 | const char *empty = ""; |
6579e57b AV |
11100 | |
11101 | parent = dev->dev.parent; | |
6579e57b | 11102 | if (!parent) |
3019de12 | 11103 | return empty; |
6579e57b AV |
11104 | |
11105 | driver = parent->driver; | |
11106 | if (driver && driver->name) | |
3019de12 DM |
11107 | return driver->name; |
11108 | return empty; | |
6579e57b AV |
11109 | } |
11110 | ||
6ea754eb JP |
11111 | static void __netdev_printk(const char *level, const struct net_device *dev, |
11112 | struct va_format *vaf) | |
256df2f3 | 11113 | { |
b004ff49 | 11114 | if (dev && dev->dev.parent) { |
6ea754eb JP |
11115 | dev_printk_emit(level[1] - '0', |
11116 | dev->dev.parent, | |
11117 | "%s %s %s%s: %pV", | |
11118 | dev_driver_string(dev->dev.parent), | |
11119 | dev_name(dev->dev.parent), | |
11120 | netdev_name(dev), netdev_reg_state(dev), | |
11121 | vaf); | |
b004ff49 | 11122 | } else if (dev) { |
6ea754eb JP |
11123 | printk("%s%s%s: %pV", |
11124 | level, netdev_name(dev), netdev_reg_state(dev), vaf); | |
b004ff49 | 11125 | } else { |
6ea754eb | 11126 | printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 11127 | } |
256df2f3 JP |
11128 | } |
11129 | ||
6ea754eb JP |
11130 | void netdev_printk(const char *level, const struct net_device *dev, |
11131 | const char *format, ...) | |
256df2f3 JP |
11132 | { |
11133 | struct va_format vaf; | |
11134 | va_list args; | |
256df2f3 JP |
11135 | |
11136 | va_start(args, format); | |
11137 | ||
11138 | vaf.fmt = format; | |
11139 | vaf.va = &args; | |
11140 | ||
6ea754eb | 11141 | __netdev_printk(level, dev, &vaf); |
b004ff49 | 11142 | |
256df2f3 | 11143 | va_end(args); |
256df2f3 JP |
11144 | } |
11145 | EXPORT_SYMBOL(netdev_printk); | |
11146 | ||
11147 | #define define_netdev_printk_level(func, level) \ | |
6ea754eb | 11148 | void func(const struct net_device *dev, const char *fmt, ...) \ |
256df2f3 | 11149 | { \ |
256df2f3 JP |
11150 | struct va_format vaf; \ |
11151 | va_list args; \ | |
11152 | \ | |
11153 | va_start(args, fmt); \ | |
11154 | \ | |
11155 | vaf.fmt = fmt; \ | |
11156 | vaf.va = &args; \ | |
11157 | \ | |
6ea754eb | 11158 | __netdev_printk(level, dev, &vaf); \ |
b004ff49 | 11159 | \ |
256df2f3 | 11160 | va_end(args); \ |
256df2f3 JP |
11161 | } \ |
11162 | EXPORT_SYMBOL(func); | |
11163 | ||
11164 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
11165 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
11166 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
11167 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
11168 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
11169 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
11170 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
11171 | ||
4665079c | 11172 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
11173 | { |
11174 | kfree(net->dev_name_head); | |
11175 | kfree(net->dev_index_head); | |
ee21b18b VA |
11176 | if (net != &init_net) |
11177 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); | |
881d966b EB |
11178 | } |
11179 | ||
022cbae6 | 11180 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
11181 | .init = netdev_init, |
11182 | .exit = netdev_exit, | |
11183 | }; | |
11184 | ||
ee403248 | 11185 | static void __net_exit default_device_exit_net(struct net *net) |
ce286d32 | 11186 | { |
e008b5fc | 11187 | struct net_device *dev, *aux; |
ce286d32 | 11188 | /* |
e008b5fc | 11189 | * Push all migratable network devices back to the |
ce286d32 EB |
11190 | * initial network namespace |
11191 | */ | |
ee403248 | 11192 | ASSERT_RTNL(); |
e008b5fc | 11193 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 11194 | int err; |
aca51397 | 11195 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
11196 | |
11197 | /* Ignore unmoveable devices (i.e. loopback) */ | |
11198 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
11199 | continue; | |
11200 | ||
e008b5fc | 11201 | /* Leave virtual devices for the generic cleanup */ |
3a5ca857 | 11202 | if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund) |
e008b5fc | 11203 | continue; |
d0c082ce | 11204 | |
25985edc | 11205 | /* Push remaining network devices to init_net */ |
aca51397 | 11206 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
75ea27d0 | 11207 | if (netdev_name_in_use(&init_net, fb_name)) |
55b40dbf | 11208 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); |
0854fa82 | 11209 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
ce286d32 | 11210 | if (err) { |
7b6cd1ce JP |
11211 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
11212 | __func__, dev->name, err); | |
aca51397 | 11213 | BUG(); |
ce286d32 EB |
11214 | } |
11215 | } | |
ce286d32 EB |
11216 | } |
11217 | ||
04dc7f6b EB |
11218 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
11219 | { | |
11220 | /* At exit all network devices most be removed from a network | |
b595076a | 11221 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
11222 | * Do this across as many network namespaces as possible to |
11223 | * improve batching efficiency. | |
11224 | */ | |
11225 | struct net_device *dev; | |
11226 | struct net *net; | |
11227 | LIST_HEAD(dev_kill_list); | |
11228 | ||
ee403248 ED |
11229 | rtnl_lock(); |
11230 | list_for_each_entry(net, net_list, exit_list) { | |
11231 | default_device_exit_net(net); | |
11232 | cond_resched(); | |
11233 | } | |
ee403248 | 11234 | |
04dc7f6b EB |
11235 | list_for_each_entry(net, net_list, exit_list) { |
11236 | for_each_netdev_reverse(net, dev) { | |
b0ab2fab | 11237 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
04dc7f6b EB |
11238 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
11239 | else | |
11240 | unregister_netdevice_queue(dev, &dev_kill_list); | |
11241 | } | |
11242 | } | |
11243 | unregister_netdevice_many(&dev_kill_list); | |
11244 | rtnl_unlock(); | |
11245 | } | |
11246 | ||
022cbae6 | 11247 | static struct pernet_operations __net_initdata default_device_ops = { |
04dc7f6b | 11248 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
11249 | }; |
11250 | ||
1da177e4 LT |
11251 | /* |
11252 | * Initialize the DEV module. At boot time this walks the device list and | |
11253 | * unhooks any devices that fail to initialise (normally hardware not | |
11254 | * present) and leaves us with a valid list of present and active devices. | |
11255 | * | |
11256 | */ | |
11257 | ||
11258 | /* | |
11259 | * This is called single threaded during boot, so no need | |
11260 | * to take the rtnl semaphore. | |
11261 | */ | |
11262 | static int __init net_dev_init(void) | |
11263 | { | |
11264 | int i, rc = -ENOMEM; | |
11265 | ||
11266 | BUG_ON(!dev_boot_phase); | |
11267 | ||
1da177e4 LT |
11268 | if (dev_proc_init()) |
11269 | goto out; | |
11270 | ||
8b41d188 | 11271 | if (netdev_kobject_init()) |
1da177e4 LT |
11272 | goto out; |
11273 | ||
11274 | INIT_LIST_HEAD(&ptype_all); | |
82d8a867 | 11275 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
11276 | INIT_LIST_HEAD(&ptype_base[i]); |
11277 | ||
881d966b EB |
11278 | if (register_pernet_subsys(&netdev_net_ops)) |
11279 | goto out; | |
1da177e4 LT |
11280 | |
11281 | /* | |
11282 | * Initialise the packet receive queues. | |
11283 | */ | |
11284 | ||
6f912042 | 11285 | for_each_possible_cpu(i) { |
41852497 | 11286 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
e36fa2f7 | 11287 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 11288 | |
41852497 ED |
11289 | INIT_WORK(flush, flush_backlog); |
11290 | ||
e36fa2f7 | 11291 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 11292 | skb_queue_head_init(&sd->process_queue); |
f53c7239 SK |
11293 | #ifdef CONFIG_XFRM_OFFLOAD |
11294 | skb_queue_head_init(&sd->xfrm_backlog); | |
11295 | #endif | |
e36fa2f7 | 11296 | INIT_LIST_HEAD(&sd->poll_list); |
a9cbd588 | 11297 | sd->output_queue_tailp = &sd->output_queue; |
df334545 | 11298 | #ifdef CONFIG_RPS |
545b8c8d | 11299 | INIT_CSD(&sd->csd, rps_trigger_softirq, sd); |
e36fa2f7 | 11300 | sd->cpu = i; |
1e94d72f | 11301 | #endif |
0a9627f2 | 11302 | |
7c4ec749 | 11303 | init_gro_hash(&sd->backlog); |
e36fa2f7 ED |
11304 | sd->backlog.poll = process_backlog; |
11305 | sd->backlog.weight = weight_p; | |
1da177e4 LT |
11306 | } |
11307 | ||
1da177e4 LT |
11308 | dev_boot_phase = 0; |
11309 | ||
505d4f73 EB |
11310 | /* The loopback device is special if any other network devices |
11311 | * is present in a network namespace the loopback device must | |
11312 | * be present. Since we now dynamically allocate and free the | |
11313 | * loopback device ensure this invariant is maintained by | |
11314 | * keeping the loopback device as the first device on the | |
11315 | * list of network devices. Ensuring the loopback devices | |
11316 | * is the first device that appears and the last network device | |
11317 | * that disappears. | |
11318 | */ | |
11319 | if (register_pernet_device(&loopback_net_ops)) | |
11320 | goto out; | |
11321 | ||
11322 | if (register_pernet_device(&default_device_ops)) | |
11323 | goto out; | |
11324 | ||
962cf36c CM |
11325 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
11326 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 | 11327 | |
f0bf90de SAS |
11328 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
11329 | NULL, dev_cpu_dead); | |
11330 | WARN_ON(rc < 0); | |
1da177e4 LT |
11331 | rc = 0; |
11332 | out: | |
11333 | return rc; | |
11334 | } | |
11335 | ||
11336 | subsys_initcall(net_dev_init); |