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