Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
722c9a0c | 3 | * NET3 Protocol independent device support routines. |
1da177e4 | 4 | * |
1da177e4 | 5 | * Derived from the non IP parts of dev.c 1.0.19 |
722c9a0c | 6 | * Authors: Ross Biro |
1da177e4 LT |
7 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
8 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
9 | * | |
10 | * Additional Authors: | |
11 | * Florian la Roche <rzsfl@rz.uni-sb.de> | |
12 | * Alan Cox <gw4pts@gw4pts.ampr.org> | |
13 | * David Hinds <dahinds@users.sourceforge.net> | |
14 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
15 | * Adam Sulmicki <adam@cfar.umd.edu> | |
16 | * Pekka Riikonen <priikone@poesidon.pspt.fi> | |
17 | * | |
18 | * Changes: | |
19 | * D.J. Barrow : Fixed bug where dev->refcnt gets set | |
722c9a0c | 20 | * to 2 if register_netdev gets called |
21 | * before net_dev_init & also removed a | |
22 | * few lines of code in the process. | |
1da177e4 LT |
23 | * Alan Cox : device private ioctl copies fields back. |
24 | * Alan Cox : Transmit queue code does relevant | |
25 | * stunts to keep the queue safe. | |
26 | * Alan Cox : Fixed double lock. | |
27 | * Alan Cox : Fixed promisc NULL pointer trap | |
28 | * ???????? : Support the full private ioctl range | |
29 | * Alan Cox : Moved ioctl permission check into | |
30 | * drivers | |
31 | * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI | |
32 | * Alan Cox : 100 backlog just doesn't cut it when | |
33 | * you start doing multicast video 8) | |
34 | * Alan Cox : Rewrote net_bh and list manager. | |
722c9a0c | 35 | * Alan Cox : Fix ETH_P_ALL echoback lengths. |
1da177e4 LT |
36 | * Alan Cox : Took out transmit every packet pass |
37 | * Saved a few bytes in the ioctl handler | |
38 | * Alan Cox : Network driver sets packet type before | |
39 | * calling netif_rx. Saves a function | |
40 | * call a packet. | |
41 | * Alan Cox : Hashed net_bh() | |
42 | * Richard Kooijman: Timestamp fixes. | |
43 | * Alan Cox : Wrong field in SIOCGIFDSTADDR | |
44 | * Alan Cox : Device lock protection. | |
722c9a0c | 45 | * Alan Cox : Fixed nasty side effect of device close |
1da177e4 LT |
46 | * changes. |
47 | * Rudi Cilibrasi : Pass the right thing to | |
48 | * set_mac_address() | |
49 | * Dave Miller : 32bit quantity for the device lock to | |
50 | * make it work out on a Sparc. | |
51 | * Bjorn Ekwall : Added KERNELD hack. | |
52 | * Alan Cox : Cleaned up the backlog initialise. | |
53 | * Craig Metz : SIOCGIFCONF fix if space for under | |
54 | * 1 device. | |
55 | * Thomas Bogendoerfer : Return ENODEV for dev_open, if there | |
56 | * is no device open function. | |
57 | * Andi Kleen : Fix error reporting for SIOCGIFCONF | |
58 | * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF | |
59 | * Cyrus Durgin : Cleaned for KMOD | |
60 | * Adam Sulmicki : Bug Fix : Network Device Unload | |
61 | * A network device unload needs to purge | |
62 | * the backlog queue. | |
63 | * Paul Rusty Russell : SIOCSIFNAME | |
64 | * Pekka Riikonen : Netdev boot-time settings code | |
65 | * Andrew Morton : Make unregister_netdevice wait | |
722c9a0c | 66 | * indefinitely on dev->refcnt |
67 | * J Hadi Salim : - Backlog queue sampling | |
1da177e4 LT |
68 | * - netif_rx() feedback |
69 | */ | |
70 | ||
7c0f6ba6 | 71 | #include <linux/uaccess.h> |
aabb4af9 | 72 | #include <linux/bitmap.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> |
2f3c7195 | 80 | #include <linux/sched/isolation.h> |
f1083048 | 81 | #include <linux/sched/mm.h> |
dad6b977 | 82 | #include <linux/smpboot.h> |
4a3e2f71 | 83 | #include <linux/mutex.h> |
11d6011c | 84 | #include <linux/rwsem.h> |
1da177e4 LT |
85 | #include <linux/string.h> |
86 | #include <linux/mm.h> | |
87 | #include <linux/socket.h> | |
88 | #include <linux/sockios.h> | |
89 | #include <linux/errno.h> | |
90 | #include <linux/interrupt.h> | |
91 | #include <linux/if_ether.h> | |
92 | #include <linux/netdevice.h> | |
93 | #include <linux/etherdevice.h> | |
0187bdfb | 94 | #include <linux/ethtool.h> |
1da177e4 | 95 | #include <linux/skbuff.h> |
29863d41 | 96 | #include <linux/kthread.h> |
a7862b45 | 97 | #include <linux/bpf.h> |
b5cdae32 | 98 | #include <linux/bpf_trace.h> |
457c4cbc | 99 | #include <net/net_namespace.h> |
1da177e4 | 100 | #include <net/sock.h> |
02d62e86 | 101 | #include <net/busy_poll.h> |
1da177e4 | 102 | #include <linux/rtnetlink.h> |
1da177e4 | 103 | #include <linux/stat.h> |
b14a9fc4 | 104 | #include <net/dsa.h> |
1da177e4 | 105 | #include <net/dst.h> |
fc4099f1 | 106 | #include <net/dst_metadata.h> |
04f00ab2 | 107 | #include <net/gro.h> |
1da177e4 | 108 | #include <net/pkt_sched.h> |
87d83093 | 109 | #include <net/pkt_cls.h> |
1da177e4 | 110 | #include <net/checksum.h> |
44540960 | 111 | #include <net/xfrm.h> |
e420bed0 | 112 | #include <net/tcx.h> |
1da177e4 LT |
113 | #include <linux/highmem.h> |
114 | #include <linux/init.h> | |
1da177e4 | 115 | #include <linux/module.h> |
1da177e4 LT |
116 | #include <linux/netpoll.h> |
117 | #include <linux/rcupdate.h> | |
118 | #include <linux/delay.h> | |
1da177e4 | 119 | #include <net/iw_handler.h> |
1da177e4 | 120 | #include <asm/current.h> |
5bdb9886 | 121 | #include <linux/audit.h> |
db217334 | 122 | #include <linux/dmaengine.h> |
f6a78bfc | 123 | #include <linux/err.h> |
c7fa9d18 | 124 | #include <linux/ctype.h> |
723e98b7 | 125 | #include <linux/if_arp.h> |
6de329e2 | 126 | #include <linux/if_vlan.h> |
8f0f2223 | 127 | #include <linux/ip.h> |
ad55dcaf | 128 | #include <net/ip.h> |
25cd9ba0 | 129 | #include <net/mpls.h> |
8f0f2223 DM |
130 | #include <linux/ipv6.h> |
131 | #include <linux/in.h> | |
b6b2fed1 DM |
132 | #include <linux/jhash.h> |
133 | #include <linux/random.h> | |
9cbc1cb8 | 134 | #include <trace/events/napi.h> |
cf66ba58 | 135 | #include <trace/events/net.h> |
07dc22e7 | 136 | #include <trace/events/skb.h> |
70713ddd | 137 | #include <trace/events/qdisc.h> |
bf4ea1d0 | 138 | #include <trace/events/xdp.h> |
caeda9b9 | 139 | #include <linux/inetdevice.h> |
c445477d | 140 | #include <linux/cpu_rmap.h> |
c5905afb | 141 | #include <linux/static_key.h> |
af12fa6e | 142 | #include <linux/hashtable.h> |
60877a32 | 143 | #include <linux/vmalloc.h> |
529d0489 | 144 | #include <linux/if_macvlan.h> |
e7fd2885 | 145 | #include <linux/errqueue.h> |
3b47d303 | 146 | #include <linux/hrtimer.h> |
7463acfb | 147 | #include <linux/netfilter_netdev.h> |
40e4e713 | 148 | #include <linux/crash_dump.h> |
b72b5bf6 | 149 | #include <linux/sctp.h> |
ae847f40 | 150 | #include <net/udp_tunnel.h> |
6621dd29 | 151 | #include <linux/net_namespace.h> |
aaa5d90b | 152 | #include <linux/indirect_call_wrapper.h> |
af3836df | 153 | #include <net/devlink.h> |
bd869245 | 154 | #include <linux/pm_runtime.h> |
3744741a | 155 | #include <linux/prandom.h> |
127d7355 | 156 | #include <linux/once_lite.h> |
49e47a5b | 157 | #include <net/netdev_rx_queue.h> |
2b0cfa6e LB |
158 | #include <net/page_pool/types.h> |
159 | #include <net/page_pool/helpers.h> | |
490a79fa | 160 | #include <net/rps.h> |
6916e461 | 161 | #include <linux/phy_link_topology_core.h> |
1da177e4 | 162 | |
6264f58c | 163 | #include "dev.h" |
342709ef PE |
164 | #include "net-sysfs.h" |
165 | ||
1da177e4 | 166 | static DEFINE_SPINLOCK(ptype_lock); |
900ff8c6 | 167 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
1da177e4 | 168 | |
ae78dbfa | 169 | static int netif_rx_internal(struct sk_buff *skb); |
26372605 PM |
170 | static int call_netdevice_notifiers_extack(unsigned long val, |
171 | struct net_device *dev, | |
172 | struct netlink_ext_ack *extack); | |
ae78dbfa | 173 | |
6c557001 FW |
174 | static DEFINE_MUTEX(ifalias_mutex); |
175 | ||
af12fa6e ET |
176 | /* protects napi_hash addition/deletion and napi_gen_id */ |
177 | static DEFINE_SPINLOCK(napi_hash_lock); | |
178 | ||
52bd2d62 | 179 | static unsigned int napi_gen_id = NR_CPUS; |
6180d9de | 180 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
af12fa6e | 181 | |
11d6011c | 182 | static DECLARE_RWSEM(devnet_rename_sem); |
c91f6df2 | 183 | |
4e985ada TG |
184 | static inline void dev_base_seq_inc(struct net *net) |
185 | { | |
590e92cd ED |
186 | unsigned int val = net->dev_base_seq + 1; |
187 | ||
188 | WRITE_ONCE(net->dev_base_seq, val ?: 1); | |
4e985ada TG |
189 | } |
190 | ||
881d966b | 191 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 192 | { |
8387ff25 | 193 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
95c96174 | 194 | |
08e9897d | 195 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
196 | } |
197 | ||
881d966b | 198 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 199 | { |
7c28bd0b | 200 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
201 | } |
202 | ||
dad6b977 SAS |
203 | #ifndef CONFIG_PREEMPT_RT |
204 | ||
205 | static DEFINE_STATIC_KEY_FALSE(use_backlog_threads_key); | |
206 | ||
207 | static int __init setup_backlog_napi_threads(char *arg) | |
208 | { | |
209 | static_branch_enable(&use_backlog_threads_key); | |
210 | return 0; | |
211 | } | |
212 | early_param("thread_backlog_napi", setup_backlog_napi_threads); | |
213 | ||
214 | static bool use_backlog_threads(void) | |
215 | { | |
216 | return static_branch_unlikely(&use_backlog_threads_key); | |
217 | } | |
218 | ||
219 | #else | |
220 | ||
221 | static bool use_backlog_threads(void) | |
222 | { | |
223 | return true; | |
224 | } | |
225 | ||
226 | #endif | |
227 | ||
765b11f8 SAS |
228 | static inline void backlog_lock_irq_save(struct softnet_data *sd, |
229 | unsigned long *flags) | |
152102c7 | 230 | { |
80d2eefc | 231 | if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) |
e722db8d SAS |
232 | spin_lock_irqsave(&sd->input_pkt_queue.lock, *flags); |
233 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
234 | local_irq_save(*flags); | |
152102c7 CG |
235 | } |
236 | ||
765b11f8 | 237 | static inline void backlog_lock_irq_disable(struct softnet_data *sd) |
152102c7 | 238 | { |
80d2eefc | 239 | if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) |
e722db8d SAS |
240 | spin_lock_irq(&sd->input_pkt_queue.lock); |
241 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
242 | local_irq_disable(); | |
243 | } | |
244 | ||
765b11f8 SAS |
245 | static inline void backlog_unlock_irq_restore(struct softnet_data *sd, |
246 | unsigned long *flags) | |
e722db8d | 247 | { |
80d2eefc | 248 | if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) |
e722db8d SAS |
249 | spin_unlock_irqrestore(&sd->input_pkt_queue.lock, *flags); |
250 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
251 | local_irq_restore(*flags); | |
252 | } | |
253 | ||
765b11f8 | 254 | static inline void backlog_unlock_irq_enable(struct softnet_data *sd) |
e722db8d | 255 | { |
80d2eefc | 256 | if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) |
e722db8d SAS |
257 | spin_unlock_irq(&sd->input_pkt_queue.lock); |
258 | else if (!IS_ENABLED(CONFIG_PREEMPT_RT)) | |
259 | local_irq_enable(); | |
152102c7 CG |
260 | } |
261 | ||
ff927412 JP |
262 | static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, |
263 | const char *name) | |
264 | { | |
265 | struct netdev_name_node *name_node; | |
266 | ||
267 | name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); | |
268 | if (!name_node) | |
269 | return NULL; | |
270 | INIT_HLIST_NODE(&name_node->hlist); | |
271 | name_node->dev = dev; | |
272 | name_node->name = name; | |
273 | return name_node; | |
274 | } | |
275 | ||
276 | static struct netdev_name_node * | |
277 | netdev_name_node_head_alloc(struct net_device *dev) | |
278 | { | |
36fbf1e5 JP |
279 | struct netdev_name_node *name_node; |
280 | ||
281 | name_node = netdev_name_node_alloc(dev, dev->name); | |
282 | if (!name_node) | |
283 | return NULL; | |
284 | INIT_LIST_HEAD(&name_node->list); | |
285 | return name_node; | |
ff927412 JP |
286 | } |
287 | ||
288 | static void netdev_name_node_free(struct netdev_name_node *name_node) | |
289 | { | |
290 | kfree(name_node); | |
291 | } | |
292 | ||
293 | static void netdev_name_node_add(struct net *net, | |
294 | struct netdev_name_node *name_node) | |
295 | { | |
296 | hlist_add_head_rcu(&name_node->hlist, | |
297 | dev_name_hash(net, name_node->name)); | |
298 | } | |
299 | ||
300 | static void netdev_name_node_del(struct netdev_name_node *name_node) | |
301 | { | |
302 | hlist_del_rcu(&name_node->hlist); | |
303 | } | |
304 | ||
305 | static struct netdev_name_node *netdev_name_node_lookup(struct net *net, | |
306 | const char *name) | |
307 | { | |
308 | struct hlist_head *head = dev_name_hash(net, name); | |
309 | struct netdev_name_node *name_node; | |
310 | ||
311 | hlist_for_each_entry(name_node, head, hlist) | |
312 | if (!strcmp(name_node->name, name)) | |
313 | return name_node; | |
314 | return NULL; | |
315 | } | |
316 | ||
317 | static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, | |
318 | const char *name) | |
319 | { | |
320 | struct hlist_head *head = dev_name_hash(net, name); | |
321 | struct netdev_name_node *name_node; | |
322 | ||
323 | hlist_for_each_entry_rcu(name_node, head, hlist) | |
324 | if (!strcmp(name_node->name, name)) | |
325 | return name_node; | |
326 | return NULL; | |
327 | } | |
328 | ||
75ea27d0 AT |
329 | bool netdev_name_in_use(struct net *net, const char *name) |
330 | { | |
331 | return netdev_name_node_lookup(net, name); | |
332 | } | |
333 | EXPORT_SYMBOL(netdev_name_in_use); | |
334 | ||
36fbf1e5 JP |
335 | int netdev_name_node_alt_create(struct net_device *dev, const char *name) |
336 | { | |
337 | struct netdev_name_node *name_node; | |
338 | struct net *net = dev_net(dev); | |
339 | ||
340 | name_node = netdev_name_node_lookup(net, name); | |
341 | if (name_node) | |
342 | return -EEXIST; | |
343 | name_node = netdev_name_node_alloc(dev, name); | |
344 | if (!name_node) | |
345 | return -ENOMEM; | |
346 | netdev_name_node_add(net, name_node); | |
347 | /* The node that holds dev->name acts as a head of per-device list. */ | |
9f308313 | 348 | list_add_tail_rcu(&name_node->list, &dev->name_node->list); |
36fbf1e5 JP |
349 | |
350 | return 0; | |
351 | } | |
36fbf1e5 | 352 | |
723de3eb | 353 | static void netdev_name_node_alt_free(struct rcu_head *head) |
36fbf1e5 | 354 | { |
723de3eb JK |
355 | struct netdev_name_node *name_node = |
356 | container_of(head, struct netdev_name_node, rcu); | |
357 | ||
36fbf1e5 JP |
358 | kfree(name_node->name); |
359 | netdev_name_node_free(name_node); | |
360 | } | |
361 | ||
723de3eb JK |
362 | static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node) |
363 | { | |
364 | netdev_name_node_del(name_node); | |
365 | list_del(&name_node->list); | |
366 | call_rcu(&name_node->rcu, netdev_name_node_alt_free); | |
367 | } | |
368 | ||
36fbf1e5 JP |
369 | int netdev_name_node_alt_destroy(struct net_device *dev, const char *name) |
370 | { | |
371 | struct netdev_name_node *name_node; | |
372 | struct net *net = dev_net(dev); | |
373 | ||
374 | name_node = netdev_name_node_lookup(net, name); | |
375 | if (!name_node) | |
376 | return -ENOENT; | |
e08ad805 ED |
377 | /* lookup might have found our primary name or a name belonging |
378 | * to another device. | |
379 | */ | |
380 | if (name_node == dev->name_node || name_node->dev != dev) | |
381 | return -EINVAL; | |
382 | ||
36fbf1e5 | 383 | __netdev_name_node_alt_destroy(name_node); |
36fbf1e5 JP |
384 | return 0; |
385 | } | |
36fbf1e5 JP |
386 | |
387 | static void netdev_name_node_alt_flush(struct net_device *dev) | |
388 | { | |
389 | struct netdev_name_node *name_node, *tmp; | |
390 | ||
723de3eb JK |
391 | list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) { |
392 | list_del(&name_node->list); | |
393 | netdev_name_node_alt_free(&name_node->rcu); | |
394 | } | |
36fbf1e5 JP |
395 | } |
396 | ||
ce286d32 | 397 | /* Device list insertion */ |
53759be9 | 398 | static void list_netdevice(struct net_device *dev) |
ce286d32 | 399 | { |
8e15aee6 | 400 | struct netdev_name_node *name_node; |
c346dca1 | 401 | struct net *net = dev_net(dev); |
ce286d32 EB |
402 | |
403 | ASSERT_RTNL(); | |
404 | ||
c6d14c84 | 405 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
ff927412 | 406 | netdev_name_node_add(net, dev->name_node); |
fb699dfd ED |
407 | hlist_add_head_rcu(&dev->index_hlist, |
408 | dev_index_hash(net, dev->ifindex)); | |
8e15aee6 JK |
409 | |
410 | netdev_for_each_altname(dev, name_node) | |
411 | netdev_name_node_add(net, name_node); | |
412 | ||
759ab1ed JK |
413 | /* We reserved the ifindex, this can't fail */ |
414 | WARN_ON(xa_store(&net->dev_by_index, dev->ifindex, dev, GFP_KERNEL)); | |
4e985ada TG |
415 | |
416 | dev_base_seq_inc(net); | |
ce286d32 EB |
417 | } |
418 | ||
fb699dfd ED |
419 | /* Device list removal |
420 | * caller must respect a RCU grace period before freeing/reusing dev | |
421 | */ | |
e51b9624 | 422 | static void unlist_netdevice(struct net_device *dev) |
ce286d32 | 423 | { |
8e15aee6 | 424 | struct netdev_name_node *name_node; |
759ab1ed JK |
425 | struct net *net = dev_net(dev); |
426 | ||
ce286d32 EB |
427 | ASSERT_RTNL(); |
428 | ||
759ab1ed JK |
429 | xa_erase(&net->dev_by_index, dev->ifindex); |
430 | ||
8e15aee6 JK |
431 | netdev_for_each_altname(dev, name_node) |
432 | netdev_name_node_del(name_node); | |
433 | ||
ce286d32 | 434 | /* Unlink dev from the device chain */ |
c6d14c84 | 435 | list_del_rcu(&dev->dev_list); |
ff927412 | 436 | netdev_name_node_del(dev->name_node); |
fb699dfd | 437 | hlist_del_rcu(&dev->index_hlist); |
4e985ada TG |
438 | |
439 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
440 | } |
441 | ||
1da177e4 LT |
442 | /* |
443 | * Our notifier list | |
444 | */ | |
445 | ||
f07d5b94 | 446 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
447 | |
448 | /* | |
449 | * Device drivers call our routines to queue packets here. We empty the | |
450 | * queue in the local softnet handler. | |
451 | */ | |
bea3348e | 452 | |
9958da05 | 453 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 454 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 455 | |
2b0cfa6e LB |
456 | /* Page_pool has a lockless array/stack to alloc/recycle pages. |
457 | * PP consumers must pay attention to run APIs in the appropriate context | |
458 | * (e.g. NAPI context). | |
459 | */ | |
5086f0fe | 460 | static DEFINE_PER_CPU(struct page_pool *, system_page_pool); |
2b0cfa6e | 461 | |
1a33e10e CW |
462 | #ifdef CONFIG_LOCKDEP |
463 | /* | |
464 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class | |
465 | * according to dev->type | |
466 | */ | |
467 | static const unsigned short netdev_lock_type[] = { | |
468 | ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, | |
469 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, | |
470 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, | |
471 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, | |
472 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, | |
473 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, | |
474 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, | |
475 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, | |
476 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, | |
477 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, | |
478 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, | |
479 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, | |
480 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, | |
481 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, | |
482 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; | |
483 | ||
484 | static const char *const netdev_lock_name[] = { | |
485 | "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", | |
486 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", | |
487 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", | |
488 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", | |
489 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", | |
490 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", | |
491 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", | |
492 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", | |
493 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", | |
494 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", | |
495 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", | |
496 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", | |
497 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", | |
498 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", | |
499 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; | |
500 | ||
501 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; | |
845e0ebb | 502 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
1a33e10e CW |
503 | |
504 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) | |
505 | { | |
506 | int i; | |
507 | ||
508 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) | |
509 | if (netdev_lock_type[i] == dev_type) | |
510 | return i; | |
511 | /* the last key is used by default */ | |
512 | return ARRAY_SIZE(netdev_lock_type) - 1; | |
513 | } | |
514 | ||
515 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
516 | unsigned short dev_type) | |
517 | { | |
518 | int i; | |
519 | ||
520 | i = netdev_lock_pos(dev_type); | |
521 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], | |
522 | netdev_lock_name[i]); | |
523 | } | |
845e0ebb CW |
524 | |
525 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
526 | { | |
527 | int i; | |
528 | ||
529 | i = netdev_lock_pos(dev->type); | |
530 | lockdep_set_class_and_name(&dev->addr_list_lock, | |
531 | &netdev_addr_lock_key[i], | |
532 | netdev_lock_name[i]); | |
533 | } | |
1a33e10e CW |
534 | #else |
535 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
536 | unsigned short dev_type) | |
537 | { | |
538 | } | |
845e0ebb CW |
539 | |
540 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
541 | { | |
542 | } | |
1a33e10e CW |
543 | #endif |
544 | ||
1da177e4 | 545 | /******************************************************************************* |
eb13da1a | 546 | * |
547 | * Protocol management and registration routines | |
548 | * | |
549 | *******************************************************************************/ | |
1da177e4 | 550 | |
1da177e4 | 551 | |
1da177e4 LT |
552 | /* |
553 | * Add a protocol ID to the list. Now that the input handler is | |
554 | * smarter we can dispense with all the messy stuff that used to be | |
555 | * here. | |
556 | * | |
557 | * BEWARE!!! Protocol handlers, mangling input packets, | |
558 | * MUST BE last in hash buckets and checking protocol handlers | |
559 | * MUST start from promiscuous ptype_all chain in net_bh. | |
560 | * It is true now, do not change it. | |
561 | * Explanation follows: if protocol handler, mangling packet, will | |
562 | * be the first on list, it is not able to sense, that packet | |
563 | * is cloned and should be copied-on-write, so that it will | |
564 | * change it and subsequent readers will get broken packet. | |
565 | * --ANK (980803) | |
566 | */ | |
567 | ||
c07b68e8 ED |
568 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
569 | { | |
570 | if (pt->type == htons(ETH_P_ALL)) | |
0b91fa4b | 571 | return pt->dev ? &pt->dev->ptype_all : &net_hotdata.ptype_all; |
c07b68e8 | 572 | else |
7866a621 SN |
573 | return pt->dev ? &pt->dev->ptype_specific : |
574 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
c07b68e8 ED |
575 | } |
576 | ||
1da177e4 LT |
577 | /** |
578 | * dev_add_pack - add packet handler | |
579 | * @pt: packet type declaration | |
580 | * | |
581 | * Add a protocol handler to the networking stack. The passed &packet_type | |
582 | * is linked into kernel lists and may not be freed until it has been | |
583 | * removed from the kernel lists. | |
584 | * | |
4ec93edb | 585 | * This call does not sleep therefore it can not |
1da177e4 LT |
586 | * guarantee all CPU's that are in middle of receiving packets |
587 | * will see the new packet type (until the next received packet). | |
588 | */ | |
589 | ||
590 | void dev_add_pack(struct packet_type *pt) | |
591 | { | |
c07b68e8 | 592 | struct list_head *head = ptype_head(pt); |
1da177e4 | 593 | |
c07b68e8 ED |
594 | spin_lock(&ptype_lock); |
595 | list_add_rcu(&pt->list, head); | |
596 | spin_unlock(&ptype_lock); | |
1da177e4 | 597 | } |
d1b19dff | 598 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 599 | |
1da177e4 LT |
600 | /** |
601 | * __dev_remove_pack - remove packet handler | |
602 | * @pt: packet type declaration | |
603 | * | |
604 | * Remove a protocol handler that was previously added to the kernel | |
605 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
606 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 607 | * returns. |
1da177e4 LT |
608 | * |
609 | * The packet type might still be in use by receivers | |
610 | * and must not be freed until after all the CPU's have gone | |
611 | * through a quiescent state. | |
612 | */ | |
613 | void __dev_remove_pack(struct packet_type *pt) | |
614 | { | |
c07b68e8 | 615 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
616 | struct packet_type *pt1; |
617 | ||
c07b68e8 | 618 | spin_lock(&ptype_lock); |
1da177e4 LT |
619 | |
620 | list_for_each_entry(pt1, head, list) { | |
621 | if (pt == pt1) { | |
622 | list_del_rcu(&pt->list); | |
623 | goto out; | |
624 | } | |
625 | } | |
626 | ||
7b6cd1ce | 627 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 628 | out: |
c07b68e8 | 629 | spin_unlock(&ptype_lock); |
1da177e4 | 630 | } |
d1b19dff ED |
631 | EXPORT_SYMBOL(__dev_remove_pack); |
632 | ||
1da177e4 LT |
633 | /** |
634 | * dev_remove_pack - remove packet handler | |
635 | * @pt: packet type declaration | |
636 | * | |
637 | * Remove a protocol handler that was previously added to the kernel | |
638 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
639 | * from the kernel lists and can be freed or reused once this function | |
640 | * returns. | |
641 | * | |
642 | * This call sleeps to guarantee that no CPU is looking at the packet | |
643 | * type after return. | |
644 | */ | |
645 | void dev_remove_pack(struct packet_type *pt) | |
646 | { | |
647 | __dev_remove_pack(pt); | |
4ec93edb | 648 | |
1da177e4 LT |
649 | synchronize_net(); |
650 | } | |
d1b19dff | 651 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 652 | |
62532da9 | 653 | |
1da177e4 | 654 | /******************************************************************************* |
eb13da1a | 655 | * |
656 | * Device Interface Subroutines | |
657 | * | |
658 | *******************************************************************************/ | |
1da177e4 | 659 | |
a54acb3a ND |
660 | /** |
661 | * dev_get_iflink - get 'iflink' value of a interface | |
662 | * @dev: targeted interface | |
663 | * | |
664 | * Indicates the ifindex the interface is linked to. | |
665 | * Physical interfaces have the same 'ifindex' and 'iflink' values. | |
666 | */ | |
667 | ||
668 | int dev_get_iflink(const struct net_device *dev) | |
669 | { | |
670 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) | |
671 | return dev->netdev_ops->ndo_get_iflink(dev); | |
672 | ||
e353ea9c | 673 | return READ_ONCE(dev->ifindex); |
a54acb3a ND |
674 | } |
675 | EXPORT_SYMBOL(dev_get_iflink); | |
676 | ||
fc4099f1 PS |
677 | /** |
678 | * dev_fill_metadata_dst - Retrieve tunnel egress information. | |
679 | * @dev: targeted interface | |
680 | * @skb: The packet. | |
681 | * | |
682 | * For better visibility of tunnel traffic OVS needs to retrieve | |
683 | * egress tunnel information for a packet. Following API allows | |
684 | * user to get this info. | |
685 | */ | |
686 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) | |
687 | { | |
688 | struct ip_tunnel_info *info; | |
689 | ||
690 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) | |
691 | return -EINVAL; | |
692 | ||
693 | info = skb_tunnel_info_unclone(skb); | |
694 | if (!info) | |
695 | return -ENOMEM; | |
696 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) | |
697 | return -EINVAL; | |
698 | ||
699 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); | |
700 | } | |
701 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); | |
702 | ||
ddb94eaf PNA |
703 | static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack) |
704 | { | |
705 | int k = stack->num_paths++; | |
706 | ||
707 | if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX)) | |
708 | return NULL; | |
709 | ||
710 | return &stack->path[k]; | |
711 | } | |
712 | ||
713 | int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr, | |
714 | struct net_device_path_stack *stack) | |
715 | { | |
716 | const struct net_device *last_dev; | |
717 | struct net_device_path_ctx ctx = { | |
718 | .dev = dev, | |
ddb94eaf PNA |
719 | }; |
720 | struct net_device_path *path; | |
721 | int ret = 0; | |
722 | ||
cf2df74e | 723 | memcpy(ctx.daddr, daddr, sizeof(ctx.daddr)); |
ddb94eaf PNA |
724 | stack->num_paths = 0; |
725 | while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) { | |
726 | last_dev = ctx.dev; | |
727 | path = dev_fwd_path(stack); | |
728 | if (!path) | |
729 | return -1; | |
730 | ||
731 | memset(path, 0, sizeof(struct net_device_path)); | |
732 | ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path); | |
733 | if (ret < 0) | |
734 | return -1; | |
735 | ||
736 | if (WARN_ON_ONCE(last_dev == ctx.dev)) | |
737 | return -1; | |
738 | } | |
a333215e FF |
739 | |
740 | if (!ctx.dev) | |
741 | return ret; | |
742 | ||
ddb94eaf PNA |
743 | path = dev_fwd_path(stack); |
744 | if (!path) | |
745 | return -1; | |
746 | path->type = DEV_PATH_ETHERNET; | |
747 | path->dev = ctx.dev; | |
748 | ||
749 | return ret; | |
750 | } | |
751 | EXPORT_SYMBOL_GPL(dev_fill_forward_path); | |
752 | ||
1da177e4 LT |
753 | /** |
754 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 755 | * @net: the applicable net namespace |
1da177e4 LT |
756 | * @name: name to find |
757 | * | |
1b3ef46c ED |
758 | * Find an interface by name. Must be called under RTNL semaphore. |
759 | * If the name is found a pointer to the device is returned. | |
760 | * If the name is not found then %NULL is returned. The | |
1da177e4 LT |
761 | * reference counters are not incremented so the caller must be |
762 | * careful with locks. | |
763 | */ | |
764 | ||
881d966b | 765 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 | 766 | { |
ff927412 | 767 | struct netdev_name_node *node_name; |
1da177e4 | 768 | |
ff927412 JP |
769 | node_name = netdev_name_node_lookup(net, name); |
770 | return node_name ? node_name->dev : NULL; | |
1da177e4 | 771 | } |
d1b19dff | 772 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 773 | |
72c9528b | 774 | /** |
722c9a0c | 775 | * dev_get_by_name_rcu - find a device by its name |
776 | * @net: the applicable net namespace | |
777 | * @name: name to find | |
778 | * | |
779 | * Find an interface by name. | |
780 | * If the name is found a pointer to the device is returned. | |
781 | * If the name is not found then %NULL is returned. | |
782 | * The reference counters are not incremented so the caller must be | |
783 | * careful with locks. The caller must hold RCU lock. | |
72c9528b ED |
784 | */ |
785 | ||
786 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
787 | { | |
ff927412 | 788 | struct netdev_name_node *node_name; |
72c9528b | 789 | |
ff927412 JP |
790 | node_name = netdev_name_node_lookup_rcu(net, name); |
791 | return node_name ? node_name->dev : NULL; | |
72c9528b ED |
792 | } |
793 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
794 | ||
70f7457a JK |
795 | /* Deprecated for new users, call netdev_get_by_name() instead */ |
796 | struct net_device *dev_get_by_name(struct net *net, const char *name) | |
797 | { | |
798 | struct net_device *dev; | |
799 | ||
800 | rcu_read_lock(); | |
801 | dev = dev_get_by_name_rcu(net, name); | |
802 | dev_hold(dev); | |
803 | rcu_read_unlock(); | |
804 | return dev; | |
805 | } | |
806 | EXPORT_SYMBOL(dev_get_by_name); | |
807 | ||
1da177e4 | 808 | /** |
70f7457a | 809 | * netdev_get_by_name() - find a device by its name |
c4ea43c5 | 810 | * @net: the applicable net namespace |
1da177e4 | 811 | * @name: name to find |
70f7457a JK |
812 | * @tracker: tracking object for the acquired reference |
813 | * @gfp: allocation flags for the tracker | |
1da177e4 LT |
814 | * |
815 | * Find an interface by name. This can be called from any | |
816 | * context and does its own locking. The returned handle has | |
70f7457a | 817 | * the usage count incremented and the caller must use netdev_put() to |
1da177e4 LT |
818 | * release it when it is no longer needed. %NULL is returned if no |
819 | * matching device is found. | |
820 | */ | |
70f7457a JK |
821 | struct net_device *netdev_get_by_name(struct net *net, const char *name, |
822 | netdevice_tracker *tracker, gfp_t gfp) | |
1da177e4 LT |
823 | { |
824 | struct net_device *dev; | |
825 | ||
70f7457a JK |
826 | dev = dev_get_by_name(net, name); |
827 | if (dev) | |
828 | netdev_tracker_alloc(dev, tracker, gfp); | |
1da177e4 LT |
829 | return dev; |
830 | } | |
70f7457a | 831 | EXPORT_SYMBOL(netdev_get_by_name); |
1da177e4 LT |
832 | |
833 | /** | |
834 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 835 | * @net: the applicable net namespace |
1da177e4 LT |
836 | * @ifindex: index of device |
837 | * | |
838 | * Search for an interface by index. Returns %NULL if the device | |
839 | * is not found or a pointer to the device. The device has not | |
840 | * had its reference counter increased so the caller must be careful | |
1b3ef46c | 841 | * about locking. The caller must hold the RTNL semaphore. |
1da177e4 LT |
842 | */ |
843 | ||
881d966b | 844 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 | 845 | { |
0bd8d536 ED |
846 | struct net_device *dev; |
847 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 848 | |
b67bfe0d | 849 | hlist_for_each_entry(dev, head, index_hlist) |
1da177e4 LT |
850 | if (dev->ifindex == ifindex) |
851 | return dev; | |
0bd8d536 | 852 | |
1da177e4 LT |
853 | return NULL; |
854 | } | |
d1b19dff | 855 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 856 | |
fb699dfd ED |
857 | /** |
858 | * dev_get_by_index_rcu - find a device by its ifindex | |
859 | * @net: the applicable net namespace | |
860 | * @ifindex: index of device | |
861 | * | |
862 | * Search for an interface by index. Returns %NULL if the device | |
863 | * is not found or a pointer to the device. The device has not | |
864 | * had its reference counter increased so the caller must be careful | |
865 | * about locking. The caller must hold RCU lock. | |
866 | */ | |
867 | ||
868 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
869 | { | |
fb699dfd ED |
870 | struct net_device *dev; |
871 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
872 | ||
b67bfe0d | 873 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
fb699dfd ED |
874 | if (dev->ifindex == ifindex) |
875 | return dev; | |
876 | ||
877 | return NULL; | |
878 | } | |
879 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
880 | ||
70f7457a JK |
881 | /* Deprecated for new users, call netdev_get_by_index() instead */ |
882 | struct net_device *dev_get_by_index(struct net *net, int ifindex) | |
883 | { | |
884 | struct net_device *dev; | |
885 | ||
886 | rcu_read_lock(); | |
887 | dev = dev_get_by_index_rcu(net, ifindex); | |
888 | dev_hold(dev); | |
889 | rcu_read_unlock(); | |
890 | return dev; | |
891 | } | |
892 | EXPORT_SYMBOL(dev_get_by_index); | |
1da177e4 LT |
893 | |
894 | /** | |
70f7457a | 895 | * netdev_get_by_index() - find a device by its ifindex |
c4ea43c5 | 896 | * @net: the applicable net namespace |
1da177e4 | 897 | * @ifindex: index of device |
70f7457a JK |
898 | * @tracker: tracking object for the acquired reference |
899 | * @gfp: allocation flags for the tracker | |
1da177e4 LT |
900 | * |
901 | * Search for an interface by index. Returns NULL if the device | |
902 | * is not found or a pointer to the device. The device returned has | |
903 | * had a reference added and the pointer is safe until the user calls | |
70f7457a | 904 | * netdev_put() to indicate they have finished with it. |
1da177e4 | 905 | */ |
70f7457a JK |
906 | struct net_device *netdev_get_by_index(struct net *net, int ifindex, |
907 | netdevice_tracker *tracker, gfp_t gfp) | |
1da177e4 LT |
908 | { |
909 | struct net_device *dev; | |
910 | ||
70f7457a JK |
911 | dev = dev_get_by_index(net, ifindex); |
912 | if (dev) | |
913 | netdev_tracker_alloc(dev, tracker, gfp); | |
1da177e4 LT |
914 | return dev; |
915 | } | |
70f7457a | 916 | EXPORT_SYMBOL(netdev_get_by_index); |
1da177e4 | 917 | |
90b602f8 ML |
918 | /** |
919 | * dev_get_by_napi_id - find a device by napi_id | |
920 | * @napi_id: ID of the NAPI struct | |
921 | * | |
922 | * Search for an interface by NAPI ID. Returns %NULL if the device | |
923 | * is not found or a pointer to the device. The device has not had | |
924 | * its reference counter increased so the caller must be careful | |
925 | * about locking. The caller must hold RCU lock. | |
926 | */ | |
927 | ||
928 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) | |
929 | { | |
930 | struct napi_struct *napi; | |
931 | ||
932 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
933 | ||
934 | if (napi_id < MIN_NAPI_ID) | |
935 | return NULL; | |
936 | ||
937 | napi = napi_by_id(napi_id); | |
938 | ||
939 | return napi ? napi->dev : NULL; | |
940 | } | |
941 | EXPORT_SYMBOL(dev_get_by_napi_id); | |
942 | ||
0840556e KI |
943 | static DEFINE_SEQLOCK(netdev_rename_lock); |
944 | ||
945 | void netdev_copy_name(struct net_device *dev, char *name) | |
946 | { | |
947 | unsigned int seq; | |
948 | ||
949 | do { | |
950 | seq = read_seqbegin(&netdev_rename_lock); | |
951 | strscpy(name, dev->name, IFNAMSIZ); | |
952 | } while (read_seqretry(&netdev_rename_lock, seq)); | |
953 | } | |
954 | ||
5dbe7c17 NS |
955 | /** |
956 | * netdev_get_name - get a netdevice name, knowing its ifindex. | |
957 | * @net: network namespace | |
958 | * @name: a pointer to the buffer where the name will be stored. | |
959 | * @ifindex: the ifindex of the interface to get the name from. | |
5dbe7c17 NS |
960 | */ |
961 | int netdev_get_name(struct net *net, char *name, int ifindex) | |
962 | { | |
963 | struct net_device *dev; | |
11d6011c | 964 | int ret; |
5dbe7c17 | 965 | |
5dbe7c17 | 966 | rcu_read_lock(); |
11d6011c | 967 | |
5dbe7c17 NS |
968 | dev = dev_get_by_index_rcu(net, ifindex); |
969 | if (!dev) { | |
11d6011c AD |
970 | ret = -ENODEV; |
971 | goto out; | |
5dbe7c17 NS |
972 | } |
973 | ||
0840556e | 974 | netdev_copy_name(dev, name); |
5dbe7c17 | 975 | |
11d6011c AD |
976 | ret = 0; |
977 | out: | |
978 | rcu_read_unlock(); | |
11d6011c | 979 | return ret; |
5dbe7c17 NS |
980 | } |
981 | ||
1da177e4 | 982 | /** |
941666c2 | 983 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 984 | * @net: the applicable net namespace |
1da177e4 LT |
985 | * @type: media type of device |
986 | * @ha: hardware address | |
987 | * | |
988 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
989 | * is not found or a pointer to the device. |
990 | * The caller must hold RCU or RTNL. | |
941666c2 | 991 | * The returned device has not had its ref count increased |
1da177e4 LT |
992 | * and the caller must therefore be careful about locking |
993 | * | |
1da177e4 LT |
994 | */ |
995 | ||
941666c2 ED |
996 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
997 | const char *ha) | |
1da177e4 LT |
998 | { |
999 | struct net_device *dev; | |
1000 | ||
941666c2 | 1001 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
1002 | if (dev->type == type && |
1003 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
1004 | return dev; |
1005 | ||
1006 | return NULL; | |
1da177e4 | 1007 | } |
941666c2 | 1008 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 1009 | |
881d966b | 1010 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 1011 | { |
99fe3c39 | 1012 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 1013 | |
99fe3c39 ED |
1014 | rcu_read_lock(); |
1015 | for_each_netdev_rcu(net, dev) | |
1016 | if (dev->type == type) { | |
1017 | dev_hold(dev); | |
1018 | ret = dev; | |
1019 | break; | |
1020 | } | |
1021 | rcu_read_unlock(); | |
1022 | return ret; | |
1da177e4 | 1023 | } |
1da177e4 LT |
1024 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
1025 | ||
1026 | /** | |
6c555490 | 1027 | * __dev_get_by_flags - find any device with given flags |
c4ea43c5 | 1028 | * @net: the applicable net namespace |
1da177e4 LT |
1029 | * @if_flags: IFF_* values |
1030 | * @mask: bitmask of bits in if_flags to check | |
1031 | * | |
1032 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 | 1033 | * is not found or a pointer to the device. Must be called inside |
6c555490 | 1034 | * rtnl_lock(), and result refcount is unchanged. |
1da177e4 LT |
1035 | */ |
1036 | ||
6c555490 WC |
1037 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
1038 | unsigned short mask) | |
1da177e4 | 1039 | { |
7562f876 | 1040 | struct net_device *dev, *ret; |
1da177e4 | 1041 | |
6c555490 WC |
1042 | ASSERT_RTNL(); |
1043 | ||
7562f876 | 1044 | ret = NULL; |
6c555490 | 1045 | for_each_netdev(net, dev) { |
1da177e4 | 1046 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 1047 | ret = dev; |
1da177e4 LT |
1048 | break; |
1049 | } | |
1050 | } | |
7562f876 | 1051 | return ret; |
1da177e4 | 1052 | } |
6c555490 | 1053 | EXPORT_SYMBOL(__dev_get_by_flags); |
1da177e4 LT |
1054 | |
1055 | /** | |
1056 | * dev_valid_name - check if name is okay for network device | |
1057 | * @name: name string | |
1058 | * | |
1059 | * Network device names need to be valid file names to | |
4250b75b | 1060 | * allow sysfs to work. We also disallow any kind of |
c7fa9d18 | 1061 | * whitespace. |
1da177e4 | 1062 | */ |
95f050bf | 1063 | bool dev_valid_name(const char *name) |
1da177e4 | 1064 | { |
c7fa9d18 | 1065 | if (*name == '\0') |
95f050bf | 1066 | return false; |
a9d48205 | 1067 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
95f050bf | 1068 | return false; |
c7fa9d18 | 1069 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 1070 | return false; |
c7fa9d18 DM |
1071 | |
1072 | while (*name) { | |
a4176a93 | 1073 | if (*name == '/' || *name == ':' || isspace(*name)) |
95f050bf | 1074 | return false; |
c7fa9d18 DM |
1075 | name++; |
1076 | } | |
95f050bf | 1077 | return true; |
1da177e4 | 1078 | } |
d1b19dff | 1079 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
1080 | |
1081 | /** | |
b267b179 EB |
1082 | * __dev_alloc_name - allocate a name for a device |
1083 | * @net: network namespace to allocate the device name in | |
1da177e4 | 1084 | * @name: name format string |
bd07063d | 1085 | * @res: result name string |
1da177e4 LT |
1086 | * |
1087 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
1088 | * id. It scans list of devices to build up a free map, then chooses |
1089 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1090 | * while allocating the name and adding the device in order to avoid | |
1091 | * duplicates. | |
1092 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1093 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
1094 | */ |
1095 | ||
bd07063d | 1096 | static int __dev_alloc_name(struct net *net, const char *name, char *res) |
1da177e4 LT |
1097 | { |
1098 | int i = 0; | |
1da177e4 LT |
1099 | const char *p; |
1100 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 1101 | unsigned long *inuse; |
1da177e4 | 1102 | struct net_device *d; |
bd07063d | 1103 | char buf[IFNAMSIZ]; |
1da177e4 | 1104 | |
9a810468 JK |
1105 | /* Verify the string as this thing may have come from the user. |
1106 | * There must be one "%d" and no other "%" characters. | |
1107 | */ | |
51f299dd | 1108 | p = strchr(name, '%'); |
9a810468 JK |
1109 | if (!p || p[1] != 'd' || strchr(p + 2, '%')) |
1110 | return -EINVAL; | |
1111 | ||
1112 | /* Use one page as a bit array of possible slots */ | |
1113 | inuse = bitmap_zalloc(max_netdevices, GFP_ATOMIC); | |
1114 | if (!inuse) | |
1115 | return -ENOMEM; | |
1116 | ||
1117 | for_each_netdev(net, d) { | |
1118 | struct netdev_name_node *name_node; | |
1119 | ||
1120 | netdev_for_each_altname(d, name_node) { | |
1121 | if (!sscanf(name_node->name, name, &i)) | |
1da177e4 LT |
1122 | continue; |
1123 | if (i < 0 || i >= max_netdevices) | |
1124 | continue; | |
1125 | ||
9a810468 | 1126 | /* avoid cases where sscanf is not exact inverse of printf */ |
b267b179 | 1127 | snprintf(buf, IFNAMSIZ, name, i); |
9a810468 | 1128 | if (!strncmp(buf, name_node->name, IFNAMSIZ)) |
25ee1660 | 1129 | __set_bit(i, inuse); |
1da177e4 | 1130 | } |
9a810468 JK |
1131 | if (!sscanf(d->name, name, &i)) |
1132 | continue; | |
1133 | if (i < 0 || i >= max_netdevices) | |
1134 | continue; | |
1135 | ||
1136 | /* avoid cases where sscanf is not exact inverse of printf */ | |
1137 | snprintf(buf, IFNAMSIZ, name, i); | |
1138 | if (!strncmp(buf, d->name, IFNAMSIZ)) | |
1139 | __set_bit(i, inuse); | |
1da177e4 LT |
1140 | } |
1141 | ||
9a810468 JK |
1142 | i = find_first_zero_bit(inuse, max_netdevices); |
1143 | bitmap_free(inuse); | |
7ad17b04 JK |
1144 | if (i == max_netdevices) |
1145 | return -ENFILE; | |
9a810468 | 1146 | |
674e3180 GP |
1147 | /* 'res' and 'name' could overlap, use 'buf' as an intermediate buffer */ |
1148 | strscpy(buf, name, IFNAMSIZ); | |
1149 | snprintf(res, IFNAMSIZ, buf, i); | |
7ad17b04 | 1150 | return i; |
1da177e4 LT |
1151 | } |
1152 | ||
556c755a | 1153 | /* Returns negative errno or allocated unit id (see __dev_alloc_name()) */ |
311cca40 | 1154 | static int dev_prep_valid_name(struct net *net, struct net_device *dev, |
556c755a JK |
1155 | const char *want_name, char *out_name, |
1156 | int dup_errno) | |
311cca40 | 1157 | { |
311cca40 JK |
1158 | if (!dev_valid_name(want_name)) |
1159 | return -EINVAL; | |
1160 | ||
ce4cfa23 | 1161 | if (strchr(want_name, '%')) |
556c755a | 1162 | return __dev_alloc_name(net, want_name, out_name); |
ce4cfa23 JK |
1163 | |
1164 | if (netdev_name_in_use(net, want_name)) | |
556c755a | 1165 | return -dup_errno; |
ce4cfa23 | 1166 | if (out_name != want_name) |
311cca40 | 1167 | strscpy(out_name, want_name, IFNAMSIZ); |
311cca40 JK |
1168 | return 0; |
1169 | } | |
1170 | ||
b267b179 EB |
1171 | /** |
1172 | * dev_alloc_name - allocate a name for a device | |
1173 | * @dev: device | |
1174 | * @name: name format string | |
1175 | * | |
1176 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1177 | * id. It scans list of devices to build up a free map, then chooses | |
1178 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1179 | * while allocating the name and adding the device in order to avoid | |
1180 | * duplicates. | |
1181 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1182 | * Returns the number of the unit assigned or a negative errno code. | |
1183 | */ | |
1184 | ||
1185 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1186 | { | |
556c755a | 1187 | return dev_prep_valid_name(dev_net(dev), dev, name, dev->name, ENFILE); |
b267b179 | 1188 | } |
d1b19dff | 1189 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1190 | |
bacb7e18 ED |
1191 | static int dev_get_valid_name(struct net *net, struct net_device *dev, |
1192 | const char *name) | |
828de4f6 | 1193 | { |
556c755a JK |
1194 | int ret; |
1195 | ||
1196 | ret = dev_prep_valid_name(net, dev, name, dev->name, EEXIST); | |
1197 | return ret < 0 ? ret : 0; | |
d9031024 | 1198 | } |
1da177e4 LT |
1199 | |
1200 | /** | |
1201 | * dev_change_name - change name of a device | |
1202 | * @dev: device | |
1203 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1204 | * | |
1205 | * Change name of a device, can pass format strings "eth%d". | |
1206 | * for wildcarding. | |
1207 | */ | |
cf04a4c7 | 1208 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1209 | { |
238fa362 | 1210 | unsigned char old_assign_type; |
fcc5a03a | 1211 | char oldname[IFNAMSIZ]; |
1da177e4 | 1212 | int err = 0; |
fcc5a03a | 1213 | int ret; |
881d966b | 1214 | struct net *net; |
1da177e4 LT |
1215 | |
1216 | ASSERT_RTNL(); | |
c346dca1 | 1217 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1218 | |
c346dca1 | 1219 | net = dev_net(dev); |
8065a779 | 1220 | |
11d6011c | 1221 | down_write(&devnet_rename_sem); |
c91f6df2 BH |
1222 | |
1223 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
11d6011c | 1224 | up_write(&devnet_rename_sem); |
c8d90dca | 1225 | return 0; |
c91f6df2 | 1226 | } |
c8d90dca | 1227 | |
fcc5a03a HX |
1228 | memcpy(oldname, dev->name, IFNAMSIZ); |
1229 | ||
0840556e | 1230 | write_seqlock(&netdev_rename_lock); |
828de4f6 | 1231 | err = dev_get_valid_name(net, dev, newname); |
0840556e KI |
1232 | write_sequnlock(&netdev_rename_lock); |
1233 | ||
c91f6df2 | 1234 | if (err < 0) { |
11d6011c | 1235 | up_write(&devnet_rename_sem); |
d9031024 | 1236 | return err; |
c91f6df2 | 1237 | } |
1da177e4 | 1238 | |
6fe82a39 | 1239 | if (oldname[0] && !strchr(oldname, '%')) |
bd039b5e AR |
1240 | netdev_info(dev, "renamed from %s%s\n", oldname, |
1241 | dev->flags & IFF_UP ? " (while UP)" : ""); | |
6fe82a39 | 1242 | |
238fa362 | 1243 | old_assign_type = dev->name_assign_type; |
1c07dbb0 | 1244 | WRITE_ONCE(dev->name_assign_type, NET_NAME_RENAMED); |
238fa362 | 1245 | |
fcc5a03a | 1246 | rollback: |
a1b3f594 EB |
1247 | ret = device_rename(&dev->dev, dev->name); |
1248 | if (ret) { | |
1249 | memcpy(dev->name, oldname, IFNAMSIZ); | |
1c07dbb0 | 1250 | WRITE_ONCE(dev->name_assign_type, old_assign_type); |
11d6011c | 1251 | up_write(&devnet_rename_sem); |
a1b3f594 | 1252 | return ret; |
dcc99773 | 1253 | } |
7f988eab | 1254 | |
11d6011c | 1255 | up_write(&devnet_rename_sem); |
c91f6df2 | 1256 | |
5bb025fa VF |
1257 | netdev_adjacent_rename_links(dev, oldname); |
1258 | ||
ff927412 | 1259 | netdev_name_node_del(dev->name_node); |
72c9528b | 1260 | |
4cd582ff | 1261 | synchronize_net(); |
72c9528b | 1262 | |
ff927412 | 1263 | netdev_name_node_add(net, dev->name_node); |
7f988eab | 1264 | |
056925ab | 1265 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1266 | ret = notifier_to_errno(ret); |
1267 | ||
1268 | if (ret) { | |
91e9c07b ED |
1269 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1270 | if (err >= 0) { | |
fcc5a03a | 1271 | err = ret; |
11d6011c | 1272 | down_write(&devnet_rename_sem); |
0840556e | 1273 | write_seqlock(&netdev_rename_lock); |
fcc5a03a | 1274 | memcpy(dev->name, oldname, IFNAMSIZ); |
0840556e | 1275 | write_sequnlock(&netdev_rename_lock); |
5bb025fa | 1276 | memcpy(oldname, newname, IFNAMSIZ); |
1c07dbb0 | 1277 | WRITE_ONCE(dev->name_assign_type, old_assign_type); |
238fa362 | 1278 | old_assign_type = NET_NAME_RENAMED; |
fcc5a03a | 1279 | goto rollback; |
91e9c07b | 1280 | } else { |
5b92be64 JB |
1281 | netdev_err(dev, "name change rollback failed: %d\n", |
1282 | ret); | |
fcc5a03a HX |
1283 | } |
1284 | } | |
1da177e4 LT |
1285 | |
1286 | return err; | |
1287 | } | |
1288 | ||
0b815a1a SH |
1289 | /** |
1290 | * dev_set_alias - change ifalias of a device | |
1291 | * @dev: device | |
1292 | * @alias: name up to IFALIASZ | |
f0db275a | 1293 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1294 | * |
1295 | * Set ifalias for a device, | |
1296 | */ | |
1297 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1298 | { | |
6c557001 | 1299 | struct dev_ifalias *new_alias = NULL; |
0b815a1a SH |
1300 | |
1301 | if (len >= IFALIASZ) | |
1302 | return -EINVAL; | |
1303 | ||
6c557001 FW |
1304 | if (len) { |
1305 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); | |
1306 | if (!new_alias) | |
1307 | return -ENOMEM; | |
1308 | ||
1309 | memcpy(new_alias->ifalias, alias, len); | |
1310 | new_alias->ifalias[len] = 0; | |
96ca4a2c OH |
1311 | } |
1312 | ||
6c557001 | 1313 | mutex_lock(&ifalias_mutex); |
e3f0d761 PM |
1314 | new_alias = rcu_replace_pointer(dev->ifalias, new_alias, |
1315 | mutex_is_locked(&ifalias_mutex)); | |
6c557001 FW |
1316 | mutex_unlock(&ifalias_mutex); |
1317 | ||
1318 | if (new_alias) | |
1319 | kfree_rcu(new_alias, rcuhead); | |
0b815a1a | 1320 | |
0b815a1a SH |
1321 | return len; |
1322 | } | |
0fe554a4 | 1323 | EXPORT_SYMBOL(dev_set_alias); |
0b815a1a | 1324 | |
6c557001 FW |
1325 | /** |
1326 | * dev_get_alias - get ifalias of a device | |
1327 | * @dev: device | |
20e88320 | 1328 | * @name: buffer to store name of ifalias |
6c557001 FW |
1329 | * @len: size of buffer |
1330 | * | |
1331 | * get ifalias for a device. Caller must make sure dev cannot go | |
1332 | * away, e.g. rcu read lock or own a reference count to device. | |
1333 | */ | |
1334 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) | |
1335 | { | |
1336 | const struct dev_ifalias *alias; | |
1337 | int ret = 0; | |
1338 | ||
1339 | rcu_read_lock(); | |
1340 | alias = rcu_dereference(dev->ifalias); | |
1341 | if (alias) | |
1342 | ret = snprintf(name, len, "%s", alias->ifalias); | |
1343 | rcu_read_unlock(); | |
1344 | ||
1345 | return ret; | |
1346 | } | |
0b815a1a | 1347 | |
d8a33ac4 | 1348 | /** |
3041a069 | 1349 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1350 | * @dev: device to cause notification |
1351 | * | |
1352 | * Called to indicate a device has changed features. | |
1353 | */ | |
1354 | void netdev_features_change(struct net_device *dev) | |
1355 | { | |
056925ab | 1356 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1357 | } |
1358 | EXPORT_SYMBOL(netdev_features_change); | |
1359 | ||
1da177e4 LT |
1360 | /** |
1361 | * netdev_state_change - device changes state | |
1362 | * @dev: device to cause notification | |
1363 | * | |
1364 | * Called to indicate a device has changed state. This function calls | |
1365 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1366 | * to the routing socket. | |
1367 | */ | |
1368 | void netdev_state_change(struct net_device *dev) | |
1369 | { | |
1370 | if (dev->flags & IFF_UP) { | |
51d0c047 DA |
1371 | struct netdev_notifier_change_info change_info = { |
1372 | .info.dev = dev, | |
1373 | }; | |
54951194 | 1374 | |
51d0c047 | 1375 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
54951194 | 1376 | &change_info.info); |
1d997f10 | 1377 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL, 0, NULL); |
1da177e4 LT |
1378 | } |
1379 | } | |
d1b19dff | 1380 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1381 | |
7061eb8c LP |
1382 | /** |
1383 | * __netdev_notify_peers - notify network peers about existence of @dev, | |
1384 | * to be called when rtnl lock is already held. | |
1385 | * @dev: network device | |
1386 | * | |
1387 | * Generate traffic such that interested network peers are aware of | |
1388 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1389 | * a device wants to inform the rest of the network about some sort of | |
1390 | * reconfiguration such as a failover event or virtual machine | |
1391 | * migration. | |
1392 | */ | |
1393 | void __netdev_notify_peers(struct net_device *dev) | |
1394 | { | |
1395 | ASSERT_RTNL(); | |
1396 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
1397 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); | |
1398 | } | |
1399 | EXPORT_SYMBOL(__netdev_notify_peers); | |
1400 | ||
ee89bab1 | 1401 | /** |
722c9a0c | 1402 | * netdev_notify_peers - notify network peers about existence of @dev |
1403 | * @dev: network device | |
ee89bab1 AW |
1404 | * |
1405 | * Generate traffic such that interested network peers are aware of | |
1406 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1407 | * a device wants to inform the rest of the network about some sort of | |
1408 | * reconfiguration such as a failover event or virtual machine | |
1409 | * migration. | |
1410 | */ | |
1411 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1412 | { |
ee89bab1 | 1413 | rtnl_lock(); |
7061eb8c | 1414 | __netdev_notify_peers(dev); |
ee89bab1 | 1415 | rtnl_unlock(); |
c1da4ac7 | 1416 | } |
ee89bab1 | 1417 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1418 | |
29863d41 WW |
1419 | static int napi_threaded_poll(void *data); |
1420 | ||
1421 | static int napi_kthread_create(struct napi_struct *n) | |
1422 | { | |
1423 | int err = 0; | |
1424 | ||
1425 | /* Create and wake up the kthread once to put it in | |
1426 | * TASK_INTERRUPTIBLE mode to avoid the blocked task | |
1427 | * warning and work with loadavg. | |
1428 | */ | |
1429 | n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d", | |
1430 | n->dev->name, n->napi_id); | |
1431 | if (IS_ERR(n->thread)) { | |
1432 | err = PTR_ERR(n->thread); | |
1433 | pr_err("kthread_run failed with err %d\n", err); | |
1434 | n->thread = NULL; | |
1435 | } | |
1436 | ||
1437 | return err; | |
1438 | } | |
1439 | ||
40c900aa | 1440 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
1da177e4 | 1441 | { |
d314774c | 1442 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1443 | int ret; |
1da177e4 | 1444 | |
e46b66bc | 1445 | ASSERT_RTNL(); |
d07b26f5 | 1446 | dev_addr_check(dev); |
e46b66bc | 1447 | |
bd869245 HK |
1448 | if (!netif_device_present(dev)) { |
1449 | /* may be detached because parent is runtime-suspended */ | |
1450 | if (dev->dev.parent) | |
1451 | pm_runtime_resume(dev->dev.parent); | |
1452 | if (!netif_device_present(dev)) | |
1453 | return -ENODEV; | |
1454 | } | |
1da177e4 | 1455 | |
ca99ca14 NH |
1456 | /* Block netpoll from trying to do any rx path servicing. |
1457 | * If we don't do this there is a chance ndo_poll_controller | |
1458 | * or ndo_poll may be running while we open the device | |
1459 | */ | |
66b5552f | 1460 | netpoll_poll_disable(dev); |
ca99ca14 | 1461 | |
40c900aa | 1462 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
3b8bcfd5 JB |
1463 | ret = notifier_to_errno(ret); |
1464 | if (ret) | |
1465 | return ret; | |
1466 | ||
1da177e4 | 1467 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1468 | |
d314774c SH |
1469 | if (ops->ndo_validate_addr) |
1470 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1471 | |
d314774c SH |
1472 | if (!ret && ops->ndo_open) |
1473 | ret = ops->ndo_open(dev); | |
1da177e4 | 1474 | |
66b5552f | 1475 | netpoll_poll_enable(dev); |
ca99ca14 | 1476 | |
bada339b JG |
1477 | if (ret) |
1478 | clear_bit(__LINK_STATE_START, &dev->state); | |
1479 | else { | |
1da177e4 | 1480 | dev->flags |= IFF_UP; |
4417da66 | 1481 | dev_set_rx_mode(dev); |
1da177e4 | 1482 | dev_activate(dev); |
7bf23575 | 1483 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1484 | } |
bada339b | 1485 | |
1da177e4 LT |
1486 | return ret; |
1487 | } | |
1488 | ||
1489 | /** | |
bd380811 | 1490 | * dev_open - prepare an interface for use. |
00f54e68 PM |
1491 | * @dev: device to open |
1492 | * @extack: netlink extended ack | |
1da177e4 | 1493 | * |
bd380811 PM |
1494 | * Takes a device from down to up state. The device's private open |
1495 | * function is invoked and then the multicast lists are loaded. Finally | |
1496 | * the device is moved into the up state and a %NETDEV_UP message is | |
1497 | * sent to the netdev notifier chain. | |
1498 | * | |
1499 | * Calling this function on an active interface is a nop. On a failure | |
1500 | * a negative errno code is returned. | |
1da177e4 | 1501 | */ |
00f54e68 | 1502 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
bd380811 PM |
1503 | { |
1504 | int ret; | |
1505 | ||
bd380811 PM |
1506 | if (dev->flags & IFF_UP) |
1507 | return 0; | |
1508 | ||
40c900aa | 1509 | ret = __dev_open(dev, extack); |
bd380811 PM |
1510 | if (ret < 0) |
1511 | return ret; | |
1512 | ||
1d997f10 | 1513 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL); |
bd380811 PM |
1514 | call_netdevice_notifiers(NETDEV_UP, dev); |
1515 | ||
1516 | return ret; | |
1517 | } | |
1518 | EXPORT_SYMBOL(dev_open); | |
1519 | ||
7051b88a | 1520 | static void __dev_close_many(struct list_head *head) |
1da177e4 | 1521 | { |
44345724 | 1522 | struct net_device *dev; |
e46b66bc | 1523 | |
bd380811 | 1524 | ASSERT_RTNL(); |
9d5010db DM |
1525 | might_sleep(); |
1526 | ||
5cde2829 | 1527 | list_for_each_entry(dev, head, close_list) { |
3f4df206 | 1528 | /* Temporarily disable netpoll until the interface is down */ |
66b5552f | 1529 | netpoll_poll_disable(dev); |
3f4df206 | 1530 | |
44345724 | 1531 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1532 | |
44345724 | 1533 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1534 | |
44345724 OP |
1535 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1536 | * can be even on different cpu. So just clear netif_running(). | |
1537 | * | |
1538 | * dev->stop() will invoke napi_disable() on all of it's | |
1539 | * napi_struct instances on this device. | |
1540 | */ | |
4e857c58 | 1541 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
44345724 | 1542 | } |
1da177e4 | 1543 | |
44345724 | 1544 | dev_deactivate_many(head); |
d8b2a4d2 | 1545 | |
5cde2829 | 1546 | list_for_each_entry(dev, head, close_list) { |
44345724 | 1547 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 1548 | |
44345724 OP |
1549 | /* |
1550 | * Call the device specific close. This cannot fail. | |
1551 | * Only if device is UP | |
1552 | * | |
1553 | * We allow it to be called even after a DETACH hot-plug | |
1554 | * event. | |
1555 | */ | |
1556 | if (ops->ndo_stop) | |
1557 | ops->ndo_stop(dev); | |
1558 | ||
44345724 | 1559 | dev->flags &= ~IFF_UP; |
66b5552f | 1560 | netpoll_poll_enable(dev); |
44345724 | 1561 | } |
44345724 OP |
1562 | } |
1563 | ||
7051b88a | 1564 | static void __dev_close(struct net_device *dev) |
44345724 OP |
1565 | { |
1566 | LIST_HEAD(single); | |
1567 | ||
5cde2829 | 1568 | list_add(&dev->close_list, &single); |
7051b88a | 1569 | __dev_close_many(&single); |
f87e6f47 | 1570 | list_del(&single); |
44345724 OP |
1571 | } |
1572 | ||
7051b88a | 1573 | void dev_close_many(struct list_head *head, bool unlink) |
44345724 OP |
1574 | { |
1575 | struct net_device *dev, *tmp; | |
1da177e4 | 1576 | |
5cde2829 EB |
1577 | /* Remove the devices that don't need to be closed */ |
1578 | list_for_each_entry_safe(dev, tmp, head, close_list) | |
44345724 | 1579 | if (!(dev->flags & IFF_UP)) |
5cde2829 | 1580 | list_del_init(&dev->close_list); |
44345724 OP |
1581 | |
1582 | __dev_close_many(head); | |
1da177e4 | 1583 | |
5cde2829 | 1584 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
1d997f10 | 1585 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL); |
44345724 | 1586 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
99c4a26a DM |
1587 | if (unlink) |
1588 | list_del_init(&dev->close_list); | |
44345724 | 1589 | } |
bd380811 | 1590 | } |
99c4a26a | 1591 | EXPORT_SYMBOL(dev_close_many); |
bd380811 PM |
1592 | |
1593 | /** | |
1594 | * dev_close - shutdown an interface. | |
1595 | * @dev: device to shutdown | |
1596 | * | |
1597 | * This function moves an active device into down state. A | |
1598 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1599 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1600 | * chain. | |
1601 | */ | |
7051b88a | 1602 | void dev_close(struct net_device *dev) |
bd380811 | 1603 | { |
e14a5993 ED |
1604 | if (dev->flags & IFF_UP) { |
1605 | LIST_HEAD(single); | |
1da177e4 | 1606 | |
5cde2829 | 1607 | list_add(&dev->close_list, &single); |
99c4a26a | 1608 | dev_close_many(&single, true); |
e14a5993 ED |
1609 | list_del(&single); |
1610 | } | |
1da177e4 | 1611 | } |
d1b19dff | 1612 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1613 | |
1614 | ||
0187bdfb BH |
1615 | /** |
1616 | * dev_disable_lro - disable Large Receive Offload on a device | |
1617 | * @dev: device | |
1618 | * | |
1619 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1620 | * called under RTNL. This is needed if received packets may be | |
1621 | * forwarded to another interface. | |
1622 | */ | |
1623 | void dev_disable_lro(struct net_device *dev) | |
1624 | { | |
fbe168ba MK |
1625 | struct net_device *lower_dev; |
1626 | struct list_head *iter; | |
529d0489 | 1627 | |
bc5787c6 MM |
1628 | dev->wanted_features &= ~NETIF_F_LRO; |
1629 | netdev_update_features(dev); | |
27660515 | 1630 | |
22d5969f MM |
1631 | if (unlikely(dev->features & NETIF_F_LRO)) |
1632 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
fbe168ba MK |
1633 | |
1634 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
1635 | dev_disable_lro(lower_dev); | |
0187bdfb BH |
1636 | } |
1637 | EXPORT_SYMBOL(dev_disable_lro); | |
1638 | ||
56f5aa77 MC |
1639 | /** |
1640 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device | |
1641 | * @dev: device | |
1642 | * | |
1643 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be | |
1644 | * called under RTNL. This is needed if Generic XDP is installed on | |
1645 | * the device. | |
1646 | */ | |
1647 | static void dev_disable_gro_hw(struct net_device *dev) | |
1648 | { | |
1649 | dev->wanted_features &= ~NETIF_F_GRO_HW; | |
1650 | netdev_update_features(dev); | |
1651 | ||
1652 | if (unlikely(dev->features & NETIF_F_GRO_HW)) | |
1653 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); | |
1654 | } | |
1655 | ||
ede2762d KT |
1656 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
1657 | { | |
1658 | #define N(val) \ | |
1659 | case NETDEV_##val: \ | |
1660 | return "NETDEV_" __stringify(val); | |
1661 | switch (cmd) { | |
1662 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) | |
1663 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) | |
1664 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) | |
02a68a47 JP |
1665 | N(POST_INIT) N(PRE_UNINIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) |
1666 | N(CHANGEUPPER) N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) | |
1667 | N(BONDING_INFO) N(PRECHANGEUPPER) N(CHANGELOWERSTATE) | |
1668 | N(UDP_TUNNEL_PUSH_INFO) N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) | |
9daae9bd GP |
1669 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
1670 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) | |
9309f97a PM |
1671 | N(PRE_CHANGEADDR) N(OFFLOAD_XSTATS_ENABLE) N(OFFLOAD_XSTATS_DISABLE) |
1672 | N(OFFLOAD_XSTATS_REPORT_USED) N(OFFLOAD_XSTATS_REPORT_DELTA) | |
5a178186 | 1673 | N(XDP_FEAT_CHANGE) |
3f5ecd8a | 1674 | } |
ede2762d KT |
1675 | #undef N |
1676 | return "UNKNOWN_NETDEV_EVENT"; | |
1677 | } | |
1678 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); | |
1679 | ||
351638e7 JP |
1680 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
1681 | struct net_device *dev) | |
1682 | { | |
51d0c047 DA |
1683 | struct netdev_notifier_info info = { |
1684 | .dev = dev, | |
1685 | }; | |
351638e7 | 1686 | |
351638e7 JP |
1687 | return nb->notifier_call(nb, val, &info); |
1688 | } | |
0187bdfb | 1689 | |
afa0df59 JP |
1690 | static int call_netdevice_register_notifiers(struct notifier_block *nb, |
1691 | struct net_device *dev) | |
1692 | { | |
1693 | int err; | |
1694 | ||
1695 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); | |
1696 | err = notifier_to_errno(err); | |
1697 | if (err) | |
1698 | return err; | |
1699 | ||
1700 | if (!(dev->flags & IFF_UP)) | |
1701 | return 0; | |
1702 | ||
1703 | call_netdevice_notifier(nb, NETDEV_UP, dev); | |
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | static void call_netdevice_unregister_notifiers(struct notifier_block *nb, | |
1708 | struct net_device *dev) | |
1709 | { | |
1710 | if (dev->flags & IFF_UP) { | |
1711 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, | |
1712 | dev); | |
1713 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
1714 | } | |
1715 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); | |
1716 | } | |
1717 | ||
1718 | static int call_netdevice_register_net_notifiers(struct notifier_block *nb, | |
1719 | struct net *net) | |
1720 | { | |
1721 | struct net_device *dev; | |
1722 | int err; | |
1723 | ||
1724 | for_each_netdev(net, dev) { | |
1725 | err = call_netdevice_register_notifiers(nb, dev); | |
1726 | if (err) | |
1727 | goto rollback; | |
1728 | } | |
1729 | return 0; | |
1730 | ||
1731 | rollback: | |
1732 | for_each_netdev_continue_reverse(net, dev) | |
1733 | call_netdevice_unregister_notifiers(nb, dev); | |
1734 | return err; | |
1735 | } | |
1736 | ||
1737 | static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb, | |
1738 | struct net *net) | |
1739 | { | |
1740 | struct net_device *dev; | |
1741 | ||
1742 | for_each_netdev(net, dev) | |
1743 | call_netdevice_unregister_notifiers(nb, dev); | |
1744 | } | |
1745 | ||
881d966b EB |
1746 | static int dev_boot_phase = 1; |
1747 | ||
1da177e4 | 1748 | /** |
722c9a0c | 1749 | * register_netdevice_notifier - register a network notifier block |
1750 | * @nb: notifier | |
1da177e4 | 1751 | * |
722c9a0c | 1752 | * Register a notifier to be called when network device events occur. |
1753 | * The notifier passed is linked into the kernel structures and must | |
1754 | * not be reused until it has been unregistered. A negative errno code | |
1755 | * is returned on a failure. | |
1da177e4 | 1756 | * |
722c9a0c | 1757 | * When registered all registration and up events are replayed |
1758 | * to the new notifier to allow device to have a race free | |
1759 | * view of the network device list. | |
1da177e4 LT |
1760 | */ |
1761 | ||
1762 | int register_netdevice_notifier(struct notifier_block *nb) | |
1763 | { | |
881d966b | 1764 | struct net *net; |
1da177e4 LT |
1765 | int err; |
1766 | ||
328fbe74 KT |
1767 | /* Close race with setup_net() and cleanup_net() */ |
1768 | down_write(&pernet_ops_rwsem); | |
1da177e4 | 1769 | rtnl_lock(); |
f07d5b94 | 1770 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1771 | if (err) |
1772 | goto unlock; | |
881d966b EB |
1773 | if (dev_boot_phase) |
1774 | goto unlock; | |
1775 | for_each_net(net) { | |
afa0df59 JP |
1776 | err = call_netdevice_register_net_notifiers(nb, net); |
1777 | if (err) | |
1778 | goto rollback; | |
1da177e4 | 1779 | } |
fcc5a03a HX |
1780 | |
1781 | unlock: | |
1da177e4 | 1782 | rtnl_unlock(); |
328fbe74 | 1783 | up_write(&pernet_ops_rwsem); |
1da177e4 | 1784 | return err; |
fcc5a03a HX |
1785 | |
1786 | rollback: | |
afa0df59 JP |
1787 | for_each_net_continue_reverse(net) |
1788 | call_netdevice_unregister_net_notifiers(nb, net); | |
c67625a1 PE |
1789 | |
1790 | raw_notifier_chain_unregister(&netdev_chain, nb); | |
fcc5a03a | 1791 | goto unlock; |
1da177e4 | 1792 | } |
d1b19dff | 1793 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1794 | |
1795 | /** | |
722c9a0c | 1796 | * unregister_netdevice_notifier - unregister a network notifier block |
1797 | * @nb: notifier | |
1da177e4 | 1798 | * |
722c9a0c | 1799 | * Unregister a notifier previously registered by |
1800 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1801 | * kernel structures and may then be reused. A negative errno code | |
1802 | * is returned on a failure. | |
7d3d43da | 1803 | * |
722c9a0c | 1804 | * After unregistering unregister and down device events are synthesized |
1805 | * for all devices on the device list to the removed notifier to remove | |
1806 | * the need for special case cleanup code. | |
1da177e4 LT |
1807 | */ |
1808 | ||
1809 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1810 | { | |
7d3d43da | 1811 | struct net *net; |
9f514950 HX |
1812 | int err; |
1813 | ||
328fbe74 KT |
1814 | /* Close race with setup_net() and cleanup_net() */ |
1815 | down_write(&pernet_ops_rwsem); | |
9f514950 | 1816 | rtnl_lock(); |
f07d5b94 | 1817 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1818 | if (err) |
1819 | goto unlock; | |
1820 | ||
48b3a137 JP |
1821 | for_each_net(net) |
1822 | call_netdevice_unregister_net_notifiers(nb, net); | |
1823 | ||
7d3d43da | 1824 | unlock: |
9f514950 | 1825 | rtnl_unlock(); |
328fbe74 | 1826 | up_write(&pernet_ops_rwsem); |
9f514950 | 1827 | return err; |
1da177e4 | 1828 | } |
d1b19dff | 1829 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 | 1830 | |
1f637703 JP |
1831 | static int __register_netdevice_notifier_net(struct net *net, |
1832 | struct notifier_block *nb, | |
1833 | bool ignore_call_fail) | |
1834 | { | |
1835 | int err; | |
1836 | ||
1837 | err = raw_notifier_chain_register(&net->netdev_chain, nb); | |
1838 | if (err) | |
1839 | return err; | |
1840 | if (dev_boot_phase) | |
1841 | return 0; | |
1842 | ||
1843 | err = call_netdevice_register_net_notifiers(nb, net); | |
1844 | if (err && !ignore_call_fail) | |
1845 | goto chain_unregister; | |
1846 | ||
1847 | return 0; | |
1848 | ||
1849 | chain_unregister: | |
1850 | raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1851 | return err; | |
1852 | } | |
1853 | ||
1854 | static int __unregister_netdevice_notifier_net(struct net *net, | |
1855 | struct notifier_block *nb) | |
1856 | { | |
1857 | int err; | |
1858 | ||
1859 | err = raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1860 | if (err) | |
1861 | return err; | |
1862 | ||
1863 | call_netdevice_unregister_net_notifiers(nb, net); | |
1864 | return 0; | |
1865 | } | |
1866 | ||
a30c7b42 JP |
1867 | /** |
1868 | * register_netdevice_notifier_net - register a per-netns network notifier block | |
1869 | * @net: network namespace | |
1870 | * @nb: notifier | |
1871 | * | |
1872 | * Register a notifier to be called when network device events occur. | |
1873 | * The notifier passed is linked into the kernel structures and must | |
1874 | * not be reused until it has been unregistered. A negative errno code | |
1875 | * is returned on a failure. | |
1876 | * | |
1877 | * When registered all registration and up events are replayed | |
1878 | * to the new notifier to allow device to have a race free | |
1879 | * view of the network device list. | |
1880 | */ | |
1881 | ||
1882 | int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb) | |
1883 | { | |
1884 | int err; | |
1885 | ||
1886 | rtnl_lock(); | |
1f637703 | 1887 | err = __register_netdevice_notifier_net(net, nb, false); |
a30c7b42 JP |
1888 | rtnl_unlock(); |
1889 | return err; | |
a30c7b42 JP |
1890 | } |
1891 | EXPORT_SYMBOL(register_netdevice_notifier_net); | |
1892 | ||
1893 | /** | |
1894 | * unregister_netdevice_notifier_net - unregister a per-netns | |
1895 | * network notifier block | |
1896 | * @net: network namespace | |
1897 | * @nb: notifier | |
1898 | * | |
1899 | * Unregister a notifier previously registered by | |
9054b41c | 1900 | * register_netdevice_notifier_net(). The notifier is unlinked from the |
a30c7b42 JP |
1901 | * kernel structures and may then be reused. A negative errno code |
1902 | * is returned on a failure. | |
1903 | * | |
1904 | * After unregistering unregister and down device events are synthesized | |
1905 | * for all devices on the device list to the removed notifier to remove | |
1906 | * the need for special case cleanup code. | |
1907 | */ | |
1908 | ||
1909 | int unregister_netdevice_notifier_net(struct net *net, | |
1910 | struct notifier_block *nb) | |
1911 | { | |
1912 | int err; | |
1913 | ||
1914 | rtnl_lock(); | |
1f637703 | 1915 | err = __unregister_netdevice_notifier_net(net, nb); |
a30c7b42 JP |
1916 | rtnl_unlock(); |
1917 | return err; | |
1918 | } | |
1919 | EXPORT_SYMBOL(unregister_netdevice_notifier_net); | |
a30c7b42 | 1920 | |
3e52fba0 JP |
1921 | static void __move_netdevice_notifier_net(struct net *src_net, |
1922 | struct net *dst_net, | |
1923 | struct notifier_block *nb) | |
1924 | { | |
1925 | __unregister_netdevice_notifier_net(src_net, nb); | |
1926 | __register_netdevice_notifier_net(dst_net, nb, true); | |
1927 | } | |
1928 | ||
93642e14 JP |
1929 | int register_netdevice_notifier_dev_net(struct net_device *dev, |
1930 | struct notifier_block *nb, | |
1931 | struct netdev_net_notifier *nn) | |
1932 | { | |
1933 | int err; | |
a30c7b42 | 1934 | |
93642e14 JP |
1935 | rtnl_lock(); |
1936 | err = __register_netdevice_notifier_net(dev_net(dev), nb, false); | |
1937 | if (!err) { | |
1938 | nn->nb = nb; | |
1939 | list_add(&nn->list, &dev->net_notifier_list); | |
1940 | } | |
a30c7b42 JP |
1941 | rtnl_unlock(); |
1942 | return err; | |
1943 | } | |
93642e14 JP |
1944 | EXPORT_SYMBOL(register_netdevice_notifier_dev_net); |
1945 | ||
1946 | int unregister_netdevice_notifier_dev_net(struct net_device *dev, | |
1947 | struct notifier_block *nb, | |
1948 | struct netdev_net_notifier *nn) | |
1949 | { | |
1950 | int err; | |
1951 | ||
1952 | rtnl_lock(); | |
1953 | list_del(&nn->list); | |
1954 | err = __unregister_netdevice_notifier_net(dev_net(dev), nb); | |
1955 | rtnl_unlock(); | |
1956 | return err; | |
1957 | } | |
1958 | EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net); | |
1959 | ||
1960 | static void move_netdevice_notifiers_dev_net(struct net_device *dev, | |
1961 | struct net *net) | |
1962 | { | |
1963 | struct netdev_net_notifier *nn; | |
1964 | ||
3e52fba0 JP |
1965 | list_for_each_entry(nn, &dev->net_notifier_list, list) |
1966 | __move_netdevice_notifier_net(dev_net(dev), net, nn->nb); | |
93642e14 | 1967 | } |
a30c7b42 | 1968 | |
351638e7 JP |
1969 | /** |
1970 | * call_netdevice_notifiers_info - call all network notifier blocks | |
1971 | * @val: value passed unmodified to notifier function | |
351638e7 JP |
1972 | * @info: notifier information data |
1973 | * | |
1974 | * Call all network notifier blocks. Parameters and return value | |
1975 | * are as for raw_notifier_call_chain(). | |
1976 | */ | |
1977 | ||
88c0a6b5 VO |
1978 | int call_netdevice_notifiers_info(unsigned long val, |
1979 | struct netdev_notifier_info *info) | |
351638e7 | 1980 | { |
a30c7b42 JP |
1981 | struct net *net = dev_net(info->dev); |
1982 | int ret; | |
1983 | ||
351638e7 | 1984 | ASSERT_RTNL(); |
a30c7b42 JP |
1985 | |
1986 | /* Run per-netns notifier block chain first, then run the global one. | |
1987 | * Hopefully, one day, the global one is going to be removed after | |
1988 | * all notifier block registrators get converted to be per-netns. | |
1989 | */ | |
1990 | ret = raw_notifier_call_chain(&net->netdev_chain, val, info); | |
1991 | if (ret & NOTIFY_STOP_MASK) | |
1992 | return ret; | |
351638e7 JP |
1993 | return raw_notifier_call_chain(&netdev_chain, val, info); |
1994 | } | |
351638e7 | 1995 | |
9309f97a PM |
1996 | /** |
1997 | * call_netdevice_notifiers_info_robust - call per-netns notifier blocks | |
1998 | * for and rollback on error | |
1999 | * @val_up: value passed unmodified to notifier function | |
2000 | * @val_down: value passed unmodified to the notifier function when | |
2001 | * recovering from an error on @val_up | |
2002 | * @info: notifier information data | |
2003 | * | |
2004 | * Call all per-netns network notifier blocks, but not notifier blocks on | |
2005 | * the global notifier chain. Parameters and return value are as for | |
2006 | * raw_notifier_call_chain_robust(). | |
2007 | */ | |
2008 | ||
2009 | static int | |
2010 | call_netdevice_notifiers_info_robust(unsigned long val_up, | |
2011 | unsigned long val_down, | |
2012 | struct netdev_notifier_info *info) | |
2013 | { | |
2014 | struct net *net = dev_net(info->dev); | |
2015 | ||
2016 | ASSERT_RTNL(); | |
2017 | ||
2018 | return raw_notifier_call_chain_robust(&net->netdev_chain, | |
2019 | val_up, val_down, info); | |
2020 | } | |
2021 | ||
26372605 PM |
2022 | static int call_netdevice_notifiers_extack(unsigned long val, |
2023 | struct net_device *dev, | |
2024 | struct netlink_ext_ack *extack) | |
2025 | { | |
2026 | struct netdev_notifier_info info = { | |
2027 | .dev = dev, | |
2028 | .extack = extack, | |
2029 | }; | |
2030 | ||
2031 | return call_netdevice_notifiers_info(val, &info); | |
2032 | } | |
2033 | ||
1da177e4 LT |
2034 | /** |
2035 | * call_netdevice_notifiers - call all network notifier blocks | |
2036 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 2037 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
2038 | * |
2039 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 2040 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
2041 | */ |
2042 | ||
ad7379d4 | 2043 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 2044 | { |
26372605 | 2045 | return call_netdevice_notifiers_extack(val, dev, NULL); |
1da177e4 | 2046 | } |
edf947f1 | 2047 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 2048 | |
af7d6cce SD |
2049 | /** |
2050 | * call_netdevice_notifiers_mtu - call all network notifier blocks | |
2051 | * @val: value passed unmodified to notifier function | |
2052 | * @dev: net_device pointer passed unmodified to notifier function | |
2053 | * @arg: additional u32 argument passed to the notifier function | |
2054 | * | |
2055 | * Call all network notifier blocks. Parameters and return value | |
2056 | * are as for raw_notifier_call_chain(). | |
2057 | */ | |
2058 | static int call_netdevice_notifiers_mtu(unsigned long val, | |
2059 | struct net_device *dev, u32 arg) | |
2060 | { | |
2061 | struct netdev_notifier_info_ext info = { | |
2062 | .info.dev = dev, | |
2063 | .ext.mtu = arg, | |
2064 | }; | |
2065 | ||
2066 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); | |
2067 | ||
2068 | return call_netdevice_notifiers_info(val, &info.info); | |
2069 | } | |
2070 | ||
1cf51900 | 2071 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 2072 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
4577139b DB |
2073 | |
2074 | void net_inc_ingress_queue(void) | |
2075 | { | |
aabf6772 | 2076 | static_branch_inc(&ingress_needed_key); |
4577139b DB |
2077 | } |
2078 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); | |
2079 | ||
2080 | void net_dec_ingress_queue(void) | |
2081 | { | |
aabf6772 | 2082 | static_branch_dec(&ingress_needed_key); |
4577139b DB |
2083 | } |
2084 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); | |
2085 | #endif | |
2086 | ||
1f211a1b | 2087 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 2088 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
1f211a1b DB |
2089 | |
2090 | void net_inc_egress_queue(void) | |
2091 | { | |
aabf6772 | 2092 | static_branch_inc(&egress_needed_key); |
1f211a1b DB |
2093 | } |
2094 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); | |
2095 | ||
2096 | void net_dec_egress_queue(void) | |
2097 | { | |
aabf6772 | 2098 | static_branch_dec(&egress_needed_key); |
1f211a1b DB |
2099 | } |
2100 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); | |
2101 | #endif | |
2102 | ||
047f340b AST |
2103 | #ifdef CONFIG_NET_CLS_ACT |
2104 | DEFINE_STATIC_KEY_FALSE(tcf_bypass_check_needed_key); | |
2105 | EXPORT_SYMBOL(tcf_bypass_check_needed_key); | |
2106 | #endif | |
2107 | ||
27942a15 MKL |
2108 | DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
2109 | EXPORT_SYMBOL(netstamp_needed_key); | |
e9666d10 | 2110 | #ifdef CONFIG_JUMP_LABEL |
b90e5794 | 2111 | static atomic_t netstamp_needed_deferred; |
13baa00a | 2112 | static atomic_t netstamp_wanted; |
5fa8bbda | 2113 | static void netstamp_clear(struct work_struct *work) |
1da177e4 | 2114 | { |
b90e5794 | 2115 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
13baa00a | 2116 | int wanted; |
b90e5794 | 2117 | |
13baa00a ED |
2118 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
2119 | if (wanted > 0) | |
39e83922 | 2120 | static_branch_enable(&netstamp_needed_key); |
13baa00a | 2121 | else |
39e83922 | 2122 | static_branch_disable(&netstamp_needed_key); |
5fa8bbda ED |
2123 | } |
2124 | static DECLARE_WORK(netstamp_work, netstamp_clear); | |
b90e5794 | 2125 | #endif |
5fa8bbda ED |
2126 | |
2127 | void net_enable_timestamp(void) | |
2128 | { | |
e9666d10 | 2129 | #ifdef CONFIG_JUMP_LABEL |
6af645a5 | 2130 | int wanted = atomic_read(&netstamp_wanted); |
13baa00a | 2131 | |
6af645a5 ED |
2132 | while (wanted > 0) { |
2133 | if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted + 1)) | |
13baa00a ED |
2134 | return; |
2135 | } | |
2136 | atomic_inc(&netstamp_needed_deferred); | |
2137 | schedule_work(&netstamp_work); | |
2138 | #else | |
39e83922 | 2139 | static_branch_inc(&netstamp_needed_key); |
13baa00a | 2140 | #endif |
1da177e4 | 2141 | } |
d1b19dff | 2142 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
2143 | |
2144 | void net_disable_timestamp(void) | |
2145 | { | |
e9666d10 | 2146 | #ifdef CONFIG_JUMP_LABEL |
6af645a5 | 2147 | int wanted = atomic_read(&netstamp_wanted); |
13baa00a | 2148 | |
6af645a5 ED |
2149 | while (wanted > 1) { |
2150 | if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted - 1)) | |
13baa00a ED |
2151 | return; |
2152 | } | |
2153 | atomic_dec(&netstamp_needed_deferred); | |
5fa8bbda ED |
2154 | schedule_work(&netstamp_work); |
2155 | #else | |
39e83922 | 2156 | static_branch_dec(&netstamp_needed_key); |
5fa8bbda | 2157 | #endif |
1da177e4 | 2158 | } |
d1b19dff | 2159 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 2160 | |
3b098e2d | 2161 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 2162 | { |
2456e855 | 2163 | skb->tstamp = 0; |
27942a15 | 2164 | skb->mono_delivery_time = 0; |
39e83922 | 2165 | if (static_branch_unlikely(&netstamp_needed_key)) |
d93376f5 | 2166 | skb->tstamp = ktime_get_real(); |
1da177e4 LT |
2167 | } |
2168 | ||
39e83922 DB |
2169 | #define net_timestamp_check(COND, SKB) \ |
2170 | if (static_branch_unlikely(&netstamp_needed_key)) { \ | |
2171 | if ((COND) && !(SKB)->tstamp) \ | |
d93376f5 | 2172 | (SKB)->tstamp = ktime_get_real(); \ |
39e83922 | 2173 | } \ |
3b098e2d | 2174 | |
f4b05d27 | 2175 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
79b569f0 | 2176 | { |
5f7d5728 | 2177 | return __is_skb_forwardable(dev, skb, true); |
79b569f0 | 2178 | } |
1ee481fb | 2179 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
79b569f0 | 2180 | |
5f7d5728 JDB |
2181 | static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb, |
2182 | bool check_mtu) | |
a0265d28 | 2183 | { |
5f7d5728 | 2184 | int ret = ____dev_forward_skb(dev, skb, check_mtu); |
a0265d28 | 2185 | |
4e3264d2 MKL |
2186 | if (likely(!ret)) { |
2187 | skb->protocol = eth_type_trans(skb, dev); | |
2188 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
2189 | } | |
a0265d28 | 2190 | |
4e3264d2 | 2191 | return ret; |
a0265d28 | 2192 | } |
5f7d5728 JDB |
2193 | |
2194 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2195 | { | |
2196 | return __dev_forward_skb2(dev, skb, true); | |
2197 | } | |
a0265d28 HX |
2198 | EXPORT_SYMBOL_GPL(__dev_forward_skb); |
2199 | ||
44540960 AB |
2200 | /** |
2201 | * dev_forward_skb - loopback an skb to another netif | |
2202 | * | |
2203 | * @dev: destination network device | |
2204 | * @skb: buffer to forward | |
2205 | * | |
2206 | * return values: | |
2207 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 2208 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
2209 | * |
2210 | * dev_forward_skb can be used for injecting an skb from the | |
2211 | * start_xmit function of one device into the receive queue | |
2212 | * of another device. | |
2213 | * | |
2214 | * The receiving device may be in another namespace, so | |
2215 | * we have to clear all information in the skb that could | |
2216 | * impact namespace isolation. | |
2217 | */ | |
2218 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2219 | { | |
a0265d28 | 2220 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
44540960 AB |
2221 | } |
2222 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
2223 | ||
5f7d5728 JDB |
2224 | int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb) |
2225 | { | |
2226 | return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb); | |
2227 | } | |
2228 | ||
71d9dec2 CG |
2229 | static inline int deliver_skb(struct sk_buff *skb, |
2230 | struct packet_type *pt_prev, | |
2231 | struct net_device *orig_dev) | |
2232 | { | |
1f8b977a | 2233 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
1080e512 | 2234 | return -ENOMEM; |
63354797 | 2235 | refcount_inc(&skb->users); |
71d9dec2 CG |
2236 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
2237 | } | |
2238 | ||
7866a621 SN |
2239 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
2240 | struct packet_type **pt, | |
fbcb2170 JP |
2241 | struct net_device *orig_dev, |
2242 | __be16 type, | |
7866a621 SN |
2243 | struct list_head *ptype_list) |
2244 | { | |
2245 | struct packet_type *ptype, *pt_prev = *pt; | |
2246 | ||
2247 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
2248 | if (ptype->type != type) | |
2249 | continue; | |
2250 | if (pt_prev) | |
fbcb2170 | 2251 | deliver_skb(skb, pt_prev, orig_dev); |
7866a621 SN |
2252 | pt_prev = ptype; |
2253 | } | |
2254 | *pt = pt_prev; | |
2255 | } | |
2256 | ||
c0de08d0 EL |
2257 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
2258 | { | |
a3d744e9 | 2259 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
2260 | return false; |
2261 | ||
2262 | if (ptype->id_match) | |
2263 | return ptype->id_match(ptype, skb->sk); | |
2264 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
2265 | return true; | |
2266 | ||
2267 | return false; | |
2268 | } | |
2269 | ||
9f9a742d MR |
2270 | /** |
2271 | * dev_nit_active - return true if any network interface taps are in use | |
2272 | * | |
2273 | * @dev: network device to check for the presence of taps | |
2274 | */ | |
2275 | bool dev_nit_active(struct net_device *dev) | |
2276 | { | |
0b91fa4b ED |
2277 | return !list_empty(&net_hotdata.ptype_all) || |
2278 | !list_empty(&dev->ptype_all); | |
9f9a742d MR |
2279 | } |
2280 | EXPORT_SYMBOL_GPL(dev_nit_active); | |
2281 | ||
1da177e4 LT |
2282 | /* |
2283 | * Support routine. Sends outgoing frames to any network | |
2284 | * taps currently in use. | |
2285 | */ | |
2286 | ||
74b20582 | 2287 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 | 2288 | { |
0b91fa4b ED |
2289 | struct list_head *ptype_list = &net_hotdata.ptype_all; |
2290 | struct packet_type *ptype, *pt_prev = NULL; | |
71d9dec2 | 2291 | struct sk_buff *skb2 = NULL; |
a61bbcf2 | 2292 | |
1da177e4 | 2293 | rcu_read_lock(); |
7866a621 SN |
2294 | again: |
2295 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
6ebfad33 | 2296 | if (READ_ONCE(ptype->ignore_outgoing)) |
fa788d98 VW |
2297 | continue; |
2298 | ||
1da177e4 LT |
2299 | /* Never send packets back to the socket |
2300 | * they originated from - MvS (miquels@drinkel.ow.org) | |
2301 | */ | |
7866a621 SN |
2302 | if (skb_loop_sk(ptype, skb)) |
2303 | continue; | |
71d9dec2 | 2304 | |
7866a621 SN |
2305 | if (pt_prev) { |
2306 | deliver_skb(skb2, pt_prev, skb->dev); | |
2307 | pt_prev = ptype; | |
2308 | continue; | |
2309 | } | |
1da177e4 | 2310 | |
7866a621 SN |
2311 | /* need to clone skb, done only once */ |
2312 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
2313 | if (!skb2) | |
2314 | goto out_unlock; | |
70978182 | 2315 | |
7866a621 | 2316 | net_timestamp_set(skb2); |
1da177e4 | 2317 | |
7866a621 SN |
2318 | /* skb->nh should be correctly |
2319 | * set by sender, so that the second statement is | |
2320 | * just protection against buggy protocols. | |
2321 | */ | |
2322 | skb_reset_mac_header(skb2); | |
2323 | ||
2324 | if (skb_network_header(skb2) < skb2->data || | |
2325 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { | |
2326 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", | |
2327 | ntohs(skb2->protocol), | |
2328 | dev->name); | |
2329 | skb_reset_network_header(skb2); | |
1da177e4 | 2330 | } |
7866a621 SN |
2331 | |
2332 | skb2->transport_header = skb2->network_header; | |
2333 | skb2->pkt_type = PACKET_OUTGOING; | |
2334 | pt_prev = ptype; | |
2335 | } | |
2336 | ||
0b91fa4b | 2337 | if (ptype_list == &net_hotdata.ptype_all) { |
7866a621 SN |
2338 | ptype_list = &dev->ptype_all; |
2339 | goto again; | |
1da177e4 | 2340 | } |
7866a621 | 2341 | out_unlock: |
581fe0ea WB |
2342 | if (pt_prev) { |
2343 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) | |
2344 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
2345 | else | |
2346 | kfree_skb(skb2); | |
2347 | } | |
1da177e4 LT |
2348 | rcu_read_unlock(); |
2349 | } | |
74b20582 | 2350 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
1da177e4 | 2351 | |
2c53040f BH |
2352 | /** |
2353 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
2354 | * @dev: Network device |
2355 | * @txq: number of queues available | |
2356 | * | |
2357 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
2358 | * valid. To resolve this verify the tc mapping remains valid and if | |
2359 | * not NULL the mapping. With no priorities mapping to this | |
2360 | * offset/count pair it will no longer be used. In the worst case TC0 | |
2361 | * is invalid nothing can be done so disable priority mappings. If is | |
2362 | * expected that drivers will fix this mapping if they can before | |
2363 | * calling netif_set_real_num_tx_queues. | |
2364 | */ | |
bb134d22 | 2365 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
2366 | { |
2367 | int i; | |
2368 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2369 | ||
2370 | /* If TC0 is invalidated disable TC mapping */ | |
2371 | if (tc->offset + tc->count > txq) { | |
5b92be64 | 2372 | netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
2373 | dev->num_tc = 0; |
2374 | return; | |
2375 | } | |
2376 | ||
2377 | /* Invalidated prio to tc mappings set to TC0 */ | |
2378 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
2379 | int q = netdev_get_prio_tc_map(dev, i); | |
2380 | ||
2381 | tc = &dev->tc_to_txq[q]; | |
2382 | if (tc->offset + tc->count > txq) { | |
5b92be64 JB |
2383 | 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", |
2384 | i, q); | |
4f57c087 JF |
2385 | netdev_set_prio_tc_map(dev, i, 0); |
2386 | } | |
2387 | } | |
2388 | } | |
2389 | ||
8d059b0f AD |
2390 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
2391 | { | |
2392 | if (dev->num_tc) { | |
2393 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2394 | int i; | |
2395 | ||
ffcfe25b | 2396 | /* walk through the TCs and see if it falls into any of them */ |
8d059b0f AD |
2397 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
2398 | if ((txq - tc->offset) < tc->count) | |
2399 | return i; | |
2400 | } | |
2401 | ||
ffcfe25b | 2402 | /* didn't find it, just return -1 to indicate no match */ |
8d059b0f AD |
2403 | return -1; |
2404 | } | |
2405 | ||
2406 | return 0; | |
2407 | } | |
8a5f2166 | 2408 | EXPORT_SYMBOL(netdev_txq_to_tc); |
8d059b0f | 2409 | |
537c00de | 2410 | #ifdef CONFIG_XPS |
5da9ace3 VO |
2411 | static struct static_key xps_needed __read_mostly; |
2412 | static struct static_key xps_rxqs_needed __read_mostly; | |
537c00de AD |
2413 | static DEFINE_MUTEX(xps_map_mutex); |
2414 | #define xmap_dereference(P) \ | |
2415 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
2416 | ||
6234f874 | 2417 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
2d05bf01 | 2418 | struct xps_dev_maps *old_maps, int tci, u16 index) |
537c00de | 2419 | { |
10cdc3f3 AD |
2420 | struct xps_map *map = NULL; |
2421 | int pos; | |
537c00de | 2422 | |
f080864a | 2423 | map = xmap_dereference(dev_maps->attr_map[tci]); |
6234f874 AD |
2424 | if (!map) |
2425 | return false; | |
537c00de | 2426 | |
6234f874 AD |
2427 | for (pos = map->len; pos--;) { |
2428 | if (map->queues[pos] != index) | |
2429 | continue; | |
2430 | ||
2431 | if (map->len > 1) { | |
2432 | map->queues[pos] = map->queues[--map->len]; | |
10cdc3f3 | 2433 | break; |
537c00de | 2434 | } |
6234f874 | 2435 | |
2d05bf01 AT |
2436 | if (old_maps) |
2437 | RCU_INIT_POINTER(old_maps->attr_map[tci], NULL); | |
80d19669 | 2438 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
6234f874 AD |
2439 | kfree_rcu(map, rcu); |
2440 | return false; | |
537c00de AD |
2441 | } |
2442 | ||
6234f874 | 2443 | return true; |
10cdc3f3 AD |
2444 | } |
2445 | ||
6234f874 AD |
2446 | static bool remove_xps_queue_cpu(struct net_device *dev, |
2447 | struct xps_dev_maps *dev_maps, | |
2448 | int cpu, u16 offset, u16 count) | |
2449 | { | |
255c04a8 | 2450 | int num_tc = dev_maps->num_tc; |
184c449f AD |
2451 | bool active = false; |
2452 | int tci; | |
6234f874 | 2453 | |
184c449f AD |
2454 | for (tci = cpu * num_tc; num_tc--; tci++) { |
2455 | int i, j; | |
2456 | ||
2457 | for (i = count, j = offset; i--; j++) { | |
2d05bf01 | 2458 | if (!remove_xps_queue(dev_maps, NULL, tci, j)) |
184c449f AD |
2459 | break; |
2460 | } | |
2461 | ||
2462 | active |= i < 0; | |
6234f874 AD |
2463 | } |
2464 | ||
184c449f | 2465 | return active; |
6234f874 AD |
2466 | } |
2467 | ||
867d0ad4 SD |
2468 | static void reset_xps_maps(struct net_device *dev, |
2469 | struct xps_dev_maps *dev_maps, | |
044ab86d | 2470 | enum xps_map_type type) |
867d0ad4 | 2471 | { |
867d0ad4 | 2472 | static_key_slow_dec_cpuslocked(&xps_needed); |
044ab86d AT |
2473 | if (type == XPS_RXQS) |
2474 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); | |
2475 | ||
2476 | RCU_INIT_POINTER(dev->xps_maps[type], NULL); | |
2477 | ||
867d0ad4 SD |
2478 | kfree_rcu(dev_maps, rcu); |
2479 | } | |
2480 | ||
044ab86d AT |
2481 | static void clean_xps_maps(struct net_device *dev, enum xps_map_type type, |
2482 | u16 offset, u16 count) | |
80d19669 | 2483 | { |
044ab86d | 2484 | struct xps_dev_maps *dev_maps; |
80d19669 AN |
2485 | bool active = false; |
2486 | int i, j; | |
2487 | ||
044ab86d AT |
2488 | dev_maps = xmap_dereference(dev->xps_maps[type]); |
2489 | if (!dev_maps) | |
2490 | return; | |
2491 | ||
6f36158e AT |
2492 | for (j = 0; j < dev_maps->nr_ids; j++) |
2493 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count); | |
867d0ad4 | 2494 | if (!active) |
044ab86d | 2495 | reset_xps_maps(dev, dev_maps, type); |
80d19669 | 2496 | |
044ab86d | 2497 | if (type == XPS_CPUS) { |
6f36158e | 2498 | for (i = offset + (count - 1); count--; i--) |
f28c020f | 2499 | netdev_queue_numa_node_write( |
6f36158e | 2500 | netdev_get_tx_queue(dev, i), NUMA_NO_NODE); |
80d19669 AN |
2501 | } |
2502 | } | |
2503 | ||
6234f874 AD |
2504 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
2505 | u16 count) | |
10cdc3f3 | 2506 | { |
04157469 AN |
2507 | if (!static_key_false(&xps_needed)) |
2508 | return; | |
10cdc3f3 | 2509 | |
4d99f660 | 2510 | cpus_read_lock(); |
04157469 | 2511 | mutex_lock(&xps_map_mutex); |
10cdc3f3 | 2512 | |
044ab86d AT |
2513 | if (static_key_false(&xps_rxqs_needed)) |
2514 | clean_xps_maps(dev, XPS_RXQS, offset, count); | |
80d19669 | 2515 | |
044ab86d | 2516 | clean_xps_maps(dev, XPS_CPUS, offset, count); |
024e9679 | 2517 | |
537c00de | 2518 | mutex_unlock(&xps_map_mutex); |
4d99f660 | 2519 | cpus_read_unlock(); |
537c00de AD |
2520 | } |
2521 | ||
6234f874 AD |
2522 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
2523 | { | |
2524 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); | |
2525 | } | |
2526 | ||
80d19669 AN |
2527 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
2528 | u16 index, bool is_rxqs_map) | |
01c5f864 AD |
2529 | { |
2530 | struct xps_map *new_map; | |
2531 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
2532 | int i, pos; | |
2533 | ||
2534 | for (pos = 0; map && pos < map->len; pos++) { | |
2535 | if (map->queues[pos] != index) | |
2536 | continue; | |
2537 | return map; | |
2538 | } | |
2539 | ||
80d19669 | 2540 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
01c5f864 AD |
2541 | if (map) { |
2542 | if (pos < map->alloc_len) | |
2543 | return map; | |
2544 | ||
2545 | alloc_len = map->alloc_len * 2; | |
2546 | } | |
2547 | ||
80d19669 AN |
2548 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
2549 | * map | |
2550 | */ | |
2551 | if (is_rxqs_map) | |
2552 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); | |
2553 | else | |
2554 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
2555 | cpu_to_node(attr_index)); | |
01c5f864 AD |
2556 | if (!new_map) |
2557 | return NULL; | |
2558 | ||
2559 | for (i = 0; i < pos; i++) | |
2560 | new_map->queues[i] = map->queues[i]; | |
2561 | new_map->alloc_len = alloc_len; | |
2562 | new_map->len = pos; | |
2563 | ||
2564 | return new_map; | |
2565 | } | |
2566 | ||
402fbb99 AT |
2567 | /* Copy xps maps at a given index */ |
2568 | static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps, | |
2569 | struct xps_dev_maps *new_dev_maps, int index, | |
2570 | int tc, bool skip_tc) | |
2571 | { | |
2572 | int i, tci = index * dev_maps->num_tc; | |
2573 | struct xps_map *map; | |
2574 | ||
2575 | /* copy maps belonging to foreign traffic classes */ | |
2576 | for (i = 0; i < dev_maps->num_tc; i++, tci++) { | |
2577 | if (i == tc && skip_tc) | |
2578 | continue; | |
2579 | ||
2580 | /* fill in the new device map from the old device map */ | |
2581 | map = xmap_dereference(dev_maps->attr_map[tci]); | |
2582 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
2583 | } | |
2584 | } | |
2585 | ||
4d99f660 | 2586 | /* Must be called under cpus_read_lock */ |
80d19669 | 2587 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
044ab86d | 2588 | u16 index, enum xps_map_type type) |
537c00de | 2589 | { |
2d05bf01 | 2590 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL; |
6f36158e | 2591 | const unsigned long *online_mask = NULL; |
255c04a8 | 2592 | bool active = false, copy = false; |
80d19669 | 2593 | int i, j, tci, numa_node_id = -2; |
184c449f | 2594 | int maps_sz, num_tc = 1, tc = 0; |
537c00de | 2595 | struct xps_map *map, *new_map; |
80d19669 | 2596 | unsigned int nr_ids; |
537c00de | 2597 | |
5dd0dfd5 NC |
2598 | WARN_ON_ONCE(index >= dev->num_tx_queues); |
2599 | ||
184c449f | 2600 | if (dev->num_tc) { |
ffcfe25b | 2601 | /* Do not allow XPS on subordinate device directly */ |
184c449f | 2602 | num_tc = dev->num_tc; |
ffcfe25b AD |
2603 | if (num_tc < 0) |
2604 | return -EINVAL; | |
2605 | ||
2606 | /* If queue belongs to subordinate dev use its map */ | |
2607 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
2608 | ||
184c449f AD |
2609 | tc = netdev_txq_to_tc(dev, index); |
2610 | if (tc < 0) | |
2611 | return -EINVAL; | |
2612 | } | |
2613 | ||
537c00de | 2614 | mutex_lock(&xps_map_mutex); |
044ab86d AT |
2615 | |
2616 | dev_maps = xmap_dereference(dev->xps_maps[type]); | |
2617 | if (type == XPS_RXQS) { | |
80d19669 | 2618 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); |
80d19669 AN |
2619 | nr_ids = dev->num_rx_queues; |
2620 | } else { | |
2621 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); | |
6f36158e | 2622 | if (num_possible_cpus() > 1) |
80d19669 | 2623 | online_mask = cpumask_bits(cpu_online_mask); |
80d19669 AN |
2624 | nr_ids = nr_cpu_ids; |
2625 | } | |
537c00de | 2626 | |
80d19669 AN |
2627 | if (maps_sz < L1_CACHE_BYTES) |
2628 | maps_sz = L1_CACHE_BYTES; | |
537c00de | 2629 | |
255c04a8 | 2630 | /* The old dev_maps could be larger or smaller than the one we're |
5478fcd0 AT |
2631 | * setting up now, as dev->num_tc or nr_ids could have been updated in |
2632 | * between. We could try to be smart, but let's be safe instead and only | |
2633 | * copy foreign traffic classes if the two map sizes match. | |
255c04a8 | 2634 | */ |
5478fcd0 AT |
2635 | if (dev_maps && |
2636 | dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids) | |
255c04a8 AT |
2637 | copy = true; |
2638 | ||
01c5f864 | 2639 | /* allocate memory for queue storage */ |
80d19669 AN |
2640 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
2641 | j < nr_ids;) { | |
2bb60cb9 | 2642 | if (!new_dev_maps) { |
255c04a8 AT |
2643 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); |
2644 | if (!new_dev_maps) { | |
2645 | mutex_unlock(&xps_map_mutex); | |
2646 | return -ENOMEM; | |
2647 | } | |
2648 | ||
5478fcd0 | 2649 | new_dev_maps->nr_ids = nr_ids; |
255c04a8 | 2650 | new_dev_maps->num_tc = num_tc; |
2bb60cb9 | 2651 | } |
01c5f864 | 2652 | |
80d19669 | 2653 | tci = j * num_tc + tc; |
255c04a8 | 2654 | map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL; |
01c5f864 | 2655 | |
044ab86d | 2656 | map = expand_xps_map(map, j, index, type == XPS_RXQS); |
01c5f864 AD |
2657 | if (!map) |
2658 | goto error; | |
2659 | ||
80d19669 | 2660 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
01c5f864 AD |
2661 | } |
2662 | ||
2663 | if (!new_dev_maps) | |
2664 | goto out_no_new_maps; | |
2665 | ||
867d0ad4 SD |
2666 | if (!dev_maps) { |
2667 | /* Increment static keys at most once per type */ | |
2668 | static_key_slow_inc_cpuslocked(&xps_needed); | |
044ab86d | 2669 | if (type == XPS_RXQS) |
867d0ad4 SD |
2670 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); |
2671 | } | |
04157469 | 2672 | |
6f36158e | 2673 | for (j = 0; j < nr_ids; j++) { |
402fbb99 | 2674 | bool skip_tc = false; |
184c449f | 2675 | |
80d19669 | 2676 | tci = j * num_tc + tc; |
80d19669 AN |
2677 | if (netif_attr_test_mask(j, mask, nr_ids) && |
2678 | netif_attr_test_online(j, online_mask, nr_ids)) { | |
2679 | /* add tx-queue to CPU/rx-queue maps */ | |
01c5f864 AD |
2680 | int pos = 0; |
2681 | ||
402fbb99 AT |
2682 | skip_tc = true; |
2683 | ||
80d19669 | 2684 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
01c5f864 AD |
2685 | while ((pos < map->len) && (map->queues[pos] != index)) |
2686 | pos++; | |
2687 | ||
2688 | if (pos == map->len) | |
2689 | map->queues[map->len++] = index; | |
537c00de | 2690 | #ifdef CONFIG_NUMA |
044ab86d | 2691 | if (type == XPS_CPUS) { |
80d19669 AN |
2692 | if (numa_node_id == -2) |
2693 | numa_node_id = cpu_to_node(j); | |
2694 | else if (numa_node_id != cpu_to_node(j)) | |
2695 | numa_node_id = -1; | |
2696 | } | |
537c00de | 2697 | #endif |
537c00de | 2698 | } |
01c5f864 | 2699 | |
402fbb99 AT |
2700 | if (copy) |
2701 | xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc, | |
2702 | skip_tc); | |
537c00de AD |
2703 | } |
2704 | ||
044ab86d | 2705 | rcu_assign_pointer(dev->xps_maps[type], new_dev_maps); |
01c5f864 | 2706 | |
537c00de | 2707 | /* Cleanup old maps */ |
184c449f AD |
2708 | if (!dev_maps) |
2709 | goto out_no_old_maps; | |
2710 | ||
6f36158e | 2711 | for (j = 0; j < dev_maps->nr_ids; j++) { |
255c04a8 | 2712 | for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) { |
80d19669 | 2713 | map = xmap_dereference(dev_maps->attr_map[tci]); |
255c04a8 AT |
2714 | if (!map) |
2715 | continue; | |
2716 | ||
2717 | if (copy) { | |
2718 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
2719 | if (map == new_map) | |
2720 | continue; | |
2721 | } | |
2722 | ||
75b2758a | 2723 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
255c04a8 | 2724 | kfree_rcu(map, rcu); |
01c5f864 | 2725 | } |
537c00de AD |
2726 | } |
2727 | ||
2d05bf01 | 2728 | old_dev_maps = dev_maps; |
184c449f AD |
2729 | |
2730 | out_no_old_maps: | |
01c5f864 AD |
2731 | dev_maps = new_dev_maps; |
2732 | active = true; | |
537c00de | 2733 | |
01c5f864 | 2734 | out_no_new_maps: |
044ab86d | 2735 | if (type == XPS_CPUS) |
80d19669 AN |
2736 | /* update Tx queue numa node */ |
2737 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), | |
2738 | (numa_node_id >= 0) ? | |
2739 | numa_node_id : NUMA_NO_NODE); | |
537c00de | 2740 | |
01c5f864 AD |
2741 | if (!dev_maps) |
2742 | goto out_no_maps; | |
2743 | ||
80d19669 | 2744 | /* removes tx-queue from unused CPUs/rx-queues */ |
6f36158e | 2745 | for (j = 0; j < dev_maps->nr_ids; j++) { |
132f743b AT |
2746 | tci = j * dev_maps->num_tc; |
2747 | ||
2748 | for (i = 0; i < dev_maps->num_tc; i++, tci++) { | |
2749 | if (i == tc && | |
2750 | netif_attr_test_mask(j, mask, dev_maps->nr_ids) && | |
2751 | netif_attr_test_online(j, online_mask, dev_maps->nr_ids)) | |
2752 | continue; | |
2753 | ||
2d05bf01 AT |
2754 | active |= remove_xps_queue(dev_maps, |
2755 | copy ? old_dev_maps : NULL, | |
2756 | tci, index); | |
132f743b | 2757 | } |
01c5f864 AD |
2758 | } |
2759 | ||
2d05bf01 AT |
2760 | if (old_dev_maps) |
2761 | kfree_rcu(old_dev_maps, rcu); | |
2762 | ||
01c5f864 | 2763 | /* free map if not active */ |
867d0ad4 | 2764 | if (!active) |
044ab86d | 2765 | reset_xps_maps(dev, dev_maps, type); |
01c5f864 AD |
2766 | |
2767 | out_no_maps: | |
537c00de AD |
2768 | mutex_unlock(&xps_map_mutex); |
2769 | ||
2770 | return 0; | |
2771 | error: | |
01c5f864 | 2772 | /* remove any maps that we added */ |
6f36158e | 2773 | for (j = 0; j < nr_ids; j++) { |
80d19669 AN |
2774 | for (i = num_tc, tci = j * num_tc; i--; tci++) { |
2775 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
255c04a8 | 2776 | map = copy ? |
80d19669 | 2777 | xmap_dereference(dev_maps->attr_map[tci]) : |
184c449f AD |
2778 | NULL; |
2779 | if (new_map && new_map != map) | |
2780 | kfree(new_map); | |
2781 | } | |
01c5f864 AD |
2782 | } |
2783 | ||
537c00de AD |
2784 | mutex_unlock(&xps_map_mutex); |
2785 | ||
537c00de AD |
2786 | kfree(new_dev_maps); |
2787 | return -ENOMEM; | |
2788 | } | |
4d99f660 | 2789 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
80d19669 AN |
2790 | |
2791 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, | |
2792 | u16 index) | |
2793 | { | |
4d99f660 AV |
2794 | int ret; |
2795 | ||
2796 | cpus_read_lock(); | |
044ab86d | 2797 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS); |
4d99f660 AV |
2798 | cpus_read_unlock(); |
2799 | ||
2800 | return ret; | |
80d19669 | 2801 | } |
537c00de AD |
2802 | EXPORT_SYMBOL(netif_set_xps_queue); |
2803 | ||
2804 | #endif | |
ffcfe25b AD |
2805 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
2806 | { | |
2807 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2808 | ||
2809 | /* Unbind any subordinate channels */ | |
2810 | while (txq-- != &dev->_tx[0]) { | |
2811 | if (txq->sb_dev) | |
2812 | netdev_unbind_sb_channel(dev, txq->sb_dev); | |
2813 | } | |
2814 | } | |
2815 | ||
9cf1f6a8 AD |
2816 | void netdev_reset_tc(struct net_device *dev) |
2817 | { | |
6234f874 AD |
2818 | #ifdef CONFIG_XPS |
2819 | netif_reset_xps_queues_gt(dev, 0); | |
2820 | #endif | |
ffcfe25b AD |
2821 | netdev_unbind_all_sb_channels(dev); |
2822 | ||
2823 | /* Reset TC configuration of device */ | |
9cf1f6a8 AD |
2824 | dev->num_tc = 0; |
2825 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); | |
2826 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); | |
2827 | } | |
2828 | EXPORT_SYMBOL(netdev_reset_tc); | |
2829 | ||
2830 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) | |
2831 | { | |
2832 | if (tc >= dev->num_tc) | |
2833 | return -EINVAL; | |
2834 | ||
6234f874 AD |
2835 | #ifdef CONFIG_XPS |
2836 | netif_reset_xps_queues(dev, offset, count); | |
2837 | #endif | |
9cf1f6a8 AD |
2838 | dev->tc_to_txq[tc].count = count; |
2839 | dev->tc_to_txq[tc].offset = offset; | |
2840 | return 0; | |
2841 | } | |
2842 | EXPORT_SYMBOL(netdev_set_tc_queue); | |
2843 | ||
2844 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) | |
2845 | { | |
2846 | if (num_tc > TC_MAX_QUEUE) | |
2847 | return -EINVAL; | |
2848 | ||
6234f874 AD |
2849 | #ifdef CONFIG_XPS |
2850 | netif_reset_xps_queues_gt(dev, 0); | |
2851 | #endif | |
ffcfe25b AD |
2852 | netdev_unbind_all_sb_channels(dev); |
2853 | ||
9cf1f6a8 AD |
2854 | dev->num_tc = num_tc; |
2855 | return 0; | |
2856 | } | |
2857 | EXPORT_SYMBOL(netdev_set_num_tc); | |
2858 | ||
ffcfe25b AD |
2859 | void netdev_unbind_sb_channel(struct net_device *dev, |
2860 | struct net_device *sb_dev) | |
2861 | { | |
2862 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2863 | ||
2864 | #ifdef CONFIG_XPS | |
2865 | netif_reset_xps_queues_gt(sb_dev, 0); | |
2866 | #endif | |
2867 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); | |
2868 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); | |
2869 | ||
2870 | while (txq-- != &dev->_tx[0]) { | |
2871 | if (txq->sb_dev == sb_dev) | |
2872 | txq->sb_dev = NULL; | |
2873 | } | |
2874 | } | |
2875 | EXPORT_SYMBOL(netdev_unbind_sb_channel); | |
2876 | ||
2877 | int netdev_bind_sb_channel_queue(struct net_device *dev, | |
2878 | struct net_device *sb_dev, | |
2879 | u8 tc, u16 count, u16 offset) | |
2880 | { | |
2881 | /* Make certain the sb_dev and dev are already configured */ | |
2882 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) | |
2883 | return -EINVAL; | |
2884 | ||
2885 | /* We cannot hand out queues we don't have */ | |
2886 | if ((offset + count) > dev->real_num_tx_queues) | |
2887 | return -EINVAL; | |
2888 | ||
2889 | /* Record the mapping */ | |
2890 | sb_dev->tc_to_txq[tc].count = count; | |
2891 | sb_dev->tc_to_txq[tc].offset = offset; | |
2892 | ||
2893 | /* Provide a way for Tx queue to find the tc_to_txq map or | |
2894 | * XPS map for itself. | |
2895 | */ | |
2896 | while (count--) | |
2897 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; | |
2898 | ||
2899 | return 0; | |
2900 | } | |
2901 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); | |
2902 | ||
2903 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) | |
2904 | { | |
2905 | /* Do not use a multiqueue device to represent a subordinate channel */ | |
2906 | if (netif_is_multiqueue(dev)) | |
2907 | return -ENODEV; | |
2908 | ||
2909 | /* We allow channels 1 - 32767 to be used for subordinate channels. | |
2910 | * Channel 0 is meant to be "native" mode and used only to represent | |
2911 | * the main root device. We allow writing 0 to reset the device back | |
2912 | * to normal mode after being used as a subordinate channel. | |
2913 | */ | |
2914 | if (channel > S16_MAX) | |
2915 | return -EINVAL; | |
2916 | ||
2917 | dev->num_tc = -channel; | |
2918 | ||
2919 | return 0; | |
2920 | } | |
2921 | EXPORT_SYMBOL(netdev_set_sb_channel); | |
2922 | ||
f0796d5c JF |
2923 | /* |
2924 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
3a053b1a | 2925 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
f0796d5c | 2926 | */ |
e6484930 | 2927 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2928 | { |
ac5b7019 | 2929 | bool disabling; |
1d24eb48 TH |
2930 | int rc; |
2931 | ||
ac5b7019 JK |
2932 | disabling = txq < dev->real_num_tx_queues; |
2933 | ||
e6484930 TH |
2934 | if (txq < 1 || txq > dev->num_tx_queues) |
2935 | return -EINVAL; | |
f0796d5c | 2936 | |
5c56580b BH |
2937 | if (dev->reg_state == NETREG_REGISTERED || |
2938 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2939 | ASSERT_RTNL(); |
2940 | ||
1d24eb48 TH |
2941 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2942 | txq); | |
bf264145 TH |
2943 | if (rc) |
2944 | return rc; | |
2945 | ||
4f57c087 JF |
2946 | if (dev->num_tc) |
2947 | netif_setup_tc(dev, txq); | |
2948 | ||
1e080f17 JK |
2949 | dev_qdisc_change_real_num_tx(dev, txq); |
2950 | ||
ac5b7019 JK |
2951 | dev->real_num_tx_queues = txq; |
2952 | ||
2953 | if (disabling) { | |
2954 | synchronize_net(); | |
e6484930 | 2955 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2956 | #ifdef CONFIG_XPS |
2957 | netif_reset_xps_queues_gt(dev, txq); | |
2958 | #endif | |
2959 | } | |
ac5b7019 JK |
2960 | } else { |
2961 | dev->real_num_tx_queues = txq; | |
f0796d5c | 2962 | } |
e6484930 | 2963 | |
e6484930 | 2964 | return 0; |
f0796d5c JF |
2965 | } |
2966 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2967 | |
a953be53 | 2968 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
2969 | /** |
2970 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2971 | * @dev: Network device | |
2972 | * @rxq: Actual number of RX queues | |
2973 | * | |
2974 | * This must be called either with the rtnl_lock held or before | |
2975 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2976 | * negative error code. If called before registration, it always |
2977 | * succeeds. | |
62fe0b40 BH |
2978 | */ |
2979 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2980 | { | |
2981 | int rc; | |
2982 | ||
bd25fa7b TH |
2983 | if (rxq < 1 || rxq > dev->num_rx_queues) |
2984 | return -EINVAL; | |
2985 | ||
62fe0b40 BH |
2986 | if (dev->reg_state == NETREG_REGISTERED) { |
2987 | ASSERT_RTNL(); | |
2988 | ||
62fe0b40 BH |
2989 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
2990 | rxq); | |
2991 | if (rc) | |
2992 | return rc; | |
62fe0b40 BH |
2993 | } |
2994 | ||
2995 | dev->real_num_rx_queues = rxq; | |
2996 | return 0; | |
2997 | } | |
2998 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
2999 | #endif | |
3000 | ||
271e5b7d JK |
3001 | /** |
3002 | * netif_set_real_num_queues - set actual number of RX and TX queues used | |
3003 | * @dev: Network device | |
3004 | * @txq: Actual number of TX queues | |
3005 | * @rxq: Actual number of RX queues | |
3006 | * | |
3007 | * Set the real number of both TX and RX queues. | |
3008 | * Does nothing if the number of queues is already correct. | |
3009 | */ | |
3010 | int netif_set_real_num_queues(struct net_device *dev, | |
3011 | unsigned int txq, unsigned int rxq) | |
3012 | { | |
3013 | unsigned int old_rxq = dev->real_num_rx_queues; | |
3014 | int err; | |
3015 | ||
3016 | if (txq < 1 || txq > dev->num_tx_queues || | |
3017 | rxq < 1 || rxq > dev->num_rx_queues) | |
3018 | return -EINVAL; | |
3019 | ||
3020 | /* Start from increases, so the error path only does decreases - | |
3021 | * decreases can't fail. | |
3022 | */ | |
3023 | if (rxq > dev->real_num_rx_queues) { | |
3024 | err = netif_set_real_num_rx_queues(dev, rxq); | |
3025 | if (err) | |
3026 | return err; | |
3027 | } | |
3028 | if (txq > dev->real_num_tx_queues) { | |
3029 | err = netif_set_real_num_tx_queues(dev, txq); | |
3030 | if (err) | |
3031 | goto undo_rx; | |
3032 | } | |
3033 | if (rxq < dev->real_num_rx_queues) | |
3034 | WARN_ON(netif_set_real_num_rx_queues(dev, rxq)); | |
3035 | if (txq < dev->real_num_tx_queues) | |
3036 | WARN_ON(netif_set_real_num_tx_queues(dev, txq)); | |
3037 | ||
3038 | return 0; | |
3039 | undo_rx: | |
3040 | WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq)); | |
3041 | return err; | |
3042 | } | |
3043 | EXPORT_SYMBOL(netif_set_real_num_queues); | |
3044 | ||
14d7b812 JK |
3045 | /** |
3046 | * netif_set_tso_max_size() - set the max size of TSO frames supported | |
3047 | * @dev: netdev to update | |
3048 | * @size: max skb->len of a TSO frame | |
3049 | * | |
3050 | * Set the limit on the size of TSO super-frames the device can handle. | |
7c4e983c AD |
3051 | * Unless explicitly set the stack will assume the value of |
3052 | * %GSO_LEGACY_MAX_SIZE. | |
14d7b812 JK |
3053 | */ |
3054 | void netif_set_tso_max_size(struct net_device *dev, unsigned int size) | |
3055 | { | |
7c4e983c | 3056 | dev->tso_max_size = min(GSO_MAX_SIZE, size); |
14d7b812 JK |
3057 | if (size < READ_ONCE(dev->gso_max_size)) |
3058 | netif_set_gso_max_size(dev, size); | |
9eefedd5 XL |
3059 | if (size < READ_ONCE(dev->gso_ipv4_max_size)) |
3060 | netif_set_gso_ipv4_max_size(dev, size); | |
14d7b812 JK |
3061 | } |
3062 | EXPORT_SYMBOL(netif_set_tso_max_size); | |
3063 | ||
3064 | /** | |
3065 | * netif_set_tso_max_segs() - set the max number of segs supported for TSO | |
3066 | * @dev: netdev to update | |
3067 | * @segs: max number of TCP segments | |
3068 | * | |
3069 | * Set the limit on the number of TCP segments the device can generate from | |
3070 | * a single TSO super-frame. | |
3071 | * Unless explicitly set the stack will assume the value of %GSO_MAX_SEGS. | |
3072 | */ | |
3073 | void netif_set_tso_max_segs(struct net_device *dev, unsigned int segs) | |
3074 | { | |
3075 | dev->tso_max_segs = segs; | |
3076 | if (segs < READ_ONCE(dev->gso_max_segs)) | |
3077 | netif_set_gso_max_segs(dev, segs); | |
3078 | } | |
3079 | EXPORT_SYMBOL(netif_set_tso_max_segs); | |
3080 | ||
6df6398f JK |
3081 | /** |
3082 | * netif_inherit_tso_max() - copy all TSO limits from a lower device to an upper | |
3083 | * @to: netdev to update | |
3084 | * @from: netdev from which to copy the limits | |
3085 | */ | |
3086 | void netif_inherit_tso_max(struct net_device *to, const struct net_device *from) | |
3087 | { | |
ee8b7a11 JK |
3088 | netif_set_tso_max_size(to, from->tso_max_size); |
3089 | netif_set_tso_max_segs(to, from->tso_max_segs); | |
6df6398f JK |
3090 | } |
3091 | EXPORT_SYMBOL(netif_inherit_tso_max); | |
3092 | ||
2c53040f BH |
3093 | /** |
3094 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 | 3095 | * |
046e1537 ÍH |
3096 | * Default value is the number of physical cores if there are only 1 or 2, or |
3097 | * divided by 2 if there are more. | |
16917b87 | 3098 | */ |
a55b138b | 3099 | int netif_get_num_default_rss_queues(void) |
16917b87 | 3100 | { |
046e1537 ÍH |
3101 | cpumask_var_t cpus; |
3102 | int cpu, count = 0; | |
3103 | ||
3104 | if (unlikely(is_kdump_kernel() || !zalloc_cpumask_var(&cpus, GFP_KERNEL))) | |
3105 | return 1; | |
3106 | ||
3107 | cpumask_copy(cpus, cpu_online_mask); | |
3108 | for_each_cpu(cpu, cpus) { | |
3109 | ++count; | |
3110 | cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu)); | |
3111 | } | |
3112 | free_cpumask_var(cpus); | |
3113 | ||
3114 | return count > 2 ? DIV_ROUND_UP(count, 2) : count; | |
16917b87 YM |
3115 | } |
3116 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
3117 | ||
3bcb846c | 3118 | static void __netif_reschedule(struct Qdisc *q) |
56079431 | 3119 | { |
def82a1d JP |
3120 | struct softnet_data *sd; |
3121 | unsigned long flags; | |
56079431 | 3122 | |
def82a1d | 3123 | local_irq_save(flags); |
903ceff7 | 3124 | sd = this_cpu_ptr(&softnet_data); |
a9cbd588 CG |
3125 | q->next_sched = NULL; |
3126 | *sd->output_queue_tailp = q; | |
3127 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
3128 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
3129 | local_irq_restore(flags); | |
3130 | } | |
3131 | ||
3132 | void __netif_schedule(struct Qdisc *q) | |
3133 | { | |
3134 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
3135 | __netif_reschedule(q); | |
56079431 DV |
3136 | } |
3137 | EXPORT_SYMBOL(__netif_schedule); | |
3138 | ||
e6247027 | 3139 | struct dev_kfree_skb_cb { |
40bbae58 | 3140 | enum skb_drop_reason reason; |
e6247027 ED |
3141 | }; |
3142 | ||
3143 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) | |
56079431 | 3144 | { |
e6247027 ED |
3145 | return (struct dev_kfree_skb_cb *)skb->cb; |
3146 | } | |
3147 | ||
46e5da40 JF |
3148 | void netif_schedule_queue(struct netdev_queue *txq) |
3149 | { | |
3150 | rcu_read_lock(); | |
5be5515a | 3151 | if (!netif_xmit_stopped(txq)) { |
46e5da40 JF |
3152 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
3153 | ||
3154 | __netif_schedule(q); | |
3155 | } | |
3156 | rcu_read_unlock(); | |
3157 | } | |
3158 | EXPORT_SYMBOL(netif_schedule_queue); | |
3159 | ||
46e5da40 JF |
3160 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
3161 | { | |
3162 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { | |
3163 | struct Qdisc *q; | |
3164 | ||
3165 | rcu_read_lock(); | |
3166 | q = rcu_dereference(dev_queue->qdisc); | |
3167 | __netif_schedule(q); | |
3168 | rcu_read_unlock(); | |
3169 | } | |
3170 | } | |
3171 | EXPORT_SYMBOL(netif_tx_wake_queue); | |
3172 | ||
40bbae58 | 3173 | void dev_kfree_skb_irq_reason(struct sk_buff *skb, enum skb_drop_reason reason) |
56079431 | 3174 | { |
e6247027 | 3175 | unsigned long flags; |
56079431 | 3176 | |
9899886d MJ |
3177 | if (unlikely(!skb)) |
3178 | return; | |
3179 | ||
63354797 | 3180 | if (likely(refcount_read(&skb->users) == 1)) { |
e6247027 | 3181 | smp_rmb(); |
63354797 RE |
3182 | refcount_set(&skb->users, 0); |
3183 | } else if (likely(!refcount_dec_and_test(&skb->users))) { | |
e6247027 | 3184 | return; |
bea3348e | 3185 | } |
e6247027 ED |
3186 | get_kfree_skb_cb(skb)->reason = reason; |
3187 | local_irq_save(flags); | |
3188 | skb->next = __this_cpu_read(softnet_data.completion_queue); | |
3189 | __this_cpu_write(softnet_data.completion_queue, skb); | |
3190 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
3191 | local_irq_restore(flags); | |
56079431 | 3192 | } |
40bbae58 | 3193 | EXPORT_SYMBOL(dev_kfree_skb_irq_reason); |
56079431 | 3194 | |
40bbae58 | 3195 | void dev_kfree_skb_any_reason(struct sk_buff *skb, enum skb_drop_reason reason) |
56079431 | 3196 | { |
afa79d08 | 3197 | if (in_hardirq() || irqs_disabled()) |
40bbae58 | 3198 | dev_kfree_skb_irq_reason(skb, reason); |
56079431 | 3199 | else |
40bbae58 | 3200 | kfree_skb_reason(skb, reason); |
56079431 | 3201 | } |
40bbae58 | 3202 | EXPORT_SYMBOL(dev_kfree_skb_any_reason); |
56079431 DV |
3203 | |
3204 | ||
bea3348e SH |
3205 | /** |
3206 | * netif_device_detach - mark device as removed | |
3207 | * @dev: network device | |
3208 | * | |
3209 | * Mark device as removed from system and therefore no longer available. | |
3210 | */ | |
56079431 DV |
3211 | void netif_device_detach(struct net_device *dev) |
3212 | { | |
3213 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3214 | netif_running(dev)) { | |
d543103a | 3215 | netif_tx_stop_all_queues(dev); |
56079431 DV |
3216 | } |
3217 | } | |
3218 | EXPORT_SYMBOL(netif_device_detach); | |
3219 | ||
bea3348e SH |
3220 | /** |
3221 | * netif_device_attach - mark device as attached | |
3222 | * @dev: network device | |
3223 | * | |
3224 | * Mark device as attached from system and restart if needed. | |
3225 | */ | |
56079431 DV |
3226 | void netif_device_attach(struct net_device *dev) |
3227 | { | |
3228 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3229 | netif_running(dev)) { | |
d543103a | 3230 | netif_tx_wake_all_queues(dev); |
4ec93edb | 3231 | __netdev_watchdog_up(dev); |
56079431 DV |
3232 | } |
3233 | } | |
3234 | EXPORT_SYMBOL(netif_device_attach); | |
3235 | ||
5605c762 JP |
3236 | /* |
3237 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
3238 | * to be used as a distribution range. | |
3239 | */ | |
eadec877 AD |
3240 | static u16 skb_tx_hash(const struct net_device *dev, |
3241 | const struct net_device *sb_dev, | |
3242 | struct sk_buff *skb) | |
5605c762 JP |
3243 | { |
3244 | u32 hash; | |
3245 | u16 qoffset = 0; | |
1b837d48 | 3246 | u16 qcount = dev->real_num_tx_queues; |
5605c762 | 3247 | |
eadec877 AD |
3248 | if (dev->num_tc) { |
3249 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
3250 | ||
3251 | qoffset = sb_dev->tc_to_txq[tc].offset; | |
3252 | qcount = sb_dev->tc_to_txq[tc].count; | |
0c57eeec MC |
3253 | if (unlikely(!qcount)) { |
3254 | net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n", | |
3255 | sb_dev->name, qoffset, tc); | |
3256 | qoffset = 0; | |
3257 | qcount = dev->real_num_tx_queues; | |
3258 | } | |
eadec877 AD |
3259 | } |
3260 | ||
5605c762 | 3261 | if (skb_rx_queue_recorded(skb)) { |
066b8678 | 3262 | DEBUG_NET_WARN_ON_ONCE(qcount == 0); |
5605c762 | 3263 | hash = skb_get_rx_queue(skb); |
6e11d157 AN |
3264 | if (hash >= qoffset) |
3265 | hash -= qoffset; | |
1b837d48 AD |
3266 | while (unlikely(hash >= qcount)) |
3267 | hash -= qcount; | |
eadec877 | 3268 | return hash + qoffset; |
5605c762 JP |
3269 | } |
3270 | ||
3271 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; | |
3272 | } | |
5605c762 | 3273 | |
d457a0e3 | 3274 | void skb_warn_bad_offload(const struct sk_buff *skb) |
36c92474 | 3275 | { |
84d15ae5 | 3276 | static const netdev_features_t null_features; |
36c92474 | 3277 | struct net_device *dev = skb->dev; |
88ad4175 | 3278 | const char *name = ""; |
36c92474 | 3279 | |
c846ad9b BG |
3280 | if (!net_ratelimit()) |
3281 | return; | |
3282 | ||
88ad4175 BM |
3283 | if (dev) { |
3284 | if (dev->dev.parent) | |
3285 | name = dev_driver_string(dev->dev.parent); | |
3286 | else | |
3287 | name = netdev_name(dev); | |
3288 | } | |
6413139d WB |
3289 | skb_dump(KERN_WARNING, skb, false); |
3290 | WARN(1, "%s: caps=(%pNF, %pNF)\n", | |
88ad4175 | 3291 | name, dev ? &dev->features : &null_features, |
6413139d | 3292 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
36c92474 BH |
3293 | } |
3294 | ||
1da177e4 LT |
3295 | /* |
3296 | * Invalidate hardware checksum when packet is to be mangled, and | |
3297 | * complete checksum manually on outgoing path. | |
3298 | */ | |
84fa7933 | 3299 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 3300 | { |
d3bc23e7 | 3301 | __wsum csum; |
663ead3b | 3302 | int ret = 0, offset; |
1da177e4 | 3303 | |
84fa7933 | 3304 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
3305 | goto out_set_summed; |
3306 | ||
3aefd7d6 | 3307 | if (unlikely(skb_is_gso(skb))) { |
36c92474 BH |
3308 | skb_warn_bad_offload(skb); |
3309 | return -EINVAL; | |
1da177e4 LT |
3310 | } |
3311 | ||
cef401de ED |
3312 | /* Before computing a checksum, we should make sure no frag could |
3313 | * be modified by an external entity : checksum could be wrong. | |
3314 | */ | |
3315 | if (skb_has_shared_frag(skb)) { | |
3316 | ret = __skb_linearize(skb); | |
3317 | if (ret) | |
3318 | goto out; | |
3319 | } | |
3320 | ||
55508d60 | 3321 | offset = skb_checksum_start_offset(skb); |
d7ea0d9d | 3322 | ret = -EINVAL; |
26c29961 | 3323 | if (unlikely(offset >= skb_headlen(skb))) { |
eeee4b77 | 3324 | DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false); |
26c29961 ED |
3325 | WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n", |
3326 | offset, skb_headlen(skb)); | |
d7ea0d9d | 3327 | goto out; |
eeee4b77 | 3328 | } |
a030847e HX |
3329 | csum = skb_checksum(skb, offset, skb->len - offset, 0); |
3330 | ||
3331 | offset += skb->csum_offset; | |
26c29961 | 3332 | if (unlikely(offset + sizeof(__sum16) > skb_headlen(skb))) { |
eeee4b77 | 3333 | DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false); |
26c29961 ED |
3334 | WARN_ONCE(true, "offset+2 (%zu) > skb_headlen() (%u)\n", |
3335 | offset + sizeof(__sum16), skb_headlen(skb)); | |
d7ea0d9d | 3336 | goto out; |
eeee4b77 | 3337 | } |
8211fbfa HK |
3338 | ret = skb_ensure_writable(skb, offset + sizeof(__sum16)); |
3339 | if (ret) | |
3340 | goto out; | |
1da177e4 | 3341 | |
4f2e4ad5 | 3342 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
a430a43d | 3343 | out_set_summed: |
1da177e4 | 3344 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 3345 | out: |
1da177e4 LT |
3346 | return ret; |
3347 | } | |
d1b19dff | 3348 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 3349 | |
b72b5bf6 DC |
3350 | int skb_crc32c_csum_help(struct sk_buff *skb) |
3351 | { | |
3352 | __le32 crc32c_csum; | |
3353 | int ret = 0, offset, start; | |
3354 | ||
3355 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
3356 | goto out; | |
3357 | ||
3358 | if (unlikely(skb_is_gso(skb))) | |
3359 | goto out; | |
3360 | ||
3361 | /* Before computing a checksum, we should make sure no frag could | |
3362 | * be modified by an external entity : checksum could be wrong. | |
3363 | */ | |
3364 | if (unlikely(skb_has_shared_frag(skb))) { | |
3365 | ret = __skb_linearize(skb); | |
3366 | if (ret) | |
3367 | goto out; | |
3368 | } | |
3369 | start = skb_checksum_start_offset(skb); | |
3370 | offset = start + offsetof(struct sctphdr, checksum); | |
3371 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { | |
3372 | ret = -EINVAL; | |
3373 | goto out; | |
3374 | } | |
8211fbfa HK |
3375 | |
3376 | ret = skb_ensure_writable(skb, offset + sizeof(__le32)); | |
3377 | if (ret) | |
3378 | goto out; | |
3379 | ||
b72b5bf6 DC |
3380 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, |
3381 | skb->len - start, ~(__u32)0, | |
3382 | crc32c_csum_stub)); | |
3383 | *(__le32 *)(skb->data + offset) = crc32c_csum; | |
c24831a1 | 3384 | skb_reset_csum_not_inet(skb); |
b72b5bf6 DC |
3385 | out: |
3386 | return ret; | |
3387 | } | |
3388 | ||
53d6471c | 3389 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
f6a78bfc | 3390 | { |
252e3346 | 3391 | __be16 type = skb->protocol; |
f6a78bfc | 3392 | |
19acc327 PS |
3393 | /* Tunnel gso handlers can set protocol to ethernet. */ |
3394 | if (type == htons(ETH_P_TEB)) { | |
3395 | struct ethhdr *eth; | |
3396 | ||
3397 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) | |
3398 | return 0; | |
3399 | ||
1dfe82eb | 3400 | eth = (struct ethhdr *)skb->data; |
19acc327 PS |
3401 | type = eth->h_proto; |
3402 | } | |
3403 | ||
4063384e | 3404 | return vlan_get_protocol_and_depth(skb, type, depth); |
ec5f0615 PS |
3405 | } |
3406 | ||
f6a78bfc | 3407 | |
fb286bb2 HX |
3408 | /* Take action when hardware reception checksum errors are detected. */ |
3409 | #ifdef CONFIG_BUG | |
127d7355 TL |
3410 | static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
3411 | { | |
5b92be64 | 3412 | netdev_err(dev, "hw csum failure\n"); |
127d7355 TL |
3413 | skb_dump(KERN_ERR, skb, true); |
3414 | dump_stack(); | |
3415 | } | |
3416 | ||
7fe50ac8 | 3417 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
fb286bb2 | 3418 | { |
127d7355 | 3419 | DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb); |
fb286bb2 HX |
3420 | } |
3421 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
3422 | #endif | |
3423 | ||
ab74cfeb | 3424 | /* XXX: check that highmem exists at all on the given machine. */ |
c1e756bf | 3425 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 3426 | { |
3d3a8533 | 3427 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 3428 | int i; |
f4563a75 | 3429 | |
5acbbd42 | 3430 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
3431 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
3432 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
f4563a75 | 3433 | |
ea2ab693 | 3434 | if (PageHighMem(skb_frag_page(frag))) |
5acbbd42 | 3435 | return 1; |
ea2ab693 | 3436 | } |
5acbbd42 | 3437 | } |
3d3a8533 | 3438 | #endif |
1da177e4 LT |
3439 | return 0; |
3440 | } | |
1da177e4 | 3441 | |
3b392ddb SH |
3442 | /* If MPLS offload request, verify we are testing hardware MPLS features |
3443 | * instead of standard features for the netdev. | |
3444 | */ | |
d0edc7bf | 3445 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
3b392ddb SH |
3446 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
3447 | netdev_features_t features, | |
3448 | __be16 type) | |
3449 | { | |
25cd9ba0 | 3450 | if (eth_p_mpls(type)) |
3b392ddb SH |
3451 | features &= skb->dev->mpls_features; |
3452 | ||
3453 | return features; | |
3454 | } | |
3455 | #else | |
3456 | static netdev_features_t net_mpls_features(struct sk_buff *skb, | |
3457 | netdev_features_t features, | |
3458 | __be16 type) | |
3459 | { | |
3460 | return features; | |
3461 | } | |
3462 | #endif | |
3463 | ||
c8f44aff | 3464 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
c1e756bf | 3465 | netdev_features_t features) |
f01a5236 | 3466 | { |
3b392ddb SH |
3467 | __be16 type; |
3468 | ||
9fc95f50 | 3469 | type = skb_network_protocol(skb, NULL); |
3b392ddb | 3470 | features = net_mpls_features(skb, features, type); |
53d6471c | 3471 | |
c0d680e5 | 3472 | if (skb->ip_summed != CHECKSUM_NONE && |
3b392ddb | 3473 | !can_checksum_protocol(features, type)) { |
996e8021 | 3474 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
f01a5236 | 3475 | } |
7be2c82c ED |
3476 | if (illegal_highdma(skb->dev, skb)) |
3477 | features &= ~NETIF_F_SG; | |
f01a5236 JG |
3478 | |
3479 | return features; | |
3480 | } | |
3481 | ||
e38f3025 TM |
3482 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
3483 | struct net_device *dev, | |
3484 | netdev_features_t features) | |
3485 | { | |
3486 | return features; | |
3487 | } | |
3488 | EXPORT_SYMBOL(passthru_features_check); | |
3489 | ||
7ce23672 | 3490 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
8cb65d00 TM |
3491 | struct net_device *dev, |
3492 | netdev_features_t features) | |
3493 | { | |
3494 | return vlan_features_check(skb, features); | |
3495 | } | |
3496 | ||
cbc53e08 AD |
3497 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
3498 | struct net_device *dev, | |
3499 | netdev_features_t features) | |
3500 | { | |
3501 | u16 gso_segs = skb_shinfo(skb)->gso_segs; | |
3502 | ||
6d872df3 | 3503 | if (gso_segs > READ_ONCE(dev->gso_max_segs)) |
cbc53e08 AD |
3504 | return features & ~NETIF_F_GSO_MASK; |
3505 | ||
24ab059d ED |
3506 | if (unlikely(skb->len >= READ_ONCE(dev->gso_max_size))) |
3507 | return features & ~NETIF_F_GSO_MASK; | |
3508 | ||
1d155dfd HK |
3509 | if (!skb_shinfo(skb)->gso_type) { |
3510 | skb_warn_bad_offload(skb); | |
3511 | return features & ~NETIF_F_GSO_MASK; | |
3512 | } | |
3513 | ||
802ab55a AD |
3514 | /* Support for GSO partial features requires software |
3515 | * intervention before we can actually process the packets | |
3516 | * so we need to strip support for any partial features now | |
3517 | * and we can pull them back in after we have partially | |
3518 | * segmented the frame. | |
3519 | */ | |
3520 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) | |
3521 | features &= ~dev->gso_partial_features; | |
3522 | ||
3523 | /* Make sure to clear the IPv4 ID mangling feature if the | |
3524 | * IPv4 header has the potential to be fragmented. | |
cbc53e08 AD |
3525 | */ |
3526 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
3527 | struct iphdr *iph = skb->encapsulation ? | |
3528 | inner_ip_hdr(skb) : ip_hdr(skb); | |
3529 | ||
3530 | if (!(iph->frag_off & htons(IP_DF))) | |
3531 | features &= ~NETIF_F_TSO_MANGLEID; | |
3532 | } | |
3533 | ||
3534 | return features; | |
3535 | } | |
3536 | ||
c1e756bf | 3537 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 | 3538 | { |
5f35227e | 3539 | struct net_device *dev = skb->dev; |
fcbeb976 | 3540 | netdev_features_t features = dev->features; |
58e998c6 | 3541 | |
cbc53e08 AD |
3542 | if (skb_is_gso(skb)) |
3543 | features = gso_features_check(skb, dev, features); | |
30b678d8 | 3544 | |
5f35227e JG |
3545 | /* If encapsulation offload request, verify we are testing |
3546 | * hardware encapsulation features instead of standard | |
3547 | * features for the netdev | |
3548 | */ | |
3549 | if (skb->encapsulation) | |
3550 | features &= dev->hw_enc_features; | |
3551 | ||
f5a7fb88 TM |
3552 | if (skb_vlan_tagged(skb)) |
3553 | features = netdev_intersect_features(features, | |
3554 | dev->vlan_features | | |
3555 | NETIF_F_HW_VLAN_CTAG_TX | | |
3556 | NETIF_F_HW_VLAN_STAG_TX); | |
f01a5236 | 3557 | |
5f35227e JG |
3558 | if (dev->netdev_ops->ndo_features_check) |
3559 | features &= dev->netdev_ops->ndo_features_check(skb, dev, | |
3560 | features); | |
8cb65d00 TM |
3561 | else |
3562 | features &= dflt_features_check(skb, dev, features); | |
5f35227e | 3563 | |
c1e756bf | 3564 | return harmonize_features(skb, features); |
58e998c6 | 3565 | } |
c1e756bf | 3566 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 3567 | |
2ea25513 | 3568 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
95f6b3dd | 3569 | struct netdev_queue *txq, bool more) |
f6a78bfc | 3570 | { |
2ea25513 DM |
3571 | unsigned int len; |
3572 | int rc; | |
00829823 | 3573 | |
9f9a742d | 3574 | if (dev_nit_active(dev)) |
2ea25513 | 3575 | dev_queue_xmit_nit(skb, dev); |
fc741216 | 3576 | |
2ea25513 DM |
3577 | len = skb->len; |
3578 | trace_net_dev_start_xmit(skb, dev); | |
95f6b3dd | 3579 | rc = netdev_start_xmit(skb, dev, txq, more); |
2ea25513 | 3580 | trace_net_dev_xmit(skb, rc, dev, len); |
adf30907 | 3581 | |
2ea25513 DM |
3582 | return rc; |
3583 | } | |
7b9c6090 | 3584 | |
8dcda22a DM |
3585 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
3586 | struct netdev_queue *txq, int *ret) | |
7f2e870f DM |
3587 | { |
3588 | struct sk_buff *skb = first; | |
3589 | int rc = NETDEV_TX_OK; | |
7b9c6090 | 3590 | |
7f2e870f DM |
3591 | while (skb) { |
3592 | struct sk_buff *next = skb->next; | |
fc70fb64 | 3593 | |
a8305bff | 3594 | skb_mark_not_on_list(skb); |
95f6b3dd | 3595 | rc = xmit_one(skb, dev, txq, next != NULL); |
7f2e870f DM |
3596 | if (unlikely(!dev_xmit_complete(rc))) { |
3597 | skb->next = next; | |
3598 | goto out; | |
3599 | } | |
6afff0ca | 3600 | |
7f2e870f | 3601 | skb = next; |
fe60faa5 | 3602 | if (netif_tx_queue_stopped(txq) && skb) { |
7f2e870f DM |
3603 | rc = NETDEV_TX_BUSY; |
3604 | break; | |
9ccb8975 | 3605 | } |
7f2e870f | 3606 | } |
9ccb8975 | 3607 | |
7f2e870f DM |
3608 | out: |
3609 | *ret = rc; | |
3610 | return skb; | |
3611 | } | |
b40863c6 | 3612 | |
1ff0dc94 ED |
3613 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
3614 | netdev_features_t features) | |
f6a78bfc | 3615 | { |
df8a39de | 3616 | if (skb_vlan_tag_present(skb) && |
5968250c JP |
3617 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
3618 | skb = __vlan_hwaccel_push_inside(skb); | |
eae3f88e DM |
3619 | return skb; |
3620 | } | |
f6a78bfc | 3621 | |
43c26a1a DC |
3622 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
3623 | const netdev_features_t features) | |
3624 | { | |
fa821170 | 3625 | if (unlikely(skb_csum_is_sctp(skb))) |
43c26a1a DC |
3626 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : |
3627 | skb_crc32c_csum_help(skb); | |
3628 | ||
62fafcd6 XL |
3629 | if (features & NETIF_F_HW_CSUM) |
3630 | return 0; | |
3631 | ||
3632 | if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { | |
3633 | switch (skb->csum_offset) { | |
3634 | case offsetof(struct tcphdr, check): | |
3635 | case offsetof(struct udphdr, check): | |
3636 | return 0; | |
3637 | } | |
3638 | } | |
3639 | ||
3640 | return skb_checksum_help(skb); | |
43c26a1a DC |
3641 | } |
3642 | EXPORT_SYMBOL(skb_csum_hwoffload_help); | |
3643 | ||
f53c7239 | 3644 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
eae3f88e DM |
3645 | { |
3646 | netdev_features_t features; | |
f6a78bfc | 3647 | |
eae3f88e DM |
3648 | features = netif_skb_features(skb); |
3649 | skb = validate_xmit_vlan(skb, features); | |
3650 | if (unlikely(!skb)) | |
3651 | goto out_null; | |
7b9c6090 | 3652 | |
ebf4e808 IL |
3653 | skb = sk_validate_xmit_skb(skb, dev); |
3654 | if (unlikely(!skb)) | |
3655 | goto out_null; | |
3656 | ||
8b86a61d | 3657 | if (netif_needs_gso(skb, features)) { |
ce93718f DM |
3658 | struct sk_buff *segs; |
3659 | ||
3660 | segs = skb_gso_segment(skb, features); | |
cecda693 | 3661 | if (IS_ERR(segs)) { |
af6dabc9 | 3662 | goto out_kfree_skb; |
cecda693 JW |
3663 | } else if (segs) { |
3664 | consume_skb(skb); | |
3665 | skb = segs; | |
f6a78bfc | 3666 | } |
eae3f88e DM |
3667 | } else { |
3668 | if (skb_needs_linearize(skb, features) && | |
3669 | __skb_linearize(skb)) | |
3670 | goto out_kfree_skb; | |
4ec93edb | 3671 | |
eae3f88e DM |
3672 | /* If packet is not checksummed and device does not |
3673 | * support checksumming for this protocol, complete | |
3674 | * checksumming here. | |
3675 | */ | |
3676 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
3677 | if (skb->encapsulation) | |
3678 | skb_set_inner_transport_header(skb, | |
3679 | skb_checksum_start_offset(skb)); | |
3680 | else | |
3681 | skb_set_transport_header(skb, | |
3682 | skb_checksum_start_offset(skb)); | |
43c26a1a | 3683 | if (skb_csum_hwoffload_help(skb, features)) |
eae3f88e | 3684 | goto out_kfree_skb; |
7b9c6090 | 3685 | } |
0c772159 | 3686 | } |
7b9c6090 | 3687 | |
f53c7239 | 3688 | skb = validate_xmit_xfrm(skb, features, again); |
3dca3f38 | 3689 | |
eae3f88e | 3690 | return skb; |
fc70fb64 | 3691 | |
f6a78bfc HX |
3692 | out_kfree_skb: |
3693 | kfree_skb(skb); | |
eae3f88e | 3694 | out_null: |
625788b5 | 3695 | dev_core_stats_tx_dropped_inc(dev); |
eae3f88e DM |
3696 | return NULL; |
3697 | } | |
6afff0ca | 3698 | |
f53c7239 | 3699 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
55a93b3e ED |
3700 | { |
3701 | struct sk_buff *next, *head = NULL, *tail; | |
3702 | ||
bec3cfdc | 3703 | for (; skb != NULL; skb = next) { |
55a93b3e | 3704 | next = skb->next; |
a8305bff | 3705 | skb_mark_not_on_list(skb); |
bec3cfdc ED |
3706 | |
3707 | /* in case skb wont be segmented, point to itself */ | |
3708 | skb->prev = skb; | |
3709 | ||
f53c7239 | 3710 | skb = validate_xmit_skb(skb, dev, again); |
bec3cfdc ED |
3711 | if (!skb) |
3712 | continue; | |
55a93b3e | 3713 | |
bec3cfdc ED |
3714 | if (!head) |
3715 | head = skb; | |
3716 | else | |
3717 | tail->next = skb; | |
3718 | /* If skb was segmented, skb->prev points to | |
3719 | * the last segment. If not, it still contains skb. | |
3720 | */ | |
3721 | tail = skb->prev; | |
55a93b3e ED |
3722 | } |
3723 | return head; | |
f6a78bfc | 3724 | } |
104ba78c | 3725 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
f6a78bfc | 3726 | |
1def9238 ED |
3727 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
3728 | { | |
3729 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
3730 | ||
3731 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
3732 | ||
3733 | /* To get more precise estimation of bytes sent on wire, | |
3734 | * we add to pkt_len the headers size of all segments | |
3735 | */ | |
a0dce875 | 3736 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
15e5a030 | 3737 | u16 gso_segs = shinfo->gso_segs; |
f5fca219 | 3738 | unsigned int hdr_len; |
1def9238 | 3739 | |
757b8b1d | 3740 | /* mac layer + network layer */ |
f5fca219 | 3741 | hdr_len = skb_transport_offset(skb); |
757b8b1d ED |
3742 | |
3743 | /* + transport layer */ | |
7c68d1a6 ED |
3744 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
3745 | const struct tcphdr *th; | |
3746 | struct tcphdr _tcphdr; | |
3747 | ||
f5fca219 | 3748 | th = skb_header_pointer(skb, hdr_len, |
7c68d1a6 ED |
3749 | sizeof(_tcphdr), &_tcphdr); |
3750 | if (likely(th)) | |
3751 | hdr_len += __tcp_hdrlen(th); | |
3752 | } else { | |
3753 | struct udphdr _udphdr; | |
3754 | ||
f5fca219 | 3755 | if (skb_header_pointer(skb, hdr_len, |
7c68d1a6 ED |
3756 | sizeof(_udphdr), &_udphdr)) |
3757 | hdr_len += sizeof(struct udphdr); | |
3758 | } | |
15e5a030 JW |
3759 | |
3760 | if (shinfo->gso_type & SKB_GSO_DODGY) | |
3761 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, | |
3762 | shinfo->gso_size); | |
3763 | ||
3764 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; | |
1def9238 ED |
3765 | } |
3766 | } | |
3767 | ||
70713ddd QX |
3768 | static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q, |
3769 | struct sk_buff **to_free, | |
3770 | struct netdev_queue *txq) | |
3771 | { | |
3772 | int rc; | |
3773 | ||
3774 | rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK; | |
3775 | if (rc == NET_XMIT_SUCCESS) | |
3776 | trace_qdisc_enqueue(q, txq, skb); | |
3777 | return rc; | |
3778 | } | |
3779 | ||
bbd8a0d3 KK |
3780 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
3781 | struct net_device *dev, | |
3782 | struct netdev_queue *txq) | |
3783 | { | |
3784 | spinlock_t *root_lock = qdisc_lock(q); | |
520ac30f | 3785 | struct sk_buff *to_free = NULL; |
a2da570d | 3786 | bool contended; |
bbd8a0d3 KK |
3787 | int rc; |
3788 | ||
a2da570d | 3789 | qdisc_calculate_pkt_len(skb, q); |
6b3ba914 | 3790 | |
b6a3c606 VN |
3791 | tcf_set_drop_reason(skb, SKB_DROP_REASON_QDISC_DROP); |
3792 | ||
6b3ba914 | 3793 | if (q->flags & TCQ_F_NOLOCK) { |
c4fef01b YL |
3794 | if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) && |
3795 | qdisc_run_begin(q)) { | |
3796 | /* Retest nolock_qdisc_is_empty() within the protection | |
3797 | * of q->seqlock to protect from racing with requeuing. | |
3798 | */ | |
3799 | if (unlikely(!nolock_qdisc_is_empty(q))) { | |
70713ddd | 3800 | rc = dev_qdisc_enqueue(skb, q, &to_free, txq); |
c4fef01b YL |
3801 | __qdisc_run(q); |
3802 | qdisc_run_end(q); | |
3803 | ||
3804 | goto no_lock_out; | |
3805 | } | |
3806 | ||
3807 | qdisc_bstats_cpu_update(q, skb); | |
3808 | if (sch_direct_xmit(skb, q, dev, txq, NULL, true) && | |
3809 | !nolock_qdisc_is_empty(q)) | |
3810 | __qdisc_run(q); | |
3811 | ||
3812 | qdisc_run_end(q); | |
3813 | return NET_XMIT_SUCCESS; | |
3814 | } | |
3815 | ||
70713ddd | 3816 | rc = dev_qdisc_enqueue(skb, q, &to_free, txq); |
c4fef01b | 3817 | qdisc_run(q); |
6b3ba914 | 3818 | |
c4fef01b | 3819 | no_lock_out: |
6b3ba914 | 3820 | if (unlikely(to_free)) |
7faef054 | 3821 | kfree_skb_list_reason(to_free, |
b6a3c606 | 3822 | tcf_get_drop_reason(to_free)); |
6b3ba914 JF |
3823 | return rc; |
3824 | } | |
3825 | ||
0f022d32 ED |
3826 | if (unlikely(READ_ONCE(q->owner) == smp_processor_id())) { |
3827 | kfree_skb_reason(skb, SKB_DROP_REASON_TC_RECLASSIFY_LOOP); | |
3828 | return NET_XMIT_DROP; | |
3829 | } | |
79640a4c ED |
3830 | /* |
3831 | * Heuristic to force contended enqueues to serialize on a | |
3832 | * separate lock before trying to get qdisc main lock. | |
f9eb8aea | 3833 | * This permits qdisc->running owner to get the lock more |
9bf2b8c2 | 3834 | * often and dequeue packets faster. |
64445dda SAS |
3835 | * On PREEMPT_RT it is possible to preempt the qdisc owner during xmit |
3836 | * and then other tasks will only enqueue packets. The packets will be | |
3837 | * sent after the qdisc owner is scheduled again. To prevent this | |
3838 | * scenario the task always serialize on the lock. | |
79640a4c | 3839 | */ |
a9aa5e33 | 3840 | contended = qdisc_is_running(q) || IS_ENABLED(CONFIG_PREEMPT_RT); |
79640a4c ED |
3841 | if (unlikely(contended)) |
3842 | spin_lock(&q->busylock); | |
3843 | ||
bbd8a0d3 KK |
3844 | spin_lock(root_lock); |
3845 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
520ac30f | 3846 | __qdisc_drop(skb, &to_free); |
bbd8a0d3 KK |
3847 | rc = NET_XMIT_DROP; |
3848 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 3849 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
3850 | /* |
3851 | * This is a work-conserving queue; there are no old skbs | |
3852 | * waiting to be sent out; and the qdisc is not running - | |
3853 | * xmit the skb directly. | |
3854 | */ | |
bfe0d029 | 3855 | |
bfe0d029 ED |
3856 | qdisc_bstats_update(q, skb); |
3857 | ||
55a93b3e | 3858 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
79640a4c ED |
3859 | if (unlikely(contended)) { |
3860 | spin_unlock(&q->busylock); | |
3861 | contended = false; | |
3862 | } | |
bbd8a0d3 | 3863 | __qdisc_run(q); |
6c148184 | 3864 | } |
bbd8a0d3 | 3865 | |
6c148184 | 3866 | qdisc_run_end(q); |
bbd8a0d3 KK |
3867 | rc = NET_XMIT_SUCCESS; |
3868 | } else { | |
0f022d32 | 3869 | WRITE_ONCE(q->owner, smp_processor_id()); |
70713ddd | 3870 | rc = dev_qdisc_enqueue(skb, q, &to_free, txq); |
0f022d32 | 3871 | WRITE_ONCE(q->owner, -1); |
79640a4c ED |
3872 | if (qdisc_run_begin(q)) { |
3873 | if (unlikely(contended)) { | |
3874 | spin_unlock(&q->busylock); | |
3875 | contended = false; | |
3876 | } | |
3877 | __qdisc_run(q); | |
6c148184 | 3878 | qdisc_run_end(q); |
79640a4c | 3879 | } |
bbd8a0d3 KK |
3880 | } |
3881 | spin_unlock(root_lock); | |
520ac30f | 3882 | if (unlikely(to_free)) |
b6a3c606 VN |
3883 | kfree_skb_list_reason(to_free, |
3884 | tcf_get_drop_reason(to_free)); | |
79640a4c ED |
3885 | if (unlikely(contended)) |
3886 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
3887 | return rc; |
3888 | } | |
3889 | ||
86f8515f | 3890 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
5bc1421e NH |
3891 | static void skb_update_prio(struct sk_buff *skb) |
3892 | { | |
4dcb31d4 ED |
3893 | const struct netprio_map *map; |
3894 | const struct sock *sk; | |
3895 | unsigned int prioidx; | |
5bc1421e | 3896 | |
4dcb31d4 ED |
3897 | if (skb->priority) |
3898 | return; | |
3899 | map = rcu_dereference_bh(skb->dev->priomap); | |
3900 | if (!map) | |
3901 | return; | |
3902 | sk = skb_to_full_sk(skb); | |
3903 | if (!sk) | |
3904 | return; | |
91c68ce2 | 3905 | |
4dcb31d4 ED |
3906 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
3907 | ||
3908 | if (prioidx < map->priomap_len) | |
3909 | skb->priority = map->priomap[prioidx]; | |
5bc1421e NH |
3910 | } |
3911 | #else | |
3912 | #define skb_update_prio(skb) | |
3913 | #endif | |
3914 | ||
95603e22 MM |
3915 | /** |
3916 | * dev_loopback_xmit - loop back @skb | |
0c4b51f0 EB |
3917 | * @net: network namespace this loopback is happening in |
3918 | * @sk: sk needed to be a netfilter okfn | |
95603e22 MM |
3919 | * @skb: buffer to transmit |
3920 | */ | |
0c4b51f0 | 3921 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
95603e22 MM |
3922 | { |
3923 | skb_reset_mac_header(skb); | |
3924 | __skb_pull(skb, skb_network_offset(skb)); | |
3925 | skb->pkt_type = PACKET_LOOPBACK; | |
9122a70a CS |
3926 | if (skb->ip_summed == CHECKSUM_NONE) |
3927 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
76458fae | 3928 | DEBUG_NET_WARN_ON_ONCE(!skb_dst(skb)); |
95603e22 | 3929 | skb_dst_force(skb); |
ad0a043f | 3930 | netif_rx(skb); |
95603e22 MM |
3931 | return 0; |
3932 | } | |
3933 | EXPORT_SYMBOL(dev_loopback_xmit); | |
3934 | ||
1f211a1b | 3935 | #ifdef CONFIG_NET_EGRESS |
e420bed0 DB |
3936 | static struct netdev_queue * |
3937 | netdev_tx_queue_mapping(struct net_device *dev, struct sk_buff *skb) | |
3938 | { | |
3939 | int qm = skb_get_queue_mapping(skb); | |
3940 | ||
3941 | return netdev_get_tx_queue(dev, netdev_cap_txqueue(dev, qm)); | |
3942 | } | |
3943 | ||
3944 | static bool netdev_xmit_txqueue_skipped(void) | |
1f211a1b | 3945 | { |
e420bed0 DB |
3946 | return __this_cpu_read(softnet_data.xmit.skip_txqueue); |
3947 | } | |
3948 | ||
3949 | void netdev_xmit_skip_txqueue(bool skip) | |
3950 | { | |
3951 | __this_cpu_write(softnet_data.xmit.skip_txqueue, skip); | |
3952 | } | |
3953 | EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue); | |
3954 | #endif /* CONFIG_NET_EGRESS */ | |
3955 | ||
3956 | #ifdef CONFIG_NET_XGRESS | |
54a59aed DB |
3957 | static int tc_run(struct tcx_entry *entry, struct sk_buff *skb, |
3958 | enum skb_drop_reason *drop_reason) | |
e420bed0 DB |
3959 | { |
3960 | int ret = TC_ACT_UNSPEC; | |
42df6e1d | 3961 | #ifdef CONFIG_NET_CLS_ACT |
e420bed0 DB |
3962 | struct mini_Qdisc *miniq = rcu_dereference_bh(entry->miniq); |
3963 | struct tcf_result res; | |
1f211a1b | 3964 | |
46209401 | 3965 | if (!miniq) |
e420bed0 | 3966 | return ret; |
1f211a1b | 3967 | |
047f340b AST |
3968 | if (static_branch_unlikely(&tcf_bypass_check_needed_key)) { |
3969 | if (tcf_block_bypass_sw(miniq->block)) | |
3970 | return ret; | |
3971 | } | |
3972 | ||
ec624fe7 PB |
3973 | tc_skb_cb(skb)->mru = 0; |
3974 | tc_skb_cb(skb)->post_ct = false; | |
fb278072 | 3975 | tcf_set_drop_reason(skb, *drop_reason); |
1f211a1b | 3976 | |
e420bed0 DB |
3977 | mini_qdisc_bstats_cpu_update(miniq, skb); |
3978 | ret = tcf_classify(skb, miniq->block, miniq->filter_list, &res, false); | |
3979 | /* Only tcf related quirks below. */ | |
3980 | switch (ret) { | |
3981 | case TC_ACT_SHOT: | |
fb278072 | 3982 | *drop_reason = tcf_get_drop_reason(skb); |
e420bed0 DB |
3983 | mini_qdisc_qstats_cpu_drop(miniq); |
3984 | break; | |
1f211a1b DB |
3985 | case TC_ACT_OK: |
3986 | case TC_ACT_RECLASSIFY: | |
e420bed0 | 3987 | skb->tc_index = TC_H_MIN(res.classid); |
1f211a1b | 3988 | break; |
e420bed0 DB |
3989 | } |
3990 | #endif /* CONFIG_NET_CLS_ACT */ | |
3991 | return ret; | |
3992 | } | |
3993 | ||
3994 | static DEFINE_STATIC_KEY_FALSE(tcx_needed_key); | |
3995 | ||
3996 | void tcx_inc(void) | |
3997 | { | |
3998 | static_branch_inc(&tcx_needed_key); | |
3999 | } | |
4000 | ||
4001 | void tcx_dec(void) | |
4002 | { | |
4003 | static_branch_dec(&tcx_needed_key); | |
4004 | } | |
4005 | ||
4006 | static __always_inline enum tcx_action_base | |
4007 | tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, | |
4008 | const bool needs_mac) | |
4009 | { | |
4010 | const struct bpf_mprog_fp *fp; | |
4011 | const struct bpf_prog *prog; | |
4012 | int ret = TCX_NEXT; | |
4013 | ||
4014 | if (needs_mac) | |
4015 | __skb_push(skb, skb->mac_len); | |
4016 | bpf_mprog_foreach_prog(entry, fp, prog) { | |
4017 | bpf_compute_data_pointers(skb); | |
4018 | ret = bpf_prog_run(prog, skb); | |
4019 | if (ret != TCX_NEXT) | |
4020 | break; | |
4021 | } | |
4022 | if (needs_mac) | |
4023 | __skb_pull(skb, skb->mac_len); | |
4024 | return tcx_action_code(skb, ret); | |
4025 | } | |
4026 | ||
4027 | static __always_inline struct sk_buff * | |
4028 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
4029 | struct net_device *orig_dev, bool *another) | |
4030 | { | |
4031 | struct bpf_mprog_entry *entry = rcu_dereference_bh(skb->dev->tcx_ingress); | |
54a59aed | 4032 | enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS; |
e420bed0 DB |
4033 | int sch_ret; |
4034 | ||
4035 | if (!entry) | |
4036 | return skb; | |
4037 | if (*pt_prev) { | |
4038 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4039 | *pt_prev = NULL; | |
4040 | } | |
4041 | ||
4042 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
4043 | tcx_set_ingress(skb, true); | |
4044 | ||
4045 | if (static_branch_unlikely(&tcx_needed_key)) { | |
4046 | sch_ret = tcx_run(entry, skb, true); | |
4047 | if (sch_ret != TC_ACT_UNSPEC) | |
4048 | goto ingress_verdict; | |
4049 | } | |
54a59aed | 4050 | sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason); |
e420bed0 DB |
4051 | ingress_verdict: |
4052 | switch (sch_ret) { | |
4053 | case TC_ACT_REDIRECT: | |
4054 | /* skb_mac_header check was done by BPF, so we can safely | |
4055 | * push the L2 header back before redirecting to another | |
4056 | * netdev. | |
4057 | */ | |
4058 | __skb_push(skb, skb->mac_len); | |
4059 | if (skb_do_redirect(skb) == -EAGAIN) { | |
4060 | __skb_pull(skb, skb->mac_len); | |
4061 | *another = true; | |
4062 | break; | |
4063 | } | |
4064 | *ret = NET_RX_SUCCESS; | |
4065 | return NULL; | |
1f211a1b | 4066 | case TC_ACT_SHOT: |
54a59aed | 4067 | kfree_skb_reason(skb, drop_reason); |
e420bed0 | 4068 | *ret = NET_RX_DROP; |
7e2c3aea | 4069 | return NULL; |
e420bed0 | 4070 | /* used by tc_run */ |
1f211a1b DB |
4071 | case TC_ACT_STOLEN: |
4072 | case TC_ACT_QUEUED: | |
e25ea21f | 4073 | case TC_ACT_TRAP: |
7e2c3aea | 4074 | consume_skb(skb); |
e420bed0 DB |
4075 | fallthrough; |
4076 | case TC_ACT_CONSUMED: | |
4077 | *ret = NET_RX_SUCCESS; | |
1f211a1b | 4078 | return NULL; |
e420bed0 DB |
4079 | } |
4080 | ||
4081 | return skb; | |
4082 | } | |
4083 | ||
4084 | static __always_inline struct sk_buff * | |
4085 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
4086 | { | |
4087 | struct bpf_mprog_entry *entry = rcu_dereference_bh(dev->tcx_egress); | |
54a59aed | 4088 | enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_EGRESS; |
e420bed0 DB |
4089 | int sch_ret; |
4090 | ||
4091 | if (!entry) | |
4092 | return skb; | |
4093 | ||
4094 | /* qdisc_skb_cb(skb)->pkt_len & tcx_set_ingress() was | |
4095 | * already set by the caller. | |
4096 | */ | |
4097 | if (static_branch_unlikely(&tcx_needed_key)) { | |
4098 | sch_ret = tcx_run(entry, skb, false); | |
4099 | if (sch_ret != TC_ACT_UNSPEC) | |
4100 | goto egress_verdict; | |
4101 | } | |
54a59aed | 4102 | sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason); |
e420bed0 DB |
4103 | egress_verdict: |
4104 | switch (sch_ret) { | |
1f211a1b DB |
4105 | case TC_ACT_REDIRECT: |
4106 | /* No need to push/pop skb's mac_header here on egress! */ | |
4107 | skb_do_redirect(skb); | |
4108 | *ret = NET_XMIT_SUCCESS; | |
4109 | return NULL; | |
e420bed0 | 4110 | case TC_ACT_SHOT: |
54a59aed | 4111 | kfree_skb_reason(skb, drop_reason); |
e420bed0 DB |
4112 | *ret = NET_XMIT_DROP; |
4113 | return NULL; | |
4114 | /* used by tc_run */ | |
4115 | case TC_ACT_STOLEN: | |
4116 | case TC_ACT_QUEUED: | |
4117 | case TC_ACT_TRAP: | |
28d18b67 | 4118 | consume_skb(skb); |
3a1e2f43 DB |
4119 | fallthrough; |
4120 | case TC_ACT_CONSUMED: | |
e420bed0 DB |
4121 | *ret = NET_XMIT_SUCCESS; |
4122 | return NULL; | |
1f211a1b | 4123 | } |
357b6cc5 | 4124 | |
1f211a1b DB |
4125 | return skb; |
4126 | } | |
e420bed0 DB |
4127 | #else |
4128 | static __always_inline struct sk_buff * | |
4129 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
4130 | struct net_device *orig_dev, bool *another) | |
2f1e85b1 | 4131 | { |
e420bed0 | 4132 | return skb; |
2f1e85b1 TZ |
4133 | } |
4134 | ||
e420bed0 DB |
4135 | static __always_inline struct sk_buff * |
4136 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
2f1e85b1 | 4137 | { |
e420bed0 | 4138 | return skb; |
2f1e85b1 | 4139 | } |
e420bed0 | 4140 | #endif /* CONFIG_NET_XGRESS */ |
1f211a1b | 4141 | |
fc9bab24 AN |
4142 | #ifdef CONFIG_XPS |
4143 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, | |
4144 | struct xps_dev_maps *dev_maps, unsigned int tci) | |
4145 | { | |
255c04a8 | 4146 | int tc = netdev_get_prio_tc_map(dev, skb->priority); |
fc9bab24 AN |
4147 | struct xps_map *map; |
4148 | int queue_index = -1; | |
4149 | ||
5478fcd0 | 4150 | if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids) |
255c04a8 AT |
4151 | return queue_index; |
4152 | ||
4153 | tci *= dev_maps->num_tc; | |
4154 | tci += tc; | |
fc9bab24 AN |
4155 | |
4156 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
4157 | if (map) { | |
4158 | if (map->len == 1) | |
4159 | queue_index = map->queues[0]; | |
4160 | else | |
4161 | queue_index = map->queues[reciprocal_scale( | |
4162 | skb_get_hash(skb), map->len)]; | |
4163 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
4164 | queue_index = -1; | |
4165 | } | |
4166 | return queue_index; | |
4167 | } | |
4168 | #endif | |
4169 | ||
eadec877 AD |
4170 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
4171 | struct sk_buff *skb) | |
638b2a69 JP |
4172 | { |
4173 | #ifdef CONFIG_XPS | |
4174 | struct xps_dev_maps *dev_maps; | |
fc9bab24 | 4175 | struct sock *sk = skb->sk; |
638b2a69 JP |
4176 | int queue_index = -1; |
4177 | ||
04157469 AN |
4178 | if (!static_key_false(&xps_needed)) |
4179 | return -1; | |
4180 | ||
638b2a69 | 4181 | rcu_read_lock(); |
fc9bab24 AN |
4182 | if (!static_key_false(&xps_rxqs_needed)) |
4183 | goto get_cpus_map; | |
4184 | ||
044ab86d | 4185 | dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]); |
638b2a69 | 4186 | if (dev_maps) { |
fc9bab24 | 4187 | int tci = sk_rx_queue_get(sk); |
184c449f | 4188 | |
5478fcd0 | 4189 | if (tci >= 0) |
fc9bab24 AN |
4190 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, |
4191 | tci); | |
4192 | } | |
184c449f | 4193 | |
fc9bab24 AN |
4194 | get_cpus_map: |
4195 | if (queue_index < 0) { | |
044ab86d | 4196 | dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]); |
fc9bab24 AN |
4197 | if (dev_maps) { |
4198 | unsigned int tci = skb->sender_cpu - 1; | |
4199 | ||
4200 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
4201 | tci); | |
638b2a69 JP |
4202 | } |
4203 | } | |
4204 | rcu_read_unlock(); | |
4205 | ||
4206 | return queue_index; | |
4207 | #else | |
4208 | return -1; | |
4209 | #endif | |
4210 | } | |
4211 | ||
a4ea8a3d | 4212 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 4213 | struct net_device *sb_dev) |
a4ea8a3d AD |
4214 | { |
4215 | return 0; | |
4216 | } | |
4217 | EXPORT_SYMBOL(dev_pick_tx_zero); | |
4218 | ||
4219 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, | |
a350ecce | 4220 | struct net_device *sb_dev) |
a4ea8a3d AD |
4221 | { |
4222 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; | |
4223 | } | |
4224 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); | |
4225 | ||
b71b5837 PA |
4226 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
4227 | struct net_device *sb_dev) | |
638b2a69 JP |
4228 | { |
4229 | struct sock *sk = skb->sk; | |
4230 | int queue_index = sk_tx_queue_get(sk); | |
4231 | ||
eadec877 AD |
4232 | sb_dev = sb_dev ? : dev; |
4233 | ||
638b2a69 JP |
4234 | if (queue_index < 0 || skb->ooo_okay || |
4235 | queue_index >= dev->real_num_tx_queues) { | |
eadec877 | 4236 | int new_index = get_xps_queue(dev, sb_dev, skb); |
f4563a75 | 4237 | |
638b2a69 | 4238 | if (new_index < 0) |
eadec877 | 4239 | new_index = skb_tx_hash(dev, sb_dev, skb); |
638b2a69 JP |
4240 | |
4241 | if (queue_index != new_index && sk && | |
004a5d01 | 4242 | sk_fullsock(sk) && |
638b2a69 JP |
4243 | rcu_access_pointer(sk->sk_dst_cache)) |
4244 | sk_tx_queue_set(sk, new_index); | |
4245 | ||
4246 | queue_index = new_index; | |
4247 | } | |
4248 | ||
4249 | return queue_index; | |
4250 | } | |
b71b5837 | 4251 | EXPORT_SYMBOL(netdev_pick_tx); |
638b2a69 | 4252 | |
4bd97d51 PA |
4253 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
4254 | struct sk_buff *skb, | |
4255 | struct net_device *sb_dev) | |
638b2a69 JP |
4256 | { |
4257 | int queue_index = 0; | |
4258 | ||
4259 | #ifdef CONFIG_XPS | |
52bd2d62 ED |
4260 | u32 sender_cpu = skb->sender_cpu - 1; |
4261 | ||
4262 | if (sender_cpu >= (u32)NR_CPUS) | |
638b2a69 JP |
4263 | skb->sender_cpu = raw_smp_processor_id() + 1; |
4264 | #endif | |
4265 | ||
4266 | if (dev->real_num_tx_queues != 1) { | |
4267 | const struct net_device_ops *ops = dev->netdev_ops; | |
f4563a75 | 4268 | |
638b2a69 | 4269 | if (ops->ndo_select_queue) |
a350ecce | 4270 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
638b2a69 | 4271 | else |
4bd97d51 | 4272 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
638b2a69 | 4273 | |
d584527c | 4274 | queue_index = netdev_cap_txqueue(dev, queue_index); |
638b2a69 JP |
4275 | } |
4276 | ||
4277 | skb_set_queue_mapping(skb, queue_index); | |
4278 | return netdev_get_tx_queue(dev, queue_index); | |
4279 | } | |
4280 | ||
d29f749e | 4281 | /** |
be76955d JK |
4282 | * __dev_queue_xmit() - transmit a buffer |
4283 | * @skb: buffer to transmit | |
4284 | * @sb_dev: suboordinate device used for L2 forwarding offload | |
d29f749e | 4285 | * |
be76955d JK |
4286 | * Queue a buffer for transmission to a network device. The caller must |
4287 | * have set the device and priority and built the buffer before calling | |
4288 | * this function. The function can be called from an interrupt. | |
d29f749e | 4289 | * |
be76955d JK |
4290 | * When calling this method, interrupts MUST be enabled. This is because |
4291 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
d29f749e | 4292 | * |
be76955d JK |
4293 | * Regardless of the return value, the skb is consumed, so it is currently |
4294 | * difficult to retry a send to this method. (You can bump the ref count | |
4295 | * before sending to hold a reference for retry if you are careful.) | |
d29f749e | 4296 | * |
be76955d JK |
4297 | * Return: |
4298 | * * 0 - buffer successfully transmitted | |
4299 | * * positive qdisc return code - NET_XMIT_DROP etc. | |
4300 | * * negative errno - other errors | |
d29f749e | 4301 | */ |
c526fd8f | 4302 | int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
1da177e4 LT |
4303 | { |
4304 | struct net_device *dev = skb->dev; | |
2f1e85b1 | 4305 | struct netdev_queue *txq = NULL; |
1da177e4 LT |
4306 | struct Qdisc *q; |
4307 | int rc = -ENOMEM; | |
f53c7239 | 4308 | bool again = false; |
1da177e4 | 4309 | |
6d1ccff6 | 4310 | skb_reset_mac_header(skb); |
fd189422 | 4311 | skb_assert_len(skb); |
6d1ccff6 | 4312 | |
e7fd2885 | 4313 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
e7ed11ee | 4314 | __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED); |
e7fd2885 | 4315 | |
4ec93edb YH |
4316 | /* Disable soft irqs for various locks below. Also |
4317 | * stops preemption for RCU. | |
1da177e4 | 4318 | */ |
4ec93edb | 4319 | rcu_read_lock_bh(); |
1da177e4 | 4320 | |
5bc1421e NH |
4321 | skb_update_prio(skb); |
4322 | ||
1f211a1b | 4323 | qdisc_pkt_len_init(skb); |
e420bed0 | 4324 | tcx_set_ingress(skb, false); |
42df6e1d | 4325 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 4326 | if (static_branch_unlikely(&egress_needed_key)) { |
42df6e1d LW |
4327 | if (nf_hook_egress_active()) { |
4328 | skb = nf_hook_egress(skb, &rc, dev); | |
4329 | if (!skb) | |
4330 | goto out; | |
4331 | } | |
2f1e85b1 TZ |
4332 | |
4333 | netdev_xmit_skip_txqueue(false); | |
4334 | ||
42df6e1d | 4335 | nf_skip_egress(skb, true); |
1f211a1b DB |
4336 | skb = sch_handle_egress(skb, &rc, dev); |
4337 | if (!skb) | |
4338 | goto out; | |
42df6e1d | 4339 | nf_skip_egress(skb, false); |
2f1e85b1 TZ |
4340 | |
4341 | if (netdev_xmit_txqueue_skipped()) | |
4342 | txq = netdev_tx_queue_mapping(dev, skb); | |
1f211a1b | 4343 | } |
1f211a1b | 4344 | #endif |
02875878 ED |
4345 | /* If device/qdisc don't need skb->dst, release it right now while |
4346 | * its hot in this cpu cache. | |
4347 | */ | |
4348 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
4349 | skb_dst_drop(skb); | |
4350 | else | |
4351 | skb_dst_force(skb); | |
4352 | ||
2f1e85b1 TZ |
4353 | if (!txq) |
4354 | txq = netdev_core_pick_tx(dev, skb, sb_dev); | |
4355 | ||
a898def2 | 4356 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 4357 | |
cf66ba58 | 4358 | trace_net_dev_queue(skb); |
1da177e4 | 4359 | if (q->enqueue) { |
bbd8a0d3 | 4360 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 4361 | goto out; |
1da177e4 LT |
4362 | } |
4363 | ||
4364 | /* The device has no queue. Common case for software devices: | |
eb13da1a | 4365 | * loopback, all the sorts of tunnels... |
1da177e4 | 4366 | |
eb13da1a | 4367 | * Really, it is unlikely that netif_tx_lock protection is necessary |
4368 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
4369 | * counters.) | |
4370 | * However, it is possible, that they rely on protection | |
4371 | * made by us here. | |
1da177e4 | 4372 | |
eb13da1a | 4373 | * Check this and shot the lock. It is not prone from deadlocks. |
4374 | *Either shot noqueue qdisc, it is even simpler 8) | |
1da177e4 LT |
4375 | */ |
4376 | if (dev->flags & IFF_UP) { | |
4377 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
4378 | ||
7a10d8c8 ED |
4379 | /* Other cpus might concurrently change txq->xmit_lock_owner |
4380 | * to -1 or to their cpu id, but not to our id. | |
4381 | */ | |
4382 | if (READ_ONCE(txq->xmit_lock_owner) != cpu) { | |
97cdcf37 | 4383 | if (dev_xmit_recursion()) |
745e20f1 ED |
4384 | goto recursion_alert; |
4385 | ||
f53c7239 | 4386 | skb = validate_xmit_skb(skb, dev, &again); |
1f59533f | 4387 | if (!skb) |
d21fd63e | 4388 | goto out; |
1f59533f | 4389 | |
c773e847 | 4390 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 4391 | |
73466498 | 4392 | if (!netif_xmit_stopped(txq)) { |
97cdcf37 | 4393 | dev_xmit_recursion_inc(); |
ce93718f | 4394 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
97cdcf37 | 4395 | dev_xmit_recursion_dec(); |
572a9d7b | 4396 | if (dev_xmit_complete(rc)) { |
c773e847 | 4397 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
4398 | goto out; |
4399 | } | |
4400 | } | |
c773e847 | 4401 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
4402 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
4403 | dev->name); | |
1da177e4 LT |
4404 | } else { |
4405 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
4406 | * unfortunately |
4407 | */ | |
4408 | recursion_alert: | |
e87cc472 JP |
4409 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
4410 | dev->name); | |
1da177e4 LT |
4411 | } |
4412 | } | |
4413 | ||
4414 | rc = -ENETDOWN; | |
d4828d85 | 4415 | rcu_read_unlock_bh(); |
1da177e4 | 4416 | |
625788b5 | 4417 | dev_core_stats_tx_dropped_inc(dev); |
1f59533f | 4418 | kfree_skb_list(skb); |
1da177e4 LT |
4419 | return rc; |
4420 | out: | |
d4828d85 | 4421 | rcu_read_unlock_bh(); |
1da177e4 LT |
4422 | return rc; |
4423 | } | |
c526fd8f | 4424 | EXPORT_SYMBOL(__dev_queue_xmit); |
f663dd9a | 4425 | |
36ccdf85 | 4426 | int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
865b03f2 MK |
4427 | { |
4428 | struct net_device *dev = skb->dev; | |
4429 | struct sk_buff *orig_skb = skb; | |
4430 | struct netdev_queue *txq; | |
4431 | int ret = NETDEV_TX_BUSY; | |
4432 | bool again = false; | |
4433 | ||
4434 | if (unlikely(!netif_running(dev) || | |
4435 | !netif_carrier_ok(dev))) | |
4436 | goto drop; | |
4437 | ||
4438 | skb = validate_xmit_skb_list(skb, dev, &again); | |
4439 | if (skb != orig_skb) | |
4440 | goto drop; | |
4441 | ||
4442 | skb_set_queue_mapping(skb, queue_id); | |
4443 | txq = skb_get_tx_queue(dev, skb); | |
4444 | ||
4445 | local_bh_disable(); | |
4446 | ||
0ad6f6e7 | 4447 | dev_xmit_recursion_inc(); |
865b03f2 MK |
4448 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
4449 | if (!netif_xmit_frozen_or_drv_stopped(txq)) | |
4450 | ret = netdev_start_xmit(skb, dev, txq, false); | |
4451 | HARD_TX_UNLOCK(dev, txq); | |
0ad6f6e7 | 4452 | dev_xmit_recursion_dec(); |
865b03f2 MK |
4453 | |
4454 | local_bh_enable(); | |
865b03f2 MK |
4455 | return ret; |
4456 | drop: | |
625788b5 | 4457 | dev_core_stats_tx_dropped_inc(dev); |
865b03f2 MK |
4458 | kfree_skb_list(skb); |
4459 | return NET_XMIT_DROP; | |
4460 | } | |
36ccdf85 | 4461 | EXPORT_SYMBOL(__dev_direct_xmit); |
1da177e4 | 4462 | |
eb13da1a | 4463 | /************************************************************************* |
4464 | * Receiver routines | |
4465 | *************************************************************************/ | |
dad6b977 | 4466 | static DEFINE_PER_CPU(struct task_struct *, backlog_napi); |
1da177e4 | 4467 | |
3d48b53f MT |
4468 | int weight_p __read_mostly = 64; /* old backlog weight */ |
4469 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ | |
4470 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ | |
1da177e4 | 4471 | |
eecfd7c4 ED |
4472 | /* Called with irq disabled */ |
4473 | static inline void ____napi_schedule(struct softnet_data *sd, | |
4474 | struct napi_struct *napi) | |
4475 | { | |
29863d41 WW |
4476 | struct task_struct *thread; |
4477 | ||
fbd9a2ce SAS |
4478 | lockdep_assert_irqs_disabled(); |
4479 | ||
29863d41 WW |
4480 | if (test_bit(NAPI_STATE_THREADED, &napi->state)) { |
4481 | /* Paired with smp_mb__before_atomic() in | |
5fdd2f0e WW |
4482 | * napi_enable()/dev_set_threaded(). |
4483 | * Use READ_ONCE() to guarantee a complete | |
4484 | * read on napi->thread. Only call | |
29863d41 WW |
4485 | * wake_up_process() when it's not NULL. |
4486 | */ | |
4487 | thread = READ_ONCE(napi->thread); | |
4488 | if (thread) { | |
dad6b977 SAS |
4489 | if (use_backlog_threads() && thread == raw_cpu_read(backlog_napi)) |
4490 | goto use_local_napi; | |
4491 | ||
56364c91 | 4492 | set_bit(NAPI_STATE_SCHED_THREADED, &napi->state); |
29863d41 WW |
4493 | wake_up_process(thread); |
4494 | return; | |
4495 | } | |
4496 | } | |
4497 | ||
dad6b977 | 4498 | use_local_napi: |
eecfd7c4 | 4499 | list_add_tail(&napi->poll_list, &sd->poll_list); |
8c48eea3 | 4500 | WRITE_ONCE(napi->list_owner, smp_processor_id()); |
8b43fd3d ED |
4501 | /* If not called from net_rx_action() |
4502 | * we have to raise NET_RX_SOFTIRQ. | |
4503 | */ | |
4504 | if (!sd->in_net_rx_action) | |
4505 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
eecfd7c4 ED |
4506 | } |
4507 | ||
bfb564e7 KK |
4508 | #ifdef CONFIG_RPS |
4509 | ||
dc05360f | 4510 | struct static_key_false rps_needed __read_mostly; |
3df97ba8 | 4511 | EXPORT_SYMBOL(rps_needed); |
dc05360f | 4512 | struct static_key_false rfs_needed __read_mostly; |
13bfff25 | 4513 | EXPORT_SYMBOL(rfs_needed); |
adc9300e | 4514 | |
c445477d BH |
4515 | static struct rps_dev_flow * |
4516 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4517 | struct rps_dev_flow *rflow, u16 next_cpu) | |
4518 | { | |
a31196b0 | 4519 | if (next_cpu < nr_cpu_ids) { |
c445477d BH |
4520 | #ifdef CONFIG_RFS_ACCEL |
4521 | struct netdev_rx_queue *rxqueue; | |
4522 | struct rps_dev_flow_table *flow_table; | |
4523 | struct rps_dev_flow *old_rflow; | |
84b6823c | 4524 | u32 flow_id, head; |
c445477d BH |
4525 | u16 rxq_index; |
4526 | int rc; | |
4527 | ||
4528 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
4529 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
4530 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
4531 | goto out; |
4532 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
4533 | if (rxq_index == skb_get_rx_queue(skb)) | |
4534 | goto out; | |
4535 | ||
4536 | rxqueue = dev->_rx + rxq_index; | |
4537 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4538 | if (!flow_table) | |
4539 | goto out; | |
61b905da | 4540 | flow_id = skb_get_hash(skb) & flow_table->mask; |
c445477d BH |
4541 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
4542 | rxq_index, flow_id); | |
4543 | if (rc < 0) | |
4544 | goto out; | |
4545 | old_rflow = rflow; | |
4546 | rflow = &flow_table->flows[flow_id]; | |
f00bf5dc JX |
4547 | WRITE_ONCE(rflow->filter, rc); |
4548 | if (old_rflow->filter == rc) | |
4549 | WRITE_ONCE(old_rflow->filter, RPS_NO_FILTER); | |
c445477d BH |
4550 | out: |
4551 | #endif | |
84b6823c JX |
4552 | head = READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head); |
4553 | rps_input_queue_tail_save(&rflow->last_qtail, head); | |
c445477d BH |
4554 | } |
4555 | ||
f7b60cce | 4556 | WRITE_ONCE(rflow->cpu, next_cpu); |
c445477d BH |
4557 | return rflow; |
4558 | } | |
4559 | ||
bfb564e7 KK |
4560 | /* |
4561 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
4562 | * CPU from the RPS map of the receiving queue for a given skb. | |
4563 | * rcu_read_lock must be held on entry. | |
4564 | */ | |
4565 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4566 | struct rps_dev_flow **rflowp) | |
4567 | { | |
567e4b79 ED |
4568 | const struct rps_sock_flow_table *sock_flow_table; |
4569 | struct netdev_rx_queue *rxqueue = dev->_rx; | |
bfb564e7 | 4570 | struct rps_dev_flow_table *flow_table; |
567e4b79 | 4571 | struct rps_map *map; |
bfb564e7 | 4572 | int cpu = -1; |
567e4b79 | 4573 | u32 tcpu; |
61b905da | 4574 | u32 hash; |
bfb564e7 KK |
4575 | |
4576 | if (skb_rx_queue_recorded(skb)) { | |
4577 | u16 index = skb_get_rx_queue(skb); | |
567e4b79 | 4578 | |
62fe0b40 BH |
4579 | if (unlikely(index >= dev->real_num_rx_queues)) { |
4580 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4581 | "%s received packet on queue %u, but number " | |
4582 | "of RX queues is %u\n", | |
4583 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
4584 | goto done; |
4585 | } | |
567e4b79 ED |
4586 | rxqueue += index; |
4587 | } | |
bfb564e7 | 4588 | |
567e4b79 ED |
4589 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
4590 | ||
4591 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
6e3f7faf | 4592 | map = rcu_dereference(rxqueue->rps_map); |
567e4b79 | 4593 | if (!flow_table && !map) |
bfb564e7 KK |
4594 | goto done; |
4595 | ||
2d47b459 | 4596 | skb_reset_network_header(skb); |
61b905da TH |
4597 | hash = skb_get_hash(skb); |
4598 | if (!hash) | |
bfb564e7 KK |
4599 | goto done; |
4600 | ||
ce7f49ab | 4601 | sock_flow_table = rcu_dereference(net_hotdata.rps_sock_flow_table); |
fec5e652 | 4602 | if (flow_table && sock_flow_table) { |
fec5e652 | 4603 | struct rps_dev_flow *rflow; |
567e4b79 ED |
4604 | u32 next_cpu; |
4605 | u32 ident; | |
4606 | ||
5c3b74a9 ED |
4607 | /* First check into global flow table if there is a match. |
4608 | * This READ_ONCE() pairs with WRITE_ONCE() from rps_record_sock_flow(). | |
4609 | */ | |
4610 | ident = READ_ONCE(sock_flow_table->ents[hash & sock_flow_table->mask]); | |
ce7f49ab | 4611 | if ((ident ^ hash) & ~net_hotdata.rps_cpu_mask) |
567e4b79 | 4612 | goto try_rps; |
fec5e652 | 4613 | |
ce7f49ab | 4614 | next_cpu = ident & net_hotdata.rps_cpu_mask; |
567e4b79 ED |
4615 | |
4616 | /* OK, now we know there is a match, | |
4617 | * we can look at the local (per receive queue) flow table | |
4618 | */ | |
61b905da | 4619 | rflow = &flow_table->flows[hash & flow_table->mask]; |
fec5e652 TH |
4620 | tcpu = rflow->cpu; |
4621 | ||
fec5e652 TH |
4622 | /* |
4623 | * If the desired CPU (where last recvmsg was done) is | |
4624 | * different from current CPU (one in the rx-queue flow | |
4625 | * table entry), switch if one of the following holds: | |
a31196b0 | 4626 | * - Current CPU is unset (>= nr_cpu_ids). |
fec5e652 TH |
4627 | * - Current CPU is offline. |
4628 | * - The current CPU's queue tail has advanced beyond the | |
4629 | * last packet that was enqueued using this table entry. | |
4630 | * This guarantees that all previous packets for the flow | |
4631 | * have been dequeued, thus preserving in order delivery. | |
4632 | */ | |
4633 | if (unlikely(tcpu != next_cpu) && | |
a31196b0 | 4634 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
c62fdf5b | 4635 | ((int)(READ_ONCE(per_cpu(softnet_data, tcpu).input_queue_head) - |
84b6823c | 4636 | rflow->last_qtail)) >= 0)) { |
baefa31d | 4637 | tcpu = next_cpu; |
c445477d | 4638 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 4639 | } |
c445477d | 4640 | |
a31196b0 | 4641 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
fec5e652 TH |
4642 | *rflowp = rflow; |
4643 | cpu = tcpu; | |
4644 | goto done; | |
4645 | } | |
4646 | } | |
4647 | ||
567e4b79 ED |
4648 | try_rps: |
4649 | ||
0a9627f2 | 4650 | if (map) { |
8fc54f68 | 4651 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
0a9627f2 TH |
4652 | if (cpu_online(tcpu)) { |
4653 | cpu = tcpu; | |
4654 | goto done; | |
4655 | } | |
4656 | } | |
4657 | ||
4658 | done: | |
0a9627f2 TH |
4659 | return cpu; |
4660 | } | |
4661 | ||
c445477d BH |
4662 | #ifdef CONFIG_RFS_ACCEL |
4663 | ||
4664 | /** | |
4665 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
4666 | * @dev: Device on which the filter was set | |
4667 | * @rxq_index: RX queue index | |
4668 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
4669 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
4670 | * | |
4671 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
4672 | * this function for each installed filter and remove the filters for | |
4673 | * which it returns %true. | |
4674 | */ | |
4675 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
4676 | u32 flow_id, u16 filter_id) | |
4677 | { | |
4678 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
4679 | struct rps_dev_flow_table *flow_table; | |
4680 | struct rps_dev_flow *rflow; | |
4681 | bool expire = true; | |
a31196b0 | 4682 | unsigned int cpu; |
c445477d BH |
4683 | |
4684 | rcu_read_lock(); | |
4685 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4686 | if (flow_table && flow_id <= flow_table->mask) { | |
4687 | rflow = &flow_table->flows[flow_id]; | |
6aa7de05 | 4688 | cpu = READ_ONCE(rflow->cpu); |
f00bf5dc | 4689 | if (READ_ONCE(rflow->filter) == filter_id && cpu < nr_cpu_ids && |
c62fdf5b | 4690 | ((int)(READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head) - |
36b83ffc | 4691 | READ_ONCE(rflow->last_qtail)) < |
c445477d BH |
4692 | (int)(10 * flow_table->mask))) |
4693 | expire = false; | |
4694 | } | |
4695 | rcu_read_unlock(); | |
4696 | return expire; | |
4697 | } | |
4698 | EXPORT_SYMBOL(rps_may_expire_flow); | |
4699 | ||
4700 | #endif /* CONFIG_RFS_ACCEL */ | |
4701 | ||
0a9627f2 | 4702 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 4703 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 4704 | { |
e36fa2f7 ED |
4705 | struct softnet_data *sd = data; |
4706 | ||
eecfd7c4 | 4707 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 4708 | sd->received_rps++; |
0a9627f2 | 4709 | } |
e36fa2f7 | 4710 | |
fec5e652 | 4711 | #endif /* CONFIG_RPS */ |
0a9627f2 | 4712 | |
68822bdf | 4713 | /* Called from hardirq (IPI) context */ |
97e719a8 | 4714 | static void trigger_rx_softirq(void *data) |
68822bdf | 4715 | { |
97e719a8 ED |
4716 | struct softnet_data *sd = data; |
4717 | ||
68822bdf | 4718 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
97e719a8 | 4719 | smp_store_release(&sd->defer_ipi_scheduled, 0); |
68822bdf ED |
4720 | } |
4721 | ||
e36fa2f7 | 4722 | /* |
8fcb76b9 ED |
4723 | * After we queued a packet into sd->input_pkt_queue, |
4724 | * we need to make sure this queue is serviced soon. | |
4725 | * | |
4726 | * - If this is another cpu queue, link it to our rps_ipi_list, | |
4727 | * and make sure we will process rps_ipi_list from net_rx_action(). | |
8fcb76b9 ED |
4728 | * |
4729 | * - If this is our own queue, NAPI schedule our backlog. | |
4730 | * Note that this also raises NET_RX_SOFTIRQ. | |
e36fa2f7 | 4731 | */ |
8fcb76b9 | 4732 | static void napi_schedule_rps(struct softnet_data *sd) |
e36fa2f7 | 4733 | { |
903ceff7 | 4734 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
e36fa2f7 | 4735 | |
e722db8d | 4736 | #ifdef CONFIG_RPS |
e36fa2f7 | 4737 | if (sd != mysd) { |
dad6b977 SAS |
4738 | if (use_backlog_threads()) { |
4739 | __napi_schedule_irqoff(&sd->backlog); | |
4740 | return; | |
4741 | } | |
4742 | ||
e36fa2f7 ED |
4743 | sd->rps_ipi_next = mysd->rps_ipi_list; |
4744 | mysd->rps_ipi_list = sd; | |
4745 | ||
87eff2ec | 4746 | /* If not called from net_rx_action() or napi_threaded_poll() |
821eba96 ED |
4747 | * we have to raise NET_RX_SOFTIRQ. |
4748 | */ | |
87eff2ec | 4749 | if (!mysd->in_net_rx_action && !mysd->in_napi_threaded_poll) |
821eba96 | 4750 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
8fcb76b9 | 4751 | return; |
e36fa2f7 ED |
4752 | } |
4753 | #endif /* CONFIG_RPS */ | |
e722db8d | 4754 | __napi_schedule_irqoff(&mysd->backlog); |
e36fa2f7 ED |
4755 | } |
4756 | ||
80d2eefc SAS |
4757 | void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu) |
4758 | { | |
4759 | unsigned long flags; | |
4760 | ||
4761 | if (use_backlog_threads()) { | |
765b11f8 | 4762 | backlog_lock_irq_save(sd, &flags); |
80d2eefc SAS |
4763 | |
4764 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) | |
4765 | __napi_schedule_irqoff(&sd->backlog); | |
4766 | ||
765b11f8 | 4767 | backlog_unlock_irq_restore(sd, &flags); |
80d2eefc SAS |
4768 | |
4769 | } else if (!cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) { | |
4770 | smp_call_function_single_async(cpu, &sd->defer_csd); | |
4771 | } | |
4772 | } | |
4773 | ||
99bbc707 WB |
4774 | #ifdef CONFIG_NET_FLOW_LIMIT |
4775 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); | |
4776 | #endif | |
4777 | ||
4778 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) | |
4779 | { | |
4780 | #ifdef CONFIG_NET_FLOW_LIMIT | |
4781 | struct sd_flow_limit *fl; | |
4782 | struct softnet_data *sd; | |
4783 | unsigned int old_flow, new_flow; | |
4784 | ||
edbc666c | 4785 | if (qlen < (READ_ONCE(net_hotdata.max_backlog) >> 1)) |
99bbc707 WB |
4786 | return false; |
4787 | ||
903ceff7 | 4788 | sd = this_cpu_ptr(&softnet_data); |
99bbc707 WB |
4789 | |
4790 | rcu_read_lock(); | |
4791 | fl = rcu_dereference(sd->flow_limit); | |
4792 | if (fl) { | |
3958afa1 | 4793 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
99bbc707 WB |
4794 | old_flow = fl->history[fl->history_head]; |
4795 | fl->history[fl->history_head] = new_flow; | |
4796 | ||
4797 | fl->history_head++; | |
4798 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; | |
4799 | ||
4800 | if (likely(fl->buckets[old_flow])) | |
4801 | fl->buckets[old_flow]--; | |
4802 | ||
4803 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { | |
4804 | fl->count++; | |
4805 | rcu_read_unlock(); | |
4806 | return true; | |
4807 | } | |
4808 | } | |
4809 | rcu_read_unlock(); | |
4810 | #endif | |
4811 | return false; | |
4812 | } | |
4813 | ||
0a9627f2 TH |
4814 | /* |
4815 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
4816 | * queue (may be a remote CPU queue). | |
4817 | */ | |
fec5e652 TH |
4818 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
4819 | unsigned int *qtail) | |
0a9627f2 | 4820 | { |
44f0bd40 | 4821 | enum skb_drop_reason reason; |
e36fa2f7 | 4822 | struct softnet_data *sd; |
0a9627f2 | 4823 | unsigned long flags; |
99bbc707 | 4824 | unsigned int qlen; |
a7ae7b0b | 4825 | int max_backlog; |
36b83ffc | 4826 | u32 tail; |
0a9627f2 | 4827 | |
95e48d86 ED |
4828 | reason = SKB_DROP_REASON_DEV_READY; |
4829 | if (!netif_running(skb->dev)) | |
4830 | goto bad_dev; | |
4831 | ||
a7ae7b0b | 4832 | reason = SKB_DROP_REASON_CPU_BACKLOG; |
e36fa2f7 | 4833 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 | 4834 | |
a7ae7b0b ED |
4835 | qlen = skb_queue_len_lockless(&sd->input_pkt_queue); |
4836 | max_backlog = READ_ONCE(net_hotdata.max_backlog); | |
4837 | if (unlikely(qlen > max_backlog)) | |
4838 | goto cpu_backlog_drop; | |
765b11f8 | 4839 | backlog_lock_irq_save(sd, &flags); |
99bbc707 | 4840 | qlen = skb_queue_len(&sd->input_pkt_queue); |
a7ae7b0b | 4841 | if (qlen <= max_backlog && !skb_flow_limit(skb, qlen)) { |
f7efd01f ED |
4842 | if (!qlen) { |
4843 | /* Schedule NAPI for backlog device. We can use | |
4844 | * non atomic operation as we own the queue lock. | |
4845 | */ | |
4846 | if (!__test_and_set_bit(NAPI_STATE_SCHED, | |
4847 | &sd->backlog.state)) | |
4848 | napi_schedule_rps(sd); | |
0a9627f2 | 4849 | } |
f7efd01f | 4850 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
36b83ffc | 4851 | tail = rps_input_queue_tail_incr(sd); |
f7efd01f | 4852 | backlog_unlock_irq_restore(sd, &flags); |
36b83ffc ED |
4853 | |
4854 | /* save the tail outside of the critical section */ | |
4855 | rps_input_queue_tail_save(qtail, tail); | |
f7efd01f | 4856 | return NET_RX_SUCCESS; |
0a9627f2 TH |
4857 | } |
4858 | ||
765b11f8 | 4859 | backlog_unlock_irq_restore(sd, &flags); |
0a9627f2 | 4860 | |
a7ae7b0b ED |
4861 | cpu_backlog_drop: |
4862 | atomic_inc(&sd->dropped); | |
95e48d86 | 4863 | bad_dev: |
625788b5 | 4864 | dev_core_stats_rx_dropped_inc(skb->dev); |
44f0bd40 | 4865 | kfree_skb_reason(skb, reason); |
0a9627f2 TH |
4866 | return NET_RX_DROP; |
4867 | } | |
1da177e4 | 4868 | |
e817f856 JDB |
4869 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
4870 | { | |
4871 | struct net_device *dev = skb->dev; | |
4872 | struct netdev_rx_queue *rxqueue; | |
4873 | ||
4874 | rxqueue = dev->_rx; | |
4875 | ||
4876 | if (skb_rx_queue_recorded(skb)) { | |
4877 | u16 index = skb_get_rx_queue(skb); | |
4878 | ||
4879 | if (unlikely(index >= dev->real_num_rx_queues)) { | |
4880 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4881 | "%s received packet on queue %u, but number " | |
4882 | "of RX queues is %u\n", | |
4883 | dev->name, index, dev->real_num_rx_queues); | |
4884 | ||
4885 | return rxqueue; /* Return first rxqueue */ | |
4886 | } | |
4887 | rxqueue += index; | |
4888 | } | |
4889 | return rxqueue; | |
4890 | } | |
4891 | ||
fe21cb91 KKD |
4892 | u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp, |
4893 | struct bpf_prog *xdp_prog) | |
d4455169 | 4894 | { |
be9df4af | 4895 | void *orig_data, *orig_data_end, *hard_start; |
e817f856 | 4896 | struct netdev_rx_queue *rxqueue; |
22b60343 | 4897 | bool orig_bcast, orig_host; |
43b5169d | 4898 | u32 mac_len, frame_sz; |
29724956 JDB |
4899 | __be16 orig_eth_type; |
4900 | struct ethhdr *eth; | |
fe21cb91 | 4901 | u32 metalen, act; |
be9df4af | 4902 | int off; |
d4455169 | 4903 | |
d4455169 JF |
4904 | /* The XDP program wants to see the packet starting at the MAC |
4905 | * header. | |
4906 | */ | |
4907 | mac_len = skb->data - skb_mac_header(skb); | |
be9df4af | 4908 | hard_start = skb->data - skb_headroom(skb); |
a075767b JDB |
4909 | |
4910 | /* SKB "head" area always have tailroom for skb_shared_info */ | |
be9df4af | 4911 | frame_sz = (void *)skb_end_pointer(skb) - hard_start; |
43b5169d | 4912 | frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
a075767b | 4913 | |
be9df4af LB |
4914 | rxqueue = netif_get_rxqueue(skb); |
4915 | xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq); | |
4916 | xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len, | |
4917 | skb_headlen(skb) + mac_len, true); | |
e6d5dbdd LB |
4918 | if (skb_is_nonlinear(skb)) { |
4919 | skb_shinfo(skb)->xdp_frags_size = skb->data_len; | |
4920 | xdp_buff_set_frags_flag(xdp); | |
4921 | } else { | |
4922 | xdp_buff_clear_frags_flag(xdp); | |
4923 | } | |
a075767b | 4924 | |
02671e23 BT |
4925 | orig_data_end = xdp->data_end; |
4926 | orig_data = xdp->data; | |
29724956 | 4927 | eth = (struct ethhdr *)xdp->data; |
22b60343 | 4928 | orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr); |
29724956 JDB |
4929 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); |
4930 | orig_eth_type = eth->h_proto; | |
d4455169 | 4931 | |
02671e23 | 4932 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
d4455169 | 4933 | |
065af355 | 4934 | /* check if bpf_xdp_adjust_head was used */ |
02671e23 | 4935 | off = xdp->data - orig_data; |
065af355 JDB |
4936 | if (off) { |
4937 | if (off > 0) | |
4938 | __skb_pull(skb, off); | |
4939 | else if (off < 0) | |
4940 | __skb_push(skb, -off); | |
4941 | ||
4942 | skb->mac_header += off; | |
4943 | skb_reset_network_header(skb); | |
4944 | } | |
d4455169 | 4945 | |
a075767b JDB |
4946 | /* check if bpf_xdp_adjust_tail was used */ |
4947 | off = xdp->data_end - orig_data_end; | |
f7613120 | 4948 | if (off != 0) { |
02671e23 | 4949 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
a075767b | 4950 | skb->len += off; /* positive on grow, negative on shrink */ |
f7613120 | 4951 | } |
198d83bb | 4952 | |
e6d5dbdd LB |
4953 | /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers |
4954 | * (e.g. bpf_xdp_adjust_tail), we need to update data_len here. | |
4955 | */ | |
4956 | if (xdp_buff_has_frags(xdp)) | |
4957 | skb->data_len = skb_shinfo(skb)->xdp_frags_size; | |
4958 | else | |
4959 | skb->data_len = 0; | |
4960 | ||
29724956 JDB |
4961 | /* check if XDP changed eth hdr such SKB needs update */ |
4962 | eth = (struct ethhdr *)xdp->data; | |
4963 | if ((orig_eth_type != eth->h_proto) || | |
22b60343 MW |
4964 | (orig_host != ether_addr_equal_64bits(eth->h_dest, |
4965 | skb->dev->dev_addr)) || | |
29724956 JDB |
4966 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { |
4967 | __skb_push(skb, ETH_HLEN); | |
22b60343 | 4968 | skb->pkt_type = PACKET_HOST; |
29724956 JDB |
4969 | skb->protocol = eth_type_trans(skb, skb->dev); |
4970 | } | |
4971 | ||
fe21cb91 KKD |
4972 | /* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull |
4973 | * before calling us again on redirect path. We do not call do_redirect | |
4974 | * as we leave that up to the caller. | |
4975 | * | |
4976 | * Caller is responsible for managing lifetime of skb (i.e. calling | |
4977 | * kfree_skb in response to actions it cannot handle/XDP_DROP). | |
4978 | */ | |
d4455169 | 4979 | switch (act) { |
6103aa96 | 4980 | case XDP_REDIRECT: |
d4455169 JF |
4981 | case XDP_TX: |
4982 | __skb_push(skb, mac_len); | |
de8f3a83 | 4983 | break; |
d4455169 | 4984 | case XDP_PASS: |
02671e23 | 4985 | metalen = xdp->data - xdp->data_meta; |
de8f3a83 DB |
4986 | if (metalen) |
4987 | skb_metadata_set(skb, metalen); | |
d4455169 | 4988 | break; |
fe21cb91 KKD |
4989 | } |
4990 | ||
4991 | return act; | |
4992 | } | |
4993 | ||
e6d5dbdd LB |
4994 | static int |
4995 | netif_skb_check_for_xdp(struct sk_buff **pskb, struct bpf_prog *prog) | |
4996 | { | |
4997 | struct sk_buff *skb = *pskb; | |
4998 | int err, hroom, troom; | |
4999 | ||
5000 | if (!skb_cow_data_for_xdp(this_cpu_read(system_page_pool), pskb, prog)) | |
5001 | return 0; | |
5002 | ||
5003 | /* In case we have to go down the path and also linearize, | |
5004 | * then lets do the pskb_expand_head() work just once here. | |
5005 | */ | |
5006 | hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); | |
5007 | troom = skb->tail + skb->data_len - skb->end; | |
5008 | err = pskb_expand_head(skb, | |
5009 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, | |
5010 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC); | |
5011 | if (err) | |
5012 | return err; | |
5013 | ||
5014 | return skb_linearize(skb); | |
5015 | } | |
5016 | ||
4d2bb0bf | 5017 | static u32 netif_receive_generic_xdp(struct sk_buff **pskb, |
fe21cb91 KKD |
5018 | struct xdp_buff *xdp, |
5019 | struct bpf_prog *xdp_prog) | |
5020 | { | |
4d2bb0bf | 5021 | struct sk_buff *skb = *pskb; |
e6d5dbdd | 5022 | u32 mac_len, act = XDP_DROP; |
fe21cb91 KKD |
5023 | |
5024 | /* Reinjected packets coming from act_mirred or similar should | |
5025 | * not get XDP generic processing. | |
5026 | */ | |
5027 | if (skb_is_redirected(skb)) | |
5028 | return XDP_PASS; | |
5029 | ||
e6d5dbdd LB |
5030 | /* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM |
5031 | * bytes. This is the guarantee that also native XDP provides, | |
5032 | * thus we need to do it here as well. | |
fe21cb91 | 5033 | */ |
e6d5dbdd LB |
5034 | mac_len = skb->data - skb_mac_header(skb); |
5035 | __skb_push(skb, mac_len); | |
5036 | ||
fe21cb91 KKD |
5037 | if (skb_cloned(skb) || skb_is_nonlinear(skb) || |
5038 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { | |
e6d5dbdd | 5039 | if (netif_skb_check_for_xdp(pskb, xdp_prog)) |
fe21cb91 KKD |
5040 | goto do_drop; |
5041 | } | |
5042 | ||
e6d5dbdd LB |
5043 | __skb_pull(*pskb, mac_len); |
5044 | ||
5045 | act = bpf_prog_run_generic_xdp(*pskb, xdp, xdp_prog); | |
fe21cb91 KKD |
5046 | switch (act) { |
5047 | case XDP_REDIRECT: | |
5048 | case XDP_TX: | |
5049 | case XDP_PASS: | |
5050 | break; | |
d4455169 | 5051 | default: |
e6d5dbdd | 5052 | bpf_warn_invalid_xdp_action((*pskb)->dev, xdp_prog, act); |
df561f66 | 5053 | fallthrough; |
d4455169 | 5054 | case XDP_ABORTED: |
e6d5dbdd | 5055 | trace_xdp_exception((*pskb)->dev, xdp_prog, act); |
df561f66 | 5056 | fallthrough; |
d4455169 JF |
5057 | case XDP_DROP: |
5058 | do_drop: | |
e6d5dbdd | 5059 | kfree_skb(*pskb); |
d4455169 JF |
5060 | break; |
5061 | } | |
5062 | ||
5063 | return act; | |
5064 | } | |
5065 | ||
5066 | /* When doing generic XDP we have to bypass the qdisc layer and the | |
1fd6e567 JA |
5067 | * network taps in order to match in-driver-XDP behavior. This also means |
5068 | * that XDP packets are able to starve other packets going through a qdisc, | |
5069 | * and DDOS attacks will be more effective. In-driver-XDP use dedicated TX | |
5070 | * queues, so they do not have this starvation issue. | |
d4455169 | 5071 | */ |
7c497478 | 5072 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
d4455169 JF |
5073 | { |
5074 | struct net_device *dev = skb->dev; | |
5075 | struct netdev_queue *txq; | |
5076 | bool free_skb = true; | |
5077 | int cpu, rc; | |
5078 | ||
4bd97d51 | 5079 | txq = netdev_core_pick_tx(dev, skb, NULL); |
d4455169 JF |
5080 | cpu = smp_processor_id(); |
5081 | HARD_TX_LOCK(dev, txq, cpu); | |
1fd6e567 | 5082 | if (!netif_xmit_frozen_or_drv_stopped(txq)) { |
d4455169 JF |
5083 | rc = netdev_start_xmit(skb, dev, txq, 0); |
5084 | if (dev_xmit_complete(rc)) | |
5085 | free_skb = false; | |
5086 | } | |
5087 | HARD_TX_UNLOCK(dev, txq); | |
5088 | if (free_skb) { | |
5089 | trace_xdp_exception(dev, xdp_prog, XDP_TX); | |
1fd6e567 | 5090 | dev_core_stats_tx_dropped_inc(dev); |
d4455169 JF |
5091 | kfree_skb(skb); |
5092 | } | |
5093 | } | |
5094 | ||
02786475 | 5095 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
d4455169 | 5096 | |
4d2bb0bf | 5097 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff **pskb) |
d4455169 | 5098 | { |
d4455169 | 5099 | if (xdp_prog) { |
02671e23 BT |
5100 | struct xdp_buff xdp; |
5101 | u32 act; | |
6103aa96 | 5102 | int err; |
d4455169 | 5103 | |
4d2bb0bf | 5104 | act = netif_receive_generic_xdp(pskb, &xdp, xdp_prog); |
d4455169 | 5105 | if (act != XDP_PASS) { |
6103aa96 JF |
5106 | switch (act) { |
5107 | case XDP_REDIRECT: | |
4d2bb0bf | 5108 | err = xdp_do_generic_redirect((*pskb)->dev, *pskb, |
02671e23 | 5109 | &xdp, xdp_prog); |
6103aa96 JF |
5110 | if (err) |
5111 | goto out_redir; | |
02671e23 | 5112 | break; |
6103aa96 | 5113 | case XDP_TX: |
4d2bb0bf | 5114 | generic_xdp_tx(*pskb, xdp_prog); |
6103aa96 JF |
5115 | break; |
5116 | } | |
d4455169 JF |
5117 | return XDP_DROP; |
5118 | } | |
5119 | } | |
5120 | return XDP_PASS; | |
6103aa96 | 5121 | out_redir: |
4d2bb0bf | 5122 | kfree_skb_reason(*pskb, SKB_DROP_REASON_XDP); |
6103aa96 | 5123 | return XDP_DROP; |
d4455169 | 5124 | } |
7c497478 | 5125 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
d4455169 | 5126 | |
ae78dbfa | 5127 | static int netif_rx_internal(struct sk_buff *skb) |
1da177e4 | 5128 | { |
b0e28f1e | 5129 | int ret; |
1da177e4 | 5130 | |
f59b5416 | 5131 | net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb); |
1da177e4 | 5132 | |
cf66ba58 | 5133 | trace_netif_rx(skb); |
d4455169 | 5134 | |
df334545 | 5135 | #ifdef CONFIG_RPS |
dc05360f | 5136 | if (static_branch_unlikely(&rps_needed)) { |
fec5e652 | 5137 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
5138 | int cpu; |
5139 | ||
5140 | rcu_read_lock(); | |
fec5e652 TH |
5141 | |
5142 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
5143 | if (cpu < 0) |
5144 | cpu = smp_processor_id(); | |
fec5e652 TH |
5145 | |
5146 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5147 | ||
b0e28f1e | 5148 | rcu_read_unlock(); |
adc9300e ED |
5149 | } else |
5150 | #endif | |
fec5e652 TH |
5151 | { |
5152 | unsigned int qtail; | |
f4563a75 | 5153 | |
f234ae29 | 5154 | ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail); |
fec5e652 | 5155 | } |
b0e28f1e | 5156 | return ret; |
1da177e4 | 5157 | } |
ae78dbfa | 5158 | |
baebdf48 SAS |
5159 | /** |
5160 | * __netif_rx - Slightly optimized version of netif_rx | |
5161 | * @skb: buffer to post | |
5162 | * | |
5163 | * This behaves as netif_rx except that it does not disable bottom halves. | |
5164 | * As a result this function may only be invoked from the interrupt context | |
5165 | * (either hard or soft interrupt). | |
5166 | */ | |
5167 | int __netif_rx(struct sk_buff *skb) | |
5168 | { | |
5169 | int ret; | |
5170 | ||
351bdbb6 | 5171 | lockdep_assert_once(hardirq_count() | softirq_count()); |
baebdf48 SAS |
5172 | |
5173 | trace_netif_rx_entry(skb); | |
5174 | ret = netif_rx_internal(skb); | |
5175 | trace_netif_rx_exit(ret); | |
5176 | return ret; | |
5177 | } | |
5178 | EXPORT_SYMBOL(__netif_rx); | |
5179 | ||
ae78dbfa BH |
5180 | /** |
5181 | * netif_rx - post buffer to the network code | |
5182 | * @skb: buffer to post | |
5183 | * | |
5184 | * This function receives a packet from a device driver and queues it for | |
baebdf48 SAS |
5185 | * the upper (protocol) levels to process via the backlog NAPI device. It |
5186 | * always succeeds. The buffer may be dropped during processing for | |
5187 | * congestion control or by the protocol layers. | |
5188 | * The network buffer is passed via the backlog NAPI device. Modern NIC | |
5189 | * driver should use NAPI and GRO. | |
167053f8 SAS |
5190 | * This function can used from interrupt and from process context. The |
5191 | * caller from process context must not disable interrupts before invoking | |
5192 | * this function. | |
ae78dbfa BH |
5193 | * |
5194 | * return values: | |
5195 | * NET_RX_SUCCESS (no congestion) | |
5196 | * NET_RX_DROP (packet was dropped) | |
5197 | * | |
5198 | */ | |
ae78dbfa BH |
5199 | int netif_rx(struct sk_buff *skb) |
5200 | { | |
167053f8 | 5201 | bool need_bh_off = !(hardirq_count() | softirq_count()); |
b0e3f1bd GB |
5202 | int ret; |
5203 | ||
167053f8 SAS |
5204 | if (need_bh_off) |
5205 | local_bh_disable(); | |
ae78dbfa | 5206 | trace_netif_rx_entry(skb); |
b0e3f1bd GB |
5207 | ret = netif_rx_internal(skb); |
5208 | trace_netif_rx_exit(ret); | |
167053f8 SAS |
5209 | if (need_bh_off) |
5210 | local_bh_enable(); | |
b0e3f1bd | 5211 | return ret; |
ae78dbfa | 5212 | } |
d1b19dff | 5213 | EXPORT_SYMBOL(netif_rx); |
1da177e4 | 5214 | |
0766f788 | 5215 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
1da177e4 | 5216 | { |
903ceff7 | 5217 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
1da177e4 LT |
5218 | |
5219 | if (sd->completion_queue) { | |
5220 | struct sk_buff *clist; | |
5221 | ||
5222 | local_irq_disable(); | |
5223 | clist = sd->completion_queue; | |
5224 | sd->completion_queue = NULL; | |
5225 | local_irq_enable(); | |
5226 | ||
5227 | while (clist) { | |
5228 | struct sk_buff *skb = clist; | |
f4563a75 | 5229 | |
1da177e4 LT |
5230 | clist = clist->next; |
5231 | ||
63354797 | 5232 | WARN_ON(refcount_read(&skb->users)); |
40bbae58 | 5233 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_CONSUMED)) |
dd1b5278 | 5234 | trace_consume_skb(skb, net_tx_action); |
e6247027 | 5235 | else |
c504e5c2 | 5236 | trace_kfree_skb(skb, net_tx_action, |
40bbae58 | 5237 | get_kfree_skb_cb(skb)->reason); |
15fad714 JDB |
5238 | |
5239 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) | |
5240 | __kfree_skb(skb); | |
5241 | else | |
8fa66e4a JK |
5242 | __napi_kfree_skb(skb, |
5243 | get_kfree_skb_cb(skb)->reason); | |
1da177e4 LT |
5244 | } |
5245 | } | |
5246 | ||
5247 | if (sd->output_queue) { | |
37437bb2 | 5248 | struct Qdisc *head; |
1da177e4 LT |
5249 | |
5250 | local_irq_disable(); | |
5251 | head = sd->output_queue; | |
5252 | sd->output_queue = NULL; | |
a9cbd588 | 5253 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
5254 | local_irq_enable(); |
5255 | ||
102b55ee YL |
5256 | rcu_read_lock(); |
5257 | ||
1da177e4 | 5258 | while (head) { |
37437bb2 | 5259 | struct Qdisc *q = head; |
6b3ba914 | 5260 | spinlock_t *root_lock = NULL; |
37437bb2 | 5261 | |
1da177e4 LT |
5262 | head = head->next_sched; |
5263 | ||
3bcb846c ED |
5264 | /* We need to make sure head->next_sched is read |
5265 | * before clearing __QDISC_STATE_SCHED | |
5266 | */ | |
5267 | smp_mb__before_atomic(); | |
102b55ee YL |
5268 | |
5269 | if (!(q->flags & TCQ_F_NOLOCK)) { | |
5270 | root_lock = qdisc_lock(q); | |
5271 | spin_lock(root_lock); | |
5272 | } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, | |
5273 | &q->state))) { | |
5274 | /* There is a synchronize_net() between | |
5275 | * STATE_DEACTIVATED flag being set and | |
5276 | * qdisc_reset()/some_qdisc_is_busy() in | |
5277 | * dev_deactivate(), so we can safely bail out | |
5278 | * early here to avoid data race between | |
5279 | * qdisc_deactivate() and some_qdisc_is_busy() | |
5280 | * for lockless qdisc. | |
5281 | */ | |
5282 | clear_bit(__QDISC_STATE_SCHED, &q->state); | |
5283 | continue; | |
5284 | } | |
5285 | ||
3bcb846c ED |
5286 | clear_bit(__QDISC_STATE_SCHED, &q->state); |
5287 | qdisc_run(q); | |
6b3ba914 JF |
5288 | if (root_lock) |
5289 | spin_unlock(root_lock); | |
1da177e4 | 5290 | } |
102b55ee YL |
5291 | |
5292 | rcu_read_unlock(); | |
1da177e4 | 5293 | } |
f53c7239 SK |
5294 | |
5295 | xfrm_dev_backlog(sd); | |
1da177e4 LT |
5296 | } |
5297 | ||
181402a5 | 5298 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
5299 | /* This hook is defined here for ATM LANE */ |
5300 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
5301 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 5302 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 5303 | #endif |
1da177e4 | 5304 | |
24b27fc4 MB |
5305 | /** |
5306 | * netdev_is_rx_handler_busy - check if receive handler is registered | |
5307 | * @dev: device to check | |
5308 | * | |
5309 | * Check if a receive handler is already registered for a given device. | |
5310 | * Return true if there one. | |
5311 | * | |
5312 | * The caller must hold the rtnl_mutex. | |
5313 | */ | |
5314 | bool netdev_is_rx_handler_busy(struct net_device *dev) | |
5315 | { | |
5316 | ASSERT_RTNL(); | |
5317 | return dev && rtnl_dereference(dev->rx_handler); | |
5318 | } | |
5319 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); | |
5320 | ||
ab95bfe0 JP |
5321 | /** |
5322 | * netdev_rx_handler_register - register receive handler | |
5323 | * @dev: device to register a handler for | |
5324 | * @rx_handler: receive handler to register | |
93e2c32b | 5325 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 | 5326 | * |
e227867f | 5327 | * Register a receive handler for a device. This handler will then be |
ab95bfe0 JP |
5328 | * called from __netif_receive_skb. A negative errno code is returned |
5329 | * on a failure. | |
5330 | * | |
5331 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
5332 | * |
5333 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
5334 | */ |
5335 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
5336 | rx_handler_func_t *rx_handler, |
5337 | void *rx_handler_data) | |
ab95bfe0 | 5338 | { |
1b7cd004 | 5339 | if (netdev_is_rx_handler_busy(dev)) |
ab95bfe0 JP |
5340 | return -EBUSY; |
5341 | ||
f5426250 PA |
5342 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
5343 | return -EINVAL; | |
5344 | ||
00cfec37 | 5345 | /* Note: rx_handler_data must be set before rx_handler */ |
93e2c32b | 5346 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
5347 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
5348 | ||
5349 | return 0; | |
5350 | } | |
5351 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
5352 | ||
5353 | /** | |
5354 | * netdev_rx_handler_unregister - unregister receive handler | |
5355 | * @dev: device to unregister a handler from | |
5356 | * | |
166ec369 | 5357 | * Unregister a receive handler from a device. |
ab95bfe0 JP |
5358 | * |
5359 | * The caller must hold the rtnl_mutex. | |
5360 | */ | |
5361 | void netdev_rx_handler_unregister(struct net_device *dev) | |
5362 | { | |
5363 | ||
5364 | ASSERT_RTNL(); | |
a9b3cd7f | 5365 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
00cfec37 ED |
5366 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
5367 | * section has a guarantee to see a non NULL rx_handler_data | |
5368 | * as well. | |
5369 | */ | |
5370 | synchronize_net(); | |
a9b3cd7f | 5371 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
ab95bfe0 JP |
5372 | } |
5373 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
5374 | ||
b4b9e355 MG |
5375 | /* |
5376 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
5377 | * the special handling of PFMEMALLOC skbs. | |
5378 | */ | |
5379 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
5380 | { | |
5381 | switch (skb->protocol) { | |
2b8837ae JP |
5382 | case htons(ETH_P_ARP): |
5383 | case htons(ETH_P_IP): | |
5384 | case htons(ETH_P_IPV6): | |
5385 | case htons(ETH_P_8021Q): | |
5386 | case htons(ETH_P_8021AD): | |
b4b9e355 MG |
5387 | return true; |
5388 | default: | |
5389 | return false; | |
5390 | } | |
5391 | } | |
5392 | ||
e687ad60 PN |
5393 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
5394 | int *ret, struct net_device *orig_dev) | |
5395 | { | |
5396 | if (nf_hook_ingress_active(skb)) { | |
2c1e2703 AC |
5397 | int ingress_retval; |
5398 | ||
e687ad60 PN |
5399 | if (*pt_prev) { |
5400 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
5401 | *pt_prev = NULL; | |
5402 | } | |
5403 | ||
2c1e2703 AC |
5404 | rcu_read_lock(); |
5405 | ingress_retval = nf_hook_ingress(skb); | |
5406 | rcu_read_unlock(); | |
5407 | return ingress_retval; | |
e687ad60 PN |
5408 | } |
5409 | return 0; | |
5410 | } | |
e687ad60 | 5411 | |
c0bbbdc3 | 5412 | static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, |
88eb1944 | 5413 | struct packet_type **ppt_prev) |
1da177e4 LT |
5414 | { |
5415 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 5416 | rx_handler_func_t *rx_handler; |
c0bbbdc3 | 5417 | struct sk_buff *skb = *pskb; |
f2ccd8fa | 5418 | struct net_device *orig_dev; |
8a4eb573 | 5419 | bool deliver_exact = false; |
1da177e4 | 5420 | int ret = NET_RX_DROP; |
252e3346 | 5421 | __be16 type; |
1da177e4 | 5422 | |
f59b5416 | 5423 | net_timestamp_check(!READ_ONCE(net_hotdata.tstamp_prequeue), skb); |
81bbb3d4 | 5424 | |
cf66ba58 | 5425 | trace_netif_receive_skb(skb); |
9b22ea56 | 5426 | |
cc9bd5ce | 5427 | orig_dev = skb->dev; |
8f903c70 | 5428 | |
c1d2bbe1 | 5429 | skb_reset_network_header(skb); |
fda55eca ED |
5430 | if (!skb_transport_header_was_set(skb)) |
5431 | skb_reset_transport_header(skb); | |
0b5c9db1 | 5432 | skb_reset_mac_len(skb); |
1da177e4 LT |
5433 | |
5434 | pt_prev = NULL; | |
5435 | ||
63d8ea7f | 5436 | another_round: |
b6858177 | 5437 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
5438 | |
5439 | __this_cpu_inc(softnet_data.processed); | |
5440 | ||
458bf2f2 SH |
5441 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
5442 | int ret2; | |
5443 | ||
2b4cd14f | 5444 | migrate_disable(); |
4d2bb0bf LB |
5445 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), |
5446 | &skb); | |
2b4cd14f | 5447 | migrate_enable(); |
458bf2f2 | 5448 | |
c0bbbdc3 BS |
5449 | if (ret2 != XDP_PASS) { |
5450 | ret = NET_RX_DROP; | |
5451 | goto out; | |
5452 | } | |
458bf2f2 SH |
5453 | } |
5454 | ||
324cefaf | 5455 | if (eth_type_vlan(skb->protocol)) { |
0d5501c1 | 5456 | skb = skb_vlan_untag(skb); |
bcc6d479 | 5457 | if (unlikely(!skb)) |
2c17d27c | 5458 | goto out; |
bcc6d479 JP |
5459 | } |
5460 | ||
cd14e9b7 | 5461 | if (skb_skip_tc_classify(skb)) |
e7246e12 | 5462 | goto skip_classify; |
1da177e4 | 5463 | |
9754e293 | 5464 | if (pfmemalloc) |
b4b9e355 MG |
5465 | goto skip_taps; |
5466 | ||
0b91fa4b | 5467 | list_for_each_entry_rcu(ptype, &net_hotdata.ptype_all, list) { |
7866a621 SN |
5468 | if (pt_prev) |
5469 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5470 | pt_prev = ptype; | |
5471 | } | |
5472 | ||
5473 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { | |
5474 | if (pt_prev) | |
5475 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5476 | pt_prev = ptype; | |
1da177e4 LT |
5477 | } |
5478 | ||
b4b9e355 | 5479 | skip_taps: |
1cf51900 | 5480 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 5481 | if (static_branch_unlikely(&ingress_needed_key)) { |
9aa1206e DB |
5482 | bool another = false; |
5483 | ||
42df6e1d | 5484 | nf_skip_egress(skb, true); |
9aa1206e DB |
5485 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev, |
5486 | &another); | |
5487 | if (another) | |
5488 | goto another_round; | |
4577139b | 5489 | if (!skb) |
2c17d27c | 5490 | goto out; |
e687ad60 | 5491 | |
42df6e1d | 5492 | nf_skip_egress(skb, false); |
e687ad60 | 5493 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) |
2c17d27c | 5494 | goto out; |
cd14e9b7 | 5495 | } |
1cf51900 | 5496 | #endif |
2c64605b | 5497 | skb_reset_redirect(skb); |
e7246e12 | 5498 | skip_classify: |
9754e293 | 5499 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
b4b9e355 MG |
5500 | goto drop; |
5501 | ||
df8a39de | 5502 | if (skb_vlan_tag_present(skb)) { |
2425717b JF |
5503 | if (pt_prev) { |
5504 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5505 | pt_prev = NULL; | |
5506 | } | |
48cc32d3 | 5507 | if (vlan_do_receive(&skb)) |
2425717b JF |
5508 | goto another_round; |
5509 | else if (unlikely(!skb)) | |
2c17d27c | 5510 | goto out; |
2425717b JF |
5511 | } |
5512 | ||
48cc32d3 | 5513 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
5514 | if (rx_handler) { |
5515 | if (pt_prev) { | |
5516 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5517 | pt_prev = NULL; | |
5518 | } | |
8a4eb573 JP |
5519 | switch (rx_handler(&skb)) { |
5520 | case RX_HANDLER_CONSUMED: | |
3bc1b1ad | 5521 | ret = NET_RX_SUCCESS; |
2c17d27c | 5522 | goto out; |
8a4eb573 | 5523 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 5524 | goto another_round; |
8a4eb573 JP |
5525 | case RX_HANDLER_EXACT: |
5526 | deliver_exact = true; | |
b1866bff | 5527 | break; |
8a4eb573 JP |
5528 | case RX_HANDLER_PASS: |
5529 | break; | |
5530 | default: | |
5531 | BUG(); | |
5532 | } | |
ab95bfe0 | 5533 | } |
1da177e4 | 5534 | |
b14a9fc4 | 5535 | if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) { |
36b2f61a GV |
5536 | check_vlan_id: |
5537 | if (skb_vlan_tag_get_id(skb)) { | |
5538 | /* Vlan id is non 0 and vlan_do_receive() above couldn't | |
5539 | * find vlan device. | |
5540 | */ | |
d4b812de | 5541 | skb->pkt_type = PACKET_OTHERHOST; |
324cefaf | 5542 | } else if (eth_type_vlan(skb->protocol)) { |
36b2f61a GV |
5543 | /* Outer header is 802.1P with vlan 0, inner header is |
5544 | * 802.1Q or 802.1AD and vlan_do_receive() above could | |
5545 | * not find vlan dev for vlan id 0. | |
5546 | */ | |
5547 | __vlan_hwaccel_clear_tag(skb); | |
5548 | skb = skb_vlan_untag(skb); | |
5549 | if (unlikely(!skb)) | |
5550 | goto out; | |
5551 | if (vlan_do_receive(&skb)) | |
5552 | /* After stripping off 802.1P header with vlan 0 | |
5553 | * vlan dev is found for inner header. | |
5554 | */ | |
5555 | goto another_round; | |
5556 | else if (unlikely(!skb)) | |
5557 | goto out; | |
5558 | else | |
5559 | /* We have stripped outer 802.1P vlan 0 header. | |
5560 | * But could not find vlan dev. | |
5561 | * check again for vlan id to set OTHERHOST. | |
5562 | */ | |
5563 | goto check_vlan_id; | |
5564 | } | |
d4b812de ED |
5565 | /* Note: we might in the future use prio bits |
5566 | * and set skb->priority like in vlan_do_receive() | |
5567 | * For the time being, just ignore Priority Code Point | |
5568 | */ | |
b1817524 | 5569 | __vlan_hwaccel_clear_tag(skb); |
d4b812de | 5570 | } |
48cc32d3 | 5571 | |
7866a621 SN |
5572 | type = skb->protocol; |
5573 | ||
63d8ea7f | 5574 | /* deliver only exact match when indicated */ |
7866a621 SN |
5575 | if (likely(!deliver_exact)) { |
5576 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5577 | &ptype_base[ntohs(type) & | |
5578 | PTYPE_HASH_MASK]); | |
5579 | } | |
1f3c8804 | 5580 | |
7866a621 SN |
5581 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
5582 | &orig_dev->ptype_specific); | |
5583 | ||
5584 | if (unlikely(skb->dev != orig_dev)) { | |
5585 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5586 | &skb->dev->ptype_specific); | |
1da177e4 LT |
5587 | } |
5588 | ||
5589 | if (pt_prev) { | |
1f8b977a | 5590 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
0e698bf6 | 5591 | goto drop; |
88eb1944 | 5592 | *ppt_prev = pt_prev; |
1da177e4 | 5593 | } else { |
b4b9e355 | 5594 | drop: |
9f8ed577 | 5595 | if (!deliver_exact) |
625788b5 | 5596 | dev_core_stats_rx_dropped_inc(skb->dev); |
9f8ed577 | 5597 | else |
625788b5 | 5598 | dev_core_stats_rx_nohandler_inc(skb->dev); |
9f8ed577 | 5599 | kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO); |
1da177e4 LT |
5600 | /* Jamal, now you will not able to escape explaining |
5601 | * me how you were going to use this. :-) | |
5602 | */ | |
5603 | ret = NET_RX_DROP; | |
5604 | } | |
5605 | ||
2c17d27c | 5606 | out: |
c0bbbdc3 BS |
5607 | /* The invariant here is that if *ppt_prev is not NULL |
5608 | * then skb should also be non-NULL. | |
5609 | * | |
5610 | * Apparently *ppt_prev assignment above holds this invariant due to | |
5611 | * skb dereferencing near it. | |
5612 | */ | |
5613 | *pskb = skb; | |
9754e293 DM |
5614 | return ret; |
5615 | } | |
5616 | ||
88eb1944 EC |
5617 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
5618 | { | |
5619 | struct net_device *orig_dev = skb->dev; | |
5620 | struct packet_type *pt_prev = NULL; | |
5621 | int ret; | |
5622 | ||
c0bbbdc3 | 5623 | ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
88eb1944 | 5624 | if (pt_prev) |
f5737cba PA |
5625 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
5626 | skb->dev, pt_prev, orig_dev); | |
88eb1944 EC |
5627 | return ret; |
5628 | } | |
5629 | ||
1c601d82 JDB |
5630 | /** |
5631 | * netif_receive_skb_core - special purpose version of netif_receive_skb | |
5632 | * @skb: buffer to process | |
5633 | * | |
5634 | * More direct receive version of netif_receive_skb(). It should | |
5635 | * only be used by callers that have a need to skip RPS and Generic XDP. | |
2de9780f | 5636 | * Caller must also take care of handling if ``(page_is_)pfmemalloc``. |
1c601d82 JDB |
5637 | * |
5638 | * This function may only be called from softirq context and interrupts | |
5639 | * should be enabled. | |
5640 | * | |
5641 | * Return values (usually ignored): | |
5642 | * NET_RX_SUCCESS: no congestion | |
5643 | * NET_RX_DROP: packet was dropped | |
5644 | */ | |
5645 | int netif_receive_skb_core(struct sk_buff *skb) | |
5646 | { | |
5647 | int ret; | |
5648 | ||
5649 | rcu_read_lock(); | |
88eb1944 | 5650 | ret = __netif_receive_skb_one_core(skb, false); |
1c601d82 JDB |
5651 | rcu_read_unlock(); |
5652 | ||
5653 | return ret; | |
5654 | } | |
5655 | EXPORT_SYMBOL(netif_receive_skb_core); | |
5656 | ||
88eb1944 EC |
5657 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
5658 | struct packet_type *pt_prev, | |
5659 | struct net_device *orig_dev) | |
4ce0017a EC |
5660 | { |
5661 | struct sk_buff *skb, *next; | |
5662 | ||
88eb1944 EC |
5663 | if (!pt_prev) |
5664 | return; | |
5665 | if (list_empty(head)) | |
5666 | return; | |
17266ee9 | 5667 | if (pt_prev->list_func != NULL) |
fdf71426 PA |
5668 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
5669 | ip_list_rcv, head, pt_prev, orig_dev); | |
17266ee9 | 5670 | else |
9a5a90d1 AL |
5671 | list_for_each_entry_safe(skb, next, head, list) { |
5672 | skb_list_del_init(skb); | |
fdf71426 | 5673 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
9a5a90d1 | 5674 | } |
88eb1944 EC |
5675 | } |
5676 | ||
5677 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) | |
5678 | { | |
5679 | /* Fast-path assumptions: | |
5680 | * - There is no RX handler. | |
5681 | * - Only one packet_type matches. | |
5682 | * If either of these fails, we will end up doing some per-packet | |
5683 | * processing in-line, then handling the 'last ptype' for the whole | |
5684 | * sublist. This can't cause out-of-order delivery to any single ptype, | |
5685 | * because the 'last ptype' must be constant across the sublist, and all | |
5686 | * other ptypes are handled per-packet. | |
5687 | */ | |
5688 | /* Current (common) ptype of sublist */ | |
5689 | struct packet_type *pt_curr = NULL; | |
5690 | /* Current (common) orig_dev of sublist */ | |
5691 | struct net_device *od_curr = NULL; | |
5692 | struct list_head sublist; | |
5693 | struct sk_buff *skb, *next; | |
5694 | ||
9af86f93 | 5695 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5696 | list_for_each_entry_safe(skb, next, head, list) { |
5697 | struct net_device *orig_dev = skb->dev; | |
5698 | struct packet_type *pt_prev = NULL; | |
5699 | ||
22f6bbb7 | 5700 | skb_list_del_init(skb); |
c0bbbdc3 | 5701 | __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
9af86f93 EC |
5702 | if (!pt_prev) |
5703 | continue; | |
88eb1944 EC |
5704 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
5705 | /* dispatch old sublist */ | |
88eb1944 EC |
5706 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
5707 | /* start new sublist */ | |
9af86f93 | 5708 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5709 | pt_curr = pt_prev; |
5710 | od_curr = orig_dev; | |
5711 | } | |
9af86f93 | 5712 | list_add_tail(&skb->list, &sublist); |
88eb1944 EC |
5713 | } |
5714 | ||
5715 | /* dispatch final sublist */ | |
9af86f93 | 5716 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
4ce0017a EC |
5717 | } |
5718 | ||
9754e293 DM |
5719 | static int __netif_receive_skb(struct sk_buff *skb) |
5720 | { | |
5721 | int ret; | |
5722 | ||
5723 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { | |
f1083048 | 5724 | unsigned int noreclaim_flag; |
9754e293 DM |
5725 | |
5726 | /* | |
5727 | * PFMEMALLOC skbs are special, they should | |
5728 | * - be delivered to SOCK_MEMALLOC sockets only | |
5729 | * - stay away from userspace | |
5730 | * - have bounded memory usage | |
5731 | * | |
5732 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
5733 | * context down to all allocation sites. | |
5734 | */ | |
f1083048 | 5735 | noreclaim_flag = memalloc_noreclaim_save(); |
88eb1944 | 5736 | ret = __netif_receive_skb_one_core(skb, true); |
f1083048 | 5737 | memalloc_noreclaim_restore(noreclaim_flag); |
9754e293 | 5738 | } else |
88eb1944 | 5739 | ret = __netif_receive_skb_one_core(skb, false); |
9754e293 | 5740 | |
1da177e4 LT |
5741 | return ret; |
5742 | } | |
0a9627f2 | 5743 | |
4ce0017a EC |
5744 | static void __netif_receive_skb_list(struct list_head *head) |
5745 | { | |
5746 | unsigned long noreclaim_flag = 0; | |
5747 | struct sk_buff *skb, *next; | |
5748 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ | |
5749 | ||
5750 | list_for_each_entry_safe(skb, next, head, list) { | |
5751 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { | |
5752 | struct list_head sublist; | |
5753 | ||
5754 | /* Handle the previous sublist */ | |
5755 | list_cut_before(&sublist, head, &skb->list); | |
b9f463d6 EC |
5756 | if (!list_empty(&sublist)) |
5757 | __netif_receive_skb_list_core(&sublist, pfmemalloc); | |
4ce0017a EC |
5758 | pfmemalloc = !pfmemalloc; |
5759 | /* See comments in __netif_receive_skb */ | |
5760 | if (pfmemalloc) | |
5761 | noreclaim_flag = memalloc_noreclaim_save(); | |
5762 | else | |
5763 | memalloc_noreclaim_restore(noreclaim_flag); | |
5764 | } | |
5765 | } | |
5766 | /* Handle the remaining sublist */ | |
b9f463d6 EC |
5767 | if (!list_empty(head)) |
5768 | __netif_receive_skb_list_core(head, pfmemalloc); | |
4ce0017a EC |
5769 | /* Restore pflags */ |
5770 | if (pfmemalloc) | |
5771 | memalloc_noreclaim_restore(noreclaim_flag); | |
5772 | } | |
5773 | ||
f4e63525 | 5774 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
b5cdae32 | 5775 | { |
58038695 | 5776 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
b5cdae32 DM |
5777 | struct bpf_prog *new = xdp->prog; |
5778 | int ret = 0; | |
5779 | ||
5780 | switch (xdp->command) { | |
58038695 | 5781 | case XDP_SETUP_PROG: |
b5cdae32 DM |
5782 | rcu_assign_pointer(dev->xdp_prog, new); |
5783 | if (old) | |
5784 | bpf_prog_put(old); | |
5785 | ||
5786 | if (old && !new) { | |
02786475 | 5787 | static_branch_dec(&generic_xdp_needed_key); |
b5cdae32 | 5788 | } else if (new && !old) { |
02786475 | 5789 | static_branch_inc(&generic_xdp_needed_key); |
b5cdae32 | 5790 | dev_disable_lro(dev); |
56f5aa77 | 5791 | dev_disable_gro_hw(dev); |
b5cdae32 DM |
5792 | } |
5793 | break; | |
b5cdae32 | 5794 | |
b5cdae32 DM |
5795 | default: |
5796 | ret = -EINVAL; | |
5797 | break; | |
5798 | } | |
5799 | ||
5800 | return ret; | |
5801 | } | |
5802 | ||
ae78dbfa | 5803 | static int netif_receive_skb_internal(struct sk_buff *skb) |
0a9627f2 | 5804 | { |
2c17d27c JA |
5805 | int ret; |
5806 | ||
f59b5416 | 5807 | net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb); |
3b098e2d | 5808 | |
c1f19b51 RC |
5809 | if (skb_defer_rx_timestamp(skb)) |
5810 | return NET_RX_SUCCESS; | |
5811 | ||
bbbe211c | 5812 | rcu_read_lock(); |
df334545 | 5813 | #ifdef CONFIG_RPS |
dc05360f | 5814 | if (static_branch_unlikely(&rps_needed)) { |
3b098e2d | 5815 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
2c17d27c | 5816 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
0a9627f2 | 5817 | |
3b098e2d ED |
5818 | if (cpu >= 0) { |
5819 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5820 | rcu_read_unlock(); | |
adc9300e | 5821 | return ret; |
3b098e2d | 5822 | } |
fec5e652 | 5823 | } |
1e94d72f | 5824 | #endif |
2c17d27c JA |
5825 | ret = __netif_receive_skb(skb); |
5826 | rcu_read_unlock(); | |
5827 | return ret; | |
0a9627f2 | 5828 | } |
ae78dbfa | 5829 | |
587652bb | 5830 | void netif_receive_skb_list_internal(struct list_head *head) |
7da517a3 | 5831 | { |
7da517a3 | 5832 | struct sk_buff *skb, *next; |
8c057efa | 5833 | struct list_head sublist; |
7da517a3 | 5834 | |
8c057efa | 5835 | INIT_LIST_HEAD(&sublist); |
7da517a3 | 5836 | list_for_each_entry_safe(skb, next, head, list) { |
f59b5416 ED |
5837 | net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), |
5838 | skb); | |
22f6bbb7 | 5839 | skb_list_del_init(skb); |
8c057efa EC |
5840 | if (!skb_defer_rx_timestamp(skb)) |
5841 | list_add_tail(&skb->list, &sublist); | |
7da517a3 | 5842 | } |
8c057efa | 5843 | list_splice_init(&sublist, head); |
7da517a3 | 5844 | |
7da517a3 EC |
5845 | rcu_read_lock(); |
5846 | #ifdef CONFIG_RPS | |
dc05360f | 5847 | if (static_branch_unlikely(&rps_needed)) { |
7da517a3 EC |
5848 | list_for_each_entry_safe(skb, next, head, list) { |
5849 | struct rps_dev_flow voidflow, *rflow = &voidflow; | |
5850 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
5851 | ||
5852 | if (cpu >= 0) { | |
8c057efa | 5853 | /* Will be handled, remove from list */ |
22f6bbb7 | 5854 | skb_list_del_init(skb); |
8c057efa | 5855 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
7da517a3 EC |
5856 | } |
5857 | } | |
5858 | } | |
5859 | #endif | |
5860 | __netif_receive_skb_list(head); | |
5861 | rcu_read_unlock(); | |
5862 | } | |
5863 | ||
ae78dbfa BH |
5864 | /** |
5865 | * netif_receive_skb - process receive buffer from network | |
5866 | * @skb: buffer to process | |
5867 | * | |
5868 | * netif_receive_skb() is the main receive data processing function. | |
5869 | * It always succeeds. The buffer may be dropped during processing | |
5870 | * for congestion control or by the protocol layers. | |
5871 | * | |
5872 | * This function may only be called from softirq context and interrupts | |
5873 | * should be enabled. | |
5874 | * | |
5875 | * Return values (usually ignored): | |
5876 | * NET_RX_SUCCESS: no congestion | |
5877 | * NET_RX_DROP: packet was dropped | |
5878 | */ | |
04eb4489 | 5879 | int netif_receive_skb(struct sk_buff *skb) |
ae78dbfa | 5880 | { |
b0e3f1bd GB |
5881 | int ret; |
5882 | ||
ae78dbfa BH |
5883 | trace_netif_receive_skb_entry(skb); |
5884 | ||
b0e3f1bd GB |
5885 | ret = netif_receive_skb_internal(skb); |
5886 | trace_netif_receive_skb_exit(ret); | |
5887 | ||
5888 | return ret; | |
ae78dbfa | 5889 | } |
04eb4489 | 5890 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 5891 | |
f6ad8c1b EC |
5892 | /** |
5893 | * netif_receive_skb_list - process many receive buffers from network | |
5894 | * @head: list of skbs to process. | |
5895 | * | |
7da517a3 EC |
5896 | * Since return value of netif_receive_skb() is normally ignored, and |
5897 | * wouldn't be meaningful for a list, this function returns void. | |
f6ad8c1b EC |
5898 | * |
5899 | * This function may only be called from softirq context and interrupts | |
5900 | * should be enabled. | |
5901 | */ | |
5902 | void netif_receive_skb_list(struct list_head *head) | |
5903 | { | |
7da517a3 | 5904 | struct sk_buff *skb; |
f6ad8c1b | 5905 | |
b9f463d6 EC |
5906 | if (list_empty(head)) |
5907 | return; | |
b0e3f1bd GB |
5908 | if (trace_netif_receive_skb_list_entry_enabled()) { |
5909 | list_for_each_entry(skb, head, list) | |
5910 | trace_netif_receive_skb_list_entry(skb); | |
5911 | } | |
7da517a3 | 5912 | netif_receive_skb_list_internal(head); |
b0e3f1bd | 5913 | trace_netif_receive_skb_list_exit(0); |
f6ad8c1b EC |
5914 | } |
5915 | EXPORT_SYMBOL(netif_receive_skb_list); | |
5916 | ||
ce1e2a77 | 5917 | static DEFINE_PER_CPU(struct work_struct, flush_works); |
145dd5f9 PA |
5918 | |
5919 | /* Network device is going away, flush any packets still pending */ | |
5920 | static void flush_backlog(struct work_struct *work) | |
6e583ce5 | 5921 | { |
6e583ce5 | 5922 | struct sk_buff *skb, *tmp; |
145dd5f9 PA |
5923 | struct softnet_data *sd; |
5924 | ||
5925 | local_bh_disable(); | |
5926 | sd = this_cpu_ptr(&softnet_data); | |
6e583ce5 | 5927 | |
765b11f8 | 5928 | backlog_lock_irq_disable(sd); |
6e7676c1 | 5929 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
41852497 | 5930 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
e36fa2f7 | 5931 | __skb_unlink(skb, &sd->input_pkt_queue); |
7df5cb75 | 5932 | dev_kfree_skb_irq(skb); |
36b83ffc | 5933 | rps_input_queue_head_incr(sd); |
6e583ce5 | 5934 | } |
6e7676c1 | 5935 | } |
765b11f8 | 5936 | backlog_unlock_irq_enable(sd); |
6e7676c1 CG |
5937 | |
5938 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
41852497 | 5939 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
6e7676c1 CG |
5940 | __skb_unlink(skb, &sd->process_queue); |
5941 | kfree_skb(skb); | |
36b83ffc | 5942 | rps_input_queue_head_incr(sd); |
6e7676c1 CG |
5943 | } |
5944 | } | |
145dd5f9 PA |
5945 | local_bh_enable(); |
5946 | } | |
5947 | ||
2de79ee2 PA |
5948 | static bool flush_required(int cpu) |
5949 | { | |
5950 | #if IS_ENABLED(CONFIG_RPS) | |
5951 | struct softnet_data *sd = &per_cpu(softnet_data, cpu); | |
5952 | bool do_flush; | |
5953 | ||
765b11f8 | 5954 | backlog_lock_irq_disable(sd); |
2de79ee2 PA |
5955 | |
5956 | /* as insertion into process_queue happens with the rps lock held, | |
5957 | * process_queue access may race only with dequeue | |
5958 | */ | |
5959 | do_flush = !skb_queue_empty(&sd->input_pkt_queue) || | |
5960 | !skb_queue_empty_lockless(&sd->process_queue); | |
765b11f8 | 5961 | backlog_unlock_irq_enable(sd); |
2de79ee2 PA |
5962 | |
5963 | return do_flush; | |
5964 | #endif | |
5965 | /* without RPS we can't safely check input_pkt_queue: during a | |
5966 | * concurrent remote skb_queue_splice() we can detect as empty both | |
5967 | * input_pkt_queue and process_queue even if the latter could end-up | |
5968 | * containing a lot of packets. | |
5969 | */ | |
5970 | return true; | |
5971 | } | |
5972 | ||
41852497 | 5973 | static void flush_all_backlogs(void) |
145dd5f9 | 5974 | { |
2de79ee2 | 5975 | static cpumask_t flush_cpus; |
145dd5f9 PA |
5976 | unsigned int cpu; |
5977 | ||
2de79ee2 PA |
5978 | /* since we are under rtnl lock protection we can use static data |
5979 | * for the cpumask and avoid allocating on stack the possibly | |
5980 | * large mask | |
5981 | */ | |
5982 | ASSERT_RTNL(); | |
5983 | ||
372bbdd5 | 5984 | cpus_read_lock(); |
145dd5f9 | 5985 | |
2de79ee2 PA |
5986 | cpumask_clear(&flush_cpus); |
5987 | for_each_online_cpu(cpu) { | |
5988 | if (flush_required(cpu)) { | |
5989 | queue_work_on(cpu, system_highpri_wq, | |
5990 | per_cpu_ptr(&flush_works, cpu)); | |
5991 | cpumask_set_cpu(cpu, &flush_cpus); | |
5992 | } | |
5993 | } | |
145dd5f9 | 5994 | |
2de79ee2 | 5995 | /* we can have in flight packet[s] on the cpus we are not flushing, |
0cbe1e57 | 5996 | * synchronize_net() in unregister_netdevice_many() will take care of |
2de79ee2 PA |
5997 | * them |
5998 | */ | |
5999 | for_each_cpu(cpu, &flush_cpus) | |
41852497 | 6000 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
145dd5f9 | 6001 | |
372bbdd5 | 6002 | cpus_read_unlock(); |
6e583ce5 SH |
6003 | } |
6004 | ||
773fc8f6 | 6005 | static void net_rps_send_ipi(struct softnet_data *remsd) |
6006 | { | |
6007 | #ifdef CONFIG_RPS | |
6008 | while (remsd) { | |
6009 | struct softnet_data *next = remsd->rps_ipi_next; | |
6010 | ||
6011 | if (cpu_online(remsd->cpu)) | |
6012 | smp_call_function_single_async(remsd->cpu, &remsd->csd); | |
6013 | remsd = next; | |
6014 | } | |
6015 | #endif | |
6016 | } | |
6017 | ||
e326bed2 | 6018 | /* |
855abcf0 | 6019 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
e326bed2 ED |
6020 | * Note: called with local irq disabled, but exits with local irq enabled. |
6021 | */ | |
6022 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
6023 | { | |
6024 | #ifdef CONFIG_RPS | |
6025 | struct softnet_data *remsd = sd->rps_ipi_list; | |
6026 | ||
dad6b977 | 6027 | if (!use_backlog_threads() && remsd) { |
e326bed2 ED |
6028 | sd->rps_ipi_list = NULL; |
6029 | ||
6030 | local_irq_enable(); | |
6031 | ||
6032 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
773fc8f6 | 6033 | net_rps_send_ipi(remsd); |
e326bed2 ED |
6034 | } else |
6035 | #endif | |
6036 | local_irq_enable(); | |
6037 | } | |
6038 | ||
d75b1ade ED |
6039 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
6040 | { | |
6041 | #ifdef CONFIG_RPS | |
dad6b977 | 6042 | return !use_backlog_threads() && sd->rps_ipi_list; |
d75b1ade ED |
6043 | #else |
6044 | return false; | |
6045 | #endif | |
6046 | } | |
6047 | ||
bea3348e | 6048 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 | 6049 | { |
eecfd7c4 | 6050 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
145dd5f9 PA |
6051 | bool again = true; |
6052 | int work = 0; | |
1da177e4 | 6053 | |
e326bed2 ED |
6054 | /* Check if we have pending ipi, its better to send them now, |
6055 | * not waiting net_rx_action() end. | |
6056 | */ | |
d75b1ade | 6057 | if (sd_has_rps_ipi_waiting(sd)) { |
e326bed2 ED |
6058 | local_irq_disable(); |
6059 | net_rps_action_and_irq_enable(sd); | |
6060 | } | |
d75b1ade | 6061 | |
71c0de9b | 6062 | napi->weight = READ_ONCE(net_hotdata.dev_rx_weight); |
145dd5f9 | 6063 | while (again) { |
1da177e4 | 6064 | struct sk_buff *skb; |
6e7676c1 CG |
6065 | |
6066 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
2c17d27c | 6067 | rcu_read_lock(); |
6e7676c1 | 6068 | __netif_receive_skb(skb); |
2c17d27c | 6069 | rcu_read_unlock(); |
c62fdf5b ED |
6070 | if (++work >= quota) { |
6071 | rps_input_queue_head_add(sd, work); | |
76cc8b13 | 6072 | return work; |
c62fdf5b | 6073 | } |
145dd5f9 | 6074 | |
6e7676c1 | 6075 | } |
1da177e4 | 6076 | |
765b11f8 | 6077 | backlog_lock_irq_disable(sd); |
11ef7a89 | 6078 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
eecfd7c4 ED |
6079 | /* |
6080 | * Inline a custom version of __napi_complete(). | |
6081 | * only current cpu owns and manipulates this napi, | |
11ef7a89 TH |
6082 | * and NAPI_STATE_SCHED is the only possible flag set |
6083 | * on backlog. | |
6084 | * We can use a plain write instead of clear_bit(), | |
eecfd7c4 ED |
6085 | * and we dont need an smp_mb() memory barrier. |
6086 | */ | |
dad6b977 | 6087 | napi->state &= NAPIF_STATE_THREADED; |
145dd5f9 PA |
6088 | again = false; |
6089 | } else { | |
6090 | skb_queue_splice_tail_init(&sd->input_pkt_queue, | |
6091 | &sd->process_queue); | |
bea3348e | 6092 | } |
765b11f8 | 6093 | backlog_unlock_irq_enable(sd); |
6e7676c1 | 6094 | } |
1da177e4 | 6095 | |
c62fdf5b ED |
6096 | if (work) |
6097 | rps_input_queue_head_add(sd, work); | |
bea3348e SH |
6098 | return work; |
6099 | } | |
1da177e4 | 6100 | |
bea3348e SH |
6101 | /** |
6102 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 6103 | * @n: entry to schedule |
bea3348e | 6104 | * |
bc9ad166 ED |
6105 | * The entry's receive function will be scheduled to run. |
6106 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. | |
bea3348e | 6107 | */ |
b5606c2d | 6108 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
6109 | { |
6110 | unsigned long flags; | |
1da177e4 | 6111 | |
bea3348e | 6112 | local_irq_save(flags); |
903ceff7 | 6113 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
bea3348e | 6114 | local_irq_restore(flags); |
1da177e4 | 6115 | } |
bea3348e SH |
6116 | EXPORT_SYMBOL(__napi_schedule); |
6117 | ||
39e6c820 ED |
6118 | /** |
6119 | * napi_schedule_prep - check if napi can be scheduled | |
6120 | * @n: napi context | |
6121 | * | |
6122 | * Test if NAPI routine is already running, and if not mark | |
ee1a4c84 | 6123 | * it as running. This is used as a condition variable to |
39e6c820 ED |
6124 | * insure only one NAPI poll instance runs. We also make |
6125 | * sure there is no pending NAPI disable. | |
6126 | */ | |
6127 | bool napi_schedule_prep(struct napi_struct *n) | |
6128 | { | |
1462160c | 6129 | unsigned long new, val = READ_ONCE(n->state); |
39e6c820 ED |
6130 | |
6131 | do { | |
39e6c820 ED |
6132 | if (unlikely(val & NAPIF_STATE_DISABLE)) |
6133 | return false; | |
6134 | new = val | NAPIF_STATE_SCHED; | |
6135 | ||
6136 | /* Sets STATE_MISSED bit if STATE_SCHED was already set | |
6137 | * This was suggested by Alexander Duyck, as compiler | |
6138 | * emits better code than : | |
6139 | * if (val & NAPIF_STATE_SCHED) | |
6140 | * new |= NAPIF_STATE_MISSED; | |
6141 | */ | |
6142 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * | |
6143 | NAPIF_STATE_MISSED; | |
1462160c | 6144 | } while (!try_cmpxchg(&n->state, &val, new)); |
39e6c820 ED |
6145 | |
6146 | return !(val & NAPIF_STATE_SCHED); | |
6147 | } | |
6148 | EXPORT_SYMBOL(napi_schedule_prep); | |
6149 | ||
bc9ad166 ED |
6150 | /** |
6151 | * __napi_schedule_irqoff - schedule for receive | |
6152 | * @n: entry to schedule | |
6153 | * | |
8380c81d SAS |
6154 | * Variant of __napi_schedule() assuming hard irqs are masked. |
6155 | * | |
6156 | * On PREEMPT_RT enabled kernels this maps to __napi_schedule() | |
6157 | * because the interrupt disabled assumption might not be true | |
6158 | * due to force-threaded interrupts and spinlock substitution. | |
bc9ad166 ED |
6159 | */ |
6160 | void __napi_schedule_irqoff(struct napi_struct *n) | |
6161 | { | |
8380c81d SAS |
6162 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
6163 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); | |
6164 | else | |
6165 | __napi_schedule(n); | |
bc9ad166 ED |
6166 | } |
6167 | EXPORT_SYMBOL(__napi_schedule_irqoff); | |
6168 | ||
364b6055 | 6169 | bool napi_complete_done(struct napi_struct *n, int work_done) |
d565b0a1 | 6170 | { |
6f8b12d6 ED |
6171 | unsigned long flags, val, new, timeout = 0; |
6172 | bool ret = true; | |
d565b0a1 HX |
6173 | |
6174 | /* | |
217f6974 ED |
6175 | * 1) Don't let napi dequeue from the cpu poll list |
6176 | * just in case its running on a different cpu. | |
6177 | * 2) If we are busy polling, do nothing here, we have | |
6178 | * the guarantee we will be called later. | |
d565b0a1 | 6179 | */ |
217f6974 ED |
6180 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
6181 | NAPIF_STATE_IN_BUSY_POLL))) | |
364b6055 | 6182 | return false; |
d565b0a1 | 6183 | |
6f8b12d6 ED |
6184 | if (work_done) { |
6185 | if (n->gro_bitmask) | |
7e417a66 ED |
6186 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6187 | n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs); | |
6f8b12d6 ED |
6188 | } |
6189 | if (n->defer_hard_irqs_count > 0) { | |
6190 | n->defer_hard_irqs_count--; | |
7e417a66 | 6191 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6f8b12d6 ED |
6192 | if (timeout) |
6193 | ret = false; | |
6194 | } | |
6195 | if (n->gro_bitmask) { | |
605108ac PA |
6196 | /* When the NAPI instance uses a timeout and keeps postponing |
6197 | * it, we need to bound somehow the time packets are kept in | |
6198 | * the GRO layer | |
6199 | */ | |
6200 | napi_gro_flush(n, !!timeout); | |
3b47d303 | 6201 | } |
c8079432 MM |
6202 | |
6203 | gro_normal_list(n); | |
6204 | ||
02c1602e | 6205 | if (unlikely(!list_empty(&n->poll_list))) { |
d75b1ade ED |
6206 | /* If n->poll_list is not empty, we need to mask irqs */ |
6207 | local_irq_save(flags); | |
02c1602e | 6208 | list_del_init(&n->poll_list); |
d75b1ade ED |
6209 | local_irq_restore(flags); |
6210 | } | |
8c48eea3 | 6211 | WRITE_ONCE(n->list_owner, -1); |
39e6c820 | 6212 | |
1462160c | 6213 | val = READ_ONCE(n->state); |
39e6c820 | 6214 | do { |
39e6c820 ED |
6215 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); |
6216 | ||
7fd3253a | 6217 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED | |
cb038357 | 6218 | NAPIF_STATE_SCHED_THREADED | |
7fd3253a | 6219 | NAPIF_STATE_PREFER_BUSY_POLL); |
39e6c820 ED |
6220 | |
6221 | /* If STATE_MISSED was set, leave STATE_SCHED set, | |
6222 | * because we will call napi->poll() one more time. | |
6223 | * This C code was suggested by Alexander Duyck to help gcc. | |
6224 | */ | |
6225 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * | |
6226 | NAPIF_STATE_SCHED; | |
1462160c | 6227 | } while (!try_cmpxchg(&n->state, &val, new)); |
39e6c820 ED |
6228 | |
6229 | if (unlikely(val & NAPIF_STATE_MISSED)) { | |
6230 | __napi_schedule(n); | |
6231 | return false; | |
6232 | } | |
6233 | ||
6f8b12d6 ED |
6234 | if (timeout) |
6235 | hrtimer_start(&n->timer, ns_to_ktime(timeout), | |
6236 | HRTIMER_MODE_REL_PINNED); | |
6237 | return ret; | |
d565b0a1 | 6238 | } |
3b47d303 | 6239 | EXPORT_SYMBOL(napi_complete_done); |
d565b0a1 | 6240 | |
af12fa6e | 6241 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
27f91aaf | 6242 | struct napi_struct *napi_by_id(unsigned int napi_id) |
af12fa6e ET |
6243 | { |
6244 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); | |
6245 | struct napi_struct *napi; | |
6246 | ||
6247 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) | |
6248 | if (napi->napi_id == napi_id) | |
6249 | return napi; | |
6250 | ||
6251 | return NULL; | |
6252 | } | |
02d62e86 | 6253 | |
1200097f ED |
6254 | static void skb_defer_free_flush(struct softnet_data *sd) |
6255 | { | |
6256 | struct sk_buff *skb, *next; | |
6257 | ||
6258 | /* Paired with WRITE_ONCE() in skb_attempt_defer_free() */ | |
6259 | if (!READ_ONCE(sd->defer_list)) | |
6260 | return; | |
6261 | ||
6262 | spin_lock(&sd->defer_lock); | |
6263 | skb = sd->defer_list; | |
6264 | sd->defer_list = NULL; | |
6265 | sd->defer_count = 0; | |
6266 | spin_unlock(&sd->defer_lock); | |
6267 | ||
6268 | while (skb != NULL) { | |
6269 | next = skb->next; | |
6270 | napi_consume_skb(skb, 1); | |
6271 | skb = next; | |
6272 | } | |
6273 | } | |
6274 | ||
02d62e86 | 6275 | #if defined(CONFIG_NET_RX_BUSY_POLL) |
217f6974 | 6276 | |
7fd3253a | 6277 | static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule) |
217f6974 | 6278 | { |
7fd3253a BT |
6279 | if (!skip_schedule) { |
6280 | gro_normal_list(napi); | |
6281 | __napi_schedule(napi); | |
6282 | return; | |
6283 | } | |
217f6974 | 6284 | |
7fd3253a BT |
6285 | if (napi->gro_bitmask) { |
6286 | /* flush too old packets | |
6287 | * If HZ < 1000, flush all packets. | |
6288 | */ | |
6289 | napi_gro_flush(napi, HZ >= 1000); | |
6290 | } | |
217f6974 | 6291 | |
7fd3253a BT |
6292 | gro_normal_list(napi); |
6293 | clear_bit(NAPI_STATE_SCHED, &napi->state); | |
6294 | } | |
6295 | ||
13d381b4 SR |
6296 | enum { |
6297 | NAPI_F_PREFER_BUSY_POLL = 1, | |
b4e8ae5c | 6298 | NAPI_F_END_ON_RESCHED = 2, |
13d381b4 SR |
6299 | }; |
6300 | ||
6301 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, | |
6302 | unsigned flags, u16 budget) | |
217f6974 | 6303 | { |
7fd3253a BT |
6304 | bool skip_schedule = false; |
6305 | unsigned long timeout; | |
217f6974 ED |
6306 | int rc; |
6307 | ||
39e6c820 ED |
6308 | /* Busy polling means there is a high chance device driver hard irq |
6309 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was | |
6310 | * set in napi_schedule_prep(). | |
6311 | * Since we are about to call napi->poll() once more, we can safely | |
6312 | * clear NAPI_STATE_MISSED. | |
6313 | * | |
6314 | * Note: x86 could use a single "lock and ..." instruction | |
6315 | * to perform these two clear_bit() | |
6316 | */ | |
6317 | clear_bit(NAPI_STATE_MISSED, &napi->state); | |
217f6974 ED |
6318 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
6319 | ||
6320 | local_bh_disable(); | |
6321 | ||
13d381b4 | 6322 | if (flags & NAPI_F_PREFER_BUSY_POLL) { |
7fd3253a BT |
6323 | napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs); |
6324 | timeout = READ_ONCE(napi->dev->gro_flush_timeout); | |
6325 | if (napi->defer_hard_irqs_count && timeout) { | |
6326 | hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED); | |
6327 | skip_schedule = true; | |
6328 | } | |
6329 | } | |
6330 | ||
217f6974 ED |
6331 | /* All we really want here is to re-enable device interrupts. |
6332 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. | |
6333 | */ | |
7c951caf | 6334 | rc = napi->poll(napi, budget); |
323ebb61 EC |
6335 | /* We can't gro_normal_list() here, because napi->poll() might have |
6336 | * rearmed the napi (napi_complete_done()) in which case it could | |
6337 | * already be running on another CPU. | |
6338 | */ | |
7c951caf | 6339 | trace_napi_poll(napi, rc, budget); |
217f6974 | 6340 | netpoll_poll_unlock(have_poll_lock); |
7c951caf | 6341 | if (rc == budget) |
7fd3253a | 6342 | __busy_poll_stop(napi, skip_schedule); |
217f6974 | 6343 | local_bh_enable(); |
217f6974 ED |
6344 | } |
6345 | ||
13d381b4 SR |
6346 | static void __napi_busy_loop(unsigned int napi_id, |
6347 | bool (*loop_end)(void *, unsigned long), | |
6348 | void *loop_end_arg, unsigned flags, u16 budget) | |
02d62e86 | 6349 | { |
7db6b048 | 6350 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
217f6974 | 6351 | int (*napi_poll)(struct napi_struct *napi, int budget); |
217f6974 | 6352 | void *have_poll_lock = NULL; |
02d62e86 | 6353 | struct napi_struct *napi; |
217f6974 | 6354 | |
13d381b4 SR |
6355 | WARN_ON_ONCE(!rcu_read_lock_held()); |
6356 | ||
217f6974 | 6357 | restart: |
217f6974 | 6358 | napi_poll = NULL; |
02d62e86 | 6359 | |
545cd5e5 | 6360 | napi = napi_by_id(napi_id); |
02d62e86 | 6361 | if (!napi) |
13d381b4 | 6362 | return; |
02d62e86 | 6363 | |
c857946a KK |
6364 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
6365 | preempt_disable(); | |
217f6974 | 6366 | for (;;) { |
2b5cd0df AD |
6367 | int work = 0; |
6368 | ||
2a028ecb | 6369 | local_bh_disable(); |
217f6974 ED |
6370 | if (!napi_poll) { |
6371 | unsigned long val = READ_ONCE(napi->state); | |
6372 | ||
6373 | /* If multiple threads are competing for this napi, | |
6374 | * we avoid dirtying napi->state as much as we can. | |
6375 | */ | |
6376 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | | |
7fd3253a | 6377 | NAPIF_STATE_IN_BUSY_POLL)) { |
13d381b4 | 6378 | if (flags & NAPI_F_PREFER_BUSY_POLL) |
7fd3253a | 6379 | set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); |
217f6974 | 6380 | goto count; |
7fd3253a | 6381 | } |
217f6974 ED |
6382 | if (cmpxchg(&napi->state, val, |
6383 | val | NAPIF_STATE_IN_BUSY_POLL | | |
7fd3253a | 6384 | NAPIF_STATE_SCHED) != val) { |
13d381b4 | 6385 | if (flags & NAPI_F_PREFER_BUSY_POLL) |
7fd3253a | 6386 | set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); |
217f6974 | 6387 | goto count; |
7fd3253a | 6388 | } |
217f6974 ED |
6389 | have_poll_lock = netpoll_poll_lock(napi); |
6390 | napi_poll = napi->poll; | |
6391 | } | |
7c951caf BT |
6392 | work = napi_poll(napi, budget); |
6393 | trace_napi_poll(napi, work, budget); | |
323ebb61 | 6394 | gro_normal_list(napi); |
217f6974 | 6395 | count: |
2b5cd0df | 6396 | if (work > 0) |
7db6b048 | 6397 | __NET_ADD_STATS(dev_net(napi->dev), |
2b5cd0df | 6398 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
1200097f | 6399 | skb_defer_free_flush(this_cpu_ptr(&softnet_data)); |
2a028ecb | 6400 | local_bh_enable(); |
02d62e86 | 6401 | |
7db6b048 | 6402 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
217f6974 | 6403 | break; |
02d62e86 | 6404 | |
217f6974 | 6405 | if (unlikely(need_resched())) { |
b4e8ae5c SR |
6406 | if (flags & NAPI_F_END_ON_RESCHED) |
6407 | break; | |
217f6974 | 6408 | if (napi_poll) |
13d381b4 | 6409 | busy_poll_stop(napi, have_poll_lock, flags, budget); |
c857946a KK |
6410 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
6411 | preempt_enable(); | |
217f6974 ED |
6412 | rcu_read_unlock(); |
6413 | cond_resched(); | |
13d381b4 | 6414 | rcu_read_lock(); |
7db6b048 | 6415 | if (loop_end(loop_end_arg, start_time)) |
2b5cd0df | 6416 | return; |
217f6974 ED |
6417 | goto restart; |
6418 | } | |
6cdf89b1 | 6419 | cpu_relax(); |
217f6974 ED |
6420 | } |
6421 | if (napi_poll) | |
13d381b4 | 6422 | busy_poll_stop(napi, have_poll_lock, flags, budget); |
c857946a KK |
6423 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
6424 | preempt_enable(); | |
13d381b4 SR |
6425 | } |
6426 | ||
b4e8ae5c SR |
6427 | void napi_busy_loop_rcu(unsigned int napi_id, |
6428 | bool (*loop_end)(void *, unsigned long), | |
6429 | void *loop_end_arg, bool prefer_busy_poll, u16 budget) | |
6430 | { | |
6431 | unsigned flags = NAPI_F_END_ON_RESCHED; | |
6432 | ||
6433 | if (prefer_busy_poll) | |
6434 | flags |= NAPI_F_PREFER_BUSY_POLL; | |
6435 | ||
6436 | __napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget); | |
6437 | } | |
6438 | ||
13d381b4 SR |
6439 | void napi_busy_loop(unsigned int napi_id, |
6440 | bool (*loop_end)(void *, unsigned long), | |
6441 | void *loop_end_arg, bool prefer_busy_poll, u16 budget) | |
6442 | { | |
6443 | unsigned flags = prefer_busy_poll ? NAPI_F_PREFER_BUSY_POLL : 0; | |
6444 | ||
6445 | rcu_read_lock(); | |
6446 | __napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget); | |
2a028ecb | 6447 | rcu_read_unlock(); |
02d62e86 | 6448 | } |
7db6b048 | 6449 | EXPORT_SYMBOL(napi_busy_loop); |
02d62e86 ED |
6450 | |
6451 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
af12fa6e | 6452 | |
149d6ad8 | 6453 | static void napi_hash_add(struct napi_struct *napi) |
af12fa6e | 6454 | { |
4d092dd2 | 6455 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state)) |
52bd2d62 | 6456 | return; |
af12fa6e | 6457 | |
52bd2d62 | 6458 | spin_lock(&napi_hash_lock); |
af12fa6e | 6459 | |
545cd5e5 | 6460 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
52bd2d62 | 6461 | do { |
545cd5e5 AD |
6462 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
6463 | napi_gen_id = MIN_NAPI_ID; | |
52bd2d62 ED |
6464 | } while (napi_by_id(napi_gen_id)); |
6465 | napi->napi_id = napi_gen_id; | |
af12fa6e | 6466 | |
52bd2d62 ED |
6467 | hlist_add_head_rcu(&napi->napi_hash_node, |
6468 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); | |
af12fa6e | 6469 | |
52bd2d62 | 6470 | spin_unlock(&napi_hash_lock); |
af12fa6e | 6471 | } |
af12fa6e ET |
6472 | |
6473 | /* Warning : caller is responsible to make sure rcu grace period | |
6474 | * is respected before freeing memory containing @napi | |
6475 | */ | |
5198d545 | 6476 | static void napi_hash_del(struct napi_struct *napi) |
af12fa6e ET |
6477 | { |
6478 | spin_lock(&napi_hash_lock); | |
6479 | ||
4d092dd2 | 6480 | hlist_del_init_rcu(&napi->napi_hash_node); |
5198d545 | 6481 | |
af12fa6e ET |
6482 | spin_unlock(&napi_hash_lock); |
6483 | } | |
af12fa6e | 6484 | |
3b47d303 ED |
6485 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
6486 | { | |
6487 | struct napi_struct *napi; | |
6488 | ||
6489 | napi = container_of(timer, struct napi_struct, timer); | |
39e6c820 ED |
6490 | |
6491 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting | |
6492 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. | |
6493 | */ | |
6f8b12d6 | 6494 | if (!napi_disable_pending(napi) && |
7fd3253a BT |
6495 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) { |
6496 | clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
39e6c820 | 6497 | __napi_schedule_irqoff(napi); |
7fd3253a | 6498 | } |
3b47d303 ED |
6499 | |
6500 | return HRTIMER_NORESTART; | |
6501 | } | |
6502 | ||
7c4ec749 | 6503 | static void init_gro_hash(struct napi_struct *napi) |
d565b0a1 | 6504 | { |
07d78363 DM |
6505 | int i; |
6506 | ||
6312fe77 LR |
6507 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6508 | INIT_LIST_HEAD(&napi->gro_hash[i].list); | |
6509 | napi->gro_hash[i].count = 0; | |
6510 | } | |
7c4ec749 DM |
6511 | napi->gro_bitmask = 0; |
6512 | } | |
6513 | ||
5fdd2f0e WW |
6514 | int dev_set_threaded(struct net_device *dev, bool threaded) |
6515 | { | |
6516 | struct napi_struct *napi; | |
6517 | int err = 0; | |
6518 | ||
6519 | if (dev->threaded == threaded) | |
6520 | return 0; | |
6521 | ||
6522 | if (threaded) { | |
6523 | list_for_each_entry(napi, &dev->napi_list, dev_list) { | |
6524 | if (!napi->thread) { | |
6525 | err = napi_kthread_create(napi); | |
6526 | if (err) { | |
6527 | threaded = false; | |
6528 | break; | |
6529 | } | |
6530 | } | |
6531 | } | |
6532 | } | |
6533 | ||
c1742dcb | 6534 | WRITE_ONCE(dev->threaded, threaded); |
5fdd2f0e WW |
6535 | |
6536 | /* Make sure kthread is created before THREADED bit | |
6537 | * is set. | |
6538 | */ | |
6539 | smp_mb__before_atomic(); | |
6540 | ||
6541 | /* Setting/unsetting threaded mode on a napi might not immediately | |
6542 | * take effect, if the current napi instance is actively being | |
6543 | * polled. In this case, the switch between threaded mode and | |
6544 | * softirq mode will happen in the next round of napi_schedule(). | |
6545 | * This should not cause hiccups/stalls to the live traffic. | |
6546 | */ | |
274c4a6d AS |
6547 | list_for_each_entry(napi, &dev->napi_list, dev_list) |
6548 | assign_bit(NAPI_STATE_THREADED, &napi->state, threaded); | |
5fdd2f0e WW |
6549 | |
6550 | return err; | |
6551 | } | |
8f64860f | 6552 | EXPORT_SYMBOL(dev_set_threaded); |
5fdd2f0e | 6553 | |
2a502ff0 AN |
6554 | /** |
6555 | * netif_queue_set_napi - Associate queue with the napi | |
6556 | * @dev: device to which NAPI and queue belong | |
6557 | * @queue_index: Index of queue | |
6558 | * @type: queue type as RX or TX | |
6559 | * @napi: NAPI context, pass NULL to clear previously set NAPI | |
6560 | * | |
6561 | * Set queue with its corresponding napi context. This should be done after | |
6562 | * registering the NAPI handler for the queue-vector and the queues have been | |
6563 | * mapped to the corresponding interrupt vector. | |
6564 | */ | |
6565 | void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index, | |
6566 | enum netdev_queue_type type, struct napi_struct *napi) | |
6567 | { | |
6568 | struct netdev_rx_queue *rxq; | |
6569 | struct netdev_queue *txq; | |
6570 | ||
6571 | if (WARN_ON_ONCE(napi && !napi->dev)) | |
6572 | return; | |
6573 | if (dev->reg_state >= NETREG_REGISTERED) | |
6574 | ASSERT_RTNL(); | |
6575 | ||
6576 | switch (type) { | |
6577 | case NETDEV_QUEUE_TYPE_RX: | |
6578 | rxq = __netif_get_rx_queue(dev, queue_index); | |
6579 | rxq->napi = napi; | |
6580 | return; | |
6581 | case NETDEV_QUEUE_TYPE_TX: | |
6582 | txq = netdev_get_tx_queue(dev, queue_index); | |
6583 | txq->napi = napi; | |
6584 | return; | |
6585 | default: | |
6586 | return; | |
6587 | } | |
6588 | } | |
6589 | EXPORT_SYMBOL(netif_queue_set_napi); | |
6590 | ||
58caed3d JK |
6591 | void netif_napi_add_weight(struct net_device *dev, struct napi_struct *napi, |
6592 | int (*poll)(struct napi_struct *, int), int weight) | |
7c4ec749 | 6593 | { |
4d092dd2 JK |
6594 | if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state))) |
6595 | return; | |
6596 | ||
7c4ec749 | 6597 | INIT_LIST_HEAD(&napi->poll_list); |
4d092dd2 | 6598 | INIT_HLIST_NODE(&napi->napi_hash_node); |
7c4ec749 DM |
6599 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
6600 | napi->timer.function = napi_watchdog; | |
6601 | init_gro_hash(napi); | |
5d38a079 | 6602 | napi->skb = NULL; |
323ebb61 EC |
6603 | INIT_LIST_HEAD(&napi->rx_list); |
6604 | napi->rx_count = 0; | |
d565b0a1 | 6605 | napi->poll = poll; |
82dc3c63 | 6606 | if (weight > NAPI_POLL_WEIGHT) |
bf29e9e9 QC |
6607 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
6608 | weight); | |
d565b0a1 | 6609 | napi->weight = weight; |
d565b0a1 | 6610 | napi->dev = dev; |
5d38a079 | 6611 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
6612 | napi->poll_owner = -1; |
6613 | #endif | |
8c48eea3 | 6614 | napi->list_owner = -1; |
d565b0a1 | 6615 | set_bit(NAPI_STATE_SCHED, &napi->state); |
96e97bc0 JK |
6616 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
6617 | list_add_rcu(&napi->dev_list, &dev->napi_list); | |
93d05d4a | 6618 | napi_hash_add(napi); |
fd9ea57f | 6619 | napi_get_frags_check(napi); |
29863d41 WW |
6620 | /* Create kthread for this napi if dev->threaded is set. |
6621 | * Clear dev->threaded if kthread creation failed so that | |
6622 | * threaded mode will not be enabled in napi_enable(). | |
6623 | */ | |
6624 | if (dev->threaded && napi_kthread_create(napi)) | |
c1742dcb | 6625 | dev->threaded = false; |
26793bfb | 6626 | netif_napi_set_irq(napi, -1); |
d565b0a1 | 6627 | } |
58caed3d | 6628 | EXPORT_SYMBOL(netif_napi_add_weight); |
d565b0a1 | 6629 | |
3b47d303 ED |
6630 | void napi_disable(struct napi_struct *n) |
6631 | { | |
719c5719 JK |
6632 | unsigned long val, new; |
6633 | ||
3b47d303 ED |
6634 | might_sleep(); |
6635 | set_bit(NAPI_STATE_DISABLE, &n->state); | |
6636 | ||
4ffa1d1c ED |
6637 | val = READ_ONCE(n->state); |
6638 | do { | |
fd896e38 | 6639 | while (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) { |
719c5719 | 6640 | usleep_range(20, 200); |
fd896e38 | 6641 | val = READ_ONCE(n->state); |
719c5719 JK |
6642 | } |
6643 | ||
6644 | new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC; | |
6645 | new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL); | |
4ffa1d1c | 6646 | } while (!try_cmpxchg(&n->state, &val, new)); |
3b47d303 ED |
6647 | |
6648 | hrtimer_cancel(&n->timer); | |
6649 | ||
6650 | clear_bit(NAPI_STATE_DISABLE, &n->state); | |
6651 | } | |
6652 | EXPORT_SYMBOL(napi_disable); | |
6653 | ||
29863d41 WW |
6654 | /** |
6655 | * napi_enable - enable NAPI scheduling | |
6656 | * @n: NAPI context | |
6657 | * | |
6658 | * Resume NAPI from being scheduled on this context. | |
6659 | * Must be paired with napi_disable. | |
6660 | */ | |
6661 | void napi_enable(struct napi_struct *n) | |
6662 | { | |
4ffa1d1c | 6663 | unsigned long new, val = READ_ONCE(n->state); |
3765996e XZ |
6664 | |
6665 | do { | |
3765996e XZ |
6666 | BUG_ON(!test_bit(NAPI_STATE_SCHED, &val)); |
6667 | ||
6668 | new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC); | |
6669 | if (n->dev->threaded && n->thread) | |
6670 | new |= NAPIF_STATE_THREADED; | |
4ffa1d1c | 6671 | } while (!try_cmpxchg(&n->state, &val, new)); |
29863d41 WW |
6672 | } |
6673 | EXPORT_SYMBOL(napi_enable); | |
6674 | ||
07d78363 | 6675 | static void flush_gro_hash(struct napi_struct *napi) |
d4546c25 | 6676 | { |
07d78363 | 6677 | int i; |
d4546c25 | 6678 | |
07d78363 DM |
6679 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6680 | struct sk_buff *skb, *n; | |
6681 | ||
6312fe77 | 6682 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
07d78363 | 6683 | kfree_skb(skb); |
6312fe77 | 6684 | napi->gro_hash[i].count = 0; |
07d78363 | 6685 | } |
d4546c25 DM |
6686 | } |
6687 | ||
93d05d4a | 6688 | /* Must be called in process context */ |
5198d545 | 6689 | void __netif_napi_del(struct napi_struct *napi) |
d565b0a1 | 6690 | { |
4d092dd2 JK |
6691 | if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state)) |
6692 | return; | |
6693 | ||
5198d545 | 6694 | napi_hash_del(napi); |
5251ef82 | 6695 | list_del_rcu(&napi->dev_list); |
76620aaf | 6696 | napi_free_frags(napi); |
d565b0a1 | 6697 | |
07d78363 | 6698 | flush_gro_hash(napi); |
d9f37d01 | 6699 | napi->gro_bitmask = 0; |
29863d41 WW |
6700 | |
6701 | if (napi->thread) { | |
6702 | kthread_stop(napi->thread); | |
6703 | napi->thread = NULL; | |
6704 | } | |
d565b0a1 | 6705 | } |
5198d545 | 6706 | EXPORT_SYMBOL(__netif_napi_del); |
d565b0a1 | 6707 | |
898f8015 | 6708 | static int __napi_poll(struct napi_struct *n, bool *repoll) |
726ce70e | 6709 | { |
726ce70e HX |
6710 | int work, weight; |
6711 | ||
726ce70e HX |
6712 | weight = n->weight; |
6713 | ||
6714 | /* This NAPI_STATE_SCHED test is for avoiding a race | |
6715 | * with netpoll's poll_napi(). Only the entity which | |
6716 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
6717 | * actually make the ->poll() call. Therefore we avoid | |
6718 | * accidentally calling ->poll() when NAPI is not scheduled. | |
6719 | */ | |
6720 | work = 0; | |
7f3eb217 | 6721 | if (napi_is_scheduled(n)) { |
726ce70e | 6722 | work = n->poll(n, weight); |
1db19db7 | 6723 | trace_napi_poll(n, work, weight); |
9a675ba5 SAS |
6724 | |
6725 | xdp_do_check_flushed(n); | |
726ce70e HX |
6726 | } |
6727 | ||
427d5838 | 6728 | if (unlikely(work > weight)) |
5b92be64 JB |
6729 | netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n", |
6730 | n->poll, work, weight); | |
726ce70e HX |
6731 | |
6732 | if (likely(work < weight)) | |
898f8015 | 6733 | return work; |
726ce70e HX |
6734 | |
6735 | /* Drivers must not modify the NAPI state if they | |
6736 | * consume the entire weight. In such cases this code | |
6737 | * still "owns" the NAPI instance and therefore can | |
6738 | * move the instance around on the list at-will. | |
6739 | */ | |
6740 | if (unlikely(napi_disable_pending(n))) { | |
6741 | napi_complete(n); | |
898f8015 | 6742 | return work; |
726ce70e HX |
6743 | } |
6744 | ||
7fd3253a BT |
6745 | /* The NAPI context has more processing work, but busy-polling |
6746 | * is preferred. Exit early. | |
6747 | */ | |
6748 | if (napi_prefer_busy_poll(n)) { | |
6749 | if (napi_complete_done(n, work)) { | |
6750 | /* If timeout is not set, we need to make sure | |
6751 | * that the NAPI is re-scheduled. | |
6752 | */ | |
6753 | napi_schedule(n); | |
6754 | } | |
898f8015 | 6755 | return work; |
7fd3253a BT |
6756 | } |
6757 | ||
d9f37d01 | 6758 | if (n->gro_bitmask) { |
726ce70e HX |
6759 | /* flush too old packets |
6760 | * If HZ < 1000, flush all packets. | |
6761 | */ | |
6762 | napi_gro_flush(n, HZ >= 1000); | |
6763 | } | |
6764 | ||
c8079432 MM |
6765 | gro_normal_list(n); |
6766 | ||
001ce546 HX |
6767 | /* Some drivers may have called napi_schedule |
6768 | * prior to exhausting their budget. | |
6769 | */ | |
6770 | if (unlikely(!list_empty(&n->poll_list))) { | |
6771 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", | |
6772 | n->dev ? n->dev->name : "backlog"); | |
898f8015 | 6773 | return work; |
001ce546 HX |
6774 | } |
6775 | ||
898f8015 FF |
6776 | *repoll = true; |
6777 | ||
6778 | return work; | |
6779 | } | |
6780 | ||
6781 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) | |
6782 | { | |
6783 | bool do_repoll = false; | |
6784 | void *have; | |
6785 | int work; | |
6786 | ||
6787 | list_del_init(&n->poll_list); | |
6788 | ||
6789 | have = netpoll_poll_lock(n); | |
6790 | ||
6791 | work = __napi_poll(n, &do_repoll); | |
6792 | ||
6793 | if (do_repoll) | |
6794 | list_add_tail(&n->poll_list, repoll); | |
726ce70e | 6795 | |
726ce70e HX |
6796 | netpoll_poll_unlock(have); |
6797 | ||
6798 | return work; | |
6799 | } | |
6800 | ||
29863d41 WW |
6801 | static int napi_thread_wait(struct napi_struct *napi) |
6802 | { | |
6803 | set_current_state(TASK_INTERRUPTIBLE); | |
6804 | ||
27f0ad71 | 6805 | while (!kthread_should_stop()) { |
cb038357 WW |
6806 | /* Testing SCHED_THREADED bit here to make sure the current |
6807 | * kthread owns this napi and could poll on this napi. | |
6808 | * Testing SCHED bit is not enough because SCHED bit might be | |
6809 | * set by some other busy poll thread or by napi_disable(). | |
6810 | */ | |
56364c91 | 6811 | if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state)) { |
29863d41 WW |
6812 | WARN_ON(!list_empty(&napi->poll_list)); |
6813 | __set_current_state(TASK_RUNNING); | |
6814 | return 0; | |
6815 | } | |
6816 | ||
6817 | schedule(); | |
6818 | set_current_state(TASK_INTERRUPTIBLE); | |
6819 | } | |
6820 | __set_current_state(TASK_RUNNING); | |
27f0ad71 | 6821 | |
29863d41 WW |
6822 | return -1; |
6823 | } | |
6824 | ||
dad6b977 | 6825 | static void napi_threaded_poll_loop(struct napi_struct *napi) |
29863d41 | 6826 | { |
a1aaee7f | 6827 | struct softnet_data *sd; |
dad6b977 | 6828 | unsigned long last_qs = jiffies; |
d6dbbb11 | 6829 | |
dad6b977 SAS |
6830 | for (;;) { |
6831 | bool repoll = false; | |
6832 | void *have; | |
29863d41 | 6833 | |
dad6b977 SAS |
6834 | local_bh_disable(); |
6835 | sd = this_cpu_ptr(&softnet_data); | |
6836 | sd->in_napi_threaded_poll = true; | |
29863d41 | 6837 | |
dad6b977 SAS |
6838 | have = netpoll_poll_lock(napi); |
6839 | __napi_poll(napi, &repoll); | |
6840 | netpoll_poll_unlock(have); | |
29863d41 | 6841 | |
dad6b977 SAS |
6842 | sd->in_napi_threaded_poll = false; |
6843 | barrier(); | |
87eff2ec | 6844 | |
dad6b977 SAS |
6845 | if (sd_has_rps_ipi_waiting(sd)) { |
6846 | local_irq_disable(); | |
6847 | net_rps_action_and_irq_enable(sd); | |
6848 | } | |
6849 | skb_defer_free_flush(sd); | |
6850 | local_bh_enable(); | |
29863d41 | 6851 | |
dad6b977 SAS |
6852 | if (!repoll) |
6853 | break; | |
29863d41 | 6854 | |
dad6b977 SAS |
6855 | rcu_softirq_qs_periodic(last_qs); |
6856 | cond_resched(); | |
29863d41 | 6857 | } |
dad6b977 SAS |
6858 | } |
6859 | ||
6860 | static int napi_threaded_poll(void *data) | |
6861 | { | |
6862 | struct napi_struct *napi = data; | |
6863 | ||
6864 | while (!napi_thread_wait(napi)) | |
6865 | napi_threaded_poll_loop(napi); | |
6866 | ||
29863d41 WW |
6867 | return 0; |
6868 | } | |
6869 | ||
0766f788 | 6870 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
1da177e4 | 6871 | { |
903ceff7 | 6872 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
7acf8a1e | 6873 | unsigned long time_limit = jiffies + |
ae6e22f7 ED |
6874 | usecs_to_jiffies(READ_ONCE(net_hotdata.netdev_budget_usecs)); |
6875 | int budget = READ_ONCE(net_hotdata.netdev_budget); | |
d75b1ade ED |
6876 | LIST_HEAD(list); |
6877 | LIST_HEAD(repoll); | |
53fb95d3 | 6878 | |
8b43fd3d | 6879 | start: |
c59647c0 | 6880 | sd->in_net_rx_action = true; |
1da177e4 | 6881 | local_irq_disable(); |
d75b1ade ED |
6882 | list_splice_init(&sd->poll_list, &list); |
6883 | local_irq_enable(); | |
1da177e4 | 6884 | |
ceb8d5bf | 6885 | for (;;) { |
bea3348e | 6886 | struct napi_struct *n; |
1da177e4 | 6887 | |
90987650 ED |
6888 | skb_defer_free_flush(sd); |
6889 | ||
ceb8d5bf | 6890 | if (list_empty(&list)) { |
8b43fd3d ED |
6891 | if (list_empty(&repoll)) { |
6892 | sd->in_net_rx_action = false; | |
6893 | barrier(); | |
6894 | /* We need to check if ____napi_schedule() | |
6895 | * had refilled poll_list while | |
6896 | * sd->in_net_rx_action was true. | |
6897 | */ | |
6898 | if (!list_empty(&sd->poll_list)) | |
6899 | goto start; | |
6900 | if (!sd_has_rps_ipi_waiting(sd)) | |
6901 | goto end; | |
6902 | } | |
ceb8d5bf HX |
6903 | break; |
6904 | } | |
6905 | ||
6bd373eb HX |
6906 | n = list_first_entry(&list, struct napi_struct, poll_list); |
6907 | budget -= napi_poll(n, &repoll); | |
6908 | ||
d75b1ade | 6909 | /* If softirq window is exhausted then punt. |
24f8b238 SH |
6910 | * Allow this to run for 2 jiffies since which will allow |
6911 | * an average latency of 1.5/HZ. | |
bea3348e | 6912 | */ |
ceb8d5bf HX |
6913 | if (unlikely(budget <= 0 || |
6914 | time_after_eq(jiffies, time_limit))) { | |
6915 | sd->time_squeeze++; | |
6916 | break; | |
6917 | } | |
1da177e4 | 6918 | } |
d75b1ade | 6919 | |
d75b1ade ED |
6920 | local_irq_disable(); |
6921 | ||
6922 | list_splice_tail_init(&sd->poll_list, &list); | |
6923 | list_splice_tail(&repoll, &list); | |
6924 | list_splice(&list, &sd->poll_list); | |
6925 | if (!list_empty(&sd->poll_list)) | |
6926 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
c59647c0 ED |
6927 | else |
6928 | sd->in_net_rx_action = false; | |
d75b1ade | 6929 | |
e326bed2 | 6930 | net_rps_action_and_irq_enable(sd); |
90987650 | 6931 | end:; |
1da177e4 LT |
6932 | } |
6933 | ||
aa9d8560 | 6934 | struct netdev_adjacent { |
9ff162a8 | 6935 | struct net_device *dev; |
f77159a3 | 6936 | netdevice_tracker dev_tracker; |
5d261913 VF |
6937 | |
6938 | /* upper master flag, there can only be one master device per list */ | |
9ff162a8 | 6939 | bool master; |
5d261913 | 6940 | |
32b6d34f TY |
6941 | /* lookup ignore flag */ |
6942 | bool ignore; | |
6943 | ||
5d261913 VF |
6944 | /* counter for the number of times this device was added to us */ |
6945 | u16 ref_nr; | |
6946 | ||
402dae96 VF |
6947 | /* private field for the users */ |
6948 | void *private; | |
6949 | ||
9ff162a8 JP |
6950 | struct list_head list; |
6951 | struct rcu_head rcu; | |
9ff162a8 JP |
6952 | }; |
6953 | ||
6ea29da1 | 6954 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
2f268f12 | 6955 | struct list_head *adj_list) |
9ff162a8 | 6956 | { |
5d261913 | 6957 | struct netdev_adjacent *adj; |
5d261913 | 6958 | |
2f268f12 | 6959 | list_for_each_entry(adj, adj_list, list) { |
5d261913 VF |
6960 | if (adj->dev == adj_dev) |
6961 | return adj; | |
9ff162a8 JP |
6962 | } |
6963 | return NULL; | |
6964 | } | |
6965 | ||
eff74233 TY |
6966 | static int ____netdev_has_upper_dev(struct net_device *upper_dev, |
6967 | struct netdev_nested_priv *priv) | |
f1170fd4 | 6968 | { |
eff74233 | 6969 | struct net_device *dev = (struct net_device *)priv->data; |
f1170fd4 DA |
6970 | |
6971 | return upper_dev == dev; | |
6972 | } | |
6973 | ||
9ff162a8 JP |
6974 | /** |
6975 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
6976 | * @dev: device | |
6977 | * @upper_dev: upper device to check | |
6978 | * | |
6979 | * Find out if a device is linked to specified upper device and return true | |
6980 | * in case it is. Note that this checks only immediate upper device, | |
6981 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
6982 | */ | |
6983 | bool netdev_has_upper_dev(struct net_device *dev, | |
6984 | struct net_device *upper_dev) | |
6985 | { | |
eff74233 TY |
6986 | struct netdev_nested_priv priv = { |
6987 | .data = (void *)upper_dev, | |
6988 | }; | |
6989 | ||
9ff162a8 JP |
6990 | ASSERT_RTNL(); |
6991 | ||
32b6d34f | 6992 | return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
eff74233 | 6993 | &priv); |
9ff162a8 JP |
6994 | } |
6995 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
6996 | ||
1a3f060c | 6997 | /** |
c1639be9 | 6998 | * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device |
1a3f060c DA |
6999 | * @dev: device |
7000 | * @upper_dev: upper device to check | |
7001 | * | |
7002 | * Find out if a device is linked to specified upper device and return true | |
7003 | * in case it is. Note that this checks the entire upper device chain. | |
7004 | * The caller must hold rcu lock. | |
7005 | */ | |
7006 | ||
1a3f060c DA |
7007 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
7008 | struct net_device *upper_dev) | |
7009 | { | |
eff74233 TY |
7010 | struct netdev_nested_priv priv = { |
7011 | .data = (void *)upper_dev, | |
7012 | }; | |
7013 | ||
32b6d34f | 7014 | return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
eff74233 | 7015 | &priv); |
1a3f060c DA |
7016 | } |
7017 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); | |
7018 | ||
9ff162a8 JP |
7019 | /** |
7020 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
7021 | * @dev: device | |
7022 | * | |
7023 | * Find out if a device is linked to an upper device and return true in case | |
7024 | * it is. The caller must hold the RTNL lock. | |
7025 | */ | |
25cc72a3 | 7026 | bool netdev_has_any_upper_dev(struct net_device *dev) |
9ff162a8 JP |
7027 | { |
7028 | ASSERT_RTNL(); | |
7029 | ||
f1170fd4 | 7030 | return !list_empty(&dev->adj_list.upper); |
9ff162a8 | 7031 | } |
25cc72a3 | 7032 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
9ff162a8 JP |
7033 | |
7034 | /** | |
7035 | * netdev_master_upper_dev_get - Get master upper device | |
7036 | * @dev: device | |
7037 | * | |
7038 | * Find a master upper device and return pointer to it or NULL in case | |
7039 | * it's not there. The caller must hold the RTNL lock. | |
7040 | */ | |
7041 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
7042 | { | |
aa9d8560 | 7043 | struct netdev_adjacent *upper; |
9ff162a8 JP |
7044 | |
7045 | ASSERT_RTNL(); | |
7046 | ||
2f268f12 | 7047 | if (list_empty(&dev->adj_list.upper)) |
9ff162a8 JP |
7048 | return NULL; |
7049 | ||
2f268f12 | 7050 | upper = list_first_entry(&dev->adj_list.upper, |
aa9d8560 | 7051 | struct netdev_adjacent, list); |
9ff162a8 JP |
7052 | if (likely(upper->master)) |
7053 | return upper->dev; | |
7054 | return NULL; | |
7055 | } | |
7056 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
7057 | ||
32b6d34f TY |
7058 | static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) |
7059 | { | |
7060 | struct netdev_adjacent *upper; | |
7061 | ||
7062 | ASSERT_RTNL(); | |
7063 | ||
7064 | if (list_empty(&dev->adj_list.upper)) | |
7065 | return NULL; | |
7066 | ||
7067 | upper = list_first_entry(&dev->adj_list.upper, | |
7068 | struct netdev_adjacent, list); | |
7069 | if (likely(upper->master) && !upper->ignore) | |
7070 | return upper->dev; | |
7071 | return NULL; | |
7072 | } | |
7073 | ||
0f524a80 DA |
7074 | /** |
7075 | * netdev_has_any_lower_dev - Check if device is linked to some device | |
7076 | * @dev: device | |
7077 | * | |
7078 | * Find out if a device is linked to a lower device and return true in case | |
7079 | * it is. The caller must hold the RTNL lock. | |
7080 | */ | |
7081 | static bool netdev_has_any_lower_dev(struct net_device *dev) | |
7082 | { | |
7083 | ASSERT_RTNL(); | |
7084 | ||
7085 | return !list_empty(&dev->adj_list.lower); | |
7086 | } | |
7087 | ||
b6ccba4c VF |
7088 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
7089 | { | |
7090 | struct netdev_adjacent *adj; | |
7091 | ||
7092 | adj = list_entry(adj_list, struct netdev_adjacent, list); | |
7093 | ||
7094 | return adj->private; | |
7095 | } | |
7096 | EXPORT_SYMBOL(netdev_adjacent_get_private); | |
7097 | ||
44a40855 VY |
7098 | /** |
7099 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list | |
7100 | * @dev: device | |
7101 | * @iter: list_head ** of the current position | |
7102 | * | |
7103 | * Gets the next device from the dev's upper list, starting from iter | |
7104 | * position. The caller must hold RCU read lock. | |
7105 | */ | |
7106 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, | |
7107 | struct list_head **iter) | |
7108 | { | |
7109 | struct netdev_adjacent *upper; | |
7110 | ||
7111 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
7112 | ||
7113 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7114 | ||
7115 | if (&upper->list == &dev->adj_list.upper) | |
7116 | return NULL; | |
7117 | ||
7118 | *iter = &upper->list; | |
7119 | ||
7120 | return upper->dev; | |
7121 | } | |
7122 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); | |
7123 | ||
32b6d34f TY |
7124 | static struct net_device *__netdev_next_upper_dev(struct net_device *dev, |
7125 | struct list_head **iter, | |
7126 | bool *ignore) | |
5343da4c TY |
7127 | { |
7128 | struct netdev_adjacent *upper; | |
7129 | ||
7130 | upper = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7131 | ||
7132 | if (&upper->list == &dev->adj_list.upper) | |
7133 | return NULL; | |
7134 | ||
7135 | *iter = &upper->list; | |
32b6d34f | 7136 | *ignore = upper->ignore; |
5343da4c TY |
7137 | |
7138 | return upper->dev; | |
7139 | } | |
7140 | ||
1a3f060c DA |
7141 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
7142 | struct list_head **iter) | |
7143 | { | |
7144 | struct netdev_adjacent *upper; | |
7145 | ||
7146 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
7147 | ||
7148 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7149 | ||
7150 | if (&upper->list == &dev->adj_list.upper) | |
7151 | return NULL; | |
7152 | ||
7153 | *iter = &upper->list; | |
7154 | ||
7155 | return upper->dev; | |
7156 | } | |
7157 | ||
32b6d34f TY |
7158 | static int __netdev_walk_all_upper_dev(struct net_device *dev, |
7159 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7160 | struct netdev_nested_priv *priv), |
7161 | struct netdev_nested_priv *priv) | |
5343da4c TY |
7162 | { |
7163 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7164 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7165 | int ret, cur = 0; | |
32b6d34f | 7166 | bool ignore; |
5343da4c TY |
7167 | |
7168 | now = dev; | |
7169 | iter = &dev->adj_list.upper; | |
7170 | ||
7171 | while (1) { | |
7172 | if (now != dev) { | |
eff74233 | 7173 | ret = fn(now, priv); |
5343da4c TY |
7174 | if (ret) |
7175 | return ret; | |
7176 | } | |
7177 | ||
7178 | next = NULL; | |
7179 | while (1) { | |
32b6d34f | 7180 | udev = __netdev_next_upper_dev(now, &iter, &ignore); |
5343da4c TY |
7181 | if (!udev) |
7182 | break; | |
32b6d34f TY |
7183 | if (ignore) |
7184 | continue; | |
5343da4c TY |
7185 | |
7186 | next = udev; | |
7187 | niter = &udev->adj_list.upper; | |
7188 | dev_stack[cur] = now; | |
7189 | iter_stack[cur++] = iter; | |
7190 | break; | |
7191 | } | |
7192 | ||
7193 | if (!next) { | |
7194 | if (!cur) | |
7195 | return 0; | |
7196 | next = dev_stack[--cur]; | |
7197 | niter = iter_stack[cur]; | |
7198 | } | |
7199 | ||
7200 | now = next; | |
7201 | iter = niter; | |
7202 | } | |
7203 | ||
7204 | return 0; | |
7205 | } | |
7206 | ||
1a3f060c DA |
7207 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, |
7208 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7209 | struct netdev_nested_priv *priv), |
7210 | struct netdev_nested_priv *priv) | |
1a3f060c | 7211 | { |
5343da4c TY |
7212 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7213 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7214 | int ret, cur = 0; | |
1a3f060c | 7215 | |
5343da4c TY |
7216 | now = dev; |
7217 | iter = &dev->adj_list.upper; | |
1a3f060c | 7218 | |
5343da4c TY |
7219 | while (1) { |
7220 | if (now != dev) { | |
eff74233 | 7221 | ret = fn(now, priv); |
5343da4c TY |
7222 | if (ret) |
7223 | return ret; | |
7224 | } | |
7225 | ||
7226 | next = NULL; | |
7227 | while (1) { | |
7228 | udev = netdev_next_upper_dev_rcu(now, &iter); | |
7229 | if (!udev) | |
7230 | break; | |
7231 | ||
7232 | next = udev; | |
7233 | niter = &udev->adj_list.upper; | |
7234 | dev_stack[cur] = now; | |
7235 | iter_stack[cur++] = iter; | |
7236 | break; | |
7237 | } | |
7238 | ||
7239 | if (!next) { | |
7240 | if (!cur) | |
7241 | return 0; | |
7242 | next = dev_stack[--cur]; | |
7243 | niter = iter_stack[cur]; | |
7244 | } | |
7245 | ||
7246 | now = next; | |
7247 | iter = niter; | |
1a3f060c DA |
7248 | } |
7249 | ||
7250 | return 0; | |
7251 | } | |
7252 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); | |
7253 | ||
32b6d34f TY |
7254 | static bool __netdev_has_upper_dev(struct net_device *dev, |
7255 | struct net_device *upper_dev) | |
7256 | { | |
eff74233 | 7257 | struct netdev_nested_priv priv = { |
1fc70edb | 7258 | .flags = 0, |
eff74233 TY |
7259 | .data = (void *)upper_dev, |
7260 | }; | |
7261 | ||
32b6d34f TY |
7262 | ASSERT_RTNL(); |
7263 | ||
7264 | return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, | |
eff74233 | 7265 | &priv); |
32b6d34f TY |
7266 | } |
7267 | ||
31088a11 VF |
7268 | /** |
7269 | * netdev_lower_get_next_private - Get the next ->private from the | |
7270 | * lower neighbour list | |
7271 | * @dev: device | |
7272 | * @iter: list_head ** of the current position | |
7273 | * | |
7274 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7275 | * list, starting from iter position. The caller must hold either hold the | |
7276 | * RTNL lock or its own locking that guarantees that the neighbour lower | |
b469139e | 7277 | * list will remain unchanged. |
31088a11 VF |
7278 | */ |
7279 | void *netdev_lower_get_next_private(struct net_device *dev, | |
7280 | struct list_head **iter) | |
7281 | { | |
7282 | struct netdev_adjacent *lower; | |
7283 | ||
7284 | lower = list_entry(*iter, struct netdev_adjacent, list); | |
7285 | ||
7286 | if (&lower->list == &dev->adj_list.lower) | |
7287 | return NULL; | |
7288 | ||
6859e7df | 7289 | *iter = lower->list.next; |
31088a11 VF |
7290 | |
7291 | return lower->private; | |
7292 | } | |
7293 | EXPORT_SYMBOL(netdev_lower_get_next_private); | |
7294 | ||
7295 | /** | |
7296 | * netdev_lower_get_next_private_rcu - Get the next ->private from the | |
7297 | * lower neighbour list, RCU | |
7298 | * variant | |
7299 | * @dev: device | |
7300 | * @iter: list_head ** of the current position | |
7301 | * | |
7302 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7303 | * list, starting from iter position. The caller must hold RCU read lock. | |
7304 | */ | |
7305 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, | |
7306 | struct list_head **iter) | |
7307 | { | |
7308 | struct netdev_adjacent *lower; | |
7309 | ||
68918669 | 7310 | WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); |
31088a11 VF |
7311 | |
7312 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7313 | ||
7314 | if (&lower->list == &dev->adj_list.lower) | |
7315 | return NULL; | |
7316 | ||
6859e7df | 7317 | *iter = &lower->list; |
31088a11 VF |
7318 | |
7319 | return lower->private; | |
7320 | } | |
7321 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); | |
7322 | ||
4085ebe8 VY |
7323 | /** |
7324 | * netdev_lower_get_next - Get the next device from the lower neighbour | |
7325 | * list | |
7326 | * @dev: device | |
7327 | * @iter: list_head ** of the current position | |
7328 | * | |
7329 | * Gets the next netdev_adjacent from the dev's lower neighbour | |
7330 | * list, starting from iter position. The caller must hold RTNL lock or | |
7331 | * its own locking that guarantees that the neighbour lower | |
b469139e | 7332 | * list will remain unchanged. |
4085ebe8 VY |
7333 | */ |
7334 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) | |
7335 | { | |
7336 | struct netdev_adjacent *lower; | |
7337 | ||
cfdd28be | 7338 | lower = list_entry(*iter, struct netdev_adjacent, list); |
4085ebe8 VY |
7339 | |
7340 | if (&lower->list == &dev->adj_list.lower) | |
7341 | return NULL; | |
7342 | ||
cfdd28be | 7343 | *iter = lower->list.next; |
4085ebe8 VY |
7344 | |
7345 | return lower->dev; | |
7346 | } | |
7347 | EXPORT_SYMBOL(netdev_lower_get_next); | |
7348 | ||
1a3f060c DA |
7349 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
7350 | struct list_head **iter) | |
7351 | { | |
7352 | struct netdev_adjacent *lower; | |
7353 | ||
46b5ab1a | 7354 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
1a3f060c DA |
7355 | |
7356 | if (&lower->list == &dev->adj_list.lower) | |
7357 | return NULL; | |
7358 | ||
46b5ab1a | 7359 | *iter = &lower->list; |
1a3f060c DA |
7360 | |
7361 | return lower->dev; | |
7362 | } | |
7363 | ||
32b6d34f TY |
7364 | static struct net_device *__netdev_next_lower_dev(struct net_device *dev, |
7365 | struct list_head **iter, | |
7366 | bool *ignore) | |
7367 | { | |
7368 | struct netdev_adjacent *lower; | |
7369 | ||
7370 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7371 | ||
7372 | if (&lower->list == &dev->adj_list.lower) | |
7373 | return NULL; | |
7374 | ||
7375 | *iter = &lower->list; | |
7376 | *ignore = lower->ignore; | |
7377 | ||
7378 | return lower->dev; | |
7379 | } | |
7380 | ||
1a3f060c DA |
7381 | int netdev_walk_all_lower_dev(struct net_device *dev, |
7382 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7383 | struct netdev_nested_priv *priv), |
7384 | struct netdev_nested_priv *priv) | |
1a3f060c | 7385 | { |
5343da4c TY |
7386 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7387 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7388 | int ret, cur = 0; | |
1a3f060c | 7389 | |
5343da4c TY |
7390 | now = dev; |
7391 | iter = &dev->adj_list.lower; | |
1a3f060c | 7392 | |
5343da4c TY |
7393 | while (1) { |
7394 | if (now != dev) { | |
eff74233 | 7395 | ret = fn(now, priv); |
5343da4c TY |
7396 | if (ret) |
7397 | return ret; | |
7398 | } | |
7399 | ||
7400 | next = NULL; | |
7401 | while (1) { | |
7402 | ldev = netdev_next_lower_dev(now, &iter); | |
7403 | if (!ldev) | |
7404 | break; | |
7405 | ||
7406 | next = ldev; | |
7407 | niter = &ldev->adj_list.lower; | |
7408 | dev_stack[cur] = now; | |
7409 | iter_stack[cur++] = iter; | |
7410 | break; | |
7411 | } | |
7412 | ||
7413 | if (!next) { | |
7414 | if (!cur) | |
7415 | return 0; | |
7416 | next = dev_stack[--cur]; | |
7417 | niter = iter_stack[cur]; | |
7418 | } | |
7419 | ||
7420 | now = next; | |
7421 | iter = niter; | |
1a3f060c DA |
7422 | } |
7423 | ||
7424 | return 0; | |
7425 | } | |
7426 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); | |
7427 | ||
32b6d34f TY |
7428 | static int __netdev_walk_all_lower_dev(struct net_device *dev, |
7429 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7430 | struct netdev_nested_priv *priv), |
7431 | struct netdev_nested_priv *priv) | |
32b6d34f TY |
7432 | { |
7433 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7434 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7435 | int ret, cur = 0; | |
7436 | bool ignore; | |
7437 | ||
7438 | now = dev; | |
7439 | iter = &dev->adj_list.lower; | |
7440 | ||
7441 | while (1) { | |
7442 | if (now != dev) { | |
eff74233 | 7443 | ret = fn(now, priv); |
32b6d34f TY |
7444 | if (ret) |
7445 | return ret; | |
7446 | } | |
7447 | ||
7448 | next = NULL; | |
7449 | while (1) { | |
7450 | ldev = __netdev_next_lower_dev(now, &iter, &ignore); | |
7451 | if (!ldev) | |
7452 | break; | |
7453 | if (ignore) | |
7454 | continue; | |
7455 | ||
7456 | next = ldev; | |
7457 | niter = &ldev->adj_list.lower; | |
7458 | dev_stack[cur] = now; | |
7459 | iter_stack[cur++] = iter; | |
7460 | break; | |
7461 | } | |
7462 | ||
7463 | if (!next) { | |
7464 | if (!cur) | |
7465 | return 0; | |
7466 | next = dev_stack[--cur]; | |
7467 | niter = iter_stack[cur]; | |
7468 | } | |
7469 | ||
7470 | now = next; | |
7471 | iter = niter; | |
7472 | } | |
7473 | ||
7474 | return 0; | |
7475 | } | |
7476 | ||
7151affe TY |
7477 | struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
7478 | struct list_head **iter) | |
1a3f060c DA |
7479 | { |
7480 | struct netdev_adjacent *lower; | |
7481 | ||
7482 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7483 | if (&lower->list == &dev->adj_list.lower) | |
7484 | return NULL; | |
7485 | ||
7486 | *iter = &lower->list; | |
7487 | ||
7488 | return lower->dev; | |
7489 | } | |
7151affe | 7490 | EXPORT_SYMBOL(netdev_next_lower_dev_rcu); |
1a3f060c | 7491 | |
5343da4c TY |
7492 | static u8 __netdev_upper_depth(struct net_device *dev) |
7493 | { | |
7494 | struct net_device *udev; | |
7495 | struct list_head *iter; | |
7496 | u8 max_depth = 0; | |
32b6d34f | 7497 | bool ignore; |
5343da4c TY |
7498 | |
7499 | for (iter = &dev->adj_list.upper, | |
32b6d34f | 7500 | udev = __netdev_next_upper_dev(dev, &iter, &ignore); |
5343da4c | 7501 | udev; |
32b6d34f TY |
7502 | udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { |
7503 | if (ignore) | |
7504 | continue; | |
5343da4c TY |
7505 | if (max_depth < udev->upper_level) |
7506 | max_depth = udev->upper_level; | |
7507 | } | |
7508 | ||
7509 | return max_depth; | |
7510 | } | |
7511 | ||
7512 | static u8 __netdev_lower_depth(struct net_device *dev) | |
1a3f060c DA |
7513 | { |
7514 | struct net_device *ldev; | |
7515 | struct list_head *iter; | |
5343da4c | 7516 | u8 max_depth = 0; |
32b6d34f | 7517 | bool ignore; |
1a3f060c DA |
7518 | |
7519 | for (iter = &dev->adj_list.lower, | |
32b6d34f | 7520 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore); |
1a3f060c | 7521 | ldev; |
32b6d34f TY |
7522 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { |
7523 | if (ignore) | |
7524 | continue; | |
5343da4c TY |
7525 | if (max_depth < ldev->lower_level) |
7526 | max_depth = ldev->lower_level; | |
7527 | } | |
1a3f060c | 7528 | |
5343da4c TY |
7529 | return max_depth; |
7530 | } | |
7531 | ||
eff74233 TY |
7532 | static int __netdev_update_upper_level(struct net_device *dev, |
7533 | struct netdev_nested_priv *__unused) | |
5343da4c TY |
7534 | { |
7535 | dev->upper_level = __netdev_upper_depth(dev) + 1; | |
7536 | return 0; | |
7537 | } | |
7538 | ||
f32404ae JB |
7539 | #ifdef CONFIG_LOCKDEP |
7540 | static LIST_HEAD(net_unlink_list); | |
7541 | ||
7542 | static void net_unlink_todo(struct net_device *dev) | |
7543 | { | |
7544 | if (list_empty(&dev->unlink_list)) | |
7545 | list_add_tail(&dev->unlink_list, &net_unlink_list); | |
7546 | } | |
7547 | #endif | |
7548 | ||
eff74233 | 7549 | static int __netdev_update_lower_level(struct net_device *dev, |
1fc70edb | 7550 | struct netdev_nested_priv *priv) |
5343da4c TY |
7551 | { |
7552 | dev->lower_level = __netdev_lower_depth(dev) + 1; | |
1fc70edb TY |
7553 | |
7554 | #ifdef CONFIG_LOCKDEP | |
7555 | if (!priv) | |
7556 | return 0; | |
7557 | ||
7558 | if (priv->flags & NESTED_SYNC_IMM) | |
7559 | dev->nested_level = dev->lower_level - 1; | |
7560 | if (priv->flags & NESTED_SYNC_TODO) | |
7561 | net_unlink_todo(dev); | |
7562 | #endif | |
5343da4c TY |
7563 | return 0; |
7564 | } | |
7565 | ||
7566 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, | |
7567 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7568 | struct netdev_nested_priv *priv), |
7569 | struct netdev_nested_priv *priv) | |
5343da4c TY |
7570 | { |
7571 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7572 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7573 | int ret, cur = 0; | |
7574 | ||
7575 | now = dev; | |
7576 | iter = &dev->adj_list.lower; | |
7577 | ||
7578 | while (1) { | |
7579 | if (now != dev) { | |
eff74233 | 7580 | ret = fn(now, priv); |
5343da4c TY |
7581 | if (ret) |
7582 | return ret; | |
7583 | } | |
7584 | ||
7585 | next = NULL; | |
7586 | while (1) { | |
7587 | ldev = netdev_next_lower_dev_rcu(now, &iter); | |
7588 | if (!ldev) | |
7589 | break; | |
7590 | ||
7591 | next = ldev; | |
7592 | niter = &ldev->adj_list.lower; | |
7593 | dev_stack[cur] = now; | |
7594 | iter_stack[cur++] = iter; | |
7595 | break; | |
7596 | } | |
7597 | ||
7598 | if (!next) { | |
7599 | if (!cur) | |
7600 | return 0; | |
7601 | next = dev_stack[--cur]; | |
7602 | niter = iter_stack[cur]; | |
7603 | } | |
7604 | ||
7605 | now = next; | |
7606 | iter = niter; | |
1a3f060c DA |
7607 | } |
7608 | ||
7609 | return 0; | |
7610 | } | |
7611 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); | |
7612 | ||
e001bfad | 7613 | /** |
7614 | * netdev_lower_get_first_private_rcu - Get the first ->private from the | |
7615 | * lower neighbour list, RCU | |
7616 | * variant | |
7617 | * @dev: device | |
7618 | * | |
7619 | * Gets the first netdev_adjacent->private from the dev's lower neighbour | |
7620 | * list. The caller must hold RCU read lock. | |
7621 | */ | |
7622 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) | |
7623 | { | |
7624 | struct netdev_adjacent *lower; | |
7625 | ||
7626 | lower = list_first_or_null_rcu(&dev->adj_list.lower, | |
7627 | struct netdev_adjacent, list); | |
7628 | if (lower) | |
7629 | return lower->private; | |
7630 | return NULL; | |
7631 | } | |
7632 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); | |
7633 | ||
9ff162a8 JP |
7634 | /** |
7635 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
7636 | * @dev: device | |
7637 | * | |
7638 | * Find a master upper device and return pointer to it or NULL in case | |
7639 | * it's not there. The caller must hold the RCU read lock. | |
7640 | */ | |
7641 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
7642 | { | |
aa9d8560 | 7643 | struct netdev_adjacent *upper; |
9ff162a8 | 7644 | |
2f268f12 | 7645 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
aa9d8560 | 7646 | struct netdev_adjacent, list); |
9ff162a8 JP |
7647 | if (upper && likely(upper->master)) |
7648 | return upper->dev; | |
7649 | return NULL; | |
7650 | } | |
7651 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
7652 | ||
0a59f3a9 | 7653 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
3ee32707 VF |
7654 | struct net_device *adj_dev, |
7655 | struct list_head *dev_list) | |
7656 | { | |
7657 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7658 | |
3ee32707 VF |
7659 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7660 | "upper_%s" : "lower_%s", adj_dev->name); | |
7661 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), | |
7662 | linkname); | |
7663 | } | |
0a59f3a9 | 7664 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
3ee32707 VF |
7665 | char *name, |
7666 | struct list_head *dev_list) | |
7667 | { | |
7668 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7669 | |
3ee32707 VF |
7670 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7671 | "upper_%s" : "lower_%s", name); | |
7672 | sysfs_remove_link(&(dev->dev.kobj), linkname); | |
7673 | } | |
7674 | ||
7ce64c79 AF |
7675 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
7676 | struct net_device *adj_dev, | |
7677 | struct list_head *dev_list) | |
7678 | { | |
7679 | return (dev_list == &dev->adj_list.upper || | |
7680 | dev_list == &dev->adj_list.lower) && | |
7681 | net_eq(dev_net(dev), dev_net(adj_dev)); | |
7682 | } | |
3ee32707 | 7683 | |
5d261913 VF |
7684 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
7685 | struct net_device *adj_dev, | |
7863c054 | 7686 | struct list_head *dev_list, |
402dae96 | 7687 | void *private, bool master) |
5d261913 VF |
7688 | { |
7689 | struct netdev_adjacent *adj; | |
842d67a7 | 7690 | int ret; |
5d261913 | 7691 | |
6ea29da1 | 7692 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 VF |
7693 | |
7694 | if (adj) { | |
790510d9 | 7695 | adj->ref_nr += 1; |
67b62f98 DA |
7696 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
7697 | dev->name, adj_dev->name, adj->ref_nr); | |
7698 | ||
5d261913 VF |
7699 | return 0; |
7700 | } | |
7701 | ||
7702 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); | |
7703 | if (!adj) | |
7704 | return -ENOMEM; | |
7705 | ||
7706 | adj->dev = adj_dev; | |
7707 | adj->master = master; | |
790510d9 | 7708 | adj->ref_nr = 1; |
402dae96 | 7709 | adj->private = private; |
32b6d34f | 7710 | adj->ignore = false; |
d62607c3 | 7711 | netdev_hold(adj_dev, &adj->dev_tracker, GFP_KERNEL); |
2f268f12 | 7712 | |
67b62f98 DA |
7713 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
7714 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); | |
5d261913 | 7715 | |
7ce64c79 | 7716 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
3ee32707 | 7717 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
5831d66e VF |
7718 | if (ret) |
7719 | goto free_adj; | |
7720 | } | |
7721 | ||
7863c054 | 7722 | /* Ensure that master link is always the first item in list. */ |
842d67a7 VF |
7723 | if (master) { |
7724 | ret = sysfs_create_link(&(dev->dev.kobj), | |
7725 | &(adj_dev->dev.kobj), "master"); | |
7726 | if (ret) | |
5831d66e | 7727 | goto remove_symlinks; |
842d67a7 | 7728 | |
7863c054 | 7729 | list_add_rcu(&adj->list, dev_list); |
842d67a7 | 7730 | } else { |
7863c054 | 7731 | list_add_tail_rcu(&adj->list, dev_list); |
842d67a7 | 7732 | } |
5d261913 VF |
7733 | |
7734 | return 0; | |
842d67a7 | 7735 | |
5831d66e | 7736 | remove_symlinks: |
7ce64c79 | 7737 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7738 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
842d67a7 | 7739 | free_adj: |
d62607c3 | 7740 | netdev_put(adj_dev, &adj->dev_tracker); |
842d67a7 VF |
7741 | kfree(adj); |
7742 | ||
7743 | return ret; | |
5d261913 VF |
7744 | } |
7745 | ||
1d143d9f | 7746 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
7747 | struct net_device *adj_dev, | |
93409033 | 7748 | u16 ref_nr, |
1d143d9f | 7749 | struct list_head *dev_list) |
5d261913 VF |
7750 | { |
7751 | struct netdev_adjacent *adj; | |
7752 | ||
67b62f98 DA |
7753 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
7754 | dev->name, adj_dev->name, ref_nr); | |
7755 | ||
6ea29da1 | 7756 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 | 7757 | |
2f268f12 | 7758 | if (!adj) { |
67b62f98 | 7759 | pr_err("Adjacency does not exist for device %s from %s\n", |
2f268f12 | 7760 | dev->name, adj_dev->name); |
67b62f98 DA |
7761 | WARN_ON(1); |
7762 | return; | |
2f268f12 | 7763 | } |
5d261913 | 7764 | |
93409033 | 7765 | if (adj->ref_nr > ref_nr) { |
67b62f98 DA |
7766 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
7767 | dev->name, adj_dev->name, ref_nr, | |
7768 | adj->ref_nr - ref_nr); | |
93409033 | 7769 | adj->ref_nr -= ref_nr; |
5d261913 VF |
7770 | return; |
7771 | } | |
7772 | ||
842d67a7 VF |
7773 | if (adj->master) |
7774 | sysfs_remove_link(&(dev->dev.kobj), "master"); | |
7775 | ||
7ce64c79 | 7776 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7777 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
5831d66e | 7778 | |
5d261913 | 7779 | list_del_rcu(&adj->list); |
67b62f98 | 7780 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
2f268f12 | 7781 | adj_dev->name, dev->name, adj_dev->name); |
d62607c3 | 7782 | netdev_put(adj_dev, &adj->dev_tracker); |
5d261913 VF |
7783 | kfree_rcu(adj, rcu); |
7784 | } | |
7785 | ||
1d143d9f | 7786 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
7787 | struct net_device *upper_dev, | |
7788 | struct list_head *up_list, | |
7789 | struct list_head *down_list, | |
7790 | void *private, bool master) | |
5d261913 VF |
7791 | { |
7792 | int ret; | |
7793 | ||
790510d9 | 7794 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
93409033 | 7795 | private, master); |
5d261913 VF |
7796 | if (ret) |
7797 | return ret; | |
7798 | ||
790510d9 | 7799 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
93409033 | 7800 | private, false); |
5d261913 | 7801 | if (ret) { |
790510d9 | 7802 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
5d261913 VF |
7803 | return ret; |
7804 | } | |
7805 | ||
7806 | return 0; | |
7807 | } | |
7808 | ||
1d143d9f | 7809 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
7810 | struct net_device *upper_dev, | |
93409033 | 7811 | u16 ref_nr, |
1d143d9f | 7812 | struct list_head *up_list, |
7813 | struct list_head *down_list) | |
5d261913 | 7814 | { |
93409033 AC |
7815 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
7816 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); | |
5d261913 VF |
7817 | } |
7818 | ||
1d143d9f | 7819 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
7820 | struct net_device *upper_dev, | |
7821 | void *private, bool master) | |
2f268f12 | 7822 | { |
f1170fd4 DA |
7823 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
7824 | &dev->adj_list.upper, | |
7825 | &upper_dev->adj_list.lower, | |
7826 | private, master); | |
5d261913 VF |
7827 | } |
7828 | ||
1d143d9f | 7829 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
7830 | struct net_device *upper_dev) | |
2f268f12 | 7831 | { |
93409033 | 7832 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
2f268f12 VF |
7833 | &dev->adj_list.upper, |
7834 | &upper_dev->adj_list.lower); | |
7835 | } | |
5d261913 | 7836 | |
9ff162a8 | 7837 | static int __netdev_upper_dev_link(struct net_device *dev, |
402dae96 | 7838 | struct net_device *upper_dev, bool master, |
42ab19ee | 7839 | void *upper_priv, void *upper_info, |
1fc70edb | 7840 | struct netdev_nested_priv *priv, |
42ab19ee | 7841 | struct netlink_ext_ack *extack) |
9ff162a8 | 7842 | { |
51d0c047 DA |
7843 | struct netdev_notifier_changeupper_info changeupper_info = { |
7844 | .info = { | |
7845 | .dev = dev, | |
42ab19ee | 7846 | .extack = extack, |
51d0c047 DA |
7847 | }, |
7848 | .upper_dev = upper_dev, | |
7849 | .master = master, | |
7850 | .linking = true, | |
7851 | .upper_info = upper_info, | |
7852 | }; | |
50d629e7 | 7853 | struct net_device *master_dev; |
5d261913 | 7854 | int ret = 0; |
9ff162a8 JP |
7855 | |
7856 | ASSERT_RTNL(); | |
7857 | ||
7858 | if (dev == upper_dev) | |
7859 | return -EBUSY; | |
7860 | ||
7861 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
32b6d34f | 7862 | if (__netdev_has_upper_dev(upper_dev, dev)) |
9ff162a8 JP |
7863 | return -EBUSY; |
7864 | ||
5343da4c TY |
7865 | if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) |
7866 | return -EMLINK; | |
7867 | ||
50d629e7 | 7868 | if (!master) { |
32b6d34f | 7869 | if (__netdev_has_upper_dev(dev, upper_dev)) |
50d629e7 MM |
7870 | return -EEXIST; |
7871 | } else { | |
32b6d34f | 7872 | master_dev = __netdev_master_upper_dev_get(dev); |
50d629e7 MM |
7873 | if (master_dev) |
7874 | return master_dev == upper_dev ? -EEXIST : -EBUSY; | |
7875 | } | |
9ff162a8 | 7876 | |
51d0c047 | 7877 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7878 | &changeupper_info.info); |
7879 | ret = notifier_to_errno(ret); | |
7880 | if (ret) | |
7881 | return ret; | |
7882 | ||
6dffb044 | 7883 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
402dae96 | 7884 | master); |
5d261913 VF |
7885 | if (ret) |
7886 | return ret; | |
9ff162a8 | 7887 | |
51d0c047 | 7888 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
b03804e7 IS |
7889 | &changeupper_info.info); |
7890 | ret = notifier_to_errno(ret); | |
7891 | if (ret) | |
f1170fd4 | 7892 | goto rollback; |
b03804e7 | 7893 | |
5343da4c | 7894 | __netdev_update_upper_level(dev, NULL); |
32b6d34f | 7895 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c | 7896 | |
1fc70edb | 7897 | __netdev_update_lower_level(upper_dev, priv); |
32b6d34f | 7898 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
1fc70edb | 7899 | priv); |
5343da4c | 7900 | |
9ff162a8 | 7901 | return 0; |
5d261913 | 7902 | |
f1170fd4 | 7903 | rollback: |
2f268f12 | 7904 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 VF |
7905 | |
7906 | return ret; | |
9ff162a8 JP |
7907 | } |
7908 | ||
7909 | /** | |
7910 | * netdev_upper_dev_link - Add a link to the upper device | |
7911 | * @dev: device | |
7912 | * @upper_dev: new upper device | |
7a006d59 | 7913 | * @extack: netlink extended ack |
9ff162a8 JP |
7914 | * |
7915 | * Adds a link to device which is upper to this one. The caller must hold | |
7916 | * the RTNL lock. On a failure a negative errno code is returned. | |
7917 | * On success the reference counts are adjusted and the function | |
7918 | * returns zero. | |
7919 | */ | |
7920 | int netdev_upper_dev_link(struct net_device *dev, | |
42ab19ee DA |
7921 | struct net_device *upper_dev, |
7922 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7923 | { |
1fc70edb TY |
7924 | struct netdev_nested_priv priv = { |
7925 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7926 | .data = NULL, | |
7927 | }; | |
7928 | ||
42ab19ee | 7929 | return __netdev_upper_dev_link(dev, upper_dev, false, |
1fc70edb | 7930 | NULL, NULL, &priv, extack); |
9ff162a8 JP |
7931 | } |
7932 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
7933 | ||
7934 | /** | |
7935 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
7936 | * @dev: device | |
7937 | * @upper_dev: new upper device | |
6dffb044 | 7938 | * @upper_priv: upper device private |
29bf24af | 7939 | * @upper_info: upper info to be passed down via notifier |
7a006d59 | 7940 | * @extack: netlink extended ack |
9ff162a8 JP |
7941 | * |
7942 | * Adds a link to device which is upper to this one. In this case, only | |
7943 | * one master upper device can be linked, although other non-master devices | |
7944 | * might be linked as well. The caller must hold the RTNL lock. | |
7945 | * On a failure a negative errno code is returned. On success the reference | |
7946 | * counts are adjusted and the function returns zero. | |
7947 | */ | |
7948 | int netdev_master_upper_dev_link(struct net_device *dev, | |
6dffb044 | 7949 | struct net_device *upper_dev, |
42ab19ee DA |
7950 | void *upper_priv, void *upper_info, |
7951 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7952 | { |
1fc70edb TY |
7953 | struct netdev_nested_priv priv = { |
7954 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7955 | .data = NULL, | |
7956 | }; | |
7957 | ||
29bf24af | 7958 | return __netdev_upper_dev_link(dev, upper_dev, true, |
1fc70edb | 7959 | upper_priv, upper_info, &priv, extack); |
9ff162a8 JP |
7960 | } |
7961 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
7962 | ||
fe8300fd | 7963 | static void __netdev_upper_dev_unlink(struct net_device *dev, |
1fc70edb TY |
7964 | struct net_device *upper_dev, |
7965 | struct netdev_nested_priv *priv) | |
9ff162a8 | 7966 | { |
51d0c047 DA |
7967 | struct netdev_notifier_changeupper_info changeupper_info = { |
7968 | .info = { | |
7969 | .dev = dev, | |
7970 | }, | |
7971 | .upper_dev = upper_dev, | |
7972 | .linking = false, | |
7973 | }; | |
f4563a75 | 7974 | |
9ff162a8 JP |
7975 | ASSERT_RTNL(); |
7976 | ||
0e4ead9d | 7977 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
0e4ead9d | 7978 | |
51d0c047 | 7979 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7980 | &changeupper_info.info); |
7981 | ||
2f268f12 | 7982 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 | 7983 | |
51d0c047 | 7984 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
0e4ead9d | 7985 | &changeupper_info.info); |
5343da4c TY |
7986 | |
7987 | __netdev_update_upper_level(dev, NULL); | |
32b6d34f | 7988 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c | 7989 | |
1fc70edb | 7990 | __netdev_update_lower_level(upper_dev, priv); |
32b6d34f | 7991 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
1fc70edb | 7992 | priv); |
9ff162a8 | 7993 | } |
fe8300fd TY |
7994 | |
7995 | /** | |
7996 | * netdev_upper_dev_unlink - Removes a link to upper device | |
7997 | * @dev: device | |
7998 | * @upper_dev: new upper device | |
7999 | * | |
8000 | * Removes a link to device which is upper to this one. The caller must hold | |
8001 | * the RTNL lock. | |
8002 | */ | |
8003 | void netdev_upper_dev_unlink(struct net_device *dev, | |
8004 | struct net_device *upper_dev) | |
8005 | { | |
1fc70edb TY |
8006 | struct netdev_nested_priv priv = { |
8007 | .flags = NESTED_SYNC_TODO, | |
8008 | .data = NULL, | |
8009 | }; | |
8010 | ||
8011 | __netdev_upper_dev_unlink(dev, upper_dev, &priv); | |
9ff162a8 JP |
8012 | } |
8013 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
8014 | ||
32b6d34f TY |
8015 | static void __netdev_adjacent_dev_set(struct net_device *upper_dev, |
8016 | struct net_device *lower_dev, | |
8017 | bool val) | |
8018 | { | |
8019 | struct netdev_adjacent *adj; | |
8020 | ||
8021 | adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); | |
8022 | if (adj) | |
8023 | adj->ignore = val; | |
8024 | ||
8025 | adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); | |
8026 | if (adj) | |
8027 | adj->ignore = val; | |
8028 | } | |
8029 | ||
8030 | static void netdev_adjacent_dev_disable(struct net_device *upper_dev, | |
8031 | struct net_device *lower_dev) | |
8032 | { | |
8033 | __netdev_adjacent_dev_set(upper_dev, lower_dev, true); | |
8034 | } | |
8035 | ||
8036 | static void netdev_adjacent_dev_enable(struct net_device *upper_dev, | |
8037 | struct net_device *lower_dev) | |
8038 | { | |
8039 | __netdev_adjacent_dev_set(upper_dev, lower_dev, false); | |
8040 | } | |
8041 | ||
8042 | int netdev_adjacent_change_prepare(struct net_device *old_dev, | |
8043 | struct net_device *new_dev, | |
8044 | struct net_device *dev, | |
8045 | struct netlink_ext_ack *extack) | |
8046 | { | |
1fc70edb TY |
8047 | struct netdev_nested_priv priv = { |
8048 | .flags = 0, | |
8049 | .data = NULL, | |
8050 | }; | |
32b6d34f TY |
8051 | int err; |
8052 | ||
8053 | if (!new_dev) | |
8054 | return 0; | |
8055 | ||
8056 | if (old_dev && new_dev != old_dev) | |
8057 | netdev_adjacent_dev_disable(dev, old_dev); | |
1fc70edb TY |
8058 | err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv, |
8059 | extack); | |
32b6d34f TY |
8060 | if (err) { |
8061 | if (old_dev && new_dev != old_dev) | |
8062 | netdev_adjacent_dev_enable(dev, old_dev); | |
8063 | return err; | |
8064 | } | |
8065 | ||
8066 | return 0; | |
8067 | } | |
8068 | EXPORT_SYMBOL(netdev_adjacent_change_prepare); | |
8069 | ||
8070 | void netdev_adjacent_change_commit(struct net_device *old_dev, | |
8071 | struct net_device *new_dev, | |
8072 | struct net_device *dev) | |
8073 | { | |
1fc70edb TY |
8074 | struct netdev_nested_priv priv = { |
8075 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
8076 | .data = NULL, | |
8077 | }; | |
8078 | ||
32b6d34f TY |
8079 | if (!new_dev || !old_dev) |
8080 | return; | |
8081 | ||
8082 | if (new_dev == old_dev) | |
8083 | return; | |
8084 | ||
8085 | netdev_adjacent_dev_enable(dev, old_dev); | |
1fc70edb | 8086 | __netdev_upper_dev_unlink(old_dev, dev, &priv); |
32b6d34f TY |
8087 | } |
8088 | EXPORT_SYMBOL(netdev_adjacent_change_commit); | |
8089 | ||
8090 | void netdev_adjacent_change_abort(struct net_device *old_dev, | |
8091 | struct net_device *new_dev, | |
8092 | struct net_device *dev) | |
8093 | { | |
1fc70edb TY |
8094 | struct netdev_nested_priv priv = { |
8095 | .flags = 0, | |
8096 | .data = NULL, | |
8097 | }; | |
8098 | ||
32b6d34f TY |
8099 | if (!new_dev) |
8100 | return; | |
8101 | ||
8102 | if (old_dev && new_dev != old_dev) | |
8103 | netdev_adjacent_dev_enable(dev, old_dev); | |
8104 | ||
1fc70edb | 8105 | __netdev_upper_dev_unlink(new_dev, dev, &priv); |
32b6d34f TY |
8106 | } |
8107 | EXPORT_SYMBOL(netdev_adjacent_change_abort); | |
8108 | ||
61bd3857 MS |
8109 | /** |
8110 | * netdev_bonding_info_change - Dispatch event about slave change | |
8111 | * @dev: device | |
4a26e453 | 8112 | * @bonding_info: info to dispatch |
61bd3857 MS |
8113 | * |
8114 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. | |
8115 | * The caller must hold the RTNL lock. | |
8116 | */ | |
8117 | void netdev_bonding_info_change(struct net_device *dev, | |
8118 | struct netdev_bonding_info *bonding_info) | |
8119 | { | |
51d0c047 DA |
8120 | struct netdev_notifier_bonding_info info = { |
8121 | .info.dev = dev, | |
8122 | }; | |
61bd3857 MS |
8123 | |
8124 | memcpy(&info.bonding_info, bonding_info, | |
8125 | sizeof(struct netdev_bonding_info)); | |
51d0c047 | 8126 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
61bd3857 MS |
8127 | &info.info); |
8128 | } | |
8129 | EXPORT_SYMBOL(netdev_bonding_info_change); | |
8130 | ||
9309f97a PM |
8131 | static int netdev_offload_xstats_enable_l3(struct net_device *dev, |
8132 | struct netlink_ext_ack *extack) | |
8133 | { | |
8134 | struct netdev_notifier_offload_xstats_info info = { | |
8135 | .info.dev = dev, | |
8136 | .info.extack = extack, | |
8137 | .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3, | |
8138 | }; | |
8139 | int err; | |
8140 | int rc; | |
8141 | ||
8142 | dev->offload_xstats_l3 = kzalloc(sizeof(*dev->offload_xstats_l3), | |
8143 | GFP_KERNEL); | |
8144 | if (!dev->offload_xstats_l3) | |
8145 | return -ENOMEM; | |
8146 | ||
8147 | rc = call_netdevice_notifiers_info_robust(NETDEV_OFFLOAD_XSTATS_ENABLE, | |
8148 | NETDEV_OFFLOAD_XSTATS_DISABLE, | |
8149 | &info.info); | |
8150 | err = notifier_to_errno(rc); | |
8151 | if (err) | |
8152 | goto free_stats; | |
8153 | ||
8154 | return 0; | |
8155 | ||
8156 | free_stats: | |
8157 | kfree(dev->offload_xstats_l3); | |
8158 | dev->offload_xstats_l3 = NULL; | |
8159 | return err; | |
8160 | } | |
8161 | ||
8162 | int netdev_offload_xstats_enable(struct net_device *dev, | |
8163 | enum netdev_offload_xstats_type type, | |
8164 | struct netlink_ext_ack *extack) | |
8165 | { | |
8166 | ASSERT_RTNL(); | |
8167 | ||
8168 | if (netdev_offload_xstats_enabled(dev, type)) | |
8169 | return -EALREADY; | |
8170 | ||
8171 | switch (type) { | |
8172 | case NETDEV_OFFLOAD_XSTATS_TYPE_L3: | |
8173 | return netdev_offload_xstats_enable_l3(dev, extack); | |
8174 | } | |
8175 | ||
8176 | WARN_ON(1); | |
8177 | return -EINVAL; | |
8178 | } | |
8179 | EXPORT_SYMBOL(netdev_offload_xstats_enable); | |
8180 | ||
8181 | static void netdev_offload_xstats_disable_l3(struct net_device *dev) | |
8182 | { | |
8183 | struct netdev_notifier_offload_xstats_info info = { | |
8184 | .info.dev = dev, | |
8185 | .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3, | |
8186 | }; | |
8187 | ||
8188 | call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_DISABLE, | |
8189 | &info.info); | |
8190 | kfree(dev->offload_xstats_l3); | |
8191 | dev->offload_xstats_l3 = NULL; | |
8192 | } | |
8193 | ||
8194 | int netdev_offload_xstats_disable(struct net_device *dev, | |
8195 | enum netdev_offload_xstats_type type) | |
8196 | { | |
8197 | ASSERT_RTNL(); | |
8198 | ||
8199 | if (!netdev_offload_xstats_enabled(dev, type)) | |
8200 | return -EALREADY; | |
8201 | ||
8202 | switch (type) { | |
8203 | case NETDEV_OFFLOAD_XSTATS_TYPE_L3: | |
8204 | netdev_offload_xstats_disable_l3(dev); | |
8205 | return 0; | |
8206 | } | |
8207 | ||
8208 | WARN_ON(1); | |
8209 | return -EINVAL; | |
8210 | } | |
8211 | EXPORT_SYMBOL(netdev_offload_xstats_disable); | |
8212 | ||
8213 | static void netdev_offload_xstats_disable_all(struct net_device *dev) | |
8214 | { | |
8215 | netdev_offload_xstats_disable(dev, NETDEV_OFFLOAD_XSTATS_TYPE_L3); | |
8216 | } | |
8217 | ||
8218 | static struct rtnl_hw_stats64 * | |
8219 | netdev_offload_xstats_get_ptr(const struct net_device *dev, | |
8220 | enum netdev_offload_xstats_type type) | |
8221 | { | |
8222 | switch (type) { | |
8223 | case NETDEV_OFFLOAD_XSTATS_TYPE_L3: | |
8224 | return dev->offload_xstats_l3; | |
8225 | } | |
8226 | ||
8227 | WARN_ON(1); | |
8228 | return NULL; | |
8229 | } | |
8230 | ||
8231 | bool netdev_offload_xstats_enabled(const struct net_device *dev, | |
8232 | enum netdev_offload_xstats_type type) | |
8233 | { | |
8234 | ASSERT_RTNL(); | |
8235 | ||
8236 | return netdev_offload_xstats_get_ptr(dev, type); | |
8237 | } | |
8238 | EXPORT_SYMBOL(netdev_offload_xstats_enabled); | |
8239 | ||
8240 | struct netdev_notifier_offload_xstats_ru { | |
8241 | bool used; | |
8242 | }; | |
8243 | ||
8244 | struct netdev_notifier_offload_xstats_rd { | |
8245 | struct rtnl_hw_stats64 stats; | |
8246 | bool used; | |
8247 | }; | |
8248 | ||
8249 | static void netdev_hw_stats64_add(struct rtnl_hw_stats64 *dest, | |
8250 | const struct rtnl_hw_stats64 *src) | |
8251 | { | |
8252 | dest->rx_packets += src->rx_packets; | |
8253 | dest->tx_packets += src->tx_packets; | |
8254 | dest->rx_bytes += src->rx_bytes; | |
8255 | dest->tx_bytes += src->tx_bytes; | |
8256 | dest->rx_errors += src->rx_errors; | |
8257 | dest->tx_errors += src->tx_errors; | |
8258 | dest->rx_dropped += src->rx_dropped; | |
8259 | dest->tx_dropped += src->tx_dropped; | |
8260 | dest->multicast += src->multicast; | |
8261 | } | |
8262 | ||
8263 | static int netdev_offload_xstats_get_used(struct net_device *dev, | |
8264 | enum netdev_offload_xstats_type type, | |
8265 | bool *p_used, | |
8266 | struct netlink_ext_ack *extack) | |
8267 | { | |
8268 | struct netdev_notifier_offload_xstats_ru report_used = {}; | |
8269 | struct netdev_notifier_offload_xstats_info info = { | |
8270 | .info.dev = dev, | |
8271 | .info.extack = extack, | |
8272 | .type = type, | |
8273 | .report_used = &report_used, | |
8274 | }; | |
8275 | int rc; | |
8276 | ||
8277 | WARN_ON(!netdev_offload_xstats_enabled(dev, type)); | |
8278 | rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_USED, | |
8279 | &info.info); | |
8280 | *p_used = report_used.used; | |
8281 | return notifier_to_errno(rc); | |
8282 | } | |
8283 | ||
8284 | static int netdev_offload_xstats_get_stats(struct net_device *dev, | |
8285 | enum netdev_offload_xstats_type type, | |
8286 | struct rtnl_hw_stats64 *p_stats, | |
8287 | bool *p_used, | |
8288 | struct netlink_ext_ack *extack) | |
8289 | { | |
8290 | struct netdev_notifier_offload_xstats_rd report_delta = {}; | |
8291 | struct netdev_notifier_offload_xstats_info info = { | |
8292 | .info.dev = dev, | |
8293 | .info.extack = extack, | |
8294 | .type = type, | |
8295 | .report_delta = &report_delta, | |
8296 | }; | |
8297 | struct rtnl_hw_stats64 *stats; | |
8298 | int rc; | |
8299 | ||
8300 | stats = netdev_offload_xstats_get_ptr(dev, type); | |
8301 | if (WARN_ON(!stats)) | |
8302 | return -EINVAL; | |
8303 | ||
8304 | rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_DELTA, | |
8305 | &info.info); | |
8306 | ||
8307 | /* Cache whatever we got, even if there was an error, otherwise the | |
8308 | * successful stats retrievals would get lost. | |
8309 | */ | |
8310 | netdev_hw_stats64_add(stats, &report_delta.stats); | |
8311 | ||
8312 | if (p_stats) | |
8313 | *p_stats = *stats; | |
8314 | *p_used = report_delta.used; | |
8315 | ||
8316 | return notifier_to_errno(rc); | |
8317 | } | |
8318 | ||
8319 | int netdev_offload_xstats_get(struct net_device *dev, | |
8320 | enum netdev_offload_xstats_type type, | |
8321 | struct rtnl_hw_stats64 *p_stats, bool *p_used, | |
8322 | struct netlink_ext_ack *extack) | |
8323 | { | |
8324 | ASSERT_RTNL(); | |
8325 | ||
8326 | if (p_stats) | |
8327 | return netdev_offload_xstats_get_stats(dev, type, p_stats, | |
8328 | p_used, extack); | |
8329 | else | |
8330 | return netdev_offload_xstats_get_used(dev, type, p_used, | |
8331 | extack); | |
8332 | } | |
8333 | EXPORT_SYMBOL(netdev_offload_xstats_get); | |
8334 | ||
8335 | void | |
8336 | netdev_offload_xstats_report_delta(struct netdev_notifier_offload_xstats_rd *report_delta, | |
8337 | const struct rtnl_hw_stats64 *stats) | |
8338 | { | |
8339 | report_delta->used = true; | |
8340 | netdev_hw_stats64_add(&report_delta->stats, stats); | |
8341 | } | |
8342 | EXPORT_SYMBOL(netdev_offload_xstats_report_delta); | |
8343 | ||
8344 | void | |
8345 | netdev_offload_xstats_report_used(struct netdev_notifier_offload_xstats_ru *report_used) | |
8346 | { | |
8347 | report_used->used = true; | |
8348 | } | |
8349 | EXPORT_SYMBOL(netdev_offload_xstats_report_used); | |
8350 | ||
8351 | void netdev_offload_xstats_push_delta(struct net_device *dev, | |
8352 | enum netdev_offload_xstats_type type, | |
8353 | const struct rtnl_hw_stats64 *p_stats) | |
8354 | { | |
8355 | struct rtnl_hw_stats64 *stats; | |
8356 | ||
8357 | ASSERT_RTNL(); | |
8358 | ||
8359 | stats = netdev_offload_xstats_get_ptr(dev, type); | |
8360 | if (WARN_ON(!stats)) | |
8361 | return; | |
8362 | ||
8363 | netdev_hw_stats64_add(stats, p_stats); | |
8364 | } | |
8365 | EXPORT_SYMBOL(netdev_offload_xstats_push_delta); | |
8366 | ||
cff9f12b MG |
8367 | /** |
8368 | * netdev_get_xmit_slave - Get the xmit slave of master device | |
8842500d | 8369 | * @dev: device |
cff9f12b MG |
8370 | * @skb: The packet |
8371 | * @all_slaves: assume all the slaves are active | |
8372 | * | |
8373 | * The reference counters are not incremented so the caller must be | |
8374 | * careful with locks. The caller must hold RCU lock. | |
8375 | * %NULL is returned if no slave is found. | |
8376 | */ | |
8377 | ||
8378 | struct net_device *netdev_get_xmit_slave(struct net_device *dev, | |
8379 | struct sk_buff *skb, | |
8380 | bool all_slaves) | |
8381 | { | |
8382 | const struct net_device_ops *ops = dev->netdev_ops; | |
8383 | ||
8384 | if (!ops->ndo_get_xmit_slave) | |
8385 | return NULL; | |
8386 | return ops->ndo_get_xmit_slave(dev, skb, all_slaves); | |
8387 | } | |
8388 | EXPORT_SYMBOL(netdev_get_xmit_slave); | |
8389 | ||
719a402c TT |
8390 | static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev, |
8391 | struct sock *sk) | |
8392 | { | |
8393 | const struct net_device_ops *ops = dev->netdev_ops; | |
8394 | ||
8395 | if (!ops->ndo_sk_get_lower_dev) | |
8396 | return NULL; | |
8397 | return ops->ndo_sk_get_lower_dev(dev, sk); | |
8398 | } | |
8399 | ||
8400 | /** | |
8401 | * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket | |
8402 | * @dev: device | |
8403 | * @sk: the socket | |
8404 | * | |
8405 | * %NULL is returned if no lower device is found. | |
8406 | */ | |
8407 | ||
8408 | struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev, | |
8409 | struct sock *sk) | |
8410 | { | |
8411 | struct net_device *lower; | |
8412 | ||
8413 | lower = netdev_sk_get_lower_dev(dev, sk); | |
8414 | while (lower) { | |
8415 | dev = lower; | |
8416 | lower = netdev_sk_get_lower_dev(dev, sk); | |
8417 | } | |
8418 | ||
8419 | return dev; | |
8420 | } | |
8421 | EXPORT_SYMBOL(netdev_sk_get_lowest_dev); | |
8422 | ||
2ce1ee17 | 8423 | static void netdev_adjacent_add_links(struct net_device *dev) |
4c75431a AF |
8424 | { |
8425 | struct netdev_adjacent *iter; | |
8426 | ||
8427 | struct net *net = dev_net(dev); | |
8428 | ||
8429 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8430 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8431 | continue; |
8432 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8433 | &iter->dev->adj_list.lower); | |
8434 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
8435 | &dev->adj_list.upper); | |
8436 | } | |
8437 | ||
8438 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8439 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8440 | continue; |
8441 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8442 | &iter->dev->adj_list.upper); | |
8443 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
8444 | &dev->adj_list.lower); | |
8445 | } | |
8446 | } | |
8447 | ||
2ce1ee17 | 8448 | static void netdev_adjacent_del_links(struct net_device *dev) |
4c75431a AF |
8449 | { |
8450 | struct netdev_adjacent *iter; | |
8451 | ||
8452 | struct net *net = dev_net(dev); | |
8453 | ||
8454 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8455 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8456 | continue; |
8457 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8458 | &iter->dev->adj_list.lower); | |
8459 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8460 | &dev->adj_list.upper); | |
8461 | } | |
8462 | ||
8463 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8464 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8465 | continue; |
8466 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8467 | &iter->dev->adj_list.upper); | |
8468 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8469 | &dev->adj_list.lower); | |
8470 | } | |
8471 | } | |
8472 | ||
5bb025fa | 8473 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
402dae96 | 8474 | { |
5bb025fa | 8475 | struct netdev_adjacent *iter; |
402dae96 | 8476 | |
4c75431a AF |
8477 | struct net *net = dev_net(dev); |
8478 | ||
5bb025fa | 8479 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
be4da0e3 | 8480 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8481 | continue; |
5bb025fa VF |
8482 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8483 | &iter->dev->adj_list.lower); | |
8484 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8485 | &iter->dev->adj_list.lower); | |
8486 | } | |
402dae96 | 8487 | |
5bb025fa | 8488 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
be4da0e3 | 8489 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8490 | continue; |
5bb025fa VF |
8491 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8492 | &iter->dev->adj_list.upper); | |
8493 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8494 | &iter->dev->adj_list.upper); | |
8495 | } | |
402dae96 | 8496 | } |
402dae96 VF |
8497 | |
8498 | void *netdev_lower_dev_get_private(struct net_device *dev, | |
8499 | struct net_device *lower_dev) | |
8500 | { | |
8501 | struct netdev_adjacent *lower; | |
8502 | ||
8503 | if (!lower_dev) | |
8504 | return NULL; | |
6ea29da1 | 8505 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
402dae96 VF |
8506 | if (!lower) |
8507 | return NULL; | |
8508 | ||
8509 | return lower->private; | |
8510 | } | |
8511 | EXPORT_SYMBOL(netdev_lower_dev_get_private); | |
8512 | ||
4085ebe8 | 8513 | |
04d48266 | 8514 | /** |
c1639be9 | 8515 | * netdev_lower_state_changed - Dispatch event about lower device state change |
04d48266 JP |
8516 | * @lower_dev: device |
8517 | * @lower_state_info: state to dispatch | |
8518 | * | |
8519 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. | |
8520 | * The caller must hold the RTNL lock. | |
8521 | */ | |
8522 | void netdev_lower_state_changed(struct net_device *lower_dev, | |
8523 | void *lower_state_info) | |
8524 | { | |
51d0c047 DA |
8525 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
8526 | .info.dev = lower_dev, | |
8527 | }; | |
04d48266 JP |
8528 | |
8529 | ASSERT_RTNL(); | |
8530 | changelowerstate_info.lower_state_info = lower_state_info; | |
51d0c047 | 8531 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
04d48266 JP |
8532 | &changelowerstate_info.info); |
8533 | } | |
8534 | EXPORT_SYMBOL(netdev_lower_state_changed); | |
8535 | ||
b6c40d68 PM |
8536 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
8537 | { | |
d314774c SH |
8538 | const struct net_device_ops *ops = dev->netdev_ops; |
8539 | ||
d2615bf4 | 8540 | if (ops->ndo_change_rx_flags) |
d314774c | 8541 | ops->ndo_change_rx_flags(dev, flags); |
b6c40d68 PM |
8542 | } |
8543 | ||
991fb3f7 | 8544 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8545 | { |
b536db93 | 8546 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
8547 | kuid_t uid; |
8548 | kgid_t gid; | |
1da177e4 | 8549 | |
24023451 PM |
8550 | ASSERT_RTNL(); |
8551 | ||
dad9b335 WC |
8552 | dev->flags |= IFF_PROMISC; |
8553 | dev->promiscuity += inc; | |
8554 | if (dev->promiscuity == 0) { | |
8555 | /* | |
8556 | * Avoid overflow. | |
8557 | * If inc causes overflow, untouch promisc and return error. | |
8558 | */ | |
8559 | if (inc < 0) | |
8560 | dev->flags &= ~IFF_PROMISC; | |
8561 | else { | |
8562 | dev->promiscuity -= inc; | |
5b92be64 | 8563 | netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n"); |
dad9b335 WC |
8564 | return -EOVERFLOW; |
8565 | } | |
8566 | } | |
52609c0b | 8567 | if (dev->flags != old_flags) { |
3ba0bf47 JB |
8568 | netdev_info(dev, "%s promiscuous mode\n", |
8569 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
8570 | if (audit_enabled) { |
8571 | current_uid_gid(&uid, &gid); | |
cdfb6b34 RGB |
8572 | audit_log(audit_context(), GFP_ATOMIC, |
8573 | AUDIT_ANOM_PROMISCUOUS, | |
8574 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
8575 | dev->name, (dev->flags & IFF_PROMISC), | |
8576 | (old_flags & IFF_PROMISC), | |
8577 | from_kuid(&init_user_ns, audit_get_loginuid(current)), | |
8578 | from_kuid(&init_user_ns, uid), | |
8579 | from_kgid(&init_user_ns, gid), | |
8580 | audit_get_sessionid(current)); | |
8192b0c4 | 8581 | } |
24023451 | 8582 | |
b6c40d68 | 8583 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 8584 | } |
991fb3f7 | 8585 | if (notify) |
1d997f10 | 8586 | __dev_notify_flags(dev, old_flags, IFF_PROMISC, 0, NULL); |
dad9b335 | 8587 | return 0; |
1da177e4 LT |
8588 | } |
8589 | ||
4417da66 PM |
8590 | /** |
8591 | * dev_set_promiscuity - update promiscuity count on a device | |
8592 | * @dev: device | |
8593 | * @inc: modifier | |
8594 | * | |
8595 | * Add or remove promiscuity from a device. While the count in the device | |
8596 | * remains above zero the interface remains promiscuous. Once it hits zero | |
8597 | * the device reverts back to normal filtering operation. A negative inc | |
8598 | * value is used to drop promiscuity on the device. | |
dad9b335 | 8599 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 8600 | */ |
dad9b335 | 8601 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 8602 | { |
b536db93 | 8603 | unsigned int old_flags = dev->flags; |
dad9b335 | 8604 | int err; |
4417da66 | 8605 | |
991fb3f7 | 8606 | err = __dev_set_promiscuity(dev, inc, true); |
4b5a698e | 8607 | if (err < 0) |
dad9b335 | 8608 | return err; |
4417da66 PM |
8609 | if (dev->flags != old_flags) |
8610 | dev_set_rx_mode(dev); | |
dad9b335 | 8611 | return err; |
4417da66 | 8612 | } |
d1b19dff | 8613 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 8614 | |
991fb3f7 | 8615 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8616 | { |
991fb3f7 | 8617 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
1da177e4 | 8618 | |
24023451 PM |
8619 | ASSERT_RTNL(); |
8620 | ||
1da177e4 | 8621 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
8622 | dev->allmulti += inc; |
8623 | if (dev->allmulti == 0) { | |
8624 | /* | |
8625 | * Avoid overflow. | |
8626 | * If inc causes overflow, untouch allmulti and return error. | |
8627 | */ | |
8628 | if (inc < 0) | |
8629 | dev->flags &= ~IFF_ALLMULTI; | |
8630 | else { | |
8631 | dev->allmulti -= inc; | |
5b92be64 | 8632 | netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n"); |
dad9b335 WC |
8633 | return -EOVERFLOW; |
8634 | } | |
8635 | } | |
24023451 | 8636 | if (dev->flags ^ old_flags) { |
802dcbd6 JB |
8637 | netdev_info(dev, "%s allmulticast mode\n", |
8638 | dev->flags & IFF_ALLMULTI ? "entered" : "left"); | |
b6c40d68 | 8639 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 8640 | dev_set_rx_mode(dev); |
991fb3f7 ND |
8641 | if (notify) |
8642 | __dev_notify_flags(dev, old_flags, | |
1d997f10 | 8643 | dev->gflags ^ old_gflags, 0, NULL); |
24023451 | 8644 | } |
dad9b335 | 8645 | return 0; |
4417da66 | 8646 | } |
991fb3f7 ND |
8647 | |
8648 | /** | |
8649 | * dev_set_allmulti - update allmulti count on a device | |
8650 | * @dev: device | |
8651 | * @inc: modifier | |
8652 | * | |
8653 | * Add or remove reception of all multicast frames to a device. While the | |
8654 | * count in the device remains above zero the interface remains listening | |
8655 | * to all interfaces. Once it hits zero the device reverts back to normal | |
8656 | * filtering operation. A negative @inc value is used to drop the counter | |
8657 | * when releasing a resource needing all multicasts. | |
8658 | * Return 0 if successful or a negative errno code on error. | |
8659 | */ | |
8660 | ||
8661 | int dev_set_allmulti(struct net_device *dev, int inc) | |
8662 | { | |
8663 | return __dev_set_allmulti(dev, inc, true); | |
8664 | } | |
d1b19dff | 8665 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
8666 | |
8667 | /* | |
8668 | * Upload unicast and multicast address lists to device and | |
8669 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 8670 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
8671 | * are present. |
8672 | */ | |
8673 | void __dev_set_rx_mode(struct net_device *dev) | |
8674 | { | |
d314774c SH |
8675 | const struct net_device_ops *ops = dev->netdev_ops; |
8676 | ||
4417da66 PM |
8677 | /* dev_open will call this function so the list will stay sane. */ |
8678 | if (!(dev->flags&IFF_UP)) | |
8679 | return; | |
8680 | ||
8681 | if (!netif_device_present(dev)) | |
40b77c94 | 8682 | return; |
4417da66 | 8683 | |
01789349 | 8684 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
8685 | /* Unicast addresses changes may only happen under the rtnl, |
8686 | * therefore calling __dev_set_promiscuity here is safe. | |
8687 | */ | |
32e7bfc4 | 8688 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
991fb3f7 | 8689 | __dev_set_promiscuity(dev, 1, false); |
2d348d1f | 8690 | dev->uc_promisc = true; |
32e7bfc4 | 8691 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
991fb3f7 | 8692 | __dev_set_promiscuity(dev, -1, false); |
2d348d1f | 8693 | dev->uc_promisc = false; |
4417da66 | 8694 | } |
4417da66 | 8695 | } |
01789349 JP |
8696 | |
8697 | if (ops->ndo_set_rx_mode) | |
8698 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
8699 | } |
8700 | ||
8701 | void dev_set_rx_mode(struct net_device *dev) | |
8702 | { | |
b9e40857 | 8703 | netif_addr_lock_bh(dev); |
4417da66 | 8704 | __dev_set_rx_mode(dev); |
b9e40857 | 8705 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
8706 | } |
8707 | ||
f0db275a SH |
8708 | /** |
8709 | * dev_get_flags - get flags reported to userspace | |
8710 | * @dev: device | |
8711 | * | |
8712 | * Get the combination of flag bits exported through APIs to userspace. | |
8713 | */ | |
95c96174 | 8714 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 8715 | { |
95c96174 | 8716 | unsigned int flags; |
1da177e4 | 8717 | |
8afc7a78 | 8718 | flags = (READ_ONCE(dev->flags) & ~(IFF_PROMISC | |
1da177e4 | 8719 | IFF_ALLMULTI | |
b00055aa SR |
8720 | IFF_RUNNING | |
8721 | IFF_LOWER_UP | | |
8722 | IFF_DORMANT)) | | |
8afc7a78 | 8723 | (READ_ONCE(dev->gflags) & (IFF_PROMISC | |
1da177e4 LT |
8724 | IFF_ALLMULTI)); |
8725 | ||
b00055aa SR |
8726 | if (netif_running(dev)) { |
8727 | if (netif_oper_up(dev)) | |
8728 | flags |= IFF_RUNNING; | |
8729 | if (netif_carrier_ok(dev)) | |
8730 | flags |= IFF_LOWER_UP; | |
8731 | if (netif_dormant(dev)) | |
8732 | flags |= IFF_DORMANT; | |
8733 | } | |
1da177e4 LT |
8734 | |
8735 | return flags; | |
8736 | } | |
d1b19dff | 8737 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 8738 | |
6d040321 PM |
8739 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
8740 | struct netlink_ext_ack *extack) | |
1da177e4 | 8741 | { |
b536db93 | 8742 | unsigned int old_flags = dev->flags; |
bd380811 | 8743 | int ret; |
1da177e4 | 8744 | |
24023451 PM |
8745 | ASSERT_RTNL(); |
8746 | ||
1da177e4 LT |
8747 | /* |
8748 | * Set the flags on our device. | |
8749 | */ | |
8750 | ||
8751 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
8752 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
8753 | IFF_AUTOMEDIA)) | | |
8754 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
8755 | IFF_ALLMULTI)); | |
8756 | ||
8757 | /* | |
8758 | * Load in the correct multicast list now the flags have changed. | |
8759 | */ | |
8760 | ||
b6c40d68 PM |
8761 | if ((old_flags ^ flags) & IFF_MULTICAST) |
8762 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 8763 | |
4417da66 | 8764 | dev_set_rx_mode(dev); |
1da177e4 LT |
8765 | |
8766 | /* | |
8767 | * Have we downed the interface. We handle IFF_UP ourselves | |
8768 | * according to user attempts to set it, rather than blindly | |
8769 | * setting it. | |
8770 | */ | |
8771 | ||
8772 | ret = 0; | |
7051b88a | 8773 | if ((old_flags ^ flags) & IFF_UP) { |
8774 | if (old_flags & IFF_UP) | |
8775 | __dev_close(dev); | |
8776 | else | |
40c900aa | 8777 | ret = __dev_open(dev, extack); |
7051b88a | 8778 | } |
1da177e4 | 8779 | |
1da177e4 | 8780 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff | 8781 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
991fb3f7 | 8782 | unsigned int old_flags = dev->flags; |
d1b19dff | 8783 | |
1da177e4 | 8784 | dev->gflags ^= IFF_PROMISC; |
991fb3f7 ND |
8785 | |
8786 | if (__dev_set_promiscuity(dev, inc, false) >= 0) | |
8787 | if (dev->flags != old_flags) | |
8788 | dev_set_rx_mode(dev); | |
1da177e4 LT |
8789 | } |
8790 | ||
8791 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
eb13da1a | 8792 | * is important. Some (broken) drivers set IFF_PROMISC, when |
8793 | * IFF_ALLMULTI is requested not asking us and not reporting. | |
1da177e4 LT |
8794 | */ |
8795 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
8796 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
8797 | ||
1da177e4 | 8798 | dev->gflags ^= IFF_ALLMULTI; |
991fb3f7 | 8799 | __dev_set_allmulti(dev, inc, false); |
1da177e4 LT |
8800 | } |
8801 | ||
bd380811 PM |
8802 | return ret; |
8803 | } | |
8804 | ||
a528c219 | 8805 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
1d997f10 HL |
8806 | unsigned int gchanges, u32 portid, |
8807 | const struct nlmsghdr *nlh) | |
bd380811 PM |
8808 | { |
8809 | unsigned int changes = dev->flags ^ old_flags; | |
8810 | ||
a528c219 | 8811 | if (gchanges) |
1d997f10 | 8812 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC, portid, nlh); |
a528c219 | 8813 | |
bd380811 PM |
8814 | if (changes & IFF_UP) { |
8815 | if (dev->flags & IFF_UP) | |
8816 | call_netdevice_notifiers(NETDEV_UP, dev); | |
8817 | else | |
8818 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
8819 | } | |
8820 | ||
8821 | if (dev->flags & IFF_UP && | |
be9efd36 | 8822 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
51d0c047 DA |
8823 | struct netdev_notifier_change_info change_info = { |
8824 | .info = { | |
8825 | .dev = dev, | |
8826 | }, | |
8827 | .flags_changed = changes, | |
8828 | }; | |
be9efd36 | 8829 | |
51d0c047 | 8830 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
be9efd36 | 8831 | } |
bd380811 PM |
8832 | } |
8833 | ||
8834 | /** | |
8835 | * dev_change_flags - change device settings | |
8836 | * @dev: device | |
8837 | * @flags: device state flags | |
567c5e13 | 8838 | * @extack: netlink extended ack |
bd380811 PM |
8839 | * |
8840 | * Change settings on device based state flags. The flags are | |
8841 | * in the userspace exported format. | |
8842 | */ | |
567c5e13 PM |
8843 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
8844 | struct netlink_ext_ack *extack) | |
bd380811 | 8845 | { |
b536db93 | 8846 | int ret; |
991fb3f7 | 8847 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
bd380811 | 8848 | |
6d040321 | 8849 | ret = __dev_change_flags(dev, flags, extack); |
bd380811 PM |
8850 | if (ret < 0) |
8851 | return ret; | |
8852 | ||
991fb3f7 | 8853 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
1d997f10 | 8854 | __dev_notify_flags(dev, old_flags, changes, 0, NULL); |
1da177e4 LT |
8855 | return ret; |
8856 | } | |
d1b19dff | 8857 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 8858 | |
f51048c3 | 8859 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
2315dc91 VF |
8860 | { |
8861 | const struct net_device_ops *ops = dev->netdev_ops; | |
8862 | ||
8863 | if (ops->ndo_change_mtu) | |
8864 | return ops->ndo_change_mtu(dev, new_mtu); | |
8865 | ||
501a90c9 ED |
8866 | /* Pairs with all the lockless reads of dev->mtu in the stack */ |
8867 | WRITE_ONCE(dev->mtu, new_mtu); | |
2315dc91 VF |
8868 | return 0; |
8869 | } | |
f51048c3 | 8870 | EXPORT_SYMBOL(__dev_set_mtu); |
2315dc91 | 8871 | |
d836f5c6 ED |
8872 | int dev_validate_mtu(struct net_device *dev, int new_mtu, |
8873 | struct netlink_ext_ack *extack) | |
8874 | { | |
8875 | /* MTU must be positive, and in range */ | |
8876 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { | |
8877 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); | |
8878 | return -EINVAL; | |
8879 | } | |
8880 | ||
8881 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { | |
8882 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); | |
8883 | return -EINVAL; | |
8884 | } | |
8885 | return 0; | |
8886 | } | |
8887 | ||
f0db275a | 8888 | /** |
7a4c53be | 8889 | * dev_set_mtu_ext - Change maximum transfer unit |
f0db275a SH |
8890 | * @dev: device |
8891 | * @new_mtu: new transfer unit | |
7a4c53be | 8892 | * @extack: netlink extended ack |
f0db275a SH |
8893 | * |
8894 | * Change the maximum transfer size of the network device. | |
8895 | */ | |
7a4c53be SH |
8896 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
8897 | struct netlink_ext_ack *extack) | |
1da177e4 | 8898 | { |
2315dc91 | 8899 | int err, orig_mtu; |
1da177e4 LT |
8900 | |
8901 | if (new_mtu == dev->mtu) | |
8902 | return 0; | |
8903 | ||
d836f5c6 ED |
8904 | err = dev_validate_mtu(dev, new_mtu, extack); |
8905 | if (err) | |
8906 | return err; | |
1da177e4 LT |
8907 | |
8908 | if (!netif_device_present(dev)) | |
8909 | return -ENODEV; | |
8910 | ||
1d486bfb VF |
8911 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
8912 | err = notifier_to_errno(err); | |
8913 | if (err) | |
8914 | return err; | |
d314774c | 8915 | |
2315dc91 VF |
8916 | orig_mtu = dev->mtu; |
8917 | err = __dev_set_mtu(dev, new_mtu); | |
d314774c | 8918 | |
2315dc91 | 8919 | if (!err) { |
af7d6cce SD |
8920 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8921 | orig_mtu); | |
2315dc91 VF |
8922 | err = notifier_to_errno(err); |
8923 | if (err) { | |
8924 | /* setting mtu back and notifying everyone again, | |
8925 | * so that they have a chance to revert changes. | |
8926 | */ | |
8927 | __dev_set_mtu(dev, orig_mtu); | |
af7d6cce SD |
8928 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8929 | new_mtu); | |
2315dc91 VF |
8930 | } |
8931 | } | |
1da177e4 LT |
8932 | return err; |
8933 | } | |
7a4c53be SH |
8934 | |
8935 | int dev_set_mtu(struct net_device *dev, int new_mtu) | |
8936 | { | |
8937 | struct netlink_ext_ack extack; | |
8938 | int err; | |
8939 | ||
a6bcfc89 | 8940 | memset(&extack, 0, sizeof(extack)); |
7a4c53be | 8941 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
a6bcfc89 | 8942 | if (err && extack._msg) |
7a4c53be SH |
8943 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
8944 | return err; | |
8945 | } | |
d1b19dff | 8946 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 8947 | |
6a643ddb CW |
8948 | /** |
8949 | * dev_change_tx_queue_len - Change TX queue length of a netdevice | |
8950 | * @dev: device | |
8951 | * @new_len: new tx queue length | |
8952 | */ | |
8953 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) | |
8954 | { | |
8955 | unsigned int orig_len = dev->tx_queue_len; | |
8956 | int res; | |
8957 | ||
8958 | if (new_len != (unsigned int)new_len) | |
8959 | return -ERANGE; | |
8960 | ||
8961 | if (new_len != orig_len) { | |
ad13b5b0 | 8962 | WRITE_ONCE(dev->tx_queue_len, new_len); |
6a643ddb CW |
8963 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); |
8964 | res = notifier_to_errno(res); | |
7effaf06 TT |
8965 | if (res) |
8966 | goto err_rollback; | |
8967 | res = dev_qdisc_change_tx_queue_len(dev); | |
8968 | if (res) | |
8969 | goto err_rollback; | |
6a643ddb CW |
8970 | } |
8971 | ||
8972 | return 0; | |
7effaf06 TT |
8973 | |
8974 | err_rollback: | |
8975 | netdev_err(dev, "refused to change device tx_queue_len\n"); | |
ad13b5b0 | 8976 | WRITE_ONCE(dev->tx_queue_len, orig_len); |
7effaf06 | 8977 | return res; |
6a643ddb CW |
8978 | } |
8979 | ||
cbda10fa VD |
8980 | /** |
8981 | * dev_set_group - Change group this device belongs to | |
8982 | * @dev: device | |
8983 | * @new_group: group this device should belong to | |
8984 | */ | |
8985 | void dev_set_group(struct net_device *dev, int new_group) | |
8986 | { | |
8987 | dev->group = new_group; | |
8988 | } | |
cbda10fa | 8989 | |
d59cdf94 PM |
8990 | /** |
8991 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. | |
8992 | * @dev: device | |
8993 | * @addr: new address | |
8994 | * @extack: netlink extended ack | |
8995 | */ | |
8996 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, | |
8997 | struct netlink_ext_ack *extack) | |
8998 | { | |
8999 | struct netdev_notifier_pre_changeaddr_info info = { | |
9000 | .info.dev = dev, | |
9001 | .info.extack = extack, | |
9002 | .dev_addr = addr, | |
9003 | }; | |
9004 | int rc; | |
9005 | ||
9006 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); | |
9007 | return notifier_to_errno(rc); | |
9008 | } | |
9009 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); | |
9010 | ||
f0db275a SH |
9011 | /** |
9012 | * dev_set_mac_address - Change Media Access Control Address | |
9013 | * @dev: device | |
9014 | * @sa: new address | |
3a37a963 | 9015 | * @extack: netlink extended ack |
f0db275a SH |
9016 | * |
9017 | * Change the hardware (MAC) address of the device | |
9018 | */ | |
3a37a963 PM |
9019 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
9020 | struct netlink_ext_ack *extack) | |
1da177e4 | 9021 | { |
d314774c | 9022 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
9023 | int err; |
9024 | ||
d314774c | 9025 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
9026 | return -EOPNOTSUPP; |
9027 | if (sa->sa_family != dev->type) | |
9028 | return -EINVAL; | |
9029 | if (!netif_device_present(dev)) | |
9030 | return -ENODEV; | |
d59cdf94 PM |
9031 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
9032 | if (err) | |
9033 | return err; | |
0ec92a8f PG |
9034 | if (memcmp(dev->dev_addr, sa->sa_data, dev->addr_len)) { |
9035 | err = ops->ndo_set_mac_address(dev, sa); | |
9036 | if (err) | |
9037 | return err; | |
9038 | } | |
fbdeca2d | 9039 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 9040 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 9041 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 9042 | return 0; |
1da177e4 | 9043 | } |
d1b19dff | 9044 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 9045 | |
c7d52737 | 9046 | DECLARE_RWSEM(dev_addr_sem); |
3b23a32a CW |
9047 | |
9048 | int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa, | |
9049 | struct netlink_ext_ack *extack) | |
9050 | { | |
9051 | int ret; | |
9052 | ||
9053 | down_write(&dev_addr_sem); | |
9054 | ret = dev_set_mac_address(dev, sa, extack); | |
9055 | up_write(&dev_addr_sem); | |
9056 | return ret; | |
9057 | } | |
9058 | EXPORT_SYMBOL(dev_set_mac_address_user); | |
9059 | ||
9060 | int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name) | |
9061 | { | |
b5f0de6d | 9062 | size_t size = sizeof(sa->sa_data_min); |
3b23a32a CW |
9063 | struct net_device *dev; |
9064 | int ret = 0; | |
9065 | ||
9066 | down_read(&dev_addr_sem); | |
9067 | rcu_read_lock(); | |
9068 | ||
9069 | dev = dev_get_by_name_rcu(net, dev_name); | |
9070 | if (!dev) { | |
9071 | ret = -ENODEV; | |
9072 | goto unlock; | |
9073 | } | |
9074 | if (!dev->addr_len) | |
9075 | memset(sa->sa_data, 0, size); | |
9076 | else | |
9077 | memcpy(sa->sa_data, dev->dev_addr, | |
9078 | min_t(size_t, size, dev->addr_len)); | |
9079 | sa->sa_family = dev->type; | |
9080 | ||
9081 | unlock: | |
9082 | rcu_read_unlock(); | |
9083 | up_read(&dev_addr_sem); | |
9084 | return ret; | |
9085 | } | |
9086 | EXPORT_SYMBOL(dev_get_mac_address); | |
9087 | ||
4bf84c35 JP |
9088 | /** |
9089 | * dev_change_carrier - Change device carrier | |
9090 | * @dev: device | |
691b3b7e | 9091 | * @new_carrier: new value |
4bf84c35 JP |
9092 | * |
9093 | * Change device carrier | |
9094 | */ | |
9095 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
9096 | { | |
9097 | const struct net_device_ops *ops = dev->netdev_ops; | |
9098 | ||
9099 | if (!ops->ndo_change_carrier) | |
9100 | return -EOPNOTSUPP; | |
9101 | if (!netif_device_present(dev)) | |
9102 | return -ENODEV; | |
9103 | return ops->ndo_change_carrier(dev, new_carrier); | |
9104 | } | |
4bf84c35 | 9105 | |
66b52b0d JP |
9106 | /** |
9107 | * dev_get_phys_port_id - Get device physical port ID | |
9108 | * @dev: device | |
9109 | * @ppid: port ID | |
9110 | * | |
9111 | * Get device physical port ID | |
9112 | */ | |
9113 | int dev_get_phys_port_id(struct net_device *dev, | |
02637fce | 9114 | struct netdev_phys_item_id *ppid) |
66b52b0d JP |
9115 | { |
9116 | const struct net_device_ops *ops = dev->netdev_ops; | |
9117 | ||
9118 | if (!ops->ndo_get_phys_port_id) | |
9119 | return -EOPNOTSUPP; | |
9120 | return ops->ndo_get_phys_port_id(dev, ppid); | |
9121 | } | |
66b52b0d | 9122 | |
db24a904 DA |
9123 | /** |
9124 | * dev_get_phys_port_name - Get device physical port name | |
9125 | * @dev: device | |
9126 | * @name: port name | |
ed49e650 | 9127 | * @len: limit of bytes to copy to name |
db24a904 DA |
9128 | * |
9129 | * Get device physical port name | |
9130 | */ | |
9131 | int dev_get_phys_port_name(struct net_device *dev, | |
9132 | char *name, size_t len) | |
9133 | { | |
9134 | const struct net_device_ops *ops = dev->netdev_ops; | |
af3836df | 9135 | int err; |
db24a904 | 9136 | |
af3836df JP |
9137 | if (ops->ndo_get_phys_port_name) { |
9138 | err = ops->ndo_get_phys_port_name(dev, name, len); | |
9139 | if (err != -EOPNOTSUPP) | |
9140 | return err; | |
9141 | } | |
9142 | return devlink_compat_phys_port_name_get(dev, name, len); | |
db24a904 | 9143 | } |
db24a904 | 9144 | |
d6abc596 FF |
9145 | /** |
9146 | * dev_get_port_parent_id - Get the device's port parent identifier | |
9147 | * @dev: network device | |
9148 | * @ppid: pointer to a storage for the port's parent identifier | |
9149 | * @recurse: allow/disallow recursion to lower devices | |
9150 | * | |
9151 | * Get the devices's port parent identifier | |
9152 | */ | |
9153 | int dev_get_port_parent_id(struct net_device *dev, | |
9154 | struct netdev_phys_item_id *ppid, | |
9155 | bool recurse) | |
9156 | { | |
9157 | const struct net_device_ops *ops = dev->netdev_ops; | |
9158 | struct netdev_phys_item_id first = { }; | |
9159 | struct net_device *lower_dev; | |
9160 | struct list_head *iter; | |
7e1146e8 JP |
9161 | int err; |
9162 | ||
9163 | if (ops->ndo_get_port_parent_id) { | |
9164 | err = ops->ndo_get_port_parent_id(dev, ppid); | |
9165 | if (err != -EOPNOTSUPP) | |
9166 | return err; | |
9167 | } | |
d6abc596 | 9168 | |
7e1146e8 | 9169 | err = devlink_compat_switch_id_get(dev, ppid); |
c0288ae8 | 9170 | if (!recurse || err != -EOPNOTSUPP) |
7e1146e8 | 9171 | return err; |
d6abc596 | 9172 | |
d6abc596 | 9173 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
c0288ae8 | 9174 | err = dev_get_port_parent_id(lower_dev, ppid, true); |
d6abc596 FF |
9175 | if (err) |
9176 | break; | |
9177 | if (!first.id_len) | |
9178 | first = *ppid; | |
9179 | else if (memcmp(&first, ppid, sizeof(*ppid))) | |
e1b9efe6 | 9180 | return -EOPNOTSUPP; |
d6abc596 FF |
9181 | } |
9182 | ||
9183 | return err; | |
9184 | } | |
9185 | EXPORT_SYMBOL(dev_get_port_parent_id); | |
9186 | ||
9187 | /** | |
9188 | * netdev_port_same_parent_id - Indicate if two network devices have | |
9189 | * the same port parent identifier | |
9190 | * @a: first network device | |
9191 | * @b: second network device | |
9192 | */ | |
9193 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) | |
9194 | { | |
9195 | struct netdev_phys_item_id a_id = { }; | |
9196 | struct netdev_phys_item_id b_id = { }; | |
9197 | ||
9198 | if (dev_get_port_parent_id(a, &a_id, true) || | |
9199 | dev_get_port_parent_id(b, &b_id, true)) | |
9200 | return false; | |
9201 | ||
9202 | return netdev_phys_item_id_same(&a_id, &b_id); | |
9203 | } | |
9204 | EXPORT_SYMBOL(netdev_port_same_parent_id); | |
9205 | ||
d746d707 | 9206 | /** |
2106efda JK |
9207 | * dev_change_proto_down - set carrier according to proto_down. |
9208 | * | |
d746d707 AK |
9209 | * @dev: device |
9210 | * @proto_down: new value | |
d746d707 AK |
9211 | */ |
9212 | int dev_change_proto_down(struct net_device *dev, bool proto_down) | |
9213 | { | |
2106efda | 9214 | if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) |
d746d707 AK |
9215 | return -EOPNOTSUPP; |
9216 | if (!netif_device_present(dev)) | |
9217 | return -ENODEV; | |
b5899679 AR |
9218 | if (proto_down) |
9219 | netif_carrier_off(dev); | |
9220 | else | |
9221 | netif_carrier_on(dev); | |
9222 | dev->proto_down = proto_down; | |
9223 | return 0; | |
9224 | } | |
b5899679 | 9225 | |
829eb208 RP |
9226 | /** |
9227 | * dev_change_proto_down_reason - proto down reason | |
9228 | * | |
9229 | * @dev: device | |
9230 | * @mask: proto down mask | |
9231 | * @value: proto down value | |
9232 | */ | |
9233 | void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask, | |
9234 | u32 value) | |
9235 | { | |
9236 | int b; | |
9237 | ||
9238 | if (!mask) { | |
9239 | dev->proto_down_reason = value; | |
9240 | } else { | |
9241 | for_each_set_bit(b, &mask, 32) { | |
9242 | if (value & (1 << b)) | |
9243 | dev->proto_down_reason |= BIT(b); | |
9244 | else | |
9245 | dev->proto_down_reason &= ~BIT(b); | |
9246 | } | |
9247 | } | |
9248 | } | |
829eb208 | 9249 | |
aa8d3a71 AN |
9250 | struct bpf_xdp_link { |
9251 | struct bpf_link link; | |
9252 | struct net_device *dev; /* protected by rtnl_lock, no refcnt held */ | |
9253 | int flags; | |
9254 | }; | |
9255 | ||
c8a36f19 | 9256 | static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags) |
d67b9cd2 | 9257 | { |
7f0a8382 AN |
9258 | if (flags & XDP_FLAGS_HW_MODE) |
9259 | return XDP_MODE_HW; | |
9260 | if (flags & XDP_FLAGS_DRV_MODE) | |
9261 | return XDP_MODE_DRV; | |
c8a36f19 AN |
9262 | if (flags & XDP_FLAGS_SKB_MODE) |
9263 | return XDP_MODE_SKB; | |
9264 | return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB; | |
7f0a8382 | 9265 | } |
d67b9cd2 | 9266 | |
7f0a8382 AN |
9267 | static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode) |
9268 | { | |
9269 | switch (mode) { | |
9270 | case XDP_MODE_SKB: | |
9271 | return generic_xdp_install; | |
9272 | case XDP_MODE_DRV: | |
9273 | case XDP_MODE_HW: | |
9274 | return dev->netdev_ops->ndo_bpf; | |
9275 | default: | |
9276 | return NULL; | |
5d867245 | 9277 | } |
7f0a8382 | 9278 | } |
118b4aa2 | 9279 | |
aa8d3a71 AN |
9280 | static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev, |
9281 | enum bpf_xdp_mode mode) | |
9282 | { | |
9283 | return dev->xdp_state[mode].link; | |
9284 | } | |
9285 | ||
7f0a8382 AN |
9286 | static struct bpf_prog *dev_xdp_prog(struct net_device *dev, |
9287 | enum bpf_xdp_mode mode) | |
9288 | { | |
aa8d3a71 AN |
9289 | struct bpf_xdp_link *link = dev_xdp_link(dev, mode); |
9290 | ||
9291 | if (link) | |
9292 | return link->link.prog; | |
7f0a8382 AN |
9293 | return dev->xdp_state[mode].prog; |
9294 | } | |
9295 | ||
879af96f | 9296 | u8 dev_xdp_prog_count(struct net_device *dev) |
998f1729 THJ |
9297 | { |
9298 | u8 count = 0; | |
9299 | int i; | |
9300 | ||
9301 | for (i = 0; i < __MAX_XDP_MODE; i++) | |
9302 | if (dev->xdp_state[i].prog || dev->xdp_state[i].link) | |
9303 | count++; | |
9304 | return count; | |
9305 | } | |
879af96f | 9306 | EXPORT_SYMBOL_GPL(dev_xdp_prog_count); |
998f1729 | 9307 | |
7f0a8382 AN |
9308 | u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode) |
9309 | { | |
9310 | struct bpf_prog *prog = dev_xdp_prog(dev, mode); | |
118b4aa2 | 9311 | |
7f0a8382 AN |
9312 | return prog ? prog->aux->id : 0; |
9313 | } | |
58038695 | 9314 | |
aa8d3a71 AN |
9315 | static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode, |
9316 | struct bpf_xdp_link *link) | |
9317 | { | |
9318 | dev->xdp_state[mode].link = link; | |
9319 | dev->xdp_state[mode].prog = NULL; | |
d67b9cd2 DB |
9320 | } |
9321 | ||
7f0a8382 AN |
9322 | static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode, |
9323 | struct bpf_prog *prog) | |
9324 | { | |
aa8d3a71 | 9325 | dev->xdp_state[mode].link = NULL; |
7f0a8382 | 9326 | dev->xdp_state[mode].prog = prog; |
d67b9cd2 DB |
9327 | } |
9328 | ||
7f0a8382 AN |
9329 | static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, |
9330 | bpf_op_t bpf_op, struct netlink_ext_ack *extack, | |
9331 | u32 flags, struct bpf_prog *prog) | |
d67b9cd2 | 9332 | { |
f4e63525 | 9333 | struct netdev_bpf xdp; |
7e6897f9 BT |
9334 | int err; |
9335 | ||
d67b9cd2 | 9336 | memset(&xdp, 0, sizeof(xdp)); |
7f0a8382 | 9337 | xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG; |
d67b9cd2 | 9338 | xdp.extack = extack; |
32d60277 | 9339 | xdp.flags = flags; |
d67b9cd2 DB |
9340 | xdp.prog = prog; |
9341 | ||
7f0a8382 AN |
9342 | /* Drivers assume refcnt is already incremented (i.e, prog pointer is |
9343 | * "moved" into driver), so they don't increment it on their own, but | |
9344 | * they do decrement refcnt when program is detached or replaced. | |
9345 | * Given net_device also owns link/prog, we need to bump refcnt here | |
9346 | * to prevent drivers from underflowing it. | |
9347 | */ | |
9348 | if (prog) | |
9349 | bpf_prog_inc(prog); | |
7e6897f9 | 9350 | err = bpf_op(dev, &xdp); |
7f0a8382 AN |
9351 | if (err) { |
9352 | if (prog) | |
9353 | bpf_prog_put(prog); | |
9354 | return err; | |
9355 | } | |
7e6897f9 | 9356 | |
7f0a8382 AN |
9357 | if (mode != XDP_MODE_HW) |
9358 | bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog); | |
7e6897f9 | 9359 | |
7f0a8382 | 9360 | return 0; |
d67b9cd2 DB |
9361 | } |
9362 | ||
bd0b2e7f JK |
9363 | static void dev_xdp_uninstall(struct net_device *dev) |
9364 | { | |
aa8d3a71 | 9365 | struct bpf_xdp_link *link; |
7f0a8382 AN |
9366 | struct bpf_prog *prog; |
9367 | enum bpf_xdp_mode mode; | |
9368 | bpf_op_t bpf_op; | |
bd0b2e7f | 9369 | |
7f0a8382 | 9370 | ASSERT_RTNL(); |
bd0b2e7f | 9371 | |
7f0a8382 AN |
9372 | for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) { |
9373 | prog = dev_xdp_prog(dev, mode); | |
9374 | if (!prog) | |
9375 | continue; | |
bd0b2e7f | 9376 | |
7f0a8382 AN |
9377 | bpf_op = dev_xdp_bpf_op(dev, mode); |
9378 | if (!bpf_op) | |
9379 | continue; | |
bd0b2e7f | 9380 | |
7f0a8382 AN |
9381 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); |
9382 | ||
aa8d3a71 AN |
9383 | /* auto-detach link from net device */ |
9384 | link = dev_xdp_link(dev, mode); | |
9385 | if (link) | |
9386 | link->dev = NULL; | |
9387 | else | |
9388 | bpf_prog_put(prog); | |
9389 | ||
9390 | dev_xdp_set_link(dev, mode, NULL); | |
7f0a8382 | 9391 | } |
bd0b2e7f JK |
9392 | } |
9393 | ||
d4baa936 | 9394 | static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack, |
aa8d3a71 AN |
9395 | struct bpf_xdp_link *link, struct bpf_prog *new_prog, |
9396 | struct bpf_prog *old_prog, u32 flags) | |
a7862b45 | 9397 | { |
998f1729 | 9398 | unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES); |
d4baa936 | 9399 | struct bpf_prog *cur_prog; |
879af96f JM |
9400 | struct net_device *upper; |
9401 | struct list_head *iter; | |
d4baa936 | 9402 | enum bpf_xdp_mode mode; |
7f0a8382 | 9403 | bpf_op_t bpf_op; |
a7862b45 BB |
9404 | int err; |
9405 | ||
85de8576 DB |
9406 | ASSERT_RTNL(); |
9407 | ||
aa8d3a71 AN |
9408 | /* either link or prog attachment, never both */ |
9409 | if (link && (new_prog || old_prog)) | |
9410 | return -EINVAL; | |
9411 | /* link supports only XDP mode flags */ | |
9412 | if (link && (flags & ~XDP_FLAGS_MODES)) { | |
9413 | NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment"); | |
9414 | return -EINVAL; | |
9415 | } | |
998f1729 THJ |
9416 | /* just one XDP mode bit should be set, zero defaults to drv/skb mode */ |
9417 | if (num_modes > 1) { | |
d4baa936 AN |
9418 | NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set"); |
9419 | return -EINVAL; | |
9420 | } | |
998f1729 THJ |
9421 | /* avoid ambiguity if offload + drv/skb mode progs are both loaded */ |
9422 | if (!num_modes && dev_xdp_prog_count(dev) > 1) { | |
9423 | NL_SET_ERR_MSG(extack, | |
9424 | "More than one program loaded, unset mode is ambiguous"); | |
9425 | return -EINVAL; | |
9426 | } | |
d4baa936 AN |
9427 | /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */ |
9428 | if (old_prog && !(flags & XDP_FLAGS_REPLACE)) { | |
9429 | NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified"); | |
9430 | return -EINVAL; | |
01dde20c | 9431 | } |
a25717d2 | 9432 | |
c8a36f19 | 9433 | mode = dev_xdp_mode(dev, flags); |
aa8d3a71 AN |
9434 | /* can't replace attached link */ |
9435 | if (dev_xdp_link(dev, mode)) { | |
9436 | NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link"); | |
9437 | return -EBUSY; | |
01dde20c | 9438 | } |
c14a9f63 | 9439 | |
879af96f JM |
9440 | /* don't allow if an upper device already has a program */ |
9441 | netdev_for_each_upper_dev_rcu(dev, upper, iter) { | |
9442 | if (dev_xdp_prog_count(upper) > 0) { | |
9443 | NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program"); | |
9444 | return -EEXIST; | |
9445 | } | |
9446 | } | |
9447 | ||
d4baa936 | 9448 | cur_prog = dev_xdp_prog(dev, mode); |
aa8d3a71 AN |
9449 | /* can't replace attached prog with link */ |
9450 | if (link && cur_prog) { | |
9451 | NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link"); | |
9452 | return -EBUSY; | |
9453 | } | |
d4baa936 AN |
9454 | if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) { |
9455 | NL_SET_ERR_MSG(extack, "Active program does not match expected"); | |
9456 | return -EEXIST; | |
92234c8f | 9457 | } |
c14a9f63 | 9458 | |
aa8d3a71 AN |
9459 | /* put effective new program into new_prog */ |
9460 | if (link) | |
9461 | new_prog = link->link.prog; | |
85de8576 | 9462 | |
d4baa936 AN |
9463 | if (new_prog) { |
9464 | bool offload = mode == XDP_MODE_HW; | |
7f0a8382 AN |
9465 | enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB |
9466 | ? XDP_MODE_DRV : XDP_MODE_SKB; | |
441a3303 | 9467 | |
068d9d1e AN |
9468 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) { |
9469 | NL_SET_ERR_MSG(extack, "XDP program already attached"); | |
9470 | return -EBUSY; | |
9471 | } | |
d4baa936 | 9472 | if (!offload && dev_xdp_prog(dev, other_mode)) { |
7f0a8382 | 9473 | NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time"); |
d67b9cd2 | 9474 | return -EEXIST; |
01dde20c | 9475 | } |
9d03ebc7 SF |
9476 | if (!offload && bpf_prog_is_offloaded(new_prog->aux)) { |
9477 | NL_SET_ERR_MSG(extack, "Using offloaded program without HW_MODE flag is not supported"); | |
441a3303 JK |
9478 | return -EINVAL; |
9479 | } | |
2b3486bc SF |
9480 | if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) { |
9481 | NL_SET_ERR_MSG(extack, "Program bound to different device"); | |
441a3303 JK |
9482 | return -EINVAL; |
9483 | } | |
d4baa936 | 9484 | if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) { |
fbee97fe | 9485 | NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device"); |
fbee97fe DA |
9486 | return -EINVAL; |
9487 | } | |
d4baa936 AN |
9488 | if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) { |
9489 | NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device"); | |
92164774 LB |
9490 | return -EINVAL; |
9491 | } | |
d4baa936 | 9492 | } |
92164774 | 9493 | |
d4baa936 AN |
9494 | /* don't call drivers if the effective program didn't change */ |
9495 | if (new_prog != cur_prog) { | |
9496 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9497 | if (!bpf_op) { | |
9498 | NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode"); | |
9499 | return -EOPNOTSUPP; | |
c14a9f63 | 9500 | } |
a7862b45 | 9501 | |
d4baa936 AN |
9502 | err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog); |
9503 | if (err) | |
9504 | return err; | |
7f0a8382 | 9505 | } |
d4baa936 | 9506 | |
aa8d3a71 AN |
9507 | if (link) |
9508 | dev_xdp_set_link(dev, mode, link); | |
9509 | else | |
9510 | dev_xdp_set_prog(dev, mode, new_prog); | |
d4baa936 AN |
9511 | if (cur_prog) |
9512 | bpf_prog_put(cur_prog); | |
a7862b45 | 9513 | |
7f0a8382 | 9514 | return 0; |
a7862b45 | 9515 | } |
a7862b45 | 9516 | |
aa8d3a71 AN |
9517 | static int dev_xdp_attach_link(struct net_device *dev, |
9518 | struct netlink_ext_ack *extack, | |
9519 | struct bpf_xdp_link *link) | |
9520 | { | |
9521 | return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags); | |
9522 | } | |
9523 | ||
9524 | static int dev_xdp_detach_link(struct net_device *dev, | |
9525 | struct netlink_ext_ack *extack, | |
9526 | struct bpf_xdp_link *link) | |
9527 | { | |
9528 | enum bpf_xdp_mode mode; | |
9529 | bpf_op_t bpf_op; | |
9530 | ||
9531 | ASSERT_RTNL(); | |
9532 | ||
c8a36f19 | 9533 | mode = dev_xdp_mode(dev, link->flags); |
aa8d3a71 AN |
9534 | if (dev_xdp_link(dev, mode) != link) |
9535 | return -EINVAL; | |
9536 | ||
9537 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9538 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); | |
9539 | dev_xdp_set_link(dev, mode, NULL); | |
9540 | return 0; | |
9541 | } | |
9542 | ||
9543 | static void bpf_xdp_link_release(struct bpf_link *link) | |
9544 | { | |
9545 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9546 | ||
9547 | rtnl_lock(); | |
9548 | ||
9549 | /* if racing with net_device's tear down, xdp_link->dev might be | |
9550 | * already NULL, in which case link was already auto-detached | |
9551 | */ | |
73b11c2a | 9552 | if (xdp_link->dev) { |
aa8d3a71 | 9553 | WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link)); |
73b11c2a AN |
9554 | xdp_link->dev = NULL; |
9555 | } | |
aa8d3a71 AN |
9556 | |
9557 | rtnl_unlock(); | |
9558 | } | |
9559 | ||
73b11c2a AN |
9560 | static int bpf_xdp_link_detach(struct bpf_link *link) |
9561 | { | |
9562 | bpf_xdp_link_release(link); | |
9563 | return 0; | |
9564 | } | |
9565 | ||
aa8d3a71 AN |
9566 | static void bpf_xdp_link_dealloc(struct bpf_link *link) |
9567 | { | |
9568 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9569 | ||
9570 | kfree(xdp_link); | |
9571 | } | |
9572 | ||
c1931c97 AN |
9573 | static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link, |
9574 | struct seq_file *seq) | |
9575 | { | |
9576 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9577 | u32 ifindex = 0; | |
9578 | ||
9579 | rtnl_lock(); | |
9580 | if (xdp_link->dev) | |
9581 | ifindex = xdp_link->dev->ifindex; | |
9582 | rtnl_unlock(); | |
9583 | ||
9584 | seq_printf(seq, "ifindex:\t%u\n", ifindex); | |
9585 | } | |
9586 | ||
9587 | static int bpf_xdp_link_fill_link_info(const struct bpf_link *link, | |
9588 | struct bpf_link_info *info) | |
9589 | { | |
9590 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9591 | u32 ifindex = 0; | |
9592 | ||
9593 | rtnl_lock(); | |
9594 | if (xdp_link->dev) | |
9595 | ifindex = xdp_link->dev->ifindex; | |
9596 | rtnl_unlock(); | |
9597 | ||
9598 | info->xdp.ifindex = ifindex; | |
9599 | return 0; | |
9600 | } | |
9601 | ||
026a4c28 AN |
9602 | static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, |
9603 | struct bpf_prog *old_prog) | |
9604 | { | |
9605 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9606 | enum bpf_xdp_mode mode; | |
9607 | bpf_op_t bpf_op; | |
9608 | int err = 0; | |
9609 | ||
9610 | rtnl_lock(); | |
9611 | ||
9612 | /* link might have been auto-released already, so fail */ | |
9613 | if (!xdp_link->dev) { | |
9614 | err = -ENOLINK; | |
9615 | goto out_unlock; | |
9616 | } | |
9617 | ||
9618 | if (old_prog && link->prog != old_prog) { | |
9619 | err = -EPERM; | |
9620 | goto out_unlock; | |
9621 | } | |
9622 | old_prog = link->prog; | |
382778ed THJ |
9623 | if (old_prog->type != new_prog->type || |
9624 | old_prog->expected_attach_type != new_prog->expected_attach_type) { | |
9625 | err = -EINVAL; | |
9626 | goto out_unlock; | |
9627 | } | |
9628 | ||
026a4c28 AN |
9629 | if (old_prog == new_prog) { |
9630 | /* no-op, don't disturb drivers */ | |
9631 | bpf_prog_put(new_prog); | |
9632 | goto out_unlock; | |
9633 | } | |
9634 | ||
c8a36f19 | 9635 | mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags); |
026a4c28 AN |
9636 | bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode); |
9637 | err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL, | |
9638 | xdp_link->flags, new_prog); | |
9639 | if (err) | |
9640 | goto out_unlock; | |
9641 | ||
9642 | old_prog = xchg(&link->prog, new_prog); | |
9643 | bpf_prog_put(old_prog); | |
9644 | ||
9645 | out_unlock: | |
9646 | rtnl_unlock(); | |
9647 | return err; | |
9648 | } | |
9649 | ||
aa8d3a71 AN |
9650 | static const struct bpf_link_ops bpf_xdp_link_lops = { |
9651 | .release = bpf_xdp_link_release, | |
9652 | .dealloc = bpf_xdp_link_dealloc, | |
73b11c2a | 9653 | .detach = bpf_xdp_link_detach, |
c1931c97 AN |
9654 | .show_fdinfo = bpf_xdp_link_show_fdinfo, |
9655 | .fill_link_info = bpf_xdp_link_fill_link_info, | |
026a4c28 | 9656 | .update_prog = bpf_xdp_link_update, |
aa8d3a71 AN |
9657 | }; |
9658 | ||
9659 | int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
9660 | { | |
9661 | struct net *net = current->nsproxy->net_ns; | |
9662 | struct bpf_link_primer link_primer; | |
bf4ea1d0 | 9663 | struct netlink_ext_ack extack = {}; |
aa8d3a71 AN |
9664 | struct bpf_xdp_link *link; |
9665 | struct net_device *dev; | |
9666 | int err, fd; | |
9667 | ||
5acc7d3e | 9668 | rtnl_lock(); |
aa8d3a71 | 9669 | dev = dev_get_by_index(net, attr->link_create.target_ifindex); |
5acc7d3e XZ |
9670 | if (!dev) { |
9671 | rtnl_unlock(); | |
aa8d3a71 | 9672 | return -EINVAL; |
5acc7d3e | 9673 | } |
aa8d3a71 AN |
9674 | |
9675 | link = kzalloc(sizeof(*link), GFP_USER); | |
9676 | if (!link) { | |
9677 | err = -ENOMEM; | |
5acc7d3e | 9678 | goto unlock; |
aa8d3a71 AN |
9679 | } |
9680 | ||
9681 | bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog); | |
9682 | link->dev = dev; | |
9683 | link->flags = attr->link_create.flags; | |
9684 | ||
9685 | err = bpf_link_prime(&link->link, &link_primer); | |
9686 | if (err) { | |
9687 | kfree(link); | |
5acc7d3e | 9688 | goto unlock; |
aa8d3a71 AN |
9689 | } |
9690 | ||
bf4ea1d0 | 9691 | err = dev_xdp_attach_link(dev, &extack, link); |
aa8d3a71 AN |
9692 | rtnl_unlock(); |
9693 | ||
9694 | if (err) { | |
5acc7d3e | 9695 | link->dev = NULL; |
aa8d3a71 | 9696 | bpf_link_cleanup(&link_primer); |
bf4ea1d0 | 9697 | trace_bpf_xdp_link_attach_failed(extack._msg); |
aa8d3a71 AN |
9698 | goto out_put_dev; |
9699 | } | |
9700 | ||
9701 | fd = bpf_link_settle(&link_primer); | |
9702 | /* link itself doesn't hold dev's refcnt to not complicate shutdown */ | |
9703 | dev_put(dev); | |
9704 | return fd; | |
9705 | ||
5acc7d3e XZ |
9706 | unlock: |
9707 | rtnl_unlock(); | |
9708 | ||
aa8d3a71 AN |
9709 | out_put_dev: |
9710 | dev_put(dev); | |
9711 | return err; | |
9712 | } | |
9713 | ||
d4baa936 AN |
9714 | /** |
9715 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path | |
9716 | * @dev: device | |
9717 | * @extack: netlink extended ack | |
9718 | * @fd: new program fd or negative value to clear | |
9719 | * @expected_fd: old program fd that userspace expects to replace or clear | |
9720 | * @flags: xdp-related flags | |
9721 | * | |
9722 | * Set or clear a bpf program for a device | |
9723 | */ | |
9724 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, | |
9725 | int fd, int expected_fd, u32 flags) | |
9726 | { | |
c8a36f19 | 9727 | enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags); |
d4baa936 AN |
9728 | struct bpf_prog *new_prog = NULL, *old_prog = NULL; |
9729 | int err; | |
9730 | ||
9731 | ASSERT_RTNL(); | |
9732 | ||
9733 | if (fd >= 0) { | |
9734 | new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, | |
9735 | mode != XDP_MODE_SKB); | |
9736 | if (IS_ERR(new_prog)) | |
9737 | return PTR_ERR(new_prog); | |
9738 | } | |
9739 | ||
9740 | if (expected_fd >= 0) { | |
9741 | old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, | |
9742 | mode != XDP_MODE_SKB); | |
9743 | if (IS_ERR(old_prog)) { | |
9744 | err = PTR_ERR(old_prog); | |
9745 | old_prog = NULL; | |
9746 | goto err_out; | |
c14a9f63 | 9747 | } |
a7862b45 BB |
9748 | } |
9749 | ||
aa8d3a71 | 9750 | err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags); |
a7862b45 | 9751 | |
d4baa936 AN |
9752 | err_out: |
9753 | if (err && new_prog) | |
9754 | bpf_prog_put(new_prog); | |
9755 | if (old_prog) | |
9756 | bpf_prog_put(old_prog); | |
a7862b45 BB |
9757 | return err; |
9758 | } | |
a7862b45 | 9759 | |
1da177e4 | 9760 | /** |
759ab1ed JK |
9761 | * dev_index_reserve() - allocate an ifindex in a namespace |
9762 | * @net: the applicable net namespace | |
9763 | * @ifindex: requested ifindex, pass %0 to get one allocated | |
9764 | * | |
9765 | * Allocate a ifindex for a new device. Caller must either use the ifindex | |
9766 | * to store the device (via list_netdevice()) or call dev_index_release() | |
9767 | * to give the index up. | |
1da177e4 | 9768 | * |
759ab1ed | 9769 | * Return: a suitable unique value for a new device interface number or -errno. |
1da177e4 | 9770 | */ |
759ab1ed | 9771 | static int dev_index_reserve(struct net *net, u32 ifindex) |
1da177e4 | 9772 | { |
759ab1ed | 9773 | int err; |
f4563a75 | 9774 | |
956db0a1 JK |
9775 | if (ifindex > INT_MAX) { |
9776 | DEBUG_NET_WARN_ON_ONCE(1); | |
9777 | return -EINVAL; | |
9778 | } | |
9779 | ||
759ab1ed JK |
9780 | if (!ifindex) |
9781 | err = xa_alloc_cyclic(&net->dev_by_index, &ifindex, NULL, | |
9782 | xa_limit_31b, &net->ifindex, GFP_KERNEL); | |
9783 | else | |
9784 | err = xa_insert(&net->dev_by_index, ifindex, NULL, GFP_KERNEL); | |
9785 | if (err < 0) | |
9786 | return err; | |
9787 | ||
9788 | return ifindex; | |
9789 | } | |
9790 | ||
9791 | static void dev_index_release(struct net *net, int ifindex) | |
9792 | { | |
9793 | /* Expect only unused indexes, unlist_netdevice() removes the used */ | |
9794 | WARN_ON(xa_erase(&net->dev_by_index, ifindex)); | |
1da177e4 LT |
9795 | } |
9796 | ||
1da177e4 | 9797 | /* Delayed registration/unregisteration */ |
0b5c21bb | 9798 | LIST_HEAD(net_todo_list); |
200b916f | 9799 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
ffabe98c | 9800 | atomic_t dev_unreg_count = ATOMIC_INIT(0); |
1da177e4 | 9801 | |
6f05f629 | 9802 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 9803 | { |
1da177e4 | 9804 | list_add_tail(&dev->todo_list, &net_todo_list); |
1da177e4 LT |
9805 | } |
9806 | ||
fd867d51 JW |
9807 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
9808 | struct net_device *upper, netdev_features_t features) | |
9809 | { | |
9810 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9811 | netdev_features_t feature; | |
5ba3f7d6 | 9812 | int feature_bit; |
fd867d51 | 9813 | |
3b89ea9c | 9814 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9815 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9816 | if (!(upper->wanted_features & feature) |
9817 | && (features & feature)) { | |
9818 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", | |
9819 | &feature, upper->name); | |
9820 | features &= ~feature; | |
9821 | } | |
9822 | } | |
9823 | ||
9824 | return features; | |
9825 | } | |
9826 | ||
9827 | static void netdev_sync_lower_features(struct net_device *upper, | |
9828 | struct net_device *lower, netdev_features_t features) | |
9829 | { | |
9830 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9831 | netdev_features_t feature; | |
5ba3f7d6 | 9832 | int feature_bit; |
fd867d51 | 9833 | |
3b89ea9c | 9834 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9835 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9836 | if (!(features & feature) && (lower->features & feature)) { |
9837 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", | |
9838 | &feature, lower->name); | |
9839 | lower->wanted_features &= ~feature; | |
dd912306 | 9840 | __netdev_update_features(lower); |
fd867d51 JW |
9841 | |
9842 | if (unlikely(lower->features & feature)) | |
9843 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", | |
9844 | &feature, lower->name); | |
dd912306 CW |
9845 | else |
9846 | netdev_features_change(lower); | |
fd867d51 JW |
9847 | } |
9848 | } | |
9849 | } | |
9850 | ||
c8f44aff MM |
9851 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
9852 | netdev_features_t features) | |
b63365a2 | 9853 | { |
57422dc5 MM |
9854 | /* Fix illegal checksum combinations */ |
9855 | if ((features & NETIF_F_HW_CSUM) && | |
9856 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 9857 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
9858 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
9859 | } | |
9860 | ||
b63365a2 | 9861 | /* TSO requires that SG is present as well. */ |
ea2d3688 | 9862 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 9863 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 9864 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
9865 | } |
9866 | ||
ec5f0615 PS |
9867 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
9868 | !(features & NETIF_F_IP_CSUM)) { | |
9869 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); | |
9870 | features &= ~NETIF_F_TSO; | |
9871 | features &= ~NETIF_F_TSO_ECN; | |
9872 | } | |
9873 | ||
9874 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && | |
9875 | !(features & NETIF_F_IPV6_CSUM)) { | |
9876 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); | |
9877 | features &= ~NETIF_F_TSO6; | |
9878 | } | |
9879 | ||
b1dc497b AD |
9880 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
9881 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) | |
9882 | features &= ~NETIF_F_TSO_MANGLEID; | |
9883 | ||
31d8b9e0 BH |
9884 | /* TSO ECN requires that TSO is present as well. */ |
9885 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
9886 | features &= ~NETIF_F_TSO_ECN; | |
9887 | ||
212b573f MM |
9888 | /* Software GSO depends on SG. */ |
9889 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 9890 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
9891 | features &= ~NETIF_F_GSO; |
9892 | } | |
9893 | ||
802ab55a AD |
9894 | /* GSO partial features require GSO partial be set */ |
9895 | if ((features & dev->gso_partial_features) && | |
9896 | !(features & NETIF_F_GSO_PARTIAL)) { | |
9897 | netdev_dbg(dev, | |
9898 | "Dropping partially supported GSO features since no GSO partial.\n"); | |
9899 | features &= ~dev->gso_partial_features; | |
9900 | } | |
9901 | ||
fb1f5f79 MC |
9902 | if (!(features & NETIF_F_RXCSUM)) { |
9903 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet | |
9904 | * successfully merged by hardware must also have the | |
9905 | * checksum verified by hardware. If the user does not | |
9906 | * want to enable RXCSUM, logically, we should disable GRO_HW. | |
9907 | */ | |
9908 | if (features & NETIF_F_GRO_HW) { | |
9909 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); | |
9910 | features &= ~NETIF_F_GRO_HW; | |
9911 | } | |
9912 | } | |
9913 | ||
de8d5ab2 GP |
9914 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
9915 | if (features & NETIF_F_RXFCS) { | |
9916 | if (features & NETIF_F_LRO) { | |
9917 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); | |
9918 | features &= ~NETIF_F_LRO; | |
9919 | } | |
9920 | ||
9921 | if (features & NETIF_F_GRO_HW) { | |
9922 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); | |
9923 | features &= ~NETIF_F_GRO_HW; | |
9924 | } | |
e6c6a929 GP |
9925 | } |
9926 | ||
54b2b3ec BB |
9927 | if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) { |
9928 | netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n"); | |
9929 | features &= ~NETIF_F_LRO; | |
9930 | } | |
9931 | ||
25537d71 TT |
9932 | if (features & NETIF_F_HW_TLS_TX) { |
9933 | bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) == | |
9934 | (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); | |
9935 | bool hw_csum = features & NETIF_F_HW_CSUM; | |
9936 | ||
9937 | if (!ip_csum && !hw_csum) { | |
9938 | netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n"); | |
9939 | features &= ~NETIF_F_HW_TLS_TX; | |
9940 | } | |
ae0b04b2 TT |
9941 | } |
9942 | ||
a3eb4e9d TT |
9943 | if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) { |
9944 | netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n"); | |
9945 | features &= ~NETIF_F_HW_TLS_RX; | |
9946 | } | |
9947 | ||
b63365a2 HX |
9948 | return features; |
9949 | } | |
b63365a2 | 9950 | |
6cb6a27c | 9951 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 9952 | { |
fd867d51 | 9953 | struct net_device *upper, *lower; |
c8f44aff | 9954 | netdev_features_t features; |
fd867d51 | 9955 | struct list_head *iter; |
e7868a85 | 9956 | int err = -1; |
5455c699 | 9957 | |
87267485 MM |
9958 | ASSERT_RTNL(); |
9959 | ||
5455c699 MM |
9960 | features = netdev_get_wanted_features(dev); |
9961 | ||
9962 | if (dev->netdev_ops->ndo_fix_features) | |
9963 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
9964 | ||
9965 | /* driver might be less strict about feature dependencies */ | |
9966 | features = netdev_fix_features(dev, features); | |
9967 | ||
4250b75b | 9968 | /* some features can't be enabled if they're off on an upper device */ |
fd867d51 JW |
9969 | netdev_for_each_upper_dev_rcu(dev, upper, iter) |
9970 | features = netdev_sync_upper_features(dev, upper, features); | |
9971 | ||
5455c699 | 9972 | if (dev->features == features) |
e7868a85 | 9973 | goto sync_lower; |
5455c699 | 9974 | |
c8f44aff MM |
9975 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
9976 | &dev->features, &features); | |
5455c699 MM |
9977 | |
9978 | if (dev->netdev_ops->ndo_set_features) | |
9979 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
5f8dc33e NA |
9980 | else |
9981 | err = 0; | |
5455c699 | 9982 | |
6cb6a27c | 9983 | if (unlikely(err < 0)) { |
5455c699 | 9984 | netdev_err(dev, |
c8f44aff MM |
9985 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
9986 | err, &features, &dev->features); | |
17b85d29 NA |
9987 | /* return non-0 since some features might have changed and |
9988 | * it's better to fire a spurious notification than miss it | |
9989 | */ | |
9990 | return -1; | |
6cb6a27c MM |
9991 | } |
9992 | ||
e7868a85 | 9993 | sync_lower: |
fd867d51 JW |
9994 | /* some features must be disabled on lower devices when disabled |
9995 | * on an upper device (think: bonding master or bridge) | |
9996 | */ | |
9997 | netdev_for_each_lower_dev(dev, lower, iter) | |
9998 | netdev_sync_lower_features(dev, lower, features); | |
9999 | ||
ae847f40 SD |
10000 | if (!err) { |
10001 | netdev_features_t diff = features ^ dev->features; | |
10002 | ||
10003 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
10004 | /* udp_tunnel_{get,drop}_rx_info both need | |
10005 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the | |
10006 | * device, or they won't do anything. | |
10007 | * Thus we need to update dev->features | |
10008 | * *before* calling udp_tunnel_get_rx_info, | |
10009 | * but *after* calling udp_tunnel_drop_rx_info. | |
10010 | */ | |
10011 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
10012 | dev->features = features; | |
10013 | udp_tunnel_get_rx_info(dev); | |
10014 | } else { | |
10015 | udp_tunnel_drop_rx_info(dev); | |
10016 | } | |
10017 | } | |
10018 | ||
9daae9bd GP |
10019 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
10020 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { | |
10021 | dev->features = features; | |
10022 | err |= vlan_get_rx_ctag_filter_info(dev); | |
10023 | } else { | |
10024 | vlan_drop_rx_ctag_filter_info(dev); | |
10025 | } | |
10026 | } | |
10027 | ||
10028 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { | |
10029 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { | |
10030 | dev->features = features; | |
10031 | err |= vlan_get_rx_stag_filter_info(dev); | |
10032 | } else { | |
10033 | vlan_drop_rx_stag_filter_info(dev); | |
10034 | } | |
10035 | } | |
10036 | ||
6cb6a27c | 10037 | dev->features = features; |
ae847f40 | 10038 | } |
6cb6a27c | 10039 | |
e7868a85 | 10040 | return err < 0 ? 0 : 1; |
6cb6a27c MM |
10041 | } |
10042 | ||
afe12cc8 MM |
10043 | /** |
10044 | * netdev_update_features - recalculate device features | |
10045 | * @dev: the device to check | |
10046 | * | |
10047 | * Recalculate dev->features set and send notifications if it | |
10048 | * has changed. Should be called after driver or hardware dependent | |
10049 | * conditions might have changed that influence the features. | |
10050 | */ | |
6cb6a27c MM |
10051 | void netdev_update_features(struct net_device *dev) |
10052 | { | |
10053 | if (__netdev_update_features(dev)) | |
10054 | netdev_features_change(dev); | |
5455c699 MM |
10055 | } |
10056 | EXPORT_SYMBOL(netdev_update_features); | |
10057 | ||
afe12cc8 MM |
10058 | /** |
10059 | * netdev_change_features - recalculate device features | |
10060 | * @dev: the device to check | |
10061 | * | |
10062 | * Recalculate dev->features set and send notifications even | |
10063 | * if they have not changed. Should be called instead of | |
10064 | * netdev_update_features() if also dev->vlan_features might | |
10065 | * have changed to allow the changes to be propagated to stacked | |
10066 | * VLAN devices. | |
10067 | */ | |
10068 | void netdev_change_features(struct net_device *dev) | |
10069 | { | |
10070 | __netdev_update_features(dev); | |
10071 | netdev_features_change(dev); | |
10072 | } | |
10073 | EXPORT_SYMBOL(netdev_change_features); | |
10074 | ||
fc4a7489 PM |
10075 | /** |
10076 | * netif_stacked_transfer_operstate - transfer operstate | |
10077 | * @rootdev: the root or lower level device to transfer state from | |
10078 | * @dev: the device to transfer operstate to | |
10079 | * | |
10080 | * Transfer operational state from root to device. This is normally | |
10081 | * called when a stacking relationship exists between the root | |
10082 | * device and the device(a leaf device). | |
10083 | */ | |
10084 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
10085 | struct net_device *dev) | |
10086 | { | |
10087 | if (rootdev->operstate == IF_OPER_DORMANT) | |
10088 | netif_dormant_on(dev); | |
10089 | else | |
10090 | netif_dormant_off(dev); | |
10091 | ||
eec517cd AL |
10092 | if (rootdev->operstate == IF_OPER_TESTING) |
10093 | netif_testing_on(dev); | |
10094 | else | |
10095 | netif_testing_off(dev); | |
10096 | ||
0575c86b ZS |
10097 | if (netif_carrier_ok(rootdev)) |
10098 | netif_carrier_on(dev); | |
10099 | else | |
10100 | netif_carrier_off(dev); | |
fc4a7489 PM |
10101 | } |
10102 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
10103 | ||
1b4bf461 ED |
10104 | static int netif_alloc_rx_queues(struct net_device *dev) |
10105 | { | |
1b4bf461 | 10106 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 10107 | struct netdev_rx_queue *rx; |
10595902 | 10108 | size_t sz = count * sizeof(*rx); |
e817f856 | 10109 | int err = 0; |
1b4bf461 | 10110 | |
bd25fa7b | 10111 | BUG_ON(count < 1); |
1b4bf461 | 10112 | |
c948f51c | 10113 | rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
10114 | if (!rx) |
10115 | return -ENOMEM; | |
10116 | ||
bd25fa7b TH |
10117 | dev->_rx = rx; |
10118 | ||
e817f856 | 10119 | for (i = 0; i < count; i++) { |
fe822240 | 10120 | rx[i].dev = dev; |
e817f856 JDB |
10121 | |
10122 | /* XDP RX-queue setup */ | |
b02e5a0e | 10123 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0); |
e817f856 JDB |
10124 | if (err < 0) |
10125 | goto err_rxq_info; | |
10126 | } | |
1b4bf461 | 10127 | return 0; |
e817f856 JDB |
10128 | |
10129 | err_rxq_info: | |
10130 | /* Rollback successful reg's and free other resources */ | |
10131 | while (i--) | |
10132 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); | |
141b52a9 | 10133 | kvfree(dev->_rx); |
e817f856 JDB |
10134 | dev->_rx = NULL; |
10135 | return err; | |
10136 | } | |
10137 | ||
10138 | static void netif_free_rx_queues(struct net_device *dev) | |
10139 | { | |
10140 | unsigned int i, count = dev->num_rx_queues; | |
e817f856 JDB |
10141 | |
10142 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ | |
10143 | if (!dev->_rx) | |
10144 | return; | |
10145 | ||
e817f856 | 10146 | for (i = 0; i < count; i++) |
82aaff2f JK |
10147 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
10148 | ||
10149 | kvfree(dev->_rx); | |
1b4bf461 ED |
10150 | } |
10151 | ||
aa942104 CG |
10152 | static void netdev_init_one_queue(struct net_device *dev, |
10153 | struct netdev_queue *queue, void *_unused) | |
10154 | { | |
10155 | /* Initialize queue lock */ | |
10156 | spin_lock_init(&queue->_xmit_lock); | |
1a33e10e | 10157 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); |
aa942104 | 10158 | queue->xmit_lock_owner = -1; |
b236da69 | 10159 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 10160 | queue->dev = dev; |
114cf580 TH |
10161 | #ifdef CONFIG_BQL |
10162 | dql_init(&queue->dql, HZ); | |
10163 | #endif | |
aa942104 CG |
10164 | } |
10165 | ||
60877a32 ED |
10166 | static void netif_free_tx_queues(struct net_device *dev) |
10167 | { | |
4cb28970 | 10168 | kvfree(dev->_tx); |
60877a32 ED |
10169 | } |
10170 | ||
e6484930 TH |
10171 | static int netif_alloc_netdev_queues(struct net_device *dev) |
10172 | { | |
10173 | unsigned int count = dev->num_tx_queues; | |
10174 | struct netdev_queue *tx; | |
60877a32 | 10175 | size_t sz = count * sizeof(*tx); |
e6484930 | 10176 | |
d339727c ED |
10177 | if (count < 1 || count > 0xffff) |
10178 | return -EINVAL; | |
62b5942a | 10179 | |
c948f51c | 10180 | tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
10181 | if (!tx) |
10182 | return -ENOMEM; | |
10183 | ||
e6484930 | 10184 | dev->_tx = tx; |
1d24eb48 | 10185 | |
e6484930 TH |
10186 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
10187 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
10188 | |
10189 | return 0; | |
e6484930 TH |
10190 | } |
10191 | ||
a2029240 DV |
10192 | void netif_tx_stop_all_queues(struct net_device *dev) |
10193 | { | |
10194 | unsigned int i; | |
10195 | ||
10196 | for (i = 0; i < dev->num_tx_queues; i++) { | |
10197 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
f4563a75 | 10198 | |
a2029240 DV |
10199 | netif_tx_stop_queue(txq); |
10200 | } | |
10201 | } | |
10202 | EXPORT_SYMBOL(netif_tx_stop_all_queues); | |
10203 | ||
34d21de9 DB |
10204 | static int netdev_do_alloc_pcpu_stats(struct net_device *dev) |
10205 | { | |
10206 | void __percpu *v; | |
10207 | ||
024ee930 PY |
10208 | /* Drivers implementing ndo_get_peer_dev must support tstat |
10209 | * accounting, so that skb_do_redirect() can bump the dev's | |
10210 | * RX stats upon network namespace switch. | |
10211 | */ | |
10212 | if (dev->netdev_ops->ndo_get_peer_dev && | |
10213 | dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS) | |
10214 | return -EOPNOTSUPP; | |
10215 | ||
34d21de9 DB |
10216 | switch (dev->pcpu_stat_type) { |
10217 | case NETDEV_PCPU_STAT_NONE: | |
10218 | return 0; | |
10219 | case NETDEV_PCPU_STAT_LSTATS: | |
10220 | v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats); | |
10221 | break; | |
10222 | case NETDEV_PCPU_STAT_TSTATS: | |
10223 | v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); | |
10224 | break; | |
10225 | case NETDEV_PCPU_STAT_DSTATS: | |
10226 | v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); | |
10227 | break; | |
10228 | default: | |
10229 | return -EINVAL; | |
10230 | } | |
10231 | ||
10232 | return v ? 0 : -ENOMEM; | |
10233 | } | |
10234 | ||
10235 | static void netdev_do_free_pcpu_stats(struct net_device *dev) | |
10236 | { | |
10237 | switch (dev->pcpu_stat_type) { | |
10238 | case NETDEV_PCPU_STAT_NONE: | |
10239 | return; | |
10240 | case NETDEV_PCPU_STAT_LSTATS: | |
10241 | free_percpu(dev->lstats); | |
10242 | break; | |
10243 | case NETDEV_PCPU_STAT_TSTATS: | |
10244 | free_percpu(dev->tstats); | |
10245 | break; | |
10246 | case NETDEV_PCPU_STAT_DSTATS: | |
10247 | free_percpu(dev->dstats); | |
10248 | break; | |
10249 | } | |
10250 | } | |
10251 | ||
1da177e4 | 10252 | /** |
fa926bb3 JK |
10253 | * register_netdevice() - register a network device |
10254 | * @dev: device to register | |
1da177e4 | 10255 | * |
fa926bb3 JK |
10256 | * Take a prepared network device structure and make it externally accessible. |
10257 | * A %NETDEV_REGISTER message is sent to the netdev notifier chain. | |
10258 | * Callers must hold the rtnl lock - you may want register_netdev() | |
10259 | * instead of this. | |
1da177e4 | 10260 | */ |
1da177e4 LT |
10261 | int register_netdevice(struct net_device *dev) |
10262 | { | |
1da177e4 | 10263 | int ret; |
d314774c | 10264 | struct net *net = dev_net(dev); |
1da177e4 | 10265 | |
e283de3a FF |
10266 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
10267 | NETDEV_FEATURE_COUNT); | |
1da177e4 LT |
10268 | BUG_ON(dev_boot_phase); |
10269 | ASSERT_RTNL(); | |
10270 | ||
b17a7c17 SH |
10271 | might_sleep(); |
10272 | ||
1da177e4 LT |
10273 | /* When net_device's are persistent, this will be fatal. */ |
10274 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 10275 | BUG_ON(!net); |
1da177e4 | 10276 | |
9000edb7 JK |
10277 | ret = ethtool_check_ops(dev->ethtool_ops); |
10278 | if (ret) | |
10279 | return ret; | |
10280 | ||
f1f28aa3 | 10281 | spin_lock_init(&dev->addr_list_lock); |
845e0ebb | 10282 | netdev_set_addr_lockdep_class(dev); |
1da177e4 | 10283 | |
828de4f6 | 10284 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
10285 | if (ret < 0) |
10286 | goto out; | |
10287 | ||
9077f052 | 10288 | ret = -ENOMEM; |
ff927412 JP |
10289 | dev->name_node = netdev_name_node_head_alloc(dev); |
10290 | if (!dev->name_node) | |
10291 | goto out; | |
10292 | ||
1da177e4 | 10293 | /* Init, if this function is available */ |
d314774c SH |
10294 | if (dev->netdev_ops->ndo_init) { |
10295 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
10296 | if (ret) { |
10297 | if (ret > 0) | |
10298 | ret = -EIO; | |
42c17fa6 | 10299 | goto err_free_name; |
1da177e4 LT |
10300 | } |
10301 | } | |
4ec93edb | 10302 | |
f646968f PM |
10303 | if (((dev->hw_features | dev->features) & |
10304 | NETIF_F_HW_VLAN_CTAG_FILTER) && | |
d2ed273d MM |
10305 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
10306 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { | |
10307 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); | |
10308 | ret = -EINVAL; | |
10309 | goto err_uninit; | |
10310 | } | |
10311 | ||
34d21de9 DB |
10312 | ret = netdev_do_alloc_pcpu_stats(dev); |
10313 | if (ret) | |
10314 | goto err_uninit; | |
10315 | ||
759ab1ed JK |
10316 | ret = dev_index_reserve(net, dev->ifindex); |
10317 | if (ret < 0) | |
34d21de9 | 10318 | goto err_free_pcpu; |
759ab1ed | 10319 | dev->ifindex = ret; |
9c7dafbf | 10320 | |
5455c699 MM |
10321 | /* Transfer changeable features to wanted_features and enable |
10322 | * software offloads (GSO and GRO). | |
10323 | */ | |
1a3c998f | 10324 | dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); |
14d1232f | 10325 | dev->features |= NETIF_F_SOFT_FEATURES; |
d764a122 | 10326 | |
876c4384 | 10327 | if (dev->udp_tunnel_nic_info) { |
d764a122 SD |
10328 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; |
10329 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
10330 | } | |
10331 | ||
14d1232f | 10332 | dev->wanted_features = dev->features & dev->hw_features; |
1da177e4 | 10333 | |
cbc53e08 | 10334 | if (!(dev->flags & IFF_LOOPBACK)) |
34324dc2 | 10335 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
cbc53e08 | 10336 | |
7f348a60 AD |
10337 | /* If IPv4 TCP segmentation offload is supported we should also |
10338 | * allow the device to enable segmenting the frame with the option | |
10339 | * of ignoring a static IP ID value. This doesn't enable the | |
10340 | * feature itself but allows the user to enable it later. | |
10341 | */ | |
cbc53e08 AD |
10342 | if (dev->hw_features & NETIF_F_TSO) |
10343 | dev->hw_features |= NETIF_F_TSO_MANGLEID; | |
7f348a60 AD |
10344 | if (dev->vlan_features & NETIF_F_TSO) |
10345 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; | |
10346 | if (dev->mpls_features & NETIF_F_TSO) | |
10347 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; | |
10348 | if (dev->hw_enc_features & NETIF_F_TSO) | |
10349 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; | |
c6e1a0d1 | 10350 | |
1180e7d6 | 10351 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 10352 | */ |
1180e7d6 | 10353 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 10354 | |
ee579677 PS |
10355 | /* Make NETIF_F_SG inheritable to tunnel devices. |
10356 | */ | |
802ab55a | 10357 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
ee579677 | 10358 | |
0d89d203 SH |
10359 | /* Make NETIF_F_SG inheritable to MPLS. |
10360 | */ | |
10361 | dev->mpls_features |= NETIF_F_SG; | |
10362 | ||
7ffbe3fd JB |
10363 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
10364 | ret = notifier_to_errno(ret); | |
10365 | if (ret) | |
759ab1ed | 10366 | goto err_ifindex_release; |
7ffbe3fd | 10367 | |
8b41d188 | 10368 | ret = netdev_register_kobject(dev); |
e51b9624 | 10369 | |
4d42b37d | 10370 | WRITE_ONCE(dev->reg_state, ret ? NETREG_UNREGISTERED : NETREG_REGISTERED); |
e51b9624 | 10371 | |
cc26c266 | 10372 | if (ret) |
02a68a47 | 10373 | goto err_uninit_notify; |
b17a7c17 | 10374 | |
6cb6a27c | 10375 | __netdev_update_features(dev); |
8e9b59b2 | 10376 | |
1da177e4 LT |
10377 | /* |
10378 | * Default initial state at registry is that the | |
10379 | * device is present. | |
10380 | */ | |
10381 | ||
10382 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
10383 | ||
8f4cccbb BH |
10384 | linkwatch_init_dev(dev); |
10385 | ||
1da177e4 | 10386 | dev_init_scheduler(dev); |
b2309a71 | 10387 | |
d62607c3 | 10388 | netdev_hold(dev, &dev->dev_registered_tracker, GFP_KERNEL); |
ce286d32 | 10389 | list_netdevice(dev); |
b2309a71 | 10390 | |
7bf23575 | 10391 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 10392 | |
948b337e JP |
10393 | /* If the device has permanent device address, driver should |
10394 | * set dev_addr and also addr_assign_type should be set to | |
10395 | * NET_ADDR_PERM (default value). | |
10396 | */ | |
10397 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
10398 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
10399 | ||
1da177e4 | 10400 | /* Notify protocols, that a new device appeared. */ |
056925ab | 10401 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 10402 | ret = notifier_to_errno(ret); |
93ee31f1 | 10403 | if (ret) { |
766b0515 JK |
10404 | /* Expect explicit free_netdev() on failure */ |
10405 | dev->needs_free_netdev = false; | |
037e56bd | 10406 | unregister_netdevice_queue(dev, NULL); |
766b0515 | 10407 | goto out; |
93ee31f1 | 10408 | } |
d90a909e EB |
10409 | /* |
10410 | * Prevent userspace races by waiting until the network | |
10411 | * device is fully setup before sending notifications. | |
10412 | */ | |
a2835763 PM |
10413 | if (!dev->rtnl_link_ops || |
10414 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
1d997f10 | 10415 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL); |
1da177e4 LT |
10416 | |
10417 | out: | |
10418 | return ret; | |
7ce1b0ed | 10419 | |
02a68a47 JP |
10420 | err_uninit_notify: |
10421 | call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev); | |
759ab1ed JK |
10422 | err_ifindex_release: |
10423 | dev_index_release(net, dev->ifindex); | |
34d21de9 DB |
10424 | err_free_pcpu: |
10425 | netdev_do_free_pcpu_stats(dev); | |
7ce1b0ed | 10426 | err_uninit: |
d314774c SH |
10427 | if (dev->netdev_ops->ndo_uninit) |
10428 | dev->netdev_ops->ndo_uninit(dev); | |
cf124db5 DM |
10429 | if (dev->priv_destructor) |
10430 | dev->priv_destructor(dev); | |
42c17fa6 DC |
10431 | err_free_name: |
10432 | netdev_name_node_free(dev->name_node); | |
7ce1b0ed | 10433 | goto out; |
1da177e4 | 10434 | } |
d1b19dff | 10435 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 10436 | |
c661050f BL |
10437 | /* Initialize the core of a dummy net device. |
10438 | * This is useful if you are calling this function after alloc_netdev(), | |
10439 | * since it does not memset the net_device fields. | |
937f1ba5 | 10440 | */ |
c661050f | 10441 | static void init_dummy_netdev_core(struct net_device *dev) |
937f1ba5 | 10442 | { |
937f1ba5 BH |
10443 | /* make sure we BUG if trying to hit standard |
10444 | * register/unregister code path | |
10445 | */ | |
10446 | dev->reg_state = NETREG_DUMMY; | |
10447 | ||
937f1ba5 BH |
10448 | /* NAPI wants this */ |
10449 | INIT_LIST_HEAD(&dev->napi_list); | |
10450 | ||
10451 | /* a dummy interface is started by default */ | |
10452 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
10453 | set_bit(__LINK_STATE_START, &dev->state); | |
10454 | ||
35edfdc7 JE |
10455 | /* napi_busy_loop stats accounting wants this */ |
10456 | dev_net_set(dev, &init_net); | |
10457 | ||
29b4433d ED |
10458 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
10459 | * because users of this 'device' dont need to change | |
10460 | * its refcount. | |
10461 | */ | |
937f1ba5 | 10462 | } |
937f1ba5 | 10463 | |
c661050f BL |
10464 | /** |
10465 | * init_dummy_netdev - init a dummy network device for NAPI | |
10466 | * @dev: device to init | |
10467 | * | |
10468 | * This takes a network device structure and initializes the minimum | |
10469 | * amount of fields so it can be used to schedule NAPI polls without | |
10470 | * registering a full blown interface. This is to be used by drivers | |
10471 | * that need to tie several hardware interfaces to a single NAPI | |
10472 | * poll scheduler due to HW limitations. | |
10473 | */ | |
10474 | void init_dummy_netdev(struct net_device *dev) | |
10475 | { | |
10476 | /* Clear everything. Note we don't initialize spinlocks | |
10477 | * as they aren't supposed to be taken by any of the | |
10478 | * NAPI code and this dummy netdev is supposed to be | |
10479 | * only ever used for NAPI polls | |
10480 | */ | |
10481 | memset(dev, 0, sizeof(struct net_device)); | |
10482 | init_dummy_netdev_core(dev); | |
10483 | } | |
10484 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
937f1ba5 | 10485 | |
1da177e4 LT |
10486 | /** |
10487 | * register_netdev - register a network device | |
10488 | * @dev: device to register | |
10489 | * | |
10490 | * Take a completed network device structure and add it to the kernel | |
10491 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
10492 | * chain. 0 is returned on success. A negative errno code is returned | |
10493 | * on a failure to set up the device, or if the name is a duplicate. | |
10494 | * | |
38b4da38 | 10495 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
10496 | * and expands the device name if you passed a format string to |
10497 | * alloc_netdev. | |
10498 | */ | |
10499 | int register_netdev(struct net_device *dev) | |
10500 | { | |
10501 | int err; | |
10502 | ||
b0f3debc KT |
10503 | if (rtnl_lock_killable()) |
10504 | return -EINTR; | |
1da177e4 | 10505 | err = register_netdevice(dev); |
1da177e4 LT |
10506 | rtnl_unlock(); |
10507 | return err; | |
10508 | } | |
10509 | EXPORT_SYMBOL(register_netdev); | |
10510 | ||
29b4433d ED |
10511 | int netdev_refcnt_read(const struct net_device *dev) |
10512 | { | |
919067cc | 10513 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d ED |
10514 | int i, refcnt = 0; |
10515 | ||
10516 | for_each_possible_cpu(i) | |
10517 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
10518 | return refcnt; | |
919067cc ED |
10519 | #else |
10520 | return refcount_read(&dev->dev_refcnt); | |
10521 | #endif | |
29b4433d ED |
10522 | } |
10523 | EXPORT_SYMBOL(netdev_refcnt_read); | |
10524 | ||
5aa3afe1 DV |
10525 | int netdev_unregister_timeout_secs __read_mostly = 10; |
10526 | ||
de2b541b MCC |
10527 | #define WAIT_REFS_MIN_MSECS 1 |
10528 | #define WAIT_REFS_MAX_MSECS 250 | |
2c53040f | 10529 | /** |
faab39f6 JK |
10530 | * netdev_wait_allrefs_any - wait until all references are gone. |
10531 | * @list: list of net_devices to wait on | |
1da177e4 LT |
10532 | * |
10533 | * This is called when unregistering network devices. | |
10534 | * | |
10535 | * Any protocol or device that holds a reference should register | |
10536 | * for netdevice notification, and cleanup and put back the | |
10537 | * reference if they receive an UNREGISTER event. | |
10538 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 10539 | * call dev_put. |
1da177e4 | 10540 | */ |
faab39f6 | 10541 | static struct net_device *netdev_wait_allrefs_any(struct list_head *list) |
1da177e4 LT |
10542 | { |
10543 | unsigned long rebroadcast_time, warning_time; | |
faab39f6 JK |
10544 | struct net_device *dev; |
10545 | int wait = 0; | |
1da177e4 LT |
10546 | |
10547 | rebroadcast_time = warning_time = jiffies; | |
29b4433d | 10548 | |
faab39f6 JK |
10549 | list_for_each_entry(dev, list, todo_list) |
10550 | if (netdev_refcnt_read(dev) == 1) | |
10551 | return dev; | |
10552 | ||
10553 | while (true) { | |
1da177e4 | 10554 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 10555 | rtnl_lock(); |
1da177e4 LT |
10556 | |
10557 | /* Rebroadcast unregister notification */ | |
faab39f6 JK |
10558 | list_for_each_entry(dev, list, todo_list) |
10559 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
1da177e4 | 10560 | |
748e2d93 | 10561 | __rtnl_unlock(); |
0115e8e3 | 10562 | rcu_barrier(); |
748e2d93 ED |
10563 | rtnl_lock(); |
10564 | ||
faab39f6 JK |
10565 | list_for_each_entry(dev, list, todo_list) |
10566 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, | |
10567 | &dev->state)) { | |
10568 | /* We must not have linkwatch events | |
10569 | * pending on unregister. If this | |
10570 | * happens, we simply run the queue | |
10571 | * unscheduled, resulting in a noop | |
10572 | * for this device. | |
10573 | */ | |
10574 | linkwatch_run_queue(); | |
10575 | break; | |
10576 | } | |
1da177e4 | 10577 | |
6756ae4b | 10578 | __rtnl_unlock(); |
1da177e4 LT |
10579 | |
10580 | rebroadcast_time = jiffies; | |
10581 | } | |
10582 | ||
cd42ba1c ED |
10583 | rcu_barrier(); |
10584 | ||
0e4be9e5 | 10585 | if (!wait) { |
0e4be9e5 FR |
10586 | wait = WAIT_REFS_MIN_MSECS; |
10587 | } else { | |
10588 | msleep(wait); | |
10589 | wait = min(wait << 1, WAIT_REFS_MAX_MSECS); | |
10590 | } | |
1da177e4 | 10591 | |
faab39f6 JK |
10592 | list_for_each_entry(dev, list, todo_list) |
10593 | if (netdev_refcnt_read(dev) == 1) | |
10594 | return dev; | |
29b4433d | 10595 | |
faab39f6 | 10596 | if (time_after(jiffies, warning_time + |
05e49cfc | 10597 | READ_ONCE(netdev_unregister_timeout_secs) * HZ)) { |
faab39f6 JK |
10598 | list_for_each_entry(dev, list, todo_list) { |
10599 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", | |
10600 | dev->name, netdev_refcnt_read(dev)); | |
10601 | ref_tracker_dir_print(&dev->refcnt_tracker, 10); | |
10602 | } | |
10603 | ||
1da177e4 LT |
10604 | warning_time = jiffies; |
10605 | } | |
10606 | } | |
10607 | } | |
10608 | ||
10609 | /* The sequence is: | |
10610 | * | |
10611 | * rtnl_lock(); | |
10612 | * ... | |
10613 | * register_netdevice(x1); | |
10614 | * register_netdevice(x2); | |
10615 | * ... | |
10616 | * unregister_netdevice(y1); | |
10617 | * unregister_netdevice(y2); | |
10618 | * ... | |
10619 | * rtnl_unlock(); | |
10620 | * free_netdev(y1); | |
10621 | * free_netdev(y2); | |
10622 | * | |
58ec3b4d | 10623 | * We are invoked by rtnl_unlock(). |
1da177e4 | 10624 | * This allows us to deal with problems: |
b17a7c17 | 10625 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
10626 | * without deadlocking with linkwatch via keventd. |
10627 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
10628 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
10629 | * |
10630 | * We must not return until all unregister events added during | |
10631 | * the interval the lock was held have been completed. | |
1da177e4 | 10632 | */ |
1da177e4 LT |
10633 | void netdev_run_todo(void) |
10634 | { | |
ae68db14 | 10635 | struct net_device *dev, *tmp; |
626ab0e6 | 10636 | struct list_head list; |
ffabe98c | 10637 | int cnt; |
1fc70edb TY |
10638 | #ifdef CONFIG_LOCKDEP |
10639 | struct list_head unlink_list; | |
10640 | ||
10641 | list_replace_init(&net_unlink_list, &unlink_list); | |
10642 | ||
10643 | while (!list_empty(&unlink_list)) { | |
10644 | struct net_device *dev = list_first_entry(&unlink_list, | |
10645 | struct net_device, | |
10646 | unlink_list); | |
0e8b8d6a | 10647 | list_del_init(&dev->unlink_list); |
1fc70edb TY |
10648 | dev->nested_level = dev->lower_level - 1; |
10649 | } | |
10650 | #endif | |
1da177e4 | 10651 | |
1da177e4 | 10652 | /* Snapshot list, allow later requests */ |
626ab0e6 | 10653 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
10654 | |
10655 | __rtnl_unlock(); | |
626ab0e6 | 10656 | |
0115e8e3 | 10657 | /* Wait for rcu callbacks to finish before next phase */ |
850a545b EB |
10658 | if (!list_empty(&list)) |
10659 | rcu_barrier(); | |
10660 | ||
ae68db14 | 10661 | list_for_each_entry_safe(dev, tmp, &list, todo_list) { |
b17a7c17 | 10662 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
ae68db14 JK |
10663 | netdev_WARN(dev, "run_todo but not unregistering\n"); |
10664 | list_del(&dev->todo_list); | |
b17a7c17 SH |
10665 | continue; |
10666 | } | |
1da177e4 | 10667 | |
4d42b37d | 10668 | WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERED); |
facd15df | 10669 | linkwatch_sync_dev(dev); |
ae68db14 JK |
10670 | } |
10671 | ||
ffabe98c | 10672 | cnt = 0; |
ae68db14 | 10673 | while (!list_empty(&list)) { |
faab39f6 | 10674 | dev = netdev_wait_allrefs_any(&list); |
ae68db14 | 10675 | list_del(&dev->todo_list); |
1da177e4 | 10676 | |
b17a7c17 | 10677 | /* paranoia */ |
add2d736 | 10678 | BUG_ON(netdev_refcnt_read(dev) != 1); |
7866a621 SN |
10679 | BUG_ON(!list_empty(&dev->ptype_all)); |
10680 | BUG_ON(!list_empty(&dev->ptype_specific)); | |
33d480ce ED |
10681 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
10682 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
1202cdd6 | 10683 | |
34d21de9 | 10684 | netdev_do_free_pcpu_stats(dev); |
cf124db5 DM |
10685 | if (dev->priv_destructor) |
10686 | dev->priv_destructor(dev); | |
10687 | if (dev->needs_free_netdev) | |
10688 | free_netdev(dev); | |
9093bbb2 | 10689 | |
ffabe98c | 10690 | cnt++; |
50624c93 | 10691 | |
9093bbb2 SH |
10692 | /* Free network device */ |
10693 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 10694 | } |
ffabe98c ED |
10695 | if (cnt && atomic_sub_and_test(cnt, &dev_unreg_count)) |
10696 | wake_up(&netdev_unregistering_wq); | |
1da177e4 LT |
10697 | } |
10698 | ||
9256645a JW |
10699 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
10700 | * all the same fields in the same order as net_device_stats, with only | |
10701 | * the type differing, but rtnl_link_stats64 may have additional fields | |
10702 | * at the end for newer counters. | |
3cfde79c | 10703 | */ |
77a1abf5 ED |
10704 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
10705 | const struct net_device_stats *netdev_stats) | |
3cfde79c | 10706 | { |
6c1c5097 ED |
10707 | size_t i, n = sizeof(*netdev_stats) / sizeof(atomic_long_t); |
10708 | const atomic_long_t *src = (atomic_long_t *)netdev_stats; | |
3cfde79c BH |
10709 | u64 *dst = (u64 *)stats64; |
10710 | ||
9256645a | 10711 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
3cfde79c | 10712 | for (i = 0; i < n; i++) |
9b55d3f0 | 10713 | dst[i] = (unsigned long)atomic_long_read(&src[i]); |
9256645a JW |
10714 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10715 | memset((char *)stats64 + n * sizeof(u64), 0, | |
10716 | sizeof(*stats64) - n * sizeof(u64)); | |
3cfde79c | 10717 | } |
77a1abf5 | 10718 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 10719 | |
5247dbf1 YD |
10720 | static __cold struct net_device_core_stats __percpu *netdev_core_stats_alloc( |
10721 | struct net_device *dev) | |
625788b5 ED |
10722 | { |
10723 | struct net_device_core_stats __percpu *p; | |
10724 | ||
10725 | p = alloc_percpu_gfp(struct net_device_core_stats, | |
10726 | GFP_ATOMIC | __GFP_NOWARN); | |
10727 | ||
10728 | if (p && cmpxchg(&dev->core_stats, NULL, p)) | |
10729 | free_percpu(p); | |
10730 | ||
10731 | /* This READ_ONCE() pairs with the cmpxchg() above */ | |
6510ea97 | 10732 | return READ_ONCE(dev->core_stats); |
625788b5 | 10733 | } |
5247dbf1 YD |
10734 | |
10735 | noinline void netdev_core_stats_inc(struct net_device *dev, u32 offset) | |
10736 | { | |
10737 | /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */ | |
10738 | struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats); | |
10739 | unsigned long __percpu *field; | |
10740 | ||
10741 | if (unlikely(!p)) { | |
10742 | p = netdev_core_stats_alloc(dev); | |
10743 | if (!p) | |
10744 | return; | |
10745 | } | |
10746 | ||
10747 | field = (__force unsigned long __percpu *)((__force void *)p + offset); | |
10748 | this_cpu_inc(*field); | |
10749 | } | |
10750 | EXPORT_SYMBOL_GPL(netdev_core_stats_inc); | |
625788b5 | 10751 | |
eeda3fd6 SH |
10752 | /** |
10753 | * dev_get_stats - get network device statistics | |
10754 | * @dev: device to get statistics from | |
28172739 | 10755 | * @storage: place to store stats |
eeda3fd6 | 10756 | * |
d7753516 BH |
10757 | * Get network statistics from device. Return @storage. |
10758 | * The device driver may provide its own method by setting | |
10759 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
10760 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 10761 | */ |
d7753516 BH |
10762 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
10763 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 10764 | { |
eeda3fd6 | 10765 | const struct net_device_ops *ops = dev->netdev_ops; |
625788b5 | 10766 | const struct net_device_core_stats __percpu *p; |
eeda3fd6 | 10767 | |
28172739 ED |
10768 | if (ops->ndo_get_stats64) { |
10769 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
10770 | ops->ndo_get_stats64(dev, storage); |
10771 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 10772 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
3e2f544d BL |
10773 | } else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_TSTATS) { |
10774 | dev_get_tstats64(dev, storage); | |
caf586e5 ED |
10775 | } else { |
10776 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 10777 | } |
625788b5 ED |
10778 | |
10779 | /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */ | |
10780 | p = READ_ONCE(dev->core_stats); | |
10781 | if (p) { | |
10782 | const struct net_device_core_stats *core_stats; | |
10783 | int i; | |
10784 | ||
10785 | for_each_possible_cpu(i) { | |
10786 | core_stats = per_cpu_ptr(p, i); | |
6510ea97 SAS |
10787 | storage->rx_dropped += READ_ONCE(core_stats->rx_dropped); |
10788 | storage->tx_dropped += READ_ONCE(core_stats->tx_dropped); | |
10789 | storage->rx_nohandler += READ_ONCE(core_stats->rx_nohandler); | |
0e55546b | 10790 | storage->rx_otherhost_dropped += READ_ONCE(core_stats->rx_otherhost_dropped); |
625788b5 ED |
10791 | } |
10792 | } | |
28172739 | 10793 | return storage; |
c45d286e | 10794 | } |
eeda3fd6 | 10795 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 10796 | |
44fa32f0 HK |
10797 | /** |
10798 | * dev_fetch_sw_netstats - get per-cpu network device statistics | |
10799 | * @s: place to store stats | |
10800 | * @netstats: per-cpu network stats to read from | |
10801 | * | |
10802 | * Read per-cpu network statistics and populate the related fields in @s. | |
10803 | */ | |
10804 | void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s, | |
10805 | const struct pcpu_sw_netstats __percpu *netstats) | |
10806 | { | |
10807 | int cpu; | |
10808 | ||
10809 | for_each_possible_cpu(cpu) { | |
9962acef | 10810 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
44fa32f0 | 10811 | const struct pcpu_sw_netstats *stats; |
44fa32f0 HK |
10812 | unsigned int start; |
10813 | ||
10814 | stats = per_cpu_ptr(netstats, cpu); | |
10815 | do { | |
d120d1a6 | 10816 | start = u64_stats_fetch_begin(&stats->syncp); |
9962acef ED |
10817 | rx_packets = u64_stats_read(&stats->rx_packets); |
10818 | rx_bytes = u64_stats_read(&stats->rx_bytes); | |
10819 | tx_packets = u64_stats_read(&stats->tx_packets); | |
10820 | tx_bytes = u64_stats_read(&stats->tx_bytes); | |
d120d1a6 | 10821 | } while (u64_stats_fetch_retry(&stats->syncp, start)); |
44fa32f0 | 10822 | |
9962acef ED |
10823 | s->rx_packets += rx_packets; |
10824 | s->rx_bytes += rx_bytes; | |
10825 | s->tx_packets += tx_packets; | |
10826 | s->tx_bytes += tx_bytes; | |
44fa32f0 HK |
10827 | } |
10828 | } | |
10829 | EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats); | |
10830 | ||
a1839426 HK |
10831 | /** |
10832 | * dev_get_tstats64 - ndo_get_stats64 implementation | |
10833 | * @dev: device to get statistics from | |
10834 | * @s: place to store stats | |
10835 | * | |
10836 | * Populate @s from dev->stats and dev->tstats. Can be used as | |
10837 | * ndo_get_stats64() callback. | |
10838 | */ | |
10839 | void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s) | |
10840 | { | |
10841 | netdev_stats_to_stats64(s, &dev->stats); | |
10842 | dev_fetch_sw_netstats(s, dev->tstats); | |
10843 | } | |
10844 | EXPORT_SYMBOL_GPL(dev_get_tstats64); | |
10845 | ||
24824a09 | 10846 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 10847 | { |
24824a09 | 10848 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 10849 | |
24824a09 ED |
10850 | #ifdef CONFIG_NET_CLS_ACT |
10851 | if (queue) | |
10852 | return queue; | |
10853 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
10854 | if (!queue) | |
10855 | return NULL; | |
10856 | netdev_init_one_queue(dev, queue, NULL); | |
2ce1ee17 | 10857 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
d636fc5d | 10858 | RCU_INIT_POINTER(queue->qdisc_sleeping, &noop_qdisc); |
24824a09 ED |
10859 | rcu_assign_pointer(dev->ingress_queue, queue); |
10860 | #endif | |
10861 | return queue; | |
bb949fbd DM |
10862 | } |
10863 | ||
2c60db03 ED |
10864 | static const struct ethtool_ops default_ethtool_ops; |
10865 | ||
d07d7507 SG |
10866 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
10867 | const struct ethtool_ops *ops) | |
10868 | { | |
10869 | if (dev->ethtool_ops == &default_ethtool_ops) | |
10870 | dev->ethtool_ops = ops; | |
10871 | } | |
10872 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
10873 | ||
d9360708 HK |
10874 | /** |
10875 | * netdev_sw_irq_coalesce_default_on() - enable SW IRQ coalescing by default | |
10876 | * @dev: netdev to enable the IRQ coalescing on | |
10877 | * | |
10878 | * Sets a conservative default for SW IRQ coalescing. Users can use | |
10879 | * sysfs attributes to override the default values. | |
10880 | */ | |
10881 | void netdev_sw_irq_coalesce_default_on(struct net_device *dev) | |
10882 | { | |
10883 | WARN_ON(dev->reg_state == NETREG_REGISTERED); | |
10884 | ||
748b4428 HK |
10885 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) { |
10886 | dev->gro_flush_timeout = 20000; | |
10887 | dev->napi_defer_hard_irqs = 1; | |
10888 | } | |
d9360708 HK |
10889 | } |
10890 | EXPORT_SYMBOL_GPL(netdev_sw_irq_coalesce_default_on); | |
10891 | ||
74d332c1 ED |
10892 | void netdev_freemem(struct net_device *dev) |
10893 | { | |
10894 | char *addr = (char *)dev - dev->padded; | |
10895 | ||
4cb28970 | 10896 | kvfree(addr); |
74d332c1 ED |
10897 | } |
10898 | ||
1da177e4 | 10899 | /** |
722c9a0c | 10900 | * alloc_netdev_mqs - allocate network device |
10901 | * @sizeof_priv: size of private data to allocate space for | |
10902 | * @name: device name format string | |
10903 | * @name_assign_type: origin of device name | |
10904 | * @setup: callback to initialize device | |
10905 | * @txqs: the number of TX subqueues to allocate | |
10906 | * @rxqs: the number of RX subqueues to allocate | |
10907 | * | |
10908 | * Allocates a struct net_device with private data area for driver use | |
10909 | * and performs basic initialization. Also allocates subqueue structs | |
10910 | * for each queue on the device. | |
1da177e4 | 10911 | */ |
36909ea4 | 10912 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
c835a677 | 10913 | unsigned char name_assign_type, |
36909ea4 TH |
10914 | void (*setup)(struct net_device *), |
10915 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 10916 | { |
1da177e4 | 10917 | struct net_device *dev; |
52a59bd5 | 10918 | unsigned int alloc_size; |
1ce8e7b5 | 10919 | struct net_device *p; |
1da177e4 | 10920 | |
b6fe17d6 SH |
10921 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
10922 | ||
36909ea4 | 10923 | if (txqs < 1) { |
7b6cd1ce | 10924 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
10925 | return NULL; |
10926 | } | |
10927 | ||
36909ea4 | 10928 | if (rxqs < 1) { |
7b6cd1ce | 10929 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
10930 | return NULL; |
10931 | } | |
36909ea4 | 10932 | |
fd2ea0a7 | 10933 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
10934 | if (sizeof_priv) { |
10935 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 10936 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
10937 | alloc_size += sizeof_priv; |
10938 | } | |
10939 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 10940 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 10941 | |
c948f51c | 10942 | p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); |
62b5942a | 10943 | if (!p) |
1da177e4 | 10944 | return NULL; |
1da177e4 | 10945 | |
1ce8e7b5 | 10946 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 10947 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 10948 | |
b6d7c0eb | 10949 | ref_tracker_dir_init(&dev->refcnt_tracker, 128, name); |
919067cc | 10950 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d ED |
10951 | dev->pcpu_refcnt = alloc_percpu(int); |
10952 | if (!dev->pcpu_refcnt) | |
74d332c1 | 10953 | goto free_dev; |
4c6c11ea | 10954 | __dev_hold(dev); |
add2d736 ED |
10955 | #else |
10956 | refcount_set(&dev->dev_refcnt, 1); | |
919067cc | 10957 | #endif |
ab9c73cc | 10958 | |
ab9c73cc | 10959 | if (dev_addr_init(dev)) |
29b4433d | 10960 | goto free_pcpu; |
ab9c73cc | 10961 | |
22bedad3 | 10962 | dev_mc_init(dev); |
a748ee24 | 10963 | dev_uc_init(dev); |
ccffad25 | 10964 | |
c346dca1 | 10965 | dev_net_set(dev, &init_net); |
1da177e4 | 10966 | |
7c4e983c | 10967 | dev->gso_max_size = GSO_LEGACY_MAX_SIZE; |
13ce2daa | 10968 | dev->xdp_zc_max_segs = 1; |
30b678d8 | 10969 | dev->gso_max_segs = GSO_MAX_SEGS; |
0fe79f28 | 10970 | dev->gro_max_size = GRO_LEGACY_MAX_SIZE; |
9eefedd5 XL |
10971 | dev->gso_ipv4_max_size = GSO_LEGACY_MAX_SIZE; |
10972 | dev->gro_ipv4_max_size = GRO_LEGACY_MAX_SIZE; | |
14d7b812 JK |
10973 | dev->tso_max_size = TSO_LEGACY_MAX_SIZE; |
10974 | dev->tso_max_segs = TSO_MAX_SEGS; | |
5343da4c TY |
10975 | dev->upper_level = 1; |
10976 | dev->lower_level = 1; | |
1fc70edb TY |
10977 | #ifdef CONFIG_LOCKDEP |
10978 | dev->nested_level = 0; | |
10979 | INIT_LIST_HEAD(&dev->unlink_list); | |
10980 | #endif | |
8d3bdbd5 | 10981 | |
8d3bdbd5 DM |
10982 | INIT_LIST_HEAD(&dev->napi_list); |
10983 | INIT_LIST_HEAD(&dev->unreg_list); | |
5cde2829 | 10984 | INIT_LIST_HEAD(&dev->close_list); |
8d3bdbd5 | 10985 | INIT_LIST_HEAD(&dev->link_watch_list); |
2f268f12 VF |
10986 | INIT_LIST_HEAD(&dev->adj_list.upper); |
10987 | INIT_LIST_HEAD(&dev->adj_list.lower); | |
7866a621 SN |
10988 | INIT_LIST_HEAD(&dev->ptype_all); |
10989 | INIT_LIST_HEAD(&dev->ptype_specific); | |
93642e14 | 10990 | INIT_LIST_HEAD(&dev->net_notifier_list); |
59cc1f61 JK |
10991 | #ifdef CONFIG_NET_SCHED |
10992 | hash_init(dev->qdisc_hash); | |
10993 | #endif | |
6916e461 MC |
10994 | dev->link_topo = phy_link_topo_create(dev); |
10995 | if (IS_ERR(dev->link_topo)) { | |
10996 | dev->link_topo = NULL; | |
10997 | goto free_all; | |
10998 | } | |
10999 | ||
02875878 | 11000 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
8d3bdbd5 DM |
11001 | setup(dev); |
11002 | ||
a813104d | 11003 | if (!dev->tx_queue_len) { |
f84bb1ea | 11004 | dev->priv_flags |= IFF_NO_QUEUE; |
11597084 | 11005 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
a813104d | 11006 | } |
906470c1 | 11007 | |
36909ea4 TH |
11008 | dev->num_tx_queues = txqs; |
11009 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 11010 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 11011 | goto free_all; |
e8a0464c | 11012 | |
36909ea4 TH |
11013 | dev->num_rx_queues = rxqs; |
11014 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 11015 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 11016 | goto free_all; |
0a9627f2 | 11017 | |
1da177e4 | 11018 | strcpy(dev->name, name); |
c835a677 | 11019 | dev->name_assign_type = name_assign_type; |
cbda10fa | 11020 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
11021 | if (!dev->ethtool_ops) |
11022 | dev->ethtool_ops = &default_ethtool_ops; | |
e687ad60 | 11023 | |
17d20784 | 11024 | nf_hook_netdev_init(dev); |
e687ad60 | 11025 | |
1da177e4 | 11026 | return dev; |
ab9c73cc | 11027 | |
8d3bdbd5 DM |
11028 | free_all: |
11029 | free_netdev(dev); | |
11030 | return NULL; | |
11031 | ||
29b4433d | 11032 | free_pcpu: |
919067cc | 11033 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d | 11034 | free_percpu(dev->pcpu_refcnt); |
74d332c1 | 11035 | free_dev: |
919067cc | 11036 | #endif |
74d332c1 | 11037 | netdev_freemem(dev); |
ab9c73cc | 11038 | return NULL; |
1da177e4 | 11039 | } |
36909ea4 | 11040 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
11041 | |
11042 | /** | |
722c9a0c | 11043 | * free_netdev - free network device |
11044 | * @dev: device | |
1da177e4 | 11045 | * |
722c9a0c | 11046 | * This function does the last stage of destroying an allocated device |
11047 | * interface. The reference to the device object is released. If this | |
11048 | * is the last reference then it will be freed.Must be called in process | |
11049 | * context. | |
1da177e4 LT |
11050 | */ |
11051 | void free_netdev(struct net_device *dev) | |
11052 | { | |
d565b0a1 HX |
11053 | struct napi_struct *p, *n; |
11054 | ||
93d05d4a | 11055 | might_sleep(); |
c269a24c JK |
11056 | |
11057 | /* When called immediately after register_netdevice() failed the unwind | |
11058 | * handling may still be dismantling the device. Handle that case by | |
11059 | * deferring the free. | |
11060 | */ | |
11061 | if (dev->reg_state == NETREG_UNREGISTERING) { | |
11062 | ASSERT_RTNL(); | |
11063 | dev->needs_free_netdev = true; | |
11064 | return; | |
11065 | } | |
11066 | ||
60877a32 | 11067 | netif_free_tx_queues(dev); |
e817f856 | 11068 | netif_free_rx_queues(dev); |
e8a0464c | 11069 | |
33d480ce | 11070 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 11071 | |
f001fde5 JP |
11072 | /* Flush device addresses */ |
11073 | dev_addr_flush(dev); | |
11074 | ||
d565b0a1 HX |
11075 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
11076 | netif_napi_del(p); | |
11077 | ||
4d92b95f | 11078 | ref_tracker_dir_exit(&dev->refcnt_tracker); |
919067cc | 11079 | #ifdef CONFIG_PCPU_DEV_REFCNT |
29b4433d ED |
11080 | free_percpu(dev->pcpu_refcnt); |
11081 | dev->pcpu_refcnt = NULL; | |
919067cc | 11082 | #endif |
625788b5 ED |
11083 | free_percpu(dev->core_stats); |
11084 | dev->core_stats = NULL; | |
75ccae62 THJ |
11085 | free_percpu(dev->xdp_bulkq); |
11086 | dev->xdp_bulkq = NULL; | |
29b4433d | 11087 | |
6916e461 MC |
11088 | phy_link_topo_destroy(dev->link_topo); |
11089 | ||
3041a069 | 11090 | /* Compatibility with error handling in drivers */ |
f8d05679 BL |
11091 | if (dev->reg_state == NETREG_UNINITIALIZED || |
11092 | dev->reg_state == NETREG_DUMMY) { | |
74d332c1 | 11093 | netdev_freemem(dev); |
1da177e4 LT |
11094 | return; |
11095 | } | |
11096 | ||
11097 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
4d42b37d | 11098 | WRITE_ONCE(dev->reg_state, NETREG_RELEASED); |
1da177e4 | 11099 | |
43cb76d9 GKH |
11100 | /* will free via device release */ |
11101 | put_device(&dev->dev); | |
1da177e4 | 11102 | } |
d1b19dff | 11103 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 11104 | |
c661050f BL |
11105 | /** |
11106 | * alloc_netdev_dummy - Allocate and initialize a dummy net device. | |
11107 | * @sizeof_priv: size of private data to allocate space for | |
11108 | * | |
11109 | * Return: the allocated net_device on success, NULL otherwise | |
11110 | */ | |
11111 | struct net_device *alloc_netdev_dummy(int sizeof_priv) | |
11112 | { | |
11113 | return alloc_netdev(sizeof_priv, "dummy#", NET_NAME_UNKNOWN, | |
11114 | init_dummy_netdev_core); | |
11115 | } | |
11116 | EXPORT_SYMBOL_GPL(alloc_netdev_dummy); | |
11117 | ||
f0db275a SH |
11118 | /** |
11119 | * synchronize_net - Synchronize with packet receive processing | |
11120 | * | |
11121 | * Wait for packets currently being received to be done. | |
11122 | * Does not block later packets from starting. | |
11123 | */ | |
4ec93edb | 11124 | void synchronize_net(void) |
1da177e4 LT |
11125 | { |
11126 | might_sleep(); | |
be3fc413 ED |
11127 | if (rtnl_is_locked()) |
11128 | synchronize_rcu_expedited(); | |
11129 | else | |
11130 | synchronize_rcu(); | |
1da177e4 | 11131 | } |
d1b19dff | 11132 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
11133 | |
11134 | /** | |
44a0873d | 11135 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 11136 | * @dev: device |
44a0873d | 11137 | * @head: list |
6ebfbc06 | 11138 | * |
1da177e4 | 11139 | * This function shuts down a device interface and removes it |
d59b54b1 | 11140 | * from the kernel tables. |
44a0873d | 11141 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
11142 | * |
11143 | * Callers must hold the rtnl semaphore. You may want | |
11144 | * unregister_netdev() instead of this. | |
11145 | */ | |
11146 | ||
44a0873d | 11147 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 11148 | { |
a6620712 HX |
11149 | ASSERT_RTNL(); |
11150 | ||
44a0873d | 11151 | if (head) { |
9fdce099 | 11152 | list_move_tail(&dev->unreg_list, head); |
44a0873d | 11153 | } else { |
037e56bd JK |
11154 | LIST_HEAD(single); |
11155 | ||
11156 | list_add(&dev->unreg_list, &single); | |
0cbe1e57 | 11157 | unregister_netdevice_many(&single); |
44a0873d | 11158 | } |
1da177e4 | 11159 | } |
44a0873d | 11160 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 11161 | |
77f4aa9a HL |
11162 | void unregister_netdevice_many_notify(struct list_head *head, |
11163 | u32 portid, const struct nlmsghdr *nlh) | |
bcfe2f1a JK |
11164 | { |
11165 | struct net_device *dev, *tmp; | |
11166 | LIST_HEAD(close_head); | |
ffabe98c | 11167 | int cnt = 0; |
bcfe2f1a JK |
11168 | |
11169 | BUG_ON(dev_boot_phase); | |
11170 | ASSERT_RTNL(); | |
11171 | ||
0cbe1e57 JK |
11172 | if (list_empty(head)) |
11173 | return; | |
11174 | ||
bcfe2f1a JK |
11175 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
11176 | /* Some devices call without registering | |
11177 | * for initialization unwind. Remove those | |
11178 | * devices and proceed with the remaining. | |
11179 | */ | |
11180 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
11181 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", | |
11182 | dev->name, dev); | |
11183 | ||
11184 | WARN_ON(1); | |
11185 | list_del(&dev->unreg_list); | |
11186 | continue; | |
11187 | } | |
11188 | dev->dismantle = true; | |
11189 | BUG_ON(dev->reg_state != NETREG_REGISTERED); | |
11190 | } | |
11191 | ||
11192 | /* If device is running, close it first. */ | |
11193 | list_for_each_entry(dev, head, unreg_list) | |
11194 | list_add_tail(&dev->close_list, &close_head); | |
11195 | dev_close_many(&close_head, true); | |
11196 | ||
11197 | list_for_each_entry(dev, head, unreg_list) { | |
11198 | /* And unlink it from device chain. */ | |
e51b9624 | 11199 | unlist_netdevice(dev); |
4d42b37d | 11200 | WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERING); |
bcfe2f1a JK |
11201 | } |
11202 | flush_all_backlogs(); | |
11203 | ||
11204 | synchronize_net(); | |
11205 | ||
11206 | list_for_each_entry(dev, head, unreg_list) { | |
11207 | struct sk_buff *skb = NULL; | |
11208 | ||
11209 | /* Shutdown queueing discipline. */ | |
11210 | dev_shutdown(dev); | |
e420bed0 | 11211 | dev_tcx_uninstall(dev); |
bcfe2f1a | 11212 | dev_xdp_uninstall(dev); |
2b3486bc | 11213 | bpf_dev_bound_netdev_unregister(dev); |
bcfe2f1a | 11214 | |
9309f97a PM |
11215 | netdev_offload_xstats_disable_all(dev); |
11216 | ||
bcfe2f1a JK |
11217 | /* Notify protocols, that we are about to destroy |
11218 | * this device. They should clean all the things. | |
11219 | */ | |
11220 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
11221 | ||
11222 | if (!dev->rtnl_link_ops || | |
11223 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
11224 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, | |
77f4aa9a | 11225 | GFP_KERNEL, NULL, 0, |
59d3efd2 | 11226 | portid, nlh); |
bcfe2f1a JK |
11227 | |
11228 | /* | |
11229 | * Flush the unicast and multicast chains | |
11230 | */ | |
11231 | dev_uc_flush(dev); | |
11232 | dev_mc_flush(dev); | |
11233 | ||
11234 | netdev_name_node_alt_flush(dev); | |
11235 | netdev_name_node_free(dev->name_node); | |
11236 | ||
02a68a47 JP |
11237 | call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev); |
11238 | ||
bcfe2f1a JK |
11239 | if (dev->netdev_ops->ndo_uninit) |
11240 | dev->netdev_ops->ndo_uninit(dev); | |
11241 | ||
11242 | if (skb) | |
77f4aa9a | 11243 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL, portid, nlh); |
bcfe2f1a JK |
11244 | |
11245 | /* Notifier chain MUST detach us all upper devices. */ | |
11246 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
11247 | WARN_ON(netdev_has_any_lower_dev(dev)); | |
11248 | ||
11249 | /* Remove entries from kobject tree */ | |
11250 | netdev_unregister_kobject(dev); | |
11251 | #ifdef CONFIG_XPS | |
11252 | /* Remove XPS queueing entries */ | |
11253 | netif_reset_xps_queues_gt(dev, 0); | |
11254 | #endif | |
11255 | } | |
11256 | ||
11257 | synchronize_net(); | |
11258 | ||
11259 | list_for_each_entry(dev, head, unreg_list) { | |
d62607c3 | 11260 | netdev_put(dev, &dev->dev_registered_tracker); |
bcfe2f1a | 11261 | net_set_todo(dev); |
ffabe98c | 11262 | cnt++; |
bcfe2f1a | 11263 | } |
ffabe98c | 11264 | atomic_add(cnt, &dev_unreg_count); |
0cbe1e57 JK |
11265 | |
11266 | list_del(head); | |
bcfe2f1a | 11267 | } |
77f4aa9a HL |
11268 | |
11269 | /** | |
11270 | * unregister_netdevice_many - unregister many devices | |
11271 | * @head: list of devices | |
11272 | * | |
11273 | * Note: As most callers use a stack allocated list_head, | |
11274 | * we force a list_del() to make sure stack wont be corrupted later. | |
11275 | */ | |
11276 | void unregister_netdevice_many(struct list_head *head) | |
11277 | { | |
11278 | unregister_netdevice_many_notify(head, 0, NULL); | |
11279 | } | |
0cbe1e57 | 11280 | EXPORT_SYMBOL(unregister_netdevice_many); |
bcfe2f1a | 11281 | |
1da177e4 LT |
11282 | /** |
11283 | * unregister_netdev - remove device from the kernel | |
11284 | * @dev: device | |
11285 | * | |
11286 | * This function shuts down a device interface and removes it | |
d59b54b1 | 11287 | * from the kernel tables. |
1da177e4 LT |
11288 | * |
11289 | * This is just a wrapper for unregister_netdevice that takes | |
11290 | * the rtnl semaphore. In general you want to use this and not | |
11291 | * unregister_netdevice. | |
11292 | */ | |
11293 | void unregister_netdev(struct net_device *dev) | |
11294 | { | |
11295 | rtnl_lock(); | |
11296 | unregister_netdevice(dev); | |
11297 | rtnl_unlock(); | |
11298 | } | |
1da177e4 LT |
11299 | EXPORT_SYMBOL(unregister_netdev); |
11300 | ||
ce286d32 | 11301 | /** |
0854fa82 | 11302 | * __dev_change_net_namespace - move device to different nethost namespace |
ce286d32 EB |
11303 | * @dev: device |
11304 | * @net: network namespace | |
11305 | * @pat: If not NULL name pattern to try if the current device name | |
11306 | * is already taken in the destination network namespace. | |
eeb85a14 AV |
11307 | * @new_ifindex: If not zero, specifies device index in the target |
11308 | * namespace. | |
ce286d32 EB |
11309 | * |
11310 | * This function shuts down a device interface and moves it | |
11311 | * to a new network namespace. On success 0 is returned, on | |
11312 | * a failure a netagive errno code is returned. | |
11313 | * | |
11314 | * Callers must hold the rtnl semaphore. | |
11315 | */ | |
11316 | ||
0854fa82 AV |
11317 | int __dev_change_net_namespace(struct net_device *dev, struct net *net, |
11318 | const char *pat, int new_ifindex) | |
ce286d32 | 11319 | { |
7663d522 | 11320 | struct netdev_name_node *name_node; |
ef6a4c88 | 11321 | struct net *net_old = dev_net(dev); |
311cca40 | 11322 | char new_name[IFNAMSIZ] = {}; |
eeb85a14 | 11323 | int err, new_nsid; |
ce286d32 EB |
11324 | |
11325 | ASSERT_RTNL(); | |
11326 | ||
11327 | /* Don't allow namespace local devices to be moved. */ | |
11328 | err = -EINVAL; | |
11329 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
11330 | goto out; | |
11331 | ||
11332 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
11333 | if (dev->reg_state != NETREG_REGISTERED) |
11334 | goto out; | |
11335 | ||
11336 | /* Get out if there is nothing todo */ | |
11337 | err = 0; | |
ef6a4c88 | 11338 | if (net_eq(net_old, net)) |
ce286d32 EB |
11339 | goto out; |
11340 | ||
11341 | /* Pick the destination device name, and ensure | |
11342 | * we can use it in the destination network namespace. | |
11343 | */ | |
11344 | err = -EEXIST; | |
75ea27d0 | 11345 | if (netdev_name_in_use(net, dev->name)) { |
ce286d32 EB |
11346 | /* We get here if we can't use the current device name */ |
11347 | if (!pat) | |
11348 | goto out; | |
556c755a | 11349 | err = dev_prep_valid_name(net, dev, pat, new_name, EEXIST); |
7892bd08 | 11350 | if (err < 0) |
ce286d32 EB |
11351 | goto out; |
11352 | } | |
7663d522 JK |
11353 | /* Check that none of the altnames conflicts. */ |
11354 | err = -EEXIST; | |
11355 | netdev_for_each_altname(dev, name_node) | |
11356 | if (netdev_name_in_use(net, name_node->name)) | |
11357 | goto out; | |
ce286d32 | 11358 | |
eeb85a14 | 11359 | /* Check that new_ifindex isn't used yet. */ |
759ab1ed JK |
11360 | if (new_ifindex) { |
11361 | err = dev_index_reserve(net, new_ifindex); | |
11362 | if (err < 0) | |
11363 | goto out; | |
11364 | } else { | |
11365 | /* If there is an ifindex conflict assign a new one */ | |
11366 | err = dev_index_reserve(net, dev->ifindex); | |
11367 | if (err == -EBUSY) | |
11368 | err = dev_index_reserve(net, 0); | |
11369 | if (err < 0) | |
11370 | goto out; | |
11371 | new_ifindex = err; | |
11372 | } | |
eeb85a14 | 11373 | |
ce286d32 EB |
11374 | /* |
11375 | * And now a mini version of register_netdevice unregister_netdevice. | |
11376 | */ | |
11377 | ||
11378 | /* If device is running close it first. */ | |
9b772652 | 11379 | dev_close(dev); |
ce286d32 EB |
11380 | |
11381 | /* And unlink it from device chain */ | |
e51b9624 | 11382 | unlist_netdevice(dev); |
ce286d32 EB |
11383 | |
11384 | synchronize_net(); | |
11385 | ||
11386 | /* Shutdown queueing discipline. */ | |
11387 | dev_shutdown(dev); | |
11388 | ||
11389 | /* Notify protocols, that we are about to destroy | |
eb13da1a | 11390 | * this device. They should clean all the things. |
11391 | * | |
11392 | * Note that dev->reg_state stays at NETREG_REGISTERED. | |
11393 | * This is wanted because this way 8021q and macvlan know | |
11394 | * the device is just moving and can keep their slaves up. | |
11395 | */ | |
ce286d32 | 11396 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
6549dd43 | 11397 | rcu_barrier(); |
38e01b30 | 11398 | |
d4e4fdf9 | 11399 | new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); |
38e01b30 ND |
11400 | |
11401 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, | |
11402 | new_ifindex); | |
ce286d32 EB |
11403 | |
11404 | /* | |
11405 | * Flush the unicast and multicast chains | |
11406 | */ | |
a748ee24 | 11407 | dev_uc_flush(dev); |
22bedad3 | 11408 | dev_mc_flush(dev); |
ce286d32 | 11409 | |
4e66ae2e SH |
11410 | /* Send a netdev-removed uevent to the old namespace */ |
11411 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
4c75431a | 11412 | netdev_adjacent_del_links(dev); |
4e66ae2e | 11413 | |
93642e14 JP |
11414 | /* Move per-net netdevice notifiers that are following the netdevice */ |
11415 | move_netdevice_notifiers_dev_net(dev, net); | |
11416 | ||
ce286d32 | 11417 | /* Actually switch the network namespace */ |
c346dca1 | 11418 | dev_net_set(dev, net); |
38e01b30 | 11419 | dev->ifindex = new_ifindex; |
ce286d32 | 11420 | |
0840556e KI |
11421 | if (new_name[0]) { |
11422 | /* Rename the netdev to prepared name */ | |
11423 | write_seqlock(&netdev_rename_lock); | |
311cca40 | 11424 | strscpy(dev->name, new_name, IFNAMSIZ); |
0840556e KI |
11425 | write_sequnlock(&netdev_rename_lock); |
11426 | } | |
311cca40 | 11427 | |
8b41d188 | 11428 | /* Fixup kobjects */ |
dd891b5b | 11429 | dev_set_uevent_suppress(&dev->dev, 1); |
a1b3f594 | 11430 | err = device_rename(&dev->dev, dev->name); |
dd891b5b | 11431 | dev_set_uevent_suppress(&dev->dev, 0); |
8b41d188 | 11432 | WARN_ON(err); |
ce286d32 | 11433 | |
dd891b5b JK |
11434 | /* Send a netdev-add uevent to the new namespace */ |
11435 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
11436 | netdev_adjacent_add_links(dev); | |
11437 | ||
ef6a4c88 CB |
11438 | /* Adapt owner in case owning user namespace of target network |
11439 | * namespace is different from the original one. | |
11440 | */ | |
11441 | err = netdev_change_owner(dev, net_old, net); | |
11442 | WARN_ON(err); | |
11443 | ||
ce286d32 EB |
11444 | /* Add the device back in the hashes */ |
11445 | list_netdevice(dev); | |
11446 | ||
11447 | /* Notify protocols, that a new device appeared. */ | |
11448 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
11449 | ||
d90a909e EB |
11450 | /* |
11451 | * Prevent userspace races by waiting until the network | |
11452 | * device is fully setup before sending notifications. | |
11453 | */ | |
1d997f10 | 11454 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL); |
d90a909e | 11455 | |
ce286d32 EB |
11456 | synchronize_net(); |
11457 | err = 0; | |
11458 | out: | |
11459 | return err; | |
11460 | } | |
0854fa82 | 11461 | EXPORT_SYMBOL_GPL(__dev_change_net_namespace); |
ce286d32 | 11462 | |
f0bf90de | 11463 | static int dev_cpu_dead(unsigned int oldcpu) |
1da177e4 LT |
11464 | { |
11465 | struct sk_buff **list_skb; | |
1da177e4 | 11466 | struct sk_buff *skb; |
f0bf90de | 11467 | unsigned int cpu; |
97d8b6e3 | 11468 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
1da177e4 | 11469 | |
1da177e4 LT |
11470 | local_irq_disable(); |
11471 | cpu = smp_processor_id(); | |
11472 | sd = &per_cpu(softnet_data, cpu); | |
11473 | oldsd = &per_cpu(softnet_data, oldcpu); | |
11474 | ||
11475 | /* Find end of our completion_queue. */ | |
11476 | list_skb = &sd->completion_queue; | |
11477 | while (*list_skb) | |
11478 | list_skb = &(*list_skb)->next; | |
11479 | /* Append completion queue from offline CPU. */ | |
11480 | *list_skb = oldsd->completion_queue; | |
11481 | oldsd->completion_queue = NULL; | |
11482 | ||
1da177e4 | 11483 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
11484 | if (oldsd->output_queue) { |
11485 | *sd->output_queue_tailp = oldsd->output_queue; | |
11486 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
11487 | oldsd->output_queue = NULL; | |
11488 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
11489 | } | |
ac64da0b ED |
11490 | /* Append NAPI poll list from offline CPU, with one exception : |
11491 | * process_backlog() must be called by cpu owning percpu backlog. | |
11492 | * We properly handle process_queue & input_pkt_queue later. | |
11493 | */ | |
11494 | while (!list_empty(&oldsd->poll_list)) { | |
11495 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, | |
11496 | struct napi_struct, | |
11497 | poll_list); | |
11498 | ||
11499 | list_del_init(&napi->poll_list); | |
11500 | if (napi->poll == process_backlog) | |
dad6b977 | 11501 | napi->state &= NAPIF_STATE_THREADED; |
ac64da0b ED |
11502 | else |
11503 | ____napi_schedule(sd, napi); | |
264524d5 | 11504 | } |
1da177e4 LT |
11505 | |
11506 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
11507 | local_irq_enable(); | |
11508 | ||
dad6b977 | 11509 | if (!use_backlog_threads()) { |
773fc8f6 | 11510 | #ifdef CONFIG_RPS |
dad6b977 SAS |
11511 | remsd = oldsd->rps_ipi_list; |
11512 | oldsd->rps_ipi_list = NULL; | |
773fc8f6 | 11513 | #endif |
dad6b977 SAS |
11514 | /* send out pending IPI's on offline CPU */ |
11515 | net_rps_send_ipi(remsd); | |
11516 | } | |
773fc8f6 | 11517 | |
1da177e4 | 11518 | /* Process offline CPU's input_pkt_queue */ |
76cc8b13 | 11519 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
ad0a043f | 11520 | netif_rx(skb); |
36b83ffc | 11521 | rps_input_queue_head_incr(oldsd); |
fec5e652 | 11522 | } |
ac64da0b | 11523 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
ad0a043f | 11524 | netif_rx(skb); |
36b83ffc | 11525 | rps_input_queue_head_incr(oldsd); |
76cc8b13 | 11526 | } |
1da177e4 | 11527 | |
f0bf90de | 11528 | return 0; |
1da177e4 | 11529 | } |
1da177e4 | 11530 | |
7f353bf2 | 11531 | /** |
b63365a2 HX |
11532 | * netdev_increment_features - increment feature set by one |
11533 | * @all: current feature set | |
11534 | * @one: new feature set | |
11535 | * @mask: mask feature set | |
7f353bf2 HX |
11536 | * |
11537 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
11538 | * @one to the master device with current feature set @all. Will not |
11539 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 11540 | */ |
c8f44aff MM |
11541 | netdev_features_t netdev_increment_features(netdev_features_t all, |
11542 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 11543 | { |
c8cd0989 | 11544 | if (mask & NETIF_F_HW_CSUM) |
a188222b | 11545 | mask |= NETIF_F_CSUM_MASK; |
1742f183 | 11546 | mask |= NETIF_F_VLAN_CHALLENGED; |
7f353bf2 | 11547 | |
a188222b | 11548 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
1742f183 | 11549 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
c6e1a0d1 | 11550 | |
1742f183 | 11551 | /* If one device supports hw checksumming, set for all. */ |
c8cd0989 TH |
11552 | if (all & NETIF_F_HW_CSUM) |
11553 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); | |
7f353bf2 HX |
11554 | |
11555 | return all; | |
11556 | } | |
b63365a2 | 11557 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 11558 | |
430f03cd | 11559 | static struct hlist_head * __net_init netdev_create_hash(void) |
30d97d35 PE |
11560 | { |
11561 | int i; | |
11562 | struct hlist_head *hash; | |
11563 | ||
6da2ec56 | 11564 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
30d97d35 PE |
11565 | if (hash != NULL) |
11566 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
11567 | INIT_HLIST_HEAD(&hash[i]); | |
11568 | ||
11569 | return hash; | |
11570 | } | |
11571 | ||
881d966b | 11572 | /* Initialize per network namespace state */ |
4665079c | 11573 | static int __net_init netdev_init(struct net *net) |
881d966b | 11574 | { |
d9f37d01 | 11575 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
c593642c | 11576 | 8 * sizeof_field(struct napi_struct, gro_bitmask)); |
d9f37d01 | 11577 | |
9c1be193 | 11578 | INIT_LIST_HEAD(&net->dev_base_head); |
881d966b | 11579 | |
30d97d35 PE |
11580 | net->dev_name_head = netdev_create_hash(); |
11581 | if (net->dev_name_head == NULL) | |
11582 | goto err_name; | |
881d966b | 11583 | |
30d97d35 PE |
11584 | net->dev_index_head = netdev_create_hash(); |
11585 | if (net->dev_index_head == NULL) | |
11586 | goto err_idx; | |
881d966b | 11587 | |
ceaac91d | 11588 | xa_init_flags(&net->dev_by_index, XA_FLAGS_ALLOC1); |
759ab1ed | 11589 | |
a30c7b42 JP |
11590 | RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain); |
11591 | ||
881d966b | 11592 | return 0; |
30d97d35 PE |
11593 | |
11594 | err_idx: | |
11595 | kfree(net->dev_name_head); | |
11596 | err_name: | |
11597 | return -ENOMEM; | |
881d966b EB |
11598 | } |
11599 | ||
f0db275a SH |
11600 | /** |
11601 | * netdev_drivername - network driver for the device | |
11602 | * @dev: network device | |
f0db275a SH |
11603 | * |
11604 | * Determine network driver for device. | |
11605 | */ | |
3019de12 | 11606 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 11607 | { |
cf04a4c7 SH |
11608 | const struct device_driver *driver; |
11609 | const struct device *parent; | |
3019de12 | 11610 | const char *empty = ""; |
6579e57b AV |
11611 | |
11612 | parent = dev->dev.parent; | |
6579e57b | 11613 | if (!parent) |
3019de12 | 11614 | return empty; |
6579e57b AV |
11615 | |
11616 | driver = parent->driver; | |
11617 | if (driver && driver->name) | |
3019de12 DM |
11618 | return driver->name; |
11619 | return empty; | |
6579e57b AV |
11620 | } |
11621 | ||
6ea754eb JP |
11622 | static void __netdev_printk(const char *level, const struct net_device *dev, |
11623 | struct va_format *vaf) | |
256df2f3 | 11624 | { |
b004ff49 | 11625 | if (dev && dev->dev.parent) { |
6ea754eb JP |
11626 | dev_printk_emit(level[1] - '0', |
11627 | dev->dev.parent, | |
11628 | "%s %s %s%s: %pV", | |
11629 | dev_driver_string(dev->dev.parent), | |
11630 | dev_name(dev->dev.parent), | |
11631 | netdev_name(dev), netdev_reg_state(dev), | |
11632 | vaf); | |
b004ff49 | 11633 | } else if (dev) { |
6ea754eb JP |
11634 | printk("%s%s%s: %pV", |
11635 | level, netdev_name(dev), netdev_reg_state(dev), vaf); | |
b004ff49 | 11636 | } else { |
6ea754eb | 11637 | printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 11638 | } |
256df2f3 JP |
11639 | } |
11640 | ||
6ea754eb JP |
11641 | void netdev_printk(const char *level, const struct net_device *dev, |
11642 | const char *format, ...) | |
256df2f3 JP |
11643 | { |
11644 | struct va_format vaf; | |
11645 | va_list args; | |
256df2f3 JP |
11646 | |
11647 | va_start(args, format); | |
11648 | ||
11649 | vaf.fmt = format; | |
11650 | vaf.va = &args; | |
11651 | ||
6ea754eb | 11652 | __netdev_printk(level, dev, &vaf); |
b004ff49 | 11653 | |
256df2f3 | 11654 | va_end(args); |
256df2f3 JP |
11655 | } |
11656 | EXPORT_SYMBOL(netdev_printk); | |
11657 | ||
11658 | #define define_netdev_printk_level(func, level) \ | |
6ea754eb | 11659 | void func(const struct net_device *dev, const char *fmt, ...) \ |
256df2f3 | 11660 | { \ |
256df2f3 JP |
11661 | struct va_format vaf; \ |
11662 | va_list args; \ | |
11663 | \ | |
11664 | va_start(args, fmt); \ | |
11665 | \ | |
11666 | vaf.fmt = fmt; \ | |
11667 | vaf.va = &args; \ | |
11668 | \ | |
6ea754eb | 11669 | __netdev_printk(level, dev, &vaf); \ |
b004ff49 | 11670 | \ |
256df2f3 | 11671 | va_end(args); \ |
256df2f3 JP |
11672 | } \ |
11673 | EXPORT_SYMBOL(func); | |
11674 | ||
11675 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
11676 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
11677 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
11678 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
11679 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
11680 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
11681 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
11682 | ||
4665079c | 11683 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
11684 | { |
11685 | kfree(net->dev_name_head); | |
11686 | kfree(net->dev_index_head); | |
759ab1ed | 11687 | xa_destroy(&net->dev_by_index); |
ee21b18b VA |
11688 | if (net != &init_net) |
11689 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); | |
881d966b EB |
11690 | } |
11691 | ||
022cbae6 | 11692 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
11693 | .init = netdev_init, |
11694 | .exit = netdev_exit, | |
11695 | }; | |
11696 | ||
ee403248 | 11697 | static void __net_exit default_device_exit_net(struct net *net) |
ce286d32 | 11698 | { |
d09486a0 | 11699 | struct netdev_name_node *name_node, *tmp; |
e008b5fc | 11700 | struct net_device *dev, *aux; |
ce286d32 | 11701 | /* |
e008b5fc | 11702 | * Push all migratable network devices back to the |
ce286d32 EB |
11703 | * initial network namespace |
11704 | */ | |
ee403248 | 11705 | ASSERT_RTNL(); |
e008b5fc | 11706 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 11707 | int err; |
aca51397 | 11708 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
11709 | |
11710 | /* Ignore unmoveable devices (i.e. loopback) */ | |
11711 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
11712 | continue; | |
11713 | ||
e008b5fc | 11714 | /* Leave virtual devices for the generic cleanup */ |
3a5ca857 | 11715 | if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund) |
e008b5fc | 11716 | continue; |
d0c082ce | 11717 | |
25985edc | 11718 | /* Push remaining network devices to init_net */ |
aca51397 | 11719 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
75ea27d0 | 11720 | if (netdev_name_in_use(&init_net, fb_name)) |
55b40dbf | 11721 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); |
d09486a0 JK |
11722 | |
11723 | netdev_for_each_altname_safe(dev, name_node, tmp) | |
723de3eb | 11724 | if (netdev_name_in_use(&init_net, name_node->name)) |
d09486a0 | 11725 | __netdev_name_node_alt_destroy(name_node); |
d09486a0 | 11726 | |
0854fa82 | 11727 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
ce286d32 | 11728 | if (err) { |
7b6cd1ce JP |
11729 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
11730 | __func__, dev->name, err); | |
aca51397 | 11731 | BUG(); |
ce286d32 EB |
11732 | } |
11733 | } | |
ce286d32 EB |
11734 | } |
11735 | ||
04dc7f6b EB |
11736 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
11737 | { | |
11738 | /* At exit all network devices most be removed from a network | |
b595076a | 11739 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
11740 | * Do this across as many network namespaces as possible to |
11741 | * improve batching efficiency. | |
11742 | */ | |
11743 | struct net_device *dev; | |
11744 | struct net *net; | |
11745 | LIST_HEAD(dev_kill_list); | |
11746 | ||
ee403248 ED |
11747 | rtnl_lock(); |
11748 | list_for_each_entry(net, net_list, exit_list) { | |
11749 | default_device_exit_net(net); | |
11750 | cond_resched(); | |
11751 | } | |
ee403248 | 11752 | |
04dc7f6b EB |
11753 | list_for_each_entry(net, net_list, exit_list) { |
11754 | for_each_netdev_reverse(net, dev) { | |
b0ab2fab | 11755 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
04dc7f6b EB |
11756 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
11757 | else | |
11758 | unregister_netdevice_queue(dev, &dev_kill_list); | |
11759 | } | |
11760 | } | |
11761 | unregister_netdevice_many(&dev_kill_list); | |
11762 | rtnl_unlock(); | |
11763 | } | |
11764 | ||
022cbae6 | 11765 | static struct pernet_operations __net_initdata default_device_ops = { |
04dc7f6b | 11766 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
11767 | }; |
11768 | ||
43a71cd6 CL |
11769 | static void __init net_dev_struct_check(void) |
11770 | { | |
11771 | /* TX read-mostly hotpath */ | |
11772 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, priv_flags); | |
11773 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, netdev_ops); | |
11774 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, header_ops); | |
11775 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, _tx); | |
11776 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, real_num_tx_queues); | |
11777 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_size); | |
11778 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_ipv4_max_size); | |
11779 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_segs); | |
993498e5 | 11780 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_partial_features); |
43a71cd6 CL |
11781 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, num_tc); |
11782 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, mtu); | |
11783 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, needed_headroom); | |
11784 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tc_to_txq); | |
11785 | #ifdef CONFIG_XPS | |
11786 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, xps_maps); | |
11787 | #endif | |
11788 | #ifdef CONFIG_NETFILTER_EGRESS | |
11789 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, nf_hooks_egress); | |
11790 | #endif | |
11791 | #ifdef CONFIG_NET_XGRESS | |
11792 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tcx_egress); | |
11793 | #endif | |
993498e5 | 11794 | CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_tx, 160); |
43a71cd6 CL |
11795 | |
11796 | /* TXRX read-mostly hotpath */ | |
c353c7b7 | 11797 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, lstats); |
f6e0a498 | 11798 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, state); |
43a71cd6 CL |
11799 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, flags); |
11800 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, hard_header_len); | |
11801 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, features); | |
11802 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, ip6_ptr); | |
f6e0a498 | 11803 | CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_txrx, 46); |
43a71cd6 CL |
11804 | |
11805 | /* RX read-mostly hotpath */ | |
11806 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ptype_specific); | |
11807 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ifindex); | |
11808 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, real_num_rx_queues); | |
11809 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, _rx); | |
11810 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_flush_timeout); | |
11811 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, napi_defer_hard_irqs); | |
11812 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_max_size); | |
11813 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_ipv4_max_size); | |
11814 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler); | |
11815 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler_data); | |
11816 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, nd_net); | |
11817 | #ifdef CONFIG_NETPOLL | |
11818 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, npinfo); | |
11819 | #endif | |
11820 | #ifdef CONFIG_NET_XGRESS | |
11821 | CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, tcx_ingress); | |
11822 | #endif | |
d3d344a1 | 11823 | CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_rx, 104); |
43a71cd6 CL |
11824 | } |
11825 | ||
1da177e4 LT |
11826 | /* |
11827 | * Initialize the DEV module. At boot time this walks the device list and | |
11828 | * unhooks any devices that fail to initialise (normally hardware not | |
11829 | * present) and leaves us with a valid list of present and active devices. | |
11830 | * | |
11831 | */ | |
11832 | ||
2b0cfa6e LB |
11833 | /* We allocate 256 pages for each CPU if PAGE_SHIFT is 12 */ |
11834 | #define SYSTEM_PERCPU_PAGE_POOL_SIZE ((1 << 20) / PAGE_SIZE) | |
11835 | ||
11836 | static int net_page_pool_create(int cpuid) | |
11837 | { | |
11838 | #if IS_ENABLED(CONFIG_PAGE_POOL) | |
11839 | struct page_pool_params page_pool_params = { | |
11840 | .pool_size = SYSTEM_PERCPU_PAGE_POOL_SIZE, | |
f853fa5c | 11841 | .flags = PP_FLAG_SYSTEM_POOL, |
341ee1a5 | 11842 | .nid = cpu_to_mem(cpuid), |
2b0cfa6e LB |
11843 | }; |
11844 | struct page_pool *pp_ptr; | |
11845 | ||
11846 | pp_ptr = page_pool_create_percpu(&page_pool_params, cpuid); | |
11847 | if (IS_ERR(pp_ptr)) | |
11848 | return -ENOMEM; | |
11849 | ||
11850 | per_cpu(system_page_pool, cpuid) = pp_ptr; | |
11851 | #endif | |
11852 | return 0; | |
11853 | } | |
11854 | ||
dad6b977 SAS |
11855 | static int backlog_napi_should_run(unsigned int cpu) |
11856 | { | |
11857 | struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu); | |
11858 | struct napi_struct *napi = &sd->backlog; | |
11859 | ||
11860 | return test_bit(NAPI_STATE_SCHED_THREADED, &napi->state); | |
11861 | } | |
11862 | ||
11863 | static void run_backlog_napi(unsigned int cpu) | |
11864 | { | |
11865 | struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu); | |
11866 | ||
11867 | napi_threaded_poll_loop(&sd->backlog); | |
11868 | } | |
11869 | ||
11870 | static void backlog_napi_setup(unsigned int cpu) | |
11871 | { | |
11872 | struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu); | |
11873 | struct napi_struct *napi = &sd->backlog; | |
11874 | ||
11875 | napi->thread = this_cpu_read(backlog_napi); | |
11876 | set_bit(NAPI_STATE_THREADED, &napi->state); | |
11877 | } | |
11878 | ||
11879 | static struct smp_hotplug_thread backlog_threads = { | |
11880 | .store = &backlog_napi, | |
11881 | .thread_should_run = backlog_napi_should_run, | |
11882 | .thread_fn = run_backlog_napi, | |
11883 | .thread_comm = "backlog_napi/%u", | |
11884 | .setup = backlog_napi_setup, | |
11885 | }; | |
11886 | ||
1da177e4 LT |
11887 | /* |
11888 | * This is called single threaded during boot, so no need | |
11889 | * to take the rtnl semaphore. | |
11890 | */ | |
11891 | static int __init net_dev_init(void) | |
11892 | { | |
11893 | int i, rc = -ENOMEM; | |
11894 | ||
11895 | BUG_ON(!dev_boot_phase); | |
11896 | ||
43a71cd6 CL |
11897 | net_dev_struct_check(); |
11898 | ||
1da177e4 LT |
11899 | if (dev_proc_init()) |
11900 | goto out; | |
11901 | ||
8b41d188 | 11902 | if (netdev_kobject_init()) |
1da177e4 LT |
11903 | goto out; |
11904 | ||
82d8a867 | 11905 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
11906 | INIT_LIST_HEAD(&ptype_base[i]); |
11907 | ||
881d966b EB |
11908 | if (register_pernet_subsys(&netdev_net_ops)) |
11909 | goto out; | |
1da177e4 LT |
11910 | |
11911 | /* | |
11912 | * Initialise the packet receive queues. | |
11913 | */ | |
11914 | ||
6f912042 | 11915 | for_each_possible_cpu(i) { |
41852497 | 11916 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
e36fa2f7 | 11917 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 11918 | |
41852497 ED |
11919 | INIT_WORK(flush, flush_backlog); |
11920 | ||
e36fa2f7 | 11921 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 11922 | skb_queue_head_init(&sd->process_queue); |
f53c7239 SK |
11923 | #ifdef CONFIG_XFRM_OFFLOAD |
11924 | skb_queue_head_init(&sd->xfrm_backlog); | |
11925 | #endif | |
e36fa2f7 | 11926 | INIT_LIST_HEAD(&sd->poll_list); |
a9cbd588 | 11927 | sd->output_queue_tailp = &sd->output_queue; |
df334545 | 11928 | #ifdef CONFIG_RPS |
545b8c8d | 11929 | INIT_CSD(&sd->csd, rps_trigger_softirq, sd); |
e36fa2f7 | 11930 | sd->cpu = i; |
1e94d72f | 11931 | #endif |
97e719a8 | 11932 | INIT_CSD(&sd->defer_csd, trigger_rx_softirq, sd); |
68822bdf | 11933 | spin_lock_init(&sd->defer_lock); |
0a9627f2 | 11934 | |
7c4ec749 | 11935 | init_gro_hash(&sd->backlog); |
e36fa2f7 ED |
11936 | sd->backlog.poll = process_backlog; |
11937 | sd->backlog.weight = weight_p; | |
dad6b977 | 11938 | INIT_LIST_HEAD(&sd->backlog.poll_list); |
2b0cfa6e LB |
11939 | |
11940 | if (net_page_pool_create(i)) | |
11941 | goto out; | |
1da177e4 | 11942 | } |
dad6b977 SAS |
11943 | if (use_backlog_threads()) |
11944 | smpboot_register_percpu_thread(&backlog_threads); | |
1da177e4 | 11945 | |
1da177e4 LT |
11946 | dev_boot_phase = 0; |
11947 | ||
505d4f73 EB |
11948 | /* The loopback device is special if any other network devices |
11949 | * is present in a network namespace the loopback device must | |
11950 | * be present. Since we now dynamically allocate and free the | |
11951 | * loopback device ensure this invariant is maintained by | |
11952 | * keeping the loopback device as the first device on the | |
11953 | * list of network devices. Ensuring the loopback devices | |
11954 | * is the first device that appears and the last network device | |
11955 | * that disappears. | |
11956 | */ | |
11957 | if (register_pernet_device(&loopback_net_ops)) | |
11958 | goto out; | |
11959 | ||
11960 | if (register_pernet_device(&default_device_ops)) | |
11961 | goto out; | |
11962 | ||
962cf36c CM |
11963 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
11964 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 | 11965 | |
f0bf90de SAS |
11966 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
11967 | NULL, dev_cpu_dead); | |
11968 | WARN_ON(rc < 0); | |
1da177e4 | 11969 | rc = 0; |
2f3c7195 MT |
11970 | |
11971 | /* avoid static key IPIs to isolated CPUs */ | |
11972 | if (housekeeping_enabled(HK_TYPE_MISC)) | |
11973 | net_enable_timestamp(); | |
1da177e4 | 11974 | out: |
2b0cfa6e LB |
11975 | if (rc < 0) { |
11976 | for_each_possible_cpu(i) { | |
11977 | struct page_pool *pp_ptr; | |
11978 | ||
11979 | pp_ptr = per_cpu(system_page_pool, i); | |
11980 | if (!pp_ptr) | |
11981 | continue; | |
11982 | ||
11983 | page_pool_destroy(pp_ptr); | |
11984 | per_cpu(system_page_pool, i) = NULL; | |
11985 | } | |
11986 | } | |
11987 | ||
1da177e4 LT |
11988 | return rc; |
11989 | } | |
11990 | ||
11991 | subsys_initcall(net_dev_init); |