1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_malloc.h>
11 #include "rte_port_in_action.h"
14 * RTE_PORT_IN_ACTION_FLTR
17 fltr_cfg_check(struct rte_port_in_action_fltr_config *cfg)
30 fltr_init(struct fltr_data *data,
31 struct rte_port_in_action_fltr_config *cfg)
33 data->port_id = cfg->port_id;
37 fltr_apply(struct fltr_data *data,
38 struct rte_port_in_action_fltr_params *p)
40 /* Check input arguments */
44 data->port_id = p->port_id;
50 * RTE_PORT_IN_ACTION_LB
53 lb_cfg_check(struct rte_port_in_action_lb_config *cfg)
56 (cfg->key_size < RTE_PORT_IN_ACTION_LB_KEY_SIZE_MIN) ||
57 (cfg->key_size > RTE_PORT_IN_ACTION_LB_KEY_SIZE_MAX) ||
58 (!rte_is_power_of_2(cfg->key_size)) ||
59 (cfg->f_hash == NULL))
66 uint32_t port_id[RTE_PORT_IN_ACTION_LB_TABLE_SIZE];
70 lb_init(struct lb_data *data,
71 struct rte_port_in_action_lb_config *cfg)
73 memcpy(data->port_id, cfg->port_id, sizeof(cfg->port_id));
77 lb_apply(struct lb_data *data,
78 struct rte_port_in_action_lb_params *p)
80 /* Check input arguments */
84 memcpy(data->port_id, p->port_id, sizeof(p->port_id));
93 action_valid(enum rte_port_in_action_type action)
96 case RTE_PORT_IN_ACTION_FLTR:
97 case RTE_PORT_IN_ACTION_LB:
104 #define RTE_PORT_IN_ACTION_MAX 64
107 uint64_t action_mask;
108 struct rte_port_in_action_fltr_config fltr;
109 struct rte_port_in_action_lb_config lb;
113 action_cfg_size(enum rte_port_in_action_type action)
116 case RTE_PORT_IN_ACTION_FLTR:
117 return sizeof(struct rte_port_in_action_fltr_config);
118 case RTE_PORT_IN_ACTION_LB:
119 return sizeof(struct rte_port_in_action_lb_config);
126 action_cfg_get(struct ap_config *ap_config,
127 enum rte_port_in_action_type type)
130 case RTE_PORT_IN_ACTION_FLTR:
131 return &ap_config->fltr;
133 case RTE_PORT_IN_ACTION_LB:
134 return &ap_config->lb;
142 action_cfg_set(struct ap_config *ap_config,
143 enum rte_port_in_action_type type,
146 void *dst = action_cfg_get(ap_config, type);
149 memcpy(dst, action_cfg, action_cfg_size(type));
151 ap_config->action_mask |= 1LLU << type;
155 size_t offset[RTE_PORT_IN_ACTION_MAX];
160 action_data_size(enum rte_port_in_action_type action,
161 struct ap_config *ap_config __rte_unused)
164 case RTE_PORT_IN_ACTION_FLTR:
165 return sizeof(struct fltr_data);
167 case RTE_PORT_IN_ACTION_LB:
168 return sizeof(struct lb_data);
176 action_data_offset_set(struct ap_data *ap_data,
177 struct ap_config *ap_config)
179 uint64_t action_mask = ap_config->action_mask;
183 memset(ap_data->offset, 0, sizeof(ap_data->offset));
186 for (action = 0; action < RTE_PORT_IN_ACTION_MAX; action++)
187 if (action_mask & (1LLU << action)) {
188 ap_data->offset[action] = offset;
189 offset += action_data_size((enum rte_port_in_action_type)action,
193 ap_data->total_size = offset;
196 struct rte_port_in_action_profile {
197 struct ap_config cfg;
202 struct rte_port_in_action_profile *
203 rte_port_in_action_profile_create(uint32_t socket_id)
205 struct rte_port_in_action_profile *ap;
207 /* Memory allocation */
208 ap = rte_zmalloc_socket(NULL,
209 sizeof(struct rte_port_in_action_profile),
219 rte_port_in_action_profile_action_register(struct rte_port_in_action_profile *profile,
220 enum rte_port_in_action_type type,
225 /* Check input arguments */
226 if ((profile == NULL) ||
228 (action_valid(type) == 0) ||
229 (profile->cfg.action_mask & (1LLU << type)) ||
230 ((action_cfg_size(type) == 0) && action_config) ||
231 (action_cfg_size(type) && (action_config == NULL)))
235 case RTE_PORT_IN_ACTION_FLTR:
236 status = fltr_cfg_check(action_config);
239 case RTE_PORT_IN_ACTION_LB:
240 status = lb_cfg_check(action_config);
252 action_cfg_set(&profile->cfg, type, action_config);
258 rte_port_in_action_profile_freeze(struct rte_port_in_action_profile *profile)
263 action_data_offset_set(&profile->data, &profile->cfg);
270 rte_port_in_action_profile_free(struct rte_port_in_action_profile *profile)
282 struct rte_port_in_action {
283 struct ap_config cfg;
285 uint8_t memory[0] __rte_cache_aligned;
288 static __rte_always_inline void *
289 action_data_get(struct rte_port_in_action *action,
290 enum rte_port_in_action_type type)
292 size_t offset = action->data.offset[type];
294 return &action->memory[offset];
298 action_data_init(struct rte_port_in_action *action,
299 enum rte_port_in_action_type type)
301 void *data = action_data_get(action, type);
304 case RTE_PORT_IN_ACTION_FLTR:
305 fltr_init(data, &action->cfg.fltr);
308 case RTE_PORT_IN_ACTION_LB:
309 lb_init(data, &action->cfg.lb);
317 struct rte_port_in_action *
318 rte_port_in_action_create(struct rte_port_in_action_profile *profile,
321 struct rte_port_in_action *action;
325 /* Check input arguments */
326 if ((profile == NULL) ||
327 (profile->frozen == 0))
330 /* Memory allocation */
331 size = sizeof(struct rte_port_in_action) + profile->data.total_size;
332 size = RTE_CACHE_LINE_ROUNDUP(size);
334 action = rte_zmalloc_socket(NULL,
342 memcpy(&action->cfg, &profile->cfg, sizeof(profile->cfg));
343 memcpy(&action->data, &profile->data, sizeof(profile->data));
345 for (i = 0; i < RTE_PORT_IN_ACTION_MAX; i++)
346 if (action->cfg.action_mask & (1LLU << i))
347 action_data_init(action,
348 (enum rte_port_in_action_type)i);
354 rte_port_in_action_apply(struct rte_port_in_action *action,
355 enum rte_port_in_action_type type,
360 /* Check input arguments */
361 if ((action == NULL) ||
362 (action_valid(type) == 0) ||
363 ((action->cfg.action_mask & (1LLU << type)) == 0) ||
364 (action_params == NULL))
368 action_data = action_data_get(action, type);
371 case RTE_PORT_IN_ACTION_FLTR:
372 return fltr_apply(action_data,
375 case RTE_PORT_IN_ACTION_LB:
376 return lb_apply(action_data,
385 ah_filter_on_match(struct rte_pipeline *p,
386 struct rte_mbuf **pkts,
390 struct rte_port_in_action *action = arg;
391 struct rte_port_in_action_fltr_config *cfg = &action->cfg.fltr;
392 uint64_t *key_mask = (uint64_t *) cfg->key_mask;
393 uint64_t *key = (uint64_t *) cfg->key;
394 uint32_t key_offset = cfg->key_offset;
395 struct fltr_data *data = action_data_get(action,
396 RTE_PORT_IN_ACTION_FLTR);
399 for (i = 0; i < n_pkts; i++) {
400 struct rte_mbuf *pkt = pkts[i];
401 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(pkt,
404 uint64_t xor0 = (pkt_key[0] & key_mask[0]) ^ key[0];
405 uint64_t xor1 = (pkt_key[1] & key_mask[1]) ^ key[1];
406 uint64_t or = xor0 | xor1;
409 rte_pipeline_ah_packet_hijack(p, 1LLU << i);
410 rte_pipeline_port_out_packet_insert(p,
419 ah_filter_on_mismatch(struct rte_pipeline *p,
420 struct rte_mbuf **pkts,
424 struct rte_port_in_action *action = arg;
425 struct rte_port_in_action_fltr_config *cfg = &action->cfg.fltr;
426 uint64_t *key_mask = (uint64_t *) cfg->key_mask;
427 uint64_t *key = (uint64_t *) cfg->key;
428 uint32_t key_offset = cfg->key_offset;
429 struct fltr_data *data = action_data_get(action,
430 RTE_PORT_IN_ACTION_FLTR);
433 for (i = 0; i < n_pkts; i++) {
434 struct rte_mbuf *pkt = pkts[i];
435 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(pkt,
438 uint64_t xor0 = (pkt_key[0] & key_mask[0]) ^ key[0];
439 uint64_t xor1 = (pkt_key[1] & key_mask[1]) ^ key[1];
440 uint64_t or = xor0 | xor1;
443 rte_pipeline_ah_packet_hijack(p, 1LLU << i);
444 rte_pipeline_port_out_packet_insert(p,
453 ah_lb(struct rte_pipeline *p,
454 struct rte_mbuf **pkts,
458 struct rte_port_in_action *action = arg;
459 struct rte_port_in_action_lb_config *cfg = &action->cfg.lb;
460 struct lb_data *data = action_data_get(action, RTE_PORT_IN_ACTION_LB);
461 uint64_t pkt_mask = RTE_LEN2MASK(n_pkts, uint64_t);
464 rte_pipeline_ah_packet_hijack(p, pkt_mask);
466 for (i = 0; i < n_pkts; i++) {
467 struct rte_mbuf *pkt = pkts[i];
468 uint8_t *pkt_key = RTE_MBUF_METADATA_UINT8_PTR(pkt,
471 uint64_t digest = cfg->f_hash(pkt_key,
475 uint64_t pos = digest & (RTE_PORT_IN_ACTION_LB_TABLE_SIZE - 1);
476 uint32_t port_id = data->port_id[pos];
478 rte_pipeline_port_out_packet_insert(p, port_id, pkt);
484 static rte_pipeline_port_in_action_handler
485 ah_selector(struct rte_port_in_action *action)
487 if (action->cfg.action_mask == 0)
490 if (action->cfg.action_mask == 1LLU << RTE_PORT_IN_ACTION_FLTR)
491 return (action->cfg.fltr.filter_on_match) ?
492 ah_filter_on_match : ah_filter_on_mismatch;
494 if (action->cfg.action_mask == 1LLU << RTE_PORT_IN_ACTION_LB)
501 rte_port_in_action_params_get(struct rte_port_in_action *action,
502 struct rte_pipeline_port_in_params *params)
504 rte_pipeline_port_in_action_handler f_action;
506 /* Check input arguments */
507 if ((action == NULL) ||
511 f_action = ah_selector(action);
514 params->f_action = f_action;
515 params->arg_ah = (f_action) ? action : NULL;
521 rte_port_in_action_free(struct rte_port_in_action *action)