ovs-ofctl: Ability to read a hex string from file.
authorGurucharan Shetty <gshetty@nicira.com>
Wed, 18 Jun 2014 18:12:36 +0000 (11:12 -0700)
committerGurucharan Shetty <gshetty@nicira.com>
Fri, 27 Jun 2014 00:01:17 +0000 (17:01 -0700)
The unit test, "OFPST_TABLE reply - OF1.2" in ofp-print.at
sends a very large hex string as an argument to 'ovs-ofctl ofp-print'.
The length of the hex string exceeds the maximum command line length
in Windows. With this commit, we can pass the same hex string by
placing it inside a file.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
tests/ofp-print.at
utilities/ovs-ofctl.c

index f2b896f..27187ef 100644 (file)
@@ -1442,7 +1442,7 @@ AT_KEYWORDS([ofp-print OFPT_STATS_REPLY])
  printf "%02x $pad7" $x
  printf "%s$pad32" "table$x" | od -A n -t x1 -v -N 32 | tr '\n' ' '
  echo -n "$mid 00 00 00 02 $tail") > in
-AT_CHECK([ovs-ofctl ofp-print "$(cat in)"], [0], [expout])
+AT_CHECK([ovs-ofctl ofp-print - < in], [0], [expout])
 AT_CLEANUP
 
 AT_SETUP([OFPST_TABLE reply - OF1.3])
index 93f1382..c4034a0 100644 (file)
@@ -3468,18 +3468,41 @@ ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
 
 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
  * binary data, interpreting them as an OpenFlow message, and prints the
- * OpenFlow message on stdout, at VERBOSITY (level 2 by default).  */
+ * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
+ *
+ * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
+ * HEXSTRING_FILE contains the HEXSTRING. */
 static void
 ofctl_ofp_print(int argc, char *argv[])
 {
     struct ofpbuf packet;
+    char *buffer;
+    int verbosity = 2;
+    struct ds line;
+
+    ds_init(&line);
+
+    if (!strcmp(argv[argc-1], "-")) {
+        if (ds_get_line(&line, stdin)) {
+           VLOG_FATAL("Failed to read stdin");
+        }
+
+        buffer = line.string;
+        verbosity = argc > 2 ? atoi(argv[1]) : verbosity;
+    } else if (argc > 2) {
+        buffer = argv[1];
+        verbosity = atoi(argv[2]);
+    } else {
+        buffer = argv[1];
+    }
 
-    ofpbuf_init(&packet, strlen(argv[1]) / 2);
-    if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
+    ofpbuf_init(&packet, strlen(buffer) / 2);
+    if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
         ovs_fatal(0, "trailing garbage following hex bytes");
     }
-    ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), argc > 2 ? atoi(argv[2]) : 2);
+    ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), verbosity);
     ofpbuf_uninit(&packet);
+    ds_destroy(&line);
 }
 
 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message