tunneling: Userspace datapath support for Geneve options.
[cascardo/ovs.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include "odp-util.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "byte-order.h"
29 #include "coverage.h"
30 #include "dpif.h"
31 #include "dynamic-string.h"
32 #include "flow.h"
33 #include "netlink.h"
34 #include "ofpbuf.h"
35 #include "packets.h"
36 #include "simap.h"
37 #include "timeval.h"
38 #include "tun-metadata.h"
39 #include "unaligned.h"
40 #include "util.h"
41 #include "uuid.h"
42 #include "openvswitch/vlog.h"
43
44 VLOG_DEFINE_THIS_MODULE(odp_util);
45
46 /* The interface between userspace and kernel uses an "OVS_*" prefix.
47  * Since this is fairly non-specific for the OVS userspace components,
48  * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
49  * interactions with the datapath.
50  */
51
52 /* The set of characters that may separate one action or one key attribute
53  * from another. */
54 static const char *delimiters = ", \t\r\n";
55
56 struct attr_len_tbl {
57     int len;
58     const struct attr_len_tbl *next;
59     int next_max;
60 };
61 #define ATTR_LEN_INVALID  -1
62 #define ATTR_LEN_VARIABLE -2
63 #define ATTR_LEN_NESTED   -3
64
65 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
66                               struct ofpbuf *, struct ofpbuf *);
67 static void format_odp_key_attr(const struct nlattr *a,
68                                 const struct nlattr *ma,
69                                 const struct hmap *portno_names, struct ds *ds,
70                                 bool verbose);
71
72 struct geneve_scan {
73     struct geneve_opt d[63];
74     int len;
75 };
76
77 static int scan_geneve(const char *s, struct geneve_scan *key,
78                        struct geneve_scan *mask);
79 static void format_geneve_opts(const struct geneve_opt *opt,
80                                const struct geneve_opt *mask, int opts_len,
81                                struct ds *, bool verbose);
82
83 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
84                                                  int max, struct ofpbuf *,
85                                                  const struct nlattr *key);
86 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
87  * 'type':
88  *
89  *   - For an action whose argument has a fixed length, returned that
90  *     nonnegative length in bytes.
91  *
92  *   - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
93  *
94  *   - For an invalid 'type', returns ATTR_LEN_INVALID. */
95 static int
96 odp_action_len(uint16_t type)
97 {
98     if (type > OVS_ACTION_ATTR_MAX) {
99         return -1;
100     }
101
102     switch ((enum ovs_action_attr) type) {
103     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
104     case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
105     case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
106     case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
107     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
108     case OVS_ACTION_ATTR_POP_VLAN: return 0;
109     case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
110     case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
111     case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
112     case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
113     case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
114     case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
115     case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
116
117     case OVS_ACTION_ATTR_UNSPEC:
118     case __OVS_ACTION_ATTR_MAX:
119         return ATTR_LEN_INVALID;
120     }
121
122     return ATTR_LEN_INVALID;
123 }
124
125 /* Returns a string form of 'attr'.  The return value is either a statically
126  * allocated constant string or the 'bufsize'-byte buffer 'namebuf'.  'bufsize'
127  * should be at least OVS_KEY_ATTR_BUFSIZE. */
128 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
129 static const char *
130 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
131 {
132     switch (attr) {
133     case OVS_KEY_ATTR_UNSPEC: return "unspec";
134     case OVS_KEY_ATTR_ENCAP: return "encap";
135     case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
136     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
137     case OVS_KEY_ATTR_TUNNEL: return "tunnel";
138     case OVS_KEY_ATTR_IN_PORT: return "in_port";
139     case OVS_KEY_ATTR_ETHERNET: return "eth";
140     case OVS_KEY_ATTR_VLAN: return "vlan";
141     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
142     case OVS_KEY_ATTR_IPV4: return "ipv4";
143     case OVS_KEY_ATTR_IPV6: return "ipv6";
144     case OVS_KEY_ATTR_TCP: return "tcp";
145     case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
146     case OVS_KEY_ATTR_UDP: return "udp";
147     case OVS_KEY_ATTR_SCTP: return "sctp";
148     case OVS_KEY_ATTR_ICMP: return "icmp";
149     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
150     case OVS_KEY_ATTR_ARP: return "arp";
151     case OVS_KEY_ATTR_ND: return "nd";
152     case OVS_KEY_ATTR_MPLS: return "mpls";
153     case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
154     case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
155
156     case __OVS_KEY_ATTR_MAX:
157     default:
158         snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
159         return namebuf;
160     }
161 }
162
163 static void
164 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
165 {
166     size_t len = nl_attr_get_size(a);
167
168     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
169     if (len) {
170         const uint8_t *unspec;
171         unsigned int i;
172
173         unspec = nl_attr_get(a);
174         for (i = 0; i < len; i++) {
175             ds_put_char(ds, i ? ' ': '(');
176             ds_put_format(ds, "%02x", unspec[i]);
177         }
178         ds_put_char(ds, ')');
179     }
180 }
181
182 static void
183 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
184 {
185     static const struct nl_policy ovs_sample_policy[] = {
186         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
187         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
188     };
189     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
190     double percentage;
191     const struct nlattr *nla_acts;
192     int len;
193
194     ds_put_cstr(ds, "sample");
195
196     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
197         ds_put_cstr(ds, "(error)");
198         return;
199     }
200
201     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
202                         UINT32_MAX;
203
204     ds_put_format(ds, "(sample=%.1f%%,", percentage);
205
206     ds_put_cstr(ds, "actions(");
207     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
208     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
209     format_odp_actions(ds, nla_acts, len);
210     ds_put_format(ds, "))");
211 }
212
213 static const char *
214 slow_path_reason_to_string(uint32_t reason)
215 {
216     switch ((enum slow_path_reason) reason) {
217 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
218         SLOW_PATH_REASONS
219 #undef SPR
220     }
221
222     return NULL;
223 }
224
225 const char *
226 slow_path_reason_to_explanation(enum slow_path_reason reason)
227 {
228     switch (reason) {
229 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
230         SLOW_PATH_REASONS
231 #undef SPR
232     }
233
234     return "<unknown>";
235 }
236
237 static int
238 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
239             uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
240 {
241     uint32_t result = 0;
242     int n;
243
244     /* Parse masked flags in numeric format? */
245     if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
246                              res_flags, res_mask, &n) && n > 0) {
247         if (*res_flags & ~allowed || *res_mask & ~allowed) {
248             return -EINVAL;
249         }
250         return n;
251     }
252
253     n = 0;
254
255     if (res_mask && (*s == '+' || *s == '-')) {
256         uint32_t flags = 0, mask = 0;
257
258         /* Parse masked flags. */
259         while (s[0] != ')') {
260             bool set;
261             uint32_t bit;
262             int name_len;
263
264             if (s[0] == '+') {
265                 set = true;
266             } else if (s[0] == '-') {
267                 set = false;
268             } else {
269                 return -EINVAL;
270             }
271             s++;
272             n++;
273
274             name_len = strcspn(s, "+-)");
275
276             for (bit = 1; bit; bit <<= 1) {
277                 const char *fname = bit_to_string(bit);
278                 size_t len;
279
280                 if (!fname) {
281                     continue;
282                 }
283
284                 len = strlen(fname);
285                 if (len != name_len) {
286                     continue;
287                 }
288                 if (!strncmp(s, fname, len)) {
289                     if (mask & bit) {
290                         /* bit already set. */
291                         return -EINVAL;
292                     }
293                     if (!(bit & allowed)) {
294                         return -EINVAL;
295                     }
296                     if (set) {
297                         flags |= bit;
298                     }
299                     mask |= bit;
300                     break;
301                 }
302             }
303
304             if (!bit) {
305                 return -EINVAL; /* Unknown flag name */
306             }
307             s += name_len;
308             n += name_len;
309         }
310
311         *res_flags = flags;
312         *res_mask = mask;
313         return n;
314     }
315
316     /* Parse unmasked flags.  If a flag is present, it is set, otherwise
317      * it is not set. */
318     while (s[n] != ')') {
319         unsigned long long int flags;
320         uint32_t bit;
321         int n0;
322
323         if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
324             if (flags & ~allowed) {
325                 return -EINVAL;
326             }
327             n += n0 + (s[n + n0] == ',');
328             result |= flags;
329             continue;
330         }
331
332         for (bit = 1; bit; bit <<= 1) {
333             const char *name = bit_to_string(bit);
334             size_t len;
335
336             if (!name) {
337                 continue;
338             }
339
340             len = strlen(name);
341             if (!strncmp(s + n, name, len) &&
342                 (s[n + len] == ',' || s[n + len] == ')')) {
343                 if (!(bit & allowed)) {
344                     return -EINVAL;
345                 }
346                 result |= bit;
347                 n += len + (s[n + len] == ',');
348                 break;
349             }
350         }
351
352         if (!bit) {
353             return -EINVAL;
354         }
355     }
356
357     *res_flags = result;
358     if (res_mask) {
359         *res_mask = UINT32_MAX;
360     }
361     return n;
362 }
363
364 static void
365 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
366 {
367     static const struct nl_policy ovs_userspace_policy[] = {
368         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
369         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
370                                           .optional = true },
371         [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
372                                                  .optional = true },
373     };
374     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
375     const struct nlattr *userdata_attr;
376     const struct nlattr *tunnel_out_port_attr;
377
378     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
379         ds_put_cstr(ds, "userspace(error)");
380         return;
381     }
382
383     ds_put_format(ds, "userspace(pid=%"PRIu32,
384                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
385
386     userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
387
388     if (userdata_attr) {
389         const uint8_t *userdata = nl_attr_get(userdata_attr);
390         size_t userdata_len = nl_attr_get_size(userdata_attr);
391         bool userdata_unspec = true;
392         union user_action_cookie cookie;
393
394         if (userdata_len >= sizeof cookie.type
395             && userdata_len <= sizeof cookie) {
396
397             memset(&cookie, 0, sizeof cookie);
398             memcpy(&cookie, userdata, userdata_len);
399
400             userdata_unspec = false;
401
402             if (userdata_len == sizeof cookie.sflow
403                 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
404                 ds_put_format(ds, ",sFlow("
405                               "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
406                               vlan_tci_to_vid(cookie.sflow.vlan_tci),
407                               vlan_tci_to_pcp(cookie.sflow.vlan_tci),
408                               cookie.sflow.output);
409             } else if (userdata_len == sizeof cookie.slow_path
410                        && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
411                 ds_put_cstr(ds, ",slow_path(");
412                 format_flags(ds, slow_path_reason_to_string,
413                              cookie.slow_path.reason, ',');
414                 ds_put_format(ds, ")");
415             } else if (userdata_len == sizeof cookie.flow_sample
416                        && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
417                 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
418                               ",collector_set_id=%"PRIu32
419                               ",obs_domain_id=%"PRIu32
420                               ",obs_point_id=%"PRIu32")",
421                               cookie.flow_sample.probability,
422                               cookie.flow_sample.collector_set_id,
423                               cookie.flow_sample.obs_domain_id,
424                               cookie.flow_sample.obs_point_id);
425             } else if (userdata_len >= sizeof cookie.ipfix
426                        && cookie.type == USER_ACTION_COOKIE_IPFIX) {
427                 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
428                               cookie.ipfix.output_odp_port);
429             } else {
430                 userdata_unspec = true;
431             }
432         }
433
434         if (userdata_unspec) {
435             size_t i;
436             ds_put_format(ds, ",userdata(");
437             for (i = 0; i < userdata_len; i++) {
438                 ds_put_format(ds, "%02x", userdata[i]);
439             }
440             ds_put_char(ds, ')');
441         }
442     }
443
444     tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
445     if (tunnel_out_port_attr) {
446         ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
447                       nl_attr_get_u32(tunnel_out_port_attr));
448     }
449
450     ds_put_char(ds, ')');
451 }
452
453 static void
454 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
455 {
456     if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
457         ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
458         if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
459             ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
460         };
461         ds_put_char(ds, ',');
462     }
463     if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
464         ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
465         if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
466             ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
467         }
468         ds_put_char(ds, ',');
469     }
470     if (!(tci & htons(VLAN_CFI))) {
471         ds_put_cstr(ds, "cfi=0");
472         ds_put_char(ds, ',');
473     }
474     ds_chomp(ds, ',');
475 }
476
477 static void
478 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
479 {
480     ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
481                   mpls_lse_to_label(mpls_lse),
482                   mpls_lse_to_tc(mpls_lse),
483                   mpls_lse_to_ttl(mpls_lse),
484                   mpls_lse_to_bos(mpls_lse));
485 }
486
487 static void
488 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
489             const struct ovs_key_mpls *mpls_mask, int n)
490 {
491     if (n == 1) {
492         ovs_be32 key = mpls_key->mpls_lse;
493
494         if (mpls_mask == NULL) {
495             format_mpls_lse(ds, key);
496         } else {
497             ovs_be32 mask = mpls_mask->mpls_lse;
498
499             ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
500                           mpls_lse_to_label(key), mpls_lse_to_label(mask),
501                           mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
502                           mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
503                           mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
504         }
505     } else {
506         int i;
507
508         for (i = 0; i < n; i++) {
509             ds_put_format(ds, "lse%d=%#"PRIx32,
510                           i, ntohl(mpls_key[i].mpls_lse));
511             if (mpls_mask) {
512                 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
513             }
514             ds_put_char(ds, ',');
515         }
516         ds_chomp(ds, ',');
517     }
518 }
519
520 static void
521 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
522 {
523     ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
524 }
525
526 static void
527 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
528 {
529     ds_put_format(ds, "hash(");
530
531     if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
532         ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
533     } else {
534         ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
535                       hash_act->hash_alg);
536     }
537     ds_put_format(ds, ")");
538 }
539
540 static const void *
541 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
542 {
543     const struct udp_header *udp;
544
545     udp = (const struct udp_header *) (ip + 1);
546     ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
547                   ntohs(udp->udp_src), ntohs(udp->udp_dst),
548                   ntohs(udp->udp_csum));
549
550     return udp + 1;
551 }
552
553 static void
554 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
555 {
556     const struct eth_header *eth;
557     const struct ip_header *ip;
558     const void *l3;
559
560     eth = (const struct eth_header *)data->header;
561
562     l3 = eth + 1;
563     ip = (const struct ip_header *)l3;
564
565     /* Ethernet */
566     ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
567                   data->header_len, data->tnl_type);
568     ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
569     ds_put_format(ds, ",src=");
570     ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
571     ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
572
573     /* IPv4 */
574     ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
575                   ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
576                   IP_ARGS(get_16aligned_be32(&ip->ip_src)),
577                   IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
578                   ip->ip_proto, ip->ip_tos,
579                   ip->ip_ttl,
580                   ip->ip_frag_off);
581
582     if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
583         const struct vxlanhdr *vxh;
584
585         vxh = format_udp_tnl_push_header(ds, ip);
586
587         ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
588                       ntohl(get_16aligned_be32(&vxh->vx_flags)),
589                       ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
590     } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
591         const struct genevehdr *gnh;
592
593         gnh = format_udp_tnl_push_header(ds, ip);
594
595         ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
596                       gnh->oam ? "oam," : "",
597                       gnh->critical ? "crit," : "",
598                       ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
599  
600         if (gnh->opt_len) {
601             ds_put_cstr(ds, ",options(");
602             format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
603                                ds, false);
604             ds_put_char(ds, ')');
605         }
606
607         ds_put_char(ds, ')');
608     } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
609         const struct gre_base_hdr *greh;
610         ovs_16aligned_be32 *options;
611         void *l4;
612
613         l4 = ((uint8_t *)l3  + sizeof(struct ip_header));
614         greh = (const struct gre_base_hdr *) l4;
615
616         ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
617                            ntohs(greh->flags), ntohs(greh->protocol));
618         options = (ovs_16aligned_be32 *)(greh + 1);
619         if (greh->flags & htons(GRE_CSUM)) {
620             ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
621             options++;
622         }
623         if (greh->flags & htons(GRE_KEY)) {
624             ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
625             options++;
626         }
627         if (greh->flags & htons(GRE_SEQ)) {
628             ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
629             options++;
630         }
631         ds_put_format(ds, ")");
632     }
633     ds_put_format(ds, ")");
634 }
635
636 static void
637 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
638 {
639     struct ovs_action_push_tnl *data;
640
641     data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
642
643     ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
644     format_odp_tnl_push_header(ds, data);
645     ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
646 }
647
648 static void
649 format_odp_action(struct ds *ds, const struct nlattr *a)
650 {
651     int expected_len;
652     enum ovs_action_attr type = nl_attr_type(a);
653     const struct ovs_action_push_vlan *vlan;
654     size_t size;
655
656     expected_len = odp_action_len(nl_attr_type(a));
657     if (expected_len != ATTR_LEN_VARIABLE &&
658         nl_attr_get_size(a) != expected_len) {
659         ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
660                       nl_attr_get_size(a), expected_len);
661         format_generic_odp_action(ds, a);
662         return;
663     }
664
665     switch (type) {
666     case OVS_ACTION_ATTR_OUTPUT:
667         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
668         break;
669     case OVS_ACTION_ATTR_TUNNEL_POP:
670         ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
671         break;
672     case OVS_ACTION_ATTR_TUNNEL_PUSH:
673         format_odp_tnl_push_action(ds, a);
674         break;
675     case OVS_ACTION_ATTR_USERSPACE:
676         format_odp_userspace_action(ds, a);
677         break;
678     case OVS_ACTION_ATTR_RECIRC:
679         format_odp_recirc_action(ds, nl_attr_get_u32(a));
680         break;
681     case OVS_ACTION_ATTR_HASH:
682         format_odp_hash_action(ds, nl_attr_get(a));
683         break;
684     case OVS_ACTION_ATTR_SET_MASKED:
685         a = nl_attr_get(a);
686         size = nl_attr_get_size(a) / 2;
687         ds_put_cstr(ds, "set(");
688
689         /* Masked set action not supported for tunnel key, which is bigger. */
690         if (size <= sizeof(struct ovs_key_ipv6)) {
691             struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
692                                                 sizeof(struct nlattr))];
693             struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
694                                                 sizeof(struct nlattr))];
695
696             mask->nla_type = attr->nla_type = nl_attr_type(a);
697             mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
698             memcpy(attr + 1, (char *)(a + 1), size);
699             memcpy(mask + 1, (char *)(a + 1) + size, size);
700             format_odp_key_attr(attr, mask, NULL, ds, false);
701         } else {
702             format_odp_key_attr(a, NULL, NULL, ds, false);
703         }
704         ds_put_cstr(ds, ")");
705         break;
706     case OVS_ACTION_ATTR_SET:
707         ds_put_cstr(ds, "set(");
708         format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
709         ds_put_cstr(ds, ")");
710         break;
711     case OVS_ACTION_ATTR_PUSH_VLAN:
712         vlan = nl_attr_get(a);
713         ds_put_cstr(ds, "push_vlan(");
714         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
715             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
716         }
717         format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
718         ds_put_char(ds, ')');
719         break;
720     case OVS_ACTION_ATTR_POP_VLAN:
721         ds_put_cstr(ds, "pop_vlan");
722         break;
723     case OVS_ACTION_ATTR_PUSH_MPLS: {
724         const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
725         ds_put_cstr(ds, "push_mpls(");
726         format_mpls_lse(ds, mpls->mpls_lse);
727         ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
728         break;
729     }
730     case OVS_ACTION_ATTR_POP_MPLS: {
731         ovs_be16 ethertype = nl_attr_get_be16(a);
732         ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
733         break;
734     }
735     case OVS_ACTION_ATTR_SAMPLE:
736         format_odp_sample_action(ds, a);
737         break;
738     case OVS_ACTION_ATTR_UNSPEC:
739     case __OVS_ACTION_ATTR_MAX:
740     default:
741         format_generic_odp_action(ds, a);
742         break;
743     }
744 }
745
746 void
747 format_odp_actions(struct ds *ds, const struct nlattr *actions,
748                    size_t actions_len)
749 {
750     if (actions_len) {
751         const struct nlattr *a;
752         unsigned int left;
753
754         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
755             if (a != actions) {
756                 ds_put_char(ds, ',');
757             }
758             format_odp_action(ds, a);
759         }
760         if (left) {
761             int i;
762
763             if (left == actions_len) {
764                 ds_put_cstr(ds, "<empty>");
765             }
766             ds_put_format(ds, ",***%u leftover bytes*** (", left);
767             for (i = 0; i < left; i++) {
768                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
769             }
770             ds_put_char(ds, ')');
771         }
772     } else {
773         ds_put_cstr(ds, "drop");
774     }
775 }
776
777 /* Separate out parse_odp_userspace_action() function. */
778 static int
779 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
780 {
781     uint32_t pid;
782     union user_action_cookie cookie;
783     struct ofpbuf buf;
784     odp_port_t tunnel_out_port;
785     int n = -1;
786     void *user_data = NULL;
787     size_t user_data_size = 0;
788
789     if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
790         return -EINVAL;
791     }
792
793     {
794         uint32_t output;
795         uint32_t probability;
796         uint32_t collector_set_id;
797         uint32_t obs_domain_id;
798         uint32_t obs_point_id;
799         int vid, pcp;
800         int n1 = -1;
801         if (ovs_scan(&s[n], ",sFlow(vid=%i,"
802                      "pcp=%i,output=%"SCNi32")%n",
803                      &vid, &pcp, &output, &n1)) {
804             uint16_t tci;
805
806             n += n1;
807             tci = vid | (pcp << VLAN_PCP_SHIFT);
808             if (tci) {
809                 tci |= VLAN_CFI;
810             }
811
812             cookie.type = USER_ACTION_COOKIE_SFLOW;
813             cookie.sflow.vlan_tci = htons(tci);
814             cookie.sflow.output = output;
815             user_data = &cookie;
816             user_data_size = sizeof cookie.sflow;
817         } else if (ovs_scan(&s[n], ",slow_path(%n",
818                             &n1)) {
819             int res;
820
821             n += n1;
822             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
823             cookie.slow_path.unused = 0;
824             cookie.slow_path.reason = 0;
825
826             res = parse_flags(&s[n], slow_path_reason_to_string,
827                               &cookie.slow_path.reason,
828                               SLOW_PATH_REASON_MASK, NULL);
829             if (res < 0 || s[n + res] != ')') {
830                 return res;
831             }
832             n += res + 1;
833
834             user_data = &cookie;
835             user_data_size = sizeof cookie.slow_path;
836         } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
837                             "collector_set_id=%"SCNi32","
838                             "obs_domain_id=%"SCNi32","
839                             "obs_point_id=%"SCNi32")%n",
840                             &probability, &collector_set_id,
841                             &obs_domain_id, &obs_point_id, &n1)) {
842             n += n1;
843
844             cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
845             cookie.flow_sample.probability = probability;
846             cookie.flow_sample.collector_set_id = collector_set_id;
847             cookie.flow_sample.obs_domain_id = obs_domain_id;
848             cookie.flow_sample.obs_point_id = obs_point_id;
849             user_data = &cookie;
850             user_data_size = sizeof cookie.flow_sample;
851         } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
852                             &output, &n1) ) {
853             n += n1;
854             cookie.type = USER_ACTION_COOKIE_IPFIX;
855             cookie.ipfix.output_odp_port = u32_to_odp(output);
856             user_data = &cookie;
857             user_data_size = sizeof cookie.ipfix;
858         } else if (ovs_scan(&s[n], ",userdata(%n",
859                             &n1)) {
860             char *end;
861
862             n += n1;
863             ofpbuf_init(&buf, 16);
864             end = ofpbuf_put_hex(&buf, &s[n], NULL);
865             if (end[0] != ')') {
866                 return -EINVAL;
867             }
868             user_data = buf.data;
869             user_data_size = buf.size;
870             n = (end + 1) - s;
871         }
872     }
873
874     {
875         int n1 = -1;
876         if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
877                      &tunnel_out_port, &n1)) {
878             odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
879             return n + n1;
880         } else if (s[n] == ')') {
881             odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
882             return n + 1;
883         }
884     }
885
886     return -EINVAL;
887 }
888
889 static int
890 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
891 {
892     struct eth_header *eth;
893     struct ip_header *ip;
894     struct udp_header *udp;
895     struct gre_base_hdr *greh;
896     uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
897     ovs_be32 sip, dip;
898     uint32_t tnl_type = 0, header_len = 0;
899     void *l3, *l4;
900     int n = 0;
901
902     if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
903         return -EINVAL;
904     }
905     eth = (struct eth_header *) data->header;
906     l3 = (data->header + sizeof *eth);
907     l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
908     ip = (struct ip_header *) l3;
909     if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
910                          "eth(dst="ETH_ADDR_SCAN_FMT",",
911                          &data->header_len,
912                          &data->tnl_type,
913                          ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
914         return -EINVAL;
915     }
916
917     if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
918                   ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
919         return -EINVAL;
920     }
921     if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
922         return -EINVAL;
923     }
924     eth->eth_type = htons(dl_type);
925
926     /* IPv4 */
927     if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
928                          ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
929                          IP_SCAN_ARGS(&sip),
930                          IP_SCAN_ARGS(&dip),
931                          &ip->ip_proto, &ip->ip_tos,
932                          &ip->ip_ttl, &ip->ip_frag_off)) {
933         return -EINVAL;
934     }
935     put_16aligned_be32(&ip->ip_src, sip);
936     put_16aligned_be32(&ip->ip_dst, dip);
937
938     /* Tunnel header */
939     udp = (struct udp_header *) l4;
940     greh = (struct gre_base_hdr *) l4;
941     if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
942                          &udp_src, &udp_dst, &csum)) {
943         uint32_t vx_flags, vni;
944
945         udp->udp_src = htons(udp_src);
946         udp->udp_dst = htons(udp_dst);
947         udp->udp_len = 0;
948         udp->udp_csum = htons(csum);
949
950         if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
951                             &vx_flags, &vni)) {
952             struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
953
954             put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
955             put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
956             tnl_type = OVS_VPORT_TYPE_VXLAN;
957             header_len = sizeof *eth + sizeof *ip +
958                          sizeof *udp + sizeof *vxh;
959         } else if (ovs_scan_len(s, &n, "geneve(")) {
960             struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
961
962             memset(gnh, 0, sizeof *gnh);
963             header_len = sizeof *eth + sizeof *ip +
964                          sizeof *udp + sizeof *gnh;
965
966             if (ovs_scan_len(s, &n, "oam,")) {
967                 gnh->oam = 1;
968             }
969             if (ovs_scan_len(s, &n, "crit,")) {
970                 gnh->critical = 1;
971             }
972             if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
973                 return -EINVAL;
974             }
975             if (ovs_scan_len(s, &n, ",options(")) {
976                 struct geneve_scan options;
977                 int len;
978
979                 memset(&options, 0, sizeof options);
980                 len = scan_geneve(s + n, &options, NULL);
981                 if (!len) {
982                     return -EINVAL;
983                 }
984
985                 memcpy(gnh->options, options.d, options.len);
986                 gnh->opt_len = options.len / 4;
987                 header_len += options.len;
988
989                 n += len;
990             }
991             if (!ovs_scan_len(s, &n, "))")) {
992                 return -EINVAL;
993             }
994
995             gnh->proto_type = htons(ETH_TYPE_TEB);
996             put_16aligned_be32(&gnh->vni, htonl(vni << 8));
997             tnl_type = OVS_VPORT_TYPE_GENEVE;
998         } else {
999             return -EINVAL;
1000         }
1001     } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
1002                          &gre_flags, &gre_proto)){
1003
1004         tnl_type = OVS_VPORT_TYPE_GRE;
1005         greh->flags = htons(gre_flags);
1006         greh->protocol = htons(gre_proto);
1007         ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
1008
1009         if (greh->flags & htons(GRE_CSUM)) {
1010             if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
1011                 return -EINVAL;
1012             }
1013
1014             memset(options, 0, sizeof *options);
1015             *((ovs_be16 *)options) = htons(csum);
1016             options++;
1017         }
1018         if (greh->flags & htons(GRE_KEY)) {
1019             uint32_t key;
1020
1021             if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
1022                 return -EINVAL;
1023             }
1024
1025             put_16aligned_be32(options, htonl(key));
1026             options++;
1027         }
1028         if (greh->flags & htons(GRE_SEQ)) {
1029             uint32_t seq;
1030
1031             if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
1032                 return -EINVAL;
1033             }
1034             put_16aligned_be32(options, htonl(seq));
1035             options++;
1036         }
1037
1038         if (!ovs_scan_len(s, &n, "))")) {
1039             return -EINVAL;
1040         }
1041
1042         header_len = sizeof *eth + sizeof *ip +
1043                      ((uint8_t *) options - (uint8_t *) greh);
1044     } else {
1045         return -EINVAL;
1046     }
1047
1048     /* check tunnel meta data. */
1049     if (data->tnl_type != tnl_type) {
1050         return -EINVAL;
1051     }
1052     if (data->header_len != header_len) {
1053         return -EINVAL;
1054     }
1055
1056     /* Out port */
1057     if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1058         return -EINVAL;
1059     }
1060
1061     return n;
1062 }
1063
1064 static int
1065 parse_odp_action(const char *s, const struct simap *port_names,
1066                  struct ofpbuf *actions)
1067 {
1068     {
1069         uint32_t port;
1070         int n;
1071
1072         if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1073             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1074             return n;
1075         }
1076     }
1077
1078     if (port_names) {
1079         int len = strcspn(s, delimiters);
1080         struct simap_node *node;
1081
1082         node = simap_find_len(port_names, s, len);
1083         if (node) {
1084             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1085             return len;
1086         }
1087     }
1088
1089     {
1090         uint32_t recirc_id;
1091         int n = -1;
1092
1093         if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1094             nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1095             return n;
1096         }
1097     }
1098
1099     if (!strncmp(s, "userspace(", 10)) {
1100         return parse_odp_userspace_action(s, actions);
1101     }
1102
1103     if (!strncmp(s, "set(", 4)) {
1104         size_t start_ofs;
1105         int retval;
1106         struct nlattr mask[128 / sizeof(struct nlattr)];
1107         struct ofpbuf maskbuf;
1108         struct nlattr *nested, *key;
1109         size_t size;
1110
1111         /* 'mask' is big enough to hold any key. */
1112         ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1113
1114         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1115         retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1116         if (retval < 0) {
1117             return retval;
1118         }
1119         if (s[retval + 4] != ')') {
1120             return -EINVAL;
1121         }
1122
1123         nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1124         key = nested + 1;
1125
1126         size = nl_attr_get_size(mask);
1127         if (size == nl_attr_get_size(key)) {
1128             /* Change to masked set action if not fully masked. */
1129             if (!is_all_ones(mask + 1, size)) {
1130                 key->nla_len += size;
1131                 ofpbuf_put(actions, mask + 1, size);
1132                 /* 'actions' may have been reallocated by ofpbuf_put(). */
1133                 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1134                 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1135             }
1136         }
1137
1138         nl_msg_end_nested(actions, start_ofs);
1139         return retval + 5;
1140     }
1141
1142     {
1143         struct ovs_action_push_vlan push;
1144         int tpid = ETH_TYPE_VLAN;
1145         int vid, pcp;
1146         int cfi = 1;
1147         int n = -1;
1148
1149         if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1150             || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1151                         &vid, &pcp, &cfi, &n)
1152             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1153                         &tpid, &vid, &pcp, &n)
1154             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1155                         &tpid, &vid, &pcp, &cfi, &n)) {
1156             push.vlan_tpid = htons(tpid);
1157             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1158                                   | (pcp << VLAN_PCP_SHIFT)
1159                                   | (cfi ? VLAN_CFI : 0));
1160             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1161                               &push, sizeof push);
1162
1163             return n;
1164         }
1165     }
1166
1167     if (!strncmp(s, "pop_vlan", 8)) {
1168         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1169         return 8;
1170     }
1171
1172     {
1173         double percentage;
1174         int n = -1;
1175
1176         if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1177             && percentage >= 0. && percentage <= 100.0) {
1178             size_t sample_ofs, actions_ofs;
1179             double probability;
1180
1181             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1182             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1183             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1184                            (probability <= 0 ? 0
1185                             : probability >= UINT32_MAX ? UINT32_MAX
1186                             : probability));
1187
1188             actions_ofs = nl_msg_start_nested(actions,
1189                                               OVS_SAMPLE_ATTR_ACTIONS);
1190             for (;;) {
1191                 int retval;
1192
1193                 n += strspn(s + n, delimiters);
1194                 if (s[n] == ')') {
1195                     break;
1196                 }
1197
1198                 retval = parse_odp_action(s + n, port_names, actions);
1199                 if (retval < 0) {
1200                     return retval;
1201                 }
1202                 n += retval;
1203             }
1204             nl_msg_end_nested(actions, actions_ofs);
1205             nl_msg_end_nested(actions, sample_ofs);
1206
1207             return s[n + 1] == ')' ? n + 2 : -EINVAL;
1208         }
1209     }
1210
1211     {
1212         uint32_t port;
1213         int n;
1214
1215         if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1216             nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1217             return n;
1218         }
1219     }
1220
1221     {
1222         struct ovs_action_push_tnl data;
1223         int n;
1224
1225         n = ovs_parse_tnl_push(s, &data);
1226         if (n > 0) {
1227             odp_put_tnl_push_action(actions, &data);
1228             return n;
1229         } else if (n < 0) {
1230             return n;
1231         }
1232     }
1233     return -EINVAL;
1234 }
1235
1236 /* Parses the string representation of datapath actions, in the format output
1237  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
1238  * value.  On success, the ODP actions are appended to 'actions' as a series of
1239  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
1240  * way, 'actions''s data might be reallocated. */
1241 int
1242 odp_actions_from_string(const char *s, const struct simap *port_names,
1243                         struct ofpbuf *actions)
1244 {
1245     size_t old_size;
1246
1247     if (!strcasecmp(s, "drop")) {
1248         return 0;
1249     }
1250
1251     old_size = actions->size;
1252     for (;;) {
1253         int retval;
1254
1255         s += strspn(s, delimiters);
1256         if (!*s) {
1257             return 0;
1258         }
1259
1260         retval = parse_odp_action(s, port_names, actions);
1261         if (retval < 0 || !strchr(delimiters, s[retval])) {
1262             actions->size = old_size;
1263             return -retval;
1264         }
1265         s += retval;
1266     }
1267
1268     return 0;
1269 }
1270 \f
1271 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1272     [OVS_VXLAN_EXT_GBP]                 = { .len = 4 },
1273 };
1274
1275 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1276     [OVS_TUNNEL_KEY_ATTR_ID]            = { .len = 8 },
1277     [OVS_TUNNEL_KEY_ATTR_IPV4_SRC]      = { .len = 4 },
1278     [OVS_TUNNEL_KEY_ATTR_IPV4_DST]      = { .len = 4 },
1279     [OVS_TUNNEL_KEY_ATTR_TOS]           = { .len = 1 },
1280     [OVS_TUNNEL_KEY_ATTR_TTL]           = { .len = 1 },
1281     [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1282     [OVS_TUNNEL_KEY_ATTR_CSUM]          = { .len = 0 },
1283     [OVS_TUNNEL_KEY_ATTR_TP_SRC]        = { .len = 2 },
1284     [OVS_TUNNEL_KEY_ATTR_TP_DST]        = { .len = 2 },
1285     [OVS_TUNNEL_KEY_ATTR_OAM]           = { .len = 0 },
1286     [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS]   = { .len = ATTR_LEN_VARIABLE },
1287     [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS]    = { .len = ATTR_LEN_NESTED,
1288                                             .next = ovs_vxlan_ext_attr_lens ,
1289                                             .next_max = OVS_VXLAN_EXT_MAX},
1290 };
1291
1292 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1293     [OVS_KEY_ATTR_ENCAP]     = { .len = ATTR_LEN_NESTED },
1294     [OVS_KEY_ATTR_PRIORITY]  = { .len = 4 },
1295     [OVS_KEY_ATTR_SKB_MARK]  = { .len = 4 },
1296     [OVS_KEY_ATTR_DP_HASH]   = { .len = 4 },
1297     [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1298     [OVS_KEY_ATTR_TUNNEL]    = { .len = ATTR_LEN_NESTED,
1299                                  .next = ovs_tun_key_attr_lens,
1300                                  .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1301     [OVS_KEY_ATTR_IN_PORT]   = { .len = 4  },
1302     [OVS_KEY_ATTR_ETHERNET]  = { .len = sizeof(struct ovs_key_ethernet) },
1303     [OVS_KEY_ATTR_VLAN]      = { .len = 2 },
1304     [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1305     [OVS_KEY_ATTR_MPLS]      = { .len = ATTR_LEN_VARIABLE },
1306     [OVS_KEY_ATTR_IPV4]      = { .len = sizeof(struct ovs_key_ipv4) },
1307     [OVS_KEY_ATTR_IPV6]      = { .len = sizeof(struct ovs_key_ipv6) },
1308     [OVS_KEY_ATTR_TCP]       = { .len = sizeof(struct ovs_key_tcp) },
1309     [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1310     [OVS_KEY_ATTR_UDP]       = { .len = sizeof(struct ovs_key_udp) },
1311     [OVS_KEY_ATTR_SCTP]      = { .len = sizeof(struct ovs_key_sctp) },
1312     [OVS_KEY_ATTR_ICMP]      = { .len = sizeof(struct ovs_key_icmp) },
1313     [OVS_KEY_ATTR_ICMPV6]    = { .len = sizeof(struct ovs_key_icmpv6) },
1314     [OVS_KEY_ATTR_ARP]       = { .len = sizeof(struct ovs_key_arp) },
1315     [OVS_KEY_ATTR_ND]        = { .len = sizeof(struct ovs_key_nd) },
1316 };
1317
1318 /* Returns the correct length of the payload for a flow key attribute of the
1319  * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1320  * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1321  * payload is a nested type. */
1322 static int
1323 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1324 {
1325     if (type > max_len) {
1326         return ATTR_LEN_INVALID;
1327     }
1328
1329     return tbl[type].len;
1330 }
1331
1332 static void
1333 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1334 {
1335     size_t len = nl_attr_get_size(a);
1336     if (len) {
1337         const uint8_t *unspec;
1338         unsigned int i;
1339
1340         unspec = nl_attr_get(a);
1341         for (i = 0; i < len; i++) {
1342             if (i) {
1343                 ds_put_char(ds, ' ');
1344             }
1345             ds_put_format(ds, "%02x", unspec[i]);
1346         }
1347     }
1348 }
1349
1350 static const char *
1351 ovs_frag_type_to_string(enum ovs_frag_type type)
1352 {
1353     switch (type) {
1354     case OVS_FRAG_TYPE_NONE:
1355         return "no";
1356     case OVS_FRAG_TYPE_FIRST:
1357         return "first";
1358     case OVS_FRAG_TYPE_LATER:
1359         return "later";
1360     case __OVS_FRAG_TYPE_MAX:
1361     default:
1362         return "<error>";
1363     }
1364 }
1365
1366 static enum odp_key_fitness
1367 odp_tun_key_from_attr__(const struct nlattr *attr,
1368                         const struct nlattr *flow_attrs, size_t flow_attr_len,
1369                         const struct flow_tnl *src_tun, struct flow_tnl *tun)
1370 {
1371     unsigned int left;
1372     const struct nlattr *a;
1373     bool ttl = false;
1374     bool unknown = false;
1375
1376     NL_NESTED_FOR_EACH(a, left, attr) {
1377         uint16_t type = nl_attr_type(a);
1378         size_t len = nl_attr_get_size(a);
1379         int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1380                                             OVS_TUNNEL_ATTR_MAX, type);
1381
1382         if (len != expected_len && expected_len >= 0) {
1383             return ODP_FIT_ERROR;
1384         }
1385
1386         switch (type) {
1387         case OVS_TUNNEL_KEY_ATTR_ID:
1388             tun->tun_id = nl_attr_get_be64(a);
1389             tun->flags |= FLOW_TNL_F_KEY;
1390             break;
1391         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1392             tun->ip_src = nl_attr_get_be32(a);
1393             break;
1394         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1395             tun->ip_dst = nl_attr_get_be32(a);
1396             break;
1397         case OVS_TUNNEL_KEY_ATTR_TOS:
1398             tun->ip_tos = nl_attr_get_u8(a);
1399             break;
1400         case OVS_TUNNEL_KEY_ATTR_TTL:
1401             tun->ip_ttl = nl_attr_get_u8(a);
1402             ttl = true;
1403             break;
1404         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1405             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1406             break;
1407         case OVS_TUNNEL_KEY_ATTR_CSUM:
1408             tun->flags |= FLOW_TNL_F_CSUM;
1409             break;
1410         case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1411             tun->tp_src = nl_attr_get_be16(a);
1412             break;
1413         case OVS_TUNNEL_KEY_ATTR_TP_DST:
1414             tun->tp_dst = nl_attr_get_be16(a);
1415             break;
1416         case OVS_TUNNEL_KEY_ATTR_OAM:
1417             tun->flags |= FLOW_TNL_F_OAM;
1418             break;
1419         case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1420             static const struct nl_policy vxlan_opts_policy[] = {
1421                 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1422             };
1423             struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1424
1425             if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1426                 return ODP_FIT_ERROR;
1427             }
1428
1429             if (ext[OVS_VXLAN_EXT_GBP]) {
1430                 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1431
1432                 tun->gbp_id = htons(gbp & 0xFFFF);
1433                 tun->gbp_flags = (gbp >> 16) & 0xFF;
1434             }
1435
1436             break;
1437         }
1438         case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1439             if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1440                                                 &src_tun->metadata,
1441                                                 &tun->metadata)) {
1442                 return ODP_FIT_ERROR;
1443             }
1444             break;
1445
1446         default:
1447             /* Allow this to show up as unexpected, if there are unknown
1448              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1449             unknown = true;
1450             break;
1451         }
1452     }
1453
1454     if (!ttl) {
1455         return ODP_FIT_ERROR;
1456     }
1457     if (unknown) {
1458         return ODP_FIT_TOO_MUCH;
1459     }
1460     return ODP_FIT_PERFECT;
1461 }
1462
1463 enum odp_key_fitness
1464 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1465 {
1466     return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun);
1467 }
1468
1469 static void
1470 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1471                 const struct flow_tnl *tun_flow_key,
1472                 const struct ofpbuf *key_buf)
1473 {
1474     size_t tun_key_ofs;
1475
1476     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1477
1478     /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1479     if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1480         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1481     }
1482     if (tun_key->ip_src) {
1483         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1484     }
1485     if (tun_key->ip_dst) {
1486         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1487     }
1488     if (tun_key->ip_tos) {
1489         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1490     }
1491     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1492     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1493         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1494     }
1495     if (tun_key->flags & FLOW_TNL_F_CSUM) {
1496         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1497     }
1498     if (tun_key->tp_src) {
1499         nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1500     }
1501     if (tun_key->tp_dst) {
1502         nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1503     }
1504     if (tun_key->flags & FLOW_TNL_F_OAM) {
1505         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1506     }
1507     if (tun_key->gbp_flags || tun_key->gbp_id) {
1508         size_t vxlan_opts_ofs;
1509
1510         vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1511         nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1512                        (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1513         nl_msg_end_nested(a, vxlan_opts_ofs);
1514     }
1515
1516     if (tun_key == tun_flow_key) {
1517         tun_metadata_to_geneve_nlattr_flow(&tun_key->metadata, a);
1518     } else {
1519         tun_metadata_to_geneve_nlattr_mask(key_buf, &tun_key->metadata,
1520                                            &tun_flow_key->metadata, a);
1521     }
1522
1523     nl_msg_end_nested(a, tun_key_ofs);
1524 }
1525
1526 static bool
1527 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1528 {
1529     return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1530 }
1531
1532 static bool
1533 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1534 {
1535     if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1536         return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1537     }
1538     if (attr == OVS_KEY_ATTR_IPV6) {
1539         const struct ovs_key_ipv6 *ipv6_mask = mask;
1540
1541         return
1542             ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1543              == htonl(IPV6_LABEL_MASK))
1544             && ipv6_mask->ipv6_proto == UINT8_MAX
1545             && ipv6_mask->ipv6_tclass == UINT8_MAX
1546             && ipv6_mask->ipv6_hlimit == UINT8_MAX
1547             && ipv6_mask->ipv6_frag == UINT8_MAX
1548             && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1549             && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1550     }
1551     if (attr == OVS_KEY_ATTR_TUNNEL) {
1552         return false;
1553     }
1554
1555     if (attr == OVS_KEY_ATTR_ARP) {
1556         /* ARP key has padding, ignore it. */
1557         BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1558         BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1559         size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1560         ovs_assert(((uint16_t *)mask)[size/2] == 0);
1561     }
1562
1563     return is_all_ones(mask, size);
1564 }
1565
1566 static bool
1567 odp_mask_attr_is_exact(const struct nlattr *ma)
1568 {
1569     enum ovs_key_attr attr = nl_attr_type(ma);
1570     const void *mask;
1571     size_t size;
1572
1573     if (attr == OVS_KEY_ATTR_TUNNEL) {
1574         return false;
1575     } else {
1576         mask = nl_attr_get(ma);
1577         size = nl_attr_get_size(ma);
1578     }
1579
1580     return odp_mask_is_exact(attr, mask, size);
1581 }
1582
1583 void
1584 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1585                      char *port_name)
1586 {
1587     struct odp_portno_names *odp_portno_names;
1588
1589     odp_portno_names = xmalloc(sizeof *odp_portno_names);
1590     odp_portno_names->port_no = port_no;
1591     odp_portno_names->name = xstrdup(port_name);
1592     hmap_insert(portno_names, &odp_portno_names->hmap_node,
1593                 hash_odp_port(port_no));
1594 }
1595
1596 static char *
1597 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1598 {
1599     struct odp_portno_names *odp_portno_names;
1600
1601     HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1602                              hash_odp_port(port_no), portno_names) {
1603         if (odp_portno_names->port_no == port_no) {
1604             return odp_portno_names->name;
1605         }
1606     }
1607     return NULL;
1608 }
1609
1610 void
1611 odp_portno_names_destroy(struct hmap *portno_names)
1612 {
1613     struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1614     HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1615                         hmap_node, portno_names) {
1616         hmap_remove(portno_names, &odp_portno_names->hmap_node);
1617         free(odp_portno_names->name);
1618         free(odp_portno_names);
1619     }
1620 }
1621
1622 /* Format helpers. */
1623
1624 static void
1625 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1626            const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1627 {
1628     bool mask_empty = mask && eth_addr_is_zero(*mask);
1629
1630     if (verbose || !mask_empty) {
1631         bool mask_full = !mask || eth_mask_is_exact(*mask);
1632
1633         if (mask_full) {
1634             ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1635         } else {
1636             ds_put_format(ds, "%s=", name);
1637             eth_format_masked(key, *mask, ds);
1638             ds_put_char(ds, ',');
1639         }
1640     }
1641 }
1642
1643 static void
1644 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1645             const ovs_be64 *mask, bool verbose)
1646 {
1647     bool mask_empty = mask && !*mask;
1648
1649     if (verbose || !mask_empty) {
1650         bool mask_full = !mask || *mask == OVS_BE64_MAX;
1651
1652         ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1653         if (!mask_full) { /* Partially masked. */
1654             ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1655         }
1656         ds_put_char(ds, ',');
1657     }
1658 }
1659
1660 static void
1661 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1662             const ovs_be32 *mask, bool verbose)
1663 {
1664     bool mask_empty = mask && !*mask;
1665
1666     if (verbose || !mask_empty) {
1667         bool mask_full = !mask || *mask == OVS_BE32_MAX;
1668
1669         ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1670         if (!mask_full) { /* Partially masked. */
1671             ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1672         }
1673         ds_put_char(ds, ',');
1674     }
1675 }
1676
1677 static void
1678 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1679             const ovs_be32 (*mask_)[4], bool verbose)
1680 {
1681     char buf[INET6_ADDRSTRLEN];
1682     const struct in6_addr *key = (const struct in6_addr *)key_;
1683     const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1684         : NULL;
1685     bool mask_empty = mask && ipv6_mask_is_any(mask);
1686
1687     if (verbose || !mask_empty) {
1688         bool mask_full = !mask || ipv6_mask_is_exact(mask);
1689
1690         inet_ntop(AF_INET6, key, buf, sizeof buf);
1691         ds_put_format(ds, "%s=%s", name, buf);
1692         if (!mask_full) { /* Partially masked. */
1693             inet_ntop(AF_INET6, mask, buf, sizeof buf);
1694             ds_put_format(ds, "/%s", buf);
1695         }
1696         ds_put_char(ds, ',');
1697     }
1698 }
1699
1700 static void
1701 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1702                   const ovs_be32 *mask, bool verbose)
1703 {
1704     bool mask_empty = mask && !*mask;
1705
1706     if (verbose || !mask_empty) {
1707         bool mask_full = !mask
1708             || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1709
1710         ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1711         if (!mask_full) { /* Partially masked. */
1712             ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1713         }
1714         ds_put_char(ds, ',');
1715     }
1716 }
1717
1718 static void
1719 format_u8x(struct ds *ds, const char *name, uint8_t key,
1720            const uint8_t *mask, bool verbose)
1721 {
1722     bool mask_empty = mask && !*mask;
1723
1724     if (verbose || !mask_empty) {
1725         bool mask_full = !mask || *mask == UINT8_MAX;
1726
1727         ds_put_format(ds, "%s=%#"PRIx8, name, key);
1728         if (!mask_full) { /* Partially masked. */
1729             ds_put_format(ds, "/%#"PRIx8, *mask);
1730         }
1731         ds_put_char(ds, ',');
1732     }
1733 }
1734
1735 static void
1736 format_u8u(struct ds *ds, const char *name, uint8_t key,
1737            const uint8_t *mask, bool verbose)
1738 {
1739     bool mask_empty = mask && !*mask;
1740
1741     if (verbose || !mask_empty) {
1742         bool mask_full = !mask || *mask == UINT8_MAX;
1743
1744         ds_put_format(ds, "%s=%"PRIu8, name, key);
1745         if (!mask_full) { /* Partially masked. */
1746             ds_put_format(ds, "/%#"PRIx8, *mask);
1747         }
1748         ds_put_char(ds, ',');
1749     }
1750 }
1751
1752 static void
1753 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1754             const ovs_be16 *mask, bool verbose)
1755 {
1756     bool mask_empty = mask && !*mask;
1757
1758     if (verbose || !mask_empty) {
1759         bool mask_full = !mask || *mask == OVS_BE16_MAX;
1760
1761         ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1762         if (!mask_full) { /* Partially masked. */
1763             ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1764         }
1765         ds_put_char(ds, ',');
1766     }
1767 }
1768
1769 static void
1770 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1771              const ovs_be16 *mask, bool verbose)
1772 {
1773     bool mask_empty = mask && !*mask;
1774
1775     if (verbose || !mask_empty) {
1776         bool mask_full = !mask || *mask == OVS_BE16_MAX;
1777
1778         ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1779         if (!mask_full) { /* Partially masked. */
1780             ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1781         }
1782         ds_put_char(ds, ',');
1783     }
1784 }
1785
1786 static void
1787 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1788                  const uint16_t *mask, bool verbose)
1789 {
1790     bool mask_empty = mask && !*mask;
1791
1792     if (verbose || !mask_empty) {
1793         bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1794
1795         ds_put_cstr(ds, name);
1796         ds_put_char(ds, '(');
1797         if (!mask_full) { /* Partially masked. */
1798             format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1799         } else { /* Fully masked. */
1800             format_flags(ds, flow_tun_flag_to_string, key, ',');
1801         }
1802         ds_put_cstr(ds, "),");
1803     }
1804 }
1805
1806 static bool
1807 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1808                const struct attr_len_tbl tbl[], int max_len, bool need_key)
1809 {
1810     int expected_len;
1811
1812     expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1813     if (expected_len != ATTR_LEN_VARIABLE &&
1814         expected_len != ATTR_LEN_NESTED) {
1815
1816         bool bad_key_len = nl_attr_get_size(a) != expected_len;
1817         bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1818
1819         if (bad_key_len || bad_mask_len) {
1820             if (need_key) {
1821                 ds_put_format(ds, "key%u", nl_attr_type(a));
1822             }
1823             if (bad_key_len) {
1824                 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1825                               nl_attr_get_size(a), expected_len);
1826             }
1827             format_generic_odp_key(a, ds);
1828             if (ma) {
1829                 ds_put_char(ds, '/');
1830                 if (bad_mask_len) {
1831                     ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1832                                   nl_attr_get_size(ma), expected_len);
1833                 }
1834                 format_generic_odp_key(ma, ds);
1835             }
1836             ds_put_char(ds, ')');
1837             return false;
1838         }
1839     }
1840
1841     return true;
1842 }
1843
1844 static void
1845 format_unknown_key(struct ds *ds, const struct nlattr *a,
1846                    const struct nlattr *ma)
1847 {
1848     ds_put_format(ds, "key%u(", nl_attr_type(a));
1849     format_generic_odp_key(a, ds);
1850     if (ma && !odp_mask_attr_is_exact(ma)) {
1851         ds_put_char(ds, '/');
1852         format_generic_odp_key(ma, ds);
1853     }
1854     ds_put_cstr(ds, "),");
1855 }
1856
1857 static void
1858 format_odp_tun_vxlan_opt(const struct nlattr *attr,
1859                          const struct nlattr *mask_attr, struct ds *ds,
1860                          bool verbose)
1861 {
1862     unsigned int left;
1863     const struct nlattr *a;
1864     struct ofpbuf ofp;
1865
1866     ofpbuf_init(&ofp, 100);
1867     NL_NESTED_FOR_EACH(a, left, attr) {
1868         uint16_t type = nl_attr_type(a);
1869         const struct nlattr *ma = NULL;
1870
1871         if (mask_attr) {
1872             ma = nl_attr_find__(nl_attr_get(mask_attr),
1873                                 nl_attr_get_size(mask_attr), type);
1874             if (!ma) {
1875                 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1876                                                 OVS_VXLAN_EXT_MAX,
1877                                                 &ofp, a);
1878             }
1879         }
1880
1881         if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1882                             OVS_VXLAN_EXT_MAX, true)) {
1883             continue;
1884         }
1885
1886         switch (type) {
1887         case OVS_VXLAN_EXT_GBP: {
1888             uint32_t key = nl_attr_get_u32(a);
1889             ovs_be16 id, id_mask;
1890             uint8_t flags, flags_mask;
1891
1892             id = htons(key & 0xFFFF);
1893             flags = (key >> 16) & 0xFF;
1894             if (ma) {
1895                 uint32_t mask = nl_attr_get_u32(ma);
1896                 id_mask = htons(mask & 0xFFFF);
1897                 flags_mask = (mask >> 16) & 0xFF;
1898             }
1899
1900             ds_put_cstr(ds, "gbp(");
1901             format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1902             format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1903             ds_chomp(ds, ',');
1904             ds_put_cstr(ds, "),");
1905             break;
1906         }
1907
1908         default:
1909             format_unknown_key(ds, a, ma);
1910         }
1911         ofpbuf_clear(&ofp);
1912     }
1913
1914     ds_chomp(ds, ',');
1915     ofpbuf_uninit(&ofp);
1916 }
1917
1918 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1919
1920 static void
1921 format_geneve_opts(const struct geneve_opt *opt,
1922                    const struct geneve_opt *mask, int opts_len,
1923                    struct ds *ds, bool verbose)
1924 {
1925     while (opts_len > 0) {
1926         unsigned int len;
1927         uint8_t data_len, data_len_mask;
1928
1929         if (opts_len < sizeof *opt) {
1930             ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1931                           opts_len, sizeof *opt);
1932             return;
1933         }
1934
1935         data_len = opt->length * 4;
1936         if (mask) {
1937             if (mask->length == 0x1f) {
1938                 data_len_mask = UINT8_MAX;
1939             } else {
1940                 data_len_mask = mask->length;
1941             }
1942         }
1943         len = sizeof *opt + data_len;
1944         if (len > opts_len) {
1945             ds_put_format(ds, "opt len %u greater than remaining %u",
1946                           len, opts_len);
1947             return;
1948         }
1949
1950         ds_put_char(ds, '{');
1951         format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1952                     verbose);
1953         format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1954         format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1955         if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1956             ds_put_hex(ds, opt + 1, data_len);
1957             if (mask && !is_all_ones(mask + 1, data_len)) {
1958                 ds_put_char(ds, '/');
1959                 ds_put_hex(ds, mask + 1, data_len);
1960             }
1961         } else {
1962             ds_chomp(ds, ',');
1963         }
1964         ds_put_char(ds, '}');
1965
1966         opt += len / sizeof(*opt);
1967         if (mask) {
1968             mask += len / sizeof(*opt);
1969         }
1970         opts_len -= len;
1971     };
1972 }
1973
1974 static void
1975 format_odp_tun_geneve(const struct nlattr *attr,
1976                       const struct nlattr *mask_attr, struct ds *ds,
1977                       bool verbose)
1978 {
1979     int opts_len = nl_attr_get_size(attr);
1980     const struct geneve_opt *opt = nl_attr_get(attr);
1981     const struct geneve_opt *mask = mask_attr ?
1982                                     nl_attr_get(mask_attr) : NULL;
1983
1984     if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1985         ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1986                       nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1987         return;
1988     }
1989
1990     format_geneve_opts(opt, mask, opts_len, ds, verbose);
1991 }
1992
1993 static void
1994 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1995                     struct ds *ds, bool verbose)
1996 {
1997     unsigned int left;
1998     const struct nlattr *a;
1999     uint16_t flags = 0;
2000     uint16_t mask_flags = 0;
2001     struct ofpbuf ofp;
2002
2003     ofpbuf_init(&ofp, 100);
2004     NL_NESTED_FOR_EACH(a, left, attr) {
2005         enum ovs_tunnel_key_attr type = nl_attr_type(a);
2006         const struct nlattr *ma = NULL;
2007
2008         if (mask_attr) {
2009             ma = nl_attr_find__(nl_attr_get(mask_attr),
2010                                 nl_attr_get_size(mask_attr), type);
2011             if (!ma) {
2012                 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
2013                                                 OVS_TUNNEL_KEY_ATTR_MAX,
2014                                                 &ofp, a);
2015             }
2016         }
2017
2018         if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
2019                             OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2020             continue;
2021         }
2022
2023         switch (type) {
2024         case OVS_TUNNEL_KEY_ATTR_ID:
2025             format_be64(ds, "tun_id", nl_attr_get_be64(a),
2026                         ma ? nl_attr_get(ma) : NULL, verbose);
2027             flags |= FLOW_TNL_F_KEY;
2028             if (ma) {
2029                 mask_flags |= FLOW_TNL_F_KEY;
2030             }
2031             break;
2032         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2033             format_ipv4(ds, "src", nl_attr_get_be32(a),
2034                         ma ? nl_attr_get(ma) : NULL, verbose);
2035             break;
2036         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2037             format_ipv4(ds, "dst", nl_attr_get_be32(a),
2038                         ma ? nl_attr_get(ma) : NULL, verbose);
2039             break;
2040         case OVS_TUNNEL_KEY_ATTR_TOS:
2041             format_u8x(ds, "tos", nl_attr_get_u8(a),
2042                        ma ? nl_attr_get(ma) : NULL, verbose);
2043             break;
2044         case OVS_TUNNEL_KEY_ATTR_TTL:
2045             format_u8u(ds, "ttl", nl_attr_get_u8(a),
2046                        ma ? nl_attr_get(ma) : NULL, verbose);
2047             break;
2048         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2049             flags |= FLOW_TNL_F_DONT_FRAGMENT;
2050             break;
2051         case OVS_TUNNEL_KEY_ATTR_CSUM:
2052             flags |= FLOW_TNL_F_CSUM;
2053             break;
2054         case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2055             format_be16(ds, "tp_src", nl_attr_get_be16(a),
2056                         ma ? nl_attr_get(ma) : NULL, verbose);
2057             break;
2058         case OVS_TUNNEL_KEY_ATTR_TP_DST:
2059             format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2060                         ma ? nl_attr_get(ma) : NULL, verbose);
2061             break;
2062         case OVS_TUNNEL_KEY_ATTR_OAM:
2063             flags |= FLOW_TNL_F_OAM;
2064             break;
2065         case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2066             ds_put_cstr(ds, "vxlan(");
2067             format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2068             ds_put_cstr(ds, "),");
2069             break;
2070         case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2071             ds_put_cstr(ds, "geneve(");
2072             format_odp_tun_geneve(a, ma, ds, verbose);
2073             ds_put_cstr(ds, "),");
2074             break;
2075         case __OVS_TUNNEL_KEY_ATTR_MAX:
2076         default:
2077             format_unknown_key(ds, a, ma);
2078         }
2079         ofpbuf_clear(&ofp);
2080     }
2081
2082     /* Flags can have a valid mask even if the attribute is not set, so
2083      * we need to collect these separately. */
2084     if (mask_attr) {
2085         NL_NESTED_FOR_EACH(a, left, mask_attr) {
2086             switch (nl_attr_type(a)) {
2087             case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2088                 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2089                 break;
2090             case OVS_TUNNEL_KEY_ATTR_CSUM:
2091                 mask_flags |= FLOW_TNL_F_CSUM;
2092                 break;
2093             case OVS_TUNNEL_KEY_ATTR_OAM:
2094                 mask_flags |= FLOW_TNL_F_OAM;
2095                 break;
2096             }
2097         }
2098     }
2099
2100     format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2101                      verbose);
2102     ds_chomp(ds, ',');
2103     ofpbuf_uninit(&ofp);
2104 }
2105
2106 static void
2107 format_frag(struct ds *ds, const char *name, uint8_t key,
2108             const uint8_t *mask, bool verbose)
2109 {
2110     bool mask_empty = mask && !*mask;
2111
2112     /* ODP frag is an enumeration field; partial masks are not meaningful. */
2113     if (verbose || !mask_empty) {
2114         bool mask_full = !mask || *mask == UINT8_MAX;
2115
2116         if (!mask_full) { /* Partially masked. */
2117             ds_put_format(ds, "error: partial mask not supported for frag (%#"
2118                           PRIx8"),", *mask);
2119         } else {
2120             ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2121         }
2122     }
2123 }
2124
2125 static void
2126 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2127                     const struct hmap *portno_names, struct ds *ds,
2128                     bool verbose)
2129 {
2130     enum ovs_key_attr attr = nl_attr_type(a);
2131     char namebuf[OVS_KEY_ATTR_BUFSIZE];
2132     bool is_exact;
2133
2134     is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2135
2136     ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2137
2138     if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2139                         OVS_KEY_ATTR_MAX, false)) {
2140         return;
2141     }
2142
2143     ds_put_char(ds, '(');
2144     switch (attr) {
2145     case OVS_KEY_ATTR_ENCAP:
2146         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2147             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2148                             nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2149                             verbose);
2150         } else if (nl_attr_get_size(a)) {
2151             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2152                             ds, verbose);
2153         }
2154         break;
2155
2156     case OVS_KEY_ATTR_PRIORITY:
2157     case OVS_KEY_ATTR_SKB_MARK:
2158     case OVS_KEY_ATTR_DP_HASH:
2159     case OVS_KEY_ATTR_RECIRC_ID:
2160         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2161         if (!is_exact) {
2162             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2163         }
2164         break;
2165
2166     case OVS_KEY_ATTR_TUNNEL:
2167         format_odp_tun_attr(a, ma, ds, verbose);
2168         break;
2169
2170     case OVS_KEY_ATTR_IN_PORT:
2171         if (portno_names && verbose && is_exact) {
2172             char *name = odp_portno_names_get(portno_names,
2173                             u32_to_odp(nl_attr_get_u32(a)));
2174             if (name) {
2175                 ds_put_format(ds, "%s", name);
2176             } else {
2177                 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2178             }
2179         } else {
2180             ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2181             if (!is_exact) {
2182                 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2183             }
2184         }
2185         break;
2186
2187     case OVS_KEY_ATTR_ETHERNET: {
2188         const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2189         const struct ovs_key_ethernet *key = nl_attr_get(a);
2190
2191         format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2192         format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2193         ds_chomp(ds, ',');
2194         break;
2195     }
2196     case OVS_KEY_ATTR_VLAN:
2197         format_vlan_tci(ds, nl_attr_get_be16(a),
2198                         ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2199         break;
2200
2201     case OVS_KEY_ATTR_MPLS: {
2202         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2203         const struct ovs_key_mpls *mpls_mask = NULL;
2204         size_t size = nl_attr_get_size(a);
2205
2206         if (!size || size % sizeof *mpls_key) {
2207             ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2208             return;
2209         }
2210         if (!is_exact) {
2211             mpls_mask = nl_attr_get(ma);
2212             if (size != nl_attr_get_size(ma)) {
2213                 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2214                               "mask length %"PRIuSIZE")",
2215                               size, nl_attr_get_size(ma));
2216                 return;
2217             }
2218         }
2219         format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2220         break;
2221     }
2222     case OVS_KEY_ATTR_ETHERTYPE:
2223         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2224         if (!is_exact) {
2225             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2226         }
2227         break;
2228
2229     case OVS_KEY_ATTR_IPV4: {
2230         const struct ovs_key_ipv4 *key = nl_attr_get(a);
2231         const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2232
2233         format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2234         format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2235         format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2236                       verbose);
2237         format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2238         format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2239         format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2240                     verbose);
2241         ds_chomp(ds, ',');
2242         break;
2243     }
2244     case OVS_KEY_ATTR_IPV6: {
2245         const struct ovs_key_ipv6 *key = nl_attr_get(a);
2246         const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2247
2248         format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2249         format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2250         format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2251                           verbose);
2252         format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2253                       verbose);
2254         format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2255                       verbose);
2256         format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2257                       verbose);
2258         format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2259                     verbose);
2260         ds_chomp(ds, ',');
2261         break;
2262     }
2263         /* These have the same structure and format. */
2264     case OVS_KEY_ATTR_TCP:
2265     case OVS_KEY_ATTR_UDP:
2266     case OVS_KEY_ATTR_SCTP: {
2267         const struct ovs_key_tcp *key = nl_attr_get(a);
2268         const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2269
2270         format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2271         format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2272         ds_chomp(ds, ',');
2273         break;
2274     }
2275     case OVS_KEY_ATTR_TCP_FLAGS:
2276         if (!is_exact) {
2277             format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2278                                 ntohs(nl_attr_get_be16(a)),
2279                                 ntohs(nl_attr_get_be16(ma)));
2280         } else {
2281             format_flags(ds, packet_tcp_flag_to_string,
2282                          ntohs(nl_attr_get_be16(a)), ',');
2283         }
2284         break;
2285
2286     case OVS_KEY_ATTR_ICMP: {
2287         const struct ovs_key_icmp *key = nl_attr_get(a);
2288         const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2289
2290         format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2291         format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2292         ds_chomp(ds, ',');
2293         break;
2294     }
2295     case OVS_KEY_ATTR_ICMPV6: {
2296         const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2297         const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2298
2299         format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2300                    verbose);
2301         format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2302                    verbose);
2303         ds_chomp(ds, ',');
2304         break;
2305     }
2306     case OVS_KEY_ATTR_ARP: {
2307         const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2308         const struct ovs_key_arp *key = nl_attr_get(a);
2309
2310         format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2311         format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2312         format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2313         format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2314         format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2315         ds_chomp(ds, ',');
2316         break;
2317     }
2318     case OVS_KEY_ATTR_ND: {
2319         const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2320         const struct ovs_key_nd *key = nl_attr_get(a);
2321
2322         format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2323                     verbose);
2324         format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2325         format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2326
2327         ds_chomp(ds, ',');
2328         break;
2329     }
2330     case OVS_KEY_ATTR_UNSPEC:
2331     case __OVS_KEY_ATTR_MAX:
2332     default:
2333         format_generic_odp_key(a, ds);
2334         if (!is_exact) {
2335             ds_put_char(ds, '/');
2336             format_generic_odp_key(ma, ds);
2337         }
2338         break;
2339     }
2340     ds_put_char(ds, ')');
2341 }
2342
2343 static struct nlattr *
2344 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2345                            struct ofpbuf *ofp, const struct nlattr *key)
2346 {
2347     const struct nlattr *a;
2348     unsigned int left;
2349     int type = nl_attr_type(key);
2350     int size = nl_attr_get_size(key);
2351
2352     if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2353         nl_msg_put_unspec_zero(ofp, type, size);
2354     } else {
2355         size_t nested_mask;
2356
2357         if (tbl[type].next) {
2358             tbl = tbl[type].next;
2359             max = tbl[type].next_max;
2360         }
2361
2362         nested_mask = nl_msg_start_nested(ofp, type);
2363         NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2364             generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2365         }
2366         nl_msg_end_nested(ofp, nested_mask);
2367     }
2368
2369     return ofp->base;
2370 }
2371
2372 int
2373 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2374 {
2375     const char *s = s_;
2376
2377     if (ovs_scan(s, "ufid:")) {
2378         s += 5;
2379
2380         if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2381             return -EINVAL;
2382         }
2383         s += UUID_LEN;
2384
2385         return s - s_;
2386     }
2387
2388     return 0;
2389 }
2390
2391 void
2392 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2393 {
2394     ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2395 }
2396
2397 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2398  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2399  * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2400  * non-null and 'verbose' is true, translates odp port number to its name. */
2401 void
2402 odp_flow_format(const struct nlattr *key, size_t key_len,
2403                 const struct nlattr *mask, size_t mask_len,
2404                 const struct hmap *portno_names, struct ds *ds, bool verbose)
2405 {
2406     if (key_len) {
2407         const struct nlattr *a;
2408         unsigned int left;
2409         bool has_ethtype_key = false;
2410         const struct nlattr *ma = NULL;
2411         struct ofpbuf ofp;
2412         bool first_field = true;
2413
2414         ofpbuf_init(&ofp, 100);
2415         NL_ATTR_FOR_EACH (a, left, key, key_len) {
2416             bool is_nested_attr;
2417             bool is_wildcard = false;
2418             int attr_type = nl_attr_type(a);
2419
2420             if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2421                 has_ethtype_key = true;
2422             }
2423
2424             is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2425                                               OVS_KEY_ATTR_MAX, attr_type) ==
2426                              ATTR_LEN_NESTED;
2427
2428             if (mask && mask_len) {
2429                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2430                 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2431             }
2432
2433             if (verbose || !is_wildcard  || is_nested_attr) {
2434                 if (is_wildcard && !ma) {
2435                     ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2436                                                     OVS_KEY_ATTR_MAX,
2437                                                     &ofp, a);
2438                 }
2439                 if (!first_field) {
2440                     ds_put_char(ds, ',');
2441                 }
2442                 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2443                 first_field = false;
2444             }
2445             ofpbuf_clear(&ofp);
2446         }
2447         ofpbuf_uninit(&ofp);
2448
2449         if (left) {
2450             int i;
2451
2452             if (left == key_len) {
2453                 ds_put_cstr(ds, "<empty>");
2454             }
2455             ds_put_format(ds, ",***%u leftover bytes*** (", left);
2456             for (i = 0; i < left; i++) {
2457                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2458             }
2459             ds_put_char(ds, ')');
2460         }
2461         if (!has_ethtype_key) {
2462             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2463             if (ma) {
2464                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2465                               ntohs(nl_attr_get_be16(ma)));
2466             }
2467         }
2468     } else {
2469         ds_put_cstr(ds, "<empty>");
2470     }
2471 }
2472
2473 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2474  * OVS_KEY_ATTR_* attributes in 'key'. */
2475 void
2476 odp_flow_key_format(const struct nlattr *key,
2477                     size_t key_len, struct ds *ds)
2478 {
2479     odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2480 }
2481
2482 static bool
2483 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2484 {
2485     if (!strcasecmp(s, "no")) {
2486         *type = OVS_FRAG_TYPE_NONE;
2487     } else if (!strcasecmp(s, "first")) {
2488         *type = OVS_FRAG_TYPE_FIRST;
2489     } else if (!strcasecmp(s, "later")) {
2490         *type = OVS_FRAG_TYPE_LATER;
2491     } else {
2492         return false;
2493     }
2494     return true;
2495 }
2496
2497 /* Parsing. */
2498
2499 static int
2500 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2501          uint8_t (*mask)[ETH_ADDR_LEN])
2502 {
2503     int n;
2504
2505     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2506         int len = n;
2507
2508         if (mask) {
2509             if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2510                          ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2511                 len += n;
2512             } else {
2513                 memset(mask, 0xff, sizeof *mask);
2514             }
2515         }
2516         return len;
2517     }
2518     return 0;
2519 }
2520
2521 static int
2522 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2523 {
2524     int n;
2525
2526     if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2527         int len = n;
2528
2529         if (mask) {
2530             if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2531                          IP_SCAN_ARGS(mask), &n)) {
2532                 len += n;
2533             } else {
2534                 *mask = OVS_BE32_MAX;
2535             }
2536         }
2537         return len;
2538     }
2539     return 0;
2540 }
2541
2542 static int
2543 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2544 {
2545     int n;
2546     char ipv6_s[IPV6_SCAN_LEN + 1];
2547
2548     if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2549         && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2550         int len = n;
2551
2552         if (mask) {
2553             if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2554                 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2555                 len += n;
2556             } else {
2557                 memset(mask, 0xff, sizeof *mask);
2558             }
2559         }
2560         return len;
2561     }
2562     return 0;
2563 }
2564
2565 static int
2566 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2567 {
2568     int key_, mask_;
2569     int n;
2570
2571     if (ovs_scan(s, "%i%n", &key_, &n)
2572         && (key_ & ~IPV6_LABEL_MASK) == 0) {
2573         int len = n;
2574
2575         *key = htonl(key_);
2576         if (mask) {
2577             if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2578                 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2579                 len += n;
2580                 *mask = htonl(mask_);
2581             } else {
2582                 *mask = htonl(IPV6_LABEL_MASK);
2583             }
2584         }
2585         return len;
2586     }
2587     return 0;
2588 }
2589
2590 static int
2591 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2592 {
2593     int n;
2594
2595     if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2596         int len = n;
2597
2598         if (mask) {
2599             if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2600                 len += n;
2601             } else {
2602                 *mask = UINT8_MAX;
2603             }
2604         }
2605         return len;
2606     }
2607     return 0;
2608 }
2609
2610 static int
2611 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2612 {
2613     int n;
2614
2615     if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2616         int len = n;
2617
2618         if (mask) {
2619             if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2620                 len += n;
2621             } else {
2622                 *mask = UINT32_MAX;
2623             }
2624         }
2625         return len;
2626     }
2627     return 0;
2628 }
2629
2630 static int
2631 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2632 {
2633     uint16_t key_, mask_;
2634     int n;
2635
2636     if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2637         int len = n;
2638
2639         *key = htons(key_);
2640         if (mask) {
2641             if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2642                 len += n;
2643                 *mask = htons(mask_);
2644             } else {
2645                 *mask = OVS_BE16_MAX;
2646             }
2647         }
2648         return len;
2649     }
2650     return 0;
2651 }
2652
2653 static int
2654 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2655 {
2656     uint64_t key_, mask_;
2657     int n;
2658
2659     if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2660         int len = n;
2661
2662         *key = htonll(key_);
2663         if (mask) {
2664             if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2665                 len += n;
2666                 *mask = htonll(mask_);
2667             } else {
2668                 *mask = OVS_BE64_MAX;
2669             }
2670         }
2671         return len;
2672     }
2673     return 0;
2674 }
2675
2676 static int
2677 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2678 {
2679     uint32_t flags, fmask;
2680     int n;
2681
2682     n = parse_flags(s, flow_tun_flag_to_string, &flags,
2683                     FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2684     if (n >= 0 && s[n] == ')') {
2685         *key = flags;
2686         if (mask) {
2687             *mask = fmask;
2688         }
2689         return n + 1;
2690     }
2691     return 0;
2692 }
2693
2694 static int
2695 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2696 {
2697     uint32_t flags, fmask;
2698     int n;
2699
2700     n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2701                     TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2702     if (n >= 0) {
2703         *key = htons(flags);
2704         if (mask) {
2705             *mask = htons(fmask);
2706         }
2707         return n;
2708     }
2709     return 0;
2710 }
2711
2712 static int
2713 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2714 {
2715     int n;
2716     char frag[8];
2717     enum ovs_frag_type frag_type;
2718
2719     if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2720         && ovs_frag_type_from_string(frag, &frag_type)) {
2721         int len = n;
2722
2723         *key = frag_type;
2724         if (mask) {
2725             *mask = UINT8_MAX;
2726         }
2727         return len;
2728     }
2729     return 0;
2730 }
2731
2732 static int
2733 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2734           const struct simap *port_names)
2735 {
2736     int n;
2737
2738     if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2739         int len = n;
2740
2741         if (mask) {
2742             if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2743                 len += n;
2744             } else {
2745                 *mask = UINT32_MAX;
2746             }
2747         }
2748         return len;
2749     } else if (port_names) {
2750         const struct simap_node *node;
2751         int len;
2752
2753         len = strcspn(s, ")");
2754         node = simap_find_len(port_names, s, len);
2755         if (node) {
2756             *key = node->data;
2757
2758             if (mask) {
2759                 *mask = UINT32_MAX;
2760             }
2761             return len;
2762         }
2763     }
2764     return 0;
2765 }
2766
2767 /* Helper for vlan parsing. */
2768 struct ovs_key_vlan__ {
2769     ovs_be16 tci;
2770 };
2771
2772 static bool
2773 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2774 {
2775     const uint16_t mask = ((1U << bits) - 1) << offset;
2776
2777     if (value >> bits) {
2778         return false;
2779     }
2780
2781     *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2782     return true;
2783 }
2784
2785 static int
2786 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2787              uint8_t offset)
2788 {
2789     uint16_t key_, mask_;
2790     int n;
2791
2792     if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2793         int len = n;
2794
2795         if (set_be16_bf(key, bits, offset, key_)) {
2796             if (mask) {
2797                 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2798                     len += n;
2799
2800                     if (!set_be16_bf(mask, bits, offset, mask_)) {
2801                         return 0;
2802                     }
2803                 } else {
2804                     *mask |= htons(((1U << bits) - 1) << offset);
2805                 }
2806             }
2807             return len;
2808         }
2809     }
2810     return 0;
2811 }
2812
2813 static int
2814 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2815 {
2816     return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2817 }
2818
2819 static int
2820 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2821 {
2822     return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2823 }
2824
2825 static int
2826 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2827 {
2828     return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2829 }
2830
2831 /* For MPLS. */
2832 static bool
2833 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2834 {
2835     const uint32_t mask = ((1U << bits) - 1) << offset;
2836
2837     if (value >> bits) {
2838         return false;
2839     }
2840
2841     *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2842     return true;
2843 }
2844
2845 static int
2846 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2847              uint8_t offset)
2848 {
2849     uint32_t key_, mask_;
2850     int n;
2851
2852     if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2853         int len = n;
2854
2855         if (set_be32_bf(key, bits, offset, key_)) {
2856             if (mask) {
2857                 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2858                     len += n;
2859
2860                     if (!set_be32_bf(mask, bits, offset, mask_)) {
2861                         return 0;
2862                     }
2863                 } else {
2864                     *mask |= htonl(((1U << bits) - 1) << offset);
2865                 }
2866             }
2867             return len;
2868         }
2869     }
2870     return 0;
2871 }
2872
2873 static int
2874 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2875 {
2876     return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2877 }
2878
2879 static int
2880 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2881 {
2882     return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2883 }
2884
2885 static int
2886 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2887 {
2888     return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2889 }
2890
2891 static int
2892 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2893 {
2894     return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2895 }
2896
2897 static int
2898 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2899 {
2900     const char *s_base = s;
2901     ovs_be16 id = 0, id_mask = 0;
2902     uint8_t flags = 0, flags_mask = 0;
2903
2904     if (!strncmp(s, "id=", 3)) {
2905         s += 3;
2906         s += scan_be16(s, &id, mask ? &id_mask : NULL);
2907     }
2908
2909     if (s[0] == ',') {
2910         s++;
2911     }
2912     if (!strncmp(s, "flags=", 6)) {
2913         s += 6;
2914         s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
2915     }
2916
2917     if (!strncmp(s, "))", 2)) {
2918         s += 2;
2919
2920         *key = (flags << 16) | ntohs(id);
2921         if (mask) {
2922             *mask = (flags_mask << 16) | ntohs(id_mask);
2923         }
2924
2925         return s - s_base;
2926     }
2927
2928     return 0;
2929 }
2930
2931 static int
2932 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2933 {
2934     const char *s_base = s;
2935     struct geneve_opt *opt = key->d;
2936     struct geneve_opt *opt_mask = mask ? mask->d : NULL;
2937     int len_remain = sizeof key->d;
2938
2939     while (s[0] == '{' && len_remain >= sizeof *opt) {
2940         int data_len = 0;
2941
2942         s++;
2943         len_remain -= sizeof *opt;
2944
2945         if (!strncmp(s, "class=", 6)) {
2946             s += 6;
2947             s += scan_be16(s, &opt->opt_class,
2948                            mask ? &opt_mask->opt_class : NULL);
2949         } else if (mask) {
2950             memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2951         }
2952
2953         if (s[0] == ',') {
2954             s++;
2955         }
2956         if (!strncmp(s, "type=", 5)) {
2957             s += 5;
2958             s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2959         } else if (mask) {
2960             memset(&opt_mask->type, 0, sizeof opt_mask->type);
2961         }
2962
2963         if (s[0] == ',') {
2964             s++;
2965         }
2966         if (!strncmp(s, "len=", 4)) {
2967             uint8_t opt_len, opt_len_mask;
2968             s += 4;
2969             s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2970
2971             if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2972                 return 0;
2973             }
2974             opt->length = opt_len / 4;
2975             if (mask) {
2976                 opt_mask->length = opt_len_mask;
2977             }
2978             data_len = opt_len;
2979         } else if (mask) {
2980             memset(&opt_mask->type, 0, sizeof opt_mask->type);
2981         }
2982
2983         if (s[0] == ',') {
2984             s++;
2985         }
2986         if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2987             return 0;
2988         }
2989
2990         if (mask) {
2991             if (s[0] == '/') {
2992                 s++;
2993                 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2994                                      data_len, (char **)&s)) {
2995                     return 0;
2996                 }
2997             }
2998             opt_mask->r1 = 0;
2999             opt_mask->r2 = 0;
3000             opt_mask->r3 = 0;
3001         }
3002
3003         if (s[0] == '}') {
3004             s++;
3005             opt += 1 + data_len / 4;
3006             if (mask) {
3007                 opt_mask += 1 + data_len / 4;
3008             }
3009             len_remain -= data_len;
3010         }
3011     }
3012
3013     if (s[0] == ')') {
3014         int len = sizeof key->d - len_remain;
3015
3016         s++;
3017         key->len = len;
3018         if (mask) {
3019             mask->len = len;
3020         }
3021         return s - s_base;
3022     }
3023
3024     return 0;
3025 }
3026
3027 static void
3028 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3029 {
3030     const uint16_t *flags = data_;
3031
3032     if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3033         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3034     }
3035     if (*flags & FLOW_TNL_F_CSUM) {
3036         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3037     }
3038     if (*flags & FLOW_TNL_F_OAM) {
3039         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3040     }
3041 }
3042
3043 static void
3044 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3045 {
3046     const uint32_t *gbp = data_;
3047
3048     if (*gbp) {
3049         size_t vxlan_opts_ofs;
3050
3051         vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3052         nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3053         nl_msg_end_nested(a, vxlan_opts_ofs);
3054     }
3055 }
3056
3057 static void
3058 geneve_to_attr(struct ofpbuf *a, const void *data_)
3059 {
3060     const struct geneve_scan *geneve = data_;
3061
3062     nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3063                       geneve->len);
3064 }
3065
3066 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC)                      \
3067     {                                                             \
3068         unsigned long call_fn = (unsigned long)FUNC;              \
3069         if (call_fn) {                                            \
3070             typedef void (*fn)(struct ofpbuf *, const void *);    \
3071             fn func = FUNC;                                       \
3072             func(BUF, &(DATA));                                   \
3073         } else {                                                  \
3074             nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3075         }                                                         \
3076     }
3077
3078 #define SCAN_IF(NAME)                           \
3079     if (strncmp(s, NAME, strlen(NAME)) == 0) {  \
3080         const char *start = s;                  \
3081         int len;                                \
3082                                                 \
3083         s += strlen(NAME)
3084
3085 /* Usually no special initialization is needed. */
3086 #define SCAN_BEGIN(NAME, TYPE)                  \
3087     SCAN_IF(NAME);                              \
3088         TYPE skey, smask;                       \
3089         memset(&skey, 0, sizeof skey);          \
3090         memset(&smask, 0, sizeof smask);        \
3091         do {                                    \
3092             len = 0;
3093
3094 /* Init as fully-masked as mask will not be scanned. */
3095 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE)     \
3096     SCAN_IF(NAME);                              \
3097         TYPE skey, smask;                       \
3098         memset(&skey, 0, sizeof skey);          \
3099         memset(&smask, 0xff, sizeof smask);     \
3100         do {                                    \
3101             len = 0;
3102
3103 /* VLAN needs special initialization. */
3104 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT)  \
3105     SCAN_IF(NAME);                                        \
3106         TYPE skey = KEY_INIT;                       \
3107         TYPE smask = MASK_INIT;                     \
3108         do {                                        \
3109             len = 0;
3110
3111 /* Scan unnamed entry as 'TYPE' */
3112 #define SCAN_TYPE(TYPE, KEY, MASK)              \
3113     len = scan_##TYPE(s, KEY, MASK);            \
3114     if (len == 0) {                             \
3115         return -EINVAL;                         \
3116     }                                           \
3117     s += len
3118
3119 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3120 #define SCAN_FIELD(NAME, TYPE, FIELD)                                   \
3121     if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
3122         s += strlen(NAME);                                              \
3123         SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL);       \
3124         continue;                                                       \
3125     }
3126
3127 #define SCAN_FINISH()                           \
3128         } while (*s++ == ',' && len != 0);      \
3129         if (s[-1] != ')') {                     \
3130             return -EINVAL;                     \
3131         }
3132
3133 #define SCAN_FINISH_SINGLE()                    \
3134         } while (false);                        \
3135         if (*s++ != ')') {                      \
3136             return -EINVAL;                     \
3137         }
3138
3139 /* Beginning of nested attribute. */
3140 #define SCAN_BEGIN_NESTED(NAME, ATTR)                      \
3141     SCAN_IF(NAME);                                         \
3142         size_t key_offset, mask_offset;                    \
3143         key_offset = nl_msg_start_nested(key, ATTR);       \
3144         if (mask) {                                        \
3145             mask_offset = nl_msg_start_nested(mask, ATTR); \
3146         }                                                  \
3147         do {                                               \
3148             len = 0;
3149
3150 #define SCAN_END_NESTED()                               \
3151         SCAN_FINISH();                                  \
3152         nl_msg_end_nested(key, key_offset);             \
3153         if (mask) {                                     \
3154             nl_msg_end_nested(mask, mask_offset);       \
3155         }                                               \
3156         return s - start;                               \
3157     }
3158
3159 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC)  \
3160     if (strncmp(s, NAME, strlen(NAME)) == 0) {                \
3161         TYPE skey, smask;                                     \
3162         memset(&skey, 0, sizeof skey);                        \
3163         memset(&smask, 0xff, sizeof smask);                   \
3164         s += strlen(NAME);                                    \
3165         SCAN_TYPE(SCAN_AS, &skey, &smask);                    \
3166         SCAN_PUT(ATTR, FUNC);                                 \
3167         continue;                                             \
3168     }
3169
3170 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR)  \
3171         SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3172
3173 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC)  \
3174         SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3175
3176 #define SCAN_PUT(ATTR, FUNC)                            \
3177         if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3178             SCAN_PUT_ATTR(key, ATTR, skey, FUNC);       \
3179             if (mask) {                                 \
3180                 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3181             }                                           \
3182         }
3183
3184 #define SCAN_END(ATTR)                                  \
3185         SCAN_FINISH();                                  \
3186         SCAN_PUT(ATTR, NULL);                           \
3187         return s - start;                               \
3188     }
3189
3190 #define SCAN_END_SINGLE(ATTR)                           \
3191         SCAN_FINISH_SINGLE();                           \
3192         SCAN_PUT(ATTR, NULL);                           \
3193         return s - start;                               \
3194     }
3195
3196 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR)       \
3197     SCAN_BEGIN(NAME, TYPE) {                         \
3198         SCAN_TYPE(SCAN_AS, &skey, &smask);           \
3199     } SCAN_END_SINGLE(ATTR)
3200
3201 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3202     SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) {                   \
3203         SCAN_TYPE(SCAN_AS, &skey, NULL);                    \
3204     } SCAN_END_SINGLE(ATTR)
3205
3206 /* scan_port needs one extra argument. */
3207 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR)  \
3208     SCAN_BEGIN(NAME, TYPE) {                            \
3209         len = scan_port(s, &skey, &smask, port_names);  \
3210         if (len == 0) {                                 \
3211             return -EINVAL;                             \
3212         }                                               \
3213         s += len;                                       \
3214     } SCAN_END_SINGLE(ATTR)
3215
3216 static int
3217 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3218                         struct ofpbuf *key, struct ofpbuf *mask)
3219 {
3220     ovs_u128 ufid;
3221     int len;
3222
3223     /* Skip UFID. */
3224     len = odp_ufid_from_string(s, &ufid);
3225     if (len) {
3226         return len;
3227     }
3228
3229     SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3230     SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3231     SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3232                              OVS_KEY_ATTR_RECIRC_ID);
3233     SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3234
3235     SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3236         SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3237         SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3238         SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3239         SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3240         SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3241         SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3242         SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3243         SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3244         SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3245                                geneve_to_attr);
3246         SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3247     } SCAN_END_NESTED();
3248
3249     SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3250
3251     SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3252         SCAN_FIELD("src=", eth, eth_src);
3253         SCAN_FIELD("dst=", eth, eth_dst);
3254     } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3255
3256     SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3257                     { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3258         SCAN_FIELD("vid=", vid, tci);
3259         SCAN_FIELD("pcp=", pcp, tci);
3260         SCAN_FIELD("cfi=", cfi, tci);
3261     } SCAN_END(OVS_KEY_ATTR_VLAN);
3262
3263     SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3264
3265     SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3266         SCAN_FIELD("label=", mpls_label, mpls_lse);
3267         SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3268         SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3269         SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3270     } SCAN_END(OVS_KEY_ATTR_MPLS);
3271
3272     SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3273         SCAN_FIELD("src=", ipv4, ipv4_src);
3274         SCAN_FIELD("dst=", ipv4, ipv4_dst);
3275         SCAN_FIELD("proto=", u8, ipv4_proto);
3276         SCAN_FIELD("tos=", u8, ipv4_tos);
3277         SCAN_FIELD("ttl=", u8, ipv4_ttl);
3278         SCAN_FIELD("frag=", frag, ipv4_frag);
3279     } SCAN_END(OVS_KEY_ATTR_IPV4);
3280
3281     SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3282         SCAN_FIELD("src=", ipv6, ipv6_src);
3283         SCAN_FIELD("dst=", ipv6, ipv6_dst);
3284         SCAN_FIELD("label=", ipv6_label, ipv6_label);
3285         SCAN_FIELD("proto=", u8, ipv6_proto);
3286         SCAN_FIELD("tclass=", u8, ipv6_tclass);
3287         SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3288         SCAN_FIELD("frag=", frag, ipv6_frag);
3289     } SCAN_END(OVS_KEY_ATTR_IPV6);
3290
3291     SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3292         SCAN_FIELD("src=", be16, tcp_src);
3293         SCAN_FIELD("dst=", be16, tcp_dst);
3294     } SCAN_END(OVS_KEY_ATTR_TCP);
3295
3296     SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3297
3298     SCAN_BEGIN("udp(", struct ovs_key_udp) {
3299         SCAN_FIELD("src=", be16, udp_src);
3300         SCAN_FIELD("dst=", be16, udp_dst);
3301     } SCAN_END(OVS_KEY_ATTR_UDP);
3302
3303     SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3304         SCAN_FIELD("src=", be16, sctp_src);
3305         SCAN_FIELD("dst=", be16, sctp_dst);
3306     } SCAN_END(OVS_KEY_ATTR_SCTP);
3307
3308     SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3309         SCAN_FIELD("type=", u8, icmp_type);
3310         SCAN_FIELD("code=", u8, icmp_code);
3311     } SCAN_END(OVS_KEY_ATTR_ICMP);
3312
3313     SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3314         SCAN_FIELD("type=", u8, icmpv6_type);
3315         SCAN_FIELD("code=", u8, icmpv6_code);
3316     } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3317
3318     SCAN_BEGIN("arp(", struct ovs_key_arp) {
3319         SCAN_FIELD("sip=", ipv4, arp_sip);
3320         SCAN_FIELD("tip=", ipv4, arp_tip);
3321         SCAN_FIELD("op=", be16, arp_op);
3322         SCAN_FIELD("sha=", eth, arp_sha);
3323         SCAN_FIELD("tha=", eth, arp_tha);
3324     } SCAN_END(OVS_KEY_ATTR_ARP);
3325
3326     SCAN_BEGIN("nd(", struct ovs_key_nd) {
3327         SCAN_FIELD("target=", ipv6, nd_target);
3328         SCAN_FIELD("sll=", eth, nd_sll);
3329         SCAN_FIELD("tll=", eth, nd_tll);
3330     } SCAN_END(OVS_KEY_ATTR_ND);
3331
3332     /* Encap open-coded. */
3333     if (!strncmp(s, "encap(", 6)) {
3334         const char *start = s;
3335         size_t encap, encap_mask = 0;
3336
3337         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
3338         if (mask) {
3339             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3340         }
3341
3342         s += 6;
3343         for (;;) {
3344             int retval;
3345
3346             s += strspn(s, delimiters);
3347             if (!*s) {
3348                 return -EINVAL;
3349             } else if (*s == ')') {
3350                 break;
3351             }
3352
3353             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3354             if (retval < 0) {
3355                 return retval;
3356             }
3357             s += retval;
3358         }
3359         s++;
3360
3361         nl_msg_end_nested(key, encap);
3362         if (mask) {
3363             nl_msg_end_nested(mask, encap_mask);
3364         }
3365
3366         return s - start;
3367     }
3368
3369     return -EINVAL;
3370 }
3371
3372 /* Parses the string representation of a datapath flow key, in the
3373  * format output by odp_flow_key_format().  Returns 0 if successful,
3374  * otherwise a positive errno value.  On success, the flow key is
3375  * appended to 'key' as a series of Netlink attributes.  On failure, no
3376  * data is appended to 'key'.  Either way, 'key''s data might be
3377  * reallocated.
3378  *
3379  * If 'port_names' is nonnull, it points to an simap that maps from a port name
3380  * to a port number.  (Port names may be used instead of port numbers in
3381  * in_port.)
3382  *
3383  * On success, the attributes appended to 'key' are individually syntactically
3384  * valid, but they may not be valid as a sequence.  'key' might, for example,
3385  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
3386 int
3387 odp_flow_from_string(const char *s, const struct simap *port_names,
3388                      struct ofpbuf *key, struct ofpbuf *mask)
3389 {
3390     const size_t old_size = key->size;
3391     for (;;) {
3392         int retval;
3393
3394         s += strspn(s, delimiters);
3395         if (!*s) {
3396             return 0;
3397         }
3398
3399         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3400         if (retval < 0) {
3401             key->size = old_size;
3402             return -retval;
3403         }
3404         s += retval;
3405     }
3406
3407     return 0;
3408 }
3409
3410 static uint8_t
3411 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
3412 {
3413     if (is_mask) {
3414         /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3415          * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3416          * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3417          * must use a zero mask for the netlink frag field, and all ones mask
3418          * otherwise. */
3419         return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3420     }
3421     return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3422         : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3423         : OVS_FRAG_TYPE_FIRST;
3424 }
3425
3426 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3427 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3428 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3429                          bool is_mask);
3430 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3431                          bool is_mask);
3432 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3433                          bool is_mask);
3434 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3435                          bool is_mask);
3436 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3437 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
3438 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3439 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3440
3441 /* These share the same layout. */
3442 union ovs_key_tp {
3443     struct ovs_key_tcp tcp;
3444     struct ovs_key_udp udp;
3445     struct ovs_key_sctp sctp;
3446 };
3447
3448 static void get_tp_key(const struct flow *, union ovs_key_tp *);
3449 static void put_tp_key(const union ovs_key_tp *, struct flow *);
3450
3451 static void
3452 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
3453                          bool export_mask, struct ofpbuf *buf)
3454 {
3455     struct ovs_key_ethernet *eth_key;
3456     size_t encap;
3457     const struct flow *flow = parms->flow;
3458     const struct flow *data = export_mask ? parms->mask : parms->flow;
3459
3460     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
3461
3462     if (flow->tunnel.ip_dst || export_mask) {
3463         tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
3464                         parms->key_buf);
3465     }
3466
3467     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
3468
3469     if (parms->recirc) {
3470         nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
3471         nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3472     }
3473
3474     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3475      * is not the magical value "ODPP_NONE". */
3476     if (export_mask || parms->odp_in_port != ODPP_NONE) {
3477         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
3478     }
3479
3480     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3481                                        sizeof *eth_key);
3482     get_ethernet_key(data, eth_key);
3483
3484     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3485         if (export_mask) {
3486             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3487         } else {
3488             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3489         }
3490         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3491         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3492         if (flow->vlan_tci == htons(0)) {
3493             goto unencap;
3494         }
3495     } else {
3496         encap = 0;
3497     }
3498
3499     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3500         /* For backwards compatibility with kernels that don't support
3501          * wildcarding, the following convention is used to encode the
3502          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3503          *
3504          *   key      mask    matches
3505          * -------- --------  -------
3506          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
3507          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
3508          *  <none>   0xffff   Any non-Ethernet II frame (except valid
3509          *                    802.3 SNAP packet with valid eth_type).
3510          */
3511         if (export_mask) {
3512             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3513         }
3514         goto unencap;
3515     }
3516
3517     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3518
3519     if (flow->dl_type == htons(ETH_TYPE_IP)) {
3520         struct ovs_key_ipv4 *ipv4_key;
3521
3522         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3523                                             sizeof *ipv4_key);
3524         get_ipv4_key(data, ipv4_key, export_mask);
3525     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3526         struct ovs_key_ipv6 *ipv6_key;
3527
3528         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3529                                             sizeof *ipv6_key);
3530         get_ipv6_key(data, ipv6_key, export_mask);
3531     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3532                flow->dl_type == htons(ETH_TYPE_RARP)) {
3533         struct ovs_key_arp *arp_key;
3534
3535         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3536                                            sizeof *arp_key);
3537         get_arp_key(data, arp_key);
3538     } else if (eth_type_mpls(flow->dl_type)) {
3539         struct ovs_key_mpls *mpls_key;
3540         int i, n;
3541
3542         n = flow_count_mpls_labels(flow, NULL);
3543         if (export_mask) {
3544             n = MIN(n, parms->max_mpls_depth);
3545         }
3546         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3547                                             n * sizeof *mpls_key);
3548         for (i = 0; i < n; i++) {
3549             mpls_key[i].mpls_lse = data->mpls_lse[i];
3550         }
3551     }
3552
3553     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3554         if (flow->nw_proto == IPPROTO_TCP) {
3555             union ovs_key_tp *tcp_key;
3556
3557             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3558                                                sizeof *tcp_key);
3559             get_tp_key(data, tcp_key);
3560             if (data->tcp_flags) {
3561                 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3562             }
3563         } else if (flow->nw_proto == IPPROTO_UDP) {
3564             union ovs_key_tp *udp_key;
3565
3566             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3567                                                sizeof *udp_key);
3568             get_tp_key(data, udp_key);
3569         } else if (flow->nw_proto == IPPROTO_SCTP) {
3570             union ovs_key_tp *sctp_key;
3571
3572             sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3573                                                sizeof *sctp_key);
3574             get_tp_key(data, sctp_key);
3575         } else if (flow->dl_type == htons(ETH_TYPE_IP)
3576                 && flow->nw_proto == IPPROTO_ICMP) {
3577             struct ovs_key_icmp *icmp_key;
3578
3579             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3580                                                 sizeof *icmp_key);
3581             icmp_key->icmp_type = ntohs(data->tp_src);
3582             icmp_key->icmp_code = ntohs(data->tp_dst);
3583         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3584                 && flow->nw_proto == IPPROTO_ICMPV6) {
3585             struct ovs_key_icmpv6 *icmpv6_key;
3586
3587             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3588                                                   sizeof *icmpv6_key);
3589             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3590             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3591
3592             if (flow->tp_dst == htons(0)
3593                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3594                     || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3595                 && (!export_mask || (data->tp_src == htons(0xffff)
3596                                      && data->tp_dst == htons(0xffff)))) {
3597
3598                 struct ovs_key_nd *nd_key;
3599
3600                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3601                                                     sizeof *nd_key);
3602                 memcpy(nd_key->nd_target, &data->nd_target,
3603                         sizeof nd_key->nd_target);
3604                 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3605                 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3606             }
3607         }
3608     }
3609
3610 unencap:
3611     if (encap) {
3612         nl_msg_end_nested(buf, encap);
3613     }
3614 }
3615
3616 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3617  *
3618  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3619  * capable of being expanded to allow for that much space. */
3620 void
3621 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
3622                        struct ofpbuf *buf)
3623 {
3624     odp_flow_key_from_flow__(parms, false, buf);
3625 }
3626
3627 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3628  * 'buf'.
3629  *
3630  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3631  * capable of being expanded to allow for that much space. */
3632 void
3633 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
3634                        struct ofpbuf *buf)
3635 {
3636     odp_flow_key_from_flow__(parms, true, buf);
3637 }
3638
3639 /* Generate ODP flow key from the given packet metadata */
3640 void
3641 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3642 {
3643     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3644
3645     if (md->tunnel.ip_dst) {
3646         tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
3647     }
3648
3649     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3650
3651     /* Add an ingress port attribute if 'odp_in_port' is not the magical
3652      * value "ODPP_NONE". */
3653     if (md->in_port.odp_port != ODPP_NONE) {
3654         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3655     }
3656 }
3657
3658 /* Generate packet metadata from the given ODP flow key. */
3659 void
3660 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3661                         struct pkt_metadata *md)
3662 {
3663     const struct nlattr *nla;
3664     size_t left;
3665     uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3666         1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3667         1u << OVS_KEY_ATTR_IN_PORT;
3668
3669     *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3670
3671     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3672         uint16_t type = nl_attr_type(nla);
3673         size_t len = nl_attr_get_size(nla);
3674         int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3675                                             OVS_KEY_ATTR_MAX, type);
3676
3677         if (len != expected_len && expected_len >= 0) {
3678             continue;
3679         }
3680
3681         switch (type) {
3682         case OVS_KEY_ATTR_RECIRC_ID:
3683             md->recirc_id = nl_attr_get_u32(nla);
3684             wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3685             break;
3686         case OVS_KEY_ATTR_DP_HASH:
3687             md->dp_hash = nl_attr_get_u32(nla);
3688             wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3689             break;
3690         case OVS_KEY_ATTR_PRIORITY:
3691             md->skb_priority = nl_attr_get_u32(nla);
3692             wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3693             break;
3694         case OVS_KEY_ATTR_SKB_MARK:
3695             md->pkt_mark = nl_attr_get_u32(nla);
3696             wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3697             break;
3698         case OVS_KEY_ATTR_TUNNEL: {
3699             enum odp_key_fitness res;
3700
3701             res = odp_tun_key_from_attr(nla, &md->tunnel);
3702             if (res == ODP_FIT_ERROR) {
3703                 memset(&md->tunnel, 0, sizeof md->tunnel);
3704             } else if (res == ODP_FIT_PERFECT) {
3705                 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3706             }
3707             break;
3708         }
3709         case OVS_KEY_ATTR_IN_PORT:
3710             md->in_port.odp_port = nl_attr_get_odp_port(nla);
3711             wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3712             break;
3713         default:
3714             break;
3715         }
3716
3717         if (!wanted_attrs) {
3718             return; /* Have everything. */
3719         }
3720     }
3721 }
3722
3723 uint32_t
3724 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3725 {
3726     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3727     return hash_words(ALIGNED_CAST(const uint32_t *, key),
3728                       key_len / sizeof(uint32_t), 0);
3729 }
3730
3731 static void
3732 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3733                        uint64_t attrs, int out_of_range_attr,
3734                        const struct nlattr *key, size_t key_len)
3735 {
3736     struct ds s;
3737     int i;
3738
3739     if (VLOG_DROP_DBG(rl)) {
3740         return;
3741     }
3742
3743     ds_init(&s);
3744     for (i = 0; i < 64; i++) {
3745         if (attrs & (UINT64_C(1) << i)) {
3746             char namebuf[OVS_KEY_ATTR_BUFSIZE];
3747
3748             ds_put_format(&s, " %s",
3749                           ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3750         }
3751     }
3752     if (out_of_range_attr) {
3753         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3754     }
3755
3756     ds_put_cstr(&s, ": ");
3757     odp_flow_key_format(key, key_len, &s);
3758
3759     VLOG_DBG("%s:%s", title, ds_cstr(&s));
3760     ds_destroy(&s);
3761 }
3762
3763 static uint8_t
3764 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3765 {
3766     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3767
3768     if (is_mask) {
3769         return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3770     }
3771
3772     if (odp_frag > OVS_FRAG_TYPE_LATER) {
3773         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3774         return 0xff; /* Error. */
3775     }
3776
3777     return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3778         : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3779         :  FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3780 }
3781
3782 static bool
3783 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3784                    const struct nlattr *attrs[], uint64_t *present_attrsp,
3785                    int *out_of_range_attrp)
3786 {
3787     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3788     const struct nlattr *nla;
3789     uint64_t present_attrs;
3790     size_t left;
3791
3792     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3793     present_attrs = 0;
3794     *out_of_range_attrp = 0;
3795     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3796         uint16_t type = nl_attr_type(nla);
3797         size_t len = nl_attr_get_size(nla);
3798         int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3799                                             OVS_KEY_ATTR_MAX, type);
3800
3801         if (len != expected_len && expected_len >= 0) {
3802             char namebuf[OVS_KEY_ATTR_BUFSIZE];
3803
3804             VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3805                         "length %d", ovs_key_attr_to_string(type, namebuf,
3806                                                             sizeof namebuf),
3807                         len, expected_len);
3808             return false;
3809         }
3810
3811         if (type > OVS_KEY_ATTR_MAX) {
3812             *out_of_range_attrp = type;
3813         } else {
3814             if (present_attrs & (UINT64_C(1) << type)) {
3815                 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3816
3817                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3818                             ovs_key_attr_to_string(type,
3819                                                    namebuf, sizeof namebuf));
3820                 return false;
3821             }
3822
3823             present_attrs |= UINT64_C(1) << type;
3824             attrs[type] = nla;
3825         }
3826     }
3827     if (left) {
3828         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3829         return false;
3830     }
3831
3832     *present_attrsp = present_attrs;
3833     return true;
3834 }
3835
3836 static enum odp_key_fitness
3837 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3838                    uint64_t expected_attrs,
3839                    const struct nlattr *key, size_t key_len)
3840 {
3841     uint64_t missing_attrs;
3842     uint64_t extra_attrs;
3843
3844     missing_attrs = expected_attrs & ~present_attrs;
3845     if (missing_attrs) {
3846         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3847         log_odp_key_attributes(&rl, "expected but not present",
3848                                missing_attrs, 0, key, key_len);
3849         return ODP_FIT_TOO_LITTLE;
3850     }
3851
3852     extra_attrs = present_attrs & ~expected_attrs;
3853     if (extra_attrs || out_of_range_attr) {
3854         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3855         log_odp_key_attributes(&rl, "present but not expected",
3856                                extra_attrs, out_of_range_attr, key, key_len);
3857         return ODP_FIT_TOO_MUCH;
3858     }
3859
3860     return ODP_FIT_PERFECT;
3861 }
3862
3863 static bool
3864 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3865                 uint64_t present_attrs, uint64_t *expected_attrs,
3866                 struct flow *flow, const struct flow *src_flow)
3867 {
3868     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3869     bool is_mask = flow != src_flow;
3870
3871     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3872         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3873         if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3874             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3875                         ntohs(flow->dl_type));
3876             return false;
3877         }
3878         if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3879             flow->dl_type != htons(0xffff)) {
3880             return false;
3881         }
3882         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3883     } else {
3884         if (!is_mask) {
3885             flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3886         } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3887             /* See comments in odp_flow_key_from_flow__(). */
3888             VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3889             return false;
3890         }
3891     }
3892     return true;
3893 }
3894
3895 static enum odp_key_fitness
3896 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3897                   uint64_t present_attrs, int out_of_range_attr,
3898                   uint64_t expected_attrs, struct flow *flow,
3899                   const struct nlattr *key, size_t key_len,
3900                   const struct flow *src_flow)
3901 {
3902     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3903     bool is_mask = src_flow != flow;
3904     const void *check_start = NULL;
3905     size_t check_len = 0;
3906     enum ovs_key_attr expected_bit = 0xff;
3907
3908     if (eth_type_mpls(src_flow->dl_type)) {
3909         if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3910             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3911         }
3912         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3913             size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3914             const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3915             int n = size / sizeof(ovs_be32);
3916             int i;
3917
3918             if (!size || size % sizeof(ovs_be32)) {
3919                 return ODP_FIT_ERROR;
3920             }
3921             if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3922                 return ODP_FIT_ERROR;
3923             }
3924
3925             for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3926                 flow->mpls_lse[i] = mpls_lse[i];
3927             }
3928             if (n > FLOW_MAX_MPLS_LABELS) {
3929                 return ODP_FIT_TOO_MUCH;
3930             }
3931
3932             if (!is_mask) {
3933                 /* BOS may be set only in the innermost label. */
3934                 for (i = 0; i < n - 1; i++) {
3935                     if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3936                         return ODP_FIT_ERROR;
3937                     }
3938                 }
3939
3940                 /* BOS must be set in the innermost label. */
3941                 if (n < FLOW_MAX_MPLS_LABELS
3942                     && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3943                     return ODP_FIT_TOO_LITTLE;
3944                 }
3945             }
3946         }
3947
3948         goto done;
3949     } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3950         if (!is_mask) {
3951             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3952         }
3953         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3954             const struct ovs_key_ipv4 *ipv4_key;
3955
3956             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3957             put_ipv4_key(ipv4_key, flow, is_mask);
3958             if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3959                 return ODP_FIT_ERROR;
3960             }
3961             if (is_mask) {
3962                 check_start = ipv4_key;
3963                 check_len = sizeof *ipv4_key;
3964                 expected_bit = OVS_KEY_ATTR_IPV4;
3965             }
3966         }
3967     } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3968         if (!is_mask) {
3969             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3970         }
3971         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3972             const struct ovs_key_ipv6 *ipv6_key;
3973
3974             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3975             put_ipv6_key(ipv6_key, flow, is_mask);
3976             if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3977                 return ODP_FIT_ERROR;
3978             }
3979             if (is_mask) {
3980                 check_start = ipv6_key;
3981                 check_len = sizeof *ipv6_key;
3982                 expected_bit = OVS_KEY_ATTR_IPV6;
3983             }
3984         }
3985     } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3986                src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3987         if (!is_mask) {
3988             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3989         }
3990         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3991             const struct ovs_key_arp *arp_key;
3992
3993             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3994             if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3995                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3996                             "key", ntohs(arp_key->arp_op));
3997                 return ODP_FIT_ERROR;
3998             }
3999             put_arp_key(arp_key, flow);
4000             if (is_mask) {
4001                 check_start = arp_key;
4002                 check_len = sizeof *arp_key;
4003                 expected_bit = OVS_KEY_ATTR_ARP;
4004             }
4005         }
4006     } else {
4007         goto done;
4008     }
4009     if (check_len > 0) { /* Happens only when 'is_mask'. */
4010         if (!is_all_zeros(check_start, check_len) &&
4011             flow->dl_type != htons(0xffff)) {
4012             return ODP_FIT_ERROR;
4013         } else {
4014             expected_attrs |= UINT64_C(1) << expected_bit;
4015         }
4016     }
4017
4018     expected_bit = OVS_KEY_ATTR_UNSPEC;
4019     if (src_flow->nw_proto == IPPROTO_TCP
4020         && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4021             src_flow->dl_type == htons(ETH_TYPE_IPV6))
4022         && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4023         if (!is_mask) {
4024             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4025         }
4026         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4027             const union ovs_key_tp *tcp_key;
4028
4029             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4030             put_tp_key(tcp_key, flow);
4031             expected_bit = OVS_KEY_ATTR_TCP;
4032         }
4033         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4034             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4035             flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4036         }
4037     } else if (src_flow->nw_proto == IPPROTO_UDP
4038                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4039                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
4040                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4041         if (!is_mask) {
4042             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4043         }
4044         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4045             const union ovs_key_tp *udp_key;
4046
4047             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4048             put_tp_key(udp_key, flow);
4049             expected_bit = OVS_KEY_ATTR_UDP;
4050         }
4051     } else if (src_flow->nw_proto == IPPROTO_SCTP
4052                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4053                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
4054                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4055         if (!is_mask) {
4056             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4057         }
4058         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4059             const union ovs_key_tp *sctp_key;
4060
4061             sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4062             put_tp_key(sctp_key, flow);
4063             expected_bit = OVS_KEY_ATTR_SCTP;
4064         }
4065     } else if (src_flow->nw_proto == IPPROTO_ICMP
4066                && src_flow->dl_type == htons(ETH_TYPE_IP)
4067                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4068         if (!is_mask) {
4069             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4070         }
4071         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4072             const struct ovs_key_icmp *icmp_key;
4073
4074             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4075             flow->tp_src = htons(icmp_key->icmp_type);
4076             flow->tp_dst = htons(icmp_key->icmp_code);
4077             expected_bit = OVS_KEY_ATTR_ICMP;
4078         }
4079     } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4080                && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4081                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4082         if (!is_mask) {
4083             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4084         }
4085         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4086             const struct ovs_key_icmpv6 *icmpv6_key;
4087
4088             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4089             flow->tp_src = htons(icmpv6_key->icmpv6_type);
4090             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4091             expected_bit = OVS_KEY_ATTR_ICMPV6;
4092             if (src_flow->tp_dst == htons(0) &&
4093                 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4094                  src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4095                 if (!is_mask) {
4096                     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4097                 }
4098                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4099                     const struct ovs_key_nd *nd_key;
4100
4101                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4102                     memcpy(&flow->nd_target, nd_key->nd_target,
4103                            sizeof flow->nd_target);
4104                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4105                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4106                     if (is_mask) {
4107                         if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4108                             (flow->tp_src != htons(0xffff) ||
4109                              flow->tp_dst != htons(0xffff))) {
4110                             return ODP_FIT_ERROR;
4111                         } else {
4112                             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4113                         }
4114                     }
4115                 }
4116             }
4117         }
4118     }
4119     if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4120         if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4121             return ODP_FIT_ERROR;
4122         } else {
4123             expected_attrs |= UINT64_C(1) << expected_bit;
4124         }
4125     }
4126
4127 done:
4128     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4129                               key, key_len);
4130 }
4131
4132 /* Parse 802.1Q header then encapsulated L3 attributes. */
4133 static enum odp_key_fitness
4134 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4135                    uint64_t present_attrs, int out_of_range_attr,
4136                    uint64_t expected_attrs, struct flow *flow,
4137                    const struct nlattr *key, size_t key_len,
4138                    const struct flow *src_flow)
4139 {
4140     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4141     bool is_mask = src_flow != flow;
4142
4143     const struct nlattr *encap
4144         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4145            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4146     enum odp_key_fitness encap_fitness;
4147     enum odp_key_fitness fitness;
4148
4149     /* Calculate fitness of outer attributes. */
4150     if (!is_mask) {
4151         expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4152                           (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4153     } else {
4154         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4155             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4156         }
4157         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4158             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4159         }
4160     }
4161     fitness = check_expectations(present_attrs, out_of_range_attr,
4162                                  expected_attrs, key, key_len);
4163
4164     /* Set vlan_tci.
4165      * Remove the TPID from dl_type since it's not the real Ethertype.  */
4166     flow->dl_type = htons(0);
4167     flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4168                       ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4169                       : htons(0));
4170     if (!is_mask) {
4171         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4172             return ODP_FIT_TOO_LITTLE;
4173         } else if (flow->vlan_tci == htons(0)) {
4174             /* Corner case for a truncated 802.1Q header. */
4175             if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4176                 return ODP_FIT_TOO_MUCH;
4177             }
4178             return fitness;
4179         } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4180             VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4181                         "but CFI bit is not set", ntohs(flow->vlan_tci));
4182             return ODP_FIT_ERROR;
4183         }
4184     } else {
4185         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4186             return fitness;
4187         }
4188     }
4189
4190     /* Now parse the encapsulated attributes. */
4191     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4192                             attrs, &present_attrs, &out_of_range_attr)) {
4193         return ODP_FIT_ERROR;
4194     }
4195     expected_attrs = 0;
4196
4197     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4198         return ODP_FIT_ERROR;
4199     }
4200     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4201                                       expected_attrs, flow, key, key_len,
4202                                       src_flow);
4203
4204     /* The overall fitness is the worse of the outer and inner attributes. */
4205     return MAX(fitness, encap_fitness);
4206 }
4207
4208 static enum odp_key_fitness
4209 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4210                        const struct nlattr *src_key, size_t src_key_len,
4211                        struct flow *flow, const struct flow *src_flow)
4212 {
4213     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4214     uint64_t expected_attrs;
4215     uint64_t present_attrs;
4216     int out_of_range_attr;
4217     bool is_mask = src_flow != flow;
4218
4219     memset(flow, 0, sizeof *flow);
4220
4221     /* Parse attributes. */
4222     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4223                             &out_of_range_attr)) {
4224         return ODP_FIT_ERROR;
4225     }
4226     expected_attrs = 0;
4227
4228     /* Metadata. */
4229     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4230         flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4231         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4232     } else if (is_mask) {
4233         /* Always exact match recirc_id if it is not specified. */
4234         flow->recirc_id = UINT32_MAX;
4235     }
4236
4237     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4238         flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4239         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4240     }
4241     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4242         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4243         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4244     }
4245
4246     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4247         flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4248         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4249     }
4250
4251     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4252         enum odp_key_fitness res;
4253
4254         res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], src_key,
4255                                       src_key_len, &src_flow->tunnel,
4256                                       &flow->tunnel);
4257         if (res == ODP_FIT_ERROR) {
4258             return ODP_FIT_ERROR;
4259         } else if (res == ODP_FIT_PERFECT) {
4260             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
4261         }
4262     }
4263
4264     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4265         flow->in_port.odp_port
4266             = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
4267         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4268     } else if (!is_mask) {
4269         flow->in_port.odp_port = ODPP_NONE;
4270     }
4271
4272     /* Ethernet header. */
4273     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4274         const struct ovs_key_ethernet *eth_key;
4275
4276         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
4277         put_ethernet_key(eth_key, flow);
4278         if (is_mask) {
4279             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4280         }
4281     }
4282     if (!is_mask) {
4283         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4284     }
4285
4286     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4287     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4288         src_flow)) {
4289         return ODP_FIT_ERROR;
4290     }
4291
4292     if (is_mask
4293         ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4294         : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
4295         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4296                                   expected_attrs, flow, key, key_len, src_flow);
4297     }
4298     if (is_mask) {
4299         flow->vlan_tci = htons(0xffff);
4300         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4301             flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4302             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4303         }
4304     }
4305     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4306                              expected_attrs, flow, key, key_len, src_flow);
4307 }
4308
4309 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4310  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
4311  * 'key' fits our expectations for what a flow key should contain.
4312  *
4313  * The 'in_port' will be the datapath's understanding of the port.  The
4314  * caller will need to translate with odp_port_to_ofp_port() if the
4315  * OpenFlow port is needed.
4316  *
4317  * This function doesn't take the packet itself as an argument because none of
4318  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
4319  * it is always possible to infer which additional attribute(s) should appear
4320  * by looking at the attributes for lower-level protocols, e.g. if the network
4321  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4322  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4323  * must be absent. */
4324 enum odp_key_fitness
4325 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4326                      struct flow *flow)
4327 {
4328    return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow);
4329 }
4330
4331 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
4332  * to a mask structure in 'mask'.  'flow' must be a previously translated flow
4333  * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
4334  * attributes from that flow.  Returns an ODP_FIT_* value that indicates how
4335  * well 'key' fits our expectations for what a flow key should contain. */
4336 enum odp_key_fitness
4337 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
4338                      const struct nlattr *flow_key, size_t flow_key_len,
4339                      struct flow *mask, const struct flow *flow)
4340 {
4341    return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
4342                                  mask, flow);
4343 }
4344
4345 /* Returns 'fitness' as a string, for use in debug messages. */
4346 const char *
4347 odp_key_fitness_to_string(enum odp_key_fitness fitness)
4348 {
4349     switch (fitness) {
4350     case ODP_FIT_PERFECT:
4351         return "OK";
4352     case ODP_FIT_TOO_MUCH:
4353         return "too_much";
4354     case ODP_FIT_TOO_LITTLE:
4355         return "too_little";
4356     case ODP_FIT_ERROR:
4357         return "error";
4358     default:
4359         return "<unknown>";
4360     }
4361 }
4362
4363 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
4364  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
4365  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4366  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
4367  * null, then the return value is not meaningful.) */
4368 size_t
4369 odp_put_userspace_action(uint32_t pid,
4370                          const void *userdata, size_t userdata_size,
4371                          odp_port_t tunnel_out_port,
4372                          struct ofpbuf *odp_actions)
4373 {
4374     size_t userdata_ofs;
4375     size_t offset;
4376
4377     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4378     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
4379     if (userdata) {
4380         userdata_ofs = odp_actions->size + NLA_HDRLEN;
4381
4382         /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4383          * module before Linux 3.10 required the userdata to be exactly 8 bytes
4384          * long:
4385          *
4386          *   - The kernel rejected shorter userdata with -ERANGE.
4387          *
4388          *   - The kernel silently dropped userdata beyond the first 8 bytes.
4389          *
4390          * Thus, for maximum compatibility, always put at least 8 bytes.  (We
4391          * separately disable features that required more than 8 bytes.) */
4392         memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4393                                       MAX(8, userdata_size)),
4394                userdata, userdata_size);
4395     } else {
4396         userdata_ofs = 0;
4397     }
4398     if (tunnel_out_port != ODPP_NONE) {
4399         nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4400                             tunnel_out_port);
4401     }
4402     nl_msg_end_nested(odp_actions, offset);
4403
4404     return userdata_ofs;
4405 }
4406
4407 void
4408 odp_put_tunnel_action(const struct flow_tnl *tunnel,
4409                       struct ofpbuf *odp_actions)
4410 {
4411     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4412     tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
4413     nl_msg_end_nested(odp_actions, offset);
4414 }
4415
4416 void
4417 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4418                         struct ovs_action_push_tnl *data)
4419 {
4420     int size = offsetof(struct ovs_action_push_tnl, header);
4421
4422     size += data->header_len;
4423     nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4424 }
4425
4426 \f
4427 /* The commit_odp_actions() function and its helpers. */
4428
4429 static void
4430 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4431                   const void *key, size_t key_size)
4432 {
4433     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4434     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4435     nl_msg_end_nested(odp_actions, offset);
4436 }
4437
4438 /* Masked set actions have a mask following the data within the netlink
4439  * attribute.  The unmasked bits in the data will be cleared as the data
4440  * is copied to the action. */
4441 void
4442 commit_masked_set_action(struct ofpbuf *odp_actions,
4443                          enum ovs_key_attr key_type,
4444                          const void *key_, const void *mask_, size_t key_size)
4445 {
4446     size_t offset = nl_msg_start_nested(odp_actions,
4447                                         OVS_ACTION_ATTR_SET_MASKED);
4448     char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4449     const char *key = key_, *mask = mask_;
4450
4451     memcpy(data + key_size, mask, key_size);
4452     /* Clear unmasked bits while copying. */
4453     while (key_size--) {
4454         *data++ = *key++ & *mask++;
4455     }
4456     nl_msg_end_nested(odp_actions, offset);
4457 }
4458
4459 /* If any of the flow key data that ODP actions can modify are different in
4460  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4461  * 'odp_actions' that change the flow tunneling information in key from
4462  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4463  * same way.  In other words, operates the same as commit_odp_actions(), but
4464  * only on tunneling information. */
4465 void
4466 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
4467                          struct ofpbuf *odp_actions)
4468 {
4469     /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4470     if (flow->tunnel.ip_dst) {
4471         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4472             return;
4473         }
4474         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4475         odp_put_tunnel_action(&base->tunnel, odp_actions);
4476     }
4477 }
4478
4479 static bool
4480 commit(enum ovs_key_attr attr, bool use_masked_set,
4481        const void *key, void *base, void *mask, size_t size,
4482        struct ofpbuf *odp_actions)
4483 {
4484     if (memcmp(key, base, size)) {
4485         bool fully_masked = odp_mask_is_exact(attr, mask, size);
4486
4487         if (use_masked_set && !fully_masked) {
4488             commit_masked_set_action(odp_actions, attr, key, mask, size);
4489         } else {
4490             if (!fully_masked) {
4491                 memset(mask, 0xff, size);
4492             }
4493             commit_set_action(odp_actions, attr, key, size);
4494         }
4495         memcpy(base, key, size);
4496         return true;
4497     } else {
4498         /* Mask bits are set when we have either read or set the corresponding
4499          * values.  Masked bits will be exact-matched, no need to set them
4500          * if the value did not actually change. */
4501         return false;
4502     }
4503 }
4504
4505 static void
4506 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4507 {
4508     memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4509     memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4510 }
4511
4512 static void
4513 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4514 {
4515     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4516     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4517 }
4518
4519 static void
4520 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4521                              struct ofpbuf *odp_actions,
4522                              struct flow_wildcards *wc,
4523                              bool use_masked)
4524 {
4525     struct ovs_key_ethernet key, base, mask;
4526
4527     get_ethernet_key(flow, &key);
4528     get_ethernet_key(base_flow, &base);
4529     get_ethernet_key(&wc->masks, &mask);
4530
4531     if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4532                &key, &base, &mask, sizeof key, odp_actions)) {
4533         put_ethernet_key(&base, base_flow);
4534         put_ethernet_key(&mask, &wc->masks);
4535     }
4536 }
4537
4538 static void
4539 pop_vlan(struct flow *base,
4540          struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4541 {
4542     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4543
4544     if (base->vlan_tci & htons(VLAN_CFI)) {
4545         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4546         base->vlan_tci = 0;
4547     }
4548 }
4549
4550 static void
4551 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4552                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4553 {
4554     if (base->vlan_tci == vlan_tci) {
4555         return;
4556     }
4557
4558     pop_vlan(base, odp_actions, wc);
4559     if (vlan_tci & htons(VLAN_CFI)) {
4560         struct ovs_action_push_vlan vlan;
4561
4562         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4563         vlan.vlan_tci = vlan_tci;
4564         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4565                           &vlan, sizeof vlan);
4566     }
4567     base->vlan_tci = vlan_tci;
4568 }
4569
4570 /* Wildcarding already done at action translation time. */
4571 static void
4572 commit_mpls_action(const struct flow *flow, struct flow *base,
4573                    struct ofpbuf *odp_actions)
4574 {
4575     int base_n = flow_count_mpls_labels(base, NULL);
4576     int flow_n = flow_count_mpls_labels(flow, NULL);
4577     int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4578                                                  NULL);
4579
4580     while (base_n > common_n) {
4581         if (base_n - 1 == common_n && flow_n > common_n) {
4582             /* If there is only one more LSE in base than there are common
4583              * between base and flow; and flow has at least one more LSE than
4584              * is common then the topmost LSE of base may be updated using
4585              * set */
4586             struct ovs_key_mpls mpls_key;
4587
4588             mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4589             commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4590                               &mpls_key, sizeof mpls_key);
4591             flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4592             common_n++;
4593         } else {
4594             /* Otherwise, if there more LSEs in base than are common between
4595              * base and flow then pop the topmost one. */
4596             ovs_be16 dl_type;
4597             bool popped;
4598
4599             /* If all the LSEs are to be popped and this is not the outermost
4600              * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4601              * POP_MPLS action instead of flow->dl_type.
4602              *
4603              * This is because the POP_MPLS action requires its ethertype
4604              * argument to be an MPLS ethernet type but in this case
4605              * flow->dl_type will be a non-MPLS ethernet type.
4606              *
4607              * When the final POP_MPLS action occurs it use flow->dl_type and
4608              * the and the resulting packet will have the desired dl_type. */
4609             if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4610                 dl_type = htons(ETH_TYPE_MPLS);
4611             } else {
4612                 dl_type = flow->dl_type;
4613             }
4614             nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4615             popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4616             ovs_assert(popped);
4617             base_n--;
4618         }
4619     }
4620
4621     /* If, after the above popping and setting, there are more LSEs in flow
4622      * than base then some LSEs need to be pushed. */
4623     while (base_n < flow_n) {
4624         struct ovs_action_push_mpls *mpls;
4625
4626         mpls = nl_msg_put_unspec_zero(odp_actions,
4627                                       OVS_ACTION_ATTR_PUSH_MPLS,
4628                                       sizeof *mpls);
4629         mpls->mpls_ethertype = flow->dl_type;
4630         mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4631         flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4632         flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4633         base_n++;
4634     }
4635 }
4636
4637 static void
4638 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4639 {
4640     ipv4->ipv4_src = flow->nw_src;
4641     ipv4->ipv4_dst = flow->nw_dst;
4642     ipv4->ipv4_proto = flow->nw_proto;
4643     ipv4->ipv4_tos = flow->nw_tos;
4644     ipv4->ipv4_ttl = flow->nw_ttl;
4645     ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4646 }
4647
4648 static void
4649 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4650 {
4651     flow->nw_src = ipv4->ipv4_src;
4652     flow->nw_dst = ipv4->ipv4_dst;
4653     flow->nw_proto = ipv4->ipv4_proto;
4654     flow->nw_tos = ipv4->ipv4_tos;
4655     flow->nw_ttl = ipv4->ipv4_ttl;
4656     flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4657 }
4658
4659 static void
4660 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4661                        struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4662                        bool use_masked)
4663 {
4664     struct ovs_key_ipv4 key, mask, base;
4665
4666     /* Check that nw_proto and nw_frag remain unchanged. */
4667     ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4668                flow->nw_frag == base_flow->nw_frag);
4669
4670     get_ipv4_key(flow, &key, false);
4671     get_ipv4_key(base_flow, &base, false);
4672     get_ipv4_key(&wc->masks, &mask, true);
4673     mask.ipv4_proto = 0;        /* Not writeable. */
4674     mask.ipv4_frag = 0;         /* Not writable. */
4675
4676     if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4677                odp_actions)) {
4678         put_ipv4_key(&base, base_flow, false);
4679         if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4680             put_ipv4_key(&mask, &wc->masks, true);
4681         }
4682    }
4683 }
4684
4685 static void
4686 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4687 {
4688     memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4689     memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4690     ipv6->ipv6_label = flow->ipv6_label;
4691     ipv6->ipv6_proto = flow->nw_proto;
4692     ipv6->ipv6_tclass = flow->nw_tos;
4693     ipv6->ipv6_hlimit = flow->nw_ttl;
4694     ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4695 }
4696
4697 static void
4698 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4699 {
4700     memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4701     memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4702     flow->ipv6_label = ipv6->ipv6_label;
4703     flow->nw_proto = ipv6->ipv6_proto;
4704     flow->nw_tos = ipv6->ipv6_tclass;
4705     flow->nw_ttl = ipv6->ipv6_hlimit;
4706     flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4707 }
4708
4709 static void
4710 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4711                        struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4712                        bool use_masked)
4713 {
4714     struct ovs_key_ipv6 key, mask, base;
4715
4716     /* Check that nw_proto and nw_frag remain unchanged. */
4717     ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4718                flow->nw_frag == base_flow->nw_frag);
4719
4720     get_ipv6_key(flow, &key, false);
4721     get_ipv6_key(base_flow, &base, false);
4722     get_ipv6_key(&wc->masks, &mask, true);
4723     mask.ipv6_proto = 0;        /* Not writeable. */
4724     mask.ipv6_frag = 0;         /* Not writable. */
4725
4726     if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4727                odp_actions)) {
4728         put_ipv6_key(&base, base_flow, false);
4729         if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4730             put_ipv6_key(&mask, &wc->masks, true);
4731         }
4732     }
4733 }
4734
4735 static void
4736 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4737 {
4738     /* ARP key has padding, clear it. */
4739     memset(arp, 0, sizeof *arp);
4740
4741     arp->arp_sip = flow->nw_src;
4742     arp->arp_tip = flow->nw_dst;
4743     arp->arp_op = htons(flow->nw_proto);
4744     memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4745     memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4746 }
4747
4748 static void
4749 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4750 {
4751     flow->nw_src = arp->arp_sip;
4752     flow->nw_dst = arp->arp_tip;
4753     flow->nw_proto = ntohs(arp->arp_op);
4754     memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4755     memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4756 }
4757
4758 static enum slow_path_reason
4759 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4760                       struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4761 {
4762     struct ovs_key_arp key, mask, base;
4763
4764     get_arp_key(flow, &key);
4765     get_arp_key(base_flow, &base);
4766     get_arp_key(&wc->masks, &mask);
4767
4768     if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4769                odp_actions)) {
4770         put_arp_key(&base, base_flow);
4771         put_arp_key(&mask, &wc->masks);
4772         return SLOW_ACTION;
4773     }
4774     return 0;
4775 }
4776
4777 static void
4778 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4779 {
4780     memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4781     /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4782     memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4783     memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4784 }
4785
4786 static void
4787 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4788 {
4789     memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4790     /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4791     memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4792     memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4793 }
4794
4795 static enum slow_path_reason
4796 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4797                      struct ofpbuf *odp_actions,
4798                      struct flow_wildcards *wc, bool use_masked)
4799 {
4800     struct ovs_key_nd key, mask, base;
4801
4802     get_nd_key(flow, &key);
4803     get_nd_key(base_flow, &base);
4804     get_nd_key(&wc->masks, &mask);
4805
4806     if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4807                odp_actions)) {
4808         put_nd_key(&base, base_flow);
4809         put_nd_key(&mask, &wc->masks);
4810         return SLOW_ACTION;
4811     }
4812
4813     return 0;
4814 }
4815
4816 static enum slow_path_reason
4817 commit_set_nw_action(const struct flow *flow, struct flow *base,
4818                      struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4819                      bool use_masked)
4820 {
4821     /* Check if 'flow' really has an L3 header. */
4822     if (!flow->nw_proto) {
4823         return 0;
4824     }
4825
4826     switch (ntohs(base->dl_type)) {
4827     case ETH_TYPE_IP:
4828         commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4829         break;
4830
4831     case ETH_TYPE_IPV6:
4832         commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4833         return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4834
4835     case ETH_TYPE_ARP:
4836         return commit_set_arp_action(flow, base, odp_actions, wc);
4837     }
4838
4839     return 0;
4840 }
4841
4842 /* TCP, UDP, and SCTP keys have the same layout. */
4843 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4844                   sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4845
4846 static void
4847 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4848 {
4849     tp->tcp.tcp_src = flow->tp_src;
4850     tp->tcp.tcp_dst = flow->tp_dst;
4851 }
4852
4853 static void
4854 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4855 {
4856     flow->tp_src = tp->tcp.tcp_src;
4857     flow->tp_dst = tp->tcp.tcp_dst;
4858 }
4859
4860 static void
4861 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4862                        struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4863                        bool use_masked)
4864 {
4865     enum ovs_key_attr key_type;
4866     union ovs_key_tp key, mask, base;
4867
4868     /* Check if 'flow' really has an L3 header. */
4869     if (!flow->nw_proto) {
4870         return;
4871     }
4872
4873     if (!is_ip_any(base_flow)) {
4874         return;
4875     }
4876
4877     if (flow->nw_proto == IPPROTO_TCP) {
4878         key_type = OVS_KEY_ATTR_TCP;
4879     } else if (flow->nw_proto == IPPROTO_UDP) {
4880         key_type = OVS_KEY_ATTR_UDP;
4881     } else if (flow->nw_proto == IPPROTO_SCTP) {
4882         key_type = OVS_KEY_ATTR_SCTP;
4883     } else {
4884         return;
4885     }
4886
4887     get_tp_key(flow, &key);
4888     get_tp_key(base_flow, &base);
4889     get_tp_key(&wc->masks, &mask);
4890
4891     if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4892                odp_actions)) {
4893         put_tp_key(&base, base_flow);
4894         put_tp_key(&mask, &wc->masks);
4895     }
4896 }
4897
4898 static void
4899 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4900                            struct ofpbuf *odp_actions,
4901                            struct flow_wildcards *wc,
4902                            bool use_masked)
4903 {
4904     uint32_t key, mask, base;
4905
4906     key = flow->skb_priority;
4907     base = base_flow->skb_priority;
4908     mask = wc->masks.skb_priority;
4909
4910     if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4911                sizeof key, odp_actions)) {
4912         base_flow->skb_priority = base;
4913         wc->masks.skb_priority = mask;
4914     }
4915 }
4916
4917 static void
4918 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4919                            struct ofpbuf *odp_actions,
4920                            struct flow_wildcards *wc,
4921                            bool use_masked)
4922 {
4923     uint32_t key, mask, base;
4924
4925     key = flow->pkt_mark;
4926     base = base_flow->pkt_mark;
4927     mask = wc->masks.pkt_mark;
4928
4929     if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4930                sizeof key, odp_actions)) {
4931         base_flow->pkt_mark = base;
4932         wc->masks.pkt_mark = mask;
4933     }
4934 }
4935
4936 /* If any of the flow key data that ODP actions can modify are different in
4937  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4938  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
4939  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
4940  * in addition to this function if needed.  Sets fields in 'wc' that are
4941  * used as part of the action.
4942  *
4943  * Returns a reason to force processing the flow's packets into the userspace
4944  * slow path, if there is one, otherwise 0. */
4945 enum slow_path_reason
4946 commit_odp_actions(const struct flow *flow, struct flow *base,
4947                    struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4948                    bool use_masked)
4949 {
4950     enum slow_path_reason slow;
4951
4952     commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4953     slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4954     commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4955     commit_mpls_action(flow, base, odp_actions);
4956     commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4957     commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4958     commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
4959
4960     return slow;
4961 }