ethdev: add flow API actions to modify IP addresses
[dpdk.git] / app / test-pmd / cmdline_flow.c
index f4ea0a5..1432498 100644 (file)
@@ -194,6 +194,8 @@ enum index {
        ACTION_QUEUE_INDEX,
        ACTION_DROP,
        ACTION_COUNT,
+       ACTION_COUNT_SHARED,
+       ACTION_COUNT_ID,
        ACTION_RSS,
        ACTION_RSS_FUNC,
        ACTION_RSS_LEVEL,
@@ -237,6 +239,18 @@ enum index {
        ACTION_OF_POP_MPLS_ETHERTYPE,
        ACTION_OF_PUSH_MPLS,
        ACTION_OF_PUSH_MPLS_ETHERTYPE,
+       ACTION_VXLAN_ENCAP,
+       ACTION_VXLAN_DECAP,
+       ACTION_NVGRE_ENCAP,
+       ACTION_NVGRE_DECAP,
+       ACTION_SET_IPV4_SRC,
+       ACTION_SET_IPV4_SRC_IPV4_SRC,
+       ACTION_SET_IPV4_DST,
+       ACTION_SET_IPV4_DST_IPV4_DST,
+       ACTION_SET_IPV6_SRC,
+       ACTION_SET_IPV6_SRC_IPV6_SRC,
+       ACTION_SET_IPV6_DST,
+       ACTION_SET_IPV6_DST_IPV6_DST,
 };
 
 /** Maximum size for pattern in struct rte_flow_item_raw. */
@@ -256,6 +270,39 @@ struct action_rss_data {
        uint16_t queue[ACTION_RSS_QUEUE_NUM];
 };
 
+/** Maximum number of items in struct rte_flow_action_vxlan_encap. */
+#define ACTION_VXLAN_ENCAP_ITEMS_NUM 6
+
+/** Storage for struct rte_flow_action_vxlan_encap including external data. */
+struct action_vxlan_encap_data {
+       struct rte_flow_action_vxlan_encap conf;
+       struct rte_flow_item items[ACTION_VXLAN_ENCAP_ITEMS_NUM];
+       struct rte_flow_item_eth item_eth;
+       struct rte_flow_item_vlan item_vlan;
+       union {
+               struct rte_flow_item_ipv4 item_ipv4;
+               struct rte_flow_item_ipv6 item_ipv6;
+       };
+       struct rte_flow_item_udp item_udp;
+       struct rte_flow_item_vxlan item_vxlan;
+};
+
+/** Maximum number of items in struct rte_flow_action_nvgre_encap. */
+#define ACTION_NVGRE_ENCAP_ITEMS_NUM 5
+
+/** Storage for struct rte_flow_action_nvgre_encap including external data. */
+struct action_nvgre_encap_data {
+       struct rte_flow_action_nvgre_encap conf;
+       struct rte_flow_item items[ACTION_NVGRE_ENCAP_ITEMS_NUM];
+       struct rte_flow_item_eth item_eth;
+       struct rte_flow_item_vlan item_vlan;
+       union {
+               struct rte_flow_item_ipv4 item_ipv4;
+               struct rte_flow_item_ipv6 item_ipv6;
+       };
+       struct rte_flow_item_nvgre item_nvgre;
+};
+
 /** Maximum number of subsequent tokens and arguments on the stack. */
 #define CTX_STACK_SIZE 16
 
@@ -773,6 +820,14 @@ static const enum index next_action[] = {
        ACTION_OF_SET_VLAN_PCP,
        ACTION_OF_POP_MPLS,
        ACTION_OF_PUSH_MPLS,
+       ACTION_VXLAN_ENCAP,
+       ACTION_VXLAN_DECAP,
+       ACTION_NVGRE_ENCAP,
+       ACTION_NVGRE_DECAP,
+       ACTION_SET_IPV4_SRC,
+       ACTION_SET_IPV4_DST,
+       ACTION_SET_IPV6_SRC,
+       ACTION_SET_IPV6_DST,
        ZERO,
 };
 
@@ -788,6 +843,13 @@ static const enum index action_queue[] = {
        ZERO,
 };
 
+static const enum index action_count[] = {
+       ACTION_COUNT_ID,
+       ACTION_COUNT_SHARED,
+       ACTION_NEXT,
+       ZERO,
+};
+
 static const enum index action_rss[] = {
        ACTION_RSS_FUNC,
        ACTION_RSS_LEVEL,
@@ -868,6 +930,30 @@ static const enum index action_of_push_mpls[] = {
        ZERO,
 };
 
+static const enum index action_set_ipv4_src[] = {
+       ACTION_SET_IPV4_SRC_IPV4_SRC,
+       ACTION_NEXT,
+       ZERO,
+};
+
+static const enum index action_set_ipv4_dst[] = {
+       ACTION_SET_IPV4_DST_IPV4_DST,
+       ACTION_NEXT,
+       ZERO,
+};
+
+static const enum index action_set_ipv6_src[] = {
+       ACTION_SET_IPV6_SRC_IPV6_SRC,
+       ACTION_NEXT,
+       ZERO,
+};
+
+static const enum index action_set_ipv6_dst[] = {
+       ACTION_SET_IPV6_DST_IPV6_DST,
+       ACTION_NEXT,
+       ZERO,
+};
+
 static const enum index action_jump[] = {
        ACTION_JUMP_GROUP,
        ACTION_NEXT,
@@ -896,6 +982,12 @@ static int parse_vc_action_rss_type(struct context *, const struct token *,
 static int parse_vc_action_rss_queue(struct context *, const struct token *,
                                     const char *, unsigned int, void *,
                                     unsigned int);
+static int parse_vc_action_vxlan_encap(struct context *, const struct token *,
+                                      const char *, unsigned int, void *,
+                                      unsigned int);
+static int parse_vc_action_nvgre_encap(struct context *, const struct token *,
+                                      const char *, unsigned int, void *,
+                                      unsigned int);
 static int parse_destroy(struct context *, const struct token *,
                         const char *, unsigned int,
                         void *, unsigned int);
@@ -2022,10 +2114,26 @@ static const struct token token_list[] = {
        [ACTION_COUNT] = {
                .name = "count",
                .help = "enable counters for this rule",
-               .priv = PRIV_ACTION(COUNT, 0),
-               .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
+               .priv = PRIV_ACTION(COUNT,
+                                   sizeof(struct rte_flow_action_count)),
+               .next = NEXT(action_count),
                .call = parse_vc,
        },
+       [ACTION_COUNT_ID] = {
+               .name = "identifier",
+               .help = "counter identifier to use",
+               .next = NEXT(action_count, NEXT_ENTRY(UNSIGNED)),
+               .args = ARGS(ARGS_ENTRY(struct rte_flow_action_count, id)),
+               .call = parse_vc_conf,
+       },
+       [ACTION_COUNT_SHARED] = {
+               .name = "shared",
+               .help = "shared counter",
+               .next = NEXT(action_count, NEXT_ENTRY(BOOLEAN)),
+               .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_count,
+                                          shared, 1)),
+               .call = parse_vc_conf,
+       },
        [ACTION_RSS] = {
                .name = "rss",
                .help = "spread packets among several queues",
@@ -2362,6 +2470,110 @@ static const struct token token_list[] = {
                              ethertype)),
                .call = parse_vc_conf,
        },
+       [ACTION_VXLAN_ENCAP] = {
+               .name = "vxlan_encap",
+               .help = "VXLAN encapsulation, uses configuration set by \"set"
+                       " vxlan\"",
+               .priv = PRIV_ACTION(VXLAN_ENCAP,
+                                   sizeof(struct action_vxlan_encap_data)),
+               .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
+               .call = parse_vc_action_vxlan_encap,
+       },
+       [ACTION_VXLAN_DECAP] = {
+               .name = "vxlan_decap",
+               .help = "Performs a decapsulation action by stripping all"
+                       " headers of the VXLAN tunnel network overlay from the"
+                       " matched flow.",
+               .priv = PRIV_ACTION(VXLAN_DECAP, 0),
+               .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
+               .call = parse_vc,
+       },
+       [ACTION_NVGRE_ENCAP] = {
+               .name = "nvgre_encap",
+               .help = "NVGRE encapsulation, uses configuration set by \"set"
+                       " nvgre\"",
+               .priv = PRIV_ACTION(NVGRE_ENCAP,
+                                   sizeof(struct action_nvgre_encap_data)),
+               .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
+               .call = parse_vc_action_nvgre_encap,
+       },
+       [ACTION_NVGRE_DECAP] = {
+               .name = "nvgre_decap",
+               .help = "Performs a decapsulation action by stripping all"
+                       " headers of the NVGRE tunnel network overlay from the"
+                       " matched flow.",
+               .priv = PRIV_ACTION(NVGRE_DECAP, 0),
+               .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
+               .call = parse_vc,
+       },
+       [ACTION_SET_IPV4_SRC] = {
+               .name = "set_ipv4_src",
+               .help = "Set a new IPv4 source address in the outermost"
+                       " IPv4 header",
+               .priv = PRIV_ACTION(SET_IPV4_SRC,
+                       sizeof(struct rte_flow_action_set_ipv4)),
+               .next = NEXT(action_set_ipv4_src),
+               .call = parse_vc,
+       },
+       [ACTION_SET_IPV4_SRC_IPV4_SRC] = {
+               .name = "ipv4_addr",
+               .help = "new IPv4 source address to set",
+               .next = NEXT(action_set_ipv4_src, NEXT_ENTRY(IPV4_ADDR)),
+               .args = ARGS(ARGS_ENTRY_HTON
+                       (struct rte_flow_action_set_ipv4, ipv4_addr)),
+               .call = parse_vc_conf,
+       },
+       [ACTION_SET_IPV4_DST] = {
+               .name = "set_ipv4_dst",
+               .help = "Set a new IPv4 destination address in the outermost"
+                       " IPv4 header",
+               .priv = PRIV_ACTION(SET_IPV4_DST,
+                       sizeof(struct rte_flow_action_set_ipv4)),
+               .next = NEXT(action_set_ipv4_dst),
+               .call = parse_vc,
+       },
+       [ACTION_SET_IPV4_DST_IPV4_DST] = {
+               .name = "ipv4_addr",
+               .help = "new IPv4 destination address to set",
+               .next = NEXT(action_set_ipv4_dst, NEXT_ENTRY(IPV4_ADDR)),
+               .args = ARGS(ARGS_ENTRY_HTON
+                       (struct rte_flow_action_set_ipv4, ipv4_addr)),
+               .call = parse_vc_conf,
+       },
+       [ACTION_SET_IPV6_SRC] = {
+               .name = "set_ipv6_src",
+               .help = "Set a new IPv6 source address in the outermost"
+                       " IPv6 header",
+               .priv = PRIV_ACTION(SET_IPV6_SRC,
+                       sizeof(struct rte_flow_action_set_ipv6)),
+               .next = NEXT(action_set_ipv6_src),
+               .call = parse_vc,
+       },
+       [ACTION_SET_IPV6_SRC_IPV6_SRC] = {
+               .name = "ipv6_addr",
+               .help = "new IPv6 source address to set",
+               .next = NEXT(action_set_ipv6_src, NEXT_ENTRY(IPV6_ADDR)),
+               .args = ARGS(ARGS_ENTRY_HTON
+                       (struct rte_flow_action_set_ipv6, ipv6_addr)),
+               .call = parse_vc_conf,
+       },
+       [ACTION_SET_IPV6_DST] = {
+               .name = "set_ipv6_dst",
+               .help = "Set a new IPv6 destination address in the outermost"
+                       " IPv6 header",
+               .priv = PRIV_ACTION(SET_IPV6_DST,
+                       sizeof(struct rte_flow_action_set_ipv6)),
+               .next = NEXT(action_set_ipv6_dst),
+               .call = parse_vc,
+       },
+       [ACTION_SET_IPV6_DST_IPV6_DST] = {
+               .name = "ipv6_addr",
+               .help = "new IPv6 destination address to set",
+               .next = NEXT(action_set_ipv6_dst, NEXT_ENTRY(IPV6_ADDR)),
+               .args = ARGS(ARGS_ENTRY_HTON
+                       (struct rte_flow_action_set_ipv6, ipv6_addr)),
+               .call = parse_vc_conf,
+       },
 };
 
 /** Remove and return last entry from argument stack. */
@@ -2772,7 +2984,8 @@ parse_vc_action_rss(struct context *ctx, const struct token *token,
                        .key = action_rss_data->key,
                        .queue = action_rss_data->queue,
                },
-               .key = "testpmd's default RSS hash key",
+               .key = "testpmd's default RSS hash key, "
+                       "override it for better balancing",
                .queue = { 0 },
        };
        for (i = 0; i < action_rss_data->conf.queue_num; ++i)
