1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2021 NVIDIA CORPORATION. All rights reserved. 3 */ 4 5 #ifndef MLX5_DR_H_ 6 #define MLX5_DR_H_ 7 8 #include <rte_flow.h> 9 10 struct mlx5dr_context; 11 struct mlx5dr_table; 12 struct mlx5dr_matcher; 13 struct mlx5dr_rule; 14 15 enum mlx5dr_table_type { 16 MLX5DR_TABLE_TYPE_NIC_RX, 17 MLX5DR_TABLE_TYPE_NIC_TX, 18 MLX5DR_TABLE_TYPE_FDB, 19 MLX5DR_TABLE_TYPE_MAX, 20 }; 21 22 enum mlx5dr_matcher_resource_mode { 23 /* Allocate resources based on number of rules with minimal failure probability */ 24 MLX5DR_MATCHER_RESOURCE_MODE_RULE, 25 /* Allocate fixed size hash table based on given column and rows */ 26 MLX5DR_MATCHER_RESOURCE_MODE_HTABLE, 27 }; 28 29 enum mlx5dr_action_flags { 30 MLX5DR_ACTION_FLAG_ROOT_RX = 1 << 0, 31 MLX5DR_ACTION_FLAG_ROOT_TX = 1 << 1, 32 MLX5DR_ACTION_FLAG_ROOT_FDB = 1 << 2, 33 MLX5DR_ACTION_FLAG_HWS_RX = 1 << 3, 34 MLX5DR_ACTION_FLAG_HWS_TX = 1 << 4, 35 MLX5DR_ACTION_FLAG_HWS_FDB = 1 << 5, 36 MLX5DR_ACTION_FLAG_INLINE = 1 << 6, 37 }; 38 39 enum mlx5dr_action_reformat_type { 40 MLX5DR_ACTION_REFORMAT_TYPE_TNL_L2_TO_L2, 41 MLX5DR_ACTION_REFORMAT_TYPE_L2_TO_TNL_L2, 42 MLX5DR_ACTION_REFORMAT_TYPE_TNL_L3_TO_L2, 43 MLX5DR_ACTION_REFORMAT_TYPE_L2_TO_TNL_L3, 44 }; 45 46 enum mlx5dr_match_template_flags { 47 /* Allow relaxed matching by skipping derived dependent match fields. */ 48 MLX5DR_MATCH_TEMPLATE_FLAG_RELAXED_MATCH = 1, 49 }; 50 51 enum mlx5dr_send_queue_actions { 52 /* Start executing all pending queued rules and write to HW */ 53 MLX5DR_SEND_QUEUE_ACTION_DRAIN = 1 << 0, 54 }; 55 56 struct mlx5dr_context_attr { 57 uint16_t queues; 58 uint16_t queue_size; 59 size_t initial_log_ste_memory; 60 /* Optional PD used for allocating res ources */ 61 struct ibv_pd *pd; 62 }; 63 64 struct mlx5dr_table_attr { 65 enum mlx5dr_table_type type; 66 uint32_t level; 67 }; 68 69 struct mlx5dr_matcher_attr { 70 uint32_t priority; 71 enum mlx5dr_matcher_resource_mode mode; 72 union { 73 struct { 74 uint8_t sz_row_log; 75 uint8_t sz_col_log; 76 } table; 77 78 struct { 79 uint8_t num_log; 80 } rule; 81 }; 82 }; 83 84 struct mlx5dr_rule_attr { 85 uint16_t queue_id; 86 void *user_data; 87 uint32_t burst:1; 88 }; 89 90 struct mlx5dr_devx_obj { 91 struct mlx5dv_devx_obj *obj; 92 uint32_t id; 93 }; 94 95 struct mlx5dr_rule_action { 96 struct mlx5dr_action *action; 97 union { 98 struct { 99 uint32_t value; 100 } tag; 101 102 struct { 103 uint32_t offset; 104 } counter; 105 106 struct { 107 uint32_t offset; 108 uint8_t *data; 109 } modify_header; 110 111 struct { 112 uint32_t offset; 113 uint8_t *data; 114 } reformat; 115 116 struct { 117 rte_be32_t vlan_hdr; 118 } push_vlan; 119 }; 120 }; 121 122 enum { 123 MLX5DR_MATCH_TAG_SZ = 32, 124 MLX5DR_JAMBO_TAG_SZ = 44, 125 }; 126 127 enum mlx5dr_rule_status { 128 MLX5DR_RULE_STATUS_UNKNOWN, 129 MLX5DR_RULE_STATUS_CREATING, 130 MLX5DR_RULE_STATUS_CREATED, 131 MLX5DR_RULE_STATUS_DELETING, 132 MLX5DR_RULE_STATUS_DELETED, 133 MLX5DR_RULE_STATUS_FAILED, 134 }; 135 136 struct mlx5dr_rule { 137 struct mlx5dr_matcher *matcher; 138 union { 139 uint8_t match_tag[MLX5DR_MATCH_TAG_SZ]; 140 struct ibv_flow *flow; 141 }; 142 enum mlx5dr_rule_status status; 143 uint32_t rtc_used; /* The RTC into which the STE was inserted */ 144 }; 145 146 /* Open a context used for direct rule insertion using hardware steering. 147 * Each context can contain multiple tables of different types. 148 * 149 * @param[in] ibv_ctx 150 * The ibv context to used for HWS. 151 * @param[in] attr 152 * Attributes used for context open. 153 * @return pointer to mlx5dr_context on success NULL otherwise. 154 */ 155 struct mlx5dr_context * 156 mlx5dr_context_open(void *ibv_ctx, 157 struct mlx5dr_context_attr *attr); 158 159 /* Close a context used for direct hardware steering. 160 * 161 * @param[in] ctx 162 * mlx5dr context to close. 163 * @return zero on success non zero otherwise. 164 */ 165 int mlx5dr_context_close(struct mlx5dr_context *ctx); 166 167 /* Create a new direct rule table. Each table can contain multiple matchers. 168 * 169 * @param[in] ctx 170 * The context in which the new table will be opened. 171 * @param[in] attr 172 * Attributes used for table creation. 173 * @return pointer to mlx5dr_table on success NULL otherwise. 174 */ 175 struct mlx5dr_table * 176 mlx5dr_table_create(struct mlx5dr_context *ctx, 177 struct mlx5dr_table_attr *attr); 178 179 /* Destroy direct rule table. 180 * 181 * @param[in] tbl 182 * mlx5dr table to destroy. 183 * @return zero on success non zero otherwise. 184 */ 185 int mlx5dr_table_destroy(struct mlx5dr_table *tbl); 186 187 /* Create new match template based on items mask, the match template 188 * will be used for matcher creation. 189 * 190 * @param[in] items 191 * Describe the mask for template creation 192 * @param[in] flags 193 * Template creation flags 194 * @return pointer to mlx5dr_match_template on success NULL otherwise 195 */ 196 struct mlx5dr_match_template * 197 mlx5dr_match_template_create(const struct rte_flow_item items[], 198 enum mlx5dr_match_template_flags flags); 199 200 /* Destroy match template. 201 * 202 * @param[in] mt 203 * Match template to destroy. 204 * @return zero on success non zero otherwise. 205 */ 206 int mlx5dr_match_template_destroy(struct mlx5dr_match_template *mt); 207 208 /* Create a new direct rule matcher. Each matcher can contain multiple rules. 209 * Matchers on the table will be processed by priority. Matching fields and 210 * mask are described by the match template. In some cases multiple match 211 * templates can be used on the same matcher. 212 * 213 * @param[in] table 214 * The table in which the new matcher will be opened. 215 * @param[in] mt 216 * Array of match templates to be used on matcher. 217 * @param[in] num_of_mt 218 * Number of match templates in mt array. 219 * @param[in] attr 220 * Attributes used for matcher creation. 221 * @return pointer to mlx5dr_matcher on success NULL otherwise. 222 */ 223 struct mlx5dr_matcher * 224 mlx5dr_matcher_create(struct mlx5dr_table *table, 225 struct mlx5dr_match_template *mt[], 226 uint8_t num_of_mt, 227 struct mlx5dr_matcher_attr *attr); 228 229 /* Destroy direct rule matcher. 230 * 231 * @param[in] matcher 232 * Matcher to destroy. 233 * @return zero on success non zero otherwise. 234 */ 235 int mlx5dr_matcher_destroy(struct mlx5dr_matcher *matcher); 236 237 /* Get the size of the rule handle (mlx5dr_rule) to be used on rule creation. 238 * 239 * @return size in bytes of rule handle struct. 240 */ 241 size_t mlx5dr_rule_get_handle_size(void); 242 243 /* Enqueue create rule operation. 244 * 245 * @param[in] matcher 246 * The matcher in which the new rule will be created. 247 * @param[in] mt_idx 248 * Match template index to create the rule with. 249 * @param[in] items 250 * The items used for the value matching. 251 * @param[in] rule_actions 252 * Rule action to be executed on match. 253 * @param[in] num_of_actions 254 * Number of rule actions. 255 * @param[in] attr 256 * Rule creation attributes. 257 * @param[in, out] rule_handle 258 * A valid rule handle. The handle doesn't require any initialization. 259 * @return zero on successful enqueue non zero otherwise. 260 */ 261 int mlx5dr_rule_create(struct mlx5dr_matcher *matcher, 262 uint8_t mt_idx, 263 const struct rte_flow_item items[], 264 struct mlx5dr_rule_action rule_actions[], 265 uint8_t num_of_actions, 266 struct mlx5dr_rule_attr *attr, 267 struct mlx5dr_rule *rule_handle); 268 269 /* Enqueue destroy rule operation. 270 * 271 * @param[in] rule 272 * The rule destruction to enqueue. 273 * @param[in] attr 274 * Rule destruction attributes. 275 * @return zero on successful enqueue non zero otherwise. 276 */ 277 int mlx5dr_rule_destroy(struct mlx5dr_rule *rule, 278 struct mlx5dr_rule_attr *attr); 279 280 /* Create direct rule drop action. 281 * 282 * @param[in] ctx 283 * The context in which the new action will be created. 284 * @param[in] flags 285 * Action creation flags. (enum mlx5dr_action_flags) 286 * @return pointer to mlx5dr_action on success NULL otherwise. 287 */ 288 struct mlx5dr_action * 289 mlx5dr_action_create_dest_drop(struct mlx5dr_context *ctx, 290 uint32_t flags); 291 292 /* Create direct rule default miss action. 293 * Defaults are RX: Drop TX: Wire. 294 * 295 * @param[in] ctx 296 * The context in which the new action will be created. 297 * @param[in] flags 298 * Action creation flags. (enum mlx5dr_action_flags) 299 * @return pointer to mlx5dr_action on success NULL otherwise. 300 */ 301 struct mlx5dr_action * 302 mlx5dr_action_create_default_miss(struct mlx5dr_context *ctx, 303 uint32_t flags); 304 305 /* Create direct rule goto table action. 306 * 307 * @param[in] ctx 308 * The context in which the new action will be created. 309 * @param[in] tbl 310 * Destination table. 311 * @param[in] flags 312 * Action creation flags. (enum mlx5dr_action_flags) 313 * @return pointer to mlx5dr_action on success NULL otherwise. 314 */ 315 struct mlx5dr_action * 316 mlx5dr_action_create_dest_table(struct mlx5dr_context *ctx, 317 struct mlx5dr_table *tbl, 318 uint32_t flags); 319 320 /* Create direct rule goto TIR action. 321 * 322 * @param[in] ctx 323 * The context in which the new action will be created. 324 * @param[in] obj 325 * Direct rule TIR devx object. 326 * @param[in] flags 327 * Action creation flags. (enum mlx5dr_action_flags) 328 * @return pointer to mlx5dr_action on success NULL otherwise. 329 */ 330 struct mlx5dr_action * 331 mlx5dr_action_create_dest_tir(struct mlx5dr_context *ctx, 332 struct mlx5dr_devx_obj *obj, 333 uint32_t flags); 334 335 /* Create direct rule TAG action. 336 * 337 * @param[in] ctx 338 * The context in which the new action will be created. 339 * @param[in] flags 340 * Action creation flags. (enum mlx5dr_action_flags) 341 * @return pointer to mlx5dr_action on success NULL otherwise. 342 */ 343 struct mlx5dr_action * 344 mlx5dr_action_create_tag(struct mlx5dr_context *ctx, 345 uint32_t flags); 346 347 /* Create direct rule counter action. 348 * 349 * @param[in] ctx 350 * The context in which the new action will be created. 351 * @param[in] obj 352 * Direct rule counter devx object. 353 * @param[in] flags 354 * Action creation flags. (enum mlx5dr_action_flags) 355 * @return pointer to mlx5dr_action on success NULL otherwise. 356 */ 357 struct mlx5dr_action * 358 mlx5dr_action_create_counter(struct mlx5dr_context *ctx, 359 struct mlx5dr_devx_obj *obj, 360 uint32_t flags); 361 362 /* Create direct rule reformat action. 363 * 364 * @param[in] ctx 365 * The context in which the new action will be created. 366 * @param[in] reformat_type 367 * Type of reformat. 368 * @param[in] data_sz 369 * Size in bytes of data. 370 * @param[in] inline_data 371 * Header data array in case of inline action. 372 * @param[in] log_bulk_size 373 * Number of unique values used with this pattern. 374 * @param[in] flags 375 * Action creation flags. (enum mlx5dr_action_flags) 376 * @return pointer to mlx5dr_action on success NULL otherwise. 377 */ 378 struct mlx5dr_action * 379 mlx5dr_action_create_reformat(struct mlx5dr_context *ctx, 380 enum mlx5dr_action_reformat_type reformat_type, 381 size_t data_sz, 382 void *inline_data, 383 uint32_t log_bulk_size, 384 uint32_t flags); 385 386 /* Create direct rule modify header action. 387 * 388 * @param[in] ctx 389 * The context in which the new action will be created. 390 * @param[in] pattern_sz 391 * Byte size of the pattern array. 392 * @param[in] pattern 393 * PRM format modify pattern action array. 394 * @param[in] log_bulk_size 395 * Number of unique values used with this pattern. 396 * @param[in] flags 397 * Action creation flags. (enum mlx5dr_action_flags) 398 * @return pointer to mlx5dr_action on success NULL otherwise. 399 */ 400 struct mlx5dr_action * 401 mlx5dr_action_create_modify_header(struct mlx5dr_context *ctx, 402 size_t pattern_sz, 403 rte_be64_t pattern[], 404 uint32_t log_bulk_size, 405 uint32_t flags); 406 407 /* Destroy direct rule action. 408 * 409 * @param[in] action 410 * The action to destroy. 411 * @return zero on success non zero otherwise. 412 */ 413 int mlx5dr_action_destroy(struct mlx5dr_action *action); 414 415 /* Poll queue for rule creation and deletions completions. 416 * 417 * @param[in] ctx 418 * The context to which the queue belong to. 419 * @param[in] queue_id 420 * The id of the queue to poll. 421 * @param[in, out] res 422 * Completion array. 423 * @param[in] res_nb 424 * Maximum number of results to return. 425 * @return negative number on failure, the number of completions otherwise. 426 */ 427 int mlx5dr_send_queue_poll(struct mlx5dr_context *ctx, 428 uint16_t queue_id, 429 struct rte_flow_op_result res[], 430 uint32_t res_nb); 431 432 /* Perform an action on the queue 433 * 434 * @param[in] ctx 435 * The context to which the queue belong to. 436 * @param[in] queue_id 437 * The id of the queue to perform the action on. 438 * @param[in] actions 439 * Actions to perform on the queue. (enum mlx5dr_send_queue_actions) 440 * @return zero on success non zero otherwise. 441 */ 442 int mlx5dr_send_queue_action(struct mlx5dr_context *ctx, 443 uint16_t queue_id, 444 uint32_t actions); 445 446 /* Dump HWS info 447 * 448 * @param[in] ctx 449 * The context which to dump the info from. 450 * @param[in] f 451 * The file to write the dump to. 452 * @return zero on success non zero otherwise. 453 */ 454 int mlx5dr_debug_dump(struct mlx5dr_context *ctx, FILE *f); 455 456 #endif 457