Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/if.h> |
| 17 | #include <net/sock.h> |
Tommy S. Christensen | cacaddf | 2005-05-03 16:18:52 -0700 | [diff] [blame] | 18 | #include <net/pkt_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/rtnetlink.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/bitops.h> |
| 24 | #include <asm/types.h> |
| 25 | |
| 26 | |
| 27 | enum lw_bits { |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 28 | LW_URGENT = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static unsigned long linkwatch_flags; |
| 32 | static unsigned long linkwatch_nextevent; |
| 33 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 34 | static void linkwatch_event(struct work_struct *dummy); |
| 35 | static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 37 | static LIST_HEAD(lweventlist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static DEFINE_SPINLOCK(lweventlist_lock); |
| 39 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 40 | static 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 | |
| 53 | static 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 Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 71 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 72 | |
| 73 | dev->operstate = operstate; |
| 74 | |
| 75 | write_unlock_bh(&dev_base_lock); |
| 76 | } |
| 77 | |
| 78 | |
Ben Hutchings | 8f4cccb | 2012-08-20 22:16:51 +0100 | [diff] [blame] | 79 | void 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. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 87 | static bool linkwatch_urgent_event(struct net_device *dev) |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 88 | { |
Eric Dumazet | c37e0c9 | 2011-08-30 23:31:58 +0000 | [diff] [blame] | 89 | if (!netif_running(dev)) |
| 90 | return false; |
| 91 | |
| 92 | if (dev->ifindex != dev->iflink) |
| 93 | return true; |
| 94 | |
| 95 | return netif_carrier_ok(dev) && qdisc_tx_changing(dev); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | static void linkwatch_add_event(struct net_device *dev) |
| 100 | { |
| 101 | unsigned long flags; |
| 102 | |
| 103 | spin_lock_irqsave(&lweventlist_lock, flags); |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 104 | if (list_empty(&dev->link_watch_list)) { |
| 105 | list_add_tail(&dev->link_watch_list, &lweventlist); |
| 106 | dev_hold(dev); |
| 107 | } |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 108 | spin_unlock_irqrestore(&lweventlist_lock, flags); |
| 109 | } |
| 110 | |
| 111 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 112 | static void linkwatch_schedule_work(int urgent) |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 113 | { |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 114 | unsigned long delay = linkwatch_nextevent - jiffies; |
| 115 | |
| 116 | if (test_bit(LW_URGENT, &linkwatch_flags)) |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 117 | return; |
| 118 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 119 | /* Minimise down-time: drop delay for up event. */ |
| 120 | if (urgent) { |
| 121 | if (test_and_set_bit(LW_URGENT, &linkwatch_flags)) |
| 122 | return; |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 123 | delay = 0; |
Herbert Xu | db0ccff | 2007-05-08 23:22:43 -0700 | [diff] [blame] | 124 | } |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 125 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 126 | /* If we wrap around we'll delay it by at most HZ. */ |
| 127 | if (delay > HZ) |
| 128 | delay = 0; |
| 129 | |
| 130 | /* |
Tejun Heo | e7c2f96 | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 131 | * If urgent, schedule immediate execution; otherwise, don't |
| 132 | * override the existing timer. |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 133 | */ |
Tejun Heo | e7c2f96 | 2012-08-21 13:18:24 -0700 | [diff] [blame] | 134 | if (test_bit(LW_URGENT, &linkwatch_flags)) |
| 135 | mod_delayed_work(system_wq, &linkwatch_work, 0); |
| 136 | else |
| 137 | schedule_delayed_work(&linkwatch_work, delay); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 141 | static void linkwatch_do_dev(struct net_device *dev) |
| 142 | { |
| 143 | /* |
| 144 | * Make sure the above read is complete since it can be |
| 145 | * rewritten as soon as we clear the bit below. |
| 146 | */ |
| 147 | smp_mb__before_clear_bit(); |
| 148 | |
| 149 | /* We are about to handle this device, |
| 150 | * so new events can be accepted |
| 151 | */ |
| 152 | clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state); |
| 153 | |
| 154 | rfc2863_policy(dev); |
| 155 | if (dev->flags & IFF_UP) { |
| 156 | if (netif_carrier_ok(dev)) |
| 157 | dev_activate(dev); |
| 158 | else |
| 159 | dev_deactivate(dev); |
| 160 | |
| 161 | netdev_state_change(dev); |
| 162 | } |
| 163 | dev_put(dev); |
| 164 | } |
| 165 | |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 166 | static void __linkwatch_run_queue(int urgent_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 168 | struct net_device *dev; |
| 169 | LIST_HEAD(wrk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Limit the number of linkwatch events to one |
| 173 | * per second so that a runaway driver does not |
| 174 | * cause a storm of messages on the netlink |
| 175 | * socket. This limit does not apply to up events |
| 176 | * while the device qdisc is down. |
| 177 | */ |
| 178 | if (!urgent_only) |
| 179 | linkwatch_nextevent = jiffies + HZ; |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 180 | /* Limit wrap-around effect on delay. */ |
| 181 | else if (time_after(linkwatch_nextevent, jiffies + HZ)) |
| 182 | linkwatch_nextevent = jiffies; |
| 183 | |
| 184 | clear_bit(LW_URGENT, &linkwatch_flags); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | spin_lock_irq(&lweventlist_lock); |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 187 | list_splice_init(&lweventlist, &wrk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 189 | while (!list_empty(&wrk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 191 | dev = list_first_entry(&wrk, struct net_device, link_watch_list); |
| 192 | list_del_init(&dev->link_watch_list); |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 193 | |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 194 | if (urgent_only && !linkwatch_urgent_event(dev)) { |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 195 | list_add_tail(&dev->link_watch_list, &lweventlist); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 196 | continue; |
| 197 | } |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 198 | spin_unlock_irq(&lweventlist_lock); |
| 199 | linkwatch_do_dev(dev); |
| 200 | spin_lock_irq(&lweventlist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 202 | |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 203 | if (!list_empty(&lweventlist)) |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 204 | linkwatch_schedule_work(0); |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 205 | spin_unlock_irq(&lweventlist_lock); |
| 206 | } |
| 207 | |
| 208 | void linkwatch_forget_dev(struct net_device *dev) |
| 209 | { |
| 210 | unsigned long flags; |
| 211 | int clean = 0; |
| 212 | |
| 213 | spin_lock_irqsave(&lweventlist_lock, flags); |
| 214 | if (!list_empty(&dev->link_watch_list)) { |
| 215 | list_del_init(&dev->link_watch_list); |
| 216 | clean = 1; |
| 217 | } |
| 218 | spin_unlock_irqrestore(&lweventlist_lock, flags); |
| 219 | if (clean) |
| 220 | linkwatch_do_dev(dev); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | /* Must be called with the rtnl semaphore held */ |
| 225 | void linkwatch_run_queue(void) |
| 226 | { |
| 227 | __linkwatch_run_queue(0); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 231 | static void linkwatch_event(struct work_struct *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 233 | rtnl_lock(); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 234 | __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies)); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 235 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | void linkwatch_fire_event(struct net_device *dev) |
| 240 | { |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 241 | bool urgent = linkwatch_urgent_event(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 243 | if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) { |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 244 | linkwatch_add_event(dev); |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 245 | } else if (!urgent) |
| 246 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 248 | linkwatch_schedule_work(urgent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | EXPORT_SYMBOL(linkwatch_fire_event); |