From 3186282f8e12dad3e1b1b8b287a9d62d31886c01 Mon Sep 17 00:00:00 2001 From: Jasvinder Singh Date: Thu, 29 Mar 2018 19:31:56 +0100 Subject: [PATCH] examples/ip_pipeline: add table bulk add command Add cli for adding bulk entries to pipeline table. Signed-off-by: Cristian Dumitrescu Signed-off-by: Jasvinder Singh Signed-off-by: Kevin Laatz --- examples/ip_pipeline/cli.c | 267 +++++++++++++++++++++++++++++-- examples/ip_pipeline/pipeline.h | 8 + examples/ip_pipeline/thread.c | 272 +++++++++++++++++++++++++++++++- 3 files changed, 536 insertions(+), 11 deletions(-) diff --git a/examples/ip_pipeline/cli.c b/examples/ip_pipeline/cli.c index 9fd7f32760..27388baac5 100644 --- a/examples/ip_pipeline/cli.c +++ b/examples/ip_pipeline/cli.c @@ -25,16 +25,17 @@ #define CMD_MAX_TOKENS 256 #endif -#define MSG_OUT_OF_MEMORY "Not enough memory.\n" -#define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n" -#define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n" -#define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n" -#define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n" -#define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n" -#define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n" -#define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n" -#define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n" -#define MSG_CMD_FAIL "Command \"%s\" failed.\n" +#define MSG_OUT_OF_MEMORY "Not enough memory.\n" +#define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n" +#define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n" +#define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n" +#define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n" +#define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n" +#define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n" +#define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n" +#define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n" +#define MSG_FILE_NOT_ENOUGH "Not enough rules in file \"%s\".\n" +#define MSG_CMD_FAIL "Command \"%s\" failed.\n" static int is_comment(char *in) @@ -3539,6 +3540,133 @@ cmd_pipeline_table_rule_add_default(char **tokens, } } +/** + * pipeline table rule add bulk + * + * File : + * - line format: match action + */ +static int +cli_rule_file_process(const char *file_name, + size_t line_len_max, + struct table_rule_match *m, + struct table_rule_action *a, + uint32_t *n_rules, + uint32_t *line_number, + char *out, + size_t out_size); + +static void +cmd_pipeline_table_rule_add_bulk(char **tokens, + uint32_t n_tokens, + char *out, + size_t out_size) +{ + struct table_rule_match *match; + struct table_rule_action *action; + void **data; + char *pipeline_name, *file_name; + uint32_t table_id, n_rules, n_rules_parsed, line_number; + int status; + + if (n_tokens != 9) { + snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]); + return; + } + + pipeline_name = tokens[1]; + + if (strcmp(tokens[2], "table") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "table"); + return; + } + + if (parser_read_uint32(&table_id, tokens[3]) != 0) { + snprintf(out, out_size, MSG_ARG_INVALID, "table_id"); + return; + } + + if (strcmp(tokens[4], "rule") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rule"); + return; + } + + if (strcmp(tokens[5], "add") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "add"); + return; + } + + if (strcmp(tokens[6], "bulk") != 0) { + snprintf(out, out_size, MSG_ARG_INVALID, "bulk"); + return; + } + + file_name = tokens[7]; + + if ((parser_read_uint32(&n_rules, tokens[8]) != 0) || + (n_rules == 0)) { + snprintf(out, out_size, MSG_ARG_INVALID, "n_rules"); + return; + } + + /* Memory allocation. */ + match = calloc(n_rules, sizeof(struct table_rule_match)); + action = calloc(n_rules, sizeof(struct table_rule_action)); + data = calloc(n_rules, sizeof(void *)); + if ((match == NULL) || (action == NULL) || (data == NULL)) { + snprintf(out, out_size, MSG_OUT_OF_MEMORY); + free(data); + free(action); + free(match); + return; + } + + /* Load rule file */ + n_rules_parsed = n_rules; + status = cli_rule_file_process(file_name, + 1024, + match, + action, + &n_rules_parsed, + &line_number, + out, + out_size); + if (status) { + snprintf(out, out_size, MSG_FILE_ERR, file_name, line_number); + free(data); + free(action); + free(match); + return; + } + if (n_rules_parsed != n_rules) { + snprintf(out, out_size, MSG_FILE_NOT_ENOUGH, file_name); + free(data); + free(action); + free(match); + return; + } + + /* Rule bulk add */ + status = pipeline_table_rule_add_bulk(pipeline_name, + table_id, + match, + action, + data, + &n_rules); + if (status) { + snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]); + free(data); + free(action); + free(match); + return; + } + + /* Memory free */ + free(data); + free(action); + free(match); +} + /** * pipeline table rule delete * match @@ -3940,6 +4068,16 @@ cli_process(char *in, char *out, size_t out_size) return; } + if ((n_tokens >= 7) && + (strcmp(tokens[2], "table") == 0) && + (strcmp(tokens[4], "rule") == 0) && + (strcmp(tokens[5], "add") == 0) && + (strcmp(tokens[6], "bulk") == 0)) { + cmd_pipeline_table_rule_add_bulk(tokens, + n_tokens, out, out_size); + return; + } + if ((n_tokens >= 7) && (strcmp(tokens[2], "table") == 0) && (strcmp(tokens[4], "rule") == 0) && @@ -4031,3 +4169,112 @@ cli_script_process(const char *file_name, free(msg_in); return 0; } + +static int +cli_rule_file_process(const char *file_name, + size_t line_len_max, + struct table_rule_match *m, + struct table_rule_action *a, + uint32_t *n_rules, + uint32_t *line_number, + char *out, + size_t out_size) +{ + FILE *f = NULL; + char *line = NULL; + uint32_t rule_id, line_id; + int status = 0; + + /* Check input arguments */ + if ((file_name == NULL) || + (strlen(file_name) == 0) || + (line_len_max == 0)) { + *line_number = 0; + return -EINVAL; + } + + /* Memory allocation */ + line = malloc(line_len_max + 1); + if (line == NULL) { + *line_number = 0; + return -ENOMEM; + } + + /* Open file */ + f = fopen(file_name, "r"); + if (f == NULL) { + *line_number = 0; + free(line); + return -EIO; + } + + /* Read file */ + for (line_id = 1, rule_id = 0; rule_id < *n_rules; line_id++) { + char *tokens[CMD_MAX_TOKENS]; + uint32_t n_tokens, n_tokens_parsed, t0; + + /* Read next line from file. */ + if (fgets(line, line_len_max + 1, f) == NULL) + break; + + /* Comment. */ + if (is_comment(line)) + continue; + + /* Parse line. */ + n_tokens = RTE_DIM(tokens); + status = parse_tokenize_string(line, tokens, &n_tokens); + if (status) { + status = -EINVAL; + break; + } + + /* Empty line. */ + if (n_tokens == 0) + continue; + t0 = 0; + + /* Rule match. */ + n_tokens_parsed = parse_match(tokens + t0, + n_tokens - t0, + out, + out_size, + &m[rule_id]); + if (n_tokens_parsed == 0) { + status = -EINVAL; + break; + } + t0 += n_tokens_parsed; + + /* Rule action. */ + n_tokens_parsed = parse_table_action(tokens + t0, + n_tokens - t0, + out, + out_size, + &a[rule_id]); + if (n_tokens_parsed == 0) { + status = -EINVAL; + break; + } + t0 += n_tokens_parsed; + + /* Line completed. */ + if (t0 < n_tokens) { + status = -EINVAL; + break; + } + + /* Increment rule count */ + rule_id++; + } + + /* Close file */ + fclose(f); + + /* Memory free */ + free(line); + + *n_rules = rule_id; + *line_number = line_id; + return status; +} diff --git a/examples/ip_pipeline/pipeline.h b/examples/ip_pipeline/pipeline.h index 4fc7b279c0..37d1c7afc7 100644 --- a/examples/ip_pipeline/pipeline.h +++ b/examples/ip_pipeline/pipeline.h @@ -302,6 +302,14 @@ pipeline_table_rule_add(const char *pipeline_name, struct table_rule_action *action, void **data); +int +pipeline_table_rule_add_bulk(const char *pipeline_name, + uint32_t table_id, + struct table_rule_match *match, + struct table_rule_action *action, + void **data, + uint32_t *n_rules); + int pipeline_table_rule_add_default(const char *pipeline_name, uint32_t table_id, diff --git a/examples/ip_pipeline/thread.c b/examples/ip_pipeline/thread.c index 2532e095c3..3f5e15e6a3 100644 --- a/examples/ip_pipeline/thread.c +++ b/examples/ip_pipeline/thread.c @@ -490,9 +490,9 @@ enum pipeline_req_type { PIPELINE_REQ_TABLE_STATS_READ, PIPELINE_REQ_TABLE_RULE_ADD, PIPELINE_REQ_TABLE_RULE_ADD_DEFAULT, + PIPELINE_REQ_TABLE_RULE_ADD_BULK, PIPELINE_REQ_TABLE_RULE_DELETE, PIPELINE_REQ_TABLE_RULE_DELETE_DEFAULT, - PIPELINE_REQ_MAX }; @@ -517,6 +517,14 @@ struct pipeline_msg_req_table_rule_add_default { struct table_rule_action action; }; +struct pipeline_msg_req_table_rule_add_bulk { + struct table_rule_match *match; + struct table_rule_action *action; + void **data; + uint32_t n_rules; + int bulk; +}; + struct pipeline_msg_req_table_rule_delete { struct table_rule_match match; }; @@ -532,6 +540,7 @@ struct pipeline_msg_req { struct pipeline_msg_req_table_stats_read table_stats_read; struct pipeline_msg_req_table_rule_add table_rule_add; struct pipeline_msg_req_table_rule_add_default table_rule_add_default; + struct pipeline_msg_req_table_rule_add_bulk table_rule_add_bulk; struct pipeline_msg_req_table_rule_delete table_rule_delete; }; }; @@ -556,6 +565,10 @@ struct pipeline_msg_rsp_table_rule_add_default { void *data; }; +struct pipeline_msg_rsp_table_rule_add_bulk { + uint32_t n_rules; +}; + struct pipeline_msg_rsp { int status; @@ -566,6 +579,7 @@ struct pipeline_msg_rsp { struct pipeline_msg_rsp_table_stats_read table_stats_read; struct pipeline_msg_rsp_table_rule_add table_rule_add; struct pipeline_msg_rsp_table_rule_add_default table_rule_add_default; + struct pipeline_msg_rsp_table_rule_add_bulk table_rule_add_bulk; }; }; @@ -1103,6 +1117,71 @@ pipeline_table_rule_add_default(const char *pipeline_name, return status; } +int +pipeline_table_rule_add_bulk(const char *pipeline_name, + uint32_t table_id, + struct table_rule_match *match, + struct table_rule_action *action, + void **data, + uint32_t *n_rules) +{ + struct pipeline *p; + struct pipeline_msg_req *req; + struct pipeline_msg_rsp *rsp; + uint32_t i; + int status; + + /* Check input params */ + if ((pipeline_name == NULL) || + (match == NULL) || + (action == NULL) || + (data == NULL) || + (n_rules == NULL) || + (*n_rules == 0)) + return -1; + + p = pipeline_find(pipeline_name); + if ((p == NULL) || + (p->enabled == 0) || + (table_id >= p->n_tables)) + return -1; + + for (i = 0; i < *n_rules; i++) + if (match_check(match, p, table_id) || + action_check(action, p, table_id)) + return -1; + + /* Allocate request */ + req = pipeline_msg_alloc(); + if (req == NULL) + return -1; + + /* Write request */ + req->type = PIPELINE_REQ_TABLE_RULE_ADD_BULK; + req->id = table_id; + req->table_rule_add_bulk.match = match; + req->table_rule_add_bulk.action = action; + req->table_rule_add_bulk.data = data; + req->table_rule_add_bulk.n_rules = *n_rules; + req->table_rule_add_bulk.bulk = + (p->table[table_id].params.match_type == TABLE_ACL) ? 1 : 0; + + /* Send request and wait for response */ + rsp = pipeline_msg_send_recv(p, req); + if (rsp == NULL) + return -1; + + /* Read response */ + status = rsp->status; + if (status == 0) + *n_rules = rsp->table_rule_add_bulk.n_rules; + + /* Free response */ + pipeline_msg_free(rsp); + + return status; +} + int pipeline_table_rule_delete(const char *pipeline_name, uint32_t table_id, @@ -1747,6 +1826,193 @@ pipeline_msg_handle_table_rule_add_default(struct pipeline_data *p, return rsp; } +static struct pipeline_msg_rsp * +pipeline_msg_handle_table_rule_add_bulk(struct pipeline_data *p, + struct pipeline_msg_req *req) +{ + + struct pipeline_msg_rsp *rsp = (struct pipeline_msg_rsp *) req; + + uint32_t table_id = req->id; + struct table_rule_match *match = req->table_rule_add_bulk.match; + struct table_rule_action *action = req->table_rule_add_bulk.action; + struct rte_pipeline_table_entry **data = + (struct rte_pipeline_table_entry **)req->table_rule_add_bulk.data; + uint32_t n_rules = req->table_rule_add_bulk.n_rules; + uint32_t bulk = req->table_rule_add_bulk.bulk; + + struct rte_table_action *a = p->table_data[table_id].a; + union table_rule_match_low_level *match_ll; + uint8_t *action_ll; + void **match_ll_ptr; + struct rte_pipeline_table_entry **action_ll_ptr; + int *found, status; + uint32_t i; + + /* Memory allocation */ + match_ll = calloc(n_rules, sizeof(union table_rule_match_low_level)); + action_ll = calloc(n_rules, TABLE_RULE_ACTION_SIZE_MAX); + match_ll_ptr = calloc(n_rules, sizeof(void *)); + action_ll_ptr = + calloc(n_rules, sizeof(struct rte_pipeline_table_entry *)); + found = calloc(n_rules, sizeof(int)); + + if ((match_ll == NULL) || + (action_ll == NULL) || + (match_ll_ptr == NULL) || + (action_ll_ptr == NULL) || + (found == NULL)) + goto fail; + + for (i = 0; i < n_rules; i++) { + match_ll_ptr[i] = (void *)&match_ll[i]; + action_ll_ptr[i] = + (struct rte_pipeline_table_entry *)&action_ll[i * TABLE_RULE_ACTION_SIZE_MAX]; + } + + /* Rule match conversion */ + for (i = 0; i < n_rules; i++) { + status = match_convert(&match[i], match_ll_ptr[i], 1); + if (status) + goto fail; + } + + /* Rule action conversion */ + for (i = 0; i < n_rules; i++) { + void *data_in = action_ll_ptr[i]; + struct table_rule_action *act = &action[i]; + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_FWD)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_FWD, + &act->fwd); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_MTR, + &act->mtr); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_TM, + &act->tm); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_ENCAP, + &act->encap); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_NAT, + &act->nat); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_TTL, + &act->ttl); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_STATS, + &act->stats); + + if (status) + goto fail; + } + + if (act->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) { + status = rte_table_action_apply(a, + data_in, + RTE_TABLE_ACTION_TIME, + &act->time); + + if (status) + goto fail; + } + } + + /* Add rule (match, action) to table */ + if (bulk) { + status = rte_pipeline_table_entry_add_bulk(p->p, + table_id, + match_ll_ptr, + action_ll_ptr, + n_rules, + found, + data); + if (status) + n_rules = 0; + } else + for (i = 0; i < n_rules; i++) { + status = rte_pipeline_table_entry_add(p->p, + table_id, + match_ll_ptr[i], + action_ll_ptr[i], + &found[i], + &data[i]); + if (status) { + n_rules = i; + break; + } + } + + /* Write response */ + rsp->status = 0; + rsp->table_rule_add_bulk.n_rules = n_rules; + + /* Free */ + free(found); + free(action_ll_ptr); + free(match_ll_ptr); + free(action_ll); + free(match_ll); + + return rsp; + +fail: + free(found); + free(action_ll_ptr); + free(match_ll_ptr); + free(action_ll); + free(match_ll); + + rsp->status = -1; + rsp->table_rule_add_bulk.n_rules = 0; + return rsp; +} + static struct pipeline_msg_rsp * pipeline_msg_handle_table_rule_delete(struct pipeline_data *p, struct pipeline_msg_req *req) @@ -1826,6 +2092,10 @@ pipeline_msg_handle(struct pipeline_data *p) rsp = pipeline_msg_handle_table_rule_add_default(p, req); break; + case PIPELINE_REQ_TABLE_RULE_ADD_BULK: + rsp = pipeline_msg_handle_table_rule_add_bulk(p, req); + break; + case PIPELINE_REQ_TABLE_RULE_DELETE: rsp = pipeline_msg_handle_table_rule_delete(p, req); break; -- 2.20.1