Merge branch 'mv-merge'
[linux-2.6] / net / core / link_watch.c
1 /*
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>
19 #include <net/pkt_sched.h>
20 #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
30 enum lw_bits {
31         LW_RUNNING = 0,
32         LW_SE_USED
33 };
34
35 static unsigned long linkwatch_flags;
36 static unsigned long linkwatch_nextevent;
37
38 static void linkwatch_event(void *dummy);
39 static DECLARE_WORK(linkwatch_work, linkwatch_event, NULL);
40
41 static LIST_HEAD(lweventlist);
42 static DEFINE_SPINLOCK(lweventlist_lock);
43
44 struct lw_event {
45         struct list_head list;
46         struct net_device *dev;
47 };
48
49 /* Avoid kmalloc() for most systems */
50 static struct lw_event singleevent;
51
52 static 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
65 static 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
91 /* Must be called with the rtnl semaphore held */
92 void linkwatch_run_queue(void)
93 {
94         LIST_HEAD(head);
95         struct list_head *n, *next;
96
97         spin_lock_irq(&lweventlist_lock);
98         list_splice_init(&lweventlist, &head);
99         spin_unlock_irq(&lweventlist_lock);
100
101         list_for_each_safe(n, next, &head) {
102                 struct lw_event *event = list_entry(n, struct lw_event, list);
103                 struct net_device *dev = event->dev;
104
105                 if (event == &singleevent) {
106                         clear_bit(LW_SE_USED, &linkwatch_flags);
107                 } else {
108                         kfree(event);
109                 }
110
111                 /* We are about to handle this device,
112                  * so new events can be accepted
113                  */
114                 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
115
116                 rfc2863_policy(dev);
117                 if (dev->flags & IFF_UP) {
118                         if (netif_carrier_ok(dev)) {
119                                 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
120                                 dev_activate(dev);
121                         } else
122                                 dev_deactivate(dev);
123
124                         netdev_state_change(dev);
125                 }
126
127                 dev_put(dev);
128         }
129 }       
130
131
132 static void linkwatch_event(void *dummy)
133 {
134         /* Limit the number of linkwatch events to one
135          * per second so that a runaway driver does not
136          * cause a storm of messages on the netlink
137          * socket
138          */     
139         linkwatch_nextevent = jiffies + HZ;
140         clear_bit(LW_RUNNING, &linkwatch_flags);
141
142         rtnl_lock();
143         linkwatch_run_queue();
144         rtnl_unlock();
145 }
146
147
148 void linkwatch_fire_event(struct net_device *dev)
149 {
150         if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
151                 unsigned long flags;
152                 struct lw_event *event;
153
154                 if (test_and_set_bit(LW_SE_USED, &linkwatch_flags)) {
155                         event = kmalloc(sizeof(struct lw_event), GFP_ATOMIC);
156
157                         if (unlikely(event == NULL)) {
158                                 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
159                                 return;
160                         }
161                 } else {
162                         event = &singleevent;
163                 }
164
165                 dev_hold(dev);
166                 event->dev = dev;
167
168                 spin_lock_irqsave(&lweventlist_lock, flags);
169                 list_add_tail(&event->list, &lweventlist);
170                 spin_unlock_irqrestore(&lweventlist_lock, flags);
171
172                 if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
173                         unsigned long thisevent = jiffies;
174
175                         if (thisevent >= linkwatch_nextevent) {
176                                 schedule_work(&linkwatch_work);
177                         } else {
178                                 schedule_delayed_work(&linkwatch_work, linkwatch_nextevent - thisevent);
179                         }
180                 }
181         }
182 }
183
184 EXPORT_SYMBOL(linkwatch_fire_event);