[ovs-dev,v2] ovs-ofctl: New helper command "parse-packet".

Message ID 20180710204045.17981-1-blp@ovn.org
State Accepted
Headers show
Series
  • [ovs-dev,v2] ovs-ofctl: New helper command "parse-packet".
Related show

Commit Message

Ben Pfaff July 10, 2018, 8:40 p.m.
This was useful for testing commit 4fe080160685 ("flow: Fix buffer overread
for crafted IPv6 packets.").

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
v1->v2: Updated commit message.

 utilities/ovs-ofctl.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Yifeng Sun July 12, 2018, 10:11 p.m. | #1
Thanks. It worked like a charm.

Tested-by: Yifeng Sun <pkusunyifeng@gmail.com>

Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>

On Tue, Jul 10, 2018 at 1:40 PM, Ben Pfaff <blp@ovn.org> wrote:

> This was useful for testing commit 4fe080160685 ("flow: Fix buffer overread
> for crafted IPv6 packets.").
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> ---
> v1->v2: Updated commit message.
>
>  utilities/ovs-ofctl.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
>
> diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
> index 0cd0fcb63e4b..ee08178d8fff 100644
> --- a/utilities/ovs-ofctl.c
> +++ b/utilities/ovs-ofctl.c
> @@ -4802,6 +4802,24 @@ ofctl_compose_packet(struct ovs_cmdl_context *ctx)
>      }
>  }
>
> +/* "parse-packet" reads an Ethernet packet from stdin and prints it out
> its
> + * extracted flow fields. */
> +static void
> +ofctl_parse_packet(struct ovs_cmdl_context *ctx OVS_UNUSED)
> +{
> +    char packet[65535];
> +    ssize_t size = read(STDIN_FILENO, packet, sizeof packet);
> +    if (size < 0) {
> +        ovs_fatal(errno, "failed to read packet from stdin");
> +    }
> +
> +    /* Make a copy of the packet in allocated memory to better allow
> Valgrind
> +     * and Address Sanitizer to catch out-of-range access. */
> +    void *packet_copy = xmemdup(packet, size);
> +    ofp_print_packet(stdout, packet_copy, size, 0);
> +    free(packet_copy);
> +}
> +
>  static const struct ovs_cmdl_command all_commands[] = {
>      { "show", "switch",
>        1, 1, ofctl_show, OVS_RO },
> @@ -4936,6 +4954,7 @@ static const struct ovs_cmdl_command all_commands[]
> = {
>      { "encode-hello", NULL, 1, 1, ofctl_encode_hello, OVS_RW },
>      { "parse-key-value", NULL, 1, INT_MAX, ofctl_parse_key_value, OVS_RW
> },
>      { "compose-packet", NULL, 1, 2, ofctl_compose_packet, OVS_RO },
> +    { "parse-packet", NULL, 0, 0, ofctl_parse_packet, OVS_RO },
>
>      { NULL, NULL, 0, 0, NULL, OVS_RO },
>  };
> --
> 2.16.1
>
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
>
Ben Pfaff July 12, 2018, 11:31 p.m. | #2
Thanks, applied to master.

On Thu, Jul 12, 2018 at 03:11:14PM -0700, Yifeng Sun wrote:
> Thanks. It worked like a charm.
> 
> Tested-by: Yifeng Sun <pkusunyifeng@gmail.com>
> 
> Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>
> 
> On Tue, Jul 10, 2018 at 1:40 PM, Ben Pfaff <blp@ovn.org> wrote:
> 
> > This was useful for testing commit 4fe080160685 ("flow: Fix buffer overread
> > for crafted IPv6 packets.").
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > ---
> > v1->v2: Updated commit message.
> >
> >  utilities/ovs-ofctl.c | 19 +++++++++++++++++++
> >  1 file changed, 19 insertions(+)
> >
> > diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
> > index 0cd0fcb63e4b..ee08178d8fff 100644
> > --- a/utilities/ovs-ofctl.c
> > +++ b/utilities/ovs-ofctl.c
> > @@ -4802,6 +4802,24 @@ ofctl_compose_packet(struct ovs_cmdl_context *ctx)
> >      }
> >  }
> >
> > +/* "parse-packet" reads an Ethernet packet from stdin and prints it out
> > its
> > + * extracted flow fields. */
> > +static void
> > +ofctl_parse_packet(struct ovs_cmdl_context *ctx OVS_UNUSED)
> > +{
> > +    char packet[65535];
> > +    ssize_t size = read(STDIN_FILENO, packet, sizeof packet);
> > +    if (size < 0) {
> > +        ovs_fatal(errno, "failed to read packet from stdin");
> > +    }
> > +
> > +    /* Make a copy of the packet in allocated memory to better allow
> > Valgrind
> > +     * and Address Sanitizer to catch out-of-range access. */
> > +    void *packet_copy = xmemdup(packet, size);
> > +    ofp_print_packet(stdout, packet_copy, size, 0);
> > +    free(packet_copy);
> > +}
> > +
> >  static const struct ovs_cmdl_command all_commands[] = {
> >      { "show", "switch",
> >        1, 1, ofctl_show, OVS_RO },
> > @@ -4936,6 +4954,7 @@ static const struct ovs_cmdl_command all_commands[]
> > = {
> >      { "encode-hello", NULL, 1, 1, ofctl_encode_hello, OVS_RW },
> >      { "parse-key-value", NULL, 1, INT_MAX, ofctl_parse_key_value, OVS_RW
> > },
> >      { "compose-packet", NULL, 1, 2, ofctl_compose_packet, OVS_RO },
> > +    { "parse-packet", NULL, 0, 0, ofctl_parse_packet, OVS_RO },
> >
> >      { NULL, NULL, 0, 0, NULL, OVS_RO },
> >  };
> > --
> > 2.16.1
> >
> > _______________________________________________
> > dev mailing list
> > dev@openvswitch.org
> > https://mail.openvswitch.org/mailman/listinfo/ovs-dev
> >

Patch

diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index 0cd0fcb63e4b..ee08178d8fff 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -4802,6 +4802,24 @@  ofctl_compose_packet(struct ovs_cmdl_context *ctx)
     }
 }
 
+/* "parse-packet" reads an Ethernet packet from stdin and prints it out its
+ * extracted flow fields. */
+static void
+ofctl_parse_packet(struct ovs_cmdl_context *ctx OVS_UNUSED)
+{
+    char packet[65535];
+    ssize_t size = read(STDIN_FILENO, packet, sizeof packet);
+    if (size < 0) {
+        ovs_fatal(errno, "failed to read packet from stdin");
+    }
+
+    /* Make a copy of the packet in allocated memory to better allow Valgrind
+     * and Address Sanitizer to catch out-of-range access. */
+    void *packet_copy = xmemdup(packet, size);
+    ofp_print_packet(stdout, packet_copy, size, 0);
+    free(packet_copy);
+}
+
 static const struct ovs_cmdl_command all_commands[] = {
     { "show", "switch",
       1, 1, ofctl_show, OVS_RO },
@@ -4936,6 +4954,7 @@  static const struct ovs_cmdl_command all_commands[] = {
     { "encode-hello", NULL, 1, 1, ofctl_encode_hello, OVS_RW },
     { "parse-key-value", NULL, 1, INT_MAX, ofctl_parse_key_value, OVS_RW },
     { "compose-packet", NULL, 1, 2, ofctl_compose_packet, OVS_RO },
+    { "parse-packet", NULL, 0, 0, ofctl_parse_packet, OVS_RO },
 
     { NULL, NULL, 0, 0, NULL, OVS_RO },
 };