app/testpmd: use ethdev iterator to list devices
[dpdk.git] / app / test-pmd / cmdline_flow.c
index a4e8ebe..4e99f0f 100644 (file)
@@ -39,6 +39,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <arpa/inet.h>
+#include <sys/socket.h>
 
 #include <rte_common.h>
 #include <rte_ethdev.h>
@@ -126,10 +127,20 @@ enum index {
        ITEM_VLAN,
        ITEM_VLAN_TPID,
        ITEM_VLAN_TCI,
+       ITEM_VLAN_PCP,
+       ITEM_VLAN_DEI,
+       ITEM_VLAN_VID,
        ITEM_IPV4,
+       ITEM_IPV4_TOS,
+       ITEM_IPV4_TTL,
+       ITEM_IPV4_PROTO,
        ITEM_IPV4_SRC,
        ITEM_IPV4_DST,
        ITEM_IPV6,
+       ITEM_IPV6_TC,
+       ITEM_IPV6_FLOW,
+       ITEM_IPV6_PROTO,
+       ITEM_IPV6_HOP,
        ITEM_IPV6_SRC,
        ITEM_IPV6_DST,
        ITEM_ICMP,
@@ -144,8 +155,14 @@ enum index {
        ITEM_SCTP,
        ITEM_SCTP_SRC,
        ITEM_SCTP_DST,
+       ITEM_SCTP_TAG,
+       ITEM_SCTP_CKSUM,
        ITEM_VXLAN,
        ITEM_VXLAN_VNI,
+       ITEM_MPLS,
+       ITEM_MPLS_LABEL,
+       ITEM_GRE,
+       ITEM_GRE_PROTO,
 
        /* Validate/create actions. */
        ACTIONS,
@@ -156,8 +173,15 @@ enum index {
        ACTION_MARK,
        ACTION_MARK_ID,
        ACTION_FLAG,
+       ACTION_QUEUE,
+       ACTION_QUEUE_INDEX,
        ACTION_DROP,
        ACTION_COUNT,
+       ACTION_DUP,
+       ACTION_DUP_INDEX,
+       ACTION_RSS,
+       ACTION_RSS_QUEUES,
+       ACTION_RSS_QUEUE,
        ACTION_PF,
        ACTION_VF,
        ACTION_VF_ORIGINAL,
@@ -171,6 +195,14 @@ enum index {
 #define ITEM_RAW_SIZE \
        (offsetof(struct rte_flow_item_raw, pattern) + ITEM_RAW_PATTERN_SIZE)
 
+/** Number of queue[] entries in struct rte_flow_action_rss. */
+#define ACTION_RSS_NUM 32
+
+/** Storage size for struct rte_flow_action_rss including queues. */
+#define ACTION_RSS_SIZE \
+       (offsetof(struct rte_flow_action_rss, queue) + \
+        sizeof(*((struct rte_flow_action_rss *)0)->queue) * ACTION_RSS_NUM)
+
 /** Maximum number of subsequent tokens and arguments on the stack. */
 #define CTX_STACK_SIZE 16
 
@@ -266,6 +298,23 @@ struct token {
                .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
        })
 
+/** Static initializer for ARGS() to target an arbitrary bit-mask. */
+#define ARGS_ENTRY_MASK(s, f, m) \
+       (&(const struct arg){ \
+               .offset = offsetof(s, f), \
+               .size = sizeof(((s *)0)->f), \
+               .mask = (const void *)(m), \
+       })
+
+/** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
+#define ARGS_ENTRY_MASK_HTON(s, f, m) \
+       (&(const struct arg){ \
+               .hton = 1, \
+               .offset = offsetof(s, f), \
+               .size = sizeof(((s *)0)->f), \
+               .mask = (const void *)(m), \
+       })
+
 /** Static initializer for ARGS() to target a pointer. */
 #define ARGS_ENTRY_PTR(s, f) \
        (&(const struct arg){ \
@@ -387,6 +436,8 @@ static const enum index next_item[] = {
        ITEM_TCP,
        ITEM_SCTP,
        ITEM_VXLAN,
+       ITEM_MPLS,
+       ITEM_GRE,
        ZERO,
 };
 
@@ -429,11 +480,17 @@ static const enum index item_eth[] = {
 static const enum index item_vlan[] = {
        ITEM_VLAN_TPID,
        ITEM_VLAN_TCI,
+       ITEM_VLAN_PCP,
+       ITEM_VLAN_DEI,
+       ITEM_VLAN_VID,
        ITEM_NEXT,
        ZERO,
 };
 
 static const enum index item_ipv4[] = {
+       ITEM_IPV4_TOS,
+       ITEM_IPV4_TTL,
+       ITEM_IPV4_PROTO,
        ITEM_IPV4_SRC,
        ITEM_IPV4_DST,
        ITEM_NEXT,
@@ -441,6 +498,10 @@ static const enum index item_ipv4[] = {
 };
 
 static const enum index item_ipv6[] = {
+       ITEM_IPV6_TC,
+       ITEM_IPV6_FLOW,
+       ITEM_IPV6_PROTO,
+       ITEM_IPV6_HOP,
        ITEM_IPV6_SRC,
        ITEM_IPV6_DST,
        ITEM_NEXT,
@@ -471,6 +532,8 @@ static const enum index item_tcp[] = {
 static const enum index item_sctp[] = {
        ITEM_SCTP_SRC,
        ITEM_SCTP_DST,
+       ITEM_SCTP_TAG,
+       ITEM_SCTP_CKSUM,
        ITEM_NEXT,
        ZERO,
 };
@@ -481,14 +544,29 @@ static const enum index item_vxlan[] = {
        ZERO,
 };
 
+static const enum index item_mpls[] = {
+       ITEM_MPLS_LABEL,
+       ITEM_NEXT,
+       ZERO,
+};
+
+static const enum index item_gre[] = {
+       ITEM_GRE_PROTO,
+       ITEM_NEXT,
+       ZERO,
+};
+
 static const enum index next_action[] = {
        ACTION_END,
        ACTION_VOID,
        ACTION_PASSTHRU,
        ACTION_MARK,
        ACTION_FLAG,
+       ACTION_QUEUE,
        ACTION_DROP,
        ACTION_COUNT,
+       ACTION_DUP,
+       ACTION_RSS,
        ACTION_PF,
        ACTION_VF,
        ZERO,
@@ -500,6 +578,24 @@ static const enum index action_mark[] = {
        ZERO,
 };
 
+static const enum index action_queue[] = {
+       ACTION_QUEUE_INDEX,
+       ACTION_NEXT,
+       ZERO,
+};
+
+static const enum index action_dup[] = {
+       ACTION_DUP_INDEX,
+       ACTION_NEXT,
+       ZERO,
+};
+
+static const enum index action_rss[] = {
+       ACTION_RSS_QUEUES,
+       ACTION_NEXT,
+       ZERO,
+};
+
 static const enum index action_vf[] = {
        ACTION_VF_ORIGINAL,
        ACTION_VF_ID,
@@ -517,6 +613,9 @@ static int parse_vc_spec(struct context *, const struct token *,
                         const char *, unsigned int, void *, unsigned int);
 static int parse_vc_conf(struct context *, const struct token *,
                         const char *, unsigned int, void *, unsigned int);
+static int parse_vc_action_rss_queue(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);
@@ -566,6 +665,8 @@ static int comp_port(struct context *, const struct token *,
                     unsigned int, char *, unsigned int);
 static int comp_rule_id(struct context *, const struct token *,
                        unsigned int, char *, unsigned int);
+static int comp_vc_action_rss_queue(struct context *, const struct token *,
+                                   unsigned int, char *, unsigned int);
 
 /** Token definitions. */
 static const struct token token_list[] = {
@@ -971,6 +1072,27 @@ static const struct token token_list[] = {
                .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
                .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
        },
+       [ITEM_VLAN_PCP] = {
+               .name = "pcp",
+               .help = "priority code point",
+               .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
+                                                 tci, "\xe0\x00")),
+       },
+       [ITEM_VLAN_DEI] = {
+               .name = "dei",
+               .help = "drop eligible indicator",
+               .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
+                                                 tci, "\x10\x00")),
+       },
+       [ITEM_VLAN_VID] = {
+               .name = "vid",
+               .help = "VLAN identifier",
+               .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
+                                                 tci, "\x0f\xff")),
+       },
        [ITEM_IPV4] = {
                .name = "ipv4",
                .help = "match IPv4 header",
@@ -978,6 +1100,27 @@ static const struct token token_list[] = {
                .next = NEXT(item_ipv4),
                .call = parse_vc,
        },
+       [ITEM_IPV4_TOS] = {
+               .name = "tos",
+               .help = "type of service",
+               .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
+                                            hdr.type_of_service)),
+       },
+       [ITEM_IPV4_TTL] = {
+               .name = "ttl",
+               .help = "time to live",
+               .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
+                                            hdr.time_to_live)),
+       },
+       [ITEM_IPV4_PROTO] = {
+               .name = "proto",
+               .help = "next protocol ID",
+               .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
+                                            hdr.next_proto_id)),
+       },
        [ITEM_IPV4_SRC] = {
                .name = "src",
                .help = "source address",
@@ -999,6 +1142,36 @@ static const struct token token_list[] = {
                .next = NEXT(item_ipv6),
                .call = parse_vc,
        },
+       [ITEM_IPV6_TC] = {
+               .name = "tc",
+               .help = "traffic class",
+               .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
+                                                 hdr.vtc_flow,
+                                                 "\x0f\xf0\x00\x00")),
+       },
+       [ITEM_IPV6_FLOW] = {
+               .name = "flow",
+               .help = "flow label",
+               .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
+                                                 hdr.vtc_flow,
+                                                 "\x00\x0f\xff\xff")),
+       },
+       [ITEM_IPV6_PROTO] = {
+               .name = "proto",
+               .help = "protocol (next header)",
+               .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
+                                            hdr.proto)),
+       },
+       [ITEM_IPV6_HOP] = {
+               .name = "hop",
+               .help = "hop limit",
+               .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
+                                            hdr.hop_limits)),
+       },
        [ITEM_IPV6_SRC] = {
                .name = "src",
                .help = "source address",
@@ -1097,6 +1270,20 @@ static const struct token token_list[] = {
                .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
                                             hdr.dst_port)),
        },
