1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */ 3 4 #include <linux/kernel.h> 5 #include <linux/slab.h> 6 #include <linux/errno.h> 7 #include <linux/list.h> 8 #include <linux/string.h> 9 #include <linux/rhashtable.h> 10 #include <linux/netdevice.h> 11 #include <linux/mutex.h> 12 #include <net/net_namespace.h> 13 #include <net/tc_act/tc_vlan.h> 14 15 #include "reg.h" 16 #include "core.h" 17 #include "resources.h" 18 #include "spectrum.h" 19 #include "core_acl_flex_keys.h" 20 #include "core_acl_flex_actions.h" 21 #include "spectrum_acl_tcam.h" 22 23 struct mlxsw_sp_acl { 24 struct mlxsw_sp *mlxsw_sp; 25 struct mlxsw_afk *afk; 26 struct mlxsw_sp_fid *dummy_fid; 27 struct rhashtable ruleset_ht; 28 struct list_head rules; 29 struct mutex rules_lock; /* Protects rules list */ 30 struct { 31 struct delayed_work dw; 32 unsigned long interval; /* ms */ 33 #define MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS 1000 34 } rule_activity_update; 35 struct mlxsw_sp_acl_tcam tcam; 36 }; 37 38 struct mlxsw_afk *mlxsw_sp_acl_afk(struct mlxsw_sp_acl *acl) 39 { 40 return acl->afk; 41 } 42 43 struct mlxsw_sp_acl_tcam *mlxsw_sp_acl_to_tcam(struct mlxsw_sp_acl *acl) 44 { 45 return &acl->tcam; 46 } 47 48 struct mlxsw_sp_acl_ruleset_ht_key { 49 struct mlxsw_sp_flow_block *block; 50 u32 chain_index; 51 const struct mlxsw_sp_acl_profile_ops *ops; 52 }; 53 54 struct mlxsw_sp_acl_ruleset { 55 struct rhash_head ht_node; /* Member of acl HT */ 56 struct mlxsw_sp_acl_ruleset_ht_key ht_key; 57 struct rhashtable rule_ht; 58 unsigned int ref_count; 59 unsigned int min_prio; 60 unsigned int max_prio; 61 unsigned long priv[]; 62 /* priv has to be always the last item */ 63 }; 64 65 struct mlxsw_sp_acl_rule { 66 struct rhash_head ht_node; /* Member of rule HT */ 67 struct list_head list; 68 unsigned long cookie; /* HT key */ 69 struct mlxsw_sp_acl_ruleset *ruleset; 70 struct mlxsw_sp_acl_rule_info *rulei; 71 u64 last_used; 72 u64 last_packets; 73 u64 last_bytes; 74 u64 last_drops; 75 unsigned long priv[]; 76 /* priv has to be always the last item */ 77 }; 78 79 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = { 80 .key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key), 81 .key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key), 82 .head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node), 83 .automatic_shrinking = true, 84 }; 85 86 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = { 87 .key_len = sizeof(unsigned long), 88 .key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie), 89 .head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node), 90 .automatic_shrinking = true, 91 }; 92 93 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp) 94 { 95 return mlxsw_sp->acl->dummy_fid; 96 } 97 98 static bool 99 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset) 100 { 101 /* We hold a reference on ruleset ourselves */ 102 return ruleset->ref_count == 2; 103 } 104 105 int mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp, 106 struct mlxsw_sp_flow_block *block, 107 struct mlxsw_sp_flow_block_binding *binding) 108 { 109 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero; 110 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 111 112 return ops->ruleset_bind(mlxsw_sp, ruleset->priv, 113 binding->mlxsw_sp_port, binding->ingress); 114 } 115 116 void mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp, 117 struct mlxsw_sp_flow_block *block, 118 struct mlxsw_sp_flow_block_binding *binding) 119 { 120 struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero; 121 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 122 123 ops->ruleset_unbind(mlxsw_sp, ruleset->priv, 124 binding->mlxsw_sp_port, binding->ingress); 125 } 126 127 static int 128 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp, 129 struct mlxsw_sp_acl_ruleset *ruleset, 130 struct mlxsw_sp_flow_block *block) 131 { 132 struct mlxsw_sp_flow_block_binding *binding; 133 int err; 134 135 block->ruleset_zero = ruleset; 136 list_for_each_entry(binding, &block->binding_list, list) { 137 err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding); 138 if (err) 139 goto rollback; 140 } 141 return 0; 142 143 rollback: 144 list_for_each_entry_continue_reverse(binding, &block->binding_list, 145 list) 146 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding); 147 block->ruleset_zero = NULL; 148 149 return err; 150 } 151 152 static void 153 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp, 154 struct mlxsw_sp_acl_ruleset *ruleset, 155 struct mlxsw_sp_flow_block *block) 156 { 157 struct mlxsw_sp_flow_block_binding *binding; 158 159 list_for_each_entry(binding, &block->binding_list, list) 160 mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding); 161 block->ruleset_zero = NULL; 162 } 163 164 static struct mlxsw_sp_acl_ruleset * 165 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp, 166 struct mlxsw_sp_flow_block *block, u32 chain_index, 167 const struct mlxsw_sp_acl_profile_ops *ops, 168 struct mlxsw_afk_element_usage *tmplt_elusage) 169 { 170 struct mlxsw_sp_acl *acl = mlxsw_sp->acl; 171 struct mlxsw_sp_acl_ruleset *ruleset; 172 size_t alloc_size; 173 int err; 174 175 alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size; 176 ruleset = kzalloc(alloc_size, GFP_KERNEL); 177 if (!ruleset) 178 return ERR_PTR(-ENOMEM); 179 ruleset->ref_count = 1; 180 ruleset->ht_key.block = block; 181 ruleset->ht_key.chain_index = chain_index; 182 ruleset->ht_key.ops = ops; 183 184 err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params); 185 if (err) 186 goto err_rhashtable_init; 187 188 err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv, 189 tmplt_elusage, &ruleset->min_prio, 190 &ruleset->max_prio); 191 if (err) 192 goto err_ops_ruleset_add; 193 194 err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node, 195 mlxsw_sp_acl_ruleset_ht_params); 196 if (err) 197 goto err_ht_insert; 198 199 return ruleset; 200 201 err_ht_insert: 202 ops->ruleset_del(mlxsw_sp, ruleset->priv); 203 err_ops_ruleset_add: 204 rhashtable_destroy(&ruleset->rule_ht); 205 err_rhashtable_init: 206 kfree(ruleset); 207 return ERR_PTR(err); 208 } 209 210 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp, 211 struct mlxsw_sp_acl_ruleset *ruleset) 212 { 213 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 214 struct mlxsw_sp_acl *acl = mlxsw_sp->acl; 215 216 rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node, 217 mlxsw_sp_acl_ruleset_ht_params); 218 ops->ruleset_del(mlxsw_sp, ruleset->priv); 219 rhashtable_destroy(&ruleset->rule_ht); 220 kfree(ruleset); 221 } 222 223 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset) 224 { 225 ruleset->ref_count++; 226 } 227 228 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp, 229 struct mlxsw_sp_acl_ruleset *ruleset) 230 { 231 if (--ruleset->ref_count) 232 return; 233 mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset); 234 } 235 236 static struct mlxsw_sp_acl_ruleset * 237 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl, 238 struct mlxsw_sp_flow_block *block, u32 chain_index, 239 const struct mlxsw_sp_acl_profile_ops *ops) 240 { 241 struct mlxsw_sp_acl_ruleset_ht_key ht_key; 242 243 memset(&ht_key, 0, sizeof(ht_key)); 244 ht_key.block = block; 245 ht_key.chain_index = chain_index; 246 ht_key.ops = ops; 247 return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key, 248 mlxsw_sp_acl_ruleset_ht_params); 249 } 250 251 struct mlxsw_sp_acl_ruleset * 252 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp, 253 struct mlxsw_sp_flow_block *block, u32 chain_index, 254 enum mlxsw_sp_acl_profile profile) 255 { 256 const struct mlxsw_sp_acl_profile_ops *ops; 257 struct mlxsw_sp_acl *acl = mlxsw_sp->acl; 258 struct mlxsw_sp_acl_ruleset *ruleset; 259 260 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile); 261 if (!ops) 262 return ERR_PTR(-EINVAL); 263 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops); 264 if (!ruleset) 265 return ERR_PTR(-ENOENT); 266 return ruleset; 267 } 268 269 struct mlxsw_sp_acl_ruleset * 270 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp, 271 struct mlxsw_sp_flow_block *block, u32 chain_index, 272 enum mlxsw_sp_acl_profile profile, 273 struct mlxsw_afk_element_usage *tmplt_elusage) 274 { 275 const struct mlxsw_sp_acl_profile_ops *ops; 276 struct mlxsw_sp_acl *acl = mlxsw_sp->acl; 277 struct mlxsw_sp_acl_ruleset *ruleset; 278 279 ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile); 280 if (!ops) 281 return ERR_PTR(-EINVAL); 282 283 ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops); 284 if (ruleset) { 285 mlxsw_sp_acl_ruleset_ref_inc(ruleset); 286 return ruleset; 287 } 288 return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops, 289 tmplt_elusage); 290 } 291 292 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp, 293 struct mlxsw_sp_acl_ruleset *ruleset) 294 { 295 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset); 296 } 297 298 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset) 299 { 300 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 301 302 return ops->ruleset_group_id(ruleset->priv); 303 } 304 305 void mlxsw_sp_acl_ruleset_prio_get(struct mlxsw_sp_acl_ruleset *ruleset, 306 unsigned int *p_min_prio, 307 unsigned int *p_max_prio) 308 { 309 *p_min_prio = ruleset->min_prio; 310 *p_max_prio = ruleset->max_prio; 311 } 312 313 struct mlxsw_sp_acl_rule_info * 314 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl, 315 struct mlxsw_afa_block *afa_block) 316 { 317 struct mlxsw_sp_acl_rule_info *rulei; 318 int err; 319 320 rulei = kzalloc(sizeof(*rulei), GFP_KERNEL); 321 if (!rulei) 322 return ERR_PTR(-ENOMEM); 323 324 if (afa_block) { 325 rulei->act_block = afa_block; 326 return rulei; 327 } 328 329 rulei->act_block = mlxsw_afa_block_create(acl->mlxsw_sp->afa); 330 if (IS_ERR(rulei->act_block)) { 331 err = PTR_ERR(rulei->act_block); 332 goto err_afa_block_create; 333 } 334 rulei->action_created = 1; 335 return rulei; 336 337 err_afa_block_create: 338 kfree(rulei); 339 return ERR_PTR(err); 340 } 341 342 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info *rulei) 343 { 344 if (rulei->action_created) 345 mlxsw_afa_block_destroy(rulei->act_block); 346 kfree(rulei); 347 } 348 349 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei) 350 { 351 return mlxsw_afa_block_commit(rulei->act_block); 352 } 353 354 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei, 355 unsigned int priority) 356 { 357 rulei->priority = priority; 358 } 359 360 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei, 361 enum mlxsw_afk_element element, 362 u32 key_value, u32 mask_value) 363 { 364 mlxsw_afk_values_add_u32(&rulei->values, element, 365 key_value, mask_value); 366 } 367 368 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei, 369 enum mlxsw_afk_element element, 370 const char *key_value, 371 const char *mask_value, unsigned int len) 372 { 373 mlxsw_afk_values_add_buf(&rulei->values, element, 374 key_value, mask_value, len); 375 } 376 377 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei) 378 { 379 return mlxsw_afa_block_continue(rulei->act_block); 380 } 381 382 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei, 383 u16 group_id) 384 { 385 return mlxsw_afa_block_jump(rulei->act_block, group_id); 386 } 387 388 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei) 389 { 390 return mlxsw_afa_block_terminate(rulei->act_block); 391 } 392 393 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei, 394 bool ingress, 395 const struct flow_action_cookie *fa_cookie, 396 struct netlink_ext_ack *extack) 397 { 398 return mlxsw_afa_block_append_drop(rulei->act_block, ingress, 399 fa_cookie, extack); 400 } 401 402 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei) 403 { 404 return mlxsw_afa_block_append_trap(rulei->act_block, 405 MLXSW_TRAP_ID_ACL0); 406 } 407 408 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp, 409 struct mlxsw_sp_acl_rule_info *rulei, 410 struct net_device *out_dev, 411 struct netlink_ext_ack *extack) 412 { 413 struct mlxsw_sp_port *mlxsw_sp_port; 414 u16 local_port; 415 bool in_port; 416 417 if (out_dev) { 418 if (!mlxsw_sp_port_dev_check(out_dev)) { 419 NL_SET_ERR_MSG_MOD(extack, "Invalid output device"); 420 return -EINVAL; 421 } 422 mlxsw_sp_port = netdev_priv(out_dev); 423 if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp) { 424 NL_SET_ERR_MSG_MOD(extack, "Invalid output device"); 425 return -EINVAL; 426 } 427 local_port = mlxsw_sp_port->local_port; 428 in_port = false; 429 } else { 430 /* If out_dev is NULL, the caller wants to 431 * set forward to ingress port. 432 */ 433 local_port = 0; 434 in_port = true; 435 } 436 return mlxsw_afa_block_append_fwd(rulei->act_block, 437 local_port, in_port, extack); 438 } 439 440 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp, 441 struct mlxsw_sp_acl_rule_info *rulei, 442 struct mlxsw_sp_flow_block *block, 443 struct net_device *out_dev, 444 struct netlink_ext_ack *extack) 445 { 446 struct mlxsw_sp_flow_block_binding *binding; 447 struct mlxsw_sp_port *in_port; 448 449 if (!list_is_singular(&block->binding_list)) { 450 NL_SET_ERR_MSG_MOD(extack, "Only a single mirror source is allowed"); 451 return -EOPNOTSUPP; 452 } 453 binding = list_first_entry(&block->binding_list, 454 struct mlxsw_sp_flow_block_binding, list); 455 in_port = binding->mlxsw_sp_port; 456 457 return mlxsw_afa_block_append_mirror(rulei->act_block, 458 in_port->local_port, 459 out_dev, 460 binding->ingress, 461 extack); 462 } 463 464 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp, 465 struct mlxsw_sp_acl_rule_info *rulei, 466 u32 action, u16 vid, u16 proto, u8 prio, 467 struct netlink_ext_ack *extack) 468 { 469 u8 ethertype; 470 471 if (action == FLOW_ACTION_VLAN_MANGLE) { 472 switch (proto) { 473 case ETH_P_8021Q: 474 ethertype = 0; 475 break; 476 case ETH_P_8021AD: 477 ethertype = 1; 478 break; 479 default: 480 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN protocol"); 481 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n", 482 proto); 483 return -EINVAL; 484 } 485 486 return mlxsw_afa_block_append_vlan_modify(rulei->act_block, 487 vid, prio, ethertype, 488 extack); 489 } else { 490 NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN action"); 491 dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n"); 492 return -EINVAL; 493 } 494 } 495 496 int mlxsw_sp_acl_rulei_act_priority(struct mlxsw_sp *mlxsw_sp, 497 struct mlxsw_sp_acl_rule_info *rulei, 498 u32 prio, struct netlink_ext_ack *extack) 499 { 500 /* Even though both Linux and Spectrum switches support 16 priorities, 501 * spectrum_qdisc only processes the first eight priomap elements, and 502 * the DCB and PFC features are tied to 8 priorities as well. Therefore 503 * bounce attempts to prioritize packets to higher priorities. 504 */ 505 if (prio >= IEEE_8021QAZ_MAX_TCS) { 506 NL_SET_ERR_MSG_MOD(extack, "Only priorities 0..7 are supported"); 507 return -EINVAL; 508 } 509 return mlxsw_afa_block_append_qos_switch_prio(rulei->act_block, prio, 510 extack); 511 } 512 513 struct mlxsw_sp_acl_mangle_action { 514 enum flow_action_mangle_base htype; 515 /* Offset is u32-aligned. */ 516 u32 offset; 517 /* Mask bits are unset for the modified field. */ 518 u32 mask; 519 /* Shift required to extract the set value. */ 520 u32 shift; 521 enum mlxsw_sp_acl_mangle_field field; 522 }; 523 524 #define MLXSW_SP_ACL_MANGLE_ACTION(_htype, _offset, _mask, _shift, _field) \ 525 { \ 526 .htype = _htype, \ 527 .offset = _offset, \ 528 .mask = _mask, \ 529 .shift = _shift, \ 530 .field = MLXSW_SP_ACL_MANGLE_FIELD_##_field, \ 531 } 532 533 #define MLXSW_SP_ACL_MANGLE_ACTION_IP4(_offset, _mask, _shift, _field) \ 534 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_IP4, \ 535 _offset, _mask, _shift, _field) 536 537 #define MLXSW_SP_ACL_MANGLE_ACTION_IP6(_offset, _mask, _shift, _field) \ 538 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_IP6, \ 539 _offset, _mask, _shift, _field) 540 541 #define MLXSW_SP_ACL_MANGLE_ACTION_TCP(_offset, _mask, _shift, _field) \ 542 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_TCP, _offset, _mask, _shift, _field) 543 544 #define MLXSW_SP_ACL_MANGLE_ACTION_UDP(_offset, _mask, _shift, _field) \ 545 MLXSW_SP_ACL_MANGLE_ACTION(FLOW_ACT_MANGLE_HDR_TYPE_UDP, _offset, _mask, _shift, _field) 546 547 static struct mlxsw_sp_acl_mangle_action mlxsw_sp_acl_mangle_actions[] = { 548 MLXSW_SP_ACL_MANGLE_ACTION_IP4(0, 0xff00ffff, 16, IP_DSFIELD), 549 MLXSW_SP_ACL_MANGLE_ACTION_IP4(0, 0xff03ffff, 18, IP_DSCP), 550 MLXSW_SP_ACL_MANGLE_ACTION_IP4(0, 0xfffcffff, 16, IP_ECN), 551 552 MLXSW_SP_ACL_MANGLE_ACTION_IP6(0, 0xf00fffff, 20, IP_DSFIELD), 553 MLXSW_SP_ACL_MANGLE_ACTION_IP6(0, 0xf03fffff, 22, IP_DSCP), 554 MLXSW_SP_ACL_MANGLE_ACTION_IP6(0, 0xffcfffff, 20, IP_ECN), 555 556 MLXSW_SP_ACL_MANGLE_ACTION_TCP(0, 0x0000ffff, 16, IP_SPORT), 557 MLXSW_SP_ACL_MANGLE_ACTION_TCP(0, 0xffff0000, 0, IP_DPORT), 558 559 MLXSW_SP_ACL_MANGLE_ACTION_UDP(0, 0x0000ffff, 16, IP_SPORT), 560 MLXSW_SP_ACL_MANGLE_ACTION_UDP(0, 0xffff0000, 0, IP_DPORT), 561 562 MLXSW_SP_ACL_MANGLE_ACTION_IP4(12, 0x00000000, 0, IP4_SIP), 563 MLXSW_SP_ACL_MANGLE_ACTION_IP4(16, 0x00000000, 0, IP4_DIP), 564 565 MLXSW_SP_ACL_MANGLE_ACTION_IP6(8, 0x00000000, 0, IP6_SIP_1), 566 MLXSW_SP_ACL_MANGLE_ACTION_IP6(12, 0x00000000, 0, IP6_SIP_2), 567 MLXSW_SP_ACL_MANGLE_ACTION_IP6(16, 0x00000000, 0, IP6_SIP_3), 568 MLXSW_SP_ACL_MANGLE_ACTION_IP6(20, 0x00000000, 0, IP6_SIP_4), 569 MLXSW_SP_ACL_MANGLE_ACTION_IP6(24, 0x00000000, 0, IP6_DIP_1), 570 MLXSW_SP_ACL_MANGLE_ACTION_IP6(28, 0x00000000, 0, IP6_DIP_2), 571 MLXSW_SP_ACL_MANGLE_ACTION_IP6(32, 0x00000000, 0, IP6_DIP_3), 572 MLXSW_SP_ACL_MANGLE_ACTION_IP6(36, 0x00000000, 0, IP6_DIP_4), 573 }; 574 575 static int 576 mlxsw_sp_acl_rulei_act_mangle_field(struct mlxsw_sp *mlxsw_sp, 577 struct mlxsw_sp_acl_rule_info *rulei, 578 struct mlxsw_sp_acl_mangle_action *mact, 579 u32 val, struct netlink_ext_ack *extack) 580 { 581 switch (mact->field) { 582 case MLXSW_SP_ACL_MANGLE_FIELD_IP_DSFIELD: 583 return mlxsw_afa_block_append_qos_dsfield(rulei->act_block, 584 val, extack); 585 case MLXSW_SP_ACL_MANGLE_FIELD_IP_DSCP: 586 return mlxsw_afa_block_append_qos_dscp(rulei->act_block, 587 val, extack); 588 case MLXSW_SP_ACL_MANGLE_FIELD_IP_ECN: 589 return mlxsw_afa_block_append_qos_ecn(rulei->act_block, 590 val, extack); 591 default: 592 return -EOPNOTSUPP; 593 } 594 } 595 596 static int mlxsw_sp1_acl_rulei_act_mangle_field(struct mlxsw_sp *mlxsw_sp, 597 struct mlxsw_sp_acl_rule_info *rulei, 598 struct mlxsw_sp_acl_mangle_action *mact, 599 u32 val, struct netlink_ext_ack *extack) 600 { 601 int err; 602 603 err = mlxsw_sp_acl_rulei_act_mangle_field(mlxsw_sp, rulei, mact, val, extack); 604 if (err != -EOPNOTSUPP) 605 return err; 606 607 NL_SET_ERR_MSG_MOD(extack, "Unsupported mangle field"); 608 return err; 609 } 610 611 static int 612 mlxsw_sp2_acl_rulei_act_mangle_field_ip_odd(struct mlxsw_sp_acl_rule_info *rulei, 613 enum mlxsw_sp_acl_mangle_field field, 614 u32 val, struct netlink_ext_ack *extack) 615 { 616 if (!rulei->ipv6_valid) { 617 rulei->ipv6.prev_val = val; 618 rulei->ipv6_valid = true; 619 rulei->ipv6.prev_field = field; 620 return 0; 621 } 622 623 NL_SET_ERR_MSG_MOD(extack, "Unsupported mangle field order"); 624 return -EOPNOTSUPP; 625 } 626 627 static int mlxsw_sp2_acl_rulei_act_mangle_field(struct mlxsw_sp *mlxsw_sp, 628 struct mlxsw_sp_acl_rule_info *rulei, 629 struct mlxsw_sp_acl_mangle_action *mact, 630 u32 val, struct netlink_ext_ack *extack) 631 { 632 int err; 633 634 err = mlxsw_sp_acl_rulei_act_mangle_field(mlxsw_sp, rulei, mact, val, extack); 635 if (err != -EOPNOTSUPP) 636 return err; 637 638 switch (mact->field) { 639 case MLXSW_SP_ACL_MANGLE_FIELD_IP_SPORT: 640 return mlxsw_afa_block_append_l4port(rulei->act_block, false, val, extack); 641 case MLXSW_SP_ACL_MANGLE_FIELD_IP_DPORT: 642 return mlxsw_afa_block_append_l4port(rulei->act_block, true, val, extack); 643 /* IPv4 fields */ 644 case MLXSW_SP_ACL_MANGLE_FIELD_IP4_SIP: 645 return mlxsw_afa_block_append_ip(rulei->act_block, false, 646 true, val, 0, extack); 647 case MLXSW_SP_ACL_MANGLE_FIELD_IP4_DIP: 648 return mlxsw_afa_block_append_ip(rulei->act_block, true, 649 true, val, 0, extack); 650 /* IPv6 fields */ 651 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_1: 652 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_3: 653 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_1: 654 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_3: 655 return mlxsw_sp2_acl_rulei_act_mangle_field_ip_odd(rulei, 656 mact->field, 657 val, extack); 658 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_2: 659 if (rulei->ipv6_valid && 660 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_1) { 661 rulei->ipv6_valid = false; 662 return mlxsw_afa_block_append_ip(rulei->act_block, 663 false, false, val, 664 rulei->ipv6.prev_val, 665 extack); 666 } 667 break; 668 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_4: 669 if (rulei->ipv6_valid && 670 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_SIP_3) { 671 rulei->ipv6_valid = false; 672 return mlxsw_afa_block_append_ip(rulei->act_block, 673 false, true, val, 674 rulei->ipv6.prev_val, 675 extack); 676 } 677 break; 678 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_2: 679 if (rulei->ipv6_valid && 680 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_1) { 681 rulei->ipv6_valid = false; 682 return mlxsw_afa_block_append_ip(rulei->act_block, 683 true, false, val, 684 rulei->ipv6.prev_val, 685 extack); 686 } 687 break; 688 case MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_4: 689 if (rulei->ipv6_valid && 690 rulei->ipv6.prev_field == MLXSW_SP_ACL_MANGLE_FIELD_IP6_DIP_3) { 691 rulei->ipv6_valid = false; 692 return mlxsw_afa_block_append_ip(rulei->act_block, 693 true, true, val, 694 rulei->ipv6.prev_val, 695 extack); 696 } 697 break; 698 default: 699 break; 700 } 701 702 NL_SET_ERR_MSG_MOD(extack, "Unsupported mangle field"); 703 return err; 704 } 705 706 int mlxsw_sp_acl_rulei_act_mangle(struct mlxsw_sp *mlxsw_sp, 707 struct mlxsw_sp_acl_rule_info *rulei, 708 enum flow_action_mangle_base htype, 709 u32 offset, u32 mask, u32 val, 710 struct netlink_ext_ack *extack) 711 { 712 const struct mlxsw_sp_acl_rulei_ops *acl_rulei_ops = mlxsw_sp->acl_rulei_ops; 713 struct mlxsw_sp_acl_mangle_action *mact; 714 size_t i; 715 716 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_acl_mangle_actions); ++i) { 717 mact = &mlxsw_sp_acl_mangle_actions[i]; 718 if (mact->htype == htype && 719 mact->offset == offset && 720 mact->mask == mask) { 721 val >>= mact->shift; 722 return acl_rulei_ops->act_mangle_field(mlxsw_sp, 723 rulei, mact, 724 val, extack); 725 } 726 } 727 728 NL_SET_ERR_MSG_MOD(extack, "Unknown mangle field"); 729 return -EINVAL; 730 } 731 732 int mlxsw_sp_acl_rulei_act_police(struct mlxsw_sp *mlxsw_sp, 733 struct mlxsw_sp_acl_rule_info *rulei, 734 u32 index, u64 rate_bytes_ps, 735 u32 burst, struct netlink_ext_ack *extack) 736 { 737 int err; 738 739 err = mlxsw_afa_block_append_police(rulei->act_block, index, 740 rate_bytes_ps, burst, 741 &rulei->policer_index, extack); 742 if (err) 743 return err; 744 745 rulei->policer_index_valid = true; 746 747 return 0; 748 } 749 750 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp, 751 struct mlxsw_sp_acl_rule_info *rulei, 752 struct netlink_ext_ack *extack) 753 { 754 int err; 755 756 err = mlxsw_afa_block_append_counter(rulei->act_block, 757 &rulei->counter_index, extack); 758 if (err) 759 return err; 760 rulei->counter_valid = true; 761 return 0; 762 } 763 764 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp, 765 struct mlxsw_sp_acl_rule_info *rulei, 766 u16 fid, struct netlink_ext_ack *extack) 767 { 768 return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack); 769 } 770 771 int mlxsw_sp_acl_rulei_act_sample(struct mlxsw_sp *mlxsw_sp, 772 struct mlxsw_sp_acl_rule_info *rulei, 773 struct mlxsw_sp_flow_block *block, 774 struct psample_group *psample_group, u32 rate, 775 u32 trunc_size, bool truncate, 776 struct netlink_ext_ack *extack) 777 { 778 struct mlxsw_sp_flow_block_binding *binding; 779 struct mlxsw_sp_port *mlxsw_sp_port; 780 781 if (!list_is_singular(&block->binding_list)) { 782 NL_SET_ERR_MSG_MOD(extack, "Only a single sampling source is allowed"); 783 return -EOPNOTSUPP; 784 } 785 binding = list_first_entry(&block->binding_list, 786 struct mlxsw_sp_flow_block_binding, list); 787 mlxsw_sp_port = binding->mlxsw_sp_port; 788 789 return mlxsw_afa_block_append_sampler(rulei->act_block, 790 mlxsw_sp_port->local_port, 791 psample_group, rate, trunc_size, 792 truncate, binding->ingress, 793 extack); 794 } 795 796 struct mlxsw_sp_acl_rule * 797 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp, 798 struct mlxsw_sp_acl_ruleset *ruleset, 799 unsigned long cookie, 800 struct mlxsw_afa_block *afa_block, 801 struct netlink_ext_ack *extack) 802 { 803 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 804 struct mlxsw_sp_acl_rule *rule; 805 int err; 806 807 mlxsw_sp_acl_ruleset_ref_inc(ruleset); 808 rule = kzalloc(sizeof(*rule) + ops->rule_priv_size, 809 GFP_KERNEL); 810 if (!rule) { 811 err = -ENOMEM; 812 goto err_alloc; 813 } 814 rule->cookie = cookie; 815 rule->ruleset = ruleset; 816 817 rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl, afa_block); 818 if (IS_ERR(rule->rulei)) { 819 err = PTR_ERR(rule->rulei); 820 goto err_rulei_create; 821 } 822 823 return rule; 824 825 err_rulei_create: 826 kfree(rule); 827 err_alloc: 828 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset); 829 return ERR_PTR(err); 830 } 831 832 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp, 833 struct mlxsw_sp_acl_rule *rule) 834 { 835 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset; 836 837 mlxsw_sp_acl_rulei_destroy(rule->rulei); 838 kfree(rule); 839 mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset); 840 } 841 842 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp, 843 struct mlxsw_sp_acl_rule *rule) 844 { 845 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset; 846 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 847 struct mlxsw_sp_flow_block *block = ruleset->ht_key.block; 848 int err; 849 850 err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei); 851 if (err) 852 return err; 853 854 err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node, 855 mlxsw_sp_acl_rule_ht_params); 856 if (err) 857 goto err_rhashtable_insert; 858 859 if (!ruleset->ht_key.chain_index && 860 mlxsw_sp_acl_ruleset_is_singular(ruleset)) { 861 /* We only need ruleset with chain index 0, the implicit 862 * one, to be directly bound to device. The rest of the 863 * rulesets are bound by "Goto action set". 864 */ 865 err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset, block); 866 if (err) 867 goto err_ruleset_block_bind; 868 } 869 870 mutex_lock(&mlxsw_sp->acl->rules_lock); 871 list_add_tail(&rule->list, &mlxsw_sp->acl->rules); 872 mutex_unlock(&mlxsw_sp->acl->rules_lock); 873 block->rule_count++; 874 block->ingress_blocker_rule_count += rule->rulei->ingress_bind_blocker; 875 block->egress_blocker_rule_count += rule->rulei->egress_bind_blocker; 876 return 0; 877 878 err_ruleset_block_bind: 879 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node, 880 mlxsw_sp_acl_rule_ht_params); 881 err_rhashtable_insert: 882 ops->rule_del(mlxsw_sp, rule->priv); 883 return err; 884 } 885 886 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp, 887 struct mlxsw_sp_acl_rule *rule) 888 { 889 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset; 890 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 891 struct mlxsw_sp_flow_block *block = ruleset->ht_key.block; 892 893 block->egress_blocker_rule_count -= rule->rulei->egress_bind_blocker; 894 block->ingress_blocker_rule_count -= rule->rulei->ingress_bind_blocker; 895 block->rule_count--; 896 mutex_lock(&mlxsw_sp->acl->rules_lock); 897 list_del(&rule->list); 898 mutex_unlock(&mlxsw_sp->acl->rules_lock); 899 if (!ruleset->ht_key.chain_index && 900 mlxsw_sp_acl_ruleset_is_singular(ruleset)) 901 mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset, block); 902 rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node, 903 mlxsw_sp_acl_rule_ht_params); 904 ops->rule_del(mlxsw_sp, rule->priv); 905 } 906 907 int mlxsw_sp_acl_rule_action_replace(struct mlxsw_sp *mlxsw_sp, 908 struct mlxsw_sp_acl_rule *rule, 909 struct mlxsw_afa_block *afa_block) 910 { 911 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset; 912 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 913 struct mlxsw_sp_acl_rule_info *rulei; 914 915 rulei = mlxsw_sp_acl_rule_rulei(rule); 916 rulei->act_block = afa_block; 917 918 return ops->rule_action_replace(mlxsw_sp, rule->priv, rule->rulei); 919 } 920 921 struct mlxsw_sp_acl_rule * 922 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp, 923 struct mlxsw_sp_acl_ruleset *ruleset, 924 unsigned long cookie) 925 { 926 return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie, 927 mlxsw_sp_acl_rule_ht_params); 928 } 929 930 struct mlxsw_sp_acl_rule_info * 931 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule) 932 { 933 return rule->rulei; 934 } 935 936 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp, 937 struct mlxsw_sp_acl_rule *rule) 938 { 939 struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset; 940 const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops; 941 bool active; 942 int err; 943 944 err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active); 945 if (err) 946 return err; 947 if (active) 948 rule->last_used = jiffies; 949 return 0; 950 } 951 952 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl) 953 { 954 struct mlxsw_sp_acl_rule *rule; 955 int err; 956 957 mutex_lock(&acl->rules_lock); 958 list_for_each_entry(rule, &acl->rules, list) { 959 err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp, 960 rule); 961 if (err) 962 goto err_rule_update; 963 } 964 mutex_unlock(&acl->rules_lock); 965 return 0; 966 967 err_rule_update: 968 mutex_unlock(&acl->rules_lock); 969 return err; 970 } 971 972 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl) 973 { 974 unsigned long interval = acl->rule_activity_update.interval; 975 976 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 977 msecs_to_jiffies(interval)); 978 } 979 980 static void mlxsw_sp_acl_rule_activity_update_work(struct work_struct *work) 981 { 982 struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl, 983 rule_activity_update.dw.work); 984 int err; 985 986 err = mlxsw_sp_acl_rules_activity_update(acl); 987 if (err) 988 dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity"); 989 990 mlxsw_sp_acl_rule_activity_work_schedule(acl); 991 } 992 993 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp, 994 struct mlxsw_sp_acl_rule *rule, 995 u64 *packets, u64 *bytes, u64 *drops, 996 u64 *last_use, 997 enum flow_action_hw_stats *used_hw_stats) 998 999 { 1000 enum mlxsw_sp_policer_type type = MLXSW_SP_POLICER_TYPE_SINGLE_RATE; 1001 struct mlxsw_sp_acl_rule_info *rulei; 1002 u64 current_packets = 0; 1003 u64 current_bytes = 0; 1004 u64 current_drops = 0; 1005 int err; 1006 1007 rulei = mlxsw_sp_acl_rule_rulei(rule); 1008 if (rulei->counter_valid) { 1009 err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index, 1010 ¤t_packets, 1011 ¤t_bytes); 1012 if (err) 1013 return err; 1014 *used_hw_stats = FLOW_ACTION_HW_STATS_IMMEDIATE; 1015 } 1016 if (rulei->policer_index_valid) { 1017 err = mlxsw_sp_policer_drops_counter_get(mlxsw_sp, type, 1018 rulei->policer_index, 1019 ¤t_drops); 1020 if (err) 1021 return err; 1022 } 1023 *packets = current_packets - rule->last_packets; 1024 *bytes = current_bytes - rule->last_bytes; 1025 *drops = current_drops - rule->last_drops; 1026 *last_use = rule->last_used; 1027 1028 rule->last_bytes = current_bytes; 1029 rule->last_packets = current_packets; 1030 rule->last_drops = current_drops; 1031 1032 return 0; 1033 } 1034 1035 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp) 1036 { 1037 struct mlxsw_sp_fid *fid; 1038 struct mlxsw_sp_acl *acl; 1039 size_t alloc_size; 1040 int err; 1041 1042 alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp); 1043 acl = kzalloc(alloc_size, GFP_KERNEL); 1044 if (!acl) 1045 return -ENOMEM; 1046 mlxsw_sp->acl = acl; 1047 acl->mlxsw_sp = mlxsw_sp; 1048 acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core, 1049 ACL_FLEX_KEYS), 1050 mlxsw_sp->afk_ops); 1051 if (!acl->afk) { 1052 err = -ENOMEM; 1053 goto err_afk_create; 1054 } 1055 1056 err = rhashtable_init(&acl->ruleset_ht, 1057 &mlxsw_sp_acl_ruleset_ht_params); 1058 if (err) 1059 goto err_rhashtable_init; 1060 1061 fid = mlxsw_sp_fid_dummy_get(mlxsw_sp); 1062 if (IS_ERR(fid)) { 1063 err = PTR_ERR(fid); 1064 goto err_fid_get; 1065 } 1066 acl->dummy_fid = fid; 1067 1068 INIT_LIST_HEAD(&acl->rules); 1069 mutex_init(&acl->rules_lock); 1070 err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam); 1071 if (err) 1072 goto err_acl_ops_init; 1073 1074 /* Create the delayed work for the rule activity_update */ 1075 INIT_DELAYED_WORK(&acl->rule_activity_update.dw, 1076 mlxsw_sp_acl_rule_activity_update_work); 1077 acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS; 1078 mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0); 1079 return 0; 1080 1081 err_acl_ops_init: 1082 mutex_destroy(&acl->rules_lock); 1083 mlxsw_sp_fid_put(fid); 1084 err_fid_get: 1085 rhashtable_destroy(&acl->ruleset_ht); 1086 err_rhashtable_init: 1087 mlxsw_afk_destroy(acl->afk); 1088 err_afk_create: 1089 kfree(acl); 1090 return err; 1091 } 1092 1093 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp) 1094 { 1095 struct mlxsw_sp_acl *acl = mlxsw_sp->acl; 1096 1097 cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw); 1098 mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam); 1099 mutex_destroy(&acl->rules_lock); 1100 WARN_ON(!list_empty(&acl->rules)); 1101 mlxsw_sp_fid_put(acl->dummy_fid); 1102 rhashtable_destroy(&acl->ruleset_ht); 1103 mlxsw_afk_destroy(acl->afk); 1104 kfree(acl); 1105 } 1106 1107 struct mlxsw_sp_acl_rulei_ops mlxsw_sp1_acl_rulei_ops = { 1108 .act_mangle_field = mlxsw_sp1_acl_rulei_act_mangle_field, 1109 }; 1110 1111 struct mlxsw_sp_acl_rulei_ops mlxsw_sp2_acl_rulei_ops = { 1112 .act_mangle_field = mlxsw_sp2_acl_rulei_act_mangle_field, 1113 }; 1114