ovn-test: Add 'expr-to-packets' command.
Parses OVN expressions from stdin and prints out matching packets in
hexadecimal on stdout.
Acked-by: Ben Pfaff
#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,47 @@ 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;
+ 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 +1333,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 +1497,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},
