support systemd service convention for runtime directory
[dpdk.git] / app / test-flow-perf / main.c
index 42d13ec..56d4373 100644 (file)
@@ -37,6 +37,7 @@
 #include <rte_mtr.h>
 
 #include "config.h"
+#include "actions_gen.h"
 #include "flow_gen.h"
 
 #define MAX_BATCHES_COUNT          100
@@ -49,10 +50,13 @@ static uint8_t flow_group;
 
 static uint64_t encap_data;
 static uint64_t decap_data;
+static uint64_t all_actions[RTE_COLORS][MAX_ACTIONS_NUM];
+static char *actions_str[RTE_COLORS];
 
 static uint64_t flow_items[MAX_ITEMS_NUM];
 static uint64_t flow_actions[MAX_ACTIONS_NUM];
 static uint64_t flow_attrs[MAX_ATTRS_NUM];
+static uint32_t policy_id[MAX_PORTS];
 static uint8_t items_idx, actions_idx, attrs_idx;
 
 static uint64_t ports_mask;
@@ -63,6 +67,8 @@ static bool delete_flag;
 static bool dump_socket_mem_flag;
 static bool enable_fwd;
 static bool unique_data;
+static bool policy_mtr;
+static bool packet_mode;
 
 static uint8_t rx_queues_count;
 static uint8_t tx_queues_count;
@@ -80,6 +86,7 @@ static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
 static uint32_t nb_lcores;
 static uint8_t max_priority;
 static uint32_t rand_seed;
+static uint64_t meter_profile_values[3]; /* CIR CBS EBS values. */
 
 #define MAX_PKT_BURST    32
 #define LCORE_MODE_PKT    1
@@ -479,6 +486,10 @@ usage(char *progname)
                "and S as seed for pseudo-random number generator\n");
        printf("  --unique-data: flag to set using unique data for all"
                " actions that support data, such as header modify and encap actions\n");
+       printf("  --meter-profile=cir,cbs,ebs: set CIR CBS EBS parameters in meter"
+               " profile, default values are %d,%d,%d\n", METER_CIR,
+               METER_CIR / 8, 0);
+       printf("  --packet-mode: to enable packet mode for meter profile\n");
 
        printf("To set flow attributes:\n");
        printf("  --ingress: set ingress attribute in flows\n");
@@ -560,6 +571,8 @@ usage(char *progname)
                "ipv6 dscp value to be set is random each flow\n");
        printf("  --flag: add flag action to flow actions\n");
        printf("  --meter: add meter action to flow actions\n");
+       printf("  --policy-mtr=\"g1,g2:y1:r1\": to create meter with specified "
+               "colored actions\n");
        printf("  --raw-encap=<data>: add raw encap action to flow actions\n"
                "Data is the data needed to be encaped\n"
                "Example: raw-encap=ether,ipv4,udp,vxlan\n");
@@ -572,6 +585,45 @@ usage(char *progname)
        printf("  --vxlan-decap: add vxlan_decap action to flow actions\n");
 }
 
+static void
+read_meter_policy(char *prog, char *arg)
+{
+       char *token;
+       size_t i, j, k;
+
+       j = 0;
+       k = 0;
+       policy_mtr = true;
+       token = strsep(&arg, ":\0");
+       while (token != NULL && j < RTE_COLORS) {
+               actions_str[j++] = token;
+               token = strsep(&arg, ":\0");
+       }
+       j = 0;
+       token = strtok(actions_str[0], ",\0");
+       while (token == NULL && j < RTE_COLORS - 1)
+               token = strtok(actions_str[++j], ",\0");
+       while (j < RTE_COLORS && token != NULL) {
+               for (i = 0; i < RTE_DIM(flow_options); i++) {
+                       if (!strcmp(token, flow_options[i].str)) {
+                               all_actions[j][k++] = flow_options[i].mask;
+                               break;
+                       }
+               }
+               /* Reached last action with no match */
+               if (i >= RTE_DIM(flow_options)) {
+                       fprintf(stderr, "Invalid colored actions: %s\n", token);
+                       usage(prog);
+                       rte_exit(EXIT_SUCCESS, "Invalid colored actions\n");
+               }
+               token = strtok(NULL, ",\0");
+               while (!token && j < RTE_COLORS - 1) {
+                       token = strtok(actions_str[++j], ",\0");
+                       k = 0;
+               }
+       }
+}
+
 static void
 args_parse(int argc, char **argv)
 {
@@ -660,6 +712,10 @@ args_parse(int argc, char **argv)
                { "raw-decap",                  1, 0, 0 },
                { "vxlan-encap",                0, 0, 0 },
                { "vxlan-decap",                0, 0, 0 },
+               { "policy-mtr",                 1, 0, 0 },
+               { "meter-profile",              1, 0, 0 },
+               { "packet-mode",                0, 0, 0 },
+               { 0, 0, 0, 0 },
        };
 
        RTE_ETH_FOREACH_DEV(i)