+       [ITEM_SCTP_TAG] = {
+               .name = "tag",
+               .help = "validation tag",
+               .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
+                                            hdr.tag)),
+       },
+       [ITEM_SCTP_CKSUM] = {
+               .name = "cksum",
+               .help = "checksum",
+               .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
+                                            hdr.cksum)),
+       },
        [ITEM_VXLAN] = {
                .name = "vxlan",
                .help = "match VXLAN header",
@@ -1110,6 +1297,35 @@ static const struct token token_list[] = {
                .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
                .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
        },
+       [ITEM_MPLS] = {
+               .name = "mpls",
+               .help = "match MPLS header",
+               .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
+               .next = NEXT(item_mpls),
+               .call = parse_vc,
+       },
+       [ITEM_MPLS_LABEL] = {
+               .name = "label",
+               .help = "MPLS label",
+               .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
+                                                 label_tc_s,
+                                                 "\xff\xff\xf0")),
+       },
+       [ITEM_GRE] = {
+               .name = "gre",
+               .help = "match GRE header",
+               .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
+               .next = NEXT(item_gre),
+               .call = parse_vc,
+       },
+       [ITEM_GRE_PROTO] = {
+               .name = "protocol",
+               .help = "GRE protocol type",
+               .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
+               .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
+                                            protocol)),
+       },
        /* Validate/create actions. */
        [ACTIONS] = {
                .name = "actions",
@@ -1163,6 +1379,21 @@ static const struct token token_list[] = {
                .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
                .call = parse_vc,
        },
+       [ACTION_QUEUE] = {
+               .name = "queue",
+               .help = "assign packets to a given queue index",
+               .priv = PRIV_ACTION(QUEUE,
+                                   sizeof(struct rte_flow_action_queue)),
+               .next = NEXT(action_queue),
+               .call = parse_vc,
+       },
+       [ACTION_QUEUE_INDEX] = {
+               .name = "index",
+               .help = "queue index to use",
+               .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
+               .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
+               .call = parse_vc_conf,
+       },
        [ACTION_DROP] = {
                .name = "drop",
                .help = "drop packets (note: passthru has priority)",
@@ -1177,6 +1408,39 @@ static const struct token token_list[] = {
                .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
                .call = parse_vc,
        },
+       [ACTION_DUP] = {
+               .name = "dup",
+               .help = "duplicate packets to a given queue index",
+               .priv = PRIV_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
+               .next = NEXT(action_dup),
+               .call = parse_vc,
+       },
+       [ACTION_DUP_INDEX] = {
+               .name = "index",
+               .help = "queue index to duplicate packets to",
+               .next = NEXT(action_dup, NEXT_ENTRY(UNSIGNED)),
+               .args = ARGS(ARGS_ENTRY(struct rte_flow_action_dup, index)),
+               .call = parse_vc_conf,
+       },
+       [ACTION_RSS] = {
+               .name = "rss",
+               .help = "spread packets among several queues",
+               .priv = PRIV_ACTION(RSS, ACTION_RSS_SIZE),
+               .next = NEXT(action_rss),
+               .call = parse_vc,
+       },
+       [ACTION_RSS_QUEUES] = {
+               .name = "queues",
+               .help = "queue indices to use",
+               .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
+               .call = parse_vc_conf,
+       },
+       [ACTION_RSS_QUEUE] = {
+               .name = "{queue}",
+               .help = "queue index",
+               .call = parse_vc_action_rss_queue,
+               .comp = comp_vc_action_rss_queue,
+       },
        [ACTION_PF] = {
                .name = "pf",
                .help = "redirect packets to physical device function",
@@ -1567,6 +1831,51 @@ parse_vc_conf(struct context *ctx, const struct token *token,
        return len;
 }
 
+/**
+ * Parse queue field for RSS action.
+ *
+ * Valid tokens are queue indices and the "end" token.
+ */
+static int
+parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
+                         const char *str, unsigned int len,
+                         void *buf, unsigned int size)
+{
+       static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
+       int ret;
+       int i;
+
+       (void)token;
+       (void)buf;
+       (void)size;
+       if (ctx->curr != ACTION_RSS_QUEUE)
+               return -1;
+       i = ctx->objdata >> 16;
+       if (!strncmp(str, "end", len)) {
+               ctx->objdata &= 0xffff;
+               return len;
+       }
+       if (i >= ACTION_RSS_NUM)
+               return -1;
+       if (push_args(ctx, ARGS_ENTRY(struct rte_flow_action_rss, queue[i])))
+               return -1;
+       ret = parse_int(ctx, token, str, len, NULL, 0);
+       if (ret < 0) {
+               pop_args(ctx);
+               return -1;
+       }
+       ++i;
+       ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
+       /* Repeat token. */
+       if (ctx->next_num == RTE_DIM(ctx->next))
+               return -1;
+       ctx->next[ctx->next_num++] = next;
+       if (!ctx->object)
+               return len;
+       ((struct rte_flow_action_rss *)ctx->object)->num = i;
+       return len;
+}
+
 /** Parse tokens for destroy command. */
 static int
 parse_destroy(struct context *ctx, const struct token *token,
@@ -2107,7 +2416,7 @@ comp_port(struct context *ctx, const struct token *token,
 
        (void)ctx;
        (void)token;
-       FOREACH_PORT(p, ports) {
+       RTE_ETH_FOREACH_DEV(p) {
                if (buf && i == ent)
                        return snprintf(buf, size, "%u", p);
                ++i;
@@ -2141,6 +2450,24 @@ comp_rule_id(struct context *ctx, const struct token *token,
        return i;
 }
 
+/** Complete queue field for RSS action. */
+static int
+comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
+                        unsigned int ent, char *buf, unsigned int size)
+{
+       static const char *const str[] = { "", "end", NULL };
+       unsigned int i;
+
+       (void)ctx;
+       (void)token;
+       for (i = 0; str[i] != NULL; ++i)
+               if (buf && i == ent)
+                       return snprintf(buf, size, "%s", str[i]);
+       if (buf)
+               return -1;
+       return i;
+}
+
 /** Internal context. */
 static struct context cmd_flow_context;