]>
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> |
1da177e4 LT |
82 | #include <linux/string.h> |
83 | #include <linux/mm.h> | |
84 | #include <linux/socket.h> | |
85 | #include <linux/sockios.h> | |
86 | #include <linux/errno.h> | |
87 | #include <linux/interrupt.h> | |
88 | #include <linux/if_ether.h> | |
89 | #include <linux/netdevice.h> | |
90 | #include <linux/etherdevice.h> | |
0187bdfb | 91 | #include <linux/ethtool.h> |
1da177e4 | 92 | #include <linux/skbuff.h> |
a7862b45 | 93 | #include <linux/bpf.h> |
b5cdae32 | 94 | #include <linux/bpf_trace.h> |
457c4cbc | 95 | #include <net/net_namespace.h> |
1da177e4 | 96 | #include <net/sock.h> |
02d62e86 | 97 | #include <net/busy_poll.h> |
1da177e4 | 98 | #include <linux/rtnetlink.h> |
1da177e4 | 99 | #include <linux/stat.h> |
1da177e4 | 100 | #include <net/dst.h> |
fc4099f1 | 101 | #include <net/dst_metadata.h> |
1da177e4 | 102 | #include <net/pkt_sched.h> |
87d83093 | 103 | #include <net/pkt_cls.h> |
1da177e4 | 104 | #include <net/checksum.h> |
44540960 | 105 | #include <net/xfrm.h> |
1da177e4 LT |
106 | #include <linux/highmem.h> |
107 | #include <linux/init.h> | |
1da177e4 | 108 | #include <linux/module.h> |
1da177e4 LT |
109 | #include <linux/netpoll.h> |
110 | #include <linux/rcupdate.h> | |
111 | #include <linux/delay.h> | |
1da177e4 | 112 | #include <net/iw_handler.h> |
1da177e4 | 113 | #include <asm/current.h> |
5bdb9886 | 114 | #include <linux/audit.h> |
db217334 | 115 | #include <linux/dmaengine.h> |
f6a78bfc | 116 | #include <linux/err.h> |
c7fa9d18 | 117 | #include <linux/ctype.h> |
723e98b7 | 118 | #include <linux/if_arp.h> |
6de329e2 | 119 | #include <linux/if_vlan.h> |
8f0f2223 | 120 | #include <linux/ip.h> |
ad55dcaf | 121 | #include <net/ip.h> |
25cd9ba0 | 122 | #include <net/mpls.h> |
8f0f2223 DM |
123 | #include <linux/ipv6.h> |
124 | #include <linux/in.h> | |
b6b2fed1 DM |
125 | #include <linux/jhash.h> |
126 | #include <linux/random.h> | |
9cbc1cb8 | 127 | #include <trace/events/napi.h> |
cf66ba58 | 128 | #include <trace/events/net.h> |
07dc22e7 | 129 | #include <trace/events/skb.h> |
caeda9b9 | 130 | #include <linux/inetdevice.h> |
c445477d | 131 | #include <linux/cpu_rmap.h> |
c5905afb | 132 | #include <linux/static_key.h> |
af12fa6e | 133 | #include <linux/hashtable.h> |
60877a32 | 134 | #include <linux/vmalloc.h> |
529d0489 | 135 | #include <linux/if_macvlan.h> |
e7fd2885 | 136 | #include <linux/errqueue.h> |
3b47d303 | 137 | #include <linux/hrtimer.h> |
e687ad60 | 138 | #include <linux/netfilter_ingress.h> |
40e4e713 | 139 | #include <linux/crash_dump.h> |
b72b5bf6 | 140 | #include <linux/sctp.h> |
ae847f40 | 141 | #include <net/udp_tunnel.h> |
6621dd29 | 142 | #include <linux/net_namespace.h> |
aaa5d90b | 143 | #include <linux/indirect_call_wrapper.h> |
af3836df | 144 | #include <net/devlink.h> |
1da177e4 | 145 | |
342709ef PE |
146 | #include "net-sysfs.h" |
147 | ||
d565b0a1 HX |
148 | #define MAX_GRO_SKBS 8 |
149 | ||
5d38a079 HX |
150 | /* This should be increased if a protocol with a bigger head is added. */ |
151 | #define GRO_MAX_HEAD (MAX_HEADER + 128) | |
152 | ||
1da177e4 | 153 | static DEFINE_SPINLOCK(ptype_lock); |
62532da9 | 154 | static DEFINE_SPINLOCK(offload_lock); |
900ff8c6 CW |
155 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
156 | struct list_head ptype_all __read_mostly; /* Taps */ | |
62532da9 | 157 | static struct list_head offload_base __read_mostly; |
1da177e4 | 158 | |
ae78dbfa | 159 | static int netif_rx_internal(struct sk_buff *skb); |
54951194 | 160 | static int call_netdevice_notifiers_info(unsigned long val, |
54951194 | 161 | struct netdev_notifier_info *info); |
26372605 PM |
162 | static int call_netdevice_notifiers_extack(unsigned long val, |
163 | struct net_device *dev, | |
164 | struct netlink_ext_ack *extack); | |
90b602f8 | 165 | static struct napi_struct *napi_by_id(unsigned int napi_id); |
ae78dbfa | 166 | |
1da177e4 | 167 | /* |
7562f876 | 168 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
1da177e4 LT |
169 | * semaphore. |
170 | * | |
c6d14c84 | 171 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
1da177e4 LT |
172 | * |
173 | * Writers must hold the rtnl semaphore while they loop through the | |
7562f876 | 174 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
1da177e4 LT |
175 | * actual updates. This allows pure readers to access the list even |
176 | * while a writer is preparing to update it. | |
177 | * | |
178 | * To put it another way, dev_base_lock is held for writing only to | |
179 | * protect against pure readers; the rtnl semaphore provides the | |
180 | * protection against other writers. | |
181 | * | |
182 | * See, for example usages, register_netdevice() and | |
183 | * unregister_netdevice(), which must be called with the rtnl | |
184 | * semaphore held. | |
185 | */ | |
1da177e4 | 186 | DEFINE_RWLOCK(dev_base_lock); |
1da177e4 LT |
187 | EXPORT_SYMBOL(dev_base_lock); |
188 | ||
6c557001 FW |
189 | static DEFINE_MUTEX(ifalias_mutex); |
190 | ||
af12fa6e ET |
191 | /* protects napi_hash addition/deletion and napi_gen_id */ |
192 | static DEFINE_SPINLOCK(napi_hash_lock); | |
193 | ||
52bd2d62 | 194 | static unsigned int napi_gen_id = NR_CPUS; |
6180d9de | 195 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
af12fa6e | 196 | |
18afa4b0 | 197 | static seqcount_t devnet_rename_seq; |
c91f6df2 | 198 | |
4e985ada TG |
199 | static inline void dev_base_seq_inc(struct net *net) |
200 | { | |
643aa9cb | 201 | while (++net->dev_base_seq == 0) |
202 | ; | |
4e985ada TG |
203 | } |
204 | ||
881d966b | 205 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 206 | { |
8387ff25 | 207 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
95c96174 | 208 | |
08e9897d | 209 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
210 | } |
211 | ||
881d966b | 212 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 213 | { |
7c28bd0b | 214 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
215 | } |
216 | ||
e36fa2f7 | 217 | static inline void rps_lock(struct softnet_data *sd) |
152102c7 CG |
218 | { |
219 | #ifdef CONFIG_RPS | |
e36fa2f7 | 220 | spin_lock(&sd->input_pkt_queue.lock); |
152102c7 CG |
221 | #endif |
222 | } | |
223 | ||
e36fa2f7 | 224 | static inline void rps_unlock(struct softnet_data *sd) |
152102c7 CG |
225 | { |
226 | #ifdef CONFIG_RPS | |
e36fa2f7 | 227 | spin_unlock(&sd->input_pkt_queue.lock); |
152102c7 CG |
228 | #endif |
229 | } | |
230 | ||
ff927412 JP |
231 | static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, |
232 | const char *name) | |
233 | { | |
234 | struct netdev_name_node *name_node; | |
235 | ||
236 | name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); | |
237 | if (!name_node) | |
238 | return NULL; | |
239 | INIT_HLIST_NODE(&name_node->hlist); | |
240 | name_node->dev = dev; | |
241 | name_node->name = name; | |
242 | return name_node; | |
243 | } | |
244 | ||
245 | static struct netdev_name_node * | |
246 | netdev_name_node_head_alloc(struct net_device *dev) | |
247 | { | |
248 | return netdev_name_node_alloc(dev, dev->name); | |
249 | } | |
250 | ||
251 | static void netdev_name_node_free(struct netdev_name_node *name_node) | |
252 | { | |
253 | kfree(name_node); | |
254 | } | |
255 | ||
256 | static void netdev_name_node_add(struct net *net, | |
257 | struct netdev_name_node *name_node) | |
258 | { | |
259 | hlist_add_head_rcu(&name_node->hlist, | |
260 | dev_name_hash(net, name_node->name)); | |
261 | } | |
262 | ||
263 | static void netdev_name_node_del(struct netdev_name_node *name_node) | |
264 | { | |
265 | hlist_del_rcu(&name_node->hlist); | |
266 | } | |
267 | ||
268 | static struct netdev_name_node *netdev_name_node_lookup(struct net *net, | |
269 | const char *name) | |
270 | { | |
271 | struct hlist_head *head = dev_name_hash(net, name); | |
272 | struct netdev_name_node *name_node; | |
273 | ||
274 | hlist_for_each_entry(name_node, head, hlist) | |
275 | if (!strcmp(name_node->name, name)) | |
276 | return name_node; | |
277 | return NULL; | |
278 | } | |
279 | ||
280 | static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, | |
281 | const char *name) | |
282 | { | |
283 | struct hlist_head *head = dev_name_hash(net, name); | |
284 | struct netdev_name_node *name_node; | |
285 | ||
286 | hlist_for_each_entry_rcu(name_node, head, hlist) | |
287 | if (!strcmp(name_node->name, name)) | |
288 | return name_node; | |
289 | return NULL; | |
290 | } | |
291 | ||
ce286d32 | 292 | /* Device list insertion */ |
53759be9 | 293 | static void list_netdevice(struct net_device *dev) |
ce286d32 | 294 | { |
c346dca1 | 295 | struct net *net = dev_net(dev); |
ce286d32 EB |
296 | |
297 | ASSERT_RTNL(); | |
298 | ||
299 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 300 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
ff927412 | 301 | netdev_name_node_add(net, dev->name_node); |
fb699dfd ED |
302 | hlist_add_head_rcu(&dev->index_hlist, |
303 | dev_index_hash(net, dev->ifindex)); | |
ce286d32 | 304 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
305 | |
306 | dev_base_seq_inc(net); | |
ce286d32 EB |
307 | } |
308 | ||
fb699dfd ED |
309 | /* Device list removal |
310 | * caller must respect a RCU grace period before freeing/reusing dev | |
311 | */ | |
ce286d32 EB |
312 | static void unlist_netdevice(struct net_device *dev) |
313 | { | |
314 | ASSERT_RTNL(); | |
315 | ||
316 | /* Unlink dev from the device chain */ | |
317 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 318 | list_del_rcu(&dev->dev_list); |
ff927412 | 319 | netdev_name_node_del(dev->name_node); |
fb699dfd | 320 | hlist_del_rcu(&dev->index_hlist); |
ce286d32 | 321 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
322 | |
323 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
324 | } |
325 | ||
1da177e4 LT |
326 | /* |
327 | * Our notifier list | |
328 | */ | |
329 | ||
f07d5b94 | 330 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
331 | |
332 | /* | |
333 | * Device drivers call our routines to queue packets here. We empty the | |
334 | * queue in the local softnet handler. | |
335 | */ | |
bea3348e | 336 | |
9958da05 | 337 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 338 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 339 | |
cf508b12 | 340 | #ifdef CONFIG_LOCKDEP |
723e98b7 | 341 | /* |
c773e847 | 342 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class |
723e98b7 JP |
343 | * according to dev->type |
344 | */ | |
643aa9cb | 345 | static const unsigned short netdev_lock_type[] = { |
346 | ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, | |
723e98b7 JP |
347 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, |
348 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, | |
349 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, | |
350 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, | |
351 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, | |
352 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, | |
353 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, | |
354 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, | |
355 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, | |
356 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, | |
357 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, | |
211ed865 PG |
358 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, |
359 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, | |
360 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; | |
723e98b7 | 361 | |
643aa9cb | 362 | static const char *const netdev_lock_name[] = { |
363 | "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", | |
364 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", | |
365 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", | |
366 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", | |
367 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", | |
368 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", | |
369 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", | |
370 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", | |
371 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", | |
372 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", | |
373 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", | |
374 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", | |
375 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", | |
376 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", | |
377 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; | |
723e98b7 JP |
378 | |
379 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; | |
cf508b12 | 380 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
723e98b7 JP |
381 | |
382 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) | |
383 | { | |
384 | int i; | |
385 | ||
386 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) | |
387 | if (netdev_lock_type[i] == dev_type) | |
388 | return i; | |
389 | /* the last key is used by default */ | |
390 | return ARRAY_SIZE(netdev_lock_type) - 1; | |
391 | } | |
392 | ||
cf508b12 DM |
393 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, |
394 | unsigned short dev_type) | |
723e98b7 JP |
395 | { |
396 | int i; | |
397 | ||
398 | i = netdev_lock_pos(dev_type); | |
399 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], | |
400 | netdev_lock_name[i]); | |
401 | } | |
cf508b12 DM |
402 | |
403 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
404 | { | |
405 | int i; | |
406 | ||
407 | i = netdev_lock_pos(dev->type); | |
408 | lockdep_set_class_and_name(&dev->addr_list_lock, | |
409 | &netdev_addr_lock_key[i], | |
410 | netdev_lock_name[i]); | |
411 | } | |
723e98b7 | 412 | #else |
cf508b12 DM |
413 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, |
414 | unsigned short dev_type) | |
415 | { | |
416 | } | |
417 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
723e98b7 JP |
418 | { |
419 | } | |
420 | #endif | |
1da177e4 LT |
421 | |
422 | /******************************************************************************* | |
eb13da1a | 423 | * |
424 | * Protocol management and registration routines | |
425 | * | |
426 | *******************************************************************************/ | |
1da177e4 | 427 | |
1da177e4 | 428 | |
1da177e4 LT |
429 | /* |
430 | * Add a protocol ID to the list. Now that the input handler is | |
431 | * smarter we can dispense with all the messy stuff that used to be | |
432 | * here. | |
433 | * | |
434 | * BEWARE!!! Protocol handlers, mangling input packets, | |
435 | * MUST BE last in hash buckets and checking protocol handlers | |
436 | * MUST start from promiscuous ptype_all chain in net_bh. | |
437 | * It is true now, do not change it. | |
438 | * Explanation follows: if protocol handler, mangling packet, will | |
439 | * be the first on list, it is not able to sense, that packet | |
440 | * is cloned and should be copied-on-write, so that it will | |
441 | * change it and subsequent readers will get broken packet. | |
442 | * --ANK (980803) | |
443 | */ | |
444 | ||
c07b68e8 ED |
445 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
446 | { | |
447 | if (pt->type == htons(ETH_P_ALL)) | |
7866a621 | 448 | return pt->dev ? &pt->dev->ptype_all : &ptype_all; |
c07b68e8 | 449 | else |
7866a621 SN |
450 | return pt->dev ? &pt->dev->ptype_specific : |
451 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
c07b68e8 ED |
452 | } |
453 | ||
1da177e4 LT |
454 | /** |
455 | * dev_add_pack - add packet handler | |
456 | * @pt: packet type declaration | |
457 | * | |
458 | * Add a protocol handler to the networking stack. The passed &packet_type | |
459 | * is linked into kernel lists and may not be freed until it has been | |
460 | * removed from the kernel lists. | |
461 | * | |
4ec93edb | 462 | * This call does not sleep therefore it can not |
1da177e4 LT |
463 | * guarantee all CPU's that are in middle of receiving packets |
464 | * will see the new packet type (until the next received packet). | |
465 | */ | |
466 | ||
467 | void dev_add_pack(struct packet_type *pt) | |
468 | { | |
c07b68e8 | 469 | struct list_head *head = ptype_head(pt); |
1da177e4 | 470 | |
c07b68e8 ED |
471 | spin_lock(&ptype_lock); |
472 | list_add_rcu(&pt->list, head); | |
473 | spin_unlock(&ptype_lock); | |
1da177e4 | 474 | } |
d1b19dff | 475 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 476 | |
1da177e4 LT |
477 | /** |
478 | * __dev_remove_pack - remove packet handler | |
479 | * @pt: packet type declaration | |
480 | * | |
481 | * Remove a protocol handler that was previously added to the kernel | |
482 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
483 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 484 | * returns. |
1da177e4 LT |
485 | * |
486 | * The packet type might still be in use by receivers | |
487 | * and must not be freed until after all the CPU's have gone | |
488 | * through a quiescent state. | |
489 | */ | |
490 | void __dev_remove_pack(struct packet_type *pt) | |
491 | { | |
c07b68e8 | 492 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
493 | struct packet_type *pt1; |
494 | ||
c07b68e8 | 495 | spin_lock(&ptype_lock); |
1da177e4 LT |
496 | |
497 | list_for_each_entry(pt1, head, list) { | |
498 | if (pt == pt1) { | |
499 | list_del_rcu(&pt->list); | |
500 | goto out; | |
501 | } | |
502 | } | |
503 | ||
7b6cd1ce | 504 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 505 | out: |
c07b68e8 | 506 | spin_unlock(&ptype_lock); |
1da177e4 | 507 | } |
d1b19dff ED |
508 | EXPORT_SYMBOL(__dev_remove_pack); |
509 | ||
1da177e4 LT |
510 | /** |
511 | * dev_remove_pack - remove packet handler | |
512 | * @pt: packet type declaration | |
513 | * | |
514 | * Remove a protocol handler that was previously added to the kernel | |
515 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
516 | * from the kernel lists and can be freed or reused once this function | |
517 | * returns. | |
518 | * | |
519 | * This call sleeps to guarantee that no CPU is looking at the packet | |
520 | * type after return. | |
521 | */ | |
522 | void dev_remove_pack(struct packet_type *pt) | |
523 | { | |
524 | __dev_remove_pack(pt); | |
4ec93edb | 525 | |
1da177e4 LT |
526 | synchronize_net(); |
527 | } | |
d1b19dff | 528 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 529 | |
62532da9 VY |
530 | |
531 | /** | |
532 | * dev_add_offload - register offload handlers | |
533 | * @po: protocol offload declaration | |
534 | * | |
535 | * Add protocol offload handlers to the networking stack. The passed | |
536 | * &proto_offload is linked into kernel lists and may not be freed until | |
537 | * it has been removed from the kernel lists. | |
538 | * | |
539 | * This call does not sleep therefore it can not | |
540 | * guarantee all CPU's that are in middle of receiving packets | |
541 | * will see the new offload handlers (until the next received packet). | |
542 | */ | |
543 | void dev_add_offload(struct packet_offload *po) | |
544 | { | |
bdef7de4 | 545 | struct packet_offload *elem; |
62532da9 VY |
546 | |
547 | spin_lock(&offload_lock); | |
bdef7de4 DM |
548 | list_for_each_entry(elem, &offload_base, list) { |
549 | if (po->priority < elem->priority) | |
550 | break; | |
551 | } | |
552 | list_add_rcu(&po->list, elem->list.prev); | |
62532da9 VY |
553 | spin_unlock(&offload_lock); |
554 | } | |
555 | EXPORT_SYMBOL(dev_add_offload); | |
556 | ||
557 | /** | |
558 | * __dev_remove_offload - remove offload handler | |
559 | * @po: packet offload declaration | |
560 | * | |
561 | * Remove a protocol offload handler that was previously added to the | |
562 | * kernel offload handlers by dev_add_offload(). The passed &offload_type | |
563 | * is removed from the kernel lists and can be freed or reused once this | |
564 | * function returns. | |
565 | * | |
566 | * The packet type might still be in use by receivers | |
567 | * and must not be freed until after all the CPU's have gone | |
568 | * through a quiescent state. | |
569 | */ | |
1d143d9f | 570 | static void __dev_remove_offload(struct packet_offload *po) |
62532da9 VY |
571 | { |
572 | struct list_head *head = &offload_base; | |
573 | struct packet_offload *po1; | |
574 | ||
c53aa505 | 575 | spin_lock(&offload_lock); |
62532da9 VY |
576 | |
577 | list_for_each_entry(po1, head, list) { | |
578 | if (po == po1) { | |
579 | list_del_rcu(&po->list); | |
580 | goto out; | |
581 | } | |
582 | } | |
583 | ||
584 | pr_warn("dev_remove_offload: %p not found\n", po); | |
585 | out: | |
c53aa505 | 586 | spin_unlock(&offload_lock); |
62532da9 | 587 | } |
62532da9 VY |
588 | |
589 | /** | |
590 | * dev_remove_offload - remove packet offload handler | |
591 | * @po: packet offload declaration | |
592 | * | |
593 | * Remove a packet offload handler that was previously added to the kernel | |
594 | * offload handlers by dev_add_offload(). The passed &offload_type is | |
595 | * removed from the kernel lists and can be freed or reused once this | |
596 | * function returns. | |
597 | * | |
598 | * This call sleeps to guarantee that no CPU is looking at the packet | |
599 | * type after return. | |
600 | */ | |
601 | void dev_remove_offload(struct packet_offload *po) | |
602 | { | |
603 | __dev_remove_offload(po); | |
604 | ||
605 | synchronize_net(); | |
606 | } | |
607 | EXPORT_SYMBOL(dev_remove_offload); | |
608 | ||
1da177e4 | 609 | /****************************************************************************** |
eb13da1a | 610 | * |
611 | * Device Boot-time Settings Routines | |
612 | * | |
613 | ******************************************************************************/ | |
1da177e4 LT |
614 | |
615 | /* Boot time configuration table */ | |
616 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; | |
617 | ||
618 | /** | |
619 | * netdev_boot_setup_add - add new setup entry | |
620 | * @name: name of the device | |
621 | * @map: configured settings for the device | |
622 | * | |
623 | * Adds new setup entry to the dev_boot_setup list. The function | |
624 | * returns 0 on error and 1 on success. This is a generic routine to | |
625 | * all netdevices. | |
626 | */ | |
627 | static int netdev_boot_setup_add(char *name, struct ifmap *map) | |
628 | { | |
629 | struct netdev_boot_setup *s; | |
630 | int i; | |
631 | ||
632 | s = dev_boot_setup; | |
633 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
634 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { | |
635 | memset(s[i].name, 0, sizeof(s[i].name)); | |
93b3cff9 | 636 | strlcpy(s[i].name, name, IFNAMSIZ); |
1da177e4 LT |
637 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
638 | break; | |
639 | } | |
640 | } | |
641 | ||
642 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; | |
643 | } | |
644 | ||
645 | /** | |
722c9a0c | 646 | * netdev_boot_setup_check - check boot time settings |
647 | * @dev: the netdevice | |
1da177e4 | 648 | * |
722c9a0c | 649 | * Check boot time settings for the device. |
650 | * The found settings are set for the device to be used | |
651 | * later in the device probing. | |
652 | * Returns 0 if no settings found, 1 if they are. | |
1da177e4 LT |
653 | */ |
654 | int netdev_boot_setup_check(struct net_device *dev) | |
655 | { | |
656 | struct netdev_boot_setup *s = dev_boot_setup; | |
657 | int i; | |
658 | ||
659 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
660 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && | |
93b3cff9 | 661 | !strcmp(dev->name, s[i].name)) { |
722c9a0c | 662 | dev->irq = s[i].map.irq; |
663 | dev->base_addr = s[i].map.base_addr; | |
664 | dev->mem_start = s[i].map.mem_start; | |
665 | dev->mem_end = s[i].map.mem_end; | |
1da177e4 LT |
666 | return 1; |
667 | } | |
668 | } | |
669 | return 0; | |
670 | } | |
d1b19dff | 671 | EXPORT_SYMBOL(netdev_boot_setup_check); |
1da177e4 LT |
672 | |
673 | ||
674 | /** | |
722c9a0c | 675 | * netdev_boot_base - get address from boot time settings |
676 | * @prefix: prefix for network device | |
677 | * @unit: id for network device | |
678 | * | |
679 | * Check boot time settings for the base address of device. | |
680 | * The found settings are set for the device to be used | |
681 | * later in the device probing. | |
682 | * Returns 0 if no settings found. | |
1da177e4 LT |
683 | */ |
684 | unsigned long netdev_boot_base(const char *prefix, int unit) | |
685 | { | |
686 | const struct netdev_boot_setup *s = dev_boot_setup; | |
687 | char name[IFNAMSIZ]; | |
688 | int i; | |
689 | ||
690 | sprintf(name, "%s%d", prefix, unit); | |
691 | ||
692 | /* | |
693 | * If device already registered then return base of 1 | |
694 | * to indicate not to probe for this interface | |
695 | */ | |
881d966b | 696 | if (__dev_get_by_name(&init_net, name)) |
1da177e4 LT |
697 | return 1; |
698 | ||
699 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) | |
700 | if (!strcmp(name, s[i].name)) | |
701 | return s[i].map.base_addr; | |
702 | return 0; | |
703 | } | |
704 | ||
705 | /* | |
706 | * Saves at boot time configured settings for any netdevice. | |
707 | */ | |
708 | int __init netdev_boot_setup(char *str) | |
709 | { | |
710 | int ints[5]; | |
711 | struct ifmap map; | |
712 | ||
713 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
714 | if (!str || !*str) | |
715 | return 0; | |
716 | ||
717 | /* Save settings */ | |
718 | memset(&map, 0, sizeof(map)); | |
719 | if (ints[0] > 0) | |
720 | map.irq = ints[1]; | |
721 | if (ints[0] > 1) | |
722 | map.base_addr = ints[2]; | |
723 | if (ints[0] > 2) | |
724 | map.mem_start = ints[3]; | |
725 | if (ints[0] > 3) | |
726 | map.mem_end = ints[4]; | |
727 | ||
728 | /* Add new entry to the list */ | |
729 | return netdev_boot_setup_add(str, &map); | |
730 | } | |
731 | ||
732 | __setup("netdev=", netdev_boot_setup); | |
733 | ||
734 | /******************************************************************************* | |
eb13da1a | 735 | * |
736 | * Device Interface Subroutines | |
737 | * | |
738 | *******************************************************************************/ | |
1da177e4 | 739 | |
a54acb3a ND |
740 | /** |
741 | * dev_get_iflink - get 'iflink' value of a interface | |
742 | * @dev: targeted interface | |
743 | * | |
744 | * Indicates the ifindex the interface is linked to. | |
745 | * Physical interfaces have the same 'ifindex' and 'iflink' values. | |
746 | */ | |
747 | ||
748 | int dev_get_iflink(const struct net_device *dev) | |
749 | { | |
750 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) | |
751 | return dev->netdev_ops->ndo_get_iflink(dev); | |
752 | ||
7a66bbc9 | 753 | return dev->ifindex; |
a54acb3a ND |
754 | } |
755 | EXPORT_SYMBOL(dev_get_iflink); | |
756 | ||
fc4099f1 PS |
757 | /** |
758 | * dev_fill_metadata_dst - Retrieve tunnel egress information. | |
759 | * @dev: targeted interface | |
760 | * @skb: The packet. | |
761 | * | |
762 | * For better visibility of tunnel traffic OVS needs to retrieve | |
763 | * egress tunnel information for a packet. Following API allows | |
764 | * user to get this info. | |
765 | */ | |
766 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) | |
767 | { | |
768 | struct ip_tunnel_info *info; | |
769 | ||
770 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) | |
771 | return -EINVAL; | |
772 | ||
773 | info = skb_tunnel_info_unclone(skb); | |
774 | if (!info) | |
775 | return -ENOMEM; | |
776 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) | |
777 | return -EINVAL; | |
778 | ||
779 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); | |
780 | } | |
781 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); | |
782 | ||
1da177e4 LT |
783 | /** |
784 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 785 | * @net: the applicable net namespace |
1da177e4 LT |
786 | * @name: name to find |
787 | * | |
788 | * Find an interface by name. Must be called under RTNL semaphore | |
789 | * or @dev_base_lock. If the name is found a pointer to the device | |
790 | * is returned. If the name is not found then %NULL is returned. The | |
791 | * reference counters are not incremented so the caller must be | |
792 | * careful with locks. | |
793 | */ | |
794 | ||
881d966b | 795 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 | 796 | { |
ff927412 | 797 | struct netdev_name_node *node_name; |
1da177e4 | 798 | |
ff927412 JP |
799 | node_name = netdev_name_node_lookup(net, name); |
800 | return node_name ? node_name->dev : NULL; | |
1da177e4 | 801 | } |
d1b19dff | 802 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 803 | |
72c9528b | 804 | /** |
722c9a0c | 805 | * dev_get_by_name_rcu - find a device by its name |
806 | * @net: the applicable net namespace | |
807 | * @name: name to find | |
808 | * | |
809 | * Find an interface by name. | |
810 | * If the name is found a pointer to the device is returned. | |
811 | * If the name is not found then %NULL is returned. | |
812 | * The reference counters are not incremented so the caller must be | |
813 | * careful with locks. The caller must hold RCU lock. | |
72c9528b ED |
814 | */ |
815 | ||
816 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
817 | { | |
ff927412 | 818 | struct netdev_name_node *node_name; |
72c9528b | 819 | |
ff927412 JP |
820 | node_name = netdev_name_node_lookup_rcu(net, name); |
821 | return node_name ? node_name->dev : NULL; | |
72c9528b ED |
822 | } |
823 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
824 | ||
1da177e4 LT |
825 | /** |
826 | * dev_get_by_name - find a device by its name | |
c4ea43c5 | 827 | * @net: the applicable net namespace |
1da177e4 LT |
828 | * @name: name to find |
829 | * | |
830 | * Find an interface by name. This can be called from any | |
831 | * context and does its own locking. The returned handle has | |
832 | * the usage count incremented and the caller must use dev_put() to | |
833 | * release it when it is no longer needed. %NULL is returned if no | |
834 | * matching device is found. | |
835 | */ | |
836 | ||
881d966b | 837 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
838 | { |
839 | struct net_device *dev; | |
840 | ||
72c9528b ED |
841 | rcu_read_lock(); |
842 | dev = dev_get_by_name_rcu(net, name); | |
1da177e4 LT |
843 | if (dev) |
844 | dev_hold(dev); | |
72c9528b | 845 | rcu_read_unlock(); |
1da177e4 LT |
846 | return dev; |
847 | } | |
d1b19dff | 848 | EXPORT_SYMBOL(dev_get_by_name); |
1da177e4 LT |
849 | |
850 | /** | |
851 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 852 | * @net: the applicable net namespace |
1da177e4 LT |
853 | * @ifindex: index of device |
854 | * | |
855 | * Search for an interface by index. Returns %NULL if the device | |
856 | * is not found or a pointer to the device. The device has not | |
857 | * had its reference counter increased so the caller must be careful | |
858 | * about locking. The caller must hold either the RTNL semaphore | |
859 | * or @dev_base_lock. | |
860 | */ | |
861 | ||
881d966b | 862 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 | 863 | { |
0bd8d536 ED |
864 | struct net_device *dev; |
865 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 866 | |
b67bfe0d | 867 | hlist_for_each_entry(dev, head, index_hlist) |
1da177e4 LT |
868 | if (dev->ifindex == ifindex) |
869 | return dev; | |
0bd8d536 | 870 | |
1da177e4 LT |
871 | return NULL; |
872 | } | |
d1b19dff | 873 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 874 | |
fb699dfd ED |
875 | /** |
876 | * dev_get_by_index_rcu - find a device by its ifindex | |
877 | * @net: the applicable net namespace | |
878 | * @ifindex: index of device | |
879 | * | |
880 | * Search for an interface by index. Returns %NULL if the device | |
881 | * is not found or a pointer to the device. The device has not | |
882 | * had its reference counter increased so the caller must be careful | |
883 | * about locking. The caller must hold RCU lock. | |
884 | */ | |
885 | ||
886 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
887 | { | |
fb699dfd ED |
888 | struct net_device *dev; |
889 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
890 | ||
b67bfe0d | 891 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
fb699dfd ED |
892 | if (dev->ifindex == ifindex) |
893 | return dev; | |
894 | ||
895 | return NULL; | |
896 | } | |
897 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
898 | ||
1da177e4 LT |
899 | |
900 | /** | |
901 | * dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 902 | * @net: the applicable net namespace |
1da177e4 LT |
903 | * @ifindex: index of device |
904 | * | |
905 | * Search for an interface by index. Returns NULL if the device | |
906 | * is not found or a pointer to the device. The device returned has | |
907 | * had a reference added and the pointer is safe until the user calls | |
908 | * dev_put to indicate they have finished with it. | |
909 | */ | |
910 | ||
881d966b | 911 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
912 | { |
913 | struct net_device *dev; | |
914 | ||
fb699dfd ED |
915 | rcu_read_lock(); |
916 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 LT |
917 | if (dev) |
918 | dev_hold(dev); | |
fb699dfd | 919 | rcu_read_unlock(); |
1da177e4 LT |
920 | return dev; |
921 | } | |
d1b19dff | 922 | EXPORT_SYMBOL(dev_get_by_index); |
1da177e4 | 923 | |
90b602f8 ML |
924 | /** |
925 | * dev_get_by_napi_id - find a device by napi_id | |
926 | * @napi_id: ID of the NAPI struct | |
927 | * | |
928 | * Search for an interface by NAPI ID. Returns %NULL if the device | |
929 | * is not found or a pointer to the device. The device has not had | |
930 | * its reference counter increased so the caller must be careful | |
931 | * about locking. The caller must hold RCU lock. | |
932 | */ | |
933 | ||
934 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) | |
935 | { | |
936 | struct napi_struct *napi; | |
937 | ||
938 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
939 | ||
940 | if (napi_id < MIN_NAPI_ID) | |
941 | return NULL; | |
942 | ||
943 | napi = napi_by_id(napi_id); | |
944 | ||
945 | return napi ? napi->dev : NULL; | |
946 | } | |
947 | EXPORT_SYMBOL(dev_get_by_napi_id); | |
948 | ||
5dbe7c17 NS |
949 | /** |
950 | * netdev_get_name - get a netdevice name, knowing its ifindex. | |
951 | * @net: network namespace | |
952 | * @name: a pointer to the buffer where the name will be stored. | |
953 | * @ifindex: the ifindex of the interface to get the name from. | |
954 | * | |
955 | * The use of raw_seqcount_begin() and cond_resched() before | |
956 | * retrying is required as we want to give the writers a chance | |
957 | * to complete when CONFIG_PREEMPT is not set. | |
958 | */ | |
959 | int netdev_get_name(struct net *net, char *name, int ifindex) | |
960 | { | |
961 | struct net_device *dev; | |
962 | unsigned int seq; | |
963 | ||
964 | retry: | |
965 | seq = raw_seqcount_begin(&devnet_rename_seq); | |
966 | rcu_read_lock(); | |
967 | dev = dev_get_by_index_rcu(net, ifindex); | |
968 | if (!dev) { | |
969 | rcu_read_unlock(); | |
970 | return -ENODEV; | |
971 | } | |
972 | ||
973 | strcpy(name, dev->name); | |
974 | rcu_read_unlock(); | |
975 | if (read_seqcount_retry(&devnet_rename_seq, seq)) { | |
976 | cond_resched(); | |
977 | goto retry; | |
978 | } | |
979 | ||
980 | return 0; | |
981 | } | |
982 | ||
1da177e4 | 983 | /** |
941666c2 | 984 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 985 | * @net: the applicable net namespace |
1da177e4 LT |
986 | * @type: media type of device |
987 | * @ha: hardware address | |
988 | * | |
989 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
990 | * is not found or a pointer to the device. |
991 | * The caller must hold RCU or RTNL. | |
941666c2 | 992 | * The returned device has not had its ref count increased |
1da177e4 LT |
993 | * and the caller must therefore be careful about locking |
994 | * | |
1da177e4 LT |
995 | */ |
996 | ||
941666c2 ED |
997 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
998 | const char *ha) | |
1da177e4 LT |
999 | { |
1000 | struct net_device *dev; | |
1001 | ||
941666c2 | 1002 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
1003 | if (dev->type == type && |
1004 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
1005 | return dev; |
1006 | ||
1007 | return NULL; | |
1da177e4 | 1008 | } |
941666c2 | 1009 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 1010 | |
881d966b | 1011 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
1da177e4 LT |
1012 | { |
1013 | struct net_device *dev; | |
1014 | ||
4e9cac2b | 1015 | ASSERT_RTNL(); |
881d966b | 1016 | for_each_netdev(net, dev) |
4e9cac2b | 1017 | if (dev->type == type) |
7562f876 PE |
1018 | return dev; |
1019 | ||
1020 | return NULL; | |
4e9cac2b | 1021 | } |
4e9cac2b PM |
1022 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
1023 | ||
881d966b | 1024 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 1025 | { |
99fe3c39 | 1026 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 1027 | |
99fe3c39 ED |
1028 | rcu_read_lock(); |
1029 | for_each_netdev_rcu(net, dev) | |
1030 | if (dev->type == type) { | |
1031 | dev_hold(dev); | |
1032 | ret = dev; | |
1033 | break; | |
1034 | } | |
1035 | rcu_read_unlock(); | |
1036 | return ret; | |
1da177e4 | 1037 | } |
1da177e4 LT |
1038 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
1039 | ||
1040 | /** | |
6c555490 | 1041 | * __dev_get_by_flags - find any device with given flags |
c4ea43c5 | 1042 | * @net: the applicable net namespace |
1da177e4 LT |
1043 | * @if_flags: IFF_* values |
1044 | * @mask: bitmask of bits in if_flags to check | |
1045 | * | |
1046 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 | 1047 | * is not found or a pointer to the device. Must be called inside |
6c555490 | 1048 | * rtnl_lock(), and result refcount is unchanged. |
1da177e4 LT |
1049 | */ |
1050 | ||
6c555490 WC |
1051 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
1052 | unsigned short mask) | |
1da177e4 | 1053 | { |
7562f876 | 1054 | struct net_device *dev, *ret; |
1da177e4 | 1055 | |
6c555490 WC |
1056 | ASSERT_RTNL(); |
1057 | ||
7562f876 | 1058 | ret = NULL; |
6c555490 | 1059 | for_each_netdev(net, dev) { |
1da177e4 | 1060 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 1061 | ret = dev; |
1da177e4 LT |
1062 | break; |
1063 | } | |
1064 | } | |
7562f876 | 1065 | return ret; |
1da177e4 | 1066 | } |
6c555490 | 1067 | EXPORT_SYMBOL(__dev_get_by_flags); |
1da177e4 LT |
1068 | |
1069 | /** | |
1070 | * dev_valid_name - check if name is okay for network device | |
1071 | * @name: name string | |
1072 | * | |
1073 | * Network device names need to be valid file names to | |
c7fa9d18 DM |
1074 | * to allow sysfs to work. We also disallow any kind of |
1075 | * whitespace. | |
1da177e4 | 1076 | */ |
95f050bf | 1077 | bool dev_valid_name(const char *name) |
1da177e4 | 1078 | { |
c7fa9d18 | 1079 | if (*name == '\0') |
95f050bf | 1080 | return false; |
a9d48205 | 1081 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
95f050bf | 1082 | return false; |
c7fa9d18 | 1083 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 1084 | return false; |
c7fa9d18 DM |
1085 | |
1086 | while (*name) { | |
a4176a93 | 1087 | if (*name == '/' || *name == ':' || isspace(*name)) |
95f050bf | 1088 | return false; |
c7fa9d18 DM |
1089 | name++; |
1090 | } | |
95f050bf | 1091 | return true; |
1da177e4 | 1092 | } |
d1b19dff | 1093 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
1094 | |
1095 | /** | |
b267b179 EB |
1096 | * __dev_alloc_name - allocate a name for a device |
1097 | * @net: network namespace to allocate the device name in | |
1da177e4 | 1098 | * @name: name format string |
b267b179 | 1099 | * @buf: scratch buffer and result name string |
1da177e4 LT |
1100 | * |
1101 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
1102 | * id. It scans list of devices to build up a free map, then chooses |
1103 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1104 | * while allocating the name and adding the device in order to avoid | |
1105 | * duplicates. | |
1106 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1107 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
1108 | */ |
1109 | ||
b267b179 | 1110 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
1da177e4 LT |
1111 | { |
1112 | int i = 0; | |
1da177e4 LT |
1113 | const char *p; |
1114 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 1115 | unsigned long *inuse; |
1da177e4 LT |
1116 | struct net_device *d; |
1117 | ||
93809105 RV |
1118 | if (!dev_valid_name(name)) |
1119 | return -EINVAL; | |
1120 | ||
51f299dd | 1121 | p = strchr(name, '%'); |
1da177e4 LT |
1122 | if (p) { |
1123 | /* | |
1124 | * Verify the string as this thing may have come from | |
1125 | * the user. There must be either one "%d" and no other "%" | |
1126 | * characters. | |
1127 | */ | |
1128 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
1129 | return -EINVAL; | |
1130 | ||
1131 | /* Use one page as a bit array of possible slots */ | |
cfcabdcc | 1132 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
1133 | if (!inuse) |
1134 | return -ENOMEM; | |
1135 | ||
881d966b | 1136 | for_each_netdev(net, d) { |
1da177e4 LT |
1137 | if (!sscanf(d->name, name, &i)) |
1138 | continue; | |
1139 | if (i < 0 || i >= max_netdevices) | |
1140 | continue; | |
1141 | ||
1142 | /* avoid cases where sscanf is not exact inverse of printf */ | |
b267b179 | 1143 | snprintf(buf, IFNAMSIZ, name, i); |
1da177e4 LT |
1144 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
1145 | set_bit(i, inuse); | |
1146 | } | |
1147 | ||
1148 | i = find_first_zero_bit(inuse, max_netdevices); | |
1149 | free_page((unsigned long) inuse); | |
1150 | } | |
1151 | ||
6224abda | 1152 | snprintf(buf, IFNAMSIZ, name, i); |
b267b179 | 1153 | if (!__dev_get_by_name(net, buf)) |
1da177e4 | 1154 | return i; |
1da177e4 LT |
1155 | |
1156 | /* It is possible to run out of possible slots | |
1157 | * when the name is long and there isn't enough space left | |
1158 | * for the digits, or if all bits are used. | |
1159 | */ | |
029b6d14 | 1160 | return -ENFILE; |
1da177e4 LT |
1161 | } |
1162 | ||
2c88b855 RV |
1163 | static int dev_alloc_name_ns(struct net *net, |
1164 | struct net_device *dev, | |
1165 | const char *name) | |
1166 | { | |
1167 | char buf[IFNAMSIZ]; | |
1168 | int ret; | |
1169 | ||
c46d7642 | 1170 | BUG_ON(!net); |
2c88b855 RV |
1171 | ret = __dev_alloc_name(net, name, buf); |
1172 | if (ret >= 0) | |
1173 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1174 | return ret; | |
1da177e4 LT |
1175 | } |
1176 | ||
b267b179 EB |
1177 | /** |
1178 | * dev_alloc_name - allocate a name for a device | |
1179 | * @dev: device | |
1180 | * @name: name format string | |
1181 | * | |
1182 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1183 | * id. It scans list of devices to build up a free map, then chooses | |
1184 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1185 | * while allocating the name and adding the device in order to avoid | |
1186 | * duplicates. | |
1187 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1188 | * Returns the number of the unit assigned or a negative errno code. | |
1189 | */ | |
1190 | ||
1191 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1192 | { | |
c46d7642 | 1193 | return dev_alloc_name_ns(dev_net(dev), dev, name); |
b267b179 | 1194 | } |
d1b19dff | 1195 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1196 | |
0ad646c8 CW |
1197 | int dev_get_valid_name(struct net *net, struct net_device *dev, |
1198 | const char *name) | |
828de4f6 | 1199 | { |
55a5ec9b DM |
1200 | BUG_ON(!net); |
1201 | ||
1202 | if (!dev_valid_name(name)) | |
1203 | return -EINVAL; | |
1204 | ||
1205 | if (strchr(name, '%')) | |
1206 | return dev_alloc_name_ns(net, dev, name); | |
1207 | else if (__dev_get_by_name(net, name)) | |
1208 | return -EEXIST; | |
1209 | else if (dev->name != name) | |
1210 | strlcpy(dev->name, name, IFNAMSIZ); | |
1211 | ||
1212 | return 0; | |
d9031024 | 1213 | } |
0ad646c8 | 1214 | EXPORT_SYMBOL(dev_get_valid_name); |
1da177e4 LT |
1215 | |
1216 | /** | |
1217 | * dev_change_name - change name of a device | |
1218 | * @dev: device | |
1219 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1220 | * | |
1221 | * Change name of a device, can pass format strings "eth%d". | |
1222 | * for wildcarding. | |
1223 | */ | |
cf04a4c7 | 1224 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1225 | { |
238fa362 | 1226 | unsigned char old_assign_type; |
fcc5a03a | 1227 | char oldname[IFNAMSIZ]; |
1da177e4 | 1228 | int err = 0; |
fcc5a03a | 1229 | int ret; |
881d966b | 1230 | struct net *net; |
1da177e4 LT |
1231 | |
1232 | ASSERT_RTNL(); | |
c346dca1 | 1233 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1234 | |
c346dca1 | 1235 | net = dev_net(dev); |
8065a779 SWL |
1236 | |
1237 | /* Some auto-enslaved devices e.g. failover slaves are | |
1238 | * special, as userspace might rename the device after | |
1239 | * the interface had been brought up and running since | |
1240 | * the point kernel initiated auto-enslavement. Allow | |
1241 | * live name change even when these slave devices are | |
1242 | * up and running. | |
1243 | * | |
1244 | * Typically, users of these auto-enslaving devices | |
1245 | * don't actually care about slave name change, as | |
1246 | * they are supposed to operate on master interface | |
1247 | * directly. | |
1248 | */ | |
1249 | if (dev->flags & IFF_UP && | |
1250 | likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK))) | |
1da177e4 LT |
1251 | return -EBUSY; |
1252 | ||
30e6c9fa | 1253 | write_seqcount_begin(&devnet_rename_seq); |
c91f6df2 BH |
1254 | |
1255 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
30e6c9fa | 1256 | write_seqcount_end(&devnet_rename_seq); |
c8d90dca | 1257 | return 0; |
c91f6df2 | 1258 | } |
c8d90dca | 1259 | |
fcc5a03a HX |
1260 | memcpy(oldname, dev->name, IFNAMSIZ); |
1261 | ||
828de4f6 | 1262 | err = dev_get_valid_name(net, dev, newname); |
c91f6df2 | 1263 | if (err < 0) { |
30e6c9fa | 1264 | write_seqcount_end(&devnet_rename_seq); |
d9031024 | 1265 | return err; |
c91f6df2 | 1266 | } |
1da177e4 | 1267 | |
6fe82a39 VF |
1268 | if (oldname[0] && !strchr(oldname, '%')) |
1269 | netdev_info(dev, "renamed from %s\n", oldname); | |
1270 | ||
238fa362 TG |
1271 | old_assign_type = dev->name_assign_type; |
1272 | dev->name_assign_type = NET_NAME_RENAMED; | |
1273 | ||
fcc5a03a | 1274 | rollback: |
a1b3f594 EB |
1275 | ret = device_rename(&dev->dev, dev->name); |
1276 | if (ret) { | |
1277 | memcpy(dev->name, oldname, IFNAMSIZ); | |
238fa362 | 1278 | dev->name_assign_type = old_assign_type; |
30e6c9fa | 1279 | write_seqcount_end(&devnet_rename_seq); |
a1b3f594 | 1280 | return ret; |
dcc99773 | 1281 | } |
7f988eab | 1282 | |
30e6c9fa | 1283 | write_seqcount_end(&devnet_rename_seq); |
c91f6df2 | 1284 | |
5bb025fa VF |
1285 | netdev_adjacent_rename_links(dev, oldname); |
1286 | ||
7f988eab | 1287 | write_lock_bh(&dev_base_lock); |
ff927412 | 1288 | netdev_name_node_del(dev->name_node); |
72c9528b ED |
1289 | write_unlock_bh(&dev_base_lock); |
1290 | ||
1291 | synchronize_rcu(); | |
1292 | ||
1293 | write_lock_bh(&dev_base_lock); | |
ff927412 | 1294 | netdev_name_node_add(net, dev->name_node); |
7f988eab HX |
1295 | write_unlock_bh(&dev_base_lock); |
1296 | ||
056925ab | 1297 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1298 | ret = notifier_to_errno(ret); |
1299 | ||
1300 | if (ret) { | |
91e9c07b ED |
1301 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1302 | if (err >= 0) { | |
fcc5a03a | 1303 | err = ret; |
30e6c9fa | 1304 | write_seqcount_begin(&devnet_rename_seq); |
fcc5a03a | 1305 | memcpy(dev->name, oldname, IFNAMSIZ); |
5bb025fa | 1306 | memcpy(oldname, newname, IFNAMSIZ); |
238fa362 TG |
1307 | dev->name_assign_type = old_assign_type; |
1308 | old_assign_type = NET_NAME_RENAMED; | |
fcc5a03a | 1309 | goto rollback; |
91e9c07b | 1310 | } else { |
7b6cd1ce | 1311 | pr_err("%s: name change rollback failed: %d\n", |
91e9c07b | 1312 | dev->name, ret); |
fcc5a03a HX |
1313 | } |
1314 | } | |
1da177e4 LT |
1315 | |
1316 | return err; | |
1317 | } | |
1318 | ||
0b815a1a SH |
1319 | /** |
1320 | * dev_set_alias - change ifalias of a device | |
1321 | * @dev: device | |
1322 | * @alias: name up to IFALIASZ | |
f0db275a | 1323 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1324 | * |
1325 | * Set ifalias for a device, | |
1326 | */ | |
1327 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1328 | { | |
6c557001 | 1329 | struct dev_ifalias *new_alias = NULL; |
0b815a1a SH |
1330 | |
1331 | if (len >= IFALIASZ) | |
1332 | return -EINVAL; | |
1333 | ||
6c557001 FW |
1334 | if (len) { |
1335 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); | |
1336 | if (!new_alias) | |
1337 | return -ENOMEM; | |
1338 | ||
1339 | memcpy(new_alias->ifalias, alias, len); | |
1340 | new_alias->ifalias[len] = 0; | |
96ca4a2c OH |
1341 | } |
1342 | ||
6c557001 FW |
1343 | mutex_lock(&ifalias_mutex); |
1344 | rcu_swap_protected(dev->ifalias, new_alias, | |
1345 | mutex_is_locked(&ifalias_mutex)); | |
1346 | mutex_unlock(&ifalias_mutex); | |
1347 | ||
1348 | if (new_alias) | |
1349 | kfree_rcu(new_alias, rcuhead); | |
0b815a1a | 1350 | |
0b815a1a SH |
1351 | return len; |
1352 | } | |
0fe554a4 | 1353 | EXPORT_SYMBOL(dev_set_alias); |
0b815a1a | 1354 | |
6c557001 FW |
1355 | /** |
1356 | * dev_get_alias - get ifalias of a device | |
1357 | * @dev: device | |
20e88320 | 1358 | * @name: buffer to store name of ifalias |
6c557001 FW |
1359 | * @len: size of buffer |
1360 | * | |
1361 | * get ifalias for a device. Caller must make sure dev cannot go | |
1362 | * away, e.g. rcu read lock or own a reference count to device. | |
1363 | */ | |
1364 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) | |
1365 | { | |
1366 | const struct dev_ifalias *alias; | |
1367 | int ret = 0; | |
1368 | ||
1369 | rcu_read_lock(); | |
1370 | alias = rcu_dereference(dev->ifalias); | |
1371 | if (alias) | |
1372 | ret = snprintf(name, len, "%s", alias->ifalias); | |
1373 | rcu_read_unlock(); | |
1374 | ||
1375 | return ret; | |
1376 | } | |
0b815a1a | 1377 | |
d8a33ac4 | 1378 | /** |
3041a069 | 1379 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1380 | * @dev: device to cause notification |
1381 | * | |
1382 | * Called to indicate a device has changed features. | |
1383 | */ | |
1384 | void netdev_features_change(struct net_device *dev) | |
1385 | { | |
056925ab | 1386 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1387 | } |
1388 | EXPORT_SYMBOL(netdev_features_change); | |
1389 | ||
1da177e4 LT |
1390 | /** |
1391 | * netdev_state_change - device changes state | |
1392 | * @dev: device to cause notification | |
1393 | * | |
1394 | * Called to indicate a device has changed state. This function calls | |
1395 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1396 | * to the routing socket. | |
1397 | */ | |
1398 | void netdev_state_change(struct net_device *dev) | |
1399 | { | |
1400 | if (dev->flags & IFF_UP) { | |
51d0c047 DA |
1401 | struct netdev_notifier_change_info change_info = { |
1402 | .info.dev = dev, | |
1403 | }; | |
54951194 | 1404 | |
51d0c047 | 1405 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
54951194 | 1406 | &change_info.info); |
7f294054 | 1407 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
1da177e4 LT |
1408 | } |
1409 | } | |
d1b19dff | 1410 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1411 | |
ee89bab1 | 1412 | /** |
722c9a0c | 1413 | * netdev_notify_peers - notify network peers about existence of @dev |
1414 | * @dev: network device | |
ee89bab1 AW |
1415 | * |
1416 | * Generate traffic such that interested network peers are aware of | |
1417 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1418 | * a device wants to inform the rest of the network about some sort of | |
1419 | * reconfiguration such as a failover event or virtual machine | |
1420 | * migration. | |
1421 | */ | |
1422 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1423 | { |
ee89bab1 AW |
1424 | rtnl_lock(); |
1425 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
37c343b4 | 1426 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); |
ee89bab1 | 1427 | rtnl_unlock(); |
c1da4ac7 | 1428 | } |
ee89bab1 | 1429 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1430 | |
40c900aa | 1431 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
1da177e4 | 1432 | { |
d314774c | 1433 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1434 | int ret; |
1da177e4 | 1435 | |
e46b66bc BH |
1436 | ASSERT_RTNL(); |
1437 | ||
1da177e4 LT |
1438 | if (!netif_device_present(dev)) |
1439 | return -ENODEV; | |
1440 | ||
ca99ca14 NH |
1441 | /* Block netpoll from trying to do any rx path servicing. |
1442 | * If we don't do this there is a chance ndo_poll_controller | |
1443 | * or ndo_poll may be running while we open the device | |
1444 | */ | |
66b5552f | 1445 | netpoll_poll_disable(dev); |
ca99ca14 | 1446 | |
40c900aa | 1447 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
3b8bcfd5 JB |
1448 | ret = notifier_to_errno(ret); |
1449 | if (ret) | |
1450 | return ret; | |
1451 | ||
1da177e4 | 1452 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1453 | |
d314774c SH |
1454 | if (ops->ndo_validate_addr) |
1455 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1456 | |
d314774c SH |
1457 | if (!ret && ops->ndo_open) |
1458 | ret = ops->ndo_open(dev); | |
1da177e4 | 1459 | |
66b5552f | 1460 | netpoll_poll_enable(dev); |
ca99ca14 | 1461 | |
bada339b JG |
1462 | if (ret) |
1463 | clear_bit(__LINK_STATE_START, &dev->state); | |
1464 | else { | |
1da177e4 | 1465 | dev->flags |= IFF_UP; |
4417da66 | 1466 | dev_set_rx_mode(dev); |
1da177e4 | 1467 | dev_activate(dev); |
7bf23575 | 1468 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1469 | } |
bada339b | 1470 | |
1da177e4 LT |
1471 | return ret; |
1472 | } | |
1473 | ||
1474 | /** | |
bd380811 | 1475 | * dev_open - prepare an interface for use. |
00f54e68 PM |
1476 | * @dev: device to open |
1477 | * @extack: netlink extended ack | |
1da177e4 | 1478 | * |
bd380811 PM |
1479 | * Takes a device from down to up state. The device's private open |
1480 | * function is invoked and then the multicast lists are loaded. Finally | |
1481 | * the device is moved into the up state and a %NETDEV_UP message is | |
1482 | * sent to the netdev notifier chain. | |
1483 | * | |
1484 | * Calling this function on an active interface is a nop. On a failure | |
1485 | * a negative errno code is returned. | |
1da177e4 | 1486 | */ |
00f54e68 | 1487 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
bd380811 PM |
1488 | { |
1489 | int ret; | |
1490 | ||
bd380811 PM |
1491 | if (dev->flags & IFF_UP) |
1492 | return 0; | |
1493 | ||
40c900aa | 1494 | ret = __dev_open(dev, extack); |
bd380811 PM |
1495 | if (ret < 0) |
1496 | return ret; | |
1497 | ||
7f294054 | 1498 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
bd380811 PM |
1499 | call_netdevice_notifiers(NETDEV_UP, dev); |
1500 | ||
1501 | return ret; | |
1502 | } | |
1503 | EXPORT_SYMBOL(dev_open); | |
1504 | ||
7051b88a | 1505 | static void __dev_close_many(struct list_head *head) |
1da177e4 | 1506 | { |
44345724 | 1507 | struct net_device *dev; |
e46b66bc | 1508 | |
bd380811 | 1509 | ASSERT_RTNL(); |
9d5010db DM |
1510 | might_sleep(); |
1511 | ||
5cde2829 | 1512 | list_for_each_entry(dev, head, close_list) { |
3f4df206 | 1513 | /* Temporarily disable netpoll until the interface is down */ |
66b5552f | 1514 | netpoll_poll_disable(dev); |
3f4df206 | 1515 | |
44345724 | 1516 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1517 | |
44345724 | 1518 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1519 | |
44345724 OP |
1520 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1521 | * can be even on different cpu. So just clear netif_running(). | |
1522 | * | |
1523 | * dev->stop() will invoke napi_disable() on all of it's | |
1524 | * napi_struct instances on this device. | |
1525 | */ | |
4e857c58 | 1526 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
44345724 | 1527 | } |
1da177e4 | 1528 | |
44345724 | 1529 | dev_deactivate_many(head); |
d8b2a4d2 | 1530 | |
5cde2829 | 1531 | list_for_each_entry(dev, head, close_list) { |
44345724 | 1532 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 1533 | |
44345724 OP |
1534 | /* |
1535 | * Call the device specific close. This cannot fail. | |
1536 | * Only if device is UP | |
1537 | * | |
1538 | * We allow it to be called even after a DETACH hot-plug | |
1539 | * event. | |
1540 | */ | |
1541 | if (ops->ndo_stop) | |
1542 | ops->ndo_stop(dev); | |
1543 | ||
44345724 | 1544 | dev->flags &= ~IFF_UP; |
66b5552f | 1545 | netpoll_poll_enable(dev); |
44345724 | 1546 | } |
44345724 OP |
1547 | } |
1548 | ||
7051b88a | 1549 | static void __dev_close(struct net_device *dev) |
44345724 OP |
1550 | { |
1551 | LIST_HEAD(single); | |
1552 | ||
5cde2829 | 1553 | list_add(&dev->close_list, &single); |
7051b88a | 1554 | __dev_close_many(&single); |
f87e6f47 | 1555 | list_del(&single); |
44345724 OP |
1556 | } |
1557 | ||
7051b88a | 1558 | void dev_close_many(struct list_head *head, bool unlink) |
44345724 OP |
1559 | { |
1560 | struct net_device *dev, *tmp; | |
1da177e4 | 1561 | |
5cde2829 EB |
1562 | /* Remove the devices that don't need to be closed */ |
1563 | list_for_each_entry_safe(dev, tmp, head, close_list) | |
44345724 | 1564 | if (!(dev->flags & IFF_UP)) |
5cde2829 | 1565 | list_del_init(&dev->close_list); |
44345724 OP |
1566 | |
1567 | __dev_close_many(head); | |
1da177e4 | 1568 | |
5cde2829 | 1569 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
7f294054 | 1570 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
44345724 | 1571 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
99c4a26a DM |
1572 | if (unlink) |
1573 | list_del_init(&dev->close_list); | |
44345724 | 1574 | } |
bd380811 | 1575 | } |
99c4a26a | 1576 | EXPORT_SYMBOL(dev_close_many); |
bd380811 PM |
1577 | |
1578 | /** | |
1579 | * dev_close - shutdown an interface. | |
1580 | * @dev: device to shutdown | |
1581 | * | |
1582 | * This function moves an active device into down state. A | |
1583 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1584 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1585 | * chain. | |
1586 | */ | |
7051b88a | 1587 | void dev_close(struct net_device *dev) |
bd380811 | 1588 | { |
e14a5993 ED |
1589 | if (dev->flags & IFF_UP) { |
1590 | LIST_HEAD(single); | |
1da177e4 | 1591 | |
5cde2829 | 1592 | list_add(&dev->close_list, &single); |
99c4a26a | 1593 | dev_close_many(&single, true); |
e14a5993 ED |
1594 | list_del(&single); |
1595 | } | |
1da177e4 | 1596 | } |
d1b19dff | 1597 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1598 | |
1599 | ||
0187bdfb BH |
1600 | /** |
1601 | * dev_disable_lro - disable Large Receive Offload on a device | |
1602 | * @dev: device | |
1603 | * | |
1604 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1605 | * called under RTNL. This is needed if received packets may be | |
1606 | * forwarded to another interface. | |
1607 | */ | |
1608 | void dev_disable_lro(struct net_device *dev) | |
1609 | { | |
fbe168ba MK |
1610 | struct net_device *lower_dev; |
1611 | struct list_head *iter; | |
529d0489 | 1612 | |
bc5787c6 MM |
1613 | dev->wanted_features &= ~NETIF_F_LRO; |
1614 | netdev_update_features(dev); | |
27660515 | 1615 | |
22d5969f MM |
1616 | if (unlikely(dev->features & NETIF_F_LRO)) |
1617 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
fbe168ba MK |
1618 | |
1619 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
1620 | dev_disable_lro(lower_dev); | |
0187bdfb BH |
1621 | } |
1622 | EXPORT_SYMBOL(dev_disable_lro); | |
1623 | ||
56f5aa77 MC |
1624 | /** |
1625 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device | |
1626 | * @dev: device | |
1627 | * | |
1628 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be | |
1629 | * called under RTNL. This is needed if Generic XDP is installed on | |
1630 | * the device. | |
1631 | */ | |
1632 | static void dev_disable_gro_hw(struct net_device *dev) | |
1633 | { | |
1634 | dev->wanted_features &= ~NETIF_F_GRO_HW; | |
1635 | netdev_update_features(dev); | |
1636 | ||
1637 | if (unlikely(dev->features & NETIF_F_GRO_HW)) | |
1638 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); | |
1639 | } | |
1640 | ||
ede2762d KT |
1641 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
1642 | { | |
1643 | #define N(val) \ | |
1644 | case NETDEV_##val: \ | |
1645 | return "NETDEV_" __stringify(val); | |
1646 | switch (cmd) { | |
1647 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) | |
1648 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) | |
1649 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) | |
1650 | N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER) | |
1651 | N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO) | |
1652 | N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO) | |
1653 | N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) | |
9daae9bd GP |
1654 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
1655 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) | |
1570415f | 1656 | N(PRE_CHANGEADDR) |
3f5ecd8a | 1657 | } |
ede2762d KT |
1658 | #undef N |
1659 | return "UNKNOWN_NETDEV_EVENT"; | |
1660 | } | |
1661 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); | |
1662 | ||
351638e7 JP |
1663 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
1664 | struct net_device *dev) | |
1665 | { | |
51d0c047 DA |
1666 | struct netdev_notifier_info info = { |
1667 | .dev = dev, | |
1668 | }; | |
351638e7 | 1669 | |
351638e7 JP |
1670 | return nb->notifier_call(nb, val, &info); |
1671 | } | |
0187bdfb | 1672 | |
881d966b EB |
1673 | static int dev_boot_phase = 1; |
1674 | ||
1da177e4 | 1675 | /** |
722c9a0c | 1676 | * register_netdevice_notifier - register a network notifier block |
1677 | * @nb: notifier | |
1da177e4 | 1678 | * |
722c9a0c | 1679 | * Register a notifier to be called when network device events occur. |
1680 | * The notifier passed is linked into the kernel structures and must | |
1681 | * not be reused until it has been unregistered. A negative errno code | |
1682 | * is returned on a failure. | |
1da177e4 | 1683 | * |
722c9a0c | 1684 | * When registered all registration and up events are replayed |
1685 | * to the new notifier to allow device to have a race free | |
1686 | * view of the network device list. | |
1da177e4 LT |
1687 | */ |
1688 | ||
1689 | int register_netdevice_notifier(struct notifier_block *nb) | |
1690 | { | |
1691 | struct net_device *dev; | |
fcc5a03a | 1692 | struct net_device *last; |
881d966b | 1693 | struct net *net; |
1da177e4 LT |
1694 | int err; |
1695 | ||
328fbe74 KT |
1696 | /* Close race with setup_net() and cleanup_net() */ |
1697 | down_write(&pernet_ops_rwsem); | |
1da177e4 | 1698 | rtnl_lock(); |
f07d5b94 | 1699 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1700 | if (err) |
1701 | goto unlock; | |
881d966b EB |
1702 | if (dev_boot_phase) |
1703 | goto unlock; | |
1704 | for_each_net(net) { | |
1705 | for_each_netdev(net, dev) { | |
351638e7 | 1706 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); |
881d966b EB |
1707 | err = notifier_to_errno(err); |
1708 | if (err) | |
1709 | goto rollback; | |
1710 | ||
1711 | if (!(dev->flags & IFF_UP)) | |
1712 | continue; | |
1da177e4 | 1713 | |
351638e7 | 1714 | call_netdevice_notifier(nb, NETDEV_UP, dev); |
881d966b | 1715 | } |
1da177e4 | 1716 | } |
fcc5a03a HX |
1717 | |
1718 | unlock: | |
1da177e4 | 1719 | rtnl_unlock(); |
328fbe74 | 1720 | up_write(&pernet_ops_rwsem); |
1da177e4 | 1721 | return err; |
fcc5a03a HX |
1722 | |
1723 | rollback: | |
1724 | last = dev; | |
881d966b EB |
1725 | for_each_net(net) { |
1726 | for_each_netdev(net, dev) { | |
1727 | if (dev == last) | |
8f891489 | 1728 | goto outroll; |
fcc5a03a | 1729 | |
881d966b | 1730 | if (dev->flags & IFF_UP) { |
351638e7 JP |
1731 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, |
1732 | dev); | |
1733 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
881d966b | 1734 | } |
351638e7 | 1735 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); |
fcc5a03a | 1736 | } |
fcc5a03a | 1737 | } |
c67625a1 | 1738 | |
8f891489 | 1739 | outroll: |
c67625a1 | 1740 | raw_notifier_chain_unregister(&netdev_chain, nb); |
fcc5a03a | 1741 | goto unlock; |
1da177e4 | 1742 | } |
d1b19dff | 1743 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1744 | |
1745 | /** | |
722c9a0c | 1746 | * unregister_netdevice_notifier - unregister a network notifier block |
1747 | * @nb: notifier | |
1da177e4 | 1748 | * |
722c9a0c | 1749 | * Unregister a notifier previously registered by |
1750 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1751 | * kernel structures and may then be reused. A negative errno code | |
1752 | * is returned on a failure. | |
7d3d43da | 1753 | * |
722c9a0c | 1754 | * After unregistering unregister and down device events are synthesized |
1755 | * for all devices on the device list to the removed notifier to remove | |
1756 | * the need for special case cleanup code. | |
1da177e4 LT |
1757 | */ |
1758 | ||
1759 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1760 | { | |
7d3d43da EB |
1761 | struct net_device *dev; |
1762 | struct net *net; | |
9f514950 HX |
1763 | int err; |
1764 | ||
328fbe74 KT |
1765 | /* Close race with setup_net() and cleanup_net() */ |
1766 | down_write(&pernet_ops_rwsem); | |
9f514950 | 1767 | rtnl_lock(); |
f07d5b94 | 1768 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1769 | if (err) |
1770 | goto unlock; | |
1771 | ||
1772 | for_each_net(net) { | |
1773 | for_each_netdev(net, dev) { | |
1774 | if (dev->flags & IFF_UP) { | |
351638e7 JP |
1775 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, |
1776 | dev); | |
1777 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
7d3d43da | 1778 | } |
351638e7 | 1779 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); |
7d3d43da EB |
1780 | } |
1781 | } | |
1782 | unlock: | |
9f514950 | 1783 | rtnl_unlock(); |
328fbe74 | 1784 | up_write(&pernet_ops_rwsem); |
9f514950 | 1785 | return err; |
1da177e4 | 1786 | } |
d1b19dff | 1787 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 | 1788 | |
351638e7 JP |
1789 | /** |
1790 | * call_netdevice_notifiers_info - call all network notifier blocks | |
1791 | * @val: value passed unmodified to notifier function | |
351638e7 JP |
1792 | * @info: notifier information data |
1793 | * | |
1794 | * Call all network notifier blocks. Parameters and return value | |
1795 | * are as for raw_notifier_call_chain(). | |
1796 | */ | |
1797 | ||
1d143d9f | 1798 | static int call_netdevice_notifiers_info(unsigned long val, |
1d143d9f | 1799 | struct netdev_notifier_info *info) |
351638e7 JP |
1800 | { |
1801 | ASSERT_RTNL(); | |
351638e7 JP |
1802 | return raw_notifier_call_chain(&netdev_chain, val, info); |
1803 | } | |
351638e7 | 1804 | |
26372605 PM |
1805 | static int call_netdevice_notifiers_extack(unsigned long val, |
1806 | struct net_device *dev, | |
1807 | struct netlink_ext_ack *extack) | |
1808 | { | |
1809 | struct netdev_notifier_info info = { | |
1810 | .dev = dev, | |
1811 | .extack = extack, | |
1812 | }; | |
1813 | ||
1814 | return call_netdevice_notifiers_info(val, &info); | |
1815 | } | |
1816 | ||
1da177e4 LT |
1817 | /** |
1818 | * call_netdevice_notifiers - call all network notifier blocks | |
1819 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 1820 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
1821 | * |
1822 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 1823 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
1824 | */ |
1825 | ||
ad7379d4 | 1826 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 1827 | { |
26372605 | 1828 | return call_netdevice_notifiers_extack(val, dev, NULL); |
1da177e4 | 1829 | } |
edf947f1 | 1830 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 1831 | |
af7d6cce SD |
1832 | /** |
1833 | * call_netdevice_notifiers_mtu - call all network notifier blocks | |
1834 | * @val: value passed unmodified to notifier function | |
1835 | * @dev: net_device pointer passed unmodified to notifier function | |
1836 | * @arg: additional u32 argument passed to the notifier function | |
1837 | * | |
1838 | * Call all network notifier blocks. Parameters and return value | |
1839 | * are as for raw_notifier_call_chain(). | |
1840 | */ | |
1841 | static int call_netdevice_notifiers_mtu(unsigned long val, | |
1842 | struct net_device *dev, u32 arg) | |
1843 | { | |
1844 | struct netdev_notifier_info_ext info = { | |
1845 | .info.dev = dev, | |
1846 | .ext.mtu = arg, | |
1847 | }; | |
1848 | ||
1849 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); | |
1850 | ||
1851 | return call_netdevice_notifiers_info(val, &info.info); | |
1852 | } | |
1853 | ||
1cf51900 | 1854 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 1855 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
4577139b DB |
1856 | |
1857 | void net_inc_ingress_queue(void) | |
1858 | { | |
aabf6772 | 1859 | static_branch_inc(&ingress_needed_key); |
4577139b DB |
1860 | } |
1861 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); | |
1862 | ||
1863 | void net_dec_ingress_queue(void) | |
1864 | { | |
aabf6772 | 1865 | static_branch_dec(&ingress_needed_key); |
4577139b DB |
1866 | } |
1867 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); | |
1868 | #endif | |
1869 | ||
1f211a1b | 1870 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 1871 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
1f211a1b DB |
1872 | |
1873 | void net_inc_egress_queue(void) | |
1874 | { | |
aabf6772 | 1875 | static_branch_inc(&egress_needed_key); |
1f211a1b DB |
1876 | } |
1877 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); | |
1878 | ||
1879 | void net_dec_egress_queue(void) | |
1880 | { | |
aabf6772 | 1881 | static_branch_dec(&egress_needed_key); |
1f211a1b DB |
1882 | } |
1883 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); | |
1884 | #endif | |
1885 | ||
39e83922 | 1886 | static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
e9666d10 | 1887 | #ifdef CONFIG_JUMP_LABEL |
b90e5794 | 1888 | static atomic_t netstamp_needed_deferred; |
13baa00a | 1889 | static atomic_t netstamp_wanted; |
5fa8bbda | 1890 | static void netstamp_clear(struct work_struct *work) |
1da177e4 | 1891 | { |
b90e5794 | 1892 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
13baa00a | 1893 | int wanted; |
b90e5794 | 1894 | |
13baa00a ED |
1895 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
1896 | if (wanted > 0) | |
39e83922 | 1897 | static_branch_enable(&netstamp_needed_key); |
13baa00a | 1898 | else |
39e83922 | 1899 | static_branch_disable(&netstamp_needed_key); |
5fa8bbda ED |
1900 | } |
1901 | static DECLARE_WORK(netstamp_work, netstamp_clear); | |
b90e5794 | 1902 | #endif |
5fa8bbda ED |
1903 | |
1904 | void net_enable_timestamp(void) | |
1905 | { | |
e9666d10 | 1906 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
1907 | int wanted; |
1908 | ||
1909 | while (1) { | |
1910 | wanted = atomic_read(&netstamp_wanted); | |
1911 | if (wanted <= 0) | |
1912 | break; | |
1913 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted) | |
1914 | return; | |
1915 | } | |
1916 | atomic_inc(&netstamp_needed_deferred); | |
1917 | schedule_work(&netstamp_work); | |
1918 | #else | |
39e83922 | 1919 | static_branch_inc(&netstamp_needed_key); |
13baa00a | 1920 | #endif |
1da177e4 | 1921 | } |
d1b19dff | 1922 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
1923 | |
1924 | void net_disable_timestamp(void) | |
1925 | { | |
e9666d10 | 1926 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
1927 | int wanted; |
1928 | ||
1929 | while (1) { | |
1930 | wanted = atomic_read(&netstamp_wanted); | |
1931 | if (wanted <= 1) | |
1932 | break; | |
1933 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted) | |
1934 | return; | |
1935 | } | |
1936 | atomic_dec(&netstamp_needed_deferred); | |
5fa8bbda ED |
1937 | schedule_work(&netstamp_work); |
1938 | #else | |
39e83922 | 1939 | static_branch_dec(&netstamp_needed_key); |
5fa8bbda | 1940 | #endif |
1da177e4 | 1941 | } |
d1b19dff | 1942 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 1943 | |
3b098e2d | 1944 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 1945 | { |
2456e855 | 1946 | skb->tstamp = 0; |
39e83922 | 1947 | if (static_branch_unlikely(&netstamp_needed_key)) |
a61bbcf2 | 1948 | __net_timestamp(skb); |
1da177e4 LT |
1949 | } |
1950 | ||
39e83922 DB |
1951 | #define net_timestamp_check(COND, SKB) \ |
1952 | if (static_branch_unlikely(&netstamp_needed_key)) { \ | |
1953 | if ((COND) && !(SKB)->tstamp) \ | |
1954 | __net_timestamp(SKB); \ | |
1955 | } \ | |
3b098e2d | 1956 | |
f4b05d27 | 1957 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
79b569f0 DL |
1958 | { |
1959 | unsigned int len; | |
1960 | ||
1961 | if (!(dev->flags & IFF_UP)) | |
1962 | return false; | |
1963 | ||
1964 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; | |
1965 | if (skb->len <= len) | |
1966 | return true; | |
1967 | ||
1968 | /* if TSO is enabled, we don't care about the length as the packet | |
1969 | * could be forwarded without being segmented before | |
1970 | */ | |
1971 | if (skb_is_gso(skb)) | |
1972 | return true; | |
1973 | ||
1974 | return false; | |
1975 | } | |
1ee481fb | 1976 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
79b569f0 | 1977 | |
a0265d28 HX |
1978 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
1979 | { | |
4e3264d2 | 1980 | int ret = ____dev_forward_skb(dev, skb); |
a0265d28 | 1981 | |
4e3264d2 MKL |
1982 | if (likely(!ret)) { |
1983 | skb->protocol = eth_type_trans(skb, dev); | |
1984 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
1985 | } | |
a0265d28 | 1986 | |
4e3264d2 | 1987 | return ret; |
a0265d28 HX |
1988 | } |
1989 | EXPORT_SYMBOL_GPL(__dev_forward_skb); | |
1990 | ||
44540960 AB |
1991 | /** |
1992 | * dev_forward_skb - loopback an skb to another netif | |
1993 | * | |
1994 | * @dev: destination network device | |
1995 | * @skb: buffer to forward | |
1996 | * | |
1997 | * return values: | |
1998 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 1999 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
2000 | * |
2001 | * dev_forward_skb can be used for injecting an skb from the | |
2002 | * start_xmit function of one device into the receive queue | |
2003 | * of another device. | |
2004 | * | |
2005 | * The receiving device may be in another namespace, so | |
2006 | * we have to clear all information in the skb that could | |
2007 | * impact namespace isolation. | |
2008 | */ | |
2009 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2010 | { | |
a0265d28 | 2011 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
44540960 AB |
2012 | } |
2013 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
2014 | ||
71d9dec2 CG |
2015 | static inline int deliver_skb(struct sk_buff *skb, |
2016 | struct packet_type *pt_prev, | |
2017 | struct net_device *orig_dev) | |
2018 | { | |
1f8b977a | 2019 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
1080e512 | 2020 | return -ENOMEM; |
63354797 | 2021 | refcount_inc(&skb->users); |
71d9dec2 CG |
2022 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
2023 | } | |
2024 | ||
7866a621 SN |
2025 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
2026 | struct packet_type **pt, | |
fbcb2170 JP |
2027 | struct net_device *orig_dev, |
2028 | __be16 type, | |
7866a621 SN |
2029 | struct list_head *ptype_list) |
2030 | { | |
2031 | struct packet_type *ptype, *pt_prev = *pt; | |
2032 | ||
2033 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
2034 | if (ptype->type != type) | |
2035 | continue; | |
2036 | if (pt_prev) | |
fbcb2170 | 2037 | deliver_skb(skb, pt_prev, orig_dev); |
7866a621 SN |
2038 | pt_prev = ptype; |
2039 | } | |
2040 | *pt = pt_prev; | |
2041 | } | |
2042 | ||
c0de08d0 EL |
2043 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
2044 | { | |
a3d744e9 | 2045 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
2046 | return false; |
2047 | ||
2048 | if (ptype->id_match) | |
2049 | return ptype->id_match(ptype, skb->sk); | |
2050 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
2051 | return true; | |
2052 | ||
2053 | return false; | |
2054 | } | |
2055 | ||
9f9a742d MR |
2056 | /** |
2057 | * dev_nit_active - return true if any network interface taps are in use | |
2058 | * | |
2059 | * @dev: network device to check for the presence of taps | |
2060 | */ | |
2061 | bool dev_nit_active(struct net_device *dev) | |
2062 | { | |
2063 | return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all); | |
2064 | } | |
2065 | EXPORT_SYMBOL_GPL(dev_nit_active); | |
2066 | ||
1da177e4 LT |
2067 | /* |
2068 | * Support routine. Sends outgoing frames to any network | |
2069 | * taps currently in use. | |
2070 | */ | |
2071 | ||
74b20582 | 2072 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
2073 | { |
2074 | struct packet_type *ptype; | |
71d9dec2 CG |
2075 | struct sk_buff *skb2 = NULL; |
2076 | struct packet_type *pt_prev = NULL; | |
7866a621 | 2077 | struct list_head *ptype_list = &ptype_all; |
a61bbcf2 | 2078 | |
1da177e4 | 2079 | rcu_read_lock(); |
7866a621 SN |
2080 | again: |
2081 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
fa788d98 VW |
2082 | if (ptype->ignore_outgoing) |
2083 | continue; | |
2084 | ||
1da177e4 LT |
2085 | /* Never send packets back to the socket |
2086 | * they originated from - MvS (miquels@drinkel.ow.org) | |
2087 | */ | |
7866a621 SN |
2088 | if (skb_loop_sk(ptype, skb)) |
2089 | continue; | |
71d9dec2 | 2090 | |
7866a621 SN |
2091 | if (pt_prev) { |
2092 | deliver_skb(skb2, pt_prev, skb->dev); | |
2093 | pt_prev = ptype; | |
2094 | continue; | |
2095 | } | |
1da177e4 | 2096 | |
7866a621 SN |
2097 | /* need to clone skb, done only once */ |
2098 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
2099 | if (!skb2) | |
2100 | goto out_unlock; | |
70978182 | 2101 | |
7866a621 | 2102 | net_timestamp_set(skb2); |
1da177e4 | 2103 | |
7866a621 SN |
2104 | /* skb->nh should be correctly |
2105 | * set by sender, so that the second statement is | |
2106 | * just protection against buggy protocols. | |
2107 | */ | |
2108 | skb_reset_mac_header(skb2); | |
2109 | ||
2110 | if (skb_network_header(skb2) < skb2->data || | |
2111 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { | |
2112 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", | |
2113 | ntohs(skb2->protocol), | |
2114 | dev->name); | |
2115 | skb_reset_network_header(skb2); | |
1da177e4 | 2116 | } |
7866a621 SN |
2117 | |
2118 | skb2->transport_header = skb2->network_header; | |
2119 | skb2->pkt_type = PACKET_OUTGOING; | |
2120 | pt_prev = ptype; | |
2121 | } | |
2122 | ||
2123 | if (ptype_list == &ptype_all) { | |
2124 | ptype_list = &dev->ptype_all; | |
2125 | goto again; | |
1da177e4 | 2126 | } |
7866a621 | 2127 | out_unlock: |
581fe0ea WB |
2128 | if (pt_prev) { |
2129 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) | |
2130 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
2131 | else | |
2132 | kfree_skb(skb2); | |
2133 | } | |
1da177e4 LT |
2134 | rcu_read_unlock(); |
2135 | } | |
74b20582 | 2136 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
1da177e4 | 2137 | |
2c53040f BH |
2138 | /** |
2139 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
2140 | * @dev: Network device |
2141 | * @txq: number of queues available | |
2142 | * | |
2143 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
2144 | * valid. To resolve this verify the tc mapping remains valid and if | |
2145 | * not NULL the mapping. With no priorities mapping to this | |
2146 | * offset/count pair it will no longer be used. In the worst case TC0 | |
2147 | * is invalid nothing can be done so disable priority mappings. If is | |
2148 | * expected that drivers will fix this mapping if they can before | |
2149 | * calling netif_set_real_num_tx_queues. | |
2150 | */ | |
bb134d22 | 2151 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
2152 | { |
2153 | int i; | |
2154 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2155 | ||
2156 | /* If TC0 is invalidated disable TC mapping */ | |
2157 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce | 2158 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
2159 | dev->num_tc = 0; |
2160 | return; | |
2161 | } | |
2162 | ||
2163 | /* Invalidated prio to tc mappings set to TC0 */ | |
2164 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
2165 | int q = netdev_get_prio_tc_map(dev, i); | |
2166 | ||
2167 | tc = &dev->tc_to_txq[q]; | |
2168 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce JP |
2169 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
2170 | i, q); | |
4f57c087 JF |
2171 | netdev_set_prio_tc_map(dev, i, 0); |
2172 | } | |
2173 | } | |
2174 | } | |
2175 | ||
8d059b0f AD |
2176 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
2177 | { | |
2178 | if (dev->num_tc) { | |
2179 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2180 | int i; | |
2181 | ||
ffcfe25b | 2182 | /* walk through the TCs and see if it falls into any of them */ |
8d059b0f AD |
2183 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
2184 | if ((txq - tc->offset) < tc->count) | |
2185 | return i; | |
2186 | } | |
2187 | ||
ffcfe25b | 2188 | /* didn't find it, just return -1 to indicate no match */ |
8d059b0f AD |
2189 | return -1; |
2190 | } | |
2191 | ||
2192 | return 0; | |
2193 | } | |
8a5f2166 | 2194 | EXPORT_SYMBOL(netdev_txq_to_tc); |
8d059b0f | 2195 | |
537c00de | 2196 | #ifdef CONFIG_XPS |
04157469 AN |
2197 | struct static_key xps_needed __read_mostly; |
2198 | EXPORT_SYMBOL(xps_needed); | |
2199 | struct static_key xps_rxqs_needed __read_mostly; | |
2200 | EXPORT_SYMBOL(xps_rxqs_needed); | |
537c00de AD |
2201 | static DEFINE_MUTEX(xps_map_mutex); |
2202 | #define xmap_dereference(P) \ | |
2203 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
2204 | ||
6234f874 AD |
2205 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
2206 | int tci, u16 index) | |
537c00de | 2207 | { |
10cdc3f3 AD |
2208 | struct xps_map *map = NULL; |
2209 | int pos; | |
537c00de | 2210 | |
10cdc3f3 | 2211 | if (dev_maps) |
80d19669 | 2212 | map = xmap_dereference(dev_maps->attr_map[tci]); |
6234f874 AD |
2213 | if (!map) |
2214 | return false; | |
537c00de | 2215 | |
6234f874 AD |
2216 | for (pos = map->len; pos--;) { |
2217 | if (map->queues[pos] != index) | |
2218 | continue; | |
2219 | ||
2220 | if (map->len > 1) { | |
2221 | map->queues[pos] = map->queues[--map->len]; | |
10cdc3f3 | 2222 | break; |
537c00de | 2223 | } |
6234f874 | 2224 | |
80d19669 | 2225 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
6234f874 AD |
2226 | kfree_rcu(map, rcu); |
2227 | return false; | |
537c00de AD |
2228 | } |
2229 | ||
6234f874 | 2230 | return true; |
10cdc3f3 AD |
2231 | } |
2232 | ||
6234f874 AD |
2233 | static bool remove_xps_queue_cpu(struct net_device *dev, |
2234 | struct xps_dev_maps *dev_maps, | |
2235 | int cpu, u16 offset, u16 count) | |
2236 | { | |
184c449f AD |
2237 | int num_tc = dev->num_tc ? : 1; |
2238 | bool active = false; | |
2239 | int tci; | |
6234f874 | 2240 | |
184c449f AD |
2241 | for (tci = cpu * num_tc; num_tc--; tci++) { |
2242 | int i, j; | |
2243 | ||
2244 | for (i = count, j = offset; i--; j++) { | |
6358d49a | 2245 | if (!remove_xps_queue(dev_maps, tci, j)) |
184c449f AD |
2246 | break; |
2247 | } | |
2248 | ||
2249 | active |= i < 0; | |
6234f874 AD |
2250 | } |
2251 | ||
184c449f | 2252 | return active; |
6234f874 AD |
2253 | } |
2254 | ||
867d0ad4 SD |
2255 | static void reset_xps_maps(struct net_device *dev, |
2256 | struct xps_dev_maps *dev_maps, | |
2257 | bool is_rxqs_map) | |
2258 | { | |
2259 | if (is_rxqs_map) { | |
2260 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); | |
2261 | RCU_INIT_POINTER(dev->xps_rxqs_map, NULL); | |
2262 | } else { | |
2263 | RCU_INIT_POINTER(dev->xps_cpus_map, NULL); | |
2264 | } | |
2265 | static_key_slow_dec_cpuslocked(&xps_needed); | |
2266 | kfree_rcu(dev_maps, rcu); | |
2267 | } | |
2268 | ||
80d19669 AN |
2269 | static void clean_xps_maps(struct net_device *dev, const unsigned long *mask, |
2270 | struct xps_dev_maps *dev_maps, unsigned int nr_ids, | |
2271 | u16 offset, u16 count, bool is_rxqs_map) | |
2272 | { | |
2273 | bool active = false; | |
2274 | int i, j; | |
2275 | ||
2276 | for (j = -1; j = netif_attrmask_next(j, mask, nr_ids), | |
2277 | j < nr_ids;) | |
2278 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, | |
2279 | count); | |
867d0ad4 SD |
2280 | if (!active) |
2281 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
80d19669 | 2282 | |
f28c020f SD |
2283 | if (!is_rxqs_map) { |
2284 | for (i = offset + (count - 1); count--; i--) { | |
2285 | netdev_queue_numa_node_write( | |
2286 | netdev_get_tx_queue(dev, i), | |
2287 | NUMA_NO_NODE); | |
80d19669 | 2288 | } |
80d19669 AN |
2289 | } |
2290 | } | |
2291 | ||
6234f874 AD |
2292 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
2293 | u16 count) | |
10cdc3f3 | 2294 | { |
80d19669 | 2295 | const unsigned long *possible_mask = NULL; |
10cdc3f3 | 2296 | struct xps_dev_maps *dev_maps; |
80d19669 | 2297 | unsigned int nr_ids; |
10cdc3f3 | 2298 | |
04157469 AN |
2299 | if (!static_key_false(&xps_needed)) |
2300 | return; | |
10cdc3f3 | 2301 | |
4d99f660 | 2302 | cpus_read_lock(); |
04157469 | 2303 | mutex_lock(&xps_map_mutex); |
10cdc3f3 | 2304 | |
04157469 AN |
2305 | if (static_key_false(&xps_rxqs_needed)) { |
2306 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2307 | if (dev_maps) { | |
2308 | nr_ids = dev->num_rx_queues; | |
2309 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, | |
2310 | offset, count, true); | |
2311 | } | |
537c00de AD |
2312 | } |
2313 | ||
80d19669 AN |
2314 | dev_maps = xmap_dereference(dev->xps_cpus_map); |
2315 | if (!dev_maps) | |
2316 | goto out_no_maps; | |
2317 | ||
2318 | if (num_possible_cpus() > 1) | |
2319 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2320 | nr_ids = nr_cpu_ids; | |
2321 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count, | |
2322 | false); | |
024e9679 | 2323 | |
537c00de AD |
2324 | out_no_maps: |
2325 | mutex_unlock(&xps_map_mutex); | |
4d99f660 | 2326 | cpus_read_unlock(); |
537c00de AD |
2327 | } |
2328 | ||
6234f874 AD |
2329 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
2330 | { | |
2331 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); | |
2332 | } | |
2333 | ||
80d19669 AN |
2334 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
2335 | u16 index, bool is_rxqs_map) | |
01c5f864 AD |
2336 | { |
2337 | struct xps_map *new_map; | |
2338 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
2339 | int i, pos; | |
2340 | ||
2341 | for (pos = 0; map && pos < map->len; pos++) { | |
2342 | if (map->queues[pos] != index) | |
2343 | continue; | |
2344 | return map; | |
2345 | } | |
2346 | ||
80d19669 | 2347 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
01c5f864 AD |
2348 | if (map) { |
2349 | if (pos < map->alloc_len) | |
2350 | return map; | |
2351 | ||
2352 | alloc_len = map->alloc_len * 2; | |
2353 | } | |
2354 | ||
80d19669 AN |
2355 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
2356 | * map | |
2357 | */ | |
2358 | if (is_rxqs_map) | |
2359 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); | |
2360 | else | |
2361 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
2362 | cpu_to_node(attr_index)); | |
01c5f864 AD |
2363 | if (!new_map) |
2364 | return NULL; | |
2365 | ||
2366 | for (i = 0; i < pos; i++) | |
2367 | new_map->queues[i] = map->queues[i]; | |
2368 | new_map->alloc_len = alloc_len; | |
2369 | new_map->len = pos; | |
2370 | ||
2371 | return new_map; | |
2372 | } | |
2373 | ||
4d99f660 | 2374 | /* Must be called under cpus_read_lock */ |
80d19669 AN |
2375 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
2376 | u16 index, bool is_rxqs_map) | |
537c00de | 2377 | { |
80d19669 | 2378 | const unsigned long *online_mask = NULL, *possible_mask = NULL; |
01c5f864 | 2379 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
80d19669 | 2380 | int i, j, tci, numa_node_id = -2; |
184c449f | 2381 | int maps_sz, num_tc = 1, tc = 0; |
537c00de | 2382 | struct xps_map *map, *new_map; |
01c5f864 | 2383 | bool active = false; |
80d19669 | 2384 | unsigned int nr_ids; |
537c00de | 2385 | |
184c449f | 2386 | if (dev->num_tc) { |
ffcfe25b | 2387 | /* Do not allow XPS on subordinate device directly */ |
184c449f | 2388 | num_tc = dev->num_tc; |
ffcfe25b AD |
2389 | if (num_tc < 0) |
2390 | return -EINVAL; | |
2391 | ||
2392 | /* If queue belongs to subordinate dev use its map */ | |
2393 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
2394 | ||
184c449f AD |
2395 | tc = netdev_txq_to_tc(dev, index); |
2396 | if (tc < 0) | |
2397 | return -EINVAL; | |
2398 | } | |
2399 | ||
537c00de | 2400 | mutex_lock(&xps_map_mutex); |
80d19669 AN |
2401 | if (is_rxqs_map) { |
2402 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); | |
2403 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2404 | nr_ids = dev->num_rx_queues; | |
2405 | } else { | |
2406 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); | |
2407 | if (num_possible_cpus() > 1) { | |
2408 | online_mask = cpumask_bits(cpu_online_mask); | |
2409 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2410 | } | |
2411 | dev_maps = xmap_dereference(dev->xps_cpus_map); | |
2412 | nr_ids = nr_cpu_ids; | |
2413 | } | |
537c00de | 2414 | |
80d19669 AN |
2415 | if (maps_sz < L1_CACHE_BYTES) |
2416 | maps_sz = L1_CACHE_BYTES; | |
537c00de | 2417 | |
01c5f864 | 2418 | /* allocate memory for queue storage */ |
80d19669 AN |
2419 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
2420 | j < nr_ids;) { | |
01c5f864 AD |
2421 | if (!new_dev_maps) |
2422 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); | |
2bb60cb9 AD |
2423 | if (!new_dev_maps) { |
2424 | mutex_unlock(&xps_map_mutex); | |
01c5f864 | 2425 | return -ENOMEM; |
2bb60cb9 | 2426 | } |
01c5f864 | 2427 | |
80d19669 AN |
2428 | tci = j * num_tc + tc; |
2429 | map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) : | |
01c5f864 AD |
2430 | NULL; |
2431 | ||
80d19669 | 2432 | map = expand_xps_map(map, j, index, is_rxqs_map); |
01c5f864 AD |
2433 | if (!map) |
2434 | goto error; | |
2435 | ||
80d19669 | 2436 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
01c5f864 AD |
2437 | } |
2438 | ||
2439 | if (!new_dev_maps) | |
2440 | goto out_no_new_maps; | |
2441 | ||
867d0ad4 SD |
2442 | if (!dev_maps) { |
2443 | /* Increment static keys at most once per type */ | |
2444 | static_key_slow_inc_cpuslocked(&xps_needed); | |
2445 | if (is_rxqs_map) | |
2446 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); | |
2447 | } | |
04157469 | 2448 | |
80d19669 AN |
2449 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2450 | j < nr_ids;) { | |
184c449f | 2451 | /* copy maps belonging to foreign traffic classes */ |
80d19669 | 2452 | for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) { |
184c449f | 2453 | /* fill in the new device map from the old device map */ |
80d19669 AN |
2454 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2455 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f AD |
2456 | } |
2457 | ||
2458 | /* We need to explicitly update tci as prevous loop | |
2459 | * could break out early if dev_maps is NULL. | |
2460 | */ | |
80d19669 | 2461 | tci = j * num_tc + tc; |
184c449f | 2462 | |
80d19669 AN |
2463 | if (netif_attr_test_mask(j, mask, nr_ids) && |
2464 | netif_attr_test_online(j, online_mask, nr_ids)) { | |
2465 | /* add tx-queue to CPU/rx-queue maps */ | |
01c5f864 AD |
2466 | int pos = 0; |
2467 | ||
80d19669 | 2468 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
01c5f864 AD |
2469 | while ((pos < map->len) && (map->queues[pos] != index)) |
2470 | pos++; | |
2471 | ||
2472 | if (pos == map->len) | |
2473 | map->queues[map->len++] = index; | |
537c00de | 2474 | #ifdef CONFIG_NUMA |
80d19669 AN |
2475 | if (!is_rxqs_map) { |
2476 | if (numa_node_id == -2) | |
2477 | numa_node_id = cpu_to_node(j); | |
2478 | else if (numa_node_id != cpu_to_node(j)) | |
2479 | numa_node_id = -1; | |
2480 | } | |
537c00de | 2481 | #endif |
01c5f864 AD |
2482 | } else if (dev_maps) { |
2483 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2484 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2485 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
537c00de | 2486 | } |
01c5f864 | 2487 | |
184c449f AD |
2488 | /* copy maps belonging to foreign traffic classes */ |
2489 | for (i = num_tc - tc, tci++; dev_maps && --i; tci++) { | |
2490 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2491 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2492 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f | 2493 | } |
537c00de AD |
2494 | } |
2495 | ||
80d19669 AN |
2496 | if (is_rxqs_map) |
2497 | rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps); | |
2498 | else | |
2499 | rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps); | |
01c5f864 | 2500 | |
537c00de | 2501 | /* Cleanup old maps */ |
184c449f AD |
2502 | if (!dev_maps) |
2503 | goto out_no_old_maps; | |
2504 | ||
80d19669 AN |
2505 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2506 | j < nr_ids;) { | |
2507 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2508 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
2509 | map = xmap_dereference(dev_maps->attr_map[tci]); | |
01c5f864 AD |
2510 | if (map && map != new_map) |
2511 | kfree_rcu(map, rcu); | |
2512 | } | |
537c00de AD |
2513 | } |
2514 | ||
184c449f AD |
2515 | kfree_rcu(dev_maps, rcu); |
2516 | ||
2517 | out_no_old_maps: | |
01c5f864 AD |
2518 | dev_maps = new_dev_maps; |
2519 | active = true; | |
537c00de | 2520 | |
01c5f864 | 2521 | out_no_new_maps: |
80d19669 AN |
2522 | if (!is_rxqs_map) { |
2523 | /* update Tx queue numa node */ | |
2524 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), | |
2525 | (numa_node_id >= 0) ? | |
2526 | numa_node_id : NUMA_NO_NODE); | |
2527 | } | |
537c00de | 2528 | |
01c5f864 AD |
2529 | if (!dev_maps) |
2530 | goto out_no_maps; | |
2531 | ||
80d19669 AN |
2532 | /* removes tx-queue from unused CPUs/rx-queues */ |
2533 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), | |
2534 | j < nr_ids;) { | |
2535 | for (i = tc, tci = j * num_tc; i--; tci++) | |
184c449f | 2536 | active |= remove_xps_queue(dev_maps, tci, index); |
80d19669 AN |
2537 | if (!netif_attr_test_mask(j, mask, nr_ids) || |
2538 | !netif_attr_test_online(j, online_mask, nr_ids)) | |
184c449f AD |
2539 | active |= remove_xps_queue(dev_maps, tci, index); |
2540 | for (i = num_tc - tc, tci++; --i; tci++) | |
2541 | active |= remove_xps_queue(dev_maps, tci, index); | |
01c5f864 AD |
2542 | } |
2543 | ||
2544 | /* free map if not active */ | |
867d0ad4 SD |
2545 | if (!active) |
2546 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
01c5f864 AD |
2547 | |
2548 | out_no_maps: | |
537c00de AD |
2549 | mutex_unlock(&xps_map_mutex); |
2550 | ||
2551 | return 0; | |
2552 | error: | |
01c5f864 | 2553 | /* remove any maps that we added */ |
80d19669 AN |
2554 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2555 | j < nr_ids;) { | |
2556 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2557 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
184c449f | 2558 | map = dev_maps ? |
80d19669 | 2559 | xmap_dereference(dev_maps->attr_map[tci]) : |
184c449f AD |
2560 | NULL; |
2561 | if (new_map && new_map != map) | |
2562 | kfree(new_map); | |
2563 | } | |
01c5f864 AD |
2564 | } |
2565 | ||
537c00de AD |
2566 | mutex_unlock(&xps_map_mutex); |
2567 | ||
537c00de AD |
2568 | kfree(new_dev_maps); |
2569 | return -ENOMEM; | |
2570 | } | |
4d99f660 | 2571 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
80d19669 AN |
2572 | |
2573 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, | |
2574 | u16 index) | |
2575 | { | |
4d99f660 AV |
2576 | int ret; |
2577 | ||
2578 | cpus_read_lock(); | |
2579 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false); | |
2580 | cpus_read_unlock(); | |
2581 | ||
2582 | return ret; | |
80d19669 | 2583 | } |
537c00de AD |
2584 | EXPORT_SYMBOL(netif_set_xps_queue); |
2585 | ||
2586 | #endif | |
ffcfe25b AD |
2587 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
2588 | { | |
2589 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2590 | ||
2591 | /* Unbind any subordinate channels */ | |
2592 | while (txq-- != &dev->_tx[0]) { | |
2593 | if (txq->sb_dev) | |
2594 | netdev_unbind_sb_channel(dev, txq->sb_dev); | |
2595 | } | |
2596 | } | |
2597 | ||
9cf1f6a8 AD |
2598 | void netdev_reset_tc(struct net_device *dev) |
2599 | { | |
6234f874 AD |
2600 | #ifdef CONFIG_XPS |
2601 | netif_reset_xps_queues_gt(dev, 0); | |
2602 | #endif | |
ffcfe25b AD |
2603 | netdev_unbind_all_sb_channels(dev); |
2604 | ||
2605 | /* Reset TC configuration of device */ | |
9cf1f6a8 AD |
2606 | dev->num_tc = 0; |
2607 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); | |
2608 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); | |
2609 | } | |
2610 | EXPORT_SYMBOL(netdev_reset_tc); | |
2611 | ||
2612 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) | |
2613 | { | |
2614 | if (tc >= dev->num_tc) | |
2615 | return -EINVAL; | |
2616 | ||
6234f874 AD |
2617 | #ifdef CONFIG_XPS |
2618 | netif_reset_xps_queues(dev, offset, count); | |
2619 | #endif | |
9cf1f6a8 AD |
2620 | dev->tc_to_txq[tc].count = count; |
2621 | dev->tc_to_txq[tc].offset = offset; | |
2622 | return 0; | |
2623 | } | |
2624 | EXPORT_SYMBOL(netdev_set_tc_queue); | |
2625 | ||
2626 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) | |
2627 | { | |
2628 | if (num_tc > TC_MAX_QUEUE) | |
2629 | return -EINVAL; | |
2630 | ||
6234f874 AD |
2631 | #ifdef CONFIG_XPS |
2632 | netif_reset_xps_queues_gt(dev, 0); | |
2633 | #endif | |
ffcfe25b AD |
2634 | netdev_unbind_all_sb_channels(dev); |
2635 | ||
9cf1f6a8 AD |
2636 | dev->num_tc = num_tc; |
2637 | return 0; | |
2638 | } | |
2639 | EXPORT_SYMBOL(netdev_set_num_tc); | |
2640 | ||
ffcfe25b AD |
2641 | void netdev_unbind_sb_channel(struct net_device *dev, |
2642 | struct net_device *sb_dev) | |
2643 | { | |
2644 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2645 | ||
2646 | #ifdef CONFIG_XPS | |
2647 | netif_reset_xps_queues_gt(sb_dev, 0); | |
2648 | #endif | |
2649 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); | |
2650 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); | |
2651 | ||
2652 | while (txq-- != &dev->_tx[0]) { | |
2653 | if (txq->sb_dev == sb_dev) | |
2654 | txq->sb_dev = NULL; | |
2655 | } | |
2656 | } | |
2657 | EXPORT_SYMBOL(netdev_unbind_sb_channel); | |
2658 | ||
2659 | int netdev_bind_sb_channel_queue(struct net_device *dev, | |
2660 | struct net_device *sb_dev, | |
2661 | u8 tc, u16 count, u16 offset) | |
2662 | { | |
2663 | /* Make certain the sb_dev and dev are already configured */ | |
2664 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) | |
2665 | return -EINVAL; | |
2666 | ||
2667 | /* We cannot hand out queues we don't have */ | |
2668 | if ((offset + count) > dev->real_num_tx_queues) | |
2669 | return -EINVAL; | |
2670 | ||
2671 | /* Record the mapping */ | |
2672 | sb_dev->tc_to_txq[tc].count = count; | |
2673 | sb_dev->tc_to_txq[tc].offset = offset; | |
2674 | ||
2675 | /* Provide a way for Tx queue to find the tc_to_txq map or | |
2676 | * XPS map for itself. | |
2677 | */ | |
2678 | while (count--) | |
2679 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; | |
2680 | ||
2681 | return 0; | |
2682 | } | |
2683 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); | |
2684 | ||
2685 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) | |
2686 | { | |
2687 | /* Do not use a multiqueue device to represent a subordinate channel */ | |
2688 | if (netif_is_multiqueue(dev)) | |
2689 | return -ENODEV; | |
2690 | ||
2691 | /* We allow channels 1 - 32767 to be used for subordinate channels. | |
2692 | * Channel 0 is meant to be "native" mode and used only to represent | |
2693 | * the main root device. We allow writing 0 to reset the device back | |
2694 | * to normal mode after being used as a subordinate channel. | |
2695 | */ | |
2696 | if (channel > S16_MAX) | |
2697 | return -EINVAL; | |
2698 | ||
2699 | dev->num_tc = -channel; | |
2700 | ||
2701 | return 0; | |
2702 | } | |
2703 | EXPORT_SYMBOL(netdev_set_sb_channel); | |
2704 | ||
f0796d5c JF |
2705 | /* |
2706 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
3a053b1a | 2707 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
f0796d5c | 2708 | */ |
e6484930 | 2709 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2710 | { |
ac5b7019 | 2711 | bool disabling; |
1d24eb48 TH |
2712 | int rc; |
2713 | ||
ac5b7019 JK |
2714 | disabling = txq < dev->real_num_tx_queues; |
2715 | ||
e6484930 TH |
2716 | if (txq < 1 || txq > dev->num_tx_queues) |
2717 | return -EINVAL; | |
f0796d5c | 2718 | |
5c56580b BH |
2719 | if (dev->reg_state == NETREG_REGISTERED || |
2720 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2721 | ASSERT_RTNL(); |
2722 | ||
1d24eb48 TH |
2723 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2724 | txq); | |
bf264145 TH |
2725 | if (rc) |
2726 | return rc; | |
2727 | ||
4f57c087 JF |
2728 | if (dev->num_tc) |
2729 | netif_setup_tc(dev, txq); | |
2730 | ||
ac5b7019 JK |
2731 | dev->real_num_tx_queues = txq; |
2732 | ||
2733 | if (disabling) { | |
2734 | synchronize_net(); | |
e6484930 | 2735 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2736 | #ifdef CONFIG_XPS |
2737 | netif_reset_xps_queues_gt(dev, txq); | |
2738 | #endif | |
2739 | } | |
ac5b7019 JK |
2740 | } else { |
2741 | dev->real_num_tx_queues = txq; | |
f0796d5c | 2742 | } |
e6484930 | 2743 | |
e6484930 | 2744 | return 0; |
f0796d5c JF |
2745 | } |
2746 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2747 | |
a953be53 | 2748 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
2749 | /** |
2750 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2751 | * @dev: Network device | |
2752 | * @rxq: Actual number of RX queues | |
2753 | * | |
2754 | * This must be called either with the rtnl_lock held or before | |
2755 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2756 | * negative error code. If called before registration, it always |
2757 | * succeeds. | |
62fe0b40 BH |
2758 | */ |
2759 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2760 | { | |
2761 | int rc; | |
2762 | ||
bd25fa7b TH |
2763 | if (rxq < 1 || rxq > dev->num_rx_queues) |
2764 | return -EINVAL; | |
2765 | ||
62fe0b40 BH |
2766 | if (dev->reg_state == NETREG_REGISTERED) { |
2767 | ASSERT_RTNL(); | |
2768 | ||
62fe0b40 BH |
2769 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
2770 | rxq); | |
2771 | if (rc) | |
2772 | return rc; | |
62fe0b40 BH |
2773 | } |
2774 | ||
2775 | dev->real_num_rx_queues = rxq; | |
2776 | return 0; | |
2777 | } | |
2778 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
2779 | #endif | |
2780 | ||
2c53040f BH |
2781 | /** |
2782 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 YM |
2783 | * |
2784 | * This routine should set an upper limit on the number of RSS queues | |
2785 | * used by default by multiqueue devices. | |
2786 | */ | |
a55b138b | 2787 | int netif_get_num_default_rss_queues(void) |
16917b87 | 2788 | { |
40e4e713 HS |
2789 | return is_kdump_kernel() ? |
2790 | 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); | |
16917b87 YM |
2791 | } |
2792 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
2793 | ||
3bcb846c | 2794 | static void __netif_reschedule(struct Qdisc *q) |
56079431 | 2795 | { |
def82a1d JP |
2796 | struct softnet_data *sd; |
2797 | unsigned long flags; | |
56079431 | 2798 | |
def82a1d | 2799 | local_irq_save(flags); |
903ceff7 | 2800 | sd = this_cpu_ptr(&softnet_data); |
a9cbd588 CG |
2801 | q->next_sched = NULL; |
2802 | *sd->output_queue_tailp = q; | |
2803 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
2804 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
2805 | local_irq_restore(flags); | |
2806 | } | |
2807 | ||
2808 | void __netif_schedule(struct Qdisc *q) | |
2809 | { | |
2810 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
2811 | __netif_reschedule(q); | |
56079431 DV |
2812 | } |
2813 | EXPORT_SYMBOL(__netif_schedule); | |
2814 | ||
e6247027 ED |
2815 | struct dev_kfree_skb_cb { |
2816 | enum skb_free_reason reason; | |
2817 | }; | |
2818 | ||
2819 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) | |
56079431 | 2820 | { |
e6247027 ED |
2821 | return (struct dev_kfree_skb_cb *)skb->cb; |
2822 | } | |
2823 | ||
46e5da40 JF |
2824 | void netif_schedule_queue(struct netdev_queue *txq) |
2825 | { | |
2826 | rcu_read_lock(); | |
5be5515a | 2827 | if (!netif_xmit_stopped(txq)) { |
46e5da40 JF |
2828 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
2829 | ||
2830 | __netif_schedule(q); | |
2831 | } | |
2832 | rcu_read_unlock(); | |
2833 | } | |
2834 | EXPORT_SYMBOL(netif_schedule_queue); | |
2835 | ||
46e5da40 JF |
2836 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
2837 | { | |
2838 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { | |
2839 | struct Qdisc *q; | |
2840 | ||
2841 | rcu_read_lock(); | |
2842 | q = rcu_dereference(dev_queue->qdisc); | |
2843 | __netif_schedule(q); | |
2844 | rcu_read_unlock(); | |
2845 | } | |
2846 | } | |
2847 | EXPORT_SYMBOL(netif_tx_wake_queue); | |
2848 | ||
e6247027 | 2849 | void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 | 2850 | { |
e6247027 | 2851 | unsigned long flags; |
56079431 | 2852 | |
9899886d MJ |
2853 | if (unlikely(!skb)) |
2854 | return; | |
2855 | ||
63354797 | 2856 | if (likely(refcount_read(&skb->users) == 1)) { |
e6247027 | 2857 | smp_rmb(); |
63354797 RE |
2858 | refcount_set(&skb->users, 0); |
2859 | } else if (likely(!refcount_dec_and_test(&skb->users))) { | |
e6247027 | 2860 | return; |
bea3348e | 2861 | } |
e6247027 ED |
2862 | get_kfree_skb_cb(skb)->reason = reason; |
2863 | local_irq_save(flags); | |
2864 | skb->next = __this_cpu_read(softnet_data.completion_queue); | |
2865 | __this_cpu_write(softnet_data.completion_queue, skb); | |
2866 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
2867 | local_irq_restore(flags); | |
56079431 | 2868 | } |
e6247027 | 2869 | EXPORT_SYMBOL(__dev_kfree_skb_irq); |
56079431 | 2870 | |
e6247027 | 2871 | void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 DV |
2872 | { |
2873 | if (in_irq() || irqs_disabled()) | |
e6247027 | 2874 | __dev_kfree_skb_irq(skb, reason); |
56079431 DV |
2875 | else |
2876 | dev_kfree_skb(skb); | |
2877 | } | |
e6247027 | 2878 | EXPORT_SYMBOL(__dev_kfree_skb_any); |
56079431 DV |
2879 | |
2880 | ||
bea3348e SH |
2881 | /** |
2882 | * netif_device_detach - mark device as removed | |
2883 | * @dev: network device | |
2884 | * | |
2885 | * Mark device as removed from system and therefore no longer available. | |
2886 | */ | |
56079431 DV |
2887 | void netif_device_detach(struct net_device *dev) |
2888 | { | |
2889 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
2890 | netif_running(dev)) { | |
d543103a | 2891 | netif_tx_stop_all_queues(dev); |
56079431 DV |
2892 | } |
2893 | } | |
2894 | EXPORT_SYMBOL(netif_device_detach); | |
2895 | ||
bea3348e SH |
2896 | /** |
2897 | * netif_device_attach - mark device as attached | |
2898 | * @dev: network device | |
2899 | * | |
2900 | * Mark device as attached from system and restart if needed. | |
2901 | */ | |
56079431 DV |
2902 | void netif_device_attach(struct net_device *dev) |
2903 | { | |
2904 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
2905 | netif_running(dev)) { | |
d543103a | 2906 | netif_tx_wake_all_queues(dev); |
4ec93edb | 2907 | __netdev_watchdog_up(dev); |
56079431 DV |
2908 | } |
2909 | } | |
2910 | EXPORT_SYMBOL(netif_device_attach); | |
2911 | ||
5605c762 JP |
2912 | /* |
2913 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
2914 | * to be used as a distribution range. | |
2915 | */ | |
eadec877 AD |
2916 | static u16 skb_tx_hash(const struct net_device *dev, |
2917 | const struct net_device *sb_dev, | |
2918 | struct sk_buff *skb) | |
5605c762 JP |
2919 | { |
2920 | u32 hash; | |
2921 | u16 qoffset = 0; | |
1b837d48 | 2922 | u16 qcount = dev->real_num_tx_queues; |
5605c762 | 2923 | |
eadec877 AD |
2924 | if (dev->num_tc) { |
2925 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
2926 | ||
2927 | qoffset = sb_dev->tc_to_txq[tc].offset; | |
2928 | qcount = sb_dev->tc_to_txq[tc].count; | |
2929 | } | |
2930 | ||
5605c762 JP |
2931 | if (skb_rx_queue_recorded(skb)) { |
2932 | hash = skb_get_rx_queue(skb); | |
1b837d48 AD |
2933 | while (unlikely(hash >= qcount)) |
2934 | hash -= qcount; | |
eadec877 | 2935 | return hash + qoffset; |
5605c762 JP |
2936 | } |
2937 | ||
2938 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; | |
2939 | } | |
5605c762 | 2940 | |
36c92474 BH |
2941 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
2942 | { | |
84d15ae5 | 2943 | static const netdev_features_t null_features; |
36c92474 | 2944 | struct net_device *dev = skb->dev; |
88ad4175 | 2945 | const char *name = ""; |
36c92474 | 2946 | |
c846ad9b BG |
2947 | if (!net_ratelimit()) |
2948 | return; | |
2949 | ||
88ad4175 BM |
2950 | if (dev) { |
2951 | if (dev->dev.parent) | |
2952 | name = dev_driver_string(dev->dev.parent); | |
2953 | else | |
2954 | name = netdev_name(dev); | |
2955 | } | |
6413139d WB |
2956 | skb_dump(KERN_WARNING, skb, false); |
2957 | WARN(1, "%s: caps=(%pNF, %pNF)\n", | |
88ad4175 | 2958 | name, dev ? &dev->features : &null_features, |
6413139d | 2959 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
36c92474 BH |
2960 | } |
2961 | ||
1da177e4 LT |
2962 | /* |
2963 | * Invalidate hardware checksum when packet is to be mangled, and | |
2964 | * complete checksum manually on outgoing path. | |
2965 | */ | |
84fa7933 | 2966 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 2967 | { |
d3bc23e7 | 2968 | __wsum csum; |
663ead3b | 2969 | int ret = 0, offset; |
1da177e4 | 2970 | |
84fa7933 | 2971 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
2972 | goto out_set_summed; |
2973 | ||
2974 | if (unlikely(skb_shinfo(skb)->gso_size)) { | |
36c92474 BH |
2975 | skb_warn_bad_offload(skb); |
2976 | return -EINVAL; | |
1da177e4 LT |
2977 | } |
2978 | ||
cef401de ED |
2979 | /* Before computing a checksum, we should make sure no frag could |
2980 | * be modified by an external entity : checksum could be wrong. | |
2981 | */ | |
2982 | if (skb_has_shared_frag(skb)) { | |
2983 | ret = __skb_linearize(skb); | |
2984 | if (ret) | |
2985 | goto out; | |
2986 | } | |
2987 | ||
55508d60 | 2988 | offset = skb_checksum_start_offset(skb); |
a030847e HX |
2989 | BUG_ON(offset >= skb_headlen(skb)); |
2990 | csum = skb_checksum(skb, offset, skb->len - offset, 0); | |
2991 | ||
2992 | offset += skb->csum_offset; | |
2993 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); | |
2994 | ||
2995 | if (skb_cloned(skb) && | |
2996 | !skb_clone_writable(skb, offset + sizeof(__sum16))) { | |
1da177e4 LT |
2997 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
2998 | if (ret) | |
2999 | goto out; | |
3000 | } | |
3001 | ||
4f2e4ad5 | 3002 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
a430a43d | 3003 | out_set_summed: |
1da177e4 | 3004 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 3005 | out: |
1da177e4 LT |
3006 | return ret; |
3007 | } | |
d1b19dff | 3008 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 3009 | |
b72b5bf6 DC |
3010 | int skb_crc32c_csum_help(struct sk_buff *skb) |
3011 | { | |
3012 | __le32 crc32c_csum; | |
3013 | int ret = 0, offset, start; | |
3014 | ||
3015 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
3016 | goto out; | |
3017 | ||
3018 | if (unlikely(skb_is_gso(skb))) | |
3019 | goto out; | |
3020 | ||
3021 | /* Before computing a checksum, we should make sure no frag could | |
3022 | * be modified by an external entity : checksum could be wrong. | |
3023 | */ | |
3024 | if (unlikely(skb_has_shared_frag(skb))) { | |
3025 | ret = __skb_linearize(skb); | |
3026 | if (ret) | |
3027 | goto out; | |
3028 | } | |
3029 | start = skb_checksum_start_offset(skb); | |
3030 | offset = start + offsetof(struct sctphdr, checksum); | |
3031 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { | |
3032 | ret = -EINVAL; | |
3033 | goto out; | |
3034 | } | |
3035 | if (skb_cloned(skb) && | |
3036 | !skb_clone_writable(skb, offset + sizeof(__le32))) { | |
3037 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); | |
3038 | if (ret) | |
3039 | goto out; | |
3040 | } | |
3041 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, | |
3042 | skb->len - start, ~(__u32)0, | |
3043 | crc32c_csum_stub)); | |
3044 | *(__le32 *)(skb->data + offset) = crc32c_csum; | |
3045 | skb->ip_summed = CHECKSUM_NONE; | |
dba00306 | 3046 | skb->csum_not_inet = 0; |
b72b5bf6 DC |
3047 | out: |
3048 | return ret; | |
3049 | } | |
3050 | ||
53d6471c | 3051 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
f6a78bfc | 3052 | { |
252e3346 | 3053 | __be16 type = skb->protocol; |
f6a78bfc | 3054 | |
19acc327 PS |
3055 | /* Tunnel gso handlers can set protocol to ethernet. */ |
3056 | if (type == htons(ETH_P_TEB)) { | |
3057 | struct ethhdr *eth; | |
3058 | ||
3059 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) | |
3060 | return 0; | |
3061 | ||
1dfe82eb | 3062 | eth = (struct ethhdr *)skb->data; |
19acc327 PS |
3063 | type = eth->h_proto; |
3064 | } | |
3065 | ||
d4bcef3f | 3066 | return __vlan_get_protocol(skb, type, depth); |
ec5f0615 PS |
3067 | } |
3068 | ||
3069 | /** | |
3070 | * skb_mac_gso_segment - mac layer segmentation handler. | |
3071 | * @skb: buffer to segment | |
3072 | * @features: features for the output path (see dev->features) | |
3073 | */ | |
3074 | struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb, | |
3075 | netdev_features_t features) | |
3076 | { | |
3077 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); | |
3078 | struct packet_offload *ptype; | |
53d6471c VY |
3079 | int vlan_depth = skb->mac_len; |
3080 | __be16 type = skb_network_protocol(skb, &vlan_depth); | |
ec5f0615 PS |
3081 | |
3082 | if (unlikely(!type)) | |
3083 | return ERR_PTR(-EINVAL); | |
3084 | ||
53d6471c | 3085 | __skb_pull(skb, vlan_depth); |
f6a78bfc HX |
3086 | |
3087 | rcu_read_lock(); | |
22061d80 | 3088 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
f191a1d1 | 3089 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
f191a1d1 | 3090 | segs = ptype->callbacks.gso_segment(skb, features); |
f6a78bfc HX |
3091 | break; |
3092 | } | |
3093 | } | |
3094 | rcu_read_unlock(); | |
3095 | ||
98e399f8 | 3096 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
576a30eb | 3097 | |
f6a78bfc HX |
3098 | return segs; |
3099 | } | |
05e8ef4a PS |
3100 | EXPORT_SYMBOL(skb_mac_gso_segment); |
3101 | ||
3102 | ||
3103 | /* openvswitch calls this on rx path, so we need a different check. | |
3104 | */ | |
3105 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) | |
3106 | { | |
3107 | if (tx_path) | |
0c19f846 WB |
3108 | return skb->ip_summed != CHECKSUM_PARTIAL && |
3109 | skb->ip_summed != CHECKSUM_UNNECESSARY; | |
6e7bc478 ED |
3110 | |
3111 | return skb->ip_summed == CHECKSUM_NONE; | |
05e8ef4a PS |
3112 | } |
3113 | ||
3114 | /** | |
3115 | * __skb_gso_segment - Perform segmentation on skb. | |
3116 | * @skb: buffer to segment | |
3117 | * @features: features for the output path (see dev->features) | |
3118 | * @tx_path: whether it is called in TX path | |
3119 | * | |
3120 | * This function segments the given skb and returns a list of segments. | |
3121 | * | |
3122 | * It may return NULL if the skb requires no segmentation. This is | |
3123 | * only possible when GSO is used for verifying header integrity. | |
9207f9d4 KK |
3124 | * |
3125 | * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb. | |
05e8ef4a PS |
3126 | */ |
3127 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, | |
3128 | netdev_features_t features, bool tx_path) | |
3129 | { | |
b2504a5d ED |
3130 | struct sk_buff *segs; |
3131 | ||
05e8ef4a PS |
3132 | if (unlikely(skb_needs_check(skb, tx_path))) { |
3133 | int err; | |
3134 | ||
b2504a5d | 3135 | /* We're going to init ->check field in TCP or UDP header */ |
a40e0a66 | 3136 | err = skb_cow_head(skb, 0); |
3137 | if (err < 0) | |
05e8ef4a PS |
3138 | return ERR_PTR(err); |
3139 | } | |
3140 | ||
802ab55a AD |
3141 | /* Only report GSO partial support if it will enable us to |
3142 | * support segmentation on this frame without needing additional | |
3143 | * work. | |
3144 | */ | |
3145 | if (features & NETIF_F_GSO_PARTIAL) { | |
3146 | netdev_features_t partial_features = NETIF_F_GSO_ROBUST; | |
3147 | struct net_device *dev = skb->dev; | |
3148 | ||
3149 | partial_features |= dev->features & dev->gso_partial_features; | |
3150 | if (!skb_gso_ok(skb, features | partial_features)) | |
3151 | features &= ~NETIF_F_GSO_PARTIAL; | |
3152 | } | |
3153 | ||
9207f9d4 KK |
3154 | BUILD_BUG_ON(SKB_SGO_CB_OFFSET + |
3155 | sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb)); | |
3156 | ||
68c33163 | 3157 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
3347c960 ED |
3158 | SKB_GSO_CB(skb)->encap_level = 0; |
3159 | ||
05e8ef4a PS |
3160 | skb_reset_mac_header(skb); |
3161 | skb_reset_mac_len(skb); | |
3162 | ||
b2504a5d ED |
3163 | segs = skb_mac_gso_segment(skb, features); |
3164 | ||
8d74e9f8 | 3165 | if (unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs))) |
b2504a5d ED |
3166 | skb_warn_bad_offload(skb); |
3167 | ||
3168 | return segs; | |
05e8ef4a | 3169 | } |
12b0004d | 3170 | EXPORT_SYMBOL(__skb_gso_segment); |
f6a78bfc | 3171 | |
fb286bb2 HX |
3172 | /* Take action when hardware reception checksum errors are detected. */ |
3173 | #ifdef CONFIG_BUG | |
7fe50ac8 | 3174 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
fb286bb2 HX |
3175 | { |
3176 | if (net_ratelimit()) { | |
7b6cd1ce | 3177 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
6413139d | 3178 | skb_dump(KERN_ERR, skb, true); |
fb286bb2 HX |
3179 | dump_stack(); |
3180 | } | |
3181 | } | |
3182 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
3183 | #endif | |
3184 | ||
ab74cfeb | 3185 | /* XXX: check that highmem exists at all on the given machine. */ |
c1e756bf | 3186 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 3187 | { |
3d3a8533 | 3188 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 3189 | int i; |
f4563a75 | 3190 | |
5acbbd42 | 3191 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
3192 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
3193 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
f4563a75 | 3194 | |
ea2ab693 | 3195 | if (PageHighMem(skb_frag_page(frag))) |
5acbbd42 | 3196 | return 1; |
ea2ab693 | 3197 | } |
5acbbd42 | 3198 | } |
3d3a8533 | 3199 | #endif |
1da177e4 LT |
3200 | return 0; |
3201 | } | |
1da177e4 | 3202 | |
3b392ddb SH |
3203 | /* If MPLS offload request, verify we are testing hardware MPLS features |
3204 | * instead of standard features for the netdev. | |
3205 | */ | |
d0edc7bf | 3206 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
3b392ddb SH |
3207 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
3208 | netdev_features_t features, | |
3209 | __be16 type) | |
3210 | { | |
25cd9ba0 | 3211 | if (eth_p_mpls(type)) |
3b392ddb SH |
3212 | features &= skb->dev->mpls_features; |
3213 | ||
3214 | return features; | |
3215 | } | |
3216 | #else | |
3217 | static netdev_features_t net_mpls_features(struct sk_buff *skb, | |
3218 | netdev_features_t features, | |
3219 | __be16 type) | |
3220 | { | |
3221 | return features; | |
3222 | } | |
3223 | #endif | |
3224 | ||
c8f44aff | 3225 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
c1e756bf | 3226 | netdev_features_t features) |
f01a5236 | 3227 | { |
53d6471c | 3228 | int tmp; |
3b392ddb SH |
3229 | __be16 type; |
3230 | ||
3231 | type = skb_network_protocol(skb, &tmp); | |
3232 | features = net_mpls_features(skb, features, type); | |
53d6471c | 3233 | |
c0d680e5 | 3234 | if (skb->ip_summed != CHECKSUM_NONE && |
3b392ddb | 3235 | !can_checksum_protocol(features, type)) { |
996e8021 | 3236 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
f01a5236 | 3237 | } |
7be2c82c ED |
3238 | if (illegal_highdma(skb->dev, skb)) |
3239 | features &= ~NETIF_F_SG; | |
f01a5236 JG |
3240 | |
3241 | return features; | |
3242 | } | |
3243 | ||
e38f3025 TM |
3244 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
3245 | struct net_device *dev, | |
3246 | netdev_features_t features) | |
3247 | { | |
3248 | return features; | |
3249 | } | |
3250 | EXPORT_SYMBOL(passthru_features_check); | |
3251 | ||
7ce23672 | 3252 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
8cb65d00 TM |
3253 | struct net_device *dev, |
3254 | netdev_features_t features) | |
3255 | { | |
3256 | return vlan_features_check(skb, features); | |
3257 | } | |
3258 | ||
cbc53e08 AD |
3259 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
3260 | struct net_device *dev, | |
3261 | netdev_features_t features) | |
3262 | { | |
3263 | u16 gso_segs = skb_shinfo(skb)->gso_segs; | |
3264 | ||
3265 | if (gso_segs > dev->gso_max_segs) | |
3266 | return features & ~NETIF_F_GSO_MASK; | |
3267 | ||
802ab55a AD |
3268 | /* Support for GSO partial features requires software |
3269 | * intervention before we can actually process the packets | |
3270 | * so we need to strip support for any partial features now | |
3271 | * and we can pull them back in after we have partially | |
3272 | * segmented the frame. | |
3273 | */ | |
3274 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) | |
3275 | features &= ~dev->gso_partial_features; | |
3276 | ||
3277 | /* Make sure to clear the IPv4 ID mangling feature if the | |
3278 | * IPv4 header has the potential to be fragmented. | |
cbc53e08 AD |
3279 | */ |
3280 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
3281 | struct iphdr *iph = skb->encapsulation ? | |
3282 | inner_ip_hdr(skb) : ip_hdr(skb); | |
3283 | ||
3284 | if (!(iph->frag_off & htons(IP_DF))) | |
3285 | features &= ~NETIF_F_TSO_MANGLEID; | |
3286 | } | |
3287 | ||
3288 | return features; | |
3289 | } | |
3290 | ||
c1e756bf | 3291 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 | 3292 | { |
5f35227e | 3293 | struct net_device *dev = skb->dev; |
fcbeb976 | 3294 | netdev_features_t features = dev->features; |
58e998c6 | 3295 | |
cbc53e08 AD |
3296 | if (skb_is_gso(skb)) |
3297 | features = gso_features_check(skb, dev, features); | |
30b678d8 | 3298 | |
5f35227e JG |
3299 | /* If encapsulation offload request, verify we are testing |
3300 | * hardware encapsulation features instead of standard | |
3301 | * features for the netdev | |
3302 | */ | |
3303 | if (skb->encapsulation) | |
3304 | features &= dev->hw_enc_features; | |
3305 | ||
f5a7fb88 TM |
3306 | if (skb_vlan_tagged(skb)) |
3307 | features = netdev_intersect_features(features, | |
3308 | dev->vlan_features | | |
3309 | NETIF_F_HW_VLAN_CTAG_TX | | |
3310 | NETIF_F_HW_VLAN_STAG_TX); | |
f01a5236 | 3311 | |
5f35227e JG |
3312 | if (dev->netdev_ops->ndo_features_check) |
3313 | features &= dev->netdev_ops->ndo_features_check(skb, dev, | |
3314 | features); | |
8cb65d00 TM |
3315 | else |
3316 | features &= dflt_features_check(skb, dev, features); | |
5f35227e | 3317 | |
c1e756bf | 3318 | return harmonize_features(skb, features); |
58e998c6 | 3319 | } |
c1e756bf | 3320 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 3321 | |
2ea25513 | 3322 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
95f6b3dd | 3323 | struct netdev_queue *txq, bool more) |
f6a78bfc | 3324 | { |
2ea25513 DM |
3325 | unsigned int len; |
3326 | int rc; | |
00829823 | 3327 | |
9f9a742d | 3328 | if (dev_nit_active(dev)) |
2ea25513 | 3329 | dev_queue_xmit_nit(skb, dev); |
fc741216 | 3330 | |
2ea25513 DM |
3331 | len = skb->len; |
3332 | trace_net_dev_start_xmit(skb, dev); | |
95f6b3dd | 3333 | rc = netdev_start_xmit(skb, dev, txq, more); |
2ea25513 | 3334 | trace_net_dev_xmit(skb, rc, dev, len); |
adf30907 | 3335 | |
2ea25513 DM |
3336 | return rc; |
3337 | } | |
7b9c6090 | 3338 | |
8dcda22a DM |
3339 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
3340 | struct netdev_queue *txq, int *ret) | |
7f2e870f DM |
3341 | { |
3342 | struct sk_buff *skb = first; | |
3343 | int rc = NETDEV_TX_OK; | |
7b9c6090 | 3344 | |
7f2e870f DM |
3345 | while (skb) { |
3346 | struct sk_buff *next = skb->next; | |
fc70fb64 | 3347 | |
a8305bff | 3348 | skb_mark_not_on_list(skb); |
95f6b3dd | 3349 | rc = xmit_one(skb, dev, txq, next != NULL); |
7f2e870f DM |
3350 | if (unlikely(!dev_xmit_complete(rc))) { |
3351 | skb->next = next; | |
3352 | goto out; | |
3353 | } | |
6afff0ca | 3354 | |
7f2e870f | 3355 | skb = next; |
fe60faa5 | 3356 | if (netif_tx_queue_stopped(txq) && skb) { |
7f2e870f DM |
3357 | rc = NETDEV_TX_BUSY; |
3358 | break; | |
9ccb8975 | 3359 | } |
7f2e870f | 3360 | } |
9ccb8975 | 3361 | |
7f2e870f DM |
3362 | out: |
3363 | *ret = rc; | |
3364 | return skb; | |
3365 | } | |
b40863c6 | 3366 | |
1ff0dc94 ED |
3367 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
3368 | netdev_features_t features) | |
f6a78bfc | 3369 | { |
df8a39de | 3370 | if (skb_vlan_tag_present(skb) && |
5968250c JP |
3371 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
3372 | skb = __vlan_hwaccel_push_inside(skb); | |
eae3f88e DM |
3373 | return skb; |
3374 | } | |
f6a78bfc | 3375 | |
43c26a1a DC |
3376 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
3377 | const netdev_features_t features) | |
3378 | { | |
3379 | if (unlikely(skb->csum_not_inet)) | |
3380 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : | |
3381 | skb_crc32c_csum_help(skb); | |
3382 | ||
3383 | return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb); | |
3384 | } | |
3385 | EXPORT_SYMBOL(skb_csum_hwoffload_help); | |
3386 | ||
f53c7239 | 3387 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
eae3f88e DM |
3388 | { |
3389 | netdev_features_t features; | |
f6a78bfc | 3390 | |
eae3f88e DM |
3391 | features = netif_skb_features(skb); |
3392 | skb = validate_xmit_vlan(skb, features); | |
3393 | if (unlikely(!skb)) | |
3394 | goto out_null; | |
7b9c6090 | 3395 | |
ebf4e808 IL |
3396 | skb = sk_validate_xmit_skb(skb, dev); |
3397 | if (unlikely(!skb)) | |
3398 | goto out_null; | |
3399 | ||
8b86a61d | 3400 | if (netif_needs_gso(skb, features)) { |
ce93718f DM |
3401 | struct sk_buff *segs; |
3402 | ||
3403 | segs = skb_gso_segment(skb, features); | |
cecda693 | 3404 | if (IS_ERR(segs)) { |
af6dabc9 | 3405 | goto out_kfree_skb; |
cecda693 JW |
3406 | } else if (segs) { |
3407 | consume_skb(skb); | |
3408 | skb = segs; | |
f6a78bfc | 3409 | } |
eae3f88e DM |
3410 | } else { |
3411 | if (skb_needs_linearize(skb, features) && | |
3412 | __skb_linearize(skb)) | |
3413 | goto out_kfree_skb; | |
4ec93edb | 3414 | |
eae3f88e DM |
3415 | /* If packet is not checksummed and device does not |
3416 | * support checksumming for this protocol, complete | |
3417 | * checksumming here. | |
3418 | */ | |
3419 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
3420 | if (skb->encapsulation) | |
3421 | skb_set_inner_transport_header(skb, | |
3422 | skb_checksum_start_offset(skb)); | |
3423 | else | |
3424 | skb_set_transport_header(skb, | |
3425 | skb_checksum_start_offset(skb)); | |
43c26a1a | 3426 | if (skb_csum_hwoffload_help(skb, features)) |
eae3f88e | 3427 | goto out_kfree_skb; |
7b9c6090 | 3428 | } |
0c772159 | 3429 | } |
7b9c6090 | 3430 | |
f53c7239 | 3431 | skb = validate_xmit_xfrm(skb, features, again); |
3dca3f38 | 3432 | |
eae3f88e | 3433 | return skb; |
fc70fb64 | 3434 | |
f6a78bfc HX |
3435 | out_kfree_skb: |
3436 | kfree_skb(skb); | |
eae3f88e | 3437 | out_null: |
d21fd63e | 3438 | atomic_long_inc(&dev->tx_dropped); |
eae3f88e DM |
3439 | return NULL; |
3440 | } | |
6afff0ca | 3441 | |
f53c7239 | 3442 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
55a93b3e ED |
3443 | { |
3444 | struct sk_buff *next, *head = NULL, *tail; | |
3445 | ||
bec3cfdc | 3446 | for (; skb != NULL; skb = next) { |
55a93b3e | 3447 | next = skb->next; |
a8305bff | 3448 | skb_mark_not_on_list(skb); |
bec3cfdc ED |
3449 | |
3450 | /* in case skb wont be segmented, point to itself */ | |
3451 | skb->prev = skb; | |
3452 | ||
f53c7239 | 3453 | skb = validate_xmit_skb(skb, dev, again); |
bec3cfdc ED |
3454 | if (!skb) |
3455 | continue; | |
55a93b3e | 3456 | |
bec3cfdc ED |
3457 | if (!head) |
3458 | head = skb; | |
3459 | else | |
3460 | tail->next = skb; | |
3461 | /* If skb was segmented, skb->prev points to | |
3462 | * the last segment. If not, it still contains skb. | |
3463 | */ | |
3464 | tail = skb->prev; | |
55a93b3e ED |
3465 | } |
3466 | return head; | |
f6a78bfc | 3467 | } |
104ba78c | 3468 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
f6a78bfc | 3469 | |
1def9238 ED |
3470 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
3471 | { | |
3472 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
3473 | ||
3474 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
3475 | ||
3476 | /* To get more precise estimation of bytes sent on wire, | |
3477 | * we add to pkt_len the headers size of all segments | |
3478 | */ | |
a0dce875 | 3479 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
757b8b1d | 3480 | unsigned int hdr_len; |
15e5a030 | 3481 | u16 gso_segs = shinfo->gso_segs; |
1def9238 | 3482 | |
757b8b1d ED |
3483 | /* mac layer + network layer */ |
3484 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); | |
3485 | ||
3486 | /* + transport layer */ | |
7c68d1a6 ED |
3487 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
3488 | const struct tcphdr *th; | |
3489 | struct tcphdr _tcphdr; | |
3490 | ||
3491 | th = skb_header_pointer(skb, skb_transport_offset(skb), | |
3492 | sizeof(_tcphdr), &_tcphdr); | |
3493 | if (likely(th)) | |
3494 | hdr_len += __tcp_hdrlen(th); | |
3495 | } else { | |
3496 | struct udphdr _udphdr; | |
3497 | ||
3498 | if (skb_header_pointer(skb, skb_transport_offset(skb), | |
3499 | sizeof(_udphdr), &_udphdr)) | |
3500 | hdr_len += sizeof(struct udphdr); | |
3501 | } | |
15e5a030 JW |
3502 | |
3503 | if (shinfo->gso_type & SKB_GSO_DODGY) | |
3504 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, | |
3505 | shinfo->gso_size); | |
3506 | ||
3507 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; | |
1def9238 ED |
3508 | } |
3509 | } | |
3510 | ||
bbd8a0d3 KK |
3511 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
3512 | struct net_device *dev, | |
3513 | struct netdev_queue *txq) | |
3514 | { | |
3515 | spinlock_t *root_lock = qdisc_lock(q); | |
520ac30f | 3516 | struct sk_buff *to_free = NULL; |
a2da570d | 3517 | bool contended; |
bbd8a0d3 KK |
3518 | int rc; |
3519 | ||
a2da570d | 3520 | qdisc_calculate_pkt_len(skb, q); |
6b3ba914 JF |
3521 | |
3522 | if (q->flags & TCQ_F_NOLOCK) { | |
d518d2ed PA |
3523 | if ((q->flags & TCQ_F_CAN_BYPASS) && q->empty && |
3524 | qdisc_run_begin(q)) { | |
3525 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, | |
3526 | &q->state))) { | |
3527 | __qdisc_drop(skb, &to_free); | |
3528 | rc = NET_XMIT_DROP; | |
3529 | goto end_run; | |
3530 | } | |
ba27b4cd PA |
3531 | qdisc_bstats_cpu_update(q, skb); |
3532 | ||
d518d2ed | 3533 | rc = NET_XMIT_SUCCESS; |
ba27b4cd PA |
3534 | if (sch_direct_xmit(skb, q, dev, txq, NULL, true)) |
3535 | __qdisc_run(q); | |
3536 | ||
d518d2ed | 3537 | end_run: |
ba27b4cd | 3538 | qdisc_run_end(q); |
6b3ba914 JF |
3539 | } else { |
3540 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; | |
32f7b44d | 3541 | qdisc_run(q); |
6b3ba914 JF |
3542 | } |
3543 | ||
3544 | if (unlikely(to_free)) | |
3545 | kfree_skb_list(to_free); | |
3546 | return rc; | |
3547 | } | |
3548 | ||
79640a4c ED |
3549 | /* |
3550 | * Heuristic to force contended enqueues to serialize on a | |
3551 | * separate lock before trying to get qdisc main lock. | |
f9eb8aea | 3552 | * This permits qdisc->running owner to get the lock more |
9bf2b8c2 | 3553 | * often and dequeue packets faster. |
79640a4c | 3554 | */ |
a2da570d | 3555 | contended = qdisc_is_running(q); |
79640a4c ED |
3556 | if (unlikely(contended)) |
3557 | spin_lock(&q->busylock); | |
3558 | ||
bbd8a0d3 KK |
3559 | spin_lock(root_lock); |
3560 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
520ac30f | 3561 | __qdisc_drop(skb, &to_free); |
bbd8a0d3 KK |
3562 | rc = NET_XMIT_DROP; |
3563 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 3564 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
3565 | /* |
3566 | * This is a work-conserving queue; there are no old skbs | |
3567 | * waiting to be sent out; and the qdisc is not running - | |
3568 | * xmit the skb directly. | |
3569 | */ | |
bfe0d029 | 3570 | |
bfe0d029 ED |
3571 | qdisc_bstats_update(q, skb); |
3572 | ||
55a93b3e | 3573 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
79640a4c ED |
3574 | if (unlikely(contended)) { |
3575 | spin_unlock(&q->busylock); | |
3576 | contended = false; | |
3577 | } | |
bbd8a0d3 | 3578 | __qdisc_run(q); |
6c148184 | 3579 | } |
bbd8a0d3 | 3580 | |
6c148184 | 3581 | qdisc_run_end(q); |
bbd8a0d3 KK |
3582 | rc = NET_XMIT_SUCCESS; |
3583 | } else { | |
520ac30f | 3584 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
79640a4c ED |
3585 | if (qdisc_run_begin(q)) { |
3586 | if (unlikely(contended)) { | |
3587 | spin_unlock(&q->busylock); | |
3588 | contended = false; | |
3589 | } | |
3590 | __qdisc_run(q); | |
6c148184 | 3591 | qdisc_run_end(q); |
79640a4c | 3592 | } |
bbd8a0d3 KK |
3593 | } |
3594 | spin_unlock(root_lock); | |
520ac30f ED |
3595 | if (unlikely(to_free)) |
3596 | kfree_skb_list(to_free); | |
79640a4c ED |
3597 | if (unlikely(contended)) |
3598 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
3599 | return rc; |
3600 | } | |
3601 | ||
86f8515f | 3602 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
5bc1421e NH |
3603 | static void skb_update_prio(struct sk_buff *skb) |
3604 | { | |
4dcb31d4 ED |
3605 | const struct netprio_map *map; |
3606 | const struct sock *sk; | |
3607 | unsigned int prioidx; | |
5bc1421e | 3608 | |
4dcb31d4 ED |
3609 | if (skb->priority) |
3610 | return; | |
3611 | map = rcu_dereference_bh(skb->dev->priomap); | |
3612 | if (!map) | |
3613 | return; | |
3614 | sk = skb_to_full_sk(skb); | |
3615 | if (!sk) | |
3616 | return; | |
91c68ce2 | 3617 | |
4dcb31d4 ED |
3618 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
3619 | ||
3620 | if (prioidx < map->priomap_len) | |
3621 | skb->priority = map->priomap[prioidx]; | |
5bc1421e NH |
3622 | } |
3623 | #else | |
3624 | #define skb_update_prio(skb) | |
3625 | #endif | |
3626 | ||
95603e22 MM |
3627 | /** |
3628 | * dev_loopback_xmit - loop back @skb | |
0c4b51f0 EB |
3629 | * @net: network namespace this loopback is happening in |
3630 | * @sk: sk needed to be a netfilter okfn | |
95603e22 MM |
3631 | * @skb: buffer to transmit |
3632 | */ | |
0c4b51f0 | 3633 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
95603e22 MM |
3634 | { |
3635 | skb_reset_mac_header(skb); | |
3636 | __skb_pull(skb, skb_network_offset(skb)); | |
3637 | skb->pkt_type = PACKET_LOOPBACK; | |
3638 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
3639 | WARN_ON(!skb_dst(skb)); | |
3640 | skb_dst_force(skb); | |
3641 | netif_rx_ni(skb); | |
3642 | return 0; | |
3643 | } | |
3644 | EXPORT_SYMBOL(dev_loopback_xmit); | |
3645 | ||
1f211a1b DB |
3646 | #ifdef CONFIG_NET_EGRESS |
3647 | static struct sk_buff * | |
3648 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
3649 | { | |
46209401 | 3650 | struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress); |
1f211a1b DB |
3651 | struct tcf_result cl_res; |
3652 | ||
46209401 | 3653 | if (!miniq) |
1f211a1b DB |
3654 | return skb; |
3655 | ||
8dc07fdb | 3656 | /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ |
46209401 | 3657 | mini_qdisc_bstats_cpu_update(miniq, skb); |
1f211a1b | 3658 | |
46209401 | 3659 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
1f211a1b DB |
3660 | case TC_ACT_OK: |
3661 | case TC_ACT_RECLASSIFY: | |
3662 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
3663 | break; | |
3664 | case TC_ACT_SHOT: | |
46209401 | 3665 | mini_qdisc_qstats_cpu_drop(miniq); |
1f211a1b | 3666 | *ret = NET_XMIT_DROP; |
7e2c3aea DB |
3667 | kfree_skb(skb); |
3668 | return NULL; | |
1f211a1b DB |
3669 | case TC_ACT_STOLEN: |
3670 | case TC_ACT_QUEUED: | |
e25ea21f | 3671 | case TC_ACT_TRAP: |
1f211a1b | 3672 | *ret = NET_XMIT_SUCCESS; |
7e2c3aea | 3673 | consume_skb(skb); |
1f211a1b DB |
3674 | return NULL; |
3675 | case TC_ACT_REDIRECT: | |
3676 | /* No need to push/pop skb's mac_header here on egress! */ | |
3677 | skb_do_redirect(skb); | |
3678 | *ret = NET_XMIT_SUCCESS; | |
3679 | return NULL; | |
3680 | default: | |
3681 | break; | |
3682 | } | |
3683 | ||
3684 | return skb; | |
3685 | } | |
3686 | #endif /* CONFIG_NET_EGRESS */ | |
3687 | ||
fc9bab24 AN |
3688 | #ifdef CONFIG_XPS |
3689 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, | |
3690 | struct xps_dev_maps *dev_maps, unsigned int tci) | |
3691 | { | |
3692 | struct xps_map *map; | |
3693 | int queue_index = -1; | |
3694 | ||
3695 | if (dev->num_tc) { | |
3696 | tci *= dev->num_tc; | |
3697 | tci += netdev_get_prio_tc_map(dev, skb->priority); | |
3698 | } | |
3699 | ||
3700 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
3701 | if (map) { | |
3702 | if (map->len == 1) | |
3703 | queue_index = map->queues[0]; | |
3704 | else | |
3705 | queue_index = map->queues[reciprocal_scale( | |
3706 | skb_get_hash(skb), map->len)]; | |
3707 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
3708 | queue_index = -1; | |
3709 | } | |
3710 | return queue_index; | |
3711 | } | |
3712 | #endif | |
3713 | ||
eadec877 AD |
3714 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
3715 | struct sk_buff *skb) | |
638b2a69 JP |
3716 | { |
3717 | #ifdef CONFIG_XPS | |
3718 | struct xps_dev_maps *dev_maps; | |
fc9bab24 | 3719 | struct sock *sk = skb->sk; |
638b2a69 JP |
3720 | int queue_index = -1; |
3721 | ||
04157469 AN |
3722 | if (!static_key_false(&xps_needed)) |
3723 | return -1; | |
3724 | ||
638b2a69 | 3725 | rcu_read_lock(); |
fc9bab24 AN |
3726 | if (!static_key_false(&xps_rxqs_needed)) |
3727 | goto get_cpus_map; | |
3728 | ||
eadec877 | 3729 | dev_maps = rcu_dereference(sb_dev->xps_rxqs_map); |
638b2a69 | 3730 | if (dev_maps) { |
fc9bab24 | 3731 | int tci = sk_rx_queue_get(sk); |
184c449f | 3732 | |
fc9bab24 AN |
3733 | if (tci >= 0 && tci < dev->num_rx_queues) |
3734 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3735 | tci); | |
3736 | } | |
184c449f | 3737 | |
fc9bab24 AN |
3738 | get_cpus_map: |
3739 | if (queue_index < 0) { | |
eadec877 | 3740 | dev_maps = rcu_dereference(sb_dev->xps_cpus_map); |
fc9bab24 AN |
3741 | if (dev_maps) { |
3742 | unsigned int tci = skb->sender_cpu - 1; | |
3743 | ||
3744 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3745 | tci); | |
638b2a69 JP |
3746 | } |
3747 | } | |
3748 | rcu_read_unlock(); | |
3749 | ||
3750 | return queue_index; | |
3751 | #else | |
3752 | return -1; | |
3753 | #endif | |
3754 | } | |
3755 | ||
a4ea8a3d | 3756 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 3757 | struct net_device *sb_dev) |
a4ea8a3d AD |
3758 | { |
3759 | return 0; | |
3760 | } | |
3761 | EXPORT_SYMBOL(dev_pick_tx_zero); | |
3762 | ||
3763 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, | |
a350ecce | 3764 | struct net_device *sb_dev) |
a4ea8a3d AD |
3765 | { |
3766 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; | |
3767 | } | |
3768 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); | |
3769 | ||
b71b5837 PA |
3770 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
3771 | struct net_device *sb_dev) | |
638b2a69 JP |
3772 | { |
3773 | struct sock *sk = skb->sk; | |
3774 | int queue_index = sk_tx_queue_get(sk); | |
3775 | ||
eadec877 AD |
3776 | sb_dev = sb_dev ? : dev; |
3777 | ||
638b2a69 JP |
3778 | if (queue_index < 0 || skb->ooo_okay || |
3779 | queue_index >= dev->real_num_tx_queues) { | |
eadec877 | 3780 | int new_index = get_xps_queue(dev, sb_dev, skb); |
f4563a75 | 3781 | |
638b2a69 | 3782 | if (new_index < 0) |
eadec877 | 3783 | new_index = skb_tx_hash(dev, sb_dev, skb); |
638b2a69 JP |
3784 | |
3785 | if (queue_index != new_index && sk && | |
004a5d01 | 3786 | sk_fullsock(sk) && |
638b2a69 JP |
3787 | rcu_access_pointer(sk->sk_dst_cache)) |
3788 | sk_tx_queue_set(sk, new_index); | |
3789 | ||
3790 | queue_index = new_index; | |
3791 | } | |
3792 | ||
3793 | return queue_index; | |
3794 | } | |
b71b5837 | 3795 | EXPORT_SYMBOL(netdev_pick_tx); |
638b2a69 | 3796 | |
4bd97d51 PA |
3797 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
3798 | struct sk_buff *skb, | |
3799 | struct net_device *sb_dev) | |
638b2a69 JP |
3800 | { |
3801 | int queue_index = 0; | |
3802 | ||
3803 | #ifdef CONFIG_XPS | |
52bd2d62 ED |
3804 | u32 sender_cpu = skb->sender_cpu - 1; |
3805 | ||
3806 | if (sender_cpu >= (u32)NR_CPUS) | |
638b2a69 JP |
3807 | skb->sender_cpu = raw_smp_processor_id() + 1; |
3808 | #endif | |
3809 | ||
3810 | if (dev->real_num_tx_queues != 1) { | |
3811 | const struct net_device_ops *ops = dev->netdev_ops; | |
f4563a75 | 3812 | |
638b2a69 | 3813 | if (ops->ndo_select_queue) |
a350ecce | 3814 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
638b2a69 | 3815 | else |
4bd97d51 | 3816 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
638b2a69 | 3817 | |
d584527c | 3818 | queue_index = netdev_cap_txqueue(dev, queue_index); |
638b2a69 JP |
3819 | } |
3820 | ||
3821 | skb_set_queue_mapping(skb, queue_index); | |
3822 | return netdev_get_tx_queue(dev, queue_index); | |
3823 | } | |
3824 | ||
d29f749e | 3825 | /** |
9d08dd3d | 3826 | * __dev_queue_xmit - transmit a buffer |
d29f749e | 3827 | * @skb: buffer to transmit |
eadec877 | 3828 | * @sb_dev: suboordinate device used for L2 forwarding offload |
d29f749e DJ |
3829 | * |
3830 | * Queue a buffer for transmission to a network device. The caller must | |
3831 | * have set the device and priority and built the buffer before calling | |
3832 | * this function. The function can be called from an interrupt. | |
3833 | * | |
3834 | * A negative errno code is returned on a failure. A success does not | |
3835 | * guarantee the frame will be transmitted as it may be dropped due | |
3836 | * to congestion or traffic shaping. | |
3837 | * | |
3838 | * ----------------------------------------------------------------------------------- | |
3839 | * I notice this method can also return errors from the queue disciplines, | |
3840 | * including NET_XMIT_DROP, which is a positive value. So, errors can also | |
3841 | * be positive. | |
3842 | * | |
3843 | * Regardless of the return value, the skb is consumed, so it is currently | |
3844 | * difficult to retry a send to this method. (You can bump the ref count | |
3845 | * before sending to hold a reference for retry if you are careful.) | |
3846 | * | |
3847 | * When calling this method, interrupts MUST be enabled. This is because | |
3848 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
3849 | * --BLG | |
3850 | */ | |
eadec877 | 3851 | static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
1da177e4 LT |
3852 | { |
3853 | struct net_device *dev = skb->dev; | |
dc2b4847 | 3854 | struct netdev_queue *txq; |
1da177e4 LT |
3855 | struct Qdisc *q; |
3856 | int rc = -ENOMEM; | |
f53c7239 | 3857 | bool again = false; |
1da177e4 | 3858 | |
6d1ccff6 ED |
3859 | skb_reset_mac_header(skb); |
3860 | ||
e7fd2885 WB |
3861 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
3862 | __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED); | |
3863 | ||
4ec93edb YH |
3864 | /* Disable soft irqs for various locks below. Also |
3865 | * stops preemption for RCU. | |
1da177e4 | 3866 | */ |
4ec93edb | 3867 | rcu_read_lock_bh(); |
1da177e4 | 3868 | |
5bc1421e NH |
3869 | skb_update_prio(skb); |
3870 | ||
1f211a1b DB |
3871 | qdisc_pkt_len_init(skb); |
3872 | #ifdef CONFIG_NET_CLS_ACT | |
8dc07fdb | 3873 | skb->tc_at_ingress = 0; |
1f211a1b | 3874 | # ifdef CONFIG_NET_EGRESS |
aabf6772 | 3875 | if (static_branch_unlikely(&egress_needed_key)) { |
1f211a1b DB |
3876 | skb = sch_handle_egress(skb, &rc, dev); |
3877 | if (!skb) | |
3878 | goto out; | |
3879 | } | |
3880 | # endif | |
3881 | #endif | |
02875878 ED |
3882 | /* If device/qdisc don't need skb->dst, release it right now while |
3883 | * its hot in this cpu cache. | |
3884 | */ | |
3885 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
3886 | skb_dst_drop(skb); | |
3887 | else | |
3888 | skb_dst_force(skb); | |
3889 | ||
4bd97d51 | 3890 | txq = netdev_core_pick_tx(dev, skb, sb_dev); |
a898def2 | 3891 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 3892 | |
cf66ba58 | 3893 | trace_net_dev_queue(skb); |
1da177e4 | 3894 | if (q->enqueue) { |
bbd8a0d3 | 3895 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 3896 | goto out; |
1da177e4 LT |
3897 | } |
3898 | ||
3899 | /* The device has no queue. Common case for software devices: | |
eb13da1a | 3900 | * loopback, all the sorts of tunnels... |
1da177e4 | 3901 | |
eb13da1a | 3902 | * Really, it is unlikely that netif_tx_lock protection is necessary |
3903 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
3904 | * counters.) | |
3905 | * However, it is possible, that they rely on protection | |
3906 | * made by us here. | |
1da177e4 | 3907 | |
eb13da1a | 3908 | * Check this and shot the lock. It is not prone from deadlocks. |
3909 | *Either shot noqueue qdisc, it is even simpler 8) | |
1da177e4 LT |
3910 | */ |
3911 | if (dev->flags & IFF_UP) { | |
3912 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
3913 | ||
c773e847 | 3914 | if (txq->xmit_lock_owner != cpu) { |
97cdcf37 | 3915 | if (dev_xmit_recursion()) |
745e20f1 ED |
3916 | goto recursion_alert; |
3917 | ||
f53c7239 | 3918 | skb = validate_xmit_skb(skb, dev, &again); |
1f59533f | 3919 | if (!skb) |
d21fd63e | 3920 | goto out; |
1f59533f | 3921 | |
c773e847 | 3922 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 3923 | |
73466498 | 3924 | if (!netif_xmit_stopped(txq)) { |
97cdcf37 | 3925 | dev_xmit_recursion_inc(); |
ce93718f | 3926 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
97cdcf37 | 3927 | dev_xmit_recursion_dec(); |
572a9d7b | 3928 | if (dev_xmit_complete(rc)) { |
c773e847 | 3929 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
3930 | goto out; |
3931 | } | |
3932 | } | |
c773e847 | 3933 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
3934 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
3935 | dev->name); | |
1da177e4 LT |
3936 | } else { |
3937 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
3938 | * unfortunately |
3939 | */ | |
3940 | recursion_alert: | |
e87cc472 JP |
3941 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
3942 | dev->name); | |
1da177e4 LT |
3943 | } |
3944 | } | |
3945 | ||
3946 | rc = -ENETDOWN; | |
d4828d85 | 3947 | rcu_read_unlock_bh(); |
1da177e4 | 3948 | |
015f0688 | 3949 | atomic_long_inc(&dev->tx_dropped); |
1f59533f | 3950 | kfree_skb_list(skb); |
1da177e4 LT |
3951 | return rc; |
3952 | out: | |
d4828d85 | 3953 | rcu_read_unlock_bh(); |
1da177e4 LT |
3954 | return rc; |
3955 | } | |
f663dd9a | 3956 | |
2b4aa3ce | 3957 | int dev_queue_xmit(struct sk_buff *skb) |
f663dd9a JW |
3958 | { |
3959 | return __dev_queue_xmit(skb, NULL); | |
3960 | } | |
2b4aa3ce | 3961 | EXPORT_SYMBOL(dev_queue_xmit); |
1da177e4 | 3962 | |
eadec877 | 3963 | int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev) |
f663dd9a | 3964 | { |
eadec877 | 3965 | return __dev_queue_xmit(skb, sb_dev); |
f663dd9a JW |
3966 | } |
3967 | EXPORT_SYMBOL(dev_queue_xmit_accel); | |
3968 | ||
865b03f2 MK |
3969 | int dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
3970 | { | |
3971 | struct net_device *dev = skb->dev; | |
3972 | struct sk_buff *orig_skb = skb; | |
3973 | struct netdev_queue *txq; | |
3974 | int ret = NETDEV_TX_BUSY; | |
3975 | bool again = false; | |
3976 | ||
3977 | if (unlikely(!netif_running(dev) || | |
3978 | !netif_carrier_ok(dev))) | |
3979 | goto drop; | |
3980 | ||
3981 | skb = validate_xmit_skb_list(skb, dev, &again); | |
3982 | if (skb != orig_skb) | |
3983 | goto drop; | |
3984 | ||
3985 | skb_set_queue_mapping(skb, queue_id); | |
3986 | txq = skb_get_tx_queue(dev, skb); | |
3987 | ||
3988 | local_bh_disable(); | |
3989 | ||
3990 | HARD_TX_LOCK(dev, txq, smp_processor_id()); | |
3991 | if (!netif_xmit_frozen_or_drv_stopped(txq)) | |
3992 | ret = netdev_start_xmit(skb, dev, txq, false); | |
3993 | HARD_TX_UNLOCK(dev, txq); | |
3994 | ||
3995 | local_bh_enable(); | |
3996 | ||
3997 | if (!dev_xmit_complete(ret)) | |
3998 | kfree_skb(skb); | |
3999 | ||
4000 | return ret; | |
4001 | drop: | |
4002 | atomic_long_inc(&dev->tx_dropped); | |
4003 | kfree_skb_list(skb); | |
4004 | return NET_XMIT_DROP; | |
4005 | } | |
4006 | EXPORT_SYMBOL(dev_direct_xmit); | |
1da177e4 | 4007 | |
eb13da1a | 4008 | /************************************************************************* |
4009 | * Receiver routines | |
4010 | *************************************************************************/ | |
1da177e4 | 4011 | |
6b2bedc3 | 4012 | int netdev_max_backlog __read_mostly = 1000; |
c9e6bc64 ED |
4013 | EXPORT_SYMBOL(netdev_max_backlog); |
4014 | ||
3b098e2d | 4015 | int netdev_tstamp_prequeue __read_mostly = 1; |
6b2bedc3 | 4016 | int netdev_budget __read_mostly = 300; |
7acf8a1e | 4017 | unsigned int __read_mostly netdev_budget_usecs = 2000; |
3d48b53f MT |
4018 | int weight_p __read_mostly = 64; /* old backlog weight */ |
4019 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ | |
4020 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ | |
4021 | int dev_rx_weight __read_mostly = 64; | |
4022 | int dev_tx_weight __read_mostly = 64; | |
323ebb61 EC |
4023 | /* Maximum number of GRO_NORMAL skbs to batch up for list-RX */ |
4024 | int gro_normal_batch __read_mostly = 8; | |
1da177e4 | 4025 | |
eecfd7c4 ED |
4026 | /* Called with irq disabled */ |
4027 | static inline void ____napi_schedule(struct softnet_data *sd, | |
4028 | struct napi_struct *napi) | |
4029 | { | |
4030 | list_add_tail(&napi->poll_list, &sd->poll_list); | |
4031 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4032 | } | |
4033 | ||
bfb564e7 KK |
4034 | #ifdef CONFIG_RPS |
4035 | ||
4036 | /* One global table that all flow-based protocols share. */ | |
6e3f7faf | 4037 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
bfb564e7 | 4038 | EXPORT_SYMBOL(rps_sock_flow_table); |
567e4b79 ED |
4039 | u32 rps_cpu_mask __read_mostly; |
4040 | EXPORT_SYMBOL(rps_cpu_mask); | |
bfb564e7 | 4041 | |
dc05360f | 4042 | struct static_key_false rps_needed __read_mostly; |
3df97ba8 | 4043 | EXPORT_SYMBOL(rps_needed); |
dc05360f | 4044 | struct static_key_false rfs_needed __read_mostly; |
13bfff25 | 4045 | EXPORT_SYMBOL(rfs_needed); |
adc9300e | 4046 | |
c445477d BH |
4047 | static struct rps_dev_flow * |
4048 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4049 | struct rps_dev_flow *rflow, u16 next_cpu) | |
4050 | { | |
a31196b0 | 4051 | if (next_cpu < nr_cpu_ids) { |
c445477d BH |
4052 | #ifdef CONFIG_RFS_ACCEL |
4053 | struct netdev_rx_queue *rxqueue; | |
4054 | struct rps_dev_flow_table *flow_table; | |
4055 | struct rps_dev_flow *old_rflow; | |
4056 | u32 flow_id; | |
4057 | u16 rxq_index; | |
4058 | int rc; | |
4059 | ||
4060 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
4061 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
4062 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
4063 | goto out; |
4064 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
4065 | if (rxq_index == skb_get_rx_queue(skb)) | |
4066 | goto out; | |
4067 | ||
4068 | rxqueue = dev->_rx + rxq_index; | |
4069 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4070 | if (!flow_table) | |
4071 | goto out; | |
61b905da | 4072 | flow_id = skb_get_hash(skb) & flow_table->mask; |
c445477d BH |
4073 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
4074 | rxq_index, flow_id); | |
4075 | if (rc < 0) | |
4076 | goto out; | |
4077 | old_rflow = rflow; | |
4078 | rflow = &flow_table->flows[flow_id]; | |
c445477d BH |
4079 | rflow->filter = rc; |
4080 | if (old_rflow->filter == rflow->filter) | |
4081 | old_rflow->filter = RPS_NO_FILTER; | |
4082 | out: | |
4083 | #endif | |
4084 | rflow->last_qtail = | |
09994d1b | 4085 | per_cpu(softnet_data, next_cpu).input_queue_head; |
c445477d BH |
4086 | } |
4087 | ||
09994d1b | 4088 | rflow->cpu = next_cpu; |
c445477d BH |
4089 | return rflow; |
4090 | } | |
4091 | ||
bfb564e7 KK |
4092 | /* |
4093 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
4094 | * CPU from the RPS map of the receiving queue for a given skb. | |
4095 | * rcu_read_lock must be held on entry. | |
4096 | */ | |
4097 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4098 | struct rps_dev_flow **rflowp) | |
4099 | { | |
567e4b79 ED |
4100 | const struct rps_sock_flow_table *sock_flow_table; |
4101 | struct netdev_rx_queue *rxqueue = dev->_rx; | |
bfb564e7 | 4102 | struct rps_dev_flow_table *flow_table; |
567e4b79 | 4103 | struct rps_map *map; |
bfb564e7 | 4104 | int cpu = -1; |
567e4b79 | 4105 | u32 tcpu; |
61b905da | 4106 | u32 hash; |
bfb564e7 KK |
4107 | |
4108 | if (skb_rx_queue_recorded(skb)) { | |
4109 | u16 index = skb_get_rx_queue(skb); | |
567e4b79 | 4110 | |
62fe0b40 BH |
4111 | if (unlikely(index >= dev->real_num_rx_queues)) { |
4112 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4113 | "%s received packet on queue %u, but number " | |
4114 | "of RX queues is %u\n", | |
4115 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
4116 | goto done; |
4117 | } | |
567e4b79 ED |
4118 | rxqueue += index; |
4119 | } | |
bfb564e7 | 4120 | |
567e4b79 ED |
4121 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
4122 | ||
4123 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
6e3f7faf | 4124 | map = rcu_dereference(rxqueue->rps_map); |
567e4b79 | 4125 | if (!flow_table && !map) |
bfb564e7 KK |
4126 | goto done; |
4127 | ||
2d47b459 | 4128 | skb_reset_network_header(skb); |
61b905da TH |
4129 | hash = skb_get_hash(skb); |
4130 | if (!hash) | |
bfb564e7 KK |
4131 | goto done; |
4132 | ||
fec5e652 TH |
4133 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
4134 | if (flow_table && sock_flow_table) { | |
fec5e652 | 4135 | struct rps_dev_flow *rflow; |
567e4b79 ED |
4136 | u32 next_cpu; |
4137 | u32 ident; | |
4138 | ||
4139 | /* First check into global flow table if there is a match */ | |
4140 | ident = sock_flow_table->ents[hash & sock_flow_table->mask]; | |
4141 | if ((ident ^ hash) & ~rps_cpu_mask) | |
4142 | goto try_rps; | |
fec5e652 | 4143 | |
567e4b79 ED |
4144 | next_cpu = ident & rps_cpu_mask; |
4145 | ||
4146 | /* OK, now we know there is a match, | |
4147 | * we can look at the local (per receive queue) flow table | |
4148 | */ | |
61b905da | 4149 | rflow = &flow_table->flows[hash & flow_table->mask]; |
fec5e652 TH |
4150 | tcpu = rflow->cpu; |
4151 | ||
fec5e652 TH |
4152 | /* |
4153 | * If the desired CPU (where last recvmsg was done) is | |
4154 | * different from current CPU (one in the rx-queue flow | |
4155 | * table entry), switch if one of the following holds: | |
a31196b0 | 4156 | * - Current CPU is unset (>= nr_cpu_ids). |
fec5e652 TH |
4157 | * - Current CPU is offline. |
4158 | * - The current CPU's queue tail has advanced beyond the | |
4159 | * last packet that was enqueued using this table entry. | |
4160 | * This guarantees that all previous packets for the flow | |
4161 | * have been dequeued, thus preserving in order delivery. | |
4162 | */ | |
4163 | if (unlikely(tcpu != next_cpu) && | |
a31196b0 | 4164 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
fec5e652 | 4165 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
baefa31d TH |
4166 | rflow->last_qtail)) >= 0)) { |
4167 | tcpu = next_cpu; | |
c445477d | 4168 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 4169 | } |
c445477d | 4170 | |
a31196b0 | 4171 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
fec5e652 TH |
4172 | *rflowp = rflow; |
4173 | cpu = tcpu; | |
4174 | goto done; | |
4175 | } | |
4176 | } | |
4177 | ||
567e4b79 ED |
4178 | try_rps: |
4179 | ||
0a9627f2 | 4180 | if (map) { |
8fc54f68 | 4181 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
0a9627f2 TH |
4182 | if (cpu_online(tcpu)) { |
4183 | cpu = tcpu; | |
4184 | goto done; | |
4185 | } | |
4186 | } | |
4187 | ||
4188 | done: | |
0a9627f2 TH |
4189 | return cpu; |
4190 | } | |
4191 | ||
c445477d BH |
4192 | #ifdef CONFIG_RFS_ACCEL |
4193 | ||
4194 | /** | |
4195 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
4196 | * @dev: Device on which the filter was set | |
4197 | * @rxq_index: RX queue index | |
4198 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
4199 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
4200 | * | |
4201 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
4202 | * this function for each installed filter and remove the filters for | |
4203 | * which it returns %true. | |
4204 | */ | |
4205 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
4206 | u32 flow_id, u16 filter_id) | |
4207 | { | |
4208 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
4209 | struct rps_dev_flow_table *flow_table; | |
4210 | struct rps_dev_flow *rflow; | |
4211 | bool expire = true; | |
a31196b0 | 4212 | unsigned int cpu; |
c445477d BH |
4213 | |
4214 | rcu_read_lock(); | |
4215 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4216 | if (flow_table && flow_id <= flow_table->mask) { | |
4217 | rflow = &flow_table->flows[flow_id]; | |
6aa7de05 | 4218 | cpu = READ_ONCE(rflow->cpu); |
a31196b0 | 4219 | if (rflow->filter == filter_id && cpu < nr_cpu_ids && |
c445477d BH |
4220 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
4221 | rflow->last_qtail) < | |
4222 | (int)(10 * flow_table->mask))) | |
4223 | expire = false; | |
4224 | } | |
4225 | rcu_read_unlock(); | |
4226 | return expire; | |
4227 | } | |
4228 | EXPORT_SYMBOL(rps_may_expire_flow); | |
4229 | ||
4230 | #endif /* CONFIG_RFS_ACCEL */ | |
4231 | ||
0a9627f2 | 4232 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 4233 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 4234 | { |
e36fa2f7 ED |
4235 | struct softnet_data *sd = data; |
4236 | ||
eecfd7c4 | 4237 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 4238 | sd->received_rps++; |
0a9627f2 | 4239 | } |
e36fa2f7 | 4240 | |
fec5e652 | 4241 | #endif /* CONFIG_RPS */ |
0a9627f2 | 4242 | |
e36fa2f7 ED |
4243 | /* |
4244 | * Check if this softnet_data structure is another cpu one | |
4245 | * If yes, queue it to our IPI list and return 1 | |
4246 | * If no, return 0 | |
4247 | */ | |
4248 | static int rps_ipi_queued(struct softnet_data *sd) | |
4249 | { | |
4250 | #ifdef CONFIG_RPS | |
903ceff7 | 4251 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
e36fa2f7 ED |
4252 | |
4253 | if (sd != mysd) { | |
4254 | sd->rps_ipi_next = mysd->rps_ipi_list; | |
4255 | mysd->rps_ipi_list = sd; | |
4256 | ||
4257 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4258 | return 1; | |
4259 | } | |
4260 | #endif /* CONFIG_RPS */ | |
4261 | return 0; | |
4262 | } | |
4263 | ||
99bbc707 WB |
4264 | #ifdef CONFIG_NET_FLOW_LIMIT |
4265 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); | |
4266 | #endif | |
4267 | ||
4268 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) | |
4269 | { | |
4270 | #ifdef CONFIG_NET_FLOW_LIMIT | |
4271 | struct sd_flow_limit *fl; | |
4272 | struct softnet_data *sd; | |
4273 | unsigned int old_flow, new_flow; | |
4274 | ||
4275 | if (qlen < (netdev_max_backlog >> 1)) | |
4276 | return false; | |
4277 | ||
903ceff7 | 4278 | sd = this_cpu_ptr(&softnet_data); |
99bbc707 WB |
4279 | |
4280 | rcu_read_lock(); | |
4281 | fl = rcu_dereference(sd->flow_limit); | |
4282 | if (fl) { | |
3958afa1 | 4283 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
99bbc707 WB |
4284 | old_flow = fl->history[fl->history_head]; |
4285 | fl->history[fl->history_head] = new_flow; | |
4286 | ||
4287 | fl->history_head++; | |
4288 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; | |
4289 | ||
4290 | if (likely(fl->buckets[old_flow])) | |
4291 | fl->buckets[old_flow]--; | |
4292 | ||
4293 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { | |
4294 | fl->count++; | |
4295 | rcu_read_unlock(); | |
4296 | return true; | |
4297 | } | |
4298 | } | |
4299 | rcu_read_unlock(); | |
4300 | #endif | |
4301 | return false; | |
4302 | } | |
4303 | ||
0a9627f2 TH |
4304 | /* |
4305 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
4306 | * queue (may be a remote CPU queue). | |
4307 | */ | |
fec5e652 TH |
4308 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
4309 | unsigned int *qtail) | |
0a9627f2 | 4310 | { |
e36fa2f7 | 4311 | struct softnet_data *sd; |
0a9627f2 | 4312 | unsigned long flags; |
99bbc707 | 4313 | unsigned int qlen; |
0a9627f2 | 4314 | |
e36fa2f7 | 4315 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 TH |
4316 | |
4317 | local_irq_save(flags); | |
0a9627f2 | 4318 | |
e36fa2f7 | 4319 | rps_lock(sd); |
e9e4dd32 JA |
4320 | if (!netif_running(skb->dev)) |
4321 | goto drop; | |
99bbc707 WB |
4322 | qlen = skb_queue_len(&sd->input_pkt_queue); |
4323 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { | |
e008f3f0 | 4324 | if (qlen) { |
0a9627f2 | 4325 | enqueue: |
e36fa2f7 | 4326 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
76cc8b13 | 4327 | input_queue_tail_incr_save(sd, qtail); |
e36fa2f7 | 4328 | rps_unlock(sd); |
152102c7 | 4329 | local_irq_restore(flags); |
0a9627f2 TH |
4330 | return NET_RX_SUCCESS; |
4331 | } | |
4332 | ||
ebda37c2 ED |
4333 | /* Schedule NAPI for backlog device |
4334 | * We can use non atomic operation since we own the queue lock | |
4335 | */ | |
4336 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { | |
e36fa2f7 | 4337 | if (!rps_ipi_queued(sd)) |
eecfd7c4 | 4338 | ____napi_schedule(sd, &sd->backlog); |
0a9627f2 TH |
4339 | } |
4340 | goto enqueue; | |
4341 | } | |
4342 | ||
e9e4dd32 | 4343 | drop: |
dee42870 | 4344 | sd->dropped++; |
e36fa2f7 | 4345 | rps_unlock(sd); |
0a9627f2 | 4346 | |
0a9627f2 TH |
4347 | local_irq_restore(flags); |
4348 | ||
caf586e5 | 4349 | atomic_long_inc(&skb->dev->rx_dropped); |
0a9627f2 TH |
4350 | kfree_skb(skb); |
4351 | return NET_RX_DROP; | |
4352 | } | |
1da177e4 | 4353 | |
e817f856 JDB |
4354 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
4355 | { | |
4356 | struct net_device *dev = skb->dev; | |
4357 | struct netdev_rx_queue *rxqueue; | |
4358 | ||
4359 | rxqueue = dev->_rx; | |
4360 | ||
4361 | if (skb_rx_queue_recorded(skb)) { | |
4362 | u16 index = skb_get_rx_queue(skb); | |
4363 | ||
4364 | if (unlikely(index >= dev->real_num_rx_queues)) { | |
4365 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4366 | "%s received packet on queue %u, but number " | |
4367 | "of RX queues is %u\n", | |
4368 | dev->name, index, dev->real_num_rx_queues); | |
4369 | ||
4370 | return rxqueue; /* Return first rxqueue */ | |
4371 | } | |
4372 | rxqueue += index; | |
4373 | } | |
4374 | return rxqueue; | |
4375 | } | |
4376 | ||
d4455169 | 4377 | static u32 netif_receive_generic_xdp(struct sk_buff *skb, |
02671e23 | 4378 | struct xdp_buff *xdp, |
d4455169 JF |
4379 | struct bpf_prog *xdp_prog) |
4380 | { | |
e817f856 | 4381 | struct netdev_rx_queue *rxqueue; |
198d83bb | 4382 | void *orig_data, *orig_data_end; |
de8f3a83 | 4383 | u32 metalen, act = XDP_DROP; |
29724956 JDB |
4384 | __be16 orig_eth_type; |
4385 | struct ethhdr *eth; | |
4386 | bool orig_bcast; | |
d4455169 JF |
4387 | int hlen, off; |
4388 | u32 mac_len; | |
4389 | ||
4390 | /* Reinjected packets coming from act_mirred or similar should | |
4391 | * not get XDP generic processing. | |
4392 | */ | |
cd11b164 | 4393 | if (skb_cloned(skb) || skb_is_tc_redirected(skb)) |
d4455169 JF |
4394 | return XDP_PASS; |
4395 | ||
de8f3a83 DB |
4396 | /* XDP packets must be linear and must have sufficient headroom |
4397 | * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also | |
4398 | * native XDP provides, thus we need to do it here as well. | |
4399 | */ | |
4400 | if (skb_is_nonlinear(skb) || | |
4401 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { | |
4402 | int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); | |
4403 | int troom = skb->tail + skb->data_len - skb->end; | |
4404 | ||
4405 | /* In case we have to go down the path and also linearize, | |
4406 | * then lets do the pskb_expand_head() work just once here. | |
4407 | */ | |
4408 | if (pskb_expand_head(skb, | |
4409 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, | |
4410 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC)) | |
4411 | goto do_drop; | |
2d17d8d7 | 4412 | if (skb_linearize(skb)) |
de8f3a83 DB |
4413 | goto do_drop; |
4414 | } | |
d4455169 JF |
4415 | |
4416 | /* The XDP program wants to see the packet starting at the MAC | |
4417 | * header. | |
4418 | */ | |
4419 | mac_len = skb->data - skb_mac_header(skb); | |
4420 | hlen = skb_headlen(skb) + mac_len; | |
02671e23 BT |
4421 | xdp->data = skb->data - mac_len; |
4422 | xdp->data_meta = xdp->data; | |
4423 | xdp->data_end = xdp->data + hlen; | |
4424 | xdp->data_hard_start = skb->data - skb_headroom(skb); | |
4425 | orig_data_end = xdp->data_end; | |
4426 | orig_data = xdp->data; | |
29724956 JDB |
4427 | eth = (struct ethhdr *)xdp->data; |
4428 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); | |
4429 | orig_eth_type = eth->h_proto; | |
d4455169 | 4430 | |
e817f856 | 4431 | rxqueue = netif_get_rxqueue(skb); |
02671e23 | 4432 | xdp->rxq = &rxqueue->xdp_rxq; |
e817f856 | 4433 | |
02671e23 | 4434 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
d4455169 | 4435 | |
065af355 | 4436 | /* check if bpf_xdp_adjust_head was used */ |
02671e23 | 4437 | off = xdp->data - orig_data; |
065af355 JDB |
4438 | if (off) { |
4439 | if (off > 0) | |
4440 | __skb_pull(skb, off); | |
4441 | else if (off < 0) | |
4442 | __skb_push(skb, -off); | |
4443 | ||
4444 | skb->mac_header += off; | |
4445 | skb_reset_network_header(skb); | |
4446 | } | |
d4455169 | 4447 | |
198d83bb NS |
4448 | /* check if bpf_xdp_adjust_tail was used. it can only "shrink" |
4449 | * pckt. | |
4450 | */ | |
02671e23 | 4451 | off = orig_data_end - xdp->data_end; |
f7613120 | 4452 | if (off != 0) { |
02671e23 | 4453 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
f7613120 | 4454 | skb->len -= off; |
02671e23 | 4455 | |
f7613120 | 4456 | } |
198d83bb | 4457 | |
29724956 JDB |
4458 | /* check if XDP changed eth hdr such SKB needs update */ |
4459 | eth = (struct ethhdr *)xdp->data; | |
4460 | if ((orig_eth_type != eth->h_proto) || | |
4461 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { | |
4462 | __skb_push(skb, ETH_HLEN); | |
4463 | skb->protocol = eth_type_trans(skb, skb->dev); | |
4464 | } | |
4465 | ||
d4455169 | 4466 | switch (act) { |
6103aa96 | 4467 | case XDP_REDIRECT: |
d4455169 JF |
4468 | case XDP_TX: |
4469 | __skb_push(skb, mac_len); | |
de8f3a83 | 4470 | break; |
d4455169 | 4471 | case XDP_PASS: |
02671e23 | 4472 | metalen = xdp->data - xdp->data_meta; |
de8f3a83 DB |
4473 | if (metalen) |
4474 | skb_metadata_set(skb, metalen); | |
d4455169 | 4475 | break; |
d4455169 JF |
4476 | default: |
4477 | bpf_warn_invalid_xdp_action(act); | |
4478 | /* fall through */ | |
4479 | case XDP_ABORTED: | |
4480 | trace_xdp_exception(skb->dev, xdp_prog, act); | |
4481 | /* fall through */ | |
4482 | case XDP_DROP: | |
4483 | do_drop: | |
4484 | kfree_skb(skb); | |
4485 | break; | |
4486 | } | |
4487 | ||
4488 | return act; | |
4489 | } | |
4490 | ||
4491 | /* When doing generic XDP we have to bypass the qdisc layer and the | |
4492 | * network taps in order to match in-driver-XDP behavior. | |
4493 | */ | |
7c497478 | 4494 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
d4455169 JF |
4495 | { |
4496 | struct net_device *dev = skb->dev; | |
4497 | struct netdev_queue *txq; | |
4498 | bool free_skb = true; | |
4499 | int cpu, rc; | |
4500 | ||
4bd97d51 | 4501 | txq = netdev_core_pick_tx(dev, skb, NULL); |
d4455169 JF |
4502 | cpu = smp_processor_id(); |
4503 | HARD_TX_LOCK(dev, txq, cpu); | |
4504 | if (!netif_xmit_stopped(txq)) { | |
4505 | rc = netdev_start_xmit(skb, dev, txq, 0); | |
4506 | if (dev_xmit_complete(rc)) | |
4507 | free_skb = false; | |
4508 | } | |
4509 | HARD_TX_UNLOCK(dev, txq); | |
4510 | if (free_skb) { | |
4511 | trace_xdp_exception(dev, xdp_prog, XDP_TX); | |
4512 | kfree_skb(skb); | |
4513 | } | |
4514 | } | |
7c497478 | 4515 | EXPORT_SYMBOL_GPL(generic_xdp_tx); |
d4455169 | 4516 | |
02786475 | 4517 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
d4455169 | 4518 | |
7c497478 | 4519 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb) |
d4455169 | 4520 | { |
d4455169 | 4521 | if (xdp_prog) { |
02671e23 BT |
4522 | struct xdp_buff xdp; |
4523 | u32 act; | |
6103aa96 | 4524 | int err; |
d4455169 | 4525 | |
02671e23 | 4526 | act = netif_receive_generic_xdp(skb, &xdp, xdp_prog); |
d4455169 | 4527 | if (act != XDP_PASS) { |
6103aa96 JF |
4528 | switch (act) { |
4529 | case XDP_REDIRECT: | |
2facaad6 | 4530 | err = xdp_do_generic_redirect(skb->dev, skb, |
02671e23 | 4531 | &xdp, xdp_prog); |
6103aa96 JF |
4532 | if (err) |
4533 | goto out_redir; | |
02671e23 | 4534 | break; |
6103aa96 | 4535 | case XDP_TX: |
d4455169 | 4536 | generic_xdp_tx(skb, xdp_prog); |
6103aa96 JF |
4537 | break; |
4538 | } | |
d4455169 JF |
4539 | return XDP_DROP; |
4540 | } | |
4541 | } | |
4542 | return XDP_PASS; | |
6103aa96 | 4543 | out_redir: |
6103aa96 JF |
4544 | kfree_skb(skb); |
4545 | return XDP_DROP; | |
d4455169 | 4546 | } |
7c497478 | 4547 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
d4455169 | 4548 | |
ae78dbfa | 4549 | static int netif_rx_internal(struct sk_buff *skb) |
1da177e4 | 4550 | { |
b0e28f1e | 4551 | int ret; |
1da177e4 | 4552 | |
588f0330 | 4553 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
1da177e4 | 4554 | |
cf66ba58 | 4555 | trace_netif_rx(skb); |
d4455169 | 4556 | |
df334545 | 4557 | #ifdef CONFIG_RPS |
dc05360f | 4558 | if (static_branch_unlikely(&rps_needed)) { |
fec5e652 | 4559 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
4560 | int cpu; |
4561 | ||
cece1945 | 4562 | preempt_disable(); |
b0e28f1e | 4563 | rcu_read_lock(); |
fec5e652 TH |
4564 | |
4565 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
4566 | if (cpu < 0) |
4567 | cpu = smp_processor_id(); | |
fec5e652 TH |
4568 | |
4569 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
4570 | ||
b0e28f1e | 4571 | rcu_read_unlock(); |
cece1945 | 4572 | preempt_enable(); |
adc9300e ED |
4573 | } else |
4574 | #endif | |
fec5e652 TH |
4575 | { |
4576 | unsigned int qtail; | |
f4563a75 | 4577 | |
fec5e652 TH |
4578 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); |
4579 | put_cpu(); | |
4580 | } | |
b0e28f1e | 4581 | return ret; |
1da177e4 | 4582 | } |
ae78dbfa BH |
4583 | |
4584 | /** | |
4585 | * netif_rx - post buffer to the network code | |
4586 | * @skb: buffer to post | |
4587 | * | |
4588 | * This function receives a packet from a device driver and queues it for | |
4589 | * the upper (protocol) levels to process. It always succeeds. The buffer | |
4590 | * may be dropped during processing for congestion control or by the | |
4591 | * protocol layers. | |
4592 | * | |
4593 | * return values: | |
4594 | * NET_RX_SUCCESS (no congestion) | |
4595 | * NET_RX_DROP (packet was dropped) | |
4596 | * | |
4597 | */ | |
4598 | ||
4599 | int netif_rx(struct sk_buff *skb) | |
4600 | { | |
b0e3f1bd GB |
4601 | int ret; |
4602 | ||
ae78dbfa BH |
4603 | trace_netif_rx_entry(skb); |
4604 | ||
b0e3f1bd GB |
4605 | ret = netif_rx_internal(skb); |
4606 | trace_netif_rx_exit(ret); | |
4607 | ||
4608 | return ret; | |
ae78dbfa | 4609 | } |
d1b19dff | 4610 | EXPORT_SYMBOL(netif_rx); |
1da177e4 LT |
4611 | |
4612 | int netif_rx_ni(struct sk_buff *skb) | |
4613 | { | |
4614 | int err; | |
4615 | ||
ae78dbfa BH |
4616 | trace_netif_rx_ni_entry(skb); |
4617 | ||
1da177e4 | 4618 | preempt_disable(); |
ae78dbfa | 4619 | err = netif_rx_internal(skb); |
1da177e4 LT |
4620 | if (local_softirq_pending()) |
4621 | do_softirq(); | |
4622 | preempt_enable(); | |
b0e3f1bd | 4623 | trace_netif_rx_ni_exit(err); |
1da177e4 LT |
4624 | |
4625 | return err; | |
4626 | } | |
1da177e4 LT |
4627 | EXPORT_SYMBOL(netif_rx_ni); |
4628 | ||
0766f788 | 4629 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
1da177e4 | 4630 | { |
903ceff7 | 4631 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
1da177e4 LT |
4632 | |
4633 | if (sd->completion_queue) { | |
4634 | struct sk_buff *clist; | |
4635 | ||
4636 | local_irq_disable(); | |
4637 | clist = sd->completion_queue; | |
4638 | sd->completion_queue = NULL; | |
4639 | local_irq_enable(); | |
4640 | ||
4641 | while (clist) { | |
4642 | struct sk_buff *skb = clist; | |
f4563a75 | 4643 | |
1da177e4 LT |
4644 | clist = clist->next; |
4645 | ||
63354797 | 4646 | WARN_ON(refcount_read(&skb->users)); |
e6247027 ED |
4647 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED)) |
4648 | trace_consume_skb(skb); | |
4649 | else | |
4650 | trace_kfree_skb(skb, net_tx_action); | |
15fad714 JDB |
4651 | |
4652 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) | |
4653 | __kfree_skb(skb); | |
4654 | else | |
4655 | __kfree_skb_defer(skb); | |
1da177e4 | 4656 | } |
15fad714 JDB |
4657 | |
4658 | __kfree_skb_flush(); | |
1da177e4 LT |
4659 | } |
4660 | ||
4661 | if (sd->output_queue) { | |
37437bb2 | 4662 | struct Qdisc *head; |
1da177e4 LT |
4663 | |
4664 | local_irq_disable(); | |
4665 | head = sd->output_queue; | |
4666 | sd->output_queue = NULL; | |
a9cbd588 | 4667 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
4668 | local_irq_enable(); |
4669 | ||
4670 | while (head) { | |
37437bb2 | 4671 | struct Qdisc *q = head; |
6b3ba914 | 4672 | spinlock_t *root_lock = NULL; |
37437bb2 | 4673 | |
1da177e4 LT |
4674 | head = head->next_sched; |
4675 | ||
6b3ba914 JF |
4676 | if (!(q->flags & TCQ_F_NOLOCK)) { |
4677 | root_lock = qdisc_lock(q); | |
4678 | spin_lock(root_lock); | |
4679 | } | |
3bcb846c ED |
4680 | /* We need to make sure head->next_sched is read |
4681 | * before clearing __QDISC_STATE_SCHED | |
4682 | */ | |
4683 | smp_mb__before_atomic(); | |
4684 | clear_bit(__QDISC_STATE_SCHED, &q->state); | |
4685 | qdisc_run(q); | |
6b3ba914 JF |
4686 | if (root_lock) |
4687 | spin_unlock(root_lock); | |
1da177e4 LT |
4688 | } |
4689 | } | |
f53c7239 SK |
4690 | |
4691 | xfrm_dev_backlog(sd); | |
1da177e4 LT |
4692 | } |
4693 | ||
181402a5 | 4694 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
4695 | /* This hook is defined here for ATM LANE */ |
4696 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
4697 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 4698 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 4699 | #endif |
1da177e4 | 4700 | |
1f211a1b DB |
4701 | static inline struct sk_buff * |
4702 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
4703 | struct net_device *orig_dev) | |
f697c3e8 | 4704 | { |
e7582bab | 4705 | #ifdef CONFIG_NET_CLS_ACT |
46209401 | 4706 | struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress); |
d2788d34 | 4707 | struct tcf_result cl_res; |
24824a09 | 4708 | |
c9e99fd0 DB |
4709 | /* If there's at least one ingress present somewhere (so |
4710 | * we get here via enabled static key), remaining devices | |
4711 | * that are not configured with an ingress qdisc will bail | |
d2788d34 | 4712 | * out here. |
c9e99fd0 | 4713 | */ |
46209401 | 4714 | if (!miniq) |
4577139b | 4715 | return skb; |
46209401 | 4716 | |
f697c3e8 HX |
4717 | if (*pt_prev) { |
4718 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4719 | *pt_prev = NULL; | |
1da177e4 LT |
4720 | } |
4721 | ||
3365495c | 4722 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
8dc07fdb | 4723 | skb->tc_at_ingress = 1; |
46209401 | 4724 | mini_qdisc_bstats_cpu_update(miniq, skb); |
c9e99fd0 | 4725 | |
46209401 | 4726 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
d2788d34 DB |
4727 | case TC_ACT_OK: |
4728 | case TC_ACT_RECLASSIFY: | |
4729 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
4730 | break; | |
4731 | case TC_ACT_SHOT: | |
46209401 | 4732 | mini_qdisc_qstats_cpu_drop(miniq); |
8a3a4c6e ED |
4733 | kfree_skb(skb); |
4734 | return NULL; | |
d2788d34 DB |
4735 | case TC_ACT_STOLEN: |
4736 | case TC_ACT_QUEUED: | |
e25ea21f | 4737 | case TC_ACT_TRAP: |
8a3a4c6e | 4738 | consume_skb(skb); |
d2788d34 | 4739 | return NULL; |
27b29f63 AS |
4740 | case TC_ACT_REDIRECT: |
4741 | /* skb_mac_header check was done by cls/act_bpf, so | |
4742 | * we can safely push the L2 header back before | |
4743 | * redirecting to another netdev | |
4744 | */ | |
4745 | __skb_push(skb, skb->mac_len); | |
4746 | skb_do_redirect(skb); | |
4747 | return NULL; | |
720f22fe | 4748 | case TC_ACT_CONSUMED: |
cd11b164 | 4749 | return NULL; |
d2788d34 DB |
4750 | default: |
4751 | break; | |
f697c3e8 | 4752 | } |
e7582bab | 4753 | #endif /* CONFIG_NET_CLS_ACT */ |
e687ad60 PN |
4754 | return skb; |
4755 | } | |
1da177e4 | 4756 | |
24b27fc4 MB |
4757 | /** |
4758 | * netdev_is_rx_handler_busy - check if receive handler is registered | |
4759 | * @dev: device to check | |
4760 | * | |
4761 | * Check if a receive handler is already registered for a given device. | |
4762 | * Return true if there one. | |
4763 | * | |
4764 | * The caller must hold the rtnl_mutex. | |
4765 | */ | |
4766 | bool netdev_is_rx_handler_busy(struct net_device *dev) | |
4767 | { | |
4768 | ASSERT_RTNL(); | |
4769 | return dev && rtnl_dereference(dev->rx_handler); | |
4770 | } | |
4771 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); | |
4772 | ||
ab95bfe0 JP |
4773 | /** |
4774 | * netdev_rx_handler_register - register receive handler | |
4775 | * @dev: device to register a handler for | |
4776 | * @rx_handler: receive handler to register | |
93e2c32b | 4777 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 | 4778 | * |
e227867f | 4779 | * Register a receive handler for a device. This handler will then be |
ab95bfe0 JP |
4780 | * called from __netif_receive_skb. A negative errno code is returned |
4781 | * on a failure. | |
4782 | * | |
4783 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
4784 | * |
4785 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
4786 | */ |
4787 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
4788 | rx_handler_func_t *rx_handler, |
4789 | void *rx_handler_data) | |
ab95bfe0 | 4790 | { |
1b7cd004 | 4791 | if (netdev_is_rx_handler_busy(dev)) |
ab95bfe0 JP |
4792 | return -EBUSY; |
4793 | ||
f5426250 PA |
4794 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
4795 | return -EINVAL; | |
4796 | ||
00cfec37 | 4797 | /* Note: rx_handler_data must be set before rx_handler */ |
93e2c32b | 4798 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
4799 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
4800 | ||
4801 | return 0; | |
4802 | } | |
4803 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
4804 | ||
4805 | /** | |
4806 | * netdev_rx_handler_unregister - unregister receive handler | |
4807 | * @dev: device to unregister a handler from | |
4808 | * | |
166ec369 | 4809 | * Unregister a receive handler from a device. |
ab95bfe0 JP |
4810 | * |
4811 | * The caller must hold the rtnl_mutex. | |
4812 | */ | |
4813 | void netdev_rx_handler_unregister(struct net_device *dev) | |
4814 | { | |
4815 | ||
4816 | ASSERT_RTNL(); | |
a9b3cd7f | 4817 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
00cfec37 ED |
4818 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
4819 | * section has a guarantee to see a non NULL rx_handler_data | |
4820 | * as well. | |
4821 | */ | |
4822 | synchronize_net(); | |
a9b3cd7f | 4823 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
ab95bfe0 JP |
4824 | } |
4825 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
4826 | ||
b4b9e355 MG |
4827 | /* |
4828 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
4829 | * the special handling of PFMEMALLOC skbs. | |
4830 | */ | |
4831 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
4832 | { | |
4833 | switch (skb->protocol) { | |
2b8837ae JP |
4834 | case htons(ETH_P_ARP): |
4835 | case htons(ETH_P_IP): | |
4836 | case htons(ETH_P_IPV6): | |
4837 | case htons(ETH_P_8021Q): | |
4838 | case htons(ETH_P_8021AD): | |
b4b9e355 MG |
4839 | return true; |
4840 | default: | |
4841 | return false; | |
4842 | } | |
4843 | } | |
4844 | ||
e687ad60 PN |
4845 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
4846 | int *ret, struct net_device *orig_dev) | |
4847 | { | |
e7582bab | 4848 | #ifdef CONFIG_NETFILTER_INGRESS |
e687ad60 | 4849 | if (nf_hook_ingress_active(skb)) { |
2c1e2703 AC |
4850 | int ingress_retval; |
4851 | ||
e687ad60 PN |
4852 | if (*pt_prev) { |
4853 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4854 | *pt_prev = NULL; | |
4855 | } | |
4856 | ||
2c1e2703 AC |
4857 | rcu_read_lock(); |
4858 | ingress_retval = nf_hook_ingress(skb); | |
4859 | rcu_read_unlock(); | |
4860 | return ingress_retval; | |
e687ad60 | 4861 | } |
e7582bab | 4862 | #endif /* CONFIG_NETFILTER_INGRESS */ |
e687ad60 PN |
4863 | return 0; |
4864 | } | |
e687ad60 | 4865 | |
88eb1944 EC |
4866 | static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc, |
4867 | struct packet_type **ppt_prev) | |
1da177e4 LT |
4868 | { |
4869 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 4870 | rx_handler_func_t *rx_handler; |
f2ccd8fa | 4871 | struct net_device *orig_dev; |
8a4eb573 | 4872 | bool deliver_exact = false; |
1da177e4 | 4873 | int ret = NET_RX_DROP; |
252e3346 | 4874 | __be16 type; |
1da177e4 | 4875 | |
588f0330 | 4876 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
81bbb3d4 | 4877 | |
cf66ba58 | 4878 | trace_netif_receive_skb(skb); |
9b22ea56 | 4879 | |
cc9bd5ce | 4880 | orig_dev = skb->dev; |
8f903c70 | 4881 | |
c1d2bbe1 | 4882 | skb_reset_network_header(skb); |
fda55eca ED |
4883 | if (!skb_transport_header_was_set(skb)) |
4884 | skb_reset_transport_header(skb); | |
0b5c9db1 | 4885 | skb_reset_mac_len(skb); |
1da177e4 LT |
4886 | |
4887 | pt_prev = NULL; | |
4888 | ||
63d8ea7f | 4889 | another_round: |
b6858177 | 4890 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
4891 | |
4892 | __this_cpu_inc(softnet_data.processed); | |
4893 | ||
458bf2f2 SH |
4894 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
4895 | int ret2; | |
4896 | ||
4897 | preempt_disable(); | |
4898 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb); | |
4899 | preempt_enable(); | |
4900 | ||
4901 | if (ret2 != XDP_PASS) | |
4902 | return NET_RX_DROP; | |
4903 | skb_reset_mac_len(skb); | |
4904 | } | |
4905 | ||
8ad227ff PM |
4906 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
4907 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { | |
0d5501c1 | 4908 | skb = skb_vlan_untag(skb); |
bcc6d479 | 4909 | if (unlikely(!skb)) |
2c17d27c | 4910 | goto out; |
bcc6d479 JP |
4911 | } |
4912 | ||
e7246e12 WB |
4913 | if (skb_skip_tc_classify(skb)) |
4914 | goto skip_classify; | |
1da177e4 | 4915 | |
9754e293 | 4916 | if (pfmemalloc) |
b4b9e355 MG |
4917 | goto skip_taps; |
4918 | ||
1da177e4 | 4919 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
7866a621 SN |
4920 | if (pt_prev) |
4921 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
4922 | pt_prev = ptype; | |
4923 | } | |
4924 | ||
4925 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { | |
4926 | if (pt_prev) | |
4927 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
4928 | pt_prev = ptype; | |
1da177e4 LT |
4929 | } |
4930 | ||
b4b9e355 | 4931 | skip_taps: |
1cf51900 | 4932 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 4933 | if (static_branch_unlikely(&ingress_needed_key)) { |
1f211a1b | 4934 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev); |
4577139b | 4935 | if (!skb) |
2c17d27c | 4936 | goto out; |
e687ad60 PN |
4937 | |
4938 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) | |
2c17d27c | 4939 | goto out; |
4577139b | 4940 | } |
1cf51900 | 4941 | #endif |
a5135bcf | 4942 | skb_reset_tc(skb); |
e7246e12 | 4943 | skip_classify: |
9754e293 | 4944 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
b4b9e355 MG |
4945 | goto drop; |
4946 | ||
df8a39de | 4947 | if (skb_vlan_tag_present(skb)) { |
2425717b JF |
4948 | if (pt_prev) { |
4949 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
4950 | pt_prev = NULL; | |
4951 | } | |
48cc32d3 | 4952 | if (vlan_do_receive(&skb)) |
2425717b JF |
4953 | goto another_round; |
4954 | else if (unlikely(!skb)) | |
2c17d27c | 4955 | goto out; |
2425717b JF |
4956 | } |
4957 | ||
48cc32d3 | 4958 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
4959 | if (rx_handler) { |
4960 | if (pt_prev) { | |
4961 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
4962 | pt_prev = NULL; | |
4963 | } | |
8a4eb573 JP |
4964 | switch (rx_handler(&skb)) { |
4965 | case RX_HANDLER_CONSUMED: | |
3bc1b1ad | 4966 | ret = NET_RX_SUCCESS; |
2c17d27c | 4967 | goto out; |
8a4eb573 | 4968 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 4969 | goto another_round; |
8a4eb573 JP |
4970 | case RX_HANDLER_EXACT: |
4971 | deliver_exact = true; | |
4972 | case RX_HANDLER_PASS: | |
4973 | break; | |
4974 | default: | |
4975 | BUG(); | |
4976 | } | |
ab95bfe0 | 4977 | } |
1da177e4 | 4978 | |
df8a39de | 4979 | if (unlikely(skb_vlan_tag_present(skb))) { |
36b2f61a GV |
4980 | check_vlan_id: |
4981 | if (skb_vlan_tag_get_id(skb)) { | |
4982 | /* Vlan id is non 0 and vlan_do_receive() above couldn't | |
4983 | * find vlan device. | |
4984 | */ | |
d4b812de | 4985 | skb->pkt_type = PACKET_OTHERHOST; |
36b2f61a GV |
4986 | } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
4987 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { | |
4988 | /* Outer header is 802.1P with vlan 0, inner header is | |
4989 | * 802.1Q or 802.1AD and vlan_do_receive() above could | |
4990 | * not find vlan dev for vlan id 0. | |
4991 | */ | |
4992 | __vlan_hwaccel_clear_tag(skb); | |
4993 | skb = skb_vlan_untag(skb); | |
4994 | if (unlikely(!skb)) | |
4995 | goto out; | |
4996 | if (vlan_do_receive(&skb)) | |
4997 | /* After stripping off 802.1P header with vlan 0 | |
4998 | * vlan dev is found for inner header. | |
4999 | */ | |
5000 | goto another_round; | |
5001 | else if (unlikely(!skb)) | |
5002 | goto out; | |
5003 | else | |
5004 | /* We have stripped outer 802.1P vlan 0 header. | |
5005 | * But could not find vlan dev. | |
5006 | * check again for vlan id to set OTHERHOST. | |
5007 | */ | |
5008 | goto check_vlan_id; | |
5009 | } | |
d4b812de ED |
5010 | /* Note: we might in the future use prio bits |
5011 | * and set skb->priority like in vlan_do_receive() | |
5012 | * For the time being, just ignore Priority Code Point | |
5013 | */ | |
b1817524 | 5014 | __vlan_hwaccel_clear_tag(skb); |
d4b812de | 5015 | } |
48cc32d3 | 5016 | |
7866a621 SN |
5017 | type = skb->protocol; |
5018 | ||
63d8ea7f | 5019 | /* deliver only exact match when indicated */ |
7866a621 SN |
5020 | if (likely(!deliver_exact)) { |
5021 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5022 | &ptype_base[ntohs(type) & | |
5023 | PTYPE_HASH_MASK]); | |
5024 | } | |
1f3c8804 | 5025 | |
7866a621 SN |
5026 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
5027 | &orig_dev->ptype_specific); | |
5028 | ||
5029 | if (unlikely(skb->dev != orig_dev)) { | |
5030 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5031 | &skb->dev->ptype_specific); | |
1da177e4 LT |
5032 | } |
5033 | ||
5034 | if (pt_prev) { | |
1f8b977a | 5035 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
0e698bf6 | 5036 | goto drop; |
88eb1944 | 5037 | *ppt_prev = pt_prev; |
1da177e4 | 5038 | } else { |
b4b9e355 | 5039 | drop: |
6e7333d3 JW |
5040 | if (!deliver_exact) |
5041 | atomic_long_inc(&skb->dev->rx_dropped); | |
5042 | else | |
5043 | atomic_long_inc(&skb->dev->rx_nohandler); | |
1da177e4 LT |
5044 | kfree_skb(skb); |
5045 | /* Jamal, now you will not able to escape explaining | |
5046 | * me how you were going to use this. :-) | |
5047 | */ | |
5048 | ret = NET_RX_DROP; | |
5049 | } | |
5050 | ||
2c17d27c | 5051 | out: |
9754e293 DM |
5052 | return ret; |
5053 | } | |
5054 | ||
88eb1944 EC |
5055 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
5056 | { | |
5057 | struct net_device *orig_dev = skb->dev; | |
5058 | struct packet_type *pt_prev = NULL; | |
5059 | int ret; | |
5060 | ||
5061 | ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev); | |
5062 | if (pt_prev) | |
f5737cba PA |
5063 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
5064 | skb->dev, pt_prev, orig_dev); | |
88eb1944 EC |
5065 | return ret; |
5066 | } | |
5067 | ||
1c601d82 JDB |
5068 | /** |
5069 | * netif_receive_skb_core - special purpose version of netif_receive_skb | |
5070 | * @skb: buffer to process | |
5071 | * | |
5072 | * More direct receive version of netif_receive_skb(). It should | |
5073 | * only be used by callers that have a need to skip RPS and Generic XDP. | |
5074 | * Caller must also take care of handling if (page_is_)pfmemalloc. | |
5075 | * | |
5076 | * This function may only be called from softirq context and interrupts | |
5077 | * should be enabled. | |
5078 | * | |
5079 | * Return values (usually ignored): | |
5080 | * NET_RX_SUCCESS: no congestion | |
5081 | * NET_RX_DROP: packet was dropped | |
5082 | */ | |
5083 | int netif_receive_skb_core(struct sk_buff *skb) | |
5084 | { | |
5085 | int ret; | |
5086 | ||
5087 | rcu_read_lock(); | |
88eb1944 | 5088 | ret = __netif_receive_skb_one_core(skb, false); |
1c601d82 JDB |
5089 | rcu_read_unlock(); |
5090 | ||
5091 | return ret; | |
5092 | } | |
5093 | EXPORT_SYMBOL(netif_receive_skb_core); | |
5094 | ||
88eb1944 EC |
5095 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
5096 | struct packet_type *pt_prev, | |
5097 | struct net_device *orig_dev) | |
4ce0017a EC |
5098 | { |
5099 | struct sk_buff *skb, *next; | |
5100 | ||
88eb1944 EC |
5101 | if (!pt_prev) |
5102 | return; | |
5103 | if (list_empty(head)) | |
5104 | return; | |
17266ee9 | 5105 | if (pt_prev->list_func != NULL) |
fdf71426 PA |
5106 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
5107 | ip_list_rcv, head, pt_prev, orig_dev); | |
17266ee9 | 5108 | else |
9a5a90d1 AL |
5109 | list_for_each_entry_safe(skb, next, head, list) { |
5110 | skb_list_del_init(skb); | |
fdf71426 | 5111 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
9a5a90d1 | 5112 | } |
88eb1944 EC |
5113 | } |
5114 | ||
5115 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) | |
5116 | { | |
5117 | /* Fast-path assumptions: | |
5118 | * - There is no RX handler. | |
5119 | * - Only one packet_type matches. | |
5120 | * If either of these fails, we will end up doing some per-packet | |
5121 | * processing in-line, then handling the 'last ptype' for the whole | |
5122 | * sublist. This can't cause out-of-order delivery to any single ptype, | |
5123 | * because the 'last ptype' must be constant across the sublist, and all | |
5124 | * other ptypes are handled per-packet. | |
5125 | */ | |
5126 | /* Current (common) ptype of sublist */ | |
5127 | struct packet_type *pt_curr = NULL; | |
5128 | /* Current (common) orig_dev of sublist */ | |
5129 | struct net_device *od_curr = NULL; | |
5130 | struct list_head sublist; | |
5131 | struct sk_buff *skb, *next; | |
5132 | ||
9af86f93 | 5133 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5134 | list_for_each_entry_safe(skb, next, head, list) { |
5135 | struct net_device *orig_dev = skb->dev; | |
5136 | struct packet_type *pt_prev = NULL; | |
5137 | ||
22f6bbb7 | 5138 | skb_list_del_init(skb); |
88eb1944 | 5139 | __netif_receive_skb_core(skb, pfmemalloc, &pt_prev); |
9af86f93 EC |
5140 | if (!pt_prev) |
5141 | continue; | |
88eb1944 EC |
5142 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
5143 | /* dispatch old sublist */ | |
88eb1944 EC |
5144 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
5145 | /* start new sublist */ | |
9af86f93 | 5146 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5147 | pt_curr = pt_prev; |
5148 | od_curr = orig_dev; | |
5149 | } | |
9af86f93 | 5150 | list_add_tail(&skb->list, &sublist); |
88eb1944 EC |
5151 | } |
5152 | ||
5153 | /* dispatch final sublist */ | |
9af86f93 | 5154 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
4ce0017a EC |
5155 | } |
5156 | ||
9754e293 DM |
5157 | static int __netif_receive_skb(struct sk_buff *skb) |
5158 | { | |
5159 | int ret; | |
5160 | ||
5161 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { | |
f1083048 | 5162 | unsigned int noreclaim_flag; |
9754e293 DM |
5163 | |
5164 | /* | |
5165 | * PFMEMALLOC skbs are special, they should | |
5166 | * - be delivered to SOCK_MEMALLOC sockets only | |
5167 | * - stay away from userspace | |
5168 | * - have bounded memory usage | |
5169 | * | |
5170 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
5171 | * context down to all allocation sites. | |
5172 | */ | |
f1083048 | 5173 | noreclaim_flag = memalloc_noreclaim_save(); |
88eb1944 | 5174 | ret = __netif_receive_skb_one_core(skb, true); |
f1083048 | 5175 | memalloc_noreclaim_restore(noreclaim_flag); |
9754e293 | 5176 | } else |
88eb1944 | 5177 | ret = __netif_receive_skb_one_core(skb, false); |
9754e293 | 5178 | |
1da177e4 LT |
5179 | return ret; |
5180 | } | |
0a9627f2 | 5181 | |
4ce0017a EC |
5182 | static void __netif_receive_skb_list(struct list_head *head) |
5183 | { | |
5184 | unsigned long noreclaim_flag = 0; | |
5185 | struct sk_buff *skb, *next; | |
5186 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ | |
5187 | ||
5188 | list_for_each_entry_safe(skb, next, head, list) { | |
5189 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { | |
5190 | struct list_head sublist; | |
5191 | ||
5192 | /* Handle the previous sublist */ | |
5193 | list_cut_before(&sublist, head, &skb->list); | |
b9f463d6 EC |
5194 | if (!list_empty(&sublist)) |
5195 | __netif_receive_skb_list_core(&sublist, pfmemalloc); | |
4ce0017a EC |
5196 | pfmemalloc = !pfmemalloc; |
5197 | /* See comments in __netif_receive_skb */ | |
5198 | if (pfmemalloc) | |
5199 | noreclaim_flag = memalloc_noreclaim_save(); | |
5200 | else | |
5201 | memalloc_noreclaim_restore(noreclaim_flag); | |
5202 | } | |
5203 | } | |
5204 | /* Handle the remaining sublist */ | |
b9f463d6 EC |
5205 | if (!list_empty(head)) |
5206 | __netif_receive_skb_list_core(head, pfmemalloc); | |
4ce0017a EC |
5207 | /* Restore pflags */ |
5208 | if (pfmemalloc) | |
5209 | memalloc_noreclaim_restore(noreclaim_flag); | |
5210 | } | |
5211 | ||
f4e63525 | 5212 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
b5cdae32 | 5213 | { |
58038695 | 5214 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
b5cdae32 DM |
5215 | struct bpf_prog *new = xdp->prog; |
5216 | int ret = 0; | |
5217 | ||
5218 | switch (xdp->command) { | |
58038695 | 5219 | case XDP_SETUP_PROG: |
b5cdae32 DM |
5220 | rcu_assign_pointer(dev->xdp_prog, new); |
5221 | if (old) | |
5222 | bpf_prog_put(old); | |
5223 | ||
5224 | if (old && !new) { | |
02786475 | 5225 | static_branch_dec(&generic_xdp_needed_key); |
b5cdae32 | 5226 | } else if (new && !old) { |
02786475 | 5227 | static_branch_inc(&generic_xdp_needed_key); |
b5cdae32 | 5228 | dev_disable_lro(dev); |
56f5aa77 | 5229 | dev_disable_gro_hw(dev); |
b5cdae32 DM |
5230 | } |
5231 | break; | |
b5cdae32 DM |
5232 | |
5233 | case XDP_QUERY_PROG: | |
58038695 | 5234 | xdp->prog_id = old ? old->aux->id : 0; |
b5cdae32 DM |
5235 | break; |
5236 | ||
5237 | default: | |
5238 | ret = -EINVAL; | |
5239 | break; | |
5240 | } | |
5241 | ||
5242 | return ret; | |
5243 | } | |
5244 | ||
ae78dbfa | 5245 | static int netif_receive_skb_internal(struct sk_buff *skb) |
0a9627f2 | 5246 | { |
2c17d27c JA |
5247 | int ret; |
5248 | ||
588f0330 | 5249 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
3b098e2d | 5250 | |
c1f19b51 RC |
5251 | if (skb_defer_rx_timestamp(skb)) |
5252 | return NET_RX_SUCCESS; | |
5253 | ||
bbbe211c | 5254 | rcu_read_lock(); |
df334545 | 5255 | #ifdef CONFIG_RPS |
dc05360f | 5256 | if (static_branch_unlikely(&rps_needed)) { |
3b098e2d | 5257 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
2c17d27c | 5258 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
0a9627f2 | 5259 | |
3b098e2d ED |
5260 | if (cpu >= 0) { |
5261 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5262 | rcu_read_unlock(); | |
adc9300e | 5263 | return ret; |
3b098e2d | 5264 | } |
fec5e652 | 5265 | } |
1e94d72f | 5266 | #endif |
2c17d27c JA |
5267 | ret = __netif_receive_skb(skb); |
5268 | rcu_read_unlock(); | |
5269 | return ret; | |
0a9627f2 | 5270 | } |
ae78dbfa | 5271 | |
7da517a3 EC |
5272 | static void netif_receive_skb_list_internal(struct list_head *head) |
5273 | { | |
7da517a3 | 5274 | struct sk_buff *skb, *next; |
8c057efa | 5275 | struct list_head sublist; |
7da517a3 | 5276 | |
8c057efa | 5277 | INIT_LIST_HEAD(&sublist); |
7da517a3 EC |
5278 | list_for_each_entry_safe(skb, next, head, list) { |
5279 | net_timestamp_check(netdev_tstamp_prequeue, skb); | |
22f6bbb7 | 5280 | skb_list_del_init(skb); |
8c057efa EC |
5281 | if (!skb_defer_rx_timestamp(skb)) |
5282 | list_add_tail(&skb->list, &sublist); | |
7da517a3 | 5283 | } |
8c057efa | 5284 | list_splice_init(&sublist, head); |
7da517a3 | 5285 | |
7da517a3 EC |
5286 | rcu_read_lock(); |
5287 | #ifdef CONFIG_RPS | |
dc05360f | 5288 | if (static_branch_unlikely(&rps_needed)) { |
7da517a3 EC |
5289 | list_for_each_entry_safe(skb, next, head, list) { |
5290 | struct rps_dev_flow voidflow, *rflow = &voidflow; | |
5291 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
5292 | ||
5293 | if (cpu >= 0) { | |
8c057efa | 5294 | /* Will be handled, remove from list */ |
22f6bbb7 | 5295 | skb_list_del_init(skb); |
8c057efa | 5296 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
7da517a3 EC |
5297 | } |
5298 | } | |
5299 | } | |
5300 | #endif | |
5301 | __netif_receive_skb_list(head); | |
5302 | rcu_read_unlock(); | |
5303 | } | |
5304 | ||
ae78dbfa BH |
5305 | /** |
5306 | * netif_receive_skb - process receive buffer from network | |
5307 | * @skb: buffer to process | |
5308 | * | |
5309 | * netif_receive_skb() is the main receive data processing function. | |
5310 | * It always succeeds. The buffer may be dropped during processing | |
5311 | * for congestion control or by the protocol layers. | |
5312 | * | |
5313 | * This function may only be called from softirq context and interrupts | |
5314 | * should be enabled. | |
5315 | * | |
5316 | * Return values (usually ignored): | |
5317 | * NET_RX_SUCCESS: no congestion | |
5318 | * NET_RX_DROP: packet was dropped | |
5319 | */ | |
04eb4489 | 5320 | int netif_receive_skb(struct sk_buff *skb) |
ae78dbfa | 5321 | { |
b0e3f1bd GB |
5322 | int ret; |
5323 | ||
ae78dbfa BH |
5324 | trace_netif_receive_skb_entry(skb); |
5325 | ||
b0e3f1bd GB |
5326 | ret = netif_receive_skb_internal(skb); |
5327 | trace_netif_receive_skb_exit(ret); | |
5328 | ||
5329 | return ret; | |
ae78dbfa | 5330 | } |
04eb4489 | 5331 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 5332 | |
f6ad8c1b EC |
5333 | /** |
5334 | * netif_receive_skb_list - process many receive buffers from network | |
5335 | * @head: list of skbs to process. | |
5336 | * | |
7da517a3 EC |
5337 | * Since return value of netif_receive_skb() is normally ignored, and |
5338 | * wouldn't be meaningful for a list, this function returns void. | |
f6ad8c1b EC |
5339 | * |
5340 | * This function may only be called from softirq context and interrupts | |
5341 | * should be enabled. | |
5342 | */ | |
5343 | void netif_receive_skb_list(struct list_head *head) | |
5344 | { | |
7da517a3 | 5345 | struct sk_buff *skb; |
f6ad8c1b | 5346 | |
b9f463d6 EC |
5347 | if (list_empty(head)) |
5348 | return; | |
b0e3f1bd GB |
5349 | if (trace_netif_receive_skb_list_entry_enabled()) { |
5350 | list_for_each_entry(skb, head, list) | |
5351 | trace_netif_receive_skb_list_entry(skb); | |
5352 | } | |
7da517a3 | 5353 | netif_receive_skb_list_internal(head); |
b0e3f1bd | 5354 | trace_netif_receive_skb_list_exit(0); |
f6ad8c1b EC |
5355 | } |
5356 | EXPORT_SYMBOL(netif_receive_skb_list); | |
5357 | ||
41852497 | 5358 | DEFINE_PER_CPU(struct work_struct, flush_works); |
145dd5f9 PA |
5359 | |
5360 | /* Network device is going away, flush any packets still pending */ | |
5361 | static void flush_backlog(struct work_struct *work) | |
6e583ce5 | 5362 | { |
6e583ce5 | 5363 | struct sk_buff *skb, *tmp; |
145dd5f9 PA |
5364 | struct softnet_data *sd; |
5365 | ||
5366 | local_bh_disable(); | |
5367 | sd = this_cpu_ptr(&softnet_data); | |
6e583ce5 | 5368 | |
145dd5f9 | 5369 | local_irq_disable(); |
e36fa2f7 | 5370 | rps_lock(sd); |
6e7676c1 | 5371 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
41852497 | 5372 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
e36fa2f7 | 5373 | __skb_unlink(skb, &sd->input_pkt_queue); |
6e583ce5 | 5374 | kfree_skb(skb); |
76cc8b13 | 5375 | input_queue_head_incr(sd); |
6e583ce5 | 5376 | } |
6e7676c1 | 5377 | } |
e36fa2f7 | 5378 | rps_unlock(sd); |
145dd5f9 | 5379 | local_irq_enable(); |
6e7676c1 CG |
5380 | |
5381 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
41852497 | 5382 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
6e7676c1 CG |
5383 | __skb_unlink(skb, &sd->process_queue); |
5384 | kfree_skb(skb); | |
76cc8b13 | 5385 | input_queue_head_incr(sd); |
6e7676c1 CG |
5386 | } |
5387 | } | |
145dd5f9 PA |
5388 | local_bh_enable(); |
5389 | } | |
5390 | ||
41852497 | 5391 | static void flush_all_backlogs(void) |
145dd5f9 PA |
5392 | { |
5393 | unsigned int cpu; | |
5394 | ||
5395 | get_online_cpus(); | |
5396 | ||
41852497 ED |
5397 | for_each_online_cpu(cpu) |
5398 | queue_work_on(cpu, system_highpri_wq, | |
5399 | per_cpu_ptr(&flush_works, cpu)); | |
145dd5f9 PA |
5400 | |
5401 | for_each_online_cpu(cpu) | |
41852497 | 5402 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
145dd5f9 PA |
5403 | |
5404 | put_online_cpus(); | |
6e583ce5 SH |
5405 | } |
5406 | ||
aaa5d90b PA |
5407 | INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int)); |
5408 | INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int)); | |
d565b0a1 HX |
5409 | static int napi_gro_complete(struct sk_buff *skb) |
5410 | { | |
22061d80 | 5411 | struct packet_offload *ptype; |
d565b0a1 | 5412 | __be16 type = skb->protocol; |
22061d80 | 5413 | struct list_head *head = &offload_base; |
d565b0a1 HX |
5414 | int err = -ENOENT; |
5415 | ||
c3c7c254 ED |
5416 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
5417 | ||
fc59f9a3 HX |
5418 | if (NAPI_GRO_CB(skb)->count == 1) { |
5419 | skb_shinfo(skb)->gso_size = 0; | |
d565b0a1 | 5420 | goto out; |
fc59f9a3 | 5421 | } |
d565b0a1 HX |
5422 | |
5423 | rcu_read_lock(); | |
5424 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5425 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
d565b0a1 HX |
5426 | continue; |
5427 | ||
aaa5d90b PA |
5428 | err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete, |
5429 | ipv6_gro_complete, inet_gro_complete, | |
5430 | skb, 0); | |
d565b0a1 HX |
5431 | break; |
5432 | } | |
5433 | rcu_read_unlock(); | |
5434 | ||
5435 | if (err) { | |
5436 | WARN_ON(&ptype->list == head); | |
5437 | kfree_skb(skb); | |
5438 | return NET_RX_SUCCESS; | |
5439 | } | |
5440 | ||
5441 | out: | |
ae78dbfa | 5442 | return netif_receive_skb_internal(skb); |
d565b0a1 HX |
5443 | } |
5444 | ||
6312fe77 | 5445 | static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index, |
07d78363 | 5446 | bool flush_old) |
d565b0a1 | 5447 | { |
6312fe77 | 5448 | struct list_head *head = &napi->gro_hash[index].list; |
d4546c25 | 5449 | struct sk_buff *skb, *p; |
2e71a6f8 | 5450 | |
07d78363 | 5451 | list_for_each_entry_safe_reverse(skb, p, head, list) { |
2e71a6f8 ED |
5452 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) |
5453 | return; | |
992cba7e | 5454 | skb_list_del_init(skb); |
d565b0a1 | 5455 | napi_gro_complete(skb); |
6312fe77 | 5456 | napi->gro_hash[index].count--; |
d565b0a1 | 5457 | } |
d9f37d01 LR |
5458 | |
5459 | if (!napi->gro_hash[index].count) | |
5460 | __clear_bit(index, &napi->gro_bitmask); | |
d565b0a1 | 5461 | } |
07d78363 | 5462 | |
6312fe77 | 5463 | /* napi->gro_hash[].list contains packets ordered by age. |
07d78363 DM |
5464 | * youngest packets at the head of it. |
5465 | * Complete skbs in reverse order to reduce latencies. | |
5466 | */ | |
5467 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) | |
5468 | { | |
42519ede ED |
5469 | unsigned long bitmask = napi->gro_bitmask; |
5470 | unsigned int i, base = ~0U; | |
07d78363 | 5471 | |
42519ede ED |
5472 | while ((i = ffs(bitmask)) != 0) { |
5473 | bitmask >>= i; | |
5474 | base += i; | |
5475 | __napi_gro_flush_chain(napi, base, flush_old); | |
d9f37d01 | 5476 | } |
07d78363 | 5477 | } |
86cac58b | 5478 | EXPORT_SYMBOL(napi_gro_flush); |
d565b0a1 | 5479 | |
07d78363 DM |
5480 | static struct list_head *gro_list_prepare(struct napi_struct *napi, |
5481 | struct sk_buff *skb) | |
89c5fa33 | 5482 | { |
89c5fa33 | 5483 | unsigned int maclen = skb->dev->hard_header_len; |
0b4cec8c | 5484 | u32 hash = skb_get_hash_raw(skb); |
07d78363 | 5485 | struct list_head *head; |
d4546c25 | 5486 | struct sk_buff *p; |
89c5fa33 | 5487 | |
6312fe77 | 5488 | head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list; |
07d78363 | 5489 | list_for_each_entry(p, head, list) { |
89c5fa33 ED |
5490 | unsigned long diffs; |
5491 | ||
0b4cec8c TH |
5492 | NAPI_GRO_CB(p)->flush = 0; |
5493 | ||
5494 | if (hash != skb_get_hash_raw(p)) { | |
5495 | NAPI_GRO_CB(p)->same_flow = 0; | |
5496 | continue; | |
5497 | } | |
5498 | ||
89c5fa33 | 5499 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; |
b1817524 MM |
5500 | diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb); |
5501 | if (skb_vlan_tag_present(p)) | |
5502 | diffs |= p->vlan_tci ^ skb->vlan_tci; | |
ce87fc6c | 5503 | diffs |= skb_metadata_dst_cmp(p, skb); |
de8f3a83 | 5504 | diffs |= skb_metadata_differs(p, skb); |
89c5fa33 ED |
5505 | if (maclen == ETH_HLEN) |
5506 | diffs |= compare_ether_header(skb_mac_header(p), | |
a50e233c | 5507 | skb_mac_header(skb)); |
89c5fa33 ED |
5508 | else if (!diffs) |
5509 | diffs = memcmp(skb_mac_header(p), | |
a50e233c | 5510 | skb_mac_header(skb), |
89c5fa33 ED |
5511 | maclen); |
5512 | NAPI_GRO_CB(p)->same_flow = !diffs; | |
89c5fa33 | 5513 | } |
07d78363 DM |
5514 | |
5515 | return head; | |
89c5fa33 ED |
5516 | } |
5517 | ||
299603e8 JC |
5518 | static void skb_gro_reset_offset(struct sk_buff *skb) |
5519 | { | |
5520 | const struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5521 | const skb_frag_t *frag0 = &pinfo->frags[0]; | |
5522 | ||
5523 | NAPI_GRO_CB(skb)->data_offset = 0; | |
5524 | NAPI_GRO_CB(skb)->frag0 = NULL; | |
5525 | NAPI_GRO_CB(skb)->frag0_len = 0; | |
5526 | ||
5527 | if (skb_mac_header(skb) == skb_tail_pointer(skb) && | |
5528 | pinfo->nr_frags && | |
5529 | !PageHighMem(skb_frag_page(frag0))) { | |
5530 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); | |
7cfd5fd5 ED |
5531 | NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int, |
5532 | skb_frag_size(frag0), | |
5533 | skb->end - skb->tail); | |
89c5fa33 ED |
5534 | } |
5535 | } | |
5536 | ||
a50e233c ED |
5537 | static void gro_pull_from_frag0(struct sk_buff *skb, int grow) |
5538 | { | |
5539 | struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5540 | ||
5541 | BUG_ON(skb->end - skb->tail < grow); | |
5542 | ||
5543 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); | |
5544 | ||
5545 | skb->data_len -= grow; | |
5546 | skb->tail += grow; | |
5547 | ||
b54c9d5b | 5548 | skb_frag_off_add(&pinfo->frags[0], grow); |
a50e233c ED |
5549 | skb_frag_size_sub(&pinfo->frags[0], grow); |
5550 | ||
5551 | if (unlikely(!skb_frag_size(&pinfo->frags[0]))) { | |
5552 | skb_frag_unref(skb, 0); | |
5553 | memmove(pinfo->frags, pinfo->frags + 1, | |
5554 | --pinfo->nr_frags * sizeof(pinfo->frags[0])); | |
5555 | } | |
5556 | } | |
5557 | ||
6312fe77 | 5558 | static void gro_flush_oldest(struct list_head *head) |
07d78363 | 5559 | { |
6312fe77 | 5560 | struct sk_buff *oldest; |
07d78363 | 5561 | |
6312fe77 | 5562 | oldest = list_last_entry(head, struct sk_buff, list); |
07d78363 | 5563 | |
6312fe77 | 5564 | /* We are called with head length >= MAX_GRO_SKBS, so this is |
07d78363 DM |
5565 | * impossible. |
5566 | */ | |
5567 | if (WARN_ON_ONCE(!oldest)) | |
5568 | return; | |
5569 | ||
d9f37d01 LR |
5570 | /* Do not adjust napi->gro_hash[].count, caller is adding a new |
5571 | * SKB to the chain. | |
07d78363 | 5572 | */ |
ece23711 | 5573 | skb_list_del_init(oldest); |
07d78363 DM |
5574 | napi_gro_complete(oldest); |
5575 | } | |
5576 | ||
aaa5d90b PA |
5577 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *, |
5578 | struct sk_buff *)); | |
5579 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *, | |
5580 | struct sk_buff *)); | |
bb728820 | 5581 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5582 | { |
6312fe77 | 5583 | u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1); |
d4546c25 | 5584 | struct list_head *head = &offload_base; |
22061d80 | 5585 | struct packet_offload *ptype; |
d565b0a1 | 5586 | __be16 type = skb->protocol; |
07d78363 | 5587 | struct list_head *gro_head; |
d4546c25 | 5588 | struct sk_buff *pp = NULL; |
5b252f0c | 5589 | enum gro_result ret; |
d4546c25 | 5590 | int same_flow; |
a50e233c | 5591 | int grow; |
d565b0a1 | 5592 | |
b5cdae32 | 5593 | if (netif_elide_gro(skb->dev)) |
d565b0a1 HX |
5594 | goto normal; |
5595 | ||
07d78363 | 5596 | gro_head = gro_list_prepare(napi, skb); |
89c5fa33 | 5597 | |
d565b0a1 HX |
5598 | rcu_read_lock(); |
5599 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5600 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
d565b0a1 HX |
5601 | continue; |
5602 | ||
86911732 | 5603 | skb_set_network_header(skb, skb_gro_offset(skb)); |
efd9450e | 5604 | skb_reset_mac_len(skb); |
d565b0a1 | 5605 | NAPI_GRO_CB(skb)->same_flow = 0; |
d61d072e | 5606 | NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb); |
5d38a079 | 5607 | NAPI_GRO_CB(skb)->free = 0; |
fac8e0f5 | 5608 | NAPI_GRO_CB(skb)->encap_mark = 0; |
fcd91dd4 | 5609 | NAPI_GRO_CB(skb)->recursion_counter = 0; |
a0ca153f | 5610 | NAPI_GRO_CB(skb)->is_fou = 0; |
1530545e | 5611 | NAPI_GRO_CB(skb)->is_atomic = 1; |
15e2396d | 5612 | NAPI_GRO_CB(skb)->gro_remcsum_start = 0; |
d565b0a1 | 5613 | |
662880f4 TH |
5614 | /* Setup for GRO checksum validation */ |
5615 | switch (skb->ip_summed) { | |
5616 | case CHECKSUM_COMPLETE: | |
5617 | NAPI_GRO_CB(skb)->csum = skb->csum; | |
5618 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
5619 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5620 | break; | |
5621 | case CHECKSUM_UNNECESSARY: | |
5622 | NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1; | |
5623 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5624 | break; | |
5625 | default: | |
5626 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5627 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5628 | } | |
d565b0a1 | 5629 | |
aaa5d90b PA |
5630 | pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive, |
5631 | ipv6_gro_receive, inet_gro_receive, | |
5632 | gro_head, skb); | |
d565b0a1 HX |
5633 | break; |
5634 | } | |
5635 | rcu_read_unlock(); | |
5636 | ||
5637 | if (&ptype->list == head) | |
5638 | goto normal; | |
5639 | ||
25393d3f SK |
5640 | if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) { |
5641 | ret = GRO_CONSUMED; | |
5642 | goto ok; | |
5643 | } | |
5644 | ||
0da2afd5 | 5645 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
5d0d9be8 | 5646 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
0da2afd5 | 5647 | |
d565b0a1 | 5648 | if (pp) { |
992cba7e | 5649 | skb_list_del_init(pp); |
d4546c25 | 5650 | napi_gro_complete(pp); |
6312fe77 | 5651 | napi->gro_hash[hash].count--; |
d565b0a1 HX |
5652 | } |
5653 | ||
0da2afd5 | 5654 | if (same_flow) |
d565b0a1 HX |
5655 | goto ok; |
5656 | ||
600adc18 | 5657 | if (NAPI_GRO_CB(skb)->flush) |
d565b0a1 | 5658 | goto normal; |
d565b0a1 | 5659 | |
6312fe77 LR |
5660 | if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) { |
5661 | gro_flush_oldest(gro_head); | |
600adc18 | 5662 | } else { |
6312fe77 | 5663 | napi->gro_hash[hash].count++; |
600adc18 | 5664 | } |
d565b0a1 | 5665 | NAPI_GRO_CB(skb)->count = 1; |
2e71a6f8 | 5666 | NAPI_GRO_CB(skb)->age = jiffies; |
29e98242 | 5667 | NAPI_GRO_CB(skb)->last = skb; |
86911732 | 5668 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
07d78363 | 5669 | list_add(&skb->list, gro_head); |
5d0d9be8 | 5670 | ret = GRO_HELD; |
d565b0a1 | 5671 | |
ad0f9904 | 5672 | pull: |
a50e233c ED |
5673 | grow = skb_gro_offset(skb) - skb_headlen(skb); |
5674 | if (grow > 0) | |
5675 | gro_pull_from_frag0(skb, grow); | |
d565b0a1 | 5676 | ok: |
d9f37d01 LR |
5677 | if (napi->gro_hash[hash].count) { |
5678 | if (!test_bit(hash, &napi->gro_bitmask)) | |
5679 | __set_bit(hash, &napi->gro_bitmask); | |
5680 | } else if (test_bit(hash, &napi->gro_bitmask)) { | |
5681 | __clear_bit(hash, &napi->gro_bitmask); | |
5682 | } | |
5683 | ||
5d0d9be8 | 5684 | return ret; |
d565b0a1 HX |
5685 | |
5686 | normal: | |
ad0f9904 HX |
5687 | ret = GRO_NORMAL; |
5688 | goto pull; | |
5d38a079 | 5689 | } |
96e93eab | 5690 | |
bf5a755f JC |
5691 | struct packet_offload *gro_find_receive_by_type(__be16 type) |
5692 | { | |
5693 | struct list_head *offload_head = &offload_base; | |
5694 | struct packet_offload *ptype; | |
5695 | ||
5696 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
5697 | if (ptype->type != type || !ptype->callbacks.gro_receive) | |
5698 | continue; | |
5699 | return ptype; | |
5700 | } | |
5701 | return NULL; | |
5702 | } | |
e27a2f83 | 5703 | EXPORT_SYMBOL(gro_find_receive_by_type); |
bf5a755f JC |
5704 | |
5705 | struct packet_offload *gro_find_complete_by_type(__be16 type) | |
5706 | { | |
5707 | struct list_head *offload_head = &offload_base; | |
5708 | struct packet_offload *ptype; | |
5709 | ||
5710 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
5711 | if (ptype->type != type || !ptype->callbacks.gro_complete) | |
5712 | continue; | |
5713 | return ptype; | |
5714 | } | |
5715 | return NULL; | |
5716 | } | |
e27a2f83 | 5717 | EXPORT_SYMBOL(gro_find_complete_by_type); |
5d38a079 | 5718 | |
e44699d2 MK |
5719 | static void napi_skb_free_stolen_head(struct sk_buff *skb) |
5720 | { | |
5721 | skb_dst_drop(skb); | |
174e2381 | 5722 | skb_ext_put(skb); |
e44699d2 MK |
5723 | kmem_cache_free(skbuff_head_cache, skb); |
5724 | } | |
5725 | ||
bb728820 | 5726 | static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb) |
5d38a079 | 5727 | { |
5d0d9be8 HX |
5728 | switch (ret) { |
5729 | case GRO_NORMAL: | |
ae78dbfa | 5730 | if (netif_receive_skb_internal(skb)) |
c7c4b3b6 BH |
5731 | ret = GRO_DROP; |
5732 | break; | |
5d38a079 | 5733 | |
5d0d9be8 | 5734 | case GRO_DROP: |
5d38a079 HX |
5735 | kfree_skb(skb); |
5736 | break; | |
5b252f0c | 5737 | |
daa86548 | 5738 | case GRO_MERGED_FREE: |
e44699d2 MK |
5739 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
5740 | napi_skb_free_stolen_head(skb); | |
5741 | else | |
d7e8883c | 5742 | __kfree_skb(skb); |
daa86548 ED |
5743 | break; |
5744 | ||
5b252f0c BH |
5745 | case GRO_HELD: |
5746 | case GRO_MERGED: | |
25393d3f | 5747 | case GRO_CONSUMED: |
5b252f0c | 5748 | break; |
5d38a079 HX |
5749 | } |
5750 | ||
c7c4b3b6 | 5751 | return ret; |
5d0d9be8 | 5752 | } |
5d0d9be8 | 5753 | |
c7c4b3b6 | 5754 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
5d0d9be8 | 5755 | { |
b0e3f1bd GB |
5756 | gro_result_t ret; |
5757 | ||
93f93a44 | 5758 | skb_mark_napi_id(skb, napi); |
ae78dbfa | 5759 | trace_napi_gro_receive_entry(skb); |
86911732 | 5760 | |
a50e233c ED |
5761 | skb_gro_reset_offset(skb); |
5762 | ||
b0e3f1bd GB |
5763 | ret = napi_skb_finish(dev_gro_receive(napi, skb), skb); |
5764 | trace_napi_gro_receive_exit(ret); | |
5765 | ||
5766 | return ret; | |
d565b0a1 HX |
5767 | } |
5768 | EXPORT_SYMBOL(napi_gro_receive); | |
5769 | ||
d0c2b0d2 | 5770 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
96e93eab | 5771 | { |
93a35f59 ED |
5772 | if (unlikely(skb->pfmemalloc)) { |
5773 | consume_skb(skb); | |
5774 | return; | |
5775 | } | |
96e93eab | 5776 | __skb_pull(skb, skb_headlen(skb)); |
2a2a459e ED |
5777 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
5778 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); | |
b1817524 | 5779 | __vlan_hwaccel_clear_tag(skb); |
66c46d74 | 5780 | skb->dev = napi->dev; |
6d152e23 | 5781 | skb->skb_iif = 0; |
33d9a2c7 ED |
5782 | |
5783 | /* eth_type_trans() assumes pkt_type is PACKET_HOST */ | |
5784 | skb->pkt_type = PACKET_HOST; | |
5785 | ||
c3caf119 JC |
5786 | skb->encapsulation = 0; |
5787 | skb_shinfo(skb)->gso_type = 0; | |
e33d0ba8 | 5788 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
174e2381 | 5789 | skb_ext_reset(skb); |
96e93eab HX |
5790 | |
5791 | napi->skb = skb; | |
5792 | } | |
96e93eab | 5793 | |
76620aaf | 5794 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
5d38a079 | 5795 | { |
5d38a079 | 5796 | struct sk_buff *skb = napi->skb; |
5d38a079 HX |
5797 | |
5798 | if (!skb) { | |
fd11a83d | 5799 | skb = napi_alloc_skb(napi, GRO_MAX_HEAD); |
e2f9dc3b ED |
5800 | if (skb) { |
5801 | napi->skb = skb; | |
5802 | skb_mark_napi_id(skb, napi); | |
5803 | } | |
80595d59 | 5804 | } |
96e93eab HX |
5805 | return skb; |
5806 | } | |
76620aaf | 5807 | EXPORT_SYMBOL(napi_get_frags); |
96e93eab | 5808 | |
323ebb61 EC |
5809 | /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */ |
5810 | static void gro_normal_list(struct napi_struct *napi) | |
5811 | { | |
5812 | if (!napi->rx_count) | |
5813 | return; | |
5814 | netif_receive_skb_list_internal(&napi->rx_list); | |
5815 | INIT_LIST_HEAD(&napi->rx_list); | |
5816 | napi->rx_count = 0; | |
5817 | } | |
5818 | ||
5819 | /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded, | |
5820 | * pass the whole batch up to the stack. | |
5821 | */ | |
5822 | static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb) | |
5823 | { | |
5824 | list_add_tail(&skb->list, &napi->rx_list); | |
5825 | if (++napi->rx_count >= gro_normal_batch) | |
5826 | gro_normal_list(napi); | |
5827 | } | |
5828 | ||
a50e233c ED |
5829 | static gro_result_t napi_frags_finish(struct napi_struct *napi, |
5830 | struct sk_buff *skb, | |
5831 | gro_result_t ret) | |
96e93eab | 5832 | { |
5d0d9be8 HX |
5833 | switch (ret) { |
5834 | case GRO_NORMAL: | |
a50e233c ED |
5835 | case GRO_HELD: |
5836 | __skb_push(skb, ETH_HLEN); | |
5837 | skb->protocol = eth_type_trans(skb, skb->dev); | |
323ebb61 EC |
5838 | if (ret == GRO_NORMAL) |
5839 | gro_normal_one(napi, skb); | |
86911732 | 5840 | break; |
5d38a079 | 5841 | |
5d0d9be8 | 5842 | case GRO_DROP: |
5d0d9be8 HX |
5843 | napi_reuse_skb(napi, skb); |
5844 | break; | |
5b252f0c | 5845 | |
e44699d2 MK |
5846 | case GRO_MERGED_FREE: |
5847 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) | |
5848 | napi_skb_free_stolen_head(skb); | |
5849 | else | |
5850 | napi_reuse_skb(napi, skb); | |
5851 | break; | |
5852 | ||
5b252f0c | 5853 | case GRO_MERGED: |
25393d3f | 5854 | case GRO_CONSUMED: |
5b252f0c | 5855 | break; |
5d0d9be8 | 5856 | } |
5d38a079 | 5857 | |
c7c4b3b6 | 5858 | return ret; |
5d38a079 | 5859 | } |
5d0d9be8 | 5860 | |
a50e233c ED |
5861 | /* Upper GRO stack assumes network header starts at gro_offset=0 |
5862 | * Drivers could call both napi_gro_frags() and napi_gro_receive() | |
5863 | * We copy ethernet header into skb->data to have a common layout. | |
5864 | */ | |
4adb9c4a | 5865 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
76620aaf HX |
5866 | { |
5867 | struct sk_buff *skb = napi->skb; | |
a50e233c ED |
5868 | const struct ethhdr *eth; |
5869 | unsigned int hlen = sizeof(*eth); | |
76620aaf HX |
5870 | |
5871 | napi->skb = NULL; | |
5872 | ||
a50e233c ED |
5873 | skb_reset_mac_header(skb); |
5874 | skb_gro_reset_offset(skb); | |
5875 | ||
a50e233c ED |
5876 | if (unlikely(skb_gro_header_hard(skb, hlen))) { |
5877 | eth = skb_gro_header_slow(skb, hlen, 0); | |
5878 | if (unlikely(!eth)) { | |
4da46ceb AC |
5879 | net_warn_ratelimited("%s: dropping impossible skb from %s\n", |
5880 | __func__, napi->dev->name); | |
a50e233c ED |
5881 | napi_reuse_skb(napi, skb); |
5882 | return NULL; | |
5883 | } | |
5884 | } else { | |
a4270d67 | 5885 | eth = (const struct ethhdr *)skb->data; |
a50e233c ED |
5886 | gro_pull_from_frag0(skb, hlen); |
5887 | NAPI_GRO_CB(skb)->frag0 += hlen; | |
5888 | NAPI_GRO_CB(skb)->frag0_len -= hlen; | |
76620aaf | 5889 | } |
a50e233c ED |
5890 | __skb_pull(skb, hlen); |
5891 | ||
5892 | /* | |
5893 | * This works because the only protocols we care about don't require | |
5894 | * special handling. | |
5895 | * We'll fix it up properly in napi_frags_finish() | |
5896 | */ | |
5897 | skb->protocol = eth->h_proto; | |
76620aaf | 5898 | |
76620aaf HX |
5899 | return skb; |
5900 | } | |
76620aaf | 5901 | |
c7c4b3b6 | 5902 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
5d0d9be8 | 5903 | { |
b0e3f1bd | 5904 | gro_result_t ret; |
76620aaf | 5905 | struct sk_buff *skb = napi_frags_skb(napi); |
5d0d9be8 HX |
5906 | |
5907 | if (!skb) | |
c7c4b3b6 | 5908 | return GRO_DROP; |
5d0d9be8 | 5909 | |
ae78dbfa BH |
5910 | trace_napi_gro_frags_entry(skb); |
5911 | ||
b0e3f1bd GB |
5912 | ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
5913 | trace_napi_gro_frags_exit(ret); | |
5914 | ||
5915 | return ret; | |
5d0d9be8 | 5916 | } |
5d38a079 HX |
5917 | EXPORT_SYMBOL(napi_gro_frags); |
5918 | ||
573e8fca TH |
5919 | /* Compute the checksum from gro_offset and return the folded value |
5920 | * after adding in any pseudo checksum. | |
5921 | */ | |
5922 | __sum16 __skb_gro_checksum_complete(struct sk_buff *skb) | |
5923 | { | |
5924 | __wsum wsum; | |
5925 | __sum16 sum; | |
5926 | ||
5927 | wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0); | |
5928 | ||
5929 | /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */ | |
5930 | sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum)); | |
14641931 | 5931 | /* See comments in __skb_checksum_complete(). */ |
573e8fca TH |
5932 | if (likely(!sum)) { |
5933 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && | |
5934 | !skb->csum_complete_sw) | |
7fe50ac8 | 5935 | netdev_rx_csum_fault(skb->dev, skb); |
573e8fca TH |
5936 | } |
5937 | ||
5938 | NAPI_GRO_CB(skb)->csum = wsum; | |
5939 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
5940 | ||
5941 | return sum; | |
5942 | } | |
5943 | EXPORT_SYMBOL(__skb_gro_checksum_complete); | |
5944 | ||
773fc8f6 | 5945 | static void net_rps_send_ipi(struct softnet_data *remsd) |
5946 | { | |
5947 | #ifdef CONFIG_RPS | |
5948 | while (remsd) { | |
5949 | struct softnet_data *next = remsd->rps_ipi_next; | |
5950 | ||
5951 | if (cpu_online(remsd->cpu)) | |
5952 | smp_call_function_single_async(remsd->cpu, &remsd->csd); | |
5953 | remsd = next; | |
5954 | } | |
5955 | #endif | |
5956 | } | |
5957 | ||
e326bed2 | 5958 | /* |
855abcf0 | 5959 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
e326bed2 ED |
5960 | * Note: called with local irq disabled, but exits with local irq enabled. |
5961 | */ | |
5962 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
5963 | { | |
5964 | #ifdef CONFIG_RPS | |
5965 | struct softnet_data *remsd = sd->rps_ipi_list; | |
5966 | ||
5967 | if (remsd) { | |
5968 | sd->rps_ipi_list = NULL; | |
5969 | ||
5970 | local_irq_enable(); | |
5971 | ||
5972 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
773fc8f6 | 5973 | net_rps_send_ipi(remsd); |
e326bed2 ED |
5974 | } else |
5975 | #endif | |
5976 | local_irq_enable(); | |
5977 | } | |
5978 | ||
d75b1ade ED |
5979 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
5980 | { | |
5981 | #ifdef CONFIG_RPS | |
5982 | return sd->rps_ipi_list != NULL; | |
5983 | #else | |
5984 | return false; | |
5985 | #endif | |
5986 | } | |
5987 | ||
bea3348e | 5988 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 | 5989 | { |
eecfd7c4 | 5990 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
145dd5f9 PA |
5991 | bool again = true; |
5992 | int work = 0; | |
1da177e4 | 5993 | |
e326bed2 ED |
5994 | /* Check if we have pending ipi, its better to send them now, |
5995 | * not waiting net_rx_action() end. | |
5996 | */ | |
d75b1ade | 5997 | if (sd_has_rps_ipi_waiting(sd)) { |
e326bed2 ED |
5998 | local_irq_disable(); |
5999 | net_rps_action_and_irq_enable(sd); | |
6000 | } | |
d75b1ade | 6001 | |
3d48b53f | 6002 | napi->weight = dev_rx_weight; |
145dd5f9 | 6003 | while (again) { |
1da177e4 | 6004 | struct sk_buff *skb; |
6e7676c1 CG |
6005 | |
6006 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
2c17d27c | 6007 | rcu_read_lock(); |
6e7676c1 | 6008 | __netif_receive_skb(skb); |
2c17d27c | 6009 | rcu_read_unlock(); |
76cc8b13 | 6010 | input_queue_head_incr(sd); |
145dd5f9 | 6011 | if (++work >= quota) |
76cc8b13 | 6012 | return work; |
145dd5f9 | 6013 | |
6e7676c1 | 6014 | } |
1da177e4 | 6015 | |
145dd5f9 | 6016 | local_irq_disable(); |
e36fa2f7 | 6017 | rps_lock(sd); |
11ef7a89 | 6018 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
eecfd7c4 ED |
6019 | /* |
6020 | * Inline a custom version of __napi_complete(). | |
6021 | * only current cpu owns and manipulates this napi, | |
11ef7a89 TH |
6022 | * and NAPI_STATE_SCHED is the only possible flag set |
6023 | * on backlog. | |
6024 | * We can use a plain write instead of clear_bit(), | |
eecfd7c4 ED |
6025 | * and we dont need an smp_mb() memory barrier. |
6026 | */ | |
eecfd7c4 | 6027 | napi->state = 0; |
145dd5f9 PA |
6028 | again = false; |
6029 | } else { | |
6030 | skb_queue_splice_tail_init(&sd->input_pkt_queue, | |
6031 | &sd->process_queue); | |
bea3348e | 6032 | } |
e36fa2f7 | 6033 | rps_unlock(sd); |
145dd5f9 | 6034 | local_irq_enable(); |
6e7676c1 | 6035 | } |
1da177e4 | 6036 | |
bea3348e SH |
6037 | return work; |
6038 | } | |
1da177e4 | 6039 | |
bea3348e SH |
6040 | /** |
6041 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 6042 | * @n: entry to schedule |
bea3348e | 6043 | * |
bc9ad166 ED |
6044 | * The entry's receive function will be scheduled to run. |
6045 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. | |
bea3348e | 6046 | */ |
b5606c2d | 6047 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
6048 | { |
6049 | unsigned long flags; | |
1da177e4 | 6050 | |
bea3348e | 6051 | local_irq_save(flags); |
903ceff7 | 6052 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
bea3348e | 6053 | local_irq_restore(flags); |
1da177e4 | 6054 | } |
bea3348e SH |
6055 | EXPORT_SYMBOL(__napi_schedule); |
6056 | ||
39e6c820 ED |
6057 | /** |
6058 | * napi_schedule_prep - check if napi can be scheduled | |
6059 | * @n: napi context | |
6060 | * | |
6061 | * Test if NAPI routine is already running, and if not mark | |
6062 | * it as running. This is used as a condition variable | |
6063 | * insure only one NAPI poll instance runs. We also make | |
6064 | * sure there is no pending NAPI disable. | |
6065 | */ | |
6066 | bool napi_schedule_prep(struct napi_struct *n) | |
6067 | { | |
6068 | unsigned long val, new; | |
6069 | ||
6070 | do { | |
6071 | val = READ_ONCE(n->state); | |
6072 | if (unlikely(val & NAPIF_STATE_DISABLE)) | |
6073 | return false; | |
6074 | new = val | NAPIF_STATE_SCHED; | |
6075 | ||
6076 | /* Sets STATE_MISSED bit if STATE_SCHED was already set | |
6077 | * This was suggested by Alexander Duyck, as compiler | |
6078 | * emits better code than : | |
6079 | * if (val & NAPIF_STATE_SCHED) | |
6080 | * new |= NAPIF_STATE_MISSED; | |
6081 | */ | |
6082 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * | |
6083 | NAPIF_STATE_MISSED; | |
6084 | } while (cmpxchg(&n->state, val, new) != val); | |
6085 | ||
6086 | return !(val & NAPIF_STATE_SCHED); | |
6087 | } | |
6088 | EXPORT_SYMBOL(napi_schedule_prep); | |
6089 | ||
bc9ad166 ED |
6090 | /** |
6091 | * __napi_schedule_irqoff - schedule for receive | |
6092 | * @n: entry to schedule | |
6093 | * | |
6094 | * Variant of __napi_schedule() assuming hard irqs are masked | |
6095 | */ | |
6096 | void __napi_schedule_irqoff(struct napi_struct *n) | |
6097 | { | |
6098 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); | |
6099 | } | |
6100 | EXPORT_SYMBOL(__napi_schedule_irqoff); | |
6101 | ||
364b6055 | 6102 | bool napi_complete_done(struct napi_struct *n, int work_done) |
d565b0a1 | 6103 | { |
39e6c820 | 6104 | unsigned long flags, val, new; |
d565b0a1 HX |
6105 | |
6106 | /* | |
217f6974 ED |
6107 | * 1) Don't let napi dequeue from the cpu poll list |
6108 | * just in case its running on a different cpu. | |
6109 | * 2) If we are busy polling, do nothing here, we have | |
6110 | * the guarantee we will be called later. | |
d565b0a1 | 6111 | */ |
217f6974 ED |
6112 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
6113 | NAPIF_STATE_IN_BUSY_POLL))) | |
364b6055 | 6114 | return false; |
d565b0a1 | 6115 | |
323ebb61 EC |
6116 | gro_normal_list(n); |
6117 | ||
d9f37d01 | 6118 | if (n->gro_bitmask) { |
3b47d303 | 6119 | unsigned long timeout = 0; |
d75b1ade | 6120 | |
3b47d303 ED |
6121 | if (work_done) |
6122 | timeout = n->dev->gro_flush_timeout; | |
6123 | ||
605108ac PA |
6124 | /* When the NAPI instance uses a timeout and keeps postponing |
6125 | * it, we need to bound somehow the time packets are kept in | |
6126 | * the GRO layer | |
6127 | */ | |
6128 | napi_gro_flush(n, !!timeout); | |
3b47d303 ED |
6129 | if (timeout) |
6130 | hrtimer_start(&n->timer, ns_to_ktime(timeout), | |
6131 | HRTIMER_MODE_REL_PINNED); | |
3b47d303 | 6132 | } |
02c1602e | 6133 | if (unlikely(!list_empty(&n->poll_list))) { |
d75b1ade ED |
6134 | /* If n->poll_list is not empty, we need to mask irqs */ |
6135 | local_irq_save(flags); | |
02c1602e | 6136 | list_del_init(&n->poll_list); |
d75b1ade ED |
6137 | local_irq_restore(flags); |
6138 | } | |
39e6c820 ED |
6139 | |
6140 | do { | |
6141 | val = READ_ONCE(n->state); | |
6142 | ||
6143 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); | |
6144 | ||
6145 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED); | |
6146 | ||
6147 | /* If STATE_MISSED was set, leave STATE_SCHED set, | |
6148 | * because we will call napi->poll() one more time. | |
6149 | * This C code was suggested by Alexander Duyck to help gcc. | |
6150 | */ | |
6151 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * | |
6152 | NAPIF_STATE_SCHED; | |
6153 | } while (cmpxchg(&n->state, val, new) != val); | |
6154 | ||
6155 | if (unlikely(val & NAPIF_STATE_MISSED)) { | |
6156 | __napi_schedule(n); | |
6157 | return false; | |
6158 | } | |
6159 | ||
364b6055 | 6160 | return true; |
d565b0a1 | 6161 | } |
3b47d303 | 6162 | EXPORT_SYMBOL(napi_complete_done); |
d565b0a1 | 6163 | |
af12fa6e | 6164 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
02d62e86 | 6165 | static struct napi_struct *napi_by_id(unsigned int napi_id) |
af12fa6e ET |
6166 | { |
6167 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); | |
6168 | struct napi_struct *napi; | |
6169 | ||
6170 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) | |
6171 | if (napi->napi_id == napi_id) | |
6172 | return napi; | |
6173 | ||
6174 | return NULL; | |
6175 | } | |
02d62e86 ED |
6176 | |
6177 | #if defined(CONFIG_NET_RX_BUSY_POLL) | |
217f6974 | 6178 | |
ce6aea93 | 6179 | #define BUSY_POLL_BUDGET 8 |
217f6974 ED |
6180 | |
6181 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock) | |
6182 | { | |
6183 | int rc; | |
6184 | ||
39e6c820 ED |
6185 | /* Busy polling means there is a high chance device driver hard irq |
6186 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was | |
6187 | * set in napi_schedule_prep(). | |
6188 | * Since we are about to call napi->poll() once more, we can safely | |
6189 | * clear NAPI_STATE_MISSED. | |
6190 | * | |
6191 | * Note: x86 could use a single "lock and ..." instruction | |
6192 | * to perform these two clear_bit() | |
6193 | */ | |
6194 | clear_bit(NAPI_STATE_MISSED, &napi->state); | |
217f6974 ED |
6195 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
6196 | ||
6197 | local_bh_disable(); | |
6198 | ||
6199 | /* All we really want here is to re-enable device interrupts. | |
6200 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. | |
6201 | */ | |
6202 | rc = napi->poll(napi, BUSY_POLL_BUDGET); | |
323ebb61 EC |
6203 | /* We can't gro_normal_list() here, because napi->poll() might have |
6204 | * rearmed the napi (napi_complete_done()) in which case it could | |
6205 | * already be running on another CPU. | |
6206 | */ | |
1e22391e | 6207 | trace_napi_poll(napi, rc, BUSY_POLL_BUDGET); |
217f6974 | 6208 | netpoll_poll_unlock(have_poll_lock); |
323ebb61 EC |
6209 | if (rc == BUSY_POLL_BUDGET) { |
6210 | /* As the whole budget was spent, we still own the napi so can | |
6211 | * safely handle the rx_list. | |
6212 | */ | |
6213 | gro_normal_list(napi); | |
217f6974 | 6214 | __napi_schedule(napi); |
323ebb61 | 6215 | } |
217f6974 | 6216 | local_bh_enable(); |
217f6974 ED |
6217 | } |
6218 | ||
7db6b048 SS |
6219 | void napi_busy_loop(unsigned int napi_id, |
6220 | bool (*loop_end)(void *, unsigned long), | |
6221 | void *loop_end_arg) | |
02d62e86 | 6222 | { |
7db6b048 | 6223 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
217f6974 | 6224 | int (*napi_poll)(struct napi_struct *napi, int budget); |
217f6974 | 6225 | void *have_poll_lock = NULL; |
02d62e86 | 6226 | struct napi_struct *napi; |
217f6974 ED |
6227 | |
6228 | restart: | |
217f6974 | 6229 | napi_poll = NULL; |
02d62e86 | 6230 | |
2a028ecb | 6231 | rcu_read_lock(); |
02d62e86 | 6232 | |
545cd5e5 | 6233 | napi = napi_by_id(napi_id); |
02d62e86 ED |
6234 | if (!napi) |
6235 | goto out; | |
6236 | ||
217f6974 ED |
6237 | preempt_disable(); |
6238 | for (;;) { | |
2b5cd0df AD |
6239 | int work = 0; |
6240 | ||
2a028ecb | 6241 | local_bh_disable(); |
217f6974 ED |
6242 | if (!napi_poll) { |
6243 | unsigned long val = READ_ONCE(napi->state); | |
6244 | ||
6245 | /* If multiple threads are competing for this napi, | |
6246 | * we avoid dirtying napi->state as much as we can. | |
6247 | */ | |
6248 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | | |
6249 | NAPIF_STATE_IN_BUSY_POLL)) | |
6250 | goto count; | |
6251 | if (cmpxchg(&napi->state, val, | |
6252 | val | NAPIF_STATE_IN_BUSY_POLL | | |
6253 | NAPIF_STATE_SCHED) != val) | |
6254 | goto count; | |
6255 | have_poll_lock = netpoll_poll_lock(napi); | |
6256 | napi_poll = napi->poll; | |
6257 | } | |
2b5cd0df AD |
6258 | work = napi_poll(napi, BUSY_POLL_BUDGET); |
6259 | trace_napi_poll(napi, work, BUSY_POLL_BUDGET); | |
323ebb61 | 6260 | gro_normal_list(napi); |
217f6974 | 6261 | count: |
2b5cd0df | 6262 | if (work > 0) |
7db6b048 | 6263 | __NET_ADD_STATS(dev_net(napi->dev), |
2b5cd0df | 6264 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
2a028ecb | 6265 | local_bh_enable(); |
02d62e86 | 6266 | |
7db6b048 | 6267 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
217f6974 | 6268 | break; |
02d62e86 | 6269 | |
217f6974 ED |
6270 | if (unlikely(need_resched())) { |
6271 | if (napi_poll) | |
6272 | busy_poll_stop(napi, have_poll_lock); | |
6273 | preempt_enable(); | |
6274 | rcu_read_unlock(); | |
6275 | cond_resched(); | |
7db6b048 | 6276 | if (loop_end(loop_end_arg, start_time)) |
2b5cd0df | 6277 | return; |
217f6974 ED |
6278 | goto restart; |
6279 | } | |
6cdf89b1 | 6280 | cpu_relax(); |
217f6974 ED |
6281 | } |
6282 | if (napi_poll) | |
6283 | busy_poll_stop(napi, have_poll_lock); | |
6284 | preempt_enable(); | |
02d62e86 | 6285 | out: |
2a028ecb | 6286 | rcu_read_unlock(); |
02d62e86 | 6287 | } |
7db6b048 | 6288 | EXPORT_SYMBOL(napi_busy_loop); |
02d62e86 ED |
6289 | |
6290 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
af12fa6e | 6291 | |
149d6ad8 | 6292 | static void napi_hash_add(struct napi_struct *napi) |
af12fa6e | 6293 | { |
d64b5e85 ED |
6294 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) || |
6295 | test_and_set_bit(NAPI_STATE_HASHED, &napi->state)) | |
52bd2d62 | 6296 | return; |
af12fa6e | 6297 | |
52bd2d62 | 6298 | spin_lock(&napi_hash_lock); |
af12fa6e | 6299 | |
545cd5e5 | 6300 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
52bd2d62 | 6301 | do { |
545cd5e5 AD |
6302 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
6303 | napi_gen_id = MIN_NAPI_ID; | |
52bd2d62 ED |
6304 | } while (napi_by_id(napi_gen_id)); |
6305 | napi->napi_id = napi_gen_id; | |
af12fa6e | 6306 | |
52bd2d62 ED |
6307 | hlist_add_head_rcu(&napi->napi_hash_node, |
6308 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); | |
af12fa6e | 6309 | |
52bd2d62 | 6310 | spin_unlock(&napi_hash_lock); |
af12fa6e | 6311 | } |
af12fa6e ET |
6312 | |
6313 | /* Warning : caller is responsible to make sure rcu grace period | |
6314 | * is respected before freeing memory containing @napi | |
6315 | */ | |
34cbe27e | 6316 | bool napi_hash_del(struct napi_struct *napi) |
af12fa6e | 6317 | { |
34cbe27e ED |
6318 | bool rcu_sync_needed = false; |
6319 | ||
af12fa6e ET |
6320 | spin_lock(&napi_hash_lock); |
6321 | ||
34cbe27e ED |
6322 | if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) { |
6323 | rcu_sync_needed = true; | |
af12fa6e | 6324 | hlist_del_rcu(&napi->napi_hash_node); |
34cbe27e | 6325 | } |
af12fa6e | 6326 | spin_unlock(&napi_hash_lock); |
34cbe27e | 6327 | return rcu_sync_needed; |
af12fa6e ET |
6328 | } |
6329 | EXPORT_SYMBOL_GPL(napi_hash_del); | |
6330 | ||
3b47d303 ED |
6331 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
6332 | { | |
6333 | struct napi_struct *napi; | |
6334 | ||
6335 | napi = container_of(timer, struct napi_struct, timer); | |
39e6c820 ED |
6336 | |
6337 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting | |
6338 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. | |
6339 | */ | |
d9f37d01 | 6340 | if (napi->gro_bitmask && !napi_disable_pending(napi) && |
39e6c820 ED |
6341 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) |
6342 | __napi_schedule_irqoff(napi); | |
3b47d303 ED |
6343 | |
6344 | return HRTIMER_NORESTART; | |
6345 | } | |
6346 | ||
7c4ec749 | 6347 | static void init_gro_hash(struct napi_struct *napi) |
d565b0a1 | 6348 | { |
07d78363 DM |
6349 | int i; |
6350 | ||
6312fe77 LR |
6351 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6352 | INIT_LIST_HEAD(&napi->gro_hash[i].list); | |
6353 | napi->gro_hash[i].count = 0; | |
6354 | } | |
7c4ec749 DM |
6355 | napi->gro_bitmask = 0; |
6356 | } | |
6357 | ||
6358 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, | |
6359 | int (*poll)(struct napi_struct *, int), int weight) | |
6360 | { | |
6361 | INIT_LIST_HEAD(&napi->poll_list); | |
6362 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); | |
6363 | napi->timer.function = napi_watchdog; | |
6364 | init_gro_hash(napi); | |
5d38a079 | 6365 | napi->skb = NULL; |
323ebb61 EC |
6366 | INIT_LIST_HEAD(&napi->rx_list); |
6367 | napi->rx_count = 0; | |
d565b0a1 | 6368 | napi->poll = poll; |
82dc3c63 | 6369 | if (weight > NAPI_POLL_WEIGHT) |
bf29e9e9 QC |
6370 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
6371 | weight); | |
d565b0a1 HX |
6372 | napi->weight = weight; |
6373 | list_add(&napi->dev_list, &dev->napi_list); | |
d565b0a1 | 6374 | napi->dev = dev; |
5d38a079 | 6375 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
6376 | napi->poll_owner = -1; |
6377 | #endif | |
6378 | set_bit(NAPI_STATE_SCHED, &napi->state); | |
93d05d4a | 6379 | napi_hash_add(napi); |
d565b0a1 HX |
6380 | } |
6381 | EXPORT_SYMBOL(netif_napi_add); | |
6382 | ||
3b47d303 ED |
6383 | void napi_disable(struct napi_struct *n) |
6384 | { | |
6385 | might_sleep(); | |
6386 | set_bit(NAPI_STATE_DISABLE, &n->state); | |
6387 | ||
6388 | while (test_and_set_bit(NAPI_STATE_SCHED, &n->state)) | |
6389 | msleep(1); | |
2d8bff12 NH |
6390 | while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state)) |
6391 | msleep(1); | |
3b47d303 ED |
6392 | |
6393 | hrtimer_cancel(&n->timer); | |
6394 | ||
6395 | clear_bit(NAPI_STATE_DISABLE, &n->state); | |
6396 | } | |
6397 | EXPORT_SYMBOL(napi_disable); | |
6398 | ||
07d78363 | 6399 | static void flush_gro_hash(struct napi_struct *napi) |
d4546c25 | 6400 | { |
07d78363 | 6401 | int i; |
d4546c25 | 6402 | |
07d78363 DM |
6403 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6404 | struct sk_buff *skb, *n; | |
6405 | ||
6312fe77 | 6406 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
07d78363 | 6407 | kfree_skb(skb); |
6312fe77 | 6408 | napi->gro_hash[i].count = 0; |
07d78363 | 6409 | } |
d4546c25 DM |
6410 | } |
6411 | ||
93d05d4a | 6412 | /* Must be called in process context */ |
d565b0a1 HX |
6413 | void netif_napi_del(struct napi_struct *napi) |
6414 | { | |
93d05d4a ED |
6415 | might_sleep(); |
6416 | if (napi_hash_del(napi)) | |
6417 | synchronize_net(); | |
d7b06636 | 6418 | list_del_init(&napi->dev_list); |
76620aaf | 6419 | napi_free_frags(napi); |
d565b0a1 | 6420 | |
07d78363 | 6421 | flush_gro_hash(napi); |
d9f37d01 | 6422 | napi->gro_bitmask = 0; |
d565b0a1 HX |
6423 | } |
6424 | EXPORT_SYMBOL(netif_napi_del); | |
6425 | ||
726ce70e HX |
6426 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) |
6427 | { | |
6428 | void *have; | |
6429 | int work, weight; | |
6430 | ||
6431 | list_del_init(&n->poll_list); | |
6432 | ||
6433 | have = netpoll_poll_lock(n); | |
6434 | ||
6435 | weight = n->weight; | |
6436 | ||
6437 | /* This NAPI_STATE_SCHED test is for avoiding a race | |
6438 | * with netpoll's poll_napi(). Only the entity which | |
6439 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
6440 | * actually make the ->poll() call. Therefore we avoid | |
6441 | * accidentally calling ->poll() when NAPI is not scheduled. | |
6442 | */ | |
6443 | work = 0; | |
6444 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { | |
6445 | work = n->poll(n, weight); | |
1db19db7 | 6446 | trace_napi_poll(n, work, weight); |
726ce70e HX |
6447 | } |
6448 | ||
6449 | WARN_ON_ONCE(work > weight); | |
6450 | ||
6451 | if (likely(work < weight)) | |
6452 | goto out_unlock; | |
6453 | ||
6454 | /* Drivers must not modify the NAPI state if they | |
6455 | * consume the entire weight. In such cases this code | |
6456 | * still "owns" the NAPI instance and therefore can | |
6457 | * move the instance around on the list at-will. | |
6458 | */ | |
6459 | if (unlikely(napi_disable_pending(n))) { | |
6460 | napi_complete(n); | |
6461 | goto out_unlock; | |
6462 | } | |
6463 | ||
323ebb61 EC |
6464 | gro_normal_list(n); |
6465 | ||
d9f37d01 | 6466 | if (n->gro_bitmask) { |
726ce70e HX |
6467 | /* flush too old packets |
6468 | * If HZ < 1000, flush all packets. | |
6469 | */ | |
6470 | napi_gro_flush(n, HZ >= 1000); | |
6471 | } | |
6472 | ||
001ce546 HX |
6473 | /* Some drivers may have called napi_schedule |
6474 | * prior to exhausting their budget. | |
6475 | */ | |
6476 | if (unlikely(!list_empty(&n->poll_list))) { | |
6477 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", | |
6478 | n->dev ? n->dev->name : "backlog"); | |
6479 | goto out_unlock; | |
6480 | } | |
6481 | ||
726ce70e HX |
6482 | list_add_tail(&n->poll_list, repoll); |
6483 | ||
6484 | out_unlock: | |
6485 | netpoll_poll_unlock(have); | |
6486 | ||
6487 | return work; | |
6488 | } | |
6489 | ||
0766f788 | 6490 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
1da177e4 | 6491 | { |
903ceff7 | 6492 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
7acf8a1e MW |
6493 | unsigned long time_limit = jiffies + |
6494 | usecs_to_jiffies(netdev_budget_usecs); | |
51b0bded | 6495 | int budget = netdev_budget; |
d75b1ade ED |
6496 | LIST_HEAD(list); |
6497 | LIST_HEAD(repoll); | |
53fb95d3 | 6498 | |
1da177e4 | 6499 | local_irq_disable(); |
d75b1ade ED |
6500 | list_splice_init(&sd->poll_list, &list); |
6501 | local_irq_enable(); | |
1da177e4 | 6502 | |
ceb8d5bf | 6503 | for (;;) { |
bea3348e | 6504 | struct napi_struct *n; |
1da177e4 | 6505 | |
ceb8d5bf HX |
6506 | if (list_empty(&list)) { |
6507 | if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll)) | |
f52dffe0 | 6508 | goto out; |
ceb8d5bf HX |
6509 | break; |
6510 | } | |
6511 | ||
6bd373eb HX |
6512 | n = list_first_entry(&list, struct napi_struct, poll_list); |
6513 | budget -= napi_poll(n, &repoll); | |
6514 | ||
d75b1ade | 6515 | /* If softirq window is exhausted then punt. |
24f8b238 SH |
6516 | * Allow this to run for 2 jiffies since which will allow |
6517 | * an average latency of 1.5/HZ. | |
bea3348e | 6518 | */ |
ceb8d5bf HX |
6519 | if (unlikely(budget <= 0 || |
6520 | time_after_eq(jiffies, time_limit))) { | |
6521 | sd->time_squeeze++; | |
6522 | break; | |
6523 | } | |
1da177e4 | 6524 | } |
d75b1ade | 6525 | |
d75b1ade ED |
6526 | local_irq_disable(); |
6527 | ||
6528 | list_splice_tail_init(&sd->poll_list, &list); | |
6529 | list_splice_tail(&repoll, &list); | |
6530 | list_splice(&list, &sd->poll_list); | |
6531 | if (!list_empty(&sd->poll_list)) | |
6532 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
6533 | ||
e326bed2 | 6534 | net_rps_action_and_irq_enable(sd); |
f52dffe0 ED |
6535 | out: |
6536 | __kfree_skb_flush(); | |
1da177e4 LT |
6537 | } |
6538 | ||
aa9d8560 | 6539 | struct netdev_adjacent { |
9ff162a8 | 6540 | struct net_device *dev; |
5d261913 VF |
6541 | |
6542 | /* upper master flag, there can only be one master device per list */ | |
9ff162a8 | 6543 | bool master; |
5d261913 | 6544 | |
5d261913 VF |
6545 | /* counter for the number of times this device was added to us */ |
6546 | u16 ref_nr; | |
6547 | ||
402dae96 VF |
6548 | /* private field for the users */ |
6549 | void *private; | |
6550 | ||
9ff162a8 JP |
6551 | struct list_head list; |
6552 | struct rcu_head rcu; | |
9ff162a8 JP |
6553 | }; |
6554 | ||
6ea29da1 | 6555 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
2f268f12 | 6556 | struct list_head *adj_list) |
9ff162a8 | 6557 | { |
5d261913 | 6558 | struct netdev_adjacent *adj; |
5d261913 | 6559 | |
2f268f12 | 6560 | list_for_each_entry(adj, adj_list, list) { |
5d261913 VF |
6561 | if (adj->dev == adj_dev) |
6562 | return adj; | |
9ff162a8 JP |
6563 | } |
6564 | return NULL; | |
6565 | } | |
6566 | ||
f1170fd4 DA |
6567 | static int __netdev_has_upper_dev(struct net_device *upper_dev, void *data) |
6568 | { | |
6569 | struct net_device *dev = data; | |
6570 | ||
6571 | return upper_dev == dev; | |
6572 | } | |
6573 | ||
9ff162a8 JP |
6574 | /** |
6575 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
6576 | * @dev: device | |
6577 | * @upper_dev: upper device to check | |
6578 | * | |
6579 | * Find out if a device is linked to specified upper device and return true | |
6580 | * in case it is. Note that this checks only immediate upper device, | |
6581 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
6582 | */ | |
6583 | bool netdev_has_upper_dev(struct net_device *dev, | |
6584 | struct net_device *upper_dev) | |
6585 | { | |
6586 | ASSERT_RTNL(); | |
6587 | ||
f1170fd4 DA |
6588 | return netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev, |
6589 | upper_dev); | |
9ff162a8 JP |
6590 | } |
6591 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
6592 | ||
1a3f060c DA |
6593 | /** |
6594 | * netdev_has_upper_dev_all - Check if device is linked to an upper device | |
6595 | * @dev: device | |
6596 | * @upper_dev: upper device to check | |
6597 | * | |
6598 | * Find out if a device is linked to specified upper device and return true | |
6599 | * in case it is. Note that this checks the entire upper device chain. | |
6600 | * The caller must hold rcu lock. | |
6601 | */ | |
6602 | ||
1a3f060c DA |
6603 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
6604 | struct net_device *upper_dev) | |
6605 | { | |
6606 | return !!netdev_walk_all_upper_dev_rcu(dev, __netdev_has_upper_dev, | |
6607 | upper_dev); | |
6608 | } | |
6609 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); | |
6610 | ||
9ff162a8 JP |
6611 | /** |
6612 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
6613 | * @dev: device | |
6614 | * | |
6615 | * Find out if a device is linked to an upper device and return true in case | |
6616 | * it is. The caller must hold the RTNL lock. | |
6617 | */ | |
25cc72a3 | 6618 | bool netdev_has_any_upper_dev(struct net_device *dev) |
9ff162a8 JP |
6619 | { |
6620 | ASSERT_RTNL(); | |
6621 | ||
f1170fd4 | 6622 | return !list_empty(&dev->adj_list.upper); |
9ff162a8 | 6623 | } |
25cc72a3 | 6624 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
9ff162a8 JP |
6625 | |
6626 | /** | |
6627 | * netdev_master_upper_dev_get - Get master upper device | |
6628 | * @dev: device | |
6629 | * | |
6630 | * Find a master upper device and return pointer to it or NULL in case | |
6631 | * it's not there. The caller must hold the RTNL lock. | |
6632 | */ | |
6633 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
6634 | { | |
aa9d8560 | 6635 | struct netdev_adjacent *upper; |
9ff162a8 JP |
6636 | |
6637 | ASSERT_RTNL(); | |
6638 | ||
2f268f12 | 6639 | if (list_empty(&dev->adj_list.upper)) |
9ff162a8 JP |
6640 | return NULL; |
6641 | ||
2f268f12 | 6642 | upper = list_first_entry(&dev->adj_list.upper, |
aa9d8560 | 6643 | struct netdev_adjacent, list); |
9ff162a8 JP |
6644 | if (likely(upper->master)) |
6645 | return upper->dev; | |
6646 | return NULL; | |
6647 | } | |
6648 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
6649 | ||
0f524a80 DA |
6650 | /** |
6651 | * netdev_has_any_lower_dev - Check if device is linked to some device | |
6652 | * @dev: device | |
6653 | * | |
6654 | * Find out if a device is linked to a lower device and return true in case | |
6655 | * it is. The caller must hold the RTNL lock. | |
6656 | */ | |
6657 | static bool netdev_has_any_lower_dev(struct net_device *dev) | |
6658 | { | |
6659 | ASSERT_RTNL(); | |
6660 | ||
6661 | return !list_empty(&dev->adj_list.lower); | |
6662 | } | |
6663 | ||
b6ccba4c VF |
6664 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
6665 | { | |
6666 | struct netdev_adjacent *adj; | |
6667 | ||
6668 | adj = list_entry(adj_list, struct netdev_adjacent, list); | |
6669 | ||
6670 | return adj->private; | |
6671 | } | |
6672 | EXPORT_SYMBOL(netdev_adjacent_get_private); | |
6673 | ||
44a40855 VY |
6674 | /** |
6675 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list | |
6676 | * @dev: device | |
6677 | * @iter: list_head ** of the current position | |
6678 | * | |
6679 | * Gets the next device from the dev's upper list, starting from iter | |
6680 | * position. The caller must hold RCU read lock. | |
6681 | */ | |
6682 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, | |
6683 | struct list_head **iter) | |
6684 | { | |
6685 | struct netdev_adjacent *upper; | |
6686 | ||
6687 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6688 | ||
6689 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6690 | ||
6691 | if (&upper->list == &dev->adj_list.upper) | |
6692 | return NULL; | |
6693 | ||
6694 | *iter = &upper->list; | |
6695 | ||
6696 | return upper->dev; | |
6697 | } | |
6698 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); | |
6699 | ||
1a3f060c DA |
6700 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
6701 | struct list_head **iter) | |
6702 | { | |
6703 | struct netdev_adjacent *upper; | |
6704 | ||
6705 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6706 | ||
6707 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6708 | ||
6709 | if (&upper->list == &dev->adj_list.upper) | |
6710 | return NULL; | |
6711 | ||
6712 | *iter = &upper->list; | |
6713 | ||
6714 | return upper->dev; | |
6715 | } | |
6716 | ||
6717 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, | |
6718 | int (*fn)(struct net_device *dev, | |
6719 | void *data), | |
6720 | void *data) | |
6721 | { | |
6722 | struct net_device *udev; | |
6723 | struct list_head *iter; | |
6724 | int ret; | |
6725 | ||
6726 | for (iter = &dev->adj_list.upper, | |
6727 | udev = netdev_next_upper_dev_rcu(dev, &iter); | |
6728 | udev; | |
6729 | udev = netdev_next_upper_dev_rcu(dev, &iter)) { | |
6730 | /* first is the upper device itself */ | |
6731 | ret = fn(udev, data); | |
6732 | if (ret) | |
6733 | return ret; | |
6734 | ||
6735 | /* then look at all of its upper devices */ | |
6736 | ret = netdev_walk_all_upper_dev_rcu(udev, fn, data); | |
6737 | if (ret) | |
6738 | return ret; | |
6739 | } | |
6740 | ||
6741 | return 0; | |
6742 | } | |
6743 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); | |
6744 | ||
31088a11 VF |
6745 | /** |
6746 | * netdev_lower_get_next_private - Get the next ->private from the | |
6747 | * lower neighbour list | |
6748 | * @dev: device | |
6749 | * @iter: list_head ** of the current position | |
6750 | * | |
6751 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
6752 | * list, starting from iter position. The caller must hold either hold the | |
6753 | * RTNL lock or its own locking that guarantees that the neighbour lower | |
b469139e | 6754 | * list will remain unchanged. |
31088a11 VF |
6755 | */ |
6756 | void *netdev_lower_get_next_private(struct net_device *dev, | |
6757 | struct list_head **iter) | |
6758 | { | |
6759 | struct netdev_adjacent *lower; | |
6760 | ||
6761 | lower = list_entry(*iter, struct netdev_adjacent, list); | |
6762 | ||
6763 | if (&lower->list == &dev->adj_list.lower) | |
6764 | return NULL; | |
6765 | ||
6859e7df | 6766 | *iter = lower->list.next; |
31088a11 VF |
6767 | |
6768 | return lower->private; | |
6769 | } | |
6770 | EXPORT_SYMBOL(netdev_lower_get_next_private); | |
6771 | ||
6772 | /** | |
6773 | * netdev_lower_get_next_private_rcu - Get the next ->private from the | |
6774 | * lower neighbour list, RCU | |
6775 | * variant | |
6776 | * @dev: device | |
6777 | * @iter: list_head ** of the current position | |
6778 | * | |
6779 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
6780 | * list, starting from iter position. The caller must hold RCU read lock. | |
6781 | */ | |
6782 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, | |
6783 | struct list_head **iter) | |
6784 | { | |
6785 | struct netdev_adjacent *lower; | |
6786 | ||
6787 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
6788 | ||
6789 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6790 | ||
6791 | if (&lower->list == &dev->adj_list.lower) | |
6792 | return NULL; | |
6793 | ||
6859e7df | 6794 | *iter = &lower->list; |
31088a11 VF |
6795 | |
6796 | return lower->private; | |
6797 | } | |
6798 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); | |
6799 | ||
4085ebe8 VY |
6800 | /** |
6801 | * netdev_lower_get_next - Get the next device from the lower neighbour | |
6802 | * list | |
6803 | * @dev: device | |
6804 | * @iter: list_head ** of the current position | |
6805 | * | |
6806 | * Gets the next netdev_adjacent from the dev's lower neighbour | |
6807 | * list, starting from iter position. The caller must hold RTNL lock or | |
6808 | * its own locking that guarantees that the neighbour lower | |
b469139e | 6809 | * list will remain unchanged. |
4085ebe8 VY |
6810 | */ |
6811 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) | |
6812 | { | |
6813 | struct netdev_adjacent *lower; | |
6814 | ||
cfdd28be | 6815 | lower = list_entry(*iter, struct netdev_adjacent, list); |
4085ebe8 VY |
6816 | |
6817 | if (&lower->list == &dev->adj_list.lower) | |
6818 | return NULL; | |
6819 | ||
cfdd28be | 6820 | *iter = lower->list.next; |
4085ebe8 VY |
6821 | |
6822 | return lower->dev; | |
6823 | } | |
6824 | EXPORT_SYMBOL(netdev_lower_get_next); | |
6825 | ||
1a3f060c DA |
6826 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
6827 | struct list_head **iter) | |
6828 | { | |
6829 | struct netdev_adjacent *lower; | |
6830 | ||
46b5ab1a | 6831 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
1a3f060c DA |
6832 | |
6833 | if (&lower->list == &dev->adj_list.lower) | |
6834 | return NULL; | |
6835 | ||
46b5ab1a | 6836 | *iter = &lower->list; |
1a3f060c DA |
6837 | |
6838 | return lower->dev; | |
6839 | } | |
6840 | ||
6841 | int netdev_walk_all_lower_dev(struct net_device *dev, | |
6842 | int (*fn)(struct net_device *dev, | |
6843 | void *data), | |
6844 | void *data) | |
6845 | { | |
6846 | struct net_device *ldev; | |
6847 | struct list_head *iter; | |
6848 | int ret; | |
6849 | ||
6850 | for (iter = &dev->adj_list.lower, | |
6851 | ldev = netdev_next_lower_dev(dev, &iter); | |
6852 | ldev; | |
6853 | ldev = netdev_next_lower_dev(dev, &iter)) { | |
6854 | /* first is the lower device itself */ | |
6855 | ret = fn(ldev, data); | |
6856 | if (ret) | |
6857 | return ret; | |
6858 | ||
6859 | /* then look at all of its lower devices */ | |
6860 | ret = netdev_walk_all_lower_dev(ldev, fn, data); | |
6861 | if (ret) | |
6862 | return ret; | |
6863 | } | |
6864 | ||
6865 | return 0; | |
6866 | } | |
6867 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); | |
6868 | ||
1a3f060c DA |
6869 | static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
6870 | struct list_head **iter) | |
6871 | { | |
6872 | struct netdev_adjacent *lower; | |
6873 | ||
6874 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6875 | if (&lower->list == &dev->adj_list.lower) | |
6876 | return NULL; | |
6877 | ||
6878 | *iter = &lower->list; | |
6879 | ||
6880 | return lower->dev; | |
6881 | } | |
6882 | ||
6883 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, | |
6884 | int (*fn)(struct net_device *dev, | |
6885 | void *data), | |
6886 | void *data) | |
6887 | { | |
6888 | struct net_device *ldev; | |
6889 | struct list_head *iter; | |
6890 | int ret; | |
6891 | ||
6892 | for (iter = &dev->adj_list.lower, | |
6893 | ldev = netdev_next_lower_dev_rcu(dev, &iter); | |
6894 | ldev; | |
6895 | ldev = netdev_next_lower_dev_rcu(dev, &iter)) { | |
6896 | /* first is the lower device itself */ | |
6897 | ret = fn(ldev, data); | |
6898 | if (ret) | |
6899 | return ret; | |
6900 | ||
6901 | /* then look at all of its lower devices */ | |
6902 | ret = netdev_walk_all_lower_dev_rcu(ldev, fn, data); | |
6903 | if (ret) | |
6904 | return ret; | |
6905 | } | |
6906 | ||
6907 | return 0; | |
6908 | } | |
6909 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); | |
6910 | ||
e001bfad | 6911 | /** |
6912 | * netdev_lower_get_first_private_rcu - Get the first ->private from the | |
6913 | * lower neighbour list, RCU | |
6914 | * variant | |
6915 | * @dev: device | |
6916 | * | |
6917 | * Gets the first netdev_adjacent->private from the dev's lower neighbour | |
6918 | * list. The caller must hold RCU read lock. | |
6919 | */ | |
6920 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) | |
6921 | { | |
6922 | struct netdev_adjacent *lower; | |
6923 | ||
6924 | lower = list_first_or_null_rcu(&dev->adj_list.lower, | |
6925 | struct netdev_adjacent, list); | |
6926 | if (lower) | |
6927 | return lower->private; | |
6928 | return NULL; | |
6929 | } | |
6930 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); | |
6931 | ||
9ff162a8 JP |
6932 | /** |
6933 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
6934 | * @dev: device | |
6935 | * | |
6936 | * Find a master upper device and return pointer to it or NULL in case | |
6937 | * it's not there. The caller must hold the RCU read lock. | |
6938 | */ | |
6939 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
6940 | { | |
aa9d8560 | 6941 | struct netdev_adjacent *upper; |
9ff162a8 | 6942 | |
2f268f12 | 6943 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
aa9d8560 | 6944 | struct netdev_adjacent, list); |
9ff162a8 JP |
6945 | if (upper && likely(upper->master)) |
6946 | return upper->dev; | |
6947 | return NULL; | |
6948 | } | |
6949 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
6950 | ||
0a59f3a9 | 6951 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
3ee32707 VF |
6952 | struct net_device *adj_dev, |
6953 | struct list_head *dev_list) | |
6954 | { | |
6955 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 6956 | |
3ee32707 VF |
6957 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
6958 | "upper_%s" : "lower_%s", adj_dev->name); | |
6959 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), | |
6960 | linkname); | |
6961 | } | |
0a59f3a9 | 6962 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
3ee32707 VF |
6963 | char *name, |
6964 | struct list_head *dev_list) | |
6965 | { | |
6966 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 6967 | |
3ee32707 VF |
6968 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
6969 | "upper_%s" : "lower_%s", name); | |
6970 | sysfs_remove_link(&(dev->dev.kobj), linkname); | |
6971 | } | |
6972 | ||
7ce64c79 AF |
6973 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
6974 | struct net_device *adj_dev, | |
6975 | struct list_head *dev_list) | |
6976 | { | |
6977 | return (dev_list == &dev->adj_list.upper || | |
6978 | dev_list == &dev->adj_list.lower) && | |
6979 | net_eq(dev_net(dev), dev_net(adj_dev)); | |
6980 | } | |
3ee32707 | 6981 | |
5d261913 VF |
6982 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
6983 | struct net_device *adj_dev, | |
7863c054 | 6984 | struct list_head *dev_list, |
402dae96 | 6985 | void *private, bool master) |
5d261913 VF |
6986 | { |
6987 | struct netdev_adjacent *adj; | |
842d67a7 | 6988 | int ret; |
5d261913 | 6989 | |
6ea29da1 | 6990 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 VF |
6991 | |
6992 | if (adj) { | |
790510d9 | 6993 | adj->ref_nr += 1; |
67b62f98 DA |
6994 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
6995 | dev->name, adj_dev->name, adj->ref_nr); | |
6996 | ||
5d261913 VF |
6997 | return 0; |
6998 | } | |
6999 | ||
7000 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); | |
7001 | if (!adj) | |
7002 | return -ENOMEM; | |
7003 | ||
7004 | adj->dev = adj_dev; | |
7005 | adj->master = master; | |
790510d9 | 7006 | adj->ref_nr = 1; |
402dae96 | 7007 | adj->private = private; |
5d261913 | 7008 | dev_hold(adj_dev); |
2f268f12 | 7009 | |
67b62f98 DA |
7010 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
7011 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); | |
5d261913 | 7012 | |
7ce64c79 | 7013 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
3ee32707 | 7014 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
5831d66e VF |
7015 | if (ret) |
7016 | goto free_adj; | |
7017 | } | |
7018 | ||
7863c054 | 7019 | /* Ensure that master link is always the first item in list. */ |
842d67a7 VF |
7020 | if (master) { |
7021 | ret = sysfs_create_link(&(dev->dev.kobj), | |
7022 | &(adj_dev->dev.kobj), "master"); | |
7023 | if (ret) | |
5831d66e | 7024 | goto remove_symlinks; |
842d67a7 | 7025 | |
7863c054 | 7026 | list_add_rcu(&adj->list, dev_list); |
842d67a7 | 7027 | } else { |
7863c054 | 7028 | list_add_tail_rcu(&adj->list, dev_list); |
842d67a7 | 7029 | } |
5d261913 VF |
7030 | |
7031 | return 0; | |
842d67a7 | 7032 | |
5831d66e | 7033 | remove_symlinks: |
7ce64c79 | 7034 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7035 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
842d67a7 VF |
7036 | free_adj: |
7037 | kfree(adj); | |
974daef7 | 7038 | dev_put(adj_dev); |
842d67a7 VF |
7039 | |
7040 | return ret; | |
5d261913 VF |
7041 | } |
7042 | ||
1d143d9f | 7043 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
7044 | struct net_device *adj_dev, | |
93409033 | 7045 | u16 ref_nr, |
1d143d9f | 7046 | struct list_head *dev_list) |
5d261913 VF |
7047 | { |
7048 | struct netdev_adjacent *adj; | |
7049 | ||
67b62f98 DA |
7050 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
7051 | dev->name, adj_dev->name, ref_nr); | |
7052 | ||
6ea29da1 | 7053 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 | 7054 | |
2f268f12 | 7055 | if (!adj) { |
67b62f98 | 7056 | pr_err("Adjacency does not exist for device %s from %s\n", |
2f268f12 | 7057 | dev->name, adj_dev->name); |
67b62f98 DA |
7058 | WARN_ON(1); |
7059 | return; | |
2f268f12 | 7060 | } |
5d261913 | 7061 | |
93409033 | 7062 | if (adj->ref_nr > ref_nr) { |
67b62f98 DA |
7063 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
7064 | dev->name, adj_dev->name, ref_nr, | |
7065 | adj->ref_nr - ref_nr); | |
93409033 | 7066 | adj->ref_nr -= ref_nr; |
5d261913 VF |
7067 | return; |
7068 | } | |
7069 | ||
842d67a7 VF |
7070 | if (adj->master) |
7071 | sysfs_remove_link(&(dev->dev.kobj), "master"); | |
7072 | ||
7ce64c79 | 7073 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7074 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
5831d66e | 7075 | |
5d261913 | 7076 | list_del_rcu(&adj->list); |
67b62f98 | 7077 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
2f268f12 | 7078 | adj_dev->name, dev->name, adj_dev->name); |
5d261913 VF |
7079 | dev_put(adj_dev); |
7080 | kfree_rcu(adj, rcu); | |
7081 | } | |
7082 | ||
1d143d9f | 7083 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
7084 | struct net_device *upper_dev, | |
7085 | struct list_head *up_list, | |
7086 | struct list_head *down_list, | |
7087 | void *private, bool master) | |
5d261913 VF |
7088 | { |
7089 | int ret; | |
7090 | ||
790510d9 | 7091 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
93409033 | 7092 | private, master); |
5d261913 VF |
7093 | if (ret) |
7094 | return ret; | |
7095 | ||
790510d9 | 7096 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
93409033 | 7097 | private, false); |
5d261913 | 7098 | if (ret) { |
790510d9 | 7099 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
5d261913 VF |
7100 | return ret; |
7101 | } | |
7102 | ||
7103 | return 0; | |
7104 | } | |
7105 | ||
1d143d9f | 7106 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
7107 | struct net_device *upper_dev, | |
93409033 | 7108 | u16 ref_nr, |
1d143d9f | 7109 | struct list_head *up_list, |
7110 | struct list_head *down_list) | |
5d261913 | 7111 | { |
93409033 AC |
7112 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
7113 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); | |
5d261913 VF |
7114 | } |
7115 | ||
1d143d9f | 7116 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
7117 | struct net_device *upper_dev, | |
7118 | void *private, bool master) | |
2f268f12 | 7119 | { |
f1170fd4 DA |
7120 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
7121 | &dev->adj_list.upper, | |
7122 | &upper_dev->adj_list.lower, | |
7123 | private, master); | |
5d261913 VF |
7124 | } |
7125 | ||
1d143d9f | 7126 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
7127 | struct net_device *upper_dev) | |
2f268f12 | 7128 | { |
93409033 | 7129 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
2f268f12 VF |
7130 | &dev->adj_list.upper, |
7131 | &upper_dev->adj_list.lower); | |
7132 | } | |
5d261913 | 7133 | |
9ff162a8 | 7134 | static int __netdev_upper_dev_link(struct net_device *dev, |
402dae96 | 7135 | struct net_device *upper_dev, bool master, |
42ab19ee DA |
7136 | void *upper_priv, void *upper_info, |
7137 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7138 | { |
51d0c047 DA |
7139 | struct netdev_notifier_changeupper_info changeupper_info = { |
7140 | .info = { | |
7141 | .dev = dev, | |
42ab19ee | 7142 | .extack = extack, |
51d0c047 DA |
7143 | }, |
7144 | .upper_dev = upper_dev, | |
7145 | .master = master, | |
7146 | .linking = true, | |
7147 | .upper_info = upper_info, | |
7148 | }; | |
50d629e7 | 7149 | struct net_device *master_dev; |
5d261913 | 7150 | int ret = 0; |
9ff162a8 JP |
7151 | |
7152 | ASSERT_RTNL(); | |
7153 | ||
7154 | if (dev == upper_dev) | |
7155 | return -EBUSY; | |
7156 | ||
7157 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
f1170fd4 | 7158 | if (netdev_has_upper_dev(upper_dev, dev)) |
9ff162a8 JP |
7159 | return -EBUSY; |
7160 | ||
50d629e7 MM |
7161 | if (!master) { |
7162 | if (netdev_has_upper_dev(dev, upper_dev)) | |
7163 | return -EEXIST; | |
7164 | } else { | |
7165 | master_dev = netdev_master_upper_dev_get(dev); | |
7166 | if (master_dev) | |
7167 | return master_dev == upper_dev ? -EEXIST : -EBUSY; | |
7168 | } | |
9ff162a8 | 7169 | |
51d0c047 | 7170 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7171 | &changeupper_info.info); |
7172 | ret = notifier_to_errno(ret); | |
7173 | if (ret) | |
7174 | return ret; | |
7175 | ||
6dffb044 | 7176 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
402dae96 | 7177 | master); |
5d261913 VF |
7178 | if (ret) |
7179 | return ret; | |
9ff162a8 | 7180 | |
51d0c047 | 7181 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
b03804e7 IS |
7182 | &changeupper_info.info); |
7183 | ret = notifier_to_errno(ret); | |
7184 | if (ret) | |
f1170fd4 | 7185 | goto rollback; |
b03804e7 | 7186 | |
9ff162a8 | 7187 | return 0; |
5d261913 | 7188 | |
f1170fd4 | 7189 | rollback: |
2f268f12 | 7190 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 VF |
7191 | |
7192 | return ret; | |
9ff162a8 JP |
7193 | } |
7194 | ||
7195 | /** | |
7196 | * netdev_upper_dev_link - Add a link to the upper device | |
7197 | * @dev: device | |
7198 | * @upper_dev: new upper device | |
7a006d59 | 7199 | * @extack: netlink extended ack |
9ff162a8 JP |
7200 | * |
7201 | * Adds a link to device which is upper to this one. The caller must hold | |
7202 | * the RTNL lock. On a failure a negative errno code is returned. | |
7203 | * On success the reference counts are adjusted and the function | |
7204 | * returns zero. | |
7205 | */ | |
7206 | int netdev_upper_dev_link(struct net_device *dev, | |
42ab19ee DA |
7207 | struct net_device *upper_dev, |
7208 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7209 | { |
42ab19ee DA |
7210 | return __netdev_upper_dev_link(dev, upper_dev, false, |
7211 | NULL, NULL, extack); | |
9ff162a8 JP |
7212 | } |
7213 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
7214 | ||
7215 | /** | |
7216 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
7217 | * @dev: device | |
7218 | * @upper_dev: new upper device | |
6dffb044 | 7219 | * @upper_priv: upper device private |
29bf24af | 7220 | * @upper_info: upper info to be passed down via notifier |
7a006d59 | 7221 | * @extack: netlink extended ack |
9ff162a8 JP |
7222 | * |
7223 | * Adds a link to device which is upper to this one. In this case, only | |
7224 | * one master upper device can be linked, although other non-master devices | |
7225 | * might be linked as well. The caller must hold the RTNL lock. | |
7226 | * On a failure a negative errno code is returned. On success the reference | |
7227 | * counts are adjusted and the function returns zero. | |
7228 | */ | |
7229 | int netdev_master_upper_dev_link(struct net_device *dev, | |
6dffb044 | 7230 | struct net_device *upper_dev, |
42ab19ee DA |
7231 | void *upper_priv, void *upper_info, |
7232 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7233 | { |
29bf24af | 7234 | return __netdev_upper_dev_link(dev, upper_dev, true, |
42ab19ee | 7235 | upper_priv, upper_info, extack); |
9ff162a8 JP |
7236 | } |
7237 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
7238 | ||
7239 | /** | |
7240 | * netdev_upper_dev_unlink - Removes a link to upper device | |
7241 | * @dev: device | |
7242 | * @upper_dev: new upper device | |
7243 | * | |
7244 | * Removes a link to device which is upper to this one. The caller must hold | |
7245 | * the RTNL lock. | |
7246 | */ | |
7247 | void netdev_upper_dev_unlink(struct net_device *dev, | |
7248 | struct net_device *upper_dev) | |
7249 | { | |
51d0c047 DA |
7250 | struct netdev_notifier_changeupper_info changeupper_info = { |
7251 | .info = { | |
7252 | .dev = dev, | |
7253 | }, | |
7254 | .upper_dev = upper_dev, | |
7255 | .linking = false, | |
7256 | }; | |
f4563a75 | 7257 | |
9ff162a8 JP |
7258 | ASSERT_RTNL(); |
7259 | ||
0e4ead9d | 7260 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
0e4ead9d | 7261 | |
51d0c047 | 7262 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7263 | &changeupper_info.info); |
7264 | ||
2f268f12 | 7265 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 | 7266 | |
51d0c047 | 7267 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
0e4ead9d | 7268 | &changeupper_info.info); |
9ff162a8 JP |
7269 | } |
7270 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
7271 | ||
61bd3857 MS |
7272 | /** |
7273 | * netdev_bonding_info_change - Dispatch event about slave change | |
7274 | * @dev: device | |
4a26e453 | 7275 | * @bonding_info: info to dispatch |
61bd3857 MS |
7276 | * |
7277 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. | |
7278 | * The caller must hold the RTNL lock. | |
7279 | */ | |
7280 | void netdev_bonding_info_change(struct net_device *dev, | |
7281 | struct netdev_bonding_info *bonding_info) | |
7282 | { | |
51d0c047 DA |
7283 | struct netdev_notifier_bonding_info info = { |
7284 | .info.dev = dev, | |
7285 | }; | |
61bd3857 MS |
7286 | |
7287 | memcpy(&info.bonding_info, bonding_info, | |
7288 | sizeof(struct netdev_bonding_info)); | |
51d0c047 | 7289 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
61bd3857 MS |
7290 | &info.info); |
7291 | } | |
7292 | EXPORT_SYMBOL(netdev_bonding_info_change); | |
7293 | ||
2ce1ee17 | 7294 | static void netdev_adjacent_add_links(struct net_device *dev) |
4c75431a AF |
7295 | { |
7296 | struct netdev_adjacent *iter; | |
7297 | ||
7298 | struct net *net = dev_net(dev); | |
7299 | ||
7300 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 7301 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7302 | continue; |
7303 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7304 | &iter->dev->adj_list.lower); | |
7305 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
7306 | &dev->adj_list.upper); | |
7307 | } | |
7308 | ||
7309 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 7310 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7311 | continue; |
7312 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7313 | &iter->dev->adj_list.upper); | |
7314 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
7315 | &dev->adj_list.lower); | |
7316 | } | |
7317 | } | |
7318 | ||
2ce1ee17 | 7319 | static void netdev_adjacent_del_links(struct net_device *dev) |
4c75431a AF |
7320 | { |
7321 | struct netdev_adjacent *iter; | |
7322 | ||
7323 | struct net *net = dev_net(dev); | |
7324 | ||
7325 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 7326 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7327 | continue; |
7328 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
7329 | &iter->dev->adj_list.lower); | |
7330 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
7331 | &dev->adj_list.upper); | |
7332 | } | |
7333 | ||
7334 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 7335 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7336 | continue; |
7337 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
7338 | &iter->dev->adj_list.upper); | |
7339 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
7340 | &dev->adj_list.lower); | |
7341 | } | |
7342 | } | |
7343 | ||
5bb025fa | 7344 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
402dae96 | 7345 | { |
5bb025fa | 7346 | struct netdev_adjacent *iter; |
402dae96 | 7347 | |
4c75431a AF |
7348 | struct net *net = dev_net(dev); |
7349 | ||
5bb025fa | 7350 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
be4da0e3 | 7351 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 7352 | continue; |
5bb025fa VF |
7353 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
7354 | &iter->dev->adj_list.lower); | |
7355 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7356 | &iter->dev->adj_list.lower); | |
7357 | } | |
402dae96 | 7358 | |
5bb025fa | 7359 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
be4da0e3 | 7360 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 7361 | continue; |
5bb025fa VF |
7362 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
7363 | &iter->dev->adj_list.upper); | |
7364 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7365 | &iter->dev->adj_list.upper); | |
7366 | } | |
402dae96 | 7367 | } |
402dae96 VF |
7368 | |
7369 | void *netdev_lower_dev_get_private(struct net_device *dev, | |
7370 | struct net_device *lower_dev) | |
7371 | { | |
7372 | struct netdev_adjacent *lower; | |
7373 | ||
7374 | if (!lower_dev) | |
7375 | return NULL; | |
6ea29da1 | 7376 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
402dae96 VF |
7377 | if (!lower) |
7378 | return NULL; | |
7379 | ||
7380 | return lower->private; | |
7381 | } | |
7382 | EXPORT_SYMBOL(netdev_lower_dev_get_private); | |
7383 | ||
4085ebe8 | 7384 | |
952fcfd0 | 7385 | int dev_get_nest_level(struct net_device *dev) |
4085ebe8 VY |
7386 | { |
7387 | struct net_device *lower = NULL; | |
7388 | struct list_head *iter; | |
7389 | int max_nest = -1; | |
7390 | int nest; | |
7391 | ||
7392 | ASSERT_RTNL(); | |
7393 | ||
7394 | netdev_for_each_lower_dev(dev, lower, iter) { | |
952fcfd0 | 7395 | nest = dev_get_nest_level(lower); |
4085ebe8 VY |
7396 | if (max_nest < nest) |
7397 | max_nest = nest; | |
7398 | } | |
7399 | ||
952fcfd0 | 7400 | return max_nest + 1; |
4085ebe8 VY |
7401 | } |
7402 | EXPORT_SYMBOL(dev_get_nest_level); | |
7403 | ||
04d48266 JP |
7404 | /** |
7405 | * netdev_lower_change - Dispatch event about lower device state change | |
7406 | * @lower_dev: device | |
7407 | * @lower_state_info: state to dispatch | |
7408 | * | |
7409 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. | |
7410 | * The caller must hold the RTNL lock. | |
7411 | */ | |
7412 | void netdev_lower_state_changed(struct net_device *lower_dev, | |
7413 | void *lower_state_info) | |
7414 | { | |
51d0c047 DA |
7415 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
7416 | .info.dev = lower_dev, | |
7417 | }; | |
04d48266 JP |
7418 | |
7419 | ASSERT_RTNL(); | |
7420 | changelowerstate_info.lower_state_info = lower_state_info; | |
51d0c047 | 7421 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
04d48266 JP |
7422 | &changelowerstate_info.info); |
7423 | } | |
7424 | EXPORT_SYMBOL(netdev_lower_state_changed); | |
7425 | ||
b6c40d68 PM |
7426 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
7427 | { | |
d314774c SH |
7428 | const struct net_device_ops *ops = dev->netdev_ops; |
7429 | ||
d2615bf4 | 7430 | if (ops->ndo_change_rx_flags) |
d314774c | 7431 | ops->ndo_change_rx_flags(dev, flags); |
b6c40d68 PM |
7432 | } |
7433 | ||
991fb3f7 | 7434 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
1da177e4 | 7435 | { |
b536db93 | 7436 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
7437 | kuid_t uid; |
7438 | kgid_t gid; | |
1da177e4 | 7439 | |
24023451 PM |
7440 | ASSERT_RTNL(); |
7441 | ||
dad9b335 WC |
7442 | dev->flags |= IFF_PROMISC; |
7443 | dev->promiscuity += inc; | |
7444 | if (dev->promiscuity == 0) { | |
7445 | /* | |
7446 | * Avoid overflow. | |
7447 | * If inc causes overflow, untouch promisc and return error. | |
7448 | */ | |
7449 | if (inc < 0) | |
7450 | dev->flags &= ~IFF_PROMISC; | |
7451 | else { | |
7452 | dev->promiscuity -= inc; | |
7b6cd1ce JP |
7453 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
7454 | dev->name); | |
dad9b335 WC |
7455 | return -EOVERFLOW; |
7456 | } | |
7457 | } | |
52609c0b | 7458 | if (dev->flags != old_flags) { |
7b6cd1ce JP |
7459 | pr_info("device %s %s promiscuous mode\n", |
7460 | dev->name, | |
7461 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
7462 | if (audit_enabled) { |
7463 | current_uid_gid(&uid, &gid); | |
cdfb6b34 RGB |
7464 | audit_log(audit_context(), GFP_ATOMIC, |
7465 | AUDIT_ANOM_PROMISCUOUS, | |
7466 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
7467 | dev->name, (dev->flags & IFF_PROMISC), | |
7468 | (old_flags & IFF_PROMISC), | |
7469 | from_kuid(&init_user_ns, audit_get_loginuid(current)), | |
7470 | from_kuid(&init_user_ns, uid), | |
7471 | from_kgid(&init_user_ns, gid), | |
7472 | audit_get_sessionid(current)); | |
8192b0c4 | 7473 | } |
24023451 | 7474 | |
b6c40d68 | 7475 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 7476 | } |
991fb3f7 ND |
7477 | if (notify) |
7478 | __dev_notify_flags(dev, old_flags, IFF_PROMISC); | |
dad9b335 | 7479 | return 0; |
1da177e4 LT |
7480 | } |
7481 | ||
4417da66 PM |
7482 | /** |
7483 | * dev_set_promiscuity - update promiscuity count on a device | |
7484 | * @dev: device | |
7485 | * @inc: modifier | |
7486 | * | |
7487 | * Add or remove promiscuity from a device. While the count in the device | |
7488 | * remains above zero the interface remains promiscuous. Once it hits zero | |
7489 | * the device reverts back to normal filtering operation. A negative inc | |
7490 | * value is used to drop promiscuity on the device. | |
dad9b335 | 7491 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 7492 | */ |
dad9b335 | 7493 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 7494 | { |
b536db93 | 7495 | unsigned int old_flags = dev->flags; |
dad9b335 | 7496 | int err; |
4417da66 | 7497 | |
991fb3f7 | 7498 | err = __dev_set_promiscuity(dev, inc, true); |
4b5a698e | 7499 | if (err < 0) |
dad9b335 | 7500 | return err; |
4417da66 PM |
7501 | if (dev->flags != old_flags) |
7502 | dev_set_rx_mode(dev); | |
dad9b335 | 7503 | return err; |
4417da66 | 7504 | } |
d1b19dff | 7505 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 7506 | |
991fb3f7 | 7507 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
1da177e4 | 7508 | { |
991fb3f7 | 7509 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
1da177e4 | 7510 | |
24023451 PM |
7511 | ASSERT_RTNL(); |
7512 | ||
1da177e4 | 7513 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
7514 | dev->allmulti += inc; |
7515 | if (dev->allmulti == 0) { | |
7516 | /* | |
7517 | * Avoid overflow. | |
7518 | * If inc causes overflow, untouch allmulti and return error. | |
7519 | */ | |
7520 | if (inc < 0) | |
7521 | dev->flags &= ~IFF_ALLMULTI; | |
7522 | else { | |
7523 | dev->allmulti -= inc; | |
7b6cd1ce JP |
7524 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
7525 | dev->name); | |
dad9b335 WC |
7526 | return -EOVERFLOW; |
7527 | } | |
7528 | } | |
24023451 | 7529 | if (dev->flags ^ old_flags) { |
b6c40d68 | 7530 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 7531 | dev_set_rx_mode(dev); |
991fb3f7 ND |
7532 | if (notify) |
7533 | __dev_notify_flags(dev, old_flags, | |
7534 | dev->gflags ^ old_gflags); | |
24023451 | 7535 | } |
dad9b335 | 7536 | return 0; |
4417da66 | 7537 | } |
991fb3f7 ND |
7538 | |
7539 | /** | |
7540 | * dev_set_allmulti - update allmulti count on a device | |
7541 | * @dev: device | |
7542 | * @inc: modifier | |
7543 | * | |
7544 | * Add or remove reception of all multicast frames to a device. While the | |
7545 | * count in the device remains above zero the interface remains listening | |
7546 | * to all interfaces. Once it hits zero the device reverts back to normal | |
7547 | * filtering operation. A negative @inc value is used to drop the counter | |
7548 | * when releasing a resource needing all multicasts. | |
7549 | * Return 0 if successful or a negative errno code on error. | |
7550 | */ | |
7551 | ||
7552 | int dev_set_allmulti(struct net_device *dev, int inc) | |
7553 | { | |
7554 | return __dev_set_allmulti(dev, inc, true); | |
7555 | } | |
d1b19dff | 7556 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
7557 | |
7558 | /* | |
7559 | * Upload unicast and multicast address lists to device and | |
7560 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 7561 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
7562 | * are present. |
7563 | */ | |
7564 | void __dev_set_rx_mode(struct net_device *dev) | |
7565 | { | |
d314774c SH |
7566 | const struct net_device_ops *ops = dev->netdev_ops; |
7567 | ||
4417da66 PM |
7568 | /* dev_open will call this function so the list will stay sane. */ |
7569 | if (!(dev->flags&IFF_UP)) | |
7570 | return; | |
7571 | ||
7572 | if (!netif_device_present(dev)) | |
40b77c94 | 7573 | return; |
4417da66 | 7574 | |
01789349 | 7575 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
7576 | /* Unicast addresses changes may only happen under the rtnl, |
7577 | * therefore calling __dev_set_promiscuity here is safe. | |
7578 | */ | |
32e7bfc4 | 7579 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
991fb3f7 | 7580 | __dev_set_promiscuity(dev, 1, false); |
2d348d1f | 7581 | dev->uc_promisc = true; |
32e7bfc4 | 7582 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
991fb3f7 | 7583 | __dev_set_promiscuity(dev, -1, false); |
2d348d1f | 7584 | dev->uc_promisc = false; |
4417da66 | 7585 | } |
4417da66 | 7586 | } |
01789349 JP |
7587 | |
7588 | if (ops->ndo_set_rx_mode) | |
7589 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
7590 | } |
7591 | ||
7592 | void dev_set_rx_mode(struct net_device *dev) | |
7593 | { | |
b9e40857 | 7594 | netif_addr_lock_bh(dev); |
4417da66 | 7595 | __dev_set_rx_mode(dev); |
b9e40857 | 7596 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
7597 | } |
7598 | ||
f0db275a SH |
7599 | /** |
7600 | * dev_get_flags - get flags reported to userspace | |
7601 | * @dev: device | |
7602 | * | |
7603 | * Get the combination of flag bits exported through APIs to userspace. | |
7604 | */ | |
95c96174 | 7605 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 7606 | { |
95c96174 | 7607 | unsigned int flags; |
1da177e4 LT |
7608 | |
7609 | flags = (dev->flags & ~(IFF_PROMISC | | |
7610 | IFF_ALLMULTI | | |
b00055aa SR |
7611 | IFF_RUNNING | |
7612 | IFF_LOWER_UP | | |
7613 | IFF_DORMANT)) | | |
1da177e4 LT |
7614 | (dev->gflags & (IFF_PROMISC | |
7615 | IFF_ALLMULTI)); | |
7616 | ||
b00055aa SR |
7617 | if (netif_running(dev)) { |
7618 | if (netif_oper_up(dev)) | |
7619 | flags |= IFF_RUNNING; | |
7620 | if (netif_carrier_ok(dev)) | |
7621 | flags |= IFF_LOWER_UP; | |
7622 | if (netif_dormant(dev)) | |
7623 | flags |= IFF_DORMANT; | |
7624 | } | |
1da177e4 LT |
7625 | |
7626 | return flags; | |
7627 | } | |
d1b19dff | 7628 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 7629 | |
6d040321 PM |
7630 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
7631 | struct netlink_ext_ack *extack) | |
1da177e4 | 7632 | { |
b536db93 | 7633 | unsigned int old_flags = dev->flags; |
bd380811 | 7634 | int ret; |
1da177e4 | 7635 | |
24023451 PM |
7636 | ASSERT_RTNL(); |
7637 | ||
1da177e4 LT |
7638 | /* |
7639 | * Set the flags on our device. | |
7640 | */ | |
7641 | ||
7642 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
7643 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
7644 | IFF_AUTOMEDIA)) | | |
7645 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
7646 | IFF_ALLMULTI)); | |
7647 | ||
7648 | /* | |
7649 | * Load in the correct multicast list now the flags have changed. | |
7650 | */ | |
7651 | ||
b6c40d68 PM |
7652 | if ((old_flags ^ flags) & IFF_MULTICAST) |
7653 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 7654 | |
4417da66 | 7655 | dev_set_rx_mode(dev); |
1da177e4 LT |
7656 | |
7657 | /* | |
7658 | * Have we downed the interface. We handle IFF_UP ourselves | |
7659 | * according to user attempts to set it, rather than blindly | |
7660 | * setting it. | |
7661 | */ | |
7662 | ||
7663 | ret = 0; | |
7051b88a | 7664 | if ((old_flags ^ flags) & IFF_UP) { |
7665 | if (old_flags & IFF_UP) | |
7666 | __dev_close(dev); | |
7667 | else | |
40c900aa | 7668 | ret = __dev_open(dev, extack); |
7051b88a | 7669 | } |
1da177e4 | 7670 | |
1da177e4 | 7671 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff | 7672 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
991fb3f7 | 7673 | unsigned int old_flags = dev->flags; |
d1b19dff | 7674 | |
1da177e4 | 7675 | dev->gflags ^= IFF_PROMISC; |
991fb3f7 ND |
7676 | |
7677 | if (__dev_set_promiscuity(dev, inc, false) >= 0) | |
7678 | if (dev->flags != old_flags) | |
7679 | dev_set_rx_mode(dev); | |
1da177e4 LT |
7680 | } |
7681 | ||
7682 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
eb13da1a | 7683 | * is important. Some (broken) drivers set IFF_PROMISC, when |
7684 | * IFF_ALLMULTI is requested not asking us and not reporting. | |
1da177e4 LT |
7685 | */ |
7686 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
7687 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
7688 | ||
1da177e4 | 7689 | dev->gflags ^= IFF_ALLMULTI; |
991fb3f7 | 7690 | __dev_set_allmulti(dev, inc, false); |
1da177e4 LT |
7691 | } |
7692 | ||
bd380811 PM |
7693 | return ret; |
7694 | } | |
7695 | ||
a528c219 ND |
7696 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
7697 | unsigned int gchanges) | |
bd380811 PM |
7698 | { |
7699 | unsigned int changes = dev->flags ^ old_flags; | |
7700 | ||
a528c219 | 7701 | if (gchanges) |
7f294054 | 7702 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC); |
a528c219 | 7703 | |
bd380811 PM |
7704 | if (changes & IFF_UP) { |
7705 | if (dev->flags & IFF_UP) | |
7706 | call_netdevice_notifiers(NETDEV_UP, dev); | |
7707 | else | |
7708 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
7709 | } | |
7710 | ||
7711 | if (dev->flags & IFF_UP && | |
be9efd36 | 7712 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
51d0c047 DA |
7713 | struct netdev_notifier_change_info change_info = { |
7714 | .info = { | |
7715 | .dev = dev, | |
7716 | }, | |
7717 | .flags_changed = changes, | |
7718 | }; | |
be9efd36 | 7719 | |
51d0c047 | 7720 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
be9efd36 | 7721 | } |
bd380811 PM |
7722 | } |
7723 | ||
7724 | /** | |
7725 | * dev_change_flags - change device settings | |
7726 | * @dev: device | |
7727 | * @flags: device state flags | |
567c5e13 | 7728 | * @extack: netlink extended ack |
bd380811 PM |
7729 | * |
7730 | * Change settings on device based state flags. The flags are | |
7731 | * in the userspace exported format. | |
7732 | */ | |
567c5e13 PM |
7733 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
7734 | struct netlink_ext_ack *extack) | |
bd380811 | 7735 | { |
b536db93 | 7736 | int ret; |
991fb3f7 | 7737 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
bd380811 | 7738 | |
6d040321 | 7739 | ret = __dev_change_flags(dev, flags, extack); |
bd380811 PM |
7740 | if (ret < 0) |
7741 | return ret; | |
7742 | ||
991fb3f7 | 7743 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
a528c219 | 7744 | __dev_notify_flags(dev, old_flags, changes); |
1da177e4 LT |
7745 | return ret; |
7746 | } | |
d1b19dff | 7747 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 7748 | |
f51048c3 | 7749 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
2315dc91 VF |
7750 | { |
7751 | const struct net_device_ops *ops = dev->netdev_ops; | |
7752 | ||
7753 | if (ops->ndo_change_mtu) | |
7754 | return ops->ndo_change_mtu(dev, new_mtu); | |
7755 | ||
7756 | dev->mtu = new_mtu; | |
7757 | return 0; | |
7758 | } | |
f51048c3 | 7759 | EXPORT_SYMBOL(__dev_set_mtu); |
2315dc91 | 7760 | |
f0db275a | 7761 | /** |
7a4c53be | 7762 | * dev_set_mtu_ext - Change maximum transfer unit |
f0db275a SH |
7763 | * @dev: device |
7764 | * @new_mtu: new transfer unit | |
7a4c53be | 7765 | * @extack: netlink extended ack |
f0db275a SH |
7766 | * |
7767 | * Change the maximum transfer size of the network device. | |
7768 | */ | |
7a4c53be SH |
7769 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
7770 | struct netlink_ext_ack *extack) | |
1da177e4 | 7771 | { |
2315dc91 | 7772 | int err, orig_mtu; |
1da177e4 LT |
7773 | |
7774 | if (new_mtu == dev->mtu) | |
7775 | return 0; | |
7776 | ||
61e84623 JW |
7777 | /* MTU must be positive, and in range */ |
7778 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { | |
7a4c53be | 7779 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); |
1da177e4 | 7780 | return -EINVAL; |
61e84623 JW |
7781 | } |
7782 | ||
7783 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { | |
7a4c53be | 7784 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); |
61e84623 JW |
7785 | return -EINVAL; |
7786 | } | |
1da177e4 LT |
7787 | |
7788 | if (!netif_device_present(dev)) | |
7789 | return -ENODEV; | |
7790 | ||
1d486bfb VF |
7791 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
7792 | err = notifier_to_errno(err); | |
7793 | if (err) | |
7794 | return err; | |
d314774c | 7795 | |
2315dc91 VF |
7796 | orig_mtu = dev->mtu; |
7797 | err = __dev_set_mtu(dev, new_mtu); | |
d314774c | 7798 | |
2315dc91 | 7799 | if (!err) { |
af7d6cce SD |
7800 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
7801 | orig_mtu); | |
2315dc91 VF |
7802 | err = notifier_to_errno(err); |
7803 | if (err) { | |
7804 | /* setting mtu back and notifying everyone again, | |
7805 | * so that they have a chance to revert changes. | |
7806 | */ | |
7807 | __dev_set_mtu(dev, orig_mtu); | |
af7d6cce SD |
7808 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
7809 | new_mtu); | |
2315dc91 VF |
7810 | } |
7811 | } | |
1da177e4 LT |
7812 | return err; |
7813 | } | |
7a4c53be SH |
7814 | |
7815 | int dev_set_mtu(struct net_device *dev, int new_mtu) | |
7816 | { | |
7817 | struct netlink_ext_ack extack; | |
7818 | int err; | |
7819 | ||
a6bcfc89 | 7820 | memset(&extack, 0, sizeof(extack)); |
7a4c53be | 7821 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
a6bcfc89 | 7822 | if (err && extack._msg) |
7a4c53be SH |
7823 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
7824 | return err; | |
7825 | } | |
d1b19dff | 7826 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 7827 | |
6a643ddb CW |
7828 | /** |
7829 | * dev_change_tx_queue_len - Change TX queue length of a netdevice | |
7830 | * @dev: device | |
7831 | * @new_len: new tx queue length | |
7832 | */ | |
7833 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) | |
7834 | { | |
7835 | unsigned int orig_len = dev->tx_queue_len; | |
7836 | int res; | |
7837 | ||
7838 | if (new_len != (unsigned int)new_len) | |
7839 | return -ERANGE; | |
7840 | ||
7841 | if (new_len != orig_len) { | |
7842 | dev->tx_queue_len = new_len; | |
7843 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
7844 | res = notifier_to_errno(res); | |
7effaf06 TT |
7845 | if (res) |
7846 | goto err_rollback; | |
7847 | res = dev_qdisc_change_tx_queue_len(dev); | |
7848 | if (res) | |
7849 | goto err_rollback; | |
6a643ddb CW |
7850 | } |
7851 | ||
7852 | return 0; | |
7effaf06 TT |
7853 | |
7854 | err_rollback: | |
7855 | netdev_err(dev, "refused to change device tx_queue_len\n"); | |
7856 | dev->tx_queue_len = orig_len; | |
7857 | return res; | |
6a643ddb CW |
7858 | } |
7859 | ||
cbda10fa VD |
7860 | /** |
7861 | * dev_set_group - Change group this device belongs to | |
7862 | * @dev: device | |
7863 | * @new_group: group this device should belong to | |
7864 | */ | |
7865 | void dev_set_group(struct net_device *dev, int new_group) | |
7866 | { | |
7867 | dev->group = new_group; | |
7868 | } | |
7869 | EXPORT_SYMBOL(dev_set_group); | |
7870 | ||
d59cdf94 PM |
7871 | /** |
7872 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. | |
7873 | * @dev: device | |
7874 | * @addr: new address | |
7875 | * @extack: netlink extended ack | |
7876 | */ | |
7877 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, | |
7878 | struct netlink_ext_ack *extack) | |
7879 | { | |
7880 | struct netdev_notifier_pre_changeaddr_info info = { | |
7881 | .info.dev = dev, | |
7882 | .info.extack = extack, | |
7883 | .dev_addr = addr, | |
7884 | }; | |
7885 | int rc; | |
7886 | ||
7887 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); | |
7888 | return notifier_to_errno(rc); | |
7889 | } | |
7890 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); | |
7891 | ||
f0db275a SH |
7892 | /** |
7893 | * dev_set_mac_address - Change Media Access Control Address | |
7894 | * @dev: device | |
7895 | * @sa: new address | |
3a37a963 | 7896 | * @extack: netlink extended ack |
f0db275a SH |
7897 | * |
7898 | * Change the hardware (MAC) address of the device | |
7899 | */ | |
3a37a963 PM |
7900 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
7901 | struct netlink_ext_ack *extack) | |
1da177e4 | 7902 | { |
d314774c | 7903 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
7904 | int err; |
7905 | ||
d314774c | 7906 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
7907 | return -EOPNOTSUPP; |
7908 | if (sa->sa_family != dev->type) | |
7909 | return -EINVAL; | |
7910 | if (!netif_device_present(dev)) | |
7911 | return -ENODEV; | |
d59cdf94 PM |
7912 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
7913 | if (err) | |
7914 | return err; | |
d314774c | 7915 | err = ops->ndo_set_mac_address(dev, sa); |
f6521516 JP |
7916 | if (err) |
7917 | return err; | |
fbdeca2d | 7918 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 7919 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 7920 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 7921 | return 0; |
1da177e4 | 7922 | } |
d1b19dff | 7923 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 7924 | |
4bf84c35 JP |
7925 | /** |
7926 | * dev_change_carrier - Change device carrier | |
7927 | * @dev: device | |
691b3b7e | 7928 | * @new_carrier: new value |
4bf84c35 JP |
7929 | * |
7930 | * Change device carrier | |
7931 | */ | |
7932 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
7933 | { | |
7934 | const struct net_device_ops *ops = dev->netdev_ops; | |
7935 | ||
7936 | if (!ops->ndo_change_carrier) | |
7937 | return -EOPNOTSUPP; | |
7938 | if (!netif_device_present(dev)) | |
7939 | return -ENODEV; | |
7940 | return ops->ndo_change_carrier(dev, new_carrier); | |
7941 | } | |
7942 | EXPORT_SYMBOL(dev_change_carrier); | |
7943 | ||
66b52b0d JP |
7944 | /** |
7945 | * dev_get_phys_port_id - Get device physical port ID | |
7946 | * @dev: device | |
7947 | * @ppid: port ID | |
7948 | * | |
7949 | * Get device physical port ID | |
7950 | */ | |
7951 | int dev_get_phys_port_id(struct net_device *dev, | |
02637fce | 7952 | struct netdev_phys_item_id *ppid) |
66b52b0d JP |
7953 | { |
7954 | const struct net_device_ops *ops = dev->netdev_ops; | |
7955 | ||
7956 | if (!ops->ndo_get_phys_port_id) | |
7957 | return -EOPNOTSUPP; | |
7958 | return ops->ndo_get_phys_port_id(dev, ppid); | |
7959 | } | |
7960 | EXPORT_SYMBOL(dev_get_phys_port_id); | |
7961 | ||
db24a904 DA |
7962 | /** |
7963 | * dev_get_phys_port_name - Get device physical port name | |
7964 | * @dev: device | |
7965 | * @name: port name | |
ed49e650 | 7966 | * @len: limit of bytes to copy to name |
db24a904 DA |
7967 | * |
7968 | * Get device physical port name | |
7969 | */ | |
7970 | int dev_get_phys_port_name(struct net_device *dev, | |
7971 | char *name, size_t len) | |
7972 | { | |
7973 | const struct net_device_ops *ops = dev->netdev_ops; | |
af3836df | 7974 | int err; |
db24a904 | 7975 | |
af3836df JP |
7976 | if (ops->ndo_get_phys_port_name) { |
7977 | err = ops->ndo_get_phys_port_name(dev, name, len); | |
7978 | if (err != -EOPNOTSUPP) | |
7979 | return err; | |
7980 | } | |
7981 | return devlink_compat_phys_port_name_get(dev, name, len); | |
db24a904 DA |
7982 | } |
7983 | EXPORT_SYMBOL(dev_get_phys_port_name); | |
7984 | ||
d6abc596 FF |
7985 | /** |
7986 | * dev_get_port_parent_id - Get the device's port parent identifier | |
7987 | * @dev: network device | |
7988 | * @ppid: pointer to a storage for the port's parent identifier | |
7989 | * @recurse: allow/disallow recursion to lower devices | |
7990 | * | |
7991 | * Get the devices's port parent identifier | |
7992 | */ | |
7993 | int dev_get_port_parent_id(struct net_device *dev, | |
7994 | struct netdev_phys_item_id *ppid, | |
7995 | bool recurse) | |
7996 | { | |
7997 | const struct net_device_ops *ops = dev->netdev_ops; | |
7998 | struct netdev_phys_item_id first = { }; | |
7999 | struct net_device *lower_dev; | |
8000 | struct list_head *iter; | |
7e1146e8 JP |
8001 | int err; |
8002 | ||
8003 | if (ops->ndo_get_port_parent_id) { | |
8004 | err = ops->ndo_get_port_parent_id(dev, ppid); | |
8005 | if (err != -EOPNOTSUPP) | |
8006 | return err; | |
8007 | } | |
d6abc596 | 8008 | |
7e1146e8 JP |
8009 | err = devlink_compat_switch_id_get(dev, ppid); |
8010 | if (!err || err != -EOPNOTSUPP) | |
8011 | return err; | |
d6abc596 FF |
8012 | |
8013 | if (!recurse) | |
7e1146e8 | 8014 | return -EOPNOTSUPP; |
d6abc596 FF |
8015 | |
8016 | netdev_for_each_lower_dev(dev, lower_dev, iter) { | |
8017 | err = dev_get_port_parent_id(lower_dev, ppid, recurse); | |
8018 | if (err) | |
8019 | break; | |
8020 | if (!first.id_len) | |
8021 | first = *ppid; | |
8022 | else if (memcmp(&first, ppid, sizeof(*ppid))) | |
8023 | return -ENODATA; | |
8024 | } | |
8025 | ||
8026 | return err; | |
8027 | } | |
8028 | EXPORT_SYMBOL(dev_get_port_parent_id); | |
8029 | ||
8030 | /** | |
8031 | * netdev_port_same_parent_id - Indicate if two network devices have | |
8032 | * the same port parent identifier | |
8033 | * @a: first network device | |
8034 | * @b: second network device | |
8035 | */ | |
8036 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) | |
8037 | { | |
8038 | struct netdev_phys_item_id a_id = { }; | |
8039 | struct netdev_phys_item_id b_id = { }; | |
8040 | ||
8041 | if (dev_get_port_parent_id(a, &a_id, true) || | |
8042 | dev_get_port_parent_id(b, &b_id, true)) | |
8043 | return false; | |
8044 | ||
8045 | return netdev_phys_item_id_same(&a_id, &b_id); | |
8046 | } | |
8047 | EXPORT_SYMBOL(netdev_port_same_parent_id); | |
8048 | ||
d746d707 AK |
8049 | /** |
8050 | * dev_change_proto_down - update protocol port state information | |
8051 | * @dev: device | |
8052 | * @proto_down: new value | |
8053 | * | |
8054 | * This info can be used by switch drivers to set the phys state of the | |
8055 | * port. | |
8056 | */ | |
8057 | int dev_change_proto_down(struct net_device *dev, bool proto_down) | |
8058 | { | |
8059 | const struct net_device_ops *ops = dev->netdev_ops; | |
8060 | ||
8061 | if (!ops->ndo_change_proto_down) | |
8062 | return -EOPNOTSUPP; | |
8063 | if (!netif_device_present(dev)) | |
8064 | return -ENODEV; | |
8065 | return ops->ndo_change_proto_down(dev, proto_down); | |
8066 | } | |
8067 | EXPORT_SYMBOL(dev_change_proto_down); | |
8068 | ||
b5899679 AR |
8069 | /** |
8070 | * dev_change_proto_down_generic - generic implementation for | |
8071 | * ndo_change_proto_down that sets carrier according to | |
8072 | * proto_down. | |
8073 | * | |
8074 | * @dev: device | |
8075 | * @proto_down: new value | |
8076 | */ | |
8077 | int dev_change_proto_down_generic(struct net_device *dev, bool proto_down) | |
8078 | { | |
8079 | if (proto_down) | |
8080 | netif_carrier_off(dev); | |
8081 | else | |
8082 | netif_carrier_on(dev); | |
8083 | dev->proto_down = proto_down; | |
8084 | return 0; | |
8085 | } | |
8086 | EXPORT_SYMBOL(dev_change_proto_down_generic); | |
8087 | ||
a25717d2 JK |
8088 | u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op, |
8089 | enum bpf_netdev_command cmd) | |
d67b9cd2 | 8090 | { |
a25717d2 | 8091 | struct netdev_bpf xdp; |
d67b9cd2 | 8092 | |
a25717d2 JK |
8093 | if (!bpf_op) |
8094 | return 0; | |
118b4aa2 | 8095 | |
a25717d2 JK |
8096 | memset(&xdp, 0, sizeof(xdp)); |
8097 | xdp.command = cmd; | |
118b4aa2 | 8098 | |
a25717d2 JK |
8099 | /* Query must always succeed. */ |
8100 | WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG); | |
58038695 | 8101 | |
6b867589 | 8102 | return xdp.prog_id; |
d67b9cd2 DB |
8103 | } |
8104 | ||
f4e63525 | 8105 | static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op, |
32d60277 | 8106 | struct netlink_ext_ack *extack, u32 flags, |
d67b9cd2 DB |
8107 | struct bpf_prog *prog) |
8108 | { | |
f4e63525 | 8109 | struct netdev_bpf xdp; |
d67b9cd2 DB |
8110 | |
8111 | memset(&xdp, 0, sizeof(xdp)); | |
ee5d032f JK |
8112 | if (flags & XDP_FLAGS_HW_MODE) |
8113 | xdp.command = XDP_SETUP_PROG_HW; | |
8114 | else | |
8115 | xdp.command = XDP_SETUP_PROG; | |
d67b9cd2 | 8116 | xdp.extack = extack; |
32d60277 | 8117 | xdp.flags = flags; |
d67b9cd2 DB |
8118 | xdp.prog = prog; |
8119 | ||
f4e63525 | 8120 | return bpf_op(dev, &xdp); |
d67b9cd2 DB |
8121 | } |
8122 | ||
bd0b2e7f JK |
8123 | static void dev_xdp_uninstall(struct net_device *dev) |
8124 | { | |
8125 | struct netdev_bpf xdp; | |
8126 | bpf_op_t ndo_bpf; | |
8127 | ||
8128 | /* Remove generic XDP */ | |
8129 | WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL)); | |
8130 | ||
8131 | /* Remove from the driver */ | |
8132 | ndo_bpf = dev->netdev_ops->ndo_bpf; | |
8133 | if (!ndo_bpf) | |
8134 | return; | |
8135 | ||
a25717d2 JK |
8136 | memset(&xdp, 0, sizeof(xdp)); |
8137 | xdp.command = XDP_QUERY_PROG; | |
8138 | WARN_ON(ndo_bpf(dev, &xdp)); | |
8139 | if (xdp.prog_id) | |
8140 | WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, | |
8141 | NULL)); | |
bd0b2e7f | 8142 | |
a25717d2 JK |
8143 | /* Remove HW offload */ |
8144 | memset(&xdp, 0, sizeof(xdp)); | |
8145 | xdp.command = XDP_QUERY_PROG_HW; | |
8146 | if (!ndo_bpf(dev, &xdp) && xdp.prog_id) | |
8147 | WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, | |
8148 | NULL)); | |
bd0b2e7f JK |
8149 | } |
8150 | ||
a7862b45 BB |
8151 | /** |
8152 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path | |
8153 | * @dev: device | |
b5d60989 | 8154 | * @extack: netlink extended ack |
a7862b45 | 8155 | * @fd: new program fd or negative value to clear |
85de8576 | 8156 | * @flags: xdp-related flags |
a7862b45 BB |
8157 | * |
8158 | * Set or clear a bpf program for a device | |
8159 | */ | |
ddf9f970 JK |
8160 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, |
8161 | int fd, u32 flags) | |
a7862b45 BB |
8162 | { |
8163 | const struct net_device_ops *ops = dev->netdev_ops; | |
a25717d2 | 8164 | enum bpf_netdev_command query; |
a7862b45 | 8165 | struct bpf_prog *prog = NULL; |
f4e63525 | 8166 | bpf_op_t bpf_op, bpf_chk; |
9ee963d6 | 8167 | bool offload; |
a7862b45 BB |
8168 | int err; |
8169 | ||
85de8576 DB |
8170 | ASSERT_RTNL(); |
8171 | ||
9ee963d6 JK |
8172 | offload = flags & XDP_FLAGS_HW_MODE; |
8173 | query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG; | |
a25717d2 | 8174 | |
f4e63525 | 8175 | bpf_op = bpf_chk = ops->ndo_bpf; |
01dde20c MF |
8176 | if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) { |
8177 | NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode"); | |
0489df9a | 8178 | return -EOPNOTSUPP; |
01dde20c | 8179 | } |
f4e63525 JK |
8180 | if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE)) |
8181 | bpf_op = generic_xdp_install; | |
8182 | if (bpf_op == bpf_chk) | |
8183 | bpf_chk = generic_xdp_install; | |
b5cdae32 | 8184 | |
a7862b45 | 8185 | if (fd >= 0) { |
c14a9f63 MM |
8186 | u32 prog_id; |
8187 | ||
9ee963d6 | 8188 | if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) { |
01dde20c | 8189 | NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time"); |
d67b9cd2 | 8190 | return -EEXIST; |
01dde20c | 8191 | } |
c14a9f63 MM |
8192 | |
8193 | prog_id = __dev_xdp_query(dev, bpf_op, query); | |
8194 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) { | |
01dde20c | 8195 | NL_SET_ERR_MSG(extack, "XDP program already attached"); |
d67b9cd2 | 8196 | return -EBUSY; |
01dde20c | 8197 | } |
85de8576 | 8198 | |
288b3de5 JK |
8199 | prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, |
8200 | bpf_op == ops->ndo_bpf); | |
a7862b45 BB |
8201 | if (IS_ERR(prog)) |
8202 | return PTR_ERR(prog); | |
441a3303 | 8203 | |
9ee963d6 | 8204 | if (!offload && bpf_prog_is_dev_bound(prog->aux)) { |
441a3303 JK |
8205 | NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported"); |
8206 | bpf_prog_put(prog); | |
8207 | return -EINVAL; | |
8208 | } | |
c14a9f63 MM |
8209 | |
8210 | if (prog->aux->id == prog_id) { | |
8211 | bpf_prog_put(prog); | |
8212 | return 0; | |
8213 | } | |
8214 | } else { | |
8215 | if (!__dev_xdp_query(dev, bpf_op, query)) | |
8216 | return 0; | |
a7862b45 BB |
8217 | } |
8218 | ||
f4e63525 | 8219 | err = dev_xdp_install(dev, bpf_op, extack, flags, prog); |
a7862b45 BB |
8220 | if (err < 0 && prog) |
8221 | bpf_prog_put(prog); | |
8222 | ||
8223 | return err; | |
8224 | } | |
a7862b45 | 8225 | |
1da177e4 LT |
8226 | /** |
8227 | * dev_new_index - allocate an ifindex | |
c4ea43c5 | 8228 | * @net: the applicable net namespace |
1da177e4 LT |
8229 | * |
8230 | * Returns a suitable unique value for a new device interface | |
8231 | * number. The caller must hold the rtnl semaphore or the | |
8232 | * dev_base_lock to be sure it remains unique. | |
8233 | */ | |
881d966b | 8234 | static int dev_new_index(struct net *net) |
1da177e4 | 8235 | { |
aa79e66e | 8236 | int ifindex = net->ifindex; |
f4563a75 | 8237 | |
1da177e4 LT |
8238 | for (;;) { |
8239 | if (++ifindex <= 0) | |
8240 | ifindex = 1; | |
881d966b | 8241 | if (!__dev_get_by_index(net, ifindex)) |
aa79e66e | 8242 | return net->ifindex = ifindex; |
1da177e4 LT |
8243 | } |
8244 | } | |
8245 | ||
1da177e4 | 8246 | /* Delayed registration/unregisteration */ |
3b5b34fd | 8247 | static LIST_HEAD(net_todo_list); |
200b916f | 8248 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
1da177e4 | 8249 | |
6f05f629 | 8250 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 8251 | { |
1da177e4 | 8252 | list_add_tail(&dev->todo_list, &net_todo_list); |
50624c93 | 8253 | dev_net(dev)->dev_unreg_count++; |
1da177e4 LT |
8254 | } |
8255 | ||
9b5e383c | 8256 | static void rollback_registered_many(struct list_head *head) |
93ee31f1 | 8257 | { |
e93737b0 | 8258 | struct net_device *dev, *tmp; |
5cde2829 | 8259 | LIST_HEAD(close_head); |
9b5e383c | 8260 | |
93ee31f1 DL |
8261 | BUG_ON(dev_boot_phase); |
8262 | ASSERT_RTNL(); | |
8263 | ||
e93737b0 | 8264 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
9b5e383c | 8265 | /* Some devices call without registering |
e93737b0 KK |
8266 | * for initialization unwind. Remove those |
8267 | * devices and proceed with the remaining. | |
9b5e383c ED |
8268 | */ |
8269 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
7b6cd1ce JP |
8270 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", |
8271 | dev->name, dev); | |
93ee31f1 | 8272 | |
9b5e383c | 8273 | WARN_ON(1); |
e93737b0 KK |
8274 | list_del(&dev->unreg_list); |
8275 | continue; | |
9b5e383c | 8276 | } |
449f4544 | 8277 | dev->dismantle = true; |
9b5e383c | 8278 | BUG_ON(dev->reg_state != NETREG_REGISTERED); |
44345724 | 8279 | } |
93ee31f1 | 8280 | |
44345724 | 8281 | /* If device is running, close it first. */ |
5cde2829 EB |
8282 | list_for_each_entry(dev, head, unreg_list) |
8283 | list_add_tail(&dev->close_list, &close_head); | |
99c4a26a | 8284 | dev_close_many(&close_head, true); |
93ee31f1 | 8285 | |
44345724 | 8286 | list_for_each_entry(dev, head, unreg_list) { |
9b5e383c ED |
8287 | /* And unlink it from device chain. */ |
8288 | unlist_netdevice(dev); | |
93ee31f1 | 8289 | |
9b5e383c ED |
8290 | dev->reg_state = NETREG_UNREGISTERING; |
8291 | } | |
41852497 | 8292 | flush_all_backlogs(); |
93ee31f1 DL |
8293 | |
8294 | synchronize_net(); | |
8295 | ||
9b5e383c | 8296 | list_for_each_entry(dev, head, unreg_list) { |
395eea6c MB |
8297 | struct sk_buff *skb = NULL; |
8298 | ||
9b5e383c ED |
8299 | /* Shutdown queueing discipline. */ |
8300 | dev_shutdown(dev); | |
93ee31f1 | 8301 | |
bd0b2e7f | 8302 | dev_xdp_uninstall(dev); |
93ee31f1 | 8303 | |
9b5e383c | 8304 | /* Notify protocols, that we are about to destroy |
eb13da1a | 8305 | * this device. They should clean all the things. |
8306 | */ | |
9b5e383c | 8307 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
93ee31f1 | 8308 | |
395eea6c MB |
8309 | if (!dev->rtnl_link_ops || |
8310 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
3d3ea5af | 8311 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, |
38e01b30 | 8312 | GFP_KERNEL, NULL, 0); |
395eea6c | 8313 | |
9b5e383c ED |
8314 | /* |
8315 | * Flush the unicast and multicast chains | |
8316 | */ | |
a748ee24 | 8317 | dev_uc_flush(dev); |
22bedad3 | 8318 | dev_mc_flush(dev); |
93ee31f1 | 8319 | |
ff927412 JP |
8320 | netdev_name_node_free(dev->name_node); |
8321 | ||
9b5e383c ED |
8322 | if (dev->netdev_ops->ndo_uninit) |
8323 | dev->netdev_ops->ndo_uninit(dev); | |
93ee31f1 | 8324 | |
395eea6c MB |
8325 | if (skb) |
8326 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL); | |
56bfa7ee | 8327 | |
9ff162a8 JP |
8328 | /* Notifier chain MUST detach us all upper devices. */ |
8329 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
0f524a80 | 8330 | WARN_ON(netdev_has_any_lower_dev(dev)); |
93ee31f1 | 8331 | |
9b5e383c ED |
8332 | /* Remove entries from kobject tree */ |
8333 | netdev_unregister_kobject(dev); | |
024e9679 AD |
8334 | #ifdef CONFIG_XPS |
8335 | /* Remove XPS queueing entries */ | |
8336 | netif_reset_xps_queues_gt(dev, 0); | |
8337 | #endif | |
9b5e383c | 8338 | } |
93ee31f1 | 8339 | |
850a545b | 8340 | synchronize_net(); |
395264d5 | 8341 | |
a5ee1551 | 8342 | list_for_each_entry(dev, head, unreg_list) |
9b5e383c ED |
8343 | dev_put(dev); |
8344 | } | |
8345 | ||
8346 | static void rollback_registered(struct net_device *dev) | |
8347 | { | |
8348 | LIST_HEAD(single); | |
8349 | ||
8350 | list_add(&dev->unreg_list, &single); | |
8351 | rollback_registered_many(&single); | |
ceaaec98 | 8352 | list_del(&single); |
93ee31f1 DL |
8353 | } |
8354 | ||
fd867d51 JW |
8355 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
8356 | struct net_device *upper, netdev_features_t features) | |
8357 | { | |
8358 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
8359 | netdev_features_t feature; | |
5ba3f7d6 | 8360 | int feature_bit; |
fd867d51 | 8361 | |
3b89ea9c | 8362 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 8363 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
8364 | if (!(upper->wanted_features & feature) |
8365 | && (features & feature)) { | |
8366 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", | |
8367 | &feature, upper->name); | |
8368 | features &= ~feature; | |
8369 | } | |
8370 | } | |
8371 | ||
8372 | return features; | |
8373 | } | |
8374 | ||
8375 | static void netdev_sync_lower_features(struct net_device *upper, | |
8376 | struct net_device *lower, netdev_features_t features) | |
8377 | { | |
8378 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
8379 | netdev_features_t feature; | |
5ba3f7d6 | 8380 | int feature_bit; |
fd867d51 | 8381 | |
3b89ea9c | 8382 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 8383 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
8384 | if (!(features & feature) && (lower->features & feature)) { |
8385 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", | |
8386 | &feature, lower->name); | |
8387 | lower->wanted_features &= ~feature; | |
8388 | netdev_update_features(lower); | |
8389 | ||
8390 | if (unlikely(lower->features & feature)) | |
8391 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", | |
8392 | &feature, lower->name); | |
8393 | } | |
8394 | } | |
8395 | } | |
8396 | ||
c8f44aff MM |
8397 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
8398 | netdev_features_t features) | |
b63365a2 | 8399 | { |
57422dc5 MM |
8400 | /* Fix illegal checksum combinations */ |
8401 | if ((features & NETIF_F_HW_CSUM) && | |
8402 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 8403 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
8404 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
8405 | } | |
8406 | ||
b63365a2 | 8407 | /* TSO requires that SG is present as well. */ |
ea2d3688 | 8408 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 8409 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 8410 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
8411 | } |
8412 | ||
ec5f0615 PS |
8413 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
8414 | !(features & NETIF_F_IP_CSUM)) { | |
8415 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); | |
8416 | features &= ~NETIF_F_TSO; | |
8417 | features &= ~NETIF_F_TSO_ECN; | |
8418 | } | |
8419 | ||
8420 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && | |
8421 | !(features & NETIF_F_IPV6_CSUM)) { | |
8422 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); | |
8423 | features &= ~NETIF_F_TSO6; | |
8424 | } | |
8425 | ||
b1dc497b AD |
8426 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
8427 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) | |
8428 | features &= ~NETIF_F_TSO_MANGLEID; | |
8429 | ||
31d8b9e0 BH |
8430 | /* TSO ECN requires that TSO is present as well. */ |
8431 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
8432 | features &= ~NETIF_F_TSO_ECN; | |
8433 | ||
212b573f MM |
8434 | /* Software GSO depends on SG. */ |
8435 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 8436 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
8437 | features &= ~NETIF_F_GSO; |
8438 | } | |
8439 | ||
802ab55a AD |
8440 | /* GSO partial features require GSO partial be set */ |
8441 | if ((features & dev->gso_partial_features) && | |
8442 | !(features & NETIF_F_GSO_PARTIAL)) { | |
8443 | netdev_dbg(dev, | |
8444 | "Dropping partially supported GSO features since no GSO partial.\n"); | |
8445 | features &= ~dev->gso_partial_features; | |
8446 | } | |
8447 | ||
fb1f5f79 MC |
8448 | if (!(features & NETIF_F_RXCSUM)) { |
8449 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet | |
8450 | * successfully merged by hardware must also have the | |
8451 | * checksum verified by hardware. If the user does not | |
8452 | * want to enable RXCSUM, logically, we should disable GRO_HW. | |
8453 | */ | |
8454 | if (features & NETIF_F_GRO_HW) { | |
8455 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); | |
8456 | features &= ~NETIF_F_GRO_HW; | |
8457 | } | |
8458 | } | |
8459 | ||
de8d5ab2 GP |
8460 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
8461 | if (features & NETIF_F_RXFCS) { | |
8462 | if (features & NETIF_F_LRO) { | |
8463 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); | |
8464 | features &= ~NETIF_F_LRO; | |
8465 | } | |
8466 | ||
8467 | if (features & NETIF_F_GRO_HW) { | |
8468 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); | |
8469 | features &= ~NETIF_F_GRO_HW; | |
8470 | } | |
e6c6a929 GP |
8471 | } |
8472 | ||
b63365a2 HX |
8473 | return features; |
8474 | } | |
b63365a2 | 8475 | |
6cb6a27c | 8476 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 8477 | { |
fd867d51 | 8478 | struct net_device *upper, *lower; |
c8f44aff | 8479 | netdev_features_t features; |
fd867d51 | 8480 | struct list_head *iter; |
e7868a85 | 8481 | int err = -1; |
5455c699 | 8482 | |
87267485 MM |
8483 | ASSERT_RTNL(); |
8484 | ||
5455c699 MM |
8485 | features = netdev_get_wanted_features(dev); |
8486 | ||
8487 | if (dev->netdev_ops->ndo_fix_features) | |
8488 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
8489 | ||
8490 | /* driver might be less strict about feature dependencies */ | |
8491 | features = netdev_fix_features(dev, features); | |
8492 | ||
fd867d51 JW |
8493 | /* some features can't be enabled if they're off an an upper device */ |
8494 | netdev_for_each_upper_dev_rcu(dev, upper, iter) | |
8495 | features = netdev_sync_upper_features(dev, upper, features); | |
8496 | ||
5455c699 | 8497 | if (dev->features == features) |
e7868a85 | 8498 | goto sync_lower; |
5455c699 | 8499 | |
c8f44aff MM |
8500 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
8501 | &dev->features, &features); | |
5455c699 MM |
8502 | |
8503 | if (dev->netdev_ops->ndo_set_features) | |
8504 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
5f8dc33e NA |
8505 | else |
8506 | err = 0; | |
5455c699 | 8507 | |
6cb6a27c | 8508 | if (unlikely(err < 0)) { |
5455c699 | 8509 | netdev_err(dev, |
c8f44aff MM |
8510 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
8511 | err, &features, &dev->features); | |
17b85d29 NA |
8512 | /* return non-0 since some features might have changed and |
8513 | * it's better to fire a spurious notification than miss it | |
8514 | */ | |
8515 | return -1; | |
6cb6a27c MM |
8516 | } |
8517 | ||
e7868a85 | 8518 | sync_lower: |
fd867d51 JW |
8519 | /* some features must be disabled on lower devices when disabled |
8520 | * on an upper device (think: bonding master or bridge) | |
8521 | */ | |
8522 | netdev_for_each_lower_dev(dev, lower, iter) | |
8523 | netdev_sync_lower_features(dev, lower, features); | |
8524 | ||
ae847f40 SD |
8525 | if (!err) { |
8526 | netdev_features_t diff = features ^ dev->features; | |
8527 | ||
8528 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
8529 | /* udp_tunnel_{get,drop}_rx_info both need | |
8530 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the | |
8531 | * device, or they won't do anything. | |
8532 | * Thus we need to update dev->features | |
8533 | * *before* calling udp_tunnel_get_rx_info, | |
8534 | * but *after* calling udp_tunnel_drop_rx_info. | |
8535 | */ | |
8536 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
8537 | dev->features = features; | |
8538 | udp_tunnel_get_rx_info(dev); | |
8539 | } else { | |
8540 | udp_tunnel_drop_rx_info(dev); | |
8541 | } | |
8542 | } | |
8543 | ||
9daae9bd GP |
8544 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
8545 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { | |
8546 | dev->features = features; | |
8547 | err |= vlan_get_rx_ctag_filter_info(dev); | |
8548 | } else { | |
8549 | vlan_drop_rx_ctag_filter_info(dev); | |
8550 | } | |
8551 | } | |
8552 | ||
8553 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { | |
8554 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { | |
8555 | dev->features = features; | |
8556 | err |= vlan_get_rx_stag_filter_info(dev); | |
8557 | } else { | |
8558 | vlan_drop_rx_stag_filter_info(dev); | |
8559 | } | |
8560 | } | |
8561 | ||
6cb6a27c | 8562 | dev->features = features; |
ae847f40 | 8563 | } |
6cb6a27c | 8564 | |
e7868a85 | 8565 | return err < 0 ? 0 : 1; |
6cb6a27c MM |
8566 | } |
8567 | ||
afe12cc8 MM |
8568 | /** |
8569 | * netdev_update_features - recalculate device features | |
8570 | * @dev: the device to check | |
8571 | * | |
8572 | * Recalculate dev->features set and send notifications if it | |
8573 | * has changed. Should be called after driver or hardware dependent | |
8574 | * conditions might have changed that influence the features. | |
8575 | */ | |
6cb6a27c MM |
8576 | void netdev_update_features(struct net_device *dev) |
8577 | { | |
8578 | if (__netdev_update_features(dev)) | |
8579 | netdev_features_change(dev); | |
5455c699 MM |
8580 | } |
8581 | EXPORT_SYMBOL(netdev_update_features); | |
8582 | ||
afe12cc8 MM |
8583 | /** |
8584 | * netdev_change_features - recalculate device features | |
8585 | * @dev: the device to check | |
8586 | * | |
8587 | * Recalculate dev->features set and send notifications even | |
8588 | * if they have not changed. Should be called instead of | |
8589 | * netdev_update_features() if also dev->vlan_features might | |
8590 | * have changed to allow the changes to be propagated to stacked | |
8591 | * VLAN devices. | |
8592 | */ | |
8593 | void netdev_change_features(struct net_device *dev) | |
8594 | { | |
8595 | __netdev_update_features(dev); | |
8596 | netdev_features_change(dev); | |
8597 | } | |
8598 | EXPORT_SYMBOL(netdev_change_features); | |
8599 | ||
fc4a7489 PM |
8600 | /** |
8601 | * netif_stacked_transfer_operstate - transfer operstate | |
8602 | * @rootdev: the root or lower level device to transfer state from | |
8603 | * @dev: the device to transfer operstate to | |
8604 | * | |
8605 | * Transfer operational state from root to device. This is normally | |
8606 | * called when a stacking relationship exists between the root | |
8607 | * device and the device(a leaf device). | |
8608 | */ | |
8609 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
8610 | struct net_device *dev) | |
8611 | { | |
8612 | if (rootdev->operstate == IF_OPER_DORMANT) | |
8613 | netif_dormant_on(dev); | |
8614 | else | |
8615 | netif_dormant_off(dev); | |
8616 | ||
0575c86b ZS |
8617 | if (netif_carrier_ok(rootdev)) |
8618 | netif_carrier_on(dev); | |
8619 | else | |
8620 | netif_carrier_off(dev); | |
fc4a7489 PM |
8621 | } |
8622 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
8623 | ||
1b4bf461 ED |
8624 | static int netif_alloc_rx_queues(struct net_device *dev) |
8625 | { | |
1b4bf461 | 8626 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 8627 | struct netdev_rx_queue *rx; |
10595902 | 8628 | size_t sz = count * sizeof(*rx); |
e817f856 | 8629 | int err = 0; |
1b4bf461 | 8630 | |
bd25fa7b | 8631 | BUG_ON(count < 1); |
1b4bf461 | 8632 | |
dcda9b04 | 8633 | rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
8634 | if (!rx) |
8635 | return -ENOMEM; | |
8636 | ||
bd25fa7b TH |
8637 | dev->_rx = rx; |
8638 | ||
e817f856 | 8639 | for (i = 0; i < count; i++) { |
fe822240 | 8640 | rx[i].dev = dev; |
e817f856 JDB |
8641 | |
8642 | /* XDP RX-queue setup */ | |
8643 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i); | |
8644 | if (err < 0) | |
8645 | goto err_rxq_info; | |
8646 | } | |
1b4bf461 | 8647 | return 0; |
e817f856 JDB |
8648 | |
8649 | err_rxq_info: | |
8650 | /* Rollback successful reg's and free other resources */ | |
8651 | while (i--) | |
8652 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); | |
141b52a9 | 8653 | kvfree(dev->_rx); |
e817f856 JDB |
8654 | dev->_rx = NULL; |
8655 | return err; | |
8656 | } | |
8657 | ||
8658 | static void netif_free_rx_queues(struct net_device *dev) | |
8659 | { | |
8660 | unsigned int i, count = dev->num_rx_queues; | |
e817f856 JDB |
8661 | |
8662 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ | |
8663 | if (!dev->_rx) | |
8664 | return; | |
8665 | ||
e817f856 | 8666 | for (i = 0; i < count; i++) |
82aaff2f JK |
8667 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
8668 | ||
8669 | kvfree(dev->_rx); | |
1b4bf461 ED |
8670 | } |
8671 | ||
aa942104 CG |
8672 | static void netdev_init_one_queue(struct net_device *dev, |
8673 | struct netdev_queue *queue, void *_unused) | |
8674 | { | |
8675 | /* Initialize queue lock */ | |
8676 | spin_lock_init(&queue->_xmit_lock); | |
8677 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); | |
8678 | queue->xmit_lock_owner = -1; | |
b236da69 | 8679 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 8680 | queue->dev = dev; |
114cf580 TH |
8681 | #ifdef CONFIG_BQL |
8682 | dql_init(&queue->dql, HZ); | |
8683 | #endif | |
aa942104 CG |
8684 | } |
8685 | ||
60877a32 ED |
8686 | static void netif_free_tx_queues(struct net_device *dev) |
8687 | { | |
4cb28970 | 8688 | kvfree(dev->_tx); |
60877a32 ED |
8689 | } |
8690 | ||
e6484930 TH |
8691 | static int netif_alloc_netdev_queues(struct net_device *dev) |
8692 | { | |
8693 | unsigned int count = dev->num_tx_queues; | |
8694 | struct netdev_queue *tx; | |
60877a32 | 8695 | size_t sz = count * sizeof(*tx); |
e6484930 | 8696 | |
d339727c ED |
8697 | if (count < 1 || count > 0xffff) |
8698 | return -EINVAL; | |
62b5942a | 8699 | |
dcda9b04 | 8700 | tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
8701 | if (!tx) |
8702 | return -ENOMEM; | |
8703 | ||
e6484930 | 8704 | dev->_tx = tx; |
1d24eb48 | 8705 | |
e6484930 TH |
8706 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
8707 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
8708 | |
8709 | return 0; | |
e6484930 TH |
8710 | } |
8711 | ||
a2029240 DV |
8712 | void netif_tx_stop_all_queues(struct net_device *dev) |
8713 | { | |
8714 | unsigned int i; | |
8715 | ||
8716 | for (i = 0; i < dev->num_tx_queues; i++) { | |
8717 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
f4563a75 | 8718 | |
a2029240 DV |
8719 | netif_tx_stop_queue(txq); |
8720 | } | |
8721 | } | |
8722 | EXPORT_SYMBOL(netif_tx_stop_all_queues); | |
8723 | ||
1da177e4 LT |
8724 | /** |
8725 | * register_netdevice - register a network device | |
8726 | * @dev: device to register | |
8727 | * | |
8728 | * Take a completed network device structure and add it to the kernel | |
8729 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
8730 | * chain. 0 is returned on success. A negative errno code is returned | |
8731 | * on a failure to set up the device, or if the name is a duplicate. | |
8732 | * | |
8733 | * Callers must hold the rtnl semaphore. You may want | |
8734 | * register_netdev() instead of this. | |
8735 | * | |
8736 | * BUGS: | |
8737 | * The locking appears insufficient to guarantee two parallel registers | |
8738 | * will not get the same name. | |
8739 | */ | |
8740 | ||
8741 | int register_netdevice(struct net_device *dev) | |
8742 | { | |
1da177e4 | 8743 | int ret; |
d314774c | 8744 | struct net *net = dev_net(dev); |
1da177e4 | 8745 | |
e283de3a FF |
8746 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
8747 | NETDEV_FEATURE_COUNT); | |
1da177e4 LT |
8748 | BUG_ON(dev_boot_phase); |
8749 | ASSERT_RTNL(); | |
8750 | ||
b17a7c17 SH |
8751 | might_sleep(); |
8752 | ||
1da177e4 LT |
8753 | /* When net_device's are persistent, this will be fatal. */ |
8754 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 8755 | BUG_ON(!net); |
1da177e4 | 8756 | |
f1f28aa3 | 8757 | spin_lock_init(&dev->addr_list_lock); |
cf508b12 | 8758 | netdev_set_addr_lockdep_class(dev); |
1da177e4 | 8759 | |
828de4f6 | 8760 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
8761 | if (ret < 0) |
8762 | goto out; | |
8763 | ||
ff927412 JP |
8764 | dev->name_node = netdev_name_node_head_alloc(dev); |
8765 | if (!dev->name_node) | |
8766 | goto out; | |
8767 | ||
1da177e4 | 8768 | /* Init, if this function is available */ |
d314774c SH |
8769 | if (dev->netdev_ops->ndo_init) { |
8770 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
8771 | if (ret) { |
8772 | if (ret > 0) | |
8773 | ret = -EIO; | |
90833aa4 | 8774 | goto out; |
1da177e4 LT |
8775 | } |
8776 | } | |
4ec93edb | 8777 | |
f646968f PM |
8778 | if (((dev->hw_features | dev->features) & |
8779 | NETIF_F_HW_VLAN_CTAG_FILTER) && | |
d2ed273d MM |
8780 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
8781 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { | |
8782 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); | |
8783 | ret = -EINVAL; | |
8784 | goto err_uninit; | |
8785 | } | |
8786 | ||
9c7dafbf PE |
8787 | ret = -EBUSY; |
8788 | if (!dev->ifindex) | |
8789 | dev->ifindex = dev_new_index(net); | |
8790 | else if (__dev_get_by_index(net, dev->ifindex)) | |
8791 | goto err_uninit; | |
8792 | ||
5455c699 MM |
8793 | /* Transfer changeable features to wanted_features and enable |
8794 | * software offloads (GSO and GRO). | |
8795 | */ | |
8796 | dev->hw_features |= NETIF_F_SOFT_FEATURES; | |
14d1232f | 8797 | dev->features |= NETIF_F_SOFT_FEATURES; |
d764a122 SD |
8798 | |
8799 | if (dev->netdev_ops->ndo_udp_tunnel_add) { | |
8800 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
8801 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
8802 | } | |
8803 | ||
14d1232f | 8804 | dev->wanted_features = dev->features & dev->hw_features; |
1da177e4 | 8805 | |
cbc53e08 | 8806 | if (!(dev->flags & IFF_LOOPBACK)) |
34324dc2 | 8807 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
cbc53e08 | 8808 | |
7f348a60 AD |
8809 | /* If IPv4 TCP segmentation offload is supported we should also |
8810 | * allow the device to enable segmenting the frame with the option | |
8811 | * of ignoring a static IP ID value. This doesn't enable the | |
8812 | * feature itself but allows the user to enable it later. | |
8813 | */ | |
cbc53e08 AD |
8814 | if (dev->hw_features & NETIF_F_TSO) |
8815 | dev->hw_features |= NETIF_F_TSO_MANGLEID; | |
7f348a60 AD |
8816 | if (dev->vlan_features & NETIF_F_TSO) |
8817 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; | |
8818 | if (dev->mpls_features & NETIF_F_TSO) | |
8819 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; | |
8820 | if (dev->hw_enc_features & NETIF_F_TSO) | |
8821 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; | |
c6e1a0d1 | 8822 | |
1180e7d6 | 8823 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 8824 | */ |
1180e7d6 | 8825 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 8826 | |
ee579677 PS |
8827 | /* Make NETIF_F_SG inheritable to tunnel devices. |
8828 | */ | |
802ab55a | 8829 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
ee579677 | 8830 | |
0d89d203 SH |
8831 | /* Make NETIF_F_SG inheritable to MPLS. |
8832 | */ | |
8833 | dev->mpls_features |= NETIF_F_SG; | |
8834 | ||
7ffbe3fd JB |
8835 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
8836 | ret = notifier_to_errno(ret); | |
8837 | if (ret) | |
8838 | goto err_uninit; | |
8839 | ||
8b41d188 | 8840 | ret = netdev_register_kobject(dev); |
b17a7c17 | 8841 | if (ret) |
7ce1b0ed | 8842 | goto err_uninit; |
b17a7c17 SH |
8843 | dev->reg_state = NETREG_REGISTERED; |
8844 | ||
6cb6a27c | 8845 | __netdev_update_features(dev); |
8e9b59b2 | 8846 | |
1da177e4 LT |
8847 | /* |
8848 | * Default initial state at registry is that the | |
8849 | * device is present. | |
8850 | */ | |
8851 | ||
8852 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
8853 | ||
8f4cccbb BH |
8854 | linkwatch_init_dev(dev); |
8855 | ||
1da177e4 | 8856 | dev_init_scheduler(dev); |
1da177e4 | 8857 | dev_hold(dev); |
ce286d32 | 8858 | list_netdevice(dev); |
7bf23575 | 8859 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 8860 | |
948b337e JP |
8861 | /* If the device has permanent device address, driver should |
8862 | * set dev_addr and also addr_assign_type should be set to | |
8863 | * NET_ADDR_PERM (default value). | |
8864 | */ | |
8865 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
8866 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
8867 | ||
1da177e4 | 8868 | /* Notify protocols, that a new device appeared. */ |
056925ab | 8869 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 8870 | ret = notifier_to_errno(ret); |
93ee31f1 DL |
8871 | if (ret) { |
8872 | rollback_registered(dev); | |
10cc514f SAK |
8873 | rcu_barrier(); |
8874 | ||
93ee31f1 DL |
8875 | dev->reg_state = NETREG_UNREGISTERED; |
8876 | } | |
d90a909e EB |
8877 | /* |
8878 | * Prevent userspace races by waiting until the network | |
8879 | * device is fully setup before sending notifications. | |
8880 | */ | |
a2835763 PM |
8881 | if (!dev->rtnl_link_ops || |
8882 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
7f294054 | 8883 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
1da177e4 LT |
8884 | |
8885 | out: | |
8886 | return ret; | |
7ce1b0ed HX |
8887 | |
8888 | err_uninit: | |
ff927412 JP |
8889 | if (dev->name_node) |
8890 | netdev_name_node_free(dev->name_node); | |
d314774c SH |
8891 | if (dev->netdev_ops->ndo_uninit) |
8892 | dev->netdev_ops->ndo_uninit(dev); | |
cf124db5 DM |
8893 | if (dev->priv_destructor) |
8894 | dev->priv_destructor(dev); | |
7ce1b0ed | 8895 | goto out; |
1da177e4 | 8896 | } |
d1b19dff | 8897 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 8898 | |
937f1ba5 BH |
8899 | /** |
8900 | * init_dummy_netdev - init a dummy network device for NAPI | |
8901 | * @dev: device to init | |
8902 | * | |
8903 | * This takes a network device structure and initialize the minimum | |
8904 | * amount of fields so it can be used to schedule NAPI polls without | |
8905 | * registering a full blown interface. This is to be used by drivers | |
8906 | * that need to tie several hardware interfaces to a single NAPI | |
8907 | * poll scheduler due to HW limitations. | |
8908 | */ | |
8909 | int init_dummy_netdev(struct net_device *dev) | |
8910 | { | |
8911 | /* Clear everything. Note we don't initialize spinlocks | |
8912 | * are they aren't supposed to be taken by any of the | |
8913 | * NAPI code and this dummy netdev is supposed to be | |
8914 | * only ever used for NAPI polls | |
8915 | */ | |
8916 | memset(dev, 0, sizeof(struct net_device)); | |
8917 | ||
8918 | /* make sure we BUG if trying to hit standard | |
8919 | * register/unregister code path | |
8920 | */ | |
8921 | dev->reg_state = NETREG_DUMMY; | |
8922 | ||
937f1ba5 BH |
8923 | /* NAPI wants this */ |
8924 | INIT_LIST_HEAD(&dev->napi_list); | |
8925 | ||
8926 | /* a dummy interface is started by default */ | |
8927 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
8928 | set_bit(__LINK_STATE_START, &dev->state); | |
8929 | ||
35edfdc7 JE |
8930 | /* napi_busy_loop stats accounting wants this */ |
8931 | dev_net_set(dev, &init_net); | |
8932 | ||
29b4433d ED |
8933 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
8934 | * because users of this 'device' dont need to change | |
8935 | * its refcount. | |
8936 | */ | |
8937 | ||
937f1ba5 BH |
8938 | return 0; |
8939 | } | |
8940 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
8941 | ||
8942 | ||
1da177e4 LT |
8943 | /** |
8944 | * register_netdev - register a network device | |
8945 | * @dev: device to register | |
8946 | * | |
8947 | * Take a completed network device structure and add it to the kernel | |
8948 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
8949 | * chain. 0 is returned on success. A negative errno code is returned | |
8950 | * on a failure to set up the device, or if the name is a duplicate. | |
8951 | * | |
38b4da38 | 8952 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
8953 | * and expands the device name if you passed a format string to |
8954 | * alloc_netdev. | |
8955 | */ | |
8956 | int register_netdev(struct net_device *dev) | |
8957 | { | |
8958 | int err; | |
8959 | ||
b0f3debc KT |
8960 | if (rtnl_lock_killable()) |
8961 | return -EINTR; | |
1da177e4 | 8962 | err = register_netdevice(dev); |
1da177e4 LT |
8963 | rtnl_unlock(); |
8964 | return err; | |
8965 | } | |
8966 | EXPORT_SYMBOL(register_netdev); | |
8967 | ||
29b4433d ED |
8968 | int netdev_refcnt_read(const struct net_device *dev) |
8969 | { | |
8970 | int i, refcnt = 0; | |
8971 | ||
8972 | for_each_possible_cpu(i) | |
8973 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
8974 | return refcnt; | |
8975 | } | |
8976 | EXPORT_SYMBOL(netdev_refcnt_read); | |
8977 | ||
2c53040f | 8978 | /** |
1da177e4 | 8979 | * netdev_wait_allrefs - wait until all references are gone. |
3de7a37b | 8980 | * @dev: target net_device |
1da177e4 LT |
8981 | * |
8982 | * This is called when unregistering network devices. | |
8983 | * | |
8984 | * Any protocol or device that holds a reference should register | |
8985 | * for netdevice notification, and cleanup and put back the | |
8986 | * reference if they receive an UNREGISTER event. | |
8987 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 8988 | * call dev_put. |
1da177e4 LT |
8989 | */ |
8990 | static void netdev_wait_allrefs(struct net_device *dev) | |
8991 | { | |
8992 | unsigned long rebroadcast_time, warning_time; | |
29b4433d | 8993 | int refcnt; |
1da177e4 | 8994 | |
e014debe ED |
8995 | linkwatch_forget_dev(dev); |
8996 | ||
1da177e4 | 8997 | rebroadcast_time = warning_time = jiffies; |
29b4433d ED |
8998 | refcnt = netdev_refcnt_read(dev); |
8999 | ||
9000 | while (refcnt != 0) { | |
1da177e4 | 9001 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 9002 | rtnl_lock(); |
1da177e4 LT |
9003 | |
9004 | /* Rebroadcast unregister notification */ | |
056925ab | 9005 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
1da177e4 | 9006 | |
748e2d93 | 9007 | __rtnl_unlock(); |
0115e8e3 | 9008 | rcu_barrier(); |
748e2d93 ED |
9009 | rtnl_lock(); |
9010 | ||
1da177e4 LT |
9011 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
9012 | &dev->state)) { | |
9013 | /* We must not have linkwatch events | |
9014 | * pending on unregister. If this | |
9015 | * happens, we simply run the queue | |
9016 | * unscheduled, resulting in a noop | |
9017 | * for this device. | |
9018 | */ | |
9019 | linkwatch_run_queue(); | |
9020 | } | |
9021 | ||
6756ae4b | 9022 | __rtnl_unlock(); |
1da177e4 LT |
9023 | |
9024 | rebroadcast_time = jiffies; | |
9025 | } | |
9026 | ||
9027 | msleep(250); | |
9028 | ||
29b4433d ED |
9029 | refcnt = netdev_refcnt_read(dev); |
9030 | ||
d7c04b05 | 9031 | if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) { |
7b6cd1ce JP |
9032 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
9033 | dev->name, refcnt); | |
1da177e4 LT |
9034 | warning_time = jiffies; |
9035 | } | |
9036 | } | |
9037 | } | |
9038 | ||
9039 | /* The sequence is: | |
9040 | * | |
9041 | * rtnl_lock(); | |
9042 | * ... | |
9043 | * register_netdevice(x1); | |
9044 | * register_netdevice(x2); | |
9045 | * ... | |
9046 | * unregister_netdevice(y1); | |
9047 | * unregister_netdevice(y2); | |
9048 | * ... | |
9049 | * rtnl_unlock(); | |
9050 | * free_netdev(y1); | |
9051 | * free_netdev(y2); | |
9052 | * | |
58ec3b4d | 9053 | * We are invoked by rtnl_unlock(). |
1da177e4 | 9054 | * This allows us to deal with problems: |
b17a7c17 | 9055 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
9056 | * without deadlocking with linkwatch via keventd. |
9057 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
9058 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
9059 | * |
9060 | * We must not return until all unregister events added during | |
9061 | * the interval the lock was held have been completed. | |
1da177e4 | 9062 | */ |
1da177e4 LT |
9063 | void netdev_run_todo(void) |
9064 | { | |
626ab0e6 | 9065 | struct list_head list; |
1da177e4 | 9066 | |
1da177e4 | 9067 | /* Snapshot list, allow later requests */ |
626ab0e6 | 9068 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
9069 | |
9070 | __rtnl_unlock(); | |
626ab0e6 | 9071 | |
0115e8e3 ED |
9072 | |
9073 | /* Wait for rcu callbacks to finish before next phase */ | |
850a545b EB |
9074 | if (!list_empty(&list)) |
9075 | rcu_barrier(); | |
9076 | ||
1da177e4 LT |
9077 | while (!list_empty(&list)) { |
9078 | struct net_device *dev | |
e5e26d75 | 9079 | = list_first_entry(&list, struct net_device, todo_list); |
1da177e4 LT |
9080 | list_del(&dev->todo_list); |
9081 | ||
b17a7c17 | 9082 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
7b6cd1ce | 9083 | pr_err("network todo '%s' but state %d\n", |
b17a7c17 SH |
9084 | dev->name, dev->reg_state); |
9085 | dump_stack(); | |
9086 | continue; | |
9087 | } | |
1da177e4 | 9088 | |
b17a7c17 | 9089 | dev->reg_state = NETREG_UNREGISTERED; |
1da177e4 | 9090 | |
b17a7c17 | 9091 | netdev_wait_allrefs(dev); |
1da177e4 | 9092 | |
b17a7c17 | 9093 | /* paranoia */ |
29b4433d | 9094 | BUG_ON(netdev_refcnt_read(dev)); |
7866a621 SN |
9095 | BUG_ON(!list_empty(&dev->ptype_all)); |
9096 | BUG_ON(!list_empty(&dev->ptype_specific)); | |
33d480ce ED |
9097 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
9098 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
330c7272 | 9099 | #if IS_ENABLED(CONFIG_DECNET) |
547b792c | 9100 | WARN_ON(dev->dn_ptr); |
330c7272 | 9101 | #endif |
cf124db5 DM |
9102 | if (dev->priv_destructor) |
9103 | dev->priv_destructor(dev); | |
9104 | if (dev->needs_free_netdev) | |
9105 | free_netdev(dev); | |
9093bbb2 | 9106 | |
50624c93 EB |
9107 | /* Report a network device has been unregistered */ |
9108 | rtnl_lock(); | |
9109 | dev_net(dev)->dev_unreg_count--; | |
9110 | __rtnl_unlock(); | |
9111 | wake_up(&netdev_unregistering_wq); | |
9112 | ||
9093bbb2 SH |
9113 | /* Free network device */ |
9114 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 9115 | } |
1da177e4 LT |
9116 | } |
9117 | ||
9256645a JW |
9118 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
9119 | * all the same fields in the same order as net_device_stats, with only | |
9120 | * the type differing, but rtnl_link_stats64 may have additional fields | |
9121 | * at the end for newer counters. | |
3cfde79c | 9122 | */ |
77a1abf5 ED |
9123 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
9124 | const struct net_device_stats *netdev_stats) | |
3cfde79c BH |
9125 | { |
9126 | #if BITS_PER_LONG == 64 | |
9256645a | 9127 | BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats)); |
9af9959e | 9128 | memcpy(stats64, netdev_stats, sizeof(*netdev_stats)); |
9256645a JW |
9129 | /* zero out counters that only exist in rtnl_link_stats64 */ |
9130 | memset((char *)stats64 + sizeof(*netdev_stats), 0, | |
9131 | sizeof(*stats64) - sizeof(*netdev_stats)); | |
3cfde79c | 9132 | #else |
9256645a | 9133 | size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long); |
3cfde79c BH |
9134 | const unsigned long *src = (const unsigned long *)netdev_stats; |
9135 | u64 *dst = (u64 *)stats64; | |
9136 | ||
9256645a | 9137 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
3cfde79c BH |
9138 | for (i = 0; i < n; i++) |
9139 | dst[i] = src[i]; | |
9256645a JW |
9140 | /* zero out counters that only exist in rtnl_link_stats64 */ |
9141 | memset((char *)stats64 + n * sizeof(u64), 0, | |
9142 | sizeof(*stats64) - n * sizeof(u64)); | |
3cfde79c BH |
9143 | #endif |
9144 | } | |
77a1abf5 | 9145 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 9146 | |
eeda3fd6 SH |
9147 | /** |
9148 | * dev_get_stats - get network device statistics | |
9149 | * @dev: device to get statistics from | |
28172739 | 9150 | * @storage: place to store stats |
eeda3fd6 | 9151 | * |
d7753516 BH |
9152 | * Get network statistics from device. Return @storage. |
9153 | * The device driver may provide its own method by setting | |
9154 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
9155 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 9156 | */ |
d7753516 BH |
9157 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
9158 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 9159 | { |
eeda3fd6 SH |
9160 | const struct net_device_ops *ops = dev->netdev_ops; |
9161 | ||
28172739 ED |
9162 | if (ops->ndo_get_stats64) { |
9163 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
9164 | ops->ndo_get_stats64(dev, storage); |
9165 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 9166 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
caf586e5 ED |
9167 | } else { |
9168 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 9169 | } |
6f64ec74 ED |
9170 | storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped); |
9171 | storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped); | |
9172 | storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler); | |
28172739 | 9173 | return storage; |
c45d286e | 9174 | } |
eeda3fd6 | 9175 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 9176 | |
24824a09 | 9177 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 9178 | { |
24824a09 | 9179 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 9180 | |
24824a09 ED |
9181 | #ifdef CONFIG_NET_CLS_ACT |
9182 | if (queue) | |
9183 | return queue; | |
9184 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
9185 | if (!queue) | |
9186 | return NULL; | |
9187 | netdev_init_one_queue(dev, queue, NULL); | |
2ce1ee17 | 9188 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
24824a09 ED |
9189 | queue->qdisc_sleeping = &noop_qdisc; |
9190 | rcu_assign_pointer(dev->ingress_queue, queue); | |
9191 | #endif | |
9192 | return queue; | |
bb949fbd DM |
9193 | } |
9194 | ||
2c60db03 ED |
9195 | static const struct ethtool_ops default_ethtool_ops; |
9196 | ||
d07d7507 SG |
9197 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
9198 | const struct ethtool_ops *ops) | |
9199 | { | |
9200 | if (dev->ethtool_ops == &default_ethtool_ops) | |
9201 | dev->ethtool_ops = ops; | |
9202 | } | |
9203 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
9204 | ||
74d332c1 ED |
9205 | void netdev_freemem(struct net_device *dev) |
9206 | { | |
9207 | char *addr = (char *)dev - dev->padded; | |
9208 | ||
4cb28970 | 9209 | kvfree(addr); |
74d332c1 ED |
9210 | } |
9211 | ||
1da177e4 | 9212 | /** |
722c9a0c | 9213 | * alloc_netdev_mqs - allocate network device |
9214 | * @sizeof_priv: size of private data to allocate space for | |
9215 | * @name: device name format string | |
9216 | * @name_assign_type: origin of device name | |
9217 | * @setup: callback to initialize device | |
9218 | * @txqs: the number of TX subqueues to allocate | |
9219 | * @rxqs: the number of RX subqueues to allocate | |
9220 | * | |
9221 | * Allocates a struct net_device with private data area for driver use | |
9222 | * and performs basic initialization. Also allocates subqueue structs | |
9223 | * for each queue on the device. | |
1da177e4 | 9224 | */ |
36909ea4 | 9225 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
c835a677 | 9226 | unsigned char name_assign_type, |
36909ea4 TH |
9227 | void (*setup)(struct net_device *), |
9228 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 9229 | { |
1da177e4 | 9230 | struct net_device *dev; |
52a59bd5 | 9231 | unsigned int alloc_size; |
1ce8e7b5 | 9232 | struct net_device *p; |
1da177e4 | 9233 | |
b6fe17d6 SH |
9234 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
9235 | ||
36909ea4 | 9236 | if (txqs < 1) { |
7b6cd1ce | 9237 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
9238 | return NULL; |
9239 | } | |
9240 | ||
36909ea4 | 9241 | if (rxqs < 1) { |
7b6cd1ce | 9242 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
9243 | return NULL; |
9244 | } | |
36909ea4 | 9245 | |
fd2ea0a7 | 9246 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
9247 | if (sizeof_priv) { |
9248 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 9249 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
9250 | alloc_size += sizeof_priv; |
9251 | } | |
9252 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 9253 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 9254 | |
dcda9b04 | 9255 | p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
62b5942a | 9256 | if (!p) |
1da177e4 | 9257 | return NULL; |
1da177e4 | 9258 | |
1ce8e7b5 | 9259 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 9260 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 9261 | |
29b4433d ED |
9262 | dev->pcpu_refcnt = alloc_percpu(int); |
9263 | if (!dev->pcpu_refcnt) | |
74d332c1 | 9264 | goto free_dev; |
ab9c73cc | 9265 | |
ab9c73cc | 9266 | if (dev_addr_init(dev)) |
29b4433d | 9267 | goto free_pcpu; |
ab9c73cc | 9268 | |
22bedad3 | 9269 | dev_mc_init(dev); |
a748ee24 | 9270 | dev_uc_init(dev); |
ccffad25 | 9271 | |
c346dca1 | 9272 | dev_net_set(dev, &init_net); |
1da177e4 | 9273 | |
8d3bdbd5 | 9274 | dev->gso_max_size = GSO_MAX_SIZE; |
30b678d8 | 9275 | dev->gso_max_segs = GSO_MAX_SEGS; |
8d3bdbd5 | 9276 | |
8d3bdbd5 DM |
9277 | INIT_LIST_HEAD(&dev->napi_list); |
9278 | INIT_LIST_HEAD(&dev->unreg_list); | |
5cde2829 | 9279 | INIT_LIST_HEAD(&dev->close_list); |
8d3bdbd5 | 9280 | INIT_LIST_HEAD(&dev->link_watch_list); |
2f268f12 VF |
9281 | INIT_LIST_HEAD(&dev->adj_list.upper); |
9282 | INIT_LIST_HEAD(&dev->adj_list.lower); | |
7866a621 SN |
9283 | INIT_LIST_HEAD(&dev->ptype_all); |
9284 | INIT_LIST_HEAD(&dev->ptype_specific); | |
59cc1f61 JK |
9285 | #ifdef CONFIG_NET_SCHED |
9286 | hash_init(dev->qdisc_hash); | |
9287 | #endif | |
02875878 | 9288 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
8d3bdbd5 DM |
9289 | setup(dev); |
9290 | ||
a813104d | 9291 | if (!dev->tx_queue_len) { |
f84bb1ea | 9292 | dev->priv_flags |= IFF_NO_QUEUE; |
11597084 | 9293 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
a813104d | 9294 | } |
906470c1 | 9295 | |
36909ea4 TH |
9296 | dev->num_tx_queues = txqs; |
9297 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 9298 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 9299 | goto free_all; |
e8a0464c | 9300 | |
36909ea4 TH |
9301 | dev->num_rx_queues = rxqs; |
9302 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 9303 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 9304 | goto free_all; |
0a9627f2 | 9305 | |
1da177e4 | 9306 | strcpy(dev->name, name); |
c835a677 | 9307 | dev->name_assign_type = name_assign_type; |
cbda10fa | 9308 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
9309 | if (!dev->ethtool_ops) |
9310 | dev->ethtool_ops = &default_ethtool_ops; | |
e687ad60 PN |
9311 | |
9312 | nf_hook_ingress_init(dev); | |
9313 | ||
1da177e4 | 9314 | return dev; |
ab9c73cc | 9315 | |
8d3bdbd5 DM |
9316 | free_all: |
9317 | free_netdev(dev); | |
9318 | return NULL; | |
9319 | ||
29b4433d ED |
9320 | free_pcpu: |
9321 | free_percpu(dev->pcpu_refcnt); | |
74d332c1 ED |
9322 | free_dev: |
9323 | netdev_freemem(dev); | |
ab9c73cc | 9324 | return NULL; |
1da177e4 | 9325 | } |
36909ea4 | 9326 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
9327 | |
9328 | /** | |
722c9a0c | 9329 | * free_netdev - free network device |
9330 | * @dev: device | |
1da177e4 | 9331 | * |
722c9a0c | 9332 | * This function does the last stage of destroying an allocated device |
9333 | * interface. The reference to the device object is released. If this | |
9334 | * is the last reference then it will be freed.Must be called in process | |
9335 | * context. | |
1da177e4 LT |
9336 | */ |
9337 | void free_netdev(struct net_device *dev) | |
9338 | { | |
d565b0a1 HX |
9339 | struct napi_struct *p, *n; |
9340 | ||
93d05d4a | 9341 | might_sleep(); |
60877a32 | 9342 | netif_free_tx_queues(dev); |
e817f856 | 9343 | netif_free_rx_queues(dev); |
e8a0464c | 9344 | |
33d480ce | 9345 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 9346 | |
f001fde5 JP |
9347 | /* Flush device addresses */ |
9348 | dev_addr_flush(dev); | |
9349 | ||
d565b0a1 HX |
9350 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
9351 | netif_napi_del(p); | |
9352 | ||
29b4433d ED |
9353 | free_percpu(dev->pcpu_refcnt); |
9354 | dev->pcpu_refcnt = NULL; | |
9355 | ||
3041a069 | 9356 | /* Compatibility with error handling in drivers */ |
1da177e4 | 9357 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
74d332c1 | 9358 | netdev_freemem(dev); |
1da177e4 LT |
9359 | return; |
9360 | } | |
9361 | ||
9362 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
9363 | dev->reg_state = NETREG_RELEASED; | |
9364 | ||
43cb76d9 GKH |
9365 | /* will free via device release */ |
9366 | put_device(&dev->dev); | |
1da177e4 | 9367 | } |
d1b19dff | 9368 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 9369 | |
f0db275a SH |
9370 | /** |
9371 | * synchronize_net - Synchronize with packet receive processing | |
9372 | * | |
9373 | * Wait for packets currently being received to be done. | |
9374 | * Does not block later packets from starting. | |
9375 | */ | |
4ec93edb | 9376 | void synchronize_net(void) |
1da177e4 LT |
9377 | { |
9378 | might_sleep(); | |
be3fc413 ED |
9379 | if (rtnl_is_locked()) |
9380 | synchronize_rcu_expedited(); | |
9381 | else | |
9382 | synchronize_rcu(); | |
1da177e4 | 9383 | } |
d1b19dff | 9384 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
9385 | |
9386 | /** | |
44a0873d | 9387 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 9388 | * @dev: device |
44a0873d | 9389 | * @head: list |
6ebfbc06 | 9390 | * |
1da177e4 | 9391 | * This function shuts down a device interface and removes it |
d59b54b1 | 9392 | * from the kernel tables. |
44a0873d | 9393 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
9394 | * |
9395 | * Callers must hold the rtnl semaphore. You may want | |
9396 | * unregister_netdev() instead of this. | |
9397 | */ | |
9398 | ||
44a0873d | 9399 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 9400 | { |
a6620712 HX |
9401 | ASSERT_RTNL(); |
9402 | ||
44a0873d | 9403 | if (head) { |
9fdce099 | 9404 | list_move_tail(&dev->unreg_list, head); |
44a0873d ED |
9405 | } else { |
9406 | rollback_registered(dev); | |
9407 | /* Finish processing unregister after unlock */ | |
9408 | net_set_todo(dev); | |
9409 | } | |
1da177e4 | 9410 | } |
44a0873d | 9411 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 9412 | |
9b5e383c ED |
9413 | /** |
9414 | * unregister_netdevice_many - unregister many devices | |
9415 | * @head: list of devices | |
87757a91 ED |
9416 | * |
9417 | * Note: As most callers use a stack allocated list_head, | |
9418 | * we force a list_del() to make sure stack wont be corrupted later. | |
9b5e383c ED |
9419 | */ |
9420 | void unregister_netdevice_many(struct list_head *head) | |
9421 | { | |
9422 | struct net_device *dev; | |
9423 | ||
9424 | if (!list_empty(head)) { | |
9425 | rollback_registered_many(head); | |
9426 | list_for_each_entry(dev, head, unreg_list) | |
9427 | net_set_todo(dev); | |
87757a91 | 9428 | list_del(head); |
9b5e383c ED |
9429 | } |
9430 | } | |
63c8099d | 9431 | EXPORT_SYMBOL(unregister_netdevice_many); |
9b5e383c | 9432 | |
1da177e4 LT |
9433 | /** |
9434 | * unregister_netdev - remove device from the kernel | |
9435 | * @dev: device | |
9436 | * | |
9437 | * This function shuts down a device interface and removes it | |
d59b54b1 | 9438 | * from the kernel tables. |
1da177e4 LT |
9439 | * |
9440 | * This is just a wrapper for unregister_netdevice that takes | |
9441 | * the rtnl semaphore. In general you want to use this and not | |
9442 | * unregister_netdevice. | |
9443 | */ | |
9444 | void unregister_netdev(struct net_device *dev) | |
9445 | { | |
9446 | rtnl_lock(); | |
9447 | unregister_netdevice(dev); | |
9448 | rtnl_unlock(); | |
9449 | } | |
1da177e4 LT |
9450 | EXPORT_SYMBOL(unregister_netdev); |
9451 | ||
ce286d32 EB |
9452 | /** |
9453 | * dev_change_net_namespace - move device to different nethost namespace | |
9454 | * @dev: device | |
9455 | * @net: network namespace | |
9456 | * @pat: If not NULL name pattern to try if the current device name | |
9457 | * is already taken in the destination network namespace. | |
9458 | * | |
9459 | * This function shuts down a device interface and moves it | |
9460 | * to a new network namespace. On success 0 is returned, on | |
9461 | * a failure a netagive errno code is returned. | |
9462 | * | |
9463 | * Callers must hold the rtnl semaphore. | |
9464 | */ | |
9465 | ||
9466 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) | |
9467 | { | |
38e01b30 | 9468 | int err, new_nsid, new_ifindex; |
ce286d32 EB |
9469 | |
9470 | ASSERT_RTNL(); | |
9471 | ||
9472 | /* Don't allow namespace local devices to be moved. */ | |
9473 | err = -EINVAL; | |
9474 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
9475 | goto out; | |
9476 | ||
9477 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
9478 | if (dev->reg_state != NETREG_REGISTERED) |
9479 | goto out; | |
9480 | ||
9481 | /* Get out if there is nothing todo */ | |
9482 | err = 0; | |
878628fb | 9483 | if (net_eq(dev_net(dev), net)) |
ce286d32 EB |
9484 | goto out; |
9485 | ||
9486 | /* Pick the destination device name, and ensure | |
9487 | * we can use it in the destination network namespace. | |
9488 | */ | |
9489 | err = -EEXIST; | |
d9031024 | 9490 | if (__dev_get_by_name(net, dev->name)) { |
ce286d32 EB |
9491 | /* We get here if we can't use the current device name */ |
9492 | if (!pat) | |
9493 | goto out; | |
7892bd08 LR |
9494 | err = dev_get_valid_name(net, dev, pat); |
9495 | if (err < 0) | |
ce286d32 EB |
9496 | goto out; |
9497 | } | |
9498 | ||
9499 | /* | |
9500 | * And now a mini version of register_netdevice unregister_netdevice. | |
9501 | */ | |
9502 | ||
9503 | /* If device is running close it first. */ | |
9b772652 | 9504 | dev_close(dev); |
ce286d32 EB |
9505 | |
9506 | /* And unlink it from device chain */ | |
ce286d32 EB |
9507 | unlist_netdevice(dev); |
9508 | ||
9509 | synchronize_net(); | |
9510 | ||
9511 | /* Shutdown queueing discipline. */ | |
9512 | dev_shutdown(dev); | |
9513 | ||
9514 | /* Notify protocols, that we are about to destroy | |
eb13da1a | 9515 | * this device. They should clean all the things. |
9516 | * | |
9517 | * Note that dev->reg_state stays at NETREG_REGISTERED. | |
9518 | * This is wanted because this way 8021q and macvlan know | |
9519 | * the device is just moving and can keep their slaves up. | |
9520 | */ | |
ce286d32 | 9521 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
6549dd43 | 9522 | rcu_barrier(); |
38e01b30 | 9523 | |
c36ac8e2 | 9524 | new_nsid = peernet2id_alloc(dev_net(dev), net); |
38e01b30 ND |
9525 | /* If there is an ifindex conflict assign a new one */ |
9526 | if (__dev_get_by_index(net, dev->ifindex)) | |
9527 | new_ifindex = dev_new_index(net); | |
9528 | else | |
9529 | new_ifindex = dev->ifindex; | |
9530 | ||
9531 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, | |
9532 | new_ifindex); | |
ce286d32 EB |
9533 | |
9534 | /* | |
9535 | * Flush the unicast and multicast chains | |
9536 | */ | |
a748ee24 | 9537 | dev_uc_flush(dev); |
22bedad3 | 9538 | dev_mc_flush(dev); |
ce286d32 | 9539 | |
4e66ae2e SH |
9540 | /* Send a netdev-removed uevent to the old namespace */ |
9541 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
4c75431a | 9542 | netdev_adjacent_del_links(dev); |
4e66ae2e | 9543 | |
ce286d32 | 9544 | /* Actually switch the network namespace */ |
c346dca1 | 9545 | dev_net_set(dev, net); |
38e01b30 | 9546 | dev->ifindex = new_ifindex; |
ce286d32 | 9547 | |
4e66ae2e SH |
9548 | /* Send a netdev-add uevent to the new namespace */ |
9549 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
4c75431a | 9550 | netdev_adjacent_add_links(dev); |
4e66ae2e | 9551 | |
8b41d188 | 9552 | /* Fixup kobjects */ |
a1b3f594 | 9553 | err = device_rename(&dev->dev, dev->name); |
8b41d188 | 9554 | WARN_ON(err); |
ce286d32 EB |
9555 | |
9556 | /* Add the device back in the hashes */ | |
9557 | list_netdevice(dev); | |
9558 | ||
9559 | /* Notify protocols, that a new device appeared. */ | |
9560 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
9561 | ||
d90a909e EB |
9562 | /* |
9563 | * Prevent userspace races by waiting until the network | |
9564 | * device is fully setup before sending notifications. | |
9565 | */ | |
7f294054 | 9566 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
d90a909e | 9567 | |
ce286d32 EB |
9568 | synchronize_net(); |
9569 | err = 0; | |
9570 | out: | |
9571 | return err; | |
9572 | } | |
463d0183 | 9573 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
ce286d32 | 9574 | |
f0bf90de | 9575 | static int dev_cpu_dead(unsigned int oldcpu) |
1da177e4 LT |
9576 | { |
9577 | struct sk_buff **list_skb; | |
1da177e4 | 9578 | struct sk_buff *skb; |
f0bf90de | 9579 | unsigned int cpu; |
97d8b6e3 | 9580 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
1da177e4 | 9581 | |
1da177e4 LT |
9582 | local_irq_disable(); |
9583 | cpu = smp_processor_id(); | |
9584 | sd = &per_cpu(softnet_data, cpu); | |
9585 | oldsd = &per_cpu(softnet_data, oldcpu); | |
9586 | ||
9587 | /* Find end of our completion_queue. */ | |
9588 | list_skb = &sd->completion_queue; | |
9589 | while (*list_skb) | |
9590 | list_skb = &(*list_skb)->next; | |
9591 | /* Append completion queue from offline CPU. */ | |
9592 | *list_skb = oldsd->completion_queue; | |
9593 | oldsd->completion_queue = NULL; | |
9594 | ||
1da177e4 | 9595 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
9596 | if (oldsd->output_queue) { |
9597 | *sd->output_queue_tailp = oldsd->output_queue; | |
9598 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
9599 | oldsd->output_queue = NULL; | |
9600 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
9601 | } | |
ac64da0b ED |
9602 | /* Append NAPI poll list from offline CPU, with one exception : |
9603 | * process_backlog() must be called by cpu owning percpu backlog. | |
9604 | * We properly handle process_queue & input_pkt_queue later. | |
9605 | */ | |
9606 | while (!list_empty(&oldsd->poll_list)) { | |
9607 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, | |
9608 | struct napi_struct, | |
9609 | poll_list); | |
9610 | ||
9611 | list_del_init(&napi->poll_list); | |
9612 | if (napi->poll == process_backlog) | |
9613 | napi->state = 0; | |
9614 | else | |
9615 | ____napi_schedule(sd, napi); | |
264524d5 | 9616 | } |
1da177e4 LT |
9617 | |
9618 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
9619 | local_irq_enable(); | |
9620 | ||
773fc8f6 | 9621 | #ifdef CONFIG_RPS |
9622 | remsd = oldsd->rps_ipi_list; | |
9623 | oldsd->rps_ipi_list = NULL; | |
9624 | #endif | |
9625 | /* send out pending IPI's on offline CPU */ | |
9626 | net_rps_send_ipi(remsd); | |
9627 | ||
1da177e4 | 9628 | /* Process offline CPU's input_pkt_queue */ |
76cc8b13 | 9629 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
91e83133 | 9630 | netif_rx_ni(skb); |
76cc8b13 | 9631 | input_queue_head_incr(oldsd); |
fec5e652 | 9632 | } |
ac64da0b | 9633 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
91e83133 | 9634 | netif_rx_ni(skb); |
76cc8b13 TH |
9635 | input_queue_head_incr(oldsd); |
9636 | } | |
1da177e4 | 9637 | |
f0bf90de | 9638 | return 0; |
1da177e4 | 9639 | } |
1da177e4 | 9640 | |
7f353bf2 | 9641 | /** |
b63365a2 HX |
9642 | * netdev_increment_features - increment feature set by one |
9643 | * @all: current feature set | |
9644 | * @one: new feature set | |
9645 | * @mask: mask feature set | |
7f353bf2 HX |
9646 | * |
9647 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
9648 | * @one to the master device with current feature set @all. Will not |
9649 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 9650 | */ |
c8f44aff MM |
9651 | netdev_features_t netdev_increment_features(netdev_features_t all, |
9652 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 9653 | { |
c8cd0989 | 9654 | if (mask & NETIF_F_HW_CSUM) |
a188222b | 9655 | mask |= NETIF_F_CSUM_MASK; |
1742f183 | 9656 | mask |= NETIF_F_VLAN_CHALLENGED; |
7f353bf2 | 9657 | |
a188222b | 9658 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
1742f183 | 9659 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
c6e1a0d1 | 9660 | |
1742f183 | 9661 | /* If one device supports hw checksumming, set for all. */ |
c8cd0989 TH |
9662 | if (all & NETIF_F_HW_CSUM) |
9663 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); | |
7f353bf2 HX |
9664 | |
9665 | return all; | |
9666 | } | |
b63365a2 | 9667 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 9668 | |
430f03cd | 9669 | static struct hlist_head * __net_init netdev_create_hash(void) |
30d97d35 PE |
9670 | { |
9671 | int i; | |
9672 | struct hlist_head *hash; | |
9673 | ||
6da2ec56 | 9674 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
30d97d35 PE |
9675 | if (hash != NULL) |
9676 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
9677 | INIT_HLIST_HEAD(&hash[i]); | |
9678 | ||
9679 | return hash; | |
9680 | } | |
9681 | ||
881d966b | 9682 | /* Initialize per network namespace state */ |
4665079c | 9683 | static int __net_init netdev_init(struct net *net) |
881d966b | 9684 | { |
d9f37d01 | 9685 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
ccdb5171 | 9686 | 8 * FIELD_SIZEOF(struct napi_struct, gro_bitmask)); |
d9f37d01 | 9687 | |
734b6541 RM |
9688 | if (net != &init_net) |
9689 | INIT_LIST_HEAD(&net->dev_base_head); | |
881d966b | 9690 | |
30d97d35 PE |
9691 | net->dev_name_head = netdev_create_hash(); |
9692 | if (net->dev_name_head == NULL) | |
9693 | goto err_name; | |
881d966b | 9694 | |
30d97d35 PE |
9695 | net->dev_index_head = netdev_create_hash(); |
9696 | if (net->dev_index_head == NULL) | |
9697 | goto err_idx; | |
881d966b EB |
9698 | |
9699 | return 0; | |
30d97d35 PE |
9700 | |
9701 | err_idx: | |
9702 | kfree(net->dev_name_head); | |
9703 | err_name: | |
9704 | return -ENOMEM; | |
881d966b EB |
9705 | } |
9706 | ||
f0db275a SH |
9707 | /** |
9708 | * netdev_drivername - network driver for the device | |
9709 | * @dev: network device | |
f0db275a SH |
9710 | * |
9711 | * Determine network driver for device. | |
9712 | */ | |
3019de12 | 9713 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 9714 | { |
cf04a4c7 SH |
9715 | const struct device_driver *driver; |
9716 | const struct device *parent; | |
3019de12 | 9717 | const char *empty = ""; |
6579e57b AV |
9718 | |
9719 | parent = dev->dev.parent; | |
6579e57b | 9720 | if (!parent) |
3019de12 | 9721 | return empty; |
6579e57b AV |
9722 | |
9723 | driver = parent->driver; | |
9724 | if (driver && driver->name) | |
3019de12 DM |
9725 | return driver->name; |
9726 | return empty; | |
6579e57b AV |
9727 | } |
9728 | ||
6ea754eb JP |
9729 | static void __netdev_printk(const char *level, const struct net_device *dev, |
9730 | struct va_format *vaf) | |
256df2f3 | 9731 | { |
b004ff49 | 9732 | if (dev && dev->dev.parent) { |
6ea754eb JP |
9733 | dev_printk_emit(level[1] - '0', |
9734 | dev->dev.parent, | |
9735 | "%s %s %s%s: %pV", | |
9736 | dev_driver_string(dev->dev.parent), | |
9737 | dev_name(dev->dev.parent), | |
9738 | netdev_name(dev), netdev_reg_state(dev), | |
9739 | vaf); | |
b004ff49 | 9740 | } else if (dev) { |
6ea754eb JP |
9741 | printk("%s%s%s: %pV", |
9742 | level, netdev_name(dev), netdev_reg_state(dev), vaf); | |
b004ff49 | 9743 | } else { |
6ea754eb | 9744 | printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 9745 | } |
256df2f3 JP |
9746 | } |
9747 | ||
6ea754eb JP |
9748 | void netdev_printk(const char *level, const struct net_device *dev, |
9749 | const char *format, ...) | |
256df2f3 JP |
9750 | { |
9751 | struct va_format vaf; | |
9752 | va_list args; | |
256df2f3 JP |
9753 | |
9754 | va_start(args, format); | |
9755 | ||
9756 | vaf.fmt = format; | |
9757 | vaf.va = &args; | |
9758 | ||
6ea754eb | 9759 | __netdev_printk(level, dev, &vaf); |
b004ff49 | 9760 | |
256df2f3 | 9761 | va_end(args); |
256df2f3 JP |
9762 | } |
9763 | EXPORT_SYMBOL(netdev_printk); | |
9764 | ||
9765 | #define define_netdev_printk_level(func, level) \ | |
6ea754eb | 9766 | void func(const struct net_device *dev, const char *fmt, ...) \ |
256df2f3 | 9767 | { \ |
256df2f3 JP |
9768 | struct va_format vaf; \ |
9769 | va_list args; \ | |
9770 | \ | |
9771 | va_start(args, fmt); \ | |
9772 | \ | |
9773 | vaf.fmt = fmt; \ | |
9774 | vaf.va = &args; \ | |
9775 | \ | |
6ea754eb | 9776 | __netdev_printk(level, dev, &vaf); \ |
b004ff49 | 9777 | \ |
256df2f3 | 9778 | va_end(args); \ |
256df2f3 JP |
9779 | } \ |
9780 | EXPORT_SYMBOL(func); | |
9781 | ||
9782 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
9783 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
9784 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
9785 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
9786 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
9787 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
9788 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
9789 | ||
4665079c | 9790 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
9791 | { |
9792 | kfree(net->dev_name_head); | |
9793 | kfree(net->dev_index_head); | |
ee21b18b VA |
9794 | if (net != &init_net) |
9795 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); | |
881d966b EB |
9796 | } |
9797 | ||
022cbae6 | 9798 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
9799 | .init = netdev_init, |
9800 | .exit = netdev_exit, | |
9801 | }; | |
9802 | ||
4665079c | 9803 | static void __net_exit default_device_exit(struct net *net) |
ce286d32 | 9804 | { |
e008b5fc | 9805 | struct net_device *dev, *aux; |
ce286d32 | 9806 | /* |
e008b5fc | 9807 | * Push all migratable network devices back to the |
ce286d32 EB |
9808 | * initial network namespace |
9809 | */ | |
9810 | rtnl_lock(); | |
e008b5fc | 9811 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 9812 | int err; |
aca51397 | 9813 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
9814 | |
9815 | /* Ignore unmoveable devices (i.e. loopback) */ | |
9816 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
9817 | continue; | |
9818 | ||
e008b5fc EB |
9819 | /* Leave virtual devices for the generic cleanup */ |
9820 | if (dev->rtnl_link_ops) | |
9821 | continue; | |
d0c082ce | 9822 | |
25985edc | 9823 | /* Push remaining network devices to init_net */ |
aca51397 | 9824 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
55b40dbf JP |
9825 | if (__dev_get_by_name(&init_net, fb_name)) |
9826 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); | |
aca51397 | 9827 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
ce286d32 | 9828 | if (err) { |
7b6cd1ce JP |
9829 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
9830 | __func__, dev->name, err); | |
aca51397 | 9831 | BUG(); |
ce286d32 EB |
9832 | } |
9833 | } | |
9834 | rtnl_unlock(); | |
9835 | } | |
9836 | ||
50624c93 EB |
9837 | static void __net_exit rtnl_lock_unregistering(struct list_head *net_list) |
9838 | { | |
9839 | /* Return with the rtnl_lock held when there are no network | |
9840 | * devices unregistering in any network namespace in net_list. | |
9841 | */ | |
9842 | struct net *net; | |
9843 | bool unregistering; | |
ff960a73 | 9844 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
50624c93 | 9845 | |
ff960a73 | 9846 | add_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 | 9847 | for (;;) { |
50624c93 EB |
9848 | unregistering = false; |
9849 | rtnl_lock(); | |
9850 | list_for_each_entry(net, net_list, exit_list) { | |
9851 | if (net->dev_unreg_count > 0) { | |
9852 | unregistering = true; | |
9853 | break; | |
9854 | } | |
9855 | } | |
9856 | if (!unregistering) | |
9857 | break; | |
9858 | __rtnl_unlock(); | |
ff960a73 PZ |
9859 | |
9860 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
50624c93 | 9861 | } |
ff960a73 | 9862 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 EB |
9863 | } |
9864 | ||
04dc7f6b EB |
9865 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
9866 | { | |
9867 | /* At exit all network devices most be removed from a network | |
b595076a | 9868 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
9869 | * Do this across as many network namespaces as possible to |
9870 | * improve batching efficiency. | |
9871 | */ | |
9872 | struct net_device *dev; | |
9873 | struct net *net; | |
9874 | LIST_HEAD(dev_kill_list); | |
9875 | ||
50624c93 EB |
9876 | /* To prevent network device cleanup code from dereferencing |
9877 | * loopback devices or network devices that have been freed | |
9878 | * wait here for all pending unregistrations to complete, | |
9879 | * before unregistring the loopback device and allowing the | |
9880 | * network namespace be freed. | |
9881 | * | |
9882 | * The netdev todo list containing all network devices | |
9883 | * unregistrations that happen in default_device_exit_batch | |
9884 | * will run in the rtnl_unlock() at the end of | |
9885 | * default_device_exit_batch. | |
9886 | */ | |
9887 | rtnl_lock_unregistering(net_list); | |
04dc7f6b EB |
9888 | list_for_each_entry(net, net_list, exit_list) { |
9889 | for_each_netdev_reverse(net, dev) { | |
b0ab2fab | 9890 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
04dc7f6b EB |
9891 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
9892 | else | |
9893 | unregister_netdevice_queue(dev, &dev_kill_list); | |
9894 | } | |
9895 | } | |
9896 | unregister_netdevice_many(&dev_kill_list); | |
9897 | rtnl_unlock(); | |
9898 | } | |
9899 | ||
022cbae6 | 9900 | static struct pernet_operations __net_initdata default_device_ops = { |
ce286d32 | 9901 | .exit = default_device_exit, |
04dc7f6b | 9902 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
9903 | }; |
9904 | ||
1da177e4 LT |
9905 | /* |
9906 | * Initialize the DEV module. At boot time this walks the device list and | |
9907 | * unhooks any devices that fail to initialise (normally hardware not | |
9908 | * present) and leaves us with a valid list of present and active devices. | |
9909 | * | |
9910 | */ | |
9911 | ||
9912 | /* | |
9913 | * This is called single threaded during boot, so no need | |
9914 | * to take the rtnl semaphore. | |
9915 | */ | |
9916 | static int __init net_dev_init(void) | |
9917 | { | |
9918 | int i, rc = -ENOMEM; | |
9919 | ||
9920 | BUG_ON(!dev_boot_phase); | |
9921 | ||
1da177e4 LT |
9922 | if (dev_proc_init()) |
9923 | goto out; | |
9924 | ||
8b41d188 | 9925 | if (netdev_kobject_init()) |
1da177e4 LT |
9926 | goto out; |
9927 | ||
9928 | INIT_LIST_HEAD(&ptype_all); | |
82d8a867 | 9929 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
9930 | INIT_LIST_HEAD(&ptype_base[i]); |
9931 | ||
62532da9 VY |
9932 | INIT_LIST_HEAD(&offload_base); |
9933 | ||
881d966b EB |
9934 | if (register_pernet_subsys(&netdev_net_ops)) |
9935 | goto out; | |
1da177e4 LT |
9936 | |
9937 | /* | |
9938 | * Initialise the packet receive queues. | |
9939 | */ | |
9940 | ||
6f912042 | 9941 | for_each_possible_cpu(i) { |
41852497 | 9942 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
e36fa2f7 | 9943 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 9944 | |
41852497 ED |
9945 | INIT_WORK(flush, flush_backlog); |
9946 | ||
e36fa2f7 | 9947 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 9948 | skb_queue_head_init(&sd->process_queue); |
f53c7239 SK |
9949 | #ifdef CONFIG_XFRM_OFFLOAD |
9950 | skb_queue_head_init(&sd->xfrm_backlog); | |
9951 | #endif | |
e36fa2f7 | 9952 | INIT_LIST_HEAD(&sd->poll_list); |
a9cbd588 | 9953 | sd->output_queue_tailp = &sd->output_queue; |
df334545 | 9954 | #ifdef CONFIG_RPS |
e36fa2f7 ED |
9955 | sd->csd.func = rps_trigger_softirq; |
9956 | sd->csd.info = sd; | |
e36fa2f7 | 9957 | sd->cpu = i; |
1e94d72f | 9958 | #endif |
0a9627f2 | 9959 | |
7c4ec749 | 9960 | init_gro_hash(&sd->backlog); |
e36fa2f7 ED |
9961 | sd->backlog.poll = process_backlog; |
9962 | sd->backlog.weight = weight_p; | |
1da177e4 LT |
9963 | } |
9964 | ||
1da177e4 LT |
9965 | dev_boot_phase = 0; |
9966 | ||
505d4f73 EB |
9967 | /* The loopback device is special if any other network devices |
9968 | * is present in a network namespace the loopback device must | |
9969 | * be present. Since we now dynamically allocate and free the | |
9970 | * loopback device ensure this invariant is maintained by | |
9971 | * keeping the loopback device as the first device on the | |
9972 | * list of network devices. Ensuring the loopback devices | |
9973 | * is the first device that appears and the last network device | |
9974 | * that disappears. | |
9975 | */ | |
9976 | if (register_pernet_device(&loopback_net_ops)) | |
9977 | goto out; | |
9978 | ||
9979 | if (register_pernet_device(&default_device_ops)) | |
9980 | goto out; | |
9981 | ||
962cf36c CM |
9982 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
9983 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 | 9984 | |
f0bf90de SAS |
9985 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
9986 | NULL, dev_cpu_dead); | |
9987 | WARN_ON(rc < 0); | |
1da177e4 LT |
9988 | rc = 0; |
9989 | out: | |
9990 | return rc; | |
9991 | } | |
9992 | ||
9993 | subsys_initcall(net_dev_init); |