blob: 5910b555a54afbe603322756d39d9fb9898d4830 [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/slab.h>
23#include <linux/workqueue.h>
24#include <linux/bitops.h>
25#include <asm/types.h>
26
27
28enum lw_bits {
Herbert Xud9568ba2007-05-09 00:17:30 -070029 LW_URGENT = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32static unsigned long linkwatch_flags;
33static unsigned long linkwatch_nextevent;
34
David Howells65f27f32006-11-22 14:55:48 +000035static void linkwatch_event(struct work_struct *dummy);
36static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric Dumazete014deb2009-11-17 05:59:21 +000038static LIST_HEAD(lweventlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static DEFINE_SPINLOCK(lweventlist_lock);
40
Stefan Rompfb00055a2006-03-20 17:09:11 -080041static unsigned char default_operstate(const struct net_device *dev)
42{
43 if (!netif_carrier_ok(dev))
44 return (dev->ifindex != dev->iflink ?
45 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
46
47 if (netif_dormant(dev))
48 return IF_OPER_DORMANT;
49
50 return IF_OPER_UP;
51}
52
53
54static void rfc2863_policy(struct net_device *dev)
55{
56 unsigned char operstate = default_operstate(dev);
57
58 if (operstate == dev->operstate)
59 return;
60
61 write_lock_bh(&dev_base_lock);
62
63 switch(dev->link_mode) {
64 case IF_LINK_MODE_DORMANT:
65 if (operstate == IF_OPER_UP)
66 operstate = IF_OPER_DORMANT;
67 break;
68
69 case IF_LINK_MODE_DEFAULT:
70 default:
71 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070072 }
Stefan Rompfb00055a2006-03-20 17:09:11 -080073
74 dev->operstate = operstate;
75
76 write_unlock_bh(&dev_base_lock);
77}
78
79
David S. Miller6fa98642008-07-08 23:01:06 -070080static bool linkwatch_urgent_event(struct net_device *dev)
Herbert Xu294cc442007-05-08 18:36:28 -070081{
82 return netif_running(dev) && netif_carrier_ok(dev) &&
David S. Miller6fa98642008-07-08 23:01:06 -070083 qdisc_tx_changing(dev);
Herbert Xu294cc442007-05-08 18:36:28 -070084}
85
86
87static void linkwatch_add_event(struct net_device *dev)
88{
89 unsigned long flags;
90
91 spin_lock_irqsave(&lweventlist_lock, flags);
Eric Dumazete014deb2009-11-17 05:59:21 +000092 if (list_empty(&dev->link_watch_list)) {
93 list_add_tail(&dev->link_watch_list, &lweventlist);
94 dev_hold(dev);
95 }
Herbert Xu294cc442007-05-08 18:36:28 -070096 spin_unlock_irqrestore(&lweventlist_lock, flags);
97}
98
99
Herbert Xud9568ba2007-05-09 00:17:30 -0700100static void linkwatch_schedule_work(int urgent)
Herbert Xu294cc442007-05-08 18:36:28 -0700101{
Herbert Xud9568ba2007-05-09 00:17:30 -0700102 unsigned long delay = linkwatch_nextevent - jiffies;
103
104 if (test_bit(LW_URGENT, &linkwatch_flags))
Herbert Xu294cc442007-05-08 18:36:28 -0700105 return;
106
Herbert Xud9568ba2007-05-09 00:17:30 -0700107 /* Minimise down-time: drop delay for up event. */
108 if (urgent) {
109 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
110 return;
Herbert Xu294cc442007-05-08 18:36:28 -0700111 delay = 0;
Herbert Xudb0ccff2007-05-08 23:22:43 -0700112 }
Herbert Xu294cc442007-05-08 18:36:28 -0700113
Herbert Xud9568ba2007-05-09 00:17:30 -0700114 /* If we wrap around we'll delay it by at most HZ. */
115 if (delay > HZ)
116 delay = 0;
117
118 /*
119 * This is true if we've scheduled it immeditately or if we don't
120 * need an immediate execution and it's already pending.
121 */
122 if (schedule_delayed_work(&linkwatch_work, delay) == !delay)
123 return;
124
125 /* Don't bother if there is nothing urgent. */
126 if (!test_bit(LW_URGENT, &linkwatch_flags))
127 return;
128
129 /* It's already running which is good enough. */
130 if (!cancel_delayed_work(&linkwatch_work))
131 return;
132
133 /* Otherwise we reschedule it again for immediate exection. */
134 schedule_delayed_work(&linkwatch_work, 0);
Herbert Xu294cc442007-05-08 18:36:28 -0700135}
136
137
Eric Dumazete014deb2009-11-17 05:59:21 +0000138static void linkwatch_do_dev(struct net_device *dev)
139{
140 /*
141 * Make sure the above read is complete since it can be
142 * rewritten as soon as we clear the bit below.
143 */
144 smp_mb__before_clear_bit();
145
146 /* We are about to handle this device,
147 * so new events can be accepted
148 */
149 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
150
151 rfc2863_policy(dev);
152 if (dev->flags & IFF_UP) {
153 if (netif_carrier_ok(dev))
154 dev_activate(dev);
155 else
156 dev_deactivate(dev);
157
158 netdev_state_change(dev);
159 }
160 dev_put(dev);
161}
162
Herbert Xu294cc442007-05-08 18:36:28 -0700163static void __linkwatch_run_queue(int urgent_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Eric Dumazete014deb2009-11-17 05:59:21 +0000165 struct net_device *dev;
166 LIST_HEAD(wrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Herbert Xu294cc442007-05-08 18:36:28 -0700168 /*
169 * Limit the number of linkwatch events to one
170 * per second so that a runaway driver does not
171 * cause a storm of messages on the netlink
172 * socket. This limit does not apply to up events
173 * while the device qdisc is down.
174 */
175 if (!urgent_only)
176 linkwatch_nextevent = jiffies + HZ;
Herbert Xud9568ba2007-05-09 00:17:30 -0700177 /* Limit wrap-around effect on delay. */
178 else if (time_after(linkwatch_nextevent, jiffies + HZ))
179 linkwatch_nextevent = jiffies;
180
181 clear_bit(LW_URGENT, &linkwatch_flags);
Herbert Xu294cc442007-05-08 18:36:28 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_lock_irq(&lweventlist_lock);
Eric Dumazete014deb2009-11-17 05:59:21 +0000184 list_splice_init(&lweventlist, &wrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Eric Dumazete014deb2009-11-17 05:59:21 +0000186 while (!list_empty(&wrk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Eric Dumazete014deb2009-11-17 05:59:21 +0000188 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
189 list_del_init(&dev->link_watch_list);
Herbert Xu572a1032007-05-08 18:34:17 -0700190
Herbert Xu294cc442007-05-08 18:36:28 -0700191 if (urgent_only && !linkwatch_urgent_event(dev)) {
Eric Dumazete014deb2009-11-17 05:59:21 +0000192 list_add_tail(&dev->link_watch_list, &lweventlist);
Herbert Xu294cc442007-05-08 18:36:28 -0700193 continue;
194 }
Eric Dumazete014deb2009-11-17 05:59:21 +0000195 spin_unlock_irq(&lweventlist_lock);
196 linkwatch_do_dev(dev);
197 spin_lock_irq(&lweventlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Herbert Xu294cc442007-05-08 18:36:28 -0700199
Eric Dumazete014deb2009-11-17 05:59:21 +0000200 if (!list_empty(&lweventlist))
Herbert Xud9568ba2007-05-09 00:17:30 -0700201 linkwatch_schedule_work(0);
Eric Dumazete014deb2009-11-17 05:59:21 +0000202 spin_unlock_irq(&lweventlist_lock);
203}
204
205void linkwatch_forget_dev(struct net_device *dev)
206{
207 unsigned long flags;
208 int clean = 0;
209
210 spin_lock_irqsave(&lweventlist_lock, flags);
211 if (!list_empty(&dev->link_watch_list)) {
212 list_del_init(&dev->link_watch_list);
213 clean = 1;
214 }
215 spin_unlock_irqrestore(&lweventlist_lock, flags);
216 if (clean)
217 linkwatch_do_dev(dev);
Herbert Xu294cc442007-05-08 18:36:28 -0700218}
219
220
221/* Must be called with the rtnl semaphore held */
222void linkwatch_run_queue(void)
223{
224 __linkwatch_run_queue(0);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227
David Howells65f27f32006-11-22 14:55:48 +0000228static void linkwatch_event(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800230 rtnl_lock();
Herbert Xu294cc442007-05-08 18:36:28 -0700231 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800232 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235
236void linkwatch_fire_event(struct net_device *dev)
237{
David S. Miller6fa98642008-07-08 23:01:06 -0700238 bool urgent = linkwatch_urgent_event(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Herbert Xud9568ba2007-05-09 00:17:30 -0700240 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
Herbert Xu294cc442007-05-08 18:36:28 -0700241 linkwatch_add_event(dev);
Herbert Xud9568ba2007-05-09 00:17:30 -0700242 } else if (!urgent)
243 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Herbert Xud9568ba2007-05-09 00:17:30 -0700245 linkwatch_schedule_work(urgent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248EXPORT_SYMBOL(linkwatch_fire_event);