X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fodp-util.c;h=af464a0409508a354fe1e16135dce342bfa2d424;hb=61abba807febc0954acc0d23aeb75b081ad9d77a;hp=e20a0ba2542a36c83eb4bcf275db2d3563b4df0f;hpb=b075a9b855c5234babf5304723998248ec50740f;p=sliver-openvswitch.git diff --git a/lib/odp-util.c b/lib/odp-util.c index e20a0ba25..af464a040 100644 --- a/lib/odp-util.c +++ b/lib/odp-util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ #include #include "byte-order.h" #include "coverage.h" +#include "dpif.h" #include "dynamic-string.h" #include "flow.h" #include "netlink.h" @@ -78,6 +79,8 @@ odp_action_len(uint16_t type) case OVS_ACTION_ATTR_POP_VLAN: return 0; case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls); case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16); + case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t); + case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash); case OVS_ACTION_ATTR_SET: return -2; case OVS_ACTION_ATTR_SAMPLE: return -2; @@ -117,6 +120,8 @@ ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) case OVS_KEY_ATTR_ARP: return "arp"; case OVS_KEY_ATTR_ND: return "nd"; case OVS_KEY_ATTR_MPLS: return "mpls"; + case OVS_KEY_ATTR_DP_HASH: return "dp_hash"; + case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id"; case __OVS_KEY_ATTR_MAX: default: @@ -148,9 +153,8 @@ static void format_odp_sample_action(struct ds *ds, const struct nlattr *attr) { static const struct nl_policy ovs_sample_policy[] = { - { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */ - { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */ - { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */ + [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 }, + [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED } }; struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)]; double percentage; @@ -217,7 +221,7 @@ parse_flags(const char *s, const char *(*bit_to_string)(uint32_t), uint32_t bit; int n0; - if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) { + if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) { n += n0 + (s[n + n0] == ','); result |= flags; continue; @@ -254,9 +258,9 @@ static void format_odp_userspace_action(struct ds *ds, const struct nlattr *attr) { static const struct nl_policy ovs_userspace_policy[] = { - { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */ - { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */ - { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */ + [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 }, + [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC, + .optional = true }, }; struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)]; const struct nlattr *userdata_attr; @@ -352,21 +356,55 @@ format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse) static void format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key, - const struct ovs_key_mpls *mpls_mask) + const struct ovs_key_mpls *mpls_mask, int n) { - ovs_be32 key = mpls_key->mpls_lse; + if (n == 1) { + ovs_be32 key = mpls_key->mpls_lse; - if (mpls_mask == NULL) { - format_mpls_lse(ds, key); + if (mpls_mask == NULL) { + format_mpls_lse(ds, key); + } else { + ovs_be32 mask = mpls_mask->mpls_lse; + + ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", + mpls_lse_to_label(key), mpls_lse_to_label(mask), + mpls_lse_to_tc(key), mpls_lse_to_tc(mask), + mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), + mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); + } } else { - ovs_be32 mask = mpls_mask->mpls_lse; + int i; + + for (i = 0; i < n; i++) { + ds_put_format(ds, "lse%d=%#"PRIx32, + i, ntohl(mpls_key[i].mpls_lse)); + if (mpls_mask) { + ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse)); + } + ds_put_char(ds, ','); + } + ds_chomp(ds, ','); + } +} + +static void +format_odp_recirc_action(struct ds *ds, uint32_t recirc_id) +{ + ds_put_format(ds, "recirc(%"PRIu32")", recirc_id); +} + +static void +format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act) +{ + ds_put_format(ds, "hash("); - ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x", - mpls_lse_to_label(key), mpls_lse_to_label(mask), - mpls_lse_to_tc(key), mpls_lse_to_tc(mask), - mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask), - mpls_lse_to_bos(key), mpls_lse_to_bos(mask)); + if (hash_act->hash_alg == OVS_HASH_ALG_L4) { + ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis); + } else { + ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")", + hash_act->hash_alg); } + ds_put_format(ds, ")"); } static void @@ -378,7 +416,7 @@ format_odp_action(struct ds *ds, const struct nlattr *a) expected_len = odp_action_len(nl_attr_type(a)); if (expected_len != -2 && nl_attr_get_size(a) != expected_len) { - ds_put_format(ds, "bad length %zu, expected %d for: ", + ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ", nl_attr_get_size(a), expected_len); format_generic_odp_action(ds, a); return; @@ -391,6 +429,12 @@ format_odp_action(struct ds *ds, const struct nlattr *a) case OVS_ACTION_ATTR_USERSPACE: format_odp_userspace_action(ds, a); break; + case OVS_ACTION_ATTR_RECIRC: + format_odp_recirc_action(ds, nl_attr_get_u32(a)); + break; + case OVS_ACTION_ATTR_HASH: + format_odp_hash_action(ds, nl_attr_get(a)); + break; case OVS_ACTION_ATTR_SET: ds_put_cstr(ds, "set("); format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true); @@ -466,21 +510,11 @@ static int parse_odp_action(const char *s, const struct simap *port_names, struct ofpbuf *actions) { - /* Many of the sscanf calls in this function use oversized destination - * fields because some sscanf() implementations truncate the range of %i - * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a - * value of 0x7fff. The other alternatives are to allow only a single - * radix (e.g. decimal or hexadecimal) or to write more sophisticated - * parsers. - * - * The tun_id parser has to use an alternative approach because there is no - * type larger than 64 bits. */ - { - unsigned long long int port; - int n = -1; + uint32_t port; + int n; - if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) { + if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) { nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port); return n; } @@ -498,21 +532,21 @@ parse_odp_action(const char *s, const struct simap *port_names, } { - unsigned long long int pid; - unsigned long long int output; - unsigned long long int probability; - unsigned long long int collector_set_id; - unsigned long long int obs_domain_id; - unsigned long long int obs_point_id; + uint32_t pid; + uint32_t output; + uint32_t probability; + uint32_t collector_set_id; + uint32_t obs_domain_id; + uint32_t obs_point_id; int vid, pcp; int n = -1; - if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) { + if (ovs_scan(s, "userspace(pid=%"SCNi32")%n", &pid, &n)) { odp_put_userspace_action(pid, NULL, 0, actions); return n; - } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i," - "pcp=%i,output=%lli))%n", - &pid, &vid, &pcp, &output, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "userspace(pid=%"SCNi32",sFlow(vid=%i," + "pcp=%i,output=%"SCNi32"))%n", + &pid, &vid, &pcp, &output, &n)) { union user_action_cookie cookie; uint16_t tci; @@ -527,8 +561,8 @@ parse_odp_action(const char *s, const struct simap *port_names, odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow, actions); return n; - } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "userspace(pid=%"SCNi32",slow_path%n", + &pid, &n)) { union user_action_cookie cookie; int res; @@ -550,11 +584,13 @@ parse_odp_action(const char *s, const struct simap *port_names, odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, actions); return n; - } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli," - "collector_set_id=%lli,obs_domain_id=%lli," - "obs_point_id=%lli))%n", - &pid, &probability, &collector_set_id, - &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "userspace(pid=%"SCNi32"," + "flow_sample(probability=%"SCNi32"," + "collector_set_id=%"SCNi32"," + "obs_domain_id=%"SCNi32"," + "obs_point_id=%"SCNi32"))%n", + &pid, &probability, &collector_set_id, + &obs_domain_id, &obs_point_id, &n)) { union user_action_cookie cookie; cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE; @@ -565,23 +601,22 @@ parse_odp_action(const char *s, const struct simap *port_names, odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample, actions); return n; - } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "userspace(pid=%"SCNi32",ipfix)%n", &pid, &n)) { union user_action_cookie cookie; cookie.type = USER_ACTION_COOKIE_IPFIX; odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix, actions); return n; - } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "userspace(pid=%"SCNi32",userdata(%n", + &pid, &n)) { struct ofpbuf buf; char *end; ofpbuf_init(&buf, 16); end = ofpbuf_put_hex(&buf, &s[n], NULL); if (end[0] == ')' && end[1] == ')') { - odp_put_userspace_action(pid, buf.data, buf.size, actions); + odp_put_userspace_action(pid, ofpbuf_data(&buf), ofpbuf_size(&buf), actions); ofpbuf_uninit(&buf); return (end + 2) - s; } @@ -611,14 +646,13 @@ parse_odp_action(const char *s, const struct simap *port_names, int cfi = 1; int n = -1; - if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0 - && n > 0) - || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", - &vid, &pcp, &cfi, &n) > 0 && n > 0) - || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", - &tpid, &vid, &pcp, &n) > 0 && n > 0) - || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", - &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) { + if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) + || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n", + &vid, &pcp, &cfi, &n) + || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n", + &tpid, &vid, &pcp, &n) + || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n", + &tpid, &vid, &pcp, &cfi, &n)) { push.vlan_tpid = htons(tpid); push.vlan_tci = htons((vid << VLAN_VID_SHIFT) | (pcp << VLAN_PCP_SHIFT) @@ -639,9 +673,8 @@ parse_odp_action(const char *s, const struct simap *port_names, double percentage; int n = -1; - if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0 - && percentage >= 0. && percentage <= 100.0 - && n > 0) { + if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) + && percentage >= 0. && percentage <= 100.0) { size_t sample_ofs, actions_ofs; double probability; @@ -693,7 +726,7 @@ odp_actions_from_string(const char *s, const struct simap *port_names, return 0; } - old_size = actions->size; + old_size = ofpbuf_size(actions); for (;;) { int retval; @@ -704,7 +737,7 @@ odp_actions_from_string(const char *s, const struct simap *port_names, retval = parse_odp_action(s, port_names, actions); if (retval < 0 || !strchr(delimiters, s[retval])) { - actions->size = old_size; + ofpbuf_set_size(actions, old_size); return -retval; } s += retval; @@ -727,12 +760,14 @@ odp_flow_key_attr_len(uint16_t type) case OVS_KEY_ATTR_ENCAP: return -2; case OVS_KEY_ATTR_PRIORITY: return 4; case OVS_KEY_ATTR_SKB_MARK: return 4; + case OVS_KEY_ATTR_DP_HASH: return 4; + case OVS_KEY_ATTR_RECIRC_ID: return 4; case OVS_KEY_ATTR_TUNNEL: return -2; case OVS_KEY_ATTR_IN_PORT: return 4; case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet); case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16); case OVS_KEY_ATTR_ETHERTYPE: return 2; - case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls); + case OVS_KEY_ATTR_MPLS: return -2; case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4); case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6); case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp); @@ -856,7 +891,7 @@ odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun) return ODP_FIT_ERROR; } if (unknown) { - return ODP_FIT_TOO_MUCH; + return ODP_FIT_TOO_MUCH; } return ODP_FIT_PERFECT; } @@ -868,7 +903,8 @@ tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key) tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL); - if (tun_key->flags & FLOW_TNL_F_KEY) { + /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */ + if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) { nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id); } if (tun_key->ip_src) { @@ -984,22 +1020,22 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, expected_len = odp_flow_key_attr_len(nl_attr_type(a)); if (expected_len != -2) { bool bad_key_len = nl_attr_get_size(a) != expected_len; - bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len; + bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len; if (bad_key_len || bad_mask_len) { if (bad_key_len) { - ds_put_format(ds, "(bad key length %zu, expected %d)(", - nl_attr_get_size(a), - odp_flow_key_attr_len(nl_attr_type(a))); + ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(", + nl_attr_get_size(a), expected_len); } format_generic_odp_key(a, ds); - if (bad_mask_len) { + if (ma) { ds_put_char(ds, '/'); - ds_put_format(ds, "(bad mask length %zu, expected %d)(", - nl_attr_get_size(ma), - odp_flow_key_attr_len(nl_attr_type(ma))); + if (bad_mask_len) { + ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(", + nl_attr_get_size(ma), expected_len); + } + format_generic_odp_key(ma, ds); } - format_generic_odp_key(ma, ds); ds_put_char(ds, ')'); return; } @@ -1021,6 +1057,8 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, case OVS_KEY_ATTR_PRIORITY: case OVS_KEY_ATTR_SKB_MARK: + case OVS_KEY_ATTR_DP_HASH: + case OVS_KEY_ATTR_RECIRC_ID: ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a)); if (!is_exact) { ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma)); @@ -1126,10 +1164,23 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, case OVS_KEY_ATTR_MPLS: { const struct ovs_key_mpls *mpls_key = nl_attr_get(a); const struct ovs_key_mpls *mpls_mask = NULL; + size_t size = nl_attr_get_size(a); + + if (!size || size % sizeof *mpls_key) { + ds_put_format(ds, "(bad key length %"PRIuSIZE")", + nl_attr_get_size(a)); + return; + } if (!is_exact) { mpls_mask = nl_attr_get(ma); + if (nl_attr_get_size(a) != nl_attr_get_size(ma)) { + ds_put_format(ds, "(key length %"PRIuSIZE" != " + "mask length %"PRIuSIZE")", + nl_attr_get_size(a), nl_attr_get_size(ma)); + return; + } } - format_mpls(ds, mpls_key, mpls_mask); + format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key); break; } @@ -1268,7 +1319,7 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, } else { const struct ovs_key_sctp *sctp_key = nl_attr_get(a); - ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")", + ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16, ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst)); } break; @@ -1369,7 +1420,6 @@ format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma, } break; } - case OVS_KEY_ATTR_UNSPEC: case __OVS_KEY_ATTR_MAX: default: @@ -1403,7 +1453,7 @@ generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key) nl_msg_end_nested(ofp, nested_mask); } - return ofp->base; + return ofpbuf_base(ofp); } /* Appends to 'ds' a string representation of the 'key_len' bytes of @@ -1563,28 +1613,17 @@ static int parse_odp_key_mask_attr(const char *s, const struct simap *port_names, struct ofpbuf *key, struct ofpbuf *mask) { - /* Many of the sscanf calls in this function use oversized destination - * fields because some sscanf() implementations truncate the range of %i - * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a - * value of 0x7fff. The other alternatives are to allow only a single - * radix (e.g. decimal or hexadecimal) or to write more sophisticated - * parsers. - * - * The tun_id parser has to use an alternative approach because there is no - * type larger than 64 bits. */ - { - unsigned long long int priority; - unsigned long long int priority_mask; + uint32_t priority; + uint32_t priority_mask; int n = -1; - if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority, - &priority_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "skb_priority(%"SCNi32"/%"SCNi32")%n", + &priority, &priority_mask, &n)) { nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority); nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask); return n; - } else if (sscanf(s, "skb_priority(%lli)%n", - &priority, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "skb_priority(%"SCNi32")%n", &priority, &n)) { nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority); if (mask) { nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX); @@ -1594,16 +1633,16 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, } { - unsigned long long int mark; - unsigned long long int mark_mask; + uint32_t mark; + uint32_t mark_mask; int n = -1; - if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark, - &mark_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "skb_mark(%"SCNi32"/%"SCNi32")%n", &mark, + &mark_mask, &n)) { nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark); nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask); return n; - } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "skb_mark(%"SCNi32")%n", &mark, &n)) { nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark); if (mask) { nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX); @@ -1613,31 +1652,56 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, } { - char tun_id_s[32]; - int tos, tos_mask, ttl, ttl_mask; + uint32_t recirc_id; + int n = -1; + + if (ovs_scan(s, "recirc_id(%"SCNi32")%n", &recirc_id, &n)) { + nl_msg_put_u32(key, OVS_KEY_ATTR_RECIRC_ID, recirc_id); + nl_msg_put_u32(mask, OVS_KEY_ATTR_RECIRC_ID, UINT32_MAX); + return n; + } + } + + { + uint32_t dp_hash; + uint32_t dp_hash_mask; + int n = -1; + + if (mask && ovs_scan(s, "dp_hash(%"SCNi32"/%"SCNi32")%n", &dp_hash, + &dp_hash_mask, &n)) { + nl_msg_put_u32(key, OVS_KEY_ATTR_DP_HASH, dp_hash); + nl_msg_put_u32(mask, OVS_KEY_ATTR_DP_HASH, dp_hash_mask); + return n; + } else if (ovs_scan(s, "dp_hash(%"SCNi32")%n", &dp_hash, &n)) { + nl_msg_put_u32(key, OVS_KEY_ATTR_DP_HASH, dp_hash); + if (mask) { + nl_msg_put_u32(mask, OVS_KEY_ATTR_DP_HASH, UINT32_MAX); + } + return n; + } + } + + { + uint64_t tun_id, tun_id_mask; struct flow_tnl tun_key, tun_key_mask; - unsigned long long tun_id_mask; int n = -1; - if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx," - "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT - "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n", - tun_id_s, &tun_id_mask, - IP_SCAN_ARGS(&tun_key.ip_src), - IP_SCAN_ARGS(&tun_key_mask.ip_src), - IP_SCAN_ARGS(&tun_key.ip_dst), - IP_SCAN_ARGS(&tun_key_mask.ip_dst), - &tos, &tos_mask, &ttl, &ttl_mask, - &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "tunnel(tun_id=%"SCNi64"/%"SCNi64"," + "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT + "/"IP_SCAN_FMT",tos=%"SCNi8"/%"SCNi8"," + "ttl=%"SCNi8"/%"SCNi8",flags%n", + &tun_id, &tun_id_mask, + IP_SCAN_ARGS(&tun_key.ip_src), + IP_SCAN_ARGS(&tun_key_mask.ip_src), + IP_SCAN_ARGS(&tun_key.ip_dst), + IP_SCAN_ARGS(&tun_key_mask.ip_dst), + &tun_key.ip_tos, &tun_key_mask.ip_tos, + &tun_key.ip_ttl, &tun_key_mask.ip_ttl, &n)) { int res; uint32_t flags; - tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0)); + tun_key.tun_id = htonll(tun_id); tun_key_mask.tun_id = htonll(tun_id_mask); - tun_key.ip_tos = tos; - tun_key_mask.ip_tos = tos_mask; - tun_key.ip_ttl = ttl; - tun_key_mask.ip_ttl = ttl_mask; res = parse_flags(&s[n], flow_tun_flag_to_string, &flags); tun_key.flags = flags; tun_key_mask.flags = UINT16_MAX; @@ -1655,18 +1719,16 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, tun_key_to_attr(mask, &tun_key_mask); } return n; - } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]," - "src="IP_SCAN_FMT",dst="IP_SCAN_FMT - ",tos=%i,ttl=%i,flags%n", tun_id_s, - IP_SCAN_ARGS(&tun_key.ip_src), - IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl, - &n) > 0 && n > 0) { + } else if (ovs_scan(s, "tunnel(tun_id=%"SCNi64"," + "src="IP_SCAN_FMT",dst="IP_SCAN_FMT + ",tos=%"SCNi8",ttl=%"SCNi8",flags%n", &tun_id, + IP_SCAN_ARGS(&tun_key.ip_src), + IP_SCAN_ARGS(&tun_key.ip_dst), + &tun_key.ip_tos, &tun_key.ip_ttl, &n)) { int res; uint32_t flags; - tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0)); - tun_key.ip_tos = tos; - tun_key.ip_ttl = ttl; + tun_key.tun_id = htonll(tun_id); res = parse_flags(&s[n], flow_tun_flag_to_string, &flags); tun_key.flags = flags; @@ -1689,16 +1751,16 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, } { - unsigned long long int in_port; - unsigned long long int in_port_mask; + uint32_t in_port; + uint32_t in_port_mask; int n = -1; - if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port, - &in_port_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "in_port(%"SCNi32"/%"SCNi32")%n", + &in_port, &in_port_mask, &n)) { nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port); nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask); return n; - } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "in_port(%"SCNi32")%n", &in_port, &n)) { nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port); if (mask) { nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX); @@ -1731,23 +1793,22 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, struct ovs_key_ethernet eth_key_mask; int n = -1; - if (mask && sscanf(s, - "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," - "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", - ETH_ADDR_SCAN_ARGS(eth_key.eth_src), - ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src), - ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), - ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) { - + if (mask && ovs_scan(s, + "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," + "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", + ETH_ADDR_SCAN_ARGS(eth_key.eth_src), + ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src), + ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), + ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n)) { nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET, ð_key, sizeof eth_key); nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET, ð_key_mask, sizeof eth_key_mask); return n; - } else if (sscanf(s, - "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n", - ETH_ADDR_SCAN_ARGS(eth_key.eth_src), - ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) { + } else if (ovs_scan(s, "eth(src="ETH_ADDR_SCAN_FMT"," + "dst="ETH_ADDR_SCAN_FMT")%n", + ETH_ADDR_SCAN_ARGS(eth_key.eth_src), + ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n)) { nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET, ð_key, sizeof eth_key); @@ -1761,13 +1822,13 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, } { - uint16_t vid, vid_mask; + int vid, vid_mask; int pcp, pcp_mask; int cfi, cfi_mask; int n = -1; - if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n", - &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) { + if (mask && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i)%n", + &vid, &vid_mask, &pcp, &pcp_mask, &n)) { nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, htons((vid << VLAN_VID_SHIFT) | (pcp << VLAN_PCP_SHIFT) | @@ -1777,8 +1838,7 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, (pcp_mask << VLAN_PCP_SHIFT) | (1 << VLAN_CFI_SHIFT))); return n; - } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", - &vid, &pcp, &n) > 0 && n > 0)) { + } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)) { nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, htons((vid << VLAN_VID_SHIFT) | (pcp << VLAN_PCP_SHIFT) | @@ -1787,8 +1847,10 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX); } return n; - } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n", - &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) { + } else if (mask + && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i,cfi=%i/%i)%n", + &vid, &vid_mask, &pcp, &pcp_mask, + &cfi, &cfi_mask, &n)) { nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, htons((vid << VLAN_VID_SHIFT) | (pcp << VLAN_PCP_SHIFT) | @@ -1798,8 +1860,8 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, (pcp_mask << VLAN_PCP_SHIFT) | (cfi_mask << VLAN_CFI_SHIFT))); return n; - } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n", - &vid, &pcp, &cfi, &n) > 0 && n > 0)) { + } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i,cfi=%i)%n", + &vid, &pcp, &cfi, &n)) { nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN, htons((vid << VLAN_VID_SHIFT) | (pcp << VLAN_PCP_SHIFT) | @@ -1816,14 +1878,14 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, int eth_type_mask; int n = -1; - if (mask && sscanf(s, "eth_type(%i/%i)%n", - ð_type, ð_type_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "eth_type(%i/%i)%n", + ð_type, ð_type_mask, &n)) { if (eth_type != 0) { nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type)); } nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask)); return n; - } else if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "eth_type(%i)%n", ð_type, &n)) { nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type)); if (mask) { nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); @@ -1837,8 +1899,10 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, int label_mask, tc_mask, ttl_mask, bos_mask; int n = -1; - if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n", - &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "mpls(label=%i/%i,tc=%i/%i," + "ttl=%i/%i,bos=%i/%i)%n", + &label, &label_mask, &tc, &tc_mask, + &ttl, &ttl_mask, &bos, &bos_mask, &n)) { struct ovs_key_mpls *mpls, *mpls_mask; mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS, @@ -1850,9 +1914,8 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, mpls_mask->mpls_lse = mpls_lse_from_components( label_mask, tc_mask, ttl_mask, bos_mask); return n; - } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n", - &label, &tc, &ttl, &bos, &n) > 0 && - n > 0) { + } else if (ovs_scan(s, "mpls(label=%i,tc=%i,ttl=%i,bos=%i)%n", + &label, &tc, &ttl, &bos, &n)) { struct ovs_key_mpls *mpls; mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS, @@ -1869,61 +1932,46 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, { - ovs_be32 ipv4_src, ipv4_src_mask; - ovs_be32 ipv4_dst, ipv4_dst_mask; - int ipv4_proto, ipv4_proto_mask; - int ipv4_tos, ipv4_tos_mask; - int ipv4_ttl, ipv4_ttl_mask; + struct ovs_key_ipv4 ipv4_key; + struct ovs_key_ipv4 ipv4_mask; + char frag[8]; - int ipv4_frag_mask; enum ovs_frag_type ipv4_frag; int n = -1; - if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT"," - "dst="IP_SCAN_FMT"/"IP_SCAN_FMT"," - "proto=%i/%i,tos=%i/%i,ttl=%i/%i," - "frag=%7[a-z]/%i)%n", - IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask), - IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask), - &ipv4_proto, &ipv4_proto_mask, - &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask, - frag, &ipv4_frag_mask, &n) > 0 - && n > 0 + if (mask + && ovs_scan(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT"," + "dst="IP_SCAN_FMT"/"IP_SCAN_FMT"," + "proto=%"SCNi8"/%"SCNi8"," + "tos=%"SCNi8"/%"SCNi8"," + "ttl=%"SCNi8"/%"SCNi8"," + "frag=%7[a-z]/%"SCNi8")%n", + IP_SCAN_ARGS(&ipv4_key.ipv4_src), + IP_SCAN_ARGS(&ipv4_mask.ipv4_src), + IP_SCAN_ARGS(&ipv4_key.ipv4_dst), + IP_SCAN_ARGS(&ipv4_mask.ipv4_dst), + &ipv4_key.ipv4_proto, &ipv4_mask.ipv4_proto, + &ipv4_key.ipv4_tos, &ipv4_mask.ipv4_tos, + &ipv4_key.ipv4_ttl, &ipv4_mask.ipv4_ttl, + frag, &ipv4_mask.ipv4_frag, &n) && ovs_frag_type_from_string(frag, &ipv4_frag)) { - struct ovs_key_ipv4 ipv4_key; - struct ovs_key_ipv4 ipv4_mask; - - ipv4_key.ipv4_src = ipv4_src; - ipv4_key.ipv4_dst = ipv4_dst; - ipv4_key.ipv4_proto = ipv4_proto; - ipv4_key.ipv4_tos = ipv4_tos; - ipv4_key.ipv4_ttl = ipv4_ttl; ipv4_key.ipv4_frag = ipv4_frag; nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4, &ipv4_key, sizeof ipv4_key); - ipv4_mask.ipv4_src = ipv4_src_mask; - ipv4_mask.ipv4_dst = ipv4_dst_mask; - ipv4_mask.ipv4_proto = ipv4_proto_mask; - ipv4_mask.ipv4_tos = ipv4_tos_mask; - ipv4_mask.ipv4_ttl = ipv4_ttl_mask; - ipv4_mask.ipv4_frag = ipv4_frag_mask; nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4, &ipv4_mask, sizeof ipv4_mask); return n; - } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT"," - "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n", - IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst), - &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0 - && n > 0 - && ovs_frag_type_from_string(frag, &ipv4_frag)) { - struct ovs_key_ipv4 ipv4_key; - - ipv4_key.ipv4_src = ipv4_src; - ipv4_key.ipv4_dst = ipv4_dst; - ipv4_key.ipv4_proto = ipv4_proto; - ipv4_key.ipv4_tos = ipv4_tos; - ipv4_key.ipv4_ttl = ipv4_ttl; + } else if (ovs_scan(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT"," + "proto=%"SCNi8",tos=%"SCNi8",ttl=%"SCNi8"," + "frag=%7[a-z])%n", + IP_SCAN_ARGS(&ipv4_key.ipv4_src), + IP_SCAN_ARGS(&ipv4_key.ipv4_dst), + &ipv4_key.ipv4_proto, + &ipv4_key.ipv4_tos, + &ipv4_key.ipv4_ttl, + frag, &n) > 0 + && ovs_frag_type_from_string(frag, &ipv4_frag)) { ipv4_key.ipv4_frag = ipv4_frag; nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4, &ipv4_key, sizeof ipv4_key); @@ -1951,16 +1999,16 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, int ipv6_frag_mask; int n = -1; - if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst=" - IPV6_SCAN_FMT"/"IPV6_SCAN_FMT"," - "label=%i/%i,proto=%i/%i,tclass=%i/%i," - "hlimit=%i/%i,frag=%7[a-z]/%i)%n", - ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s, - &ipv6_label, &ipv6_label_mask, &ipv6_proto, - &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask, - &ipv6_hlimit, &ipv6_hlimit_mask, frag, - &ipv6_frag_mask, &n) > 0 - && n > 0 + if (mask && ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst=" + IPV6_SCAN_FMT"/"IPV6_SCAN_FMT"," + "label=%i/%i,proto=%i/%i,tclass=%i/%i," + "hlimit=%i/%i,frag=%7[a-z]/%i)%n", + ipv6_src_s, ipv6_src_mask_s, + ipv6_dst_s, ipv6_dst_mask_s, + &ipv6_label, &ipv6_label_mask, &ipv6_proto, + &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask, + &ipv6_hlimit, &ipv6_hlimit_mask, frag, + &ipv6_frag_mask, &n) && ovs_frag_type_from_string(frag, &ipv6_frag)) { struct ovs_key_ipv6 ipv6_key; struct ovs_key_ipv6 ipv6_mask; @@ -1988,12 +2036,12 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6, &ipv6_mask, sizeof ipv6_mask); return n; - } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT"," - "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n", - ipv6_src_s, ipv6_dst_s, &ipv6_label, - &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0 - && n > 0 - && ovs_frag_type_from_string(frag, &ipv6_frag)) { + } else if (ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT"," + "label=%i,proto=%i,tclass=%i,hlimit=%i," + "frag=%7[a-z])%n", + ipv6_src_s, ipv6_dst_s, &ipv6_label, + &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) + && ovs_frag_type_from_string(frag, &ipv6_frag)) { struct ovs_key_ipv6 ipv6_key; if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 || @@ -2024,9 +2072,9 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, int tcp_dst_mask; int n = -1; - if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n", - &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0 - && n > 0) { + if (mask && ovs_scan(s, "tcp(src=%i/%i,dst=%i/%i)%n", + &tcp_src, &tcp_src_mask, &tcp_dst, + &tcp_dst_mask, &n)) { struct ovs_key_tcp tcp_key; struct ovs_key_tcp tcp_mask; @@ -2039,8 +2087,8 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP, &tcp_mask, sizeof tcp_mask); return n; - } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "tcp(src=%i,dst=%i)%n", + &tcp_src, &tcp_dst, &n)) { struct ovs_key_tcp tcp_key; tcp_key.tcp_src = htons(tcp_src); @@ -2060,12 +2108,12 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, uint16_t tcp_flags, tcp_flags_mask; int n = -1; - if (mask && sscanf(s, "tcp_flags(%"SCNi16"/%"SCNi16")%n", - &tcp_flags, &tcp_flags_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "tcp_flags(%"SCNi16"/%"SCNi16")%n", + &tcp_flags, &tcp_flags_mask, &n) > 0 && n > 0) { nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags)); nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags_mask)); return n; - } else if (sscanf(s, "tcp_flags(%"SCNi16")%n", &tcp_flags, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "tcp_flags(%"SCNi16")%n", &tcp_flags, &n)) { nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags)); if (mask) { nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, @@ -2082,9 +2130,9 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, int udp_dst_mask; int n = -1; - if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n", - &udp_src, &udp_src_mask, - &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "udp(src=%i/%i,dst=%i/%i)%n", + &udp_src, &udp_src_mask, + &udp_dst, &udp_dst_mask, &n)) { struct ovs_key_udp udp_key; struct ovs_key_udp udp_mask; @@ -2098,8 +2146,7 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, &udp_mask, sizeof udp_mask); return n; } - if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0 - && n > 0) { + if (ovs_scan(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n)) { struct ovs_key_udp udp_key; udp_key.udp_src = htons(udp_src); @@ -2121,9 +2168,9 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, int sctp_dst_mask; int n = -1; - if (mask && sscanf(s, "sctp(src=%i/%i,dst=%i/%i)%n", - &sctp_src, &sctp_src_mask, - &sctp_dst, &sctp_dst_mask, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "sctp(src=%i/%i,dst=%i/%i)%n", + &sctp_src, &sctp_src_mask, + &sctp_dst, &sctp_dst_mask, &n)) { struct ovs_key_sctp sctp_key; struct ovs_key_sctp sctp_mask; @@ -2137,8 +2184,7 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, &sctp_mask, sizeof sctp_mask); return n; } - if (sscanf(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n) > 0 - && n > 0) { + if (ovs_scan(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n)) { struct ovs_key_sctp sctp_key; sctp_key.sctp_src = htons(sctp_src); @@ -2154,35 +2200,21 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, } { - int icmp_type; - int icmp_code; - int icmp_type_mask; - int icmp_code_mask; + struct ovs_key_icmp icmp_key; + struct ovs_key_icmp icmp_mask; int n = -1; - if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n", - &icmp_type, &icmp_type_mask, - &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) { - struct ovs_key_icmp icmp_key; - struct ovs_key_icmp icmp_mask; - - icmp_key.icmp_type = icmp_type; - icmp_key.icmp_code = icmp_code; + if (mask && ovs_scan(s, "icmp(type=%"SCNi8"/%"SCNi8"," + "code=%"SCNi8"/%"SCNi8")%n", + &icmp_key.icmp_type, &icmp_mask.icmp_type, + &icmp_key.icmp_code, &icmp_mask.icmp_code, &n)) { nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP, &icmp_key, sizeof icmp_key); - - icmp_mask.icmp_type = icmp_type_mask; - icmp_mask.icmp_code = icmp_code_mask; nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_mask, sizeof icmp_mask); return n; - } else if (sscanf(s, "icmp(type=%i,code=%i)%n", - &icmp_type, &icmp_code, &n) > 0 - && n > 0) { - struct ovs_key_icmp icmp_key; - - icmp_key.icmp_type = icmp_type; - icmp_key.icmp_code = icmp_code; + } else if (ovs_scan(s, "icmp(type=%"SCNi8",code=%"SCNi8")%n", + &icmp_key.icmp_type, &icmp_key.icmp_code, &n)) { nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP, &icmp_key, sizeof icmp_key); if (mask) { @@ -2197,25 +2229,21 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, { struct ovs_key_icmpv6 icmpv6_key; struct ovs_key_icmpv6 icmpv6_mask; - int icmpv6_type_mask; - int icmpv6_code_mask; int n = -1; - if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n", - &icmpv6_key.icmpv6_type, &icmpv6_type_mask, - &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0 - && n > 0) { + if (mask && ovs_scan(s, "icmpv6(type=%"SCNi8"/%"SCNi8"," + "code=%"SCNi8"/%"SCNi8")%n", + &icmpv6_key.icmpv6_type, &icmpv6_mask.icmpv6_type, + &icmpv6_key.icmpv6_code, &icmpv6_mask.icmpv6_code, + &n)) { nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6, &icmpv6_key, sizeof icmpv6_key); - - icmpv6_mask.icmpv6_type = icmpv6_type_mask; - icmpv6_mask.icmpv6_code = icmpv6_code_mask; nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask, sizeof icmpv6_mask); return n; - } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n", - &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0 - && n > 0) { + } else if (ovs_scan(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n", + &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code, + &n)) { nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6, &icmpv6_key, sizeof icmpv6_key); @@ -2229,60 +2257,40 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, } { - ovs_be32 arp_sip, arp_sip_mask; - ovs_be32 arp_tip, arp_tip_mask; - int arp_op, arp_op_mask; - uint8_t arp_sha[ETH_ADDR_LEN]; - uint8_t arp_sha_mask[ETH_ADDR_LEN]; - uint8_t arp_tha[ETH_ADDR_LEN]; - uint8_t arp_tha_mask[ETH_ADDR_LEN]; + struct ovs_key_arp arp_key; + struct ovs_key_arp arp_mask; + uint16_t arp_op, arp_op_mask; int n = -1; - if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT"," - "tip="IP_SCAN_FMT"/"IP_SCAN_FMT"," - "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," - "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", - IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask), - IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask), - &arp_op, &arp_op_mask, - ETH_ADDR_SCAN_ARGS(arp_sha), - ETH_ADDR_SCAN_ARGS(arp_sha_mask), - ETH_ADDR_SCAN_ARGS(arp_tha), - ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) { - struct ovs_key_arp arp_key; - struct ovs_key_arp arp_mask; - - memset(&arp_key, 0, sizeof arp_key); - arp_key.arp_sip = arp_sip; - arp_key.arp_tip = arp_tip; + if (mask && ovs_scan(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT"," + "tip="IP_SCAN_FMT"/"IP_SCAN_FMT"," + "op=%"SCNi16"/%"SCNi16"," + "sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," + "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", + IP_SCAN_ARGS(&arp_key.arp_sip), + IP_SCAN_ARGS(&arp_mask.arp_sip), + IP_SCAN_ARGS(&arp_key.arp_tip), + IP_SCAN_ARGS(&arp_mask.arp_tip), + &arp_op, &arp_op_mask, + ETH_ADDR_SCAN_ARGS(arp_key.arp_sha), + ETH_ADDR_SCAN_ARGS(arp_mask.arp_sha), + ETH_ADDR_SCAN_ARGS(arp_key.arp_tha), + ETH_ADDR_SCAN_ARGS(arp_mask.arp_tha), &n)) { arp_key.arp_op = htons(arp_op); - memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN); - memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN); nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); - - arp_mask.arp_sip = arp_sip_mask; - arp_mask.arp_tip = arp_tip_mask; arp_mask.arp_op = htons(arp_op_mask); - memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN); - memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN); nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP, &arp_mask, sizeof arp_mask); return n; - } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT"," - "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n", - IP_SCAN_ARGS(&arp_sip), - IP_SCAN_ARGS(&arp_tip), - &arp_op, - ETH_ADDR_SCAN_ARGS(arp_sha), - ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) { - struct ovs_key_arp arp_key; - - memset(&arp_key, 0, sizeof arp_key); - arp_key.arp_sip = arp_sip; - arp_key.arp_tip = arp_tip; + } else if (ovs_scan(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT"," + "op=%"SCNi16",sha="ETH_ADDR_SCAN_FMT"," + "tha="ETH_ADDR_SCAN_FMT")%n", + IP_SCAN_ARGS(&arp_key.arp_sip), + IP_SCAN_ARGS(&arp_key.arp_tip), + &arp_op, + ETH_ADDR_SCAN_ARGS(arp_key.arp_sha), + ETH_ADDR_SCAN_ARGS(arp_key.arp_tha), &n)) { arp_key.arp_op = htons(arp_op); - memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN); - memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN); nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key); if (mask) { @@ -2307,59 +2315,63 @@ parse_odp_key_mask_attr(const char *s, const struct simap *port_names, memset(nd_sll_mask, 0xff, sizeof nd_sll_mask); memset(nd_tll_mask, 0xff, sizeof nd_tll_mask); - if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n", - nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) { + if (mask && ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n", + nd_target_s, nd_target_mask_s, &n)) { put_nd_key(n, nd_target_s, NULL, NULL, key); put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask); - } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n", - nd_target_s, &n) > 0 && n > 0) { + } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT")%n", + nd_target_s, &n)) { put_nd_key(n, nd_target_s, NULL, NULL, key); if (mask) { put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask); } - } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT - ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", - nd_target_s, nd_target_mask_s, - ETH_ADDR_SCAN_ARGS(nd_sll), - ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) { + } else if (mask && + ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT + ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", + nd_target_s, nd_target_mask_s, + ETH_ADDR_SCAN_ARGS(nd_sll), + ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n)) { put_nd_key(n, nd_target_s, nd_sll, NULL, key); put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask); - } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n", - nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT"," + "sll="ETH_ADDR_SCAN_FMT")%n", + nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n)) { put_nd_key(n, nd_target_s, nd_sll, NULL, key); if (mask) { put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask); } - } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT - ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", - nd_target_s, nd_target_mask_s, - ETH_ADDR_SCAN_ARGS(nd_tll), - ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) { + } else if (mask && + ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT + ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", + nd_target_s, nd_target_mask_s, + ETH_ADDR_SCAN_ARGS(nd_tll), + ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n)) { put_nd_key(n, nd_target_s, NULL, nd_tll, key); put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask); - } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n", - nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT"," + "tll="ETH_ADDR_SCAN_FMT")%n", + nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n)) { put_nd_key(n, nd_target_s, NULL, nd_tll, key); if (mask) { put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask); } - } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT - ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," - "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", - nd_target_s, nd_target_mask_s, - ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask), - ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask), - &n) > 0 - && n > 0) { + } else if (mask && + ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT + ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"," + "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n", + nd_target_s, nd_target_mask_s, + ETH_ADDR_SCAN_ARGS(nd_sll), + ETH_ADDR_SCAN_ARGS(nd_sll_mask), + ETH_ADDR_SCAN_ARGS(nd_tll), + ETH_ADDR_SCAN_ARGS(nd_tll_mask), + &n)) { put_nd_key(n, nd_target_s, nd_sll, nd_tll, key); put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask); - } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT"," - "tll="ETH_ADDR_SCAN_FMT")%n", - nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), - ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0 - && n > 0) { + } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT"," + "sll="ETH_ADDR_SCAN_FMT"," + "tll="ETH_ADDR_SCAN_FMT")%n", + nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), + ETH_ADDR_SCAN_ARGS(nd_tll), &n)) { put_nd_key(n, nd_target_s, nd_sll, nd_tll, key); if (mask) { put_nd_mask(n, nd_target_mask_s, @@ -2429,7 +2441,7 @@ int odp_flow_from_string(const char *s, const struct simap *port_names, struct ofpbuf *key, struct ofpbuf *mask) { - const size_t old_size = key->size; + const size_t old_size = ofpbuf_size(key); for (;;) { int retval; @@ -2440,7 +2452,7 @@ odp_flow_from_string(const char *s, const struct simap *port_names, retval = parse_odp_key_mask_attr(s, port_names, key, mask); if (retval < 0) { - key->size = old_size; + ofpbuf_set_size(key, old_size); return -retval; } s += retval; @@ -2469,28 +2481,33 @@ ovs_to_odp_frag_mask(uint8_t nw_frag_mask) } static void -odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, - const struct flow *flow, odp_port_t odp_in_port) +odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow, + const struct flow *mask, odp_port_t odp_in_port, + size_t max_mpls_depth, bool export_mask) { - bool is_mask; struct ovs_key_ethernet *eth_key; size_t encap; - - /* We assume that if 'data' and 'flow' are not the same, we should - * treat 'data' as a mask. */ - is_mask = (data != flow); + const struct flow *data = export_mask ? mask : flow; nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority); - if (flow->tunnel.ip_dst || is_mask) { + if (flow->tunnel.ip_dst || export_mask) { tun_key_to_attr(buf, &data->tunnel); } nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark); + if (data->recirc_id || (mask && mask->recirc_id)) { + nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id); + } + + if (data->dp_hash || (mask && mask->dp_hash)) { + nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash); + } + /* Add an ingress port attribute if this is a mask or 'odp_in_port' * is not the magical value "ODPP_NONE". */ - if (is_mask || odp_in_port != ODPP_NONE) { + if (export_mask || odp_in_port != ODPP_NONE) { nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port); } @@ -2500,7 +2517,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN); if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) { - if (is_mask) { + if (export_mask) { nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); } else { nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN)); @@ -2526,7 +2543,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, * 0xffff Any non-Ethernet II frame (except valid * 802.3 SNAP packet with valid eth_type). */ - if (is_mask) { + if (export_mask) { nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX); } goto unencap; @@ -2544,7 +2561,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, ipv4_key->ipv4_proto = data->nw_proto; ipv4_key->ipv4_tos = data->nw_tos; ipv4_key->ipv4_ttl = data->nw_ttl; - ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag) + ipv4_key->ipv4_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag) : ovs_to_odp_frag(data->nw_frag); } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) { struct ovs_key_ipv6 *ipv6_key; @@ -2557,7 +2574,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, ipv6_key->ipv6_proto = data->nw_proto; ipv6_key->ipv6_tclass = data->nw_tos; ipv6_key->ipv6_hlimit = data->nw_ttl; - ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag) + ipv6_key->ipv6_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag) : ovs_to_odp_frag(data->nw_frag); } else if (flow->dl_type == htons(ETH_TYPE_ARP) || flow->dl_type == htons(ETH_TYPE_RARP)) { @@ -2572,10 +2589,15 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN); } else if (eth_type_mpls(flow->dl_type)) { struct ovs_key_mpls *mpls_key; + int i, n; + n = flow_count_mpls_labels(flow, NULL); + n = MIN(n, max_mpls_depth); mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS, - sizeof *mpls_key); - mpls_key->mpls_lse = data->mpls_lse; + n * sizeof *mpls_key); + for (i = 0; i < n; i++) { + mpls_key[i].mpls_lse = data->mpls_lse[i]; + } } if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) { @@ -2624,7 +2646,7 @@ odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data, if (flow->tp_dst == htons(0) && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) && - (!is_mask || (data->tp_src == htons(0xffff) && + (!export_mask || (data->tp_src == htons(0xffff) && data->tp_dst == htons(0xffff)))) { struct ovs_key_nd *nd_key; @@ -2655,9 +2677,9 @@ unencap: * capable of being expanded to allow for that much space. */ void odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow, - odp_port_t odp_in_port) + const struct flow *mask, odp_port_t odp_in_port) { - odp_flow_key_from_flow__(buf, flow, flow, odp_in_port); + odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, false); } /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to @@ -2670,9 +2692,94 @@ odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow, * capable of being expanded to allow for that much space. */ void odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask, - const struct flow *flow, uint32_t odp_in_port_mask) + const struct flow *flow, uint32_t odp_in_port_mask, + size_t max_mpls_depth) +{ + odp_flow_key_from_flow__(buf, flow, mask, + u32_to_odp(odp_in_port_mask), max_mpls_depth, true); +} + +/* Generate ODP flow key from the given packet metadata */ +void +odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md) { - odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask)); + nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority); + + if (md->tunnel.ip_dst) { + tun_key_to_attr(buf, &md->tunnel); + } + + nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark); + + /* Add an ingress port attribute if 'odp_in_port' is not the magical + * value "ODPP_NONE". */ + if (md->in_port.odp_port != ODPP_NONE) { + nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port); + } +} + +/* Generate packet metadata from the given ODP flow key. */ +void +odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len, + struct pkt_metadata *md) +{ + const struct nlattr *nla; + size_t left; + uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY | + 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL | + 1u << OVS_KEY_ATTR_IN_PORT; + + *md = PKT_METADATA_INITIALIZER(ODPP_NONE); + + NL_ATTR_FOR_EACH (nla, left, key, key_len) { + uint16_t type = nl_attr_type(nla); + size_t len = nl_attr_get_size(nla); + int expected_len = odp_flow_key_attr_len(type); + + if (len != expected_len && expected_len >= 0) { + continue; + } + + switch (type) { + case OVS_KEY_ATTR_RECIRC_ID: + md->recirc_id = nl_attr_get_u32(nla); + wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID); + break; + case OVS_KEY_ATTR_DP_HASH: + md->dp_hash = nl_attr_get_u32(nla); + wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH); + break; + case OVS_KEY_ATTR_PRIORITY: + md->skb_priority = nl_attr_get_u32(nla); + wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY); + break; + case OVS_KEY_ATTR_SKB_MARK: + md->pkt_mark = nl_attr_get_u32(nla); + wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK); + break; + case OVS_KEY_ATTR_TUNNEL: { + enum odp_key_fitness res; + + res = odp_tun_key_from_attr(nla, &md->tunnel); + if (res == ODP_FIT_ERROR) { + memset(&md->tunnel, 0, sizeof md->tunnel); + } else if (res == ODP_FIT_PERFECT) { + wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL); + } + break; + } + case OVS_KEY_ATTR_IN_PORT: + md->in_port.odp_port = nl_attr_get_odp_port(nla); + wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT); + break; + default: + break; + } + + if (!wanted_attrs) { + return; /* Have everything. */ + } + } } uint32_t @@ -2755,7 +2862,7 @@ parse_flow_nlattrs(const struct nlattr *key, size_t key_len, if (len != expected_len && expected_len >= 0) { char namebuf[OVS_KEY_ATTR_BUFSIZE]; - VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have " + VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have " "length %d", ovs_key_attr_to_string(type, namebuf, sizeof namebuf), len, expected_len); @@ -2860,21 +2967,50 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], enum ovs_key_attr expected_bit = 0xff; if (eth_type_mpls(src_flow->dl_type)) { + size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]); + const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]); + int n = size / sizeof(ovs_be32); + int i; + + if (!size || size % sizeof(ovs_be32)) { + return ODP_FIT_ERROR; + } + if (!is_mask) { expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) { return ODP_FIT_TOO_LITTLE; } - flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]); } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) { - flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]); - - if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) { + if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) { return ODP_FIT_ERROR; } expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS); } + + for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) { + flow->mpls_lse[i] = mpls_lse[i]; + } + if (n > FLOW_MAX_MPLS_LABELS) { + return ODP_FIT_TOO_MUCH; + } + + if (!is_mask) { + /* BOS may be set only in the innermost label. */ + for (i = 0; i < n - 1; i++) { + if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) { + return ODP_FIT_ERROR; + } + } + + /* BOS must be set in the innermost label. */ + if (n < FLOW_MAX_MPLS_LABELS + && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) { + return ODP_FIT_TOO_LITTLE; + } + } + goto done; } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) { if (!is_mask) { @@ -2950,7 +3086,7 @@ parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], } else { goto done; } - if (is_mask) { + if (check_len > 0) { /* Happens only when 'is_mask'. */ if (!is_all_zeros(check_start, check_len) && flow->dl_type != htons(0xffff)) { return ODP_FIT_ERROR; @@ -3093,7 +3229,6 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], ? attrs[OVS_KEY_ATTR_ENCAP] : NULL); enum odp_key_fitness encap_fitness; enum odp_key_fitness fitness; - ovs_be16 tci; /* Calculate fitness of outer attributes. */ if (!is_mask) { @@ -3110,33 +3245,32 @@ parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1], fitness = check_expectations(present_attrs, out_of_range_attr, expected_attrs, key, key_len); - /* Get the VLAN TCI value. */ - if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) { - return ODP_FIT_TOO_LITTLE; + /* Set vlan_tci. + * Remove the TPID from dl_type since it's not the real Ethertype. */ + flow->dl_type = htons(0); + flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN) + ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]) + : htons(0)); + if (!is_mask) { + if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) { + return ODP_FIT_TOO_LITTLE; + } else if (flow->vlan_tci == htons(0)) { + /* Corner case for a truncated 802.1Q header. */ + if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { + return ODP_FIT_TOO_MUCH; + } + return fitness; + } else if (!(flow->vlan_tci & htons(VLAN_CFI))) { + VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " + "but CFI bit is not set", ntohs(flow->vlan_tci)); + return ODP_FIT_ERROR; + } } else { - tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]); - if (!is_mask) { - if (tci == htons(0)) { - /* Corner case for a truncated 802.1Q header. */ - if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) { - return ODP_FIT_TOO_MUCH; - } - return fitness; - } else if (!(tci & htons(VLAN_CFI))) { - VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero " - "but CFI bit is not set", ntohs(tci)); - return ODP_FIT_ERROR; - } + if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { + return fitness; } - /* Set vlan_tci. - * Remove the TPID from dl_type since it's not the real Ethertype. */ - flow->dl_type = htons(0); - flow->vlan_tci = tci; } - if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) { - return fitness; - } /* Now parse the encapsulated attributes. */ if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap), attrs, &present_attrs, &out_of_range_attr)) { @@ -3175,6 +3309,18 @@ odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len, expected_attrs = 0; /* Metadata. */ + if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) { + flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]); + expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID; + } else if (is_mask) { + /* Always exact match recirc_id when datapath does not sepcify it. */ + flow->recirc_id = UINT32_MAX; + } + + if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) { + flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]); + expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH; + } if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) { flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]); expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY; @@ -3225,8 +3371,9 @@ odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len, return ODP_FIT_ERROR; } - if ((is_mask && (src_flow->vlan_tci & htons(VLAN_CFI))) || - (!is_mask && src_flow->dl_type == htons(ETH_TYPE_VLAN))) { + if (is_mask + ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0 + : src_flow->dl_type == htons(ETH_TYPE_VLAN)) { return parse_8021q_onward(attrs, present_attrs, out_of_range_attr, expected_attrs, flow, key, key_len, src_flow); } @@ -3308,7 +3455,7 @@ odp_put_userspace_action(uint32_t pid, offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); if (userdata) { - userdata_ofs = odp_actions->size + NLA_HDRLEN; + userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN; /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel * module before Linux 3.10 required the userdata to be exactly 8 bytes @@ -3405,19 +3552,26 @@ commit_set_ether_addr_action(const struct flow *flow, struct flow *base, } static void -commit_vlan_action(ovs_be16 vlan_tci, struct flow *base, - struct ofpbuf *odp_actions, struct flow_wildcards *wc) +pop_vlan(struct flow *base, + struct ofpbuf *odp_actions, struct flow_wildcards *wc) { - if (base->vlan_tci == vlan_tci) { - return; - } - memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci); if (base->vlan_tci & htons(VLAN_CFI)) { nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); + base->vlan_tci = 0; } +} +static void +commit_vlan_action(ovs_be16 vlan_tci, struct flow *base, + struct ofpbuf *odp_actions, struct flow_wildcards *wc) +{ + if (base->vlan_tci == vlan_tci) { + return; + } + + pop_vlan(base, odp_actions, wc); if (vlan_tci & htons(VLAN_CFI)) { struct ovs_action_push_vlan vlan; @@ -3431,43 +3585,68 @@ commit_vlan_action(ovs_be16 vlan_tci, struct flow *base, static void commit_mpls_action(const struct flow *flow, struct flow *base, - struct ofpbuf *odp_actions, struct flow_wildcards *wc, - int *mpls_depth_delta) + struct ofpbuf *odp_actions, struct flow_wildcards *wc) { - if (flow->mpls_lse == base->mpls_lse && !*mpls_depth_delta) { - return; + int base_n = flow_count_mpls_labels(base, wc); + int flow_n = flow_count_mpls_labels(flow, wc); + int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n, + wc); + + while (base_n > common_n) { + if (base_n - 1 == common_n && flow_n > common_n) { + /* If there is only one more LSE in base than there are common + * between base and flow; and flow has at least one more LSE than + * is common then the topmost LSE of base may be updated using + * set */ + struct ovs_key_mpls mpls_key; + + mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n]; + commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, + &mpls_key, sizeof mpls_key); + flow_set_mpls_lse(base, 0, mpls_key.mpls_lse); + common_n++; + } else { + /* Otherwise, if there more LSEs in base than are common between + * base and flow then pop the topmost one. */ + ovs_be16 dl_type; + bool popped; + + /* If all the LSEs are to be popped and this is not the outermost + * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the + * POP_MPLS action instead of flow->dl_type. + * + * This is because the POP_MPLS action requires its ethertype + * argument to be an MPLS ethernet type but in this case + * flow->dl_type will be a non-MPLS ethernet type. + * + * When the final POP_MPLS action occurs it use flow->dl_type and + * the and the resulting packet will have the desired dl_type. */ + if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) { + dl_type = htons(ETH_TYPE_MPLS); + } else { + dl_type = flow->dl_type; + } + nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type); + popped = flow_pop_mpls(base, base_n, flow->dl_type, wc); + ovs_assert(popped); + base_n--; + } } - memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse); - - switch (*mpls_depth_delta) { - case -1: - nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type); - break; - case 1: { + /* If, after the above popping and setting, there are more LSEs in flow + * than base then some LSEs need to be pushed. */ + while (base_n < flow_n) { struct ovs_action_push_mpls *mpls; - mpls = nl_msg_put_unspec_zero(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS, + mpls = nl_msg_put_unspec_zero(odp_actions, + OVS_ACTION_ATTR_PUSH_MPLS, sizeof *mpls); mpls->mpls_ethertype = flow->dl_type; - mpls->mpls_lse = flow->mpls_lse; - break; - } - case 0: { - struct ovs_key_mpls mpls_key; - - mpls_key.mpls_lse = flow->mpls_lse; - commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS, - &mpls_key, sizeof(mpls_key)); - break; - } - default: - NOT_REACHED(); + mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1]; + flow_push_mpls(base, base_n, mpls->mpls_ethertype, wc); + flow_set_mpls_lse(base, 0, mpls->mpls_lse); + base_n++; } - - base->dl_type = flow->dl_type; - base->mpls_lse = flow->mpls_lse; - *mpls_depth_delta = 0; } static void @@ -3688,20 +3867,15 @@ commit_set_pkt_mark_action(const struct flow *flow, struct flow *base, * slow path, if there is one, otherwise 0. */ enum slow_path_reason commit_odp_actions(const struct flow *flow, struct flow *base, - struct ofpbuf *odp_actions, struct flow_wildcards *wc, - int *mpls_depth_delta) + struct ofpbuf *odp_actions, struct flow_wildcards *wc) { enum slow_path_reason slow; commit_set_ether_addr_action(flow, base, odp_actions, wc); - commit_vlan_action(flow->vlan_tci, base, odp_actions, wc); slow = commit_set_nw_action(flow, base, odp_actions, wc); commit_set_port_action(flow, base, odp_actions, wc); - /* Committing MPLS actions should occur after committing nw and port - * actions. This is because committing MPLS actions may alter a packet so - * that it is no longer IP and thus nw and port actions are no longer valid. - */ - commit_mpls_action(flow, base, odp_actions, wc, mpls_depth_delta); + commit_mpls_action(flow, base, odp_actions, wc); + commit_vlan_action(flow->vlan_tci, base, odp_actions, wc); commit_set_priority_action(flow, base, odp_actions, wc); commit_set_pkt_mark_action(flow, base, odp_actions, wc);