1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
11 #include <rte_common.h>
12 #include <rte_ethdev.h>
13 #include <cmdline_parse.h>
14 #include <cmdline_parse_string.h>
15 #include <cmdline_parse_num.h>
20 struct flex_item *flex_items[RTE_MAX_ETHPORTS][FLEX_MAX_PARSERS_NUM];
21 struct flex_pattern flex_patterns[FLEX_MAX_PATTERNS_NUM];
23 static struct flex_item *
24 flex_parser_fetch(uint16_t port_id, uint16_t flex_id)
26 if (port_id >= RTE_MAX_ETHPORTS) {
27 printf("Invalid port_id: %u\n", port_id);
28 return FLEX_PARSER_ERR;
30 if (flex_id >= FLEX_MAX_PARSERS_NUM) {
31 printf("Invalid flex item flex_id: %u\n", flex_id);
32 return FLEX_PARSER_ERR;
34 return flex_items[port_id][flex_id];
37 #ifdef RTE_HAS_JANSSON
38 static __rte_always_inline bool
39 match_strkey(const char *key, const char *pattern)
41 return strncmp(key, pattern, strlen(key)) == 0;
45 flex_tunnel_parse(json_t *jtun, enum rte_flow_item_flex_tunnel_mode *tunnel)
49 if (json_is_integer(jtun))
50 tun = (int)json_integer_value(jtun);
51 else if (json_is_real(jtun))
52 tun = (int)json_real_value(jtun);
53 else if (json_is_string(jtun)) {
54 const char *mode = json_string_value(jtun);
56 if (match_strkey(mode, "FLEX_TUNNEL_MODE_SINGLE"))
57 tun = FLEX_TUNNEL_MODE_SINGLE;
58 else if (match_strkey(mode, "FLEX_TUNNEL_MODE_OUTER"))
59 tun = FLEX_TUNNEL_MODE_OUTER;
60 else if (match_strkey(mode, "FLEX_TUNNEL_MODE_INNER"))
61 tun = FLEX_TUNNEL_MODE_INNER;
62 else if (match_strkey(mode, "FLEX_TUNNEL_MODE_MULTI"))
63 tun = FLEX_TUNNEL_MODE_MULTI;
64 else if (match_strkey(mode, "FLEX_TUNNEL_MODE_TUNNEL"))
65 tun = FLEX_TUNNEL_MODE_TUNNEL;
70 *tunnel = (enum rte_flow_item_flex_tunnel_mode)tun;
75 flex_field_parse(json_t *jfld, struct rte_flow_item_flex_field *fld)
80 #define FLEX_FIELD_GET(fm, t) \
82 if (!strncmp(key, # fm, strlen(# fm))) { \
83 if (json_is_real(je)) \
84 fld->fm = (t) json_real_value(je); \
85 else if (json_is_integer(je)) \
86 fld->fm = (t) json_integer_value(je); \
92 json_object_foreach(jfld, key, je) {
93 FLEX_FIELD_GET(field_size, uint32_t);
94 FLEX_FIELD_GET(field_base, int32_t);
95 FLEX_FIELD_GET(offset_base, uint32_t);
96 FLEX_FIELD_GET(offset_mask, uint32_t);
97 FLEX_FIELD_GET(offset_shift, int32_t);
98 FLEX_FIELD_GET(field_id, uint16_t);
99 if (match_strkey(key, "field_mode")) {
101 if (!json_is_string(je))
103 mode = json_string_value(je);
104 if (match_strkey(mode, "FIELD_MODE_DUMMY"))
105 fld->field_mode = FIELD_MODE_DUMMY;
106 else if (match_strkey(mode, "FIELD_MODE_FIXED"))
107 fld->field_mode = FIELD_MODE_FIXED;
108 else if (match_strkey(mode, "FIELD_MODE_OFFSET"))
109 fld->field_mode = FIELD_MODE_OFFSET;
110 else if (match_strkey(mode, "FIELD_MODE_BITMASK"))
111 fld->field_mode = FIELD_MODE_BITMASK;
119 enum flex_link_type {
125 flex_link_item_parse(const char *src, struct rte_flow_item *item)
127 #define FLEX_PARSE_DATA_SIZE 1024
130 uint8_t *ptr, data[FLEX_PARSE_DATA_SIZE] = {0,};
132 struct rte_flow_attr *attr;
133 struct rte_flow_item *pattern;
134 struct rte_flow_action *actions;
136 sprintf(flow_rule, "flow create 0 pattern %s / end", src);
138 ret = flow_parse(src, (void *)data, sizeof(data),
139 &attr, &pattern, &actions);
142 item->type = pattern->type;
144 ptr = (void *)(uintptr_t)item->spec;
145 memcpy(ptr, pattern->spec, FLEX_MAX_FLOW_PATTERN_LENGTH);
150 ptr = (void *)(uintptr_t)item->mask;
151 memcpy(ptr, pattern->mask, FLEX_MAX_FLOW_PATTERN_LENGTH);
156 ptr = (void *)(uintptr_t)item->last;
157 memcpy(ptr, pattern->last, FLEX_MAX_FLOW_PATTERN_LENGTH);
165 flex_link_parse(json_t *jobj, struct rte_flow_item_flex_link *link,
166 enum flex_link_type link_type)
171 json_object_foreach(jobj, key, je) {
172 if (match_strkey(key, "item")) {
173 if (!json_is_string(je))
175 ret = flex_link_item_parse(json_string_value(je),
179 if (link_type == FLEX_LINK_IN) {
180 if (!link->item.spec || !link->item.mask)
186 if (match_strkey(key, "next")) {
187 if (json_is_integer(je))
188 link->next = (typeof(link->next))
189 json_integer_value(je);
190 else if (json_is_real(je))
191 link->next = (typeof(link->next))
200 static int flex_item_config(json_t *jroot,
201 struct rte_flow_item_flex_conf *flex_conf)
207 json_object_foreach(jroot, key, jobj) {
208 if (match_strkey(key, "tunnel")) {
209 ret = flex_tunnel_parse(jobj, &flex_conf->tunnel);
211 printf("Can't parse tunnel value\n");
214 } else if (match_strkey(key, "next_header")) {
215 ret = flex_field_parse(jobj, &flex_conf->next_header);
217 printf("Can't parse next_header field\n");
220 } else if (match_strkey(key, "next_protocol")) {
221 ret = flex_field_parse(jobj,
222 &flex_conf->next_protocol);
224 printf("Can't parse next_protocol field\n");
227 } else if (match_strkey(key, "sample_data")) {
229 uint32_t i, size = json_array_size(jobj);
230 for (i = 0; i < size; i++) {
231 ji = json_array_get(jobj, i);
232 ret = flex_field_parse
233 (ji, flex_conf->sample_data + i);
235 printf("Can't parse sample_data field(s)\n");
239 flex_conf->nb_samples = size;
240 } else if (match_strkey(key, "input_link")) {
242 uint32_t i, size = json_array_size(jobj);
243 for (i = 0; i < size; i++) {
244 ji = json_array_get(jobj, i);
245 ret = flex_link_parse(ji,
246 flex_conf->input_link + i,
249 printf("Can't parse input_link(s)\n");
253 flex_conf->nb_inputs = size;
254 } else if (match_strkey(key, "output_link")) {
256 uint32_t i, size = json_array_size(jobj);
257 for (i = 0; i < size; i++) {
258 ji = json_array_get(jobj, i);
259 ret = flex_link_parse
260 (ji, flex_conf->output_link + i,
263 printf("Can't parse output_link(s)\n");
267 flex_conf->nb_outputs = size;
274 static struct flex_item *
277 size_t base_size, samples_size, links_size, spec_size;
278 struct rte_flow_item_flex_conf *conf;
279 struct flex_item *fp;
280 uint8_t (*pattern)[FLEX_MAX_FLOW_PATTERN_LENGTH];
283 base_size = RTE_ALIGN(sizeof(*conf), sizeof(uintptr_t));
284 samples_size = RTE_ALIGN(FLEX_ITEM_MAX_SAMPLES_NUM *
285 sizeof(conf->sample_data[0]),
287 links_size = RTE_ALIGN(FLEX_ITEM_MAX_LINKS_NUM *
288 sizeof(conf->input_link[0]),
290 /* spec & mask for all input links */
291 spec_size = 2 * FLEX_MAX_FLOW_PATTERN_LENGTH * FLEX_ITEM_MAX_LINKS_NUM;
292 fp = calloc(1, base_size + samples_size + 2 * links_size + spec_size);
294 printf("Can't allocate memory for flex item\n");
297 conf = &fp->flex_conf;
298 conf->sample_data = (typeof(conf->sample_data))
299 ((uint8_t *)fp + base_size);
300 conf->input_link = (typeof(conf->input_link))
301 ((uint8_t *)conf->sample_data + samples_size);
302 conf->output_link = (typeof(conf->output_link))
303 ((uint8_t *)conf->input_link + links_size);
304 pattern = (typeof(pattern))((uint8_t *)conf->output_link + links_size);
305 for (i = 0; i < FLEX_ITEM_MAX_LINKS_NUM; i++) {
306 struct rte_flow_item_flex_link *in = conf->input_link + i;
307 in->item.spec = pattern++;
308 in->item.mask = pattern++;
314 flex_item_build_config(struct flex_item *fp, const char *filename)
317 json_error_t json_error;
318 json_t *jroot = json_load_file(filename, 0, &json_error);
321 printf("Bad JSON file \"%s\": %s\n", filename, json_error.text);
324 ret = flex_item_config(jroot, &fp->flex_conf);
330 flex_item_create(portid_t port_id, uint16_t flex_id, const char *filename)
332 struct rte_flow_error flow_error;
333 struct flex_item *fp = flex_parser_fetch(port_id, flex_id);
336 if (fp == FLEX_PARSER_ERR) {
337 printf("Bad parameters: port_id=%u flex_id=%u\n",
342 printf("port-%u: flex item #%u is already in use\n",
346 fp = flex_item_init();
348 printf("Could not allocate flex item\n");
351 ret = flex_item_build_config(fp, filename);
354 fp->flex_handle = rte_flow_flex_item_create(port_id,
357 if (fp->flex_handle) {
358 flex_items[port_id][flex_id] = fp;
359 printf("port-%u: created flex item #%u\n", port_id, flex_id);
362 printf("port-%u: flex item #%u creation failed: %s\n",
364 flow_error.message ? flow_error.message : "");
370 #else /* RTE_HAS_JANSSON */
371 void flex_item_create(__rte_unused portid_t port_id,
372 __rte_unused uint16_t flex_id,
373 __rte_unused const char *filename)
375 printf("cannot create flex item - no JSON library configured\n");
377 #endif /* RTE_HAS_JANSSON */
380 flex_item_destroy(portid_t port_id, uint16_t flex_id)
383 struct rte_flow_error error;
384 struct flex_item *fp = flex_parser_fetch(port_id, flex_id);
387 if (fp == FLEX_PARSER_ERR) {
388 printf("Bad parameters: port_id=%u flex_id=%u\n",
394 ret = rte_flow_flex_item_release(port_id, fp->flex_handle, &error);
397 flex_items[port_id][flex_id] = NULL;
398 printf("port-%u: released flex item #%u\n",
402 printf("port-%u: cannot release flex item #%u: %s\n",
403 port_id, flex_id, error.message);
408 port_flex_item_flush(portid_t port_id)
412 for (i = 0; i < FLEX_MAX_PARSERS_NUM; i++) {
413 if (flex_items[port_id][i] != NULL) {
414 flex_item_destroy(port_id, i);
415 flex_items[port_id][i] = NULL;
420 struct flex_pattern_set {
421 cmdline_fixed_string_t set, flex_pattern;
422 cmdline_fixed_string_t is_spec, mask;
423 cmdline_fixed_string_t spec_data, mask_data;
427 static cmdline_parse_token_string_t flex_pattern_set_token =
428 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, set, "set");
429 static cmdline_parse_token_string_t flex_pattern_token =
430 TOKEN_STRING_INITIALIZER(struct flex_pattern_set,
431 flex_pattern, "flex_pattern");
432 static cmdline_parse_token_string_t flex_pattern_is_token =
433 TOKEN_STRING_INITIALIZER(struct flex_pattern_set,
435 static cmdline_parse_token_string_t flex_pattern_spec_token =
436 TOKEN_STRING_INITIALIZER(struct flex_pattern_set,
438 static cmdline_parse_token_string_t flex_pattern_mask_token =
439 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, mask, "mask");
440 static cmdline_parse_token_string_t flex_pattern_spec_data_token =
441 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, spec_data, NULL);
442 static cmdline_parse_token_string_t flex_pattern_mask_data_token =
443 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, mask_data, NULL);
444 static cmdline_parse_token_num_t flex_pattern_id_token =
445 TOKEN_NUM_INITIALIZER(struct flex_pattern_set, id, RTE_UINT16);
448 * flex pattern data - spec or mask is a string representation of byte array
449 * in hexadecimal format. Each byte in data string must have 2 characters:
452 * Bytes in data array are in network order.
455 flex_pattern_data(const char *str, uint8_t *data)
457 uint32_t i, len = strlen(str);
463 if (len >= FLEX_MAX_FLOW_PATTERN_LENGTH)
465 for (i = 0, b[2] = '\0'; i < len; i++) {
467 b[1] = str[2 * i + 1];
468 data[i] = strtoul(b, &endptr, 16);
476 flex_pattern_parsed_fn(void *parsed_result,
477 __rte_unused struct cmdline *cl,
478 __rte_unused void *data)
480 struct flex_pattern_set *res = parsed_result;
481 struct flex_pattern *fp;
484 if (res->id >= FLEX_MAX_PATTERNS_NUM) {
485 printf("Bad flex pattern id\n");
488 fp = flex_patterns + res->id;
489 memset(fp->spec_pattern, 0, sizeof(fp->spec_pattern));
490 memset(fp->mask_pattern, 0, sizeof(fp->mask_pattern));
491 fp->spec.length = flex_pattern_data(res->spec_data, fp->spec_pattern);
492 if (!fp->spec.length) {
493 printf("Bad flex pattern spec\n");
496 full_spec = strncmp(res->is_spec, "spec", strlen("spec")) == 0;
498 fp->mask.length = flex_pattern_data(res->mask_data,
500 if (!fp->mask.length) {
501 printf("Bad flex pattern mask\n");
505 memset(fp->mask_pattern, 0xFF, fp->spec.length);
506 fp->mask.length = fp->spec.length;
508 if (fp->mask.length != fp->spec.length) {
509 printf("Spec length do not match mask length\n");
512 fp->spec.pattern = fp->spec_pattern;
513 fp->mask.pattern = fp->mask_pattern;
514 printf("created pattern #%u\n", res->id);
517 cmdline_parse_inst_t cmd_set_flex_is_pattern = {
518 .f = flex_pattern_parsed_fn,
520 .help_str = "set flex_pattern <id> is <spec_data>",
522 (void *)&flex_pattern_set_token,
523 (void *)&flex_pattern_token,
524 (void *)&flex_pattern_id_token,
525 (void *)&flex_pattern_is_token,
526 (void *)&flex_pattern_spec_data_token,
531 cmdline_parse_inst_t cmd_set_flex_spec_pattern = {
532 .f = flex_pattern_parsed_fn,
534 .help_str = "set flex_pattern <id> spec <spec_data> mask <mask_data>",
536 (void *)&flex_pattern_set_token,
537 (void *)&flex_pattern_token,
538 (void *)&flex_pattern_id_token,
539 (void *)&flex_pattern_spec_token,
540 (void *)&flex_pattern_spec_data_token,
541 (void *)&flex_pattern_mask_token,
542 (void *)&flex_pattern_mask_data_token,