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