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