1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
4 /* A common module to handle registrations and notifications for paravirtual
5 * drivers to enable accelerated datapath and support VF live migration.
7 * The notifier and event handling code is based on netvsc driver.
10 #include <linux/module.h>
11 #include <linux/etherdevice.h>
12 #include <uapi/linux/if_arp.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/if_vlan.h>
15 #include <net/failover.h>
17 static LIST_HEAD(failover_list);
18 static DEFINE_SPINLOCK(failover_lock);
20 static struct net_device *failover_get_bymac(u8 *mac, struct failover_ops **ops)
22 struct net_device *failover_dev;
23 struct failover *failover;
25 spin_lock(&failover_lock);
26 list_for_each_entry(failover, &failover_list, list) {
27 failover_dev = rtnl_dereference(failover->failover_dev);
28 if (ether_addr_equal(failover_dev->perm_addr, mac)) {
29 *ops = rtnl_dereference(failover->ops);
30 spin_unlock(&failover_lock);
34 spin_unlock(&failover_lock);
39 * failover_slave_register - Register a slave netdev
41 * @slave_dev: slave netdev that is being registered
43 * Registers a slave device to a failover instance. Only ethernet devices
46 static int failover_slave_register(struct net_device *slave_dev)
48 struct netdev_lag_upper_info lag_upper_info;
49 struct net_device *failover_dev;
50 struct failover_ops *fops;
53 if (slave_dev->type != ARPHRD_ETHER)
58 failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
62 if (fops && fops->slave_pre_register &&
63 fops->slave_pre_register(slave_dev, failover_dev))
66 err = netdev_rx_handler_register(slave_dev, fops->slave_handle_frame,
69 netdev_err(slave_dev, "can not register failover rx handler (err = %d)\n",
74 lag_upper_info.tx_type = NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
75 err = netdev_master_upper_dev_link(slave_dev, failover_dev, NULL,
76 &lag_upper_info, NULL);
78 netdev_err(slave_dev, "can not set failover device %s (err = %d)\n",
79 failover_dev->name, err);
83 slave_dev->priv_flags |= (IFF_FAILOVER_SLAVE | IFF_NO_ADDRCONF);
85 if (fops && fops->slave_register &&
86 !fops->slave_register(slave_dev, failover_dev))
89 netdev_upper_dev_unlink(slave_dev, failover_dev);
90 slave_dev->priv_flags &= ~(IFF_FAILOVER_SLAVE | IFF_NO_ADDRCONF);
92 netdev_rx_handler_unregister(slave_dev);
98 * failover_slave_unregister - Unregister a slave netdev
100 * @slave_dev: slave netdev that is being unregistered
102 * Unregisters a slave device from a failover instance.
104 int failover_slave_unregister(struct net_device *slave_dev)
106 struct net_device *failover_dev;
107 struct failover_ops *fops;
109 if (!netif_is_failover_slave(slave_dev))
114 failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
118 if (fops && fops->slave_pre_unregister &&
119 fops->slave_pre_unregister(slave_dev, failover_dev))
122 netdev_rx_handler_unregister(slave_dev);
123 netdev_upper_dev_unlink(slave_dev, failover_dev);
124 slave_dev->priv_flags &= ~(IFF_FAILOVER_SLAVE | IFF_NO_ADDRCONF);
126 if (fops && fops->slave_unregister &&
127 !fops->slave_unregister(slave_dev, failover_dev))
133 EXPORT_SYMBOL_GPL(failover_slave_unregister);
135 static int failover_slave_link_change(struct net_device *slave_dev)
137 struct net_device *failover_dev;
138 struct failover_ops *fops;
140 if (!netif_is_failover_slave(slave_dev))
145 failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
149 if (!netif_running(failover_dev))
152 if (fops && fops->slave_link_change &&
153 !fops->slave_link_change(slave_dev, failover_dev))
160 static int failover_slave_name_change(struct net_device *slave_dev)
162 struct net_device *failover_dev;
163 struct failover_ops *fops;
165 if (!netif_is_failover_slave(slave_dev))
170 failover_dev = failover_get_bymac(slave_dev->perm_addr, &fops);
174 if (!netif_running(failover_dev))
177 if (fops && fops->slave_name_change &&
178 !fops->slave_name_change(slave_dev, failover_dev))
186 failover_event(struct notifier_block *this, unsigned long event, void *ptr)
188 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
190 /* Skip parent events */
191 if (netif_is_failover(event_dev))
195 case NETDEV_REGISTER:
196 return failover_slave_register(event_dev);
197 case NETDEV_UNREGISTER:
198 return failover_slave_unregister(event_dev);
202 return failover_slave_link_change(event_dev);
203 case NETDEV_CHANGENAME:
204 return failover_slave_name_change(event_dev);
210 static struct notifier_block failover_notifier = {
211 .notifier_call = failover_event,
215 failover_existing_slave_register(struct net_device *failover_dev)
217 struct net *net = dev_net(failover_dev);
218 struct net_device *dev;
221 for_each_netdev(net, dev) {
222 if (netif_is_failover(dev))
224 if (ether_addr_equal(failover_dev->perm_addr, dev->perm_addr))
225 failover_slave_register(dev);
231 * failover_register - Register a failover instance
233 * @dev: failover netdev
236 * Allocate and register a failover instance for a failover netdev. ops
237 * provides handlers for slave device register/unregister/link change/
238 * name change events.
240 * Return: pointer to failover instance
242 struct failover *failover_register(struct net_device *dev,
243 struct failover_ops *ops)
245 struct failover *failover;
247 if (dev->type != ARPHRD_ETHER)
248 return ERR_PTR(-EINVAL);
250 failover = kzalloc(sizeof(*failover), GFP_KERNEL);
252 return ERR_PTR(-ENOMEM);
254 rcu_assign_pointer(failover->ops, ops);
255 netdev_hold(dev, &failover->dev_tracker, GFP_KERNEL);
256 dev->priv_flags |= IFF_FAILOVER;
257 rcu_assign_pointer(failover->failover_dev, dev);
259 spin_lock(&failover_lock);
260 list_add_tail(&failover->list, &failover_list);
261 spin_unlock(&failover_lock);
263 netdev_info(dev, "failover master:%s registered\n", dev->name);
265 failover_existing_slave_register(dev);
269 EXPORT_SYMBOL_GPL(failover_register);
272 * failover_unregister - Unregister a failover instance
274 * @failover: pointer to failover instance
276 * Unregisters and frees a failover instance.
278 void failover_unregister(struct failover *failover)
280 struct net_device *failover_dev;
282 failover_dev = rcu_dereference(failover->failover_dev);
284 netdev_info(failover_dev, "failover master:%s unregistered\n",
287 failover_dev->priv_flags &= ~IFF_FAILOVER;
288 netdev_put(failover_dev, &failover->dev_tracker);
290 spin_lock(&failover_lock);
291 list_del(&failover->list);
292 spin_unlock(&failover_lock);
296 EXPORT_SYMBOL_GPL(failover_unregister);
301 register_netdevice_notifier(&failover_notifier);
305 module_init(failover_init);
308 void failover_exit(void)
310 unregister_netdevice_notifier(&failover_notifier);
312 module_exit(failover_exit);
314 MODULE_DESCRIPTION("Generic failover infrastructure/interface");
315 MODULE_LICENSE("GPL v2");