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