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 int 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 int iflink = dev_get_iflink(dev);
46 struct net_device *peer;
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_UNREGISTERED ||
52 iflink == dev->ifindex)
53 return IF_OPER_DOWN;
54
55 ASSERT_RTNL();
56 peer = __dev_get_by_index(dev_net(dev), iflink);
57 if (!peer)
58 return IF_OPER_DOWN;
59
60 return netif_carrier_ok(peer) ? IF_OPER_DOWN :
61 IF_OPER_LOWERLAYERDOWN;
62 }
63
64 if (netif_dormant(dev))
65 return IF_OPER_DORMANT;
66
67 return IF_OPER_UP;
68 }
69
rfc2863_policy(struct net_device * dev)70 static void rfc2863_policy(struct net_device *dev)
71 {
72 unsigned int operstate = default_operstate(dev);
73
74 if (operstate == READ_ONCE(dev->operstate))
75 return;
76
77 switch(dev->link_mode) {
78 case IF_LINK_MODE_TESTING:
79 if (operstate == IF_OPER_UP)
80 operstate = IF_OPER_TESTING;
81 break;
82
83 case IF_LINK_MODE_DORMANT:
84 if (operstate == IF_OPER_UP)
85 operstate = IF_OPER_DORMANT;
86 break;
87 case IF_LINK_MODE_DEFAULT:
88 default:
89 break;
90 }
91
92 WRITE_ONCE(dev->operstate, operstate);
93 }
94
95
linkwatch_init_dev(struct net_device * dev)96 void linkwatch_init_dev(struct net_device *dev)
97 {
98 /* Handle pre-registration link state changes */
99 if (!netif_carrier_ok(dev) || netif_dormant(dev) ||
100 netif_testing(dev))
101 rfc2863_policy(dev);
102 }
103
104
linkwatch_urgent_event(struct net_device * dev)105 static bool linkwatch_urgent_event(struct net_device *dev)
106 {
107 if (!netif_running(dev))
108 return false;
109
110 if (dev->ifindex != dev_get_iflink(dev))
111 return true;
112
113 if (netif_is_lag_port(dev) || netif_is_lag_master(dev))
114 return true;
115
116 return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
117 }
118
119
linkwatch_add_event(struct net_device * dev)120 static void linkwatch_add_event(struct net_device *dev)
121 {
122 unsigned long flags;
123
124 spin_lock_irqsave(&lweventlist_lock, flags);
125 if (list_empty(&dev->link_watch_list)) {
126 list_add_tail(&dev->link_watch_list, &lweventlist);
127 netdev_hold(dev, &dev->linkwatch_dev_tracker, GFP_ATOMIC);
128 }
129 spin_unlock_irqrestore(&lweventlist_lock, flags);
130 }
131
132
linkwatch_schedule_work(int urgent)133 static void linkwatch_schedule_work(int urgent)
134 {
135 unsigned long delay = linkwatch_nextevent - jiffies;
136
137 if (test_bit(LW_URGENT, &linkwatch_flags))
138 return;
139
140 /* Minimise down-time: drop delay for up event. */
141 if (urgent) {
142 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
143 return;
144 delay = 0;
145 }
146
147 /* If we wrap around we'll delay it by at most HZ. */
148 if (delay > HZ)
149 delay = 0;
150
151 /*
152 * If urgent, schedule immediate execution; otherwise, don't
153 * override the existing timer.
154 */
155 if (test_bit(LW_URGENT, &linkwatch_flags))
156 mod_delayed_work(system_unbound_wq, &linkwatch_work, 0);
157 else
158 queue_delayed_work(system_unbound_wq, &linkwatch_work, delay);
159 }
160
161
linkwatch_do_dev(struct net_device * dev)162 static 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 */
168 smp_mb__before_atomic();
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);
176 if (dev->flags & IFF_UP) {
177 if (netif_carrier_ok(dev))
178 dev_activate(dev);
179 else
180 dev_deactivate(dev);
181
182 netdev_state_change(dev);
183 }
184 /* Note: our callers are responsible for calling netdev_tracker_free().
185 * This is the reason we use __dev_put() instead of dev_put().
186 */
187 __dev_put(dev);
188 }
189
__linkwatch_run_queue(int urgent_only)190 static void __linkwatch_run_queue(int urgent_only)
191 {
192 #define MAX_DO_DEV_PER_LOOP 100
193
194 int do_dev = MAX_DO_DEV_PER_LOOP;
195 /* Use a local list here since we add non-urgent
196 * events back to the global one when called with
197 * urgent_only=1.
198 */
199 LIST_HEAD(wrk);
200
201 /* Give urgent case more budget */
202 if (urgent_only)
203 do_dev += MAX_DO_DEV_PER_LOOP;
204
205 /*
206 * Limit the number of linkwatch events to one
207 * per second so that a runaway driver does not
208 * cause a storm of messages on the netlink
209 * socket. This limit does not apply to up events
210 * while the device qdisc is down.
211 */
212 if (!urgent_only)
213 linkwatch_nextevent = jiffies + HZ;
214 /* Limit wrap-around effect on delay. */
215 else if (time_after(linkwatch_nextevent, jiffies + HZ))
216 linkwatch_nextevent = jiffies;
217
218 clear_bit(LW_URGENT, &linkwatch_flags);
219
220 spin_lock_irq(&lweventlist_lock);
221 list_splice_init(&lweventlist, &wrk);
222
223 while (!list_empty(&wrk) && do_dev > 0) {
224 struct net_device *dev;
225
226 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
227 list_del_init(&dev->link_watch_list);
228
229 if (!netif_device_present(dev) ||
230 (urgent_only && !linkwatch_urgent_event(dev))) {
231 list_add_tail(&dev->link_watch_list, &lweventlist);
232 continue;
233 }
234 /* We must free netdev tracker under
235 * the spinlock protection.
236 */
237 netdev_tracker_free(dev, &dev->linkwatch_dev_tracker);
238 spin_unlock_irq(&lweventlist_lock);
239 linkwatch_do_dev(dev);
240 do_dev--;
241 spin_lock_irq(&lweventlist_lock);
242 }
243
244 /* Add the remaining work back to lweventlist */
245 list_splice_init(&wrk, &lweventlist);
246
247 if (!list_empty(&lweventlist))
248 linkwatch_schedule_work(0);
249 spin_unlock_irq(&lweventlist_lock);
250 }
251
linkwatch_sync_dev(struct net_device * dev)252 void linkwatch_sync_dev(struct net_device *dev)
253 {
254 unsigned long flags;
255 int clean = 0;
256
257 spin_lock_irqsave(&lweventlist_lock, flags);
258 if (!list_empty(&dev->link_watch_list)) {
259 list_del_init(&dev->link_watch_list);
260 clean = 1;
261 /* We must release netdev tracker under
262 * the spinlock protection.
263 */
264 netdev_tracker_free(dev, &dev->linkwatch_dev_tracker);
265 }
266 spin_unlock_irqrestore(&lweventlist_lock, flags);
267 if (clean)
268 linkwatch_do_dev(dev);
269 }
270
271
272 /* Must be called with the rtnl semaphore held */
linkwatch_run_queue(void)273 void linkwatch_run_queue(void)
274 {
275 __linkwatch_run_queue(0);
276 }
277
278
linkwatch_event(struct work_struct * dummy)279 static void linkwatch_event(struct work_struct *dummy)
280 {
281 rtnl_lock();
282 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
283 rtnl_unlock();
284 }
285
286
linkwatch_fire_event(struct net_device * dev)287 void linkwatch_fire_event(struct net_device *dev)
288 {
289 bool urgent = linkwatch_urgent_event(dev);
290
291 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
292 linkwatch_add_event(dev);
293 } else if (!urgent)
294 return;
295
296 linkwatch_schedule_work(urgent);
297 }
298 EXPORT_SYMBOL(linkwatch_fire_event);
299