link_watch.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/config.h>
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/if.h>
  17. #include <net/sock.h>
  18. #include <net/pkt_sched.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/list.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/bitops.h>
  26. #include <asm/types.h>
  27. enum lw_bits {
  28. LW_RUNNING = 0,
  29. LW_SE_USED
  30. };
  31. static unsigned long linkwatch_flags;
  32. static unsigned long linkwatch_nextevent;
  33. static void linkwatch_event(void *dummy);
  34. static DECLARE_WORK(linkwatch_work, linkwatch_event, NULL);
  35. static LIST_HEAD(lweventlist);
  36. static DEFINE_SPINLOCK(lweventlist_lock);
  37. struct lw_event {
  38. struct list_head list;
  39. struct net_device *dev;
  40. };
  41. /* Avoid kmalloc() for most systems */
  42. static struct lw_event singleevent;
  43. static unsigned char default_operstate(const struct net_device *dev)
  44. {
  45. if (!netif_carrier_ok(dev))
  46. return (dev->ifindex != dev->iflink ?
  47. IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
  48. if (netif_dormant(dev))
  49. return IF_OPER_DORMANT;
  50. return IF_OPER_UP;
  51. }
  52. static void rfc2863_policy(struct net_device *dev)
  53. {
  54. unsigned char operstate = default_operstate(dev);
  55. if (operstate == dev->operstate)
  56. return;
  57. write_lock_bh(&dev_base_lock);
  58. switch(dev->link_mode) {
  59. case IF_LINK_MODE_DORMANT:
  60. if (operstate == IF_OPER_UP)
  61. operstate = IF_OPER_DORMANT;
  62. break;
  63. case IF_LINK_MODE_DEFAULT:
  64. default:
  65. break;
  66. };
  67. dev->operstate = operstate;
  68. write_unlock_bh(&dev_base_lock);
  69. }
  70. /* Must be called with the rtnl semaphore held */
  71. void linkwatch_run_queue(void)
  72. {
  73. LIST_HEAD(head);
  74. struct list_head *n, *next;
  75. spin_lock_irq(&lweventlist_lock);
  76. list_splice_init(&lweventlist, &head);
  77. spin_unlock_irq(&lweventlist_lock);
  78. list_for_each_safe(n, next, &head) {
  79. struct lw_event *event = list_entry(n, struct lw_event, list);
  80. struct net_device *dev = event->dev;
  81. if (event == &singleevent) {
  82. clear_bit(LW_SE_USED, &linkwatch_flags);
  83. } else {
  84. kfree(event);
  85. }
  86. /* We are about to handle this device,
  87. * so new events can be accepted
  88. */
  89. clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
  90. rfc2863_policy(dev);
  91. if (dev->flags & IFF_UP) {
  92. if (netif_carrier_ok(dev)) {
  93. WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
  94. dev_activate(dev);
  95. } else
  96. dev_deactivate(dev);
  97. netdev_state_change(dev);
  98. }
  99. dev_put(dev);
  100. }
  101. }
  102. static void linkwatch_event(void *dummy)
  103. {
  104. /* Limit the number of linkwatch events to one
  105. * per second so that a runaway driver does not
  106. * cause a storm of messages on the netlink
  107. * socket
  108. */
  109. linkwatch_nextevent = jiffies + HZ;
  110. clear_bit(LW_RUNNING, &linkwatch_flags);
  111. rtnl_lock();
  112. linkwatch_run_queue();
  113. rtnl_unlock();
  114. }
  115. void linkwatch_fire_event(struct net_device *dev)
  116. {
  117. if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
  118. unsigned long flags;
  119. struct lw_event *event;
  120. if (test_and_set_bit(LW_SE_USED, &linkwatch_flags)) {
  121. event = kmalloc(sizeof(struct lw_event), GFP_ATOMIC);
  122. if (unlikely(event == NULL)) {
  123. clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
  124. return;
  125. }
  126. } else {
  127. event = &singleevent;
  128. }
  129. dev_hold(dev);
  130. event->dev = dev;
  131. spin_lock_irqsave(&lweventlist_lock, flags);
  132. list_add_tail(&event->list, &lweventlist);
  133. spin_unlock_irqrestore(&lweventlist_lock, flags);
  134. if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
  135. unsigned long thisevent = jiffies;
  136. if (thisevent >= linkwatch_nextevent) {
  137. schedule_work(&linkwatch_work);
  138. } else {
  139. schedule_delayed_work(&linkwatch_work, linkwatch_nextevent - thisevent);
  140. }
  141. }
  142. }
  143. }
  144. EXPORT_SYMBOL(linkwatch_fire_event);