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