1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 // Copyright (c) 2019 Mellanox Technologies. 3 4 #include <linux/mlx5/fs.h> 5 #include "eswitch.h" 6 7 struct mlx5_termtbl_handle { 8 struct hlist_node termtbl_hlist; 9 10 struct mlx5_flow_table *termtbl; 11 struct mlx5_flow_act flow_act; 12 struct mlx5_flow_destination dest; 13 14 struct mlx5_flow_handle *rule; 15 int ref_count; 16 }; 17 18 static u32 19 mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act, 20 struct mlx5_flow_destination *dest) 21 { 22 u32 hash; 23 24 hash = jhash_1word(flow_act->action, 0); 25 hash = jhash((const void *)&flow_act->vlan, 26 sizeof(flow_act->vlan), hash); 27 hash = jhash((const void *)&dest->vport.num, 28 sizeof(dest->vport.num), hash); 29 hash = jhash((const void *)&dest->vport.vhca_id, 30 sizeof(dest->vport.num), hash); 31 return hash; 32 } 33 34 static int 35 mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act *flow_act1, 36 struct mlx5_flow_destination *dest1, 37 struct mlx5_flow_act *flow_act2, 38 struct mlx5_flow_destination *dest2) 39 { 40 return flow_act1->action != flow_act2->action || 41 dest1->vport.num != dest2->vport.num || 42 dest1->vport.vhca_id != dest2->vport.vhca_id || 43 memcmp(&flow_act1->vlan, &flow_act2->vlan, 44 sizeof(flow_act1->vlan)); 45 } 46 47 static int 48 mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev, 49 struct mlx5_termtbl_handle *tt, 50 struct mlx5_flow_act *flow_act) 51 { 52 static const struct mlx5_flow_spec spec = {}; 53 struct mlx5_flow_namespace *root_ns; 54 int prio, flags; 55 int err; 56 57 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB); 58 if (!root_ns) { 59 esw_warn(dev, "Failed to get FDB flow namespace\n"); 60 return -EOPNOTSUPP; 61 } 62 63 /* As this is the terminating action then the termination table is the 64 * same prio as the slow path 65 */ 66 prio = FDB_SLOW_PATH; 67 flags = MLX5_FLOW_TABLE_TERMINATION; 68 tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, prio, 1, 1, 69 0, flags); 70 if (IS_ERR(tt->termtbl)) { 71 esw_warn(dev, "Failed to create termination table\n"); 72 return -EOPNOTSUPP; 73 } 74 75 tt->rule = mlx5_add_flow_rules(tt->termtbl, &spec, flow_act, 76 &tt->dest, 1); 77 78 if (IS_ERR(tt->rule)) { 79 esw_warn(dev, "Failed to create termination table rule\n"); 80 goto add_flow_err; 81 } 82 return 0; 83 84 add_flow_err: 85 err = mlx5_destroy_flow_table(tt->termtbl); 86 if (err) 87 esw_warn(dev, "Failed to destroy termination table\n"); 88 89 return -EOPNOTSUPP; 90 } 91 92 static struct mlx5_termtbl_handle * 93 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw, 94 struct mlx5_flow_act *flow_act, 95 struct mlx5_flow_destination *dest) 96 { 97 struct mlx5_termtbl_handle *tt; 98 bool found = false; 99 u32 hash_key; 100 int err; 101 102 mutex_lock(&esw->offloads.termtbl_mutex); 103 104 hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest); 105 hash_for_each_possible(esw->offloads.termtbl_tbl, tt, 106 termtbl_hlist, hash_key) { 107 if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest, 108 flow_act, dest)) { 109 found = true; 110 break; 111 } 112 } 113 if (found) 114 goto tt_add_ref; 115 116 tt = kzalloc(sizeof(*tt), GFP_KERNEL); 117 if (!tt) { 118 err = -ENOMEM; 119 goto tt_create_err; 120 } 121 122 tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT; 123 tt->dest.vport.num = dest->vport.num; 124 tt->dest.vport.vhca_id = dest->vport.vhca_id; 125 memcpy(&tt->flow_act, flow_act, sizeof(*flow_act)); 126 127 err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act); 128 if (err) { 129 esw_warn(esw->dev, "Failed to create termination table\n"); 130 goto tt_create_err; 131 } 132 hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key); 133 tt_add_ref: 134 tt->ref_count++; 135 mutex_unlock(&esw->offloads.termtbl_mutex); 136 return tt; 137 tt_create_err: 138 kfree(tt); 139 mutex_unlock(&esw->offloads.termtbl_mutex); 140 return ERR_PTR(err); 141 } 142 143 void 144 mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw, 145 struct mlx5_termtbl_handle *tt) 146 { 147 mutex_lock(&esw->offloads.termtbl_mutex); 148 if (--tt->ref_count == 0) 149 hash_del(&tt->termtbl_hlist); 150 mutex_unlock(&esw->offloads.termtbl_mutex); 151 152 if (!tt->ref_count) { 153 mlx5_del_flow_rules(tt->rule); 154 mlx5_destroy_flow_table(tt->termtbl); 155 kfree(tt); 156 } 157 } 158 159 static void 160 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src, 161 struct mlx5_flow_act *dst) 162 { 163 if (!(src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)) 164 return; 165 166 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH; 167 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH; 168 memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0])); 169 memset(&src->vlan[0], 0, sizeof(src->vlan[0])); 170 171 if (!(src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2)) 172 return; 173 174 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2; 175 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2; 176 memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1])); 177 memset(&src->vlan[1], 0, sizeof(src->vlan[1])); 178 } 179 180 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw, 181 const struct mlx5_flow_spec *spec) 182 { 183 u32 port_mask, port_value; 184 185 if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source)) 186 return spec->flow_context.flow_source == 187 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK; 188 189 port_mask = MLX5_GET(fte_match_param, spec->match_criteria, 190 misc_parameters.source_port); 191 port_value = MLX5_GET(fte_match_param, spec->match_value, 192 misc_parameters.source_port); 193 return (port_mask & port_value & 0xffff) == MLX5_VPORT_UPLINK; 194 } 195 196 bool 197 mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw, 198 struct mlx5_flow_act *flow_act, 199 struct mlx5_flow_spec *spec) 200 { 201 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table)) 202 return false; 203 204 /* push vlan on RX */ 205 return (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) && 206 mlx5_eswitch_offload_is_uplink_port(esw, spec); 207 } 208 209 struct mlx5_flow_handle * 210 mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw, 211 struct mlx5_flow_table *fdb, 212 struct mlx5_flow_spec *spec, 213 struct mlx5_esw_flow_attr *attr, 214 struct mlx5_flow_act *flow_act, 215 struct mlx5_flow_destination *dest, 216 int num_dest) 217 { 218 struct mlx5_flow_act term_tbl_act = {}; 219 struct mlx5_flow_handle *rule = NULL; 220 bool term_table_created = false; 221 int num_vport_dests = 0; 222 int i, curr_dest; 223 224 mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act); 225 term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; 226 227 for (i = 0; i < num_dest; i++) { 228 struct mlx5_termtbl_handle *tt; 229 230 /* only vport destinations can be terminated */ 231 if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT) 232 continue; 233 234 /* get the terminating table for the action list */ 235 tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act, 236 &dest[i]); 237 if (IS_ERR(tt)) { 238 esw_warn(esw->dev, "Failed to create termination table\n"); 239 goto revert_changes; 240 } 241 attr->dests[num_vport_dests].termtbl = tt; 242 num_vport_dests++; 243 244 /* link the destination with the termination table */ 245 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; 246 dest[i].ft = tt->termtbl; 247 term_table_created = true; 248 } 249 250 /* at least one destination should reference a termination table */ 251 if (!term_table_created) 252 goto revert_changes; 253 254 /* create the FTE */ 255 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest); 256 if (IS_ERR(rule)) 257 goto revert_changes; 258 259 goto out; 260 261 revert_changes: 262 /* revert the changes that were made to the original flow_act 263 * and fall-back to the original rule actions 264 */ 265 mlx5_eswitch_termtbl_actions_move(&term_tbl_act, flow_act); 266 267 for (curr_dest = 0; curr_dest < num_vport_dests; curr_dest++) { 268 struct mlx5_termtbl_handle *tt = attr->dests[curr_dest].termtbl; 269 270 /* search for the destination associated with the 271 * current term table 272 */ 273 for (i = 0; i < num_dest; i++) { 274 if (dest[i].ft != tt->termtbl) 275 continue; 276 277 memset(&dest[i], 0, sizeof(dest[i])); 278 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT; 279 dest[i].vport.num = tt->dest.vport.num; 280 dest[i].vport.vhca_id = tt->dest.vport.vhca_id; 281 mlx5_eswitch_termtbl_put(esw, tt); 282 break; 283 } 284 } 285 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest); 286 out: 287 return rule; 288 } 289