raw/ifpga: fix file handle leak
[dpdk.git] / examples / pipeline / cli.c
index 83b460c..ad553f1 100644 (file)
@@ -1346,7 +1346,7 @@ cmd_pipeline_table_default(char **tokens,
 }
 
 static const char cmd_pipeline_table_show_help[] =
-"pipeline <pipeline_name> table <table_name> show\n";
+"pipeline <pipeline_name> table <table_name> show [filename]\n";
 
 static void
 cmd_pipeline_table_show(char **tokens,
@@ -1357,9 +1357,10 @@ cmd_pipeline_table_show(char **tokens,
 {
        struct pipeline *p;
        char *pipeline_name, *table_name;
+       FILE *file = NULL;
        int status;
 
-       if (n_tokens != 5) {
+       if (n_tokens != 5 && n_tokens != 6) {
                snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
                return;
        }
@@ -1372,9 +1373,18 @@ cmd_pipeline_table_show(char **tokens,
        }
 
        table_name = tokens[3];
-       status = rte_swx_ctl_pipeline_table_fprintf(stdout, p->ctl, table_name);
+       file = (n_tokens == 6) ? fopen(tokens[5], "w") : stdout;
+       if (!file) {
+               snprintf(out, out_size, "Cannot open file %s.\n", tokens[5]);
+               return;
+       }
+
+       status = rte_swx_ctl_pipeline_table_fprintf(file, p->ctl, table_name);
        if (status)
                snprintf(out, out_size, MSG_ARG_INVALID, "table_name");
+
+       if (file)
+               fclose(file);
 }
 
 static const char cmd_pipeline_selector_group_add_help[] =
@@ -1806,7 +1816,7 @@ cmd_pipeline_selector_group_member_delete(char **tokens,
 }
 
 static const char cmd_pipeline_selector_show_help[] =
-"pipeline <pipeline_name> selector <selector_name> show\n";
+"pipeline <pipeline_name> selector <selector_name> show [filename]\n";
 
 static void
 cmd_pipeline_selector_show(char **tokens,
@@ -1817,9 +1827,10 @@ cmd_pipeline_selector_show(char **tokens,
 {
        struct pipeline *p;
        char *pipeline_name, *selector_name;
+       FILE *file = NULL;
        int status;
 
-       if (n_tokens != 5) {
+       if (n_tokens != 5 && n_tokens != 6) {
                snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
                return;
        }
@@ -1832,10 +1843,19 @@ cmd_pipeline_selector_show(char **tokens,
        }
 
        selector_name = tokens[3];
-       status = rte_swx_ctl_pipeline_selector_fprintf(stdout,
-               p->ctl, selector_name);
+
+       file = (n_tokens == 6) ? fopen(tokens[5], "w") : stdout;
+       if (!file) {
+               snprintf(out, out_size, "Cannot open file %s.\n", tokens[5]);
+               return;
+       }
+
+       status = rte_swx_ctl_pipeline_selector_fprintf(file, p->ctl, selector_name);
        if (status)
                snprintf(out, out_size, MSG_ARG_INVALID, "selector_name");
+
+       if (file)
+               fclose(file);
 }
 
 static int
@@ -2551,10 +2571,25 @@ cmd_pipeline_stats(char **tokens,
 
                rte_swx_ctl_pipeline_port_out_stats_read(p->p, i, &stats);
 
-               snprintf(out, out_size, "\tPort %u:"
+               if (i != info.n_ports_out - 1)
+                       snprintf(out, out_size, "\tPort %u:", i);
+               else
+                       snprintf(out, out_size, "\tDROP:");
+
+               out_size -= strlen(out);
+               out += strlen(out);
+
+               snprintf(out,
+                       out_size,
                        " packets %" PRIu64
-                       " bytes %" PRIu64 "\n",
-                       i, stats.n_pkts, stats.n_bytes);
+                       " bytes %" PRIu64
+                       " clone %" PRIu64
+                       " clonerr %" PRIu64 "\n",
+                       stats.n_pkts,
+                       stats.n_bytes,
+                       stats.n_pkts_clone,
+                       stats.n_pkts_clone_err);
+
                out_size -= strlen(out);
                out += strlen(out);
        }
@@ -2642,12 +2677,14 @@ cmd_pipeline_stats(char **tokens,
                        "\t\tMiss (packets): %" PRIu64 "\n"
                        "\t\tLearn OK (packets): %" PRIu64 "\n"
                        "\t\tLearn error (packets): %" PRIu64 "\n"
+                       "\t\tRearm (packets): %" PRIu64 "\n"
                        "\t\tForget (packets): %" PRIu64 "\n",
                        learner_info.name,
                        stats.n_pkts_hit,
                        stats.n_pkts_miss,
                        stats.n_pkts_learn_ok,
                        stats.n_pkts_learn_err,
+                       stats.n_pkts_rearm,
                        stats.n_pkts_forget);
                out_size -= strlen(out);
                out += strlen(out);
@@ -2670,6 +2707,156 @@ cmd_pipeline_stats(char **tokens,
        }
 }
 
+static const char cmd_pipeline_mirror_help[] =
+"pipeline <pipeline_name> mirror slots <n_slots> sessions <n_sessions>\n";
+
+static void
+cmd_pipeline_mirror(char **tokens,
+       uint32_t n_tokens,
+       char *out,
+       size_t out_size,
+       void *obj)
+{
+       struct rte_swx_pipeline_mirroring_params params;
+       struct pipeline *p;
+       int status;
+
+       if (n_tokens != 7) {
+               snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
+               return;
+       }
+
+       if (strcmp(tokens[0], "pipeline")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipeline");
+               return;
+       }
+
+       p = pipeline_find(obj, tokens[1]);
+       if (!p) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
+               return;
+       }
+
+       if (strcmp(tokens[2], "mirror")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mirror");
+               return;
+       }
+
+       if (strcmp(tokens[3], "slots")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "slots");
+               return;
+       }
+
+       if (parser_read_uint32(&params.n_slots, tokens[4])) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "n_slots");
+               return;
+       }
+
+       if (strcmp(tokens[5], "sessions")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "sessions");
+               return;
+       }
+
+       if (parser_read_uint32(&params.n_sessions, tokens[6])) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "n_sessions");
+               return;
+       }
+
+       status = rte_swx_pipeline_mirroring_config(p->p, &params);
+       if (status) {
+               snprintf(out, out_size, "Command failed!\n");
+               return;
+       }
+}
+
+static const char cmd_pipeline_mirror_session_help[] =
+"pipeline <pipeline_name> mirror session <session_id> port <port_id> clone fast | slow "
+"truncate <truncation_length>\n";
+
+static void
+cmd_pipeline_mirror_session(char **tokens,
+       uint32_t n_tokens,
+       char *out,
+       size_t out_size,
+       void *obj)
+{
+       struct rte_swx_pipeline_mirroring_session_params params;
+       struct pipeline *p;
+       uint32_t session_id = 0;
+       int status;
+
+       if (n_tokens != 11) {
+               snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
+               return;
+       }
+
+       if (strcmp(tokens[0], "pipeline")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipeline");
+               return;
+       }
+
+       p = pipeline_find(obj, tokens[1]);
+       if (!p || !p->ctl) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
+               return;
+       }
+
+       if (strcmp(tokens[2], "mirror")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mirror");
+               return;
+       }
+
+       if (strcmp(tokens[3], "session")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "session");
+               return;
+       }
+
+       if (parser_read_uint32(&session_id, tokens[4])) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "session_id");
+               return;
+       }
+
+       if (strcmp(tokens[5], "port")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "port");
+               return;
+       }
+
+       if (parser_read_uint32(&params.port_id, tokens[6])) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
+               return;
+       }
+
+       if (strcmp(tokens[7], "clone")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "clone");
+               return;
+       }
+
+       if (!strcmp(tokens[8], "fast"))
+               params.fast_clone = 1;
+       else if (!strcmp(tokens[8], "slow"))
+               params.fast_clone = 0;
+       else {
+               snprintf(out, out_size, MSG_ARG_INVALID, "clone");
+               return;
+       }
+
+       if (strcmp(tokens[9], "truncate")) {
+               snprintf(out, out_size, MSG_ARG_NOT_FOUND, "truncate");
+               return;
+       }
+
+       if (parser_read_uint32(&params.truncation_length, tokens[10])) {
+               snprintf(out, out_size, MSG_ARG_INVALID, "truncation_length");
+               return;
+       }
+
+       status = rte_swx_ctl_pipeline_mirroring_session_set(p->p, session_id, &params);
+       if (status) {
+               snprintf(out, out_size, "Command failed!\n");
+               return;
+       }
+}
+
 static const char cmd_thread_pipeline_enable_help[] =
 "thread <thread_id> pipeline <pipeline_name> enable\n";
 
