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;
137 "flow create 0 pattern %s / end actions drop / end", src);
139 ret = flow_parse(src, (void *)data, sizeof(data),
140 &attr, &pattern, &actions);
143 item->type = pattern->type;
145 ptr = (void *)(uintptr_t)item->spec;
146 memcpy(ptr, pattern->spec, FLEX_MAX_FLOW_PATTERN_LENGTH);
151 ptr = (void *)(uintptr_t)item->mask;
152 memcpy(ptr, pattern->mask, FLEX_MAX_FLOW_PATTERN_LENGTH);
157 ptr = (void *)(uintptr_t)item->last;
158 memcpy(ptr, pattern->last, FLEX_MAX_FLOW_PATTERN_LENGTH);
166 flex_link_parse(json_t *jobj, struct rte_flow_item_flex_link *link,
167 enum flex_link_type link_type)
172 json_object_foreach(jobj, key, je) {
173 if (match_strkey(key, "item")) {
174 if (!json_is_string(je))
176 ret = flex_link_item_parse(json_string_value(je),
180 if (link_type == FLEX_LINK_IN) {
181 if (!link->item.spec || !link->item.mask)
187 if (match_strkey(key, "next")) {
188 if (json_is_integer(je))
189 link->next = (typeof(link->next))
190 json_integer_value(je);
191 else if (json_is_real(je))
192 link->next = (typeof(link->next))
201 static int flex_item_config(json_t *jroot,
202 struct rte_flow_item_flex_conf *flex_conf)
208 json_object_foreach(jroot, key, jobj) {
209 if (match_strkey(key, "tunnel")) {
210 ret = flex_tunnel_parse(jobj, &flex_conf->tunnel);
212 printf("Can't parse tunnel value\n");
215 } else if (match_strkey(key, "next_header")) {
216 ret = flex_field_parse(jobj, &flex_conf->next_header);
218 printf("Can't parse next_header field\n");
221 } else if (match_strkey(key, "next_protocol")) {
222 ret = flex_field_parse(jobj,
223 &flex_conf->next_protocol);
225 printf("Can't parse next_protocol field\n");
228 } else if (match_strkey(key, "sample_data")) {
230 uint32_t i, size = json_array_size(jobj);
231 for (i = 0; i < size; i++) {
232 ji = json_array_get(jobj, i);
233 ret = flex_field_parse
234 (ji, flex_conf->sample_data + i);
236 printf("Can't parse sample_data field(s)\n");
240 flex_conf->nb_samples = size;
241 } else if (match_strkey(key, "input_link")) {
243 uint32_t i, size = json_array_size(jobj);
244 for (i = 0; i < size; i++) {
245 ji = json_array_get(jobj, i);
246 ret = flex_link_parse(ji,
247 flex_conf->input_link + i,
250 printf("Can't parse input_link(s)\n");
254 flex_conf->nb_inputs = size;
255 } else if (match_strkey(key, "output_link")) {
257 uint32_t i, size = json_array_size(jobj);
258 for (i = 0; i < size; i++) {
259 ji = json_array_get(jobj, i);
260 ret = flex_link_parse
261 (ji, flex_conf->output_link + i,
264 printf("Can't parse output_link(s)\n");
268 flex_conf->nb_outputs = size;
275 static struct flex_item *
278 size_t base_size, samples_size, links_size, spec_size;
279 struct rte_flow_item_flex_conf *conf;
280 struct flex_item *fp;
281 uint8_t (*pattern)[FLEX_MAX_FLOW_PATTERN_LENGTH];
284 base_size = RTE_ALIGN(sizeof(*conf), sizeof(uintptr_t));
285 samples_size = RTE_ALIGN(FLEX_ITEM_MAX_SAMPLES_NUM *
286 sizeof(conf->sample_data[0]),
288 links_size = RTE_ALIGN(FLEX_ITEM_MAX_LINKS_NUM *
289 sizeof(conf->input_link[0]),
291 /* spec & mask for all input links */
292 spec_size = 2 * FLEX_MAX_FLOW_PATTERN_LENGTH * FLEX_ITEM_MAX_LINKS_NUM;
293 fp = calloc(1, base_size + samples_size + 2 * links_size + spec_size);
295 printf("Can't allocate memory for flex item\n");
298 conf = &fp->flex_conf;
299 conf->sample_data = (typeof(conf->sample_data))
300 ((uint8_t *)fp + base_size);
301 conf->input_link = (typeof(conf->input_link))
302 ((uint8_t *)conf->sample_data + samples_size);
303 conf->output_link = (typeof(conf->output_link))
304 ((uint8_t *)conf->input_link + links_size);
305 pattern = (typeof(pattern))((uint8_t *)conf->output_link + links_size);
306 for (i = 0; i < FLEX_ITEM_MAX_LINKS_NUM; i++) {
307 struct rte_flow_item_flex_link *in = conf->input_link + i;
308 in->item.spec = pattern++;
309 in->item.mask = pattern++;
315 flex_item_build_config(struct flex_item *fp, const char *filename)
318 json_error_t json_error;
319 json_t *jroot = json_load_file(filename, 0, &json_error);
322 printf("Bad JSON file \"%s\": %s\n", filename, json_error.text);
325 ret = flex_item_config(jroot, &fp->flex_conf);
331 flex_item_create(portid_t port_id, uint16_t flex_id, const char *filename)
333 struct rte_flow_error flow_error;
334 struct flex_item *fp = flex_parser_fetch(port_id, flex_id);
337 if (fp == FLEX_PARSER_ERR) {
338 printf("Bad parameters: port_id=%u flex_id=%u\n",
343 printf("port-%u: flex item #%u is already in use\n",
347 fp = flex_item_init();
349 printf("Could not allocate flex item\n");
352 ret = flex_item_build_config(fp, filename);
355 fp->flex_handle = rte_flow_flex_item_create(port_id,
358 if (fp->flex_handle) {
359 flex_items[port_id][flex_id] = fp;
360 printf("port-%u: created flex item #%u\n", port_id, flex_id);
363 printf("port-%u: flex item #%u creation failed: %s\n",
365 flow_error.message ? flow_error.message : "");
371 #else /* RTE_HAS_JANSSON */
372 void flex_item_create(__rte_unused portid_t port_id,
373 __rte_unused uint16_t flex_id,
374 __rte_unused const char *filename)
376 printf("cannot create flex item - no JSON library configured\n");
378 #endif /* RTE_HAS_JANSSON */
381 flex_item_destroy(portid_t port_id, uint16_t flex_id)
384 struct rte_flow_error error;
385 struct flex_item *fp = flex_parser_fetch(port_id, flex_id);
388 if (fp == FLEX_PARSER_ERR) {
389 printf("Bad parameters: port_id=%u flex_id=%u\n",
395 ret = rte_flow_flex_item_release(port_id, fp->flex_handle, &error);
398 flex_items[port_id][flex_id] = NULL;
399 printf("port-%u: released flex item #%u\n",
403 printf("port-%u: cannot release flex item #%u: %s\n",
404 port_id, flex_id, error.message);
409 port_flex_item_flush(portid_t port_id)
413 for (i = 0; i < FLEX_MAX_PARSERS_NUM; i++) {
414 if (flex_items[port_id][i] != NULL) {
415 flex_item_destroy(port_id, i);
416 flex_items[port_id][i] = NULL;
421 struct flex_pattern_set {
422 cmdline_fixed_string_t set, flex_pattern;
423 cmdline_fixed_string_t is_spec, mask;
424 cmdline_fixed_string_t spec_data, mask_data;
428 static cmdline_parse_token_string_t flex_pattern_set_token =
429 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, set, "set");
430 static cmdline_parse_token_string_t flex_pattern_token =
431 TOKEN_STRING_INITIALIZER(struct flex_pattern_set,
432 flex_pattern, "flex_pattern");
433 static cmdline_parse_token_string_t flex_pattern_is_token =
434 TOKEN_STRING_INITIALIZER(struct flex_pattern_set,
436 static cmdline_parse_token_string_t flex_pattern_spec_token =
437 TOKEN_STRING_INITIALIZER(struct flex_pattern_set,
439 static cmdline_parse_token_string_t flex_pattern_mask_token =
440 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, mask, "mask");
441 static cmdline_parse_token_string_t flex_pattern_spec_data_token =
442 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, spec_data, NULL);
443 static cmdline_parse_token_string_t flex_pattern_mask_data_token =
444 TOKEN_STRING_INITIALIZER(struct flex_pattern_set, mask_data, NULL);
445 static cmdline_parse_token_num_t flex_pattern_id_token =
446 TOKEN_NUM_INITIALIZER(struct flex_pattern_set, id, RTE_UINT16);
449 * flex pattern data - spec or mask is a string representation of byte array
450 * in hexadecimal format. Each byte in data string must have 2 characters:
453 * Bytes in data array are in network order.
456 flex_pattern_data(const char *str, uint8_t *data)
458 uint32_t i, len = strlen(str);
464 if (len >= FLEX_MAX_FLOW_PATTERN_LENGTH)
466 for (i = 0, b[2] = '\0'; i < len; i++) {
468 b[1] = str[2 * i + 1];
469 data[i] = strtoul(b, &endptr, 16);
477 flex_pattern_parsed_fn(void *parsed_result,
478 __rte_unused struct cmdline *cl,
479 __rte_unused void *data)
481 struct flex_pattern_set *res = parsed_result;
482 struct flex_pattern *fp;
485 if (res->id >= FLEX_MAX_PATTERNS_NUM) {
486 printf("Bad flex pattern id\n");
489 fp = flex_patterns + res->id;
490 memset(fp->spec_pattern, 0, sizeof(fp->spec_pattern));
491 memset(fp->mask_pattern, 0, sizeof(fp->mask_pattern));
492 fp->spec.length = flex_pattern_data(res->spec_data, fp->spec_pattern);
493 if (!fp->spec.length) {
494 printf("Bad flex pattern spec\n");
497 full_spec = strncmp(res->is_spec, "spec", strlen("spec")) == 0;
499 fp->mask.length = flex_pattern_data(res->mask_data,
501 if (!fp->mask.length) {
502 printf("Bad flex pattern mask\n");
506 memset(fp->mask_pattern, 0xFF, fp->spec.length);
507 fp->mask.length = fp->spec.length;
509 if (fp->mask.length != fp->spec.length) {
510 printf("Spec length do not match mask length\n");
513 fp->spec.pattern = fp->spec_pattern;
514 fp->mask.pattern = fp->mask_pattern;
515 printf("created pattern #%u\n", res->id);
518 cmdline_parse_inst_t cmd_set_flex_is_pattern = {
519 .f = flex_pattern_parsed_fn,
521 .help_str = "set flex_pattern <id> is <spec_data>",
523 (void *)&flex_pattern_set_token,
524 (void *)&flex_pattern_token,
525 (void *)&flex_pattern_id_token,
526 (void *)&flex_pattern_is_token,
527 (void *)&flex_pattern_spec_data_token,
532 cmdline_parse_inst_t cmd_set_flex_spec_pattern = {
533 .f = flex_pattern_parsed_fn,
535 .help_str = "set flex_pattern <id> spec <spec_data> mask <mask_data>",
537 (void *)&flex_pattern_set_token,
538 (void *)&flex_pattern_token,
539 (void *)&flex_pattern_id_token,
540 (void *)&flex_pattern_spec_token,
541 (void *)&flex_pattern_spec_data_token,
542 (void *)&flex_pattern_mask_token,
543 (void *)&flex_pattern_mask_data_token,