@@ -877,11 +933,26 @@ args_parse(int argc, char **argv)
                                                RTE_MAX_LCORE);
                                }
                        }
+                       if (strcmp(lgopts[opt_idx].name, "policy-mtr") == 0)
+                               read_meter_policy(argv[0], optarg);
+                       if (strcmp(lgopts[opt_idx].name,
+                                               "meter-profile") == 0) {
+                               i = 0;
+                               token = strsep(&optarg, ",\0");
+                               while (token != NULL && i < sizeof(
+                                               meter_profile_values) /
+                                               sizeof(uint64_t)) {
+                                       meter_profile_values[i++] = atol(token);
+                                       token = strsep(&optarg, ",\0");
+                               }
+                       }
+                       if (strcmp(lgopts[opt_idx].name, "packet-mode") == 0)
+                               packet_mode = true;
                        break;
                default:
                        usage(argv[0]);
                        rte_exit(EXIT_FAILURE, "Invalid option: %s\n",
-                                       argv[optind]);
+                                       argv[optind - 1]);
                        break;
                }
        }
@@ -981,7 +1052,6 @@ print_rules_batches(double *cpu_time_per_batch)
        }
 }
 
-
 static inline int
 has_meter(void)
 {
@@ -997,12 +1067,66 @@ has_meter(void)
        return 0;
 }
 
