1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/slab.h>
7 #include <linux/errno.h>
8 #include <linux/rhashtable.h>
9 #include <linux/list.h>
10 #include <linux/idr.h>
11 #include <linux/refcount.h>
12 #include <net/flow_offload.h>
16 #include "core_acl_flex_actions.h"
18 enum mlxsw_afa_set_type {
19 MLXSW_AFA_SET_TYPE_NEXT,
20 MLXSW_AFA_SET_TYPE_GOTO,
24 * Type of the record at the end of the action set.
26 MLXSW_ITEM32(afa, set, type, 0xA0, 28, 4);
28 /* afa_set_next_action_set_ptr
29 * A pointer to the next action set in the KVD Centralized database.
31 MLXSW_ITEM32(afa, set, next_action_set_ptr, 0xA4, 0, 24);
34 * group - When set, the binding is of an ACL group. When cleared,
35 * the binding is of an ACL.
36 * Must be set to 1 for Spectrum.
38 MLXSW_ITEM32(afa, set, goto_g, 0xA4, 29, 1);
40 enum mlxsw_afa_set_goto_binding_cmd {
41 /* continue go the next binding point */
42 MLXSW_AFA_SET_GOTO_BINDING_CMD_NONE,
43 /* jump to the next binding point no return */
44 MLXSW_AFA_SET_GOTO_BINDING_CMD_JUMP,
45 /* terminate the acl binding */
46 MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM = 4,
49 /* afa_set_goto_binding_cmd */
50 MLXSW_ITEM32(afa, set, goto_binding_cmd, 0xA4, 24, 3);
52 /* afa_set_goto_next_binding
53 * ACL/ACL group identifier. If the g bit is set, this field should hold
54 * the acl_group_id, else it should hold the acl_id.
56 MLXSW_ITEM32(afa, set, goto_next_binding, 0xA4, 0, 16);
58 /* afa_all_action_type
61 MLXSW_ITEM32(afa, all, action_type, 0x00, 24, 6);
64 unsigned int max_acts_per_set;
65 const struct mlxsw_afa_ops *ops;
67 struct rhashtable set_ht;
68 struct rhashtable fwd_entry_ht;
69 struct rhashtable cookie_ht;
70 struct rhashtable policer_ht;
71 struct idr cookie_idr;
72 struct list_head policer_list;
75 #define MLXSW_AFA_SET_LEN 0xA8
77 struct mlxsw_afa_set_ht_key {
78 char enc_actions[MLXSW_AFA_SET_LEN]; /* Encoded set */
82 /* Set structure holds one action set record. It contains up to three
83 * actions (depends on size of particular actions). The set is either
84 * put directly to a rule, or it is stored in KVD linear area.
85 * To prevent duplicate entries in KVD linear area, a hashtable is
86 * used to track sets that were previously inserted and may be shared.
89 struct mlxsw_afa_set {
90 struct rhash_head ht_node;
91 struct mlxsw_afa_set_ht_key ht_key;
93 u8 shared:1, /* Inserted in hashtable (doesn't mean that
94 * kvdl_index is valid).
99 struct mlxsw_afa_set *next; /* Pointer to the next set. */
100 struct mlxsw_afa_set *prev; /* Pointer to the previous set,
101 * note that set may have multiple
102 * sets from multiple blocks
103 * pointing at it. This is only
104 * usable until commit.
108 static const struct rhashtable_params mlxsw_afa_set_ht_params = {
109 .key_len = sizeof(struct mlxsw_afa_set_ht_key),
110 .key_offset = offsetof(struct mlxsw_afa_set, ht_key),
111 .head_offset = offsetof(struct mlxsw_afa_set, ht_node),
112 .automatic_shrinking = true,
115 struct mlxsw_afa_fwd_entry_ht_key {
119 struct mlxsw_afa_fwd_entry {
120 struct rhash_head ht_node;
121 struct mlxsw_afa_fwd_entry_ht_key ht_key;
123 refcount_t ref_count;
126 static const struct rhashtable_params mlxsw_afa_fwd_entry_ht_params = {
127 .key_len = sizeof(struct mlxsw_afa_fwd_entry_ht_key),
128 .key_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_key),
129 .head_offset = offsetof(struct mlxsw_afa_fwd_entry, ht_node),
130 .automatic_shrinking = true,
133 struct mlxsw_afa_cookie {
134 struct rhash_head ht_node;
135 refcount_t ref_count;
138 struct flow_action_cookie fa_cookie;
141 static u32 mlxsw_afa_cookie_hash(const struct flow_action_cookie *fa_cookie,
144 return jhash2((u32 *) fa_cookie->cookie,
145 fa_cookie->cookie_len / sizeof(u32), seed);
148 static u32 mlxsw_afa_cookie_key_hashfn(const void *data, u32 len, u32 seed)
150 const struct flow_action_cookie *fa_cookie = data;
152 return mlxsw_afa_cookie_hash(fa_cookie, seed);
155 static u32 mlxsw_afa_cookie_obj_hashfn(const void *data, u32 len, u32 seed)
157 const struct mlxsw_afa_cookie *cookie = data;
159 return mlxsw_afa_cookie_hash(&cookie->fa_cookie, seed);
162 static int mlxsw_afa_cookie_obj_cmpfn(struct rhashtable_compare_arg *arg,
165 const struct flow_action_cookie *fa_cookie = arg->key;
166 const struct mlxsw_afa_cookie *cookie = obj;
168 if (cookie->fa_cookie.cookie_len == fa_cookie->cookie_len)
169 return memcmp(cookie->fa_cookie.cookie, fa_cookie->cookie,
170 fa_cookie->cookie_len);
174 static const struct rhashtable_params mlxsw_afa_cookie_ht_params = {
175 .head_offset = offsetof(struct mlxsw_afa_cookie, ht_node),
176 .hashfn = mlxsw_afa_cookie_key_hashfn,
177 .obj_hashfn = mlxsw_afa_cookie_obj_hashfn,
178 .obj_cmpfn = mlxsw_afa_cookie_obj_cmpfn,
179 .automatic_shrinking = true,
182 struct mlxsw_afa_policer {
183 struct rhash_head ht_node;
184 struct list_head list; /* Member of policer_list */
185 refcount_t ref_count;
190 static const struct rhashtable_params mlxsw_afa_policer_ht_params = {
191 .key_len = sizeof(u32),
192 .key_offset = offsetof(struct mlxsw_afa_policer, fa_index),
193 .head_offset = offsetof(struct mlxsw_afa_policer, ht_node),
194 .automatic_shrinking = true,
197 struct mlxsw_afa *mlxsw_afa_create(unsigned int max_acts_per_set,
198 const struct mlxsw_afa_ops *ops,
201 struct mlxsw_afa *mlxsw_afa;
204 mlxsw_afa = kzalloc(sizeof(*mlxsw_afa), GFP_KERNEL);
206 return ERR_PTR(-ENOMEM);
207 err = rhashtable_init(&mlxsw_afa->set_ht, &mlxsw_afa_set_ht_params);
209 goto err_set_rhashtable_init;
210 err = rhashtable_init(&mlxsw_afa->fwd_entry_ht,
211 &mlxsw_afa_fwd_entry_ht_params);
213 goto err_fwd_entry_rhashtable_init;
214 err = rhashtable_init(&mlxsw_afa->cookie_ht,
215 &mlxsw_afa_cookie_ht_params);
217 goto err_cookie_rhashtable_init;
218 err = rhashtable_init(&mlxsw_afa->policer_ht,
219 &mlxsw_afa_policer_ht_params);
221 goto err_policer_rhashtable_init;
222 idr_init(&mlxsw_afa->cookie_idr);
223 INIT_LIST_HEAD(&mlxsw_afa->policer_list);
224 mlxsw_afa->max_acts_per_set = max_acts_per_set;
225 mlxsw_afa->ops = ops;
226 mlxsw_afa->ops_priv = ops_priv;
229 err_policer_rhashtable_init:
230 rhashtable_destroy(&mlxsw_afa->cookie_ht);
231 err_cookie_rhashtable_init:
232 rhashtable_destroy(&mlxsw_afa->fwd_entry_ht);
233 err_fwd_entry_rhashtable_init:
234 rhashtable_destroy(&mlxsw_afa->set_ht);
235 err_set_rhashtable_init:
239 EXPORT_SYMBOL(mlxsw_afa_create);
241 void mlxsw_afa_destroy(struct mlxsw_afa *mlxsw_afa)
243 WARN_ON(!list_empty(&mlxsw_afa->policer_list));
244 WARN_ON(!idr_is_empty(&mlxsw_afa->cookie_idr));
245 idr_destroy(&mlxsw_afa->cookie_idr);
246 rhashtable_destroy(&mlxsw_afa->policer_ht);
247 rhashtable_destroy(&mlxsw_afa->cookie_ht);
248 rhashtable_destroy(&mlxsw_afa->fwd_entry_ht);
249 rhashtable_destroy(&mlxsw_afa->set_ht);
252 EXPORT_SYMBOL(mlxsw_afa_destroy);
254 static void mlxsw_afa_set_goto_set(struct mlxsw_afa_set *set,
255 enum mlxsw_afa_set_goto_binding_cmd cmd,
258 char *actions = set->ht_key.enc_actions;
260 mlxsw_afa_set_type_set(actions, MLXSW_AFA_SET_TYPE_GOTO);
261 mlxsw_afa_set_goto_g_set(actions, true);
262 mlxsw_afa_set_goto_binding_cmd_set(actions, cmd);
263 mlxsw_afa_set_goto_next_binding_set(actions, group_id);
266 static void mlxsw_afa_set_next_set(struct mlxsw_afa_set *set,
267 u32 next_set_kvdl_index)
269 char *actions = set->ht_key.enc_actions;
271 mlxsw_afa_set_type_set(actions, MLXSW_AFA_SET_TYPE_NEXT);
272 mlxsw_afa_set_next_action_set_ptr_set(actions, next_set_kvdl_index);
275 static struct mlxsw_afa_set *mlxsw_afa_set_create(bool is_first)
277 struct mlxsw_afa_set *set;
279 set = kzalloc(sizeof(*set), GFP_KERNEL);
282 /* Need to initialize the set to pass by default */
283 mlxsw_afa_set_goto_set(set, MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM, 0);
284 set->ht_key.is_first = is_first;
285 refcount_set(&set->ref_count, 1);
289 static void mlxsw_afa_set_destroy(struct mlxsw_afa_set *set)
294 static int mlxsw_afa_set_share(struct mlxsw_afa *mlxsw_afa,
295 struct mlxsw_afa_set *set)
299 err = rhashtable_insert_fast(&mlxsw_afa->set_ht, &set->ht_node,
300 mlxsw_afa_set_ht_params);
303 err = mlxsw_afa->ops->kvdl_set_add(mlxsw_afa->ops_priv,
305 set->ht_key.enc_actions,
306 set->ht_key.is_first);
308 goto err_kvdl_set_add;
314 rhashtable_remove_fast(&mlxsw_afa->set_ht, &set->ht_node,
315 mlxsw_afa_set_ht_params);
319 static void mlxsw_afa_set_unshare(struct mlxsw_afa *mlxsw_afa,
320 struct mlxsw_afa_set *set)
322 mlxsw_afa->ops->kvdl_set_del(mlxsw_afa->ops_priv,
324 set->ht_key.is_first);
325 rhashtable_remove_fast(&mlxsw_afa->set_ht, &set->ht_node,
326 mlxsw_afa_set_ht_params);
330 static void mlxsw_afa_set_put(struct mlxsw_afa *mlxsw_afa,
331 struct mlxsw_afa_set *set)
333 if (!refcount_dec_and_test(&set->ref_count))
336 mlxsw_afa_set_unshare(mlxsw_afa, set);
337 mlxsw_afa_set_destroy(set);
340 static struct mlxsw_afa_set *mlxsw_afa_set_get(struct mlxsw_afa *mlxsw_afa,
341 struct mlxsw_afa_set *orig_set)
343 struct mlxsw_afa_set *set;
346 /* There is a hashtable of sets maintained. If a set with the exact
347 * same encoding exists, we reuse it. Otherwise, the current set
348 * is shared by making it available to others using the hash table.
350 set = rhashtable_lookup_fast(&mlxsw_afa->set_ht, &orig_set->ht_key,
351 mlxsw_afa_set_ht_params);
353 refcount_inc(&set->ref_count);
354 mlxsw_afa_set_put(mlxsw_afa, orig_set);
357 err = mlxsw_afa_set_share(mlxsw_afa, set);
364 /* Block structure holds a list of action sets. One action block
365 * represents one chain of actions executed upon match of a rule.
368 struct mlxsw_afa_block {
369 struct mlxsw_afa *afa;
371 struct mlxsw_afa_set *first_set;
372 struct mlxsw_afa_set *cur_set;
373 unsigned int cur_act_index; /* In current set. */
374 struct list_head resource_list; /* List of resources held by actions
379 struct mlxsw_afa_resource {
380 struct list_head list;
381 void (*destructor)(struct mlxsw_afa_block *block,
382 struct mlxsw_afa_resource *resource);
385 static void mlxsw_afa_resource_add(struct mlxsw_afa_block *block,
386 struct mlxsw_afa_resource *resource)
388 list_add(&resource->list, &block->resource_list);
391 static void mlxsw_afa_resource_del(struct mlxsw_afa_resource *resource)
393 list_del(&resource->list);
396 static void mlxsw_afa_resources_destroy(struct mlxsw_afa_block *block)
398 struct mlxsw_afa_resource *resource, *tmp;
400 list_for_each_entry_safe(resource, tmp, &block->resource_list, list) {
401 resource->destructor(block, resource);
405 struct mlxsw_afa_block *mlxsw_afa_block_create(struct mlxsw_afa *mlxsw_afa)
407 struct mlxsw_afa_block *block;
409 block = kzalloc(sizeof(*block), GFP_KERNEL);
411 return ERR_PTR(-ENOMEM);
412 INIT_LIST_HEAD(&block->resource_list);
413 block->afa = mlxsw_afa;
415 /* At least one action set is always present, so just create it here */
416 block->first_set = mlxsw_afa_set_create(true);
417 if (!block->first_set)
418 goto err_first_set_create;
420 /* In case user instructs to have dummy first set, we leave it
421 * empty here and create another, real, set right away.
423 if (mlxsw_afa->ops->dummy_first_set) {
424 block->cur_set = mlxsw_afa_set_create(false);
426 goto err_second_set_create;
427 block->cur_set->prev = block->first_set;
428 block->first_set->next = block->cur_set;
430 block->cur_set = block->first_set;
435 err_second_set_create:
436 mlxsw_afa_set_destroy(block->first_set);
437 err_first_set_create:
439 return ERR_PTR(-ENOMEM);
441 EXPORT_SYMBOL(mlxsw_afa_block_create);
443 void mlxsw_afa_block_destroy(struct mlxsw_afa_block *block)
445 struct mlxsw_afa_set *set = block->first_set;
446 struct mlxsw_afa_set *next_set;
449 next_set = set->next;
450 mlxsw_afa_set_put(block->afa, set);
453 mlxsw_afa_resources_destroy(block);
456 EXPORT_SYMBOL(mlxsw_afa_block_destroy);
458 int mlxsw_afa_block_commit(struct mlxsw_afa_block *block)
460 struct mlxsw_afa_set *set = block->cur_set;
461 struct mlxsw_afa_set *prev_set;
463 block->cur_set = NULL;
464 block->finished = true;
466 /* Go over all linked sets starting from last
467 * and try to find existing set in the hash table.
468 * In case it is not there, assign a KVD linear index
472 prev_set = set->prev;
473 set = mlxsw_afa_set_get(block->afa, set);
475 /* No rollback is needed since the chain is
476 * in consistent state and mlxsw_afa_block_destroy
477 * will take care of putting it away.
481 prev_set->next = set;
482 mlxsw_afa_set_next_set(prev_set, set->kvdl_index);
487 block->first_set = set;
490 EXPORT_SYMBOL(mlxsw_afa_block_commit);
492 char *mlxsw_afa_block_first_set(struct mlxsw_afa_block *block)
494 return block->first_set->ht_key.enc_actions;
496 EXPORT_SYMBOL(mlxsw_afa_block_first_set);
498 char *mlxsw_afa_block_cur_set(struct mlxsw_afa_block *block)
500 return block->cur_set->ht_key.enc_actions;
502 EXPORT_SYMBOL(mlxsw_afa_block_cur_set);
504 u32 mlxsw_afa_block_first_kvdl_index(struct mlxsw_afa_block *block)
506 /* First set is never in KVD linear. So the first set
507 * with valid KVD linear index is always the second one.
509 if (WARN_ON(!block->first_set->next))
511 return block->first_set->next->kvdl_index;
513 EXPORT_SYMBOL(mlxsw_afa_block_first_kvdl_index);
515 int mlxsw_afa_block_activity_get(struct mlxsw_afa_block *block, bool *activity)
517 u32 kvdl_index = mlxsw_afa_block_first_kvdl_index(block);
519 return block->afa->ops->kvdl_set_activity_get(block->afa->ops_priv,
520 kvdl_index, activity);
522 EXPORT_SYMBOL(mlxsw_afa_block_activity_get);
524 int mlxsw_afa_block_continue(struct mlxsw_afa_block *block)
528 mlxsw_afa_set_goto_set(block->cur_set,
529 MLXSW_AFA_SET_GOTO_BINDING_CMD_NONE, 0);
530 block->finished = true;
533 EXPORT_SYMBOL(mlxsw_afa_block_continue);
535 int mlxsw_afa_block_jump(struct mlxsw_afa_block *block, u16 group_id)
539 mlxsw_afa_set_goto_set(block->cur_set,
540 MLXSW_AFA_SET_GOTO_BINDING_CMD_JUMP, group_id);
541 block->finished = true;
544 EXPORT_SYMBOL(mlxsw_afa_block_jump);
546 int mlxsw_afa_block_terminate(struct mlxsw_afa_block *block)
550 mlxsw_afa_set_goto_set(block->cur_set,
551 MLXSW_AFA_SET_GOTO_BINDING_CMD_TERM, 0);
552 block->finished = true;
555 EXPORT_SYMBOL(mlxsw_afa_block_terminate);
557 static struct mlxsw_afa_fwd_entry *
558 mlxsw_afa_fwd_entry_create(struct mlxsw_afa *mlxsw_afa, u16 local_port)
560 struct mlxsw_afa_fwd_entry *fwd_entry;
563 fwd_entry = kzalloc(sizeof(*fwd_entry), GFP_KERNEL);
565 return ERR_PTR(-ENOMEM);
566 fwd_entry->ht_key.local_port = local_port;
567 refcount_set(&fwd_entry->ref_count, 1);
569 err = rhashtable_insert_fast(&mlxsw_afa->fwd_entry_ht,
571 mlxsw_afa_fwd_entry_ht_params);
573 goto err_rhashtable_insert;
575 err = mlxsw_afa->ops->kvdl_fwd_entry_add(mlxsw_afa->ops_priv,
576 &fwd_entry->kvdl_index,
579 goto err_kvdl_fwd_entry_add;
582 err_kvdl_fwd_entry_add:
583 rhashtable_remove_fast(&mlxsw_afa->fwd_entry_ht, &fwd_entry->ht_node,
584 mlxsw_afa_fwd_entry_ht_params);
585 err_rhashtable_insert:
590 static void mlxsw_afa_fwd_entry_destroy(struct mlxsw_afa *mlxsw_afa,
591 struct mlxsw_afa_fwd_entry *fwd_entry)
593 mlxsw_afa->ops->kvdl_fwd_entry_del(mlxsw_afa->ops_priv,
594 fwd_entry->kvdl_index);
595 rhashtable_remove_fast(&mlxsw_afa->fwd_entry_ht, &fwd_entry->ht_node,
596 mlxsw_afa_fwd_entry_ht_params);
600 static struct mlxsw_afa_fwd_entry *
601 mlxsw_afa_fwd_entry_get(struct mlxsw_afa *mlxsw_afa, u16 local_port)
603 struct mlxsw_afa_fwd_entry_ht_key ht_key = {0};
604 struct mlxsw_afa_fwd_entry *fwd_entry;
606 ht_key.local_port = local_port;
607 fwd_entry = rhashtable_lookup_fast(&mlxsw_afa->fwd_entry_ht, &ht_key,
608 mlxsw_afa_fwd_entry_ht_params);
610 refcount_inc(&fwd_entry->ref_count);
613 return mlxsw_afa_fwd_entry_create(mlxsw_afa, local_port);
616 static void mlxsw_afa_fwd_entry_put(struct mlxsw_afa *mlxsw_afa,
617 struct mlxsw_afa_fwd_entry *fwd_entry)
619 if (!refcount_dec_and_test(&fwd_entry->ref_count))
621 mlxsw_afa_fwd_entry_destroy(mlxsw_afa, fwd_entry);
624 struct mlxsw_afa_fwd_entry_ref {
625 struct mlxsw_afa_resource resource;
626 struct mlxsw_afa_fwd_entry *fwd_entry;
630 mlxsw_afa_fwd_entry_ref_destroy(struct mlxsw_afa_block *block,
631 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref)
633 mlxsw_afa_resource_del(&fwd_entry_ref->resource);
634 mlxsw_afa_fwd_entry_put(block->afa, fwd_entry_ref->fwd_entry);
635 kfree(fwd_entry_ref);
639 mlxsw_afa_fwd_entry_ref_destructor(struct mlxsw_afa_block *block,
640 struct mlxsw_afa_resource *resource)
642 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
644 fwd_entry_ref = container_of(resource, struct mlxsw_afa_fwd_entry_ref,
646 mlxsw_afa_fwd_entry_ref_destroy(block, fwd_entry_ref);
649 static struct mlxsw_afa_fwd_entry_ref *
650 mlxsw_afa_fwd_entry_ref_create(struct mlxsw_afa_block *block, u16 local_port)
652 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
653 struct mlxsw_afa_fwd_entry *fwd_entry;
656 fwd_entry_ref = kzalloc(sizeof(*fwd_entry_ref), GFP_KERNEL);
658 return ERR_PTR(-ENOMEM);
659 fwd_entry = mlxsw_afa_fwd_entry_get(block->afa, local_port);
660 if (IS_ERR(fwd_entry)) {
661 err = PTR_ERR(fwd_entry);
662 goto err_fwd_entry_get;
664 fwd_entry_ref->fwd_entry = fwd_entry;
665 fwd_entry_ref->resource.destructor = mlxsw_afa_fwd_entry_ref_destructor;
666 mlxsw_afa_resource_add(block, &fwd_entry_ref->resource);
667 return fwd_entry_ref;
670 kfree(fwd_entry_ref);
674 struct mlxsw_afa_counter {
675 struct mlxsw_afa_resource resource;
680 mlxsw_afa_counter_destroy(struct mlxsw_afa_block *block,
681 struct mlxsw_afa_counter *counter)
683 mlxsw_afa_resource_del(&counter->resource);
684 block->afa->ops->counter_index_put(block->afa->ops_priv,
685 counter->counter_index);
690 mlxsw_afa_counter_destructor(struct mlxsw_afa_block *block,
691 struct mlxsw_afa_resource *resource)
693 struct mlxsw_afa_counter *counter;
695 counter = container_of(resource, struct mlxsw_afa_counter, resource);
696 mlxsw_afa_counter_destroy(block, counter);
699 static struct mlxsw_afa_counter *
700 mlxsw_afa_counter_create(struct mlxsw_afa_block *block)
702 struct mlxsw_afa_counter *counter;
705 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
707 return ERR_PTR(-ENOMEM);
709 err = block->afa->ops->counter_index_get(block->afa->ops_priv,
710 &counter->counter_index);
712 goto err_counter_index_get;
713 counter->resource.destructor = mlxsw_afa_counter_destructor;
714 mlxsw_afa_resource_add(block, &counter->resource);
717 err_counter_index_get:
722 /* 20 bits is a maximum that hardware can handle in trap with userdef action
723 * and carry along with the trapped packet.
725 #define MLXSW_AFA_COOKIE_INDEX_BITS 20
726 #define MLXSW_AFA_COOKIE_INDEX_MAX ((1 << MLXSW_AFA_COOKIE_INDEX_BITS) - 1)
728 static struct mlxsw_afa_cookie *
729 mlxsw_afa_cookie_create(struct mlxsw_afa *mlxsw_afa,
730 const struct flow_action_cookie *fa_cookie)
732 struct mlxsw_afa_cookie *cookie;
736 cookie = kzalloc(sizeof(*cookie) + fa_cookie->cookie_len, GFP_KERNEL);
738 return ERR_PTR(-ENOMEM);
739 refcount_set(&cookie->ref_count, 1);
740 cookie->fa_cookie = *fa_cookie;
741 memcpy(cookie->fa_cookie.cookie, fa_cookie->cookie,
742 fa_cookie->cookie_len);
744 err = rhashtable_insert_fast(&mlxsw_afa->cookie_ht, &cookie->ht_node,
745 mlxsw_afa_cookie_ht_params);
747 goto err_rhashtable_insert;
749 /* Start cookie indexes with 1. Leave the 0 index unused. Packets
750 * that come from the HW which are not dropped by drop-with-cookie
751 * action are going to pass cookie_index 0 to lookup.
754 err = idr_alloc_u32(&mlxsw_afa->cookie_idr, cookie, &cookie_index,
755 MLXSW_AFA_COOKIE_INDEX_MAX, GFP_KERNEL);
758 cookie->cookie_index = cookie_index;
762 rhashtable_remove_fast(&mlxsw_afa->cookie_ht, &cookie->ht_node,
763 mlxsw_afa_cookie_ht_params);
764 err_rhashtable_insert:
769 static void mlxsw_afa_cookie_destroy(struct mlxsw_afa *mlxsw_afa,
770 struct mlxsw_afa_cookie *cookie)
772 idr_remove(&mlxsw_afa->cookie_idr, cookie->cookie_index);
773 rhashtable_remove_fast(&mlxsw_afa->cookie_ht, &cookie->ht_node,
774 mlxsw_afa_cookie_ht_params);
775 kfree_rcu(cookie, rcu);
778 static struct mlxsw_afa_cookie *
779 mlxsw_afa_cookie_get(struct mlxsw_afa *mlxsw_afa,
780 const struct flow_action_cookie *fa_cookie)
782 struct mlxsw_afa_cookie *cookie;
784 cookie = rhashtable_lookup_fast(&mlxsw_afa->cookie_ht, fa_cookie,
785 mlxsw_afa_cookie_ht_params);
787 refcount_inc(&cookie->ref_count);
790 return mlxsw_afa_cookie_create(mlxsw_afa, fa_cookie);
793 static void mlxsw_afa_cookie_put(struct mlxsw_afa *mlxsw_afa,
794 struct mlxsw_afa_cookie *cookie)
796 if (!refcount_dec_and_test(&cookie->ref_count))
798 mlxsw_afa_cookie_destroy(mlxsw_afa, cookie);
801 /* RCU read lock must be held */
802 const struct flow_action_cookie *
803 mlxsw_afa_cookie_lookup(struct mlxsw_afa *mlxsw_afa, u32 cookie_index)
805 struct mlxsw_afa_cookie *cookie;
807 /* 0 index means no cookie */
810 cookie = idr_find(&mlxsw_afa->cookie_idr, cookie_index);
813 return &cookie->fa_cookie;
815 EXPORT_SYMBOL(mlxsw_afa_cookie_lookup);
817 struct mlxsw_afa_cookie_ref {
818 struct mlxsw_afa_resource resource;
819 struct mlxsw_afa_cookie *cookie;
823 mlxsw_afa_cookie_ref_destroy(struct mlxsw_afa_block *block,
824 struct mlxsw_afa_cookie_ref *cookie_ref)
826 mlxsw_afa_resource_del(&cookie_ref->resource);
827 mlxsw_afa_cookie_put(block->afa, cookie_ref->cookie);
832 mlxsw_afa_cookie_ref_destructor(struct mlxsw_afa_block *block,
833 struct mlxsw_afa_resource *resource)
835 struct mlxsw_afa_cookie_ref *cookie_ref;
837 cookie_ref = container_of(resource, struct mlxsw_afa_cookie_ref,
839 mlxsw_afa_cookie_ref_destroy(block, cookie_ref);
842 static struct mlxsw_afa_cookie_ref *
843 mlxsw_afa_cookie_ref_create(struct mlxsw_afa_block *block,
844 const struct flow_action_cookie *fa_cookie)
846 struct mlxsw_afa_cookie_ref *cookie_ref;
847 struct mlxsw_afa_cookie *cookie;
850 cookie_ref = kzalloc(sizeof(*cookie_ref), GFP_KERNEL);
852 return ERR_PTR(-ENOMEM);
853 cookie = mlxsw_afa_cookie_get(block->afa, fa_cookie);
854 if (IS_ERR(cookie)) {
855 err = PTR_ERR(cookie);
858 cookie_ref->cookie = cookie;
859 cookie_ref->resource.destructor = mlxsw_afa_cookie_ref_destructor;
860 mlxsw_afa_resource_add(block, &cookie_ref->resource);
868 static struct mlxsw_afa_policer *
869 mlxsw_afa_policer_create(struct mlxsw_afa *mlxsw_afa, u32 fa_index,
870 u64 rate_bytes_ps, u32 burst,
871 struct netlink_ext_ack *extack)
873 struct mlxsw_afa_policer *policer;
876 policer = kzalloc(sizeof(*policer), GFP_KERNEL);
878 return ERR_PTR(-ENOMEM);
880 err = mlxsw_afa->ops->policer_add(mlxsw_afa->ops_priv, rate_bytes_ps,
881 burst, &policer->policer_index,
884 goto err_policer_add;
886 refcount_set(&policer->ref_count, 1);
887 policer->fa_index = fa_index;
889 err = rhashtable_insert_fast(&mlxsw_afa->policer_ht, &policer->ht_node,
890 mlxsw_afa_policer_ht_params);
892 goto err_rhashtable_insert;
894 list_add_tail(&policer->list, &mlxsw_afa->policer_list);
898 err_rhashtable_insert:
899 mlxsw_afa->ops->policer_del(mlxsw_afa->ops_priv,
900 policer->policer_index);
906 static void mlxsw_afa_policer_destroy(struct mlxsw_afa *mlxsw_afa,
907 struct mlxsw_afa_policer *policer)
909 list_del(&policer->list);
910 rhashtable_remove_fast(&mlxsw_afa->policer_ht, &policer->ht_node,
911 mlxsw_afa_policer_ht_params);
912 mlxsw_afa->ops->policer_del(mlxsw_afa->ops_priv,
913 policer->policer_index);
917 static struct mlxsw_afa_policer *
918 mlxsw_afa_policer_get(struct mlxsw_afa *mlxsw_afa, u32 fa_index,
919 u64 rate_bytes_ps, u32 burst,
920 struct netlink_ext_ack *extack)
922 struct mlxsw_afa_policer *policer;
924 policer = rhashtable_lookup_fast(&mlxsw_afa->policer_ht, &fa_index,
925 mlxsw_afa_policer_ht_params);
927 refcount_inc(&policer->ref_count);
931 return mlxsw_afa_policer_create(mlxsw_afa, fa_index, rate_bytes_ps,
935 static void mlxsw_afa_policer_put(struct mlxsw_afa *mlxsw_afa,
936 struct mlxsw_afa_policer *policer)
938 if (!refcount_dec_and_test(&policer->ref_count))
940 mlxsw_afa_policer_destroy(mlxsw_afa, policer);
943 struct mlxsw_afa_policer_ref {
944 struct mlxsw_afa_resource resource;
945 struct mlxsw_afa_policer *policer;
949 mlxsw_afa_policer_ref_destroy(struct mlxsw_afa_block *block,
950 struct mlxsw_afa_policer_ref *policer_ref)
952 mlxsw_afa_resource_del(&policer_ref->resource);
953 mlxsw_afa_policer_put(block->afa, policer_ref->policer);
958 mlxsw_afa_policer_ref_destructor(struct mlxsw_afa_block *block,
959 struct mlxsw_afa_resource *resource)
961 struct mlxsw_afa_policer_ref *policer_ref;
963 policer_ref = container_of(resource, struct mlxsw_afa_policer_ref,
965 mlxsw_afa_policer_ref_destroy(block, policer_ref);
968 static struct mlxsw_afa_policer_ref *
969 mlxsw_afa_policer_ref_create(struct mlxsw_afa_block *block, u32 fa_index,
970 u64 rate_bytes_ps, u32 burst,
971 struct netlink_ext_ack *extack)
973 struct mlxsw_afa_policer_ref *policer_ref;
974 struct mlxsw_afa_policer *policer;
977 policer_ref = kzalloc(sizeof(*policer_ref), GFP_KERNEL);
979 return ERR_PTR(-ENOMEM);
981 policer = mlxsw_afa_policer_get(block->afa, fa_index, rate_bytes_ps,
983 if (IS_ERR(policer)) {
984 err = PTR_ERR(policer);
985 goto err_policer_get;
988 policer_ref->policer = policer;
989 policer_ref->resource.destructor = mlxsw_afa_policer_ref_destructor;
990 mlxsw_afa_resource_add(block, &policer_ref->resource);
999 #define MLXSW_AFA_ONE_ACTION_LEN 32
1000 #define MLXSW_AFA_PAYLOAD_OFFSET 4
1002 enum mlxsw_afa_action_type {
1003 MLXSW_AFA_ACTION_TYPE_TRAP,
1004 MLXSW_AFA_ACTION_TYPE_POLICE,
1005 MLXSW_AFA_ACTION_TYPE_OTHER,
1009 mlxsw_afa_block_need_split(const struct mlxsw_afa_block *block,
1010 enum mlxsw_afa_action_type type)
1012 struct mlxsw_afa_set *cur_set = block->cur_set;
1014 /* Due to a hardware limitation, police action cannot be in the same
1015 * action set with MLXSW_AFA_TRAP_CODE or MLXSW_AFA_TRAPWU_CODE
1016 * actions. Work around this limitation by creating a new action set
1017 * and place the new action there.
1019 return (cur_set->has_trap && type == MLXSW_AFA_ACTION_TYPE_POLICE) ||
1020 (cur_set->has_police && type == MLXSW_AFA_ACTION_TYPE_TRAP);
1023 static char *mlxsw_afa_block_append_action_ext(struct mlxsw_afa_block *block,
1024 u8 action_code, u8 action_size,
1025 enum mlxsw_afa_action_type type)
1030 if (block->finished)
1031 return ERR_PTR(-EINVAL);
1032 if (block->cur_act_index + action_size > block->afa->max_acts_per_set ||
1033 mlxsw_afa_block_need_split(block, type)) {
1034 struct mlxsw_afa_set *set;
1036 /* The appended action won't fit into the current action set,
1037 * so create a new set.
1039 set = mlxsw_afa_set_create(false);
1041 return ERR_PTR(-ENOBUFS);
1042 set->prev = block->cur_set;
1043 block->cur_act_index = 0;
1044 block->cur_set->next = set;
1045 block->cur_set = set;
1049 case MLXSW_AFA_ACTION_TYPE_TRAP:
1050 block->cur_set->has_trap = true;
1052 case MLXSW_AFA_ACTION_TYPE_POLICE:
1053 block->cur_set->has_police = true;
1059 actions = block->cur_set->ht_key.enc_actions;
1060 oneact = actions + block->cur_act_index * MLXSW_AFA_ONE_ACTION_LEN;
1061 block->cur_act_index += action_size;
1062 mlxsw_afa_all_action_type_set(oneact, action_code);
1063 return oneact + MLXSW_AFA_PAYLOAD_OFFSET;
1066 static char *mlxsw_afa_block_append_action(struct mlxsw_afa_block *block,
1067 u8 action_code, u8 action_size)
1069 return mlxsw_afa_block_append_action_ext(block, action_code,
1071 MLXSW_AFA_ACTION_TYPE_OTHER);
1076 * VLAN action is used for manipulating VLANs. It can be used to implement QinQ,
1077 * VLAN translation, change of PCP bits of the VLAN tag, push, pop as swap VLANs
1081 #define MLXSW_AFA_VLAN_CODE 0x02
1082 #define MLXSW_AFA_VLAN_SIZE 1
1084 enum mlxsw_afa_vlan_vlan_tag_cmd {
1085 MLXSW_AFA_VLAN_VLAN_TAG_CMD_NOP,
1086 MLXSW_AFA_VLAN_VLAN_TAG_CMD_PUSH_TAG,
1087 MLXSW_AFA_VLAN_VLAN_TAG_CMD_POP_TAG,
1090 enum mlxsw_afa_vlan_cmd {
1091 MLXSW_AFA_VLAN_CMD_NOP,
1092 MLXSW_AFA_VLAN_CMD_SET_OUTER,
1093 MLXSW_AFA_VLAN_CMD_SET_INNER,
1094 MLXSW_AFA_VLAN_CMD_COPY_OUTER_TO_INNER,
1095 MLXSW_AFA_VLAN_CMD_COPY_INNER_TO_OUTER,
1096 MLXSW_AFA_VLAN_CMD_SWAP,
1099 /* afa_vlan_vlan_tag_cmd
1100 * Tag command: push, pop, nop VLAN header.
1102 MLXSW_ITEM32(afa, vlan, vlan_tag_cmd, 0x00, 29, 3);
1104 /* afa_vlan_vid_cmd */
1105 MLXSW_ITEM32(afa, vlan, vid_cmd, 0x04, 29, 3);
1108 MLXSW_ITEM32(afa, vlan, vid, 0x04, 0, 12);
1110 /* afa_vlan_ethertype_cmd */
1111 MLXSW_ITEM32(afa, vlan, ethertype_cmd, 0x08, 29, 3);
1113 /* afa_vlan_ethertype
1114 * Index to EtherTypes in Switch VLAN EtherType Register (SVER).
1116 MLXSW_ITEM32(afa, vlan, ethertype, 0x08, 24, 3);
1118 /* afa_vlan_pcp_cmd */
1119 MLXSW_ITEM32(afa, vlan, pcp_cmd, 0x08, 13, 3);
1122 MLXSW_ITEM32(afa, vlan, pcp, 0x08, 8, 3);
1125 mlxsw_afa_vlan_pack(char *payload,
1126 enum mlxsw_afa_vlan_vlan_tag_cmd vlan_tag_cmd,
1127 enum mlxsw_afa_vlan_cmd vid_cmd, u16 vid,
1128 enum mlxsw_afa_vlan_cmd pcp_cmd, u8 pcp,
1129 enum mlxsw_afa_vlan_cmd ethertype_cmd, u8 ethertype)
1131 mlxsw_afa_vlan_vlan_tag_cmd_set(payload, vlan_tag_cmd);
1132 mlxsw_afa_vlan_vid_cmd_set(payload, vid_cmd);
1133 mlxsw_afa_vlan_vid_set(payload, vid);
1134 mlxsw_afa_vlan_pcp_cmd_set(payload, pcp_cmd);
1135 mlxsw_afa_vlan_pcp_set(payload, pcp);
1136 mlxsw_afa_vlan_ethertype_cmd_set(payload, ethertype_cmd);
1137 mlxsw_afa_vlan_ethertype_set(payload, ethertype);
1140 int mlxsw_afa_block_append_vlan_modify(struct mlxsw_afa_block *block,
1141 u16 vid, u8 pcp, u8 et,
1142 struct netlink_ext_ack *extack)
1144 char *act = mlxsw_afa_block_append_action(block,
1145 MLXSW_AFA_VLAN_CODE,
1146 MLXSW_AFA_VLAN_SIZE);
1149 NL_SET_ERR_MSG_MOD(extack, "Cannot append vlan_modify action");
1150 return PTR_ERR(act);
1152 mlxsw_afa_vlan_pack(act, MLXSW_AFA_VLAN_VLAN_TAG_CMD_NOP,
1153 MLXSW_AFA_VLAN_CMD_SET_OUTER, vid,
1154 MLXSW_AFA_VLAN_CMD_SET_OUTER, pcp,
1155 MLXSW_AFA_VLAN_CMD_SET_OUTER, et);
1158 EXPORT_SYMBOL(mlxsw_afa_block_append_vlan_modify);
1160 /* Trap Action / Trap With Userdef Action
1161 * --------------------------------------
1162 * The Trap action enables trapping / mirroring packets to the CPU
1163 * as well as discarding packets.
1164 * The ACL Trap / Discard separates the forward/discard control from CPU
1165 * trap control. In addition, the Trap / Discard action enables activating
1166 * SPAN (port mirroring).
1168 * The Trap with userdef action has the same functionality as
1169 * the Trap action with addition of user defined value that can be set
1170 * and used by higher layer applications.
1173 #define MLXSW_AFA_TRAP_CODE 0x03
1174 #define MLXSW_AFA_TRAP_SIZE 1
1176 #define MLXSW_AFA_TRAPWU_CODE 0x04
1177 #define MLXSW_AFA_TRAPWU_SIZE 2
1179 enum mlxsw_afa_trap_trap_action {
1180 MLXSW_AFA_TRAP_TRAP_ACTION_NOP = 0,
1181 MLXSW_AFA_TRAP_TRAP_ACTION_TRAP = 2,
1184 /* afa_trap_trap_action
1187 MLXSW_ITEM32(afa, trap, trap_action, 0x00, 24, 4);
1189 enum mlxsw_afa_trap_forward_action {
1190 MLXSW_AFA_TRAP_FORWARD_ACTION_FORWARD = 1,
1191 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD = 3,
1194 /* afa_trap_forward_action
1197 MLXSW_ITEM32(afa, trap, forward_action, 0x00, 0, 4);
1200 * Trap ID to configure.
1202 MLXSW_ITEM32(afa, trap, trap_id, 0x04, 0, 9);
1204 /* afa_trap_mirror_agent
1207 MLXSW_ITEM32(afa, trap, mirror_agent, 0x08, 29, 3);
1209 /* afa_trap_mirror_enable
1212 MLXSW_ITEM32(afa, trap, mirror_enable, 0x08, 24, 1);
1215 * Value for the SW usage. Can be used to pass information of which
1216 * rule has caused a trap. This may be overwritten by later traps.
1217 * This field does a set on the packet's user_def_val only if this
1218 * is the first trap_id or if the trap_id has replaced the previous
1221 MLXSW_ITEM32(afa, trap, user_def_val, 0x0C, 0, 20);
1224 mlxsw_afa_trap_pack(char *payload,
1225 enum mlxsw_afa_trap_trap_action trap_action,
1226 enum mlxsw_afa_trap_forward_action forward_action,
1229 mlxsw_afa_trap_trap_action_set(payload, trap_action);
1230 mlxsw_afa_trap_forward_action_set(payload, forward_action);
1231 mlxsw_afa_trap_trap_id_set(payload, trap_id);
1235 mlxsw_afa_trapwu_pack(char *payload,
1236 enum mlxsw_afa_trap_trap_action trap_action,
1237 enum mlxsw_afa_trap_forward_action forward_action,
1238 u16 trap_id, u32 user_def_val)
1240 mlxsw_afa_trap_pack(payload, trap_action, forward_action, trap_id);
1241 mlxsw_afa_trap_user_def_val_set(payload, user_def_val);
1245 mlxsw_afa_trap_mirror_pack(char *payload, bool mirror_enable,
1248 mlxsw_afa_trap_mirror_enable_set(payload, mirror_enable);
1249 mlxsw_afa_trap_mirror_agent_set(payload, mirror_agent);
1252 static char *mlxsw_afa_block_append_action_trap(struct mlxsw_afa_block *block,
1253 u8 action_code, u8 action_size)
1255 return mlxsw_afa_block_append_action_ext(block, action_code,
1257 MLXSW_AFA_ACTION_TYPE_TRAP);
1260 static int mlxsw_afa_block_append_drop_plain(struct mlxsw_afa_block *block,
1263 char *act = mlxsw_afa_block_append_action_trap(block,
1264 MLXSW_AFA_TRAP_CODE,
1265 MLXSW_AFA_TRAP_SIZE);
1268 return PTR_ERR(act);
1269 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1270 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD,
1271 ingress ? MLXSW_TRAP_ID_DISCARD_INGRESS_ACL :
1272 MLXSW_TRAP_ID_DISCARD_EGRESS_ACL);
1277 mlxsw_afa_block_append_drop_with_cookie(struct mlxsw_afa_block *block,
1279 const struct flow_action_cookie *fa_cookie,
1280 struct netlink_ext_ack *extack)
1282 struct mlxsw_afa_cookie_ref *cookie_ref;
1287 cookie_ref = mlxsw_afa_cookie_ref_create(block, fa_cookie);
1288 if (IS_ERR(cookie_ref)) {
1289 NL_SET_ERR_MSG_MOD(extack, "Cannot create cookie for drop action");
1290 return PTR_ERR(cookie_ref);
1292 cookie_index = cookie_ref->cookie->cookie_index;
1294 act = mlxsw_afa_block_append_action_trap(block, MLXSW_AFA_TRAPWU_CODE,
1295 MLXSW_AFA_TRAPWU_SIZE);
1297 NL_SET_ERR_MSG_MOD(extack, "Cannot append drop with cookie action");
1299 goto err_append_action;
1301 mlxsw_afa_trapwu_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1302 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD,
1303 ingress ? MLXSW_TRAP_ID_DISCARD_INGRESS_ACL :
1304 MLXSW_TRAP_ID_DISCARD_EGRESS_ACL,
1309 mlxsw_afa_cookie_ref_destroy(block, cookie_ref);
1313 int mlxsw_afa_block_append_drop(struct mlxsw_afa_block *block, bool ingress,
1314 const struct flow_action_cookie *fa_cookie,
1315 struct netlink_ext_ack *extack)
1318 mlxsw_afa_block_append_drop_with_cookie(block, ingress,
1319 fa_cookie, extack) :
1320 mlxsw_afa_block_append_drop_plain(block, ingress);
1322 EXPORT_SYMBOL(mlxsw_afa_block_append_drop);
1324 int mlxsw_afa_block_append_trap(struct mlxsw_afa_block *block, u16 trap_id)
1326 char *act = mlxsw_afa_block_append_action_trap(block,
1327 MLXSW_AFA_TRAP_CODE,
1328 MLXSW_AFA_TRAP_SIZE);
1331 return PTR_ERR(act);
1332 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1333 MLXSW_AFA_TRAP_FORWARD_ACTION_DISCARD, trap_id);
1336 EXPORT_SYMBOL(mlxsw_afa_block_append_trap);
1338 int mlxsw_afa_block_append_trap_and_forward(struct mlxsw_afa_block *block,
1341 char *act = mlxsw_afa_block_append_action_trap(block,
1342 MLXSW_AFA_TRAP_CODE,
1343 MLXSW_AFA_TRAP_SIZE);
1346 return PTR_ERR(act);
1347 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_TRAP,
1348 MLXSW_AFA_TRAP_FORWARD_ACTION_FORWARD, trap_id);
1351 EXPORT_SYMBOL(mlxsw_afa_block_append_trap_and_forward);
1353 struct mlxsw_afa_mirror {
1354 struct mlxsw_afa_resource resource;
1361 mlxsw_afa_mirror_destroy(struct mlxsw_afa_block *block,
1362 struct mlxsw_afa_mirror *mirror)
1364 mlxsw_afa_resource_del(&mirror->resource);
1365 block->afa->ops->mirror_del(block->afa->ops_priv,
1366 mirror->local_in_port,
1373 mlxsw_afa_mirror_destructor(struct mlxsw_afa_block *block,
1374 struct mlxsw_afa_resource *resource)
1376 struct mlxsw_afa_mirror *mirror;
1378 mirror = container_of(resource, struct mlxsw_afa_mirror, resource);
1379 mlxsw_afa_mirror_destroy(block, mirror);
1382 static struct mlxsw_afa_mirror *
1383 mlxsw_afa_mirror_create(struct mlxsw_afa_block *block, u16 local_in_port,
1384 const struct net_device *out_dev, bool ingress)
1386 struct mlxsw_afa_mirror *mirror;
1389 mirror = kzalloc(sizeof(*mirror), GFP_KERNEL);
1391 return ERR_PTR(-ENOMEM);
1393 err = block->afa->ops->mirror_add(block->afa->ops_priv,
1394 local_in_port, out_dev,
1395 ingress, &mirror->span_id);
1397 goto err_mirror_add;
1399 mirror->ingress = ingress;
1400 mirror->local_in_port = local_in_port;
1401 mirror->resource.destructor = mlxsw_afa_mirror_destructor;
1402 mlxsw_afa_resource_add(block, &mirror->resource);
1407 return ERR_PTR(err);
1411 mlxsw_afa_block_append_allocated_mirror(struct mlxsw_afa_block *block,
1414 char *act = mlxsw_afa_block_append_action_trap(block,
1415 MLXSW_AFA_TRAP_CODE,
1416 MLXSW_AFA_TRAP_SIZE);
1419 return PTR_ERR(act);
1420 mlxsw_afa_trap_pack(act, MLXSW_AFA_TRAP_TRAP_ACTION_NOP,
1421 MLXSW_AFA_TRAP_FORWARD_ACTION_FORWARD, 0);
1422 mlxsw_afa_trap_mirror_pack(act, true, mirror_agent);
1427 mlxsw_afa_block_append_mirror(struct mlxsw_afa_block *block, u16 local_in_port,
1428 const struct net_device *out_dev, bool ingress,
1429 struct netlink_ext_ack *extack)
1431 struct mlxsw_afa_mirror *mirror;
1434 mirror = mlxsw_afa_mirror_create(block, local_in_port, out_dev,
1436 if (IS_ERR(mirror)) {
1437 NL_SET_ERR_MSG_MOD(extack, "Cannot create mirror action");
1438 return PTR_ERR(mirror);
1440 err = mlxsw_afa_block_append_allocated_mirror(block, mirror->span_id);
1442 NL_SET_ERR_MSG_MOD(extack, "Cannot append mirror action");
1443 goto err_append_allocated_mirror;
1448 err_append_allocated_mirror:
1449 mlxsw_afa_mirror_destroy(block, mirror);
1452 EXPORT_SYMBOL(mlxsw_afa_block_append_mirror);
1456 * The QOS_ACTION is used for manipulating the QoS attributes of a packet. It
1457 * can be used to change the DCSP, ECN, Color and Switch Priority of the packet.
1458 * Note that PCP field can be changed using the VLAN action.
1461 #define MLXSW_AFA_QOS_CODE 0x06
1462 #define MLXSW_AFA_QOS_SIZE 1
1464 enum mlxsw_afa_qos_ecn_cmd {
1466 MLXSW_AFA_QOS_ECN_CMD_NOP,
1467 /* Set ECN to afa_qos_ecn */
1468 MLXSW_AFA_QOS_ECN_CMD_SET,
1473 MLXSW_ITEM32(afa, qos, ecn_cmd, 0x04, 29, 3);
1478 MLXSW_ITEM32(afa, qos, ecn, 0x04, 24, 2);
1480 enum mlxsw_afa_qos_dscp_cmd {
1482 MLXSW_AFA_QOS_DSCP_CMD_NOP,
1483 /* Set DSCP 3 LSB bits according to dscp[2:0] */
1484 MLXSW_AFA_QOS_DSCP_CMD_SET_3LSB,
1485 /* Set DSCP 3 MSB bits according to dscp[5:3] */
1486 MLXSW_AFA_QOS_DSCP_CMD_SET_3MSB,
1487 /* Set DSCP 6 bits according to dscp[5:0] */
1488 MLXSW_AFA_QOS_DSCP_CMD_SET_ALL,
1494 MLXSW_ITEM32(afa, qos, dscp_cmd, 0x04, 14, 2);
1499 MLXSW_ITEM32(afa, qos, dscp, 0x04, 0, 6);
1501 enum mlxsw_afa_qos_switch_prio_cmd {
1503 MLXSW_AFA_QOS_SWITCH_PRIO_CMD_NOP,
1504 /* Set Switch Priority to afa_qos_switch_prio */
1505 MLXSW_AFA_QOS_SWITCH_PRIO_CMD_SET,
1508 /* afa_qos_switch_prio_cmd
1510 MLXSW_ITEM32(afa, qos, switch_prio_cmd, 0x08, 14, 2);
1512 /* afa_qos_switch_prio
1515 MLXSW_ITEM32(afa, qos, switch_prio, 0x08, 0, 4);
1517 enum mlxsw_afa_qos_dscp_rw {
1518 MLXSW_AFA_QOS_DSCP_RW_PRESERVE,
1519 MLXSW_AFA_QOS_DSCP_RW_SET,
1520 MLXSW_AFA_QOS_DSCP_RW_CLEAR,
1524 * DSCP Re-write Enable. Controlling the rewrite_enable for DSCP.
1526 MLXSW_ITEM32(afa, qos, dscp_rw, 0x0C, 30, 2);
1529 mlxsw_afa_qos_ecn_pack(char *payload,
1530 enum mlxsw_afa_qos_ecn_cmd ecn_cmd, u8 ecn)
1532 mlxsw_afa_qos_ecn_cmd_set(payload, ecn_cmd);
1533 mlxsw_afa_qos_ecn_set(payload, ecn);
1537 mlxsw_afa_qos_dscp_pack(char *payload,
1538 enum mlxsw_afa_qos_dscp_cmd dscp_cmd, u8 dscp)
1540 mlxsw_afa_qos_dscp_cmd_set(payload, dscp_cmd);
1541 mlxsw_afa_qos_dscp_set(payload, dscp);
1545 mlxsw_afa_qos_switch_prio_pack(char *payload,
1546 enum mlxsw_afa_qos_switch_prio_cmd prio_cmd,
1549 mlxsw_afa_qos_switch_prio_cmd_set(payload, prio_cmd);
1550 mlxsw_afa_qos_switch_prio_set(payload, prio);
1553 static int __mlxsw_afa_block_append_qos_dsfield(struct mlxsw_afa_block *block,
1554 bool set_dscp, u8 dscp,
1555 bool set_ecn, u8 ecn,
1556 struct netlink_ext_ack *extack)
1558 char *act = mlxsw_afa_block_append_action(block,
1560 MLXSW_AFA_QOS_SIZE);
1563 NL_SET_ERR_MSG_MOD(extack, "Cannot append QOS action");
1564 return PTR_ERR(act);
1568 mlxsw_afa_qos_ecn_pack(act, MLXSW_AFA_QOS_ECN_CMD_SET, ecn);
1570 mlxsw_afa_qos_dscp_pack(act, MLXSW_AFA_QOS_DSCP_CMD_SET_ALL,
1572 mlxsw_afa_qos_dscp_rw_set(act, MLXSW_AFA_QOS_DSCP_RW_CLEAR);
1578 int mlxsw_afa_block_append_qos_dsfield(struct mlxsw_afa_block *block,
1580 struct netlink_ext_ack *extack)
1582 return __mlxsw_afa_block_append_qos_dsfield(block,
1584 true, dsfield & 0x03,
1587 EXPORT_SYMBOL(mlxsw_afa_block_append_qos_dsfield);
1589 int mlxsw_afa_block_append_qos_dscp(struct mlxsw_afa_block *block,
1590 u8 dscp, struct netlink_ext_ack *extack)
1592 return __mlxsw_afa_block_append_qos_dsfield(block,
1597 EXPORT_SYMBOL(mlxsw_afa_block_append_qos_dscp);
1599 int mlxsw_afa_block_append_qos_ecn(struct mlxsw_afa_block *block,
1600 u8 ecn, struct netlink_ext_ack *extack)
1602 return __mlxsw_afa_block_append_qos_dsfield(block,
1607 EXPORT_SYMBOL(mlxsw_afa_block_append_qos_ecn);
1609 int mlxsw_afa_block_append_qos_switch_prio(struct mlxsw_afa_block *block,
1611 struct netlink_ext_ack *extack)
1613 char *act = mlxsw_afa_block_append_action(block,
1615 MLXSW_AFA_QOS_SIZE);
1618 NL_SET_ERR_MSG_MOD(extack, "Cannot append QOS action");
1619 return PTR_ERR(act);
1621 mlxsw_afa_qos_switch_prio_pack(act, MLXSW_AFA_QOS_SWITCH_PRIO_CMD_SET,
1625 EXPORT_SYMBOL(mlxsw_afa_block_append_qos_switch_prio);
1627 /* Forwarding Action
1629 * Forwarding Action can be used to implement Policy Based Switching (PBS)
1630 * as well as OpenFlow related "Output" action.
1633 #define MLXSW_AFA_FORWARD_CODE 0x07
1634 #define MLXSW_AFA_FORWARD_SIZE 1
1636 enum mlxsw_afa_forward_type {
1637 /* PBS, Policy Based Switching */
1638 MLXSW_AFA_FORWARD_TYPE_PBS,
1639 /* Output, OpenFlow output type */
1640 MLXSW_AFA_FORWARD_TYPE_OUTPUT,
1643 /* afa_forward_type */
1644 MLXSW_ITEM32(afa, forward, type, 0x00, 24, 2);
1646 /* afa_forward_pbs_ptr
1647 * A pointer to the PBS entry configured by PPBS register.
1648 * Reserved when in_port is set.
1650 MLXSW_ITEM32(afa, forward, pbs_ptr, 0x08, 0, 24);
1652 /* afa_forward_in_port
1653 * Packet is forwarded back to the ingress port.
1655 MLXSW_ITEM32(afa, forward, in_port, 0x0C, 0, 1);
1658 mlxsw_afa_forward_pack(char *payload, enum mlxsw_afa_forward_type type,
1659 u32 pbs_ptr, bool in_port)
1661 mlxsw_afa_forward_type_set(payload, type);
1662 mlxsw_afa_forward_pbs_ptr_set(payload, pbs_ptr);
1663 mlxsw_afa_forward_in_port_set(payload, in_port);
1666 int mlxsw_afa_block_append_fwd(struct mlxsw_afa_block *block,
1667 u16 local_port, bool in_port,
1668 struct netlink_ext_ack *extack)
1670 struct mlxsw_afa_fwd_entry_ref *fwd_entry_ref;
1676 NL_SET_ERR_MSG_MOD(extack, "Forwarding to ingress port is not supported");
1679 fwd_entry_ref = mlxsw_afa_fwd_entry_ref_create(block, local_port);
1680 if (IS_ERR(fwd_entry_ref)) {
1681 NL_SET_ERR_MSG_MOD(extack, "Cannot create forward action");
1682 return PTR_ERR(fwd_entry_ref);
1684 kvdl_index = fwd_entry_ref->fwd_entry->kvdl_index;
1686 act = mlxsw_afa_block_append_action(block, MLXSW_AFA_FORWARD_CODE,
1687 MLXSW_AFA_FORWARD_SIZE);
1689 NL_SET_ERR_MSG_MOD(extack, "Cannot append forward action");
1691 goto err_append_action;
1693 mlxsw_afa_forward_pack(act, MLXSW_AFA_FORWARD_TYPE_PBS,
1694 kvdl_index, in_port);
1698 mlxsw_afa_fwd_entry_ref_destroy(block, fwd_entry_ref);
1701 EXPORT_SYMBOL(mlxsw_afa_block_append_fwd);
1703 /* Policing and Counting Action
1704 * ----------------------------
1705 * Policing and Counting action is used for binding policer and counter
1709 #define MLXSW_AFA_POLCNT_CODE 0x08
1710 #define MLXSW_AFA_POLCNT_SIZE 1
1713 MLXSW_AFA_POLCNT_COUNTER,
1714 MLXSW_AFA_POLCNT_POLICER,
1718 * Counter or policer.
1719 * Indicates whether the action binds a policer or a counter to the flow.
1723 MLXSW_ITEM32(afa, polcnt, c_p, 0x00, 31, 1);
1725 enum mlxsw_afa_polcnt_counter_set_type {
1727 MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_NO_COUNT = 0x00,
1728 /* Count packets and bytes */
1729 MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS_BYTES = 0x03,
1730 /* Count only packets */
1731 MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS = 0x05,
1734 /* afa_polcnt_counter_set_type
1735 * Counter set type for flow counters.
1737 MLXSW_ITEM32(afa, polcnt, counter_set_type, 0x04, 24, 8);
1739 /* afa_polcnt_counter_index
1740 * Counter index for flow counters.
1742 MLXSW_ITEM32(afa, polcnt, counter_index, 0x04, 0, 24);
1746 * Reserved when c_p = 0
1748 MLXSW_ITEM32(afa, polcnt, pid, 0x08, 0, 14);
1751 mlxsw_afa_polcnt_pack(char *payload,
1752 enum mlxsw_afa_polcnt_counter_set_type set_type,
1755 mlxsw_afa_polcnt_c_p_set(payload, MLXSW_AFA_POLCNT_COUNTER);
1756 mlxsw_afa_polcnt_counter_set_type_set(payload, set_type);
1757 mlxsw_afa_polcnt_counter_index_set(payload, counter_index);
1760 static void mlxsw_afa_polcnt_policer_pack(char *payload, u16 policer_index)
1762 mlxsw_afa_polcnt_c_p_set(payload, MLXSW_AFA_POLCNT_POLICER);
1763 mlxsw_afa_polcnt_pid_set(payload, policer_index);
1766 int mlxsw_afa_block_append_allocated_counter(struct mlxsw_afa_block *block,
1769 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_POLCNT_CODE,
1770 MLXSW_AFA_POLCNT_SIZE);
1772 return PTR_ERR(act);
1773 mlxsw_afa_polcnt_pack(act, MLXSW_AFA_POLCNT_COUNTER_SET_TYPE_PACKETS_BYTES,
1777 EXPORT_SYMBOL(mlxsw_afa_block_append_allocated_counter);
1779 int mlxsw_afa_block_append_counter(struct mlxsw_afa_block *block,
1780 u32 *p_counter_index,
1781 struct netlink_ext_ack *extack)
1783 struct mlxsw_afa_counter *counter;
1787 counter = mlxsw_afa_counter_create(block);
1788 if (IS_ERR(counter)) {
1789 NL_SET_ERR_MSG_MOD(extack, "Cannot create count action");
1790 return PTR_ERR(counter);
1792 counter_index = counter->counter_index;
1794 err = mlxsw_afa_block_append_allocated_counter(block, counter_index);
1796 NL_SET_ERR_MSG_MOD(extack, "Cannot append count action");
1797 goto err_append_allocated_counter;
1799 if (p_counter_index)
1800 *p_counter_index = counter_index;
1803 err_append_allocated_counter:
1804 mlxsw_afa_counter_destroy(block, counter);
1807 EXPORT_SYMBOL(mlxsw_afa_block_append_counter);
1809 int mlxsw_afa_block_append_police(struct mlxsw_afa_block *block,
1810 u32 fa_index, u64 rate_bytes_ps, u32 burst,
1811 u16 *p_policer_index,
1812 struct netlink_ext_ack *extack)
1814 struct mlxsw_afa_policer_ref *policer_ref;
1818 policer_ref = mlxsw_afa_policer_ref_create(block, fa_index,
1821 if (IS_ERR(policer_ref))
1822 return PTR_ERR(policer_ref);
1823 *p_policer_index = policer_ref->policer->policer_index;
1825 act = mlxsw_afa_block_append_action_ext(block, MLXSW_AFA_POLCNT_CODE,
1826 MLXSW_AFA_POLCNT_SIZE,
1827 MLXSW_AFA_ACTION_TYPE_POLICE);
1829 NL_SET_ERR_MSG_MOD(extack, "Cannot append police action");
1831 goto err_append_action;
1833 mlxsw_afa_polcnt_policer_pack(act, *p_policer_index);
1838 mlxsw_afa_policer_ref_destroy(block, policer_ref);
1841 EXPORT_SYMBOL(mlxsw_afa_block_append_police);
1843 /* Virtual Router and Forwarding Domain Action
1844 * -------------------------------------------
1845 * Virtual Switch action is used for manipulate the Virtual Router (VR),
1846 * MPLS label space and the Forwarding Identifier (FID).
1849 #define MLXSW_AFA_VIRFWD_CODE 0x0E
1850 #define MLXSW_AFA_VIRFWD_SIZE 1
1852 enum mlxsw_afa_virfwd_fid_cmd {
1854 MLXSW_AFA_VIRFWD_FID_CMD_NOOP,
1855 /* Set the Forwarding Identifier (FID) to fid */
1856 MLXSW_AFA_VIRFWD_FID_CMD_SET,
1859 /* afa_virfwd_fid_cmd */
1860 MLXSW_ITEM32(afa, virfwd, fid_cmd, 0x08, 29, 3);
1865 MLXSW_ITEM32(afa, virfwd, fid, 0x08, 0, 16);
1867 static inline void mlxsw_afa_virfwd_pack(char *payload,
1868 enum mlxsw_afa_virfwd_fid_cmd fid_cmd,
1871 mlxsw_afa_virfwd_fid_cmd_set(payload, fid_cmd);
1872 mlxsw_afa_virfwd_fid_set(payload, fid);
1875 int mlxsw_afa_block_append_fid_set(struct mlxsw_afa_block *block, u16 fid,
1876 struct netlink_ext_ack *extack)
1878 char *act = mlxsw_afa_block_append_action(block,
1879 MLXSW_AFA_VIRFWD_CODE,
1880 MLXSW_AFA_VIRFWD_SIZE);
1882 NL_SET_ERR_MSG_MOD(extack, "Cannot append fid_set action");
1883 return PTR_ERR(act);
1885 mlxsw_afa_virfwd_pack(act, MLXSW_AFA_VIRFWD_FID_CMD_SET, fid);
1888 EXPORT_SYMBOL(mlxsw_afa_block_append_fid_set);
1892 * The ignore action is used to ignore basic switching functions such as
1893 * learning on a per-packet basis.
1896 #define MLXSW_AFA_IGNORE_CODE 0x0F
1897 #define MLXSW_AFA_IGNORE_SIZE 1
1899 /* afa_ignore_disable_learning
1900 * Disable learning on ingress.
1902 MLXSW_ITEM32(afa, ignore, disable_learning, 0x00, 29, 1);
1904 /* afa_ignore_disable_security
1905 * Disable security lookup on ingress.
1906 * Reserved when Spectrum-1.
1908 MLXSW_ITEM32(afa, ignore, disable_security, 0x00, 28, 1);
1910 static void mlxsw_afa_ignore_pack(char *payload, bool disable_learning,
1911 bool disable_security)
1913 mlxsw_afa_ignore_disable_learning_set(payload, disable_learning);
1914 mlxsw_afa_ignore_disable_security_set(payload, disable_security);
1917 int mlxsw_afa_block_append_ignore(struct mlxsw_afa_block *block,
1918 bool disable_learning, bool disable_security)
1920 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_IGNORE_CODE,
1921 MLXSW_AFA_IGNORE_SIZE);
1924 return PTR_ERR(act);
1925 mlxsw_afa_ignore_pack(act, disable_learning, disable_security);
1928 EXPORT_SYMBOL(mlxsw_afa_block_append_ignore);
1930 /* MC Routing Action
1932 * The Multicast router action. Can be used by RMFT_V2 - Router Multicast
1933 * Forwarding Table Version 2 Register.
1936 #define MLXSW_AFA_MCROUTER_CODE 0x10
1937 #define MLXSW_AFA_MCROUTER_SIZE 2
1939 enum mlxsw_afa_mcrouter_rpf_action {
1940 MLXSW_AFA_MCROUTER_RPF_ACTION_NOP,
1941 MLXSW_AFA_MCROUTER_RPF_ACTION_TRAP,
1942 MLXSW_AFA_MCROUTER_RPF_ACTION_DISCARD_ERROR,
1945 /* afa_mcrouter_rpf_action */
1946 MLXSW_ITEM32(afa, mcrouter, rpf_action, 0x00, 28, 3);
1948 /* afa_mcrouter_expected_irif */
1949 MLXSW_ITEM32(afa, mcrouter, expected_irif, 0x00, 0, 16);
1951 /* afa_mcrouter_min_mtu */
1952 MLXSW_ITEM32(afa, mcrouter, min_mtu, 0x08, 0, 16);
1954 enum mlxsw_afa_mrouter_vrmid {
1955 MLXSW_AFA_MCROUTER_VRMID_INVALID,
1956 MLXSW_AFA_MCROUTER_VRMID_VALID
1959 /* afa_mcrouter_vrmid
1960 * Valid RMID: rigr_rmid_index is used as RMID
1962 MLXSW_ITEM32(afa, mcrouter, vrmid, 0x0C, 31, 1);
1964 /* afa_mcrouter_rigr_rmid_index
1965 * When the vrmid field is set to invalid, the field is used as pointer to
1966 * Router Interface Group (RIGR) Table in the KVD linear.
1967 * When the vrmid is set to valid, the field is used as RMID index, ranged
1968 * from 0 to max_mid - 1. The index is to the Port Group Table.
1970 MLXSW_ITEM32(afa, mcrouter, rigr_rmid_index, 0x0C, 0, 24);
1973 mlxsw_afa_mcrouter_pack(char *payload,
1974 enum mlxsw_afa_mcrouter_rpf_action rpf_action,
1975 u16 expected_irif, u16 min_mtu,
1976 enum mlxsw_afa_mrouter_vrmid vrmid, u32 rigr_rmid_index)
1979 mlxsw_afa_mcrouter_rpf_action_set(payload, rpf_action);
1980 mlxsw_afa_mcrouter_expected_irif_set(payload, expected_irif);
1981 mlxsw_afa_mcrouter_min_mtu_set(payload, min_mtu);
1982 mlxsw_afa_mcrouter_vrmid_set(payload, vrmid);
1983 mlxsw_afa_mcrouter_rigr_rmid_index_set(payload, rigr_rmid_index);
1986 int mlxsw_afa_block_append_mcrouter(struct mlxsw_afa_block *block,
1987 u16 expected_irif, u16 min_mtu,
1988 bool rmid_valid, u32 kvdl_index)
1990 char *act = mlxsw_afa_block_append_action(block,
1991 MLXSW_AFA_MCROUTER_CODE,
1992 MLXSW_AFA_MCROUTER_SIZE);
1994 return PTR_ERR(act);
1995 mlxsw_afa_mcrouter_pack(act, MLXSW_AFA_MCROUTER_RPF_ACTION_TRAP,
1996 expected_irif, min_mtu, rmid_valid, kvdl_index);
1999 EXPORT_SYMBOL(mlxsw_afa_block_append_mcrouter);
2003 * The SIP_DIP_ACTION is used for modifying the SIP and DIP fields of the
2004 * packet, e.g. for NAT. The L3 checksum is updated. Also, if the L4 is TCP or
2005 * if the L4 is UDP and the checksum field is not zero, then the L4 checksum is
2009 #define MLXSW_AFA_IP_CODE 0x11
2010 #define MLXSW_AFA_IP_SIZE 2
2012 enum mlxsw_afa_ip_s_d {
2013 /* ip refers to dip */
2014 MLXSW_AFA_IP_S_D_DIP,
2015 /* ip refers to sip */
2016 MLXSW_AFA_IP_S_D_SIP,
2020 * Source or destination.
2022 MLXSW_ITEM32(afa, ip, s_d, 0x00, 31, 1);
2024 enum mlxsw_afa_ip_m_l {
2025 /* LSB: ip[63:0] refers to ip[63:0] */
2026 MLXSW_AFA_IP_M_L_LSB,
2027 /* MSB: ip[63:0] refers to ip[127:64] */
2028 MLXSW_AFA_IP_M_L_MSB,
2034 MLXSW_ITEM32(afa, ip, m_l, 0x00, 30, 1);
2037 * Bits [63:32] in the IP address to change to.
2039 MLXSW_ITEM32(afa, ip, ip_63_32, 0x08, 0, 32);
2042 * Bits [31:0] in the IP address to change to.
2044 MLXSW_ITEM32(afa, ip, ip_31_0, 0x0C, 0, 32);
2046 static void mlxsw_afa_ip_pack(char *payload, enum mlxsw_afa_ip_s_d s_d,
2047 enum mlxsw_afa_ip_m_l m_l, u32 ip_31_0,
2050 mlxsw_afa_ip_s_d_set(payload, s_d);
2051 mlxsw_afa_ip_m_l_set(payload, m_l);
2052 mlxsw_afa_ip_ip_31_0_set(payload, ip_31_0);
2053 mlxsw_afa_ip_ip_63_32_set(payload, ip_63_32);
2056 int mlxsw_afa_block_append_ip(struct mlxsw_afa_block *block, bool is_dip,
2057 bool is_lsb, u32 val_31_0, u32 val_63_32,
2058 struct netlink_ext_ack *extack)
2060 enum mlxsw_afa_ip_s_d s_d = is_dip ? MLXSW_AFA_IP_S_D_DIP :
2061 MLXSW_AFA_IP_S_D_SIP;
2062 enum mlxsw_afa_ip_m_l m_l = is_lsb ? MLXSW_AFA_IP_M_L_LSB :
2063 MLXSW_AFA_IP_M_L_MSB;
2064 char *act = mlxsw_afa_block_append_action(block,
2069 NL_SET_ERR_MSG_MOD(extack, "Cannot append IP action");
2070 return PTR_ERR(act);
2073 mlxsw_afa_ip_pack(act, s_d, m_l, val_31_0, val_63_32);
2076 EXPORT_SYMBOL(mlxsw_afa_block_append_ip);
2080 * The L4_PORT_ACTION is used for modifying the sport and dport fields of the packet, e.g. for NAT.
2081 * If (the L4 is TCP) or if (the L4 is UDP and checksum field!=0) then the L4 checksum is updated.
2084 #define MLXSW_AFA_L4PORT_CODE 0x12
2085 #define MLXSW_AFA_L4PORT_SIZE 1
2087 enum mlxsw_afa_l4port_s_d {
2088 /* configure src_l4_port */
2089 MLXSW_AFA_L4PORT_S_D_SRC,
2090 /* configure dst_l4_port */
2091 MLXSW_AFA_L4PORT_S_D_DST,
2095 * Source or destination.
2097 MLXSW_ITEM32(afa, l4port, s_d, 0x00, 31, 1);
2099 /* afa_l4port_l4_port
2100 * Number of port to change to.
2102 MLXSW_ITEM32(afa, l4port, l4_port, 0x08, 0, 16);
2104 static void mlxsw_afa_l4port_pack(char *payload, enum mlxsw_afa_l4port_s_d s_d, u16 l4_port)
2106 mlxsw_afa_l4port_s_d_set(payload, s_d);
2107 mlxsw_afa_l4port_l4_port_set(payload, l4_port);
2110 int mlxsw_afa_block_append_l4port(struct mlxsw_afa_block *block, bool is_dport, u16 l4_port,
2111 struct netlink_ext_ack *extack)
2113 enum mlxsw_afa_l4port_s_d s_d = is_dport ? MLXSW_AFA_L4PORT_S_D_DST :
2114 MLXSW_AFA_L4PORT_S_D_SRC;
2115 char *act = mlxsw_afa_block_append_action(block,
2116 MLXSW_AFA_L4PORT_CODE,
2117 MLXSW_AFA_L4PORT_SIZE);
2120 NL_SET_ERR_MSG_MOD(extack, "Cannot append L4_PORT action");
2121 return PTR_ERR(act);
2124 mlxsw_afa_l4port_pack(act, s_d, l4_port);
2127 EXPORT_SYMBOL(mlxsw_afa_block_append_l4port);
2129 /* Mirror Sampler Action
2130 * ---------------------
2131 * The SAMPLER_ACTION is used to mirror packets with a probability (sampling).
2134 #define MLXSW_AFA_SAMPLER_CODE 0x13
2135 #define MLXSW_AFA_SAMPLER_SIZE 1
2137 /* afa_sampler_mirror_agent
2138 * Mirror (SPAN) agent.
2140 MLXSW_ITEM32(afa, sampler, mirror_agent, 0x04, 0, 3);
2142 #define MLXSW_AFA_SAMPLER_RATE_MAX (BIT(24) - 1)
2144 /* afa_sampler_mirror_probability_rate
2145 * Mirroring probability.
2146 * Valid values are 1 to 2^24 - 1
2148 MLXSW_ITEM32(afa, sampler, mirror_probability_rate, 0x08, 0, 24);
2150 static void mlxsw_afa_sampler_pack(char *payload, u8 mirror_agent, u32 rate)
2152 mlxsw_afa_sampler_mirror_agent_set(payload, mirror_agent);
2153 mlxsw_afa_sampler_mirror_probability_rate_set(payload, rate);
2156 struct mlxsw_afa_sampler {
2157 struct mlxsw_afa_resource resource;
2163 static void mlxsw_afa_sampler_destroy(struct mlxsw_afa_block *block,
2164 struct mlxsw_afa_sampler *sampler)
2166 mlxsw_afa_resource_del(&sampler->resource);
2167 block->afa->ops->sampler_del(block->afa->ops_priv, sampler->local_port,
2168 sampler->span_id, sampler->ingress);
2172 static void mlxsw_afa_sampler_destructor(struct mlxsw_afa_block *block,
2173 struct mlxsw_afa_resource *resource)
2175 struct mlxsw_afa_sampler *sampler;
2177 sampler = container_of(resource, struct mlxsw_afa_sampler, resource);
2178 mlxsw_afa_sampler_destroy(block, sampler);
2181 static struct mlxsw_afa_sampler *
2182 mlxsw_afa_sampler_create(struct mlxsw_afa_block *block, u16 local_port,
2183 struct psample_group *psample_group, u32 rate,
2184 u32 trunc_size, bool truncate, bool ingress,
2185 struct netlink_ext_ack *extack)
2187 struct mlxsw_afa_sampler *sampler;
2190 sampler = kzalloc(sizeof(*sampler), GFP_KERNEL);
2192 return ERR_PTR(-ENOMEM);
2194 err = block->afa->ops->sampler_add(block->afa->ops_priv, local_port,
2195 psample_group, rate, trunc_size,
2196 truncate, ingress, &sampler->span_id,
2199 goto err_sampler_add;
2201 sampler->ingress = ingress;
2202 sampler->local_port = local_port;
2203 sampler->resource.destructor = mlxsw_afa_sampler_destructor;
2204 mlxsw_afa_resource_add(block, &sampler->resource);
2209 return ERR_PTR(err);
2213 mlxsw_afa_block_append_allocated_sampler(struct mlxsw_afa_block *block,
2214 u8 mirror_agent, u32 rate)
2216 char *act = mlxsw_afa_block_append_action(block, MLXSW_AFA_SAMPLER_CODE,
2217 MLXSW_AFA_SAMPLER_SIZE);
2220 return PTR_ERR(act);
2221 mlxsw_afa_sampler_pack(act, mirror_agent, rate);
2225 int mlxsw_afa_block_append_sampler(struct mlxsw_afa_block *block, u16 local_port,
2226 struct psample_group *psample_group,
2227 u32 rate, u32 trunc_size, bool truncate,
2229 struct netlink_ext_ack *extack)
2231 struct mlxsw_afa_sampler *sampler;
2234 if (rate > MLXSW_AFA_SAMPLER_RATE_MAX) {
2235 NL_SET_ERR_MSG_MOD(extack, "Sampling rate is too high");
2239 sampler = mlxsw_afa_sampler_create(block, local_port, psample_group,
2240 rate, trunc_size, truncate, ingress,
2242 if (IS_ERR(sampler))
2243 return PTR_ERR(sampler);
2245 err = mlxsw_afa_block_append_allocated_sampler(block, sampler->span_id,
2248 NL_SET_ERR_MSG_MOD(extack, "Cannot append sampler action");
2249 goto err_append_allocated_sampler;
2254 err_append_allocated_sampler:
2255 mlxsw_afa_sampler_destroy(block, sampler);
2258 EXPORT_SYMBOL(mlxsw_afa_block_append_sampler);