blob: 0f37266411b507a208c3f4dcd80240445cbc37d2 [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
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/netdevice.h>
17#include <linux/if.h>
18#include <net/sock.h>
Tommy S. Christensencacaddf2005-05-03 16:18:52 -070019#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/rtnetlink.h>
21#include <linux/jiffies.h>
22#include <linux/spinlock.h>
23#include <linux/list.h>
24#include <linux/slab.h>
25#include <linux/workqueue.h>
26#include <linux/bitops.h>
27#include <asm/types.h>
28
29
30enum lw_bits {
31 LW_RUNNING = 0,
32 LW_SE_USED
33};
34
35static unsigned long linkwatch_flags;
36static unsigned long linkwatch_nextevent;
37
38static void linkwatch_event(void *dummy);
39static DECLARE_WORK(linkwatch_work, linkwatch_event, NULL);
40
41static LIST_HEAD(lweventlist);
42static DEFINE_SPINLOCK(lweventlist_lock);
43
44struct lw_event {
45 struct list_head list;
46 struct net_device *dev;
47};
48
49/* Avoid kmalloc() for most systems */
50static struct lw_event singleevent;
51
Stefan Rompfb00055a2006-03-20 17:09:11 -080052static unsigned char default_operstate(const struct net_device *dev)
53{
54 if (!netif_carrier_ok(dev))
55 return (dev->ifindex != dev->iflink ?
56 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
57
58 if (netif_dormant(dev))
59 return IF_OPER_DORMANT;
60
61 return IF_OPER_UP;
62}
63
64
65static void rfc2863_policy(struct net_device *dev)
66{
67 unsigned char operstate = default_operstate(dev);
68
69 if (operstate == dev->operstate)
70 return;
71
72 write_lock_bh(&dev_base_lock);
73
74 switch(dev->link_mode) {
75 case IF_LINK_MODE_DORMANT:
76 if (operstate == IF_OPER_UP)
77 operstate = IF_OPER_DORMANT;
78 break;
79
80 case IF_LINK_MODE_DEFAULT:
81 default:
82 break;
83 };
84
85 dev->operstate = operstate;
86
87 write_unlock_bh(&dev_base_lock);
88}
89
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Must be called with the rtnl semaphore held */
92void linkwatch_run_queue(void)
93{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070094 struct list_head head, *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 spin_lock_irq(&lweventlist_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070097 list_replace_init(&lweventlist, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 spin_unlock_irq(&lweventlist_lock);
99
100 list_for_each_safe(n, next, &head) {
101 struct lw_event *event = list_entry(n, struct lw_event, list);
102 struct net_device *dev = event->dev;
103
104 if (event == &singleevent) {
105 clear_bit(LW_SE_USED, &linkwatch_flags);
106 } else {
107 kfree(event);
108 }
109
110 /* We are about to handle this device,
111 * so new events can be accepted
112 */
113 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
114
Stefan Rompfb00055a2006-03-20 17:09:11 -0800115 rfc2863_policy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (dev->flags & IFF_UP) {
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700117 if (netif_carrier_ok(dev)) {
118 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
119 dev_activate(dev);
120 } else
121 dev_deactivate(dev);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 netdev_state_change(dev);
124 }
125
126 dev_put(dev);
127 }
128}
129
130
131static void linkwatch_event(void *dummy)
132{
133 /* Limit the number of linkwatch events to one
134 * per second so that a runaway driver does not
135 * cause a storm of messages on the netlink
136 * socket
137 */
138 linkwatch_nextevent = jiffies + HZ;
139 clear_bit(LW_RUNNING, &linkwatch_flags);
140
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800141 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 linkwatch_run_queue();
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800143 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146
147void linkwatch_fire_event(struct net_device *dev)
148{
149 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
150 unsigned long flags;
151 struct lw_event *event;
152
153 if (test_and_set_bit(LW_SE_USED, &linkwatch_flags)) {
154 event = kmalloc(sizeof(struct lw_event), GFP_ATOMIC);
155
156 if (unlikely(event == NULL)) {
157 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
158 return;
159 }
160 } else {
161 event = &singleevent;
162 }
163
164 dev_hold(dev);
165 event->dev = dev;
166
167 spin_lock_irqsave(&lweventlist_lock, flags);
168 list_add_tail(&event->list, &lweventlist);
169 spin_unlock_irqrestore(&lweventlist_lock, flags);
170
171 if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
Herbert Xu8c105682006-05-09 15:27:54 -0700172 unsigned long delay = linkwatch_nextevent - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Herbert Xu8c105682006-05-09 15:27:54 -0700174 /* If we wrap around we'll delay it by at most HZ. */
175 if (!delay || delay > HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 schedule_work(&linkwatch_work);
Herbert Xu8c105682006-05-09 15:27:54 -0700177 else
178 schedule_delayed_work(&linkwatch_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 }
181}
182
183EXPORT_SYMBOL(linkwatch_fire_event);