Linux Audio

Check our new training course

Yocto distribution development and maintenance

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