1 /* 2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #include <linux/mutex.h> 34 #include <linux/mlx5/driver.h> 35 #include <linux/mlx5/vport.h> 36 #include <linux/mlx5/eswitch.h> 37 38 #include "mlx5_core.h" 39 #include "fs_core.h" 40 #include "fs_cmd.h" 41 #include "diag/fs_tracepoint.h" 42 #include "accel/ipsec.h" 43 #include "fpga/ipsec.h" 44 #include "eswitch.h" 45 46 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\ 47 sizeof(struct init_tree_node)) 48 49 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\ 50 ...) {.type = FS_TYPE_PRIO,\ 51 .min_ft_level = min_level_val,\ 52 .num_levels = num_levels_val,\ 53 .num_leaf_prios = num_prios_val,\ 54 .caps = caps_val,\ 55 .children = (struct init_tree_node[]) {__VA_ARGS__},\ 56 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \ 57 } 58 59 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\ 60 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\ 61 __VA_ARGS__)\ 62 63 #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \ 64 .def_miss_action = def_miss_act,\ 65 .children = (struct init_tree_node[]) {__VA_ARGS__},\ 66 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \ 67 } 68 69 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\ 70 sizeof(long)) 71 72 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap)) 73 74 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \ 75 .caps = (long[]) {__VA_ARGS__} } 76 77 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \ 78 FS_CAP(flow_table_properties_nic_receive.modify_root), \ 79 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \ 80 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)) 81 82 #define FS_CHAINING_CAPS_EGRESS \ 83 FS_REQUIRED_CAPS( \ 84 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \ 85 FS_CAP(flow_table_properties_nic_transmit.modify_root), \ 86 FS_CAP(flow_table_properties_nic_transmit \ 87 .identified_miss_table_mode), \ 88 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify)) 89 90 #define LEFTOVERS_NUM_LEVELS 1 91 #define LEFTOVERS_NUM_PRIOS 1 92 93 #define BY_PASS_PRIO_NUM_LEVELS 1 94 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\ 95 LEFTOVERS_NUM_PRIOS) 96 97 #define ETHTOOL_PRIO_NUM_LEVELS 1 98 #define ETHTOOL_NUM_PRIOS 11 99 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS) 100 /* Vlan, mac, ttc, inner ttc, aRFS */ 101 #define KERNEL_NIC_PRIO_NUM_LEVELS 5 102 #define KERNEL_NIC_NUM_PRIOS 1 103 /* One more level for tc */ 104 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1) 105 106 #define KERNEL_NIC_TC_NUM_PRIOS 1 107 #define KERNEL_NIC_TC_NUM_LEVELS 2 108 109 #define ANCHOR_NUM_LEVELS 1 110 #define ANCHOR_NUM_PRIOS 1 111 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1) 112 113 #define OFFLOADS_MAX_FT 1 114 #define OFFLOADS_NUM_PRIOS 1 115 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1) 116 117 #define LAG_PRIO_NUM_LEVELS 1 118 #define LAG_NUM_PRIOS 1 119 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1) 120 121 struct node_caps { 122 size_t arr_sz; 123 long *caps; 124 }; 125 126 static struct init_tree_node { 127 enum fs_node_type type; 128 struct init_tree_node *children; 129 int ar_size; 130 struct node_caps caps; 131 int min_ft_level; 132 int num_leaf_prios; 133 int prio; 134 int num_levels; 135 enum mlx5_flow_table_miss_action def_miss_action; 136 } root_fs = { 137 .type = FS_TYPE_NAMESPACE, 138 .ar_size = 7, 139 .children = (struct init_tree_node[]){ 140 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS, 141 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 142 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS, 143 BY_PASS_PRIO_NUM_LEVELS))), 144 ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS, 145 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 146 ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS, 147 LAG_PRIO_NUM_LEVELS))), 148 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {}, 149 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 150 ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, 151 OFFLOADS_MAX_FT))), 152 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS, 153 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 154 ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS, 155 ETHTOOL_PRIO_NUM_LEVELS))), 156 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {}, 157 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 158 ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS, 159 KERNEL_NIC_TC_NUM_LEVELS), 160 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS, 161 KERNEL_NIC_PRIO_NUM_LEVELS))), 162 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS, 163 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 164 ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, 165 LEFTOVERS_NUM_LEVELS))), 166 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {}, 167 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 168 ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, 169 ANCHOR_NUM_LEVELS))), 170 } 171 }; 172 173 static struct init_tree_node egress_root_fs = { 174 .type = FS_TYPE_NAMESPACE, 175 .ar_size = 1, 176 .children = (struct init_tree_node[]) { 177 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0, 178 FS_CHAINING_CAPS_EGRESS, 179 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 180 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS, 181 BY_PASS_PRIO_NUM_LEVELS))), 182 } 183 }; 184 185 #define RDMA_RX_BYPASS_PRIO 0 186 #define RDMA_RX_KERNEL_PRIO 1 187 static struct init_tree_node rdma_rx_root_fs = { 188 .type = FS_TYPE_NAMESPACE, 189 .ar_size = 2, 190 .children = (struct init_tree_node[]) { 191 [RDMA_RX_BYPASS_PRIO] = 192 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS, 0, 193 FS_CHAINING_CAPS, 194 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF, 195 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS, 196 BY_PASS_PRIO_NUM_LEVELS))), 197 [RDMA_RX_KERNEL_PRIO] = 198 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS + 1, 0, 199 FS_CHAINING_CAPS, 200 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN, 201 ADD_MULTIPLE_PRIO(1, 1))), 202 } 203 }; 204 205 enum fs_i_lock_class { 206 FS_LOCK_GRANDPARENT, 207 FS_LOCK_PARENT, 208 FS_LOCK_CHILD 209 }; 210 211 static const struct rhashtable_params rhash_fte = { 212 .key_len = FIELD_SIZEOF(struct fs_fte, val), 213 .key_offset = offsetof(struct fs_fte, val), 214 .head_offset = offsetof(struct fs_fte, hash), 215 .automatic_shrinking = true, 216 .min_size = 1, 217 }; 218 219 static const struct rhashtable_params rhash_fg = { 220 .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask), 221 .key_offset = offsetof(struct mlx5_flow_group, mask), 222 .head_offset = offsetof(struct mlx5_flow_group, hash), 223 .automatic_shrinking = true, 224 .min_size = 1, 225 226 }; 227 228 static void del_hw_flow_table(struct fs_node *node); 229 static void del_hw_flow_group(struct fs_node *node); 230 static void del_hw_fte(struct fs_node *node); 231 static void del_sw_flow_table(struct fs_node *node); 232 static void del_sw_flow_group(struct fs_node *node); 233 static void del_sw_fte(struct fs_node *node); 234 static void del_sw_prio(struct fs_node *node); 235 static void del_sw_ns(struct fs_node *node); 236 /* Delete rule (destination) is special case that 237 * requires to lock the FTE for all the deletion process. 238 */ 239 static void del_sw_hw_rule(struct fs_node *node); 240 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1, 241 struct mlx5_flow_destination *d2); 242 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns); 243 static struct mlx5_flow_rule * 244 find_flow_rule(struct fs_fte *fte, 245 struct mlx5_flow_destination *dest); 246 247 static void tree_init_node(struct fs_node *node, 248 void (*del_hw_func)(struct fs_node *), 249 void (*del_sw_func)(struct fs_node *)) 250 { 251 refcount_set(&node->refcount, 1); 252 INIT_LIST_HEAD(&node->list); 253 INIT_LIST_HEAD(&node->children); 254 init_rwsem(&node->lock); 255 node->del_hw_func = del_hw_func; 256 node->del_sw_func = del_sw_func; 257 node->active = false; 258 } 259 260 static void tree_add_node(struct fs_node *node, struct fs_node *parent) 261 { 262 if (parent) 263 refcount_inc(&parent->refcount); 264 node->parent = parent; 265 266 /* Parent is the root */ 267 if (!parent) 268 node->root = node; 269 else 270 node->root = parent->root; 271 } 272 273 static int tree_get_node(struct fs_node *node) 274 { 275 return refcount_inc_not_zero(&node->refcount); 276 } 277 278 static void nested_down_read_ref_node(struct fs_node *node, 279 enum fs_i_lock_class class) 280 { 281 if (node) { 282 down_read_nested(&node->lock, class); 283 refcount_inc(&node->refcount); 284 } 285 } 286 287 static void nested_down_write_ref_node(struct fs_node *node, 288 enum fs_i_lock_class class) 289 { 290 if (node) { 291 down_write_nested(&node->lock, class); 292 refcount_inc(&node->refcount); 293 } 294 } 295 296 static void down_write_ref_node(struct fs_node *node, bool locked) 297 { 298 if (node) { 299 if (!locked) 300 down_write(&node->lock); 301 refcount_inc(&node->refcount); 302 } 303 } 304 305 static void up_read_ref_node(struct fs_node *node) 306 { 307 refcount_dec(&node->refcount); 308 up_read(&node->lock); 309 } 310 311 static void up_write_ref_node(struct fs_node *node, bool locked) 312 { 313 refcount_dec(&node->refcount); 314 if (!locked) 315 up_write(&node->lock); 316 } 317 318 static void tree_put_node(struct fs_node *node, bool locked) 319 { 320 struct fs_node *parent_node = node->parent; 321 322 if (refcount_dec_and_test(&node->refcount)) { 323 if (node->del_hw_func) 324 node->del_hw_func(node); 325 if (parent_node) { 326 /* Only root namespace doesn't have parent and we just 327 * need to free its node. 328 */ 329 down_write_ref_node(parent_node, locked); 330 list_del_init(&node->list); 331 if (node->del_sw_func) 332 node->del_sw_func(node); 333 up_write_ref_node(parent_node, locked); 334 } else { 335 kfree(node); 336 } 337 node = NULL; 338 } 339 if (!node && parent_node) 340 tree_put_node(parent_node, locked); 341 } 342 343 static int tree_remove_node(struct fs_node *node, bool locked) 344 { 345 if (refcount_read(&node->refcount) > 1) { 346 refcount_dec(&node->refcount); 347 return -EEXIST; 348 } 349 tree_put_node(node, locked); 350 return 0; 351 } 352 353 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns, 354 unsigned int prio) 355 { 356 struct fs_prio *iter_prio; 357 358 fs_for_each_prio(iter_prio, ns) { 359 if (iter_prio->prio == prio) 360 return iter_prio; 361 } 362 363 return NULL; 364 } 365 366 static bool check_valid_spec(const struct mlx5_flow_spec *spec) 367 { 368 int i; 369 370 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++) 371 if (spec->match_value[i] & ~spec->match_criteria[i]) { 372 pr_warn("mlx5_core: match_value differs from match_criteria\n"); 373 return false; 374 } 375 376 return true; 377 } 378 379 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node) 380 { 381 struct fs_node *root; 382 struct mlx5_flow_namespace *ns; 383 384 root = node->root; 385 386 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) { 387 pr_warn("mlx5: flow steering node is not in tree or garbaged\n"); 388 return NULL; 389 } 390 391 ns = container_of(root, struct mlx5_flow_namespace, node); 392 return container_of(ns, struct mlx5_flow_root_namespace, ns); 393 } 394 395 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node) 396 { 397 struct mlx5_flow_root_namespace *root = find_root(node); 398 399 if (root) 400 return root->dev->priv.steering; 401 return NULL; 402 } 403 404 static inline struct mlx5_core_dev *get_dev(struct fs_node *node) 405 { 406 struct mlx5_flow_root_namespace *root = find_root(node); 407 408 if (root) 409 return root->dev; 410 return NULL; 411 } 412 413 static void del_sw_ns(struct fs_node *node) 414 { 415 kfree(node); 416 } 417 418 static void del_sw_prio(struct fs_node *node) 419 { 420 kfree(node); 421 } 422 423 static void del_hw_flow_table(struct fs_node *node) 424 { 425 struct mlx5_flow_root_namespace *root; 426 struct mlx5_flow_table *ft; 427 struct mlx5_core_dev *dev; 428 int err; 429 430 fs_get_obj(ft, node); 431 dev = get_dev(&ft->node); 432 root = find_root(&ft->node); 433 trace_mlx5_fs_del_ft(ft); 434 435 if (node->active) { 436 err = root->cmds->destroy_flow_table(root, ft); 437 if (err) 438 mlx5_core_warn(dev, "flow steering can't destroy ft\n"); 439 } 440 } 441 442 static void del_sw_flow_table(struct fs_node *node) 443 { 444 struct mlx5_flow_table *ft; 445 struct fs_prio *prio; 446 447 fs_get_obj(ft, node); 448 449 rhltable_destroy(&ft->fgs_hash); 450 fs_get_obj(prio, ft->node.parent); 451 prio->num_ft--; 452 kfree(ft); 453 } 454 455 static void modify_fte(struct fs_fte *fte) 456 { 457 struct mlx5_flow_root_namespace *root; 458 struct mlx5_flow_table *ft; 459 struct mlx5_flow_group *fg; 460 struct mlx5_core_dev *dev; 461 int err; 462 463 fs_get_obj(fg, fte->node.parent); 464 fs_get_obj(ft, fg->node.parent); 465 dev = get_dev(&fte->node); 466 467 root = find_root(&ft->node); 468 err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte); 469 if (err) 470 mlx5_core_warn(dev, 471 "%s can't del rule fg id=%d fte_index=%d\n", 472 __func__, fg->id, fte->index); 473 fte->modify_mask = 0; 474 } 475 476 static void del_sw_hw_rule(struct fs_node *node) 477 { 478 struct mlx5_flow_rule *rule; 479 struct fs_fte *fte; 480 481 fs_get_obj(rule, node); 482 fs_get_obj(fte, rule->node.parent); 483 trace_mlx5_fs_del_rule(rule); 484 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) { 485 mutex_lock(&rule->dest_attr.ft->lock); 486 list_del(&rule->next_ft); 487 mutex_unlock(&rule->dest_attr.ft->lock); 488 } 489 490 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER && 491 --fte->dests_size) { 492 fte->modify_mask |= 493 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) | 494 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS); 495 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT; 496 goto out; 497 } 498 499 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) && 500 --fte->dests_size) { 501 fte->modify_mask |= 502 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST); 503 } 504 out: 505 kfree(rule); 506 } 507 508 static void del_hw_fte(struct fs_node *node) 509 { 510 struct mlx5_flow_root_namespace *root; 511 struct mlx5_flow_table *ft; 512 struct mlx5_flow_group *fg; 513 struct mlx5_core_dev *dev; 514 struct fs_fte *fte; 515 int err; 516 517 fs_get_obj(fte, node); 518 fs_get_obj(fg, fte->node.parent); 519 fs_get_obj(ft, fg->node.parent); 520 521 trace_mlx5_fs_del_fte(fte); 522 dev = get_dev(&ft->node); 523 root = find_root(&ft->node); 524 if (node->active) { 525 err = root->cmds->delete_fte(root, ft, fte); 526 if (err) 527 mlx5_core_warn(dev, 528 "flow steering can't delete fte in index %d of flow group id %d\n", 529 fte->index, fg->id); 530 node->active = 0; 531 } 532 } 533 534 static void del_sw_fte(struct fs_node *node) 535 { 536 struct mlx5_flow_steering *steering = get_steering(node); 537 struct mlx5_flow_group *fg; 538 struct fs_fte *fte; 539 int err; 540 541 fs_get_obj(fte, node); 542 fs_get_obj(fg, fte->node.parent); 543 544 err = rhashtable_remove_fast(&fg->ftes_hash, 545 &fte->hash, 546 rhash_fte); 547 WARN_ON(err); 548 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index); 549 kmem_cache_free(steering->ftes_cache, fte); 550 } 551 552 static void del_hw_flow_group(struct fs_node *node) 553 { 554 struct mlx5_flow_root_namespace *root; 555 struct mlx5_flow_group *fg; 556 struct mlx5_flow_table *ft; 557 struct mlx5_core_dev *dev; 558 559 fs_get_obj(fg, node); 560 fs_get_obj(ft, fg->node.parent); 561 dev = get_dev(&ft->node); 562 trace_mlx5_fs_del_fg(fg); 563 564 root = find_root(&ft->node); 565 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg)) 566 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n", 567 fg->id, ft->id); 568 } 569 570 static void del_sw_flow_group(struct fs_node *node) 571 { 572 struct mlx5_flow_steering *steering = get_steering(node); 573 struct mlx5_flow_group *fg; 574 struct mlx5_flow_table *ft; 575 int err; 576 577 fs_get_obj(fg, node); 578 fs_get_obj(ft, fg->node.parent); 579 580 rhashtable_destroy(&fg->ftes_hash); 581 ida_destroy(&fg->fte_allocator); 582 if (ft->autogroup.active) 583 ft->autogroup.num_groups--; 584 err = rhltable_remove(&ft->fgs_hash, 585 &fg->hash, 586 rhash_fg); 587 WARN_ON(err); 588 kmem_cache_free(steering->fgs_cache, fg); 589 } 590 591 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte) 592 { 593 int index; 594 int ret; 595 596 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL); 597 if (index < 0) 598 return index; 599 600 fte->index = index + fg->start_index; 601 ret = rhashtable_insert_fast(&fg->ftes_hash, 602 &fte->hash, 603 rhash_fte); 604 if (ret) 605 goto err_ida_remove; 606 607 tree_add_node(&fte->node, &fg->node); 608 list_add_tail(&fte->node.list, &fg->node.children); 609 return 0; 610 611 err_ida_remove: 612 ida_simple_remove(&fg->fte_allocator, index); 613 return ret; 614 } 615 616 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft, 617 const struct mlx5_flow_spec *spec, 618 struct mlx5_flow_act *flow_act) 619 { 620 struct mlx5_flow_steering *steering = get_steering(&ft->node); 621 struct fs_fte *fte; 622 623 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL); 624 if (!fte) 625 return ERR_PTR(-ENOMEM); 626 627 memcpy(fte->val, &spec->match_value, sizeof(fte->val)); 628 fte->node.type = FS_TYPE_FLOW_ENTRY; 629 fte->action = *flow_act; 630 fte->flow_context = spec->flow_context; 631 632 tree_init_node(&fte->node, NULL, del_sw_fte); 633 634 return fte; 635 } 636 637 static void dealloc_flow_group(struct mlx5_flow_steering *steering, 638 struct mlx5_flow_group *fg) 639 { 640 rhashtable_destroy(&fg->ftes_hash); 641 kmem_cache_free(steering->fgs_cache, fg); 642 } 643 644 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering, 645 u8 match_criteria_enable, 646 const void *match_criteria, 647 int start_index, 648 int end_index) 649 { 650 struct mlx5_flow_group *fg; 651 int ret; 652 653 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL); 654 if (!fg) 655 return ERR_PTR(-ENOMEM); 656 657 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte); 658 if (ret) { 659 kmem_cache_free(steering->fgs_cache, fg); 660 return ERR_PTR(ret); 661 } 662 663 ida_init(&fg->fte_allocator); 664 fg->mask.match_criteria_enable = match_criteria_enable; 665 memcpy(&fg->mask.match_criteria, match_criteria, 666 sizeof(fg->mask.match_criteria)); 667 fg->node.type = FS_TYPE_FLOW_GROUP; 668 fg->start_index = start_index; 669 fg->max_ftes = end_index - start_index + 1; 670 671 return fg; 672 } 673 674 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft, 675 u8 match_criteria_enable, 676 const void *match_criteria, 677 int start_index, 678 int end_index, 679 struct list_head *prev) 680 { 681 struct mlx5_flow_steering *steering = get_steering(&ft->node); 682 struct mlx5_flow_group *fg; 683 int ret; 684 685 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria, 686 start_index, end_index); 687 if (IS_ERR(fg)) 688 return fg; 689 690 /* initialize refcnt, add to parent list */ 691 ret = rhltable_insert(&ft->fgs_hash, 692 &fg->hash, 693 rhash_fg); 694 if (ret) { 695 dealloc_flow_group(steering, fg); 696 return ERR_PTR(ret); 697 } 698 699 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group); 700 tree_add_node(&fg->node, &ft->node); 701 /* Add node to group list */ 702 list_add(&fg->node.list, prev); 703 atomic_inc(&ft->node.version); 704 705 return fg; 706 } 707 708 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte, 709 enum fs_flow_table_type table_type, 710 enum fs_flow_table_op_mod op_mod, 711 u32 flags) 712 { 713 struct mlx5_flow_table *ft; 714 int ret; 715 716 ft = kzalloc(sizeof(*ft), GFP_KERNEL); 717 if (!ft) 718 return ERR_PTR(-ENOMEM); 719 720 ret = rhltable_init(&ft->fgs_hash, &rhash_fg); 721 if (ret) { 722 kfree(ft); 723 return ERR_PTR(ret); 724 } 725 726 ft->level = level; 727 ft->node.type = FS_TYPE_FLOW_TABLE; 728 ft->op_mod = op_mod; 729 ft->type = table_type; 730 ft->vport = vport; 731 ft->max_fte = max_fte; 732 ft->flags = flags; 733 INIT_LIST_HEAD(&ft->fwd_rules); 734 mutex_init(&ft->lock); 735 736 return ft; 737 } 738 739 /* If reverse is false, then we search for the first flow table in the 740 * root sub-tree from start(closest from right), else we search for the 741 * last flow table in the root sub-tree till start(closest from left). 742 */ 743 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root, 744 struct list_head *start, 745 bool reverse) 746 { 747 #define list_advance_entry(pos, reverse) \ 748 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list)) 749 750 #define list_for_each_advance_continue(pos, head, reverse) \ 751 for (pos = list_advance_entry(pos, reverse); \ 752 &pos->list != (head); \ 753 pos = list_advance_entry(pos, reverse)) 754 755 struct fs_node *iter = list_entry(start, struct fs_node, list); 756 struct mlx5_flow_table *ft = NULL; 757 758 if (!root || root->type == FS_TYPE_PRIO_CHAINS) 759 return NULL; 760 761 list_for_each_advance_continue(iter, &root->children, reverse) { 762 if (iter->type == FS_TYPE_FLOW_TABLE) { 763 fs_get_obj(ft, iter); 764 return ft; 765 } 766 ft = find_closest_ft_recursive(iter, &iter->children, reverse); 767 if (ft) 768 return ft; 769 } 770 771 return ft; 772 } 773 774 /* If reverse if false then return the first flow table in next priority of 775 * prio in the tree, else return the last flow table in the previous priority 776 * of prio in the tree. 777 */ 778 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse) 779 { 780 struct mlx5_flow_table *ft = NULL; 781 struct fs_node *curr_node; 782 struct fs_node *parent; 783 784 parent = prio->node.parent; 785 curr_node = &prio->node; 786 while (!ft && parent) { 787 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse); 788 curr_node = parent; 789 parent = curr_node->parent; 790 } 791 return ft; 792 } 793 794 /* Assuming all the tree is locked by mutex chain lock */ 795 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio) 796 { 797 return find_closest_ft(prio, false); 798 } 799 800 /* Assuming all the tree is locked by mutex chain lock */ 801 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio) 802 { 803 return find_closest_ft(prio, true); 804 } 805 806 static int connect_fts_in_prio(struct mlx5_core_dev *dev, 807 struct fs_prio *prio, 808 struct mlx5_flow_table *ft) 809 { 810 struct mlx5_flow_root_namespace *root = find_root(&prio->node); 811 struct mlx5_flow_table *iter; 812 int i = 0; 813 int err; 814 815 fs_for_each_ft(iter, prio) { 816 i++; 817 err = root->cmds->modify_flow_table(root, iter, ft); 818 if (err) { 819 mlx5_core_warn(dev, "Failed to modify flow table %d\n", 820 iter->id); 821 /* The driver is out of sync with the FW */ 822 if (i > 1) 823 WARN_ON(true); 824 return err; 825 } 826 } 827 return 0; 828 } 829 830 /* Connect flow tables from previous priority of prio to ft */ 831 static int connect_prev_fts(struct mlx5_core_dev *dev, 832 struct mlx5_flow_table *ft, 833 struct fs_prio *prio) 834 { 835 struct mlx5_flow_table *prev_ft; 836 837 prev_ft = find_prev_chained_ft(prio); 838 if (prev_ft) { 839 struct fs_prio *prev_prio; 840 841 fs_get_obj(prev_prio, prev_ft->node.parent); 842 return connect_fts_in_prio(dev, prev_prio, ft); 843 } 844 return 0; 845 } 846 847 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio 848 *prio) 849 { 850 struct mlx5_flow_root_namespace *root = find_root(&prio->node); 851 struct mlx5_ft_underlay_qp *uqp; 852 int min_level = INT_MAX; 853 int err = 0; 854 u32 qpn; 855 856 if (root->root_ft) 857 min_level = root->root_ft->level; 858 859 if (ft->level >= min_level) 860 return 0; 861 862 if (list_empty(&root->underlay_qpns)) { 863 /* Don't set any QPN (zero) in case QPN list is empty */ 864 qpn = 0; 865 err = root->cmds->update_root_ft(root, ft, qpn, false); 866 } else { 867 list_for_each_entry(uqp, &root->underlay_qpns, list) { 868 qpn = uqp->qpn; 869 err = root->cmds->update_root_ft(root, ft, 870 qpn, false); 871 if (err) 872 break; 873 } 874 } 875 876 if (err) 877 mlx5_core_warn(root->dev, 878 "Update root flow table of id(%u) qpn(%d) failed\n", 879 ft->id, qpn); 880 else 881 root->root_ft = ft; 882 883 return err; 884 } 885 886 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule, 887 struct mlx5_flow_destination *dest) 888 { 889 struct mlx5_flow_root_namespace *root; 890 struct mlx5_flow_table *ft; 891 struct mlx5_flow_group *fg; 892 struct fs_fte *fte; 893 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST); 894 int err = 0; 895 896 fs_get_obj(fte, rule->node.parent); 897 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST)) 898 return -EINVAL; 899 down_write_ref_node(&fte->node, false); 900 fs_get_obj(fg, fte->node.parent); 901 fs_get_obj(ft, fg->node.parent); 902 903 memcpy(&rule->dest_attr, dest, sizeof(*dest)); 904 root = find_root(&ft->node); 905 err = root->cmds->update_fte(root, ft, fg, 906 modify_mask, fte); 907 up_write_ref_node(&fte->node, false); 908 909 return err; 910 } 911 912 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle, 913 struct mlx5_flow_destination *new_dest, 914 struct mlx5_flow_destination *old_dest) 915 { 916 int i; 917 918 if (!old_dest) { 919 if (handle->num_rules != 1) 920 return -EINVAL; 921 return _mlx5_modify_rule_destination(handle->rule[0], 922 new_dest); 923 } 924 925 for (i = 0; i < handle->num_rules; i++) { 926 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr)) 927 return _mlx5_modify_rule_destination(handle->rule[i], 928 new_dest); 929 } 930 931 return -EINVAL; 932 } 933 934 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */ 935 static int connect_fwd_rules(struct mlx5_core_dev *dev, 936 struct mlx5_flow_table *new_next_ft, 937 struct mlx5_flow_table *old_next_ft) 938 { 939 struct mlx5_flow_destination dest = {}; 940 struct mlx5_flow_rule *iter; 941 int err = 0; 942 943 /* new_next_ft and old_next_ft could be NULL only 944 * when we create/destroy the anchor flow table. 945 */ 946 if (!new_next_ft || !old_next_ft) 947 return 0; 948 949 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; 950 dest.ft = new_next_ft; 951 952 mutex_lock(&old_next_ft->lock); 953 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules); 954 mutex_unlock(&old_next_ft->lock); 955 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) { 956 err = _mlx5_modify_rule_destination(iter, &dest); 957 if (err) 958 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n", 959 new_next_ft->id); 960 } 961 return 0; 962 } 963 964 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft, 965 struct fs_prio *prio) 966 { 967 struct mlx5_flow_table *next_ft; 968 int err = 0; 969 970 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */ 971 972 if (list_empty(&prio->node.children)) { 973 err = connect_prev_fts(dev, ft, prio); 974 if (err) 975 return err; 976 977 next_ft = find_next_chained_ft(prio); 978 err = connect_fwd_rules(dev, ft, next_ft); 979 if (err) 980 return err; 981 } 982 983 if (MLX5_CAP_FLOWTABLE(dev, 984 flow_table_properties_nic_receive.modify_root)) 985 err = update_root_ft_create(ft, prio); 986 return err; 987 } 988 989 static void list_add_flow_table(struct mlx5_flow_table *ft, 990 struct fs_prio *prio) 991 { 992 struct list_head *prev = &prio->node.children; 993 struct mlx5_flow_table *iter; 994 995 fs_for_each_ft(iter, prio) { 996 if (iter->level > ft->level) 997 break; 998 prev = &iter->node.list; 999 } 1000 list_add(&ft->node.list, prev); 1001 } 1002 1003 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns, 1004 struct mlx5_flow_table_attr *ft_attr, 1005 enum fs_flow_table_op_mod op_mod, 1006 u16 vport) 1007 { 1008 struct mlx5_flow_root_namespace *root = find_root(&ns->node); 1009 struct mlx5_flow_table *next_ft = NULL; 1010 struct fs_prio *fs_prio = NULL; 1011 struct mlx5_flow_table *ft; 1012 int log_table_sz; 1013 int err; 1014 1015 if (!root) { 1016 pr_err("mlx5: flow steering failed to find root of namespace\n"); 1017 return ERR_PTR(-ENODEV); 1018 } 1019 1020 mutex_lock(&root->chain_lock); 1021 fs_prio = find_prio(ns, ft_attr->prio); 1022 if (!fs_prio) { 1023 err = -EINVAL; 1024 goto unlock_root; 1025 } 1026 if (ft_attr->level >= fs_prio->num_levels) { 1027 err = -ENOSPC; 1028 goto unlock_root; 1029 } 1030 /* The level is related to the 1031 * priority level range. 1032 */ 1033 ft_attr->level += fs_prio->start_level; 1034 ft = alloc_flow_table(ft_attr->level, 1035 vport, 1036 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0, 1037 root->table_type, 1038 op_mod, ft_attr->flags); 1039 if (IS_ERR(ft)) { 1040 err = PTR_ERR(ft); 1041 goto unlock_root; 1042 } 1043 1044 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table); 1045 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0; 1046 next_ft = find_next_chained_ft(fs_prio); 1047 ft->def_miss_action = ns->def_miss_action; 1048 err = root->cmds->create_flow_table(root, ft, log_table_sz, next_ft); 1049 if (err) 1050 goto free_ft; 1051 1052 err = connect_flow_table(root->dev, ft, fs_prio); 1053 if (err) 1054 goto destroy_ft; 1055 ft->node.active = true; 1056 down_write_ref_node(&fs_prio->node, false); 1057 tree_add_node(&ft->node, &fs_prio->node); 1058 list_add_flow_table(ft, fs_prio); 1059 fs_prio->num_ft++; 1060 up_write_ref_node(&fs_prio->node, false); 1061 mutex_unlock(&root->chain_lock); 1062 trace_mlx5_fs_add_ft(ft); 1063 return ft; 1064 destroy_ft: 1065 root->cmds->destroy_flow_table(root, ft); 1066 free_ft: 1067 kfree(ft); 1068 unlock_root: 1069 mutex_unlock(&root->chain_lock); 1070 return ERR_PTR(err); 1071 } 1072 1073 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns, 1074 struct mlx5_flow_table_attr *ft_attr) 1075 { 1076 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0); 1077 } 1078 1079 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns, 1080 int prio, int max_fte, 1081 u32 level, u16 vport) 1082 { 1083 struct mlx5_flow_table_attr ft_attr = {}; 1084 1085 ft_attr.max_fte = max_fte; 1086 ft_attr.level = level; 1087 ft_attr.prio = prio; 1088 1089 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport); 1090 } 1091 1092 struct mlx5_flow_table* 1093 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns, 1094 int prio, u32 level) 1095 { 1096 struct mlx5_flow_table_attr ft_attr = {}; 1097 1098 ft_attr.level = level; 1099 ft_attr.prio = prio; 1100 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0); 1101 } 1102 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table); 1103 1104 struct mlx5_flow_table* 1105 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns, 1106 int prio, 1107 int num_flow_table_entries, 1108 int max_num_groups, 1109 u32 level, 1110 u32 flags) 1111 { 1112 struct mlx5_flow_table_attr ft_attr = {}; 1113 struct mlx5_flow_table *ft; 1114 1115 if (max_num_groups > num_flow_table_entries) 1116 return ERR_PTR(-EINVAL); 1117 1118 ft_attr.max_fte = num_flow_table_entries; 1119 ft_attr.prio = prio; 1120 ft_attr.level = level; 1121 ft_attr.flags = flags; 1122 1123 ft = mlx5_create_flow_table(ns, &ft_attr); 1124 if (IS_ERR(ft)) 1125 return ft; 1126 1127 ft->autogroup.active = true; 1128 ft->autogroup.required_groups = max_num_groups; 1129 1130 return ft; 1131 } 1132 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table); 1133 1134 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft, 1135 u32 *fg_in) 1136 { 1137 struct mlx5_flow_root_namespace *root = find_root(&ft->node); 1138 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in, 1139 fg_in, match_criteria); 1140 u8 match_criteria_enable = MLX5_GET(create_flow_group_in, 1141 fg_in, 1142 match_criteria_enable); 1143 int start_index = MLX5_GET(create_flow_group_in, fg_in, 1144 start_flow_index); 1145 int end_index = MLX5_GET(create_flow_group_in, fg_in, 1146 end_flow_index); 1147 struct mlx5_flow_group *fg; 1148 int err; 1149 1150 if (ft->autogroup.active) 1151 return ERR_PTR(-EPERM); 1152 1153 down_write_ref_node(&ft->node, false); 1154 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria, 1155 start_index, end_index, 1156 ft->node.children.prev); 1157 up_write_ref_node(&ft->node, false); 1158 if (IS_ERR(fg)) 1159 return fg; 1160 1161 err = root->cmds->create_flow_group(root, ft, fg_in, fg); 1162 if (err) { 1163 tree_put_node(&fg->node, false); 1164 return ERR_PTR(err); 1165 } 1166 trace_mlx5_fs_add_fg(fg); 1167 fg->node.active = true; 1168 1169 return fg; 1170 } 1171 1172 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest) 1173 { 1174 struct mlx5_flow_rule *rule; 1175 1176 rule = kzalloc(sizeof(*rule), GFP_KERNEL); 1177 if (!rule) 1178 return NULL; 1179 1180 INIT_LIST_HEAD(&rule->next_ft); 1181 rule->node.type = FS_TYPE_FLOW_DEST; 1182 if (dest) 1183 memcpy(&rule->dest_attr, dest, sizeof(*dest)); 1184 1185 return rule; 1186 } 1187 1188 static struct mlx5_flow_handle *alloc_handle(int num_rules) 1189 { 1190 struct mlx5_flow_handle *handle; 1191 1192 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL); 1193 if (!handle) 1194 return NULL; 1195 1196 handle->num_rules = num_rules; 1197 1198 return handle; 1199 } 1200 1201 static void destroy_flow_handle(struct fs_fte *fte, 1202 struct mlx5_flow_handle *handle, 1203 struct mlx5_flow_destination *dest, 1204 int i) 1205 { 1206 for (; --i >= 0;) { 1207 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) { 1208 fte->dests_size--; 1209 list_del(&handle->rule[i]->node.list); 1210 kfree(handle->rule[i]); 1211 } 1212 } 1213 kfree(handle); 1214 } 1215 1216 static struct mlx5_flow_handle * 1217 create_flow_handle(struct fs_fte *fte, 1218 struct mlx5_flow_destination *dest, 1219 int dest_num, 1220 int *modify_mask, 1221 bool *new_rule) 1222 { 1223 struct mlx5_flow_handle *handle; 1224 struct mlx5_flow_rule *rule = NULL; 1225 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS); 1226 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST); 1227 int type; 1228 int i = 0; 1229 1230 handle = alloc_handle((dest_num) ? dest_num : 1); 1231 if (!handle) 1232 return ERR_PTR(-ENOMEM); 1233 1234 do { 1235 if (dest) { 1236 rule = find_flow_rule(fte, dest + i); 1237 if (rule) { 1238 refcount_inc(&rule->node.refcount); 1239 goto rule_found; 1240 } 1241 } 1242 1243 *new_rule = true; 1244 rule = alloc_rule(dest + i); 1245 if (!rule) 1246 goto free_rules; 1247 1248 /* Add dest to dests list- we need flow tables to be in the 1249 * end of the list for forward to next prio rules. 1250 */ 1251 tree_init_node(&rule->node, NULL, del_sw_hw_rule); 1252 if (dest && 1253 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) 1254 list_add(&rule->node.list, &fte->node.children); 1255 else 1256 list_add_tail(&rule->node.list, &fte->node.children); 1257 if (dest) { 1258 fte->dests_size++; 1259 1260 type = dest[i].type == 1261 MLX5_FLOW_DESTINATION_TYPE_COUNTER; 1262 *modify_mask |= type ? count : dst; 1263 } 1264 rule_found: 1265 handle->rule[i] = rule; 1266 } while (++i < dest_num); 1267 1268 return handle; 1269 1270 free_rules: 1271 destroy_flow_handle(fte, handle, dest, i); 1272 return ERR_PTR(-ENOMEM); 1273 } 1274 1275 /* fte should not be deleted while calling this function */ 1276 static struct mlx5_flow_handle * 1277 add_rule_fte(struct fs_fte *fte, 1278 struct mlx5_flow_group *fg, 1279 struct mlx5_flow_destination *dest, 1280 int dest_num, 1281 bool update_action) 1282 { 1283 struct mlx5_flow_root_namespace *root; 1284 struct mlx5_flow_handle *handle; 1285 struct mlx5_flow_table *ft; 1286 int modify_mask = 0; 1287 int err; 1288 bool new_rule = false; 1289 1290 handle = create_flow_handle(fte, dest, dest_num, &modify_mask, 1291 &new_rule); 1292 if (IS_ERR(handle) || !new_rule) 1293 goto out; 1294 1295 if (update_action) 1296 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION); 1297 1298 fs_get_obj(ft, fg->node.parent); 1299 root = find_root(&fg->node); 1300 if (!(fte->status & FS_FTE_STATUS_EXISTING)) 1301 err = root->cmds->create_fte(root, ft, fg, fte); 1302 else 1303 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte); 1304 if (err) 1305 goto free_handle; 1306 1307 fte->node.active = true; 1308 fte->status |= FS_FTE_STATUS_EXISTING; 1309 atomic_inc(&fte->node.version); 1310 1311 out: 1312 return handle; 1313 1314 free_handle: 1315 destroy_flow_handle(fte, handle, dest, handle->num_rules); 1316 return ERR_PTR(err); 1317 } 1318 1319 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft, 1320 const struct mlx5_flow_spec *spec) 1321 { 1322 struct list_head *prev = &ft->node.children; 1323 struct mlx5_flow_group *fg; 1324 unsigned int candidate_index = 0; 1325 unsigned int group_size = 0; 1326 1327 if (!ft->autogroup.active) 1328 return ERR_PTR(-ENOENT); 1329 1330 if (ft->autogroup.num_groups < ft->autogroup.required_groups) 1331 /* We save place for flow groups in addition to max types */ 1332 group_size = ft->max_fte / (ft->autogroup.required_groups + 1); 1333 1334 /* ft->max_fte == ft->autogroup.max_types */ 1335 if (group_size == 0) 1336 group_size = 1; 1337 1338 /* sorted by start_index */ 1339 fs_for_each_fg(fg, ft) { 1340 if (candidate_index + group_size > fg->start_index) 1341 candidate_index = fg->start_index + fg->max_ftes; 1342 else 1343 break; 1344 prev = &fg->node.list; 1345 } 1346 1347 if (candidate_index + group_size > ft->max_fte) 1348 return ERR_PTR(-ENOSPC); 1349 1350 fg = alloc_insert_flow_group(ft, 1351 spec->match_criteria_enable, 1352 spec->match_criteria, 1353 candidate_index, 1354 candidate_index + group_size - 1, 1355 prev); 1356 if (IS_ERR(fg)) 1357 goto out; 1358 1359 ft->autogroup.num_groups++; 1360 1361 out: 1362 return fg; 1363 } 1364 1365 static int create_auto_flow_group(struct mlx5_flow_table *ft, 1366 struct mlx5_flow_group *fg) 1367 { 1368 struct mlx5_flow_root_namespace *root = find_root(&ft->node); 1369 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in); 1370 void *match_criteria_addr; 1371 u8 src_esw_owner_mask_on; 1372 void *misc; 1373 int err; 1374 u32 *in; 1375 1376 in = kvzalloc(inlen, GFP_KERNEL); 1377 if (!in) 1378 return -ENOMEM; 1379 1380 MLX5_SET(create_flow_group_in, in, match_criteria_enable, 1381 fg->mask.match_criteria_enable); 1382 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index); 1383 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index + 1384 fg->max_ftes - 1); 1385 1386 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria, 1387 misc_parameters); 1388 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc, 1389 source_eswitch_owner_vhca_id); 1390 MLX5_SET(create_flow_group_in, in, 1391 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on); 1392 1393 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in, 1394 in, match_criteria); 1395 memcpy(match_criteria_addr, fg->mask.match_criteria, 1396 sizeof(fg->mask.match_criteria)); 1397 1398 err = root->cmds->create_flow_group(root, ft, in, fg); 1399 if (!err) { 1400 fg->node.active = true; 1401 trace_mlx5_fs_add_fg(fg); 1402 } 1403 1404 kvfree(in); 1405 return err; 1406 } 1407 1408 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1, 1409 struct mlx5_flow_destination *d2) 1410 { 1411 if (d1->type == d2->type) { 1412 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT && 1413 d1->vport.num == d2->vport.num && 1414 d1->vport.flags == d2->vport.flags && 1415 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ? 1416 (d1->vport.vhca_id == d2->vport.vhca_id) : true) && 1417 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ? 1418 (d1->vport.pkt_reformat->id == 1419 d2->vport.pkt_reformat->id) : true)) || 1420 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE && 1421 d1->ft == d2->ft) || 1422 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR && 1423 d1->tir_num == d2->tir_num) || 1424 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM && 1425 d1->ft_num == d2->ft_num)) 1426 return true; 1427 } 1428 1429 return false; 1430 } 1431 1432 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte, 1433 struct mlx5_flow_destination *dest) 1434 { 1435 struct mlx5_flow_rule *rule; 1436 1437 list_for_each_entry(rule, &fte->node.children, node.list) { 1438 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest)) 1439 return rule; 1440 } 1441 return NULL; 1442 } 1443 1444 static bool check_conflicting_actions(u32 action1, u32 action2) 1445 { 1446 u32 xored_actions = action1 ^ action2; 1447 1448 /* if one rule only wants to count, it's ok */ 1449 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT || 1450 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT) 1451 return false; 1452 1453 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP | 1454 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT | 1455 MLX5_FLOW_CONTEXT_ACTION_DECAP | 1456 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR | 1457 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP | 1458 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH | 1459 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 | 1460 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2)) 1461 return true; 1462 1463 return false; 1464 } 1465 1466 static int check_conflicting_ftes(struct fs_fte *fte, 1467 const struct mlx5_flow_context *flow_context, 1468 const struct mlx5_flow_act *flow_act) 1469 { 1470 if (check_conflicting_actions(flow_act->action, fte->action.action)) { 1471 mlx5_core_warn(get_dev(&fte->node), 1472 "Found two FTEs with conflicting actions\n"); 1473 return -EEXIST; 1474 } 1475 1476 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) && 1477 fte->flow_context.flow_tag != flow_context->flow_tag) { 1478 mlx5_core_warn(get_dev(&fte->node), 1479 "FTE flow tag %u already exists with different flow tag %u\n", 1480 fte->flow_context.flow_tag, 1481 flow_context->flow_tag); 1482 return -EEXIST; 1483 } 1484 1485 return 0; 1486 } 1487 1488 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg, 1489 const struct mlx5_flow_spec *spec, 1490 struct mlx5_flow_act *flow_act, 1491 struct mlx5_flow_destination *dest, 1492 int dest_num, 1493 struct fs_fte *fte) 1494 { 1495 struct mlx5_flow_handle *handle; 1496 int old_action; 1497 int i; 1498 int ret; 1499 1500 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act); 1501 if (ret) 1502 return ERR_PTR(ret); 1503 1504 old_action = fte->action.action; 1505 fte->action.action |= flow_act->action; 1506 handle = add_rule_fte(fte, fg, dest, dest_num, 1507 old_action != flow_act->action); 1508 if (IS_ERR(handle)) { 1509 fte->action.action = old_action; 1510 return handle; 1511 } 1512 trace_mlx5_fs_set_fte(fte, false); 1513 1514 for (i = 0; i < handle->num_rules; i++) { 1515 if (refcount_read(&handle->rule[i]->node.refcount) == 1) { 1516 tree_add_node(&handle->rule[i]->node, &fte->node); 1517 trace_mlx5_fs_add_rule(handle->rule[i]); 1518 } 1519 } 1520 return handle; 1521 } 1522 1523 static bool counter_is_valid(u32 action) 1524 { 1525 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP | 1526 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST)); 1527 } 1528 1529 static bool dest_is_valid(struct mlx5_flow_destination *dest, 1530 u32 action, 1531 struct mlx5_flow_table *ft) 1532 { 1533 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)) 1534 return counter_is_valid(action); 1535 1536 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST)) 1537 return true; 1538 1539 if (!dest || ((dest->type == 1540 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) && 1541 (dest->ft->level <= ft->level))) 1542 return false; 1543 return true; 1544 } 1545 1546 struct match_list { 1547 struct list_head list; 1548 struct mlx5_flow_group *g; 1549 }; 1550 1551 struct match_list_head { 1552 struct list_head list; 1553 struct match_list first; 1554 }; 1555 1556 static void free_match_list(struct match_list_head *head) 1557 { 1558 if (!list_empty(&head->list)) { 1559 struct match_list *iter, *match_tmp; 1560 1561 list_del(&head->first.list); 1562 tree_put_node(&head->first.g->node, false); 1563 list_for_each_entry_safe(iter, match_tmp, &head->list, 1564 list) { 1565 tree_put_node(&iter->g->node, false); 1566 list_del(&iter->list); 1567 kfree(iter); 1568 } 1569 } 1570 } 1571 1572 static int build_match_list(struct match_list_head *match_head, 1573 struct mlx5_flow_table *ft, 1574 const struct mlx5_flow_spec *spec) 1575 { 1576 struct rhlist_head *tmp, *list; 1577 struct mlx5_flow_group *g; 1578 int err = 0; 1579 1580 rcu_read_lock(); 1581 INIT_LIST_HEAD(&match_head->list); 1582 /* Collect all fgs which has a matching match_criteria */ 1583 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg); 1584 /* RCU is atomic, we can't execute FW commands here */ 1585 rhl_for_each_entry_rcu(g, tmp, list, hash) { 1586 struct match_list *curr_match; 1587 1588 if (likely(list_empty(&match_head->list))) { 1589 if (!tree_get_node(&g->node)) 1590 continue; 1591 match_head->first.g = g; 1592 list_add_tail(&match_head->first.list, 1593 &match_head->list); 1594 continue; 1595 } 1596 1597 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC); 1598 if (!curr_match) { 1599 free_match_list(match_head); 1600 err = -ENOMEM; 1601 goto out; 1602 } 1603 if (!tree_get_node(&g->node)) { 1604 kfree(curr_match); 1605 continue; 1606 } 1607 curr_match->g = g; 1608 list_add_tail(&curr_match->list, &match_head->list); 1609 } 1610 out: 1611 rcu_read_unlock(); 1612 return err; 1613 } 1614 1615 static u64 matched_fgs_get_version(struct list_head *match_head) 1616 { 1617 struct match_list *iter; 1618 u64 version = 0; 1619 1620 list_for_each_entry(iter, match_head, list) 1621 version += (u64)atomic_read(&iter->g->node.version); 1622 return version; 1623 } 1624 1625 static struct fs_fte * 1626 lookup_fte_locked(struct mlx5_flow_group *g, 1627 const u32 *match_value, 1628 bool take_write) 1629 { 1630 struct fs_fte *fte_tmp; 1631 1632 if (take_write) 1633 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT); 1634 else 1635 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT); 1636 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value, 1637 rhash_fte); 1638 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) { 1639 fte_tmp = NULL; 1640 goto out; 1641 } 1642 if (!fte_tmp->node.active) { 1643 tree_put_node(&fte_tmp->node, false); 1644 fte_tmp = NULL; 1645 goto out; 1646 } 1647 1648 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD); 1649 out: 1650 if (take_write) 1651 up_write_ref_node(&g->node, false); 1652 else 1653 up_read_ref_node(&g->node); 1654 return fte_tmp; 1655 } 1656 1657 static struct mlx5_flow_handle * 1658 try_add_to_existing_fg(struct mlx5_flow_table *ft, 1659 struct list_head *match_head, 1660 const struct mlx5_flow_spec *spec, 1661 struct mlx5_flow_act *flow_act, 1662 struct mlx5_flow_destination *dest, 1663 int dest_num, 1664 int ft_version) 1665 { 1666 struct mlx5_flow_steering *steering = get_steering(&ft->node); 1667 struct mlx5_flow_group *g; 1668 struct mlx5_flow_handle *rule; 1669 struct match_list *iter; 1670 bool take_write = false; 1671 struct fs_fte *fte; 1672 u64 version; 1673 int err; 1674 1675 fte = alloc_fte(ft, spec, flow_act); 1676 if (IS_ERR(fte)) 1677 return ERR_PTR(-ENOMEM); 1678 1679 search_again_locked: 1680 version = matched_fgs_get_version(match_head); 1681 if (flow_act->flags & FLOW_ACT_NO_APPEND) 1682 goto skip_search; 1683 /* Try to find a fg that already contains a matching fte */ 1684 list_for_each_entry(iter, match_head, list) { 1685 struct fs_fte *fte_tmp; 1686 1687 g = iter->g; 1688 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write); 1689 if (!fte_tmp) 1690 continue; 1691 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp); 1692 up_write_ref_node(&fte_tmp->node, false); 1693 tree_put_node(&fte_tmp->node, false); 1694 kmem_cache_free(steering->ftes_cache, fte); 1695 return rule; 1696 } 1697 1698 skip_search: 1699 /* No group with matching fte found, or we skipped the search. 1700 * Try to add a new fte to any matching fg. 1701 */ 1702 1703 /* Check the ft version, for case that new flow group 1704 * was added while the fgs weren't locked 1705 */ 1706 if (atomic_read(&ft->node.version) != ft_version) { 1707 rule = ERR_PTR(-EAGAIN); 1708 goto out; 1709 } 1710 1711 /* Check the fgs version, for case the new FTE with the 1712 * same values was added while the fgs weren't locked 1713 */ 1714 if (version != matched_fgs_get_version(match_head)) { 1715 take_write = true; 1716 goto search_again_locked; 1717 } 1718 1719 list_for_each_entry(iter, match_head, list) { 1720 g = iter->g; 1721 1722 if (!g->node.active) 1723 continue; 1724 1725 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT); 1726 1727 err = insert_fte(g, fte); 1728 if (err) { 1729 up_write_ref_node(&g->node, false); 1730 if (err == -ENOSPC) 1731 continue; 1732 kmem_cache_free(steering->ftes_cache, fte); 1733 return ERR_PTR(err); 1734 } 1735 1736 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD); 1737 up_write_ref_node(&g->node, false); 1738 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte); 1739 up_write_ref_node(&fte->node, false); 1740 tree_put_node(&fte->node, false); 1741 return rule; 1742 } 1743 rule = ERR_PTR(-ENOENT); 1744 out: 1745 kmem_cache_free(steering->ftes_cache, fte); 1746 return rule; 1747 } 1748 1749 static struct mlx5_flow_handle * 1750 _mlx5_add_flow_rules(struct mlx5_flow_table *ft, 1751 const struct mlx5_flow_spec *spec, 1752 struct mlx5_flow_act *flow_act, 1753 struct mlx5_flow_destination *dest, 1754 int dest_num) 1755 1756 { 1757 struct mlx5_flow_steering *steering = get_steering(&ft->node); 1758 struct mlx5_flow_group *g; 1759 struct mlx5_flow_handle *rule; 1760 struct match_list_head match_head; 1761 bool take_write = false; 1762 struct fs_fte *fte; 1763 int version; 1764 int err; 1765 int i; 1766 1767 if (!check_valid_spec(spec)) 1768 return ERR_PTR(-EINVAL); 1769 1770 for (i = 0; i < dest_num; i++) { 1771 if (!dest_is_valid(&dest[i], flow_act->action, ft)) 1772 return ERR_PTR(-EINVAL); 1773 } 1774 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT); 1775 search_again_locked: 1776 version = atomic_read(&ft->node.version); 1777 1778 /* Collect all fgs which has a matching match_criteria */ 1779 err = build_match_list(&match_head, ft, spec); 1780 if (err) { 1781 if (take_write) 1782 up_write_ref_node(&ft->node, false); 1783 else 1784 up_read_ref_node(&ft->node); 1785 return ERR_PTR(err); 1786 } 1787 1788 if (!take_write) 1789 up_read_ref_node(&ft->node); 1790 1791 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest, 1792 dest_num, version); 1793 free_match_list(&match_head); 1794 if (!IS_ERR(rule) || 1795 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) { 1796 if (take_write) 1797 up_write_ref_node(&ft->node, false); 1798 return rule; 1799 } 1800 1801 if (!take_write) { 1802 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT); 1803 take_write = true; 1804 } 1805 1806 if (PTR_ERR(rule) == -EAGAIN || 1807 version != atomic_read(&ft->node.version)) 1808 goto search_again_locked; 1809 1810 g = alloc_auto_flow_group(ft, spec); 1811 if (IS_ERR(g)) { 1812 rule = ERR_CAST(g); 1813 up_write_ref_node(&ft->node, false); 1814 return rule; 1815 } 1816 1817 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT); 1818 up_write_ref_node(&ft->node, false); 1819 1820 err = create_auto_flow_group(ft, g); 1821 if (err) 1822 goto err_release_fg; 1823 1824 fte = alloc_fte(ft, spec, flow_act); 1825 if (IS_ERR(fte)) { 1826 err = PTR_ERR(fte); 1827 goto err_release_fg; 1828 } 1829 1830 err = insert_fte(g, fte); 1831 if (err) { 1832 kmem_cache_free(steering->ftes_cache, fte); 1833 goto err_release_fg; 1834 } 1835 1836 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD); 1837 up_write_ref_node(&g->node, false); 1838 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte); 1839 up_write_ref_node(&fte->node, false); 1840 tree_put_node(&fte->node, false); 1841 tree_put_node(&g->node, false); 1842 return rule; 1843 1844 err_release_fg: 1845 up_write_ref_node(&g->node, false); 1846 tree_put_node(&g->node, false); 1847 return ERR_PTR(err); 1848 } 1849 1850 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft) 1851 { 1852 return ((ft->type == FS_FT_NIC_RX) && 1853 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs))); 1854 } 1855 1856 struct mlx5_flow_handle * 1857 mlx5_add_flow_rules(struct mlx5_flow_table *ft, 1858 const struct mlx5_flow_spec *spec, 1859 struct mlx5_flow_act *flow_act, 1860 struct mlx5_flow_destination *dest, 1861 int num_dest) 1862 { 1863 struct mlx5_flow_root_namespace *root = find_root(&ft->node); 1864 struct mlx5_flow_destination gen_dest = {}; 1865 struct mlx5_flow_table *next_ft = NULL; 1866 struct mlx5_flow_handle *handle = NULL; 1867 u32 sw_action = flow_act->action; 1868 struct fs_prio *prio; 1869 1870 fs_get_obj(prio, ft->node.parent); 1871 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) { 1872 if (!fwd_next_prio_supported(ft)) 1873 return ERR_PTR(-EOPNOTSUPP); 1874 if (num_dest) 1875 return ERR_PTR(-EINVAL); 1876 mutex_lock(&root->chain_lock); 1877 next_ft = find_next_chained_ft(prio); 1878 if (next_ft) { 1879 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE; 1880 gen_dest.ft = next_ft; 1881 dest = &gen_dest; 1882 num_dest = 1; 1883 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; 1884 } else { 1885 mutex_unlock(&root->chain_lock); 1886 return ERR_PTR(-EOPNOTSUPP); 1887 } 1888 } 1889 1890 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest); 1891 1892 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) { 1893 if (!IS_ERR_OR_NULL(handle) && 1894 (list_empty(&handle->rule[0]->next_ft))) { 1895 mutex_lock(&next_ft->lock); 1896 list_add(&handle->rule[0]->next_ft, 1897 &next_ft->fwd_rules); 1898 mutex_unlock(&next_ft->lock); 1899 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 1900 } 1901 mutex_unlock(&root->chain_lock); 1902 } 1903 return handle; 1904 } 1905 EXPORT_SYMBOL(mlx5_add_flow_rules); 1906 1907 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle) 1908 { 1909 struct fs_fte *fte; 1910 int i; 1911 1912 /* In order to consolidate the HW changes we lock the FTE for other 1913 * changes, and increase its refcount, in order not to perform the 1914 * "del" functions of the FTE. Will handle them here. 1915 * The removal of the rules is done under locked FTE. 1916 * After removing all the handle's rules, if there are remaining 1917 * rules, it means we just need to modify the FTE in FW, and 1918 * unlock/decrease the refcount we increased before. 1919 * Otherwise, it means the FTE should be deleted. First delete the 1920 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of 1921 * the FTE, which will handle the last decrease of the refcount, as 1922 * well as required handling of its parent. 1923 */ 1924 fs_get_obj(fte, handle->rule[0]->node.parent); 1925 down_write_ref_node(&fte->node, false); 1926 for (i = handle->num_rules - 1; i >= 0; i--) 1927 tree_remove_node(&handle->rule[i]->node, true); 1928 if (fte->modify_mask && fte->dests_size) { 1929 modify_fte(fte); 1930 up_write_ref_node(&fte->node, false); 1931 } else { 1932 del_hw_fte(&fte->node); 1933 up_write(&fte->node.lock); 1934 tree_put_node(&fte->node, false); 1935 } 1936 kfree(handle); 1937 } 1938 EXPORT_SYMBOL(mlx5_del_flow_rules); 1939 1940 /* Assuming prio->node.children(flow tables) is sorted by level */ 1941 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft) 1942 { 1943 struct fs_prio *prio; 1944 1945 fs_get_obj(prio, ft->node.parent); 1946 1947 if (!list_is_last(&ft->node.list, &prio->node.children)) 1948 return list_next_entry(ft, node.list); 1949 return find_next_chained_ft(prio); 1950 } 1951 1952 static int update_root_ft_destroy(struct mlx5_flow_table *ft) 1953 { 1954 struct mlx5_flow_root_namespace *root = find_root(&ft->node); 1955 struct mlx5_ft_underlay_qp *uqp; 1956 struct mlx5_flow_table *new_root_ft = NULL; 1957 int err = 0; 1958 u32 qpn; 1959 1960 if (root->root_ft != ft) 1961 return 0; 1962 1963 new_root_ft = find_next_ft(ft); 1964 if (!new_root_ft) { 1965 root->root_ft = NULL; 1966 return 0; 1967 } 1968 1969 if (list_empty(&root->underlay_qpns)) { 1970 /* Don't set any QPN (zero) in case QPN list is empty */ 1971 qpn = 0; 1972 err = root->cmds->update_root_ft(root, new_root_ft, 1973 qpn, false); 1974 } else { 1975 list_for_each_entry(uqp, &root->underlay_qpns, list) { 1976 qpn = uqp->qpn; 1977 err = root->cmds->update_root_ft(root, 1978 new_root_ft, qpn, 1979 false); 1980 if (err) 1981 break; 1982 } 1983 } 1984 1985 if (err) 1986 mlx5_core_warn(root->dev, 1987 "Update root flow table of id(%u) qpn(%d) failed\n", 1988 ft->id, qpn); 1989 else 1990 root->root_ft = new_root_ft; 1991 1992 return 0; 1993 } 1994 1995 /* Connect flow table from previous priority to 1996 * the next flow table. 1997 */ 1998 static int disconnect_flow_table(struct mlx5_flow_table *ft) 1999 { 2000 struct mlx5_core_dev *dev = get_dev(&ft->node); 2001 struct mlx5_flow_table *next_ft; 2002 struct fs_prio *prio; 2003 int err = 0; 2004 2005 err = update_root_ft_destroy(ft); 2006 if (err) 2007 return err; 2008 2009 fs_get_obj(prio, ft->node.parent); 2010 if (!(list_first_entry(&prio->node.children, 2011 struct mlx5_flow_table, 2012 node.list) == ft)) 2013 return 0; 2014 2015 next_ft = find_next_chained_ft(prio); 2016 err = connect_fwd_rules(dev, next_ft, ft); 2017 if (err) 2018 return err; 2019 2020 err = connect_prev_fts(dev, next_ft, prio); 2021 if (err) 2022 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n", 2023 ft->id); 2024 return err; 2025 } 2026 2027 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft) 2028 { 2029 struct mlx5_flow_root_namespace *root = find_root(&ft->node); 2030 int err = 0; 2031 2032 mutex_lock(&root->chain_lock); 2033 err = disconnect_flow_table(ft); 2034 if (err) { 2035 mutex_unlock(&root->chain_lock); 2036 return err; 2037 } 2038 if (tree_remove_node(&ft->node, false)) 2039 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n", 2040 ft->id); 2041 mutex_unlock(&root->chain_lock); 2042 2043 return err; 2044 } 2045 EXPORT_SYMBOL(mlx5_destroy_flow_table); 2046 2047 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg) 2048 { 2049 if (tree_remove_node(&fg->node, false)) 2050 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n", 2051 fg->id); 2052 } 2053 2054 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev, 2055 int n) 2056 { 2057 struct mlx5_flow_steering *steering = dev->priv.steering; 2058 2059 if (!steering || !steering->fdb_sub_ns) 2060 return NULL; 2061 2062 return steering->fdb_sub_ns[n]; 2063 } 2064 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns); 2065 2066 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev, 2067 enum mlx5_flow_namespace_type type) 2068 { 2069 struct mlx5_flow_steering *steering = dev->priv.steering; 2070 struct mlx5_flow_root_namespace *root_ns; 2071 int prio = 0; 2072 struct fs_prio *fs_prio; 2073 struct mlx5_flow_namespace *ns; 2074 2075 if (!steering) 2076 return NULL; 2077 2078 switch (type) { 2079 case MLX5_FLOW_NAMESPACE_FDB: 2080 if (steering->fdb_root_ns) 2081 return &steering->fdb_root_ns->ns; 2082 return NULL; 2083 case MLX5_FLOW_NAMESPACE_SNIFFER_RX: 2084 if (steering->sniffer_rx_root_ns) 2085 return &steering->sniffer_rx_root_ns->ns; 2086 return NULL; 2087 case MLX5_FLOW_NAMESPACE_SNIFFER_TX: 2088 if (steering->sniffer_tx_root_ns) 2089 return &steering->sniffer_tx_root_ns->ns; 2090 return NULL; 2091 default: 2092 break; 2093 } 2094 2095 if (type == MLX5_FLOW_NAMESPACE_EGRESS) { 2096 root_ns = steering->egress_root_ns; 2097 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) { 2098 root_ns = steering->rdma_rx_root_ns; 2099 prio = RDMA_RX_BYPASS_PRIO; 2100 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) { 2101 root_ns = steering->rdma_rx_root_ns; 2102 prio = RDMA_RX_KERNEL_PRIO; 2103 } else { /* Must be NIC RX */ 2104 root_ns = steering->root_ns; 2105 prio = type; 2106 } 2107 2108 if (!root_ns) 2109 return NULL; 2110 2111 fs_prio = find_prio(&root_ns->ns, prio); 2112 if (!fs_prio) 2113 return NULL; 2114 2115 ns = list_first_entry(&fs_prio->node.children, 2116 typeof(*ns), 2117 node.list); 2118 2119 return ns; 2120 } 2121 EXPORT_SYMBOL(mlx5_get_flow_namespace); 2122 2123 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev, 2124 enum mlx5_flow_namespace_type type, 2125 int vport) 2126 { 2127 struct mlx5_flow_steering *steering = dev->priv.steering; 2128 2129 if (!steering || vport >= mlx5_eswitch_get_total_vports(dev)) 2130 return NULL; 2131 2132 switch (type) { 2133 case MLX5_FLOW_NAMESPACE_ESW_EGRESS: 2134 if (steering->esw_egress_root_ns && 2135 steering->esw_egress_root_ns[vport]) 2136 return &steering->esw_egress_root_ns[vport]->ns; 2137 else 2138 return NULL; 2139 case MLX5_FLOW_NAMESPACE_ESW_INGRESS: 2140 if (steering->esw_ingress_root_ns && 2141 steering->esw_ingress_root_ns[vport]) 2142 return &steering->esw_ingress_root_ns[vport]->ns; 2143 else 2144 return NULL; 2145 default: 2146 return NULL; 2147 } 2148 } 2149 2150 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns, 2151 unsigned int prio, 2152 int num_levels, 2153 enum fs_node_type type) 2154 { 2155 struct fs_prio *fs_prio; 2156 2157 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL); 2158 if (!fs_prio) 2159 return ERR_PTR(-ENOMEM); 2160 2161 fs_prio->node.type = type; 2162 tree_init_node(&fs_prio->node, NULL, del_sw_prio); 2163 tree_add_node(&fs_prio->node, &ns->node); 2164 fs_prio->num_levels = num_levels; 2165 fs_prio->prio = prio; 2166 list_add_tail(&fs_prio->node.list, &ns->node.children); 2167 2168 return fs_prio; 2169 } 2170 2171 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns, 2172 unsigned int prio, 2173 int num_levels) 2174 { 2175 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS); 2176 } 2177 2178 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns, 2179 unsigned int prio, int num_levels) 2180 { 2181 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO); 2182 } 2183 2184 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace 2185 *ns) 2186 { 2187 ns->node.type = FS_TYPE_NAMESPACE; 2188 2189 return ns; 2190 } 2191 2192 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio, 2193 int def_miss_act) 2194 { 2195 struct mlx5_flow_namespace *ns; 2196 2197 ns = kzalloc(sizeof(*ns), GFP_KERNEL); 2198 if (!ns) 2199 return ERR_PTR(-ENOMEM); 2200 2201 fs_init_namespace(ns); 2202 ns->def_miss_action = def_miss_act; 2203 tree_init_node(&ns->node, NULL, del_sw_ns); 2204 tree_add_node(&ns->node, &prio->node); 2205 list_add_tail(&ns->node.list, &prio->node.children); 2206 2207 return ns; 2208 } 2209 2210 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio, 2211 struct init_tree_node *prio_metadata) 2212 { 2213 struct fs_prio *fs_prio; 2214 int i; 2215 2216 for (i = 0; i < prio_metadata->num_leaf_prios; i++) { 2217 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels); 2218 if (IS_ERR(fs_prio)) 2219 return PTR_ERR(fs_prio); 2220 } 2221 return 0; 2222 } 2223 2224 #define FLOW_TABLE_BIT_SZ 1 2225 #define GET_FLOW_TABLE_CAP(dev, offset) \ 2226 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \ 2227 offset / 32)) >> \ 2228 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ) 2229 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps) 2230 { 2231 int i; 2232 2233 for (i = 0; i < caps->arr_sz; i++) { 2234 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i])) 2235 return false; 2236 } 2237 return true; 2238 } 2239 2240 static int init_root_tree_recursive(struct mlx5_flow_steering *steering, 2241 struct init_tree_node *init_node, 2242 struct fs_node *fs_parent_node, 2243 struct init_tree_node *init_parent_node, 2244 int prio) 2245 { 2246 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev, 2247 flow_table_properties_nic_receive. 2248 max_ft_level); 2249 struct mlx5_flow_namespace *fs_ns; 2250 struct fs_prio *fs_prio; 2251 struct fs_node *base; 2252 int i; 2253 int err; 2254 2255 if (init_node->type == FS_TYPE_PRIO) { 2256 if ((init_node->min_ft_level > max_ft_level) || 2257 !has_required_caps(steering->dev, &init_node->caps)) 2258 return 0; 2259 2260 fs_get_obj(fs_ns, fs_parent_node); 2261 if (init_node->num_leaf_prios) 2262 return create_leaf_prios(fs_ns, prio, init_node); 2263 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels); 2264 if (IS_ERR(fs_prio)) 2265 return PTR_ERR(fs_prio); 2266 base = &fs_prio->node; 2267 } else if (init_node->type == FS_TYPE_NAMESPACE) { 2268 fs_get_obj(fs_prio, fs_parent_node); 2269 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action); 2270 if (IS_ERR(fs_ns)) 2271 return PTR_ERR(fs_ns); 2272 base = &fs_ns->node; 2273 } else { 2274 return -EINVAL; 2275 } 2276 prio = 0; 2277 for (i = 0; i < init_node->ar_size; i++) { 2278 err = init_root_tree_recursive(steering, &init_node->children[i], 2279 base, init_node, prio); 2280 if (err) 2281 return err; 2282 if (init_node->children[i].type == FS_TYPE_PRIO && 2283 init_node->children[i].num_leaf_prios) { 2284 prio += init_node->children[i].num_leaf_prios; 2285 } 2286 } 2287 2288 return 0; 2289 } 2290 2291 static int init_root_tree(struct mlx5_flow_steering *steering, 2292 struct init_tree_node *init_node, 2293 struct fs_node *fs_parent_node) 2294 { 2295 int i; 2296 struct mlx5_flow_namespace *fs_ns; 2297 int err; 2298 2299 fs_get_obj(fs_ns, fs_parent_node); 2300 for (i = 0; i < init_node->ar_size; i++) { 2301 err = init_root_tree_recursive(steering, &init_node->children[i], 2302 &fs_ns->node, 2303 init_node, i); 2304 if (err) 2305 return err; 2306 } 2307 return 0; 2308 } 2309 2310 static struct mlx5_flow_root_namespace 2311 *create_root_ns(struct mlx5_flow_steering *steering, 2312 enum fs_flow_table_type table_type) 2313 { 2314 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type); 2315 struct mlx5_flow_root_namespace *root_ns; 2316 struct mlx5_flow_namespace *ns; 2317 2318 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE && 2319 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX)) 2320 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type); 2321 2322 /* Create the root namespace */ 2323 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL); 2324 if (!root_ns) 2325 return NULL; 2326 2327 root_ns->dev = steering->dev; 2328 root_ns->table_type = table_type; 2329 root_ns->cmds = cmds; 2330 2331 INIT_LIST_HEAD(&root_ns->underlay_qpns); 2332 2333 ns = &root_ns->ns; 2334 fs_init_namespace(ns); 2335 mutex_init(&root_ns->chain_lock); 2336 tree_init_node(&ns->node, NULL, NULL); 2337 tree_add_node(&ns->node, NULL); 2338 2339 return root_ns; 2340 } 2341 2342 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level); 2343 2344 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level) 2345 { 2346 struct fs_prio *prio; 2347 2348 fs_for_each_prio(prio, ns) { 2349 /* This updates prio start_level and num_levels */ 2350 set_prio_attrs_in_prio(prio, acc_level); 2351 acc_level += prio->num_levels; 2352 } 2353 return acc_level; 2354 } 2355 2356 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level) 2357 { 2358 struct mlx5_flow_namespace *ns; 2359 int acc_level_ns = acc_level; 2360 2361 prio->start_level = acc_level; 2362 fs_for_each_ns(ns, prio) 2363 /* This updates start_level and num_levels of ns's priority descendants */ 2364 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level); 2365 if (!prio->num_levels) 2366 prio->num_levels = acc_level_ns - prio->start_level; 2367 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level); 2368 } 2369 2370 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns) 2371 { 2372 struct mlx5_flow_namespace *ns = &root_ns->ns; 2373 struct fs_prio *prio; 2374 int start_level = 0; 2375 2376 fs_for_each_prio(prio, ns) { 2377 set_prio_attrs_in_prio(prio, start_level); 2378 start_level += prio->num_levels; 2379 } 2380 } 2381 2382 #define ANCHOR_PRIO 0 2383 #define ANCHOR_SIZE 1 2384 #define ANCHOR_LEVEL 0 2385 static int create_anchor_flow_table(struct mlx5_flow_steering *steering) 2386 { 2387 struct mlx5_flow_namespace *ns = NULL; 2388 struct mlx5_flow_table_attr ft_attr = {}; 2389 struct mlx5_flow_table *ft; 2390 2391 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR); 2392 if (WARN_ON(!ns)) 2393 return -EINVAL; 2394 2395 ft_attr.max_fte = ANCHOR_SIZE; 2396 ft_attr.level = ANCHOR_LEVEL; 2397 ft_attr.prio = ANCHOR_PRIO; 2398 2399 ft = mlx5_create_flow_table(ns, &ft_attr); 2400 if (IS_ERR(ft)) { 2401 mlx5_core_err(steering->dev, "Failed to create last anchor flow table"); 2402 return PTR_ERR(ft); 2403 } 2404 return 0; 2405 } 2406 2407 static int init_root_ns(struct mlx5_flow_steering *steering) 2408 { 2409 int err; 2410 2411 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX); 2412 if (!steering->root_ns) 2413 return -ENOMEM; 2414 2415 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node); 2416 if (err) 2417 goto out_err; 2418 2419 set_prio_attrs(steering->root_ns); 2420 err = create_anchor_flow_table(steering); 2421 if (err) 2422 goto out_err; 2423 2424 return 0; 2425 2426 out_err: 2427 cleanup_root_ns(steering->root_ns); 2428 steering->root_ns = NULL; 2429 return err; 2430 } 2431 2432 static void clean_tree(struct fs_node *node) 2433 { 2434 if (node) { 2435 struct fs_node *iter; 2436 struct fs_node *temp; 2437 2438 tree_get_node(node); 2439 list_for_each_entry_safe(iter, temp, &node->children, list) 2440 clean_tree(iter); 2441 tree_put_node(node, false); 2442 tree_remove_node(node, false); 2443 } 2444 } 2445 2446 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns) 2447 { 2448 if (!root_ns) 2449 return; 2450 2451 clean_tree(&root_ns->ns.node); 2452 } 2453 2454 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev) 2455 { 2456 struct mlx5_flow_steering *steering = dev->priv.steering; 2457 int i; 2458 2459 if (!steering->esw_egress_root_ns) 2460 return; 2461 2462 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++) 2463 cleanup_root_ns(steering->esw_egress_root_ns[i]); 2464 2465 kfree(steering->esw_egress_root_ns); 2466 steering->esw_egress_root_ns = NULL; 2467 } 2468 2469 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev) 2470 { 2471 struct mlx5_flow_steering *steering = dev->priv.steering; 2472 int i; 2473 2474 if (!steering->esw_ingress_root_ns) 2475 return; 2476 2477 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++) 2478 cleanup_root_ns(steering->esw_ingress_root_ns[i]); 2479 2480 kfree(steering->esw_ingress_root_ns); 2481 steering->esw_ingress_root_ns = NULL; 2482 } 2483 2484 void mlx5_cleanup_fs(struct mlx5_core_dev *dev) 2485 { 2486 struct mlx5_flow_steering *steering = dev->priv.steering; 2487 2488 cleanup_root_ns(steering->root_ns); 2489 cleanup_egress_acls_root_ns(dev); 2490 cleanup_ingress_acls_root_ns(dev); 2491 cleanup_root_ns(steering->fdb_root_ns); 2492 steering->fdb_root_ns = NULL; 2493 kfree(steering->fdb_sub_ns); 2494 steering->fdb_sub_ns = NULL; 2495 cleanup_root_ns(steering->sniffer_rx_root_ns); 2496 cleanup_root_ns(steering->sniffer_tx_root_ns); 2497 cleanup_root_ns(steering->rdma_rx_root_ns); 2498 cleanup_root_ns(steering->egress_root_ns); 2499 mlx5_cleanup_fc_stats(dev); 2500 kmem_cache_destroy(steering->ftes_cache); 2501 kmem_cache_destroy(steering->fgs_cache); 2502 kfree(steering); 2503 } 2504 2505 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering) 2506 { 2507 struct fs_prio *prio; 2508 2509 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX); 2510 if (!steering->sniffer_tx_root_ns) 2511 return -ENOMEM; 2512 2513 /* Create single prio */ 2514 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1); 2515 return PTR_ERR_OR_ZERO(prio); 2516 } 2517 2518 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering) 2519 { 2520 struct fs_prio *prio; 2521 2522 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX); 2523 if (!steering->sniffer_rx_root_ns) 2524 return -ENOMEM; 2525 2526 /* Create single prio */ 2527 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1); 2528 return PTR_ERR_OR_ZERO(prio); 2529 } 2530 2531 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering) 2532 { 2533 int err; 2534 2535 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX); 2536 if (!steering->rdma_rx_root_ns) 2537 return -ENOMEM; 2538 2539 err = init_root_tree(steering, &rdma_rx_root_fs, 2540 &steering->rdma_rx_root_ns->ns.node); 2541 if (err) 2542 goto out_err; 2543 2544 set_prio_attrs(steering->rdma_rx_root_ns); 2545 2546 return 0; 2547 2548 out_err: 2549 cleanup_root_ns(steering->rdma_rx_root_ns); 2550 steering->rdma_rx_root_ns = NULL; 2551 return err; 2552 } 2553 static int init_fdb_root_ns(struct mlx5_flow_steering *steering) 2554 { 2555 struct mlx5_flow_namespace *ns; 2556 struct fs_prio *maj_prio; 2557 struct fs_prio *min_prio; 2558 int levels; 2559 int chain; 2560 int prio; 2561 int err; 2562 2563 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB); 2564 if (!steering->fdb_root_ns) 2565 return -ENOMEM; 2566 2567 steering->fdb_sub_ns = kzalloc(sizeof(steering->fdb_sub_ns) * 2568 (FDB_MAX_CHAIN + 1), GFP_KERNEL); 2569 if (!steering->fdb_sub_ns) 2570 return -ENOMEM; 2571 2572 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH, 2573 1); 2574 if (IS_ERR(maj_prio)) { 2575 err = PTR_ERR(maj_prio); 2576 goto out_err; 2577 } 2578 2579 levels = 2 * FDB_MAX_PRIO * (FDB_MAX_CHAIN + 1); 2580 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns, 2581 FDB_FAST_PATH, 2582 levels); 2583 if (IS_ERR(maj_prio)) { 2584 err = PTR_ERR(maj_prio); 2585 goto out_err; 2586 } 2587 2588 for (chain = 0; chain <= FDB_MAX_CHAIN; chain++) { 2589 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF); 2590 if (IS_ERR(ns)) { 2591 err = PTR_ERR(ns); 2592 goto out_err; 2593 } 2594 2595 for (prio = 0; prio < FDB_MAX_PRIO * (chain + 1); prio++) { 2596 min_prio = fs_create_prio(ns, prio, 2); 2597 if (IS_ERR(min_prio)) { 2598 err = PTR_ERR(min_prio); 2599 goto out_err; 2600 } 2601 } 2602 2603 steering->fdb_sub_ns[chain] = ns; 2604 } 2605 2606 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1); 2607 if (IS_ERR(maj_prio)) { 2608 err = PTR_ERR(maj_prio); 2609 goto out_err; 2610 } 2611 2612 set_prio_attrs(steering->fdb_root_ns); 2613 return 0; 2614 2615 out_err: 2616 cleanup_root_ns(steering->fdb_root_ns); 2617 kfree(steering->fdb_sub_ns); 2618 steering->fdb_sub_ns = NULL; 2619 steering->fdb_root_ns = NULL; 2620 return err; 2621 } 2622 2623 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport) 2624 { 2625 struct fs_prio *prio; 2626 2627 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL); 2628 if (!steering->esw_egress_root_ns[vport]) 2629 return -ENOMEM; 2630 2631 /* create 1 prio*/ 2632 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1); 2633 return PTR_ERR_OR_ZERO(prio); 2634 } 2635 2636 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport) 2637 { 2638 struct fs_prio *prio; 2639 2640 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL); 2641 if (!steering->esw_ingress_root_ns[vport]) 2642 return -ENOMEM; 2643 2644 /* create 1 prio*/ 2645 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1); 2646 return PTR_ERR_OR_ZERO(prio); 2647 } 2648 2649 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev) 2650 { 2651 struct mlx5_flow_steering *steering = dev->priv.steering; 2652 int total_vports = mlx5_eswitch_get_total_vports(dev); 2653 int err; 2654 int i; 2655 2656 steering->esw_egress_root_ns = 2657 kcalloc(total_vports, 2658 sizeof(*steering->esw_egress_root_ns), 2659 GFP_KERNEL); 2660 if (!steering->esw_egress_root_ns) 2661 return -ENOMEM; 2662 2663 for (i = 0; i < total_vports; i++) { 2664 err = init_egress_acl_root_ns(steering, i); 2665 if (err) 2666 goto cleanup_root_ns; 2667 } 2668 2669 return 0; 2670 2671 cleanup_root_ns: 2672 for (i--; i >= 0; i--) 2673 cleanup_root_ns(steering->esw_egress_root_ns[i]); 2674 kfree(steering->esw_egress_root_ns); 2675 steering->esw_egress_root_ns = NULL; 2676 return err; 2677 } 2678 2679 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev) 2680 { 2681 struct mlx5_flow_steering *steering = dev->priv.steering; 2682 int total_vports = mlx5_eswitch_get_total_vports(dev); 2683 int err; 2684 int i; 2685 2686 steering->esw_ingress_root_ns = 2687 kcalloc(total_vports, 2688 sizeof(*steering->esw_ingress_root_ns), 2689 GFP_KERNEL); 2690 if (!steering->esw_ingress_root_ns) 2691 return -ENOMEM; 2692 2693 for (i = 0; i < total_vports; i++) { 2694 err = init_ingress_acl_root_ns(steering, i); 2695 if (err) 2696 goto cleanup_root_ns; 2697 } 2698 2699 return 0; 2700 2701 cleanup_root_ns: 2702 for (i--; i >= 0; i--) 2703 cleanup_root_ns(steering->esw_ingress_root_ns[i]); 2704 kfree(steering->esw_ingress_root_ns); 2705 steering->esw_ingress_root_ns = NULL; 2706 return err; 2707 } 2708 2709 static int init_egress_root_ns(struct mlx5_flow_steering *steering) 2710 { 2711 int err; 2712 2713 steering->egress_root_ns = create_root_ns(steering, 2714 FS_FT_NIC_TX); 2715 if (!steering->egress_root_ns) 2716 return -ENOMEM; 2717 2718 err = init_root_tree(steering, &egress_root_fs, 2719 &steering->egress_root_ns->ns.node); 2720 if (err) 2721 goto cleanup; 2722 set_prio_attrs(steering->egress_root_ns); 2723 return 0; 2724 cleanup: 2725 cleanup_root_ns(steering->egress_root_ns); 2726 steering->egress_root_ns = NULL; 2727 return err; 2728 } 2729 2730 int mlx5_init_fs(struct mlx5_core_dev *dev) 2731 { 2732 struct mlx5_flow_steering *steering; 2733 int err = 0; 2734 2735 err = mlx5_init_fc_stats(dev); 2736 if (err) 2737 return err; 2738 2739 steering = kzalloc(sizeof(*steering), GFP_KERNEL); 2740 if (!steering) 2741 return -ENOMEM; 2742 steering->dev = dev; 2743 dev->priv.steering = steering; 2744 2745 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs", 2746 sizeof(struct mlx5_flow_group), 0, 2747 0, NULL); 2748 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0, 2749 0, NULL); 2750 if (!steering->ftes_cache || !steering->fgs_cache) { 2751 err = -ENOMEM; 2752 goto err; 2753 } 2754 2755 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 2756 (MLX5_CAP_GEN(dev, nic_flow_table))) || 2757 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) && 2758 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) && 2759 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) { 2760 err = init_root_ns(steering); 2761 if (err) 2762 goto err; 2763 } 2764 2765 if (MLX5_ESWITCH_MANAGER(dev)) { 2766 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) { 2767 err = init_fdb_root_ns(steering); 2768 if (err) 2769 goto err; 2770 } 2771 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) { 2772 err = init_egress_acls_root_ns(dev); 2773 if (err) 2774 goto err; 2775 } 2776 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) { 2777 err = init_ingress_acls_root_ns(dev); 2778 if (err) 2779 goto err; 2780 } 2781 } 2782 2783 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) { 2784 err = init_sniffer_rx_root_ns(steering); 2785 if (err) 2786 goto err; 2787 } 2788 2789 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) { 2790 err = init_sniffer_tx_root_ns(steering); 2791 if (err) 2792 goto err; 2793 } 2794 2795 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) && 2796 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) { 2797 err = init_rdma_rx_root_ns(steering); 2798 if (err) 2799 goto err; 2800 } 2801 2802 if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) { 2803 err = init_egress_root_ns(steering); 2804 if (err) 2805 goto err; 2806 } 2807 2808 return 0; 2809 err: 2810 mlx5_cleanup_fs(dev); 2811 return err; 2812 } 2813 2814 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn) 2815 { 2816 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns; 2817 struct mlx5_ft_underlay_qp *new_uqp; 2818 int err = 0; 2819 2820 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL); 2821 if (!new_uqp) 2822 return -ENOMEM; 2823 2824 mutex_lock(&root->chain_lock); 2825 2826 if (!root->root_ft) { 2827 err = -EINVAL; 2828 goto update_ft_fail; 2829 } 2830 2831 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn, 2832 false); 2833 if (err) { 2834 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n", 2835 underlay_qpn, err); 2836 goto update_ft_fail; 2837 } 2838 2839 new_uqp->qpn = underlay_qpn; 2840 list_add_tail(&new_uqp->list, &root->underlay_qpns); 2841 2842 mutex_unlock(&root->chain_lock); 2843 2844 return 0; 2845 2846 update_ft_fail: 2847 mutex_unlock(&root->chain_lock); 2848 kfree(new_uqp); 2849 return err; 2850 } 2851 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn); 2852 2853 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn) 2854 { 2855 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns; 2856 struct mlx5_ft_underlay_qp *uqp; 2857 bool found = false; 2858 int err = 0; 2859 2860 mutex_lock(&root->chain_lock); 2861 list_for_each_entry(uqp, &root->underlay_qpns, list) { 2862 if (uqp->qpn == underlay_qpn) { 2863 found = true; 2864 break; 2865 } 2866 } 2867 2868 if (!found) { 2869 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n", 2870 underlay_qpn); 2871 err = -EINVAL; 2872 goto out; 2873 } 2874 2875 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn, 2876 true); 2877 if (err) 2878 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n", 2879 underlay_qpn, err); 2880 2881 list_del(&uqp->list); 2882 mutex_unlock(&root->chain_lock); 2883 kfree(uqp); 2884 2885 return 0; 2886 2887 out: 2888 mutex_unlock(&root->chain_lock); 2889 return err; 2890 } 2891 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn); 2892 2893 static struct mlx5_flow_root_namespace 2894 *get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type) 2895 { 2896 struct mlx5_flow_namespace *ns; 2897 2898 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS || 2899 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS) 2900 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0); 2901 else 2902 ns = mlx5_get_flow_namespace(dev, ns_type); 2903 if (!ns) 2904 return NULL; 2905 2906 return find_root(&ns->node); 2907 } 2908 2909 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev, 2910 u8 ns_type, u8 num_actions, 2911 void *modify_actions) 2912 { 2913 struct mlx5_flow_root_namespace *root; 2914 struct mlx5_modify_hdr *modify_hdr; 2915 int err; 2916 2917 root = get_root_namespace(dev, ns_type); 2918 if (!root) 2919 return ERR_PTR(-EOPNOTSUPP); 2920 2921 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL); 2922 if (!modify_hdr) 2923 return ERR_PTR(-ENOMEM); 2924 2925 modify_hdr->ns_type = ns_type; 2926 err = root->cmds->modify_header_alloc(root, ns_type, num_actions, 2927 modify_actions, modify_hdr); 2928 if (err) { 2929 kfree(modify_hdr); 2930 return ERR_PTR(err); 2931 } 2932 2933 return modify_hdr; 2934 } 2935 EXPORT_SYMBOL(mlx5_modify_header_alloc); 2936 2937 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev, 2938 struct mlx5_modify_hdr *modify_hdr) 2939 { 2940 struct mlx5_flow_root_namespace *root; 2941 2942 root = get_root_namespace(dev, modify_hdr->ns_type); 2943 if (WARN_ON(!root)) 2944 return; 2945 root->cmds->modify_header_dealloc(root, modify_hdr); 2946 kfree(modify_hdr); 2947 } 2948 EXPORT_SYMBOL(mlx5_modify_header_dealloc); 2949 2950 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev, 2951 int reformat_type, 2952 size_t size, 2953 void *reformat_data, 2954 enum mlx5_flow_namespace_type ns_type) 2955 { 2956 struct mlx5_pkt_reformat *pkt_reformat; 2957 struct mlx5_flow_root_namespace *root; 2958 int err; 2959 2960 root = get_root_namespace(dev, ns_type); 2961 if (!root) 2962 return ERR_PTR(-EOPNOTSUPP); 2963 2964 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL); 2965 if (!pkt_reformat) 2966 return ERR_PTR(-ENOMEM); 2967 2968 pkt_reformat->ns_type = ns_type; 2969 pkt_reformat->reformat_type = reformat_type; 2970 err = root->cmds->packet_reformat_alloc(root, reformat_type, size, 2971 reformat_data, ns_type, 2972 pkt_reformat); 2973 if (err) { 2974 kfree(pkt_reformat); 2975 return ERR_PTR(err); 2976 } 2977 2978 return pkt_reformat; 2979 } 2980 EXPORT_SYMBOL(mlx5_packet_reformat_alloc); 2981 2982 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev, 2983 struct mlx5_pkt_reformat *pkt_reformat) 2984 { 2985 struct mlx5_flow_root_namespace *root; 2986 2987 root = get_root_namespace(dev, pkt_reformat->ns_type); 2988 if (WARN_ON(!root)) 2989 return; 2990 root->cmds->packet_reformat_dealloc(root, pkt_reformat); 2991 kfree(pkt_reformat); 2992 } 2993 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc); 2994 2995 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns, 2996 struct mlx5_flow_root_namespace *peer_ns) 2997 { 2998 if (peer_ns && ns->mode != peer_ns->mode) { 2999 mlx5_core_err(ns->dev, 3000 "Can't peer namespace of different steering mode\n"); 3001 return -EINVAL; 3002 } 3003 3004 return ns->cmds->set_peer(ns, peer_ns); 3005 } 3006 3007 /* This function should be called only at init stage of the namespace. 3008 * It is not safe to call this function while steering operations 3009 * are executed in the namespace. 3010 */ 3011 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns, 3012 enum mlx5_flow_steering_mode mode) 3013 { 3014 struct mlx5_flow_root_namespace *root; 3015 const struct mlx5_flow_cmds *cmds; 3016 int err; 3017 3018 root = find_root(&ns->node); 3019 if (&root->ns != ns) 3020 /* Can't set cmds to non root namespace */ 3021 return -EINVAL; 3022 3023 if (root->table_type != FS_FT_FDB) 3024 return -EOPNOTSUPP; 3025 3026 if (root->mode == mode) 3027 return 0; 3028 3029 if (mode == MLX5_FLOW_STEERING_MODE_SMFS) 3030 cmds = mlx5_fs_cmd_get_dr_cmds(); 3031 else 3032 cmds = mlx5_fs_cmd_get_fw_cmds(); 3033 if (!cmds) 3034 return -EOPNOTSUPP; 3035 3036 err = cmds->create_ns(root); 3037 if (err) { 3038 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n", 3039 err); 3040 return err; 3041 } 3042 3043 root->cmds->destroy_ns(root); 3044 root->cmds = cmds; 3045 root->mode = mode; 3046 3047 return 0; 3048 } 3049