staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / net / core / link_watch.c
blob04fdc95357725606f59abec109f1bf2f0a6514fc
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux network device link state notification
5 * Author:
6 * Stefan Rompf <sux@loplof.de>
7 */
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>
22 enum lw_bits {
23 LW_URGENT = 0,
26 static unsigned long linkwatch_flags;
27 static unsigned long linkwatch_nextevent;
29 static void linkwatch_event(struct work_struct *dummy);
30 static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
32 static LIST_HEAD(lweventlist);
33 static DEFINE_SPINLOCK(lweventlist_lock);
35 static unsigned char default_operstate(const struct net_device *dev)
37 if (!netif_carrier_ok(dev))
38 return (dev->ifindex != dev_get_iflink(dev) ?
39 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
41 if (netif_dormant(dev))
42 return IF_OPER_DORMANT;
44 return IF_OPER_UP;
48 static void rfc2863_policy(struct net_device *dev)
50 unsigned char operstate = default_operstate(dev);
52 if (operstate == dev->operstate)
53 return;
55 write_lock_bh(&dev_base_lock);
57 switch(dev->link_mode) {
58 case IF_LINK_MODE_DORMANT:
59 if (operstate == IF_OPER_UP)
60 operstate = IF_OPER_DORMANT;
61 break;
63 case IF_LINK_MODE_DEFAULT:
64 default:
65 break;
68 dev->operstate = operstate;
70 write_unlock_bh(&dev_base_lock);
74 void linkwatch_init_dev(struct net_device *dev)
76 /* Handle pre-registration link state changes */
77 if (!netif_carrier_ok(dev) || netif_dormant(dev))
78 rfc2863_policy(dev);
82 static bool linkwatch_urgent_event(struct net_device *dev)
84 if (!netif_running(dev))
85 return false;
87 if (dev->ifindex != dev_get_iflink(dev))
88 return true;
90 if (netif_is_lag_port(dev) || netif_is_lag_master(dev))
91 return true;
93 return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
97 static void linkwatch_add_event(struct net_device *dev)
99 unsigned long flags;
101 spin_lock_irqsave(&lweventlist_lock, flags);
102 if (list_empty(&dev->link_watch_list)) {
103 list_add_tail(&dev->link_watch_list, &lweventlist);
104 dev_hold(dev);
106 spin_unlock_irqrestore(&lweventlist_lock, flags);
110 static void linkwatch_schedule_work(int urgent)
112 unsigned long delay = linkwatch_nextevent - jiffies;
114 if (test_bit(LW_URGENT, &linkwatch_flags))
115 return;
117 /* Minimise down-time: drop delay for up event. */
118 if (urgent) {
119 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
120 return;
121 delay = 0;
124 /* If we wrap around we'll delay it by at most HZ. */
125 if (delay > HZ)
126 delay = 0;
129 * If urgent, schedule immediate execution; otherwise, don't
130 * override the existing timer.
132 if (test_bit(LW_URGENT, &linkwatch_flags))
133 mod_delayed_work(system_wq, &linkwatch_work, 0);
134 else
135 schedule_delayed_work(&linkwatch_work, delay);
139 static void linkwatch_do_dev(struct net_device *dev)
142 * Make sure the above read is complete since it can be
143 * rewritten as soon as we clear the bit below.
145 smp_mb__before_atomic();
147 /* We are about to handle this device,
148 * so new events can be accepted
150 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
152 rfc2863_policy(dev);
153 if (dev->flags & IFF_UP && netif_device_present(dev)) {
154 if (netif_carrier_ok(dev))
155 dev_activate(dev);
156 else
157 dev_deactivate(dev);
159 netdev_state_change(dev);
161 dev_put(dev);
164 static void __linkwatch_run_queue(int urgent_only)
166 struct net_device *dev;
167 LIST_HEAD(wrk);
170 * Limit the number of linkwatch events to one
171 * per second so that a runaway driver does not
172 * cause a storm of messages on the netlink
173 * socket. This limit does not apply to up events
174 * while the device qdisc is down.
176 if (!urgent_only)
177 linkwatch_nextevent = jiffies + HZ;
178 /* Limit wrap-around effect on delay. */
179 else if (time_after(linkwatch_nextevent, jiffies + HZ))
180 linkwatch_nextevent = jiffies;
182 clear_bit(LW_URGENT, &linkwatch_flags);
184 spin_lock_irq(&lweventlist_lock);
185 list_splice_init(&lweventlist, &wrk);
187 while (!list_empty(&wrk)) {
189 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
190 list_del_init(&dev->link_watch_list);
192 if (urgent_only && !linkwatch_urgent_event(dev)) {
193 list_add_tail(&dev->link_watch_list, &lweventlist);
194 continue;
196 spin_unlock_irq(&lweventlist_lock);
197 linkwatch_do_dev(dev);
198 spin_lock_irq(&lweventlist_lock);
201 if (!list_empty(&lweventlist))
202 linkwatch_schedule_work(0);
203 spin_unlock_irq(&lweventlist_lock);
206 void linkwatch_forget_dev(struct net_device *dev)
208 unsigned long flags;
209 int clean = 0;
211 spin_lock_irqsave(&lweventlist_lock, flags);
212 if (!list_empty(&dev->link_watch_list)) {
213 list_del_init(&dev->link_watch_list);
214 clean = 1;
216 spin_unlock_irqrestore(&lweventlist_lock, flags);
217 if (clean)
218 linkwatch_do_dev(dev);
222 /* Must be called with the rtnl semaphore held */
223 void linkwatch_run_queue(void)
225 __linkwatch_run_queue(0);
229 static void linkwatch_event(struct work_struct *dummy)
231 rtnl_lock();
232 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
233 rtnl_unlock();
237 void linkwatch_fire_event(struct net_device *dev)
239 bool urgent = linkwatch_urgent_event(dev);
241 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
242 linkwatch_add_event(dev);
243 } else if (!urgent)
244 return;
246 linkwatch_schedule_work(urgent);
248 EXPORT_SYMBOL(linkwatch_fire_event);