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