Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 2 | /* |
Jiri Pirko | 0d572e4 | 2012-06-19 05:54:09 +0000 | [diff] [blame] | 3 | * drivers/net/team/team_mode_activebackup.c - Active-backup mode for team |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com> |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <net/rtnetlink.h> |
| 14 | #include <linux/if_team.h> |
| 15 | |
| 16 | struct ab_priv { |
| 17 | struct team_port __rcu *active_port; |
Jiri Pirko | 675b804 | 2013-02-01 08:17:26 +0000 | [diff] [blame] | 18 | struct team_option_inst_info *ap_opt_inst_info; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | static struct ab_priv *ab_priv(struct team *team) |
| 22 | { |
| 23 | return (struct ab_priv *) &team->mode_priv; |
| 24 | } |
| 25 | |
| 26 | static rx_handler_result_t ab_receive(struct team *team, struct team_port *port, |
| 27 | struct sk_buff *skb) { |
| 28 | struct team_port *active_port; |
| 29 | |
| 30 | active_port = rcu_dereference(ab_priv(team)->active_port); |
| 31 | if (active_port != port) |
| 32 | return RX_HANDLER_EXACT; |
| 33 | return RX_HANDLER_ANOTHER; |
| 34 | } |
| 35 | |
| 36 | static bool ab_transmit(struct team *team, struct sk_buff *skb) |
| 37 | { |
| 38 | struct team_port *active_port; |
| 39 | |
Jiri Pirko | d1904fb | 2012-06-19 05:54:21 +0000 | [diff] [blame] | 40 | active_port = rcu_dereference_bh(ab_priv(team)->active_port); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 41 | if (unlikely(!active_port)) |
| 42 | goto drop; |
Jiri Pirko | bd2d083 | 2012-07-17 05:22:36 +0000 | [diff] [blame] | 43 | if (team_dev_queue_xmit(team, active_port, skb)) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 44 | return false; |
| 45 | return true; |
| 46 | |
| 47 | drop: |
| 48 | dev_kfree_skb_any(skb); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | static void ab_port_leave(struct team *team, struct team_port *port) |
| 53 | { |
Jiri Pirko | 675b804 | 2013-02-01 08:17:26 +0000 | [diff] [blame] | 54 | if (ab_priv(team)->active_port == port) { |
Eric Dumazet | 2cfa5a0 | 2011-11-23 07:09:32 +0000 | [diff] [blame] | 55 | RCU_INIT_POINTER(ab_priv(team)->active_port, NULL); |
Jiri Pirko | 675b804 | 2013-02-01 08:17:26 +0000 | [diff] [blame] | 56 | team_option_inst_set_change(ab_priv(team)->ap_opt_inst_info); |
| 57 | } |
| 58 | } |
| 59 | |
Zhengchao Shao | de3ecc4 | 2023-08-07 09:25:54 +0800 | [diff] [blame] | 60 | static void ab_active_port_init(struct team *team, |
| 61 | struct team_option_inst_info *info) |
Jiri Pirko | 675b804 | 2013-02-01 08:17:26 +0000 | [diff] [blame] | 62 | { |
| 63 | ab_priv(team)->ap_opt_inst_info = info; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Zhengchao Shao | c3b41f4 | 2023-08-07 09:25:55 +0800 | [diff] [blame] | 66 | static void ab_active_port_get(struct team *team, struct team_gsetter_ctx *ctx) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 67 | { |
Jiri Pirko | 6dab015 | 2012-06-20 08:39:39 +0000 | [diff] [blame] | 68 | struct team_port *active_port; |
| 69 | |
| 70 | active_port = rcu_dereference_protected(ab_priv(team)->active_port, |
| 71 | lockdep_is_held(&team->lock)); |
| 72 | if (active_port) |
| 73 | ctx->data.u32_val = active_port->dev->ifindex; |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 74 | else |
| 75 | ctx->data.u32_val = 0; |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 78 | static int ab_active_port_set(struct team *team, struct team_gsetter_ctx *ctx) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 79 | { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 80 | struct team_port *port; |
| 81 | |
Jiri Pirko | 679b160 | 2012-04-10 05:15:45 +0000 | [diff] [blame] | 82 | list_for_each_entry(port, &team->port_list, list) { |
Jiri Pirko | 80f7c66 | 2012-04-10 05:15:42 +0000 | [diff] [blame] | 83 | if (port->dev->ifindex == ctx->data.u32_val) { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 84 | rcu_assign_pointer(ab_priv(team)->active_port, port); |
| 85 | return 0; |
| 86 | } |
| 87 | } |
| 88 | return -ENOENT; |
| 89 | } |
| 90 | |
Jiri Pirko | 358b838 | 2011-11-16 11:09:09 +0000 | [diff] [blame] | 91 | static const struct team_option ab_options[] = { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 92 | { |
| 93 | .name = "activeport", |
| 94 | .type = TEAM_OPTION_TYPE_U32, |
Jiri Pirko | 675b804 | 2013-02-01 08:17:26 +0000 | [diff] [blame] | 95 | .init = ab_active_port_init, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 96 | .getter = ab_active_port_get, |
| 97 | .setter = ab_active_port_set, |
| 98 | }, |
| 99 | }; |
| 100 | |
Jiri Pirko | cade455 | 2012-04-10 05:15:46 +0000 | [diff] [blame] | 101 | static int ab_init(struct team *team) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 102 | { |
Jiri Pirko | 358b838 | 2011-11-16 11:09:09 +0000 | [diff] [blame] | 103 | return team_options_register(team, ab_options, ARRAY_SIZE(ab_options)); |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jiri Pirko | cade455 | 2012-04-10 05:15:46 +0000 | [diff] [blame] | 106 | static void ab_exit(struct team *team) |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 107 | { |
| 108 | team_options_unregister(team, ab_options, ARRAY_SIZE(ab_options)); |
| 109 | } |
| 110 | |
| 111 | static const struct team_mode_ops ab_mode_ops = { |
| 112 | .init = ab_init, |
| 113 | .exit = ab_exit, |
| 114 | .receive = ab_receive, |
| 115 | .transmit = ab_transmit, |
| 116 | .port_leave = ab_port_leave, |
| 117 | }; |
| 118 | |
Jiri Pirko | 0402788 | 2012-06-19 05:54:03 +0000 | [diff] [blame] | 119 | static const struct team_mode ab_mode = { |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 120 | .kind = "activebackup", |
| 121 | .owner = THIS_MODULE, |
| 122 | .priv_size = sizeof(struct ab_priv), |
| 123 | .ops = &ab_mode_ops, |
Jiri Pirko | 8fd7285 | 2015-12-03 12:12:13 +0100 | [diff] [blame] | 124 | .lag_tx_type = NETDEV_LAG_TX_TYPE_ACTIVEBACKUP, |
Jiri Pirko | 3d249d4 | 2011-11-11 22:16:48 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static int __init ab_init_module(void) |
| 128 | { |
| 129 | return team_mode_register(&ab_mode); |
| 130 | } |
| 131 | |
| 132 | static void __exit ab_cleanup_module(void) |
| 133 | { |
| 134 | team_mode_unregister(&ab_mode); |
| 135 | } |
| 136 | |
| 137 | module_init(ab_init_module); |
| 138 | module_exit(ab_cleanup_module); |
| 139 | |
| 140 | MODULE_LICENSE("GPL v2"); |
| 141 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); |
| 142 | MODULE_DESCRIPTION("Active-backup mode for team"); |
Zhang Shengju | 3a5f899 | 2017-06-01 15:37:02 +0800 | [diff] [blame] | 143 | MODULE_ALIAS_TEAM_MODE("activebackup"); |