1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2020 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/netdevice.h>
7 #include <net/flow_offload.h>
10 #include "spectrum_span.h"
13 static struct mlxsw_sp_mall_entry *
14 mlxsw_sp_mall_entry_find(struct mlxsw_sp_flow_block *block, unsigned long cookie)
16 struct mlxsw_sp_mall_entry *mall_entry;
18 list_for_each_entry(mall_entry, &block->mall.list, list)
19 if (mall_entry->cookie == cookie)
26 mlxsw_sp_mall_port_mirror_add(struct mlxsw_sp_port *mlxsw_sp_port,
27 struct mlxsw_sp_mall_entry *mall_entry,
28 struct netlink_ext_ack *extack)
30 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
31 struct mlxsw_sp_span_agent_parms agent_parms = {};
32 struct mlxsw_sp_span_trigger_parms parms;
33 enum mlxsw_sp_span_trigger trigger;
36 if (!mall_entry->mirror.to_dev) {
37 NL_SET_ERR_MSG(extack, "Could not find requested device");
41 agent_parms.to_dev = mall_entry->mirror.to_dev;
42 err = mlxsw_sp_span_agent_get(mlxsw_sp, &mall_entry->mirror.span_id,
45 NL_SET_ERR_MSG(extack, "Failed to get SPAN agent");
49 err = mlxsw_sp_span_analyzed_port_get(mlxsw_sp_port,
52 NL_SET_ERR_MSG(extack, "Failed to get analyzed port");
53 goto err_analyzed_port_get;
56 trigger = mall_entry->ingress ? MLXSW_SP_SPAN_TRIGGER_INGRESS :
57 MLXSW_SP_SPAN_TRIGGER_EGRESS;
58 parms.span_id = mall_entry->mirror.span_id;
59 parms.probability_rate = 1;
60 err = mlxsw_sp_span_agent_bind(mlxsw_sp, trigger, mlxsw_sp_port,
63 NL_SET_ERR_MSG(extack, "Failed to bind SPAN agent");
70 mlxsw_sp_span_analyzed_port_put(mlxsw_sp_port, mall_entry->ingress);
71 err_analyzed_port_get:
72 mlxsw_sp_span_agent_put(mlxsw_sp, mall_entry->mirror.span_id);
77 mlxsw_sp_mall_port_mirror_del(struct mlxsw_sp_port *mlxsw_sp_port,
78 struct mlxsw_sp_mall_entry *mall_entry)
80 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
81 struct mlxsw_sp_span_trigger_parms parms;
82 enum mlxsw_sp_span_trigger trigger;
84 trigger = mall_entry->ingress ? MLXSW_SP_SPAN_TRIGGER_INGRESS :
85 MLXSW_SP_SPAN_TRIGGER_EGRESS;
86 parms.span_id = mall_entry->mirror.span_id;
87 mlxsw_sp_span_agent_unbind(mlxsw_sp, trigger, mlxsw_sp_port, &parms);
88 mlxsw_sp_span_analyzed_port_put(mlxsw_sp_port, mall_entry->ingress);
89 mlxsw_sp_span_agent_put(mlxsw_sp, mall_entry->mirror.span_id);
92 static int mlxsw_sp_mall_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
93 bool enable, u32 rate)
95 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
96 char mpsc_pl[MLXSW_REG_MPSC_LEN];
98 mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
99 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
103 mlxsw_sp_mall_port_sample_add(struct mlxsw_sp_port *mlxsw_sp_port,
104 struct mlxsw_sp_mall_entry *mall_entry,
105 struct netlink_ext_ack *extack)
107 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
108 struct mlxsw_sp_sample_trigger trigger;
111 if (mall_entry->ingress)
112 trigger.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_INGRESS;
114 trigger.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_EGRESS;
115 trigger.local_port = mlxsw_sp_port->local_port;
116 err = mlxsw_sp_sample_trigger_params_set(mlxsw_sp, &trigger,
117 &mall_entry->sample.params,
122 err = mlxsw_sp->mall_ops->sample_add(mlxsw_sp, mlxsw_sp_port,
125 goto err_port_sample_set;
129 mlxsw_sp_sample_trigger_params_unset(mlxsw_sp, &trigger);
134 mlxsw_sp_mall_port_sample_del(struct mlxsw_sp_port *mlxsw_sp_port,
135 struct mlxsw_sp_mall_entry *mall_entry)
137 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
138 struct mlxsw_sp_sample_trigger trigger;
140 if (mall_entry->ingress)
141 trigger.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_INGRESS;
143 trigger.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_EGRESS;
144 trigger.local_port = mlxsw_sp_port->local_port;
146 mlxsw_sp->mall_ops->sample_del(mlxsw_sp, mlxsw_sp_port, mall_entry);
147 mlxsw_sp_sample_trigger_params_unset(mlxsw_sp, &trigger);
151 mlxsw_sp_mall_port_rule_add(struct mlxsw_sp_port *mlxsw_sp_port,
152 struct mlxsw_sp_mall_entry *mall_entry,
153 struct netlink_ext_ack *extack)
155 switch (mall_entry->type) {
156 case MLXSW_SP_MALL_ACTION_TYPE_MIRROR:
157 return mlxsw_sp_mall_port_mirror_add(mlxsw_sp_port, mall_entry,
159 case MLXSW_SP_MALL_ACTION_TYPE_SAMPLE:
160 return mlxsw_sp_mall_port_sample_add(mlxsw_sp_port, mall_entry,
169 mlxsw_sp_mall_port_rule_del(struct mlxsw_sp_port *mlxsw_sp_port,
170 struct mlxsw_sp_mall_entry *mall_entry)
172 switch (mall_entry->type) {
173 case MLXSW_SP_MALL_ACTION_TYPE_MIRROR:
174 mlxsw_sp_mall_port_mirror_del(mlxsw_sp_port, mall_entry);
176 case MLXSW_SP_MALL_ACTION_TYPE_SAMPLE:
177 mlxsw_sp_mall_port_sample_del(mlxsw_sp_port, mall_entry);
184 static void mlxsw_sp_mall_prio_update(struct mlxsw_sp_flow_block *block)
186 struct mlxsw_sp_mall_entry *mall_entry;
188 if (list_empty(&block->mall.list))
190 block->mall.min_prio = UINT_MAX;
191 block->mall.max_prio = 0;
192 list_for_each_entry(mall_entry, &block->mall.list, list) {
193 if (mall_entry->priority < block->mall.min_prio)
194 block->mall.min_prio = mall_entry->priority;
195 if (mall_entry->priority > block->mall.max_prio)
196 block->mall.max_prio = mall_entry->priority;
200 int mlxsw_sp_mall_replace(struct mlxsw_sp *mlxsw_sp,
201 struct mlxsw_sp_flow_block *block,
202 struct tc_cls_matchall_offload *f)
204 struct mlxsw_sp_flow_block_binding *binding;
205 struct mlxsw_sp_mall_entry *mall_entry;
206 __be16 protocol = f->common.protocol;
207 struct flow_action_entry *act;
208 unsigned int flower_min_prio;
209 unsigned int flower_max_prio;
210 bool flower_prio_valid;
213 if (!flow_offload_has_one_action(&f->rule->action)) {
214 NL_SET_ERR_MSG(f->common.extack, "Only singular actions are supported");
218 if (f->common.chain_index) {
219 NL_SET_ERR_MSG(f->common.extack, "Only chain 0 is supported");
223 if (mlxsw_sp_flow_block_is_mixed_bound(block)) {
224 NL_SET_ERR_MSG(f->common.extack, "Only not mixed bound blocks are supported");
228 err = mlxsw_sp_flower_prio_get(mlxsw_sp, block, f->common.chain_index,
229 &flower_min_prio, &flower_max_prio);
231 if (err != -ENOENT) {
232 NL_SET_ERR_MSG(f->common.extack, "Failed to get flower priorities");
235 flower_prio_valid = false;
236 /* No flower filters are installed in specified chain. */
238 flower_prio_valid = true;
241 if (protocol != htons(ETH_P_ALL)) {
242 NL_SET_ERR_MSG(f->common.extack, "matchall rules only supported with 'all' protocol");
246 mall_entry = kzalloc(sizeof(*mall_entry), GFP_KERNEL);
249 mall_entry->cookie = f->cookie;
250 mall_entry->priority = f->common.prio;
251 mall_entry->ingress = mlxsw_sp_flow_block_is_ingress_bound(block);
253 if (flower_prio_valid && mall_entry->ingress &&
254 mall_entry->priority >= flower_min_prio) {
255 NL_SET_ERR_MSG(f->common.extack, "Failed to add behind existing flower rules");
259 if (flower_prio_valid && !mall_entry->ingress &&
260 mall_entry->priority <= flower_max_prio) {
261 NL_SET_ERR_MSG(f->common.extack, "Failed to add in front of existing flower rules");
266 act = &f->rule->action.entries[0];
269 case FLOW_ACTION_MIRRED:
270 mall_entry->type = MLXSW_SP_MALL_ACTION_TYPE_MIRROR;
271 mall_entry->mirror.to_dev = act->dev;
273 case FLOW_ACTION_SAMPLE:
274 mall_entry->type = MLXSW_SP_MALL_ACTION_TYPE_SAMPLE;
275 mall_entry->sample.params.psample_group = act->sample.psample_group;
276 mall_entry->sample.params.truncate = act->sample.truncate;
277 mall_entry->sample.params.trunc_size = act->sample.trunc_size;
278 mall_entry->sample.params.rate = act->sample.rate;
285 list_for_each_entry(binding, &block->binding_list, list) {
286 err = mlxsw_sp_mall_port_rule_add(binding->mlxsw_sp_port,
287 mall_entry, f->common.extack);
293 if (mall_entry->ingress)
294 block->egress_blocker_rule_count++;
296 block->ingress_blocker_rule_count++;
297 list_add_tail(&mall_entry->list, &block->mall.list);
298 mlxsw_sp_mall_prio_update(block);
302 list_for_each_entry_continue_reverse(binding, &block->binding_list,
304 mlxsw_sp_mall_port_rule_del(binding->mlxsw_sp_port, mall_entry);
310 void mlxsw_sp_mall_destroy(struct mlxsw_sp_flow_block *block,
311 struct tc_cls_matchall_offload *f)
313 struct mlxsw_sp_flow_block_binding *binding;
314 struct mlxsw_sp_mall_entry *mall_entry;
316 mall_entry = mlxsw_sp_mall_entry_find(block, f->cookie);
318 NL_SET_ERR_MSG(f->common.extack, "Entry not found");
322 list_del(&mall_entry->list);
323 if (mall_entry->ingress)
324 block->egress_blocker_rule_count--;
326 block->ingress_blocker_rule_count--;
328 list_for_each_entry(binding, &block->binding_list, list)
329 mlxsw_sp_mall_port_rule_del(binding->mlxsw_sp_port, mall_entry);
330 kfree_rcu(mall_entry, rcu); /* sample RX packets may be in-flight */
331 mlxsw_sp_mall_prio_update(block);
334 int mlxsw_sp_mall_port_bind(struct mlxsw_sp_flow_block *block,
335 struct mlxsw_sp_port *mlxsw_sp_port,
336 struct netlink_ext_ack *extack)
338 struct mlxsw_sp_mall_entry *mall_entry;
341 list_for_each_entry(mall_entry, &block->mall.list, list) {
342 err = mlxsw_sp_mall_port_rule_add(mlxsw_sp_port, mall_entry,
350 list_for_each_entry_continue_reverse(mall_entry, &block->mall.list,
352 mlxsw_sp_mall_port_rule_del(mlxsw_sp_port, mall_entry);
356 void mlxsw_sp_mall_port_unbind(struct mlxsw_sp_flow_block *block,
357 struct mlxsw_sp_port *mlxsw_sp_port)
359 struct mlxsw_sp_mall_entry *mall_entry;
361 list_for_each_entry(mall_entry, &block->mall.list, list)
362 mlxsw_sp_mall_port_rule_del(mlxsw_sp_port, mall_entry);
365 int mlxsw_sp_mall_prio_get(struct mlxsw_sp_flow_block *block, u32 chain_index,
366 unsigned int *p_min_prio, unsigned int *p_max_prio)
368 if (chain_index || list_empty(&block->mall.list))
369 /* In case there are no matchall rules, the caller
370 * receives -ENOENT to indicate there is no need
371 * to check the priorities.
374 *p_min_prio = block->mall.min_prio;
375 *p_max_prio = block->mall.max_prio;
379 static int mlxsw_sp1_mall_sample_add(struct mlxsw_sp *mlxsw_sp,
380 struct mlxsw_sp_port *mlxsw_sp_port,
381 struct mlxsw_sp_mall_entry *mall_entry,
382 struct netlink_ext_ack *extack)
384 u32 rate = mall_entry->sample.params.rate;
386 if (!mall_entry->ingress) {
387 NL_SET_ERR_MSG(extack, "Sampling is not supported on egress");
391 if (rate > MLXSW_REG_MPSC_RATE_MAX) {
392 NL_SET_ERR_MSG(extack, "Unsupported sampling rate");
396 return mlxsw_sp_mall_port_sample_set(mlxsw_sp_port, true, rate);
399 static void mlxsw_sp1_mall_sample_del(struct mlxsw_sp *mlxsw_sp,
400 struct mlxsw_sp_port *mlxsw_sp_port,
401 struct mlxsw_sp_mall_entry *mall_entry)
403 mlxsw_sp_mall_port_sample_set(mlxsw_sp_port, false, 1);
406 const struct mlxsw_sp_mall_ops mlxsw_sp1_mall_ops = {
407 .sample_add = mlxsw_sp1_mall_sample_add,
408 .sample_del = mlxsw_sp1_mall_sample_del,
411 static int mlxsw_sp2_mall_sample_add(struct mlxsw_sp *mlxsw_sp,
412 struct mlxsw_sp_port *mlxsw_sp_port,
413 struct mlxsw_sp_mall_entry *mall_entry,
414 struct netlink_ext_ack *extack)
416 struct mlxsw_sp_span_trigger_parms trigger_parms = {};
417 struct mlxsw_sp_span_agent_parms agent_parms = {
418 .to_dev = NULL, /* Mirror to CPU. */
419 .session_id = MLXSW_SP_SPAN_SESSION_ID_SAMPLING,
421 u32 rate = mall_entry->sample.params.rate;
422 enum mlxsw_sp_span_trigger span_trigger;
425 err = mlxsw_sp_span_agent_get(mlxsw_sp, &mall_entry->sample.span_id,
428 NL_SET_ERR_MSG(extack, "Failed to get SPAN agent");
432 err = mlxsw_sp_span_analyzed_port_get(mlxsw_sp_port,
433 mall_entry->ingress);
435 NL_SET_ERR_MSG(extack, "Failed to get analyzed port");
436 goto err_analyzed_port_get;
439 span_trigger = mall_entry->ingress ? MLXSW_SP_SPAN_TRIGGER_INGRESS :
440 MLXSW_SP_SPAN_TRIGGER_EGRESS;
441 trigger_parms.span_id = mall_entry->sample.span_id;
442 trigger_parms.probability_rate = rate;
443 err = mlxsw_sp_span_agent_bind(mlxsw_sp, span_trigger, mlxsw_sp_port,
446 NL_SET_ERR_MSG(extack, "Failed to bind SPAN agent");
453 mlxsw_sp_span_analyzed_port_put(mlxsw_sp_port, mall_entry->ingress);
454 err_analyzed_port_get:
455 mlxsw_sp_span_agent_put(mlxsw_sp, mall_entry->sample.span_id);
459 static void mlxsw_sp2_mall_sample_del(struct mlxsw_sp *mlxsw_sp,
460 struct mlxsw_sp_port *mlxsw_sp_port,
461 struct mlxsw_sp_mall_entry *mall_entry)
463 struct mlxsw_sp_span_trigger_parms trigger_parms = {};
464 enum mlxsw_sp_span_trigger span_trigger;
466 span_trigger = mall_entry->ingress ? MLXSW_SP_SPAN_TRIGGER_INGRESS :
467 MLXSW_SP_SPAN_TRIGGER_EGRESS;
468 trigger_parms.span_id = mall_entry->sample.span_id;
469 mlxsw_sp_span_agent_unbind(mlxsw_sp, span_trigger, mlxsw_sp_port,
471 mlxsw_sp_span_analyzed_port_put(mlxsw_sp_port, mall_entry->ingress);
472 mlxsw_sp_span_agent_put(mlxsw_sp, mall_entry->sample.span_id);
475 const struct mlxsw_sp_mall_ops mlxsw_sp2_mall_ops = {
476 .sample_add = mlxsw_sp2_mall_sample_add,
477 .sample_del = mlxsw_sp2_mall_sample_del,