+static void
+create_meter_policy(void)
+{
+       struct rte_mtr_error error;
+       int ret, port_id;
+       struct rte_mtr_meter_policy_params policy;
+       uint16_t nr_ports;
+       struct rte_flow_action actions[RTE_COLORS][MAX_ACTIONS_NUM];
+       int i;
+
+       memset(actions, 0, sizeof(actions));
+       memset(&policy, 0, sizeof(policy));
+       nr_ports = rte_eth_dev_count_avail();
+       for (port_id = 0; port_id < nr_ports; port_id++) {
+               for (i = 0; i < RTE_COLORS; i++)
+                       fill_actions(actions[i], all_actions[i], 0, 0, 0,
+                                    0, 0, 0, unique_data, rx_queues_count,
+                                    dst_ports[port_id]);
+               policy.actions[RTE_COLOR_GREEN] = actions[RTE_COLOR_GREEN];
+               policy.actions[RTE_COLOR_YELLOW] = actions[RTE_COLOR_YELLOW];
+               policy.actions[RTE_COLOR_RED] = actions[RTE_COLOR_RED];
+               policy_id[port_id] = port_id + 10;
+               ret = rte_mtr_meter_policy_add(port_id, policy_id[port_id],
+                                              &policy, &error);
+               if (ret) {
+                       fprintf(stderr, "port %d: failed to create meter policy\n",
+                               port_id);
+                       policy_id[port_id] = UINT32_MAX;
+               }
+               memset(actions, 0, sizeof(actions));
+       }
+}
+
+static void
+destroy_meter_policy(void)
+{
+       struct rte_mtr_error error;
+       uint16_t nr_ports;
+       int port_id;
+
+       nr_ports = rte_eth_dev_count_avail();
+       for (port_id = 0; port_id < nr_ports; port_id++) {
+               /* If port outside portmask */
+               if (!((ports_mask >> port_id) & 0x1))
+                       continue;
+
+               if (rte_mtr_meter_policy_delete
+                       (port_id, policy_id[port_id], &error)) {
+                       fprintf(stderr, "port %u:  failed to  delete meter policy\n",
+                               port_id);
+                       rte_exit(EXIT_FAILURE, "Error: Failed to delete meter policy.\n");
+               }
+       }
+}
+
 static void
 create_meter_rule(int port_id, uint32_t counter)
 {
        int ret;
        struct rte_mtr_params params;
-       uint32_t default_prof_id = 100;
        struct rte_mtr_error error;
 
        memset(&params, 0, sizeof(struct rte_mtr_params));
@@ -1012,8 +1136,15 @@ create_meter_rule(int port_id, uint32_t counter)
        params.dscp_table = NULL;
 
        /*create meter*/
-       params.meter_profile_id = default_prof_id;
-       ret = rte_mtr_create(port_id, counter, &params, 1, &error);
+       params.meter_profile_id = DEFAULT_METER_PROF_ID;
+
+       if (!policy_mtr) {
+               ret = rte_mtr_create(port_id, counter, &params, 1, &error);
+       } else {
+               params.meter_policy_id = policy_id[port_id];
+               ret = rte_mtr_create(port_id, counter, &params, 0, &error);
+       }
+
        if (ret != 0) {
                printf("Port %u create meter idx(%d) error(%d) message: %s\n",
                        port_id, counter, error.type,
@@ -1027,11 +1158,16 @@ destroy_meter_rule(int port_id, uint32_t counter)
 {
        struct rte_mtr_error error;
 
+       if (policy_mtr && policy_id[port_id] != UINT32_MAX) {
+               if (rte_mtr_meter_policy_delete(port_id, policy_id[port_id],
+                                       &error))
+                       fprintf(stderr, "Error: Failed to delete meter policy\n");
+               policy_id[port_id] = UINT32_MAX;
+       }
        if (rte_mtr_destroy(port_id, counter, &error)) {
-               printf("Port %u destroy meter(%d) error(%d) message: %s\n",
-                       port_id, counter, error.type,
-                       error.message ? error.message : "(no stated reason)");
-               rte_exit(EXIT_FAILURE, "Error in deleting meter rule\n");
+               fprintf(stderr, "Port %d: Failed to delete meter.\n",
+                               port_id);
+               rte_exit(EXIT_FAILURE, "Error in deleting meter rule");
        }
 }
 
@@ -1136,12 +1272,13 @@ create_meter_profile(void)
                /* If port outside portmask */
                if (!((ports_mask >> port_id) & 0x1))
                        continue;
-
                mp.alg = RTE_MTR_SRTCM_RFC2697;
-               mp.srtcm_rfc2697.cir = METER_CIR;
-               mp.srtcm_rfc2697.cbs = METER_CIR / 8;
-               mp.srtcm_rfc2697.ebs = 0;
-
+               mp.srtcm_rfc2697.cir = meter_profile_values[0] ?
+                       meter_profile_values[0] : METER_CIR;
+               mp.srtcm_rfc2697.cbs = meter_profile_values[1] ?
+                       meter_profile_values[1] : METER_CIR / 8;
+               mp.srtcm_rfc2697.ebs = meter_profile_values[2];
+               mp.packet_mode = packet_mode;
                ret = rte_mtr_meter_profile_add
                        (port_id, DEFAULT_METER_PROF_ID, &mp, &error);
                if (ret != 0) {
@@ -1382,7 +1519,7 @@ dump_used_cpu_time(const char *item,
         * threads time.
         *
         * Throughput: total count of rte rules divided
-        * over the average of the time cosumed by all
+        * over the average of the time consumed by all
         * threads time.
         */
        double insertion_latency_time;
@@ -2012,16 +2149,22 @@ main(int argc, char **argv)
 
        rte_srand(rand_seed);
 
-       if (has_meter())
+       if (has_meter()) {
                create_meter_profile();
+               if (policy_mtr)
+                       create_meter_policy();
+       }
        rte_eal_mp_remote_launch(run_rte_flow_handler_cores, NULL, CALL_MAIN);
 
        if (enable_fwd) {
                init_lcore_info();
                rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
        }
-       if (has_meter() && delete_flag)
+       if (has_meter() && delete_flag) {
                destroy_meter_profile();
+               if (policy_mtr)
+                       destroy_meter_policy();
+       }
 
        RTE_ETH_FOREACH_DEV(port) {
                rte_flow_flush(port, &error);