[ovs-dev] [mirror 10/13] odp-util: Add support for named ports to odp_flow_key_from_string().

Justin Pettit jpettit at nicira.com
Mon Nov 14 01:03:17 UTC 2011


Looks good.

--Justin


On Oct 26, 2011, at 10:09 AM, Ben Pfaff wrote:

> Really the "trace" command should support this but in fact I need it for
> an upcoming update to a test.
> ---
> lib/odp-util.c         |   29 ++++++++++++++++++++++++++---
> lib/odp-util.h         |    4 +++-
> ofproto/ofproto-dpif.c |    2 +-
> tests/test-odp.c       |    2 +-
> 4 files changed, 31 insertions(+), 6 deletions(-)
> 
> diff --git a/lib/odp-util.c b/lib/odp-util.c
> index 89bee20..79fe4ca 100644
> --- a/lib/odp-util.c
> +++ b/lib/odp-util.c
> @@ -30,10 +30,13 @@
> #include "ofpbuf.h"
> #include "openvswitch/tunnel.h"
> #include "packets.h"
> +#include "shash.h"
> #include "timeval.h"
> #include "util.h"
> 
> static void format_odp_key_attr(const struct nlattr *, struct ds *);
> +static int parse_odp_key_attr(const char *, const struct shash *port_names,
> +                              struct ofpbuf *);
> 
> /* The interface between userspace and kernel uses an "OVS_*" prefix.
>  * Since this is fairly non-specific for the OVS userspace components,
> @@ -542,7 +545,8 @@ ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
> }
> 
> static int
> -parse_odp_key_attr(const char *s, struct ofpbuf *key)
> +parse_odp_key_attr(const char *s, const struct shash *port_names,
> +                   struct ofpbuf *key)
> {
>     /* Many of the sscanf calls in this function use oversized destination
>      * fields because some sscanf() implementations truncate the range of %i
> @@ -576,6 +580,20 @@ parse_odp_key_attr(const char *s, struct ofpbuf *key)
>         }
>     }
> 
> +    if (port_names && !strncmp(s, "in_port(", 8)) {
> +        const char *name;
> +        const struct shash_node *node;
> +        int name_len;
> +
> +        name = s + 8;
> +        name_len = strcspn(s, ")");
> +        node = shash_find_len(port_names, name, name_len);
> +        if (node) {
> +            nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, (uintptr_t) node->data);
> +            return 8 + name_len + 1;
> +        }
> +    }
> +
>     {
>         struct ovs_key_ethernet eth_key;
>         int n = -1;
> @@ -810,12 +828,17 @@ parse_odp_key_attr(const char *s, struct ofpbuf *key)
>  * data is appended to 'key'.  Either way, 'key''s data might be
>  * reallocated.
>  *
> + * If 'port_names' is nonnull, it points to an shash that maps from a port name
> + * to a port number cast to void *.  (Port names may be used instead of port
> + * numbers in in_port.)
> + *
>  * On success, the attributes appended to 'key' are individually syntactically
>  * valid, but they may not be valid as a sequence.  'key' might, for example,
>  * be missing an "in_port" key, have duplicated keys, or have keys in the wrong
>  * order.  odp_flow_key_to_flow() will detect those errors. */
> int
> -odp_flow_key_from_string(const char *s, struct ofpbuf *key)
> +odp_flow_key_from_string(const char *s, const struct shash *port_names,
> +                         struct ofpbuf *key)
> {
>     const size_t old_size = key->size;
>     for (;;) {
> @@ -826,7 +849,7 @@ odp_flow_key_from_string(const char *s, struct ofpbuf *key)
>             return 0;
>         }
> 
> -        retval = parse_odp_key_attr(s, key);
> +        retval = parse_odp_key_attr(s, port_names, key);
>         if (retval < 0) {
>             key->size = old_size;
>             return -retval;
> diff --git a/lib/odp-util.h b/lib/odp-util.h
> index 6525512..455da69 100644
> --- a/lib/odp-util.h
> +++ b/lib/odp-util.h
> @@ -30,6 +30,7 @@ struct ds;
> struct flow;
> struct nlattr;
> struct ofpbuf;
> +struct shash;
> 
> #define OVSP_NONE ((uint16_t) -1)
> 
> @@ -92,7 +93,8 @@ struct odputil_keybuf {
> };
> 
> void odp_flow_key_format(const struct nlattr *, size_t, struct ds *);
> -int odp_flow_key_from_string(const char *s, struct ofpbuf *);
> +int odp_flow_key_from_string(const char *s, const struct shash *port_names,
> +                             struct ofpbuf *);
> 
> void odp_flow_key_from_flow(struct ofpbuf *, const struct flow *);
> int odp_flow_key_to_flow(const struct nlattr *, size_t, struct flow *);
> diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c
> index 4e55b43..37381f9 100644
> --- a/ofproto/ofproto-dpif.c
> +++ b/ofproto/ofproto-dpif.c
> @@ -5264,7 +5264,7 @@ ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
> 
>         /* Convert string to datapath key. */
>         ofpbuf_init(&odp_key, 0);
> -        error = odp_flow_key_from_string(arg1, &odp_key);
> +        error = odp_flow_key_from_string(arg1, NULL, &odp_key);
>         if (error) {
>             unixctl_command_reply(conn, 501, "Bad flow syntax");
>             goto exit;
> diff --git a/tests/test-odp.c b/tests/test-odp.c
> index 9a2bc0a..0656bf4 100644
> --- a/tests/test-odp.c
> +++ b/tests/test-odp.c
> @@ -52,7 +52,7 @@ main(void)
> 
>         /* Convert string to OVS DP key. */
>         ofpbuf_init(&odp_key, 0);
> -        error = odp_flow_key_from_string(ds_cstr(&in), &odp_key);
> +        error = odp_flow_key_from_string(ds_cstr(&in), NULL, &odp_key);
>         if (error) {
>             printf("odp_flow_key_from_string: error\n");
>             goto next;
> -- 
> 1.7.2.5
> 




More information about the dev mailing list