[ovs-dev,8/9] ovn-test: Add 'expr-to-packets' command.
diff mbox

Message ID 1483582391-114359-8-git-send-email-jpettit@ovn.org
State Accepted
Headers show

Commit Message

Justin Pettit Jan. 5, 2017, 2:13 a.m. UTC
Parses OVN expressions from stdin and prints out matching packets in
hexadecimal on stdout.

Signed-off-by: Justin Pettit <jpettit@ovn.org>
---
 tests/test-ovn.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

Comments

Ben Pfaff Jan. 5, 2017, 3:47 p.m. UTC | #1
On Wed, Jan 04, 2017 at 06:13:10PM -0800, Justin Pettit wrote:
> Parses OVN expressions from stdin and prints out matching packets in
> hexadecimal on stdout.
> 
> Signed-off-by: Justin Pettit <jpettit@ovn.org>

'\0' looks weird here, a flow is not a character string:
        struct flow uflow;
        memset(&uflow, '\0', sizeof uflow);
But you can just remove the memset() call entirely because
expr_parse_microflow() always initializes the whole flow.

Acked-by: Ben Pfaff <blp@ovn.org>

Patch
diff mbox

diff --git a/tests/test-ovn.c b/tests/test-ovn.c
index c0bc484..9165f5c 100644
--- a/tests/test-ovn.c
+++ b/tests/test-ovn.c
@@ -19,6 +19,7 @@ 
 #include <getopt.h>
 #include <sys/wait.h>
 #include "command-line.h"
+#include "dp-packet.h"
 #include "fatal-signal.h"
 #include "flow.h"
 #include "openvswitch/dynamic-string.h"
@@ -1120,6 +1121,48 @@  test_exhaustive(struct ovs_cmdl_context *ctx OVS_UNUSED)
     expr_symtab_destroy(&symtab);
     shash_destroy(&symtab);
 }
+
+static void
+test_expr_to_packets(struct ovs_cmdl_context *ctx OVS_UNUSED)
+{
+    struct shash symtab;
+    struct ds input;
+
+    create_symtab(&symtab);
+
+    ds_init(&input);
+    while (!ds_get_test_line(&input, stdin)) {
+        struct flow uflow;
+        memset(&uflow, '\0', sizeof uflow);
+        char *error = expr_parse_microflow(ds_cstr(&input), &symtab, NULL,
+                                           lookup_atoi_cb, NULL, &uflow);
+        if (error) {
+            puts(error);
+            free(error);
+            continue;
+        }
+
+        uint64_t packet_stub[128 / 8];
+        struct dp_packet packet;
+        dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
+        flow_compose(&packet, &uflow);
+
+        struct ds output = DS_EMPTY_INITIALIZER;
+        const uint8_t *buf = dp_packet_data(&packet);
+        for (int i = 0; i < dp_packet_size(&packet); i++) {
+            uint8_t val = buf[i];
+            ds_put_format(&output, "%02"PRIx8, val);
+        }
+        puts(ds_cstr(&output));
+        ds_destroy(&output);
+
+        dp_packet_uninit(&packet);
+    }
+    ds_destroy(&input);
+
+    expr_symtab_destroy(&symtab);
+    shash_destroy(&symtab);
+}
 
 /* Actions. */
 
@@ -1291,10 +1334,14 @@  annotate-expr\n\
 simplify-expr\n\
 normalize-expr\n\
 expr-to-flows\n\
-  Parses OVN expressions from stdin and print them back on stdout after\n\
+  Parses OVN expressions from stdin and prints them back on stdout after\n\
   differing degrees of analysis.  Available fields are based on packet\n\
   headers.\n\
 \n\
+expr-to-packets\n\
+  Parses OVN expressions from stdin and prints out matching packets in\n\
+  hexadecimal on stdout.\n\
+\n\
 evaluate-expr MICROFLOW\n\
   Parses OVN expressions from stdin and evaluates them against the flow\n\
   specified in MICROFLOW, which must be an expression that constrains\n\
@@ -1451,6 +1498,7 @@  test_ovn_main(int argc, char *argv[])
         {"composition", NULL, 1, 1, test_composition, OVS_RO},
         {"tree-shape", NULL, 1, 1, test_tree_shape, OVS_RO},
         {"exhaustive", NULL, 1, 1, test_exhaustive, OVS_RO},
+        {"expr-to-packets", NULL, 0, 0, test_expr_to_packets, OVS_RO},
 
         /* Actions. */
         {"parse-actions", NULL, 0, 0, test_parse_actions, OVS_RO},