blob: 982861607f883e15f8c81921b72330985349bef9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux network device link state notification
3 *
4 * Author:
5 * Stefan Rompf <sux@loplof.de>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/netdevice.h>
16#include <linux/if.h>
17#include <net/sock.h>
Tommy S. Christensencacaddf2005-05-03 16:18:52 -070018#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/rtnetlink.h>
20#include <linux/jiffies.h>
21#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/workqueue.h>
23#include <linux/bitops.h>
Fabian Fredericke56f7352014-11-17 22:08:22 +010024#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26
27enum lw_bits {
Herbert Xud9568ba2007-05-09 00:17:30 -070028 LW_URGENT = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
31static unsigned long linkwatch_flags;
32static unsigned long linkwatch_nextevent;
33
David Howells65f27f32006-11-22 14:55:48 +000034static void linkwatch_event(struct work_struct *dummy);
35static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric Dumazete014deb2009-11-17 05:59:21 +000037static LIST_HEAD(lweventlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static DEFINE_SPINLOCK(lweventlist_lock);
39
Stefan Rompfb00055a2006-03-20 17:09:11 -080040static unsigned char default_operstate(const struct net_device *dev)
41{
42 if (!netif_carrier_ok(dev))
Nicolas Dichtela54acb32015-04-02 17:07:00 +020043 return (dev->ifindex != dev_get_iflink(dev) ?
Stefan Rompfb00055a2006-03-20 17:09:11 -080044 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
45
46 if (netif_dormant(dev))
47 return IF_OPER_DORMANT;
48
49 return IF_OPER_UP;
50}
51
52
53static void rfc2863_policy(struct net_device *dev)
54{
55 unsigned char operstate = default_operstate(dev);
56
57 if (operstate == dev->operstate)
58 return;
59
60 write_lock_bh(&dev_base_lock);
61
62 switch(dev->link_mode) {
63 case IF_LINK_MODE_DORMANT:
64 if (operstate == IF_OPER_UP)
65 operstate = IF_OPER_DORMANT;
66 break;
67
68 case IF_LINK_MODE_DEFAULT:
69 default:
70 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070071 }
Stefan Rompfb00055a2006-03-20 17:09:11 -080072
73 dev->operstate = operstate;
74
75 write_unlock_bh(&dev_base_lock);
76}
77
78
Ben Hutchings8f4cccb2012-08-20 22:16:51 +010079void linkwatch_init_dev(struct net_device *dev)
80{
81 /* Handle pre-registration link state changes */
82 if (!netif_carrier_ok(dev) || netif_dormant(dev))
83 rfc2863_policy(dev);
84}
85
86
David S. Miller6fa98642008-07-08 23:01:06 -070087static bool linkwatch_urgent_event(struct net_device *dev)
Herbert Xu294cc442007-05-08 18:36:28 -070088{
Eric Dumazetc37e0c92011-08-30 23:31:58 +000089 if (!netif_running(dev))
90 return false;
91
Nicolas Dichtela54acb32015-04-02 17:07:00 +020092 if (dev->ifindex != dev_get_iflink(dev))
Eric Dumazetc37e0c92011-08-30 23:31:58 +000093 return true;
94
Flavio Leitner194f4a62013-06-11 23:09:29 +020095 if (dev->priv_flags & IFF_TEAM_PORT)
96 return true;
97
Eric Dumazetc37e0c92011-08-30 23:31:58 +000098 return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
Herbert Xu294cc442007-05-08 18:36:28 -070099}
100
101
102static void linkwatch_add_event(struct net_device *dev)
103{
104 unsigned long flags;
105
106 spin_lock_irqsave(&lweventlist_lock, flags);
Eric Dumazete014deb2009-11-17 05:59:21 +0000107 if (list_empty(&dev->link_watch_list)) {
108 list_add_tail(&dev->link_watch_list, &lweventlist);
109 dev_hold(dev);
110 }
Herbert Xu294cc442007-05-08 18:36:28 -0700111 spin_unlock_irqrestore(&lweventlist_lock, flags);
112}
113
114
Herbert Xud9568ba2007-05-09 00:17:30 -0700115static void linkwatch_schedule_work(int urgent)
Herbert Xu294cc442007-05-08 18:36:28 -0700116{
Herbert Xud9568ba2007-05-09 00:17:30 -0700117 unsigned long delay = linkwatch_nextevent - jiffies;
118
119 if (test_bit(LW_URGENT, &linkwatch_flags))
Herbert Xu294cc442007-05-08 18:36:28 -0700120 return;
121
Herbert Xud9568ba2007-05-09 00:17:30 -0700122 /* Minimise down-time: drop delay for up event. */
123 if (urgent) {
124 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
125 return;
Herbert Xu294cc442007-05-08 18:36:28 -0700126 delay = 0;
Herbert Xudb0ccff2007-05-08 23:22:43 -0700127 }
Herbert Xu294cc442007-05-08 18:36:28 -0700128
Herbert Xud9568ba2007-05-09 00:17:30 -0700129 /* If we wrap around we'll delay it by at most HZ. */
130 if (delay > HZ)
131 delay = 0;
132
133 /*
Tejun Heoe7c2f962012-08-21 13:18:24 -0700134 * If urgent, schedule immediate execution; otherwise, don't
135 * override the existing timer.
Herbert Xud9568ba2007-05-09 00:17:30 -0700136 */
Tejun Heoe7c2f962012-08-21 13:18:24 -0700137 if (test_bit(LW_URGENT, &linkwatch_flags))
138 mod_delayed_work(system_wq, &linkwatch_work, 0);
139 else
140 schedule_delayed_work(&linkwatch_work, delay);
Herbert Xu294cc442007-05-08 18:36:28 -0700141}
142
143
Eric Dumazete014deb2009-11-17 05:59:21 +0000144static void linkwatch_do_dev(struct net_device *dev)
145{
146 /*
147 * Make sure the above read is complete since it can be
148 * rewritten as soon as we clear the bit below.
149 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100150 smp_mb__before_atomic();
Eric Dumazete014deb2009-11-17 05:59:21 +0000151
152 /* We are about to handle this device,
153 * so new events can be accepted
154 */
155 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
156
157 rfc2863_policy(dev);
158 if (dev->flags & IFF_UP) {
159 if (netif_carrier_ok(dev))
160 dev_activate(dev);
161 else
162 dev_deactivate(dev);
163
164 netdev_state_change(dev);
165 }
166 dev_put(dev);
167}
168
Herbert Xu294cc442007-05-08 18:36:28 -0700169static void __linkwatch_run_queue(int urgent_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Eric Dumazete014deb2009-11-17 05:59:21 +0000171 struct net_device *dev;
172 LIST_HEAD(wrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Herbert Xu294cc442007-05-08 18:36:28 -0700174 /*
175 * Limit the number of linkwatch events to one
176 * per second so that a runaway driver does not
177 * cause a storm of messages on the netlink
178 * socket. This limit does not apply to up events
179 * while the device qdisc is down.
180 */
181 if (!urgent_only)
182 linkwatch_nextevent = jiffies + HZ;
Herbert Xud9568ba2007-05-09 00:17:30 -0700183 /* Limit wrap-around effect on delay. */
184 else if (time_after(linkwatch_nextevent, jiffies + HZ))
185 linkwatch_nextevent = jiffies;
186
187 clear_bit(LW_URGENT, &linkwatch_flags);
Herbert Xu294cc442007-05-08 18:36:28 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 spin_lock_irq(&lweventlist_lock);
Eric Dumazete014deb2009-11-17 05:59:21 +0000190 list_splice_init(&lweventlist, &wrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Eric Dumazete014deb2009-11-17 05:59:21 +0000192 while (!list_empty(&wrk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Eric Dumazete014deb2009-11-17 05:59:21 +0000194 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
195 list_del_init(&dev->link_watch_list);
Herbert Xu572a1032007-05-08 18:34:17 -0700196
Herbert Xu294cc442007-05-08 18:36:28 -0700197 if (urgent_only && !linkwatch_urgent_event(dev)) {
Eric Dumazete014deb2009-11-17 05:59:21 +0000198 list_add_tail(&dev->link_watch_list, &lweventlist);
Herbert Xu294cc442007-05-08 18:36:28 -0700199 continue;
200 }
Eric Dumazete014deb2009-11-17 05:59:21 +0000201 spin_unlock_irq(&lweventlist_lock);
202 linkwatch_do_dev(dev);
203 spin_lock_irq(&lweventlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Herbert Xu294cc442007-05-08 18:36:28 -0700205
Eric Dumazete014deb2009-11-17 05:59:21 +0000206 if (!list_empty(&lweventlist))
Herbert Xud9568ba2007-05-09 00:17:30 -0700207 linkwatch_schedule_work(0);
Eric Dumazete014deb2009-11-17 05:59:21 +0000208 spin_unlock_irq(&lweventlist_lock);
209}
210
211void linkwatch_forget_dev(struct net_device *dev)
212{
213 unsigned long flags;
214 int clean = 0;
215
216 spin_lock_irqsave(&lweventlist_lock, flags);
217 if (!list_empty(&dev->link_watch_list)) {
218 list_del_init(&dev->link_watch_list);
219 clean = 1;
220 }
221 spin_unlock_irqrestore(&lweventlist_lock, flags);
222 if (clean)
223 linkwatch_do_dev(dev);
Herbert Xu294cc442007-05-08 18:36:28 -0700224}
225
226
227/* Must be called with the rtnl semaphore held */
228void linkwatch_run_queue(void)
229{
230 __linkwatch_run_queue(0);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233
David Howells65f27f32006-11-22 14:55:48 +0000234static void linkwatch_event(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800236 rtnl_lock();
Herbert Xu294cc442007-05-08 18:36:28 -0700237 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800238 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241
242void linkwatch_fire_event(struct net_device *dev)
243{
David S. Miller6fa98642008-07-08 23:01:06 -0700244 bool urgent = linkwatch_urgent_event(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Herbert Xud9568ba2007-05-09 00:17:30 -0700246 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
Herbert Xu294cc442007-05-08 18:36:28 -0700247 linkwatch_add_event(dev);
Herbert Xud9568ba2007-05-09 00:17:30 -0700248 } else if (!urgent)
249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Herbert Xud9568ba2007-05-09 00:17:30 -0700251 linkwatch_schedule_work(urgent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253EXPORT_SYMBOL(linkwatch_fire_event);