wifi: mac80211: add eht_capa debugfs field
[linux-2.6-block.git] / net / core / link_watch.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Linux network device link state notification
4 *
5 * Author:
6 * Stefan Rompf <sux@loplof.de>
1da177e4
LT
7 */
8
1da177e4
LT
9#include <linux/module.h>
10#include <linux/netdevice.h>
11#include <linux/if.h>
12#include <net/sock.h>
cacaddf5 13#include <net/pkt_sched.h>
1da177e4
LT
14#include <linux/rtnetlink.h>
15#include <linux/jiffies.h>
16#include <linux/spinlock.h>
1da177e4
LT
17#include <linux/workqueue.h>
18#include <linux/bitops.h>
e56f7359 19#include <linux/types.h>
1da177e4 20
6264f58c 21#include "dev.h"
1da177e4
LT
22
23enum lw_bits {
d9568ba9 24 LW_URGENT = 0,
1da177e4
LT
25};
26
27static unsigned long linkwatch_flags;
28static unsigned long linkwatch_nextevent;
29
65f27f38
DH
30static void linkwatch_event(struct work_struct *dummy);
31static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
1da177e4 32
e014debe 33static LIST_HEAD(lweventlist);
1da177e4
LT
34static DEFINE_SPINLOCK(lweventlist_lock);
35
b00055aa
SR
36static unsigned char default_operstate(const struct net_device *dev)
37{
eec517cd
AL
38 if (netif_testing(dev))
39 return IF_OPER_TESTING;
40
8c55face
VO
41 /* Some uppers (DSA) have additional sources for being down, so
42 * first check whether lower is indeed the source of its down state.
43 */
44 if (!netif_carrier_ok(dev)) {
45 int iflink = dev_get_iflink(dev);
46 struct net_device *peer;
47
48 if (iflink == dev->ifindex)
49 return IF_OPER_DOWN;
50
51 peer = __dev_get_by_index(dev_net(dev), iflink);
52 if (!peer)
53 return IF_OPER_DOWN;
54
55 return netif_carrier_ok(peer) ? IF_OPER_DOWN :
56 IF_OPER_LOWERLAYERDOWN;
57 }
b00055aa
SR
58
59 if (netif_dormant(dev))
60 return IF_OPER_DORMANT;
61
62 return IF_OPER_UP;
63}
64
65
66static void rfc2863_policy(struct net_device *dev)
67{
68 unsigned char operstate = default_operstate(dev);
69
70 if (operstate == dev->operstate)
71 return;
72
fd888e85 73 write_lock(&dev_base_lock);
b00055aa
SR
74
75 switch(dev->link_mode) {
eec517cd
AL
76 case IF_LINK_MODE_TESTING:
77 if (operstate == IF_OPER_UP)
78 operstate = IF_OPER_TESTING;
79 break;
80
b00055aa
SR
81 case IF_LINK_MODE_DORMANT:
82 if (operstate == IF_OPER_UP)
83 operstate = IF_OPER_DORMANT;
84 break;
b00055aa
SR
85 case IF_LINK_MODE_DEFAULT:
86 default:
87 break;
3ff50b79 88 }
b00055aa
SR
89
90 dev->operstate = operstate;
91
fd888e85 92 write_unlock(&dev_base_lock);
b00055aa
SR
93}
94
95
8f4cccbb
BH
96void linkwatch_init_dev(struct net_device *dev)
97{
98 /* Handle pre-registration link state changes */
eec517cd
AL
99 if (!netif_carrier_ok(dev) || netif_dormant(dev) ||
100 netif_testing(dev))
8f4cccbb
BH
101 rfc2863_policy(dev);
102}
103
104
6fa9864b 105static bool linkwatch_urgent_event(struct net_device *dev)
294cc44b 106{
c37e0c99
ED
107 if (!netif_running(dev))
108 return false;
109
a54acb3a 110 if (dev->ifindex != dev_get_iflink(dev))
c37e0c99
ED
111 return true;
112
b76f4189 113 if (netif_is_lag_port(dev) || netif_is_lag_master(dev))
194f4a6d
FL
114 return true;
115
c37e0c99 116 return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
294cc44b
HX
117}
118
119
120static void linkwatch_add_event(struct net_device *dev)
121{
122 unsigned long flags;
123
124 spin_lock_irqsave(&lweventlist_lock, flags);
e014debe
ED
125 if (list_empty(&dev->link_watch_list)) {
126 list_add_tail(&dev->link_watch_list, &lweventlist);
d62607c3 127 netdev_hold(dev, &dev->linkwatch_dev_tracker, GFP_ATOMIC);
e014debe 128 }
294cc44b
HX
129 spin_unlock_irqrestore(&lweventlist_lock, flags);
130}
131
132
d9568ba9 133static void linkwatch_schedule_work(int urgent)
294cc44b 134{
d9568ba9
HX
135 unsigned long delay = linkwatch_nextevent - jiffies;
136
137 if (test_bit(LW_URGENT, &linkwatch_flags))
294cc44b
HX
138 return;
139
d9568ba9
HX
140 /* Minimise down-time: drop delay for up event. */
141 if (urgent) {
142 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
143 return;
294cc44b 144 delay = 0;
db0ccffe 145 }
294cc44b 146
d9568ba9
HX
147 /* If we wrap around we'll delay it by at most HZ. */
148 if (delay > HZ)
149 delay = 0;
150
151 /*
e7c2f967
TH
152 * If urgent, schedule immediate execution; otherwise, don't
153 * override the existing timer.
d9568ba9 154 */
e7c2f967
TH
155 if (test_bit(LW_URGENT, &linkwatch_flags))
156 mod_delayed_work(system_wq, &linkwatch_work, 0);
157 else
158 schedule_delayed_work(&linkwatch_work, delay);
294cc44b
HX
159}
160
161
e014debe
ED
162static void linkwatch_do_dev(struct net_device *dev)
163{
164 /*
165 * Make sure the above read is complete since it can be
166 * rewritten as soon as we clear the bit below.
167 */
4e857c58 168 smp_mb__before_atomic();
e014debe
ED
169
170 /* We are about to handle this device,
171 * so new events can be accepted
172 */
173 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
174
175 rfc2863_policy(dev);
6922110d 176 if (dev->flags & IFF_UP) {
e014debe
ED
177 if (netif_carrier_ok(dev))
178 dev_activate(dev);
179 else
180 dev_deactivate(dev);
181
182 netdev_state_change(dev);
183 }
4c6c11ea
ED
184 /* Note: our callers are responsible for calling netdev_tracker_free().
185 * This is the reason we use __dev_put() instead of dev_put().
123e495e 186 */
4c6c11ea 187 __dev_put(dev);
e014debe
ED
188}
189
294cc44b 190static void __linkwatch_run_queue(int urgent_only)
1da177e4 191{
27ba4059
YL
192#define MAX_DO_DEV_PER_LOOP 100
193
194 int do_dev = MAX_DO_DEV_PER_LOOP;
e014debe
ED
195 struct net_device *dev;
196 LIST_HEAD(wrk);
1da177e4 197
27ba4059
YL
198 /* Give urgent case more budget */
199 if (urgent_only)
200 do_dev += MAX_DO_DEV_PER_LOOP;
201
294cc44b
HX
202 /*
203 * Limit the number of linkwatch events to one
204 * per second so that a runaway driver does not
205 * cause a storm of messages on the netlink
206 * socket. This limit does not apply to up events
207 * while the device qdisc is down.
208 */
209 if (!urgent_only)
210 linkwatch_nextevent = jiffies + HZ;
d9568ba9
HX
211 /* Limit wrap-around effect on delay. */
212 else if (time_after(linkwatch_nextevent, jiffies + HZ))
213 linkwatch_nextevent = jiffies;
214
215 clear_bit(LW_URGENT, &linkwatch_flags);
294cc44b 216
1da177e4 217 spin_lock_irq(&lweventlist_lock);
e014debe 218 list_splice_init(&lweventlist, &wrk);
1da177e4 219
27ba4059 220 while (!list_empty(&wrk) && do_dev > 0) {
1da177e4 221
e014debe
ED
222 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
223 list_del_init(&dev->link_watch_list);
572a103d 224
6922110d
WT
225 if (!netif_device_present(dev) ||
226 (urgent_only && !linkwatch_urgent_event(dev))) {
e014debe 227 list_add_tail(&dev->link_watch_list, &lweventlist);
294cc44b
HX
228 continue;
229 }
123e495e
ED
230 /* We must free netdev tracker under
231 * the spinlock protection.
232 */
233 netdev_tracker_free(dev, &dev->linkwatch_dev_tracker);
e014debe
ED
234 spin_unlock_irq(&lweventlist_lock);
235 linkwatch_do_dev(dev);
27ba4059 236 do_dev--;
e014debe 237 spin_lock_irq(&lweventlist_lock);
1da177e4 238 }
294cc44b 239
27ba4059
YL
240 /* Add the remaining work back to lweventlist */
241 list_splice_init(&wrk, &lweventlist);
242
e014debe 243 if (!list_empty(&lweventlist))
d9568ba9 244 linkwatch_schedule_work(0);
e014debe
ED
245 spin_unlock_irq(&lweventlist_lock);
246}
247
248void linkwatch_forget_dev(struct net_device *dev)
249{
250 unsigned long flags;
251 int clean = 0;
252
253 spin_lock_irqsave(&lweventlist_lock, flags);
254 if (!list_empty(&dev->link_watch_list)) {
255 list_del_init(&dev->link_watch_list);
256 clean = 1;
123e495e
ED
257 /* We must release netdev tracker under
258 * the spinlock protection.
259 */
260 netdev_tracker_free(dev, &dev->linkwatch_dev_tracker);
e014debe
ED
261 }
262 spin_unlock_irqrestore(&lweventlist_lock, flags);
263 if (clean)
264 linkwatch_do_dev(dev);
4ec93edb 265}
1da177e4
LT
266
267
294cc44b
HX
268/* Must be called with the rtnl semaphore held */
269void linkwatch_run_queue(void)
1da177e4 270{
294cc44b
HX
271 __linkwatch_run_queue(0);
272}
273
1da177e4 274
294cc44b
HX
275static void linkwatch_event(struct work_struct *dummy)
276{
6756ae4b 277 rtnl_lock();
294cc44b 278 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
6756ae4b 279 rtnl_unlock();
1da177e4
LT
280}
281
282
283void linkwatch_fire_event(struct net_device *dev)
284{
6fa9864b 285 bool urgent = linkwatch_urgent_event(dev);
1da177e4 286
d9568ba9 287 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
294cc44b 288 linkwatch_add_event(dev);
d9568ba9
HX
289 } else if (!urgent)
290 return;
1da177e4 291
d9568ba9 292 linkwatch_schedule_work(urgent);
1da177e4 293}
1da177e4 294EXPORT_SYMBOL(linkwatch_fire_event);