Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /* Copyright (c) 2018, Intel Corporation. */
3 :
4 : /* This provides a net_failover interface for paravirtual drivers to
5 : * provide an alternate datapath by exporting APIs to create and
6 : * destroy a upper 'net_failover' netdev. The upper dev manages the
7 : * original paravirtual interface as a 'standby' netdev and uses the
8 : * generic failover infrastructure to register and manage a direct
9 : * attached VF as a 'primary' netdev. This enables live migration of
10 : * a VM with direct attached VF by failing over to the paravirtual
11 : * datapath when the VF is unplugged.
12 : *
13 : * Some of the netdev management routines are based on bond/team driver as
14 : * this driver provides active-backup functionality similar to those drivers.
15 : */
16 :
17 : #include <linux/netdevice.h>
18 : #include <linux/etherdevice.h>
19 : #include <linux/ethtool.h>
20 : #include <linux/module.h>
21 : #include <linux/slab.h>
22 : #include <linux/netpoll.h>
23 : #include <linux/rtnetlink.h>
24 : #include <linux/if_vlan.h>
25 : #include <linux/pci.h>
26 : #include <net/sch_generic.h>
27 : #include <uapi/linux/if_arp.h>
28 : #include <net/net_failover.h>
29 :
30 0 : static bool net_failover_xmit_ready(struct net_device *dev)
31 : {
32 0 : return netif_running(dev) && netif_carrier_ok(dev);
33 : }
34 :
35 0 : static int net_failover_open(struct net_device *dev)
36 : {
37 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
38 0 : struct net_device *primary_dev, *standby_dev;
39 0 : int err;
40 :
41 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
42 0 : if (primary_dev) {
43 0 : err = dev_open(primary_dev, NULL);
44 0 : if (err)
45 0 : goto err_primary_open;
46 : }
47 :
48 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
49 0 : if (standby_dev) {
50 0 : err = dev_open(standby_dev, NULL);
51 0 : if (err)
52 0 : goto err_standby_open;
53 : }
54 :
55 0 : if ((primary_dev && net_failover_xmit_ready(primary_dev)) ||
56 0 : (standby_dev && net_failover_xmit_ready(standby_dev))) {
57 0 : netif_carrier_on(dev);
58 0 : netif_tx_wake_all_queues(dev);
59 : }
60 :
61 : return 0;
62 :
63 0 : err_standby_open:
64 0 : if (primary_dev)
65 0 : dev_close(primary_dev);
66 0 : err_primary_open:
67 0 : netif_tx_disable(dev);
68 0 : return err;
69 : }
70 :
71 0 : static int net_failover_close(struct net_device *dev)
72 : {
73 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
74 0 : struct net_device *slave_dev;
75 :
76 0 : netif_tx_disable(dev);
77 :
78 0 : slave_dev = rtnl_dereference(nfo_info->primary_dev);
79 0 : if (slave_dev)
80 0 : dev_close(slave_dev);
81 :
82 0 : slave_dev = rtnl_dereference(nfo_info->standby_dev);
83 0 : if (slave_dev)
84 0 : dev_close(slave_dev);
85 :
86 0 : return 0;
87 : }
88 :
89 0 : static netdev_tx_t net_failover_drop_xmit(struct sk_buff *skb,
90 : struct net_device *dev)
91 : {
92 0 : atomic_long_inc(&dev->tx_dropped);
93 0 : dev_kfree_skb_any(skb);
94 0 : return NETDEV_TX_OK;
95 : }
96 :
97 0 : static netdev_tx_t net_failover_start_xmit(struct sk_buff *skb,
98 : struct net_device *dev)
99 : {
100 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
101 0 : struct net_device *xmit_dev;
102 :
103 : /* Try xmit via primary netdev followed by standby netdev */
104 0 : xmit_dev = rcu_dereference_bh(nfo_info->primary_dev);
105 0 : if (!xmit_dev || !net_failover_xmit_ready(xmit_dev)) {
106 0 : xmit_dev = rcu_dereference_bh(nfo_info->standby_dev);
107 0 : if (!xmit_dev || !net_failover_xmit_ready(xmit_dev))
108 0 : return net_failover_drop_xmit(skb, dev);
109 : }
110 :
111 0 : skb->dev = xmit_dev;
112 0 : skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
113 :
114 0 : return dev_queue_xmit(skb);
115 : }
116 :
117 0 : static u16 net_failover_select_queue(struct net_device *dev,
118 : struct sk_buff *skb,
119 : struct net_device *sb_dev)
120 : {
121 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
122 0 : struct net_device *primary_dev;
123 0 : u16 txq;
124 :
125 0 : primary_dev = rcu_dereference(nfo_info->primary_dev);
126 0 : if (primary_dev) {
127 0 : const struct net_device_ops *ops = primary_dev->netdev_ops;
128 :
129 0 : if (ops->ndo_select_queue)
130 0 : txq = ops->ndo_select_queue(primary_dev, skb, sb_dev);
131 : else
132 0 : txq = netdev_pick_tx(primary_dev, skb, NULL);
133 :
134 0 : qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
135 :
136 0 : return txq;
137 : }
138 :
139 0 : txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
140 :
141 : /* Save the original txq to restore before passing to the driver */
142 0 : qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
143 :
144 0 : if (unlikely(txq >= dev->real_num_tx_queues)) {
145 0 : do {
146 0 : txq -= dev->real_num_tx_queues;
147 0 : } while (txq >= dev->real_num_tx_queues);
148 : }
149 :
150 : return txq;
151 : }
152 :
153 : /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
154 : * that some drivers can provide 32bit values only.
155 : */
156 0 : static void net_failover_fold_stats(struct rtnl_link_stats64 *_res,
157 : const struct rtnl_link_stats64 *_new,
158 : const struct rtnl_link_stats64 *_old)
159 : {
160 0 : const u64 *new = (const u64 *)_new;
161 0 : const u64 *old = (const u64 *)_old;
162 0 : u64 *res = (u64 *)_res;
163 0 : int i;
164 :
165 0 : for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
166 0 : u64 nv = new[i];
167 0 : u64 ov = old[i];
168 0 : s64 delta = nv - ov;
169 :
170 : /* detects if this particular field is 32bit only */
171 0 : if (((nv | ov) >> 32) == 0)
172 0 : delta = (s64)(s32)((u32)nv - (u32)ov);
173 :
174 : /* filter anomalies, some drivers reset their stats
175 : * at down/up events.
176 : */
177 0 : if (delta > 0)
178 0 : res[i] += delta;
179 : }
180 0 : }
181 :
182 0 : static void net_failover_get_stats(struct net_device *dev,
183 : struct rtnl_link_stats64 *stats)
184 : {
185 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
186 0 : const struct rtnl_link_stats64 *new;
187 0 : struct rtnl_link_stats64 temp;
188 0 : struct net_device *slave_dev;
189 :
190 0 : spin_lock(&nfo_info->stats_lock);
191 0 : memcpy(stats, &nfo_info->failover_stats, sizeof(*stats));
192 :
193 0 : rcu_read_lock();
194 :
195 0 : slave_dev = rcu_dereference(nfo_info->primary_dev);
196 0 : if (slave_dev) {
197 0 : new = dev_get_stats(slave_dev, &temp);
198 0 : net_failover_fold_stats(stats, new, &nfo_info->primary_stats);
199 0 : memcpy(&nfo_info->primary_stats, new, sizeof(*new));
200 : }
201 :
202 0 : slave_dev = rcu_dereference(nfo_info->standby_dev);
203 0 : if (slave_dev) {
204 0 : new = dev_get_stats(slave_dev, &temp);
205 0 : net_failover_fold_stats(stats, new, &nfo_info->standby_stats);
206 0 : memcpy(&nfo_info->standby_stats, new, sizeof(*new));
207 : }
208 :
209 0 : rcu_read_unlock();
210 :
211 0 : memcpy(&nfo_info->failover_stats, stats, sizeof(*stats));
212 0 : spin_unlock(&nfo_info->stats_lock);
213 0 : }
214 :
215 0 : static int net_failover_change_mtu(struct net_device *dev, int new_mtu)
216 : {
217 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
218 0 : struct net_device *primary_dev, *standby_dev;
219 0 : int ret = 0;
220 :
221 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
222 0 : if (primary_dev) {
223 0 : ret = dev_set_mtu(primary_dev, new_mtu);
224 0 : if (ret)
225 : return ret;
226 : }
227 :
228 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
229 0 : if (standby_dev) {
230 0 : ret = dev_set_mtu(standby_dev, new_mtu);
231 0 : if (ret) {
232 0 : if (primary_dev)
233 0 : dev_set_mtu(primary_dev, dev->mtu);
234 0 : return ret;
235 : }
236 : }
237 :
238 0 : dev->mtu = new_mtu;
239 :
240 0 : return 0;
241 : }
242 :
243 0 : static void net_failover_set_rx_mode(struct net_device *dev)
244 : {
245 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
246 0 : struct net_device *slave_dev;
247 :
248 0 : rcu_read_lock();
249 :
250 0 : slave_dev = rcu_dereference(nfo_info->primary_dev);
251 0 : if (slave_dev) {
252 0 : dev_uc_sync_multiple(slave_dev, dev);
253 0 : dev_mc_sync_multiple(slave_dev, dev);
254 : }
255 :
256 0 : slave_dev = rcu_dereference(nfo_info->standby_dev);
257 0 : if (slave_dev) {
258 0 : dev_uc_sync_multiple(slave_dev, dev);
259 0 : dev_mc_sync_multiple(slave_dev, dev);
260 : }
261 :
262 0 : rcu_read_unlock();
263 0 : }
264 :
265 0 : static int net_failover_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
266 : u16 vid)
267 : {
268 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
269 0 : struct net_device *primary_dev, *standby_dev;
270 0 : int ret = 0;
271 :
272 0 : primary_dev = rcu_dereference(nfo_info->primary_dev);
273 0 : if (primary_dev) {
274 : ret = vlan_vid_add(primary_dev, proto, vid);
275 : if (ret)
276 : return ret;
277 : }
278 :
279 0 : standby_dev = rcu_dereference(nfo_info->standby_dev);
280 0 : if (standby_dev) {
281 : ret = vlan_vid_add(standby_dev, proto, vid);
282 : if (ret)
283 : if (primary_dev)
284 0 : vlan_vid_del(primary_dev, proto, vid);
285 : }
286 :
287 0 : return ret;
288 : }
289 :
290 0 : static int net_failover_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
291 : u16 vid)
292 : {
293 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
294 0 : struct net_device *slave_dev;
295 :
296 0 : slave_dev = rcu_dereference(nfo_info->primary_dev);
297 0 : if (slave_dev)
298 0 : vlan_vid_del(slave_dev, proto, vid);
299 :
300 0 : slave_dev = rcu_dereference(nfo_info->standby_dev);
301 0 : if (slave_dev)
302 0 : vlan_vid_del(slave_dev, proto, vid);
303 :
304 0 : return 0;
305 : }
306 :
307 : static const struct net_device_ops failover_dev_ops = {
308 : .ndo_open = net_failover_open,
309 : .ndo_stop = net_failover_close,
310 : .ndo_start_xmit = net_failover_start_xmit,
311 : .ndo_select_queue = net_failover_select_queue,
312 : .ndo_get_stats64 = net_failover_get_stats,
313 : .ndo_change_mtu = net_failover_change_mtu,
314 : .ndo_set_rx_mode = net_failover_set_rx_mode,
315 : .ndo_vlan_rx_add_vid = net_failover_vlan_rx_add_vid,
316 : .ndo_vlan_rx_kill_vid = net_failover_vlan_rx_kill_vid,
317 : .ndo_validate_addr = eth_validate_addr,
318 : .ndo_features_check = passthru_features_check,
319 : };
320 :
321 : #define FAILOVER_NAME "net_failover"
322 : #define FAILOVER_VERSION "0.1"
323 :
324 0 : static void nfo_ethtool_get_drvinfo(struct net_device *dev,
325 : struct ethtool_drvinfo *drvinfo)
326 : {
327 0 : strlcpy(drvinfo->driver, FAILOVER_NAME, sizeof(drvinfo->driver));
328 0 : strlcpy(drvinfo->version, FAILOVER_VERSION, sizeof(drvinfo->version));
329 0 : }
330 :
331 0 : static int nfo_ethtool_get_link_ksettings(struct net_device *dev,
332 : struct ethtool_link_ksettings *cmd)
333 : {
334 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
335 0 : struct net_device *slave_dev;
336 :
337 0 : slave_dev = rtnl_dereference(nfo_info->primary_dev);
338 0 : if (!slave_dev || !net_failover_xmit_ready(slave_dev)) {
339 0 : slave_dev = rtnl_dereference(nfo_info->standby_dev);
340 0 : if (!slave_dev || !net_failover_xmit_ready(slave_dev)) {
341 0 : cmd->base.duplex = DUPLEX_UNKNOWN;
342 0 : cmd->base.port = PORT_OTHER;
343 0 : cmd->base.speed = SPEED_UNKNOWN;
344 :
345 0 : return 0;
346 : }
347 : }
348 :
349 0 : return __ethtool_get_link_ksettings(slave_dev, cmd);
350 : }
351 :
352 : static const struct ethtool_ops failover_ethtool_ops = {
353 : .get_drvinfo = nfo_ethtool_get_drvinfo,
354 : .get_link = ethtool_op_get_link,
355 : .get_link_ksettings = nfo_ethtool_get_link_ksettings,
356 : };
357 :
358 : /* Called when slave dev is injecting data into network stack.
359 : * Change the associated network device from lower dev to failover dev.
360 : * note: already called with rcu_read_lock
361 : */
362 0 : static rx_handler_result_t net_failover_handle_frame(struct sk_buff **pskb)
363 : {
364 0 : struct sk_buff *skb = *pskb;
365 0 : struct net_device *dev = rcu_dereference(skb->dev->rx_handler_data);
366 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
367 0 : struct net_device *primary_dev, *standby_dev;
368 :
369 0 : primary_dev = rcu_dereference(nfo_info->primary_dev);
370 0 : standby_dev = rcu_dereference(nfo_info->standby_dev);
371 :
372 0 : if (primary_dev && skb->dev == standby_dev)
373 : return RX_HANDLER_EXACT;
374 :
375 0 : skb->dev = dev;
376 :
377 0 : return RX_HANDLER_ANOTHER;
378 : }
379 :
380 0 : static void net_failover_compute_features(struct net_device *dev)
381 : {
382 0 : netdev_features_t vlan_features = FAILOVER_VLAN_FEATURES &
383 : NETIF_F_ALL_FOR_ALL;
384 0 : netdev_features_t enc_features = FAILOVER_ENC_FEATURES;
385 0 : unsigned short max_hard_header_len = ETH_HLEN;
386 0 : unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
387 : IFF_XMIT_DST_RELEASE_PERM;
388 0 : struct net_failover_info *nfo_info = netdev_priv(dev);
389 0 : struct net_device *primary_dev, *standby_dev;
390 :
391 0 : primary_dev = rcu_dereference(nfo_info->primary_dev);
392 0 : if (primary_dev) {
393 0 : vlan_features =
394 0 : netdev_increment_features(vlan_features,
395 : primary_dev->vlan_features,
396 : FAILOVER_VLAN_FEATURES);
397 0 : enc_features =
398 0 : netdev_increment_features(enc_features,
399 : primary_dev->hw_enc_features,
400 : FAILOVER_ENC_FEATURES);
401 :
402 0 : dst_release_flag &= primary_dev->priv_flags;
403 0 : if (primary_dev->hard_header_len > max_hard_header_len)
404 : max_hard_header_len = primary_dev->hard_header_len;
405 : }
406 :
407 0 : standby_dev = rcu_dereference(nfo_info->standby_dev);
408 0 : if (standby_dev) {
409 0 : vlan_features =
410 0 : netdev_increment_features(vlan_features,
411 : standby_dev->vlan_features,
412 : FAILOVER_VLAN_FEATURES);
413 0 : enc_features =
414 0 : netdev_increment_features(enc_features,
415 : standby_dev->hw_enc_features,
416 : FAILOVER_ENC_FEATURES);
417 :
418 0 : dst_release_flag &= standby_dev->priv_flags;
419 0 : if (standby_dev->hard_header_len > max_hard_header_len)
420 : max_hard_header_len = standby_dev->hard_header_len;
421 : }
422 :
423 0 : dev->vlan_features = vlan_features;
424 0 : dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL;
425 0 : dev->hard_header_len = max_hard_header_len;
426 :
427 0 : dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
428 0 : if (dst_release_flag == (IFF_XMIT_DST_RELEASE |
429 : IFF_XMIT_DST_RELEASE_PERM))
430 0 : dev->priv_flags |= IFF_XMIT_DST_RELEASE;
431 :
432 0 : netdev_change_features(dev);
433 0 : }
434 :
435 0 : static void net_failover_lower_state_changed(struct net_device *slave_dev,
436 : struct net_device *primary_dev,
437 : struct net_device *standby_dev)
438 : {
439 0 : struct netdev_lag_lower_state_info info;
440 :
441 0 : if (netif_carrier_ok(slave_dev))
442 0 : info.link_up = true;
443 : else
444 0 : info.link_up = false;
445 :
446 0 : if (slave_dev == primary_dev) {
447 0 : if (netif_running(primary_dev))
448 0 : info.tx_enabled = true;
449 : else
450 0 : info.tx_enabled = false;
451 : } else {
452 0 : if ((primary_dev && netif_running(primary_dev)) ||
453 0 : (!netif_running(standby_dev)))
454 0 : info.tx_enabled = false;
455 : else
456 0 : info.tx_enabled = true;
457 : }
458 :
459 0 : netdev_lower_state_changed(slave_dev, &info);
460 0 : }
461 :
462 0 : static int net_failover_slave_pre_register(struct net_device *slave_dev,
463 : struct net_device *failover_dev)
464 : {
465 0 : struct net_device *standby_dev, *primary_dev;
466 0 : struct net_failover_info *nfo_info;
467 0 : bool slave_is_standby;
468 :
469 0 : nfo_info = netdev_priv(failover_dev);
470 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
471 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
472 0 : slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent;
473 0 : if (slave_is_standby ? standby_dev : primary_dev) {
474 0 : netdev_err(failover_dev, "%s attempting to register as slave dev when %s already present\n",
475 0 : slave_dev->name,
476 : slave_is_standby ? "standby" : "primary");
477 0 : return -EINVAL;
478 : }
479 :
480 : /* We want to allow only a direct attached VF device as a primary
481 : * netdev. As there is no easy way to check for a VF device, restrict
482 : * this to a pci device.
483 : */
484 0 : if (!slave_is_standby && (!slave_dev->dev.parent ||
485 : !dev_is_pci(slave_dev->dev.parent)))
486 0 : return -EINVAL;
487 :
488 : if (failover_dev->features & NETIF_F_VLAN_CHALLENGED &&
489 0 : vlan_uses_dev(failover_dev)) {
490 : netdev_err(failover_dev, "Device %s is VLAN challenged and failover device has VLAN set up\n",
491 : failover_dev->name);
492 : return -EINVAL;
493 : }
494 :
495 : return 0;
496 : }
497 :
498 0 : static int net_failover_slave_register(struct net_device *slave_dev,
499 : struct net_device *failover_dev)
500 : {
501 0 : struct net_device *standby_dev, *primary_dev;
502 0 : struct net_failover_info *nfo_info;
503 0 : bool slave_is_standby;
504 0 : u32 orig_mtu;
505 0 : int err;
506 :
507 : /* Align MTU of slave with failover dev */
508 0 : orig_mtu = slave_dev->mtu;
509 0 : err = dev_set_mtu(slave_dev, failover_dev->mtu);
510 0 : if (err) {
511 0 : netdev_err(failover_dev, "unable to change mtu of %s to %u register failed\n",
512 0 : slave_dev->name, failover_dev->mtu);
513 0 : goto done;
514 : }
515 :
516 0 : dev_hold(slave_dev);
517 :
518 0 : if (netif_running(failover_dev)) {
519 0 : err = dev_open(slave_dev, NULL);
520 0 : if (err && (err != -EBUSY)) {
521 0 : netdev_err(failover_dev, "Opening slave %s failed err:%d\n",
522 0 : slave_dev->name, err);
523 0 : goto err_dev_open;
524 : }
525 : }
526 :
527 0 : netif_addr_lock_bh(failover_dev);
528 0 : dev_uc_sync_multiple(slave_dev, failover_dev);
529 0 : dev_mc_sync_multiple(slave_dev, failover_dev);
530 0 : netif_addr_unlock_bh(failover_dev);
531 :
532 0 : err = vlan_vids_add_by_dev(slave_dev, failover_dev);
533 0 : if (err) {
534 : netdev_err(failover_dev, "Failed to add vlan ids to device %s err:%d\n",
535 : slave_dev->name, err);
536 : goto err_vlan_add;
537 : }
538 :
539 0 : nfo_info = netdev_priv(failover_dev);
540 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
541 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
542 0 : slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent;
543 :
544 0 : if (slave_is_standby) {
545 0 : rcu_assign_pointer(nfo_info->standby_dev, slave_dev);
546 0 : standby_dev = slave_dev;
547 0 : dev_get_stats(standby_dev, &nfo_info->standby_stats);
548 : } else {
549 0 : rcu_assign_pointer(nfo_info->primary_dev, slave_dev);
550 0 : primary_dev = slave_dev;
551 0 : dev_get_stats(primary_dev, &nfo_info->primary_stats);
552 0 : failover_dev->min_mtu = slave_dev->min_mtu;
553 0 : failover_dev->max_mtu = slave_dev->max_mtu;
554 : }
555 :
556 0 : net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev);
557 0 : net_failover_compute_features(failover_dev);
558 :
559 0 : call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
560 :
561 0 : netdev_info(failover_dev, "failover %s slave:%s registered\n",
562 0 : slave_is_standby ? "standby" : "primary", slave_dev->name);
563 :
564 0 : return 0;
565 :
566 : err_vlan_add:
567 : dev_uc_unsync(slave_dev, failover_dev);
568 : dev_mc_unsync(slave_dev, failover_dev);
569 : dev_close(slave_dev);
570 0 : err_dev_open:
571 0 : dev_put(slave_dev);
572 0 : dev_set_mtu(slave_dev, orig_mtu);
573 : done:
574 : return err;
575 : }
576 :
577 0 : static int net_failover_slave_pre_unregister(struct net_device *slave_dev,
578 : struct net_device *failover_dev)
579 : {
580 0 : struct net_device *standby_dev, *primary_dev;
581 0 : struct net_failover_info *nfo_info;
582 :
583 0 : nfo_info = netdev_priv(failover_dev);
584 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
585 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
586 :
587 0 : if (slave_dev != primary_dev && slave_dev != standby_dev)
588 0 : return -ENODEV;
589 :
590 : return 0;
591 : }
592 :
593 0 : static int net_failover_slave_unregister(struct net_device *slave_dev,
594 : struct net_device *failover_dev)
595 : {
596 0 : struct net_device *standby_dev, *primary_dev;
597 0 : struct net_failover_info *nfo_info;
598 0 : bool slave_is_standby;
599 :
600 0 : nfo_info = netdev_priv(failover_dev);
601 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
602 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
603 :
604 0 : if (WARN_ON_ONCE(slave_dev != primary_dev && slave_dev != standby_dev))
605 : return -ENODEV;
606 :
607 0 : vlan_vids_del_by_dev(slave_dev, failover_dev);
608 0 : dev_uc_unsync(slave_dev, failover_dev);
609 0 : dev_mc_unsync(slave_dev, failover_dev);
610 0 : dev_close(slave_dev);
611 :
612 0 : nfo_info = netdev_priv(failover_dev);
613 0 : dev_get_stats(failover_dev, &nfo_info->failover_stats);
614 :
615 0 : slave_is_standby = slave_dev->dev.parent == failover_dev->dev.parent;
616 0 : if (slave_is_standby) {
617 0 : RCU_INIT_POINTER(nfo_info->standby_dev, NULL);
618 : } else {
619 0 : RCU_INIT_POINTER(nfo_info->primary_dev, NULL);
620 0 : if (standby_dev) {
621 0 : failover_dev->min_mtu = standby_dev->min_mtu;
622 0 : failover_dev->max_mtu = standby_dev->max_mtu;
623 : }
624 : }
625 :
626 0 : dev_put(slave_dev);
627 :
628 0 : net_failover_compute_features(failover_dev);
629 :
630 0 : netdev_info(failover_dev, "failover %s slave:%s unregistered\n",
631 0 : slave_is_standby ? "standby" : "primary", slave_dev->name);
632 :
633 0 : return 0;
634 : }
635 :
636 0 : static int net_failover_slave_link_change(struct net_device *slave_dev,
637 : struct net_device *failover_dev)
638 : {
639 0 : struct net_device *primary_dev, *standby_dev;
640 0 : struct net_failover_info *nfo_info;
641 :
642 0 : nfo_info = netdev_priv(failover_dev);
643 :
644 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
645 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
646 :
647 0 : if (slave_dev != primary_dev && slave_dev != standby_dev)
648 : return -ENODEV;
649 :
650 0 : if ((primary_dev && net_failover_xmit_ready(primary_dev)) ||
651 0 : (standby_dev && net_failover_xmit_ready(standby_dev))) {
652 0 : netif_carrier_on(failover_dev);
653 0 : netif_tx_wake_all_queues(failover_dev);
654 : } else {
655 0 : dev_get_stats(failover_dev, &nfo_info->failover_stats);
656 0 : netif_carrier_off(failover_dev);
657 0 : netif_tx_stop_all_queues(failover_dev);
658 : }
659 :
660 0 : net_failover_lower_state_changed(slave_dev, primary_dev, standby_dev);
661 :
662 0 : return 0;
663 : }
664 :
665 0 : static int net_failover_slave_name_change(struct net_device *slave_dev,
666 : struct net_device *failover_dev)
667 : {
668 0 : struct net_device *primary_dev, *standby_dev;
669 0 : struct net_failover_info *nfo_info;
670 :
671 0 : nfo_info = netdev_priv(failover_dev);
672 :
673 0 : primary_dev = rtnl_dereference(nfo_info->primary_dev);
674 0 : standby_dev = rtnl_dereference(nfo_info->standby_dev);
675 :
676 0 : if (slave_dev != primary_dev && slave_dev != standby_dev)
677 : return -ENODEV;
678 :
679 : /* We need to bring up the slave after the rename by udev in case
680 : * open failed with EBUSY when it was registered.
681 : */
682 0 : dev_open(slave_dev, NULL);
683 :
684 0 : return 0;
685 : }
686 :
687 : static struct failover_ops net_failover_ops = {
688 : .slave_pre_register = net_failover_slave_pre_register,
689 : .slave_register = net_failover_slave_register,
690 : .slave_pre_unregister = net_failover_slave_pre_unregister,
691 : .slave_unregister = net_failover_slave_unregister,
692 : .slave_link_change = net_failover_slave_link_change,
693 : .slave_name_change = net_failover_slave_name_change,
694 : .slave_handle_frame = net_failover_handle_frame,
695 : };
696 :
697 : /**
698 : * net_failover_create - Create and register a failover instance
699 : *
700 : * @standby_dev: standby netdev
701 : *
702 : * Creates a failover netdev and registers a failover instance for a standby
703 : * netdev. Used by paravirtual drivers that use 3-netdev model.
704 : * The failover netdev acts as a master device and controls 2 slave devices -
705 : * the original standby netdev and a VF netdev with the same MAC gets
706 : * registered as primary netdev.
707 : *
708 : * Return: pointer to failover instance
709 : */
710 0 : struct failover *net_failover_create(struct net_device *standby_dev)
711 : {
712 0 : struct device *dev = standby_dev->dev.parent;
713 0 : struct net_device *failover_dev;
714 0 : struct failover *failover;
715 0 : int err;
716 :
717 : /* Alloc at least 2 queues, for now we are going with 16 assuming
718 : * that VF devices being enslaved won't have too many queues.
719 : */
720 0 : failover_dev = alloc_etherdev_mq(sizeof(struct net_failover_info), 16);
721 0 : if (!failover_dev) {
722 0 : dev_err(dev, "Unable to allocate failover_netdev!\n");
723 0 : return ERR_PTR(-ENOMEM);
724 : }
725 :
726 0 : dev_net_set(failover_dev, dev_net(standby_dev));
727 0 : SET_NETDEV_DEV(failover_dev, dev);
728 :
729 0 : failover_dev->netdev_ops = &failover_dev_ops;
730 0 : failover_dev->ethtool_ops = &failover_ethtool_ops;
731 :
732 : /* Initialize the device options */
733 0 : failover_dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
734 0 : failover_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE |
735 : IFF_TX_SKB_SHARING);
736 :
737 : /* don't acquire failover netdev's netif_tx_lock when transmitting */
738 0 : failover_dev->features |= NETIF_F_LLTX;
739 :
740 : /* Don't allow failover devices to change network namespaces. */
741 0 : failover_dev->features |= NETIF_F_NETNS_LOCAL;
742 :
743 0 : failover_dev->hw_features = FAILOVER_VLAN_FEATURES |
744 : NETIF_F_HW_VLAN_CTAG_TX |
745 : NETIF_F_HW_VLAN_CTAG_RX |
746 : NETIF_F_HW_VLAN_CTAG_FILTER;
747 :
748 0 : failover_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
749 0 : failover_dev->features |= failover_dev->hw_features;
750 :
751 0 : memcpy(failover_dev->dev_addr, standby_dev->dev_addr,
752 0 : failover_dev->addr_len);
753 :
754 0 : failover_dev->min_mtu = standby_dev->min_mtu;
755 0 : failover_dev->max_mtu = standby_dev->max_mtu;
756 :
757 0 : err = register_netdev(failover_dev);
758 0 : if (err) {
759 0 : dev_err(dev, "Unable to register failover_dev!\n");
760 0 : goto err_register_netdev;
761 : }
762 :
763 0 : netif_carrier_off(failover_dev);
764 :
765 0 : failover = failover_register(failover_dev, &net_failover_ops);
766 0 : if (IS_ERR(failover)) {
767 0 : err = PTR_ERR(failover);
768 0 : goto err_failover_register;
769 : }
770 :
771 : return failover;
772 :
773 0 : err_failover_register:
774 0 : unregister_netdev(failover_dev);
775 0 : err_register_netdev:
776 0 : free_netdev(failover_dev);
777 :
778 0 : return ERR_PTR(err);
779 : }
780 : EXPORT_SYMBOL_GPL(net_failover_create);
781 :
782 : /**
783 : * net_failover_destroy - Destroy a failover instance
784 : *
785 : * @failover: pointer to failover instance
786 : *
787 : * Unregisters any slave netdevs associated with the failover instance by
788 : * calling failover_slave_unregister().
789 : * unregisters the failover instance itself and finally frees the failover
790 : * netdev. Used by paravirtual drivers that use 3-netdev model.
791 : *
792 : */
793 0 : void net_failover_destroy(struct failover *failover)
794 : {
795 0 : struct net_failover_info *nfo_info;
796 0 : struct net_device *failover_dev;
797 0 : struct net_device *slave_dev;
798 :
799 0 : if (!failover)
800 : return;
801 :
802 0 : failover_dev = rcu_dereference(failover->failover_dev);
803 0 : nfo_info = netdev_priv(failover_dev);
804 :
805 0 : netif_device_detach(failover_dev);
806 :
807 0 : rtnl_lock();
808 :
809 0 : slave_dev = rtnl_dereference(nfo_info->primary_dev);
810 0 : if (slave_dev)
811 0 : failover_slave_unregister(slave_dev);
812 :
813 0 : slave_dev = rtnl_dereference(nfo_info->standby_dev);
814 0 : if (slave_dev)
815 0 : failover_slave_unregister(slave_dev);
816 :
817 0 : failover_unregister(failover);
818 :
819 0 : unregister_netdevice(failover_dev);
820 :
821 0 : rtnl_unlock();
822 :
823 0 : free_netdev(failover_dev);
824 : }
825 : EXPORT_SYMBOL_GPL(net_failover_destroy);
826 :
827 : static __init int
828 1 : net_failover_init(void)
829 : {
830 1 : return 0;
831 : }
832 : module_init(net_failover_init);
833 :
834 : static __exit
835 0 : void net_failover_exit(void)
836 : {
837 0 : }
838 : module_exit(net_failover_exit);
839 :
840 : MODULE_DESCRIPTION("Failover driver for Paravirtual drivers");
841 : MODULE_LICENSE("GPL v2");
|