From: Ronan Randles <ronan.randles@intel.com>
To: dev@dpdk.org
Cc: harry.van.haaren@intel.com
Subject: [PATCH 06/12] gen: add parsing infrastructure and Ether protocol
Date: Tue, 14 Dec 2021 14:12:36 +0000 [thread overview]
Message-ID: <20211214141242.3383831-7-ronan.randles@intel.com> (raw)
In-Reply-To: <20211214141242.3383831-1-ronan.randles@intel.com>
From: Harry van Haaren <harry.van.haaren@intel.com>
This commit adds parsing infrastructure and support
for Ether parsing. Appropriate unit tests are also added
Signed-off-by: Harry van Haaren <harry.van.haaren@intel.com>
---
app/test/test_gen.c | 29 +++++
lib/gen/meson.build | 2 +-
lib/gen/rte_gen.c | 281 ++++++++++++++++++++++++++++++++++++++++++++
lib/gen/rte_gen.h | 14 ++-
lib/gen/version.map | 1 +
5 files changed, 325 insertions(+), 2 deletions(-)
diff --git a/app/test/test_gen.c b/app/test/test_gen.c
index b60ceaef8a..324582d0a5 100644
--- a/app/test/test_gen.c
+++ b/app/test/test_gen.c
@@ -112,6 +112,34 @@ test_gen_packet_set_raw(void)
return 0;
}
+static int
+test_gen_packet_parse_string(void)
+{
+ struct rte_gen *gen = rte_gen_create(mp);
+ TEST_ASSERT_FAIL(gen, "Expected valid pointer after create()");
+
+ struct str_parse_t {
+ const char *str;
+ } pkt_strings[] = {
+ { .str = "Ether()"},
+ { .str = "Ether()/"},
+ { .str = "/Ether()"},
+ { .str = "/Ether()/"}
+ };
+
+ uint32_t i;
+ for (i = 0; i < RTE_DIM(pkt_strings); i++) {
+ const char *pkt_str = pkt_strings[i].str;
+ int32_t err = rte_gen_packet_parse_string(gen, pkt_str, NULL);
+ TEST_ASSERT_EQUAL(err, 0, "Expected string %s to parse.",
+ pkt_str);
+ }
+
+ rte_gen_destroy(gen);
+ return 0;
+}
+
+
static struct unit_test_suite gen_suite = {
.suite_name = "gen: packet generator unit test suite",
.setup = testsuite_setup,
@@ -121,6 +149,7 @@ static struct unit_test_suite gen_suite = {
TEST_CASE_ST(NULL, NULL, test_gen_basic_rxtx),
TEST_CASE_ST(NULL, NULL, test_gen_loop_rxtx),
TEST_CASE_ST(NULL, NULL, test_gen_packet_set_raw),
+ TEST_CASE_ST(NULL, NULL, test_gen_packet_parse_string),
TEST_CASES_END() /**< NULL terminate unit test array */
}
};
diff --git a/lib/gen/meson.build b/lib/gen/meson.build
index 753984cbba..b3a55564f4 100644
--- a/lib/gen/meson.build
+++ b/lib/gen/meson.build
@@ -3,4 +3,4 @@
sources = files('rte_gen.c')
headers = files('rte_gen.h')
-deps += ['mempool', 'mbuf']
+deps += ['mempool', 'mbuf', 'net']
diff --git a/lib/gen/rte_gen.c b/lib/gen/rte_gen.c
index 432be65f1a..ab73120791 100644
--- a/lib/gen/rte_gen.c
+++ b/lib/gen/rte_gen.c
@@ -9,12 +9,18 @@
#include <rte_hexdump.h>
#include <rte_log.h>
+#include <rte_ether.h>
+
RTE_LOG_REGISTER(gen_logtype, lib.gen, NOTICE);
#define TGEN_LOG(level, fmt, args...) \
rte_log(RTE_LOG_ ## level, gen_logtype, "%s(): " fmt, \
__func__, ## args)
+/* Don't prefix with function name, breaks the Scapy style formatting. */
+#define TGEN_LOG_PROTOCOL(level, fmt, args...) \
+ rte_log(RTE_LOG_ ## level, gen_logtype, fmt, ## args)
+
#define GEN_MAX_BURST 32
#define GEN_INIT_PKT_SIZE 64
@@ -126,3 +132,278 @@ rte_gen_tx_burst(struct rte_gen *gen,
return nb_pkts;
}
+
+enum GEN_PROTO {
+ GEN_PROTO_INVALID,
+ GEN_PROTO_ETHER,
+
+ /* Must be last. */
+ GEN_PROTO_COUNT,
+};
+
+typedef void (*gen_log_func)(void *data, const char *indent);
+
+/* Structure for holding offset and function pointers for protocol. */
+struct protocol_meta {
+ /* Byte offset into packet where this protocol starts. */
+ uint32_t offset;
+ /* Function to call to log the packet's information. */
+ gen_log_func log_func;
+};
+
+/* Allow up to 32 nexted '/' characters in the protocol string. */
+#define GEN_PROTO_PARSE_MAX 16
+
+/* Structure to hold state required while parsing. */
+struct gen_parser {
+ /* Mbuf the parsed data is being put into. */
+ struct rte_mbuf *mbuf;
+ uint8_t *mbuf_data;
+
+ /* Offset into the packet data to parse to next. */
+ uint32_t buf_write_offset;
+
+ /* Parsing state. */
+ uint8_t parse_iter;
+ char indent_str[(GEN_PROTO_PARSE_MAX * 2) + 1];
+
+ /* String being parsed. */
+ char *parse_string;
+ char *parse_strtok_save_ptr;
+
+ /* Store metadata for parse/display of protocols. */
+ struct protocol_meta proto_meta[GEN_PROTO_PARSE_MAX];
+
+ /* Per protocol hit counters. */
+ uint32_t proto_hit_counters[GEN_PROTO_COUNT];
+};
+
+/* Forward declaration of recursive parsing function.
+ * @param inner reports back the inner protocol that was handled. This is often
+ * required for the outer protocol to indicate what the inner protocol is.
+ */
+static int32_t
+gen_parser_parse_next(struct gen_parser *parser, enum GEN_PROTO *inner);
+
+/* Return void pointer to the position in the data buffer to parse into. */
+static inline void *
+gen_parser_get_data_ptr(struct gen_parser *parser)
+{
+ return &parser->mbuf_data[parser->buf_write_offset];
+}
+
+/* Initialize a parser structure. */
+static int32_t
+gen_parser_init(struct gen_parser *parser, struct rte_gen *gen,
+ const char *pkt_string)
+{
+ /* Initialize own memory to zero. */
+ memset(parser, 0, sizeof(*parser));
+
+ /* Duplicate string for tokenizing string. */
+ parser->parse_string = strdup(pkt_string);
+ if (!parser->parse_string)
+ goto error;
+
+ /* Allocate mbuf to parse packet into. */
+ parser->mbuf = rte_pktmbuf_alloc(gen->mp);
+ if (!parser->mbuf)
+ goto error;
+
+ parser->mbuf_data = rte_pktmbuf_mtod(parser->mbuf, uint8_t *);
+
+ return 0;
+
+error:
+ free(parser->parse_string);
+ return -ENOMEM;
+}
+
+static void
+gen_log_ether(void *data, const char *indent)
+{
+ struct rte_ether_hdr *eth = data;
+ char src[64];
+ char dst[64];
+
+ rte_ether_format_addr(src, 64, ð->src_addr);
+ rte_ether_format_addr(dst, 64, ð->dst_addr);
+ const char *type_str;
+ switch (rte_be_to_cpu_16(eth->ether_type)) {
+ case RTE_ETHER_TYPE_IPV4:
+ type_str = "IPv4";
+ break;
+ default:
+ type_str = "0x9000";
+ break;
+ };
+ TGEN_LOG_PROTOCOL(DEBUG,
+ "###[ Ethernet ]###\n%sdst= %s\n%ssrc= %s\n%stype= %s\n",
+ indent, dst, indent, src, indent, type_str);
+}
+
+/* Ether(...) string detected, supports parameters:
+ * - dst : Destination MAC in 00:11:22:33:44:55 or 0011:2233:4455 forms.
+ * - src : Source MAC in the same forms.
+ * Note:
+ * - type is set based on the next header
+ */
+static int32_t
+gen_parse_ether(struct gen_parser *parser, char *protocol_str)
+{
+ struct rte_ether_hdr *eth = gen_parser_get_data_ptr(parser);
+
+ char *dst_ptr = strstr(protocol_str, "dst=");
+ if (dst_ptr) {
+ char *dup = strdup(dst_ptr);
+ rte_ether_unformat_addr(&dup[4], ð->dst_addr);
+ free(dup);
+ } else
+ rte_ether_unformat_addr("ff:ff:ff:ff:ff:ff", ð->dst_addr);
+
+ char *src_ptr = strstr(protocol_str, "src=");
+ if (src_ptr)
+ rte_ether_unformat_addr(&src_ptr[4], ð->src_addr);
+ else
+ rte_ether_unformat_addr("00:00:00:00:00:00", ð->src_addr);
+
+ /* Move up write pointer in packet. */
+ parser->buf_write_offset += sizeof(*eth);
+
+ /* Recurse and handle inner protocol. */
+ enum GEN_PROTO inner;
+ int32_t err = gen_parser_parse_next(parser, &inner);
+ if (err) {
+ TGEN_LOG(ERR, "parser parse next() error %d\n", err);
+ return err;
+ }
+
+ switch (inner) {
+ default:
+ eth->ether_type = rte_cpu_to_be_16(0x9000);
+ break;
+ };
+ return 0;
+}
+
+/* (Name, Function-pointer) pairs for supported parse types */
+typedef int32_t (*gen_parse_func)(struct gen_parser *parser,
+ char *protocol_str);
+
+struct gen_parse_func_t {
+ const char *name;
+ enum GEN_PROTO proto;
+ gen_parse_func parse_func;
+ gen_log_func log_func;
+};
+
+/* Mapping from string to function to parse that protocol. */
+static struct gen_parse_func_t gen_protocols[] = {
+ {
+ .name = "Ether(",
+ .proto = GEN_PROTO_ETHER,
+ .parse_func = gen_parse_ether,
+ .log_func = gen_log_ether,
+ }
+};
+
+/* Function to tokenize and parse each segment of a string.
+ * @param outer indicates the protocol before this one.
+ * @param inner returns the protocol that is parsed here/now.
+ */
+static int32_t
+gen_parser_parse_next(struct gen_parser *parser,
+ enum GEN_PROTO *inner_proto)
+{
+ /* Tokenize the input string based on '/' character. */
+ char *tok_str = (parser->parse_iter == 0) ?
+ parser->parse_string : NULL;
+ parser->parse_string = strtok_r(tok_str, "/",
+ &parser->parse_strtok_save_ptr);
+
+ /* End protocol parsing recursion when parse_string is NULL, or max
+ * protocol recursion depth is reached.
+ */
+ if (!parser->parse_string ||
+ parser->parse_iter >= GEN_PROTO_PARSE_MAX) {
+ struct rte_mbuf *mbuf = parser->mbuf;
+ mbuf->data_len = parser->buf_write_offset;
+ mbuf->pkt_len = parser->buf_write_offset;
+ TGEN_LOG(DEBUG, "packet length %d\n", mbuf->pkt_len);
+ return 0;
+ }
+
+ uint32_t i;
+ /* Loop over protocols, and identify the parse function to call. */
+ for (i = 0; i < RTE_DIM(gen_protocols); i++) {
+ const char *proto = gen_protocols[i].name;
+ uint32_t proto_len = strlen(proto);
+ if (strncmp(proto, parser->parse_string, proto_len))
+ continue;
+
+ /* Store the log function pointer to output later. */
+ uint32_t iter = parser->parse_iter;
+ parser->proto_hit_counters[i]++;
+ struct protocol_meta *meta = &parser->proto_meta[iter];
+ meta->log_func = gen_protocols[i].log_func;
+ meta->offset = parser->buf_write_offset;
+
+ /* Handle protocol recursively. */
+ parser->parse_iter++;
+ int err = gen_protocols[i].parse_func(parser,
+ parser->parse_string);
+ *inner_proto = gen_protocols[i].proto;
+
+ return err;
+ }
+
+ TGEN_LOG(ERR, "parser does not understand protocol %s\n",
+ parser->parse_string);
+ return -1;
+}
+
+int32_t
+rte_gen_packet_parse_string(struct rte_gen *gen,
+ const char *pkt_string,
+ struct rte_mbuf **old_mbuf_to_user)
+{
+ struct gen_parser parser;
+ int32_t err = gen_parser_init(&parser, gen, pkt_string);
+ if (err) {
+ TGEN_LOG(ERR, "error with parser_init(), %d\n", err);
+ return -1;
+ };
+
+ /* Recursively parse each protocol. */
+ enum GEN_PROTO inner;
+ err = gen_parser_parse_next(&parser, &inner);
+ if (err) {
+ TGEN_LOG(ERR, "Error in parsing packet string. "
+ "Set \"gen\" log level to debug for more info.\n");
+ return -1;
+ }
+
+ uint32_t i;
+ /* Iterate the per protocol stored metadata to log output. */
+ for (i = 0; i < parser.parse_iter; i++) {
+ snprintf(parser.indent_str, 2 + i * 2,
+ " " /* 32 spaces. */);
+ void *buf_off = parser.mbuf_data + parser.proto_meta[i].offset;
+ parser.proto_meta[i].log_func(buf_off, parser.indent_str);
+ }
+
+ if (inner != GEN_PROTO_ETHER) {
+ TGEN_LOG(WARNING,
+ "Outer protocol of frame is not Ethernet.\n");
+ }
+
+ /* Free the currently in use mbuf. */
+ if (old_mbuf_to_user)
+ *old_mbuf_to_user = gen->base_pkt;
+ else
+ rte_pktmbuf_free(gen->base_pkt);
+
+ /* TODO: HVH design race-condition above vs rx/tx*/
+ gen->base_pkt = parser.mbuf;
+ return 0;
+}
diff --git a/lib/gen/rte_gen.h b/lib/gen/rte_gen.h
index c8d85a5b72..93b3346436 100644
--- a/lib/gen/rte_gen.h
+++ b/lib/gen/rte_gen.h
@@ -89,12 +89,24 @@ rte_gen_tx_burst(struct rte_gen *gen,
* @retval 0 Success.
* @retval -ENOMEM No memory available.
*/
-int32_t
__rte_experimental
+int32_t
rte_gen_packet_set_raw(struct rte_gen *gen,
const uint8_t *raw_data,
uint32_t raw_data_size);
+/* Parse a string description of a packet.
+ *
+ * The optional out parameter supplies the previously being sent mbuf to
+ * the user to be freed later. If this argument is not provided, then the
+ * mbuf is freed by this function.
+ */
+__rte_experimental
+int32_t
+rte_gen_packet_parse_string(struct rte_gen *gen,
+ const char *pkt_string,
+ struct rte_mbuf **old_mbuf_to_user);
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/gen/version.map b/lib/gen/version.map
index d75e0b4bac..e335c608ee 100644
--- a/lib/gen/version.map
+++ b/lib/gen/version.map
@@ -6,4 +6,5 @@ EXPERIMENTAL {
rte_gen_rx_burst;
rte_gen_tx_burst;
rte_gen_packet_set_raw;
+ rte_gen_packet_parse_string;
};
--
2.25.1
next prev parent reply other threads:[~2021-12-14 14:13 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-14 14:12 [PATCH 00/12] add packet generator library and example app Ronan Randles
2021-12-14 14:12 ` [PATCH 01/12] net: add string to IPv4 parse function Ronan Randles
2021-12-14 17:31 ` Morten Brørup
2021-12-15 9:27 ` Bruce Richardson
2021-12-15 9:35 ` Morten Brørup
2021-12-15 10:11 ` Bruce Richardson
2022-01-19 14:20 ` Thomas Monjalon
2021-12-14 14:12 ` [PATCH 02/12] net: add function to pretty print IPv4 Ronan Randles
2021-12-14 16:08 ` Stephen Hemminger
2021-12-14 17:42 ` Morten Brørup
2021-12-14 17:31 ` Morten Brørup
2021-12-15 1:06 ` Ananyev, Konstantin
2021-12-15 3:20 ` Stephen Hemminger
2021-12-15 7:23 ` Morten Brørup
2021-12-15 13:06 ` Ananyev, Konstantin
2022-01-19 14:24 ` Thomas Monjalon
2022-01-19 14:41 ` Van Haaren, Harry
2021-12-14 14:12 ` [PATCH 03/12] gen: add files for initial traffic generation library Ronan Randles
2021-12-14 14:12 ` [PATCH 04/12] gen: add basic Rx and Tx routines and tests Ronan Randles
2021-12-14 14:12 ` [PATCH 05/12] gen: add raw packet data API " Ronan Randles
2021-12-15 12:40 ` Jerin Jacob
2021-12-17 11:40 ` Van Haaren, Harry
2021-12-17 16:19 ` Thomas Monjalon
2021-12-20 10:21 ` Van Haaren, Harry
2022-01-19 14:56 ` Thomas Monjalon
2022-01-20 10:21 ` Van Haaren, Harry
2022-01-21 10:45 ` Van Haaren, Harry
2021-12-20 13:21 ` Jerin Jacob
2022-01-21 14:20 ` Xueming(Steven) Li
2021-12-14 14:12 ` Ronan Randles [this message]
2021-12-14 14:12 ` [PATCH 07/12] gen: add gen IP parsing Ronan Randles
2021-12-14 14:12 ` [PATCH 08/12] examples/generator: import code from basicfwd.c Ronan Randles
2021-12-14 14:12 ` [PATCH 09/12] examples/generator: enable gen library for traffic gen Ronan Randles
2021-12-14 14:12 ` [PATCH 10/12] examples/generator: telemetry support Ronan Randles
2021-12-14 14:12 ` [PATCH 11/12] examples/generator: link status check added Ronan Randles
2021-12-14 14:12 ` [PATCH 12/12] examples/generator: line rate limiting Ronan Randles
2021-12-14 16:10 ` Stephen Hemminger
2021-12-14 14:57 ` [PATCH 00/12] add packet generator library and example app Bruce Richardson
2021-12-14 15:59 ` Randles, Ronan
2022-01-12 16:18 ` Morten Brørup
2021-12-15 12:31 ` Jerin Jacob
2021-12-15 14:07 ` Bruce Richardson
2022-01-21 10:31 ` [PATCH v2 00/15] " Ronan Randles
2022-01-21 10:31 ` [PATCH v2 01/15] net: add string to IPv4 parse function Ronan Randles
2022-01-21 10:31 ` [PATCH v2 02/15] net: add function to pretty print IPv4 Ronan Randles
2022-01-21 16:20 ` Stephen Hemminger
2022-01-21 10:31 ` [PATCH v2 03/15] gen: add files for initial traffic generation library Ronan Randles
2022-01-21 10:31 ` [PATCH v2 04/15] gen: add basic Rx and Tx routines and tests Ronan Randles
2022-01-21 10:31 ` [PATCH v2 05/15] gen: add raw packet data API " Ronan Randles
2022-01-21 10:31 ` [PATCH v2 06/15] gen: add parsing infrastructure and Ether protocol Ronan Randles
2022-01-21 10:31 ` [PATCH v2 07/15] gen: add gen IP parsing Ronan Randles
2022-01-21 10:31 ` [PATCH v2 08/15] examples/generator: import code from basicfwd.c Ronan Randles
2022-01-21 10:31 ` [PATCH v2 09/15] examples/generator: enable gen library for traffic gen Ronan Randles
2022-01-21 10:31 ` [PATCH v2 10/15] examples/generator: telemetry support Ronan Randles
2022-01-21 10:31 ` [PATCH v2 11/15] examples/generator: link status check added Ronan Randles
2022-01-21 10:31 ` [PATCH v2 12/15] examples/generator: line rate limiting Ronan Randles
2022-01-21 10:31 ` [PATCH v2 13/15] gen: add UDP support Ronan Randles
2022-01-21 10:31 ` [PATCH v2 14/15] net/vxlan: instance flag endianness refactored Ronan Randles
2022-01-21 10:31 ` [PATCH v2 15/15] gen: add VXLAN support Ronan Randles
2022-01-21 14:44 ` [PATCH 00/12] add packet generator library and example app Xueming(Steven) Li
2022-01-21 15:24 ` Van Haaren, Harry
2022-01-24 10:48 ` Ananyev, Konstantin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211214141242.3383831-7-ronan.randles@intel.com \
--to=ronan.randles@intel.com \
--cc=dev@dpdk.org \
--cc=harry.van.haaren@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).