failover.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. /* A common module to handle registrations and notifications for paravirtual
  4. * drivers to enable accelerated datapath and support VF live migration.
  5. *
  6. * The notifier and event handling code is based on netvsc driver.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/etherdevice.h>
  10. #include <uapi/linux/if_arp.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/if_vlan.h>
  13. #include <net/failover.h>
  14. static LIST_HEAD(failover_list);
  15. static DEFINE_SPINLOCK(failover_lock);
  16. static struct net_device *failover_get_bymac(u8 *mac, struct failover_ops **ops)
  17. {
  18. struct net_device *failover_dev;
  19. struct failover *failover;
  20. spin_lock(&failover_lock);
  21. list_for_each_entry(failover, &failover_list, list) {
  22. failover_dev = rtnl_dereference(failover->failover_dev);
  23. if (ether_addr_equal(failover_dev->perm_addr, mac)) {
  24. *ops = rtnl_dereference(failover->ops);
  25. spin_unlock(&failover_lock);
  26. return failover_dev;
  27. }
  28. }
  29. spin_unlock(&failover_lock);
  30. return NULL;
  31. }
  32. /**
  33. * failover_slave_register - Register a slave netdev
  34. *
  35. * @slave_dev: slave netdev that is being registered
  36. *
  37. * Registers a slave device to a failover instance. Only ethernet devices
  38. * are supported.
  39. */
  40. static int failover_slave_register(struct net_device *slave_dev)
  41. {
  42. struct netdev_lag_upper_info lag_upper_info;
  43. struct net_device *failover_dev;
  44. struct failover_ops *fops;
  45. int err;
  46. if (slave_dev->type != ARPHRD_ETHER)
  47. goto done;
  48. ASSERT_RTNL();
  49. failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
  50. if (!failover_dev)
  51. goto done;
  52. if (fops && fops->slave_pre_register &&
  53. fops->slave_pre_register(slave_dev, failover_dev))
  54. goto done;
  55. err = netdev_rx_handler_register(slave_dev, fops->slave_handle_frame,
  56. failover_dev);
  57. if (err) {
  58. netdev_err(slave_dev, "can not register failover rx handler (err = %d)\n",
  59. err);
  60. goto done;
  61. }
  62. lag_upper_info.tx_type = NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
  63. err = netdev_master_upper_dev_link(slave_dev, failover_dev, NULL,
  64. &lag_upper_info, NULL);
  65. if (err) {
  66. netdev_err(slave_dev, "can not set failover device %s (err = %d)\n",
  67. failover_dev->name, err);
  68. goto err_upper_link;
  69. }
  70. slave_dev->priv_flags |= (IFF_FAILOVER_SLAVE | IFF_LIVE_RENAME_OK);
  71. if (fops && fops->slave_register &&
  72. !fops->slave_register(slave_dev, failover_dev))
  73. return NOTIFY_OK;
  74. netdev_upper_dev_unlink(slave_dev, failover_dev);
  75. slave_dev->priv_flags &= ~(IFF_FAILOVER_SLAVE | IFF_LIVE_RENAME_OK);
  76. err_upper_link:
  77. netdev_rx_handler_unregister(slave_dev);
  78. done:
  79. return NOTIFY_DONE;
  80. }
  81. /**
  82. * failover_slave_unregister - Unregister a slave netdev
  83. *
  84. * @slave_dev: slave netdev that is being unregistered
  85. *
  86. * Unregisters a slave device from a failover instance.
  87. */
  88. int failover_slave_unregister(struct net_device *slave_dev)
  89. {
  90. struct net_device *failover_dev;
  91. struct failover_ops *fops;
  92. if (!netif_is_failover_slave(slave_dev))
  93. goto done;
  94. ASSERT_RTNL();
  95. failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
  96. if (!failover_dev)
  97. goto done;
  98. if (fops && fops->slave_pre_unregister &&
  99. fops->slave_pre_unregister(slave_dev, failover_dev))
  100. goto done;
  101. netdev_rx_handler_unregister(slave_dev);
  102. netdev_upper_dev_unlink(slave_dev, failover_dev);
  103. slave_dev->priv_flags &= ~(IFF_FAILOVER_SLAVE | IFF_LIVE_RENAME_OK);
  104. if (fops && fops->slave_unregister &&
  105. !fops->slave_unregister(slave_dev, failover_dev))
  106. return NOTIFY_OK;
  107. done:
  108. return NOTIFY_DONE;
  109. }
  110. EXPORT_SYMBOL_GPL(failover_slave_unregister);
  111. static int failover_slave_link_change(struct net_device *slave_dev)
  112. {
  113. struct net_device *failover_dev;
  114. struct failover_ops *fops;
  115. if (!netif_is_failover_slave(slave_dev))
  116. goto done;
  117. ASSERT_RTNL();
  118. failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
  119. if (!failover_dev)
  120. goto done;
  121. if (!netif_running(failover_dev))
  122. goto done;
  123. if (fops && fops->slave_link_change &&
  124. !fops->slave_link_change(slave_dev, failover_dev))
  125. return NOTIFY_OK;
  126. done:
  127. return NOTIFY_DONE;
  128. }
  129. static int failover_slave_name_change(struct net_device *slave_dev)
  130. {
  131. struct net_device *failover_dev;
  132. struct failover_ops *fops;
  133. if (!netif_is_failover_slave(slave_dev))
  134. goto done;
  135. ASSERT_RTNL();
  136. failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
  137. if (!failover_dev)
  138. goto done;
  139. if (!netif_running(failover_dev))
  140. goto done;
  141. if (fops && fops->slave_name_change &&
  142. !fops->slave_name_change(slave_dev, failover_dev))
  143. return NOTIFY_OK;
  144. done:
  145. return NOTIFY_DONE;
  146. }
  147. static int
  148. failover_event(struct notifier_block *this, unsigned long event, void *ptr)
  149. {
  150. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  151. /* Skip parent events */
  152. if (netif_is_failover(event_dev))
  153. return NOTIFY_DONE;
  154. switch (event) {
  155. case NETDEV_REGISTER:
  156. return failover_slave_register(event_dev);
  157. case NETDEV_UNREGISTER:
  158. return failover_slave_unregister(event_dev);
  159. case NETDEV_UP:
  160. case NETDEV_DOWN:
  161. case NETDEV_CHANGE:
  162. return failover_slave_link_change(event_dev);
  163. case NETDEV_CHANGENAME:
  164. return failover_slave_name_change(event_dev);
  165. default:
  166. return NOTIFY_DONE;
  167. }
  168. }
  169. static struct notifier_block failover_notifier = {
  170. .notifier_call = failover_event,
  171. };
  172. static void
  173. failover_existing_slave_register(struct net_device *failover_dev)
  174. {
  175. struct net *net = dev_net(failover_dev);
  176. struct net_device *dev;
  177. rtnl_lock();
  178. for_each_netdev(net, dev) {
  179. if (netif_is_failover(dev))
  180. continue;
  181. if (ether_addr_equal(failover_dev->perm_addr, dev->perm_addr))
  182. failover_slave_register(dev);
  183. }
  184. rtnl_unlock();
  185. }
  186. /**
  187. * failover_register - Register a failover instance
  188. *
  189. * @dev: failover netdev
  190. * @ops: failover ops
  191. *
  192. * Allocate and register a failover instance for a failover netdev. ops
  193. * provides handlers for slave device register/unregister/link change/
  194. * name change events.
  195. *
  196. * Return: pointer to failover instance
  197. */
  198. struct failover *failover_register(struct net_device *dev,
  199. struct failover_ops *ops)
  200. {
  201. struct failover *failover;
  202. if (dev->type != ARPHRD_ETHER)
  203. return ERR_PTR(-EINVAL);
  204. failover = kzalloc(sizeof(*failover), GFP_KERNEL);
  205. if (!failover)
  206. return ERR_PTR(-ENOMEM);
  207. rcu_assign_pointer(failover->ops, ops);
  208. dev_hold(dev);
  209. dev->priv_flags |= IFF_FAILOVER;
  210. rcu_assign_pointer(failover->failover_dev, dev);
  211. spin_lock(&failover_lock);
  212. list_add_tail(&failover->list, &failover_list);
  213. spin_unlock(&failover_lock);
  214. netdev_info(dev, "failover master:%s registered\n", dev->name);
  215. failover_existing_slave_register(dev);
  216. return failover;
  217. }
  218. EXPORT_SYMBOL_GPL(failover_register);
  219. /**
  220. * failover_unregister - Unregister a failover instance
  221. *
  222. * @failover: pointer to failover instance
  223. *
  224. * Unregisters and frees a failover instance.
  225. */
  226. void failover_unregister(struct failover *failover)
  227. {
  228. struct net_device *failover_dev;
  229. failover_dev = rcu_dereference(failover->failover_dev);
  230. netdev_info(failover_dev, "failover master:%s unregistered\n",
  231. failover_dev->name);
  232. failover_dev->priv_flags &= ~IFF_FAILOVER;
  233. dev_put(failover_dev);
  234. spin_lock(&failover_lock);
  235. list_del(&failover->list);
  236. spin_unlock(&failover_lock);
  237. kfree(failover);
  238. }
  239. EXPORT_SYMBOL_GPL(failover_unregister);
  240. static __init int
  241. failover_init(void)
  242. {
  243. register_netdevice_notifier(&failover_notifier);
  244. return 0;
  245. }
  246. module_init(failover_init);
  247. static __exit
  248. void failover_exit(void)
  249. {
  250. unregister_netdevice_notifier(&failover_notifier);
  251. }
  252. module_exit(failover_exit);
  253. MODULE_DESCRIPTION("Generic failover infrastructure/interface");
  254. MODULE_LICENSE("GPL v2");