blob: c3519c6d1b169a5c895efd781c7958218d7f8dc6 [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>
24#include <asm/types.h>
25
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))
43 return (dev->ifindex != dev->iflink ?
44 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
David S. Miller6fa98642008-07-08 23:01:06 -070079static bool linkwatch_urgent_event(struct net_device *dev)
Herbert Xu294cc442007-05-08 18:36:28 -070080{
Eric Dumazetc37e0c92011-08-30 23:31:58 +000081 if (!netif_running(dev))
82 return false;
83
84 if (dev->ifindex != dev->iflink)
85 return true;
86
87 return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
Herbert Xu294cc442007-05-08 18:36:28 -070088}
89
90
91static void linkwatch_add_event(struct net_device *dev)
92{
93 unsigned long flags;
94
95 spin_lock_irqsave(&lweventlist_lock, flags);
Eric Dumazete014deb2009-11-17 05:59:21 +000096 if (list_empty(&dev->link_watch_list)) {
97 list_add_tail(&dev->link_watch_list, &lweventlist);
98 dev_hold(dev);
99 }
Herbert Xu294cc442007-05-08 18:36:28 -0700100 spin_unlock_irqrestore(&lweventlist_lock, flags);
101}
102
103
Herbert Xud9568ba2007-05-09 00:17:30 -0700104static void linkwatch_schedule_work(int urgent)
Herbert Xu294cc442007-05-08 18:36:28 -0700105{
Herbert Xud9568ba2007-05-09 00:17:30 -0700106 unsigned long delay = linkwatch_nextevent - jiffies;
107
108 if (test_bit(LW_URGENT, &linkwatch_flags))
Herbert Xu294cc442007-05-08 18:36:28 -0700109 return;
110
Herbert Xud9568ba2007-05-09 00:17:30 -0700111 /* Minimise down-time: drop delay for up event. */
112 if (urgent) {
113 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
114 return;
Herbert Xu294cc442007-05-08 18:36:28 -0700115 delay = 0;
Herbert Xudb0ccff2007-05-08 23:22:43 -0700116 }
Herbert Xu294cc442007-05-08 18:36:28 -0700117
Herbert Xud9568ba2007-05-09 00:17:30 -0700118 /* If we wrap around we'll delay it by at most HZ. */
119 if (delay > HZ)
120 delay = 0;
121
122 /*
123 * This is true if we've scheduled it immeditately or if we don't
124 * need an immediate execution and it's already pending.
125 */
126 if (schedule_delayed_work(&linkwatch_work, delay) == !delay)
127 return;
128
129 /* Don't bother if there is nothing urgent. */
130 if (!test_bit(LW_URGENT, &linkwatch_flags))
131 return;
132
133 /* It's already running which is good enough. */
stephen hemminger1821f7c2011-07-22 12:53:56 +0000134 if (!__cancel_delayed_work(&linkwatch_work))
Herbert Xud9568ba2007-05-09 00:17:30 -0700135 return;
136
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300137 /* Otherwise we reschedule it again for immediate execution. */
Herbert Xud9568ba2007-05-09 00:17:30 -0700138 schedule_delayed_work(&linkwatch_work, 0);
Herbert Xu294cc442007-05-08 18:36:28 -0700139}
140
141
Eric Dumazete014deb2009-11-17 05:59:21 +0000142static void linkwatch_do_dev(struct net_device *dev)
143{
144 /*
145 * Make sure the above read is complete since it can be
146 * rewritten as soon as we clear the bit below.
147 */
148 smp_mb__before_clear_bit();
149
150 /* We are about to handle this device,
151 * so new events can be accepted
152 */
153 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
154
155 rfc2863_policy(dev);
156 if (dev->flags & IFF_UP) {
157 if (netif_carrier_ok(dev))
158 dev_activate(dev);
159 else
160 dev_deactivate(dev);
161
162 netdev_state_change(dev);
163 }
164 dev_put(dev);
165}
166
Herbert Xu294cc442007-05-08 18:36:28 -0700167static void __linkwatch_run_queue(int urgent_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Eric Dumazete014deb2009-11-17 05:59:21 +0000169 struct net_device *dev;
170 LIST_HEAD(wrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Herbert Xu294cc442007-05-08 18:36:28 -0700172 /*
173 * Limit the number of linkwatch events to one
174 * per second so that a runaway driver does not
175 * cause a storm of messages on the netlink
176 * socket. This limit does not apply to up events
177 * while the device qdisc is down.
178 */
179 if (!urgent_only)
180 linkwatch_nextevent = jiffies + HZ;
Herbert Xud9568ba2007-05-09 00:17:30 -0700181 /* Limit wrap-around effect on delay. */
182 else if (time_after(linkwatch_nextevent, jiffies + HZ))
183 linkwatch_nextevent = jiffies;
184
185 clear_bit(LW_URGENT, &linkwatch_flags);
Herbert Xu294cc442007-05-08 18:36:28 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 spin_lock_irq(&lweventlist_lock);
Eric Dumazete014deb2009-11-17 05:59:21 +0000188 list_splice_init(&lweventlist, &wrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Eric Dumazete014deb2009-11-17 05:59:21 +0000190 while (!list_empty(&wrk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Eric Dumazete014deb2009-11-17 05:59:21 +0000192 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
193 list_del_init(&dev->link_watch_list);
Herbert Xu572a1032007-05-08 18:34:17 -0700194
Herbert Xu294cc442007-05-08 18:36:28 -0700195 if (urgent_only && !linkwatch_urgent_event(dev)) {
Eric Dumazete014deb2009-11-17 05:59:21 +0000196 list_add_tail(&dev->link_watch_list, &lweventlist);
Herbert Xu294cc442007-05-08 18:36:28 -0700197 continue;
198 }
Eric Dumazete014deb2009-11-17 05:59:21 +0000199 spin_unlock_irq(&lweventlist_lock);
200 linkwatch_do_dev(dev);
201 spin_lock_irq(&lweventlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
Herbert Xu294cc442007-05-08 18:36:28 -0700203
Eric Dumazete014deb2009-11-17 05:59:21 +0000204 if (!list_empty(&lweventlist))
Herbert Xud9568ba2007-05-09 00:17:30 -0700205 linkwatch_schedule_work(0);
Eric Dumazete014deb2009-11-17 05:59:21 +0000206 spin_unlock_irq(&lweventlist_lock);
207}
208
209void linkwatch_forget_dev(struct net_device *dev)
210{
211 unsigned long flags;
212 int clean = 0;
213
214 spin_lock_irqsave(&lweventlist_lock, flags);
215 if (!list_empty(&dev->link_watch_list)) {
216 list_del_init(&dev->link_watch_list);
217 clean = 1;
218 }
219 spin_unlock_irqrestore(&lweventlist_lock, flags);
220 if (clean)
221 linkwatch_do_dev(dev);
Herbert Xu294cc442007-05-08 18:36:28 -0700222}
223
224
225/* Must be called with the rtnl semaphore held */
226void linkwatch_run_queue(void)
227{
228 __linkwatch_run_queue(0);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231
David Howells65f27f32006-11-22 14:55:48 +0000232static void linkwatch_event(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800234 rtnl_lock();
Herbert Xu294cc442007-05-08 18:36:28 -0700235 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800236 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239
240void linkwatch_fire_event(struct net_device *dev)
241{
David S. Miller6fa98642008-07-08 23:01:06 -0700242 bool urgent = linkwatch_urgent_event(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Herbert Xud9568ba2007-05-09 00:17:30 -0700244 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
Herbert Xu294cc442007-05-08 18:36:28 -0700245 linkwatch_add_event(dev);
Herbert Xud9568ba2007-05-09 00:17:30 -0700246 } else if (!urgent)
247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Herbert Xud9568ba2007-05-09 00:17:30 -0700249 linkwatch_schedule_work(urgent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251EXPORT_SYMBOL(linkwatch_fire_event);