@@ -2925,6 +3138,197 @@ end:
        return len;
 }
 
+/** Parse VXLAN encap action. */
+static int
+parse_vc_action_vxlan_encap(struct context *ctx, const struct token *token,
+                           const char *str, unsigned int len,
+                           void *buf, unsigned int size)
+{
+       struct buffer *out = buf;
+       struct rte_flow_action *action;
+       struct action_vxlan_encap_data *action_vxlan_encap_data;
+       int ret;
+
+       ret = parse_vc(ctx, token, str, len, buf, size);
+       if (ret < 0)
+               return ret;
+       /* Nothing else to do if there is no buffer. */
+       if (!out)
+               return ret;
+       if (!out->args.vc.actions_n)
+               return -1;
+       action = &out->args.vc.actions[out->args.vc.actions_n - 1];
+       /* Point to selected object. */
+       ctx->object = out->args.vc.data;
+       ctx->objmask = NULL;
+       /* Set up default configuration. */
+       action_vxlan_encap_data = ctx->object;
+       *action_vxlan_encap_data = (struct action_vxlan_encap_data){
+               .conf = (struct rte_flow_action_vxlan_encap){
+                       .definition = action_vxlan_encap_data->items,
+               },
+               .items = {
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_ETH,
+                               .spec = &action_vxlan_encap_data->item_eth,
+                               .mask = &rte_flow_item_eth_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_VLAN,
+                               .spec = &action_vxlan_encap_data->item_vlan,
+                               .mask = &rte_flow_item_vlan_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_IPV4,
+                               .spec = &action_vxlan_encap_data->item_ipv4,
+                               .mask = &rte_flow_item_ipv4_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_UDP,
+                               .spec = &action_vxlan_encap_data->item_udp,
+                               .mask = &rte_flow_item_udp_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_VXLAN,
+                               .spec = &action_vxlan_encap_data->item_vxlan,
+                               .mask = &rte_flow_item_vxlan_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_END,
+                       },
+               },
+               .item_eth.type = 0,
+               .item_vlan = {
+                       .tci = vxlan_encap_conf.vlan_tci,
+                       .inner_type = 0,
+               },
+               .item_ipv4.hdr = {
+                       .src_addr = vxlan_encap_conf.ipv4_src,
+                       .dst_addr = vxlan_encap_conf.ipv4_dst,
+               },
+               .item_udp.hdr = {
+                       .src_port = vxlan_encap_conf.udp_src,
+                       .dst_port = vxlan_encap_conf.udp_dst,
+               },
+               .item_vxlan.flags = 0,
+       };
+       memcpy(action_vxlan_encap_data->item_eth.dst.addr_bytes,
+              vxlan_encap_conf.eth_dst, ETHER_ADDR_LEN);
+       memcpy(action_vxlan_encap_data->item_eth.src.addr_bytes,
+              vxlan_encap_conf.eth_src, ETHER_ADDR_LEN);
+       if (!vxlan_encap_conf.select_ipv4) {
+               memcpy(&action_vxlan_encap_data->item_ipv6.hdr.src_addr,
+                      &vxlan_encap_conf.ipv6_src,
+                      sizeof(vxlan_encap_conf.ipv6_src));
+               memcpy(&action_vxlan_encap_data->item_ipv6.hdr.dst_addr,
+                      &vxlan_encap_conf.ipv6_dst,
+                      sizeof(vxlan_encap_conf.ipv6_dst));
+               action_vxlan_encap_data->items[2] = (struct rte_flow_item){
+                       .type = RTE_FLOW_ITEM_TYPE_IPV6,
+                       .spec = &action_vxlan_encap_data->item_ipv6,
+                       .mask = &rte_flow_item_ipv6_mask,
+               };
+       }
+       if (!vxlan_encap_conf.select_vlan)
+               action_vxlan_encap_data->items[1].type =
+                       RTE_FLOW_ITEM_TYPE_VOID;
+       memcpy(action_vxlan_encap_data->item_vxlan.vni, vxlan_encap_conf.vni,
+              RTE_DIM(vxlan_encap_conf.vni));
+       action->conf = &action_vxlan_encap_data->conf;
+       return ret;
+}
+
+/** Parse NVGRE encap action. */
+static int
+parse_vc_action_nvgre_encap(struct context *ctx, const struct token *token,
+                           const char *str, unsigned int len,
+                           void *buf, unsigned int size)
+{
+       struct buffer *out = buf;
+       struct rte_flow_action *action;
+       struct action_nvgre_encap_data *action_nvgre_encap_data;
+       int ret;
+
+       ret = parse_vc(ctx, token, str, len, buf, size);
+       if (ret < 0)
+               return ret;
+       /* Nothing else to do if there is no buffer. */
+       if (!out)
+               return ret;
+       if (!out->args.vc.actions_n)
+               return -1;
+       action = &out->args.vc.actions[out->args.vc.actions_n - 1];
+       /* Point to selected object. */
+       ctx->object = out->args.vc.data;
+       ctx->objmask = NULL;
+       /* Set up default configuration. */
+       action_nvgre_encap_data = ctx->object;
+       *action_nvgre_encap_data = (struct action_nvgre_encap_data){
+               .conf = (struct rte_flow_action_nvgre_encap){
+                       .definition = action_nvgre_encap_data->items,
+               },
+               .items = {
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_ETH,
+                               .spec = &action_nvgre_encap_data->item_eth,
+                               .mask = &rte_flow_item_eth_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_VLAN,
+                               .spec = &action_nvgre_encap_data->item_vlan,
+                               .mask = &rte_flow_item_vlan_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_IPV4,
+                               .spec = &action_nvgre_encap_data->item_ipv4,
+                               .mask = &rte_flow_item_ipv4_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_NVGRE,
+                               .spec = &action_nvgre_encap_data->item_nvgre,
+                               .mask = &rte_flow_item_nvgre_mask,
+                       },
+                       {
+                               .type = RTE_FLOW_ITEM_TYPE_END,
+                       },
+               },
+               .item_eth.type = 0,
+               .item_vlan = {
+                       .tci = nvgre_encap_conf.vlan_tci,
+                       .inner_type = 0,
+               },
+               .item_ipv4.hdr = {
+                      .src_addr = nvgre_encap_conf.ipv4_src,
+                      .dst_addr = nvgre_encap_conf.ipv4_dst,
+               },
+               .item_nvgre.flow_id = 0,
+       };
+       memcpy(action_nvgre_encap_data->item_eth.dst.addr_bytes,
+              nvgre_encap_conf.eth_dst, ETHER_ADDR_LEN);
+       memcpy(action_nvgre_encap_data->item_eth.src.addr_bytes,
+              nvgre_encap_conf.eth_src, ETHER_ADDR_LEN);
+       if (!nvgre_encap_conf.select_ipv4) {
+               memcpy(&action_nvgre_encap_data->item_ipv6.hdr.src_addr,
+                      &nvgre_encap_conf.ipv6_src,
+                      sizeof(nvgre_encap_conf.ipv6_src));
+               memcpy(&action_nvgre_encap_data->item_ipv6.hdr.dst_addr,
+                      &nvgre_encap_conf.ipv6_dst,
+                      sizeof(nvgre_encap_conf.ipv6_dst));
+               action_nvgre_encap_data->items[2] = (struct rte_flow_item){
+                       .type = RTE_FLOW_ITEM_TYPE_IPV6,
+                       .spec = &action_nvgre_encap_data->item_ipv6,
+                       .mask = &rte_flow_item_ipv6_mask,
+               };
+       }
+       if (!nvgre_encap_conf.select_vlan)
+               action_nvgre_encap_data->items[1].type =
+                       RTE_FLOW_ITEM_TYPE_VOID;
+       memcpy(action_nvgre_encap_data->item_nvgre.tni, nvgre_encap_conf.tni,
+              RTE_DIM(nvgre_encap_conf.tni));
+       action->conf = &action_nvgre_encap_data->conf;
+       return ret;
+}
+
 /** Parse tokens for destroy command. */
 static int
 parse_destroy(struct context *ctx, const struct token *token,