link_watch.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/if.h>
  16. #include <net/sock.h>
  17. #include <net/pkt_sched.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/bitops.h>
  24. #include <asm/types.h>
  25. enum lw_bits {
  26. LW_URGENT = 0,
  27. };
  28. static unsigned long linkwatch_flags;
  29. static unsigned long linkwatch_nextevent;
  30. static void linkwatch_event(struct work_struct *dummy);
  31. static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
  32. static struct net_device *lweventlist;
  33. static DEFINE_SPINLOCK(lweventlist_lock);
  34. static unsigned char default_operstate(const struct net_device *dev)
  35. {
  36. if (!netif_carrier_ok(dev))
  37. return (dev->ifindex != dev->iflink ?
  38. IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
  39. if (netif_dormant(dev))
  40. return IF_OPER_DORMANT;
  41. return IF_OPER_UP;
  42. }
  43. static void rfc2863_policy(struct net_device *dev)
  44. {
  45. unsigned char operstate = default_operstate(dev);
  46. if (operstate == dev->operstate)
  47. return;
  48. write_lock_bh(&dev_base_lock);
  49. switch(dev->link_mode) {
  50. case IF_LINK_MODE_DORMANT:
  51. if (operstate == IF_OPER_UP)
  52. operstate = IF_OPER_DORMANT;
  53. break;
  54. case IF_LINK_MODE_DEFAULT:
  55. default:
  56. break;
  57. }
  58. dev->operstate = operstate;
  59. write_unlock_bh(&dev_base_lock);
  60. }
  61. static bool linkwatch_urgent_event(struct net_device *dev)
  62. {
  63. return netif_running(dev) && netif_carrier_ok(dev) &&
  64. qdisc_tx_changing(dev);
  65. }
  66. static void linkwatch_add_event(struct net_device *dev)
  67. {
  68. unsigned long flags;
  69. spin_lock_irqsave(&lweventlist_lock, flags);
  70. dev->link_watch_next = lweventlist;
  71. lweventlist = dev;
  72. spin_unlock_irqrestore(&lweventlist_lock, flags);
  73. }
  74. static void linkwatch_schedule_work(int urgent)
  75. {
  76. unsigned long delay = linkwatch_nextevent - jiffies;
  77. if (test_bit(LW_URGENT, &linkwatch_flags))
  78. return;
  79. /* Minimise down-time: drop delay for up event. */
  80. if (urgent) {
  81. if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
  82. return;
  83. delay = 0;
  84. }
  85. /* If we wrap around we'll delay it by at most HZ. */
  86. if (delay > HZ)
  87. delay = 0;
  88. /*
  89. * This is true if we've scheduled it immeditately or if we don't
  90. * need an immediate execution and it's already pending.
  91. */
  92. if (schedule_delayed_work(&linkwatch_work, delay) == !delay)
  93. return;
  94. /* Don't bother if there is nothing urgent. */
  95. if (!test_bit(LW_URGENT, &linkwatch_flags))
  96. return;
  97. /* It's already running which is good enough. */
  98. if (!cancel_delayed_work(&linkwatch_work))
  99. return;
  100. /* Otherwise we reschedule it again for immediate exection. */
  101. schedule_delayed_work(&linkwatch_work, 0);
  102. }
  103. static void __linkwatch_run_queue(int urgent_only)
  104. {
  105. struct net_device *next;
  106. /*
  107. * Limit the number of linkwatch events to one
  108. * per second so that a runaway driver does not
  109. * cause a storm of messages on the netlink
  110. * socket. This limit does not apply to up events
  111. * while the device qdisc is down.
  112. */
  113. if (!urgent_only)
  114. linkwatch_nextevent = jiffies + HZ;
  115. /* Limit wrap-around effect on delay. */
  116. else if (time_after(linkwatch_nextevent, jiffies + HZ))
  117. linkwatch_nextevent = jiffies;
  118. clear_bit(LW_URGENT, &linkwatch_flags);
  119. spin_lock_irq(&lweventlist_lock);
  120. next = lweventlist;
  121. lweventlist = NULL;
  122. spin_unlock_irq(&lweventlist_lock);
  123. while (next) {
  124. struct net_device *dev = next;
  125. next = dev->link_watch_next;
  126. if (urgent_only && !linkwatch_urgent_event(dev)) {
  127. linkwatch_add_event(dev);
  128. continue;
  129. }
  130. /*
  131. * Make sure the above read is complete since it can be
  132. * rewritten as soon as we clear the bit below.
  133. */
  134. smp_mb__before_clear_bit();
  135. /* We are about to handle this device,
  136. * so new events can be accepted
  137. */
  138. clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
  139. rfc2863_policy(dev);
  140. if (dev->flags & IFF_UP) {
  141. if (netif_carrier_ok(dev))
  142. dev_activate(dev);
  143. else
  144. dev_deactivate(dev);
  145. netdev_state_change(dev);
  146. }
  147. dev_put(dev);
  148. }
  149. if (lweventlist)
  150. linkwatch_schedule_work(0);
  151. }
  152. /* Must be called with the rtnl semaphore held */
  153. void linkwatch_run_queue(void)
  154. {
  155. __linkwatch_run_queue(0);
  156. }
  157. static void linkwatch_event(struct work_struct *dummy)
  158. {
  159. rtnl_lock();
  160. __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
  161. rtnl_unlock();
  162. }
  163. void linkwatch_fire_event(struct net_device *dev)
  164. {
  165. bool urgent = linkwatch_urgent_event(dev);
  166. if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
  167. dev_hold(dev);
  168. linkwatch_add_event(dev);
  169. } else if (!urgent)
  170. return;
  171. linkwatch_schedule_work(urgent);
  172. }
  173. EXPORT_SYMBOL(linkwatch_fire_event);