Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
722c9a0c | 3 | * NET3 Protocol independent device support routines. |
1da177e4 | 4 | * |
1da177e4 | 5 | * Derived from the non IP parts of dev.c 1.0.19 |
722c9a0c | 6 | * Authors: Ross Biro |
1da177e4 LT |
7 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
8 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
9 | * | |
10 | * Additional Authors: | |
11 | * Florian la Roche <rzsfl@rz.uni-sb.de> | |
12 | * Alan Cox <gw4pts@gw4pts.ampr.org> | |
13 | * David Hinds <dahinds@users.sourceforge.net> | |
14 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
15 | * Adam Sulmicki <adam@cfar.umd.edu> | |
16 | * Pekka Riikonen <priikone@poesidon.pspt.fi> | |
17 | * | |
18 | * Changes: | |
19 | * D.J. Barrow : Fixed bug where dev->refcnt gets set | |
722c9a0c | 20 | * to 2 if register_netdev gets called |
21 | * before net_dev_init & also removed a | |
22 | * few lines of code in the process. | |
1da177e4 LT |
23 | * Alan Cox : device private ioctl copies fields back. |
24 | * Alan Cox : Transmit queue code does relevant | |
25 | * stunts to keep the queue safe. | |
26 | * Alan Cox : Fixed double lock. | |
27 | * Alan Cox : Fixed promisc NULL pointer trap | |
28 | * ???????? : Support the full private ioctl range | |
29 | * Alan Cox : Moved ioctl permission check into | |
30 | * drivers | |
31 | * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI | |
32 | * Alan Cox : 100 backlog just doesn't cut it when | |
33 | * you start doing multicast video 8) | |
34 | * Alan Cox : Rewrote net_bh and list manager. | |
722c9a0c | 35 | * Alan Cox : Fix ETH_P_ALL echoback lengths. |
1da177e4 LT |
36 | * Alan Cox : Took out transmit every packet pass |
37 | * Saved a few bytes in the ioctl handler | |
38 | * Alan Cox : Network driver sets packet type before | |
39 | * calling netif_rx. Saves a function | |
40 | * call a packet. | |
41 | * Alan Cox : Hashed net_bh() | |
42 | * Richard Kooijman: Timestamp fixes. | |
43 | * Alan Cox : Wrong field in SIOCGIFDSTADDR | |
44 | * Alan Cox : Device lock protection. | |
722c9a0c | 45 | * Alan Cox : Fixed nasty side effect of device close |
1da177e4 LT |
46 | * changes. |
47 | * Rudi Cilibrasi : Pass the right thing to | |
48 | * set_mac_address() | |
49 | * Dave Miller : 32bit quantity for the device lock to | |
50 | * make it work out on a Sparc. | |
51 | * Bjorn Ekwall : Added KERNELD hack. | |
52 | * Alan Cox : Cleaned up the backlog initialise. | |
53 | * Craig Metz : SIOCGIFCONF fix if space for under | |
54 | * 1 device. | |
55 | * Thomas Bogendoerfer : Return ENODEV for dev_open, if there | |
56 | * is no device open function. | |
57 | * Andi Kleen : Fix error reporting for SIOCGIFCONF | |
58 | * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF | |
59 | * Cyrus Durgin : Cleaned for KMOD | |
60 | * Adam Sulmicki : Bug Fix : Network Device Unload | |
61 | * A network device unload needs to purge | |
62 | * the backlog queue. | |
63 | * Paul Rusty Russell : SIOCSIFNAME | |
64 | * Pekka Riikonen : Netdev boot-time settings code | |
65 | * Andrew Morton : Make unregister_netdevice wait | |
722c9a0c | 66 | * indefinitely on dev->refcnt |
67 | * J Hadi Salim : - Backlog queue sampling | |
1da177e4 LT |
68 | * - netif_rx() feedback |
69 | */ | |
70 | ||
7c0f6ba6 | 71 | #include <linux/uaccess.h> |
1da177e4 | 72 | #include <linux/bitops.h> |
4fc268d2 | 73 | #include <linux/capability.h> |
1da177e4 LT |
74 | #include <linux/cpu.h> |
75 | #include <linux/types.h> | |
76 | #include <linux/kernel.h> | |
08e9897d | 77 | #include <linux/hash.h> |
5a0e3ad6 | 78 | #include <linux/slab.h> |
1da177e4 | 79 | #include <linux/sched.h> |
f1083048 | 80 | #include <linux/sched/mm.h> |
4a3e2f71 | 81 | #include <linux/mutex.h> |
11d6011c | 82 | #include <linux/rwsem.h> |
1da177e4 LT |
83 | #include <linux/string.h> |
84 | #include <linux/mm.h> | |
85 | #include <linux/socket.h> | |
86 | #include <linux/sockios.h> | |
87 | #include <linux/errno.h> | |
88 | #include <linux/interrupt.h> | |
89 | #include <linux/if_ether.h> | |
90 | #include <linux/netdevice.h> | |
91 | #include <linux/etherdevice.h> | |
0187bdfb | 92 | #include <linux/ethtool.h> |
1da177e4 | 93 | #include <linux/skbuff.h> |
a7862b45 | 94 | #include <linux/bpf.h> |
b5cdae32 | 95 | #include <linux/bpf_trace.h> |
457c4cbc | 96 | #include <net/net_namespace.h> |
1da177e4 | 97 | #include <net/sock.h> |
02d62e86 | 98 | #include <net/busy_poll.h> |
1da177e4 | 99 | #include <linux/rtnetlink.h> |
1da177e4 | 100 | #include <linux/stat.h> |
b14a9fc4 | 101 | #include <net/dsa.h> |
1da177e4 | 102 | #include <net/dst.h> |
fc4099f1 | 103 | #include <net/dst_metadata.h> |
1da177e4 | 104 | #include <net/pkt_sched.h> |
87d83093 | 105 | #include <net/pkt_cls.h> |
1da177e4 | 106 | #include <net/checksum.h> |
44540960 | 107 | #include <net/xfrm.h> |
1da177e4 LT |
108 | #include <linux/highmem.h> |
109 | #include <linux/init.h> | |
1da177e4 | 110 | #include <linux/module.h> |
1da177e4 LT |
111 | #include <linux/netpoll.h> |
112 | #include <linux/rcupdate.h> | |
113 | #include <linux/delay.h> | |
1da177e4 | 114 | #include <net/iw_handler.h> |
1da177e4 | 115 | #include <asm/current.h> |
5bdb9886 | 116 | #include <linux/audit.h> |
db217334 | 117 | #include <linux/dmaengine.h> |
f6a78bfc | 118 | #include <linux/err.h> |
c7fa9d18 | 119 | #include <linux/ctype.h> |
723e98b7 | 120 | #include <linux/if_arp.h> |
6de329e2 | 121 | #include <linux/if_vlan.h> |
8f0f2223 | 122 | #include <linux/ip.h> |
ad55dcaf | 123 | #include <net/ip.h> |
25cd9ba0 | 124 | #include <net/mpls.h> |
8f0f2223 DM |
125 | #include <linux/ipv6.h> |
126 | #include <linux/in.h> | |
b6b2fed1 DM |
127 | #include <linux/jhash.h> |
128 | #include <linux/random.h> | |
9cbc1cb8 | 129 | #include <trace/events/napi.h> |
cf66ba58 | 130 | #include <trace/events/net.h> |
07dc22e7 | 131 | #include <trace/events/skb.h> |
caeda9b9 | 132 | #include <linux/inetdevice.h> |
c445477d | 133 | #include <linux/cpu_rmap.h> |
c5905afb | 134 | #include <linux/static_key.h> |
af12fa6e | 135 | #include <linux/hashtable.h> |
60877a32 | 136 | #include <linux/vmalloc.h> |
529d0489 | 137 | #include <linux/if_macvlan.h> |
e7fd2885 | 138 | #include <linux/errqueue.h> |
3b47d303 | 139 | #include <linux/hrtimer.h> |
357b6cc5 | 140 | #include <linux/netfilter_ingress.h> |
40e4e713 | 141 | #include <linux/crash_dump.h> |
b72b5bf6 | 142 | #include <linux/sctp.h> |
ae847f40 | 143 | #include <net/udp_tunnel.h> |
6621dd29 | 144 | #include <linux/net_namespace.h> |
aaa5d90b | 145 | #include <linux/indirect_call_wrapper.h> |
af3836df | 146 | #include <net/devlink.h> |
bd869245 | 147 | #include <linux/pm_runtime.h> |
3744741a | 148 | #include <linux/prandom.h> |
1da177e4 | 149 | |
342709ef PE |
150 | #include "net-sysfs.h" |
151 | ||
d565b0a1 HX |
152 | #define MAX_GRO_SKBS 8 |
153 | ||
5d38a079 HX |
154 | /* This should be increased if a protocol with a bigger head is added. */ |
155 | #define GRO_MAX_HEAD (MAX_HEADER + 128) | |
156 | ||
1da177e4 | 157 | static DEFINE_SPINLOCK(ptype_lock); |
62532da9 | 158 | static DEFINE_SPINLOCK(offload_lock); |
900ff8c6 CW |
159 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
160 | struct list_head ptype_all __read_mostly; /* Taps */ | |
62532da9 | 161 | static struct list_head offload_base __read_mostly; |
1da177e4 | 162 | |
ae78dbfa | 163 | static int netif_rx_internal(struct sk_buff *skb); |
54951194 | 164 | static int call_netdevice_notifiers_info(unsigned long val, |
54951194 | 165 | struct netdev_notifier_info *info); |
26372605 PM |
166 | static int call_netdevice_notifiers_extack(unsigned long val, |
167 | struct net_device *dev, | |
168 | struct netlink_ext_ack *extack); | |
90b602f8 | 169 | static struct napi_struct *napi_by_id(unsigned int napi_id); |
ae78dbfa | 170 | |
1da177e4 | 171 | /* |
7562f876 | 172 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
1da177e4 LT |
173 | * semaphore. |
174 | * | |
c6d14c84 | 175 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
1da177e4 LT |
176 | * |
177 | * Writers must hold the rtnl semaphore while they loop through the | |
7562f876 | 178 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
1da177e4 LT |
179 | * actual updates. This allows pure readers to access the list even |
180 | * while a writer is preparing to update it. | |
181 | * | |
182 | * To put it another way, dev_base_lock is held for writing only to | |
183 | * protect against pure readers; the rtnl semaphore provides the | |
184 | * protection against other writers. | |
185 | * | |
186 | * See, for example usages, register_netdevice() and | |
187 | * unregister_netdevice(), which must be called with the rtnl | |
188 | * semaphore held. | |
189 | */ | |
1da177e4 | 190 | DEFINE_RWLOCK(dev_base_lock); |
1da177e4 LT |
191 | EXPORT_SYMBOL(dev_base_lock); |
192 | ||
6c557001 FW |
193 | static DEFINE_MUTEX(ifalias_mutex); |
194 | ||
af12fa6e ET |
195 | /* protects napi_hash addition/deletion and napi_gen_id */ |
196 | static DEFINE_SPINLOCK(napi_hash_lock); | |
197 | ||
52bd2d62 | 198 | static unsigned int napi_gen_id = NR_CPUS; |
6180d9de | 199 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
af12fa6e | 200 | |
11d6011c | 201 | static DECLARE_RWSEM(devnet_rename_sem); |
c91f6df2 | 202 | |
4e985ada TG |
203 | static inline void dev_base_seq_inc(struct net *net) |
204 | { | |
643aa9cb | 205 | while (++net->dev_base_seq == 0) |
206 | ; | |
4e985ada TG |
207 | } |
208 | ||
881d966b | 209 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 210 | { |
8387ff25 | 211 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
95c96174 | 212 | |
08e9897d | 213 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
214 | } |
215 | ||
881d966b | 216 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 217 | { |
7c28bd0b | 218 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
219 | } |
220 | ||
e36fa2f7 | 221 | static inline void rps_lock(struct softnet_data *sd) |
152102c7 CG |
222 | { |
223 | #ifdef CONFIG_RPS | |
e36fa2f7 | 224 | spin_lock(&sd->input_pkt_queue.lock); |
152102c7 CG |
225 | #endif |
226 | } | |
227 | ||
e36fa2f7 | 228 | static inline void rps_unlock(struct softnet_data *sd) |
152102c7 CG |
229 | { |
230 | #ifdef CONFIG_RPS | |
e36fa2f7 | 231 | spin_unlock(&sd->input_pkt_queue.lock); |
152102c7 CG |
232 | #endif |
233 | } | |
234 | ||
ff927412 JP |
235 | static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, |
236 | const char *name) | |
237 | { | |
238 | struct netdev_name_node *name_node; | |
239 | ||
240 | name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); | |
241 | if (!name_node) | |
242 | return NULL; | |
243 | INIT_HLIST_NODE(&name_node->hlist); | |
244 | name_node->dev = dev; | |
245 | name_node->name = name; | |
246 | return name_node; | |
247 | } | |
248 | ||
249 | static struct netdev_name_node * | |
250 | netdev_name_node_head_alloc(struct net_device *dev) | |
251 | { | |
36fbf1e5 JP |
252 | struct netdev_name_node *name_node; |
253 | ||
254 | name_node = netdev_name_node_alloc(dev, dev->name); | |
255 | if (!name_node) | |
256 | return NULL; | |
257 | INIT_LIST_HEAD(&name_node->list); | |
258 | return name_node; | |
ff927412 JP |
259 | } |
260 | ||
261 | static void netdev_name_node_free(struct netdev_name_node *name_node) | |
262 | { | |
263 | kfree(name_node); | |
264 | } | |
265 | ||
266 | static void netdev_name_node_add(struct net *net, | |
267 | struct netdev_name_node *name_node) | |
268 | { | |
269 | hlist_add_head_rcu(&name_node->hlist, | |
270 | dev_name_hash(net, name_node->name)); | |
271 | } | |
272 | ||
273 | static void netdev_name_node_del(struct netdev_name_node *name_node) | |
274 | { | |
275 | hlist_del_rcu(&name_node->hlist); | |
276 | } | |
277 | ||
278 | static struct netdev_name_node *netdev_name_node_lookup(struct net *net, | |
279 | const char *name) | |
280 | { | |
281 | struct hlist_head *head = dev_name_hash(net, name); | |
282 | struct netdev_name_node *name_node; | |
283 | ||
284 | hlist_for_each_entry(name_node, head, hlist) | |
285 | if (!strcmp(name_node->name, name)) | |
286 | return name_node; | |
287 | return NULL; | |
288 | } | |
289 | ||
290 | static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, | |
291 | const char *name) | |
292 | { | |
293 | struct hlist_head *head = dev_name_hash(net, name); | |
294 | struct netdev_name_node *name_node; | |
295 | ||
296 | hlist_for_each_entry_rcu(name_node, head, hlist) | |
297 | if (!strcmp(name_node->name, name)) | |
298 | return name_node; | |
299 | return NULL; | |
300 | } | |
301 | ||
36fbf1e5 JP |
302 | int netdev_name_node_alt_create(struct net_device *dev, const char *name) |
303 | { | |
304 | struct netdev_name_node *name_node; | |
305 | struct net *net = dev_net(dev); | |
306 | ||
307 | name_node = netdev_name_node_lookup(net, name); | |
308 | if (name_node) | |
309 | return -EEXIST; | |
310 | name_node = netdev_name_node_alloc(dev, name); | |
311 | if (!name_node) | |
312 | return -ENOMEM; | |
313 | netdev_name_node_add(net, name_node); | |
314 | /* The node that holds dev->name acts as a head of per-device list. */ | |
315 | list_add_tail(&name_node->list, &dev->name_node->list); | |
316 | ||
317 | return 0; | |
318 | } | |
319 | EXPORT_SYMBOL(netdev_name_node_alt_create); | |
320 | ||
321 | static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node) | |
322 | { | |
323 | list_del(&name_node->list); | |
324 | netdev_name_node_del(name_node); | |
325 | kfree(name_node->name); | |
326 | netdev_name_node_free(name_node); | |
327 | } | |
328 | ||
329 | int netdev_name_node_alt_destroy(struct net_device *dev, const char *name) | |
330 | { | |
331 | struct netdev_name_node *name_node; | |
332 | struct net *net = dev_net(dev); | |
333 | ||
334 | name_node = netdev_name_node_lookup(net, name); | |
335 | if (!name_node) | |
336 | return -ENOENT; | |
e08ad805 ED |
337 | /* lookup might have found our primary name or a name belonging |
338 | * to another device. | |
339 | */ | |
340 | if (name_node == dev->name_node || name_node->dev != dev) | |
341 | return -EINVAL; | |
342 | ||
36fbf1e5 JP |
343 | __netdev_name_node_alt_destroy(name_node); |
344 | ||
345 | return 0; | |
346 | } | |
347 | EXPORT_SYMBOL(netdev_name_node_alt_destroy); | |
348 | ||
349 | static void netdev_name_node_alt_flush(struct net_device *dev) | |
350 | { | |
351 | struct netdev_name_node *name_node, *tmp; | |
352 | ||
353 | list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) | |
354 | __netdev_name_node_alt_destroy(name_node); | |
355 | } | |
356 | ||
ce286d32 | 357 | /* Device list insertion */ |
53759be9 | 358 | static void list_netdevice(struct net_device *dev) |
ce286d32 | 359 | { |
c346dca1 | 360 | struct net *net = dev_net(dev); |
ce286d32 EB |
361 | |
362 | ASSERT_RTNL(); | |
363 | ||
364 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 365 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
ff927412 | 366 | netdev_name_node_add(net, dev->name_node); |
fb699dfd ED |
367 | hlist_add_head_rcu(&dev->index_hlist, |
368 | dev_index_hash(net, dev->ifindex)); | |
ce286d32 | 369 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
370 | |
371 | dev_base_seq_inc(net); | |
ce286d32 EB |
372 | } |
373 | ||
fb699dfd ED |
374 | /* Device list removal |
375 | * caller must respect a RCU grace period before freeing/reusing dev | |
376 | */ | |
ce286d32 EB |
377 | static void unlist_netdevice(struct net_device *dev) |
378 | { | |
379 | ASSERT_RTNL(); | |
380 | ||
381 | /* Unlink dev from the device chain */ | |
382 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 383 | list_del_rcu(&dev->dev_list); |
ff927412 | 384 | netdev_name_node_del(dev->name_node); |
fb699dfd | 385 | hlist_del_rcu(&dev->index_hlist); |
ce286d32 | 386 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
387 | |
388 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
389 | } |
390 | ||
1da177e4 LT |
391 | /* |
392 | * Our notifier list | |
393 | */ | |
394 | ||
f07d5b94 | 395 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
396 | |
397 | /* | |
398 | * Device drivers call our routines to queue packets here. We empty the | |
399 | * queue in the local softnet handler. | |
400 | */ | |
bea3348e | 401 | |
9958da05 | 402 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 403 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 404 | |
1a33e10e CW |
405 | #ifdef CONFIG_LOCKDEP |
406 | /* | |
407 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class | |
408 | * according to dev->type | |
409 | */ | |
410 | static const unsigned short netdev_lock_type[] = { | |
411 | ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, | |
412 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, | |
413 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, | |
414 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, | |
415 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, | |
416 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, | |
417 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, | |
418 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, | |
419 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, | |
420 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, | |
421 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, | |
422 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, | |
423 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, | |
424 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, | |
425 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; | |
426 | ||
427 | static const char *const netdev_lock_name[] = { | |
428 | "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", | |
429 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", | |
430 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", | |
431 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", | |
432 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", | |
433 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", | |
434 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", | |
435 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", | |
436 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", | |
437 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", | |
438 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", | |
439 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", | |
440 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", | |
441 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", | |
442 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; | |
443 | ||
444 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; | |
845e0ebb | 445 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
1a33e10e CW |
446 | |
447 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) | |
448 | { | |
449 | int i; | |
450 | ||
451 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) | |
452 | if (netdev_lock_type[i] == dev_type) | |
453 | return i; | |
454 | /* the last key is used by default */ | |
455 | return ARRAY_SIZE(netdev_lock_type) - 1; | |
456 | } | |
457 | ||
458 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
459 | unsigned short dev_type) | |
460 | { | |
461 | int i; | |
462 | ||
463 | i = netdev_lock_pos(dev_type); | |
464 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], | |
465 | netdev_lock_name[i]); | |
466 | } | |
845e0ebb CW |
467 | |
468 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
469 | { | |
470 | int i; | |
471 | ||
472 | i = netdev_lock_pos(dev->type); | |
473 | lockdep_set_class_and_name(&dev->addr_list_lock, | |
474 | &netdev_addr_lock_key[i], | |
475 | netdev_lock_name[i]); | |
476 | } | |
1a33e10e CW |
477 | #else |
478 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, | |
479 | unsigned short dev_type) | |
480 | { | |
481 | } | |
845e0ebb CW |
482 | |
483 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
484 | { | |
485 | } | |
1a33e10e CW |
486 | #endif |
487 | ||
1da177e4 | 488 | /******************************************************************************* |
eb13da1a | 489 | * |
490 | * Protocol management and registration routines | |
491 | * | |
492 | *******************************************************************************/ | |
1da177e4 | 493 | |
1da177e4 | 494 | |
1da177e4 LT |
495 | /* |
496 | * Add a protocol ID to the list. Now that the input handler is | |
497 | * smarter we can dispense with all the messy stuff that used to be | |
498 | * here. | |
499 | * | |
500 | * BEWARE!!! Protocol handlers, mangling input packets, | |
501 | * MUST BE last in hash buckets and checking protocol handlers | |
502 | * MUST start from promiscuous ptype_all chain in net_bh. | |
503 | * It is true now, do not change it. | |
504 | * Explanation follows: if protocol handler, mangling packet, will | |
505 | * be the first on list, it is not able to sense, that packet | |
506 | * is cloned and should be copied-on-write, so that it will | |
507 | * change it and subsequent readers will get broken packet. | |
508 | * --ANK (980803) | |
509 | */ | |
510 | ||
c07b68e8 ED |
511 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
512 | { | |
513 | if (pt->type == htons(ETH_P_ALL)) | |
7866a621 | 514 | return pt->dev ? &pt->dev->ptype_all : &ptype_all; |
c07b68e8 | 515 | else |
7866a621 SN |
516 | return pt->dev ? &pt->dev->ptype_specific : |
517 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
c07b68e8 ED |
518 | } |
519 | ||
1da177e4 LT |
520 | /** |
521 | * dev_add_pack - add packet handler | |
522 | * @pt: packet type declaration | |
523 | * | |
524 | * Add a protocol handler to the networking stack. The passed &packet_type | |
525 | * is linked into kernel lists and may not be freed until it has been | |
526 | * removed from the kernel lists. | |
527 | * | |
4ec93edb | 528 | * This call does not sleep therefore it can not |
1da177e4 LT |
529 | * guarantee all CPU's that are in middle of receiving packets |
530 | * will see the new packet type (until the next received packet). | |
531 | */ | |
532 | ||
533 | void dev_add_pack(struct packet_type *pt) | |
534 | { | |
c07b68e8 | 535 | struct list_head *head = ptype_head(pt); |
1da177e4 | 536 | |
c07b68e8 ED |
537 | spin_lock(&ptype_lock); |
538 | list_add_rcu(&pt->list, head); | |
539 | spin_unlock(&ptype_lock); | |
1da177e4 | 540 | } |
d1b19dff | 541 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 542 | |
1da177e4 LT |
543 | /** |
544 | * __dev_remove_pack - remove packet handler | |
545 | * @pt: packet type declaration | |
546 | * | |
547 | * Remove a protocol handler that was previously added to the kernel | |
548 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
549 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 550 | * returns. |
1da177e4 LT |
551 | * |
552 | * The packet type might still be in use by receivers | |
553 | * and must not be freed until after all the CPU's have gone | |
554 | * through a quiescent state. | |
555 | */ | |
556 | void __dev_remove_pack(struct packet_type *pt) | |
557 | { | |
c07b68e8 | 558 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
559 | struct packet_type *pt1; |
560 | ||
c07b68e8 | 561 | spin_lock(&ptype_lock); |
1da177e4 LT |
562 | |
563 | list_for_each_entry(pt1, head, list) { | |
564 | if (pt == pt1) { | |
565 | list_del_rcu(&pt->list); | |
566 | goto out; | |
567 | } | |
568 | } | |
569 | ||
7b6cd1ce | 570 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 571 | out: |
c07b68e8 | 572 | spin_unlock(&ptype_lock); |
1da177e4 | 573 | } |
d1b19dff ED |
574 | EXPORT_SYMBOL(__dev_remove_pack); |
575 | ||
1da177e4 LT |
576 | /** |
577 | * dev_remove_pack - remove packet handler | |
578 | * @pt: packet type declaration | |
579 | * | |
580 | * Remove a protocol handler that was previously added to the kernel | |
581 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
582 | * from the kernel lists and can be freed or reused once this function | |
583 | * returns. | |
584 | * | |
585 | * This call sleeps to guarantee that no CPU is looking at the packet | |
586 | * type after return. | |
587 | */ | |
588 | void dev_remove_pack(struct packet_type *pt) | |
589 | { | |
590 | __dev_remove_pack(pt); | |
4ec93edb | 591 | |
1da177e4 LT |
592 | synchronize_net(); |
593 | } | |
d1b19dff | 594 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 595 | |
62532da9 VY |
596 | |
597 | /** | |
598 | * dev_add_offload - register offload handlers | |
599 | * @po: protocol offload declaration | |
600 | * | |
601 | * Add protocol offload handlers to the networking stack. The passed | |
602 | * &proto_offload is linked into kernel lists and may not be freed until | |
603 | * it has been removed from the kernel lists. | |
604 | * | |
605 | * This call does not sleep therefore it can not | |
606 | * guarantee all CPU's that are in middle of receiving packets | |
607 | * will see the new offload handlers (until the next received packet). | |
608 | */ | |
609 | void dev_add_offload(struct packet_offload *po) | |
610 | { | |
bdef7de4 | 611 | struct packet_offload *elem; |
62532da9 VY |
612 | |
613 | spin_lock(&offload_lock); | |
bdef7de4 DM |
614 | list_for_each_entry(elem, &offload_base, list) { |
615 | if (po->priority < elem->priority) | |
616 | break; | |
617 | } | |
618 | list_add_rcu(&po->list, elem->list.prev); | |
62532da9 VY |
619 | spin_unlock(&offload_lock); |
620 | } | |
621 | EXPORT_SYMBOL(dev_add_offload); | |
622 | ||
623 | /** | |
624 | * __dev_remove_offload - remove offload handler | |
625 | * @po: packet offload declaration | |
626 | * | |
627 | * Remove a protocol offload handler that was previously added to the | |
628 | * kernel offload handlers by dev_add_offload(). The passed &offload_type | |
629 | * is removed from the kernel lists and can be freed or reused once this | |
630 | * function returns. | |
631 | * | |
632 | * The packet type might still be in use by receivers | |
633 | * and must not be freed until after all the CPU's have gone | |
634 | * through a quiescent state. | |
635 | */ | |
1d143d9f | 636 | static void __dev_remove_offload(struct packet_offload *po) |
62532da9 VY |
637 | { |
638 | struct list_head *head = &offload_base; | |
639 | struct packet_offload *po1; | |
640 | ||
c53aa505 | 641 | spin_lock(&offload_lock); |
62532da9 VY |
642 | |
643 | list_for_each_entry(po1, head, list) { | |
644 | if (po == po1) { | |
645 | list_del_rcu(&po->list); | |
646 | goto out; | |
647 | } | |
648 | } | |
649 | ||
650 | pr_warn("dev_remove_offload: %p not found\n", po); | |
651 | out: | |
c53aa505 | 652 | spin_unlock(&offload_lock); |
62532da9 | 653 | } |
62532da9 VY |
654 | |
655 | /** | |
656 | * dev_remove_offload - remove packet offload handler | |
657 | * @po: packet offload declaration | |
658 | * | |
659 | * Remove a packet offload handler that was previously added to the kernel | |
660 | * offload handlers by dev_add_offload(). The passed &offload_type is | |
661 | * removed from the kernel lists and can be freed or reused once this | |
662 | * function returns. | |
663 | * | |
664 | * This call sleeps to guarantee that no CPU is looking at the packet | |
665 | * type after return. | |
666 | */ | |
667 | void dev_remove_offload(struct packet_offload *po) | |
668 | { | |
669 | __dev_remove_offload(po); | |
670 | ||
671 | synchronize_net(); | |
672 | } | |
673 | EXPORT_SYMBOL(dev_remove_offload); | |
674 | ||
1da177e4 | 675 | /****************************************************************************** |
eb13da1a | 676 | * |
677 | * Device Boot-time Settings Routines | |
678 | * | |
679 | ******************************************************************************/ | |
1da177e4 LT |
680 | |
681 | /* Boot time configuration table */ | |
682 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; | |
683 | ||
684 | /** | |
685 | * netdev_boot_setup_add - add new setup entry | |
686 | * @name: name of the device | |
687 | * @map: configured settings for the device | |
688 | * | |
689 | * Adds new setup entry to the dev_boot_setup list. The function | |
690 | * returns 0 on error and 1 on success. This is a generic routine to | |
691 | * all netdevices. | |
692 | */ | |
693 | static int netdev_boot_setup_add(char *name, struct ifmap *map) | |
694 | { | |
695 | struct netdev_boot_setup *s; | |
696 | int i; | |
697 | ||
698 | s = dev_boot_setup; | |
699 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
700 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { | |
701 | memset(s[i].name, 0, sizeof(s[i].name)); | |
93b3cff9 | 702 | strlcpy(s[i].name, name, IFNAMSIZ); |
1da177e4 LT |
703 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
704 | break; | |
705 | } | |
706 | } | |
707 | ||
708 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; | |
709 | } | |
710 | ||
711 | /** | |
722c9a0c | 712 | * netdev_boot_setup_check - check boot time settings |
713 | * @dev: the netdevice | |
1da177e4 | 714 | * |
722c9a0c | 715 | * Check boot time settings for the device. |
716 | * The found settings are set for the device to be used | |
717 | * later in the device probing. | |
718 | * Returns 0 if no settings found, 1 if they are. | |
1da177e4 LT |
719 | */ |
720 | int netdev_boot_setup_check(struct net_device *dev) | |
721 | { | |
722 | struct netdev_boot_setup *s = dev_boot_setup; | |
723 | int i; | |
724 | ||
725 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
726 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && | |
93b3cff9 | 727 | !strcmp(dev->name, s[i].name)) { |
722c9a0c | 728 | dev->irq = s[i].map.irq; |
729 | dev->base_addr = s[i].map.base_addr; | |
730 | dev->mem_start = s[i].map.mem_start; | |
731 | dev->mem_end = s[i].map.mem_end; | |
1da177e4 LT |
732 | return 1; |
733 | } | |
734 | } | |
735 | return 0; | |
736 | } | |
d1b19dff | 737 | EXPORT_SYMBOL(netdev_boot_setup_check); |
1da177e4 LT |
738 | |
739 | ||
740 | /** | |
722c9a0c | 741 | * netdev_boot_base - get address from boot time settings |
742 | * @prefix: prefix for network device | |
743 | * @unit: id for network device | |
744 | * | |
745 | * Check boot time settings for the base address of device. | |
746 | * The found settings are set for the device to be used | |
747 | * later in the device probing. | |
748 | * Returns 0 if no settings found. | |
1da177e4 LT |
749 | */ |
750 | unsigned long netdev_boot_base(const char *prefix, int unit) | |
751 | { | |
752 | const struct netdev_boot_setup *s = dev_boot_setup; | |
753 | char name[IFNAMSIZ]; | |
754 | int i; | |
755 | ||
756 | sprintf(name, "%s%d", prefix, unit); | |
757 | ||
758 | /* | |
759 | * If device already registered then return base of 1 | |
760 | * to indicate not to probe for this interface | |
761 | */ | |
881d966b | 762 | if (__dev_get_by_name(&init_net, name)) |
1da177e4 LT |
763 | return 1; |
764 | ||
765 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) | |
766 | if (!strcmp(name, s[i].name)) | |
767 | return s[i].map.base_addr; | |
768 | return 0; | |
769 | } | |
770 | ||
771 | /* | |
772 | * Saves at boot time configured settings for any netdevice. | |
773 | */ | |
774 | int __init netdev_boot_setup(char *str) | |
775 | { | |
776 | int ints[5]; | |
777 | struct ifmap map; | |
778 | ||
779 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
780 | if (!str || !*str) | |
781 | return 0; | |
782 | ||
783 | /* Save settings */ | |
784 | memset(&map, 0, sizeof(map)); | |
785 | if (ints[0] > 0) | |
786 | map.irq = ints[1]; | |
787 | if (ints[0] > 1) | |
788 | map.base_addr = ints[2]; | |
789 | if (ints[0] > 2) | |
790 | map.mem_start = ints[3]; | |
791 | if (ints[0] > 3) | |
792 | map.mem_end = ints[4]; | |
793 | ||
794 | /* Add new entry to the list */ | |
795 | return netdev_boot_setup_add(str, &map); | |
796 | } | |
797 | ||
798 | __setup("netdev=", netdev_boot_setup); | |
799 | ||
800 | /******************************************************************************* | |
eb13da1a | 801 | * |
802 | * Device Interface Subroutines | |
803 | * | |
804 | *******************************************************************************/ | |
1da177e4 | 805 | |
a54acb3a ND |
806 | /** |
807 | * dev_get_iflink - get 'iflink' value of a interface | |
808 | * @dev: targeted interface | |
809 | * | |
810 | * Indicates the ifindex the interface is linked to. | |
811 | * Physical interfaces have the same 'ifindex' and 'iflink' values. | |
812 | */ | |
813 | ||
814 | int dev_get_iflink(const struct net_device *dev) | |
815 | { | |
816 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) | |
817 | return dev->netdev_ops->ndo_get_iflink(dev); | |
818 | ||
7a66bbc9 | 819 | return dev->ifindex; |
a54acb3a ND |
820 | } |
821 | EXPORT_SYMBOL(dev_get_iflink); | |
822 | ||
fc4099f1 PS |
823 | /** |
824 | * dev_fill_metadata_dst - Retrieve tunnel egress information. | |
825 | * @dev: targeted interface | |
826 | * @skb: The packet. | |
827 | * | |
828 | * For better visibility of tunnel traffic OVS needs to retrieve | |
829 | * egress tunnel information for a packet. Following API allows | |
830 | * user to get this info. | |
831 | */ | |
832 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) | |
833 | { | |
834 | struct ip_tunnel_info *info; | |
835 | ||
836 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) | |
837 | return -EINVAL; | |
838 | ||
839 | info = skb_tunnel_info_unclone(skb); | |
840 | if (!info) | |
841 | return -ENOMEM; | |
842 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) | |
843 | return -EINVAL; | |
844 | ||
845 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); | |
846 | } | |
847 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); | |
848 | ||
1da177e4 LT |
849 | /** |
850 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 851 | * @net: the applicable net namespace |
1da177e4 LT |
852 | * @name: name to find |
853 | * | |
854 | * Find an interface by name. Must be called under RTNL semaphore | |
855 | * or @dev_base_lock. If the name is found a pointer to the device | |
856 | * is returned. If the name is not found then %NULL is returned. The | |
857 | * reference counters are not incremented so the caller must be | |
858 | * careful with locks. | |
859 | */ | |
860 | ||
881d966b | 861 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 | 862 | { |
ff927412 | 863 | struct netdev_name_node *node_name; |
1da177e4 | 864 | |
ff927412 JP |
865 | node_name = netdev_name_node_lookup(net, name); |
866 | return node_name ? node_name->dev : NULL; | |
1da177e4 | 867 | } |
d1b19dff | 868 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 869 | |
72c9528b | 870 | /** |
722c9a0c | 871 | * dev_get_by_name_rcu - find a device by its name |
872 | * @net: the applicable net namespace | |
873 | * @name: name to find | |
874 | * | |
875 | * Find an interface by name. | |
876 | * If the name is found a pointer to the device is returned. | |
877 | * If the name is not found then %NULL is returned. | |
878 | * The reference counters are not incremented so the caller must be | |
879 | * careful with locks. The caller must hold RCU lock. | |
72c9528b ED |
880 | */ |
881 | ||
882 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
883 | { | |
ff927412 | 884 | struct netdev_name_node *node_name; |
72c9528b | 885 | |
ff927412 JP |
886 | node_name = netdev_name_node_lookup_rcu(net, name); |
887 | return node_name ? node_name->dev : NULL; | |
72c9528b ED |
888 | } |
889 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
890 | ||
1da177e4 LT |
891 | /** |
892 | * dev_get_by_name - find a device by its name | |
c4ea43c5 | 893 | * @net: the applicable net namespace |
1da177e4 LT |
894 | * @name: name to find |
895 | * | |
896 | * Find an interface by name. This can be called from any | |
897 | * context and does its own locking. The returned handle has | |
898 | * the usage count incremented and the caller must use dev_put() to | |
899 | * release it when it is no longer needed. %NULL is returned if no | |
900 | * matching device is found. | |
901 | */ | |
902 | ||
881d966b | 903 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
904 | { |
905 | struct net_device *dev; | |
906 | ||
72c9528b ED |
907 | rcu_read_lock(); |
908 | dev = dev_get_by_name_rcu(net, name); | |
1da177e4 LT |
909 | if (dev) |
910 | dev_hold(dev); | |
72c9528b | 911 | rcu_read_unlock(); |
1da177e4 LT |
912 | return dev; |
913 | } | |
d1b19dff | 914 | EXPORT_SYMBOL(dev_get_by_name); |
1da177e4 LT |
915 | |
916 | /** | |
917 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 918 | * @net: the applicable net namespace |
1da177e4 LT |
919 | * @ifindex: index of device |
920 | * | |
921 | * Search for an interface by index. Returns %NULL if the device | |
922 | * is not found or a pointer to the device. The device has not | |
923 | * had its reference counter increased so the caller must be careful | |
924 | * about locking. The caller must hold either the RTNL semaphore | |
925 | * or @dev_base_lock. | |
926 | */ | |
927 | ||
881d966b | 928 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 | 929 | { |
0bd8d536 ED |
930 | struct net_device *dev; |
931 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 932 | |
b67bfe0d | 933 | hlist_for_each_entry(dev, head, index_hlist) |
1da177e4 LT |
934 | if (dev->ifindex == ifindex) |
935 | return dev; | |
0bd8d536 | 936 | |
1da177e4 LT |
937 | return NULL; |
938 | } | |
d1b19dff | 939 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 940 | |
fb699dfd ED |
941 | /** |
942 | * dev_get_by_index_rcu - find a device by its ifindex | |
943 | * @net: the applicable net namespace | |
944 | * @ifindex: index of device | |
945 | * | |
946 | * Search for an interface by index. Returns %NULL if the device | |
947 | * is not found or a pointer to the device. The device has not | |
948 | * had its reference counter increased so the caller must be careful | |
949 | * about locking. The caller must hold RCU lock. | |
950 | */ | |
951 | ||
952 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
953 | { | |
fb699dfd ED |
954 | struct net_device *dev; |
955 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
956 | ||
b67bfe0d | 957 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
fb699dfd ED |
958 | if (dev->ifindex == ifindex) |
959 | return dev; | |
960 | ||
961 | return NULL; | |
962 | } | |
963 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
964 | ||
1da177e4 LT |
965 | |
966 | /** | |
967 | * dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 968 | * @net: the applicable net namespace |
1da177e4 LT |
969 | * @ifindex: index of device |
970 | * | |
971 | * Search for an interface by index. Returns NULL if the device | |
972 | * is not found or a pointer to the device. The device returned has | |
973 | * had a reference added and the pointer is safe until the user calls | |
974 | * dev_put to indicate they have finished with it. | |
975 | */ | |
976 | ||
881d966b | 977 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
978 | { |
979 | struct net_device *dev; | |
980 | ||
fb699dfd ED |
981 | rcu_read_lock(); |
982 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 LT |
983 | if (dev) |
984 | dev_hold(dev); | |
fb699dfd | 985 | rcu_read_unlock(); |
1da177e4 LT |
986 | return dev; |
987 | } | |
d1b19dff | 988 | EXPORT_SYMBOL(dev_get_by_index); |
1da177e4 | 989 | |
90b602f8 ML |
990 | /** |
991 | * dev_get_by_napi_id - find a device by napi_id | |
992 | * @napi_id: ID of the NAPI struct | |
993 | * | |
994 | * Search for an interface by NAPI ID. Returns %NULL if the device | |
995 | * is not found or a pointer to the device. The device has not had | |
996 | * its reference counter increased so the caller must be careful | |
997 | * about locking. The caller must hold RCU lock. | |
998 | */ | |
999 | ||
1000 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) | |
1001 | { | |
1002 | struct napi_struct *napi; | |
1003 | ||
1004 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
1005 | ||
1006 | if (napi_id < MIN_NAPI_ID) | |
1007 | return NULL; | |
1008 | ||
1009 | napi = napi_by_id(napi_id); | |
1010 | ||
1011 | return napi ? napi->dev : NULL; | |
1012 | } | |
1013 | EXPORT_SYMBOL(dev_get_by_napi_id); | |
1014 | ||
5dbe7c17 NS |
1015 | /** |
1016 | * netdev_get_name - get a netdevice name, knowing its ifindex. | |
1017 | * @net: network namespace | |
1018 | * @name: a pointer to the buffer where the name will be stored. | |
1019 | * @ifindex: the ifindex of the interface to get the name from. | |
5dbe7c17 NS |
1020 | */ |
1021 | int netdev_get_name(struct net *net, char *name, int ifindex) | |
1022 | { | |
1023 | struct net_device *dev; | |
11d6011c | 1024 | int ret; |
5dbe7c17 | 1025 | |
11d6011c | 1026 | down_read(&devnet_rename_sem); |
5dbe7c17 | 1027 | rcu_read_lock(); |
11d6011c | 1028 | |
5dbe7c17 NS |
1029 | dev = dev_get_by_index_rcu(net, ifindex); |
1030 | if (!dev) { | |
11d6011c AD |
1031 | ret = -ENODEV; |
1032 | goto out; | |
5dbe7c17 NS |
1033 | } |
1034 | ||
1035 | strcpy(name, dev->name); | |
5dbe7c17 | 1036 | |
11d6011c AD |
1037 | ret = 0; |
1038 | out: | |
1039 | rcu_read_unlock(); | |
1040 | up_read(&devnet_rename_sem); | |
1041 | return ret; | |
5dbe7c17 NS |
1042 | } |
1043 | ||
1da177e4 | 1044 | /** |
941666c2 | 1045 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 1046 | * @net: the applicable net namespace |
1da177e4 LT |
1047 | * @type: media type of device |
1048 | * @ha: hardware address | |
1049 | * | |
1050 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
1051 | * is not found or a pointer to the device. |
1052 | * The caller must hold RCU or RTNL. | |
941666c2 | 1053 | * The returned device has not had its ref count increased |
1da177e4 LT |
1054 | * and the caller must therefore be careful about locking |
1055 | * | |
1da177e4 LT |
1056 | */ |
1057 | ||
941666c2 ED |
1058 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
1059 | const char *ha) | |
1da177e4 LT |
1060 | { |
1061 | struct net_device *dev; | |
1062 | ||
941666c2 | 1063 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
1064 | if (dev->type == type && |
1065 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
1066 | return dev; |
1067 | ||
1068 | return NULL; | |
1da177e4 | 1069 | } |
941666c2 | 1070 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 1071 | |
881d966b | 1072 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 1073 | { |
99fe3c39 | 1074 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 1075 | |
99fe3c39 ED |
1076 | rcu_read_lock(); |
1077 | for_each_netdev_rcu(net, dev) | |
1078 | if (dev->type == type) { | |
1079 | dev_hold(dev); | |
1080 | ret = dev; | |
1081 | break; | |
1082 | } | |
1083 | rcu_read_unlock(); | |
1084 | return ret; | |
1da177e4 | 1085 | } |
1da177e4 LT |
1086 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
1087 | ||
1088 | /** | |
6c555490 | 1089 | * __dev_get_by_flags - find any device with given flags |
c4ea43c5 | 1090 | * @net: the applicable net namespace |
1da177e4 LT |
1091 | * @if_flags: IFF_* values |
1092 | * @mask: bitmask of bits in if_flags to check | |
1093 | * | |
1094 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 | 1095 | * is not found or a pointer to the device. Must be called inside |
6c555490 | 1096 | * rtnl_lock(), and result refcount is unchanged. |
1da177e4 LT |
1097 | */ |
1098 | ||
6c555490 WC |
1099 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
1100 | unsigned short mask) | |
1da177e4 | 1101 | { |
7562f876 | 1102 | struct net_device *dev, *ret; |
1da177e4 | 1103 | |
6c555490 WC |
1104 | ASSERT_RTNL(); |
1105 | ||
7562f876 | 1106 | ret = NULL; |
6c555490 | 1107 | for_each_netdev(net, dev) { |
1da177e4 | 1108 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 1109 | ret = dev; |
1da177e4 LT |
1110 | break; |
1111 | } | |
1112 | } | |
7562f876 | 1113 | return ret; |
1da177e4 | 1114 | } |
6c555490 | 1115 | EXPORT_SYMBOL(__dev_get_by_flags); |
1da177e4 LT |
1116 | |
1117 | /** | |
1118 | * dev_valid_name - check if name is okay for network device | |
1119 | * @name: name string | |
1120 | * | |
1121 | * Network device names need to be valid file names to | |
4250b75b | 1122 | * allow sysfs to work. We also disallow any kind of |
c7fa9d18 | 1123 | * whitespace. |
1da177e4 | 1124 | */ |
95f050bf | 1125 | bool dev_valid_name(const char *name) |
1da177e4 | 1126 | { |
c7fa9d18 | 1127 | if (*name == '\0') |
95f050bf | 1128 | return false; |
a9d48205 | 1129 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
95f050bf | 1130 | return false; |
c7fa9d18 | 1131 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 1132 | return false; |
c7fa9d18 DM |
1133 | |
1134 | while (*name) { | |
a4176a93 | 1135 | if (*name == '/' || *name == ':' || isspace(*name)) |
95f050bf | 1136 | return false; |
c7fa9d18 DM |
1137 | name++; |
1138 | } | |
95f050bf | 1139 | return true; |
1da177e4 | 1140 | } |
d1b19dff | 1141 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
1142 | |
1143 | /** | |
b267b179 EB |
1144 | * __dev_alloc_name - allocate a name for a device |
1145 | * @net: network namespace to allocate the device name in | |
1da177e4 | 1146 | * @name: name format string |
b267b179 | 1147 | * @buf: scratch buffer and result name string |
1da177e4 LT |
1148 | * |
1149 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
1150 | * id. It scans list of devices to build up a free map, then chooses |
1151 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1152 | * while allocating the name and adding the device in order to avoid | |
1153 | * duplicates. | |
1154 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1155 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
1156 | */ |
1157 | ||
b267b179 | 1158 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
1da177e4 LT |
1159 | { |
1160 | int i = 0; | |
1da177e4 LT |
1161 | const char *p; |
1162 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 1163 | unsigned long *inuse; |
1da177e4 LT |
1164 | struct net_device *d; |
1165 | ||
93809105 RV |
1166 | if (!dev_valid_name(name)) |
1167 | return -EINVAL; | |
1168 | ||
51f299dd | 1169 | p = strchr(name, '%'); |
1da177e4 LT |
1170 | if (p) { |
1171 | /* | |
1172 | * Verify the string as this thing may have come from | |
1173 | * the user. There must be either one "%d" and no other "%" | |
1174 | * characters. | |
1175 | */ | |
1176 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
1177 | return -EINVAL; | |
1178 | ||
1179 | /* Use one page as a bit array of possible slots */ | |
cfcabdcc | 1180 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
1181 | if (!inuse) |
1182 | return -ENOMEM; | |
1183 | ||
881d966b | 1184 | for_each_netdev(net, d) { |
1da177e4 LT |
1185 | if (!sscanf(d->name, name, &i)) |
1186 | continue; | |
1187 | if (i < 0 || i >= max_netdevices) | |
1188 | continue; | |
1189 | ||
1190 | /* avoid cases where sscanf is not exact inverse of printf */ | |
b267b179 | 1191 | snprintf(buf, IFNAMSIZ, name, i); |
1da177e4 LT |
1192 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
1193 | set_bit(i, inuse); | |
1194 | } | |
1195 | ||
1196 | i = find_first_zero_bit(inuse, max_netdevices); | |
1197 | free_page((unsigned long) inuse); | |
1198 | } | |
1199 | ||
6224abda | 1200 | snprintf(buf, IFNAMSIZ, name, i); |
b267b179 | 1201 | if (!__dev_get_by_name(net, buf)) |
1da177e4 | 1202 | return i; |
1da177e4 LT |
1203 | |
1204 | /* It is possible to run out of possible slots | |
1205 | * when the name is long and there isn't enough space left | |
1206 | * for the digits, or if all bits are used. | |
1207 | */ | |
029b6d14 | 1208 | return -ENFILE; |
1da177e4 LT |
1209 | } |
1210 | ||
2c88b855 RV |
1211 | static int dev_alloc_name_ns(struct net *net, |
1212 | struct net_device *dev, | |
1213 | const char *name) | |
1214 | { | |
1215 | char buf[IFNAMSIZ]; | |
1216 | int ret; | |
1217 | ||
c46d7642 | 1218 | BUG_ON(!net); |
2c88b855 RV |
1219 | ret = __dev_alloc_name(net, name, buf); |
1220 | if (ret >= 0) | |
1221 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1222 | return ret; | |
1da177e4 LT |
1223 | } |
1224 | ||
b267b179 EB |
1225 | /** |
1226 | * dev_alloc_name - allocate a name for a device | |
1227 | * @dev: device | |
1228 | * @name: name format string | |
1229 | * | |
1230 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1231 | * id. It scans list of devices to build up a free map, then chooses | |
1232 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1233 | * while allocating the name and adding the device in order to avoid | |
1234 | * duplicates. | |
1235 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1236 | * Returns the number of the unit assigned or a negative errno code. | |
1237 | */ | |
1238 | ||
1239 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1240 | { | |
c46d7642 | 1241 | return dev_alloc_name_ns(dev_net(dev), dev, name); |
b267b179 | 1242 | } |
d1b19dff | 1243 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1244 | |
bacb7e18 ED |
1245 | static int dev_get_valid_name(struct net *net, struct net_device *dev, |
1246 | const char *name) | |
828de4f6 | 1247 | { |
55a5ec9b DM |
1248 | BUG_ON(!net); |
1249 | ||
1250 | if (!dev_valid_name(name)) | |
1251 | return -EINVAL; | |
1252 | ||
1253 | if (strchr(name, '%')) | |
1254 | return dev_alloc_name_ns(net, dev, name); | |
1255 | else if (__dev_get_by_name(net, name)) | |
1256 | return -EEXIST; | |
1257 | else if (dev->name != name) | |
1258 | strlcpy(dev->name, name, IFNAMSIZ); | |
1259 | ||
1260 | return 0; | |
d9031024 | 1261 | } |
1da177e4 LT |
1262 | |
1263 | /** | |
1264 | * dev_change_name - change name of a device | |
1265 | * @dev: device | |
1266 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1267 | * | |
1268 | * Change name of a device, can pass format strings "eth%d". | |
1269 | * for wildcarding. | |
1270 | */ | |
cf04a4c7 | 1271 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1272 | { |
238fa362 | 1273 | unsigned char old_assign_type; |
fcc5a03a | 1274 | char oldname[IFNAMSIZ]; |
1da177e4 | 1275 | int err = 0; |
fcc5a03a | 1276 | int ret; |
881d966b | 1277 | struct net *net; |
1da177e4 LT |
1278 | |
1279 | ASSERT_RTNL(); | |
c346dca1 | 1280 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1281 | |
c346dca1 | 1282 | net = dev_net(dev); |
8065a779 SWL |
1283 | |
1284 | /* Some auto-enslaved devices e.g. failover slaves are | |
1285 | * special, as userspace might rename the device after | |
1286 | * the interface had been brought up and running since | |
1287 | * the point kernel initiated auto-enslavement. Allow | |
1288 | * live name change even when these slave devices are | |
1289 | * up and running. | |
1290 | * | |
1291 | * Typically, users of these auto-enslaving devices | |
1292 | * don't actually care about slave name change, as | |
1293 | * they are supposed to operate on master interface | |
1294 | * directly. | |
1295 | */ | |
1296 | if (dev->flags & IFF_UP && | |
1297 | likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK))) | |
1da177e4 LT |
1298 | return -EBUSY; |
1299 | ||
11d6011c | 1300 | down_write(&devnet_rename_sem); |
c91f6df2 BH |
1301 | |
1302 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
11d6011c | 1303 | up_write(&devnet_rename_sem); |
c8d90dca | 1304 | return 0; |
c91f6df2 | 1305 | } |
c8d90dca | 1306 | |
fcc5a03a HX |
1307 | memcpy(oldname, dev->name, IFNAMSIZ); |
1308 | ||
828de4f6 | 1309 | err = dev_get_valid_name(net, dev, newname); |
c91f6df2 | 1310 | if (err < 0) { |
11d6011c | 1311 | up_write(&devnet_rename_sem); |
d9031024 | 1312 | return err; |
c91f6df2 | 1313 | } |
1da177e4 | 1314 | |
6fe82a39 VF |
1315 | if (oldname[0] && !strchr(oldname, '%')) |
1316 | netdev_info(dev, "renamed from %s\n", oldname); | |
1317 | ||
238fa362 TG |
1318 | old_assign_type = dev->name_assign_type; |
1319 | dev->name_assign_type = NET_NAME_RENAMED; | |
1320 | ||
fcc5a03a | 1321 | rollback: |
a1b3f594 EB |
1322 | ret = device_rename(&dev->dev, dev->name); |
1323 | if (ret) { | |
1324 | memcpy(dev->name, oldname, IFNAMSIZ); | |
238fa362 | 1325 | dev->name_assign_type = old_assign_type; |
11d6011c | 1326 | up_write(&devnet_rename_sem); |
a1b3f594 | 1327 | return ret; |
dcc99773 | 1328 | } |
7f988eab | 1329 | |
11d6011c | 1330 | up_write(&devnet_rename_sem); |
c91f6df2 | 1331 | |
5bb025fa VF |
1332 | netdev_adjacent_rename_links(dev, oldname); |
1333 | ||
7f988eab | 1334 | write_lock_bh(&dev_base_lock); |
ff927412 | 1335 | netdev_name_node_del(dev->name_node); |
72c9528b ED |
1336 | write_unlock_bh(&dev_base_lock); |
1337 | ||
1338 | synchronize_rcu(); | |
1339 | ||
1340 | write_lock_bh(&dev_base_lock); | |
ff927412 | 1341 | netdev_name_node_add(net, dev->name_node); |
7f988eab HX |
1342 | write_unlock_bh(&dev_base_lock); |
1343 | ||
056925ab | 1344 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1345 | ret = notifier_to_errno(ret); |
1346 | ||
1347 | if (ret) { | |
91e9c07b ED |
1348 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1349 | if (err >= 0) { | |
fcc5a03a | 1350 | err = ret; |
11d6011c | 1351 | down_write(&devnet_rename_sem); |
fcc5a03a | 1352 | memcpy(dev->name, oldname, IFNAMSIZ); |
5bb025fa | 1353 | memcpy(oldname, newname, IFNAMSIZ); |
238fa362 TG |
1354 | dev->name_assign_type = old_assign_type; |
1355 | old_assign_type = NET_NAME_RENAMED; | |
fcc5a03a | 1356 | goto rollback; |
91e9c07b | 1357 | } else { |
7b6cd1ce | 1358 | pr_err("%s: name change rollback failed: %d\n", |
91e9c07b | 1359 | dev->name, ret); |
fcc5a03a HX |
1360 | } |
1361 | } | |
1da177e4 LT |
1362 | |
1363 | return err; | |
1364 | } | |
1365 | ||
0b815a1a SH |
1366 | /** |
1367 | * dev_set_alias - change ifalias of a device | |
1368 | * @dev: device | |
1369 | * @alias: name up to IFALIASZ | |
f0db275a | 1370 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1371 | * |
1372 | * Set ifalias for a device, | |
1373 | */ | |
1374 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1375 | { | |
6c557001 | 1376 | struct dev_ifalias *new_alias = NULL; |
0b815a1a SH |
1377 | |
1378 | if (len >= IFALIASZ) | |
1379 | return -EINVAL; | |
1380 | ||
6c557001 FW |
1381 | if (len) { |
1382 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); | |
1383 | if (!new_alias) | |
1384 | return -ENOMEM; | |
1385 | ||
1386 | memcpy(new_alias->ifalias, alias, len); | |
1387 | new_alias->ifalias[len] = 0; | |
96ca4a2c OH |
1388 | } |
1389 | ||
6c557001 | 1390 | mutex_lock(&ifalias_mutex); |
e3f0d761 PM |
1391 | new_alias = rcu_replace_pointer(dev->ifalias, new_alias, |
1392 | mutex_is_locked(&ifalias_mutex)); | |
6c557001 FW |
1393 | mutex_unlock(&ifalias_mutex); |
1394 | ||
1395 | if (new_alias) | |
1396 | kfree_rcu(new_alias, rcuhead); | |
0b815a1a | 1397 | |
0b815a1a SH |
1398 | return len; |
1399 | } | |
0fe554a4 | 1400 | EXPORT_SYMBOL(dev_set_alias); |
0b815a1a | 1401 | |
6c557001 FW |
1402 | /** |
1403 | * dev_get_alias - get ifalias of a device | |
1404 | * @dev: device | |
20e88320 | 1405 | * @name: buffer to store name of ifalias |
6c557001 FW |
1406 | * @len: size of buffer |
1407 | * | |
1408 | * get ifalias for a device. Caller must make sure dev cannot go | |
1409 | * away, e.g. rcu read lock or own a reference count to device. | |
1410 | */ | |
1411 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) | |
1412 | { | |
1413 | const struct dev_ifalias *alias; | |
1414 | int ret = 0; | |
1415 | ||
1416 | rcu_read_lock(); | |
1417 | alias = rcu_dereference(dev->ifalias); | |
1418 | if (alias) | |
1419 | ret = snprintf(name, len, "%s", alias->ifalias); | |
1420 | rcu_read_unlock(); | |
1421 | ||
1422 | return ret; | |
1423 | } | |
0b815a1a | 1424 | |
d8a33ac4 | 1425 | /** |
3041a069 | 1426 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1427 | * @dev: device to cause notification |
1428 | * | |
1429 | * Called to indicate a device has changed features. | |
1430 | */ | |
1431 | void netdev_features_change(struct net_device *dev) | |
1432 | { | |
056925ab | 1433 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1434 | } |
1435 | EXPORT_SYMBOL(netdev_features_change); | |
1436 | ||
1da177e4 LT |
1437 | /** |
1438 | * netdev_state_change - device changes state | |
1439 | * @dev: device to cause notification | |
1440 | * | |
1441 | * Called to indicate a device has changed state. This function calls | |
1442 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1443 | * to the routing socket. | |
1444 | */ | |
1445 | void netdev_state_change(struct net_device *dev) | |
1446 | { | |
1447 | if (dev->flags & IFF_UP) { | |
51d0c047 DA |
1448 | struct netdev_notifier_change_info change_info = { |
1449 | .info.dev = dev, | |
1450 | }; | |
54951194 | 1451 | |
51d0c047 | 1452 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
54951194 | 1453 | &change_info.info); |
7f294054 | 1454 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
1da177e4 LT |
1455 | } |
1456 | } | |
d1b19dff | 1457 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1458 | |
7061eb8c LP |
1459 | /** |
1460 | * __netdev_notify_peers - notify network peers about existence of @dev, | |
1461 | * to be called when rtnl lock is already held. | |
1462 | * @dev: network device | |
1463 | * | |
1464 | * Generate traffic such that interested network peers are aware of | |
1465 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1466 | * a device wants to inform the rest of the network about some sort of | |
1467 | * reconfiguration such as a failover event or virtual machine | |
1468 | * migration. | |
1469 | */ | |
1470 | void __netdev_notify_peers(struct net_device *dev) | |
1471 | { | |
1472 | ASSERT_RTNL(); | |
1473 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
1474 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); | |
1475 | } | |
1476 | EXPORT_SYMBOL(__netdev_notify_peers); | |
1477 | ||
ee89bab1 | 1478 | /** |
722c9a0c | 1479 | * netdev_notify_peers - notify network peers about existence of @dev |
1480 | * @dev: network device | |
ee89bab1 AW |
1481 | * |
1482 | * Generate traffic such that interested network peers are aware of | |
1483 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1484 | * a device wants to inform the rest of the network about some sort of | |
1485 | * reconfiguration such as a failover event or virtual machine | |
1486 | * migration. | |
1487 | */ | |
1488 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1489 | { |
ee89bab1 | 1490 | rtnl_lock(); |
7061eb8c | 1491 | __netdev_notify_peers(dev); |
ee89bab1 | 1492 | rtnl_unlock(); |
c1da4ac7 | 1493 | } |
ee89bab1 | 1494 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1495 | |
40c900aa | 1496 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
1da177e4 | 1497 | { |
d314774c | 1498 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1499 | int ret; |
1da177e4 | 1500 | |
e46b66bc BH |
1501 | ASSERT_RTNL(); |
1502 | ||
bd869245 HK |
1503 | if (!netif_device_present(dev)) { |
1504 | /* may be detached because parent is runtime-suspended */ | |
1505 | if (dev->dev.parent) | |
1506 | pm_runtime_resume(dev->dev.parent); | |
1507 | if (!netif_device_present(dev)) | |
1508 | return -ENODEV; | |
1509 | } | |
1da177e4 | 1510 | |
ca99ca14 NH |
1511 | /* Block netpoll from trying to do any rx path servicing. |
1512 | * If we don't do this there is a chance ndo_poll_controller | |
1513 | * or ndo_poll may be running while we open the device | |
1514 | */ | |
66b5552f | 1515 | netpoll_poll_disable(dev); |
ca99ca14 | 1516 | |
40c900aa | 1517 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
3b8bcfd5 JB |
1518 | ret = notifier_to_errno(ret); |
1519 | if (ret) | |
1520 | return ret; | |
1521 | ||
1da177e4 | 1522 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1523 | |
d314774c SH |
1524 | if (ops->ndo_validate_addr) |
1525 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1526 | |
d314774c SH |
1527 | if (!ret && ops->ndo_open) |
1528 | ret = ops->ndo_open(dev); | |
1da177e4 | 1529 | |
66b5552f | 1530 | netpoll_poll_enable(dev); |
ca99ca14 | 1531 | |
bada339b JG |
1532 | if (ret) |
1533 | clear_bit(__LINK_STATE_START, &dev->state); | |
1534 | else { | |
1da177e4 | 1535 | dev->flags |= IFF_UP; |
4417da66 | 1536 | dev_set_rx_mode(dev); |
1da177e4 | 1537 | dev_activate(dev); |
7bf23575 | 1538 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1539 | } |
bada339b | 1540 | |
1da177e4 LT |
1541 | return ret; |
1542 | } | |
1543 | ||
1544 | /** | |
bd380811 | 1545 | * dev_open - prepare an interface for use. |
00f54e68 PM |
1546 | * @dev: device to open |
1547 | * @extack: netlink extended ack | |
1da177e4 | 1548 | * |
bd380811 PM |
1549 | * Takes a device from down to up state. The device's private open |
1550 | * function is invoked and then the multicast lists are loaded. Finally | |
1551 | * the device is moved into the up state and a %NETDEV_UP message is | |
1552 | * sent to the netdev notifier chain. | |
1553 | * | |
1554 | * Calling this function on an active interface is a nop. On a failure | |
1555 | * a negative errno code is returned. | |
1da177e4 | 1556 | */ |
00f54e68 | 1557 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
bd380811 PM |
1558 | { |
1559 | int ret; | |
1560 | ||
bd380811 PM |
1561 | if (dev->flags & IFF_UP) |
1562 | return 0; | |
1563 | ||
40c900aa | 1564 | ret = __dev_open(dev, extack); |
bd380811 PM |
1565 | if (ret < 0) |
1566 | return ret; | |
1567 | ||
7f294054 | 1568 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
bd380811 PM |
1569 | call_netdevice_notifiers(NETDEV_UP, dev); |
1570 | ||
1571 | return ret; | |
1572 | } | |
1573 | EXPORT_SYMBOL(dev_open); | |
1574 | ||
7051b88a | 1575 | static void __dev_close_many(struct list_head *head) |
1da177e4 | 1576 | { |
44345724 | 1577 | struct net_device *dev; |
e46b66bc | 1578 | |
bd380811 | 1579 | ASSERT_RTNL(); |
9d5010db DM |
1580 | might_sleep(); |
1581 | ||
5cde2829 | 1582 | list_for_each_entry(dev, head, close_list) { |
3f4df206 | 1583 | /* Temporarily disable netpoll until the interface is down */ |
66b5552f | 1584 | netpoll_poll_disable(dev); |
3f4df206 | 1585 | |
44345724 | 1586 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1587 | |
44345724 | 1588 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1589 | |
44345724 OP |
1590 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1591 | * can be even on different cpu. So just clear netif_running(). | |
1592 | * | |
1593 | * dev->stop() will invoke napi_disable() on all of it's | |
1594 | * napi_struct instances on this device. | |
1595 | */ | |
4e857c58 | 1596 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
44345724 | 1597 | } |
1da177e4 | 1598 | |
44345724 | 1599 | dev_deactivate_many(head); |
d8b2a4d2 | 1600 | |
5cde2829 | 1601 | list_for_each_entry(dev, head, close_list) { |
44345724 | 1602 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 1603 | |
44345724 OP |
1604 | /* |
1605 | * Call the device specific close. This cannot fail. | |
1606 | * Only if device is UP | |
1607 | * | |
1608 | * We allow it to be called even after a DETACH hot-plug | |
1609 | * event. | |
1610 | */ | |
1611 | if (ops->ndo_stop) | |
1612 | ops->ndo_stop(dev); | |
1613 | ||
44345724 | 1614 | dev->flags &= ~IFF_UP; |
66b5552f | 1615 | netpoll_poll_enable(dev); |
44345724 | 1616 | } |
44345724 OP |
1617 | } |
1618 | ||
7051b88a | 1619 | static void __dev_close(struct net_device *dev) |
44345724 OP |
1620 | { |
1621 | LIST_HEAD(single); | |
1622 | ||
5cde2829 | 1623 | list_add(&dev->close_list, &single); |
7051b88a | 1624 | __dev_close_many(&single); |
f87e6f47 | 1625 | list_del(&single); |
44345724 OP |
1626 | } |
1627 | ||
7051b88a | 1628 | void dev_close_many(struct list_head *head, bool unlink) |
44345724 OP |
1629 | { |
1630 | struct net_device *dev, *tmp; | |
1da177e4 | 1631 | |
5cde2829 EB |
1632 | /* Remove the devices that don't need to be closed */ |
1633 | list_for_each_entry_safe(dev, tmp, head, close_list) | |
44345724 | 1634 | if (!(dev->flags & IFF_UP)) |
5cde2829 | 1635 | list_del_init(&dev->close_list); |
44345724 OP |
1636 | |
1637 | __dev_close_many(head); | |
1da177e4 | 1638 | |
5cde2829 | 1639 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
7f294054 | 1640 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
44345724 | 1641 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
99c4a26a DM |
1642 | if (unlink) |
1643 | list_del_init(&dev->close_list); | |
44345724 | 1644 | } |
bd380811 | 1645 | } |
99c4a26a | 1646 | EXPORT_SYMBOL(dev_close_many); |
bd380811 PM |
1647 | |
1648 | /** | |
1649 | * dev_close - shutdown an interface. | |
1650 | * @dev: device to shutdown | |
1651 | * | |
1652 | * This function moves an active device into down state. A | |
1653 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1654 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1655 | * chain. | |
1656 | */ | |
7051b88a | 1657 | void dev_close(struct net_device *dev) |
bd380811 | 1658 | { |
e14a5993 ED |
1659 | if (dev->flags & IFF_UP) { |
1660 | LIST_HEAD(single); | |
1da177e4 | 1661 | |
5cde2829 | 1662 | list_add(&dev->close_list, &single); |
99c4a26a | 1663 | dev_close_many(&single, true); |
e14a5993 ED |
1664 | list_del(&single); |
1665 | } | |
1da177e4 | 1666 | } |
d1b19dff | 1667 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1668 | |
1669 | ||
0187bdfb BH |
1670 | /** |
1671 | * dev_disable_lro - disable Large Receive Offload on a device | |
1672 | * @dev: device | |
1673 | * | |
1674 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1675 | * called under RTNL. This is needed if received packets may be | |
1676 | * forwarded to another interface. | |
1677 | */ | |
1678 | void dev_disable_lro(struct net_device *dev) | |
1679 | { | |
fbe168ba MK |
1680 | struct net_device *lower_dev; |
1681 | struct list_head *iter; | |
529d0489 | 1682 | |
bc5787c6 MM |
1683 | dev->wanted_features &= ~NETIF_F_LRO; |
1684 | netdev_update_features(dev); | |
27660515 | 1685 | |
22d5969f MM |
1686 | if (unlikely(dev->features & NETIF_F_LRO)) |
1687 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
fbe168ba MK |
1688 | |
1689 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
1690 | dev_disable_lro(lower_dev); | |
0187bdfb BH |
1691 | } |
1692 | EXPORT_SYMBOL(dev_disable_lro); | |
1693 | ||
56f5aa77 MC |
1694 | /** |
1695 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device | |
1696 | * @dev: device | |
1697 | * | |
1698 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be | |
1699 | * called under RTNL. This is needed if Generic XDP is installed on | |
1700 | * the device. | |
1701 | */ | |
1702 | static void dev_disable_gro_hw(struct net_device *dev) | |
1703 | { | |
1704 | dev->wanted_features &= ~NETIF_F_GRO_HW; | |
1705 | netdev_update_features(dev); | |
1706 | ||
1707 | if (unlikely(dev->features & NETIF_F_GRO_HW)) | |
1708 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); | |
1709 | } | |
1710 | ||
ede2762d KT |
1711 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
1712 | { | |
1713 | #define N(val) \ | |
1714 | case NETDEV_##val: \ | |
1715 | return "NETDEV_" __stringify(val); | |
1716 | switch (cmd) { | |
1717 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) | |
1718 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) | |
1719 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) | |
1720 | N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER) | |
1721 | N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO) | |
1722 | N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO) | |
1723 | N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) | |
9daae9bd GP |
1724 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
1725 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) | |
1570415f | 1726 | N(PRE_CHANGEADDR) |
3f5ecd8a | 1727 | } |
ede2762d KT |
1728 | #undef N |
1729 | return "UNKNOWN_NETDEV_EVENT"; | |
1730 | } | |
1731 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); | |
1732 | ||
351638e7 JP |
1733 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
1734 | struct net_device *dev) | |
1735 | { | |
51d0c047 DA |
1736 | struct netdev_notifier_info info = { |
1737 | .dev = dev, | |
1738 | }; | |
351638e7 | 1739 | |
351638e7 JP |
1740 | return nb->notifier_call(nb, val, &info); |
1741 | } | |
0187bdfb | 1742 | |
afa0df59 JP |
1743 | static int call_netdevice_register_notifiers(struct notifier_block *nb, |
1744 | struct net_device *dev) | |
1745 | { | |
1746 | int err; | |
1747 | ||
1748 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); | |
1749 | err = notifier_to_errno(err); | |
1750 | if (err) | |
1751 | return err; | |
1752 | ||
1753 | if (!(dev->flags & IFF_UP)) | |
1754 | return 0; | |
1755 | ||
1756 | call_netdevice_notifier(nb, NETDEV_UP, dev); | |
1757 | return 0; | |
1758 | } | |
1759 | ||
1760 | static void call_netdevice_unregister_notifiers(struct notifier_block *nb, | |
1761 | struct net_device *dev) | |
1762 | { | |
1763 | if (dev->flags & IFF_UP) { | |
1764 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, | |
1765 | dev); | |
1766 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
1767 | } | |
1768 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); | |
1769 | } | |
1770 | ||
1771 | static int call_netdevice_register_net_notifiers(struct notifier_block *nb, | |
1772 | struct net *net) | |
1773 | { | |
1774 | struct net_device *dev; | |
1775 | int err; | |
1776 | ||
1777 | for_each_netdev(net, dev) { | |
1778 | err = call_netdevice_register_notifiers(nb, dev); | |
1779 | if (err) | |
1780 | goto rollback; | |
1781 | } | |
1782 | return 0; | |
1783 | ||
1784 | rollback: | |
1785 | for_each_netdev_continue_reverse(net, dev) | |
1786 | call_netdevice_unregister_notifiers(nb, dev); | |
1787 | return err; | |
1788 | } | |
1789 | ||
1790 | static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb, | |
1791 | struct net *net) | |
1792 | { | |
1793 | struct net_device *dev; | |
1794 | ||
1795 | for_each_netdev(net, dev) | |
1796 | call_netdevice_unregister_notifiers(nb, dev); | |
1797 | } | |
1798 | ||
881d966b EB |
1799 | static int dev_boot_phase = 1; |
1800 | ||
1da177e4 | 1801 | /** |
722c9a0c | 1802 | * register_netdevice_notifier - register a network notifier block |
1803 | * @nb: notifier | |
1da177e4 | 1804 | * |
722c9a0c | 1805 | * Register a notifier to be called when network device events occur. |
1806 | * The notifier passed is linked into the kernel structures and must | |
1807 | * not be reused until it has been unregistered. A negative errno code | |
1808 | * is returned on a failure. | |
1da177e4 | 1809 | * |
722c9a0c | 1810 | * When registered all registration and up events are replayed |
1811 | * to the new notifier to allow device to have a race free | |
1812 | * view of the network device list. | |
1da177e4 LT |
1813 | */ |
1814 | ||
1815 | int register_netdevice_notifier(struct notifier_block *nb) | |
1816 | { | |
881d966b | 1817 | struct net *net; |
1da177e4 LT |
1818 | int err; |
1819 | ||
328fbe74 KT |
1820 | /* Close race with setup_net() and cleanup_net() */ |
1821 | down_write(&pernet_ops_rwsem); | |
1da177e4 | 1822 | rtnl_lock(); |
f07d5b94 | 1823 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1824 | if (err) |
1825 | goto unlock; | |
881d966b EB |
1826 | if (dev_boot_phase) |
1827 | goto unlock; | |
1828 | for_each_net(net) { | |
afa0df59 JP |
1829 | err = call_netdevice_register_net_notifiers(nb, net); |
1830 | if (err) | |
1831 | goto rollback; | |
1da177e4 | 1832 | } |
fcc5a03a HX |
1833 | |
1834 | unlock: | |
1da177e4 | 1835 | rtnl_unlock(); |
328fbe74 | 1836 | up_write(&pernet_ops_rwsem); |
1da177e4 | 1837 | return err; |
fcc5a03a HX |
1838 | |
1839 | rollback: | |
afa0df59 JP |
1840 | for_each_net_continue_reverse(net) |
1841 | call_netdevice_unregister_net_notifiers(nb, net); | |
c67625a1 PE |
1842 | |
1843 | raw_notifier_chain_unregister(&netdev_chain, nb); | |
fcc5a03a | 1844 | goto unlock; |
1da177e4 | 1845 | } |
d1b19dff | 1846 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1847 | |
1848 | /** | |
722c9a0c | 1849 | * unregister_netdevice_notifier - unregister a network notifier block |
1850 | * @nb: notifier | |
1da177e4 | 1851 | * |
722c9a0c | 1852 | * Unregister a notifier previously registered by |
1853 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1854 | * kernel structures and may then be reused. A negative errno code | |
1855 | * is returned on a failure. | |
7d3d43da | 1856 | * |
722c9a0c | 1857 | * After unregistering unregister and down device events are synthesized |
1858 | * for all devices on the device list to the removed notifier to remove | |
1859 | * the need for special case cleanup code. | |
1da177e4 LT |
1860 | */ |
1861 | ||
1862 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1863 | { | |
7d3d43da | 1864 | struct net *net; |
9f514950 HX |
1865 | int err; |
1866 | ||
328fbe74 KT |
1867 | /* Close race with setup_net() and cleanup_net() */ |
1868 | down_write(&pernet_ops_rwsem); | |
9f514950 | 1869 | rtnl_lock(); |
f07d5b94 | 1870 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1871 | if (err) |
1872 | goto unlock; | |
1873 | ||
48b3a137 JP |
1874 | for_each_net(net) |
1875 | call_netdevice_unregister_net_notifiers(nb, net); | |
1876 | ||
7d3d43da | 1877 | unlock: |
9f514950 | 1878 | rtnl_unlock(); |
328fbe74 | 1879 | up_write(&pernet_ops_rwsem); |
9f514950 | 1880 | return err; |
1da177e4 | 1881 | } |
d1b19dff | 1882 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 | 1883 | |
1f637703 JP |
1884 | static int __register_netdevice_notifier_net(struct net *net, |
1885 | struct notifier_block *nb, | |
1886 | bool ignore_call_fail) | |
1887 | { | |
1888 | int err; | |
1889 | ||
1890 | err = raw_notifier_chain_register(&net->netdev_chain, nb); | |
1891 | if (err) | |
1892 | return err; | |
1893 | if (dev_boot_phase) | |
1894 | return 0; | |
1895 | ||
1896 | err = call_netdevice_register_net_notifiers(nb, net); | |
1897 | if (err && !ignore_call_fail) | |
1898 | goto chain_unregister; | |
1899 | ||
1900 | return 0; | |
1901 | ||
1902 | chain_unregister: | |
1903 | raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1904 | return err; | |
1905 | } | |
1906 | ||
1907 | static int __unregister_netdevice_notifier_net(struct net *net, | |
1908 | struct notifier_block *nb) | |
1909 | { | |
1910 | int err; | |
1911 | ||
1912 | err = raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1913 | if (err) | |
1914 | return err; | |
1915 | ||
1916 | call_netdevice_unregister_net_notifiers(nb, net); | |
1917 | return 0; | |
1918 | } | |
1919 | ||
a30c7b42 JP |
1920 | /** |
1921 | * register_netdevice_notifier_net - register a per-netns network notifier block | |
1922 | * @net: network namespace | |
1923 | * @nb: notifier | |
1924 | * | |
1925 | * Register a notifier to be called when network device events occur. | |
1926 | * The notifier passed is linked into the kernel structures and must | |
1927 | * not be reused until it has been unregistered. A negative errno code | |
1928 | * is returned on a failure. | |
1929 | * | |
1930 | * When registered all registration and up events are replayed | |
1931 | * to the new notifier to allow device to have a race free | |
1932 | * view of the network device list. | |
1933 | */ | |
1934 | ||
1935 | int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb) | |
1936 | { | |
1937 | int err; | |
1938 | ||
1939 | rtnl_lock(); | |
1f637703 | 1940 | err = __register_netdevice_notifier_net(net, nb, false); |
a30c7b42 JP |
1941 | rtnl_unlock(); |
1942 | return err; | |
a30c7b42 JP |
1943 | } |
1944 | EXPORT_SYMBOL(register_netdevice_notifier_net); | |
1945 | ||
1946 | /** | |
1947 | * unregister_netdevice_notifier_net - unregister a per-netns | |
1948 | * network notifier block | |
1949 | * @net: network namespace | |
1950 | * @nb: notifier | |
1951 | * | |
1952 | * Unregister a notifier previously registered by | |
1953 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1954 | * kernel structures and may then be reused. A negative errno code | |
1955 | * is returned on a failure. | |
1956 | * | |
1957 | * After unregistering unregister and down device events are synthesized | |
1958 | * for all devices on the device list to the removed notifier to remove | |
1959 | * the need for special case cleanup code. | |
1960 | */ | |
1961 | ||
1962 | int unregister_netdevice_notifier_net(struct net *net, | |
1963 | struct notifier_block *nb) | |
1964 | { | |
1965 | int err; | |
1966 | ||
1967 | rtnl_lock(); | |
1f637703 | 1968 | err = __unregister_netdevice_notifier_net(net, nb); |
a30c7b42 JP |
1969 | rtnl_unlock(); |
1970 | return err; | |
1971 | } | |
1972 | EXPORT_SYMBOL(unregister_netdevice_notifier_net); | |
a30c7b42 | 1973 | |
93642e14 JP |
1974 | int register_netdevice_notifier_dev_net(struct net_device *dev, |
1975 | struct notifier_block *nb, | |
1976 | struct netdev_net_notifier *nn) | |
1977 | { | |
1978 | int err; | |
a30c7b42 | 1979 | |
93642e14 JP |
1980 | rtnl_lock(); |
1981 | err = __register_netdevice_notifier_net(dev_net(dev), nb, false); | |
1982 | if (!err) { | |
1983 | nn->nb = nb; | |
1984 | list_add(&nn->list, &dev->net_notifier_list); | |
1985 | } | |
a30c7b42 JP |
1986 | rtnl_unlock(); |
1987 | return err; | |
1988 | } | |
93642e14 JP |
1989 | EXPORT_SYMBOL(register_netdevice_notifier_dev_net); |
1990 | ||
1991 | int unregister_netdevice_notifier_dev_net(struct net_device *dev, | |
1992 | struct notifier_block *nb, | |
1993 | struct netdev_net_notifier *nn) | |
1994 | { | |
1995 | int err; | |
1996 | ||
1997 | rtnl_lock(); | |
1998 | list_del(&nn->list); | |
1999 | err = __unregister_netdevice_notifier_net(dev_net(dev), nb); | |
2000 | rtnl_unlock(); | |
2001 | return err; | |
2002 | } | |
2003 | EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net); | |
2004 | ||
2005 | static void move_netdevice_notifiers_dev_net(struct net_device *dev, | |
2006 | struct net *net) | |
2007 | { | |
2008 | struct netdev_net_notifier *nn; | |
2009 | ||
2010 | list_for_each_entry(nn, &dev->net_notifier_list, list) { | |
2011 | __unregister_netdevice_notifier_net(dev_net(dev), nn->nb); | |
2012 | __register_netdevice_notifier_net(net, nn->nb, true); | |
2013 | } | |
2014 | } | |
a30c7b42 | 2015 | |
351638e7 JP |
2016 | /** |
2017 | * call_netdevice_notifiers_info - call all network notifier blocks | |
2018 | * @val: value passed unmodified to notifier function | |
351638e7 JP |
2019 | * @info: notifier information data |
2020 | * | |
2021 | * Call all network notifier blocks. Parameters and return value | |
2022 | * are as for raw_notifier_call_chain(). | |
2023 | */ | |
2024 | ||
1d143d9f | 2025 | static int call_netdevice_notifiers_info(unsigned long val, |
1d143d9f | 2026 | struct netdev_notifier_info *info) |
351638e7 | 2027 | { |
a30c7b42 JP |
2028 | struct net *net = dev_net(info->dev); |
2029 | int ret; | |
2030 | ||
351638e7 | 2031 | ASSERT_RTNL(); |
a30c7b42 JP |
2032 | |
2033 | /* Run per-netns notifier block chain first, then run the global one. | |
2034 | * Hopefully, one day, the global one is going to be removed after | |
2035 | * all notifier block registrators get converted to be per-netns. | |
2036 | */ | |
2037 | ret = raw_notifier_call_chain(&net->netdev_chain, val, info); | |
2038 | if (ret & NOTIFY_STOP_MASK) | |
2039 | return ret; | |
351638e7 JP |
2040 | return raw_notifier_call_chain(&netdev_chain, val, info); |
2041 | } | |
351638e7 | 2042 | |
26372605 PM |
2043 | static int call_netdevice_notifiers_extack(unsigned long val, |
2044 | struct net_device *dev, | |
2045 | struct netlink_ext_ack *extack) | |
2046 | { | |
2047 | struct netdev_notifier_info info = { | |
2048 | .dev = dev, | |
2049 | .extack = extack, | |
2050 | }; | |
2051 | ||
2052 | return call_netdevice_notifiers_info(val, &info); | |
2053 | } | |
2054 | ||
1da177e4 LT |
2055 | /** |
2056 | * call_netdevice_notifiers - call all network notifier blocks | |
2057 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 2058 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
2059 | * |
2060 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 2061 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
2062 | */ |
2063 | ||
ad7379d4 | 2064 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 2065 | { |
26372605 | 2066 | return call_netdevice_notifiers_extack(val, dev, NULL); |
1da177e4 | 2067 | } |
edf947f1 | 2068 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 2069 | |
af7d6cce SD |
2070 | /** |
2071 | * call_netdevice_notifiers_mtu - call all network notifier blocks | |
2072 | * @val: value passed unmodified to notifier function | |
2073 | * @dev: net_device pointer passed unmodified to notifier function | |
2074 | * @arg: additional u32 argument passed to the notifier function | |
2075 | * | |
2076 | * Call all network notifier blocks. Parameters and return value | |
2077 | * are as for raw_notifier_call_chain(). | |
2078 | */ | |
2079 | static int call_netdevice_notifiers_mtu(unsigned long val, | |
2080 | struct net_device *dev, u32 arg) | |
2081 | { | |
2082 | struct netdev_notifier_info_ext info = { | |
2083 | .info.dev = dev, | |
2084 | .ext.mtu = arg, | |
2085 | }; | |
2086 | ||
2087 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); | |
2088 | ||
2089 | return call_netdevice_notifiers_info(val, &info.info); | |
2090 | } | |
2091 | ||
1cf51900 | 2092 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 2093 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
4577139b DB |
2094 | |
2095 | void net_inc_ingress_queue(void) | |
2096 | { | |
aabf6772 | 2097 | static_branch_inc(&ingress_needed_key); |
4577139b DB |
2098 | } |
2099 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); | |
2100 | ||
2101 | void net_dec_ingress_queue(void) | |
2102 | { | |
aabf6772 | 2103 | static_branch_dec(&ingress_needed_key); |
4577139b DB |
2104 | } |
2105 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); | |
2106 | #endif | |
2107 | ||
1f211a1b | 2108 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 2109 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
1f211a1b DB |
2110 | |
2111 | void net_inc_egress_queue(void) | |
2112 | { | |
aabf6772 | 2113 | static_branch_inc(&egress_needed_key); |
1f211a1b DB |
2114 | } |
2115 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); | |
2116 | ||
2117 | void net_dec_egress_queue(void) | |
2118 | { | |
aabf6772 | 2119 | static_branch_dec(&egress_needed_key); |
1f211a1b DB |
2120 | } |
2121 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); | |
2122 | #endif | |
2123 | ||
39e83922 | 2124 | static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
e9666d10 | 2125 | #ifdef CONFIG_JUMP_LABEL |
b90e5794 | 2126 | static atomic_t netstamp_needed_deferred; |
13baa00a | 2127 | static atomic_t netstamp_wanted; |
5fa8bbda | 2128 | static void netstamp_clear(struct work_struct *work) |
1da177e4 | 2129 | { |
b90e5794 | 2130 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
13baa00a | 2131 | int wanted; |
b90e5794 | 2132 | |
13baa00a ED |
2133 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
2134 | if (wanted > 0) | |
39e83922 | 2135 | static_branch_enable(&netstamp_needed_key); |
13baa00a | 2136 | else |
39e83922 | 2137 | static_branch_disable(&netstamp_needed_key); |
5fa8bbda ED |
2138 | } |
2139 | static DECLARE_WORK(netstamp_work, netstamp_clear); | |
b90e5794 | 2140 | #endif |
5fa8bbda ED |
2141 | |
2142 | void net_enable_timestamp(void) | |
2143 | { | |
e9666d10 | 2144 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2145 | int wanted; |
2146 | ||
2147 | while (1) { | |
2148 | wanted = atomic_read(&netstamp_wanted); | |
2149 | if (wanted <= 0) | |
2150 | break; | |
2151 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted) | |
2152 | return; | |
2153 | } | |
2154 | atomic_inc(&netstamp_needed_deferred); | |
2155 | schedule_work(&netstamp_work); | |
2156 | #else | |
39e83922 | 2157 | static_branch_inc(&netstamp_needed_key); |
13baa00a | 2158 | #endif |
1da177e4 | 2159 | } |
d1b19dff | 2160 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
2161 | |
2162 | void net_disable_timestamp(void) | |
2163 | { | |
e9666d10 | 2164 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2165 | int wanted; |
2166 | ||
2167 | while (1) { | |
2168 | wanted = atomic_read(&netstamp_wanted); | |
2169 | if (wanted <= 1) | |
2170 | break; | |
2171 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted) | |
2172 | return; | |
2173 | } | |
2174 | atomic_dec(&netstamp_needed_deferred); | |
5fa8bbda ED |
2175 | schedule_work(&netstamp_work); |
2176 | #else | |
39e83922 | 2177 | static_branch_dec(&netstamp_needed_key); |
5fa8bbda | 2178 | #endif |
1da177e4 | 2179 | } |
d1b19dff | 2180 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 2181 | |
3b098e2d | 2182 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 2183 | { |
2456e855 | 2184 | skb->tstamp = 0; |
39e83922 | 2185 | if (static_branch_unlikely(&netstamp_needed_key)) |
a61bbcf2 | 2186 | __net_timestamp(skb); |
1da177e4 LT |
2187 | } |
2188 | ||
39e83922 DB |
2189 | #define net_timestamp_check(COND, SKB) \ |
2190 | if (static_branch_unlikely(&netstamp_needed_key)) { \ | |
2191 | if ((COND) && !(SKB)->tstamp) \ | |
2192 | __net_timestamp(SKB); \ | |
2193 | } \ | |
3b098e2d | 2194 | |
f4b05d27 | 2195 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
79b569f0 DL |
2196 | { |
2197 | unsigned int len; | |
2198 | ||
2199 | if (!(dev->flags & IFF_UP)) | |
2200 | return false; | |
2201 | ||
2202 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; | |
2203 | if (skb->len <= len) | |
2204 | return true; | |
2205 | ||
2206 | /* if TSO is enabled, we don't care about the length as the packet | |
2207 | * could be forwarded without being segmented before | |
2208 | */ | |
2209 | if (skb_is_gso(skb)) | |
2210 | return true; | |
2211 | ||
2212 | return false; | |
2213 | } | |
1ee481fb | 2214 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
79b569f0 | 2215 | |
a0265d28 HX |
2216 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
2217 | { | |
4e3264d2 | 2218 | int ret = ____dev_forward_skb(dev, skb); |
a0265d28 | 2219 | |
4e3264d2 MKL |
2220 | if (likely(!ret)) { |
2221 | skb->protocol = eth_type_trans(skb, dev); | |
2222 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
2223 | } | |
a0265d28 | 2224 | |
4e3264d2 | 2225 | return ret; |
a0265d28 HX |
2226 | } |
2227 | EXPORT_SYMBOL_GPL(__dev_forward_skb); | |
2228 | ||
44540960 AB |
2229 | /** |
2230 | * dev_forward_skb - loopback an skb to another netif | |
2231 | * | |
2232 | * @dev: destination network device | |
2233 | * @skb: buffer to forward | |
2234 | * | |
2235 | * return values: | |
2236 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 2237 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
2238 | * |
2239 | * dev_forward_skb can be used for injecting an skb from the | |
2240 | * start_xmit function of one device into the receive queue | |
2241 | * of another device. | |
2242 | * | |
2243 | * The receiving device may be in another namespace, so | |
2244 | * we have to clear all information in the skb that could | |
2245 | * impact namespace isolation. | |
2246 | */ | |
2247 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2248 | { | |
a0265d28 | 2249 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
44540960 AB |
2250 | } |
2251 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
2252 | ||
71d9dec2 CG |
2253 | static inline int deliver_skb(struct sk_buff *skb, |
2254 | struct packet_type *pt_prev, | |
2255 | struct net_device *orig_dev) | |
2256 | { | |
1f8b977a | 2257 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
1080e512 | 2258 | return -ENOMEM; |
63354797 | 2259 | refcount_inc(&skb->users); |
71d9dec2 CG |
2260 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
2261 | } | |
2262 | ||
7866a621 SN |
2263 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
2264 | struct packet_type **pt, | |
fbcb2170 JP |
2265 | struct net_device *orig_dev, |
2266 | __be16 type, | |
7866a621 SN |
2267 | struct list_head *ptype_list) |
2268 | { | |
2269 | struct packet_type *ptype, *pt_prev = *pt; | |
2270 | ||
2271 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
2272 | if (ptype->type != type) | |
2273 | continue; | |
2274 | if (pt_prev) | |
fbcb2170 | 2275 | deliver_skb(skb, pt_prev, orig_dev); |
7866a621 SN |
2276 | pt_prev = ptype; |
2277 | } | |
2278 | *pt = pt_prev; | |
2279 | } | |
2280 | ||
c0de08d0 EL |
2281 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
2282 | { | |
a3d744e9 | 2283 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
2284 | return false; |
2285 | ||
2286 | if (ptype->id_match) | |
2287 | return ptype->id_match(ptype, skb->sk); | |
2288 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
2289 | return true; | |
2290 | ||
2291 | return false; | |
2292 | } | |
2293 | ||
9f9a742d MR |
2294 | /** |
2295 | * dev_nit_active - return true if any network interface taps are in use | |
2296 | * | |
2297 | * @dev: network device to check for the presence of taps | |
2298 | */ | |
2299 | bool dev_nit_active(struct net_device *dev) | |
2300 | { | |
2301 | return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all); | |
2302 | } | |
2303 | EXPORT_SYMBOL_GPL(dev_nit_active); | |
2304 | ||
1da177e4 LT |
2305 | /* |
2306 | * Support routine. Sends outgoing frames to any network | |
2307 | * taps currently in use. | |
2308 | */ | |
2309 | ||
74b20582 | 2310 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
2311 | { |
2312 | struct packet_type *ptype; | |
71d9dec2 CG |
2313 | struct sk_buff *skb2 = NULL; |
2314 | struct packet_type *pt_prev = NULL; | |
7866a621 | 2315 | struct list_head *ptype_list = &ptype_all; |
a61bbcf2 | 2316 | |
1da177e4 | 2317 | rcu_read_lock(); |
7866a621 SN |
2318 | again: |
2319 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
fa788d98 VW |
2320 | if (ptype->ignore_outgoing) |
2321 | continue; | |
2322 | ||
1da177e4 LT |
2323 | /* Never send packets back to the socket |
2324 | * they originated from - MvS (miquels@drinkel.ow.org) | |
2325 | */ | |
7866a621 SN |
2326 | if (skb_loop_sk(ptype, skb)) |
2327 | continue; | |
71d9dec2 | 2328 | |
7866a621 SN |
2329 | if (pt_prev) { |
2330 | deliver_skb(skb2, pt_prev, skb->dev); | |
2331 | pt_prev = ptype; | |
2332 | continue; | |
2333 | } | |
1da177e4 | 2334 | |
7866a621 SN |
2335 | /* need to clone skb, done only once */ |
2336 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
2337 | if (!skb2) | |
2338 | goto out_unlock; | |
70978182 | 2339 | |
7866a621 | 2340 | net_timestamp_set(skb2); |
1da177e4 | 2341 | |
7866a621 SN |
2342 | /* skb->nh should be correctly |
2343 | * set by sender, so that the second statement is | |
2344 | * just protection against buggy protocols. | |
2345 | */ | |
2346 | skb_reset_mac_header(skb2); | |
2347 | ||
2348 | if (skb_network_header(skb2) < skb2->data || | |
2349 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { | |
2350 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", | |
2351 | ntohs(skb2->protocol), | |
2352 | dev->name); | |
2353 | skb_reset_network_header(skb2); | |
1da177e4 | 2354 | } |
7866a621 SN |
2355 | |
2356 | skb2->transport_header = skb2->network_header; | |
2357 | skb2->pkt_type = PACKET_OUTGOING; | |
2358 | pt_prev = ptype; | |
2359 | } | |
2360 | ||
2361 | if (ptype_list == &ptype_all) { | |
2362 | ptype_list = &dev->ptype_all; | |
2363 | goto again; | |
1da177e4 | 2364 | } |
7866a621 | 2365 | out_unlock: |
581fe0ea WB |
2366 | if (pt_prev) { |
2367 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) | |
2368 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
2369 | else | |
2370 | kfree_skb(skb2); | |
2371 | } | |
1da177e4 LT |
2372 | rcu_read_unlock(); |
2373 | } | |
74b20582 | 2374 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
1da177e4 | 2375 | |
2c53040f BH |
2376 | /** |
2377 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
2378 | * @dev: Network device |
2379 | * @txq: number of queues available | |
2380 | * | |
2381 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
2382 | * valid. To resolve this verify the tc mapping remains valid and if | |
2383 | * not NULL the mapping. With no priorities mapping to this | |
2384 | * offset/count pair it will no longer be used. In the worst case TC0 | |
2385 | * is invalid nothing can be done so disable priority mappings. If is | |
2386 | * expected that drivers will fix this mapping if they can before | |
2387 | * calling netif_set_real_num_tx_queues. | |
2388 | */ | |
bb134d22 | 2389 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
2390 | { |
2391 | int i; | |
2392 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2393 | ||
2394 | /* If TC0 is invalidated disable TC mapping */ | |
2395 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce | 2396 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
2397 | dev->num_tc = 0; |
2398 | return; | |
2399 | } | |
2400 | ||
2401 | /* Invalidated prio to tc mappings set to TC0 */ | |
2402 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
2403 | int q = netdev_get_prio_tc_map(dev, i); | |
2404 | ||
2405 | tc = &dev->tc_to_txq[q]; | |
2406 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce JP |
2407 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
2408 | i, q); | |
4f57c087 JF |
2409 | netdev_set_prio_tc_map(dev, i, 0); |
2410 | } | |
2411 | } | |
2412 | } | |
2413 | ||
8d059b0f AD |
2414 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
2415 | { | |
2416 | if (dev->num_tc) { | |
2417 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2418 | int i; | |
2419 | ||
ffcfe25b | 2420 | /* walk through the TCs and see if it falls into any of them */ |
8d059b0f AD |
2421 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
2422 | if ((txq - tc->offset) < tc->count) | |
2423 | return i; | |
2424 | } | |
2425 | ||
ffcfe25b | 2426 | /* didn't find it, just return -1 to indicate no match */ |
8d059b0f AD |
2427 | return -1; |
2428 | } | |
2429 | ||
2430 | return 0; | |
2431 | } | |
8a5f2166 | 2432 | EXPORT_SYMBOL(netdev_txq_to_tc); |
8d059b0f | 2433 | |
537c00de | 2434 | #ifdef CONFIG_XPS |
04157469 AN |
2435 | struct static_key xps_needed __read_mostly; |
2436 | EXPORT_SYMBOL(xps_needed); | |
2437 | struct static_key xps_rxqs_needed __read_mostly; | |
2438 | EXPORT_SYMBOL(xps_rxqs_needed); | |
537c00de AD |
2439 | static DEFINE_MUTEX(xps_map_mutex); |
2440 | #define xmap_dereference(P) \ | |
2441 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
2442 | ||
6234f874 AD |
2443 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
2444 | int tci, u16 index) | |
537c00de | 2445 | { |
10cdc3f3 AD |
2446 | struct xps_map *map = NULL; |
2447 | int pos; | |
537c00de | 2448 | |
10cdc3f3 | 2449 | if (dev_maps) |
80d19669 | 2450 | map = xmap_dereference(dev_maps->attr_map[tci]); |
6234f874 AD |
2451 | if (!map) |
2452 | return false; | |
537c00de | 2453 | |
6234f874 AD |
2454 | for (pos = map->len; pos--;) { |
2455 | if (map->queues[pos] != index) | |
2456 | continue; | |
2457 | ||
2458 | if (map->len > 1) { | |
2459 | map->queues[pos] = map->queues[--map->len]; | |
10cdc3f3 | 2460 | break; |
537c00de | 2461 | } |
6234f874 | 2462 | |
80d19669 | 2463 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
6234f874 AD |
2464 | kfree_rcu(map, rcu); |
2465 | return false; | |
537c00de AD |
2466 | } |
2467 | ||
6234f874 | 2468 | return true; |
10cdc3f3 AD |
2469 | } |
2470 | ||
6234f874 AD |
2471 | static bool remove_xps_queue_cpu(struct net_device *dev, |
2472 | struct xps_dev_maps *dev_maps, | |
2473 | int cpu, u16 offset, u16 count) | |
2474 | { | |
184c449f AD |
2475 | int num_tc = dev->num_tc ? : 1; |
2476 | bool active = false; | |
2477 | int tci; | |
6234f874 | 2478 | |
184c449f AD |
2479 | for (tci = cpu * num_tc; num_tc--; tci++) { |
2480 | int i, j; | |
2481 | ||
2482 | for (i = count, j = offset; i--; j++) { | |
6358d49a | 2483 | if (!remove_xps_queue(dev_maps, tci, j)) |
184c449f AD |
2484 | break; |
2485 | } | |
2486 | ||
2487 | active |= i < 0; | |
6234f874 AD |
2488 | } |
2489 | ||
184c449f | 2490 | return active; |
6234f874 AD |
2491 | } |
2492 | ||
867d0ad4 SD |
2493 | static void reset_xps_maps(struct net_device *dev, |
2494 | struct xps_dev_maps *dev_maps, | |
2495 | bool is_rxqs_map) | |
2496 | { | |
2497 | if (is_rxqs_map) { | |
2498 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); | |
2499 | RCU_INIT_POINTER(dev->xps_rxqs_map, NULL); | |
2500 | } else { | |
2501 | RCU_INIT_POINTER(dev->xps_cpus_map, NULL); | |
2502 | } | |
2503 | static_key_slow_dec_cpuslocked(&xps_needed); | |
2504 | kfree_rcu(dev_maps, rcu); | |
2505 | } | |
2506 | ||
80d19669 AN |
2507 | static void clean_xps_maps(struct net_device *dev, const unsigned long *mask, |
2508 | struct xps_dev_maps *dev_maps, unsigned int nr_ids, | |
2509 | u16 offset, u16 count, bool is_rxqs_map) | |
2510 | { | |
2511 | bool active = false; | |
2512 | int i, j; | |
2513 | ||
2514 | for (j = -1; j = netif_attrmask_next(j, mask, nr_ids), | |
2515 | j < nr_ids;) | |
2516 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, | |
2517 | count); | |
867d0ad4 SD |
2518 | if (!active) |
2519 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
80d19669 | 2520 | |
f28c020f SD |
2521 | if (!is_rxqs_map) { |
2522 | for (i = offset + (count - 1); count--; i--) { | |
2523 | netdev_queue_numa_node_write( | |
2524 | netdev_get_tx_queue(dev, i), | |
2525 | NUMA_NO_NODE); | |
80d19669 | 2526 | } |
80d19669 AN |
2527 | } |
2528 | } | |
2529 | ||
6234f874 AD |
2530 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
2531 | u16 count) | |
10cdc3f3 | 2532 | { |
80d19669 | 2533 | const unsigned long *possible_mask = NULL; |
10cdc3f3 | 2534 | struct xps_dev_maps *dev_maps; |
80d19669 | 2535 | unsigned int nr_ids; |
10cdc3f3 | 2536 | |
04157469 AN |
2537 | if (!static_key_false(&xps_needed)) |
2538 | return; | |
10cdc3f3 | 2539 | |
4d99f660 | 2540 | cpus_read_lock(); |
04157469 | 2541 | mutex_lock(&xps_map_mutex); |
10cdc3f3 | 2542 | |
04157469 AN |
2543 | if (static_key_false(&xps_rxqs_needed)) { |
2544 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2545 | if (dev_maps) { | |
2546 | nr_ids = dev->num_rx_queues; | |
2547 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, | |
2548 | offset, count, true); | |
2549 | } | |
537c00de AD |
2550 | } |
2551 | ||
80d19669 AN |
2552 | dev_maps = xmap_dereference(dev->xps_cpus_map); |
2553 | if (!dev_maps) | |
2554 | goto out_no_maps; | |
2555 | ||
2556 | if (num_possible_cpus() > 1) | |
2557 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2558 | nr_ids = nr_cpu_ids; | |
2559 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count, | |
2560 | false); | |
024e9679 | 2561 | |
537c00de AD |
2562 | out_no_maps: |
2563 | mutex_unlock(&xps_map_mutex); | |
4d99f660 | 2564 | cpus_read_unlock(); |
537c00de AD |
2565 | } |
2566 | ||
6234f874 AD |
2567 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
2568 | { | |
2569 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); | |
2570 | } | |
2571 | ||
80d19669 AN |
2572 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
2573 | u16 index, bool is_rxqs_map) | |
01c5f864 AD |
2574 | { |
2575 | struct xps_map *new_map; | |
2576 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
2577 | int i, pos; | |
2578 | ||
2579 | for (pos = 0; map && pos < map->len; pos++) { | |
2580 | if (map->queues[pos] != index) | |
2581 | continue; | |
2582 | return map; | |
2583 | } | |
2584 | ||
80d19669 | 2585 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
01c5f864 AD |
2586 | if (map) { |
2587 | if (pos < map->alloc_len) | |
2588 | return map; | |
2589 | ||
2590 | alloc_len = map->alloc_len * 2; | |
2591 | } | |
2592 | ||
80d19669 AN |
2593 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
2594 | * map | |
2595 | */ | |
2596 | if (is_rxqs_map) | |
2597 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); | |
2598 | else | |
2599 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
2600 | cpu_to_node(attr_index)); | |
01c5f864 AD |
2601 | if (!new_map) |
2602 | return NULL; | |
2603 | ||
2604 | for (i = 0; i < pos; i++) | |
2605 | new_map->queues[i] = map->queues[i]; | |
2606 | new_map->alloc_len = alloc_len; | |
2607 | new_map->len = pos; | |
2608 | ||
2609 | return new_map; | |
2610 | } | |
2611 | ||
4d99f660 | 2612 | /* Must be called under cpus_read_lock */ |
80d19669 AN |
2613 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
2614 | u16 index, bool is_rxqs_map) | |
537c00de | 2615 | { |
80d19669 | 2616 | const unsigned long *online_mask = NULL, *possible_mask = NULL; |
01c5f864 | 2617 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
80d19669 | 2618 | int i, j, tci, numa_node_id = -2; |
184c449f | 2619 | int maps_sz, num_tc = 1, tc = 0; |
537c00de | 2620 | struct xps_map *map, *new_map; |
01c5f864 | 2621 | bool active = false; |
80d19669 | 2622 | unsigned int nr_ids; |
537c00de | 2623 | |
184c449f | 2624 | if (dev->num_tc) { |
ffcfe25b | 2625 | /* Do not allow XPS on subordinate device directly */ |
184c449f | 2626 | num_tc = dev->num_tc; |
ffcfe25b AD |
2627 | if (num_tc < 0) |
2628 | return -EINVAL; | |
2629 | ||
2630 | /* If queue belongs to subordinate dev use its map */ | |
2631 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
2632 | ||
184c449f AD |
2633 | tc = netdev_txq_to_tc(dev, index); |
2634 | if (tc < 0) | |
2635 | return -EINVAL; | |
2636 | } | |
2637 | ||
537c00de | 2638 | mutex_lock(&xps_map_mutex); |
80d19669 AN |
2639 | if (is_rxqs_map) { |
2640 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); | |
2641 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2642 | nr_ids = dev->num_rx_queues; | |
2643 | } else { | |
2644 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); | |
2645 | if (num_possible_cpus() > 1) { | |
2646 | online_mask = cpumask_bits(cpu_online_mask); | |
2647 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2648 | } | |
2649 | dev_maps = xmap_dereference(dev->xps_cpus_map); | |
2650 | nr_ids = nr_cpu_ids; | |
2651 | } | |
537c00de | 2652 | |
80d19669 AN |
2653 | if (maps_sz < L1_CACHE_BYTES) |
2654 | maps_sz = L1_CACHE_BYTES; | |
537c00de | 2655 | |
01c5f864 | 2656 | /* allocate memory for queue storage */ |
80d19669 AN |
2657 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
2658 | j < nr_ids;) { | |
01c5f864 AD |
2659 | if (!new_dev_maps) |
2660 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); | |
2bb60cb9 AD |
2661 | if (!new_dev_maps) { |
2662 | mutex_unlock(&xps_map_mutex); | |
01c5f864 | 2663 | return -ENOMEM; |
2bb60cb9 | 2664 | } |
01c5f864 | 2665 | |
80d19669 AN |
2666 | tci = j * num_tc + tc; |
2667 | map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) : | |
01c5f864 AD |
2668 | NULL; |
2669 | ||
80d19669 | 2670 | map = expand_xps_map(map, j, index, is_rxqs_map); |
01c5f864 AD |
2671 | if (!map) |
2672 | goto error; | |
2673 | ||
80d19669 | 2674 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
01c5f864 AD |
2675 | } |
2676 | ||
2677 | if (!new_dev_maps) | |
2678 | goto out_no_new_maps; | |
2679 | ||
867d0ad4 SD |
2680 | if (!dev_maps) { |
2681 | /* Increment static keys at most once per type */ | |
2682 | static_key_slow_inc_cpuslocked(&xps_needed); | |
2683 | if (is_rxqs_map) | |
2684 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); | |
2685 | } | |
04157469 | 2686 | |
80d19669 AN |
2687 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2688 | j < nr_ids;) { | |
184c449f | 2689 | /* copy maps belonging to foreign traffic classes */ |
80d19669 | 2690 | for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) { |
184c449f | 2691 | /* fill in the new device map from the old device map */ |
80d19669 AN |
2692 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2693 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f AD |
2694 | } |
2695 | ||
2696 | /* We need to explicitly update tci as prevous loop | |
2697 | * could break out early if dev_maps is NULL. | |
2698 | */ | |
80d19669 | 2699 | tci = j * num_tc + tc; |
184c449f | 2700 | |
80d19669 AN |
2701 | if (netif_attr_test_mask(j, mask, nr_ids) && |
2702 | netif_attr_test_online(j, online_mask, nr_ids)) { | |
2703 | /* add tx-queue to CPU/rx-queue maps */ | |
01c5f864 AD |
2704 | int pos = 0; |
2705 | ||
80d19669 | 2706 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
01c5f864 AD |
2707 | while ((pos < map->len) && (map->queues[pos] != index)) |
2708 | pos++; | |
2709 | ||
2710 | if (pos == map->len) | |
2711 | map->queues[map->len++] = index; | |
537c00de | 2712 | #ifdef CONFIG_NUMA |
80d19669 AN |
2713 | if (!is_rxqs_map) { |
2714 | if (numa_node_id == -2) | |
2715 | numa_node_id = cpu_to_node(j); | |
2716 | else if (numa_node_id != cpu_to_node(j)) | |
2717 | numa_node_id = -1; | |
2718 | } | |
537c00de | 2719 | #endif |
01c5f864 AD |
2720 | } else if (dev_maps) { |
2721 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2722 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2723 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
537c00de | 2724 | } |
01c5f864 | 2725 | |
184c449f AD |
2726 | /* copy maps belonging to foreign traffic classes */ |
2727 | for (i = num_tc - tc, tci++; dev_maps && --i; tci++) { | |
2728 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2729 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2730 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f | 2731 | } |
537c00de AD |
2732 | } |
2733 | ||
80d19669 AN |
2734 | if (is_rxqs_map) |
2735 | rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps); | |
2736 | else | |
2737 | rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps); | |
01c5f864 | 2738 | |
537c00de | 2739 | /* Cleanup old maps */ |
184c449f AD |
2740 | if (!dev_maps) |
2741 | goto out_no_old_maps; | |
2742 | ||
80d19669 AN |
2743 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2744 | j < nr_ids;) { | |
2745 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2746 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
2747 | map = xmap_dereference(dev_maps->attr_map[tci]); | |
01c5f864 AD |
2748 | if (map && map != new_map) |
2749 | kfree_rcu(map, rcu); | |
2750 | } | |
537c00de AD |
2751 | } |
2752 | ||
184c449f AD |
2753 | kfree_rcu(dev_maps, rcu); |
2754 | ||
2755 | out_no_old_maps: | |
01c5f864 AD |
2756 | dev_maps = new_dev_maps; |
2757 | active = true; | |
537c00de | 2758 | |
01c5f864 | 2759 | out_no_new_maps: |
80d19669 AN |
2760 | if (!is_rxqs_map) { |
2761 | /* update Tx queue numa node */ | |
2762 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), | |
2763 | (numa_node_id >= 0) ? | |
2764 | numa_node_id : NUMA_NO_NODE); | |
2765 | } | |
537c00de | 2766 | |
01c5f864 AD |
2767 | if (!dev_maps) |
2768 | goto out_no_maps; | |
2769 | ||
80d19669 AN |
2770 | /* removes tx-queue from unused CPUs/rx-queues */ |
2771 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), | |
2772 | j < nr_ids;) { | |
2773 | for (i = tc, tci = j * num_tc; i--; tci++) | |
184c449f | 2774 | active |= remove_xps_queue(dev_maps, tci, index); |
80d19669 AN |
2775 | if (!netif_attr_test_mask(j, mask, nr_ids) || |
2776 | !netif_attr_test_online(j, online_mask, nr_ids)) | |
184c449f AD |
2777 | active |= remove_xps_queue(dev_maps, tci, index); |
2778 | for (i = num_tc - tc, tci++; --i; tci++) | |
2779 | active |= remove_xps_queue(dev_maps, tci, index); | |
01c5f864 AD |
2780 | } |
2781 | ||
2782 | /* free map if not active */ | |
867d0ad4 SD |
2783 | if (!active) |
2784 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
01c5f864 AD |
2785 | |
2786 | out_no_maps: | |
537c00de AD |
2787 | mutex_unlock(&xps_map_mutex); |
2788 | ||
2789 | return 0; | |
2790 | error: | |
01c5f864 | 2791 | /* remove any maps that we added */ |
80d19669 AN |
2792 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2793 | j < nr_ids;) { | |
2794 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2795 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
184c449f | 2796 | map = dev_maps ? |
80d19669 | 2797 | xmap_dereference(dev_maps->attr_map[tci]) : |
184c449f AD |
2798 | NULL; |
2799 | if (new_map && new_map != map) | |
2800 | kfree(new_map); | |
2801 | } | |
01c5f864 AD |
2802 | } |
2803 | ||
537c00de AD |
2804 | mutex_unlock(&xps_map_mutex); |
2805 | ||
537c00de AD |
2806 | kfree(new_dev_maps); |
2807 | return -ENOMEM; | |
2808 | } | |
4d99f660 | 2809 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
80d19669 AN |
2810 | |
2811 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, | |
2812 | u16 index) | |
2813 | { | |
4d99f660 AV |
2814 | int ret; |
2815 | ||
2816 | cpus_read_lock(); | |
2817 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false); | |
2818 | cpus_read_unlock(); | |
2819 | ||
2820 | return ret; | |
80d19669 | 2821 | } |
537c00de AD |
2822 | EXPORT_SYMBOL(netif_set_xps_queue); |
2823 | ||
2824 | #endif | |
ffcfe25b AD |
2825 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
2826 | { | |
2827 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2828 | ||
2829 | /* Unbind any subordinate channels */ | |
2830 | while (txq-- != &dev->_tx[0]) { | |
2831 | if (txq->sb_dev) | |
2832 | netdev_unbind_sb_channel(dev, txq->sb_dev); | |
2833 | } | |
2834 | } | |
2835 | ||
9cf1f6a8 AD |
2836 | void netdev_reset_tc(struct net_device *dev) |
2837 | { | |
6234f874 AD |
2838 | #ifdef CONFIG_XPS |
2839 | netif_reset_xps_queues_gt(dev, 0); | |
2840 | #endif | |
ffcfe25b AD |
2841 | netdev_unbind_all_sb_channels(dev); |
2842 | ||
2843 | /* Reset TC configuration of device */ | |
9cf1f6a8 AD |
2844 | dev->num_tc = 0; |
2845 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); | |
2846 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); | |
2847 | } | |
2848 | EXPORT_SYMBOL(netdev_reset_tc); | |
2849 | ||
2850 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) | |
2851 | { | |
2852 | if (tc >= dev->num_tc) | |
2853 | return -EINVAL; | |
2854 | ||
6234f874 AD |
2855 | #ifdef CONFIG_XPS |
2856 | netif_reset_xps_queues(dev, offset, count); | |
2857 | #endif | |
9cf1f6a8 AD |
2858 | dev->tc_to_txq[tc].count = count; |
2859 | dev->tc_to_txq[tc].offset = offset; | |
2860 | return 0; | |
2861 | } | |
2862 | EXPORT_SYMBOL(netdev_set_tc_queue); | |
2863 | ||
2864 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) | |
2865 | { | |
2866 | if (num_tc > TC_MAX_QUEUE) | |
2867 | return -EINVAL; | |
2868 | ||
6234f874 AD |
2869 | #ifdef CONFIG_XPS |
2870 | netif_reset_xps_queues_gt(dev, 0); | |
2871 | #endif | |
ffcfe25b AD |
2872 | netdev_unbind_all_sb_channels(dev); |
2873 | ||
9cf1f6a8 AD |
2874 | dev->num_tc = num_tc; |
2875 | return 0; | |
2876 | } | |
2877 | EXPORT_SYMBOL(netdev_set_num_tc); | |
2878 | ||
ffcfe25b AD |
2879 | void netdev_unbind_sb_channel(struct net_device *dev, |
2880 | struct net_device *sb_dev) | |
2881 | { | |
2882 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2883 | ||
2884 | #ifdef CONFIG_XPS | |
2885 | netif_reset_xps_queues_gt(sb_dev, 0); | |
2886 | #endif | |
2887 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); | |
2888 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); | |
2889 | ||
2890 | while (txq-- != &dev->_tx[0]) { | |
2891 | if (txq->sb_dev == sb_dev) | |
2892 | txq->sb_dev = NULL; | |
2893 | } | |
2894 | } | |
2895 | EXPORT_SYMBOL(netdev_unbind_sb_channel); | |
2896 | ||
2897 | int netdev_bind_sb_channel_queue(struct net_device *dev, | |
2898 | struct net_device *sb_dev, | |
2899 | u8 tc, u16 count, u16 offset) | |
2900 | { | |
2901 | /* Make certain the sb_dev and dev are already configured */ | |
2902 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) | |
2903 | return -EINVAL; | |
2904 | ||
2905 | /* We cannot hand out queues we don't have */ | |
2906 | if ((offset + count) > dev->real_num_tx_queues) | |
2907 | return -EINVAL; | |
2908 | ||
2909 | /* Record the mapping */ | |
2910 | sb_dev->tc_to_txq[tc].count = count; | |
2911 | sb_dev->tc_to_txq[tc].offset = offset; | |
2912 | ||
2913 | /* Provide a way for Tx queue to find the tc_to_txq map or | |
2914 | * XPS map for itself. | |
2915 | */ | |
2916 | while (count--) | |
2917 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; | |
2918 | ||
2919 | return 0; | |
2920 | } | |
2921 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); | |
2922 | ||
2923 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) | |
2924 | { | |
2925 | /* Do not use a multiqueue device to represent a subordinate channel */ | |
2926 | if (netif_is_multiqueue(dev)) | |
2927 | return -ENODEV; | |
2928 | ||
2929 | /* We allow channels 1 - 32767 to be used for subordinate channels. | |
2930 | * Channel 0 is meant to be "native" mode and used only to represent | |
2931 | * the main root device. We allow writing 0 to reset the device back | |
2932 | * to normal mode after being used as a subordinate channel. | |
2933 | */ | |
2934 | if (channel > S16_MAX) | |
2935 | return -EINVAL; | |
2936 | ||
2937 | dev->num_tc = -channel; | |
2938 | ||
2939 | return 0; | |
2940 | } | |
2941 | EXPORT_SYMBOL(netdev_set_sb_channel); | |
2942 | ||
f0796d5c JF |
2943 | /* |
2944 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
3a053b1a | 2945 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
f0796d5c | 2946 | */ |
e6484930 | 2947 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2948 | { |
ac5b7019 | 2949 | bool disabling; |
1d24eb48 TH |
2950 | int rc; |
2951 | ||
ac5b7019 JK |
2952 | disabling = txq < dev->real_num_tx_queues; |
2953 | ||
e6484930 TH |
2954 | if (txq < 1 || txq > dev->num_tx_queues) |
2955 | return -EINVAL; | |
f0796d5c | 2956 | |
5c56580b BH |
2957 | if (dev->reg_state == NETREG_REGISTERED || |
2958 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2959 | ASSERT_RTNL(); |
2960 | ||
1d24eb48 TH |
2961 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2962 | txq); | |
bf264145 TH |
2963 | if (rc) |
2964 | return rc; | |
2965 | ||
4f57c087 JF |
2966 | if (dev->num_tc) |
2967 | netif_setup_tc(dev, txq); | |
2968 | ||
ac5b7019 JK |
2969 | dev->real_num_tx_queues = txq; |
2970 | ||
2971 | if (disabling) { | |
2972 | synchronize_net(); | |
e6484930 | 2973 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2974 | #ifdef CONFIG_XPS |
2975 | netif_reset_xps_queues_gt(dev, txq); | |
2976 | #endif | |
2977 | } | |
ac5b7019 JK |
2978 | } else { |
2979 | dev->real_num_tx_queues = txq; | |
f0796d5c | 2980 | } |
e6484930 | 2981 | |
e6484930 | 2982 | return 0; |
f0796d5c JF |
2983 | } |
2984 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2985 | |
a953be53 | 2986 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
2987 | /** |
2988 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2989 | * @dev: Network device | |
2990 | * @rxq: Actual number of RX queues | |
2991 | * | |
2992 | * This must be called either with the rtnl_lock held or before | |
2993 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2994 | * negative error code. If called before registration, it always |
2995 | * succeeds. | |
62fe0b40 BH |
2996 | */ |
2997 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2998 | { | |
2999 | int rc; | |
3000 | ||
bd25fa7b TH |
3001 | if (rxq < 1 || rxq > dev->num_rx_queues) |
3002 | return -EINVAL; | |
3003 | ||
62fe0b40 BH |
3004 | if (dev->reg_state == NETREG_REGISTERED) { |
3005 | ASSERT_RTNL(); | |
3006 | ||
62fe0b40 BH |
3007 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
3008 | rxq); | |
3009 | if (rc) | |
3010 | return rc; | |
62fe0b40 BH |
3011 | } |
3012 | ||
3013 | dev->real_num_rx_queues = rxq; | |
3014 | return 0; | |
3015 | } | |
3016 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
3017 | #endif | |
3018 | ||
2c53040f BH |
3019 | /** |
3020 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 YM |
3021 | * |
3022 | * This routine should set an upper limit on the number of RSS queues | |
3023 | * used by default by multiqueue devices. | |
3024 | */ | |
a55b138b | 3025 | int netif_get_num_default_rss_queues(void) |
16917b87 | 3026 | { |
40e4e713 HS |
3027 | return is_kdump_kernel() ? |
3028 | 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); | |
16917b87 YM |
3029 | } |
3030 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
3031 | ||
3bcb846c | 3032 | static void __netif_reschedule(struct Qdisc *q) |
56079431 | 3033 | { |
def82a1d JP |
3034 | struct softnet_data *sd; |
3035 | unsigned long flags; | |
56079431 | 3036 | |
def82a1d | 3037 | local_irq_save(flags); |
903ceff7 | 3038 | sd = this_cpu_ptr(&softnet_data); |
a9cbd588 CG |
3039 | q->next_sched = NULL; |
3040 | *sd->output_queue_tailp = q; | |
3041 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
3042 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
3043 | local_irq_restore(flags); | |
3044 | } | |
3045 | ||
3046 | void __netif_schedule(struct Qdisc *q) | |
3047 | { | |
3048 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
3049 | __netif_reschedule(q); | |
56079431 DV |
3050 | } |
3051 | EXPORT_SYMBOL(__netif_schedule); | |
3052 | ||
e6247027 ED |
3053 | struct dev_kfree_skb_cb { |
3054 | enum skb_free_reason reason; | |
3055 | }; | |
3056 | ||
3057 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) | |
56079431 | 3058 | { |
e6247027 ED |
3059 | return (struct dev_kfree_skb_cb *)skb->cb; |
3060 | } | |
3061 | ||
46e5da40 JF |
3062 | void netif_schedule_queue(struct netdev_queue *txq) |
3063 | { | |
3064 | rcu_read_lock(); | |
5be5515a | 3065 | if (!netif_xmit_stopped(txq)) { |
46e5da40 JF |
3066 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
3067 | ||
3068 | __netif_schedule(q); | |
3069 | } | |
3070 | rcu_read_unlock(); | |
3071 | } | |
3072 | EXPORT_SYMBOL(netif_schedule_queue); | |
3073 | ||
46e5da40 JF |
3074 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
3075 | { | |
3076 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { | |
3077 | struct Qdisc *q; | |
3078 | ||
3079 | rcu_read_lock(); | |
3080 | q = rcu_dereference(dev_queue->qdisc); | |
3081 | __netif_schedule(q); | |
3082 | rcu_read_unlock(); | |
3083 | } | |
3084 | } | |
3085 | EXPORT_SYMBOL(netif_tx_wake_queue); | |
3086 | ||
e6247027 | 3087 | void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 | 3088 | { |
e6247027 | 3089 | unsigned long flags; |
56079431 | 3090 | |
9899886d MJ |
3091 | if (unlikely(!skb)) |
3092 | return; | |
3093 | ||
63354797 | 3094 | if (likely(refcount_read(&skb->users) == 1)) { |
e6247027 | 3095 | smp_rmb(); |
63354797 RE |
3096 | refcount_set(&skb->users, 0); |
3097 | } else if (likely(!refcount_dec_and_test(&skb->users))) { | |
e6247027 | 3098 | return; |
bea3348e | 3099 | } |
e6247027 ED |
3100 | get_kfree_skb_cb(skb)->reason = reason; |
3101 | local_irq_save(flags); | |
3102 | skb->next = __this_cpu_read(softnet_data.completion_queue); | |
3103 | __this_cpu_write(softnet_data.completion_queue, skb); | |
3104 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
3105 | local_irq_restore(flags); | |
56079431 | 3106 | } |
e6247027 | 3107 | EXPORT_SYMBOL(__dev_kfree_skb_irq); |
56079431 | 3108 | |
e6247027 | 3109 | void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 DV |
3110 | { |
3111 | if (in_irq() || irqs_disabled()) | |
e6247027 | 3112 | __dev_kfree_skb_irq(skb, reason); |
56079431 DV |
3113 | else |
3114 | dev_kfree_skb(skb); | |
3115 | } | |
e6247027 | 3116 | EXPORT_SYMBOL(__dev_kfree_skb_any); |
56079431 DV |
3117 | |
3118 | ||
bea3348e SH |
3119 | /** |
3120 | * netif_device_detach - mark device as removed | |
3121 | * @dev: network device | |
3122 | * | |
3123 | * Mark device as removed from system and therefore no longer available. | |
3124 | */ | |
56079431 DV |
3125 | void netif_device_detach(struct net_device *dev) |
3126 | { | |
3127 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3128 | netif_running(dev)) { | |
d543103a | 3129 | netif_tx_stop_all_queues(dev); |
56079431 DV |
3130 | } |
3131 | } | |
3132 | EXPORT_SYMBOL(netif_device_detach); | |
3133 | ||
bea3348e SH |
3134 | /** |
3135 | * netif_device_attach - mark device as attached | |
3136 | * @dev: network device | |
3137 | * | |
3138 | * Mark device as attached from system and restart if needed. | |
3139 | */ | |
56079431 DV |
3140 | void netif_device_attach(struct net_device *dev) |
3141 | { | |
3142 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3143 | netif_running(dev)) { | |
d543103a | 3144 | netif_tx_wake_all_queues(dev); |
4ec93edb | 3145 | __netdev_watchdog_up(dev); |
56079431 DV |
3146 | } |
3147 | } | |
3148 | EXPORT_SYMBOL(netif_device_attach); | |
3149 | ||
5605c762 JP |
3150 | /* |
3151 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
3152 | * to be used as a distribution range. | |
3153 | */ | |
eadec877 AD |
3154 | static u16 skb_tx_hash(const struct net_device *dev, |
3155 | const struct net_device *sb_dev, | |
3156 | struct sk_buff *skb) | |
5605c762 JP |
3157 | { |
3158 | u32 hash; | |
3159 | u16 qoffset = 0; | |
1b837d48 | 3160 | u16 qcount = dev->real_num_tx_queues; |
5605c762 | 3161 | |
eadec877 AD |
3162 | if (dev->num_tc) { |
3163 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
3164 | ||
3165 | qoffset = sb_dev->tc_to_txq[tc].offset; | |
3166 | qcount = sb_dev->tc_to_txq[tc].count; | |
3167 | } | |
3168 | ||
5605c762 JP |
3169 | if (skb_rx_queue_recorded(skb)) { |
3170 | hash = skb_get_rx_queue(skb); | |
6e11d157 AN |
3171 | if (hash >= qoffset) |
3172 | hash -= qoffset; | |
1b837d48 AD |
3173 | while (unlikely(hash >= qcount)) |
3174 | hash -= qcount; | |
eadec877 | 3175 | return hash + qoffset; |
5605c762 JP |
3176 | } |
3177 | ||
3178 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; | |
3179 | } | |
5605c762 | 3180 | |
36c92474 BH |
3181 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
3182 | { | |
84d15ae5 | 3183 | static const netdev_features_t null_features; |
36c92474 | 3184 | struct net_device *dev = skb->dev; |
88ad4175 | 3185 | const char *name = ""; |
36c92474 | 3186 | |
c846ad9b BG |
3187 | if (!net_ratelimit()) |
3188 | return; | |
3189 | ||
88ad4175 BM |
3190 | if (dev) { |
3191 | if (dev->dev.parent) | |
3192 | name = dev_driver_string(dev->dev.parent); | |
3193 | else | |
3194 | name = netdev_name(dev); | |
3195 | } | |
6413139d WB |
3196 | skb_dump(KERN_WARNING, skb, false); |
3197 | WARN(1, "%s: caps=(%pNF, %pNF)\n", | |
88ad4175 | 3198 | name, dev ? &dev->features : &null_features, |
6413139d | 3199 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
36c92474 BH |
3200 | } |
3201 | ||
1da177e4 LT |
3202 | /* |
3203 | * Invalidate hardware checksum when packet is to be mangled, and | |
3204 | * complete checksum manually on outgoing path. | |
3205 | */ | |
84fa7933 | 3206 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 3207 | { |
d3bc23e7 | 3208 | __wsum csum; |
663ead3b | 3209 | int ret = 0, offset; |
1da177e4 | 3210 | |
84fa7933 | 3211 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
3212 | goto out_set_summed; |
3213 | ||
3aefd7d6 | 3214 | if (unlikely(skb_is_gso(skb))) { |
36c92474 BH |
3215 | skb_warn_bad_offload(skb); |
3216 | return -EINVAL; | |
1da177e4 LT |
3217 | } |
3218 | ||
cef401de ED |
3219 | /* Before computing a checksum, we should make sure no frag could |
3220 | * be modified by an external entity : checksum could be wrong. | |
3221 | */ | |
3222 | if (skb_has_shared_frag(skb)) { | |
3223 | ret = __skb_linearize(skb); | |
3224 | if (ret) | |
3225 | goto out; | |
3226 | } | |
3227 | ||
55508d60 | 3228 | offset = skb_checksum_start_offset(skb); |
a030847e HX |
3229 | BUG_ON(offset >= skb_headlen(skb)); |
3230 | csum = skb_checksum(skb, offset, skb->len - offset, 0); | |
3231 | ||
3232 | offset += skb->csum_offset; | |
3233 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); | |
3234 | ||
8211fbfa HK |
3235 | ret = skb_ensure_writable(skb, offset + sizeof(__sum16)); |
3236 | if (ret) | |
3237 | goto out; | |
1da177e4 | 3238 | |
4f2e4ad5 | 3239 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
a430a43d | 3240 | out_set_summed: |
1da177e4 | 3241 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 3242 | out: |
1da177e4 LT |
3243 | return ret; |
3244 | } | |
d1b19dff | 3245 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 3246 | |
b72b5bf6 DC |
3247 | int skb_crc32c_csum_help(struct sk_buff *skb) |
3248 | { | |
3249 | __le32 crc32c_csum; | |
3250 | int ret = 0, offset, start; | |
3251 | ||
3252 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
3253 | goto out; | |
3254 | ||
3255 | if (unlikely(skb_is_gso(skb))) | |
3256 | goto out; | |
3257 | ||
3258 | /* Before computing a checksum, we should make sure no frag could | |
3259 | * be modified by an external entity : checksum could be wrong. | |
3260 | */ | |
3261 | if (unlikely(skb_has_shared_frag(skb))) { | |
3262 | ret = __skb_linearize(skb); | |
3263 | if (ret) | |
3264 | goto out; | |
3265 | } | |
3266 | start = skb_checksum_start_offset(skb); | |
3267 | offset = start + offsetof(struct sctphdr, checksum); | |
3268 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { | |
3269 | ret = -EINVAL; | |
3270 | goto out; | |
3271 | } | |
8211fbfa HK |
3272 | |
3273 | ret = skb_ensure_writable(skb, offset + sizeof(__le32)); | |
3274 | if (ret) | |
3275 | goto out; | |
3276 | ||
b72b5bf6 DC |
3277 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, |
3278 | skb->len - start, ~(__u32)0, | |
3279 | crc32c_csum_stub)); | |
3280 | *(__le32 *)(skb->data + offset) = crc32c_csum; | |
3281 | skb->ip_summed = CHECKSUM_NONE; | |
dba00306 | 3282 | skb->csum_not_inet = 0; |
b72b5bf6 DC |
3283 | out: |
3284 | return ret; | |
3285 | } | |
3286 | ||
53d6471c | 3287 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
f6a78bfc | 3288 | { |
252e3346 | 3289 | __be16 type = skb->protocol; |
f6a78bfc | 3290 | |
19acc327 PS |
3291 | /* Tunnel gso handlers can set protocol to ethernet. */ |
3292 | if (type == htons(ETH_P_TEB)) { | |
3293 | struct ethhdr *eth; | |
3294 | ||
3295 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) | |
3296 | return 0; | |
3297 | ||
1dfe82eb | 3298 | eth = (struct ethhdr *)skb->data; |
19acc327 PS |
3299 | type = eth->h_proto; |
3300 | } | |
3301 | ||
d4bcef3f | 3302 | return __vlan_get_protocol(skb, type, depth); |
ec5f0615 PS |
3303 | } |
3304 | ||
3305 | /** | |
3306 | * skb_mac_gso_segment - mac layer segmentation handler. | |
3307 | * @skb: buffer to segment | |
3308 | * @features: features for the output path (see dev->features) | |
3309 | */ | |
3310 | struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb, | |
3311 | netdev_features_t features) | |
3312 | { | |
3313 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); | |
3314 | struct packet_offload *ptype; | |
53d6471c VY |
3315 | int vlan_depth = skb->mac_len; |
3316 | __be16 type = skb_network_protocol(skb, &vlan_depth); | |
ec5f0615 PS |
3317 | |
3318 | if (unlikely(!type)) | |
3319 | return ERR_PTR(-EINVAL); | |
3320 | ||
53d6471c | 3321 | __skb_pull(skb, vlan_depth); |
f6a78bfc HX |
3322 | |
3323 | rcu_read_lock(); | |
22061d80 | 3324 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
f191a1d1 | 3325 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
f191a1d1 | 3326 | segs = ptype->callbacks.gso_segment(skb, features); |
f6a78bfc HX |
3327 | break; |
3328 | } | |
3329 | } | |
3330 | rcu_read_unlock(); | |
3331 | ||
98e399f8 | 3332 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
576a30eb | 3333 | |
f6a78bfc HX |
3334 | return segs; |
3335 | } | |
05e8ef4a PS |
3336 | EXPORT_SYMBOL(skb_mac_gso_segment); |
3337 | ||
3338 | ||
3339 | /* openvswitch calls this on rx path, so we need a different check. | |
3340 | */ | |
3341 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) | |
3342 | { | |
3343 | if (tx_path) | |
0c19f846 WB |
3344 | return skb->ip_summed != CHECKSUM_PARTIAL && |
3345 | skb->ip_summed != CHECKSUM_UNNECESSARY; | |
6e7bc478 ED |
3346 | |
3347 | return skb->ip_summed == CHECKSUM_NONE; | |
05e8ef4a PS |
3348 | } |
3349 | ||
3350 | /** | |
3351 | * __skb_gso_segment - Perform segmentation on skb. | |
3352 | * @skb: buffer to segment | |
3353 | * @features: features for the output path (see dev->features) | |
3354 | * @tx_path: whether it is called in TX path | |
3355 | * | |
3356 | * This function segments the given skb and returns a list of segments. | |
3357 | * | |
3358 | * It may return NULL if the skb requires no segmentation. This is | |
3359 | * only possible when GSO is used for verifying header integrity. | |
9207f9d4 | 3360 | * |
a08e7fd9 | 3361 | * Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb. |
05e8ef4a PS |
3362 | */ |
3363 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, | |
3364 | netdev_features_t features, bool tx_path) | |
3365 | { | |
b2504a5d ED |
3366 | struct sk_buff *segs; |
3367 | ||
05e8ef4a PS |
3368 | if (unlikely(skb_needs_check(skb, tx_path))) { |
3369 | int err; | |
3370 | ||
b2504a5d | 3371 | /* We're going to init ->check field in TCP or UDP header */ |
a40e0a66 | 3372 | err = skb_cow_head(skb, 0); |
3373 | if (err < 0) | |
05e8ef4a PS |
3374 | return ERR_PTR(err); |
3375 | } | |
3376 | ||
802ab55a AD |
3377 | /* Only report GSO partial support if it will enable us to |
3378 | * support segmentation on this frame without needing additional | |
3379 | * work. | |
3380 | */ | |
3381 | if (features & NETIF_F_GSO_PARTIAL) { | |
3382 | netdev_features_t partial_features = NETIF_F_GSO_ROBUST; | |
3383 | struct net_device *dev = skb->dev; | |
3384 | ||
3385 | partial_features |= dev->features & dev->gso_partial_features; | |
3386 | if (!skb_gso_ok(skb, features | partial_features)) | |
3387 | features &= ~NETIF_F_GSO_PARTIAL; | |
3388 | } | |
3389 | ||
a08e7fd9 | 3390 | BUILD_BUG_ON(SKB_GSO_CB_OFFSET + |
9207f9d4 KK |
3391 | sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb)); |
3392 | ||
68c33163 | 3393 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
3347c960 ED |
3394 | SKB_GSO_CB(skb)->encap_level = 0; |
3395 | ||
05e8ef4a PS |
3396 | skb_reset_mac_header(skb); |
3397 | skb_reset_mac_len(skb); | |
3398 | ||
b2504a5d ED |
3399 | segs = skb_mac_gso_segment(skb, features); |
3400 | ||
3a1296a3 | 3401 | if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs))) |
b2504a5d ED |
3402 | skb_warn_bad_offload(skb); |
3403 | ||
3404 | return segs; | |
05e8ef4a | 3405 | } |
12b0004d | 3406 | EXPORT_SYMBOL(__skb_gso_segment); |
f6a78bfc | 3407 | |
fb286bb2 HX |
3408 | /* Take action when hardware reception checksum errors are detected. */ |
3409 | #ifdef CONFIG_BUG | |
7fe50ac8 | 3410 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
fb286bb2 HX |
3411 | { |
3412 | if (net_ratelimit()) { | |
7b6cd1ce | 3413 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
6413139d | 3414 | skb_dump(KERN_ERR, skb, true); |
fb286bb2 HX |
3415 | dump_stack(); |
3416 | } | |
3417 | } | |
3418 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
3419 | #endif | |
3420 | ||
ab74cfeb | 3421 | /* XXX: check that highmem exists at all on the given machine. */ |
c1e756bf | 3422 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 3423 | { |
3d3a8533 | 3424 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 3425 | int i; |
f4563a75 | 3426 | |
5acbbd42 | 3427 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
3428 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
3429 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
f4563a75 | 3430 | |
ea2ab693 | 3431 | if (PageHighMem(skb_frag_page(frag))) |
5acbbd42 | 3432 | return 1; |
ea2ab693 | 3433 | } |
5acbbd42 | 3434 | } |
3d3a8533 | 3435 | #endif |
1da177e4 LT |
3436 | return 0; |
3437 | } | |
1da177e4 | 3438 | |
3b392ddb SH |
3439 | /* If MPLS offload request, verify we are testing hardware MPLS features |
3440 | * instead of standard features for the netdev. | |
3441 | */ | |
d0edc7bf | 3442 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
3b392ddb SH |
3443 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
3444 | netdev_features_t features, | |
3445 | __be16 type) | |
3446 | { | |
25cd9ba0 | 3447 | if (eth_p_mpls(type)) |
3b392ddb SH |
3448 | features &= skb->dev->mpls_features; |
3449 | ||
3450 | return features; | |
3451 | } | |
3452 | #else | |
3453 | static netdev_features_t net_mpls_features(struct sk_buff *skb, | |
3454 | netdev_features_t features, | |
3455 | __be16 type) | |
3456 | { | |
3457 | return features; | |
3458 | } | |
3459 | #endif | |
3460 | ||
c8f44aff | 3461 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
c1e756bf | 3462 | netdev_features_t features) |
f01a5236 | 3463 | { |
3b392ddb SH |
3464 | __be16 type; |
3465 | ||
9fc95f50 | 3466 | type = skb_network_protocol(skb, NULL); |
3b392ddb | 3467 | features = net_mpls_features(skb, features, type); |
53d6471c | 3468 | |
c0d680e5 | 3469 | if (skb->ip_summed != CHECKSUM_NONE && |
3b392ddb | 3470 | !can_checksum_protocol(features, type)) { |
996e8021 | 3471 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
f01a5236 | 3472 | } |
7be2c82c ED |
3473 | if (illegal_highdma(skb->dev, skb)) |
3474 | features &= ~NETIF_F_SG; | |
f01a5236 JG |
3475 | |
3476 | return features; | |
3477 | } | |
3478 | ||
e38f3025 TM |
3479 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
3480 | struct net_device *dev, | |
3481 | netdev_features_t features) | |
3482 | { | |
3483 | return features; | |
3484 | } | |
3485 | EXPORT_SYMBOL(passthru_features_check); | |
3486 | ||
7ce23672 | 3487 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
8cb65d00 TM |
3488 | struct net_device *dev, |
3489 | netdev_features_t features) | |
3490 | { | |
3491 | return vlan_features_check(skb, features); | |
3492 | } | |
3493 | ||
cbc53e08 AD |
3494 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
3495 | struct net_device *dev, | |
3496 | netdev_features_t features) | |
3497 | { | |
3498 | u16 gso_segs = skb_shinfo(skb)->gso_segs; | |
3499 | ||
3500 | if (gso_segs > dev->gso_max_segs) | |
3501 | return features & ~NETIF_F_GSO_MASK; | |
3502 | ||
1d155dfd HK |
3503 | if (!skb_shinfo(skb)->gso_type) { |
3504 | skb_warn_bad_offload(skb); | |
3505 | return features & ~NETIF_F_GSO_MASK; | |
3506 | } | |
3507 | ||
802ab55a AD |
3508 | /* Support for GSO partial features requires software |
3509 | * intervention before we can actually process the packets | |
3510 | * so we need to strip support for any partial features now | |
3511 | * and we can pull them back in after we have partially | |
3512 | * segmented the frame. | |
3513 | */ | |
3514 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) | |
3515 | features &= ~dev->gso_partial_features; | |
3516 | ||
3517 | /* Make sure to clear the IPv4 ID mangling feature if the | |
3518 | * IPv4 header has the potential to be fragmented. | |
cbc53e08 AD |
3519 | */ |
3520 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
3521 | struct iphdr *iph = skb->encapsulation ? | |
3522 | inner_ip_hdr(skb) : ip_hdr(skb); | |
3523 | ||
3524 | if (!(iph->frag_off & htons(IP_DF))) | |
3525 | features &= ~NETIF_F_TSO_MANGLEID; | |
3526 | } | |
3527 | ||
3528 | return features; | |
3529 | } | |
3530 | ||
c1e756bf | 3531 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 | 3532 | { |
5f35227e | 3533 | struct net_device *dev = skb->dev; |
fcbeb976 | 3534 | netdev_features_t features = dev->features; |
58e998c6 | 3535 | |
cbc53e08 AD |
3536 | if (skb_is_gso(skb)) |
3537 | features = gso_features_check(skb, dev, features); | |
30b678d8 | 3538 | |
5f35227e JG |
3539 | /* If encapsulation offload request, verify we are testing |
3540 | * hardware encapsulation features instead of standard | |
3541 | * features for the netdev | |
3542 | */ | |
3543 | if (skb->encapsulation) | |
3544 | features &= dev->hw_enc_features; | |
3545 | ||
f5a7fb88 TM |
3546 | if (skb_vlan_tagged(skb)) |
3547 | features = netdev_intersect_features(features, | |
3548 | dev->vlan_features | | |
3549 | NETIF_F_HW_VLAN_CTAG_TX | | |
3550 | NETIF_F_HW_VLAN_STAG_TX); | |
f01a5236 | 3551 | |
5f35227e JG |
3552 | if (dev->netdev_ops->ndo_features_check) |
3553 | features &= dev->netdev_ops->ndo_features_check(skb, dev, | |
3554 | features); | |
8cb65d00 TM |
3555 | else |
3556 | features &= dflt_features_check(skb, dev, features); | |
5f35227e | 3557 | |
c1e756bf | 3558 | return harmonize_features(skb, features); |
58e998c6 | 3559 | } |
c1e756bf | 3560 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 3561 | |
2ea25513 | 3562 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
95f6b3dd | 3563 | struct netdev_queue *txq, bool more) |
f6a78bfc | 3564 | { |
2ea25513 DM |
3565 | unsigned int len; |
3566 | int rc; | |
00829823 | 3567 | |
9f9a742d | 3568 | if (dev_nit_active(dev)) |
2ea25513 | 3569 | dev_queue_xmit_nit(skb, dev); |
fc741216 | 3570 | |
2ea25513 | 3571 | len = skb->len; |
3744741a | 3572 | PRANDOM_ADD_NOISE(skb, dev, txq, len + jiffies); |
2ea25513 | 3573 | trace_net_dev_start_xmit(skb, dev); |
95f6b3dd | 3574 | rc = netdev_start_xmit(skb, dev, txq, more); |
2ea25513 | 3575 | trace_net_dev_xmit(skb, rc, dev, len); |
adf30907 | 3576 | |
2ea25513 DM |
3577 | return rc; |
3578 | } | |
7b9c6090 | 3579 | |
8dcda22a DM |
3580 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
3581 | struct netdev_queue *txq, int *ret) | |
7f2e870f DM |
3582 | { |
3583 | struct sk_buff *skb = first; | |
3584 | int rc = NETDEV_TX_OK; | |
7b9c6090 | 3585 | |
7f2e870f DM |
3586 | while (skb) { |
3587 | struct sk_buff *next = skb->next; | |
fc70fb64 | 3588 | |
a8305bff | 3589 | skb_mark_not_on_list(skb); |
95f6b3dd | 3590 | rc = xmit_one(skb, dev, txq, next != NULL); |
7f2e870f DM |
3591 | if (unlikely(!dev_xmit_complete(rc))) { |
3592 | skb->next = next; | |
3593 | goto out; | |
3594 | } | |
6afff0ca | 3595 | |
7f2e870f | 3596 | skb = next; |
fe60faa5 | 3597 | if (netif_tx_queue_stopped(txq) && skb) { |
7f2e870f DM |
3598 | rc = NETDEV_TX_BUSY; |
3599 | break; | |
9ccb8975 | 3600 | } |
7f2e870f | 3601 | } |
9ccb8975 | 3602 | |
7f2e870f DM |
3603 | out: |
3604 | *ret = rc; | |
3605 | return skb; | |
3606 | } | |
b40863c6 | 3607 | |
1ff0dc94 ED |
3608 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
3609 | netdev_features_t features) | |
f6a78bfc | 3610 | { |
df8a39de | 3611 | if (skb_vlan_tag_present(skb) && |
5968250c JP |
3612 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
3613 | skb = __vlan_hwaccel_push_inside(skb); | |
eae3f88e DM |
3614 | return skb; |
3615 | } | |
f6a78bfc | 3616 | |
43c26a1a DC |
3617 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
3618 | const netdev_features_t features) | |
3619 | { | |
fa821170 | 3620 | if (unlikely(skb_csum_is_sctp(skb))) |
43c26a1a DC |
3621 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : |
3622 | skb_crc32c_csum_help(skb); | |
3623 | ||
62fafcd6 XL |
3624 | if (features & NETIF_F_HW_CSUM) |
3625 | return 0; | |
3626 | ||
3627 | if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { | |
3628 | switch (skb->csum_offset) { | |
3629 | case offsetof(struct tcphdr, check): | |
3630 | case offsetof(struct udphdr, check): | |
3631 | return 0; | |
3632 | } | |
3633 | } | |
3634 | ||
3635 | return skb_checksum_help(skb); | |
43c26a1a DC |
3636 | } |
3637 | EXPORT_SYMBOL(skb_csum_hwoffload_help); | |
3638 | ||
f53c7239 | 3639 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
eae3f88e DM |
3640 | { |
3641 | netdev_features_t features; | |
f6a78bfc | 3642 | |
eae3f88e DM |
3643 | features = netif_skb_features(skb); |
3644 | skb = validate_xmit_vlan(skb, features); | |
3645 | if (unlikely(!skb)) | |
3646 | goto out_null; | |
7b9c6090 | 3647 | |
ebf4e808 IL |
3648 | skb = sk_validate_xmit_skb(skb, dev); |
3649 | if (unlikely(!skb)) | |
3650 | goto out_null; | |
3651 | ||
8b86a61d | 3652 | if (netif_needs_gso(skb, features)) { |
ce93718f DM |
3653 | struct sk_buff *segs; |
3654 | ||
3655 | segs = skb_gso_segment(skb, features); | |
cecda693 | 3656 | if (IS_ERR(segs)) { |
af6dabc9 | 3657 | goto out_kfree_skb; |
cecda693 JW |
3658 | } else if (segs) { |
3659 | consume_skb(skb); | |
3660 | skb = segs; | |
f6a78bfc | 3661 | } |
eae3f88e DM |
3662 | } else { |
3663 | if (skb_needs_linearize(skb, features) && | |
3664 | __skb_linearize(skb)) | |
3665 | goto out_kfree_skb; | |
4ec93edb | 3666 | |
eae3f88e DM |
3667 | /* If packet is not checksummed and device does not |
3668 | * support checksumming for this protocol, complete | |
3669 | * checksumming here. | |
3670 | */ | |
3671 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
3672 | if (skb->encapsulation) | |
3673 | skb_set_inner_transport_header(skb, | |
3674 | skb_checksum_start_offset(skb)); | |
3675 | else | |
3676 | skb_set_transport_header(skb, | |
3677 | skb_checksum_start_offset(skb)); | |
43c26a1a | 3678 | if (skb_csum_hwoffload_help(skb, features)) |
eae3f88e | 3679 | goto out_kfree_skb; |
7b9c6090 | 3680 | } |
0c772159 | 3681 | } |
7b9c6090 | 3682 | |
f53c7239 | 3683 | skb = validate_xmit_xfrm(skb, features, again); |
3dca3f38 | 3684 | |
eae3f88e | 3685 | return skb; |
fc70fb64 | 3686 | |
f6a78bfc HX |
3687 | out_kfree_skb: |
3688 | kfree_skb(skb); | |
eae3f88e | 3689 | out_null: |
d21fd63e | 3690 | atomic_long_inc(&dev->tx_dropped); |
eae3f88e DM |
3691 | return NULL; |
3692 | } | |
6afff0ca | 3693 | |
f53c7239 | 3694 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
55a93b3e ED |
3695 | { |
3696 | struct sk_buff *next, *head = NULL, *tail; | |
3697 | ||
bec3cfdc | 3698 | for (; skb != NULL; skb = next) { |
55a93b3e | 3699 | next = skb->next; |
a8305bff | 3700 | skb_mark_not_on_list(skb); |
bec3cfdc ED |
3701 | |
3702 | /* in case skb wont be segmented, point to itself */ | |
3703 | skb->prev = skb; | |
3704 | ||
f53c7239 | 3705 | skb = validate_xmit_skb(skb, dev, again); |
bec3cfdc ED |
3706 | if (!skb) |
3707 | continue; | |
55a93b3e | 3708 | |
bec3cfdc ED |
3709 | if (!head) |
3710 | head = skb; | |
3711 | else | |
3712 | tail->next = skb; | |
3713 | /* If skb was segmented, skb->prev points to | |
3714 | * the last segment. If not, it still contains skb. | |
3715 | */ | |
3716 | tail = skb->prev; | |
55a93b3e ED |
3717 | } |
3718 | return head; | |
f6a78bfc | 3719 | } |
104ba78c | 3720 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
f6a78bfc | 3721 | |
1def9238 ED |
3722 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
3723 | { | |
3724 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
3725 | ||
3726 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
3727 | ||
3728 | /* To get more precise estimation of bytes sent on wire, | |
3729 | * we add to pkt_len the headers size of all segments | |
3730 | */ | |
a0dce875 | 3731 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
757b8b1d | 3732 | unsigned int hdr_len; |
15e5a030 | 3733 | u16 gso_segs = shinfo->gso_segs; |
1def9238 | 3734 | |
757b8b1d ED |
3735 | /* mac layer + network layer */ |
3736 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); | |
3737 | ||
3738 | /* + transport layer */ | |
7c68d1a6 ED |
3739 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
3740 | const struct tcphdr *th; | |
3741 | struct tcphdr _tcphdr; | |
3742 | ||
3743 | th = skb_header_pointer(skb, skb_transport_offset(skb), | |
3744 | sizeof(_tcphdr), &_tcphdr); | |
3745 | if (likely(th)) | |
3746 | hdr_len += __tcp_hdrlen(th); | |
3747 | } else { | |
3748 | struct udphdr _udphdr; | |
3749 | ||
3750 | if (skb_header_pointer(skb, skb_transport_offset(skb), | |
3751 | sizeof(_udphdr), &_udphdr)) | |
3752 | hdr_len += sizeof(struct udphdr); | |
3753 | } | |
15e5a030 JW |
3754 | |
3755 | if (shinfo->gso_type & SKB_GSO_DODGY) | |
3756 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, | |
3757 | shinfo->gso_size); | |
3758 | ||
3759 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; | |
1def9238 ED |
3760 | } |
3761 | } | |
3762 | ||
bbd8a0d3 KK |
3763 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
3764 | struct net_device *dev, | |
3765 | struct netdev_queue *txq) | |
3766 | { | |
3767 | spinlock_t *root_lock = qdisc_lock(q); | |
520ac30f | 3768 | struct sk_buff *to_free = NULL; |
a2da570d | 3769 | bool contended; |
bbd8a0d3 KK |
3770 | int rc; |
3771 | ||
a2da570d | 3772 | qdisc_calculate_pkt_len(skb, q); |
6b3ba914 JF |
3773 | |
3774 | if (q->flags & TCQ_F_NOLOCK) { | |
ac5c66f2 | 3775 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
379349e9 | 3776 | qdisc_run(q); |
6b3ba914 JF |
3777 | |
3778 | if (unlikely(to_free)) | |
3779 | kfree_skb_list(to_free); | |
3780 | return rc; | |
3781 | } | |
3782 | ||
79640a4c ED |
3783 | /* |
3784 | * Heuristic to force contended enqueues to serialize on a | |
3785 | * separate lock before trying to get qdisc main lock. | |
f9eb8aea | 3786 | * This permits qdisc->running owner to get the lock more |
9bf2b8c2 | 3787 | * often and dequeue packets faster. |
79640a4c | 3788 | */ |
a2da570d | 3789 | contended = qdisc_is_running(q); |
79640a4c ED |
3790 | if (unlikely(contended)) |
3791 | spin_lock(&q->busylock); | |
3792 | ||
bbd8a0d3 KK |
3793 | spin_lock(root_lock); |
3794 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
520ac30f | 3795 | __qdisc_drop(skb, &to_free); |
bbd8a0d3 KK |
3796 | rc = NET_XMIT_DROP; |
3797 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 3798 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
3799 | /* |
3800 | * This is a work-conserving queue; there are no old skbs | |
3801 | * waiting to be sent out; and the qdisc is not running - | |
3802 | * xmit the skb directly. | |
3803 | */ | |
bfe0d029 | 3804 | |
bfe0d029 ED |
3805 | qdisc_bstats_update(q, skb); |
3806 | ||
55a93b3e | 3807 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
79640a4c ED |
3808 | if (unlikely(contended)) { |
3809 | spin_unlock(&q->busylock); | |
3810 | contended = false; | |
3811 | } | |
bbd8a0d3 | 3812 | __qdisc_run(q); |
6c148184 | 3813 | } |
bbd8a0d3 | 3814 | |
6c148184 | 3815 | qdisc_run_end(q); |
bbd8a0d3 KK |
3816 | rc = NET_XMIT_SUCCESS; |
3817 | } else { | |
ac5c66f2 | 3818 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
79640a4c ED |
3819 | if (qdisc_run_begin(q)) { |
3820 | if (unlikely(contended)) { | |
3821 | spin_unlock(&q->busylock); | |
3822 | contended = false; | |
3823 | } | |
3824 | __qdisc_run(q); | |
6c148184 | 3825 | qdisc_run_end(q); |
79640a4c | 3826 | } |
bbd8a0d3 KK |
3827 | } |
3828 | spin_unlock(root_lock); | |
520ac30f ED |
3829 | if (unlikely(to_free)) |
3830 | kfree_skb_list(to_free); | |
79640a4c ED |
3831 | if (unlikely(contended)) |
3832 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
3833 | return rc; |
3834 | } | |
3835 | ||
86f8515f | 3836 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
5bc1421e NH |
3837 | static void skb_update_prio(struct sk_buff *skb) |
3838 | { | |
4dcb31d4 ED |
3839 | const struct netprio_map *map; |
3840 | const struct sock *sk; | |
3841 | unsigned int prioidx; | |
5bc1421e | 3842 | |
4dcb31d4 ED |
3843 | if (skb->priority) |
3844 | return; | |
3845 | map = rcu_dereference_bh(skb->dev->priomap); | |
3846 | if (!map) | |
3847 | return; | |
3848 | sk = skb_to_full_sk(skb); | |
3849 | if (!sk) | |
3850 | return; | |
91c68ce2 | 3851 | |
4dcb31d4 ED |
3852 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
3853 | ||
3854 | if (prioidx < map->priomap_len) | |
3855 | skb->priority = map->priomap[prioidx]; | |
5bc1421e NH |
3856 | } |
3857 | #else | |
3858 | #define skb_update_prio(skb) | |
3859 | #endif | |
3860 | ||
95603e22 MM |
3861 | /** |
3862 | * dev_loopback_xmit - loop back @skb | |
0c4b51f0 EB |
3863 | * @net: network namespace this loopback is happening in |
3864 | * @sk: sk needed to be a netfilter okfn | |
95603e22 MM |
3865 | * @skb: buffer to transmit |
3866 | */ | |
0c4b51f0 | 3867 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
95603e22 MM |
3868 | { |
3869 | skb_reset_mac_header(skb); | |
3870 | __skb_pull(skb, skb_network_offset(skb)); | |
3871 | skb->pkt_type = PACKET_LOOPBACK; | |
3872 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
3873 | WARN_ON(!skb_dst(skb)); | |
3874 | skb_dst_force(skb); | |
3875 | netif_rx_ni(skb); | |
3876 | return 0; | |
3877 | } | |
3878 | EXPORT_SYMBOL(dev_loopback_xmit); | |
3879 | ||
1f211a1b DB |
3880 | #ifdef CONFIG_NET_EGRESS |
3881 | static struct sk_buff * | |
3882 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
3883 | { | |
46209401 | 3884 | struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress); |
1f211a1b DB |
3885 | struct tcf_result cl_res; |
3886 | ||
46209401 | 3887 | if (!miniq) |
1f211a1b DB |
3888 | return skb; |
3889 | ||
8dc07fdb | 3890 | /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ |
aadaca9e | 3891 | qdisc_skb_cb(skb)->mru = 0; |
7baf2429 | 3892 | qdisc_skb_cb(skb)->post_ct = false; |
46209401 | 3893 | mini_qdisc_bstats_cpu_update(miniq, skb); |
1f211a1b | 3894 | |
46209401 | 3895 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
1f211a1b DB |
3896 | case TC_ACT_OK: |
3897 | case TC_ACT_RECLASSIFY: | |
3898 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
3899 | break; | |
3900 | case TC_ACT_SHOT: | |
46209401 | 3901 | mini_qdisc_qstats_cpu_drop(miniq); |
1f211a1b | 3902 | *ret = NET_XMIT_DROP; |
7e2c3aea DB |
3903 | kfree_skb(skb); |
3904 | return NULL; | |
1f211a1b DB |
3905 | case TC_ACT_STOLEN: |
3906 | case TC_ACT_QUEUED: | |
e25ea21f | 3907 | case TC_ACT_TRAP: |
1f211a1b | 3908 | *ret = NET_XMIT_SUCCESS; |
7e2c3aea | 3909 | consume_skb(skb); |
1f211a1b DB |
3910 | return NULL; |
3911 | case TC_ACT_REDIRECT: | |
3912 | /* No need to push/pop skb's mac_header here on egress! */ | |
3913 | skb_do_redirect(skb); | |
3914 | *ret = NET_XMIT_SUCCESS; | |
3915 | return NULL; | |
3916 | default: | |
3917 | break; | |
3918 | } | |
357b6cc5 | 3919 | |
1f211a1b DB |
3920 | return skb; |
3921 | } | |
3922 | #endif /* CONFIG_NET_EGRESS */ | |
3923 | ||
fc9bab24 AN |
3924 | #ifdef CONFIG_XPS |
3925 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, | |
3926 | struct xps_dev_maps *dev_maps, unsigned int tci) | |
3927 | { | |
3928 | struct xps_map *map; | |
3929 | int queue_index = -1; | |
3930 | ||
3931 | if (dev->num_tc) { | |
3932 | tci *= dev->num_tc; | |
3933 | tci += netdev_get_prio_tc_map(dev, skb->priority); | |
3934 | } | |
3935 | ||
3936 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
3937 | if (map) { | |
3938 | if (map->len == 1) | |
3939 | queue_index = map->queues[0]; | |
3940 | else | |
3941 | queue_index = map->queues[reciprocal_scale( | |
3942 | skb_get_hash(skb), map->len)]; | |
3943 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
3944 | queue_index = -1; | |
3945 | } | |
3946 | return queue_index; | |
3947 | } | |
3948 | #endif | |
3949 | ||
eadec877 AD |
3950 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
3951 | struct sk_buff *skb) | |
638b2a69 JP |
3952 | { |
3953 | #ifdef CONFIG_XPS | |
3954 | struct xps_dev_maps *dev_maps; | |
fc9bab24 | 3955 | struct sock *sk = skb->sk; |
638b2a69 JP |
3956 | int queue_index = -1; |
3957 | ||
04157469 AN |
3958 | if (!static_key_false(&xps_needed)) |
3959 | return -1; | |
3960 | ||
638b2a69 | 3961 | rcu_read_lock(); |
fc9bab24 AN |
3962 | if (!static_key_false(&xps_rxqs_needed)) |
3963 | goto get_cpus_map; | |
3964 | ||
eadec877 | 3965 | dev_maps = rcu_dereference(sb_dev->xps_rxqs_map); |
638b2a69 | 3966 | if (dev_maps) { |
fc9bab24 | 3967 | int tci = sk_rx_queue_get(sk); |
184c449f | 3968 | |
fc9bab24 AN |
3969 | if (tci >= 0 && tci < dev->num_rx_queues) |
3970 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3971 | tci); | |
3972 | } | |
184c449f | 3973 | |
fc9bab24 AN |
3974 | get_cpus_map: |
3975 | if (queue_index < 0) { | |
eadec877 | 3976 | dev_maps = rcu_dereference(sb_dev->xps_cpus_map); |
fc9bab24 AN |
3977 | if (dev_maps) { |
3978 | unsigned int tci = skb->sender_cpu - 1; | |
3979 | ||
3980 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3981 | tci); | |
638b2a69 JP |
3982 | } |
3983 | } | |
3984 | rcu_read_unlock(); | |
3985 | ||
3986 | return queue_index; | |
3987 | #else | |
3988 | return -1; | |
3989 | #endif | |
3990 | } | |
3991 | ||
a4ea8a3d | 3992 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 3993 | struct net_device *sb_dev) |
a4ea8a3d AD |
3994 | { |
3995 | return 0; | |
3996 | } | |
3997 | EXPORT_SYMBOL(dev_pick_tx_zero); | |
3998 | ||
3999 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, | |
a350ecce | 4000 | struct net_device *sb_dev) |
a4ea8a3d AD |
4001 | { |
4002 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; | |
4003 | } | |
4004 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); | |
4005 | ||
b71b5837 PA |
4006 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
4007 | struct net_device *sb_dev) | |
638b2a69 JP |
4008 | { |
4009 | struct sock *sk = skb->sk; | |
4010 | int queue_index = sk_tx_queue_get(sk); | |
4011 | ||
eadec877 AD |
4012 | sb_dev = sb_dev ? : dev; |
4013 | ||
638b2a69 JP |
4014 | if (queue_index < 0 || skb->ooo_okay || |
4015 | queue_index >= dev->real_num_tx_queues) { | |
eadec877 | 4016 | int new_index = get_xps_queue(dev, sb_dev, skb); |
f4563a75 | 4017 | |
638b2a69 | 4018 | if (new_index < 0) |
eadec877 | 4019 | new_index = skb_tx_hash(dev, sb_dev, skb); |
638b2a69 JP |
4020 | |
4021 | if (queue_index != new_index && sk && | |
004a5d01 | 4022 | sk_fullsock(sk) && |
638b2a69 JP |
4023 | rcu_access_pointer(sk->sk_dst_cache)) |
4024 | sk_tx_queue_set(sk, new_index); | |
4025 | ||
4026 | queue_index = new_index; | |
4027 | } | |
4028 | ||
4029 | return queue_index; | |
4030 | } | |
b71b5837 | 4031 | EXPORT_SYMBOL(netdev_pick_tx); |
638b2a69 | 4032 | |
4bd97d51 PA |
4033 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
4034 | struct sk_buff *skb, | |
4035 | struct net_device *sb_dev) | |
638b2a69 JP |
4036 | { |
4037 | int queue_index = 0; | |
4038 | ||
4039 | #ifdef CONFIG_XPS | |
52bd2d62 ED |
4040 | u32 sender_cpu = skb->sender_cpu - 1; |
4041 | ||
4042 | if (sender_cpu >= (u32)NR_CPUS) | |
638b2a69 JP |
4043 | skb->sender_cpu = raw_smp_processor_id() + 1; |
4044 | #endif | |
4045 | ||
4046 | if (dev->real_num_tx_queues != 1) { | |
4047 | const struct net_device_ops *ops = dev->netdev_ops; | |
f4563a75 | 4048 | |
638b2a69 | 4049 | if (ops->ndo_select_queue) |
a350ecce | 4050 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
638b2a69 | 4051 | else |
4bd97d51 | 4052 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
638b2a69 | 4053 | |
d584527c | 4054 | queue_index = netdev_cap_txqueue(dev, queue_index); |
638b2a69 JP |
4055 | } |
4056 | ||
4057 | skb_set_queue_mapping(skb, queue_index); | |
4058 | return netdev_get_tx_queue(dev, queue_index); | |
4059 | } | |
4060 | ||
d29f749e | 4061 | /** |
9d08dd3d | 4062 | * __dev_queue_xmit - transmit a buffer |
d29f749e | 4063 | * @skb: buffer to transmit |
eadec877 | 4064 | * @sb_dev: suboordinate device used for L2 forwarding offload |
d29f749e DJ |
4065 | * |
4066 | * Queue a buffer for transmission to a network device. The caller must | |
4067 | * have set the device and priority and built the buffer before calling | |
4068 | * this function. The function can be called from an interrupt. | |
4069 | * | |
4070 | * A negative errno code is returned on a failure. A success does not | |
4071 | * guarantee the frame will be transmitted as it may be dropped due | |
4072 | * to congestion or traffic shaping. | |
4073 | * | |
4074 | * ----------------------------------------------------------------------------------- | |
4075 | * I notice this method can also return errors from the queue disciplines, | |
4076 | * including NET_XMIT_DROP, which is a positive value. So, errors can also | |
4077 | * be positive. | |
4078 | * | |
4079 | * Regardless of the return value, the skb is consumed, so it is currently | |
4080 | * difficult to retry a send to this method. (You can bump the ref count | |
4081 | * before sending to hold a reference for retry if you are careful.) | |
4082 | * | |
4083 | * When calling this method, interrupts MUST be enabled. This is because | |
4084 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
4085 | * --BLG | |
4086 | */ | |
eadec877 | 4087 | static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
1da177e4 LT |
4088 | { |
4089 | struct net_device *dev = skb->dev; | |
dc2b4847 | 4090 | struct netdev_queue *txq; |
1da177e4 LT |
4091 | struct Qdisc *q; |
4092 | int rc = -ENOMEM; | |
f53c7239 | 4093 | bool again = false; |
1da177e4 | 4094 | |
6d1ccff6 ED |
4095 | skb_reset_mac_header(skb); |
4096 | ||
e7fd2885 | 4097 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
e7ed11ee | 4098 | __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED); |
e7fd2885 | 4099 | |
4ec93edb YH |
4100 | /* Disable soft irqs for various locks below. Also |
4101 | * stops preemption for RCU. | |
1da177e4 | 4102 | */ |
4ec93edb | 4103 | rcu_read_lock_bh(); |
1da177e4 | 4104 | |
5bc1421e NH |
4105 | skb_update_prio(skb); |
4106 | ||
1f211a1b DB |
4107 | qdisc_pkt_len_init(skb); |
4108 | #ifdef CONFIG_NET_CLS_ACT | |
8dc07fdb | 4109 | skb->tc_at_ingress = 0; |
357b6cc5 | 4110 | # ifdef CONFIG_NET_EGRESS |
aabf6772 | 4111 | if (static_branch_unlikely(&egress_needed_key)) { |
1f211a1b DB |
4112 | skb = sch_handle_egress(skb, &rc, dev); |
4113 | if (!skb) | |
4114 | goto out; | |
4115 | } | |
357b6cc5 | 4116 | # endif |
1f211a1b | 4117 | #endif |
02875878 ED |
4118 | /* If device/qdisc don't need skb->dst, release it right now while |
4119 | * its hot in this cpu cache. | |
4120 | */ | |
4121 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
4122 | skb_dst_drop(skb); | |
4123 | else | |
4124 | skb_dst_force(skb); | |
4125 | ||
4bd97d51 | 4126 | txq = netdev_core_pick_tx(dev, skb, sb_dev); |
a898def2 | 4127 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 4128 | |
cf66ba58 | 4129 | trace_net_dev_queue(skb); |
1da177e4 | 4130 | if (q->enqueue) { |
bbd8a0d3 | 4131 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 4132 | goto out; |
1da177e4 LT |
4133 | } |
4134 | ||
4135 | /* The device has no queue. Common case for software devices: | |
eb13da1a | 4136 | * loopback, all the sorts of tunnels... |
1da177e4 | 4137 | |
eb13da1a | 4138 | * Really, it is unlikely that netif_tx_lock protection is necessary |
4139 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
4140 | * counters.) | |
4141 | * However, it is possible, that they rely on protection | |
4142 | * made by us here. | |
1da177e4 | 4143 | |
eb13da1a | 4144 | * Check this and shot the lock. It is not prone from deadlocks. |
4145 | *Either shot noqueue qdisc, it is even simpler 8) | |
1da177e4 LT |
4146 | */ |
4147 | if (dev->flags & IFF_UP) { | |
4148 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
4149 | ||
c773e847 | 4150 | if (txq->xmit_lock_owner != cpu) { |
97cdcf37 | 4151 | if (dev_xmit_recursion()) |
745e20f1 ED |
4152 | goto recursion_alert; |
4153 | ||
f53c7239 | 4154 | skb = validate_xmit_skb(skb, dev, &again); |
1f59533f | 4155 | if (!skb) |
d21fd63e | 4156 | goto out; |
1f59533f | 4157 | |
3744741a | 4158 | PRANDOM_ADD_NOISE(skb, dev, txq, jiffies); |
c773e847 | 4159 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 4160 | |
73466498 | 4161 | if (!netif_xmit_stopped(txq)) { |
97cdcf37 | 4162 | dev_xmit_recursion_inc(); |
ce93718f | 4163 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
97cdcf37 | 4164 | dev_xmit_recursion_dec(); |
572a9d7b | 4165 | if (dev_xmit_complete(rc)) { |
c773e847 | 4166 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
4167 | goto out; |
4168 | } | |
4169 | } | |
c773e847 | 4170 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
4171 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
4172 | dev->name); | |
1da177e4 LT |
4173 | } else { |
4174 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
4175 | * unfortunately |
4176 | */ | |
4177 | recursion_alert: | |
e87cc472 JP |
4178 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
4179 | dev->name); | |
1da177e4 LT |
4180 | } |
4181 | } | |
4182 | ||
4183 | rc = -ENETDOWN; | |
d4828d85 | 4184 | rcu_read_unlock_bh(); |
1da177e4 | 4185 | |
015f0688 | 4186 | atomic_long_inc(&dev->tx_dropped); |
1f59533f | 4187 | kfree_skb_list(skb); |
1da177e4 LT |
4188 | return rc; |
4189 | out: | |
d4828d85 | 4190 | rcu_read_unlock_bh(); |
1da177e4 LT |
4191 | return rc; |
4192 | } | |
f663dd9a | 4193 | |
2b4aa3ce | 4194 | int dev_queue_xmit(struct sk_buff *skb) |
f663dd9a JW |
4195 | { |
4196 | return __dev_queue_xmit(skb, NULL); | |
4197 | } | |
2b4aa3ce | 4198 | EXPORT_SYMBOL(dev_queue_xmit); |
1da177e4 | 4199 | |
eadec877 | 4200 | int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev) |
f663dd9a | 4201 | { |
eadec877 | 4202 | return __dev_queue_xmit(skb, sb_dev); |
f663dd9a JW |
4203 | } |
4204 | EXPORT_SYMBOL(dev_queue_xmit_accel); | |
4205 | ||
36ccdf85 | 4206 | int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
865b03f2 MK |
4207 | { |
4208 | struct net_device *dev = skb->dev; | |
4209 | struct sk_buff *orig_skb = skb; | |
4210 | struct netdev_queue *txq; | |
4211 | int ret = NETDEV_TX_BUSY; | |
4212 | bool again = false; | |
4213 | ||
4214 | if (unlikely(!netif_running(dev) || | |
4215 | !netif_carrier_ok(dev))) | |
4216 | goto drop; | |
4217 | ||
4218 | skb = validate_xmit_skb_list(skb, dev, &again); | |
4219 | if (skb != orig_skb) | |
4220 | goto drop; | |
4221 | ||
4222 | skb_set_queue_mapping(skb, queue_id); | |
4223 | txq = skb_get_tx_queue(dev, skb); | |
3744741a | 4224 | PRANDOM_ADD_NOISE(skb, dev, txq, jiffies); |
865b03f2 MK |
4225 | |
4226 | local_bh_disable(); | |
4227 | ||
0ad6f6e7 | 4228 | dev_xmit_recursion_inc(); |
865b03f2 MK |
4229 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
4230 | if (!netif_xmit_frozen_or_drv_stopped(txq)) | |
4231 | ret = netdev_start_xmit(skb, dev, txq, false); | |
4232 | HARD_TX_UNLOCK(dev, txq); | |
0ad6f6e7 | 4233 | dev_xmit_recursion_dec(); |
865b03f2 MK |
4234 | |
4235 | local_bh_enable(); | |
865b03f2 MK |
4236 | return ret; |
4237 | drop: | |
4238 | atomic_long_inc(&dev->tx_dropped); | |
4239 | kfree_skb_list(skb); | |
4240 | return NET_XMIT_DROP; | |
4241 | } | |
36ccdf85 | 4242 | EXPORT_SYMBOL(__dev_direct_xmit); |
1da177e4 | 4243 | |
eb13da1a | 4244 | /************************************************************************* |
4245 | * Receiver routines | |
4246 | *************************************************************************/ | |
1da177e4 | 4247 | |
6b2bedc3 | 4248 | int netdev_max_backlog __read_mostly = 1000; |
c9e6bc64 ED |
4249 | EXPORT_SYMBOL(netdev_max_backlog); |
4250 | ||
3b098e2d | 4251 | int netdev_tstamp_prequeue __read_mostly = 1; |
6b2bedc3 | 4252 | int netdev_budget __read_mostly = 300; |
a4837980 KK |
4253 | /* Must be at least 2 jiffes to guarantee 1 jiffy timeout */ |
4254 | unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ; | |
3d48b53f MT |
4255 | int weight_p __read_mostly = 64; /* old backlog weight */ |
4256 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ | |
4257 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ | |
4258 | int dev_rx_weight __read_mostly = 64; | |
4259 | int dev_tx_weight __read_mostly = 64; | |
323ebb61 EC |
4260 | /* Maximum number of GRO_NORMAL skbs to batch up for list-RX */ |
4261 | int gro_normal_batch __read_mostly = 8; | |
1da177e4 | 4262 | |
eecfd7c4 ED |
4263 | /* Called with irq disabled */ |
4264 | static inline void ____napi_schedule(struct softnet_data *sd, | |
4265 | struct napi_struct *napi) | |
4266 | { | |
4267 | list_add_tail(&napi->poll_list, &sd->poll_list); | |
4268 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4269 | } | |
4270 | ||
bfb564e7 KK |
4271 | #ifdef CONFIG_RPS |
4272 | ||
4273 | /* One global table that all flow-based protocols share. */ | |
6e3f7faf | 4274 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
bfb564e7 | 4275 | EXPORT_SYMBOL(rps_sock_flow_table); |
567e4b79 ED |
4276 | u32 rps_cpu_mask __read_mostly; |
4277 | EXPORT_SYMBOL(rps_cpu_mask); | |
bfb564e7 | 4278 | |
dc05360f | 4279 | struct static_key_false rps_needed __read_mostly; |
3df97ba8 | 4280 | EXPORT_SYMBOL(rps_needed); |
dc05360f | 4281 | struct static_key_false rfs_needed __read_mostly; |
13bfff25 | 4282 | EXPORT_SYMBOL(rfs_needed); |
adc9300e | 4283 | |
c445477d BH |
4284 | static struct rps_dev_flow * |
4285 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4286 | struct rps_dev_flow *rflow, u16 next_cpu) | |
4287 | { | |
a31196b0 | 4288 | if (next_cpu < nr_cpu_ids) { |
c445477d BH |
4289 | #ifdef CONFIG_RFS_ACCEL |
4290 | struct netdev_rx_queue *rxqueue; | |
4291 | struct rps_dev_flow_table *flow_table; | |
4292 | struct rps_dev_flow *old_rflow; | |
4293 | u32 flow_id; | |
4294 | u16 rxq_index; | |
4295 | int rc; | |
4296 | ||
4297 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
4298 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
4299 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
4300 | goto out; |
4301 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
4302 | if (rxq_index == skb_get_rx_queue(skb)) | |
4303 | goto out; | |
4304 | ||
4305 | rxqueue = dev->_rx + rxq_index; | |
4306 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4307 | if (!flow_table) | |
4308 | goto out; | |
61b905da | 4309 | flow_id = skb_get_hash(skb) & flow_table->mask; |
c445477d BH |
4310 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
4311 | rxq_index, flow_id); | |
4312 | if (rc < 0) | |
4313 | goto out; | |
4314 | old_rflow = rflow; | |
4315 | rflow = &flow_table->flows[flow_id]; | |
c445477d BH |
4316 | rflow->filter = rc; |
4317 | if (old_rflow->filter == rflow->filter) | |
4318 | old_rflow->filter = RPS_NO_FILTER; | |
4319 | out: | |
4320 | #endif | |
4321 | rflow->last_qtail = | |
09994d1b | 4322 | per_cpu(softnet_data, next_cpu).input_queue_head; |
c445477d BH |
4323 | } |
4324 | ||
09994d1b | 4325 | rflow->cpu = next_cpu; |
c445477d BH |
4326 | return rflow; |
4327 | } | |
4328 | ||
bfb564e7 KK |
4329 | /* |
4330 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
4331 | * CPU from the RPS map of the receiving queue for a given skb. | |
4332 | * rcu_read_lock must be held on entry. | |
4333 | */ | |
4334 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4335 | struct rps_dev_flow **rflowp) | |
4336 | { | |
567e4b79 ED |
4337 | const struct rps_sock_flow_table *sock_flow_table; |
4338 | struct netdev_rx_queue *rxqueue = dev->_rx; | |
bfb564e7 | 4339 | struct rps_dev_flow_table *flow_table; |
567e4b79 | 4340 | struct rps_map *map; |
bfb564e7 | 4341 | int cpu = -1; |
567e4b79 | 4342 | u32 tcpu; |
61b905da | 4343 | u32 hash; |
bfb564e7 KK |
4344 | |
4345 | if (skb_rx_queue_recorded(skb)) { | |
4346 | u16 index = skb_get_rx_queue(skb); | |
567e4b79 | 4347 | |
62fe0b40 BH |
4348 | if (unlikely(index >= dev->real_num_rx_queues)) { |
4349 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4350 | "%s received packet on queue %u, but number " | |
4351 | "of RX queues is %u\n", | |
4352 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
4353 | goto done; |
4354 | } | |
567e4b79 ED |
4355 | rxqueue += index; |
4356 | } | |
bfb564e7 | 4357 | |
567e4b79 ED |
4358 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
4359 | ||
4360 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
6e3f7faf | 4361 | map = rcu_dereference(rxqueue->rps_map); |
567e4b79 | 4362 | if (!flow_table && !map) |
bfb564e7 KK |
4363 | goto done; |
4364 | ||
2d47b459 | 4365 | skb_reset_network_header(skb); |
61b905da TH |
4366 | hash = skb_get_hash(skb); |
4367 | if (!hash) | |
bfb564e7 KK |
4368 | goto done; |
4369 | ||
fec5e652 TH |
4370 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
4371 | if (flow_table && sock_flow_table) { | |
fec5e652 | 4372 | struct rps_dev_flow *rflow; |
567e4b79 ED |
4373 | u32 next_cpu; |
4374 | u32 ident; | |
4375 | ||
4376 | /* First check into global flow table if there is a match */ | |
4377 | ident = sock_flow_table->ents[hash & sock_flow_table->mask]; | |
4378 | if ((ident ^ hash) & ~rps_cpu_mask) | |
4379 | goto try_rps; | |
fec5e652 | 4380 | |
567e4b79 ED |
4381 | next_cpu = ident & rps_cpu_mask; |
4382 | ||
4383 | /* OK, now we know there is a match, | |
4384 | * we can look at the local (per receive queue) flow table | |
4385 | */ | |
61b905da | 4386 | rflow = &flow_table->flows[hash & flow_table->mask]; |
fec5e652 TH |
4387 | tcpu = rflow->cpu; |
4388 | ||
fec5e652 TH |
4389 | /* |
4390 | * If the desired CPU (where last recvmsg was done) is | |
4391 | * different from current CPU (one in the rx-queue flow | |
4392 | * table entry), switch if one of the following holds: | |
a31196b0 | 4393 | * - Current CPU is unset (>= nr_cpu_ids). |
fec5e652 TH |
4394 | * - Current CPU is offline. |
4395 | * - The current CPU's queue tail has advanced beyond the | |
4396 | * last packet that was enqueued using this table entry. | |
4397 | * This guarantees that all previous packets for the flow | |
4398 | * have been dequeued, thus preserving in order delivery. | |
4399 | */ | |
4400 | if (unlikely(tcpu != next_cpu) && | |
a31196b0 | 4401 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
fec5e652 | 4402 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
baefa31d TH |
4403 | rflow->last_qtail)) >= 0)) { |
4404 | tcpu = next_cpu; | |
c445477d | 4405 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 4406 | } |
c445477d | 4407 | |
a31196b0 | 4408 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
fec5e652 TH |
4409 | *rflowp = rflow; |
4410 | cpu = tcpu; | |
4411 | goto done; | |
4412 | } | |
4413 | } | |
4414 | ||
567e4b79 ED |
4415 | try_rps: |
4416 | ||
0a9627f2 | 4417 | if (map) { |
8fc54f68 | 4418 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
0a9627f2 TH |
4419 | if (cpu_online(tcpu)) { |
4420 | cpu = tcpu; | |
4421 | goto done; | |
4422 | } | |
4423 | } | |
4424 | ||
4425 | done: | |
0a9627f2 TH |
4426 | return cpu; |
4427 | } | |
4428 | ||
c445477d BH |
4429 | #ifdef CONFIG_RFS_ACCEL |
4430 | ||
4431 | /** | |
4432 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
4433 | * @dev: Device on which the filter was set | |
4434 | * @rxq_index: RX queue index | |
4435 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
4436 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
4437 | * | |
4438 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
4439 | * this function for each installed filter and remove the filters for | |
4440 | * which it returns %true. | |
4441 | */ | |
4442 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
4443 | u32 flow_id, u16 filter_id) | |
4444 | { | |
4445 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
4446 | struct rps_dev_flow_table *flow_table; | |
4447 | struct rps_dev_flow *rflow; | |
4448 | bool expire = true; | |
a31196b0 | 4449 | unsigned int cpu; |
c445477d BH |
4450 | |
4451 | rcu_read_lock(); | |
4452 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4453 | if (flow_table && flow_id <= flow_table->mask) { | |
4454 | rflow = &flow_table->flows[flow_id]; | |
6aa7de05 | 4455 | cpu = READ_ONCE(rflow->cpu); |
a31196b0 | 4456 | if (rflow->filter == filter_id && cpu < nr_cpu_ids && |
c445477d BH |
4457 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
4458 | rflow->last_qtail) < | |
4459 | (int)(10 * flow_table->mask))) | |
4460 | expire = false; | |
4461 | } | |
4462 | rcu_read_unlock(); | |
4463 | return expire; | |
4464 | } | |
4465 | EXPORT_SYMBOL(rps_may_expire_flow); | |
4466 | ||
4467 | #endif /* CONFIG_RFS_ACCEL */ | |
4468 | ||
0a9627f2 | 4469 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 4470 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 4471 | { |
e36fa2f7 ED |
4472 | struct softnet_data *sd = data; |
4473 | ||
eecfd7c4 | 4474 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 4475 | sd->received_rps++; |
0a9627f2 | 4476 | } |
e36fa2f7 | 4477 | |
fec5e652 | 4478 | #endif /* CONFIG_RPS */ |
0a9627f2 | 4479 | |
e36fa2f7 ED |
4480 | /* |
4481 | * Check if this softnet_data structure is another cpu one | |
4482 | * If yes, queue it to our IPI list and return 1 | |
4483 | * If no, return 0 | |
4484 | */ | |
4485 | static int rps_ipi_queued(struct softnet_data *sd) | |
4486 | { | |
4487 | #ifdef CONFIG_RPS | |
903ceff7 | 4488 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
e36fa2f7 ED |
4489 | |
4490 | if (sd != mysd) { | |
4491 | sd->rps_ipi_next = mysd->rps_ipi_list; | |
4492 | mysd->rps_ipi_list = sd; | |
4493 | ||
4494 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4495 | return 1; | |
4496 | } | |
4497 | #endif /* CONFIG_RPS */ | |
4498 | return 0; | |
4499 | } | |
4500 | ||
99bbc707 WB |
4501 | #ifdef CONFIG_NET_FLOW_LIMIT |
4502 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); | |
4503 | #endif | |
4504 | ||
4505 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) | |
4506 | { | |
4507 | #ifdef CONFIG_NET_FLOW_LIMIT | |
4508 | struct sd_flow_limit *fl; | |
4509 | struct softnet_data *sd; | |
4510 | unsigned int old_flow, new_flow; | |
4511 | ||
4512 | if (qlen < (netdev_max_backlog >> 1)) | |
4513 | return false; | |
4514 | ||
903ceff7 | 4515 | sd = this_cpu_ptr(&softnet_data); |
99bbc707 WB |
4516 | |
4517 | rcu_read_lock(); | |
4518 | fl = rcu_dereference(sd->flow_limit); | |
4519 | if (fl) { | |
3958afa1 | 4520 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
99bbc707 WB |
4521 | old_flow = fl->history[fl->history_head]; |
4522 | fl->history[fl->history_head] = new_flow; | |
4523 | ||
4524 | fl->history_head++; | |
4525 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; | |
4526 | ||
4527 | if (likely(fl->buckets[old_flow])) | |
4528 | fl->buckets[old_flow]--; | |
4529 | ||
4530 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { | |
4531 | fl->count++; | |
4532 | rcu_read_unlock(); | |
4533 | return true; | |
4534 | } | |
4535 | } | |
4536 | rcu_read_unlock(); | |
4537 | #endif | |
4538 | return false; | |
4539 | } | |
4540 | ||
0a9627f2 TH |
4541 | /* |
4542 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
4543 | * queue (may be a remote CPU queue). | |
4544 | */ | |
fec5e652 TH |
4545 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
4546 | unsigned int *qtail) | |
0a9627f2 | 4547 | { |
e36fa2f7 | 4548 | struct softnet_data *sd; |
0a9627f2 | 4549 | unsigned long flags; |
99bbc707 | 4550 | unsigned int qlen; |
0a9627f2 | 4551 | |
e36fa2f7 | 4552 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 TH |
4553 | |
4554 | local_irq_save(flags); | |
0a9627f2 | 4555 | |
e36fa2f7 | 4556 | rps_lock(sd); |
e9e4dd32 JA |
4557 | if (!netif_running(skb->dev)) |
4558 | goto drop; | |
99bbc707 WB |
4559 | qlen = skb_queue_len(&sd->input_pkt_queue); |
4560 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { | |
e008f3f0 | 4561 | if (qlen) { |
0a9627f2 | 4562 | enqueue: |
e36fa2f7 | 4563 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
76cc8b13 | 4564 | input_queue_tail_incr_save(sd, qtail); |
e36fa2f7 | 4565 | rps_unlock(sd); |
152102c7 | 4566 | local_irq_restore(flags); |
0a9627f2 TH |
4567 | return NET_RX_SUCCESS; |
4568 | } | |
4569 | ||
ebda37c2 ED |
4570 | /* Schedule NAPI for backlog device |
4571 | * We can use non atomic operation since we own the queue lock | |
4572 | */ | |
4573 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { | |
e36fa2f7 | 4574 | if (!rps_ipi_queued(sd)) |
eecfd7c4 | 4575 | ____napi_schedule(sd, &sd->backlog); |
0a9627f2 TH |
4576 | } |
4577 | goto enqueue; | |
4578 | } | |
4579 | ||
e9e4dd32 | 4580 | drop: |
dee42870 | 4581 | sd->dropped++; |
e36fa2f7 | 4582 | rps_unlock(sd); |
0a9627f2 | 4583 | |
0a9627f2 TH |
4584 | local_irq_restore(flags); |
4585 | ||
caf586e5 | 4586 | atomic_long_inc(&skb->dev->rx_dropped); |
0a9627f2 TH |
4587 | kfree_skb(skb); |
4588 | return NET_RX_DROP; | |
4589 | } | |
1da177e4 | 4590 | |
e817f856 JDB |
4591 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
4592 | { | |
4593 | struct net_device *dev = skb->dev; | |
4594 | struct netdev_rx_queue *rxqueue; | |
4595 | ||
4596 | rxqueue = dev->_rx; | |
4597 | ||
4598 | if (skb_rx_queue_recorded(skb)) { | |
4599 | u16 index = skb_get_rx_queue(skb); | |
4600 | ||
4601 | if (unlikely(index >= dev->real_num_rx_queues)) { | |
4602 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4603 | "%s received packet on queue %u, but number " | |
4604 | "of RX queues is %u\n", | |
4605 | dev->name, index, dev->real_num_rx_queues); | |
4606 | ||
4607 | return rxqueue; /* Return first rxqueue */ | |
4608 | } | |
4609 | rxqueue += index; | |
4610 | } | |
4611 | return rxqueue; | |
4612 | } | |
4613 | ||
d4455169 | 4614 | static u32 netif_receive_generic_xdp(struct sk_buff *skb, |
02671e23 | 4615 | struct xdp_buff *xdp, |
d4455169 JF |
4616 | struct bpf_prog *xdp_prog) |
4617 | { | |
be9df4af | 4618 | void *orig_data, *orig_data_end, *hard_start; |
e817f856 | 4619 | struct netdev_rx_queue *rxqueue; |
de8f3a83 | 4620 | u32 metalen, act = XDP_DROP; |
43b5169d | 4621 | u32 mac_len, frame_sz; |
29724956 JDB |
4622 | __be16 orig_eth_type; |
4623 | struct ethhdr *eth; | |
4624 | bool orig_bcast; | |
be9df4af | 4625 | int off; |
d4455169 JF |
4626 | |
4627 | /* Reinjected packets coming from act_mirred or similar should | |
4628 | * not get XDP generic processing. | |
4629 | */ | |
2c64605b | 4630 | if (skb_is_redirected(skb)) |
d4455169 JF |
4631 | return XDP_PASS; |
4632 | ||
de8f3a83 DB |
4633 | /* XDP packets must be linear and must have sufficient headroom |
4634 | * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also | |
4635 | * native XDP provides, thus we need to do it here as well. | |
4636 | */ | |
ad1e03b2 | 4637 | if (skb_cloned(skb) || skb_is_nonlinear(skb) || |
de8f3a83 DB |
4638 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { |
4639 | int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); | |
4640 | int troom = skb->tail + skb->data_len - skb->end; | |
4641 | ||
4642 | /* In case we have to go down the path and also linearize, | |
4643 | * then lets do the pskb_expand_head() work just once here. | |
4644 | */ | |
4645 | if (pskb_expand_head(skb, | |
4646 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, | |
4647 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC)) | |
4648 | goto do_drop; | |
2d17d8d7 | 4649 | if (skb_linearize(skb)) |
de8f3a83 DB |
4650 | goto do_drop; |
4651 | } | |
d4455169 JF |
4652 | |
4653 | /* The XDP program wants to see the packet starting at the MAC | |
4654 | * header. | |
4655 | */ | |
4656 | mac_len = skb->data - skb_mac_header(skb); | |
be9df4af | 4657 | hard_start = skb->data - skb_headroom(skb); |
a075767b JDB |
4658 | |
4659 | /* SKB "head" area always have tailroom for skb_shared_info */ | |
be9df4af | 4660 | frame_sz = (void *)skb_end_pointer(skb) - hard_start; |
43b5169d | 4661 | frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
a075767b | 4662 | |
be9df4af LB |
4663 | rxqueue = netif_get_rxqueue(skb); |
4664 | xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq); | |
4665 | xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len, | |
4666 | skb_headlen(skb) + mac_len, true); | |
a075767b | 4667 | |
02671e23 BT |
4668 | orig_data_end = xdp->data_end; |
4669 | orig_data = xdp->data; | |
29724956 JDB |
4670 | eth = (struct ethhdr *)xdp->data; |
4671 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); | |
4672 | orig_eth_type = eth->h_proto; | |
d4455169 | 4673 | |
02671e23 | 4674 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
d4455169 | 4675 | |
065af355 | 4676 | /* check if bpf_xdp_adjust_head was used */ |
02671e23 | 4677 | off = xdp->data - orig_data; |
065af355 JDB |
4678 | if (off) { |
4679 | if (off > 0) | |
4680 | __skb_pull(skb, off); | |
4681 | else if (off < 0) | |
4682 | __skb_push(skb, -off); | |
4683 | ||
4684 | skb->mac_header += off; | |
4685 | skb_reset_network_header(skb); | |
4686 | } | |
d4455169 | 4687 | |
a075767b JDB |
4688 | /* check if bpf_xdp_adjust_tail was used */ |
4689 | off = xdp->data_end - orig_data_end; | |
f7613120 | 4690 | if (off != 0) { |
02671e23 | 4691 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
a075767b | 4692 | skb->len += off; /* positive on grow, negative on shrink */ |
f7613120 | 4693 | } |
198d83bb | 4694 | |
29724956 JDB |
4695 | /* check if XDP changed eth hdr such SKB needs update */ |
4696 | eth = (struct ethhdr *)xdp->data; | |
4697 | if ((orig_eth_type != eth->h_proto) || | |
4698 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { | |
4699 | __skb_push(skb, ETH_HLEN); | |
4700 | skb->protocol = eth_type_trans(skb, skb->dev); | |
4701 | } | |
4702 | ||
d4455169 | 4703 | switch (act) { |
6103aa96 | 4704 | case XDP_REDIRECT: |
d4455169 JF |
4705 | case XDP_TX: |
4706 | __skb_push(skb, mac_len); | |
de8f3a83 | 4707 | break; |
d4455169 | 4708 | case XDP_PASS: |
02671e23 | 4709 | metalen = xdp->data - xdp->data_meta; |
de8f3a83 DB |
4710 | if (metalen) |
4711 | skb_metadata_set(skb, metalen); | |
d4455169 | 4712 | break; |
d4455169 JF |
4713 | default: |
4714 | bpf_warn_invalid_xdp_action(act); | |
df561f66 | 4715 | fallthrough; |
d4455169 JF |
4716 | case XDP_ABORTED: |
4717 | trace_xdp_exception(skb->dev, xdp_prog, act); | |
df561f66 | 4718 | fallthrough; |
d4455169 JF |
4719 | case XDP_DROP: |
4720 | do_drop: | |
4721 | kfree_skb(skb); | |
4722 | break; | |
4723 | } | |
4724 | ||
4725 | return act; | |
4726 | } | |
4727 | ||
4728 | /* When doing generic XDP we have to bypass the qdisc layer and the | |
4729 | * network taps in order to match in-driver-XDP behavior. | |
4730 | */ | |
7c497478 | 4731 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
d4455169 JF |
4732 | { |
4733 | struct net_device *dev = skb->dev; | |
4734 | struct netdev_queue *txq; | |
4735 | bool free_skb = true; | |
4736 | int cpu, rc; | |
4737 | ||
4bd97d51 | 4738 | txq = netdev_core_pick_tx(dev, skb, NULL); |
d4455169 JF |
4739 | cpu = smp_processor_id(); |
4740 | HARD_TX_LOCK(dev, txq, cpu); | |
4741 | if (!netif_xmit_stopped(txq)) { | |
4742 | rc = netdev_start_xmit(skb, dev, txq, 0); | |
4743 | if (dev_xmit_complete(rc)) | |
4744 | free_skb = false; | |
4745 | } | |
4746 | HARD_TX_UNLOCK(dev, txq); | |
4747 | if (free_skb) { | |
4748 | trace_xdp_exception(dev, xdp_prog, XDP_TX); | |
4749 | kfree_skb(skb); | |
4750 | } | |
4751 | } | |
4752 | ||
02786475 | 4753 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
d4455169 | 4754 | |
7c497478 | 4755 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb) |
d4455169 | 4756 | { |
d4455169 | 4757 | if (xdp_prog) { |
02671e23 BT |
4758 | struct xdp_buff xdp; |
4759 | u32 act; | |
6103aa96 | 4760 | int err; |
d4455169 | 4761 | |
02671e23 | 4762 | act = netif_receive_generic_xdp(skb, &xdp, xdp_prog); |
d4455169 | 4763 | if (act != XDP_PASS) { |
6103aa96 JF |
4764 | switch (act) { |
4765 | case XDP_REDIRECT: | |
2facaad6 | 4766 | err = xdp_do_generic_redirect(skb->dev, skb, |
02671e23 | 4767 | &xdp, xdp_prog); |
6103aa96 JF |
4768 | if (err) |
4769 | goto out_redir; | |
02671e23 | 4770 | break; |
6103aa96 | 4771 | case XDP_TX: |
d4455169 | 4772 | generic_xdp_tx(skb, xdp_prog); |
6103aa96 JF |
4773 | break; |
4774 | } | |
d4455169 JF |
4775 | return XDP_DROP; |
4776 | } | |
4777 | } | |
4778 | return XDP_PASS; | |
6103aa96 | 4779 | out_redir: |
6103aa96 JF |
4780 | kfree_skb(skb); |
4781 | return XDP_DROP; | |
d4455169 | 4782 | } |
7c497478 | 4783 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
d4455169 | 4784 | |
ae78dbfa | 4785 | static int netif_rx_internal(struct sk_buff *skb) |
1da177e4 | 4786 | { |
b0e28f1e | 4787 | int ret; |
1da177e4 | 4788 | |
588f0330 | 4789 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
1da177e4 | 4790 | |
cf66ba58 | 4791 | trace_netif_rx(skb); |
d4455169 | 4792 | |
df334545 | 4793 | #ifdef CONFIG_RPS |
dc05360f | 4794 | if (static_branch_unlikely(&rps_needed)) { |
fec5e652 | 4795 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
4796 | int cpu; |
4797 | ||
cece1945 | 4798 | preempt_disable(); |
b0e28f1e | 4799 | rcu_read_lock(); |
fec5e652 TH |
4800 | |
4801 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
4802 | if (cpu < 0) |
4803 | cpu = smp_processor_id(); | |
fec5e652 TH |
4804 | |
4805 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
4806 | ||
b0e28f1e | 4807 | rcu_read_unlock(); |
cece1945 | 4808 | preempt_enable(); |
adc9300e ED |
4809 | } else |
4810 | #endif | |
fec5e652 TH |
4811 | { |
4812 | unsigned int qtail; | |
f4563a75 | 4813 | |
fec5e652 TH |
4814 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); |
4815 | put_cpu(); | |
4816 | } | |
b0e28f1e | 4817 | return ret; |
1da177e4 | 4818 | } |
ae78dbfa BH |
4819 | |
4820 | /** | |
4821 | * netif_rx - post buffer to the network code | |
4822 | * @skb: buffer to post | |
4823 | * | |
4824 | * This function receives a packet from a device driver and queues it for | |
4825 | * the upper (protocol) levels to process. It always succeeds. The buffer | |
4826 | * may be dropped during processing for congestion control or by the | |
4827 | * protocol layers. | |
4828 | * | |
4829 | * return values: | |
4830 | * NET_RX_SUCCESS (no congestion) | |
4831 | * NET_RX_DROP (packet was dropped) | |
4832 | * | |
4833 | */ | |
4834 | ||
4835 | int netif_rx(struct sk_buff *skb) | |
4836 | { | |
b0e3f1bd GB |
4837 | int ret; |
4838 | ||
ae78dbfa BH |
4839 | trace_netif_rx_entry(skb); |
4840 | ||
b0e3f1bd GB |
4841 | ret = netif_rx_internal(skb); |
4842 | trace_netif_rx_exit(ret); | |
4843 | ||
4844 | return ret; | |
ae78dbfa | 4845 | } |
d1b19dff | 4846 | EXPORT_SYMBOL(netif_rx); |
1da177e4 LT |
4847 | |
4848 | int netif_rx_ni(struct sk_buff *skb) | |
4849 | { | |
4850 | int err; | |
4851 | ||
ae78dbfa BH |
4852 | trace_netif_rx_ni_entry(skb); |
4853 | ||
1da177e4 | 4854 | preempt_disable(); |
ae78dbfa | 4855 | err = netif_rx_internal(skb); |
1da177e4 LT |
4856 | if (local_softirq_pending()) |
4857 | do_softirq(); | |
4858 | preempt_enable(); | |
b0e3f1bd | 4859 | trace_netif_rx_ni_exit(err); |
1da177e4 LT |
4860 | |
4861 | return err; | |
4862 | } | |
1da177e4 LT |
4863 | EXPORT_SYMBOL(netif_rx_ni); |
4864 | ||
c11171a4 SAS |
4865 | int netif_rx_any_context(struct sk_buff *skb) |
4866 | { | |
4867 | /* | |
4868 | * If invoked from contexts which do not invoke bottom half | |
4869 | * processing either at return from interrupt or when softrqs are | |
4870 | * reenabled, use netif_rx_ni() which invokes bottomhalf processing | |
4871 | * directly. | |
4872 | */ | |
4873 | if (in_interrupt()) | |
4874 | return netif_rx(skb); | |
4875 | else | |
4876 | return netif_rx_ni(skb); | |
4877 | } | |
4878 | EXPORT_SYMBOL(netif_rx_any_context); | |
4879 | ||
0766f788 | 4880 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
1da177e4 | 4881 | { |
903ceff7 | 4882 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
1da177e4 LT |
4883 | |
4884 | if (sd->completion_queue) { | |
4885 | struct sk_buff *clist; | |
4886 | ||
4887 | local_irq_disable(); | |
4888 | clist = sd->completion_queue; | |
4889 | sd->completion_queue = NULL; | |
4890 | local_irq_enable(); | |
4891 | ||
4892 | while (clist) { | |
4893 | struct sk_buff *skb = clist; | |
f4563a75 | 4894 | |
1da177e4 LT |
4895 | clist = clist->next; |
4896 | ||
63354797 | 4897 | WARN_ON(refcount_read(&skb->users)); |
e6247027 ED |
4898 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED)) |
4899 | trace_consume_skb(skb); | |
4900 | else | |
4901 | trace_kfree_skb(skb, net_tx_action); | |
15fad714 JDB |
4902 | |
4903 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) | |
4904 | __kfree_skb(skb); | |
4905 | else | |
4906 | __kfree_skb_defer(skb); | |
1da177e4 | 4907 | } |
15fad714 JDB |
4908 | |
4909 | __kfree_skb_flush(); | |
1da177e4 LT |
4910 | } |
4911 | ||
4912 | if (sd->output_queue) { | |
37437bb2 | 4913 | struct Qdisc *head; |
1da177e4 LT |
4914 | |
4915 | local_irq_disable(); | |
4916 | head = sd->output_queue; | |
4917 | sd->output_queue = NULL; | |
a9cbd588 | 4918 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
4919 | local_irq_enable(); |
4920 | ||
4921 | while (head) { | |
37437bb2 | 4922 | struct Qdisc *q = head; |
6b3ba914 | 4923 | spinlock_t *root_lock = NULL; |
37437bb2 | 4924 | |
1da177e4 LT |
4925 | head = head->next_sched; |
4926 | ||
6b3ba914 JF |
4927 | if (!(q->flags & TCQ_F_NOLOCK)) { |
4928 | root_lock = qdisc_lock(q); | |
4929 | spin_lock(root_lock); | |
4930 | } | |
3bcb846c ED |
4931 | /* We need to make sure head->next_sched is read |
4932 | * before clearing __QDISC_STATE_SCHED | |
4933 | */ | |
4934 | smp_mb__before_atomic(); | |
4935 | clear_bit(__QDISC_STATE_SCHED, &q->state); | |
4936 | qdisc_run(q); | |
6b3ba914 JF |
4937 | if (root_lock) |
4938 | spin_unlock(root_lock); | |
1da177e4 LT |
4939 | } |
4940 | } | |
f53c7239 SK |
4941 | |
4942 | xfrm_dev_backlog(sd); | |
1da177e4 LT |
4943 | } |
4944 | ||
181402a5 | 4945 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
4946 | /* This hook is defined here for ATM LANE */ |
4947 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
4948 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 4949 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 4950 | #endif |
1da177e4 | 4951 | |
1f211a1b DB |
4952 | static inline struct sk_buff * |
4953 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
9aa1206e | 4954 | struct net_device *orig_dev, bool *another) |
f697c3e8 | 4955 | { |
e7582bab | 4956 | #ifdef CONFIG_NET_CLS_ACT |
46209401 | 4957 | struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress); |
d2788d34 | 4958 | struct tcf_result cl_res; |
24824a09 | 4959 | |
c9e99fd0 DB |
4960 | /* If there's at least one ingress present somewhere (so |
4961 | * we get here via enabled static key), remaining devices | |
4962 | * that are not configured with an ingress qdisc will bail | |
d2788d34 | 4963 | * out here. |
c9e99fd0 | 4964 | */ |
46209401 | 4965 | if (!miniq) |
4577139b | 4966 | return skb; |
46209401 | 4967 | |
f697c3e8 HX |
4968 | if (*pt_prev) { |
4969 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4970 | *pt_prev = NULL; | |
1da177e4 LT |
4971 | } |
4972 | ||
3365495c | 4973 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
aadaca9e | 4974 | qdisc_skb_cb(skb)->mru = 0; |
7baf2429 | 4975 | qdisc_skb_cb(skb)->post_ct = false; |
8dc07fdb | 4976 | skb->tc_at_ingress = 1; |
46209401 | 4977 | mini_qdisc_bstats_cpu_update(miniq, skb); |
c9e99fd0 | 4978 | |
7d17c544 PB |
4979 | switch (tcf_classify_ingress(skb, miniq->block, miniq->filter_list, |
4980 | &cl_res, false)) { | |
d2788d34 DB |
4981 | case TC_ACT_OK: |
4982 | case TC_ACT_RECLASSIFY: | |
4983 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
4984 | break; | |
4985 | case TC_ACT_SHOT: | |
46209401 | 4986 | mini_qdisc_qstats_cpu_drop(miniq); |
8a3a4c6e ED |
4987 | kfree_skb(skb); |
4988 | return NULL; | |
d2788d34 DB |
4989 | case TC_ACT_STOLEN: |
4990 | case TC_ACT_QUEUED: | |
e25ea21f | 4991 | case TC_ACT_TRAP: |
8a3a4c6e | 4992 | consume_skb(skb); |
d2788d34 | 4993 | return NULL; |
27b29f63 AS |
4994 | case TC_ACT_REDIRECT: |
4995 | /* skb_mac_header check was done by cls/act_bpf, so | |
4996 | * we can safely push the L2 header back before | |
4997 | * redirecting to another netdev | |
4998 | */ | |
4999 | __skb_push(skb, skb->mac_len); | |
9aa1206e DB |
5000 | if (skb_do_redirect(skb) == -EAGAIN) { |
5001 | __skb_pull(skb, skb->mac_len); | |
5002 | *another = true; | |
5003 | break; | |
5004 | } | |
27b29f63 | 5005 | return NULL; |
720f22fe | 5006 | case TC_ACT_CONSUMED: |
cd11b164 | 5007 | return NULL; |
d2788d34 DB |
5008 | default: |
5009 | break; | |
f697c3e8 | 5010 | } |
e7582bab | 5011 | #endif /* CONFIG_NET_CLS_ACT */ |
e687ad60 PN |
5012 | return skb; |
5013 | } | |
1da177e4 | 5014 | |
24b27fc4 MB |
5015 | /** |
5016 | * netdev_is_rx_handler_busy - check if receive handler is registered | |
5017 | * @dev: device to check | |
5018 | * | |
5019 | * Check if a receive handler is already registered for a given device. | |
5020 | * Return true if there one. | |
5021 | * | |
5022 | * The caller must hold the rtnl_mutex. | |
5023 | */ | |
5024 | bool netdev_is_rx_handler_busy(struct net_device *dev) | |
5025 | { | |
5026 | ASSERT_RTNL(); | |
5027 | return dev && rtnl_dereference(dev->rx_handler); | |
5028 | } | |
5029 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); | |
5030 | ||
ab95bfe0 JP |
5031 | /** |
5032 | * netdev_rx_handler_register - register receive handler | |
5033 | * @dev: device to register a handler for | |
5034 | * @rx_handler: receive handler to register | |
93e2c32b | 5035 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 | 5036 | * |
e227867f | 5037 | * Register a receive handler for a device. This handler will then be |
ab95bfe0 JP |
5038 | * called from __netif_receive_skb. A negative errno code is returned |
5039 | * on a failure. | |
5040 | * | |
5041 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
5042 | * |
5043 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
5044 | */ |
5045 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
5046 | rx_handler_func_t *rx_handler, |
5047 | void *rx_handler_data) | |
ab95bfe0 | 5048 | { |
1b7cd004 | 5049 | if (netdev_is_rx_handler_busy(dev)) |
ab95bfe0 JP |
5050 | return -EBUSY; |
5051 | ||
f5426250 PA |
5052 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
5053 | return -EINVAL; | |
5054 | ||
00cfec37 | 5055 | /* Note: rx_handler_data must be set before rx_handler */ |
93e2c32b | 5056 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
5057 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
5058 | ||
5059 | return 0; | |
5060 | } | |
5061 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
5062 | ||
5063 | /** | |
5064 | * netdev_rx_handler_unregister - unregister receive handler | |
5065 | * @dev: device to unregister a handler from | |
5066 | * | |
166ec369 | 5067 | * Unregister a receive handler from a device. |
ab95bfe0 JP |
5068 | * |
5069 | * The caller must hold the rtnl_mutex. | |
5070 | */ | |
5071 | void netdev_rx_handler_unregister(struct net_device *dev) | |
5072 | { | |
5073 | ||
5074 | ASSERT_RTNL(); | |
a9b3cd7f | 5075 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
00cfec37 ED |
5076 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
5077 | * section has a guarantee to see a non NULL rx_handler_data | |
5078 | * as well. | |
5079 | */ | |
5080 | synchronize_net(); | |
a9b3cd7f | 5081 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
ab95bfe0 JP |
5082 | } |
5083 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
5084 | ||
b4b9e355 MG |
5085 | /* |
5086 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
5087 | * the special handling of PFMEMALLOC skbs. | |
5088 | */ | |
5089 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
5090 | { | |
5091 | switch (skb->protocol) { | |
2b8837ae JP |
5092 | case htons(ETH_P_ARP): |
5093 | case htons(ETH_P_IP): | |
5094 | case htons(ETH_P_IPV6): | |
5095 | case htons(ETH_P_8021Q): | |
5096 | case htons(ETH_P_8021AD): | |
b4b9e355 MG |
5097 | return true; |
5098 | default: | |
5099 | return false; | |
5100 | } | |
5101 | } | |
5102 | ||
e687ad60 PN |
5103 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
5104 | int *ret, struct net_device *orig_dev) | |
5105 | { | |
5106 | if (nf_hook_ingress_active(skb)) { | |
2c1e2703 AC |
5107 | int ingress_retval; |
5108 | ||
e687ad60 PN |
5109 | if (*pt_prev) { |
5110 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
5111 | *pt_prev = NULL; | |
5112 | } | |
5113 | ||
2c1e2703 AC |
5114 | rcu_read_lock(); |
5115 | ingress_retval = nf_hook_ingress(skb); | |
5116 | rcu_read_unlock(); | |
5117 | return ingress_retval; | |
e687ad60 PN |
5118 | } |
5119 | return 0; | |
5120 | } | |
e687ad60 | 5121 | |
c0bbbdc3 | 5122 | static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, |
88eb1944 | 5123 | struct packet_type **ppt_prev) |
1da177e4 LT |
5124 | { |
5125 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 5126 | rx_handler_func_t *rx_handler; |
c0bbbdc3 | 5127 | struct sk_buff *skb = *pskb; |
f2ccd8fa | 5128 | struct net_device *orig_dev; |
8a4eb573 | 5129 | bool deliver_exact = false; |
1da177e4 | 5130 | int ret = NET_RX_DROP; |
252e3346 | 5131 | __be16 type; |
1da177e4 | 5132 | |
588f0330 | 5133 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
81bbb3d4 | 5134 | |
cf66ba58 | 5135 | trace_netif_receive_skb(skb); |
9b22ea56 | 5136 | |
cc9bd5ce | 5137 | orig_dev = skb->dev; |
8f903c70 | 5138 | |
c1d2bbe1 | 5139 | skb_reset_network_header(skb); |
fda55eca ED |
5140 | if (!skb_transport_header_was_set(skb)) |
5141 | skb_reset_transport_header(skb); | |
0b5c9db1 | 5142 | skb_reset_mac_len(skb); |
1da177e4 LT |
5143 | |
5144 | pt_prev = NULL; | |
5145 | ||
63d8ea7f | 5146 | another_round: |
b6858177 | 5147 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
5148 | |
5149 | __this_cpu_inc(softnet_data.processed); | |
5150 | ||
458bf2f2 SH |
5151 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
5152 | int ret2; | |
5153 | ||
5154 | preempt_disable(); | |
5155 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb); | |
5156 | preempt_enable(); | |
5157 | ||
c0bbbdc3 BS |
5158 | if (ret2 != XDP_PASS) { |
5159 | ret = NET_RX_DROP; | |
5160 | goto out; | |
5161 | } | |
458bf2f2 SH |
5162 | skb_reset_mac_len(skb); |
5163 | } | |
5164 | ||
324cefaf | 5165 | if (eth_type_vlan(skb->protocol)) { |
0d5501c1 | 5166 | skb = skb_vlan_untag(skb); |
bcc6d479 | 5167 | if (unlikely(!skb)) |
2c17d27c | 5168 | goto out; |
bcc6d479 JP |
5169 | } |
5170 | ||
e7246e12 WB |
5171 | if (skb_skip_tc_classify(skb)) |
5172 | goto skip_classify; | |
1da177e4 | 5173 | |
9754e293 | 5174 | if (pfmemalloc) |
b4b9e355 MG |
5175 | goto skip_taps; |
5176 | ||
1da177e4 | 5177 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
7866a621 SN |
5178 | if (pt_prev) |
5179 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5180 | pt_prev = ptype; | |
5181 | } | |
5182 | ||
5183 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { | |
5184 | if (pt_prev) | |
5185 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5186 | pt_prev = ptype; | |
1da177e4 LT |
5187 | } |
5188 | ||
b4b9e355 | 5189 | skip_taps: |
1cf51900 | 5190 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 5191 | if (static_branch_unlikely(&ingress_needed_key)) { |
9aa1206e DB |
5192 | bool another = false; |
5193 | ||
5194 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev, | |
5195 | &another); | |
5196 | if (another) | |
5197 | goto another_round; | |
4577139b | 5198 | if (!skb) |
2c17d27c | 5199 | goto out; |
e687ad60 PN |
5200 | |
5201 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) | |
2c17d27c | 5202 | goto out; |
4577139b | 5203 | } |
1cf51900 | 5204 | #endif |
2c64605b | 5205 | skb_reset_redirect(skb); |
e7246e12 | 5206 | skip_classify: |
9754e293 | 5207 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
b4b9e355 MG |
5208 | goto drop; |
5209 | ||
df8a39de | 5210 | if (skb_vlan_tag_present(skb)) { |
2425717b JF |
5211 | if (pt_prev) { |
5212 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5213 | pt_prev = NULL; | |
5214 | } | |
48cc32d3 | 5215 | if (vlan_do_receive(&skb)) |
2425717b JF |
5216 | goto another_round; |
5217 | else if (unlikely(!skb)) | |
2c17d27c | 5218 | goto out; |
2425717b JF |
5219 | } |
5220 | ||
48cc32d3 | 5221 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
5222 | if (rx_handler) { |
5223 | if (pt_prev) { | |
5224 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5225 | pt_prev = NULL; | |
5226 | } | |
8a4eb573 JP |
5227 | switch (rx_handler(&skb)) { |
5228 | case RX_HANDLER_CONSUMED: | |
3bc1b1ad | 5229 | ret = NET_RX_SUCCESS; |
2c17d27c | 5230 | goto out; |
8a4eb573 | 5231 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 5232 | goto another_round; |
8a4eb573 JP |
5233 | case RX_HANDLER_EXACT: |
5234 | deliver_exact = true; | |
5235 | case RX_HANDLER_PASS: | |
5236 | break; | |
5237 | default: | |
5238 | BUG(); | |
5239 | } | |
ab95bfe0 | 5240 | } |
1da177e4 | 5241 | |
b14a9fc4 | 5242 | if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) { |
36b2f61a GV |
5243 | check_vlan_id: |
5244 | if (skb_vlan_tag_get_id(skb)) { | |
5245 | /* Vlan id is non 0 and vlan_do_receive() above couldn't | |
5246 | * find vlan device. | |
5247 | */ | |
d4b812de | 5248 | skb->pkt_type = PACKET_OTHERHOST; |
324cefaf | 5249 | } else if (eth_type_vlan(skb->protocol)) { |
36b2f61a GV |
5250 | /* Outer header is 802.1P with vlan 0, inner header is |
5251 | * 802.1Q or 802.1AD and vlan_do_receive() above could | |
5252 | * not find vlan dev for vlan id 0. | |
5253 | */ | |
5254 | __vlan_hwaccel_clear_tag(skb); | |
5255 | skb = skb_vlan_untag(skb); | |
5256 | if (unlikely(!skb)) | |
5257 | goto out; | |
5258 | if (vlan_do_receive(&skb)) | |
5259 | /* After stripping off 802.1P header with vlan 0 | |
5260 | * vlan dev is found for inner header. | |
5261 | */ | |
5262 | goto another_round; | |
5263 | else if (unlikely(!skb)) | |
5264 | goto out; | |
5265 | else | |
5266 | /* We have stripped outer 802.1P vlan 0 header. | |
5267 | * But could not find vlan dev. | |
5268 | * check again for vlan id to set OTHERHOST. | |
5269 | */ | |
5270 | goto check_vlan_id; | |
5271 | } | |
d4b812de ED |
5272 | /* Note: we might in the future use prio bits |
5273 | * and set skb->priority like in vlan_do_receive() | |
5274 | * For the time being, just ignore Priority Code Point | |
5275 | */ | |
b1817524 | 5276 | __vlan_hwaccel_clear_tag(skb); |
d4b812de | 5277 | } |
48cc32d3 | 5278 | |
7866a621 SN |
5279 | type = skb->protocol; |
5280 | ||
63d8ea7f | 5281 | /* deliver only exact match when indicated */ |
7866a621 SN |
5282 | if (likely(!deliver_exact)) { |
5283 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5284 | &ptype_base[ntohs(type) & | |
5285 | PTYPE_HASH_MASK]); | |
5286 | } | |
1f3c8804 | 5287 | |
7866a621 SN |
5288 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
5289 | &orig_dev->ptype_specific); | |
5290 | ||
5291 | if (unlikely(skb->dev != orig_dev)) { | |
5292 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5293 | &skb->dev->ptype_specific); | |
1da177e4 LT |
5294 | } |
5295 | ||
5296 | if (pt_prev) { | |
1f8b977a | 5297 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
0e698bf6 | 5298 | goto drop; |
88eb1944 | 5299 | *ppt_prev = pt_prev; |
1da177e4 | 5300 | } else { |
b4b9e355 | 5301 | drop: |
6e7333d3 JW |
5302 | if (!deliver_exact) |
5303 | atomic_long_inc(&skb->dev->rx_dropped); | |
5304 | else | |
5305 | atomic_long_inc(&skb->dev->rx_nohandler); | |
1da177e4 LT |
5306 | kfree_skb(skb); |
5307 | /* Jamal, now you will not able to escape explaining | |
5308 | * me how you were going to use this. :-) | |
5309 | */ | |
5310 | ret = NET_RX_DROP; | |
5311 | } | |
5312 | ||
2c17d27c | 5313 | out: |
c0bbbdc3 BS |
5314 | /* The invariant here is that if *ppt_prev is not NULL |
5315 | * then skb should also be non-NULL. | |
5316 | * | |
5317 | * Apparently *ppt_prev assignment above holds this invariant due to | |
5318 | * skb dereferencing near it. | |
5319 | */ | |
5320 | *pskb = skb; | |
9754e293 DM |
5321 | return ret; |
5322 | } | |
5323 | ||
88eb1944 EC |
5324 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
5325 | { | |
5326 | struct net_device *orig_dev = skb->dev; | |
5327 | struct packet_type *pt_prev = NULL; | |
5328 | int ret; | |
5329 | ||
c0bbbdc3 | 5330 | ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
88eb1944 | 5331 | if (pt_prev) |
f5737cba PA |
5332 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
5333 | skb->dev, pt_prev, orig_dev); | |
88eb1944 EC |
5334 | return ret; |
5335 | } | |
5336 | ||
1c601d82 JDB |
5337 | /** |
5338 | * netif_receive_skb_core - special purpose version of netif_receive_skb | |
5339 | * @skb: buffer to process | |
5340 | * | |
5341 | * More direct receive version of netif_receive_skb(). It should | |
5342 | * only be used by callers that have a need to skip RPS and Generic XDP. | |
2de9780f | 5343 | * Caller must also take care of handling if ``(page_is_)pfmemalloc``. |
1c601d82 JDB |
5344 | * |
5345 | * This function may only be called from softirq context and interrupts | |
5346 | * should be enabled. | |
5347 | * | |
5348 | * Return values (usually ignored): | |
5349 | * NET_RX_SUCCESS: no congestion | |
5350 | * NET_RX_DROP: packet was dropped | |
5351 | */ | |
5352 | int netif_receive_skb_core(struct sk_buff *skb) | |
5353 | { | |
5354 | int ret; | |
5355 | ||
5356 | rcu_read_lock(); | |
88eb1944 | 5357 | ret = __netif_receive_skb_one_core(skb, false); |
1c601d82 JDB |
5358 | rcu_read_unlock(); |
5359 | ||
5360 | return ret; | |
5361 | } | |
5362 | EXPORT_SYMBOL(netif_receive_skb_core); | |
5363 | ||
88eb1944 EC |
5364 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
5365 | struct packet_type *pt_prev, | |
5366 | struct net_device *orig_dev) | |
4ce0017a EC |
5367 | { |
5368 | struct sk_buff *skb, *next; | |
5369 | ||
88eb1944 EC |
5370 | if (!pt_prev) |
5371 | return; | |
5372 | if (list_empty(head)) | |
5373 | return; | |
17266ee9 | 5374 | if (pt_prev->list_func != NULL) |
fdf71426 PA |
5375 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
5376 | ip_list_rcv, head, pt_prev, orig_dev); | |
17266ee9 | 5377 | else |
9a5a90d1 AL |
5378 | list_for_each_entry_safe(skb, next, head, list) { |
5379 | skb_list_del_init(skb); | |
fdf71426 | 5380 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
9a5a90d1 | 5381 | } |
88eb1944 EC |
5382 | } |
5383 | ||
5384 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) | |
5385 | { | |
5386 | /* Fast-path assumptions: | |
5387 | * - There is no RX handler. | |
5388 | * - Only one packet_type matches. | |
5389 | * If either of these fails, we will end up doing some per-packet | |
5390 | * processing in-line, then handling the 'last ptype' for the whole | |
5391 | * sublist. This can't cause out-of-order delivery to any single ptype, | |
5392 | * because the 'last ptype' must be constant across the sublist, and all | |
5393 | * other ptypes are handled per-packet. | |
5394 | */ | |
5395 | /* Current (common) ptype of sublist */ | |
5396 | struct packet_type *pt_curr = NULL; | |
5397 | /* Current (common) orig_dev of sublist */ | |
5398 | struct net_device *od_curr = NULL; | |
5399 | struct list_head sublist; | |
5400 | struct sk_buff *skb, *next; | |
5401 | ||
9af86f93 | 5402 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5403 | list_for_each_entry_safe(skb, next, head, list) { |
5404 | struct net_device *orig_dev = skb->dev; | |
5405 | struct packet_type *pt_prev = NULL; | |
5406 | ||
22f6bbb7 | 5407 | skb_list_del_init(skb); |
c0bbbdc3 | 5408 | __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
9af86f93 EC |
5409 | if (!pt_prev) |
5410 | continue; | |
88eb1944 EC |
5411 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
5412 | /* dispatch old sublist */ | |
88eb1944 EC |
5413 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
5414 | /* start new sublist */ | |
9af86f93 | 5415 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5416 | pt_curr = pt_prev; |
5417 | od_curr = orig_dev; | |
5418 | } | |
9af86f93 | 5419 | list_add_tail(&skb->list, &sublist); |
88eb1944 EC |
5420 | } |
5421 | ||
5422 | /* dispatch final sublist */ | |
9af86f93 | 5423 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
4ce0017a EC |
5424 | } |
5425 | ||
9754e293 DM |
5426 | static int __netif_receive_skb(struct sk_buff *skb) |
5427 | { | |
5428 | int ret; | |
5429 | ||
5430 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { | |
f1083048 | 5431 | unsigned int noreclaim_flag; |
9754e293 DM |
5432 | |
5433 | /* | |
5434 | * PFMEMALLOC skbs are special, they should | |
5435 | * - be delivered to SOCK_MEMALLOC sockets only | |
5436 | * - stay away from userspace | |
5437 | * - have bounded memory usage | |
5438 | * | |
5439 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
5440 | * context down to all allocation sites. | |
5441 | */ | |
f1083048 | 5442 | noreclaim_flag = memalloc_noreclaim_save(); |
88eb1944 | 5443 | ret = __netif_receive_skb_one_core(skb, true); |
f1083048 | 5444 | memalloc_noreclaim_restore(noreclaim_flag); |
9754e293 | 5445 | } else |
88eb1944 | 5446 | ret = __netif_receive_skb_one_core(skb, false); |
9754e293 | 5447 | |
1da177e4 LT |
5448 | return ret; |
5449 | } | |
0a9627f2 | 5450 | |
4ce0017a EC |
5451 | static void __netif_receive_skb_list(struct list_head *head) |
5452 | { | |
5453 | unsigned long noreclaim_flag = 0; | |
5454 | struct sk_buff *skb, *next; | |
5455 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ | |
5456 | ||
5457 | list_for_each_entry_safe(skb, next, head, list) { | |
5458 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { | |
5459 | struct list_head sublist; | |
5460 | ||
5461 | /* Handle the previous sublist */ | |
5462 | list_cut_before(&sublist, head, &skb->list); | |
b9f463d6 EC |
5463 | if (!list_empty(&sublist)) |
5464 | __netif_receive_skb_list_core(&sublist, pfmemalloc); | |
4ce0017a EC |
5465 | pfmemalloc = !pfmemalloc; |
5466 | /* See comments in __netif_receive_skb */ | |
5467 | if (pfmemalloc) | |
5468 | noreclaim_flag = memalloc_noreclaim_save(); | |
5469 | else | |
5470 | memalloc_noreclaim_restore(noreclaim_flag); | |
5471 | } | |
5472 | } | |
5473 | /* Handle the remaining sublist */ | |
b9f463d6 EC |
5474 | if (!list_empty(head)) |
5475 | __netif_receive_skb_list_core(head, pfmemalloc); | |
4ce0017a EC |
5476 | /* Restore pflags */ |
5477 | if (pfmemalloc) | |
5478 | memalloc_noreclaim_restore(noreclaim_flag); | |
5479 | } | |
5480 | ||
f4e63525 | 5481 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
b5cdae32 | 5482 | { |
58038695 | 5483 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
b5cdae32 DM |
5484 | struct bpf_prog *new = xdp->prog; |
5485 | int ret = 0; | |
5486 | ||
fbee97fe DA |
5487 | if (new) { |
5488 | u32 i; | |
5489 | ||
984fe94f YZ |
5490 | mutex_lock(&new->aux->used_maps_mutex); |
5491 | ||
fbee97fe DA |
5492 | /* generic XDP does not work with DEVMAPs that can |
5493 | * have a bpf_prog installed on an entry | |
5494 | */ | |
5495 | for (i = 0; i < new->aux->used_map_cnt; i++) { | |
984fe94f YZ |
5496 | if (dev_map_can_have_prog(new->aux->used_maps[i]) || |
5497 | cpu_map_prog_allowed(new->aux->used_maps[i])) { | |
5498 | mutex_unlock(&new->aux->used_maps_mutex); | |
92164774 | 5499 | return -EINVAL; |
984fe94f | 5500 | } |
fbee97fe | 5501 | } |
984fe94f YZ |
5502 | |
5503 | mutex_unlock(&new->aux->used_maps_mutex); | |
fbee97fe DA |
5504 | } |
5505 | ||
b5cdae32 | 5506 | switch (xdp->command) { |
58038695 | 5507 | case XDP_SETUP_PROG: |
b5cdae32 DM |
5508 | rcu_assign_pointer(dev->xdp_prog, new); |
5509 | if (old) | |
5510 | bpf_prog_put(old); | |
5511 | ||
5512 | if (old && !new) { | |
02786475 | 5513 | static_branch_dec(&generic_xdp_needed_key); |
b5cdae32 | 5514 | } else if (new && !old) { |
02786475 | 5515 | static_branch_inc(&generic_xdp_needed_key); |
b5cdae32 | 5516 | dev_disable_lro(dev); |
56f5aa77 | 5517 | dev_disable_gro_hw(dev); |
b5cdae32 DM |
5518 | } |
5519 | break; | |
b5cdae32 | 5520 | |
b5cdae32 DM |
5521 | default: |
5522 | ret = -EINVAL; | |
5523 | break; | |
5524 | } | |
5525 | ||
5526 | return ret; | |
5527 | } | |
5528 | ||
ae78dbfa | 5529 | static int netif_receive_skb_internal(struct sk_buff *skb) |
0a9627f2 | 5530 | { |
2c17d27c JA |
5531 | int ret; |
5532 | ||
588f0330 | 5533 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
3b098e2d | 5534 | |
c1f19b51 RC |
5535 | if (skb_defer_rx_timestamp(skb)) |
5536 | return NET_RX_SUCCESS; | |
5537 | ||
bbbe211c | 5538 | rcu_read_lock(); |
df334545 | 5539 | #ifdef CONFIG_RPS |
dc05360f | 5540 | if (static_branch_unlikely(&rps_needed)) { |
3b098e2d | 5541 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
2c17d27c | 5542 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
0a9627f2 | 5543 | |
3b098e2d ED |
5544 | if (cpu >= 0) { |
5545 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5546 | rcu_read_unlock(); | |
adc9300e | 5547 | return ret; |
3b098e2d | 5548 | } |
fec5e652 | 5549 | } |
1e94d72f | 5550 | #endif |
2c17d27c JA |
5551 | ret = __netif_receive_skb(skb); |
5552 | rcu_read_unlock(); | |
5553 | return ret; | |
0a9627f2 | 5554 | } |
ae78dbfa | 5555 | |
7da517a3 EC |
5556 | static void netif_receive_skb_list_internal(struct list_head *head) |
5557 | { | |
7da517a3 | 5558 | struct sk_buff *skb, *next; |
8c057efa | 5559 | struct list_head sublist; |
7da517a3 | 5560 | |
8c057efa | 5561 | INIT_LIST_HEAD(&sublist); |
7da517a3 EC |
5562 | list_for_each_entry_safe(skb, next, head, list) { |
5563 | net_timestamp_check(netdev_tstamp_prequeue, skb); | |
22f6bbb7 | 5564 | skb_list_del_init(skb); |
8c057efa EC |
5565 | if (!skb_defer_rx_timestamp(skb)) |
5566 | list_add_tail(&skb->list, &sublist); | |
7da517a3 | 5567 | } |
8c057efa | 5568 | list_splice_init(&sublist, head); |
7da517a3 | 5569 | |
7da517a3 EC |
5570 | rcu_read_lock(); |
5571 | #ifdef CONFIG_RPS | |
dc05360f | 5572 | if (static_branch_unlikely(&rps_needed)) { |
7da517a3 EC |
5573 | list_for_each_entry_safe(skb, next, head, list) { |
5574 | struct rps_dev_flow voidflow, *rflow = &voidflow; | |
5575 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
5576 | ||
5577 | if (cpu >= 0) { | |
8c057efa | 5578 | /* Will be handled, remove from list */ |
22f6bbb7 | 5579 | skb_list_del_init(skb); |
8c057efa | 5580 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
7da517a3 EC |
5581 | } |
5582 | } | |
5583 | } | |
5584 | #endif | |
5585 | __netif_receive_skb_list(head); | |
5586 | rcu_read_unlock(); | |
5587 | } | |
5588 | ||
ae78dbfa BH |
5589 | /** |
5590 | * netif_receive_skb - process receive buffer from network | |
5591 | * @skb: buffer to process | |
5592 | * | |
5593 | * netif_receive_skb() is the main receive data processing function. | |
5594 | * It always succeeds. The buffer may be dropped during processing | |
5595 | * for congestion control or by the protocol layers. | |
5596 | * | |
5597 | * This function may only be called from softirq context and interrupts | |
5598 | * should be enabled. | |
5599 | * | |
5600 | * Return values (usually ignored): | |
5601 | * NET_RX_SUCCESS: no congestion | |
5602 | * NET_RX_DROP: packet was dropped | |
5603 | */ | |
04eb4489 | 5604 | int netif_receive_skb(struct sk_buff *skb) |
ae78dbfa | 5605 | { |
b0e3f1bd GB |
5606 | int ret; |
5607 | ||
ae78dbfa BH |
5608 | trace_netif_receive_skb_entry(skb); |
5609 | ||
b0e3f1bd GB |
5610 | ret = netif_receive_skb_internal(skb); |
5611 | trace_netif_receive_skb_exit(ret); | |
5612 | ||
5613 | return ret; | |
ae78dbfa | 5614 | } |
04eb4489 | 5615 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 5616 | |
f6ad8c1b EC |
5617 | /** |
5618 | * netif_receive_skb_list - process many receive buffers from network | |
5619 | * @head: list of skbs to process. | |
5620 | * | |
7da517a3 EC |
5621 | * Since return value of netif_receive_skb() is normally ignored, and |
5622 | * wouldn't be meaningful for a list, this function returns void. | |
f6ad8c1b EC |
5623 | * |
5624 | * This function may only be called from softirq context and interrupts | |
5625 | * should be enabled. | |
5626 | */ | |
5627 | void netif_receive_skb_list(struct list_head *head) | |
5628 | { | |
7da517a3 | 5629 | struct sk_buff *skb; |
f6ad8c1b | 5630 | |
b9f463d6 EC |
5631 | if (list_empty(head)) |
5632 | return; | |
b0e3f1bd GB |
5633 | if (trace_netif_receive_skb_list_entry_enabled()) { |
5634 | list_for_each_entry(skb, head, list) | |
5635 | trace_netif_receive_skb_list_entry(skb); | |
5636 | } | |
7da517a3 | 5637 | netif_receive_skb_list_internal(head); |
b0e3f1bd | 5638 | trace_netif_receive_skb_list_exit(0); |
f6ad8c1b EC |
5639 | } |
5640 | EXPORT_SYMBOL(netif_receive_skb_list); | |
5641 | ||
ce1e2a77 | 5642 | static DEFINE_PER_CPU(struct work_struct, flush_works); |
145dd5f9 PA |
5643 | |
5644 | /* Network device is going away, flush any packets still pending */ | |
5645 | static void flush_backlog(struct work_struct *work) | |
6e583ce5 | 5646 | { |
6e583ce5 | 5647 | struct sk_buff *skb, *tmp; |
145dd5f9 PA |
5648 | struct softnet_data *sd; |
5649 | ||
5650 | local_bh_disable(); | |
5651 | sd = this_cpu_ptr(&softnet_data); | |
6e583ce5 | 5652 | |
145dd5f9 | 5653 | local_irq_disable(); |
e36fa2f7 | 5654 | rps_lock(sd); |
6e7676c1 | 5655 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
41852497 | 5656 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
e36fa2f7 | 5657 | __skb_unlink(skb, &sd->input_pkt_queue); |
7df5cb75 | 5658 | dev_kfree_skb_irq(skb); |
76cc8b13 | 5659 | input_queue_head_incr(sd); |
6e583ce5 | 5660 | } |
6e7676c1 | 5661 | } |
e36fa2f7 | 5662 | rps_unlock(sd); |
145dd5f9 | 5663 | local_irq_enable(); |
6e7676c1 CG |
5664 | |
5665 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
41852497 | 5666 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
6e7676c1 CG |
5667 | __skb_unlink(skb, &sd->process_queue); |
5668 | kfree_skb(skb); | |
76cc8b13 | 5669 | input_queue_head_incr(sd); |
6e7676c1 CG |
5670 | } |
5671 | } | |
145dd5f9 PA |
5672 | local_bh_enable(); |
5673 | } | |
5674 | ||
2de79ee2 PA |
5675 | static bool flush_required(int cpu) |
5676 | { | |
5677 | #if IS_ENABLED(CONFIG_RPS) | |
5678 | struct softnet_data *sd = &per_cpu(softnet_data, cpu); | |
5679 | bool do_flush; | |
5680 | ||
5681 | local_irq_disable(); | |
5682 | rps_lock(sd); | |
5683 | ||
5684 | /* as insertion into process_queue happens with the rps lock held, | |
5685 | * process_queue access may race only with dequeue | |
5686 | */ | |
5687 | do_flush = !skb_queue_empty(&sd->input_pkt_queue) || | |
5688 | !skb_queue_empty_lockless(&sd->process_queue); | |
5689 | rps_unlock(sd); | |
5690 | local_irq_enable(); | |
5691 | ||
5692 | return do_flush; | |
5693 | #endif | |
5694 | /* without RPS we can't safely check input_pkt_queue: during a | |
5695 | * concurrent remote skb_queue_splice() we can detect as empty both | |
5696 | * input_pkt_queue and process_queue even if the latter could end-up | |
5697 | * containing a lot of packets. | |
5698 | */ | |
5699 | return true; | |
5700 | } | |
5701 | ||
41852497 | 5702 | static void flush_all_backlogs(void) |
145dd5f9 | 5703 | { |
2de79ee2 | 5704 | static cpumask_t flush_cpus; |
145dd5f9 PA |
5705 | unsigned int cpu; |
5706 | ||
2de79ee2 PA |
5707 | /* since we are under rtnl lock protection we can use static data |
5708 | * for the cpumask and avoid allocating on stack the possibly | |
5709 | * large mask | |
5710 | */ | |
5711 | ASSERT_RTNL(); | |
5712 | ||
145dd5f9 PA |
5713 | get_online_cpus(); |
5714 | ||
2de79ee2 PA |
5715 | cpumask_clear(&flush_cpus); |
5716 | for_each_online_cpu(cpu) { | |
5717 | if (flush_required(cpu)) { | |
5718 | queue_work_on(cpu, system_highpri_wq, | |
5719 | per_cpu_ptr(&flush_works, cpu)); | |
5720 | cpumask_set_cpu(cpu, &flush_cpus); | |
5721 | } | |
5722 | } | |
145dd5f9 | 5723 | |
2de79ee2 | 5724 | /* we can have in flight packet[s] on the cpus we are not flushing, |
0cbe1e57 | 5725 | * synchronize_net() in unregister_netdevice_many() will take care of |
2de79ee2 PA |
5726 | * them |
5727 | */ | |
5728 | for_each_cpu(cpu, &flush_cpus) | |
41852497 | 5729 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
145dd5f9 PA |
5730 | |
5731 | put_online_cpus(); | |
6e583ce5 SH |
5732 | } |
5733 | ||
c8079432 MM |
5734 | /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */ |
5735 | static void gro_normal_list(struct napi_struct *napi) | |
5736 | { | |
5737 | if (!napi->rx_count) | |
5738 | return; | |
5739 | netif_receive_skb_list_internal(&napi->rx_list); | |
5740 | INIT_LIST_HEAD(&napi->rx_list); | |
5741 | napi->rx_count = 0; | |
5742 | } | |
5743 | ||
5744 | /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded, | |
5745 | * pass the whole batch up to the stack. | |
5746 | */ | |
5747 | static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb) | |
5748 | { | |
5749 | list_add_tail(&skb->list, &napi->rx_list); | |
5750 | if (++napi->rx_count >= gro_normal_batch) | |
5751 | gro_normal_list(napi); | |
5752 | } | |
5753 | ||
aaa5d90b PA |
5754 | INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int)); |
5755 | INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int)); | |
c8079432 | 5756 | static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5757 | { |
22061d80 | 5758 | struct packet_offload *ptype; |
d565b0a1 | 5759 | __be16 type = skb->protocol; |
22061d80 | 5760 | struct list_head *head = &offload_base; |
d565b0a1 HX |
5761 | int err = -ENOENT; |
5762 | ||
c3c7c254 ED |
5763 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
5764 | ||
fc59f9a3 HX |
5765 | if (NAPI_GRO_CB(skb)->count == 1) { |
5766 | skb_shinfo(skb)->gso_size = 0; | |
d565b0a1 | 5767 | goto out; |
fc59f9a3 | 5768 | } |
d565b0a1 HX |
5769 | |
5770 | rcu_read_lock(); | |
5771 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5772 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
d565b0a1 HX |
5773 | continue; |
5774 | ||
aaa5d90b PA |
5775 | err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete, |
5776 | ipv6_gro_complete, inet_gro_complete, | |
5777 | skb, 0); | |
d565b0a1 HX |
5778 | break; |
5779 | } | |
5780 | rcu_read_unlock(); | |
5781 | ||
5782 | if (err) { | |
5783 | WARN_ON(&ptype->list == head); | |
5784 | kfree_skb(skb); | |
5785 | return NET_RX_SUCCESS; | |
5786 | } | |
5787 | ||
5788 | out: | |
c8079432 MM |
5789 | gro_normal_one(napi, skb); |
5790 | return NET_RX_SUCCESS; | |
d565b0a1 HX |
5791 | } |
5792 | ||
6312fe77 | 5793 | static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index, |
07d78363 | 5794 | bool flush_old) |
d565b0a1 | 5795 | { |
6312fe77 | 5796 | struct list_head *head = &napi->gro_hash[index].list; |
d4546c25 | 5797 | struct sk_buff *skb, *p; |
2e71a6f8 | 5798 | |
07d78363 | 5799 | list_for_each_entry_safe_reverse(skb, p, head, list) { |
2e71a6f8 ED |
5800 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) |
5801 | return; | |
992cba7e | 5802 | skb_list_del_init(skb); |
c8079432 | 5803 | napi_gro_complete(napi, skb); |
6312fe77 | 5804 | napi->gro_hash[index].count--; |
d565b0a1 | 5805 | } |
d9f37d01 LR |
5806 | |
5807 | if (!napi->gro_hash[index].count) | |
5808 | __clear_bit(index, &napi->gro_bitmask); | |
d565b0a1 | 5809 | } |
07d78363 | 5810 | |
6312fe77 | 5811 | /* napi->gro_hash[].list contains packets ordered by age. |
07d78363 DM |
5812 | * youngest packets at the head of it. |
5813 | * Complete skbs in reverse order to reduce latencies. | |
5814 | */ | |
5815 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) | |
5816 | { | |
42519ede ED |
5817 | unsigned long bitmask = napi->gro_bitmask; |
5818 | unsigned int i, base = ~0U; | |
07d78363 | 5819 | |
42519ede ED |
5820 | while ((i = ffs(bitmask)) != 0) { |
5821 | bitmask >>= i; | |
5822 | base += i; | |
5823 | __napi_gro_flush_chain(napi, base, flush_old); | |
d9f37d01 | 5824 | } |
07d78363 | 5825 | } |
86cac58b | 5826 | EXPORT_SYMBOL(napi_gro_flush); |
d565b0a1 | 5827 | |
07d78363 DM |
5828 | static struct list_head *gro_list_prepare(struct napi_struct *napi, |
5829 | struct sk_buff *skb) | |
89c5fa33 | 5830 | { |
89c5fa33 | 5831 | unsigned int maclen = skb->dev->hard_header_len; |
0b4cec8c | 5832 | u32 hash = skb_get_hash_raw(skb); |
07d78363 | 5833 | struct list_head *head; |
d4546c25 | 5834 | struct sk_buff *p; |
89c5fa33 | 5835 | |
6312fe77 | 5836 | head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list; |
07d78363 | 5837 | list_for_each_entry(p, head, list) { |
89c5fa33 ED |
5838 | unsigned long diffs; |
5839 | ||
0b4cec8c TH |
5840 | NAPI_GRO_CB(p)->flush = 0; |
5841 | ||
5842 | if (hash != skb_get_hash_raw(p)) { | |
5843 | NAPI_GRO_CB(p)->same_flow = 0; | |
5844 | continue; | |
5845 | } | |
5846 | ||
89c5fa33 | 5847 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; |
b1817524 MM |
5848 | diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb); |
5849 | if (skb_vlan_tag_present(p)) | |
fc5141cb | 5850 | diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb); |
ce87fc6c | 5851 | diffs |= skb_metadata_dst_cmp(p, skb); |
de8f3a83 | 5852 | diffs |= skb_metadata_differs(p, skb); |
89c5fa33 ED |
5853 | if (maclen == ETH_HLEN) |
5854 | diffs |= compare_ether_header(skb_mac_header(p), | |
a50e233c | 5855 | skb_mac_header(skb)); |
89c5fa33 ED |
5856 | else if (!diffs) |
5857 | diffs = memcmp(skb_mac_header(p), | |
a50e233c | 5858 | skb_mac_header(skb), |
89c5fa33 ED |
5859 | maclen); |
5860 | NAPI_GRO_CB(p)->same_flow = !diffs; | |
89c5fa33 | 5861 | } |
07d78363 DM |
5862 | |
5863 | return head; | |
89c5fa33 ED |
5864 | } |
5865 | ||
299603e8 JC |
5866 | static void skb_gro_reset_offset(struct sk_buff *skb) |
5867 | { | |
5868 | const struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5869 | const skb_frag_t *frag0 = &pinfo->frags[0]; | |
5870 | ||
5871 | NAPI_GRO_CB(skb)->data_offset = 0; | |
5872 | NAPI_GRO_CB(skb)->frag0 = NULL; | |
5873 | NAPI_GRO_CB(skb)->frag0_len = 0; | |
5874 | ||
8aef998d | 5875 | if (!skb_headlen(skb) && pinfo->nr_frags && |
299603e8 JC |
5876 | !PageHighMem(skb_frag_page(frag0))) { |
5877 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); | |
7cfd5fd5 ED |
5878 | NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int, |
5879 | skb_frag_size(frag0), | |
5880 | skb->end - skb->tail); | |
89c5fa33 ED |
5881 | } |
5882 | } | |
5883 | ||
a50e233c ED |
5884 | static void gro_pull_from_frag0(struct sk_buff *skb, int grow) |
5885 | { | |
5886 | struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5887 | ||
5888 | BUG_ON(skb->end - skb->tail < grow); | |
5889 | ||
5890 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); | |
5891 | ||
5892 | skb->data_len -= grow; | |
5893 | skb->tail += grow; | |
5894 | ||
b54c9d5b | 5895 | skb_frag_off_add(&pinfo->frags[0], grow); |
a50e233c ED |
5896 | skb_frag_size_sub(&pinfo->frags[0], grow); |
5897 | ||
5898 | if (unlikely(!skb_frag_size(&pinfo->frags[0]))) { | |
5899 | skb_frag_unref(skb, 0); | |
5900 | memmove(pinfo->frags, pinfo->frags + 1, | |
5901 | --pinfo->nr_frags * sizeof(pinfo->frags[0])); | |
5902 | } | |
5903 | } | |
5904 | ||
c8079432 | 5905 | static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head) |
07d78363 | 5906 | { |
6312fe77 | 5907 | struct sk_buff *oldest; |
07d78363 | 5908 | |
6312fe77 | 5909 | oldest = list_last_entry(head, struct sk_buff, list); |
07d78363 | 5910 | |
6312fe77 | 5911 | /* We are called with head length >= MAX_GRO_SKBS, so this is |
07d78363 DM |
5912 | * impossible. |
5913 | */ | |
5914 | if (WARN_ON_ONCE(!oldest)) | |
5915 | return; | |
5916 | ||
d9f37d01 LR |
5917 | /* Do not adjust napi->gro_hash[].count, caller is adding a new |
5918 | * SKB to the chain. | |
07d78363 | 5919 | */ |
ece23711 | 5920 | skb_list_del_init(oldest); |
c8079432 | 5921 | napi_gro_complete(napi, oldest); |
07d78363 DM |
5922 | } |
5923 | ||
aaa5d90b PA |
5924 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *, |
5925 | struct sk_buff *)); | |
5926 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *, | |
5927 | struct sk_buff *)); | |
bb728820 | 5928 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5929 | { |
6312fe77 | 5930 | u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1); |
d4546c25 | 5931 | struct list_head *head = &offload_base; |
22061d80 | 5932 | struct packet_offload *ptype; |
d565b0a1 | 5933 | __be16 type = skb->protocol; |
07d78363 | 5934 | struct list_head *gro_head; |
d4546c25 | 5935 | struct sk_buff *pp = NULL; |
5b252f0c | 5936 | enum gro_result ret; |
d4546c25 | 5937 | int same_flow; |
a50e233c | 5938 | int grow; |
d565b0a1 | 5939 | |
b5cdae32 | 5940 | if (netif_elide_gro(skb->dev)) |
d565b0a1 HX |
5941 | goto normal; |
5942 | ||
07d78363 | 5943 | gro_head = gro_list_prepare(napi, skb); |
89c5fa33 | 5944 | |
d565b0a1 HX |
5945 | rcu_read_lock(); |
5946 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5947 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
d565b0a1 HX |
5948 | continue; |
5949 | ||
86911732 | 5950 | skb_set_network_header(skb, skb_gro_offset(skb)); |
efd9450e | 5951 | skb_reset_mac_len(skb); |
d565b0a1 | 5952 | NAPI_GRO_CB(skb)->same_flow = 0; |
d61d072e | 5953 | NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb); |
5d38a079 | 5954 | NAPI_GRO_CB(skb)->free = 0; |
fac8e0f5 | 5955 | NAPI_GRO_CB(skb)->encap_mark = 0; |
fcd91dd4 | 5956 | NAPI_GRO_CB(skb)->recursion_counter = 0; |
a0ca153f | 5957 | NAPI_GRO_CB(skb)->is_fou = 0; |
1530545e | 5958 | NAPI_GRO_CB(skb)->is_atomic = 1; |
15e2396d | 5959 | NAPI_GRO_CB(skb)->gro_remcsum_start = 0; |
d565b0a1 | 5960 | |
662880f4 TH |
5961 | /* Setup for GRO checksum validation */ |
5962 | switch (skb->ip_summed) { | |
5963 | case CHECKSUM_COMPLETE: | |
5964 | NAPI_GRO_CB(skb)->csum = skb->csum; | |
5965 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
5966 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5967 | break; | |
5968 | case CHECKSUM_UNNECESSARY: | |
5969 | NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1; | |
5970 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5971 | break; | |
5972 | default: | |
5973 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5974 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5975 | } | |
d565b0a1 | 5976 | |
aaa5d90b PA |
5977 | pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive, |
5978 | ipv6_gro_receive, inet_gro_receive, | |
5979 | gro_head, skb); | |
d565b0a1 HX |
5980 | break; |
5981 | } | |
5982 | rcu_read_unlock(); | |
5983 | ||
5984 | if (&ptype->list == head) | |
5985 | goto normal; | |
5986 | ||
45586c70 | 5987 | if (PTR_ERR(pp) == -EINPROGRESS) { |
25393d3f SK |
5988 | ret = GRO_CONSUMED; |
5989 | goto ok; | |
5990 | } | |
5991 | ||
0da2afd5 | 5992 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
5d0d9be8 | 5993 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
0da2afd5 | 5994 | |
d565b0a1 | 5995 | if (pp) { |
992cba7e | 5996 | skb_list_del_init(pp); |
c8079432 | 5997 | napi_gro_complete(napi, pp); |
6312fe77 | 5998 | napi->gro_hash[hash].count--; |
d565b0a1 HX |
5999 | } |
6000 | ||
0da2afd5 | 6001 | if (same_flow) |
d565b0a1 HX |
6002 | goto ok; |
6003 | ||
600adc18 | 6004 | if (NAPI_GRO_CB(skb)->flush) |
d565b0a1 | 6005 | goto normal; |
d565b0a1 | 6006 | |
6312fe77 | 6007 | if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) { |
c8079432 | 6008 | gro_flush_oldest(napi, gro_head); |
600adc18 | 6009 | } else { |
6312fe77 | 6010 | napi->gro_hash[hash].count++; |
600adc18 | 6011 | } |
d565b0a1 | 6012 | NAPI_GRO_CB(skb)->count = 1; |
2e71a6f8 | 6013 | NAPI_GRO_CB(skb)->age = jiffies; |
29e98242 | 6014 | NAPI_GRO_CB(skb)->last = skb; |
86911732 | 6015 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
07d78363 | 6016 | list_add(&skb->list, gro_head); |
5d0d9be8 | 6017 | ret = GRO_HELD; |
d565b0a1 | 6018 | |
ad0f9904 | 6019 | pull: |
a50e233c ED |
6020 | grow = skb_gro_offset(skb) - skb_headlen(skb); |
6021 | if (grow > 0) | |
6022 | gro_pull_from_frag0(skb, grow); | |
d565b0a1 | 6023 | ok: |
d9f37d01 LR |
6024 | if (napi->gro_hash[hash].count) { |
6025 | if (!test_bit(hash, &napi->gro_bitmask)) | |
6026 | __set_bit(hash, &napi->gro_bitmask); | |
6027 | } else if (test_bit(hash, &napi->gro_bitmask)) { | |
6028 | __clear_bit(hash, &napi->gro_bitmask); | |
6029 | } | |
6030 | ||
5d0d9be8 | 6031 | return ret; |
d565b0a1 HX |
6032 | |
6033 | normal: | |
ad0f9904 HX |
6034 | ret = GRO_NORMAL; |
6035 | goto pull; | |
5d38a079 | 6036 | } |
96e93eab | 6037 | |
bf5a755f JC |
6038 | struct packet_offload *gro_find_receive_by_type(__be16 type) |
6039 | { | |
6040 | struct list_head *offload_head = &offload_base; | |
6041 | struct packet_offload *ptype; | |
6042 | ||
6043 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
6044 | if (ptype->type != type || !ptype->callbacks.gro_receive) | |
6045 | continue; | |
6046 | return ptype; | |
6047 | } | |
6048 | return NULL; | |
6049 | } | |
e27a2f83 | 6050 | EXPORT_SYMBOL(gro_find_receive_by_type); |
bf5a755f JC |
6051 | |
6052 | struct packet_offload *gro_find_complete_by_type(__be16 type) | |
6053 | { | |
6054 | struct list_head *offload_head = &offload_base; | |
6055 | struct packet_offload *ptype; | |
6056 | ||
6057 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
6058 | if (ptype->type != type || !ptype->callbacks.gro_complete) | |
6059 | continue; | |
6060 | return ptype; | |
6061 | } | |
6062 | return NULL; | |
6063 | } | |
e27a2f83 | 6064 | EXPORT_SYMBOL(gro_find_complete_by_type); |
5d38a079 | 6065 | |
e44699d2 MK |
6066 | static void napi_skb_free_stolen_head(struct sk_buff *skb) |
6067 | { | |
6068 | skb_dst_drop(skb); | |
174e2381 | 6069 | skb_ext_put(skb); |
e44699d2 MK |
6070 | kmem_cache_free(skbuff_head_cache, skb); |
6071 | } | |
6072 | ||
6570bc79 AL |
6073 | static gro_result_t napi_skb_finish(struct napi_struct *napi, |
6074 | struct sk_buff *skb, | |
6075 | gro_result_t ret) | |
5d38a079 | 6076 | { |
5d0d9be8 HX |
6077 | switch (ret) { |
6078 | case GRO_NORMAL: | |
6570bc79 | 6079 | gro_normal_one(napi, skb); |
c7c4b3b6 | 6080 | break; |
5d38a079 | 6081 | |
daa86548 | 6082 | case GRO_MERGED_FREE: |
e44699d2 MK |
6083 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
6084 | napi_skb_free_stolen_head(skb); | |
6085 | else | |
d7e8883c | 6086 | __kfree_skb(skb); |
daa86548 ED |
6087 | break; |
6088 | ||
5b252f0c BH |
6089 | case GRO_HELD: |
6090 | case GRO_MERGED: | |
25393d3f | 6091 | case GRO_CONSUMED: |
5b252f0c | 6092 | break; |
5d38a079 HX |
6093 | } |
6094 | ||
c7c4b3b6 | 6095 | return ret; |
5d0d9be8 | 6096 | } |
5d0d9be8 | 6097 | |
c7c4b3b6 | 6098 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
5d0d9be8 | 6099 | { |
b0e3f1bd GB |
6100 | gro_result_t ret; |
6101 | ||
93f93a44 | 6102 | skb_mark_napi_id(skb, napi); |
ae78dbfa | 6103 | trace_napi_gro_receive_entry(skb); |
86911732 | 6104 | |
a50e233c ED |
6105 | skb_gro_reset_offset(skb); |
6106 | ||
6570bc79 | 6107 | ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb)); |
b0e3f1bd GB |
6108 | trace_napi_gro_receive_exit(ret); |
6109 | ||
6110 | return ret; | |
d565b0a1 HX |
6111 | } |
6112 | EXPORT_SYMBOL(napi_gro_receive); | |
6113 | ||
d0c2b0d2 | 6114 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
96e93eab | 6115 | { |
93a35f59 ED |
6116 | if (unlikely(skb->pfmemalloc)) { |
6117 | consume_skb(skb); | |
6118 | return; | |
6119 | } | |
96e93eab | 6120 | __skb_pull(skb, skb_headlen(skb)); |
2a2a459e ED |
6121 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
6122 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); | |
b1817524 | 6123 | __vlan_hwaccel_clear_tag(skb); |
66c46d74 | 6124 | skb->dev = napi->dev; |
6d152e23 | 6125 | skb->skb_iif = 0; |
33d9a2c7 ED |
6126 | |
6127 | /* eth_type_trans() assumes pkt_type is PACKET_HOST */ | |
6128 | skb->pkt_type = PACKET_HOST; | |
6129 | ||
c3caf119 JC |
6130 | skb->encapsulation = 0; |
6131 | skb_shinfo(skb)->gso_type = 0; | |
e33d0ba8 | 6132 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
174e2381 | 6133 | skb_ext_reset(skb); |
96e93eab HX |
6134 | |
6135 | napi->skb = skb; | |
6136 | } | |
96e93eab | 6137 | |
76620aaf | 6138 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
5d38a079 | 6139 | { |
5d38a079 | 6140 | struct sk_buff *skb = napi->skb; |
5d38a079 HX |
6141 | |
6142 | if (!skb) { | |
fd11a83d | 6143 | skb = napi_alloc_skb(napi, GRO_MAX_HEAD); |
e2f9dc3b ED |
6144 | if (skb) { |
6145 | napi->skb = skb; | |
6146 | skb_mark_napi_id(skb, napi); | |
6147 | } | |
80595d59 | 6148 | } |
96e93eab HX |
6149 | return skb; |
6150 | } | |
76620aaf | 6151 | EXPORT_SYMBOL(napi_get_frags); |
96e93eab | 6152 | |
a50e233c ED |
6153 | static gro_result_t napi_frags_finish(struct napi_struct *napi, |
6154 | struct sk_buff *skb, | |
6155 | gro_result_t ret) | |
96e93eab | 6156 | { |
5d0d9be8 HX |
6157 | switch (ret) { |
6158 | case GRO_NORMAL: | |
a50e233c ED |
6159 | case GRO_HELD: |
6160 | __skb_push(skb, ETH_HLEN); | |
6161 | skb->protocol = eth_type_trans(skb, skb->dev); | |
323ebb61 EC |
6162 | if (ret == GRO_NORMAL) |
6163 | gro_normal_one(napi, skb); | |
86911732 | 6164 | break; |
5d38a079 | 6165 | |
e44699d2 MK |
6166 | case GRO_MERGED_FREE: |
6167 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) | |
6168 | napi_skb_free_stolen_head(skb); | |
6169 | else | |
6170 | napi_reuse_skb(napi, skb); | |
6171 | break; | |
6172 | ||
5b252f0c | 6173 | case GRO_MERGED: |
25393d3f | 6174 | case GRO_CONSUMED: |
5b252f0c | 6175 | break; |
5d0d9be8 | 6176 | } |
5d38a079 | 6177 | |
c7c4b3b6 | 6178 | return ret; |
5d38a079 | 6179 | } |
5d0d9be8 | 6180 | |
a50e233c ED |
6181 | /* Upper GRO stack assumes network header starts at gro_offset=0 |
6182 | * Drivers could call both napi_gro_frags() and napi_gro_receive() | |
6183 | * We copy ethernet header into skb->data to have a common layout. | |
6184 | */ | |
4adb9c4a | 6185 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
76620aaf HX |
6186 | { |
6187 | struct sk_buff *skb = napi->skb; | |
a50e233c ED |
6188 | const struct ethhdr *eth; |
6189 | unsigned int hlen = sizeof(*eth); | |
76620aaf HX |
6190 | |
6191 | napi->skb = NULL; | |
6192 | ||
a50e233c ED |
6193 | skb_reset_mac_header(skb); |
6194 | skb_gro_reset_offset(skb); | |
6195 | ||
a50e233c ED |
6196 | if (unlikely(skb_gro_header_hard(skb, hlen))) { |
6197 | eth = skb_gro_header_slow(skb, hlen, 0); | |
6198 | if (unlikely(!eth)) { | |
4da46ceb AC |
6199 | net_warn_ratelimited("%s: dropping impossible skb from %s\n", |
6200 | __func__, napi->dev->name); | |
a50e233c ED |
6201 | napi_reuse_skb(napi, skb); |
6202 | return NULL; | |
6203 | } | |
6204 | } else { | |
a4270d67 | 6205 | eth = (const struct ethhdr *)skb->data; |
a50e233c ED |
6206 | gro_pull_from_frag0(skb, hlen); |
6207 | NAPI_GRO_CB(skb)->frag0 += hlen; | |
6208 | NAPI_GRO_CB(skb)->frag0_len -= hlen; | |
76620aaf | 6209 | } |
a50e233c ED |
6210 | __skb_pull(skb, hlen); |
6211 | ||
6212 | /* | |
6213 | * This works because the only protocols we care about don't require | |
6214 | * special handling. | |
6215 | * We'll fix it up properly in napi_frags_finish() | |
6216 | */ | |
6217 | skb->protocol = eth->h_proto; | |
76620aaf | 6218 | |
76620aaf HX |
6219 | return skb; |
6220 | } | |
76620aaf | 6221 | |
c7c4b3b6 | 6222 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
5d0d9be8 | 6223 | { |
b0e3f1bd | 6224 | gro_result_t ret; |
76620aaf | 6225 | struct sk_buff *skb = napi_frags_skb(napi); |
5d0d9be8 | 6226 | |
ae78dbfa BH |
6227 | trace_napi_gro_frags_entry(skb); |
6228 | ||
b0e3f1bd GB |
6229 | ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
6230 | trace_napi_gro_frags_exit(ret); | |
6231 | ||
6232 | return ret; | |
5d0d9be8 | 6233 | } |
5d38a079 HX |
6234 | EXPORT_SYMBOL(napi_gro_frags); |
6235 | ||
573e8fca TH |
6236 | /* Compute the checksum from gro_offset and return the folded value |
6237 | * after adding in any pseudo checksum. | |
6238 | */ | |
6239 | __sum16 __skb_gro_checksum_complete(struct sk_buff *skb) | |
6240 | { | |
6241 | __wsum wsum; | |
6242 | __sum16 sum; | |
6243 | ||
6244 | wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0); | |
6245 | ||
6246 | /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */ | |
6247 | sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum)); | |
14641931 | 6248 | /* See comments in __skb_checksum_complete(). */ |
573e8fca TH |
6249 | if (likely(!sum)) { |
6250 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && | |
6251 | !skb->csum_complete_sw) | |
7fe50ac8 | 6252 | netdev_rx_csum_fault(skb->dev, skb); |
573e8fca TH |
6253 | } |
6254 | ||
6255 | NAPI_GRO_CB(skb)->csum = wsum; | |
6256 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
6257 | ||
6258 | return sum; | |
6259 | } | |
6260 | EXPORT_SYMBOL(__skb_gro_checksum_complete); | |
6261 | ||
773fc8f6 | 6262 | static void net_rps_send_ipi(struct softnet_data *remsd) |
6263 | { | |
6264 | #ifdef CONFIG_RPS | |
6265 | while (remsd) { | |
6266 | struct softnet_data *next = remsd->rps_ipi_next; | |
6267 | ||
6268 | if (cpu_online(remsd->cpu)) | |
6269 | smp_call_function_single_async(remsd->cpu, &remsd->csd); | |
6270 | remsd = next; | |
6271 | } | |
6272 | #endif | |
6273 | } | |
6274 | ||
e326bed2 | 6275 | /* |
855abcf0 | 6276 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
e326bed2 ED |
6277 | * Note: called with local irq disabled, but exits with local irq enabled. |
6278 | */ | |
6279 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
6280 | { | |
6281 | #ifdef CONFIG_RPS | |
6282 | struct softnet_data *remsd = sd->rps_ipi_list; | |
6283 | ||
6284 | if (remsd) { | |
6285 | sd->rps_ipi_list = NULL; | |
6286 | ||
6287 | local_irq_enable(); | |
6288 | ||
6289 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
773fc8f6 | 6290 | net_rps_send_ipi(remsd); |
e326bed2 ED |
6291 | } else |
6292 | #endif | |
6293 | local_irq_enable(); | |
6294 | } | |
6295 | ||
d75b1ade ED |
6296 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
6297 | { | |
6298 | #ifdef CONFIG_RPS | |
6299 | return sd->rps_ipi_list != NULL; | |
6300 | #else | |
6301 | return false; | |
6302 | #endif | |
6303 | } | |
6304 | ||
bea3348e | 6305 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 | 6306 | { |
eecfd7c4 | 6307 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
145dd5f9 PA |
6308 | bool again = true; |
6309 | int work = 0; | |
1da177e4 | 6310 | |
e326bed2 ED |
6311 | /* Check if we have pending ipi, its better to send them now, |
6312 | * not waiting net_rx_action() end. | |
6313 | */ | |
d75b1ade | 6314 | if (sd_has_rps_ipi_waiting(sd)) { |
e326bed2 ED |
6315 | local_irq_disable(); |
6316 | net_rps_action_and_irq_enable(sd); | |
6317 | } | |
d75b1ade | 6318 | |
3d48b53f | 6319 | napi->weight = dev_rx_weight; |
145dd5f9 | 6320 | while (again) { |
1da177e4 | 6321 | struct sk_buff *skb; |
6e7676c1 CG |
6322 | |
6323 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
2c17d27c | 6324 | rcu_read_lock(); |
6e7676c1 | 6325 | __netif_receive_skb(skb); |
2c17d27c | 6326 | rcu_read_unlock(); |
76cc8b13 | 6327 | input_queue_head_incr(sd); |
145dd5f9 | 6328 | if (++work >= quota) |
76cc8b13 | 6329 | return work; |
145dd5f9 | 6330 | |
6e7676c1 | 6331 | } |
1da177e4 | 6332 | |
145dd5f9 | 6333 | local_irq_disable(); |
e36fa2f7 | 6334 | rps_lock(sd); |
11ef7a89 | 6335 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
eecfd7c4 ED |
6336 | /* |
6337 | * Inline a custom version of __napi_complete(). | |
6338 | * only current cpu owns and manipulates this napi, | |
11ef7a89 TH |
6339 | * and NAPI_STATE_SCHED is the only possible flag set |
6340 | * on backlog. | |
6341 | * We can use a plain write instead of clear_bit(), | |
eecfd7c4 ED |
6342 | * and we dont need an smp_mb() memory barrier. |
6343 | */ | |
eecfd7c4 | 6344 | napi->state = 0; |
145dd5f9 PA |
6345 | again = false; |
6346 | } else { | |
6347 | skb_queue_splice_tail_init(&sd->input_pkt_queue, | |
6348 | &sd->process_queue); | |
bea3348e | 6349 | } |
e36fa2f7 | 6350 | rps_unlock(sd); |
145dd5f9 | 6351 | local_irq_enable(); |
6e7676c1 | 6352 | } |
1da177e4 | 6353 | |
bea3348e SH |
6354 | return work; |
6355 | } | |
1da177e4 | 6356 | |
bea3348e SH |
6357 | /** |
6358 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 6359 | * @n: entry to schedule |
bea3348e | 6360 | * |
bc9ad166 ED |
6361 | * The entry's receive function will be scheduled to run. |
6362 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. | |
bea3348e | 6363 | */ |
b5606c2d | 6364 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
6365 | { |
6366 | unsigned long flags; | |
1da177e4 | 6367 | |
bea3348e | 6368 | local_irq_save(flags); |
903ceff7 | 6369 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
bea3348e | 6370 | local_irq_restore(flags); |
1da177e4 | 6371 | } |
bea3348e SH |
6372 | EXPORT_SYMBOL(__napi_schedule); |
6373 | ||
39e6c820 ED |
6374 | /** |
6375 | * napi_schedule_prep - check if napi can be scheduled | |
6376 | * @n: napi context | |
6377 | * | |
6378 | * Test if NAPI routine is already running, and if not mark | |
ee1a4c84 | 6379 | * it as running. This is used as a condition variable to |
39e6c820 ED |
6380 | * insure only one NAPI poll instance runs. We also make |
6381 | * sure there is no pending NAPI disable. | |
6382 | */ | |
6383 | bool napi_schedule_prep(struct napi_struct *n) | |
6384 | { | |
6385 | unsigned long val, new; | |
6386 | ||
6387 | do { | |
6388 | val = READ_ONCE(n->state); | |
6389 | if (unlikely(val & NAPIF_STATE_DISABLE)) | |
6390 | return false; | |
6391 | new = val | NAPIF_STATE_SCHED; | |
6392 | ||
6393 | /* Sets STATE_MISSED bit if STATE_SCHED was already set | |
6394 | * This was suggested by Alexander Duyck, as compiler | |
6395 | * emits better code than : | |
6396 | * if (val & NAPIF_STATE_SCHED) | |
6397 | * new |= NAPIF_STATE_MISSED; | |
6398 | */ | |
6399 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * | |
6400 | NAPIF_STATE_MISSED; | |
6401 | } while (cmpxchg(&n->state, val, new) != val); | |
6402 | ||
6403 | return !(val & NAPIF_STATE_SCHED); | |
6404 | } | |
6405 | EXPORT_SYMBOL(napi_schedule_prep); | |
6406 | ||
bc9ad166 ED |
6407 | /** |
6408 | * __napi_schedule_irqoff - schedule for receive | |
6409 | * @n: entry to schedule | |
6410 | * | |
6411 | * Variant of __napi_schedule() assuming hard irqs are masked | |
6412 | */ | |
6413 | void __napi_schedule_irqoff(struct napi_struct *n) | |
6414 | { | |
6415 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); | |
6416 | } | |
6417 | EXPORT_SYMBOL(__napi_schedule_irqoff); | |
6418 | ||
364b6055 | 6419 | bool napi_complete_done(struct napi_struct *n, int work_done) |
d565b0a1 | 6420 | { |
6f8b12d6 ED |
6421 | unsigned long flags, val, new, timeout = 0; |
6422 | bool ret = true; | |
d565b0a1 HX |
6423 | |
6424 | /* | |
217f6974 ED |
6425 | * 1) Don't let napi dequeue from the cpu poll list |
6426 | * just in case its running on a different cpu. | |
6427 | * 2) If we are busy polling, do nothing here, we have | |
6428 | * the guarantee we will be called later. | |
d565b0a1 | 6429 | */ |
217f6974 ED |
6430 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
6431 | NAPIF_STATE_IN_BUSY_POLL))) | |
364b6055 | 6432 | return false; |
d565b0a1 | 6433 | |
6f8b12d6 ED |
6434 | if (work_done) { |
6435 | if (n->gro_bitmask) | |
7e417a66 ED |
6436 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6437 | n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs); | |
6f8b12d6 ED |
6438 | } |
6439 | if (n->defer_hard_irqs_count > 0) { | |
6440 | n->defer_hard_irqs_count--; | |
7e417a66 | 6441 | timeout = READ_ONCE(n->dev->gro_flush_timeout); |
6f8b12d6 ED |
6442 | if (timeout) |
6443 | ret = false; | |
6444 | } | |
6445 | if (n->gro_bitmask) { | |
605108ac PA |
6446 | /* When the NAPI instance uses a timeout and keeps postponing |
6447 | * it, we need to bound somehow the time packets are kept in | |
6448 | * the GRO layer | |
6449 | */ | |
6450 | napi_gro_flush(n, !!timeout); | |
3b47d303 | 6451 | } |
c8079432 MM |
6452 | |
6453 | gro_normal_list(n); | |
6454 | ||
02c1602e | 6455 | if (unlikely(!list_empty(&n->poll_list))) { |
d75b1ade ED |
6456 | /* If n->poll_list is not empty, we need to mask irqs */ |
6457 | local_irq_save(flags); | |
02c1602e | 6458 | list_del_init(&n->poll_list); |
d75b1ade ED |
6459 | local_irq_restore(flags); |
6460 | } | |
39e6c820 ED |
6461 | |
6462 | do { | |
6463 | val = READ_ONCE(n->state); | |
6464 | ||
6465 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); | |
6466 | ||
7fd3253a BT |
6467 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED | |
6468 | NAPIF_STATE_PREFER_BUSY_POLL); | |
39e6c820 ED |
6469 | |
6470 | /* If STATE_MISSED was set, leave STATE_SCHED set, | |
6471 | * because we will call napi->poll() one more time. | |
6472 | * This C code was suggested by Alexander Duyck to help gcc. | |
6473 | */ | |
6474 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * | |
6475 | NAPIF_STATE_SCHED; | |
6476 | } while (cmpxchg(&n->state, val, new) != val); | |
6477 | ||
6478 | if (unlikely(val & NAPIF_STATE_MISSED)) { | |
6479 | __napi_schedule(n); | |
6480 | return false; | |
6481 | } | |
6482 | ||
6f8b12d6 ED |
6483 | if (timeout) |
6484 | hrtimer_start(&n->timer, ns_to_ktime(timeout), | |
6485 | HRTIMER_MODE_REL_PINNED); | |
6486 | return ret; | |
d565b0a1 | 6487 | } |
3b47d303 | 6488 | EXPORT_SYMBOL(napi_complete_done); |
d565b0a1 | 6489 | |
af12fa6e | 6490 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
02d62e86 | 6491 | static struct napi_struct *napi_by_id(unsigned int napi_id) |
af12fa6e ET |
6492 | { |
6493 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); | |
6494 | struct napi_struct *napi; | |
6495 | ||
6496 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) | |
6497 | if (napi->napi_id == napi_id) | |
6498 | return napi; | |
6499 | ||
6500 | return NULL; | |
6501 | } | |
02d62e86 ED |
6502 | |
6503 | #if defined(CONFIG_NET_RX_BUSY_POLL) | |
217f6974 | 6504 | |
7fd3253a | 6505 | static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule) |
217f6974 | 6506 | { |
7fd3253a BT |
6507 | if (!skip_schedule) { |
6508 | gro_normal_list(napi); | |
6509 | __napi_schedule(napi); | |
6510 | return; | |
6511 | } | |
217f6974 | 6512 | |
7fd3253a BT |
6513 | if (napi->gro_bitmask) { |
6514 | /* flush too old packets | |
6515 | * If HZ < 1000, flush all packets. | |
6516 | */ | |
6517 | napi_gro_flush(napi, HZ >= 1000); | |
6518 | } | |
217f6974 | 6519 | |
7fd3253a BT |
6520 | gro_normal_list(napi); |
6521 | clear_bit(NAPI_STATE_SCHED, &napi->state); | |
6522 | } | |
6523 | ||
7c951caf BT |
6524 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, bool prefer_busy_poll, |
6525 | u16 budget) | |
217f6974 | 6526 | { |
7fd3253a BT |
6527 | bool skip_schedule = false; |
6528 | unsigned long timeout; | |
217f6974 ED |
6529 | int rc; |
6530 | ||
39e6c820 ED |
6531 | /* Busy polling means there is a high chance device driver hard irq |
6532 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was | |
6533 | * set in napi_schedule_prep(). | |
6534 | * Since we are about to call napi->poll() once more, we can safely | |
6535 | * clear NAPI_STATE_MISSED. | |
6536 | * | |
6537 | * Note: x86 could use a single "lock and ..." instruction | |
6538 | * to perform these two clear_bit() | |
6539 | */ | |
6540 | clear_bit(NAPI_STATE_MISSED, &napi->state); | |
217f6974 ED |
6541 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
6542 | ||
6543 | local_bh_disable(); | |
6544 | ||
7fd3253a BT |
6545 | if (prefer_busy_poll) { |
6546 | napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs); | |
6547 | timeout = READ_ONCE(napi->dev->gro_flush_timeout); | |
6548 | if (napi->defer_hard_irqs_count && timeout) { | |
6549 | hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED); | |
6550 | skip_schedule = true; | |
6551 | } | |
6552 | } | |
6553 | ||
217f6974 ED |
6554 | /* All we really want here is to re-enable device interrupts. |
6555 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. | |
6556 | */ | |
7c951caf | 6557 | rc = napi->poll(napi, budget); |
323ebb61 EC |
6558 | /* We can't gro_normal_list() here, because napi->poll() might have |
6559 | * rearmed the napi (napi_complete_done()) in which case it could | |
6560 | * already be running on another CPU. | |
6561 | */ | |
7c951caf | 6562 | trace_napi_poll(napi, rc, budget); |
217f6974 | 6563 | netpoll_poll_unlock(have_poll_lock); |
7c951caf | 6564 | if (rc == budget) |
7fd3253a | 6565 | __busy_poll_stop(napi, skip_schedule); |
217f6974 | 6566 | local_bh_enable(); |
217f6974 ED |
6567 | } |
6568 | ||
7db6b048 SS |
6569 | void napi_busy_loop(unsigned int napi_id, |
6570 | bool (*loop_end)(void *, unsigned long), | |
7c951caf | 6571 | void *loop_end_arg, bool prefer_busy_poll, u16 budget) |
02d62e86 | 6572 | { |
7db6b048 | 6573 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
217f6974 | 6574 | int (*napi_poll)(struct napi_struct *napi, int budget); |
217f6974 | 6575 | void *have_poll_lock = NULL; |
02d62e86 | 6576 | struct napi_struct *napi; |
217f6974 ED |
6577 | |
6578 | restart: | |
217f6974 | 6579 | napi_poll = NULL; |
02d62e86 | 6580 | |
2a028ecb | 6581 | rcu_read_lock(); |
02d62e86 | 6582 | |
545cd5e5 | 6583 | napi = napi_by_id(napi_id); |
02d62e86 ED |
6584 | if (!napi) |
6585 | goto out; | |
6586 | ||
217f6974 ED |
6587 | preempt_disable(); |
6588 | for (;;) { | |
2b5cd0df AD |
6589 | int work = 0; |
6590 | ||
2a028ecb | 6591 | local_bh_disable(); |
217f6974 ED |
6592 | if (!napi_poll) { |
6593 | unsigned long val = READ_ONCE(napi->state); | |
6594 | ||
6595 | /* If multiple threads are competing for this napi, | |
6596 | * we avoid dirtying napi->state as much as we can. | |
6597 | */ | |
6598 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | | |
7fd3253a BT |
6599 | NAPIF_STATE_IN_BUSY_POLL)) { |
6600 | if (prefer_busy_poll) | |
6601 | set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
217f6974 | 6602 | goto count; |
7fd3253a | 6603 | } |
217f6974 ED |
6604 | if (cmpxchg(&napi->state, val, |
6605 | val | NAPIF_STATE_IN_BUSY_POLL | | |
7fd3253a BT |
6606 | NAPIF_STATE_SCHED) != val) { |
6607 | if (prefer_busy_poll) | |
6608 | set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
217f6974 | 6609 | goto count; |
7fd3253a | 6610 | } |
217f6974 ED |
6611 | have_poll_lock = netpoll_poll_lock(napi); |
6612 | napi_poll = napi->poll; | |
6613 | } | |
7c951caf BT |
6614 | work = napi_poll(napi, budget); |
6615 | trace_napi_poll(napi, work, budget); | |
323ebb61 | 6616 | gro_normal_list(napi); |
217f6974 | 6617 | count: |
2b5cd0df | 6618 | if (work > 0) |
7db6b048 | 6619 | __NET_ADD_STATS(dev_net(napi->dev), |
2b5cd0df | 6620 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
2a028ecb | 6621 | local_bh_enable(); |
02d62e86 | 6622 | |
7db6b048 | 6623 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
217f6974 | 6624 | break; |
02d62e86 | 6625 | |
217f6974 ED |
6626 | if (unlikely(need_resched())) { |
6627 | if (napi_poll) | |
7c951caf | 6628 | busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget); |
217f6974 ED |
6629 | preempt_enable(); |
6630 | rcu_read_unlock(); | |
6631 | cond_resched(); | |
7db6b048 | 6632 | if (loop_end(loop_end_arg, start_time)) |
2b5cd0df | 6633 | return; |
217f6974 ED |
6634 | goto restart; |
6635 | } | |
6cdf89b1 | 6636 | cpu_relax(); |
217f6974 ED |
6637 | } |
6638 | if (napi_poll) | |
7c951caf | 6639 | busy_poll_stop(napi, have_poll_lock, prefer_busy_poll, budget); |
217f6974 | 6640 | preempt_enable(); |
02d62e86 | 6641 | out: |
2a028ecb | 6642 | rcu_read_unlock(); |
02d62e86 | 6643 | } |
7db6b048 | 6644 | EXPORT_SYMBOL(napi_busy_loop); |
02d62e86 ED |
6645 | |
6646 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
af12fa6e | 6647 | |
149d6ad8 | 6648 | static void napi_hash_add(struct napi_struct *napi) |
af12fa6e | 6649 | { |
4d092dd2 | 6650 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state)) |
52bd2d62 | 6651 | return; |
af12fa6e | 6652 | |
52bd2d62 | 6653 | spin_lock(&napi_hash_lock); |
af12fa6e | 6654 | |
545cd5e5 | 6655 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
52bd2d62 | 6656 | do { |
545cd5e5 AD |
6657 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
6658 | napi_gen_id = MIN_NAPI_ID; | |
52bd2d62 ED |
6659 | } while (napi_by_id(napi_gen_id)); |
6660 | napi->napi_id = napi_gen_id; | |
af12fa6e | 6661 | |
52bd2d62 ED |
6662 | hlist_add_head_rcu(&napi->napi_hash_node, |
6663 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); | |
af12fa6e | 6664 | |
52bd2d62 | 6665 | spin_unlock(&napi_hash_lock); |
af12fa6e | 6666 | } |
af12fa6e ET |
6667 | |
6668 | /* Warning : caller is responsible to make sure rcu grace period | |
6669 | * is respected before freeing memory containing @napi | |
6670 | */ | |
5198d545 | 6671 | static void napi_hash_del(struct napi_struct *napi) |
af12fa6e ET |
6672 | { |
6673 | spin_lock(&napi_hash_lock); | |
6674 | ||
4d092dd2 | 6675 | hlist_del_init_rcu(&napi->napi_hash_node); |
5198d545 | 6676 | |
af12fa6e ET |
6677 | spin_unlock(&napi_hash_lock); |
6678 | } | |
af12fa6e | 6679 | |
3b47d303 ED |
6680 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
6681 | { | |
6682 | struct napi_struct *napi; | |
6683 | ||
6684 | napi = container_of(timer, struct napi_struct, timer); | |
39e6c820 ED |
6685 | |
6686 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting | |
6687 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. | |
6688 | */ | |
6f8b12d6 | 6689 | if (!napi_disable_pending(napi) && |
7fd3253a BT |
6690 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) { |
6691 | clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); | |
39e6c820 | 6692 | __napi_schedule_irqoff(napi); |
7fd3253a | 6693 | } |
3b47d303 ED |
6694 | |
6695 | return HRTIMER_NORESTART; | |
6696 | } | |
6697 | ||
7c4ec749 | 6698 | static void init_gro_hash(struct napi_struct *napi) |
d565b0a1 | 6699 | { |
07d78363 DM |
6700 | int i; |
6701 | ||
6312fe77 LR |
6702 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6703 | INIT_LIST_HEAD(&napi->gro_hash[i].list); | |
6704 | napi->gro_hash[i].count = 0; | |
6705 | } | |
7c4ec749 DM |
6706 | napi->gro_bitmask = 0; |
6707 | } | |
6708 | ||
6709 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, | |
6710 | int (*poll)(struct napi_struct *, int), int weight) | |
6711 | { | |
4d092dd2 JK |
6712 | if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state))) |
6713 | return; | |
6714 | ||
7c4ec749 | 6715 | INIT_LIST_HEAD(&napi->poll_list); |
4d092dd2 | 6716 | INIT_HLIST_NODE(&napi->napi_hash_node); |
7c4ec749 DM |
6717 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
6718 | napi->timer.function = napi_watchdog; | |
6719 | init_gro_hash(napi); | |
5d38a079 | 6720 | napi->skb = NULL; |
323ebb61 EC |
6721 | INIT_LIST_HEAD(&napi->rx_list); |
6722 | napi->rx_count = 0; | |
d565b0a1 | 6723 | napi->poll = poll; |
82dc3c63 | 6724 | if (weight > NAPI_POLL_WEIGHT) |
bf29e9e9 QC |
6725 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
6726 | weight); | |
d565b0a1 | 6727 | napi->weight = weight; |
d565b0a1 | 6728 | napi->dev = dev; |
5d38a079 | 6729 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
6730 | napi->poll_owner = -1; |
6731 | #endif | |
6732 | set_bit(NAPI_STATE_SCHED, &napi->state); | |
96e97bc0 JK |
6733 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
6734 | list_add_rcu(&napi->dev_list, &dev->napi_list); | |
93d05d4a | 6735 | napi_hash_add(napi); |
d565b0a1 HX |
6736 | } |
6737 | EXPORT_SYMBOL(netif_napi_add); | |
6738 | ||
3b47d303 ED |
6739 | void napi_disable(struct napi_struct *n) |
6740 | { | |
6741 | might_sleep(); | |
6742 | set_bit(NAPI_STATE_DISABLE, &n->state); | |
6743 | ||
6744 | while (test_and_set_bit(NAPI_STATE_SCHED, &n->state)) | |
6745 | msleep(1); | |
2d8bff12 NH |
6746 | while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state)) |
6747 | msleep(1); | |
3b47d303 ED |
6748 | |
6749 | hrtimer_cancel(&n->timer); | |
6750 | ||
7fd3253a | 6751 | clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state); |
3b47d303 ED |
6752 | clear_bit(NAPI_STATE_DISABLE, &n->state); |
6753 | } | |
6754 | EXPORT_SYMBOL(napi_disable); | |
6755 | ||
07d78363 | 6756 | static void flush_gro_hash(struct napi_struct *napi) |
d4546c25 | 6757 | { |
07d78363 | 6758 | int i; |
d4546c25 | 6759 | |
07d78363 DM |
6760 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6761 | struct sk_buff *skb, *n; | |
6762 | ||
6312fe77 | 6763 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
07d78363 | 6764 | kfree_skb(skb); |
6312fe77 | 6765 | napi->gro_hash[i].count = 0; |
07d78363 | 6766 | } |
d4546c25 DM |
6767 | } |
6768 | ||
93d05d4a | 6769 | /* Must be called in process context */ |
5198d545 | 6770 | void __netif_napi_del(struct napi_struct *napi) |
d565b0a1 | 6771 | { |
4d092dd2 JK |
6772 | if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state)) |
6773 | return; | |
6774 | ||
5198d545 | 6775 | napi_hash_del(napi); |
5251ef82 | 6776 | list_del_rcu(&napi->dev_list); |
76620aaf | 6777 | napi_free_frags(napi); |
d565b0a1 | 6778 | |
07d78363 | 6779 | flush_gro_hash(napi); |
d9f37d01 | 6780 | napi->gro_bitmask = 0; |
d565b0a1 | 6781 | } |
5198d545 | 6782 | EXPORT_SYMBOL(__netif_napi_del); |
d565b0a1 | 6783 | |
726ce70e HX |
6784 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) |
6785 | { | |
6786 | void *have; | |
6787 | int work, weight; | |
6788 | ||
6789 | list_del_init(&n->poll_list); | |
6790 | ||
6791 | have = netpoll_poll_lock(n); | |
6792 | ||
6793 | weight = n->weight; | |
6794 | ||
6795 | /* This NAPI_STATE_SCHED test is for avoiding a race | |
6796 | * with netpoll's poll_napi(). Only the entity which | |
6797 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
6798 | * actually make the ->poll() call. Therefore we avoid | |
6799 | * accidentally calling ->poll() when NAPI is not scheduled. | |
6800 | */ | |
6801 | work = 0; | |
6802 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { | |
6803 | work = n->poll(n, weight); | |
1db19db7 | 6804 | trace_napi_poll(n, work, weight); |
726ce70e HX |
6805 | } |
6806 | ||
427d5838 ED |
6807 | if (unlikely(work > weight)) |
6808 | pr_err_once("NAPI poll function %pS returned %d, exceeding its budget of %d.\n", | |
6809 | n->poll, work, weight); | |
726ce70e HX |
6810 | |
6811 | if (likely(work < weight)) | |
6812 | goto out_unlock; | |
6813 | ||
6814 | /* Drivers must not modify the NAPI state if they | |
6815 | * consume the entire weight. In such cases this code | |
6816 | * still "owns" the NAPI instance and therefore can | |
6817 | * move the instance around on the list at-will. | |
6818 | */ | |
6819 | if (unlikely(napi_disable_pending(n))) { | |
6820 | napi_complete(n); | |
6821 | goto out_unlock; | |
6822 | } | |
6823 | ||
7fd3253a BT |
6824 | /* The NAPI context has more processing work, but busy-polling |
6825 | * is preferred. Exit early. | |
6826 | */ | |
6827 | if (napi_prefer_busy_poll(n)) { | |
6828 | if (napi_complete_done(n, work)) { | |
6829 | /* If timeout is not set, we need to make sure | |
6830 | * that the NAPI is re-scheduled. | |
6831 | */ | |
6832 | napi_schedule(n); | |
6833 | } | |
6834 | goto out_unlock; | |
6835 | } | |
6836 | ||
d9f37d01 | 6837 | if (n->gro_bitmask) { |
726ce70e HX |
6838 | /* flush too old packets |
6839 | * If HZ < 1000, flush all packets. | |
6840 | */ | |
6841 | napi_gro_flush(n, HZ >= 1000); | |
6842 | } | |
6843 | ||
c8079432 MM |
6844 | gro_normal_list(n); |
6845 | ||
001ce546 HX |
6846 | /* Some drivers may have called napi_schedule |
6847 | * prior to exhausting their budget. | |
6848 | */ | |
6849 | if (unlikely(!list_empty(&n->poll_list))) { | |
6850 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", | |
6851 | n->dev ? n->dev->name : "backlog"); | |
6852 | goto out_unlock; | |
6853 | } | |
6854 | ||
726ce70e HX |
6855 | list_add_tail(&n->poll_list, repoll); |
6856 | ||
6857 | out_unlock: | |
6858 | netpoll_poll_unlock(have); | |
6859 | ||
6860 | return work; | |
6861 | } | |
6862 | ||
0766f788 | 6863 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
1da177e4 | 6864 | { |
903ceff7 | 6865 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
7acf8a1e MW |
6866 | unsigned long time_limit = jiffies + |
6867 | usecs_to_jiffies(netdev_budget_usecs); | |
51b0bded | 6868 | int budget = netdev_budget; |
d75b1ade ED |
6869 | LIST_HEAD(list); |
6870 | LIST_HEAD(repoll); | |
53fb95d3 | 6871 | |
1da177e4 | 6872 | local_irq_disable(); |
d75b1ade ED |
6873 | list_splice_init(&sd->poll_list, &list); |
6874 | local_irq_enable(); | |
1da177e4 | 6875 | |
ceb8d5bf | 6876 | for (;;) { |
bea3348e | 6877 | struct napi_struct *n; |
1da177e4 | 6878 | |
ceb8d5bf HX |
6879 | if (list_empty(&list)) { |
6880 | if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll)) | |
f52dffe0 | 6881 | goto out; |
ceb8d5bf HX |
6882 | break; |
6883 | } | |
6884 | ||
6bd373eb HX |
6885 | n = list_first_entry(&list, struct napi_struct, poll_list); |
6886 | budget -= napi_poll(n, &repoll); | |
6887 | ||
d75b1ade | 6888 | /* If softirq window is exhausted then punt. |
24f8b238 SH |
6889 | * Allow this to run for 2 jiffies since which will allow |
6890 | * an average latency of 1.5/HZ. | |
bea3348e | 6891 | */ |
ceb8d5bf HX |
6892 | if (unlikely(budget <= 0 || |
6893 | time_after_eq(jiffies, time_limit))) { | |
6894 | sd->time_squeeze++; | |
6895 | break; | |
6896 | } | |
1da177e4 | 6897 | } |
d75b1ade | 6898 | |
d75b1ade ED |
6899 | local_irq_disable(); |
6900 | ||
6901 | list_splice_tail_init(&sd->poll_list, &list); | |
6902 | list_splice_tail(&repoll, &list); | |
6903 | list_splice(&list, &sd->poll_list); | |
6904 | if (!list_empty(&sd->poll_list)) | |
6905 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
6906 | ||
e326bed2 | 6907 | net_rps_action_and_irq_enable(sd); |
f52dffe0 ED |
6908 | out: |
6909 | __kfree_skb_flush(); | |
1da177e4 LT |
6910 | } |
6911 | ||
aa9d8560 | 6912 | struct netdev_adjacent { |
9ff162a8 | 6913 | struct net_device *dev; |
5d261913 VF |
6914 | |
6915 | /* upper master flag, there can only be one master device per list */ | |
9ff162a8 | 6916 | bool master; |
5d261913 | 6917 | |
32b6d34f TY |
6918 | /* lookup ignore flag */ |
6919 | bool ignore; | |
6920 | ||
5d261913 VF |
6921 | /* counter for the number of times this device was added to us */ |
6922 | u16 ref_nr; | |
6923 | ||
402dae96 VF |
6924 | /* private field for the users */ |
6925 | void *private; | |
6926 | ||
9ff162a8 JP |
6927 | struct list_head list; |
6928 | struct rcu_head rcu; | |
9ff162a8 JP |
6929 | }; |
6930 | ||
6ea29da1 | 6931 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
2f268f12 | 6932 | struct list_head *adj_list) |
9ff162a8 | 6933 | { |
5d261913 | 6934 | struct netdev_adjacent *adj; |
5d261913 | 6935 | |
2f268f12 | 6936 | list_for_each_entry(adj, adj_list, list) { |
5d261913 VF |
6937 | if (adj->dev == adj_dev) |
6938 | return adj; | |
9ff162a8 JP |
6939 | } |
6940 | return NULL; | |
6941 | } | |
6942 | ||
eff74233 TY |
6943 | static int ____netdev_has_upper_dev(struct net_device *upper_dev, |
6944 | struct netdev_nested_priv *priv) | |
f1170fd4 | 6945 | { |
eff74233 | 6946 | struct net_device *dev = (struct net_device *)priv->data; |
f1170fd4 DA |
6947 | |
6948 | return upper_dev == dev; | |
6949 | } | |
6950 | ||
9ff162a8 JP |
6951 | /** |
6952 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
6953 | * @dev: device | |
6954 | * @upper_dev: upper device to check | |
6955 | * | |
6956 | * Find out if a device is linked to specified upper device and return true | |
6957 | * in case it is. Note that this checks only immediate upper device, | |
6958 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
6959 | */ | |
6960 | bool netdev_has_upper_dev(struct net_device *dev, | |
6961 | struct net_device *upper_dev) | |
6962 | { | |
eff74233 TY |
6963 | struct netdev_nested_priv priv = { |
6964 | .data = (void *)upper_dev, | |
6965 | }; | |
6966 | ||
9ff162a8 JP |
6967 | ASSERT_RTNL(); |
6968 | ||
32b6d34f | 6969 | return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
eff74233 | 6970 | &priv); |
9ff162a8 JP |
6971 | } |
6972 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
6973 | ||
1a3f060c | 6974 | /** |
c1639be9 | 6975 | * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device |
1a3f060c DA |
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 the entire upper device chain. | |
6981 | * The caller must hold rcu lock. | |
6982 | */ | |
6983 | ||
1a3f060c DA |
6984 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
6985 | struct net_device *upper_dev) | |
6986 | { | |
eff74233 TY |
6987 | struct netdev_nested_priv priv = { |
6988 | .data = (void *)upper_dev, | |
6989 | }; | |
6990 | ||
32b6d34f | 6991 | return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
eff74233 | 6992 | &priv); |
1a3f060c DA |
6993 | } |
6994 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); | |
6995 | ||
9ff162a8 JP |
6996 | /** |
6997 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
6998 | * @dev: device | |
6999 | * | |
7000 | * Find out if a device is linked to an upper device and return true in case | |
7001 | * it is. The caller must hold the RTNL lock. | |
7002 | */ | |
25cc72a3 | 7003 | bool netdev_has_any_upper_dev(struct net_device *dev) |
9ff162a8 JP |
7004 | { |
7005 | ASSERT_RTNL(); | |
7006 | ||
f1170fd4 | 7007 | return !list_empty(&dev->adj_list.upper); |
9ff162a8 | 7008 | } |
25cc72a3 | 7009 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
9ff162a8 JP |
7010 | |
7011 | /** | |
7012 | * netdev_master_upper_dev_get - Get master upper device | |
7013 | * @dev: device | |
7014 | * | |
7015 | * Find a master upper device and return pointer to it or NULL in case | |
7016 | * it's not there. The caller must hold the RTNL lock. | |
7017 | */ | |
7018 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
7019 | { | |
aa9d8560 | 7020 | struct netdev_adjacent *upper; |
9ff162a8 JP |
7021 | |
7022 | ASSERT_RTNL(); | |
7023 | ||
2f268f12 | 7024 | if (list_empty(&dev->adj_list.upper)) |
9ff162a8 JP |
7025 | return NULL; |
7026 | ||
2f268f12 | 7027 | upper = list_first_entry(&dev->adj_list.upper, |
aa9d8560 | 7028 | struct netdev_adjacent, list); |
9ff162a8 JP |
7029 | if (likely(upper->master)) |
7030 | return upper->dev; | |
7031 | return NULL; | |
7032 | } | |
7033 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
7034 | ||
32b6d34f TY |
7035 | static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) |
7036 | { | |
7037 | struct netdev_adjacent *upper; | |
7038 | ||
7039 | ASSERT_RTNL(); | |
7040 | ||
7041 | if (list_empty(&dev->adj_list.upper)) | |
7042 | return NULL; | |
7043 | ||
7044 | upper = list_first_entry(&dev->adj_list.upper, | |
7045 | struct netdev_adjacent, list); | |
7046 | if (likely(upper->master) && !upper->ignore) | |
7047 | return upper->dev; | |
7048 | return NULL; | |
7049 | } | |
7050 | ||
0f524a80 DA |
7051 | /** |
7052 | * netdev_has_any_lower_dev - Check if device is linked to some device | |
7053 | * @dev: device | |
7054 | * | |
7055 | * Find out if a device is linked to a lower device and return true in case | |
7056 | * it is. The caller must hold the RTNL lock. | |
7057 | */ | |
7058 | static bool netdev_has_any_lower_dev(struct net_device *dev) | |
7059 | { | |
7060 | ASSERT_RTNL(); | |
7061 | ||
7062 | return !list_empty(&dev->adj_list.lower); | |
7063 | } | |
7064 | ||
b6ccba4c VF |
7065 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
7066 | { | |
7067 | struct netdev_adjacent *adj; | |
7068 | ||
7069 | adj = list_entry(adj_list, struct netdev_adjacent, list); | |
7070 | ||
7071 | return adj->private; | |
7072 | } | |
7073 | EXPORT_SYMBOL(netdev_adjacent_get_private); | |
7074 | ||
44a40855 VY |
7075 | /** |
7076 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list | |
7077 | * @dev: device | |
7078 | * @iter: list_head ** of the current position | |
7079 | * | |
7080 | * Gets the next device from the dev's upper list, starting from iter | |
7081 | * position. The caller must hold RCU read lock. | |
7082 | */ | |
7083 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, | |
7084 | struct list_head **iter) | |
7085 | { | |
7086 | struct netdev_adjacent *upper; | |
7087 | ||
7088 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
7089 | ||
7090 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7091 | ||
7092 | if (&upper->list == &dev->adj_list.upper) | |
7093 | return NULL; | |
7094 | ||
7095 | *iter = &upper->list; | |
7096 | ||
7097 | return upper->dev; | |
7098 | } | |
7099 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); | |
7100 | ||
32b6d34f TY |
7101 | static struct net_device *__netdev_next_upper_dev(struct net_device *dev, |
7102 | struct list_head **iter, | |
7103 | bool *ignore) | |
5343da4c TY |
7104 | { |
7105 | struct netdev_adjacent *upper; | |
7106 | ||
7107 | upper = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7108 | ||
7109 | if (&upper->list == &dev->adj_list.upper) | |
7110 | return NULL; | |
7111 | ||
7112 | *iter = &upper->list; | |
32b6d34f | 7113 | *ignore = upper->ignore; |
5343da4c TY |
7114 | |
7115 | return upper->dev; | |
7116 | } | |
7117 | ||
1a3f060c DA |
7118 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
7119 | struct list_head **iter) | |
7120 | { | |
7121 | struct netdev_adjacent *upper; | |
7122 | ||
7123 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
7124 | ||
7125 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7126 | ||
7127 | if (&upper->list == &dev->adj_list.upper) | |
7128 | return NULL; | |
7129 | ||
7130 | *iter = &upper->list; | |
7131 | ||
7132 | return upper->dev; | |
7133 | } | |
7134 | ||
32b6d34f TY |
7135 | static int __netdev_walk_all_upper_dev(struct net_device *dev, |
7136 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7137 | struct netdev_nested_priv *priv), |
7138 | struct netdev_nested_priv *priv) | |
5343da4c TY |
7139 | { |
7140 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7141 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7142 | int ret, cur = 0; | |
32b6d34f | 7143 | bool ignore; |
5343da4c TY |
7144 | |
7145 | now = dev; | |
7146 | iter = &dev->adj_list.upper; | |
7147 | ||
7148 | while (1) { | |
7149 | if (now != dev) { | |
eff74233 | 7150 | ret = fn(now, priv); |
5343da4c TY |
7151 | if (ret) |
7152 | return ret; | |
7153 | } | |
7154 | ||
7155 | next = NULL; | |
7156 | while (1) { | |
32b6d34f | 7157 | udev = __netdev_next_upper_dev(now, &iter, &ignore); |
5343da4c TY |
7158 | if (!udev) |
7159 | break; | |
32b6d34f TY |
7160 | if (ignore) |
7161 | continue; | |
5343da4c TY |
7162 | |
7163 | next = udev; | |
7164 | niter = &udev->adj_list.upper; | |
7165 | dev_stack[cur] = now; | |
7166 | iter_stack[cur++] = iter; | |
7167 | break; | |
7168 | } | |
7169 | ||
7170 | if (!next) { | |
7171 | if (!cur) | |
7172 | return 0; | |
7173 | next = dev_stack[--cur]; | |
7174 | niter = iter_stack[cur]; | |
7175 | } | |
7176 | ||
7177 | now = next; | |
7178 | iter = niter; | |
7179 | } | |
7180 | ||
7181 | return 0; | |
7182 | } | |
7183 | ||
1a3f060c DA |
7184 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, |
7185 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7186 | struct netdev_nested_priv *priv), |
7187 | struct netdev_nested_priv *priv) | |
1a3f060c | 7188 | { |
5343da4c TY |
7189 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7190 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7191 | int ret, cur = 0; | |
1a3f060c | 7192 | |
5343da4c TY |
7193 | now = dev; |
7194 | iter = &dev->adj_list.upper; | |
1a3f060c | 7195 | |
5343da4c TY |
7196 | while (1) { |
7197 | if (now != dev) { | |
eff74233 | 7198 | ret = fn(now, priv); |
5343da4c TY |
7199 | if (ret) |
7200 | return ret; | |
7201 | } | |
7202 | ||
7203 | next = NULL; | |
7204 | while (1) { | |
7205 | udev = netdev_next_upper_dev_rcu(now, &iter); | |
7206 | if (!udev) | |
7207 | break; | |
7208 | ||
7209 | next = udev; | |
7210 | niter = &udev->adj_list.upper; | |
7211 | dev_stack[cur] = now; | |
7212 | iter_stack[cur++] = iter; | |
7213 | break; | |
7214 | } | |
7215 | ||
7216 | if (!next) { | |
7217 | if (!cur) | |
7218 | return 0; | |
7219 | next = dev_stack[--cur]; | |
7220 | niter = iter_stack[cur]; | |
7221 | } | |
7222 | ||
7223 | now = next; | |
7224 | iter = niter; | |
1a3f060c DA |
7225 | } |
7226 | ||
7227 | return 0; | |
7228 | } | |
7229 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); | |
7230 | ||
32b6d34f TY |
7231 | static bool __netdev_has_upper_dev(struct net_device *dev, |
7232 | struct net_device *upper_dev) | |
7233 | { | |
eff74233 | 7234 | struct netdev_nested_priv priv = { |
1fc70edb | 7235 | .flags = 0, |
eff74233 TY |
7236 | .data = (void *)upper_dev, |
7237 | }; | |
7238 | ||
32b6d34f TY |
7239 | ASSERT_RTNL(); |
7240 | ||
7241 | return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, | |
eff74233 | 7242 | &priv); |
32b6d34f TY |
7243 | } |
7244 | ||
31088a11 VF |
7245 | /** |
7246 | * netdev_lower_get_next_private - Get the next ->private from the | |
7247 | * lower neighbour list | |
7248 | * @dev: device | |
7249 | * @iter: list_head ** of the current position | |
7250 | * | |
7251 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7252 | * list, starting from iter position. The caller must hold either hold the | |
7253 | * RTNL lock or its own locking that guarantees that the neighbour lower | |
b469139e | 7254 | * list will remain unchanged. |
31088a11 VF |
7255 | */ |
7256 | void *netdev_lower_get_next_private(struct net_device *dev, | |
7257 | struct list_head **iter) | |
7258 | { | |
7259 | struct netdev_adjacent *lower; | |
7260 | ||
7261 | lower = list_entry(*iter, struct netdev_adjacent, list); | |
7262 | ||
7263 | if (&lower->list == &dev->adj_list.lower) | |
7264 | return NULL; | |
7265 | ||
6859e7df | 7266 | *iter = lower->list.next; |
31088a11 VF |
7267 | |
7268 | return lower->private; | |
7269 | } | |
7270 | EXPORT_SYMBOL(netdev_lower_get_next_private); | |
7271 | ||
7272 | /** | |
7273 | * netdev_lower_get_next_private_rcu - Get the next ->private from the | |
7274 | * lower neighbour list, RCU | |
7275 | * variant | |
7276 | * @dev: device | |
7277 | * @iter: list_head ** of the current position | |
7278 | * | |
7279 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7280 | * list, starting from iter position. The caller must hold RCU read lock. | |
7281 | */ | |
7282 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, | |
7283 | struct list_head **iter) | |
7284 | { | |
7285 | struct netdev_adjacent *lower; | |
7286 | ||
7287 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
7288 | ||
7289 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7290 | ||
7291 | if (&lower->list == &dev->adj_list.lower) | |
7292 | return NULL; | |
7293 | ||
6859e7df | 7294 | *iter = &lower->list; |
31088a11 VF |
7295 | |
7296 | return lower->private; | |
7297 | } | |
7298 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); | |
7299 | ||
4085ebe8 VY |
7300 | /** |
7301 | * netdev_lower_get_next - Get the next device from the lower neighbour | |
7302 | * list | |
7303 | * @dev: device | |
7304 | * @iter: list_head ** of the current position | |
7305 | * | |
7306 | * Gets the next netdev_adjacent from the dev's lower neighbour | |
7307 | * list, starting from iter position. The caller must hold RTNL lock or | |
7308 | * its own locking that guarantees that the neighbour lower | |
b469139e | 7309 | * list will remain unchanged. |
4085ebe8 VY |
7310 | */ |
7311 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) | |
7312 | { | |
7313 | struct netdev_adjacent *lower; | |
7314 | ||
cfdd28be | 7315 | lower = list_entry(*iter, struct netdev_adjacent, list); |
4085ebe8 VY |
7316 | |
7317 | if (&lower->list == &dev->adj_list.lower) | |
7318 | return NULL; | |
7319 | ||
cfdd28be | 7320 | *iter = lower->list.next; |
4085ebe8 VY |
7321 | |
7322 | return lower->dev; | |
7323 | } | |
7324 | EXPORT_SYMBOL(netdev_lower_get_next); | |
7325 | ||
1a3f060c DA |
7326 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
7327 | struct list_head **iter) | |
7328 | { | |
7329 | struct netdev_adjacent *lower; | |
7330 | ||
46b5ab1a | 7331 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
1a3f060c DA |
7332 | |
7333 | if (&lower->list == &dev->adj_list.lower) | |
7334 | return NULL; | |
7335 | ||
46b5ab1a | 7336 | *iter = &lower->list; |
1a3f060c DA |
7337 | |
7338 | return lower->dev; | |
7339 | } | |
7340 | ||
32b6d34f TY |
7341 | static struct net_device *__netdev_next_lower_dev(struct net_device *dev, |
7342 | struct list_head **iter, | |
7343 | bool *ignore) | |
7344 | { | |
7345 | struct netdev_adjacent *lower; | |
7346 | ||
7347 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7348 | ||
7349 | if (&lower->list == &dev->adj_list.lower) | |
7350 | return NULL; | |
7351 | ||
7352 | *iter = &lower->list; | |
7353 | *ignore = lower->ignore; | |
7354 | ||
7355 | return lower->dev; | |
7356 | } | |
7357 | ||
1a3f060c DA |
7358 | int netdev_walk_all_lower_dev(struct net_device *dev, |
7359 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7360 | struct netdev_nested_priv *priv), |
7361 | struct netdev_nested_priv *priv) | |
1a3f060c | 7362 | { |
5343da4c TY |
7363 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7364 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7365 | int ret, cur = 0; | |
1a3f060c | 7366 | |
5343da4c TY |
7367 | now = dev; |
7368 | iter = &dev->adj_list.lower; | |
1a3f060c | 7369 | |
5343da4c TY |
7370 | while (1) { |
7371 | if (now != dev) { | |
eff74233 | 7372 | ret = fn(now, priv); |
5343da4c TY |
7373 | if (ret) |
7374 | return ret; | |
7375 | } | |
7376 | ||
7377 | next = NULL; | |
7378 | while (1) { | |
7379 | ldev = netdev_next_lower_dev(now, &iter); | |
7380 | if (!ldev) | |
7381 | break; | |
7382 | ||
7383 | next = ldev; | |
7384 | niter = &ldev->adj_list.lower; | |
7385 | dev_stack[cur] = now; | |
7386 | iter_stack[cur++] = iter; | |
7387 | break; | |
7388 | } | |
7389 | ||
7390 | if (!next) { | |
7391 | if (!cur) | |
7392 | return 0; | |
7393 | next = dev_stack[--cur]; | |
7394 | niter = iter_stack[cur]; | |
7395 | } | |
7396 | ||
7397 | now = next; | |
7398 | iter = niter; | |
1a3f060c DA |
7399 | } |
7400 | ||
7401 | return 0; | |
7402 | } | |
7403 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); | |
7404 | ||
32b6d34f TY |
7405 | static int __netdev_walk_all_lower_dev(struct net_device *dev, |
7406 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7407 | struct netdev_nested_priv *priv), |
7408 | struct netdev_nested_priv *priv) | |
32b6d34f TY |
7409 | { |
7410 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7411 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7412 | int ret, cur = 0; | |
7413 | bool ignore; | |
7414 | ||
7415 | now = dev; | |
7416 | iter = &dev->adj_list.lower; | |
7417 | ||
7418 | while (1) { | |
7419 | if (now != dev) { | |
eff74233 | 7420 | ret = fn(now, priv); |
32b6d34f TY |
7421 | if (ret) |
7422 | return ret; | |
7423 | } | |
7424 | ||
7425 | next = NULL; | |
7426 | while (1) { | |
7427 | ldev = __netdev_next_lower_dev(now, &iter, &ignore); | |
7428 | if (!ldev) | |
7429 | break; | |
7430 | if (ignore) | |
7431 | continue; | |
7432 | ||
7433 | next = ldev; | |
7434 | niter = &ldev->adj_list.lower; | |
7435 | dev_stack[cur] = now; | |
7436 | iter_stack[cur++] = iter; | |
7437 | break; | |
7438 | } | |
7439 | ||
7440 | if (!next) { | |
7441 | if (!cur) | |
7442 | return 0; | |
7443 | next = dev_stack[--cur]; | |
7444 | niter = iter_stack[cur]; | |
7445 | } | |
7446 | ||
7447 | now = next; | |
7448 | iter = niter; | |
7449 | } | |
7450 | ||
7451 | return 0; | |
7452 | } | |
7453 | ||
7151affe TY |
7454 | struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
7455 | struct list_head **iter) | |
1a3f060c DA |
7456 | { |
7457 | struct netdev_adjacent *lower; | |
7458 | ||
7459 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7460 | if (&lower->list == &dev->adj_list.lower) | |
7461 | return NULL; | |
7462 | ||
7463 | *iter = &lower->list; | |
7464 | ||
7465 | return lower->dev; | |
7466 | } | |
7151affe | 7467 | EXPORT_SYMBOL(netdev_next_lower_dev_rcu); |
1a3f060c | 7468 | |
5343da4c TY |
7469 | static u8 __netdev_upper_depth(struct net_device *dev) |
7470 | { | |
7471 | struct net_device *udev; | |
7472 | struct list_head *iter; | |
7473 | u8 max_depth = 0; | |
32b6d34f | 7474 | bool ignore; |
5343da4c TY |
7475 | |
7476 | for (iter = &dev->adj_list.upper, | |
32b6d34f | 7477 | udev = __netdev_next_upper_dev(dev, &iter, &ignore); |
5343da4c | 7478 | udev; |
32b6d34f TY |
7479 | udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { |
7480 | if (ignore) | |
7481 | continue; | |
5343da4c TY |
7482 | if (max_depth < udev->upper_level) |
7483 | max_depth = udev->upper_level; | |
7484 | } | |
7485 | ||
7486 | return max_depth; | |
7487 | } | |
7488 | ||
7489 | static u8 __netdev_lower_depth(struct net_device *dev) | |
1a3f060c DA |
7490 | { |
7491 | struct net_device *ldev; | |
7492 | struct list_head *iter; | |
5343da4c | 7493 | u8 max_depth = 0; |
32b6d34f | 7494 | bool ignore; |
1a3f060c DA |
7495 | |
7496 | for (iter = &dev->adj_list.lower, | |
32b6d34f | 7497 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore); |
1a3f060c | 7498 | ldev; |
32b6d34f TY |
7499 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { |
7500 | if (ignore) | |
7501 | continue; | |
5343da4c TY |
7502 | if (max_depth < ldev->lower_level) |
7503 | max_depth = ldev->lower_level; | |
7504 | } | |
1a3f060c | 7505 | |
5343da4c TY |
7506 | return max_depth; |
7507 | } | |
7508 | ||
eff74233 TY |
7509 | static int __netdev_update_upper_level(struct net_device *dev, |
7510 | struct netdev_nested_priv *__unused) | |
5343da4c TY |
7511 | { |
7512 | dev->upper_level = __netdev_upper_depth(dev) + 1; | |
7513 | return 0; | |
7514 | } | |
7515 | ||
eff74233 | 7516 | static int __netdev_update_lower_level(struct net_device *dev, |
1fc70edb | 7517 | struct netdev_nested_priv *priv) |
5343da4c TY |
7518 | { |
7519 | dev->lower_level = __netdev_lower_depth(dev) + 1; | |
1fc70edb TY |
7520 | |
7521 | #ifdef CONFIG_LOCKDEP | |
7522 | if (!priv) | |
7523 | return 0; | |
7524 | ||
7525 | if (priv->flags & NESTED_SYNC_IMM) | |
7526 | dev->nested_level = dev->lower_level - 1; | |
7527 | if (priv->flags & NESTED_SYNC_TODO) | |
7528 | net_unlink_todo(dev); | |
7529 | #endif | |
5343da4c TY |
7530 | return 0; |
7531 | } | |
7532 | ||
7533 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, | |
7534 | int (*fn)(struct net_device *dev, | |
eff74233 TY |
7535 | struct netdev_nested_priv *priv), |
7536 | struct netdev_nested_priv *priv) | |
5343da4c TY |
7537 | { |
7538 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7539 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7540 | int ret, cur = 0; | |
7541 | ||
7542 | now = dev; | |
7543 | iter = &dev->adj_list.lower; | |
7544 | ||
7545 | while (1) { | |
7546 | if (now != dev) { | |
eff74233 | 7547 | ret = fn(now, priv); |
5343da4c TY |
7548 | if (ret) |
7549 | return ret; | |
7550 | } | |
7551 | ||
7552 | next = NULL; | |
7553 | while (1) { | |
7554 | ldev = netdev_next_lower_dev_rcu(now, &iter); | |
7555 | if (!ldev) | |
7556 | break; | |
7557 | ||
7558 | next = ldev; | |
7559 | niter = &ldev->adj_list.lower; | |
7560 | dev_stack[cur] = now; | |
7561 | iter_stack[cur++] = iter; | |
7562 | break; | |
7563 | } | |
7564 | ||
7565 | if (!next) { | |
7566 | if (!cur) | |
7567 | return 0; | |
7568 | next = dev_stack[--cur]; | |
7569 | niter = iter_stack[cur]; | |
7570 | } | |
7571 | ||
7572 | now = next; | |
7573 | iter = niter; | |
1a3f060c DA |
7574 | } |
7575 | ||
7576 | return 0; | |
7577 | } | |
7578 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); | |
7579 | ||
e001bfad | 7580 | /** |
7581 | * netdev_lower_get_first_private_rcu - Get the first ->private from the | |
7582 | * lower neighbour list, RCU | |
7583 | * variant | |
7584 | * @dev: device | |
7585 | * | |
7586 | * Gets the first netdev_adjacent->private from the dev's lower neighbour | |
7587 | * list. The caller must hold RCU read lock. | |
7588 | */ | |
7589 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) | |
7590 | { | |
7591 | struct netdev_adjacent *lower; | |
7592 | ||
7593 | lower = list_first_or_null_rcu(&dev->adj_list.lower, | |
7594 | struct netdev_adjacent, list); | |
7595 | if (lower) | |
7596 | return lower->private; | |
7597 | return NULL; | |
7598 | } | |
7599 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); | |
7600 | ||
9ff162a8 JP |
7601 | /** |
7602 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
7603 | * @dev: device | |
7604 | * | |
7605 | * Find a master upper device and return pointer to it or NULL in case | |
7606 | * it's not there. The caller must hold the RCU read lock. | |
7607 | */ | |
7608 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
7609 | { | |
aa9d8560 | 7610 | struct netdev_adjacent *upper; |
9ff162a8 | 7611 | |
2f268f12 | 7612 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
aa9d8560 | 7613 | struct netdev_adjacent, list); |
9ff162a8 JP |
7614 | if (upper && likely(upper->master)) |
7615 | return upper->dev; | |
7616 | return NULL; | |
7617 | } | |
7618 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
7619 | ||
0a59f3a9 | 7620 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
3ee32707 VF |
7621 | struct net_device *adj_dev, |
7622 | struct list_head *dev_list) | |
7623 | { | |
7624 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7625 | |
3ee32707 VF |
7626 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7627 | "upper_%s" : "lower_%s", adj_dev->name); | |
7628 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), | |
7629 | linkname); | |
7630 | } | |
0a59f3a9 | 7631 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
3ee32707 VF |
7632 | char *name, |
7633 | struct list_head *dev_list) | |
7634 | { | |
7635 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7636 | |
3ee32707 VF |
7637 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7638 | "upper_%s" : "lower_%s", name); | |
7639 | sysfs_remove_link(&(dev->dev.kobj), linkname); | |
7640 | } | |
7641 | ||
7ce64c79 AF |
7642 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
7643 | struct net_device *adj_dev, | |
7644 | struct list_head *dev_list) | |
7645 | { | |
7646 | return (dev_list == &dev->adj_list.upper || | |
7647 | dev_list == &dev->adj_list.lower) && | |
7648 | net_eq(dev_net(dev), dev_net(adj_dev)); | |
7649 | } | |
3ee32707 | 7650 | |
5d261913 VF |
7651 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
7652 | struct net_device *adj_dev, | |
7863c054 | 7653 | struct list_head *dev_list, |
402dae96 | 7654 | void *private, bool master) |
5d261913 VF |
7655 | { |
7656 | struct netdev_adjacent *adj; | |
842d67a7 | 7657 | int ret; |
5d261913 | 7658 | |
6ea29da1 | 7659 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 VF |
7660 | |
7661 | if (adj) { | |
790510d9 | 7662 | adj->ref_nr += 1; |
67b62f98 DA |
7663 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
7664 | dev->name, adj_dev->name, adj->ref_nr); | |
7665 | ||
5d261913 VF |
7666 | return 0; |
7667 | } | |
7668 | ||
7669 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); | |
7670 | if (!adj) | |
7671 | return -ENOMEM; | |
7672 | ||
7673 | adj->dev = adj_dev; | |
7674 | adj->master = master; | |
790510d9 | 7675 | adj->ref_nr = 1; |
402dae96 | 7676 | adj->private = private; |
32b6d34f | 7677 | adj->ignore = false; |
5d261913 | 7678 | dev_hold(adj_dev); |
2f268f12 | 7679 | |
67b62f98 DA |
7680 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
7681 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); | |
5d261913 | 7682 | |
7ce64c79 | 7683 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
3ee32707 | 7684 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
5831d66e VF |
7685 | if (ret) |
7686 | goto free_adj; | |
7687 | } | |
7688 | ||
7863c054 | 7689 | /* Ensure that master link is always the first item in list. */ |
842d67a7 VF |
7690 | if (master) { |
7691 | ret = sysfs_create_link(&(dev->dev.kobj), | |
7692 | &(adj_dev->dev.kobj), "master"); | |
7693 | if (ret) | |
5831d66e | 7694 | goto remove_symlinks; |
842d67a7 | 7695 | |
7863c054 | 7696 | list_add_rcu(&adj->list, dev_list); |
842d67a7 | 7697 | } else { |
7863c054 | 7698 | list_add_tail_rcu(&adj->list, dev_list); |
842d67a7 | 7699 | } |
5d261913 VF |
7700 | |
7701 | return 0; | |
842d67a7 | 7702 | |
5831d66e | 7703 | remove_symlinks: |
7ce64c79 | 7704 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7705 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
842d67a7 VF |
7706 | free_adj: |
7707 | kfree(adj); | |
974daef7 | 7708 | dev_put(adj_dev); |
842d67a7 VF |
7709 | |
7710 | return ret; | |
5d261913 VF |
7711 | } |
7712 | ||
1d143d9f | 7713 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
7714 | struct net_device *adj_dev, | |
93409033 | 7715 | u16 ref_nr, |
1d143d9f | 7716 | struct list_head *dev_list) |
5d261913 VF |
7717 | { |
7718 | struct netdev_adjacent *adj; | |
7719 | ||
67b62f98 DA |
7720 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
7721 | dev->name, adj_dev->name, ref_nr); | |
7722 | ||
6ea29da1 | 7723 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 | 7724 | |
2f268f12 | 7725 | if (!adj) { |
67b62f98 | 7726 | pr_err("Adjacency does not exist for device %s from %s\n", |
2f268f12 | 7727 | dev->name, adj_dev->name); |
67b62f98 DA |
7728 | WARN_ON(1); |
7729 | return; | |
2f268f12 | 7730 | } |
5d261913 | 7731 | |
93409033 | 7732 | if (adj->ref_nr > ref_nr) { |
67b62f98 DA |
7733 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
7734 | dev->name, adj_dev->name, ref_nr, | |
7735 | adj->ref_nr - ref_nr); | |
93409033 | 7736 | adj->ref_nr -= ref_nr; |
5d261913 VF |
7737 | return; |
7738 | } | |
7739 | ||
842d67a7 VF |
7740 | if (adj->master) |
7741 | sysfs_remove_link(&(dev->dev.kobj), "master"); | |
7742 | ||
7ce64c79 | 7743 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7744 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
5831d66e | 7745 | |
5d261913 | 7746 | list_del_rcu(&adj->list); |
67b62f98 | 7747 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
2f268f12 | 7748 | adj_dev->name, dev->name, adj_dev->name); |
5d261913 VF |
7749 | dev_put(adj_dev); |
7750 | kfree_rcu(adj, rcu); | |
7751 | } | |
7752 | ||
1d143d9f | 7753 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
7754 | struct net_device *upper_dev, | |
7755 | struct list_head *up_list, | |
7756 | struct list_head *down_list, | |
7757 | void *private, bool master) | |
5d261913 VF |
7758 | { |
7759 | int ret; | |
7760 | ||
790510d9 | 7761 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
93409033 | 7762 | private, master); |
5d261913 VF |
7763 | if (ret) |
7764 | return ret; | |
7765 | ||
790510d9 | 7766 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
93409033 | 7767 | private, false); |
5d261913 | 7768 | if (ret) { |
790510d9 | 7769 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
5d261913 VF |
7770 | return ret; |
7771 | } | |
7772 | ||
7773 | return 0; | |
7774 | } | |
7775 | ||
1d143d9f | 7776 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
7777 | struct net_device *upper_dev, | |
93409033 | 7778 | u16 ref_nr, |
1d143d9f | 7779 | struct list_head *up_list, |
7780 | struct list_head *down_list) | |
5d261913 | 7781 | { |
93409033 AC |
7782 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
7783 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); | |
5d261913 VF |
7784 | } |
7785 | ||
1d143d9f | 7786 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
7787 | struct net_device *upper_dev, | |
7788 | void *private, bool master) | |
2f268f12 | 7789 | { |
f1170fd4 DA |
7790 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
7791 | &dev->adj_list.upper, | |
7792 | &upper_dev->adj_list.lower, | |
7793 | private, master); | |
5d261913 VF |
7794 | } |
7795 | ||
1d143d9f | 7796 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
7797 | struct net_device *upper_dev) | |
2f268f12 | 7798 | { |
93409033 | 7799 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
2f268f12 VF |
7800 | &dev->adj_list.upper, |
7801 | &upper_dev->adj_list.lower); | |
7802 | } | |
5d261913 | 7803 | |
9ff162a8 | 7804 | static int __netdev_upper_dev_link(struct net_device *dev, |
402dae96 | 7805 | struct net_device *upper_dev, bool master, |
42ab19ee | 7806 | void *upper_priv, void *upper_info, |
1fc70edb | 7807 | struct netdev_nested_priv *priv, |
42ab19ee | 7808 | struct netlink_ext_ack *extack) |
9ff162a8 | 7809 | { |
51d0c047 DA |
7810 | struct netdev_notifier_changeupper_info changeupper_info = { |
7811 | .info = { | |
7812 | .dev = dev, | |
42ab19ee | 7813 | .extack = extack, |
51d0c047 DA |
7814 | }, |
7815 | .upper_dev = upper_dev, | |
7816 | .master = master, | |
7817 | .linking = true, | |
7818 | .upper_info = upper_info, | |
7819 | }; | |
50d629e7 | 7820 | struct net_device *master_dev; |
5d261913 | 7821 | int ret = 0; |
9ff162a8 JP |
7822 | |
7823 | ASSERT_RTNL(); | |
7824 | ||
7825 | if (dev == upper_dev) | |
7826 | return -EBUSY; | |
7827 | ||
7828 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
32b6d34f | 7829 | if (__netdev_has_upper_dev(upper_dev, dev)) |
9ff162a8 JP |
7830 | return -EBUSY; |
7831 | ||
5343da4c TY |
7832 | if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) |
7833 | return -EMLINK; | |
7834 | ||
50d629e7 | 7835 | if (!master) { |
32b6d34f | 7836 | if (__netdev_has_upper_dev(dev, upper_dev)) |
50d629e7 MM |
7837 | return -EEXIST; |
7838 | } else { | |
32b6d34f | 7839 | master_dev = __netdev_master_upper_dev_get(dev); |
50d629e7 MM |
7840 | if (master_dev) |
7841 | return master_dev == upper_dev ? -EEXIST : -EBUSY; | |
7842 | } | |
9ff162a8 | 7843 | |
51d0c047 | 7844 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7845 | &changeupper_info.info); |
7846 | ret = notifier_to_errno(ret); | |
7847 | if (ret) | |
7848 | return ret; | |
7849 | ||
6dffb044 | 7850 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
402dae96 | 7851 | master); |
5d261913 VF |
7852 | if (ret) |
7853 | return ret; | |
9ff162a8 | 7854 | |
51d0c047 | 7855 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
b03804e7 IS |
7856 | &changeupper_info.info); |
7857 | ret = notifier_to_errno(ret); | |
7858 | if (ret) | |
f1170fd4 | 7859 | goto rollback; |
b03804e7 | 7860 | |
5343da4c | 7861 | __netdev_update_upper_level(dev, NULL); |
32b6d34f | 7862 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c | 7863 | |
1fc70edb | 7864 | __netdev_update_lower_level(upper_dev, priv); |
32b6d34f | 7865 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
1fc70edb | 7866 | priv); |
5343da4c | 7867 | |
9ff162a8 | 7868 | return 0; |
5d261913 | 7869 | |
f1170fd4 | 7870 | rollback: |
2f268f12 | 7871 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 VF |
7872 | |
7873 | return ret; | |
9ff162a8 JP |
7874 | } |
7875 | ||
7876 | /** | |
7877 | * netdev_upper_dev_link - Add a link to the upper device | |
7878 | * @dev: device | |
7879 | * @upper_dev: new upper device | |
7a006d59 | 7880 | * @extack: netlink extended ack |
9ff162a8 JP |
7881 | * |
7882 | * Adds a link to device which is upper to this one. The caller must hold | |
7883 | * the RTNL lock. On a failure a negative errno code is returned. | |
7884 | * On success the reference counts are adjusted and the function | |
7885 | * returns zero. | |
7886 | */ | |
7887 | int netdev_upper_dev_link(struct net_device *dev, | |
42ab19ee DA |
7888 | struct net_device *upper_dev, |
7889 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7890 | { |
1fc70edb TY |
7891 | struct netdev_nested_priv priv = { |
7892 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7893 | .data = NULL, | |
7894 | }; | |
7895 | ||
42ab19ee | 7896 | return __netdev_upper_dev_link(dev, upper_dev, false, |
1fc70edb | 7897 | NULL, NULL, &priv, extack); |
9ff162a8 JP |
7898 | } |
7899 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
7900 | ||
7901 | /** | |
7902 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
7903 | * @dev: device | |
7904 | * @upper_dev: new upper device | |
6dffb044 | 7905 | * @upper_priv: upper device private |
29bf24af | 7906 | * @upper_info: upper info to be passed down via notifier |
7a006d59 | 7907 | * @extack: netlink extended ack |
9ff162a8 JP |
7908 | * |
7909 | * Adds a link to device which is upper to this one. In this case, only | |
7910 | * one master upper device can be linked, although other non-master devices | |
7911 | * might be linked as well. The caller must hold the RTNL lock. | |
7912 | * On a failure a negative errno code is returned. On success the reference | |
7913 | * counts are adjusted and the function returns zero. | |
7914 | */ | |
7915 | int netdev_master_upper_dev_link(struct net_device *dev, | |
6dffb044 | 7916 | struct net_device *upper_dev, |
42ab19ee DA |
7917 | void *upper_priv, void *upper_info, |
7918 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7919 | { |
1fc70edb TY |
7920 | struct netdev_nested_priv priv = { |
7921 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
7922 | .data = NULL, | |
7923 | }; | |
7924 | ||
29bf24af | 7925 | return __netdev_upper_dev_link(dev, upper_dev, true, |
1fc70edb | 7926 | upper_priv, upper_info, &priv, extack); |
9ff162a8 JP |
7927 | } |
7928 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
7929 | ||
fe8300fd | 7930 | static void __netdev_upper_dev_unlink(struct net_device *dev, |
1fc70edb TY |
7931 | struct net_device *upper_dev, |
7932 | struct netdev_nested_priv *priv) | |
9ff162a8 | 7933 | { |
51d0c047 DA |
7934 | struct netdev_notifier_changeupper_info changeupper_info = { |
7935 | .info = { | |
7936 | .dev = dev, | |
7937 | }, | |
7938 | .upper_dev = upper_dev, | |
7939 | .linking = false, | |
7940 | }; | |
f4563a75 | 7941 | |
9ff162a8 JP |
7942 | ASSERT_RTNL(); |
7943 | ||
0e4ead9d | 7944 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
0e4ead9d | 7945 | |
51d0c047 | 7946 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7947 | &changeupper_info.info); |
7948 | ||
2f268f12 | 7949 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 | 7950 | |
51d0c047 | 7951 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
0e4ead9d | 7952 | &changeupper_info.info); |
5343da4c TY |
7953 | |
7954 | __netdev_update_upper_level(dev, NULL); | |
32b6d34f | 7955 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c | 7956 | |
1fc70edb | 7957 | __netdev_update_lower_level(upper_dev, priv); |
32b6d34f | 7958 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
1fc70edb | 7959 | priv); |
9ff162a8 | 7960 | } |
fe8300fd TY |
7961 | |
7962 | /** | |
7963 | * netdev_upper_dev_unlink - Removes a link to upper device | |
7964 | * @dev: device | |
7965 | * @upper_dev: new upper device | |
7966 | * | |
7967 | * Removes a link to device which is upper to this one. The caller must hold | |
7968 | * the RTNL lock. | |
7969 | */ | |
7970 | void netdev_upper_dev_unlink(struct net_device *dev, | |
7971 | struct net_device *upper_dev) | |
7972 | { | |
1fc70edb TY |
7973 | struct netdev_nested_priv priv = { |
7974 | .flags = NESTED_SYNC_TODO, | |
7975 | .data = NULL, | |
7976 | }; | |
7977 | ||
7978 | __netdev_upper_dev_unlink(dev, upper_dev, &priv); | |
9ff162a8 JP |
7979 | } |
7980 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
7981 | ||
32b6d34f TY |
7982 | static void __netdev_adjacent_dev_set(struct net_device *upper_dev, |
7983 | struct net_device *lower_dev, | |
7984 | bool val) | |
7985 | { | |
7986 | struct netdev_adjacent *adj; | |
7987 | ||
7988 | adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); | |
7989 | if (adj) | |
7990 | adj->ignore = val; | |
7991 | ||
7992 | adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); | |
7993 | if (adj) | |
7994 | adj->ignore = val; | |
7995 | } | |
7996 | ||
7997 | static void netdev_adjacent_dev_disable(struct net_device *upper_dev, | |
7998 | struct net_device *lower_dev) | |
7999 | { | |
8000 | __netdev_adjacent_dev_set(upper_dev, lower_dev, true); | |
8001 | } | |
8002 | ||
8003 | static void netdev_adjacent_dev_enable(struct net_device *upper_dev, | |
8004 | struct net_device *lower_dev) | |
8005 | { | |
8006 | __netdev_adjacent_dev_set(upper_dev, lower_dev, false); | |
8007 | } | |
8008 | ||
8009 | int netdev_adjacent_change_prepare(struct net_device *old_dev, | |
8010 | struct net_device *new_dev, | |
8011 | struct net_device *dev, | |
8012 | struct netlink_ext_ack *extack) | |
8013 | { | |
1fc70edb TY |
8014 | struct netdev_nested_priv priv = { |
8015 | .flags = 0, | |
8016 | .data = NULL, | |
8017 | }; | |
32b6d34f TY |
8018 | int err; |
8019 | ||
8020 | if (!new_dev) | |
8021 | return 0; | |
8022 | ||
8023 | if (old_dev && new_dev != old_dev) | |
8024 | netdev_adjacent_dev_disable(dev, old_dev); | |
1fc70edb TY |
8025 | err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv, |
8026 | extack); | |
32b6d34f TY |
8027 | if (err) { |
8028 | if (old_dev && new_dev != old_dev) | |
8029 | netdev_adjacent_dev_enable(dev, old_dev); | |
8030 | return err; | |
8031 | } | |
8032 | ||
8033 | return 0; | |
8034 | } | |
8035 | EXPORT_SYMBOL(netdev_adjacent_change_prepare); | |
8036 | ||
8037 | void netdev_adjacent_change_commit(struct net_device *old_dev, | |
8038 | struct net_device *new_dev, | |
8039 | struct net_device *dev) | |
8040 | { | |
1fc70edb TY |
8041 | struct netdev_nested_priv priv = { |
8042 | .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, | |
8043 | .data = NULL, | |
8044 | }; | |
8045 | ||
32b6d34f TY |
8046 | if (!new_dev || !old_dev) |
8047 | return; | |
8048 | ||
8049 | if (new_dev == old_dev) | |
8050 | return; | |
8051 | ||
8052 | netdev_adjacent_dev_enable(dev, old_dev); | |
1fc70edb | 8053 | __netdev_upper_dev_unlink(old_dev, dev, &priv); |
32b6d34f TY |
8054 | } |
8055 | EXPORT_SYMBOL(netdev_adjacent_change_commit); | |
8056 | ||
8057 | void netdev_adjacent_change_abort(struct net_device *old_dev, | |
8058 | struct net_device *new_dev, | |
8059 | struct net_device *dev) | |
8060 | { | |
1fc70edb TY |
8061 | struct netdev_nested_priv priv = { |
8062 | .flags = 0, | |
8063 | .data = NULL, | |
8064 | }; | |
8065 | ||
32b6d34f TY |
8066 | if (!new_dev) |
8067 | return; | |
8068 | ||
8069 | if (old_dev && new_dev != old_dev) | |
8070 | netdev_adjacent_dev_enable(dev, old_dev); | |
8071 | ||
1fc70edb | 8072 | __netdev_upper_dev_unlink(new_dev, dev, &priv); |
32b6d34f TY |
8073 | } |
8074 | EXPORT_SYMBOL(netdev_adjacent_change_abort); | |
8075 | ||
61bd3857 MS |
8076 | /** |
8077 | * netdev_bonding_info_change - Dispatch event about slave change | |
8078 | * @dev: device | |
4a26e453 | 8079 | * @bonding_info: info to dispatch |
61bd3857 MS |
8080 | * |
8081 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. | |
8082 | * The caller must hold the RTNL lock. | |
8083 | */ | |
8084 | void netdev_bonding_info_change(struct net_device *dev, | |
8085 | struct netdev_bonding_info *bonding_info) | |
8086 | { | |
51d0c047 DA |
8087 | struct netdev_notifier_bonding_info info = { |
8088 | .info.dev = dev, | |
8089 | }; | |
61bd3857 MS |
8090 | |
8091 | memcpy(&info.bonding_info, bonding_info, | |
8092 | sizeof(struct netdev_bonding_info)); | |
51d0c047 | 8093 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
61bd3857 MS |
8094 | &info.info); |
8095 | } | |
8096 | EXPORT_SYMBOL(netdev_bonding_info_change); | |
8097 | ||
cff9f12b MG |
8098 | /** |
8099 | * netdev_get_xmit_slave - Get the xmit slave of master device | |
8842500d | 8100 | * @dev: device |
cff9f12b MG |
8101 | * @skb: The packet |
8102 | * @all_slaves: assume all the slaves are active | |
8103 | * | |
8104 | * The reference counters are not incremented so the caller must be | |
8105 | * careful with locks. The caller must hold RCU lock. | |
8106 | * %NULL is returned if no slave is found. | |
8107 | */ | |
8108 | ||
8109 | struct net_device *netdev_get_xmit_slave(struct net_device *dev, | |
8110 | struct sk_buff *skb, | |
8111 | bool all_slaves) | |
8112 | { | |
8113 | const struct net_device_ops *ops = dev->netdev_ops; | |
8114 | ||
8115 | if (!ops->ndo_get_xmit_slave) | |
8116 | return NULL; | |
8117 | return ops->ndo_get_xmit_slave(dev, skb, all_slaves); | |
8118 | } | |
8119 | EXPORT_SYMBOL(netdev_get_xmit_slave); | |
8120 | ||
719a402c TT |
8121 | static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev, |
8122 | struct sock *sk) | |
8123 | { | |
8124 | const struct net_device_ops *ops = dev->netdev_ops; | |
8125 | ||
8126 | if (!ops->ndo_sk_get_lower_dev) | |
8127 | return NULL; | |
8128 | return ops->ndo_sk_get_lower_dev(dev, sk); | |
8129 | } | |
8130 | ||
8131 | /** | |
8132 | * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket | |
8133 | * @dev: device | |
8134 | * @sk: the socket | |
8135 | * | |
8136 | * %NULL is returned if no lower device is found. | |
8137 | */ | |
8138 | ||
8139 | struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev, | |
8140 | struct sock *sk) | |
8141 | { | |
8142 | struct net_device *lower; | |
8143 | ||
8144 | lower = netdev_sk_get_lower_dev(dev, sk); | |
8145 | while (lower) { | |
8146 | dev = lower; | |
8147 | lower = netdev_sk_get_lower_dev(dev, sk); | |
8148 | } | |
8149 | ||
8150 | return dev; | |
8151 | } | |
8152 | EXPORT_SYMBOL(netdev_sk_get_lowest_dev); | |
8153 | ||
2ce1ee17 | 8154 | static void netdev_adjacent_add_links(struct net_device *dev) |
4c75431a AF |
8155 | { |
8156 | struct netdev_adjacent *iter; | |
8157 | ||
8158 | struct net *net = dev_net(dev); | |
8159 | ||
8160 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8161 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8162 | continue; |
8163 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8164 | &iter->dev->adj_list.lower); | |
8165 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
8166 | &dev->adj_list.upper); | |
8167 | } | |
8168 | ||
8169 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8170 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8171 | continue; |
8172 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8173 | &iter->dev->adj_list.upper); | |
8174 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
8175 | &dev->adj_list.lower); | |
8176 | } | |
8177 | } | |
8178 | ||
2ce1ee17 | 8179 | static void netdev_adjacent_del_links(struct net_device *dev) |
4c75431a AF |
8180 | { |
8181 | struct netdev_adjacent *iter; | |
8182 | ||
8183 | struct net *net = dev_net(dev); | |
8184 | ||
8185 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 8186 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8187 | continue; |
8188 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8189 | &iter->dev->adj_list.lower); | |
8190 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8191 | &dev->adj_list.upper); | |
8192 | } | |
8193 | ||
8194 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 8195 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
8196 | continue; |
8197 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
8198 | &iter->dev->adj_list.upper); | |
8199 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
8200 | &dev->adj_list.lower); | |
8201 | } | |
8202 | } | |
8203 | ||
5bb025fa | 8204 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
402dae96 | 8205 | { |
5bb025fa | 8206 | struct netdev_adjacent *iter; |
402dae96 | 8207 | |
4c75431a AF |
8208 | struct net *net = dev_net(dev); |
8209 | ||
5bb025fa | 8210 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
be4da0e3 | 8211 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8212 | continue; |
5bb025fa VF |
8213 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8214 | &iter->dev->adj_list.lower); | |
8215 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8216 | &iter->dev->adj_list.lower); | |
8217 | } | |
402dae96 | 8218 | |
5bb025fa | 8219 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
be4da0e3 | 8220 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 8221 | continue; |
5bb025fa VF |
8222 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
8223 | &iter->dev->adj_list.upper); | |
8224 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
8225 | &iter->dev->adj_list.upper); | |
8226 | } | |
402dae96 | 8227 | } |
402dae96 VF |
8228 | |
8229 | void *netdev_lower_dev_get_private(struct net_device *dev, | |
8230 | struct net_device *lower_dev) | |
8231 | { | |
8232 | struct netdev_adjacent *lower; | |
8233 | ||
8234 | if (!lower_dev) | |
8235 | return NULL; | |
6ea29da1 | 8236 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
402dae96 VF |
8237 | if (!lower) |
8238 | return NULL; | |
8239 | ||
8240 | return lower->private; | |
8241 | } | |
8242 | EXPORT_SYMBOL(netdev_lower_dev_get_private); | |
8243 | ||
4085ebe8 | 8244 | |
04d48266 | 8245 | /** |
c1639be9 | 8246 | * netdev_lower_state_changed - Dispatch event about lower device state change |
04d48266 JP |
8247 | * @lower_dev: device |
8248 | * @lower_state_info: state to dispatch | |
8249 | * | |
8250 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. | |
8251 | * The caller must hold the RTNL lock. | |
8252 | */ | |
8253 | void netdev_lower_state_changed(struct net_device *lower_dev, | |
8254 | void *lower_state_info) | |
8255 | { | |
51d0c047 DA |
8256 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
8257 | .info.dev = lower_dev, | |
8258 | }; | |
04d48266 JP |
8259 | |
8260 | ASSERT_RTNL(); | |
8261 | changelowerstate_info.lower_state_info = lower_state_info; | |
51d0c047 | 8262 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
04d48266 JP |
8263 | &changelowerstate_info.info); |
8264 | } | |
8265 | EXPORT_SYMBOL(netdev_lower_state_changed); | |
8266 | ||
b6c40d68 PM |
8267 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
8268 | { | |
d314774c SH |
8269 | const struct net_device_ops *ops = dev->netdev_ops; |
8270 | ||
d2615bf4 | 8271 | if (ops->ndo_change_rx_flags) |
d314774c | 8272 | ops->ndo_change_rx_flags(dev, flags); |
b6c40d68 PM |
8273 | } |
8274 | ||
991fb3f7 | 8275 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8276 | { |
b536db93 | 8277 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
8278 | kuid_t uid; |
8279 | kgid_t gid; | |
1da177e4 | 8280 | |
24023451 PM |
8281 | ASSERT_RTNL(); |
8282 | ||
dad9b335 WC |
8283 | dev->flags |= IFF_PROMISC; |
8284 | dev->promiscuity += inc; | |
8285 | if (dev->promiscuity == 0) { | |
8286 | /* | |
8287 | * Avoid overflow. | |
8288 | * If inc causes overflow, untouch promisc and return error. | |
8289 | */ | |
8290 | if (inc < 0) | |
8291 | dev->flags &= ~IFF_PROMISC; | |
8292 | else { | |
8293 | dev->promiscuity -= inc; | |
7b6cd1ce JP |
8294 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
8295 | dev->name); | |
dad9b335 WC |
8296 | return -EOVERFLOW; |
8297 | } | |
8298 | } | |
52609c0b | 8299 | if (dev->flags != old_flags) { |
7b6cd1ce JP |
8300 | pr_info("device %s %s promiscuous mode\n", |
8301 | dev->name, | |
8302 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
8303 | if (audit_enabled) { |
8304 | current_uid_gid(&uid, &gid); | |
cdfb6b34 RGB |
8305 | audit_log(audit_context(), GFP_ATOMIC, |
8306 | AUDIT_ANOM_PROMISCUOUS, | |
8307 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
8308 | dev->name, (dev->flags & IFF_PROMISC), | |
8309 | (old_flags & IFF_PROMISC), | |
8310 | from_kuid(&init_user_ns, audit_get_loginuid(current)), | |
8311 | from_kuid(&init_user_ns, uid), | |
8312 | from_kgid(&init_user_ns, gid), | |
8313 | audit_get_sessionid(current)); | |
8192b0c4 | 8314 | } |
24023451 | 8315 | |
b6c40d68 | 8316 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 8317 | } |
991fb3f7 ND |
8318 | if (notify) |
8319 | __dev_notify_flags(dev, old_flags, IFF_PROMISC); | |
dad9b335 | 8320 | return 0; |
1da177e4 LT |
8321 | } |
8322 | ||
4417da66 PM |
8323 | /** |
8324 | * dev_set_promiscuity - update promiscuity count on a device | |
8325 | * @dev: device | |
8326 | * @inc: modifier | |
8327 | * | |
8328 | * Add or remove promiscuity from a device. While the count in the device | |
8329 | * remains above zero the interface remains promiscuous. Once it hits zero | |
8330 | * the device reverts back to normal filtering operation. A negative inc | |
8331 | * value is used to drop promiscuity on the device. | |
dad9b335 | 8332 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 8333 | */ |
dad9b335 | 8334 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 8335 | { |
b536db93 | 8336 | unsigned int old_flags = dev->flags; |
dad9b335 | 8337 | int err; |
4417da66 | 8338 | |
991fb3f7 | 8339 | err = __dev_set_promiscuity(dev, inc, true); |
4b5a698e | 8340 | if (err < 0) |
dad9b335 | 8341 | return err; |
4417da66 PM |
8342 | if (dev->flags != old_flags) |
8343 | dev_set_rx_mode(dev); | |
dad9b335 | 8344 | return err; |
4417da66 | 8345 | } |
d1b19dff | 8346 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 8347 | |
991fb3f7 | 8348 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
1da177e4 | 8349 | { |
991fb3f7 | 8350 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
1da177e4 | 8351 | |
24023451 PM |
8352 | ASSERT_RTNL(); |
8353 | ||
1da177e4 | 8354 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
8355 | dev->allmulti += inc; |
8356 | if (dev->allmulti == 0) { | |
8357 | /* | |
8358 | * Avoid overflow. | |
8359 | * If inc causes overflow, untouch allmulti and return error. | |
8360 | */ | |
8361 | if (inc < 0) | |
8362 | dev->flags &= ~IFF_ALLMULTI; | |
8363 | else { | |
8364 | dev->allmulti -= inc; | |
7b6cd1ce JP |
8365 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
8366 | dev->name); | |
dad9b335 WC |
8367 | return -EOVERFLOW; |
8368 | } | |
8369 | } | |
24023451 | 8370 | if (dev->flags ^ old_flags) { |
b6c40d68 | 8371 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 8372 | dev_set_rx_mode(dev); |
991fb3f7 ND |
8373 | if (notify) |
8374 | __dev_notify_flags(dev, old_flags, | |
8375 | dev->gflags ^ old_gflags); | |
24023451 | 8376 | } |
dad9b335 | 8377 | return 0; |
4417da66 | 8378 | } |
991fb3f7 ND |
8379 | |
8380 | /** | |
8381 | * dev_set_allmulti - update allmulti count on a device | |
8382 | * @dev: device | |
8383 | * @inc: modifier | |
8384 | * | |
8385 | * Add or remove reception of all multicast frames to a device. While the | |
8386 | * count in the device remains above zero the interface remains listening | |
8387 | * to all interfaces. Once it hits zero the device reverts back to normal | |
8388 | * filtering operation. A negative @inc value is used to drop the counter | |
8389 | * when releasing a resource needing all multicasts. | |
8390 | * Return 0 if successful or a negative errno code on error. | |
8391 | */ | |
8392 | ||
8393 | int dev_set_allmulti(struct net_device *dev, int inc) | |
8394 | { | |
8395 | return __dev_set_allmulti(dev, inc, true); | |
8396 | } | |
d1b19dff | 8397 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
8398 | |
8399 | /* | |
8400 | * Upload unicast and multicast address lists to device and | |
8401 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 8402 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
8403 | * are present. |
8404 | */ | |
8405 | void __dev_set_rx_mode(struct net_device *dev) | |
8406 | { | |
d314774c SH |
8407 | const struct net_device_ops *ops = dev->netdev_ops; |
8408 | ||
4417da66 PM |
8409 | /* dev_open will call this function so the list will stay sane. */ |
8410 | if (!(dev->flags&IFF_UP)) | |
8411 | return; | |
8412 | ||
8413 | if (!netif_device_present(dev)) | |
40b77c94 | 8414 | return; |
4417da66 | 8415 | |
01789349 | 8416 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
8417 | /* Unicast addresses changes may only happen under the rtnl, |
8418 | * therefore calling __dev_set_promiscuity here is safe. | |
8419 | */ | |
32e7bfc4 | 8420 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
991fb3f7 | 8421 | __dev_set_promiscuity(dev, 1, false); |
2d348d1f | 8422 | dev->uc_promisc = true; |
32e7bfc4 | 8423 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
991fb3f7 | 8424 | __dev_set_promiscuity(dev, -1, false); |
2d348d1f | 8425 | dev->uc_promisc = false; |
4417da66 | 8426 | } |
4417da66 | 8427 | } |
01789349 JP |
8428 | |
8429 | if (ops->ndo_set_rx_mode) | |
8430 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
8431 | } |
8432 | ||
8433 | void dev_set_rx_mode(struct net_device *dev) | |
8434 | { | |
b9e40857 | 8435 | netif_addr_lock_bh(dev); |
4417da66 | 8436 | __dev_set_rx_mode(dev); |
b9e40857 | 8437 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
8438 | } |
8439 | ||
f0db275a SH |
8440 | /** |
8441 | * dev_get_flags - get flags reported to userspace | |
8442 | * @dev: device | |
8443 | * | |
8444 | * Get the combination of flag bits exported through APIs to userspace. | |
8445 | */ | |
95c96174 | 8446 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 8447 | { |
95c96174 | 8448 | unsigned int flags; |
1da177e4 LT |
8449 | |
8450 | flags = (dev->flags & ~(IFF_PROMISC | | |
8451 | IFF_ALLMULTI | | |
b00055aa SR |
8452 | IFF_RUNNING | |
8453 | IFF_LOWER_UP | | |
8454 | IFF_DORMANT)) | | |
1da177e4 LT |
8455 | (dev->gflags & (IFF_PROMISC | |
8456 | IFF_ALLMULTI)); | |
8457 | ||
b00055aa SR |
8458 | if (netif_running(dev)) { |
8459 | if (netif_oper_up(dev)) | |
8460 | flags |= IFF_RUNNING; | |
8461 | if (netif_carrier_ok(dev)) | |
8462 | flags |= IFF_LOWER_UP; | |
8463 | if (netif_dormant(dev)) | |
8464 | flags |= IFF_DORMANT; | |
8465 | } | |
1da177e4 LT |
8466 | |
8467 | return flags; | |
8468 | } | |
d1b19dff | 8469 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 8470 | |
6d040321 PM |
8471 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
8472 | struct netlink_ext_ack *extack) | |
1da177e4 | 8473 | { |
b536db93 | 8474 | unsigned int old_flags = dev->flags; |
bd380811 | 8475 | int ret; |
1da177e4 | 8476 | |
24023451 PM |
8477 | ASSERT_RTNL(); |
8478 | ||
1da177e4 LT |
8479 | /* |
8480 | * Set the flags on our device. | |
8481 | */ | |
8482 | ||
8483 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
8484 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
8485 | IFF_AUTOMEDIA)) | | |
8486 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
8487 | IFF_ALLMULTI)); | |
8488 | ||
8489 | /* | |
8490 | * Load in the correct multicast list now the flags have changed. | |
8491 | */ | |
8492 | ||
b6c40d68 PM |
8493 | if ((old_flags ^ flags) & IFF_MULTICAST) |
8494 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 8495 | |
4417da66 | 8496 | dev_set_rx_mode(dev); |
1da177e4 LT |
8497 | |
8498 | /* | |
8499 | * Have we downed the interface. We handle IFF_UP ourselves | |
8500 | * according to user attempts to set it, rather than blindly | |
8501 | * setting it. | |
8502 | */ | |
8503 | ||
8504 | ret = 0; | |
7051b88a | 8505 | if ((old_flags ^ flags) & IFF_UP) { |
8506 | if (old_flags & IFF_UP) | |
8507 | __dev_close(dev); | |
8508 | else | |
40c900aa | 8509 | ret = __dev_open(dev, extack); |
7051b88a | 8510 | } |
1da177e4 | 8511 | |
1da177e4 | 8512 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff | 8513 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
991fb3f7 | 8514 | unsigned int old_flags = dev->flags; |
d1b19dff | 8515 | |
1da177e4 | 8516 | dev->gflags ^= IFF_PROMISC; |
991fb3f7 ND |
8517 | |
8518 | if (__dev_set_promiscuity(dev, inc, false) >= 0) | |
8519 | if (dev->flags != old_flags) | |
8520 | dev_set_rx_mode(dev); | |
1da177e4 LT |
8521 | } |
8522 | ||
8523 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
eb13da1a | 8524 | * is important. Some (broken) drivers set IFF_PROMISC, when |
8525 | * IFF_ALLMULTI is requested not asking us and not reporting. | |
1da177e4 LT |
8526 | */ |
8527 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
8528 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
8529 | ||
1da177e4 | 8530 | dev->gflags ^= IFF_ALLMULTI; |
991fb3f7 | 8531 | __dev_set_allmulti(dev, inc, false); |
1da177e4 LT |
8532 | } |
8533 | ||
bd380811 PM |
8534 | return ret; |
8535 | } | |
8536 | ||
a528c219 ND |
8537 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
8538 | unsigned int gchanges) | |
bd380811 PM |
8539 | { |
8540 | unsigned int changes = dev->flags ^ old_flags; | |
8541 | ||
a528c219 | 8542 | if (gchanges) |
7f294054 | 8543 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC); |
a528c219 | 8544 | |
bd380811 PM |
8545 | if (changes & IFF_UP) { |
8546 | if (dev->flags & IFF_UP) | |
8547 | call_netdevice_notifiers(NETDEV_UP, dev); | |
8548 | else | |
8549 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
8550 | } | |
8551 | ||
8552 | if (dev->flags & IFF_UP && | |
be9efd36 | 8553 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
51d0c047 DA |
8554 | struct netdev_notifier_change_info change_info = { |
8555 | .info = { | |
8556 | .dev = dev, | |
8557 | }, | |
8558 | .flags_changed = changes, | |
8559 | }; | |
be9efd36 | 8560 | |
51d0c047 | 8561 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
be9efd36 | 8562 | } |
bd380811 PM |
8563 | } |
8564 | ||
8565 | /** | |
8566 | * dev_change_flags - change device settings | |
8567 | * @dev: device | |
8568 | * @flags: device state flags | |
567c5e13 | 8569 | * @extack: netlink extended ack |
bd380811 PM |
8570 | * |
8571 | * Change settings on device based state flags. The flags are | |
8572 | * in the userspace exported format. | |
8573 | */ | |
567c5e13 PM |
8574 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
8575 | struct netlink_ext_ack *extack) | |
bd380811 | 8576 | { |
b536db93 | 8577 | int ret; |
991fb3f7 | 8578 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
bd380811 | 8579 | |
6d040321 | 8580 | ret = __dev_change_flags(dev, flags, extack); |
bd380811 PM |
8581 | if (ret < 0) |
8582 | return ret; | |
8583 | ||
991fb3f7 | 8584 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
a528c219 | 8585 | __dev_notify_flags(dev, old_flags, changes); |
1da177e4 LT |
8586 | return ret; |
8587 | } | |
d1b19dff | 8588 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 8589 | |
f51048c3 | 8590 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
2315dc91 VF |
8591 | { |
8592 | const struct net_device_ops *ops = dev->netdev_ops; | |
8593 | ||
8594 | if (ops->ndo_change_mtu) | |
8595 | return ops->ndo_change_mtu(dev, new_mtu); | |
8596 | ||
501a90c9 ED |
8597 | /* Pairs with all the lockless reads of dev->mtu in the stack */ |
8598 | WRITE_ONCE(dev->mtu, new_mtu); | |
2315dc91 VF |
8599 | return 0; |
8600 | } | |
f51048c3 | 8601 | EXPORT_SYMBOL(__dev_set_mtu); |
2315dc91 | 8602 | |
d836f5c6 ED |
8603 | int dev_validate_mtu(struct net_device *dev, int new_mtu, |
8604 | struct netlink_ext_ack *extack) | |
8605 | { | |
8606 | /* MTU must be positive, and in range */ | |
8607 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { | |
8608 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); | |
8609 | return -EINVAL; | |
8610 | } | |
8611 | ||
8612 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { | |
8613 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); | |
8614 | return -EINVAL; | |
8615 | } | |
8616 | return 0; | |
8617 | } | |
8618 | ||
f0db275a | 8619 | /** |
7a4c53be | 8620 | * dev_set_mtu_ext - Change maximum transfer unit |
f0db275a SH |
8621 | * @dev: device |
8622 | * @new_mtu: new transfer unit | |
7a4c53be | 8623 | * @extack: netlink extended ack |
f0db275a SH |
8624 | * |
8625 | * Change the maximum transfer size of the network device. | |
8626 | */ | |
7a4c53be SH |
8627 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
8628 | struct netlink_ext_ack *extack) | |
1da177e4 | 8629 | { |
2315dc91 | 8630 | int err, orig_mtu; |
1da177e4 LT |
8631 | |
8632 | if (new_mtu == dev->mtu) | |
8633 | return 0; | |
8634 | ||
d836f5c6 ED |
8635 | err = dev_validate_mtu(dev, new_mtu, extack); |
8636 | if (err) | |
8637 | return err; | |
1da177e4 LT |
8638 | |
8639 | if (!netif_device_present(dev)) | |
8640 | return -ENODEV; | |
8641 | ||
1d486bfb VF |
8642 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
8643 | err = notifier_to_errno(err); | |
8644 | if (err) | |
8645 | return err; | |
d314774c | 8646 | |
2315dc91 VF |
8647 | orig_mtu = dev->mtu; |
8648 | err = __dev_set_mtu(dev, new_mtu); | |
d314774c | 8649 | |
2315dc91 | 8650 | if (!err) { |
af7d6cce SD |
8651 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8652 | orig_mtu); | |
2315dc91 VF |
8653 | err = notifier_to_errno(err); |
8654 | if (err) { | |
8655 | /* setting mtu back and notifying everyone again, | |
8656 | * so that they have a chance to revert changes. | |
8657 | */ | |
8658 | __dev_set_mtu(dev, orig_mtu); | |
af7d6cce SD |
8659 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8660 | new_mtu); | |
2315dc91 VF |
8661 | } |
8662 | } | |
1da177e4 LT |
8663 | return err; |
8664 | } | |
7a4c53be SH |
8665 | |
8666 | int dev_set_mtu(struct net_device *dev, int new_mtu) | |
8667 | { | |
8668 | struct netlink_ext_ack extack; | |
8669 | int err; | |
8670 | ||
a6bcfc89 | 8671 | memset(&extack, 0, sizeof(extack)); |
7a4c53be | 8672 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
a6bcfc89 | 8673 | if (err && extack._msg) |
7a4c53be SH |
8674 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
8675 | return err; | |
8676 | } | |
d1b19dff | 8677 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 8678 | |
6a643ddb CW |
8679 | /** |
8680 | * dev_change_tx_queue_len - Change TX queue length of a netdevice | |
8681 | * @dev: device | |
8682 | * @new_len: new tx queue length | |
8683 | */ | |
8684 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) | |
8685 | { | |
8686 | unsigned int orig_len = dev->tx_queue_len; | |
8687 | int res; | |
8688 | ||
8689 | if (new_len != (unsigned int)new_len) | |
8690 | return -ERANGE; | |
8691 | ||
8692 | if (new_len != orig_len) { | |
8693 | dev->tx_queue_len = new_len; | |
8694 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
8695 | res = notifier_to_errno(res); | |
7effaf06 TT |
8696 | if (res) |
8697 | goto err_rollback; | |
8698 | res = dev_qdisc_change_tx_queue_len(dev); | |
8699 | if (res) | |
8700 | goto err_rollback; | |
6a643ddb CW |
8701 | } |
8702 | ||
8703 | return 0; | |
7effaf06 TT |
8704 | |
8705 | err_rollback: | |
8706 | netdev_err(dev, "refused to change device tx_queue_len\n"); | |
8707 | dev->tx_queue_len = orig_len; | |
8708 | return res; | |
6a643ddb CW |
8709 | } |
8710 | ||
cbda10fa VD |
8711 | /** |
8712 | * dev_set_group - Change group this device belongs to | |
8713 | * @dev: device | |
8714 | * @new_group: group this device should belong to | |
8715 | */ | |
8716 | void dev_set_group(struct net_device *dev, int new_group) | |
8717 | { | |
8718 | dev->group = new_group; | |
8719 | } | |
8720 | EXPORT_SYMBOL(dev_set_group); | |
8721 | ||
d59cdf94 PM |
8722 | /** |
8723 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. | |
8724 | * @dev: device | |
8725 | * @addr: new address | |
8726 | * @extack: netlink extended ack | |
8727 | */ | |
8728 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, | |
8729 | struct netlink_ext_ack *extack) | |
8730 | { | |
8731 | struct netdev_notifier_pre_changeaddr_info info = { | |
8732 | .info.dev = dev, | |
8733 | .info.extack = extack, | |
8734 | .dev_addr = addr, | |
8735 | }; | |
8736 | int rc; | |
8737 | ||
8738 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); | |
8739 | return notifier_to_errno(rc); | |
8740 | } | |
8741 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); | |
8742 | ||
f0db275a SH |
8743 | /** |
8744 | * dev_set_mac_address - Change Media Access Control Address | |
8745 | * @dev: device | |
8746 | * @sa: new address | |
3a37a963 | 8747 | * @extack: netlink extended ack |
f0db275a SH |
8748 | * |
8749 | * Change the hardware (MAC) address of the device | |
8750 | */ | |
3a37a963 PM |
8751 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
8752 | struct netlink_ext_ack *extack) | |
1da177e4 | 8753 | { |
d314774c | 8754 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
8755 | int err; |
8756 | ||
d314774c | 8757 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
8758 | return -EOPNOTSUPP; |
8759 | if (sa->sa_family != dev->type) | |
8760 | return -EINVAL; | |
8761 | if (!netif_device_present(dev)) | |
8762 | return -ENODEV; | |
d59cdf94 PM |
8763 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
8764 | if (err) | |
8765 | return err; | |
d314774c | 8766 | err = ops->ndo_set_mac_address(dev, sa); |
f6521516 JP |
8767 | if (err) |
8768 | return err; | |
fbdeca2d | 8769 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 8770 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 8771 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 8772 | return 0; |
1da177e4 | 8773 | } |
d1b19dff | 8774 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 8775 | |
4bf84c35 JP |
8776 | /** |
8777 | * dev_change_carrier - Change device carrier | |
8778 | * @dev: device | |
691b3b7e | 8779 | * @new_carrier: new value |
4bf84c35 JP |
8780 | * |
8781 | * Change device carrier | |
8782 | */ | |
8783 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
8784 | { | |
8785 | const struct net_device_ops *ops = dev->netdev_ops; | |
8786 | ||
8787 | if (!ops->ndo_change_carrier) | |
8788 | return -EOPNOTSUPP; | |
8789 | if (!netif_device_present(dev)) | |
8790 | return -ENODEV; | |
8791 | return ops->ndo_change_carrier(dev, new_carrier); | |
8792 | } | |
8793 | EXPORT_SYMBOL(dev_change_carrier); | |
8794 | ||
66b52b0d JP |
8795 | /** |
8796 | * dev_get_phys_port_id - Get device physical port ID | |
8797 | * @dev: device | |
8798 | * @ppid: port ID | |
8799 | * | |
8800 | * Get device physical port ID | |
8801 | */ | |
8802 | int dev_get_phys_port_id(struct net_device *dev, | |
02637fce | 8803 | struct netdev_phys_item_id *ppid) |
66b52b0d JP |
8804 | { |
8805 | const struct net_device_ops *ops = dev->netdev_ops; | |
8806 | ||
8807 | if (!ops->ndo_get_phys_port_id) | |
8808 | return -EOPNOTSUPP; | |
8809 | return ops->ndo_get_phys_port_id(dev, ppid); | |
8810 | } | |
8811 | EXPORT_SYMBOL(dev_get_phys_port_id); | |
8812 | ||
db24a904 DA |
8813 | /** |
8814 | * dev_get_phys_port_name - Get device physical port name | |
8815 | * @dev: device | |
8816 | * @name: port name | |
ed49e650 | 8817 | * @len: limit of bytes to copy to name |
db24a904 DA |
8818 | * |
8819 | * Get device physical port name | |
8820 | */ | |
8821 | int dev_get_phys_port_name(struct net_device *dev, | |
8822 | char *name, size_t len) | |
8823 | { | |
8824 | const struct net_device_ops *ops = dev->netdev_ops; | |
af3836df | 8825 | int err; |
db24a904 | 8826 | |
af3836df JP |
8827 | if (ops->ndo_get_phys_port_name) { |
8828 | err = ops->ndo_get_phys_port_name(dev, name, len); | |
8829 | if (err != -EOPNOTSUPP) | |
8830 | return err; | |
8831 | } | |
8832 | return devlink_compat_phys_port_name_get(dev, name, len); | |
db24a904 DA |
8833 | } |
8834 | EXPORT_SYMBOL(dev_get_phys_port_name); | |
8835 | ||
d6abc596 FF |
8836 | /** |
8837 | * dev_get_port_parent_id - Get the device's port parent identifier | |
8838 | * @dev: network device | |
8839 | * @ppid: pointer to a storage for the port's parent identifier | |
8840 | * @recurse: allow/disallow recursion to lower devices | |
8841 | * | |
8842 | * Get the devices's port parent identifier | |
8843 | */ | |
8844 | int dev_get_port_parent_id(struct net_device *dev, | |
8845 | struct netdev_phys_item_id *ppid, | |
8846 | bool recurse) | |
8847 | { | |
8848 | const struct net_device_ops *ops = dev->netdev_ops; | |
8849 | struct netdev_phys_item_id first = { }; | |
8850 | struct net_device *lower_dev; | |
8851 | struct list_head *iter; | |
7e1146e8 JP |
8852 | int err; |
8853 | ||
8854 | if (ops->ndo_get_port_parent_id) { | |
8855 | err = ops->ndo_get_port_parent_id(dev, ppid); | |
8856 | if (err != -EOPNOTSUPP) | |
8857 | return err; | |
8858 | } | |
d6abc596 | 8859 | |
7e1146e8 JP |
8860 | err = devlink_compat_switch_id_get(dev, ppid); |
8861 | if (!err || err != -EOPNOTSUPP) | |
8862 | return err; | |
d6abc596 FF |
8863 | |
8864 | if (!recurse) | |
7e1146e8 | 8865 | return -EOPNOTSUPP; |
d6abc596 FF |
8866 | |
8867 | netdev_for_each_lower_dev(dev, lower_dev, iter) { | |
8868 | err = dev_get_port_parent_id(lower_dev, ppid, recurse); | |
8869 | if (err) | |
8870 | break; | |
8871 | if (!first.id_len) | |
8872 | first = *ppid; | |
8873 | else if (memcmp(&first, ppid, sizeof(*ppid))) | |
e1b9efe6 | 8874 | return -EOPNOTSUPP; |
d6abc596 FF |
8875 | } |
8876 | ||
8877 | return err; | |
8878 | } | |
8879 | EXPORT_SYMBOL(dev_get_port_parent_id); | |
8880 | ||
8881 | /** | |
8882 | * netdev_port_same_parent_id - Indicate if two network devices have | |
8883 | * the same port parent identifier | |
8884 | * @a: first network device | |
8885 | * @b: second network device | |
8886 | */ | |
8887 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) | |
8888 | { | |
8889 | struct netdev_phys_item_id a_id = { }; | |
8890 | struct netdev_phys_item_id b_id = { }; | |
8891 | ||
8892 | if (dev_get_port_parent_id(a, &a_id, true) || | |
8893 | dev_get_port_parent_id(b, &b_id, true)) | |
8894 | return false; | |
8895 | ||
8896 | return netdev_phys_item_id_same(&a_id, &b_id); | |
8897 | } | |
8898 | EXPORT_SYMBOL(netdev_port_same_parent_id); | |
8899 | ||
d746d707 AK |
8900 | /** |
8901 | * dev_change_proto_down - update protocol port state information | |
8902 | * @dev: device | |
8903 | * @proto_down: new value | |
8904 | * | |
8905 | * This info can be used by switch drivers to set the phys state of the | |
8906 | * port. | |
8907 | */ | |
8908 | int dev_change_proto_down(struct net_device *dev, bool proto_down) | |
8909 | { | |
8910 | const struct net_device_ops *ops = dev->netdev_ops; | |
8911 | ||
8912 | if (!ops->ndo_change_proto_down) | |
8913 | return -EOPNOTSUPP; | |
8914 | if (!netif_device_present(dev)) | |
8915 | return -ENODEV; | |
8916 | return ops->ndo_change_proto_down(dev, proto_down); | |
8917 | } | |
8918 | EXPORT_SYMBOL(dev_change_proto_down); | |
8919 | ||
b5899679 AR |
8920 | /** |
8921 | * dev_change_proto_down_generic - generic implementation for | |
8922 | * ndo_change_proto_down that sets carrier according to | |
8923 | * proto_down. | |
8924 | * | |
8925 | * @dev: device | |
8926 | * @proto_down: new value | |
8927 | */ | |
8928 | int dev_change_proto_down_generic(struct net_device *dev, bool proto_down) | |
8929 | { | |
8930 | if (proto_down) | |
8931 | netif_carrier_off(dev); | |
8932 | else | |
8933 | netif_carrier_on(dev); | |
8934 | dev->proto_down = proto_down; | |
8935 | return 0; | |
8936 | } | |
8937 | EXPORT_SYMBOL(dev_change_proto_down_generic); | |
8938 | ||
829eb208 RP |
8939 | /** |
8940 | * dev_change_proto_down_reason - proto down reason | |
8941 | * | |
8942 | * @dev: device | |
8943 | * @mask: proto down mask | |
8944 | * @value: proto down value | |
8945 | */ | |
8946 | void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask, | |
8947 | u32 value) | |
8948 | { | |
8949 | int b; | |
8950 | ||
8951 | if (!mask) { | |
8952 | dev->proto_down_reason = value; | |
8953 | } else { | |
8954 | for_each_set_bit(b, &mask, 32) { | |
8955 | if (value & (1 << b)) | |
8956 | dev->proto_down_reason |= BIT(b); | |
8957 | else | |
8958 | dev->proto_down_reason &= ~BIT(b); | |
8959 | } | |
8960 | } | |
8961 | } | |
8962 | EXPORT_SYMBOL(dev_change_proto_down_reason); | |
8963 | ||
aa8d3a71 AN |
8964 | struct bpf_xdp_link { |
8965 | struct bpf_link link; | |
8966 | struct net_device *dev; /* protected by rtnl_lock, no refcnt held */ | |
8967 | int flags; | |
8968 | }; | |
8969 | ||
c8a36f19 | 8970 | static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags) |
d67b9cd2 | 8971 | { |
7f0a8382 AN |
8972 | if (flags & XDP_FLAGS_HW_MODE) |
8973 | return XDP_MODE_HW; | |
8974 | if (flags & XDP_FLAGS_DRV_MODE) | |
8975 | return XDP_MODE_DRV; | |
c8a36f19 AN |
8976 | if (flags & XDP_FLAGS_SKB_MODE) |
8977 | return XDP_MODE_SKB; | |
8978 | return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB; | |
7f0a8382 | 8979 | } |
d67b9cd2 | 8980 | |
7f0a8382 AN |
8981 | static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode) |
8982 | { | |
8983 | switch (mode) { | |
8984 | case XDP_MODE_SKB: | |
8985 | return generic_xdp_install; | |
8986 | case XDP_MODE_DRV: | |
8987 | case XDP_MODE_HW: | |
8988 | return dev->netdev_ops->ndo_bpf; | |
8989 | default: | |
8990 | return NULL; | |
5d867245 | 8991 | } |
7f0a8382 | 8992 | } |
118b4aa2 | 8993 | |
aa8d3a71 AN |
8994 | static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev, |
8995 | enum bpf_xdp_mode mode) | |
8996 | { | |
8997 | return dev->xdp_state[mode].link; | |
8998 | } | |
8999 | ||
7f0a8382 AN |
9000 | static struct bpf_prog *dev_xdp_prog(struct net_device *dev, |
9001 | enum bpf_xdp_mode mode) | |
9002 | { | |
aa8d3a71 AN |
9003 | struct bpf_xdp_link *link = dev_xdp_link(dev, mode); |
9004 | ||
9005 | if (link) | |
9006 | return link->link.prog; | |
7f0a8382 AN |
9007 | return dev->xdp_state[mode].prog; |
9008 | } | |
9009 | ||
998f1729 THJ |
9010 | static u8 dev_xdp_prog_count(struct net_device *dev) |
9011 | { | |
9012 | u8 count = 0; | |
9013 | int i; | |
9014 | ||
9015 | for (i = 0; i < __MAX_XDP_MODE; i++) | |
9016 | if (dev->xdp_state[i].prog || dev->xdp_state[i].link) | |
9017 | count++; | |
9018 | return count; | |
9019 | } | |
9020 | ||
7f0a8382 AN |
9021 | u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode) |
9022 | { | |
9023 | struct bpf_prog *prog = dev_xdp_prog(dev, mode); | |
118b4aa2 | 9024 | |
7f0a8382 AN |
9025 | return prog ? prog->aux->id : 0; |
9026 | } | |
58038695 | 9027 | |
aa8d3a71 AN |
9028 | static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode, |
9029 | struct bpf_xdp_link *link) | |
9030 | { | |
9031 | dev->xdp_state[mode].link = link; | |
9032 | dev->xdp_state[mode].prog = NULL; | |
d67b9cd2 DB |
9033 | } |
9034 | ||
7f0a8382 AN |
9035 | static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode, |
9036 | struct bpf_prog *prog) | |
9037 | { | |
aa8d3a71 | 9038 | dev->xdp_state[mode].link = NULL; |
7f0a8382 | 9039 | dev->xdp_state[mode].prog = prog; |
d67b9cd2 DB |
9040 | } |
9041 | ||
7f0a8382 AN |
9042 | static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, |
9043 | bpf_op_t bpf_op, struct netlink_ext_ack *extack, | |
9044 | u32 flags, struct bpf_prog *prog) | |
d67b9cd2 | 9045 | { |
f4e63525 | 9046 | struct netdev_bpf xdp; |
7e6897f9 BT |
9047 | int err; |
9048 | ||
d67b9cd2 | 9049 | memset(&xdp, 0, sizeof(xdp)); |
7f0a8382 | 9050 | xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG; |
d67b9cd2 | 9051 | xdp.extack = extack; |
32d60277 | 9052 | xdp.flags = flags; |
d67b9cd2 DB |
9053 | xdp.prog = prog; |
9054 | ||
7f0a8382 AN |
9055 | /* Drivers assume refcnt is already incremented (i.e, prog pointer is |
9056 | * "moved" into driver), so they don't increment it on their own, but | |
9057 | * they do decrement refcnt when program is detached or replaced. | |
9058 | * Given net_device also owns link/prog, we need to bump refcnt here | |
9059 | * to prevent drivers from underflowing it. | |
9060 | */ | |
9061 | if (prog) | |
9062 | bpf_prog_inc(prog); | |
7e6897f9 | 9063 | err = bpf_op(dev, &xdp); |
7f0a8382 AN |
9064 | if (err) { |
9065 | if (prog) | |
9066 | bpf_prog_put(prog); | |
9067 | return err; | |
9068 | } | |
7e6897f9 | 9069 | |
7f0a8382 AN |
9070 | if (mode != XDP_MODE_HW) |
9071 | bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog); | |
7e6897f9 | 9072 | |
7f0a8382 | 9073 | return 0; |
d67b9cd2 DB |
9074 | } |
9075 | ||
bd0b2e7f JK |
9076 | static void dev_xdp_uninstall(struct net_device *dev) |
9077 | { | |
aa8d3a71 | 9078 | struct bpf_xdp_link *link; |
7f0a8382 AN |
9079 | struct bpf_prog *prog; |
9080 | enum bpf_xdp_mode mode; | |
9081 | bpf_op_t bpf_op; | |
bd0b2e7f | 9082 | |
7f0a8382 | 9083 | ASSERT_RTNL(); |
bd0b2e7f | 9084 | |
7f0a8382 AN |
9085 | for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) { |
9086 | prog = dev_xdp_prog(dev, mode); | |
9087 | if (!prog) | |
9088 | continue; | |
bd0b2e7f | 9089 | |
7f0a8382 AN |
9090 | bpf_op = dev_xdp_bpf_op(dev, mode); |
9091 | if (!bpf_op) | |
9092 | continue; | |
bd0b2e7f | 9093 | |
7f0a8382 AN |
9094 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); |
9095 | ||
aa8d3a71 AN |
9096 | /* auto-detach link from net device */ |
9097 | link = dev_xdp_link(dev, mode); | |
9098 | if (link) | |
9099 | link->dev = NULL; | |
9100 | else | |
9101 | bpf_prog_put(prog); | |
9102 | ||
9103 | dev_xdp_set_link(dev, mode, NULL); | |
7f0a8382 | 9104 | } |
bd0b2e7f JK |
9105 | } |
9106 | ||
d4baa936 | 9107 | static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack, |
aa8d3a71 AN |
9108 | struct bpf_xdp_link *link, struct bpf_prog *new_prog, |
9109 | struct bpf_prog *old_prog, u32 flags) | |
a7862b45 | 9110 | { |
998f1729 | 9111 | unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES); |
d4baa936 AN |
9112 | struct bpf_prog *cur_prog; |
9113 | enum bpf_xdp_mode mode; | |
7f0a8382 | 9114 | bpf_op_t bpf_op; |
a7862b45 BB |
9115 | int err; |
9116 | ||
85de8576 DB |
9117 | ASSERT_RTNL(); |
9118 | ||
aa8d3a71 AN |
9119 | /* either link or prog attachment, never both */ |
9120 | if (link && (new_prog || old_prog)) | |
9121 | return -EINVAL; | |
9122 | /* link supports only XDP mode flags */ | |
9123 | if (link && (flags & ~XDP_FLAGS_MODES)) { | |
9124 | NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment"); | |
9125 | return -EINVAL; | |
9126 | } | |
998f1729 THJ |
9127 | /* just one XDP mode bit should be set, zero defaults to drv/skb mode */ |
9128 | if (num_modes > 1) { | |
d4baa936 AN |
9129 | NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set"); |
9130 | return -EINVAL; | |
9131 | } | |
998f1729 THJ |
9132 | /* avoid ambiguity if offload + drv/skb mode progs are both loaded */ |
9133 | if (!num_modes && dev_xdp_prog_count(dev) > 1) { | |
9134 | NL_SET_ERR_MSG(extack, | |
9135 | "More than one program loaded, unset mode is ambiguous"); | |
9136 | return -EINVAL; | |
9137 | } | |
d4baa936 AN |
9138 | /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */ |
9139 | if (old_prog && !(flags & XDP_FLAGS_REPLACE)) { | |
9140 | NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified"); | |
9141 | return -EINVAL; | |
01dde20c | 9142 | } |
a25717d2 | 9143 | |
c8a36f19 | 9144 | mode = dev_xdp_mode(dev, flags); |
aa8d3a71 AN |
9145 | /* can't replace attached link */ |
9146 | if (dev_xdp_link(dev, mode)) { | |
9147 | NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link"); | |
9148 | return -EBUSY; | |
01dde20c | 9149 | } |
c14a9f63 | 9150 | |
d4baa936 | 9151 | cur_prog = dev_xdp_prog(dev, mode); |
aa8d3a71 AN |
9152 | /* can't replace attached prog with link */ |
9153 | if (link && cur_prog) { | |
9154 | NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link"); | |
9155 | return -EBUSY; | |
9156 | } | |
d4baa936 AN |
9157 | if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) { |
9158 | NL_SET_ERR_MSG(extack, "Active program does not match expected"); | |
9159 | return -EEXIST; | |
92234c8f | 9160 | } |
c14a9f63 | 9161 | |
aa8d3a71 AN |
9162 | /* put effective new program into new_prog */ |
9163 | if (link) | |
9164 | new_prog = link->link.prog; | |
85de8576 | 9165 | |
d4baa936 AN |
9166 | if (new_prog) { |
9167 | bool offload = mode == XDP_MODE_HW; | |
7f0a8382 AN |
9168 | enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB |
9169 | ? XDP_MODE_DRV : XDP_MODE_SKB; | |
441a3303 | 9170 | |
068d9d1e AN |
9171 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) { |
9172 | NL_SET_ERR_MSG(extack, "XDP program already attached"); | |
9173 | return -EBUSY; | |
9174 | } | |
d4baa936 | 9175 | if (!offload && dev_xdp_prog(dev, other_mode)) { |
7f0a8382 | 9176 | NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time"); |
d67b9cd2 | 9177 | return -EEXIST; |
01dde20c | 9178 | } |
d4baa936 | 9179 | if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) { |
7f0a8382 | 9180 | NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported"); |
441a3303 JK |
9181 | return -EINVAL; |
9182 | } | |
d4baa936 | 9183 | if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) { |
fbee97fe | 9184 | NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device"); |
fbee97fe DA |
9185 | return -EINVAL; |
9186 | } | |
d4baa936 AN |
9187 | if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) { |
9188 | NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device"); | |
92164774 LB |
9189 | return -EINVAL; |
9190 | } | |
d4baa936 | 9191 | } |
92164774 | 9192 | |
d4baa936 AN |
9193 | /* don't call drivers if the effective program didn't change */ |
9194 | if (new_prog != cur_prog) { | |
9195 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9196 | if (!bpf_op) { | |
9197 | NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode"); | |
9198 | return -EOPNOTSUPP; | |
c14a9f63 | 9199 | } |
a7862b45 | 9200 | |
d4baa936 AN |
9201 | err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog); |
9202 | if (err) | |
9203 | return err; | |
7f0a8382 | 9204 | } |
d4baa936 | 9205 | |
aa8d3a71 AN |
9206 | if (link) |
9207 | dev_xdp_set_link(dev, mode, link); | |
9208 | else | |
9209 | dev_xdp_set_prog(dev, mode, new_prog); | |
d4baa936 AN |
9210 | if (cur_prog) |
9211 | bpf_prog_put(cur_prog); | |
a7862b45 | 9212 | |
7f0a8382 | 9213 | return 0; |
a7862b45 | 9214 | } |
a7862b45 | 9215 | |
aa8d3a71 AN |
9216 | static int dev_xdp_attach_link(struct net_device *dev, |
9217 | struct netlink_ext_ack *extack, | |
9218 | struct bpf_xdp_link *link) | |
9219 | { | |
9220 | return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags); | |
9221 | } | |
9222 | ||
9223 | static int dev_xdp_detach_link(struct net_device *dev, | |
9224 | struct netlink_ext_ack *extack, | |
9225 | struct bpf_xdp_link *link) | |
9226 | { | |
9227 | enum bpf_xdp_mode mode; | |
9228 | bpf_op_t bpf_op; | |
9229 | ||
9230 | ASSERT_RTNL(); | |
9231 | ||
c8a36f19 | 9232 | mode = dev_xdp_mode(dev, link->flags); |
aa8d3a71 AN |
9233 | if (dev_xdp_link(dev, mode) != link) |
9234 | return -EINVAL; | |
9235 | ||
9236 | bpf_op = dev_xdp_bpf_op(dev, mode); | |
9237 | WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); | |
9238 | dev_xdp_set_link(dev, mode, NULL); | |
9239 | return 0; | |
9240 | } | |
9241 | ||
9242 | static void bpf_xdp_link_release(struct bpf_link *link) | |
9243 | { | |
9244 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9245 | ||
9246 | rtnl_lock(); | |
9247 | ||
9248 | /* if racing with net_device's tear down, xdp_link->dev might be | |
9249 | * already NULL, in which case link was already auto-detached | |
9250 | */ | |
73b11c2a | 9251 | if (xdp_link->dev) { |
aa8d3a71 | 9252 | WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link)); |
73b11c2a AN |
9253 | xdp_link->dev = NULL; |
9254 | } | |
aa8d3a71 AN |
9255 | |
9256 | rtnl_unlock(); | |
9257 | } | |
9258 | ||
73b11c2a AN |
9259 | static int bpf_xdp_link_detach(struct bpf_link *link) |
9260 | { | |
9261 | bpf_xdp_link_release(link); | |
9262 | return 0; | |
9263 | } | |
9264 | ||
aa8d3a71 AN |
9265 | static void bpf_xdp_link_dealloc(struct bpf_link *link) |
9266 | { | |
9267 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9268 | ||
9269 | kfree(xdp_link); | |
9270 | } | |
9271 | ||
c1931c97 AN |
9272 | static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link, |
9273 | struct seq_file *seq) | |
9274 | { | |
9275 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9276 | u32 ifindex = 0; | |
9277 | ||
9278 | rtnl_lock(); | |
9279 | if (xdp_link->dev) | |
9280 | ifindex = xdp_link->dev->ifindex; | |
9281 | rtnl_unlock(); | |
9282 | ||
9283 | seq_printf(seq, "ifindex:\t%u\n", ifindex); | |
9284 | } | |
9285 | ||
9286 | static int bpf_xdp_link_fill_link_info(const struct bpf_link *link, | |
9287 | struct bpf_link_info *info) | |
9288 | { | |
9289 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9290 | u32 ifindex = 0; | |
9291 | ||
9292 | rtnl_lock(); | |
9293 | if (xdp_link->dev) | |
9294 | ifindex = xdp_link->dev->ifindex; | |
9295 | rtnl_unlock(); | |
9296 | ||
9297 | info->xdp.ifindex = ifindex; | |
9298 | return 0; | |
9299 | } | |
9300 | ||
026a4c28 AN |
9301 | static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, |
9302 | struct bpf_prog *old_prog) | |
9303 | { | |
9304 | struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | |
9305 | enum bpf_xdp_mode mode; | |
9306 | bpf_op_t bpf_op; | |
9307 | int err = 0; | |
9308 | ||
9309 | rtnl_lock(); | |
9310 | ||
9311 | /* link might have been auto-released already, so fail */ | |
9312 | if (!xdp_link->dev) { | |
9313 | err = -ENOLINK; | |
9314 | goto out_unlock; | |
9315 | } | |
9316 | ||
9317 | if (old_prog && link->prog != old_prog) { | |
9318 | err = -EPERM; | |
9319 | goto out_unlock; | |
9320 | } | |
9321 | old_prog = link->prog; | |
9322 | if (old_prog == new_prog) { | |
9323 | /* no-op, don't disturb drivers */ | |
9324 | bpf_prog_put(new_prog); | |
9325 | goto out_unlock; | |
9326 | } | |
9327 | ||
c8a36f19 | 9328 | mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags); |
026a4c28 AN |
9329 | bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode); |
9330 | err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL, | |
9331 | xdp_link->flags, new_prog); | |
9332 | if (err) | |
9333 | goto out_unlock; | |
9334 | ||
9335 | old_prog = xchg(&link->prog, new_prog); | |
9336 | bpf_prog_put(old_prog); | |
9337 | ||
9338 | out_unlock: | |
9339 | rtnl_unlock(); | |
9340 | return err; | |
9341 | } | |
9342 | ||
aa8d3a71 AN |
9343 | static const struct bpf_link_ops bpf_xdp_link_lops = { |
9344 | .release = bpf_xdp_link_release, | |
9345 | .dealloc = bpf_xdp_link_dealloc, | |
73b11c2a | 9346 | .detach = bpf_xdp_link_detach, |
c1931c97 AN |
9347 | .show_fdinfo = bpf_xdp_link_show_fdinfo, |
9348 | .fill_link_info = bpf_xdp_link_fill_link_info, | |
026a4c28 | 9349 | .update_prog = bpf_xdp_link_update, |
aa8d3a71 AN |
9350 | }; |
9351 | ||
9352 | int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
9353 | { | |
9354 | struct net *net = current->nsproxy->net_ns; | |
9355 | struct bpf_link_primer link_primer; | |
9356 | struct bpf_xdp_link *link; | |
9357 | struct net_device *dev; | |
9358 | int err, fd; | |
9359 | ||
9360 | dev = dev_get_by_index(net, attr->link_create.target_ifindex); | |
9361 | if (!dev) | |
9362 | return -EINVAL; | |
9363 | ||
9364 | link = kzalloc(sizeof(*link), GFP_USER); | |
9365 | if (!link) { | |
9366 | err = -ENOMEM; | |
9367 | goto out_put_dev; | |
9368 | } | |
9369 | ||
9370 | bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog); | |
9371 | link->dev = dev; | |
9372 | link->flags = attr->link_create.flags; | |
9373 | ||
9374 | err = bpf_link_prime(&link->link, &link_primer); | |
9375 | if (err) { | |
9376 | kfree(link); | |
9377 | goto out_put_dev; | |
9378 | } | |
9379 | ||
9380 | rtnl_lock(); | |
9381 | err = dev_xdp_attach_link(dev, NULL, link); | |
9382 | rtnl_unlock(); | |
9383 | ||
9384 | if (err) { | |
9385 | bpf_link_cleanup(&link_primer); | |
9386 | goto out_put_dev; | |
9387 | } | |
9388 | ||
9389 | fd = bpf_link_settle(&link_primer); | |
9390 | /* link itself doesn't hold dev's refcnt to not complicate shutdown */ | |
9391 | dev_put(dev); | |
9392 | return fd; | |
9393 | ||
9394 | out_put_dev: | |
9395 | dev_put(dev); | |
9396 | return err; | |
9397 | } | |
9398 | ||
d4baa936 AN |
9399 | /** |
9400 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path | |
9401 | * @dev: device | |
9402 | * @extack: netlink extended ack | |
9403 | * @fd: new program fd or negative value to clear | |
9404 | * @expected_fd: old program fd that userspace expects to replace or clear | |
9405 | * @flags: xdp-related flags | |
9406 | * | |
9407 | * Set or clear a bpf program for a device | |
9408 | */ | |
9409 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, | |
9410 | int fd, int expected_fd, u32 flags) | |
9411 | { | |
c8a36f19 | 9412 | enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags); |
d4baa936 AN |
9413 | struct bpf_prog *new_prog = NULL, *old_prog = NULL; |
9414 | int err; | |
9415 | ||
9416 | ASSERT_RTNL(); | |
9417 | ||
9418 | if (fd >= 0) { | |
9419 | new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, | |
9420 | mode != XDP_MODE_SKB); | |
9421 | if (IS_ERR(new_prog)) | |
9422 | return PTR_ERR(new_prog); | |
9423 | } | |
9424 | ||
9425 | if (expected_fd >= 0) { | |
9426 | old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, | |
9427 | mode != XDP_MODE_SKB); | |
9428 | if (IS_ERR(old_prog)) { | |
9429 | err = PTR_ERR(old_prog); | |
9430 | old_prog = NULL; | |
9431 | goto err_out; | |
c14a9f63 | 9432 | } |
a7862b45 BB |
9433 | } |
9434 | ||
aa8d3a71 | 9435 | err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags); |
a7862b45 | 9436 | |
d4baa936 AN |
9437 | err_out: |
9438 | if (err && new_prog) | |
9439 | bpf_prog_put(new_prog); | |
9440 | if (old_prog) | |
9441 | bpf_prog_put(old_prog); | |
a7862b45 BB |
9442 | return err; |
9443 | } | |
a7862b45 | 9444 | |
1da177e4 LT |
9445 | /** |
9446 | * dev_new_index - allocate an ifindex | |
c4ea43c5 | 9447 | * @net: the applicable net namespace |
1da177e4 LT |
9448 | * |
9449 | * Returns a suitable unique value for a new device interface | |
9450 | * number. The caller must hold the rtnl semaphore or the | |
9451 | * dev_base_lock to be sure it remains unique. | |
9452 | */ | |
881d966b | 9453 | static int dev_new_index(struct net *net) |
1da177e4 | 9454 | { |
aa79e66e | 9455 | int ifindex = net->ifindex; |
f4563a75 | 9456 | |
1da177e4 LT |
9457 | for (;;) { |
9458 | if (++ifindex <= 0) | |
9459 | ifindex = 1; | |
881d966b | 9460 | if (!__dev_get_by_index(net, ifindex)) |
aa79e66e | 9461 | return net->ifindex = ifindex; |
1da177e4 LT |
9462 | } |
9463 | } | |
9464 | ||
1da177e4 | 9465 | /* Delayed registration/unregisteration */ |
3b5b34fd | 9466 | static LIST_HEAD(net_todo_list); |
200b916f | 9467 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
1da177e4 | 9468 | |
6f05f629 | 9469 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 9470 | { |
1da177e4 | 9471 | list_add_tail(&dev->todo_list, &net_todo_list); |
50624c93 | 9472 | dev_net(dev)->dev_unreg_count++; |
1da177e4 LT |
9473 | } |
9474 | ||
fd867d51 JW |
9475 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
9476 | struct net_device *upper, netdev_features_t features) | |
9477 | { | |
9478 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9479 | netdev_features_t feature; | |
5ba3f7d6 | 9480 | int feature_bit; |
fd867d51 | 9481 | |
3b89ea9c | 9482 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9483 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9484 | if (!(upper->wanted_features & feature) |
9485 | && (features & feature)) { | |
9486 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", | |
9487 | &feature, upper->name); | |
9488 | features &= ~feature; | |
9489 | } | |
9490 | } | |
9491 | ||
9492 | return features; | |
9493 | } | |
9494 | ||
9495 | static void netdev_sync_lower_features(struct net_device *upper, | |
9496 | struct net_device *lower, netdev_features_t features) | |
9497 | { | |
9498 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
9499 | netdev_features_t feature; | |
5ba3f7d6 | 9500 | int feature_bit; |
fd867d51 | 9501 | |
3b89ea9c | 9502 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 9503 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
9504 | if (!(features & feature) && (lower->features & feature)) { |
9505 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", | |
9506 | &feature, lower->name); | |
9507 | lower->wanted_features &= ~feature; | |
dd912306 | 9508 | __netdev_update_features(lower); |
fd867d51 JW |
9509 | |
9510 | if (unlikely(lower->features & feature)) | |
9511 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", | |
9512 | &feature, lower->name); | |
dd912306 CW |
9513 | else |
9514 | netdev_features_change(lower); | |
fd867d51 JW |
9515 | } |
9516 | } | |
9517 | } | |
9518 | ||
c8f44aff MM |
9519 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
9520 | netdev_features_t features) | |
b63365a2 | 9521 | { |
57422dc5 MM |
9522 | /* Fix illegal checksum combinations */ |
9523 | if ((features & NETIF_F_HW_CSUM) && | |
9524 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 9525 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
9526 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
9527 | } | |
9528 | ||
b63365a2 | 9529 | /* TSO requires that SG is present as well. */ |
ea2d3688 | 9530 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 9531 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 9532 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
9533 | } |
9534 | ||
ec5f0615 PS |
9535 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
9536 | !(features & NETIF_F_IP_CSUM)) { | |
9537 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); | |
9538 | features &= ~NETIF_F_TSO; | |
9539 | features &= ~NETIF_F_TSO_ECN; | |
9540 | } | |
9541 | ||
9542 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && | |
9543 | !(features & NETIF_F_IPV6_CSUM)) { | |
9544 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); | |
9545 | features &= ~NETIF_F_TSO6; | |
9546 | } | |
9547 | ||
b1dc497b AD |
9548 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
9549 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) | |
9550 | features &= ~NETIF_F_TSO_MANGLEID; | |
9551 | ||
31d8b9e0 BH |
9552 | /* TSO ECN requires that TSO is present as well. */ |
9553 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
9554 | features &= ~NETIF_F_TSO_ECN; | |
9555 | ||
212b573f MM |
9556 | /* Software GSO depends on SG. */ |
9557 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 9558 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
9559 | features &= ~NETIF_F_GSO; |
9560 | } | |
9561 | ||
802ab55a AD |
9562 | /* GSO partial features require GSO partial be set */ |
9563 | if ((features & dev->gso_partial_features) && | |
9564 | !(features & NETIF_F_GSO_PARTIAL)) { | |
9565 | netdev_dbg(dev, | |
9566 | "Dropping partially supported GSO features since no GSO partial.\n"); | |
9567 | features &= ~dev->gso_partial_features; | |
9568 | } | |
9569 | ||
fb1f5f79 MC |
9570 | if (!(features & NETIF_F_RXCSUM)) { |
9571 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet | |
9572 | * successfully merged by hardware must also have the | |
9573 | * checksum verified by hardware. If the user does not | |
9574 | * want to enable RXCSUM, logically, we should disable GRO_HW. | |
9575 | */ | |
9576 | if (features & NETIF_F_GRO_HW) { | |
9577 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); | |
9578 | features &= ~NETIF_F_GRO_HW; | |
9579 | } | |
9580 | } | |
9581 | ||
de8d5ab2 GP |
9582 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
9583 | if (features & NETIF_F_RXFCS) { | |
9584 | if (features & NETIF_F_LRO) { | |
9585 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); | |
9586 | features &= ~NETIF_F_LRO; | |
9587 | } | |
9588 | ||
9589 | if (features & NETIF_F_GRO_HW) { | |
9590 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); | |
9591 | features &= ~NETIF_F_GRO_HW; | |
9592 | } | |
e6c6a929 GP |
9593 | } |
9594 | ||
25537d71 TT |
9595 | if (features & NETIF_F_HW_TLS_TX) { |
9596 | bool ip_csum = (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) == | |
9597 | (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM); | |
9598 | bool hw_csum = features & NETIF_F_HW_CSUM; | |
9599 | ||
9600 | if (!ip_csum && !hw_csum) { | |
9601 | netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n"); | |
9602 | features &= ~NETIF_F_HW_TLS_TX; | |
9603 | } | |
ae0b04b2 TT |
9604 | } |
9605 | ||
a3eb4e9d TT |
9606 | if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) { |
9607 | netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n"); | |
9608 | features &= ~NETIF_F_HW_TLS_RX; | |
9609 | } | |
9610 | ||
b63365a2 HX |
9611 | return features; |
9612 | } | |
b63365a2 | 9613 | |
6cb6a27c | 9614 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 9615 | { |
fd867d51 | 9616 | struct net_device *upper, *lower; |
c8f44aff | 9617 | netdev_features_t features; |
fd867d51 | 9618 | struct list_head *iter; |
e7868a85 | 9619 | int err = -1; |
5455c699 | 9620 | |
87267485 MM |
9621 | ASSERT_RTNL(); |
9622 | ||
5455c699 MM |
9623 | features = netdev_get_wanted_features(dev); |
9624 | ||
9625 | if (dev->netdev_ops->ndo_fix_features) | |
9626 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
9627 | ||
9628 | /* driver might be less strict about feature dependencies */ | |
9629 | features = netdev_fix_features(dev, features); | |
9630 | ||
4250b75b | 9631 | /* some features can't be enabled if they're off on an upper device */ |
fd867d51 JW |
9632 | netdev_for_each_upper_dev_rcu(dev, upper, iter) |
9633 | features = netdev_sync_upper_features(dev, upper, features); | |
9634 | ||
5455c699 | 9635 | if (dev->features == features) |
e7868a85 | 9636 | goto sync_lower; |
5455c699 | 9637 | |
c8f44aff MM |
9638 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
9639 | &dev->features, &features); | |
5455c699 MM |
9640 | |
9641 | if (dev->netdev_ops->ndo_set_features) | |
9642 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
5f8dc33e NA |
9643 | else |
9644 | err = 0; | |
5455c699 | 9645 | |
6cb6a27c | 9646 | if (unlikely(err < 0)) { |
5455c699 | 9647 | netdev_err(dev, |
c8f44aff MM |
9648 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
9649 | err, &features, &dev->features); | |
17b85d29 NA |
9650 | /* return non-0 since some features might have changed and |
9651 | * it's better to fire a spurious notification than miss it | |
9652 | */ | |
9653 | return -1; | |
6cb6a27c MM |
9654 | } |
9655 | ||
e7868a85 | 9656 | sync_lower: |
fd867d51 JW |
9657 | /* some features must be disabled on lower devices when disabled |
9658 | * on an upper device (think: bonding master or bridge) | |
9659 | */ | |
9660 | netdev_for_each_lower_dev(dev, lower, iter) | |
9661 | netdev_sync_lower_features(dev, lower, features); | |
9662 | ||
ae847f40 SD |
9663 | if (!err) { |
9664 | netdev_features_t diff = features ^ dev->features; | |
9665 | ||
9666 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9667 | /* udp_tunnel_{get,drop}_rx_info both need | |
9668 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the | |
9669 | * device, or they won't do anything. | |
9670 | * Thus we need to update dev->features | |
9671 | * *before* calling udp_tunnel_get_rx_info, | |
9672 | * but *after* calling udp_tunnel_drop_rx_info. | |
9673 | */ | |
9674 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9675 | dev->features = features; | |
9676 | udp_tunnel_get_rx_info(dev); | |
9677 | } else { | |
9678 | udp_tunnel_drop_rx_info(dev); | |
9679 | } | |
9680 | } | |
9681 | ||
9daae9bd GP |
9682 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
9683 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { | |
9684 | dev->features = features; | |
9685 | err |= vlan_get_rx_ctag_filter_info(dev); | |
9686 | } else { | |
9687 | vlan_drop_rx_ctag_filter_info(dev); | |
9688 | } | |
9689 | } | |
9690 | ||
9691 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9692 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9693 | dev->features = features; | |
9694 | err |= vlan_get_rx_stag_filter_info(dev); | |
9695 | } else { | |
9696 | vlan_drop_rx_stag_filter_info(dev); | |
9697 | } | |
9698 | } | |
9699 | ||
6cb6a27c | 9700 | dev->features = features; |
ae847f40 | 9701 | } |
6cb6a27c | 9702 | |
e7868a85 | 9703 | return err < 0 ? 0 : 1; |
6cb6a27c MM |
9704 | } |
9705 | ||
afe12cc8 MM |
9706 | /** |
9707 | * netdev_update_features - recalculate device features | |
9708 | * @dev: the device to check | |
9709 | * | |
9710 | * Recalculate dev->features set and send notifications if it | |
9711 | * has changed. Should be called after driver or hardware dependent | |
9712 | * conditions might have changed that influence the features. | |
9713 | */ | |
6cb6a27c MM |
9714 | void netdev_update_features(struct net_device *dev) |
9715 | { | |
9716 | if (__netdev_update_features(dev)) | |
9717 | netdev_features_change(dev); | |
5455c699 MM |
9718 | } |
9719 | EXPORT_SYMBOL(netdev_update_features); | |
9720 | ||
afe12cc8 MM |
9721 | /** |
9722 | * netdev_change_features - recalculate device features | |
9723 | * @dev: the device to check | |
9724 | * | |
9725 | * Recalculate dev->features set and send notifications even | |
9726 | * if they have not changed. Should be called instead of | |
9727 | * netdev_update_features() if also dev->vlan_features might | |
9728 | * have changed to allow the changes to be propagated to stacked | |
9729 | * VLAN devices. | |
9730 | */ | |
9731 | void netdev_change_features(struct net_device *dev) | |
9732 | { | |
9733 | __netdev_update_features(dev); | |
9734 | netdev_features_change(dev); | |
9735 | } | |
9736 | EXPORT_SYMBOL(netdev_change_features); | |
9737 | ||
fc4a7489 PM |
9738 | /** |
9739 | * netif_stacked_transfer_operstate - transfer operstate | |
9740 | * @rootdev: the root or lower level device to transfer state from | |
9741 | * @dev: the device to transfer operstate to | |
9742 | * | |
9743 | * Transfer operational state from root to device. This is normally | |
9744 | * called when a stacking relationship exists between the root | |
9745 | * device and the device(a leaf device). | |
9746 | */ | |
9747 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
9748 | struct net_device *dev) | |
9749 | { | |
9750 | if (rootdev->operstate == IF_OPER_DORMANT) | |
9751 | netif_dormant_on(dev); | |
9752 | else | |
9753 | netif_dormant_off(dev); | |
9754 | ||
eec517cd AL |
9755 | if (rootdev->operstate == IF_OPER_TESTING) |
9756 | netif_testing_on(dev); | |
9757 | else | |
9758 | netif_testing_off(dev); | |
9759 | ||
0575c86b ZS |
9760 | if (netif_carrier_ok(rootdev)) |
9761 | netif_carrier_on(dev); | |
9762 | else | |
9763 | netif_carrier_off(dev); | |
fc4a7489 PM |
9764 | } |
9765 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
9766 | ||
1b4bf461 ED |
9767 | static int netif_alloc_rx_queues(struct net_device *dev) |
9768 | { | |
1b4bf461 | 9769 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 9770 | struct netdev_rx_queue *rx; |
10595902 | 9771 | size_t sz = count * sizeof(*rx); |
e817f856 | 9772 | int err = 0; |
1b4bf461 | 9773 | |
bd25fa7b | 9774 | BUG_ON(count < 1); |
1b4bf461 | 9775 | |
dcda9b04 | 9776 | rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9777 | if (!rx) |
9778 | return -ENOMEM; | |
9779 | ||
bd25fa7b TH |
9780 | dev->_rx = rx; |
9781 | ||
e817f856 | 9782 | for (i = 0; i < count; i++) { |
fe822240 | 9783 | rx[i].dev = dev; |
e817f856 JDB |
9784 | |
9785 | /* XDP RX-queue setup */ | |
b02e5a0e | 9786 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0); |
e817f856 JDB |
9787 | if (err < 0) |
9788 | goto err_rxq_info; | |
9789 | } | |
1b4bf461 | 9790 | return 0; |
e817f856 JDB |
9791 | |
9792 | err_rxq_info: | |
9793 | /* Rollback successful reg's and free other resources */ | |
9794 | while (i--) | |
9795 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); | |
141b52a9 | 9796 | kvfree(dev->_rx); |
e817f856 JDB |
9797 | dev->_rx = NULL; |
9798 | return err; | |
9799 | } | |
9800 | ||
9801 | static void netif_free_rx_queues(struct net_device *dev) | |
9802 | { | |
9803 | unsigned int i, count = dev->num_rx_queues; | |
e817f856 JDB |
9804 | |
9805 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ | |
9806 | if (!dev->_rx) | |
9807 | return; | |
9808 | ||
e817f856 | 9809 | for (i = 0; i < count; i++) |
82aaff2f JK |
9810 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
9811 | ||
9812 | kvfree(dev->_rx); | |
1b4bf461 ED |
9813 | } |
9814 | ||
aa942104 CG |
9815 | static void netdev_init_one_queue(struct net_device *dev, |
9816 | struct netdev_queue *queue, void *_unused) | |
9817 | { | |
9818 | /* Initialize queue lock */ | |
9819 | spin_lock_init(&queue->_xmit_lock); | |
1a33e10e | 9820 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); |
aa942104 | 9821 | queue->xmit_lock_owner = -1; |
b236da69 | 9822 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 9823 | queue->dev = dev; |
114cf580 TH |
9824 | #ifdef CONFIG_BQL |
9825 | dql_init(&queue->dql, HZ); | |
9826 | #endif | |
aa942104 CG |
9827 | } |
9828 | ||
60877a32 ED |
9829 | static void netif_free_tx_queues(struct net_device *dev) |
9830 | { | |
4cb28970 | 9831 | kvfree(dev->_tx); |
60877a32 ED |
9832 | } |
9833 | ||
e6484930 TH |
9834 | static int netif_alloc_netdev_queues(struct net_device *dev) |
9835 | { | |
9836 | unsigned int count = dev->num_tx_queues; | |
9837 | struct netdev_queue *tx; | |
60877a32 | 9838 | size_t sz = count * sizeof(*tx); |
e6484930 | 9839 | |
d339727c ED |
9840 | if (count < 1 || count > 0xffff) |
9841 | return -EINVAL; | |
62b5942a | 9842 | |
dcda9b04 | 9843 | tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9844 | if (!tx) |
9845 | return -ENOMEM; | |
9846 | ||
e6484930 | 9847 | dev->_tx = tx; |
1d24eb48 | 9848 | |
e6484930 TH |
9849 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
9850 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
9851 | |
9852 | return 0; | |
e6484930 TH |
9853 | } |
9854 | ||
a2029240 DV |
9855 | void netif_tx_stop_all_queues(struct net_device *dev) |
9856 | { | |
9857 | unsigned int i; | |
9858 | ||
9859 | for (i = 0; i < dev->num_tx_queues; i++) { | |
9860 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
f4563a75 | 9861 | |
a2029240 DV |
9862 | netif_tx_stop_queue(txq); |
9863 | } | |
9864 | } | |
9865 | EXPORT_SYMBOL(netif_tx_stop_all_queues); | |
9866 | ||
1da177e4 LT |
9867 | /** |
9868 | * register_netdevice - register a network device | |
9869 | * @dev: device to register | |
9870 | * | |
9871 | * Take a completed network device structure and add it to the kernel | |
9872 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
9873 | * chain. 0 is returned on success. A negative errno code is returned | |
9874 | * on a failure to set up the device, or if the name is a duplicate. | |
9875 | * | |
9876 | * Callers must hold the rtnl semaphore. You may want | |
9877 | * register_netdev() instead of this. | |
9878 | * | |
9879 | * BUGS: | |
9880 | * The locking appears insufficient to guarantee two parallel registers | |
9881 | * will not get the same name. | |
9882 | */ | |
9883 | ||
9884 | int register_netdevice(struct net_device *dev) | |
9885 | { | |
1da177e4 | 9886 | int ret; |
d314774c | 9887 | struct net *net = dev_net(dev); |
1da177e4 | 9888 | |
e283de3a FF |
9889 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
9890 | NETDEV_FEATURE_COUNT); | |
1da177e4 LT |
9891 | BUG_ON(dev_boot_phase); |
9892 | ASSERT_RTNL(); | |
9893 | ||
b17a7c17 SH |
9894 | might_sleep(); |
9895 | ||
1da177e4 LT |
9896 | /* When net_device's are persistent, this will be fatal. */ |
9897 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 9898 | BUG_ON(!net); |
1da177e4 | 9899 | |
9000edb7 JK |
9900 | ret = ethtool_check_ops(dev->ethtool_ops); |
9901 | if (ret) | |
9902 | return ret; | |
9903 | ||
f1f28aa3 | 9904 | spin_lock_init(&dev->addr_list_lock); |
845e0ebb | 9905 | netdev_set_addr_lockdep_class(dev); |
1da177e4 | 9906 | |
828de4f6 | 9907 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
9908 | if (ret < 0) |
9909 | goto out; | |
9910 | ||
9077f052 | 9911 | ret = -ENOMEM; |
ff927412 JP |
9912 | dev->name_node = netdev_name_node_head_alloc(dev); |
9913 | if (!dev->name_node) | |
9914 | goto out; | |
9915 | ||
1da177e4 | 9916 | /* Init, if this function is available */ |
d314774c SH |
9917 | if (dev->netdev_ops->ndo_init) { |
9918 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
9919 | if (ret) { |
9920 | if (ret > 0) | |
9921 | ret = -EIO; | |
42c17fa6 | 9922 | goto err_free_name; |
1da177e4 LT |
9923 | } |
9924 | } | |
4ec93edb | 9925 | |
f646968f PM |
9926 | if (((dev->hw_features | dev->features) & |
9927 | NETIF_F_HW_VLAN_CTAG_FILTER) && | |
d2ed273d MM |
9928 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
9929 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { | |
9930 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); | |
9931 | ret = -EINVAL; | |
9932 | goto err_uninit; | |
9933 | } | |
9934 | ||
9c7dafbf PE |
9935 | ret = -EBUSY; |
9936 | if (!dev->ifindex) | |
9937 | dev->ifindex = dev_new_index(net); | |
9938 | else if (__dev_get_by_index(net, dev->ifindex)) | |
9939 | goto err_uninit; | |
9940 | ||
5455c699 MM |
9941 | /* Transfer changeable features to wanted_features and enable |
9942 | * software offloads (GSO and GRO). | |
9943 | */ | |
1a3c998f | 9944 | dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); |
14d1232f | 9945 | dev->features |= NETIF_F_SOFT_FEATURES; |
d764a122 | 9946 | |
876c4384 | 9947 | if (dev->udp_tunnel_nic_info) { |
d764a122 SD |
9948 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; |
9949 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
9950 | } | |
9951 | ||
14d1232f | 9952 | dev->wanted_features = dev->features & dev->hw_features; |
1da177e4 | 9953 | |
cbc53e08 | 9954 | if (!(dev->flags & IFF_LOOPBACK)) |
34324dc2 | 9955 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
cbc53e08 | 9956 | |
7f348a60 AD |
9957 | /* If IPv4 TCP segmentation offload is supported we should also |
9958 | * allow the device to enable segmenting the frame with the option | |
9959 | * of ignoring a static IP ID value. This doesn't enable the | |
9960 | * feature itself but allows the user to enable it later. | |
9961 | */ | |
cbc53e08 AD |
9962 | if (dev->hw_features & NETIF_F_TSO) |
9963 | dev->hw_features |= NETIF_F_TSO_MANGLEID; | |
7f348a60 AD |
9964 | if (dev->vlan_features & NETIF_F_TSO) |
9965 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; | |
9966 | if (dev->mpls_features & NETIF_F_TSO) | |
9967 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; | |
9968 | if (dev->hw_enc_features & NETIF_F_TSO) | |
9969 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; | |
c6e1a0d1 | 9970 | |
1180e7d6 | 9971 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 9972 | */ |
1180e7d6 | 9973 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 9974 | |
ee579677 PS |
9975 | /* Make NETIF_F_SG inheritable to tunnel devices. |
9976 | */ | |
802ab55a | 9977 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
ee579677 | 9978 | |
0d89d203 SH |
9979 | /* Make NETIF_F_SG inheritable to MPLS. |
9980 | */ | |
9981 | dev->mpls_features |= NETIF_F_SG; | |
9982 | ||
7ffbe3fd JB |
9983 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
9984 | ret = notifier_to_errno(ret); | |
9985 | if (ret) | |
9986 | goto err_uninit; | |
9987 | ||
8b41d188 | 9988 | ret = netdev_register_kobject(dev); |
cb626bf5 JH |
9989 | if (ret) { |
9990 | dev->reg_state = NETREG_UNREGISTERED; | |
7ce1b0ed | 9991 | goto err_uninit; |
cb626bf5 | 9992 | } |
b17a7c17 SH |
9993 | dev->reg_state = NETREG_REGISTERED; |
9994 | ||
6cb6a27c | 9995 | __netdev_update_features(dev); |
8e9b59b2 | 9996 | |
1da177e4 LT |
9997 | /* |
9998 | * Default initial state at registry is that the | |
9999 | * device is present. | |
10000 | */ | |
10001 | ||
10002 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
10003 | ||
8f4cccbb BH |
10004 | linkwatch_init_dev(dev); |
10005 | ||
1da177e4 | 10006 | dev_init_scheduler(dev); |
1da177e4 | 10007 | dev_hold(dev); |
ce286d32 | 10008 | list_netdevice(dev); |
7bf23575 | 10009 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 10010 | |
948b337e JP |
10011 | /* If the device has permanent device address, driver should |
10012 | * set dev_addr and also addr_assign_type should be set to | |
10013 | * NET_ADDR_PERM (default value). | |
10014 | */ | |
10015 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
10016 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
10017 | ||
1da177e4 | 10018 | /* Notify protocols, that a new device appeared. */ |
056925ab | 10019 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 10020 | ret = notifier_to_errno(ret); |
93ee31f1 | 10021 | if (ret) { |
766b0515 JK |
10022 | /* Expect explicit free_netdev() on failure */ |
10023 | dev->needs_free_netdev = false; | |
037e56bd | 10024 | unregister_netdevice_queue(dev, NULL); |
766b0515 | 10025 | goto out; |
93ee31f1 | 10026 | } |
d90a909e EB |
10027 | /* |
10028 | * Prevent userspace races by waiting until the network | |
10029 | * device is fully setup before sending notifications. | |
10030 | */ | |
a2835763 PM |
10031 | if (!dev->rtnl_link_ops || |
10032 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
7f294054 | 10033 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
1da177e4 LT |
10034 | |
10035 | out: | |
10036 | return ret; | |
7ce1b0ed HX |
10037 | |
10038 | err_uninit: | |
d314774c SH |
10039 | if (dev->netdev_ops->ndo_uninit) |
10040 | dev->netdev_ops->ndo_uninit(dev); | |
cf124db5 DM |
10041 | if (dev->priv_destructor) |
10042 | dev->priv_destructor(dev); | |
42c17fa6 DC |
10043 | err_free_name: |
10044 | netdev_name_node_free(dev->name_node); | |
7ce1b0ed | 10045 | goto out; |
1da177e4 | 10046 | } |
d1b19dff | 10047 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 10048 | |
937f1ba5 BH |
10049 | /** |
10050 | * init_dummy_netdev - init a dummy network device for NAPI | |
10051 | * @dev: device to init | |
10052 | * | |
10053 | * This takes a network device structure and initialize the minimum | |
10054 | * amount of fields so it can be used to schedule NAPI polls without | |
10055 | * registering a full blown interface. This is to be used by drivers | |
10056 | * that need to tie several hardware interfaces to a single NAPI | |
10057 | * poll scheduler due to HW limitations. | |
10058 | */ | |
10059 | int init_dummy_netdev(struct net_device *dev) | |
10060 | { | |
10061 | /* Clear everything. Note we don't initialize spinlocks | |
10062 | * are they aren't supposed to be taken by any of the | |
10063 | * NAPI code and this dummy netdev is supposed to be | |
10064 | * only ever used for NAPI polls | |
10065 | */ | |
10066 | memset(dev, 0, sizeof(struct net_device)); | |
10067 | ||
10068 | /* make sure we BUG if trying to hit standard | |
10069 | * register/unregister code path | |
10070 | */ | |
10071 | dev->reg_state = NETREG_DUMMY; | |
10072 | ||
937f1ba5 BH |
10073 | /* NAPI wants this */ |
10074 | INIT_LIST_HEAD(&dev->napi_list); | |
10075 | ||
10076 | /* a dummy interface is started by default */ | |
10077 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
10078 | set_bit(__LINK_STATE_START, &dev->state); | |
10079 | ||
35edfdc7 JE |
10080 | /* napi_busy_loop stats accounting wants this */ |
10081 | dev_net_set(dev, &init_net); | |
10082 | ||
29b4433d ED |
10083 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
10084 | * because users of this 'device' dont need to change | |
10085 | * its refcount. | |
10086 | */ | |
10087 | ||
937f1ba5 BH |
10088 | return 0; |
10089 | } | |
10090 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
10091 | ||
10092 | ||
1da177e4 LT |
10093 | /** |
10094 | * register_netdev - register a network device | |
10095 | * @dev: device to register | |
10096 | * | |
10097 | * Take a completed network device structure and add it to the kernel | |
10098 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
10099 | * chain. 0 is returned on success. A negative errno code is returned | |
10100 | * on a failure to set up the device, or if the name is a duplicate. | |
10101 | * | |
38b4da38 | 10102 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
10103 | * and expands the device name if you passed a format string to |
10104 | * alloc_netdev. | |
10105 | */ | |
10106 | int register_netdev(struct net_device *dev) | |
10107 | { | |
10108 | int err; | |
10109 | ||
b0f3debc KT |
10110 | if (rtnl_lock_killable()) |
10111 | return -EINTR; | |
1da177e4 | 10112 | err = register_netdevice(dev); |
1da177e4 LT |
10113 | rtnl_unlock(); |
10114 | return err; | |
10115 | } | |
10116 | EXPORT_SYMBOL(register_netdev); | |
10117 | ||
29b4433d ED |
10118 | int netdev_refcnt_read(const struct net_device *dev) |
10119 | { | |
10120 | int i, refcnt = 0; | |
10121 | ||
10122 | for_each_possible_cpu(i) | |
10123 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
10124 | return refcnt; | |
10125 | } | |
10126 | EXPORT_SYMBOL(netdev_refcnt_read); | |
10127 | ||
de2b541b MCC |
10128 | #define WAIT_REFS_MIN_MSECS 1 |
10129 | #define WAIT_REFS_MAX_MSECS 250 | |
2c53040f | 10130 | /** |
1da177e4 | 10131 | * netdev_wait_allrefs - wait until all references are gone. |
3de7a37b | 10132 | * @dev: target net_device |
1da177e4 LT |
10133 | * |
10134 | * This is called when unregistering network devices. | |
10135 | * | |
10136 | * Any protocol or device that holds a reference should register | |
10137 | * for netdevice notification, and cleanup and put back the | |
10138 | * reference if they receive an UNREGISTER event. | |
10139 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 10140 | * call dev_put. |
1da177e4 LT |
10141 | */ |
10142 | static void netdev_wait_allrefs(struct net_device *dev) | |
10143 | { | |
10144 | unsigned long rebroadcast_time, warning_time; | |
0e4be9e5 | 10145 | int wait = 0, refcnt; |
1da177e4 | 10146 | |
e014debe ED |
10147 | linkwatch_forget_dev(dev); |
10148 | ||
1da177e4 | 10149 | rebroadcast_time = warning_time = jiffies; |
29b4433d ED |
10150 | refcnt = netdev_refcnt_read(dev); |
10151 | ||
10152 | while (refcnt != 0) { | |
1da177e4 | 10153 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 10154 | rtnl_lock(); |
1da177e4 LT |
10155 | |
10156 | /* Rebroadcast unregister notification */ | |
056925ab | 10157 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
1da177e4 | 10158 | |
748e2d93 | 10159 | __rtnl_unlock(); |
0115e8e3 | 10160 | rcu_barrier(); |
748e2d93 ED |
10161 | rtnl_lock(); |
10162 | ||
1da177e4 LT |
10163 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
10164 | &dev->state)) { | |
10165 | /* We must not have linkwatch events | |
10166 | * pending on unregister. If this | |
10167 | * happens, we simply run the queue | |
10168 | * unscheduled, resulting in a noop | |
10169 | * for this device. | |
10170 | */ | |
10171 | linkwatch_run_queue(); | |
10172 | } | |
10173 | ||
6756ae4b | 10174 | __rtnl_unlock(); |
1da177e4 LT |
10175 | |
10176 | rebroadcast_time = jiffies; | |
10177 | } | |
10178 | ||
0e4be9e5 FR |
10179 | if (!wait) { |
10180 | rcu_barrier(); | |
10181 | wait = WAIT_REFS_MIN_MSECS; | |
10182 | } else { | |
10183 | msleep(wait); | |
10184 | wait = min(wait << 1, WAIT_REFS_MAX_MSECS); | |
10185 | } | |
1da177e4 | 10186 | |
29b4433d ED |
10187 | refcnt = netdev_refcnt_read(dev); |
10188 | ||
d7c04b05 | 10189 | if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) { |
7b6cd1ce JP |
10190 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
10191 | dev->name, refcnt); | |
1da177e4 LT |
10192 | warning_time = jiffies; |
10193 | } | |
10194 | } | |
10195 | } | |
10196 | ||
10197 | /* The sequence is: | |
10198 | * | |
10199 | * rtnl_lock(); | |
10200 | * ... | |
10201 | * register_netdevice(x1); | |
10202 | * register_netdevice(x2); | |
10203 | * ... | |
10204 | * unregister_netdevice(y1); | |
10205 | * unregister_netdevice(y2); | |
10206 | * ... | |
10207 | * rtnl_unlock(); | |
10208 | * free_netdev(y1); | |
10209 | * free_netdev(y2); | |
10210 | * | |
58ec3b4d | 10211 | * We are invoked by rtnl_unlock(). |
1da177e4 | 10212 | * This allows us to deal with problems: |
b17a7c17 | 10213 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
10214 | * without deadlocking with linkwatch via keventd. |
10215 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
10216 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
10217 | * |
10218 | * We must not return until all unregister events added during | |
10219 | * the interval the lock was held have been completed. | |
1da177e4 | 10220 | */ |
1da177e4 LT |
10221 | void netdev_run_todo(void) |
10222 | { | |
626ab0e6 | 10223 | struct list_head list; |
1fc70edb TY |
10224 | #ifdef CONFIG_LOCKDEP |
10225 | struct list_head unlink_list; | |
10226 | ||
10227 | list_replace_init(&net_unlink_list, &unlink_list); | |
10228 | ||
10229 | while (!list_empty(&unlink_list)) { | |
10230 | struct net_device *dev = list_first_entry(&unlink_list, | |
10231 | struct net_device, | |
10232 | unlink_list); | |
0e8b8d6a | 10233 | list_del_init(&dev->unlink_list); |
1fc70edb TY |
10234 | dev->nested_level = dev->lower_level - 1; |
10235 | } | |
10236 | #endif | |
1da177e4 | 10237 | |
1da177e4 | 10238 | /* Snapshot list, allow later requests */ |
626ab0e6 | 10239 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
10240 | |
10241 | __rtnl_unlock(); | |
626ab0e6 | 10242 | |
0115e8e3 ED |
10243 | |
10244 | /* Wait for rcu callbacks to finish before next phase */ | |
850a545b EB |
10245 | if (!list_empty(&list)) |
10246 | rcu_barrier(); | |
10247 | ||
1da177e4 LT |
10248 | while (!list_empty(&list)) { |
10249 | struct net_device *dev | |
e5e26d75 | 10250 | = list_first_entry(&list, struct net_device, todo_list); |
1da177e4 LT |
10251 | list_del(&dev->todo_list); |
10252 | ||
b17a7c17 | 10253 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
7b6cd1ce | 10254 | pr_err("network todo '%s' but state %d\n", |
b17a7c17 SH |
10255 | dev->name, dev->reg_state); |
10256 | dump_stack(); | |
10257 | continue; | |
10258 | } | |
1da177e4 | 10259 | |
b17a7c17 | 10260 | dev->reg_state = NETREG_UNREGISTERED; |
1da177e4 | 10261 | |
b17a7c17 | 10262 | netdev_wait_allrefs(dev); |
1da177e4 | 10263 | |
b17a7c17 | 10264 | /* paranoia */ |
29b4433d | 10265 | BUG_ON(netdev_refcnt_read(dev)); |
7866a621 SN |
10266 | BUG_ON(!list_empty(&dev->ptype_all)); |
10267 | BUG_ON(!list_empty(&dev->ptype_specific)); | |
33d480ce ED |
10268 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
10269 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
330c7272 | 10270 | #if IS_ENABLED(CONFIG_DECNET) |
547b792c | 10271 | WARN_ON(dev->dn_ptr); |
330c7272 | 10272 | #endif |
cf124db5 DM |
10273 | if (dev->priv_destructor) |
10274 | dev->priv_destructor(dev); | |
10275 | if (dev->needs_free_netdev) | |
10276 | free_netdev(dev); | |
9093bbb2 | 10277 | |
50624c93 EB |
10278 | /* Report a network device has been unregistered */ |
10279 | rtnl_lock(); | |
10280 | dev_net(dev)->dev_unreg_count--; | |
10281 | __rtnl_unlock(); | |
10282 | wake_up(&netdev_unregistering_wq); | |
10283 | ||
9093bbb2 SH |
10284 | /* Free network device */ |
10285 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 10286 | } |
1da177e4 LT |
10287 | } |
10288 | ||
9256645a JW |
10289 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
10290 | * all the same fields in the same order as net_device_stats, with only | |
10291 | * the type differing, but rtnl_link_stats64 may have additional fields | |
10292 | * at the end for newer counters. | |
3cfde79c | 10293 | */ |
77a1abf5 ED |
10294 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
10295 | const struct net_device_stats *netdev_stats) | |
3cfde79c BH |
10296 | { |
10297 | #if BITS_PER_LONG == 64 | |
9256645a | 10298 | BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats)); |
9af9959e | 10299 | memcpy(stats64, netdev_stats, sizeof(*netdev_stats)); |
9256645a JW |
10300 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10301 | memset((char *)stats64 + sizeof(*netdev_stats), 0, | |
10302 | sizeof(*stats64) - sizeof(*netdev_stats)); | |
3cfde79c | 10303 | #else |
9256645a | 10304 | size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long); |
3cfde79c BH |
10305 | const unsigned long *src = (const unsigned long *)netdev_stats; |
10306 | u64 *dst = (u64 *)stats64; | |
10307 | ||
9256645a | 10308 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
3cfde79c BH |
10309 | for (i = 0; i < n; i++) |
10310 | dst[i] = src[i]; | |
9256645a JW |
10311 | /* zero out counters that only exist in rtnl_link_stats64 */ |
10312 | memset((char *)stats64 + n * sizeof(u64), 0, | |
10313 | sizeof(*stats64) - n * sizeof(u64)); | |
3cfde79c BH |
10314 | #endif |
10315 | } | |
77a1abf5 | 10316 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 10317 | |
eeda3fd6 SH |
10318 | /** |
10319 | * dev_get_stats - get network device statistics | |
10320 | * @dev: device to get statistics from | |
28172739 | 10321 | * @storage: place to store stats |
eeda3fd6 | 10322 | * |
d7753516 BH |
10323 | * Get network statistics from device. Return @storage. |
10324 | * The device driver may provide its own method by setting | |
10325 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
10326 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 10327 | */ |
d7753516 BH |
10328 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
10329 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 10330 | { |
eeda3fd6 SH |
10331 | const struct net_device_ops *ops = dev->netdev_ops; |
10332 | ||
28172739 ED |
10333 | if (ops->ndo_get_stats64) { |
10334 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
10335 | ops->ndo_get_stats64(dev, storage); |
10336 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 10337 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
caf586e5 ED |
10338 | } else { |
10339 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 10340 | } |
6f64ec74 ED |
10341 | storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped); |
10342 | storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped); | |
10343 | storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler); | |
28172739 | 10344 | return storage; |
c45d286e | 10345 | } |
eeda3fd6 | 10346 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 10347 | |
44fa32f0 HK |
10348 | /** |
10349 | * dev_fetch_sw_netstats - get per-cpu network device statistics | |
10350 | * @s: place to store stats | |
10351 | * @netstats: per-cpu network stats to read from | |
10352 | * | |
10353 | * Read per-cpu network statistics and populate the related fields in @s. | |
10354 | */ | |
10355 | void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s, | |
10356 | const struct pcpu_sw_netstats __percpu *netstats) | |
10357 | { | |
10358 | int cpu; | |
10359 | ||
10360 | for_each_possible_cpu(cpu) { | |
10361 | const struct pcpu_sw_netstats *stats; | |
10362 | struct pcpu_sw_netstats tmp; | |
10363 | unsigned int start; | |
10364 | ||
10365 | stats = per_cpu_ptr(netstats, cpu); | |
10366 | do { | |
10367 | start = u64_stats_fetch_begin_irq(&stats->syncp); | |
10368 | tmp.rx_packets = stats->rx_packets; | |
10369 | tmp.rx_bytes = stats->rx_bytes; | |
10370 | tmp.tx_packets = stats->tx_packets; | |
10371 | tmp.tx_bytes = stats->tx_bytes; | |
10372 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); | |
10373 | ||
10374 | s->rx_packets += tmp.rx_packets; | |
10375 | s->rx_bytes += tmp.rx_bytes; | |
10376 | s->tx_packets += tmp.tx_packets; | |
10377 | s->tx_bytes += tmp.tx_bytes; | |
10378 | } | |
10379 | } | |
10380 | EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats); | |
10381 | ||
a1839426 HK |
10382 | /** |
10383 | * dev_get_tstats64 - ndo_get_stats64 implementation | |
10384 | * @dev: device to get statistics from | |
10385 | * @s: place to store stats | |
10386 | * | |
10387 | * Populate @s from dev->stats and dev->tstats. Can be used as | |
10388 | * ndo_get_stats64() callback. | |
10389 | */ | |
10390 | void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s) | |
10391 | { | |
10392 | netdev_stats_to_stats64(s, &dev->stats); | |
10393 | dev_fetch_sw_netstats(s, dev->tstats); | |
10394 | } | |
10395 | EXPORT_SYMBOL_GPL(dev_get_tstats64); | |
10396 | ||
24824a09 | 10397 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 10398 | { |
24824a09 | 10399 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 10400 | |
24824a09 ED |
10401 | #ifdef CONFIG_NET_CLS_ACT |
10402 | if (queue) | |
10403 | return queue; | |
10404 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
10405 | if (!queue) | |
10406 | return NULL; | |
10407 | netdev_init_one_queue(dev, queue, NULL); | |
2ce1ee17 | 10408 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
24824a09 ED |
10409 | queue->qdisc_sleeping = &noop_qdisc; |
10410 | rcu_assign_pointer(dev->ingress_queue, queue); | |
10411 | #endif | |
10412 | return queue; | |
bb949fbd DM |
10413 | } |
10414 | ||
2c60db03 ED |
10415 | static const struct ethtool_ops default_ethtool_ops; |
10416 | ||
d07d7507 SG |
10417 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
10418 | const struct ethtool_ops *ops) | |
10419 | { | |
10420 | if (dev->ethtool_ops == &default_ethtool_ops) | |
10421 | dev->ethtool_ops = ops; | |
10422 | } | |
10423 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
10424 | ||
74d332c1 ED |
10425 | void netdev_freemem(struct net_device *dev) |
10426 | { | |
10427 | char *addr = (char *)dev - dev->padded; | |
10428 | ||
4cb28970 | 10429 | kvfree(addr); |
74d332c1 ED |
10430 | } |
10431 | ||
1da177e4 | 10432 | /** |
722c9a0c | 10433 | * alloc_netdev_mqs - allocate network device |
10434 | * @sizeof_priv: size of private data to allocate space for | |
10435 | * @name: device name format string | |
10436 | * @name_assign_type: origin of device name | |
10437 | * @setup: callback to initialize device | |
10438 | * @txqs: the number of TX subqueues to allocate | |
10439 | * @rxqs: the number of RX subqueues to allocate | |
10440 | * | |
10441 | * Allocates a struct net_device with private data area for driver use | |
10442 | * and performs basic initialization. Also allocates subqueue structs | |
10443 | * for each queue on the device. | |
1da177e4 | 10444 | */ |
36909ea4 | 10445 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
c835a677 | 10446 | unsigned char name_assign_type, |
36909ea4 TH |
10447 | void (*setup)(struct net_device *), |
10448 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 10449 | { |
1da177e4 | 10450 | struct net_device *dev; |
52a59bd5 | 10451 | unsigned int alloc_size; |
1ce8e7b5 | 10452 | struct net_device *p; |
1da177e4 | 10453 | |
b6fe17d6 SH |
10454 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
10455 | ||
36909ea4 | 10456 | if (txqs < 1) { |
7b6cd1ce | 10457 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
10458 | return NULL; |
10459 | } | |
10460 | ||
36909ea4 | 10461 | if (rxqs < 1) { |
7b6cd1ce | 10462 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
10463 | return NULL; |
10464 | } | |
36909ea4 | 10465 | |
fd2ea0a7 | 10466 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
10467 | if (sizeof_priv) { |
10468 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 10469 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
10470 | alloc_size += sizeof_priv; |
10471 | } | |
10472 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 10473 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 10474 | |
dcda9b04 | 10475 | p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
62b5942a | 10476 | if (!p) |
1da177e4 | 10477 | return NULL; |
1da177e4 | 10478 | |
1ce8e7b5 | 10479 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 10480 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 10481 | |
29b4433d ED |
10482 | dev->pcpu_refcnt = alloc_percpu(int); |
10483 | if (!dev->pcpu_refcnt) | |
74d332c1 | 10484 | goto free_dev; |
ab9c73cc | 10485 | |
ab9c73cc | 10486 | if (dev_addr_init(dev)) |
29b4433d | 10487 | goto free_pcpu; |
ab9c73cc | 10488 | |
22bedad3 | 10489 | dev_mc_init(dev); |
a748ee24 | 10490 | dev_uc_init(dev); |
ccffad25 | 10491 | |
c346dca1 | 10492 | dev_net_set(dev, &init_net); |
1da177e4 | 10493 | |
8d3bdbd5 | 10494 | dev->gso_max_size = GSO_MAX_SIZE; |
30b678d8 | 10495 | dev->gso_max_segs = GSO_MAX_SEGS; |
5343da4c TY |
10496 | dev->upper_level = 1; |
10497 | dev->lower_level = 1; | |
1fc70edb TY |
10498 | #ifdef CONFIG_LOCKDEP |
10499 | dev->nested_level = 0; | |
10500 | INIT_LIST_HEAD(&dev->unlink_list); | |
10501 | #endif | |
8d3bdbd5 | 10502 | |
8d3bdbd5 DM |
10503 | INIT_LIST_HEAD(&dev->napi_list); |
10504 | INIT_LIST_HEAD(&dev->unreg_list); | |
5cde2829 | 10505 | INIT_LIST_HEAD(&dev->close_list); |
8d3bdbd5 | 10506 | INIT_LIST_HEAD(&dev->link_watch_list); |
2f268f12 VF |
10507 | INIT_LIST_HEAD(&dev->adj_list.upper); |
10508 | INIT_LIST_HEAD(&dev->adj_list.lower); | |
7866a621 SN |
10509 | INIT_LIST_HEAD(&dev->ptype_all); |
10510 | INIT_LIST_HEAD(&dev->ptype_specific); | |
93642e14 | 10511 | INIT_LIST_HEAD(&dev->net_notifier_list); |
59cc1f61 JK |
10512 | #ifdef CONFIG_NET_SCHED |
10513 | hash_init(dev->qdisc_hash); | |
10514 | #endif | |
02875878 | 10515 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
8d3bdbd5 DM |
10516 | setup(dev); |
10517 | ||
a813104d | 10518 | if (!dev->tx_queue_len) { |
f84bb1ea | 10519 | dev->priv_flags |= IFF_NO_QUEUE; |
11597084 | 10520 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
a813104d | 10521 | } |
906470c1 | 10522 | |
36909ea4 TH |
10523 | dev->num_tx_queues = txqs; |
10524 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 10525 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 10526 | goto free_all; |
e8a0464c | 10527 | |
36909ea4 TH |
10528 | dev->num_rx_queues = rxqs; |
10529 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 10530 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 10531 | goto free_all; |
0a9627f2 | 10532 | |
1da177e4 | 10533 | strcpy(dev->name, name); |
c835a677 | 10534 | dev->name_assign_type = name_assign_type; |
cbda10fa | 10535 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
10536 | if (!dev->ethtool_ops) |
10537 | dev->ethtool_ops = &default_ethtool_ops; | |
e687ad60 | 10538 | |
357b6cc5 | 10539 | nf_hook_ingress_init(dev); |
e687ad60 | 10540 | |
1da177e4 | 10541 | return dev; |
ab9c73cc | 10542 | |
8d3bdbd5 DM |
10543 | free_all: |
10544 | free_netdev(dev); | |
10545 | return NULL; | |
10546 | ||
29b4433d ED |
10547 | free_pcpu: |
10548 | free_percpu(dev->pcpu_refcnt); | |
74d332c1 ED |
10549 | free_dev: |
10550 | netdev_freemem(dev); | |
ab9c73cc | 10551 | return NULL; |
1da177e4 | 10552 | } |
36909ea4 | 10553 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
10554 | |
10555 | /** | |
722c9a0c | 10556 | * free_netdev - free network device |
10557 | * @dev: device | |
1da177e4 | 10558 | * |
722c9a0c | 10559 | * This function does the last stage of destroying an allocated device |
10560 | * interface. The reference to the device object is released. If this | |
10561 | * is the last reference then it will be freed.Must be called in process | |
10562 | * context. | |
1da177e4 LT |
10563 | */ |
10564 | void free_netdev(struct net_device *dev) | |
10565 | { | |
d565b0a1 HX |
10566 | struct napi_struct *p, *n; |
10567 | ||
93d05d4a | 10568 | might_sleep(); |
c269a24c JK |
10569 | |
10570 | /* When called immediately after register_netdevice() failed the unwind | |
10571 | * handling may still be dismantling the device. Handle that case by | |
10572 | * deferring the free. | |
10573 | */ | |
10574 | if (dev->reg_state == NETREG_UNREGISTERING) { | |
10575 | ASSERT_RTNL(); | |
10576 | dev->needs_free_netdev = true; | |
10577 | return; | |
10578 | } | |
10579 | ||
60877a32 | 10580 | netif_free_tx_queues(dev); |
e817f856 | 10581 | netif_free_rx_queues(dev); |
e8a0464c | 10582 | |
33d480ce | 10583 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 10584 | |
f001fde5 JP |
10585 | /* Flush device addresses */ |
10586 | dev_addr_flush(dev); | |
10587 | ||
d565b0a1 HX |
10588 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
10589 | netif_napi_del(p); | |
10590 | ||
29b4433d ED |
10591 | free_percpu(dev->pcpu_refcnt); |
10592 | dev->pcpu_refcnt = NULL; | |
75ccae62 THJ |
10593 | free_percpu(dev->xdp_bulkq); |
10594 | dev->xdp_bulkq = NULL; | |
29b4433d | 10595 | |
3041a069 | 10596 | /* Compatibility with error handling in drivers */ |
1da177e4 | 10597 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
74d332c1 | 10598 | netdev_freemem(dev); |
1da177e4 LT |
10599 | return; |
10600 | } | |
10601 | ||
10602 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
10603 | dev->reg_state = NETREG_RELEASED; | |
10604 | ||
43cb76d9 GKH |
10605 | /* will free via device release */ |
10606 | put_device(&dev->dev); | |
1da177e4 | 10607 | } |
d1b19dff | 10608 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 10609 | |
f0db275a SH |
10610 | /** |
10611 | * synchronize_net - Synchronize with packet receive processing | |
10612 | * | |
10613 | * Wait for packets currently being received to be done. | |
10614 | * Does not block later packets from starting. | |
10615 | */ | |
4ec93edb | 10616 | void synchronize_net(void) |
1da177e4 LT |
10617 | { |
10618 | might_sleep(); | |
be3fc413 ED |
10619 | if (rtnl_is_locked()) |
10620 | synchronize_rcu_expedited(); | |
10621 | else | |
10622 | synchronize_rcu(); | |
1da177e4 | 10623 | } |
d1b19dff | 10624 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
10625 | |
10626 | /** | |
44a0873d | 10627 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 10628 | * @dev: device |
44a0873d | 10629 | * @head: list |
6ebfbc06 | 10630 | * |
1da177e4 | 10631 | * This function shuts down a device interface and removes it |
d59b54b1 | 10632 | * from the kernel tables. |
44a0873d | 10633 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
10634 | * |
10635 | * Callers must hold the rtnl semaphore. You may want | |
10636 | * unregister_netdev() instead of this. | |
10637 | */ | |
10638 | ||
44a0873d | 10639 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 10640 | { |
a6620712 HX |
10641 | ASSERT_RTNL(); |
10642 | ||
44a0873d | 10643 | if (head) { |
9fdce099 | 10644 | list_move_tail(&dev->unreg_list, head); |
44a0873d | 10645 | } else { |
037e56bd JK |
10646 | LIST_HEAD(single); |
10647 | ||
10648 | list_add(&dev->unreg_list, &single); | |
0cbe1e57 | 10649 | unregister_netdevice_many(&single); |
44a0873d | 10650 | } |
1da177e4 | 10651 | } |
44a0873d | 10652 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 10653 | |
9b5e383c ED |
10654 | /** |
10655 | * unregister_netdevice_many - unregister many devices | |
10656 | * @head: list of devices | |
87757a91 ED |
10657 | * |
10658 | * Note: As most callers use a stack allocated list_head, | |
10659 | * we force a list_del() to make sure stack wont be corrupted later. | |
9b5e383c ED |
10660 | */ |
10661 | void unregister_netdevice_many(struct list_head *head) | |
bcfe2f1a JK |
10662 | { |
10663 | struct net_device *dev, *tmp; | |
10664 | LIST_HEAD(close_head); | |
10665 | ||
10666 | BUG_ON(dev_boot_phase); | |
10667 | ASSERT_RTNL(); | |
10668 | ||
0cbe1e57 JK |
10669 | if (list_empty(head)) |
10670 | return; | |
10671 | ||
bcfe2f1a JK |
10672 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
10673 | /* Some devices call without registering | |
10674 | * for initialization unwind. Remove those | |
10675 | * devices and proceed with the remaining. | |
10676 | */ | |
10677 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
10678 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", | |
10679 | dev->name, dev); | |
10680 | ||
10681 | WARN_ON(1); | |
10682 | list_del(&dev->unreg_list); | |
10683 | continue; | |
10684 | } | |
10685 | dev->dismantle = true; | |
10686 | BUG_ON(dev->reg_state != NETREG_REGISTERED); | |
10687 | } | |
10688 | ||
10689 | /* If device is running, close it first. */ | |
10690 | list_for_each_entry(dev, head, unreg_list) | |
10691 | list_add_tail(&dev->close_list, &close_head); | |
10692 | dev_close_many(&close_head, true); | |
10693 | ||
10694 | list_for_each_entry(dev, head, unreg_list) { | |
10695 | /* And unlink it from device chain. */ | |
10696 | unlist_netdevice(dev); | |
10697 | ||
10698 | dev->reg_state = NETREG_UNREGISTERING; | |
10699 | } | |
10700 | flush_all_backlogs(); | |
10701 | ||
10702 | synchronize_net(); | |
10703 | ||
10704 | list_for_each_entry(dev, head, unreg_list) { | |
10705 | struct sk_buff *skb = NULL; | |
10706 | ||
10707 | /* Shutdown queueing discipline. */ | |
10708 | dev_shutdown(dev); | |
10709 | ||
10710 | dev_xdp_uninstall(dev); | |
10711 | ||
10712 | /* Notify protocols, that we are about to destroy | |
10713 | * this device. They should clean all the things. | |
10714 | */ | |
10715 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
10716 | ||
10717 | if (!dev->rtnl_link_ops || | |
10718 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
10719 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, | |
10720 | GFP_KERNEL, NULL, 0); | |
10721 | ||
10722 | /* | |
10723 | * Flush the unicast and multicast chains | |
10724 | */ | |
10725 | dev_uc_flush(dev); | |
10726 | dev_mc_flush(dev); | |
10727 | ||
10728 | netdev_name_node_alt_flush(dev); | |
10729 | netdev_name_node_free(dev->name_node); | |
10730 | ||
10731 | if (dev->netdev_ops->ndo_uninit) | |
10732 | dev->netdev_ops->ndo_uninit(dev); | |
10733 | ||
10734 | if (skb) | |
10735 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL); | |
10736 | ||
10737 | /* Notifier chain MUST detach us all upper devices. */ | |
10738 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
10739 | WARN_ON(netdev_has_any_lower_dev(dev)); | |
10740 | ||
10741 | /* Remove entries from kobject tree */ | |
10742 | netdev_unregister_kobject(dev); | |
10743 | #ifdef CONFIG_XPS | |
10744 | /* Remove XPS queueing entries */ | |
10745 | netif_reset_xps_queues_gt(dev, 0); | |
10746 | #endif | |
10747 | } | |
10748 | ||
10749 | synchronize_net(); | |
10750 | ||
10751 | list_for_each_entry(dev, head, unreg_list) { | |
10752 | dev_put(dev); | |
10753 | net_set_todo(dev); | |
10754 | } | |
0cbe1e57 JK |
10755 | |
10756 | list_del(head); | |
bcfe2f1a | 10757 | } |
0cbe1e57 | 10758 | EXPORT_SYMBOL(unregister_netdevice_many); |
bcfe2f1a | 10759 | |
1da177e4 LT |
10760 | /** |
10761 | * unregister_netdev - remove device from the kernel | |
10762 | * @dev: device | |
10763 | * | |
10764 | * This function shuts down a device interface and removes it | |
d59b54b1 | 10765 | * from the kernel tables. |
1da177e4 LT |
10766 | * |
10767 | * This is just a wrapper for unregister_netdevice that takes | |
10768 | * the rtnl semaphore. In general you want to use this and not | |
10769 | * unregister_netdevice. | |
10770 | */ | |
10771 | void unregister_netdev(struct net_device *dev) | |
10772 | { | |
10773 | rtnl_lock(); | |
10774 | unregister_netdevice(dev); | |
10775 | rtnl_unlock(); | |
10776 | } | |
1da177e4 LT |
10777 | EXPORT_SYMBOL(unregister_netdev); |
10778 | ||
ce286d32 EB |
10779 | /** |
10780 | * dev_change_net_namespace - move device to different nethost namespace | |
10781 | * @dev: device | |
10782 | * @net: network namespace | |
10783 | * @pat: If not NULL name pattern to try if the current device name | |
10784 | * is already taken in the destination network namespace. | |
10785 | * | |
10786 | * This function shuts down a device interface and moves it | |
10787 | * to a new network namespace. On success 0 is returned, on | |
10788 | * a failure a netagive errno code is returned. | |
10789 | * | |
10790 | * Callers must hold the rtnl semaphore. | |
10791 | */ | |
10792 | ||
10793 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) | |
10794 | { | |
ef6a4c88 | 10795 | struct net *net_old = dev_net(dev); |
38e01b30 | 10796 | int err, new_nsid, new_ifindex; |
ce286d32 EB |
10797 | |
10798 | ASSERT_RTNL(); | |
10799 | ||
10800 | /* Don't allow namespace local devices to be moved. */ | |
10801 | err = -EINVAL; | |
10802 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
10803 | goto out; | |
10804 | ||
10805 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
10806 | if (dev->reg_state != NETREG_REGISTERED) |
10807 | goto out; | |
10808 | ||
10809 | /* Get out if there is nothing todo */ | |
10810 | err = 0; | |
ef6a4c88 | 10811 | if (net_eq(net_old, net)) |
ce286d32 EB |
10812 | goto out; |
10813 | ||
10814 | /* Pick the destination device name, and ensure | |
10815 | * we can use it in the destination network namespace. | |
10816 | */ | |
10817 | err = -EEXIST; | |
d9031024 | 10818 | if (__dev_get_by_name(net, dev->name)) { |
ce286d32 EB |
10819 | /* We get here if we can't use the current device name */ |
10820 | if (!pat) | |
10821 | goto out; | |
7892bd08 LR |
10822 | err = dev_get_valid_name(net, dev, pat); |
10823 | if (err < 0) | |
ce286d32 EB |
10824 | goto out; |
10825 | } | |
10826 | ||
10827 | /* | |
10828 | * And now a mini version of register_netdevice unregister_netdevice. | |
10829 | */ | |
10830 | ||
10831 | /* If device is running close it first. */ | |
9b772652 | 10832 | dev_close(dev); |
ce286d32 EB |
10833 | |
10834 | /* And unlink it from device chain */ | |
ce286d32 EB |
10835 | unlist_netdevice(dev); |
10836 | ||
10837 | synchronize_net(); | |
10838 | ||
10839 | /* Shutdown queueing discipline. */ | |
10840 | dev_shutdown(dev); | |
10841 | ||
10842 | /* Notify protocols, that we are about to destroy | |
eb13da1a | 10843 | * this device. They should clean all the things. |
10844 | * | |
10845 | * Note that dev->reg_state stays at NETREG_REGISTERED. | |
10846 | * This is wanted because this way 8021q and macvlan know | |
10847 | * the device is just moving and can keep their slaves up. | |
10848 | */ | |
ce286d32 | 10849 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
6549dd43 | 10850 | rcu_barrier(); |
38e01b30 | 10851 | |
d4e4fdf9 | 10852 | new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); |
38e01b30 ND |
10853 | /* If there is an ifindex conflict assign a new one */ |
10854 | if (__dev_get_by_index(net, dev->ifindex)) | |
10855 | new_ifindex = dev_new_index(net); | |
10856 | else | |
10857 | new_ifindex = dev->ifindex; | |
10858 | ||
10859 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, | |
10860 | new_ifindex); | |
ce286d32 EB |
10861 | |
10862 | /* | |
10863 | * Flush the unicast and multicast chains | |
10864 | */ | |
a748ee24 | 10865 | dev_uc_flush(dev); |
22bedad3 | 10866 | dev_mc_flush(dev); |
ce286d32 | 10867 | |
4e66ae2e SH |
10868 | /* Send a netdev-removed uevent to the old namespace */ |
10869 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
4c75431a | 10870 | netdev_adjacent_del_links(dev); |
4e66ae2e | 10871 | |
93642e14 JP |
10872 | /* Move per-net netdevice notifiers that are following the netdevice */ |
10873 | move_netdevice_notifiers_dev_net(dev, net); | |
10874 | ||
ce286d32 | 10875 | /* Actually switch the network namespace */ |
c346dca1 | 10876 | dev_net_set(dev, net); |
38e01b30 | 10877 | dev->ifindex = new_ifindex; |
ce286d32 | 10878 | |
4e66ae2e SH |
10879 | /* Send a netdev-add uevent to the new namespace */ |
10880 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
4c75431a | 10881 | netdev_adjacent_add_links(dev); |
4e66ae2e | 10882 | |
8b41d188 | 10883 | /* Fixup kobjects */ |
a1b3f594 | 10884 | err = device_rename(&dev->dev, dev->name); |
8b41d188 | 10885 | WARN_ON(err); |
ce286d32 | 10886 | |
ef6a4c88 CB |
10887 | /* Adapt owner in case owning user namespace of target network |
10888 | * namespace is different from the original one. | |
10889 | */ | |
10890 | err = netdev_change_owner(dev, net_old, net); | |
10891 | WARN_ON(err); | |
10892 | ||
ce286d32 EB |
10893 | /* Add the device back in the hashes */ |
10894 | list_netdevice(dev); | |
10895 | ||
10896 | /* Notify protocols, that a new device appeared. */ | |
10897 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
10898 | ||
d90a909e EB |
10899 | /* |
10900 | * Prevent userspace races by waiting until the network | |
10901 | * device is fully setup before sending notifications. | |
10902 | */ | |
7f294054 | 10903 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
d90a909e | 10904 | |
ce286d32 EB |
10905 | synchronize_net(); |
10906 | err = 0; | |
10907 | out: | |
10908 | return err; | |
10909 | } | |
463d0183 | 10910 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
ce286d32 | 10911 | |
f0bf90de | 10912 | static int dev_cpu_dead(unsigned int oldcpu) |
1da177e4 LT |
10913 | { |
10914 | struct sk_buff **list_skb; | |
1da177e4 | 10915 | struct sk_buff *skb; |
f0bf90de | 10916 | unsigned int cpu; |
97d8b6e3 | 10917 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
1da177e4 | 10918 | |
1da177e4 LT |
10919 | local_irq_disable(); |
10920 | cpu = smp_processor_id(); | |
10921 | sd = &per_cpu(softnet_data, cpu); | |
10922 | oldsd = &per_cpu(softnet_data, oldcpu); | |
10923 | ||
10924 | /* Find end of our completion_queue. */ | |
10925 | list_skb = &sd->completion_queue; | |
10926 | while (*list_skb) | |
10927 | list_skb = &(*list_skb)->next; | |
10928 | /* Append completion queue from offline CPU. */ | |
10929 | *list_skb = oldsd->completion_queue; | |
10930 | oldsd->completion_queue = NULL; | |
10931 | ||
1da177e4 | 10932 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
10933 | if (oldsd->output_queue) { |
10934 | *sd->output_queue_tailp = oldsd->output_queue; | |
10935 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
10936 | oldsd->output_queue = NULL; | |
10937 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
10938 | } | |
ac64da0b ED |
10939 | /* Append NAPI poll list from offline CPU, with one exception : |
10940 | * process_backlog() must be called by cpu owning percpu backlog. | |
10941 | * We properly handle process_queue & input_pkt_queue later. | |
10942 | */ | |
10943 | while (!list_empty(&oldsd->poll_list)) { | |
10944 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, | |
10945 | struct napi_struct, | |
10946 | poll_list); | |
10947 | ||
10948 | list_del_init(&napi->poll_list); | |
10949 | if (napi->poll == process_backlog) | |
10950 | napi->state = 0; | |
10951 | else | |
10952 | ____napi_schedule(sd, napi); | |
264524d5 | 10953 | } |
1da177e4 LT |
10954 | |
10955 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
10956 | local_irq_enable(); | |
10957 | ||
773fc8f6 | 10958 | #ifdef CONFIG_RPS |
10959 | remsd = oldsd->rps_ipi_list; | |
10960 | oldsd->rps_ipi_list = NULL; | |
10961 | #endif | |
10962 | /* send out pending IPI's on offline CPU */ | |
10963 | net_rps_send_ipi(remsd); | |
10964 | ||
1da177e4 | 10965 | /* Process offline CPU's input_pkt_queue */ |
76cc8b13 | 10966 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
91e83133 | 10967 | netif_rx_ni(skb); |
76cc8b13 | 10968 | input_queue_head_incr(oldsd); |
fec5e652 | 10969 | } |
ac64da0b | 10970 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
91e83133 | 10971 | netif_rx_ni(skb); |
76cc8b13 TH |
10972 | input_queue_head_incr(oldsd); |
10973 | } | |
1da177e4 | 10974 | |
f0bf90de | 10975 | return 0; |
1da177e4 | 10976 | } |
1da177e4 | 10977 | |
7f353bf2 | 10978 | /** |
b63365a2 HX |
10979 | * netdev_increment_features - increment feature set by one |
10980 | * @all: current feature set | |
10981 | * @one: new feature set | |
10982 | * @mask: mask feature set | |
7f353bf2 HX |
10983 | * |
10984 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
10985 | * @one to the master device with current feature set @all. Will not |
10986 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 10987 | */ |
c8f44aff MM |
10988 | netdev_features_t netdev_increment_features(netdev_features_t all, |
10989 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 10990 | { |
c8cd0989 | 10991 | if (mask & NETIF_F_HW_CSUM) |
a188222b | 10992 | mask |= NETIF_F_CSUM_MASK; |
1742f183 | 10993 | mask |= NETIF_F_VLAN_CHALLENGED; |
7f353bf2 | 10994 | |
a188222b | 10995 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
1742f183 | 10996 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
c6e1a0d1 | 10997 | |
1742f183 | 10998 | /* If one device supports hw checksumming, set for all. */ |
c8cd0989 TH |
10999 | if (all & NETIF_F_HW_CSUM) |
11000 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); | |
7f353bf2 HX |
11001 | |
11002 | return all; | |
11003 | } | |
b63365a2 | 11004 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 11005 | |
430f03cd | 11006 | static struct hlist_head * __net_init netdev_create_hash(void) |
30d97d35 PE |
11007 | { |
11008 | int i; | |
11009 | struct hlist_head *hash; | |
11010 | ||
6da2ec56 | 11011 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
30d97d35 PE |
11012 | if (hash != NULL) |
11013 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
11014 | INIT_HLIST_HEAD(&hash[i]); | |
11015 | ||
11016 | return hash; | |
11017 | } | |
11018 | ||
881d966b | 11019 | /* Initialize per network namespace state */ |
4665079c | 11020 | static int __net_init netdev_init(struct net *net) |
881d966b | 11021 | { |
d9f37d01 | 11022 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
c593642c | 11023 | 8 * sizeof_field(struct napi_struct, gro_bitmask)); |
d9f37d01 | 11024 | |
734b6541 RM |
11025 | if (net != &init_net) |
11026 | INIT_LIST_HEAD(&net->dev_base_head); | |
881d966b | 11027 | |
30d97d35 PE |
11028 | net->dev_name_head = netdev_create_hash(); |
11029 | if (net->dev_name_head == NULL) | |
11030 | goto err_name; | |
881d966b | 11031 | |
30d97d35 PE |
11032 | net->dev_index_head = netdev_create_hash(); |
11033 | if (net->dev_index_head == NULL) | |
11034 | goto err_idx; | |
881d966b | 11035 | |
a30c7b42 JP |
11036 | RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain); |
11037 | ||
881d966b | 11038 | return 0; |
30d97d35 PE |
11039 | |
11040 | err_idx: | |
11041 | kfree(net->dev_name_head); | |
11042 | err_name: | |
11043 | return -ENOMEM; | |
881d966b EB |
11044 | } |
11045 | ||
f0db275a SH |
11046 | /** |
11047 | * netdev_drivername - network driver for the device | |
11048 | * @dev: network device | |
f0db275a SH |
11049 | * |
11050 | * Determine network driver for device. | |
11051 | */ | |
3019de12 | 11052 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 11053 | { |
cf04a4c7 SH |
11054 | const struct device_driver *driver; |
11055 | const struct device *parent; | |
3019de12 | 11056 | const char *empty = ""; |
6579e57b AV |
11057 | |
11058 | parent = dev->dev.parent; | |
6579e57b | 11059 | if (!parent) |
3019de12 | 11060 | return empty; |
6579e57b AV |
11061 | |
11062 | driver = parent->driver; | |
11063 | if (driver && driver->name) | |
3019de12 DM |
11064 | return driver->name; |
11065 | return empty; | |
6579e57b AV |
11066 | } |
11067 | ||
6ea754eb JP |
11068 | static void __netdev_printk(const char *level, const struct net_device *dev, |
11069 | struct va_format *vaf) | |
256df2f3 | 11070 | { |
b004ff49 | 11071 | if (dev && dev->dev.parent) { |
6ea754eb JP |
11072 | dev_printk_emit(level[1] - '0', |
11073 | dev->dev.parent, | |
11074 | "%s %s %s%s: %pV", | |
11075 | dev_driver_string(dev->dev.parent), | |
11076 | dev_name(dev->dev.parent), | |
11077 | netdev_name(dev), netdev_reg_state(dev), | |
11078 | vaf); | |
b004ff49 | 11079 | } else if (dev) { |
6ea754eb JP |
11080 | printk("%s%s%s: %pV", |
11081 | level, netdev_name(dev), netdev_reg_state(dev), vaf); | |
b004ff49 | 11082 | } else { |
6ea754eb | 11083 | printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 11084 | } |
256df2f3 JP |
11085 | } |
11086 | ||
6ea754eb JP |
11087 | void netdev_printk(const char *level, const struct net_device *dev, |
11088 | const char *format, ...) | |
256df2f3 JP |
11089 | { |
11090 | struct va_format vaf; | |
11091 | va_list args; | |
256df2f3 JP |
11092 | |
11093 | va_start(args, format); | |
11094 | ||
11095 | vaf.fmt = format; | |
11096 | vaf.va = &args; | |
11097 | ||
6ea754eb | 11098 | __netdev_printk(level, dev, &vaf); |
b004ff49 | 11099 | |
256df2f3 | 11100 | va_end(args); |
256df2f3 JP |
11101 | } |
11102 | EXPORT_SYMBOL(netdev_printk); | |
11103 | ||
11104 | #define define_netdev_printk_level(func, level) \ | |
6ea754eb | 11105 | void func(const struct net_device *dev, const char *fmt, ...) \ |
256df2f3 | 11106 | { \ |
256df2f3 JP |
11107 | struct va_format vaf; \ |
11108 | va_list args; \ | |
11109 | \ | |
11110 | va_start(args, fmt); \ | |
11111 | \ | |
11112 | vaf.fmt = fmt; \ | |
11113 | vaf.va = &args; \ | |
11114 | \ | |
6ea754eb | 11115 | __netdev_printk(level, dev, &vaf); \ |
b004ff49 | 11116 | \ |
256df2f3 | 11117 | va_end(args); \ |
256df2f3 JP |
11118 | } \ |
11119 | EXPORT_SYMBOL(func); | |
11120 | ||
11121 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
11122 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
11123 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
11124 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
11125 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
11126 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
11127 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
11128 | ||
4665079c | 11129 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
11130 | { |
11131 | kfree(net->dev_name_head); | |
11132 | kfree(net->dev_index_head); | |
ee21b18b VA |
11133 | if (net != &init_net) |
11134 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); | |
881d966b EB |
11135 | } |
11136 | ||
022cbae6 | 11137 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
11138 | .init = netdev_init, |
11139 | .exit = netdev_exit, | |
11140 | }; | |
11141 | ||
4665079c | 11142 | static void __net_exit default_device_exit(struct net *net) |
ce286d32 | 11143 | { |
e008b5fc | 11144 | struct net_device *dev, *aux; |
ce286d32 | 11145 | /* |
e008b5fc | 11146 | * Push all migratable network devices back to the |
ce286d32 EB |
11147 | * initial network namespace |
11148 | */ | |
11149 | rtnl_lock(); | |
e008b5fc | 11150 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 11151 | int err; |
aca51397 | 11152 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
11153 | |
11154 | /* Ignore unmoveable devices (i.e. loopback) */ | |
11155 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
11156 | continue; | |
11157 | ||
e008b5fc EB |
11158 | /* Leave virtual devices for the generic cleanup */ |
11159 | if (dev->rtnl_link_ops) | |
11160 | continue; | |
d0c082ce | 11161 | |
25985edc | 11162 | /* Push remaining network devices to init_net */ |
aca51397 | 11163 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
55b40dbf JP |
11164 | if (__dev_get_by_name(&init_net, fb_name)) |
11165 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); | |
aca51397 | 11166 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
ce286d32 | 11167 | if (err) { |
7b6cd1ce JP |
11168 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
11169 | __func__, dev->name, err); | |
aca51397 | 11170 | BUG(); |
ce286d32 EB |
11171 | } |
11172 | } | |
11173 | rtnl_unlock(); | |
11174 | } | |
11175 | ||
50624c93 EB |
11176 | static void __net_exit rtnl_lock_unregistering(struct list_head *net_list) |
11177 | { | |
11178 | /* Return with the rtnl_lock held when there are no network | |
11179 | * devices unregistering in any network namespace in net_list. | |
11180 | */ | |
11181 | struct net *net; | |
11182 | bool unregistering; | |
ff960a73 | 11183 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
50624c93 | 11184 | |
ff960a73 | 11185 | add_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 | 11186 | for (;;) { |
50624c93 EB |
11187 | unregistering = false; |
11188 | rtnl_lock(); | |
11189 | list_for_each_entry(net, net_list, exit_list) { | |
11190 | if (net->dev_unreg_count > 0) { | |
11191 | unregistering = true; | |
11192 | break; | |
11193 | } | |
11194 | } | |
11195 | if (!unregistering) | |
11196 | break; | |
11197 | __rtnl_unlock(); | |
ff960a73 PZ |
11198 | |
11199 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
50624c93 | 11200 | } |
ff960a73 | 11201 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 EB |
11202 | } |
11203 | ||
04dc7f6b EB |
11204 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
11205 | { | |
11206 | /* At exit all network devices most be removed from a network | |
b595076a | 11207 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
11208 | * Do this across as many network namespaces as possible to |
11209 | * improve batching efficiency. | |
11210 | */ | |
11211 | struct net_device *dev; | |
11212 | struct net *net; | |
11213 | LIST_HEAD(dev_kill_list); | |
11214 | ||
50624c93 EB |
11215 | /* To prevent network device cleanup code from dereferencing |
11216 | * loopback devices or network devices that have been freed | |
11217 | * wait here for all pending unregistrations to complete, | |
11218 | * before unregistring the loopback device and allowing the | |
11219 | * network namespace be freed. | |
11220 | * | |
11221 | * The netdev todo list containing all network devices | |
11222 | * unregistrations that happen in default_device_exit_batch | |
11223 | * will run in the rtnl_unlock() at the end of | |
11224 | * default_device_exit_batch. | |
11225 | */ | |
11226 | rtnl_lock_unregistering(net_list); | |
04dc7f6b EB |
11227 | list_for_each_entry(net, net_list, exit_list) { |
11228 | for_each_netdev_reverse(net, dev) { | |
b0ab2fab | 11229 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
04dc7f6b EB |
11230 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
11231 | else | |
11232 | unregister_netdevice_queue(dev, &dev_kill_list); | |
11233 | } | |
11234 | } | |
11235 | unregister_netdevice_many(&dev_kill_list); | |
11236 | rtnl_unlock(); | |
11237 | } | |
11238 | ||
022cbae6 | 11239 | static struct pernet_operations __net_initdata default_device_ops = { |
ce286d32 | 11240 | .exit = default_device_exit, |
04dc7f6b | 11241 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
11242 | }; |
11243 | ||
1da177e4 LT |
11244 | /* |
11245 | * Initialize the DEV module. At boot time this walks the device list and | |
11246 | * unhooks any devices that fail to initialise (normally hardware not | |
11247 | * present) and leaves us with a valid list of present and active devices. | |
11248 | * | |
11249 | */ | |
11250 | ||
11251 | /* | |
11252 | * This is called single threaded during boot, so no need | |
11253 | * to take the rtnl semaphore. | |
11254 | */ | |
11255 | static int __init net_dev_init(void) | |
11256 | { | |
11257 | int i, rc = -ENOMEM; | |
11258 | ||
11259 | BUG_ON(!dev_boot_phase); | |
11260 | ||
1da177e4 LT |
11261 | if (dev_proc_init()) |
11262 | goto out; | |
11263 | ||
8b41d188 | 11264 | if (netdev_kobject_init()) |
1da177e4 LT |
11265 | goto out; |
11266 | ||
11267 | INIT_LIST_HEAD(&ptype_all); | |
82d8a867 | 11268 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
11269 | INIT_LIST_HEAD(&ptype_base[i]); |
11270 | ||
62532da9 VY |
11271 | INIT_LIST_HEAD(&offload_base); |
11272 | ||
881d966b EB |
11273 | if (register_pernet_subsys(&netdev_net_ops)) |
11274 | goto out; | |
1da177e4 LT |
11275 | |
11276 | /* | |
11277 | * Initialise the packet receive queues. | |
11278 | */ | |
11279 | ||
6f912042 | 11280 | for_each_possible_cpu(i) { |
41852497 | 11281 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
e36fa2f7 | 11282 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 11283 | |
41852497 ED |
11284 | INIT_WORK(flush, flush_backlog); |
11285 | ||
e36fa2f7 | 11286 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 11287 | skb_queue_head_init(&sd->process_queue); |
f53c7239 SK |
11288 | #ifdef CONFIG_XFRM_OFFLOAD |
11289 | skb_queue_head_init(&sd->xfrm_backlog); | |
11290 | #endif | |
e36fa2f7 | 11291 | INIT_LIST_HEAD(&sd->poll_list); |
a9cbd588 | 11292 | sd->output_queue_tailp = &sd->output_queue; |
df334545 | 11293 | #ifdef CONFIG_RPS |
545b8c8d | 11294 | INIT_CSD(&sd->csd, rps_trigger_softirq, sd); |
e36fa2f7 | 11295 | sd->cpu = i; |
1e94d72f | 11296 | #endif |
0a9627f2 | 11297 | |
7c4ec749 | 11298 | init_gro_hash(&sd->backlog); |
e36fa2f7 ED |
11299 | sd->backlog.poll = process_backlog; |
11300 | sd->backlog.weight = weight_p; | |
1da177e4 LT |
11301 | } |
11302 | ||
1da177e4 LT |
11303 | dev_boot_phase = 0; |
11304 | ||
505d4f73 EB |
11305 | /* The loopback device is special if any other network devices |
11306 | * is present in a network namespace the loopback device must | |
11307 | * be present. Since we now dynamically allocate and free the | |
11308 | * loopback device ensure this invariant is maintained by | |
11309 | * keeping the loopback device as the first device on the | |
11310 | * list of network devices. Ensuring the loopback devices | |
11311 | * is the first device that appears and the last network device | |
11312 | * that disappears. | |
11313 | */ | |
11314 | if (register_pernet_device(&loopback_net_ops)) | |
11315 | goto out; | |
11316 | ||
11317 | if (register_pernet_device(&default_device_ops)) | |
11318 | goto out; | |
11319 | ||
962cf36c CM |
11320 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
11321 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 | 11322 | |
f0bf90de SAS |
11323 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
11324 | NULL, dev_cpu_dead); | |
11325 | WARN_ON(rc < 0); | |
1da177e4 LT |
11326 | rc = 0; |
11327 | out: | |
11328 | return rc; | |
11329 | } | |
11330 | ||
11331 | subsys_initcall(net_dev_init); |