@@ -2810,6 +2997,8 @@ cmd_help(char **tokens,
                        "\tpipeline meter set\n"
                        "\tpipeline meter stats\n"
                        "\tpipeline stats\n"
+                       "\tpipeline mirror\n"
+                       "\tpipeline mirror session\n"
                        "\tthread pipeline enable\n"
                        "\tthread pipeline disable\n\n");
                return;
@@ -3029,6 +3218,19 @@ cmd_help(char **tokens,
                return;
        }
 
+       if (!strcmp(tokens[0], "pipeline") &&
+               (n_tokens == 2) && !strcmp(tokens[1], "mirror")) {
+               snprintf(out, out_size, "\n%s\n", cmd_pipeline_mirror_help);
+               return;
+       }
+
+       if (!strcmp(tokens[0], "pipeline") &&
+               (n_tokens == 3) && !strcmp(tokens[1], "mirror")
+               && !strcmp(tokens[2], "session")) {
+               snprintf(out, out_size, "\n%s\n", cmd_pipeline_mirror_session_help);
+               return;
+       }
+
        if ((n_tokens == 3) &&
                (strcmp(tokens[0], "thread") == 0) &&
                (strcmp(tokens[1], "pipeline") == 0)) {
@@ -3283,6 +3485,20 @@ cli_process(char *in, char *out, size_t out_size, void *obj)
                                obj);
                        return;
                }
+
+               if ((n_tokens >= 4) &&
+                       (strcmp(tokens[2], "mirror") == 0) &&
+                       (strcmp(tokens[3], "slots") == 0)) {
+                       cmd_pipeline_mirror(tokens, n_tokens, out, out_size, obj);
+                       return;
+               }
+
+               if ((n_tokens >= 4) &&
+                       (strcmp(tokens[2], "mirror") == 0) &&
+                       (strcmp(tokens[3], "session") == 0)) {
+                       cmd_pipeline_mirror_session(tokens, n_tokens, out, out_size, obj);
+                       return;
+               }
        }
 
        if (strcmp(tokens[0], "thread") == 0) {