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