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