lib/odp-util: Skip ignored fields when parsing and formatting.
[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     if (!strncmp(s, "userspace(", 10)) {
769         return parse_odp_userspace_action(s, actions);
770     }
771
772     if (!strncmp(s, "set(", 4)) {
773         size_t start_ofs;
774         int retval;
775         struct nlattr mask[128 / sizeof(struct nlattr)];
776         struct ofpbuf maskbuf;
777         struct nlattr *nested, *key;
778         size_t size;
779
780         /* 'mask' is big enough to hold any key. */
781         ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
782
783         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
784         retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
785         if (retval < 0) {
786             return retval;
787         }
788         if (s[retval + 4] != ')') {
789             return -EINVAL;
790         }
791
792         nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
793         key = nested + 1;
794
795         size = nl_attr_get_size(mask);
796         if (size == nl_attr_get_size(key)) {
797             /* Change to masked set action if not fully masked. */
798             if (!is_all_ones(mask + 1, size)) {
799                 key->nla_len += size;
800                 ofpbuf_put(actions, mask + 1, size);
801                 /* 'actions' may have been reallocated by ofpbuf_put(). */
802                 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
803                 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
804             }
805         }
806
807         nl_msg_end_nested(actions, start_ofs);
808         return retval + 5;
809     }
810
811     {
812         struct ovs_action_push_vlan push;
813         int tpid = ETH_TYPE_VLAN;
814         int vid, pcp;
815         int cfi = 1;
816         int n = -1;
817
818         if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
819             || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
820                         &vid, &pcp, &cfi, &n)
821             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
822                         &tpid, &vid, &pcp, &n)
823             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
824                         &tpid, &vid, &pcp, &cfi, &n)) {
825             push.vlan_tpid = htons(tpid);
826             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
827                                   | (pcp << VLAN_PCP_SHIFT)
828                                   | (cfi ? VLAN_CFI : 0));
829             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
830                               &push, sizeof push);
831
832             return n;
833         }
834     }
835
836     if (!strncmp(s, "pop_vlan", 8)) {
837         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
838         return 8;
839     }
840
841     {
842         double percentage;
843         int n = -1;
844
845         if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
846             && percentage >= 0. && percentage <= 100.0) {
847             size_t sample_ofs, actions_ofs;
848             double probability;
849
850             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
851             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
852             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
853                            (probability <= 0 ? 0
854                             : probability >= UINT32_MAX ? UINT32_MAX
855                             : probability));
856
857             actions_ofs = nl_msg_start_nested(actions,
858                                               OVS_SAMPLE_ATTR_ACTIONS);
859             for (;;) {
860                 int retval;
861
862                 n += strspn(s + n, delimiters);
863                 if (s[n] == ')') {
864                     break;
865                 }
866
867                 retval = parse_odp_action(s + n, port_names, actions);
868                 if (retval < 0) {
869                     return retval;
870                 }
871                 n += retval;
872             }
873             nl_msg_end_nested(actions, actions_ofs);
874             nl_msg_end_nested(actions, sample_ofs);
875
876             return s[n + 1] == ')' ? n + 2 : -EINVAL;
877         }
878     }
879
880     return -EINVAL;
881 }
882
883 /* Parses the string representation of datapath actions, in the format output
884  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
885  * value.  On success, the ODP actions are appended to 'actions' as a series of
886  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
887  * way, 'actions''s data might be reallocated. */
888 int
889 odp_actions_from_string(const char *s, const struct simap *port_names,
890                         struct ofpbuf *actions)
891 {
892     size_t old_size;
893
894     if (!strcasecmp(s, "drop")) {
895         return 0;
896     }
897
898     old_size = ofpbuf_size(actions);
899     for (;;) {
900         int retval;
901
902         s += strspn(s, delimiters);
903         if (!*s) {
904             return 0;
905         }
906
907         retval = parse_odp_action(s, port_names, actions);
908         if (retval < 0 || !strchr(delimiters, s[retval])) {
909             ofpbuf_set_size(actions, old_size);
910             return -retval;
911         }
912         s += retval;
913     }
914
915     return 0;
916 }
917 \f
918 /* Returns the correct length of the payload for a flow key attribute of the
919  * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
920  * is variable length. */
921 static int
922 odp_flow_key_attr_len(uint16_t type)
923 {
924     if (type > OVS_KEY_ATTR_MAX) {
925         return -1;
926     }
927
928     switch ((enum ovs_key_attr) type) {
929     case OVS_KEY_ATTR_ENCAP: return -2;
930     case OVS_KEY_ATTR_PRIORITY: return 4;
931     case OVS_KEY_ATTR_SKB_MARK: return 4;
932     case OVS_KEY_ATTR_DP_HASH: return 4;
933     case OVS_KEY_ATTR_RECIRC_ID: return 4;
934     case OVS_KEY_ATTR_TUNNEL: return -2;
935     case OVS_KEY_ATTR_IN_PORT: return 4;
936     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
937     case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
938     case OVS_KEY_ATTR_ETHERTYPE: return 2;
939     case OVS_KEY_ATTR_MPLS: return -2;
940     case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
941     case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
942     case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
943     case OVS_KEY_ATTR_TCP_FLAGS: return 2;
944     case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
945     case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
946     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
947     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
948     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
949     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
950
951     case OVS_KEY_ATTR_UNSPEC:
952     case __OVS_KEY_ATTR_MAX:
953         return -1;
954     }
955
956     return -1;
957 }
958
959 static void
960 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
961 {
962     size_t len = nl_attr_get_size(a);
963     if (len) {
964         const uint8_t *unspec;
965         unsigned int i;
966
967         unspec = nl_attr_get(a);
968         for (i = 0; i < len; i++) {
969             if (i) {
970                 ds_put_char(ds, ' ');
971             }
972             ds_put_format(ds, "%02x", unspec[i]);
973         }
974     }
975 }
976
977 static const char *
978 ovs_frag_type_to_string(enum ovs_frag_type type)
979 {
980     switch (type) {
981     case OVS_FRAG_TYPE_NONE:
982         return "no";
983     case OVS_FRAG_TYPE_FIRST:
984         return "first";
985     case OVS_FRAG_TYPE_LATER:
986         return "later";
987     case __OVS_FRAG_TYPE_MAX:
988     default:
989         return "<error>";
990     }
991 }
992
993 static int
994 tunnel_key_attr_len(int type)
995 {
996     switch (type) {
997     case OVS_TUNNEL_KEY_ATTR_ID: return 8;
998     case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
999     case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1000     case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1001     case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1002     case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1003     case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1004     case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1005     case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1006     case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1007     case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1008     case __OVS_TUNNEL_KEY_ATTR_MAX:
1009         return -1;
1010     }
1011     return -1;
1012 }
1013
1014 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1015 static int
1016 parse_geneve_opts(const struct nlattr *attr)
1017 {
1018     int opts_len = nl_attr_get_size(attr);
1019     const struct geneve_opt *opt = nl_attr_get(attr);
1020
1021     while (opts_len > 0) {
1022         int len;
1023
1024         if (opts_len < sizeof(*opt)) {
1025             return -EINVAL;
1026         }
1027
1028         len = sizeof(*opt) + opt->length * 4;
1029         if (len > opts_len) {
1030             return -EINVAL;
1031         }
1032
1033         switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1034         default:
1035             if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1036                 return -EINVAL;
1037             }
1038         };
1039
1040         opt = opt + len / sizeof(*opt);
1041         opts_len -= len;
1042     };
1043
1044     return 0;
1045 }
1046
1047 enum odp_key_fitness
1048 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1049 {
1050     unsigned int left;
1051     const struct nlattr *a;
1052     bool ttl = false;
1053     bool unknown = false;
1054
1055     NL_NESTED_FOR_EACH(a, left, attr) {
1056         uint16_t type = nl_attr_type(a);
1057         size_t len = nl_attr_get_size(a);
1058         int expected_len = tunnel_key_attr_len(type);
1059
1060         if (len != expected_len && expected_len >= 0) {
1061             return ODP_FIT_ERROR;
1062         }
1063
1064         switch (type) {
1065         case OVS_TUNNEL_KEY_ATTR_ID:
1066             tun->tun_id = nl_attr_get_be64(a);
1067             tun->flags |= FLOW_TNL_F_KEY;
1068             break;
1069         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1070             tun->ip_src = nl_attr_get_be32(a);
1071             break;
1072         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1073             tun->ip_dst = nl_attr_get_be32(a);
1074             break;
1075         case OVS_TUNNEL_KEY_ATTR_TOS:
1076             tun->ip_tos = nl_attr_get_u8(a);
1077             break;
1078         case OVS_TUNNEL_KEY_ATTR_TTL:
1079             tun->ip_ttl = nl_attr_get_u8(a);
1080             ttl = true;
1081             break;
1082         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1083             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1084             break;
1085         case OVS_TUNNEL_KEY_ATTR_CSUM:
1086             tun->flags |= FLOW_TNL_F_CSUM;
1087             break;
1088         case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1089             tun->tp_src = nl_attr_get_be16(a);
1090             break;
1091         case OVS_TUNNEL_KEY_ATTR_TP_DST:
1092             tun->tp_dst = nl_attr_get_be16(a);
1093             break;
1094         case OVS_TUNNEL_KEY_ATTR_OAM:
1095             tun->flags |= FLOW_TNL_F_OAM;
1096             break;
1097         case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1098             if (parse_geneve_opts(a)) {
1099                 return ODP_FIT_ERROR;
1100             }
1101             /* It is necessary to reproduce options exactly (including order)
1102              * so it's easiest to just echo them back. */
1103             unknown = true;
1104             break;
1105         }
1106         default:
1107             /* Allow this to show up as unexpected, if there are unknown
1108              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1109             unknown = true;
1110             break;
1111         }
1112     }
1113
1114     if (!ttl) {
1115         return ODP_FIT_ERROR;
1116     }
1117     if (unknown) {
1118         return ODP_FIT_TOO_MUCH;
1119     }
1120     return ODP_FIT_PERFECT;
1121 }
1122
1123 static void
1124 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1125 {
1126     size_t tun_key_ofs;
1127
1128     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1129
1130     /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1131     if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1132         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1133     }
1134     if (tun_key->ip_src) {
1135         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1136     }
1137     if (tun_key->ip_dst) {
1138         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1139     }
1140     if (tun_key->ip_tos) {
1141         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1142     }
1143     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1144     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1145         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1146     }
1147     if (tun_key->flags & FLOW_TNL_F_CSUM) {
1148         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1149     }
1150     if (tun_key->tp_src) {
1151         nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1152     }
1153     if (tun_key->tp_dst) {
1154         nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1155     }
1156     if (tun_key->flags & FLOW_TNL_F_OAM) {
1157         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1158     }
1159
1160     nl_msg_end_nested(a, tun_key_ofs);
1161 }
1162
1163 static bool
1164 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1165 {
1166     return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1167 }
1168
1169 static bool
1170 odp_mask_attr_is_exact(const struct nlattr *ma)
1171 {
1172     bool is_exact;
1173     enum ovs_key_attr attr = nl_attr_type(ma);
1174
1175     if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1176         is_exact = TCP_FLAGS(nl_attr_get_be16(ma)) == TCP_FLAGS(OVS_BE16_MAX);
1177     } else if (attr == OVS_KEY_ATTR_IPV6) {
1178         const struct ovs_key_ipv6 *mask = nl_attr_get(ma);
1179
1180         is_exact =
1181             ((mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1182              == htonl(IPV6_LABEL_MASK))
1183             && mask->ipv6_proto == UINT8_MAX
1184             && mask->ipv6_tclass == UINT8_MAX
1185             && mask->ipv6_hlimit == UINT8_MAX
1186             && mask->ipv6_frag == UINT8_MAX
1187             && ipv6_mask_is_exact((const struct in6_addr *)mask->ipv6_src)
1188             && ipv6_mask_is_exact((const struct in6_addr *)mask->ipv6_dst);
1189     } else if (attr == OVS_KEY_ATTR_TUNNEL) {
1190         struct flow_tnl tun_mask;
1191
1192         memset(&tun_mask, 0, sizeof tun_mask);
1193         odp_tun_key_from_attr(ma, &tun_mask);
1194         is_exact = tun_mask.flags == FLOW_TNL_F_MASK
1195             && tun_mask.tun_id == OVS_BE64_MAX
1196             && tun_mask.ip_src == OVS_BE32_MAX
1197             && tun_mask.ip_dst == OVS_BE32_MAX
1198             && tun_mask.ip_tos == UINT8_MAX
1199             && tun_mask.ip_ttl == UINT8_MAX
1200             && tun_mask.tp_src == OVS_BE16_MAX
1201             && tun_mask.tp_dst == OVS_BE16_MAX;
1202     } else {
1203         is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
1204     }
1205
1206     return is_exact;
1207 }
1208
1209 void
1210 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1211                      char *port_name)
1212 {
1213     struct odp_portno_names *odp_portno_names;
1214
1215     odp_portno_names = xmalloc(sizeof *odp_portno_names);
1216     odp_portno_names->port_no = port_no;
1217     odp_portno_names->name = xstrdup(port_name);
1218     hmap_insert(portno_names, &odp_portno_names->hmap_node,
1219                 hash_odp_port(port_no));
1220 }
1221
1222 static char *
1223 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1224 {
1225     struct odp_portno_names *odp_portno_names;
1226
1227     HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1228                              hash_odp_port(port_no), portno_names) {
1229         if (odp_portno_names->port_no == port_no) {
1230             return odp_portno_names->name;
1231         }
1232     }
1233     return NULL;
1234 }
1235
1236 void
1237 odp_portno_names_destroy(struct hmap *portno_names)
1238 {
1239     struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1240     HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1241                         hmap_node, portno_names) {
1242         hmap_remove(portno_names, &odp_portno_names->hmap_node);
1243         free(odp_portno_names->name);
1244         free(odp_portno_names);
1245     }
1246 }
1247
1248 /* Format helpers. */
1249
1250 static void
1251 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1252            const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1253 {
1254     bool mask_empty = mask && eth_addr_is_zero(*mask);
1255
1256     if (verbose || !mask_empty) {
1257         bool mask_full = !mask || eth_mask_is_exact(*mask);
1258
1259         if (mask_full) {
1260             ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1261         } else {
1262             ds_put_format(ds, "%s=", name);
1263             eth_format_masked(key, *mask, ds);
1264             ds_put_char(ds, ',');
1265         }
1266     }
1267 }
1268
1269 static void
1270 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1271             const ovs_be64 *mask, bool verbose)
1272 {
1273     bool mask_empty = mask && !*mask;
1274
1275     if (verbose || !mask_empty) {
1276         bool mask_full = !mask || *mask == OVS_BE64_MAX;
1277
1278         ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1279         if (!mask_full) { /* Partially masked. */
1280             ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1281         }
1282         ds_put_char(ds, ',');
1283     }
1284 }
1285
1286 static void
1287 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1288             const ovs_be32 *mask, bool verbose)
1289 {
1290     bool mask_empty = mask && !*mask;
1291
1292     if (verbose || !mask_empty) {
1293         bool mask_full = !mask || *mask == OVS_BE32_MAX;
1294
1295         ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1296         if (!mask_full) { /* Partially masked. */
1297             ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1298         }
1299         ds_put_char(ds, ',');
1300     }
1301 }
1302
1303 static void
1304 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1305             const ovs_be32 (*mask_)[4], bool verbose)
1306 {
1307     char buf[INET6_ADDRSTRLEN];
1308     const struct in6_addr *key = (const struct in6_addr *)key_;
1309     const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1310         : NULL;
1311     bool mask_empty = mask && ipv6_mask_is_any(mask);
1312
1313     if (verbose || !mask_empty) {
1314         bool mask_full = !mask || ipv6_mask_is_exact(mask);
1315
1316         inet_ntop(AF_INET6, key, buf, sizeof buf);
1317         ds_put_format(ds, "%s=%s", name, buf);
1318         if (!mask_full) { /* Partially masked. */
1319             inet_ntop(AF_INET6, mask, buf, sizeof buf);
1320             ds_put_format(ds, "/%s", buf);
1321         }
1322         ds_put_char(ds, ',');
1323     }
1324 }
1325
1326 static void
1327 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1328                   const ovs_be32 *mask, bool verbose)
1329 {
1330     bool mask_empty = mask && !*mask;
1331
1332     if (verbose || !mask_empty) {
1333         bool mask_full = !mask
1334             || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1335
1336         ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1337         if (!mask_full) { /* Partially masked. */
1338             ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1339         }
1340         ds_put_char(ds, ',');
1341     }
1342 }
1343
1344 static void
1345 format_u8x(struct ds *ds, const char *name, uint8_t key,
1346            const uint8_t *mask, bool verbose)
1347 {
1348     bool mask_empty = mask && !*mask;
1349
1350     if (verbose || !mask_empty) {
1351         bool mask_full = !mask || *mask == UINT8_MAX;
1352
1353         ds_put_format(ds, "%s=%#"PRIx8, name, key);
1354         if (!mask_full) { /* Partially masked. */
1355             ds_put_format(ds, "/%#"PRIx8, *mask);
1356         }
1357         ds_put_char(ds, ',');
1358     }
1359 }
1360
1361 static void
1362 format_u8u(struct ds *ds, const char *name, uint8_t key,
1363            const uint8_t *mask, bool verbose)
1364 {
1365     bool mask_empty = mask && !*mask;
1366
1367     if (verbose || !mask_empty) {
1368         bool mask_full = !mask || *mask == UINT8_MAX;
1369
1370         ds_put_format(ds, "%s=%"PRIu8, name, key);
1371         if (!mask_full) { /* Partially masked. */
1372             ds_put_format(ds, "/%#"PRIx8, *mask);
1373         }
1374         ds_put_char(ds, ',');
1375     }
1376 }
1377
1378 static void
1379 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1380             const ovs_be16 *mask, bool verbose)
1381 {
1382     bool mask_empty = mask && !*mask;
1383
1384     if (verbose || !mask_empty) {
1385         bool mask_full = !mask || *mask == OVS_BE16_MAX;
1386
1387         ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1388         if (!mask_full) { /* Partially masked. */
1389             ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1390         }
1391         ds_put_char(ds, ',');
1392     }
1393 }
1394
1395 static void
1396 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1397                  const uint16_t *mask, bool verbose)
1398 {
1399     bool mask_empty = mask && !*mask;
1400
1401     if (verbose || !mask_empty) {
1402         bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1403
1404         ds_put_cstr(ds, name);
1405         ds_put_char(ds, '(');
1406         if (!mask_full) { /* Partially masked. */
1407             format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1408         } else { /* Fully masked. */
1409             format_flags(ds, flow_tun_flag_to_string, key, ',');
1410         }
1411         ds_put_cstr(ds, "),");
1412     }
1413 }
1414
1415 static void
1416 format_frag(struct ds *ds, const char *name, uint8_t key,
1417             const uint8_t *mask, bool verbose)
1418 {
1419     bool mask_empty = mask && !*mask;
1420
1421     /* ODP frag is an enumeration field; partial masks are not meaningful. */
1422     if (verbose || !mask_empty) {
1423         bool mask_full = !mask || *mask == UINT8_MAX;
1424
1425         if (!mask_full) { /* Partially masked. */
1426             ds_put_format(ds, "error: partial mask not supported for frag (%#"
1427                           PRIx8"),", *mask);
1428         } else {
1429             ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1430         }
1431     }
1432 }
1433
1434 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1435
1436 static void
1437 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1438                     const struct hmap *portno_names, struct ds *ds,
1439                     bool verbose)
1440 {
1441     enum ovs_key_attr attr = nl_attr_type(a);
1442     char namebuf[OVS_KEY_ATTR_BUFSIZE];
1443     int expected_len;
1444     bool is_exact;
1445
1446     is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1447
1448     ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1449
1450     {
1451         expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1452         if (expected_len != -2) {
1453             bool bad_key_len = nl_attr_get_size(a) != expected_len;
1454             bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1455
1456             if (bad_key_len || bad_mask_len) {
1457                 if (bad_key_len) {
1458                     ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1459                                   nl_attr_get_size(a), expected_len);
1460                 }
1461                 format_generic_odp_key(a, ds);
1462                 if (ma) {
1463                     ds_put_char(ds, '/');
1464                     if (bad_mask_len) {
1465                         ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1466                                       nl_attr_get_size(ma), expected_len);
1467                     }
1468                     format_generic_odp_key(ma, ds);
1469                 }
1470                 ds_put_char(ds, ')');
1471                 return;
1472             }
1473         }
1474     }
1475
1476     ds_put_char(ds, '(');
1477     switch (attr) {
1478     case OVS_KEY_ATTR_ENCAP:
1479         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1480             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1481                             nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1482                             verbose);
1483         } else if (nl_attr_get_size(a)) {
1484             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1485                             ds, verbose);
1486         }
1487         break;
1488
1489     case OVS_KEY_ATTR_PRIORITY:
1490     case OVS_KEY_ATTR_SKB_MARK:
1491     case OVS_KEY_ATTR_DP_HASH:
1492     case OVS_KEY_ATTR_RECIRC_ID:
1493         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1494         if (!is_exact) {
1495             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1496         }
1497         break;
1498
1499     case OVS_KEY_ATTR_TUNNEL: {
1500         struct flow_tnl key, mask_;
1501         struct flow_tnl *mask = ma ? &mask_ : NULL;
1502
1503         if (mask) {
1504             memset(mask, 0, sizeof *mask);
1505             odp_tun_key_from_attr(ma, mask);
1506         }
1507         memset(&key, 0, sizeof key);
1508         if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1509             ds_put_format(ds, "error");
1510             return;
1511         }
1512         format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1513         format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1514         format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1515         format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1516         format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1517         format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1518         format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1519         format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1520         ds_chomp(ds, ',');
1521         break;
1522     }
1523     case OVS_KEY_ATTR_IN_PORT:
1524         if (portno_names && verbose && is_exact) {
1525             char *name = odp_portno_names_get(portno_names,
1526                             u32_to_odp(nl_attr_get_u32(a)));
1527             if (name) {
1528                 ds_put_format(ds, "%s", name);
1529             } else {
1530                 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1531             }
1532         } else {
1533             ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1534             if (!is_exact) {
1535                 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1536             }
1537         }
1538         break;
1539
1540     case OVS_KEY_ATTR_ETHERNET: {
1541         const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1542         const struct ovs_key_ethernet *key = nl_attr_get(a);
1543
1544         format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1545         format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1546         ds_chomp(ds, ',');
1547         break;
1548     }
1549     case OVS_KEY_ATTR_VLAN:
1550         format_vlan_tci(ds, nl_attr_get_be16(a),
1551                         ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1552         break;
1553
1554     case OVS_KEY_ATTR_MPLS: {
1555         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1556         const struct ovs_key_mpls *mpls_mask = NULL;
1557         size_t size = nl_attr_get_size(a);
1558
1559         if (!size || size % sizeof *mpls_key) {
1560             ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1561             return;
1562         }
1563         if (!is_exact) {
1564             mpls_mask = nl_attr_get(ma);
1565             if (size != nl_attr_get_size(ma)) {
1566                 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1567                               "mask length %"PRIuSIZE")",
1568                               size, nl_attr_get_size(ma));
1569                 return;
1570             }
1571         }
1572         format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1573         break;
1574     }
1575     case OVS_KEY_ATTR_ETHERTYPE:
1576         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1577         if (!is_exact) {
1578             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1579         }
1580         break;
1581
1582     case OVS_KEY_ATTR_IPV4: {
1583         const struct ovs_key_ipv4 *key = nl_attr_get(a);
1584         const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1585
1586         format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1587         format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1588         format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1589                       verbose);
1590         format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1591         format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1592         format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1593                     verbose);
1594         ds_chomp(ds, ',');
1595         break;
1596     }
1597     case OVS_KEY_ATTR_IPV6: {
1598         const struct ovs_key_ipv6 *key = nl_attr_get(a);
1599         const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1600
1601         format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1602         format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1603         format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1604                           verbose);
1605         format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1606                       verbose);
1607         format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1608                       verbose);
1609         format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1610                       verbose);
1611         format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1612                     verbose);
1613         ds_chomp(ds, ',');
1614         break;
1615     }
1616         /* These have the same structure and format. */
1617     case OVS_KEY_ATTR_TCP:
1618     case OVS_KEY_ATTR_UDP:
1619     case OVS_KEY_ATTR_SCTP: {
1620         const struct ovs_key_tcp *key = nl_attr_get(a);
1621         const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1622
1623         format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1624         format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1625         ds_chomp(ds, ',');
1626         break;
1627     }
1628     case OVS_KEY_ATTR_TCP_FLAGS:
1629         if (!is_exact) {
1630             format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1631                                 ntohs(nl_attr_get_be16(a)),
1632                                 ntohs(nl_attr_get_be16(ma)));
1633         } else {
1634             format_flags(ds, packet_tcp_flag_to_string,
1635                          ntohs(nl_attr_get_be16(a)), ',');
1636         }
1637         break;
1638
1639     case OVS_KEY_ATTR_ICMP: {
1640         const struct ovs_key_icmp *key = nl_attr_get(a);
1641         const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1642
1643         format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1644         format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1645         ds_chomp(ds, ',');
1646         break;
1647     }
1648     case OVS_KEY_ATTR_ICMPV6: {
1649         const struct ovs_key_icmpv6 *key = nl_attr_get(a);
1650         const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
1651
1652         format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
1653                    verbose);
1654         format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
1655                    verbose);
1656         ds_chomp(ds, ',');
1657         break;
1658     }
1659     case OVS_KEY_ATTR_ARP: {
1660         const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
1661         const struct ovs_key_arp *key = nl_attr_get(a);
1662
1663         format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
1664         format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
1665         format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
1666         format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
1667         format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
1668         ds_chomp(ds, ',');
1669         break;
1670     }
1671     case OVS_KEY_ATTR_ND: {
1672         const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
1673         const struct ovs_key_nd *key = nl_attr_get(a);
1674
1675         format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
1676                     verbose);
1677         format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
1678         format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
1679
1680         ds_chomp(ds, ',');
1681         break;
1682     }
1683     case OVS_KEY_ATTR_UNSPEC:
1684     case __OVS_KEY_ATTR_MAX:
1685     default:
1686         format_generic_odp_key(a, ds);
1687         if (!is_exact) {
1688             ds_put_char(ds, '/');
1689             format_generic_odp_key(ma, ds);
1690         }
1691         break;
1692     }
1693     ds_put_char(ds, ')');
1694 }
1695
1696 static struct nlattr *
1697 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1698 {
1699     const struct nlattr *a;
1700     unsigned int left;
1701     int type = nl_attr_type(key);
1702     int size = nl_attr_get_size(key);
1703
1704     if (odp_flow_key_attr_len(type) >=0) {
1705         nl_msg_put_unspec_zero(ofp, type, size);
1706     } else {
1707         size_t nested_mask;
1708
1709         nested_mask = nl_msg_start_nested(ofp, type);
1710         NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1711             generate_all_wildcard_mask(ofp, nl_attr_get(a));
1712         }
1713         nl_msg_end_nested(ofp, nested_mask);
1714     }
1715
1716     return ofpbuf_base(ofp);
1717 }
1718
1719 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1720  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1721  * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
1722  * non-null and 'verbose' is true, translates odp port number to its name. */
1723 void
1724 odp_flow_format(const struct nlattr *key, size_t key_len,
1725                 const struct nlattr *mask, size_t mask_len,
1726                 const struct hmap *portno_names, struct ds *ds, bool verbose)
1727 {
1728     if (key_len) {
1729         const struct nlattr *a;
1730         unsigned int left;
1731         bool has_ethtype_key = false;
1732         const struct nlattr *ma = NULL;
1733         struct ofpbuf ofp;
1734         bool first_field = true;
1735
1736         ofpbuf_init(&ofp, 100);
1737         NL_ATTR_FOR_EACH (a, left, key, key_len) {
1738             bool is_nested_attr;
1739             bool is_wildcard = false;
1740             int attr_type = nl_attr_type(a);
1741
1742             if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1743                 has_ethtype_key = true;
1744             }
1745
1746             is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1747
1748             if (mask && mask_len) {
1749                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1750                 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1751             }
1752
1753             if (verbose || !is_wildcard  || is_nested_attr) {
1754                 if (is_wildcard && !ma) {
1755                     ma = generate_all_wildcard_mask(&ofp, a);
1756                 }
1757                 if (!first_field) {
1758                     ds_put_char(ds, ',');
1759                 }
1760                 format_odp_key_attr(a, ma, portno_names, ds, verbose);
1761                 first_field = false;
1762             }
1763             ofpbuf_clear(&ofp);
1764         }
1765         ofpbuf_uninit(&ofp);
1766
1767         if (left) {
1768             int i;
1769
1770             if (left == key_len) {
1771                 ds_put_cstr(ds, "<empty>");
1772             }
1773             ds_put_format(ds, ",***%u leftover bytes*** (", left);
1774             for (i = 0; i < left; i++) {
1775                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1776             }
1777             ds_put_char(ds, ')');
1778         }
1779         if (!has_ethtype_key) {
1780             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1781             if (ma) {
1782                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1783                               ntohs(nl_attr_get_be16(ma)));
1784             }
1785         }
1786     } else {
1787         ds_put_cstr(ds, "<empty>");
1788     }
1789 }
1790
1791 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1792  * OVS_KEY_ATTR_* attributes in 'key'. */
1793 void
1794 odp_flow_key_format(const struct nlattr *key,
1795                     size_t key_len, struct ds *ds)
1796 {
1797     odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
1798 }
1799
1800 static bool
1801 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1802 {
1803     if (!strcasecmp(s, "no")) {
1804         *type = OVS_FRAG_TYPE_NONE;
1805     } else if (!strcasecmp(s, "first")) {
1806         *type = OVS_FRAG_TYPE_FIRST;
1807     } else if (!strcasecmp(s, "later")) {
1808         *type = OVS_FRAG_TYPE_LATER;
1809     } else {
1810         return false;
1811     }
1812     return true;
1813 }
1814
1815 /* Parsing. */
1816
1817 static int
1818 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
1819          uint8_t (*mask)[ETH_ADDR_LEN])
1820 {
1821     int n;
1822
1823     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
1824         int len = n;
1825
1826         if (mask) {
1827             if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
1828                          ETH_ADDR_SCAN_ARGS(*mask), &n)) {
1829                 len += n;
1830             } else {
1831                 memset(mask, 0xff, sizeof *mask);
1832             }
1833         }
1834         return len;
1835     }
1836     return 0;
1837 }
1838
1839 static int
1840 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
1841 {
1842     int n;
1843
1844     if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
1845         int len = n;
1846
1847         if (mask) {
1848             if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
1849                          IP_SCAN_ARGS(mask), &n)) {
1850                 len += n;
1851             } else {
1852                 *mask = OVS_BE32_MAX;
1853             }
1854         }
1855         return len;
1856     }
1857     return 0;
1858 }
1859
1860 static int
1861 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
1862 {
1863     int n;
1864     char ipv6_s[IPV6_SCAN_LEN + 1];
1865
1866     if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
1867         && inet_pton(AF_INET6, ipv6_s, key) == 1) {
1868         int len = n;
1869
1870         if (mask) {
1871             if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
1872                 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
1873                 len += n;
1874             } else {
1875                 memset(mask, 0xff, sizeof *mask);
1876             }
1877         }
1878         return len;
1879     }
1880     return 0;
1881 }
1882
1883 static int
1884 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
1885 {
1886     int key_, mask_;
1887     int n;
1888
1889     if (ovs_scan(s, "%i%n", &key_, &n)
1890         && (key_ & ~IPV6_LABEL_MASK) == 0) {
1891         int len = n;
1892
1893         *key = htonl(key_);
1894         if (mask) {
1895             if (ovs_scan(s + len, "/%i%n", &mask_, &n)
1896                 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
1897                 len += n;
1898                 *mask = htonl(mask_);
1899             } else {
1900                 *mask = htonl(IPV6_LABEL_MASK);
1901             }
1902         }
1903         return len;
1904     }
1905     return 0;
1906 }
1907
1908 static int
1909 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
1910 {
1911     int n;
1912
1913     if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
1914         int len = n;
1915
1916         if (mask) {
1917             if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
1918                 len += n;
1919             } else {
1920                 *mask = UINT8_MAX;
1921             }
1922         }
1923         return len;
1924     }
1925     return 0;
1926 }
1927
1928 static int
1929 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
1930 {
1931     int n;
1932
1933     if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
1934         int len = n;
1935
1936         if (mask) {
1937             if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
1938                 len += n;
1939             } else {
1940                 *mask = UINT32_MAX;
1941             }
1942         }
1943         return len;
1944     }
1945     return 0;
1946 }
1947
1948 static int
1949 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
1950 {
1951     uint16_t key_, mask_;
1952     int n;
1953
1954     if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
1955         int len = n;
1956
1957         *key = htons(key_);
1958         if (mask) {
1959             if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
1960                 len += n;
1961                 *mask = htons(mask_);
1962             } else {
1963                 *mask = OVS_BE16_MAX;
1964             }
1965         }
1966         return len;
1967     }
1968     return 0;
1969 }
1970
1971 static int
1972 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
1973 {
1974     uint64_t key_, mask_;
1975     int n;
1976
1977     if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
1978         int len = n;
1979
1980         *key = htonll(key_);
1981         if (mask) {
1982             if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
1983                 len += n;
1984                 *mask = htonll(mask_);
1985             } else {
1986                 *mask = OVS_BE64_MAX;
1987             }
1988         }
1989         return len;
1990     }
1991     return 0;
1992 }
1993
1994 static int
1995 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
1996 {
1997     uint32_t flags, fmask;
1998     int n;
1999
2000     n = parse_flags(s, flow_tun_flag_to_string, &flags,
2001                     FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2002     if (n >= 0 && s[n] == ')') {
2003         *key = flags;
2004         if (mask) {
2005             *mask = fmask;
2006         }
2007         return n + 1;
2008     }
2009     return 0;
2010 }
2011
2012 static int
2013 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2014 {
2015     uint32_t flags, fmask;
2016     int n;
2017
2018     n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2019                     TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2020     if (n >= 0) {
2021         *key = htons(flags);
2022         if (mask) {
2023             *mask = htons(fmask);
2024         }
2025         return n;
2026     }
2027     return 0;
2028 }
2029
2030 static int
2031 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2032 {
2033     int n;
2034     char frag[8];
2035     enum ovs_frag_type frag_type;
2036
2037     if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2038         && ovs_frag_type_from_string(frag, &frag_type)) {
2039         int len = n;
2040
2041         *key = frag_type;
2042         if (mask) {
2043             *mask = UINT8_MAX;
2044         }
2045         return len;
2046     }
2047     return 0;
2048 }
2049
2050 static int
2051 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2052           const struct simap *port_names)
2053 {
2054     int n;
2055
2056     if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2057         int len = n;
2058
2059         if (mask) {
2060             if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2061                 len += n;
2062             } else {
2063                 *mask = UINT32_MAX;
2064             }
2065         }
2066         return len;
2067     } else if (port_names) {
2068         const struct simap_node *node;
2069         int len;
2070
2071         len = strcspn(s, ")");
2072         node = simap_find_len(port_names, s, len);
2073         if (node) {
2074             *key = node->data;
2075
2076             if (mask) {
2077                 *mask = UINT32_MAX;
2078             }
2079             return len;
2080         }
2081     }
2082     return 0;
2083 }
2084
2085 /* Helper for vlan parsing. */
2086 struct ovs_key_vlan__ {
2087     ovs_be16 tci;
2088 };
2089
2090 static bool
2091 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2092 {
2093     const uint16_t mask = ((1U << bits) - 1) << offset;
2094
2095     if (value >> bits) {
2096         return false;
2097     }
2098
2099     *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2100     return true;
2101 }
2102
2103 static int
2104 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2105              uint8_t offset)
2106 {
2107     uint16_t key_, mask_;
2108     int n;
2109
2110     if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2111         int len = n;
2112
2113         if (set_be16_bf(key, bits, offset, key_)) {
2114             if (mask) {
2115                 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2116                     len += n;
2117
2118                     if (!set_be16_bf(mask, bits, offset, mask_)) {
2119                         return 0;
2120                     }
2121                 } else {
2122                     *mask |= htons(((1U << bits) - 1) << offset);
2123                 }
2124             }
2125             return len;
2126         }
2127     }
2128     return 0;
2129 }
2130
2131 static int
2132 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2133 {
2134     return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2135 }
2136
2137 static int
2138 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2139 {
2140     return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2141 }
2142
2143 static int
2144 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2145 {
2146     return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2147 }
2148
2149 /* For MPLS. */
2150 static bool
2151 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2152 {
2153     const uint32_t mask = ((1U << bits) - 1) << offset;
2154
2155     if (value >> bits) {
2156         return false;
2157     }
2158
2159     *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2160     return true;
2161 }
2162
2163 static int
2164 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2165              uint8_t offset)
2166 {
2167     uint32_t key_, mask_;
2168     int n;
2169
2170     if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2171         int len = n;
2172
2173         if (set_be32_bf(key, bits, offset, key_)) {
2174             if (mask) {
2175                 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2176                     len += n;
2177
2178                     if (!set_be32_bf(mask, bits, offset, mask_)) {
2179                         return 0;
2180                     }
2181                 } else {
2182                     *mask |= htonl(((1U << bits) - 1) << offset);
2183                 }
2184             }
2185             return len;
2186         }
2187     }
2188     return 0;
2189 }
2190
2191 static int
2192 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2193 {
2194     return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2195 }
2196
2197 static int
2198 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2199 {
2200     return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2201 }
2202
2203 static int
2204 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2205 {
2206     return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2207 }
2208
2209 static int
2210 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2211 {
2212     return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2213 }
2214
2215 /* ATTR is compile-time constant, so only the case with correct data type
2216  * will be used.  However, the compiler complains about the data  type for
2217  * the other cases, so we must cast to make the compiler silent. */
2218 #define SCAN_PUT_ATTR(BUF, ATTR, DATA)                          \
2219     if ((ATTR) == OVS_KEY_ATTR_TUNNEL) {                              \
2220         tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2221     } else {                                                    \
2222         nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA));   \
2223     }
2224
2225 #define SCAN_IF(NAME)                           \
2226     if (strncmp(s, NAME, strlen(NAME)) == 0) {  \
2227         const char *start = s;                  \
2228         int len;                                \
2229                                                 \
2230         s += strlen(NAME)
2231
2232 /* Usually no special initialization is needed. */
2233 #define SCAN_BEGIN(NAME, TYPE)                  \
2234     SCAN_IF(NAME);                              \
2235         TYPE skey, smask;                       \
2236         memset(&skey, 0, sizeof skey);          \
2237         memset(&smask, 0, sizeof smask);        \
2238         do {                                    \
2239             len = 0;
2240
2241 /* VLAN needs special initialization. */
2242 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT)  \
2243     SCAN_IF(NAME);                                        \
2244         TYPE skey = KEY_INIT;                       \
2245         TYPE smask = MASK_INIT;                     \
2246         do {                                        \
2247             len = 0;
2248
2249 /* Scan unnamed entry as 'TYPE' */
2250 #define SCAN_TYPE(TYPE, KEY, MASK)              \
2251     len = scan_##TYPE(s, KEY, MASK);            \
2252     if (len == 0) {                             \
2253         return -EINVAL;                         \
2254     }                                           \
2255     s += len
2256
2257 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2258 #define SCAN_FIELD(NAME, TYPE, FIELD)                                   \
2259     if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
2260         s += strlen(NAME);                                              \
2261         SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL);       \
2262         continue;                                                       \
2263     }
2264
2265 #define SCAN_FINISH()                           \
2266         } while (*s++ == ',' && len != 0);      \
2267         if (s[-1] != ')') {                     \
2268             return -EINVAL;                     \
2269         }
2270
2271 #define SCAN_FINISH_SINGLE()                    \
2272         } while (false);                        \
2273         if (*s++ != ')') {                      \
2274             return -EINVAL;                     \
2275         }
2276
2277 #define SCAN_PUT(ATTR)                                  \
2278         if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2279             SCAN_PUT_ATTR(key, ATTR, skey);             \
2280             if (mask) {                                 \
2281                 SCAN_PUT_ATTR(mask, ATTR, smask);       \
2282             }                                           \
2283         }
2284
2285 #define SCAN_END(ATTR)                                  \
2286         SCAN_FINISH();                                  \
2287         SCAN_PUT(ATTR);                                 \
2288         return s - start;                               \
2289     }
2290
2291 #define SCAN_END_SINGLE(ATTR)                           \
2292         SCAN_FINISH_SINGLE();                           \
2293         SCAN_PUT(ATTR);                                 \
2294         return s - start;                               \
2295     }
2296
2297 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR)       \
2298     SCAN_BEGIN(NAME, TYPE) {                         \
2299         SCAN_TYPE(SCAN_AS, &skey, &smask);           \
2300     } SCAN_END_SINGLE(ATTR)
2301
2302 #define SCAN_SINGLE_NO_MASK(NAME, TYPE, SCAN_AS, ATTR)       \
2303     SCAN_BEGIN(NAME, TYPE) {                         \
2304         SCAN_TYPE(SCAN_AS, &skey, NULL);           \
2305     } SCAN_END_SINGLE(ATTR)
2306
2307 /* scan_port needs one extra argument. */
2308 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR)  \
2309     SCAN_BEGIN(NAME, TYPE) {                            \
2310         len = scan_port(s, &skey, &smask, port_names);  \
2311         if (len == 0) {                                 \
2312             return -EINVAL;                             \
2313         }                                               \
2314         s += len;                                       \
2315     } SCAN_END_SINGLE(ATTR)
2316
2317 static int
2318 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2319                         struct ofpbuf *key, struct ofpbuf *mask)
2320 {
2321     SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2322     SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2323     SCAN_SINGLE_NO_MASK("recirc_id(", uint32_t, u32, OVS_KEY_ATTR_RECIRC_ID);
2324     SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2325
2326     SCAN_BEGIN("tunnel(", struct flow_tnl) {
2327         SCAN_FIELD("tun_id=", be64, tun_id);
2328         SCAN_FIELD("src=", ipv4, ip_src);
2329         SCAN_FIELD("dst=", ipv4, ip_dst);
2330         SCAN_FIELD("tos=", u8, ip_tos);
2331         SCAN_FIELD("ttl=", u8, ip_ttl);
2332         SCAN_FIELD("tp_src=", be16, tp_src);
2333         SCAN_FIELD("tp_dst=", be16, tp_dst);
2334         SCAN_FIELD("flags(", tun_flags, flags);
2335     } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2336
2337     SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2338
2339     SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2340         SCAN_FIELD("src=", eth, eth_src);
2341         SCAN_FIELD("dst=", eth, eth_dst);
2342     } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2343
2344     SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2345                     { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2346         SCAN_FIELD("vid=", vid, tci);
2347         SCAN_FIELD("pcp=", pcp, tci);
2348         SCAN_FIELD("cfi=", cfi, tci);
2349     } SCAN_END(OVS_KEY_ATTR_VLAN);
2350
2351     SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2352
2353     SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2354         SCAN_FIELD("label=", mpls_label, mpls_lse);
2355         SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2356         SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2357         SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2358     } SCAN_END(OVS_KEY_ATTR_MPLS);
2359
2360     SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2361         SCAN_FIELD("src=", ipv4, ipv4_src);
2362         SCAN_FIELD("dst=", ipv4, ipv4_dst);
2363         SCAN_FIELD("proto=", u8, ipv4_proto);
2364         SCAN_FIELD("tos=", u8, ipv4_tos);
2365         SCAN_FIELD("ttl=", u8, ipv4_ttl);
2366         SCAN_FIELD("frag=", frag, ipv4_frag);
2367     } SCAN_END(OVS_KEY_ATTR_IPV4);
2368
2369     SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2370         SCAN_FIELD("src=", ipv6, ipv6_src);
2371         SCAN_FIELD("dst=", ipv6, ipv6_dst);
2372         SCAN_FIELD("label=", ipv6_label, ipv6_label);
2373         SCAN_FIELD("proto=", u8, ipv6_proto);
2374         SCAN_FIELD("tclass=", u8, ipv6_tclass);
2375         SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2376         SCAN_FIELD("frag=", frag, ipv6_frag);
2377     } SCAN_END(OVS_KEY_ATTR_IPV6);
2378
2379     SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2380         SCAN_FIELD("src=", be16, tcp_src);
2381         SCAN_FIELD("dst=", be16, tcp_dst);
2382     } SCAN_END(OVS_KEY_ATTR_TCP);
2383
2384     SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2385
2386     SCAN_BEGIN("udp(", struct ovs_key_udp) {
2387         SCAN_FIELD("src=", be16, udp_src);
2388         SCAN_FIELD("dst=", be16, udp_dst);
2389     } SCAN_END(OVS_KEY_ATTR_UDP);
2390
2391     SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2392         SCAN_FIELD("src=", be16, sctp_src);
2393         SCAN_FIELD("dst=", be16, sctp_dst);
2394     } SCAN_END(OVS_KEY_ATTR_SCTP);
2395
2396     SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2397         SCAN_FIELD("type=", u8, icmp_type);
2398         SCAN_FIELD("code=", u8, icmp_code);
2399     } SCAN_END(OVS_KEY_ATTR_ICMP);
2400
2401     SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2402         SCAN_FIELD("type=", u8, icmpv6_type);
2403         SCAN_FIELD("code=", u8, icmpv6_code);
2404     } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2405
2406     SCAN_BEGIN("arp(", struct ovs_key_arp) {
2407         SCAN_FIELD("sip=", ipv4, arp_sip);
2408         SCAN_FIELD("tip=", ipv4, arp_tip);
2409         SCAN_FIELD("op=", be16, arp_op);
2410         SCAN_FIELD("sha=", eth, arp_sha);
2411         SCAN_FIELD("tha=", eth, arp_tha);
2412     } SCAN_END(OVS_KEY_ATTR_ARP);
2413
2414     SCAN_BEGIN("nd(", struct ovs_key_nd) {
2415         SCAN_FIELD("target=", ipv6, nd_target);
2416         SCAN_FIELD("sll=", eth, nd_sll);
2417         SCAN_FIELD("tll=", eth, nd_tll);
2418     } SCAN_END(OVS_KEY_ATTR_ND);
2419
2420     /* Encap open-coded. */
2421     if (!strncmp(s, "encap(", 6)) {
2422         const char *start = s;
2423         size_t encap, encap_mask = 0;
2424
2425         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2426         if (mask) {
2427             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2428         }
2429
2430         s += 6;
2431         for (;;) {
2432             int retval;
2433
2434             s += strspn(s, ", \t\r\n");
2435             if (!*s) {
2436                 return -EINVAL;
2437             } else if (*s == ')') {
2438                 break;
2439             }
2440
2441             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2442             if (retval < 0) {
2443                 return retval;
2444             }
2445             s += retval;
2446         }
2447         s++;
2448
2449         nl_msg_end_nested(key, encap);
2450         if (mask) {
2451             nl_msg_end_nested(mask, encap_mask);
2452         }
2453
2454         return s - start;
2455     }
2456
2457     return -EINVAL;
2458 }
2459
2460 /* Parses the string representation of a datapath flow key, in the
2461  * format output by odp_flow_key_format().  Returns 0 if successful,
2462  * otherwise a positive errno value.  On success, the flow key is
2463  * appended to 'key' as a series of Netlink attributes.  On failure, no
2464  * data is appended to 'key'.  Either way, 'key''s data might be
2465  * reallocated.
2466  *
2467  * If 'port_names' is nonnull, it points to an simap that maps from a port name
2468  * to a port number.  (Port names may be used instead of port numbers in
2469  * in_port.)
2470  *
2471  * On success, the attributes appended to 'key' are individually syntactically
2472  * valid, but they may not be valid as a sequence.  'key' might, for example,
2473  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
2474 int
2475 odp_flow_from_string(const char *s, const struct simap *port_names,
2476                      struct ofpbuf *key, struct ofpbuf *mask)
2477 {
2478     const size_t old_size = ofpbuf_size(key);
2479     for (;;) {
2480         int retval;
2481
2482         s += strspn(s, delimiters);
2483         if (!*s) {
2484             return 0;
2485         }
2486
2487         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2488         if (retval < 0) {
2489             ofpbuf_set_size(key, old_size);
2490             return -retval;
2491         }
2492         s += retval;
2493     }
2494
2495     return 0;
2496 }
2497
2498 static uint8_t
2499 ovs_to_odp_frag(uint8_t nw_frag)
2500 {
2501     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2502           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2503           : OVS_FRAG_TYPE_LATER);
2504 }
2505
2506 static uint8_t
2507 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2508 {
2509     uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2510
2511     frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2512     frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2513
2514     return frag_mask;
2515 }
2516
2517 static void
2518 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2519                          const struct flow *mask, odp_port_t odp_in_port,
2520                          size_t max_mpls_depth, bool recirc, bool export_mask)
2521 {
2522     struct ovs_key_ethernet *eth_key;
2523     size_t encap;
2524     const struct flow *data = export_mask ? mask : flow;
2525
2526     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2527
2528     if (flow->tunnel.ip_dst || export_mask) {
2529         tun_key_to_attr(buf, &data->tunnel);
2530     }
2531
2532     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2533
2534     if (recirc) {
2535         nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2536         nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2537     }
2538
2539     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2540      * is not the magical value "ODPP_NONE". */
2541     if (export_mask || odp_in_port != ODPP_NONE) {
2542         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2543     }
2544
2545     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2546                                        sizeof *eth_key);
2547     memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2548     memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2549
2550     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2551         if (export_mask) {
2552             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2553         } else {
2554             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2555         }
2556         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2557         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2558         if (flow->vlan_tci == htons(0)) {
2559             goto unencap;
2560         }
2561     } else {
2562         encap = 0;
2563     }
2564
2565     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2566         /* For backwards compatibility with kernels that don't support
2567          * wildcarding, the following convention is used to encode the
2568          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2569          *
2570          *   key      mask    matches
2571          * -------- --------  -------
2572          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
2573          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
2574          *  <none>   0xffff   Any non-Ethernet II frame (except valid
2575          *                    802.3 SNAP packet with valid eth_type).
2576          */
2577         if (export_mask) {
2578             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2579         }
2580         goto unencap;
2581     }
2582
2583     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2584
2585     if (flow->dl_type == htons(ETH_TYPE_IP)) {
2586         struct ovs_key_ipv4 *ipv4_key;
2587
2588         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2589                                             sizeof *ipv4_key);
2590         ipv4_key->ipv4_src = data->nw_src;
2591         ipv4_key->ipv4_dst = data->nw_dst;
2592         ipv4_key->ipv4_proto = data->nw_proto;
2593         ipv4_key->ipv4_tos = data->nw_tos;
2594         ipv4_key->ipv4_ttl = data->nw_ttl;
2595         ipv4_key->ipv4_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2596                                       : ovs_to_odp_frag(data->nw_frag);
2597     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2598         struct ovs_key_ipv6 *ipv6_key;
2599
2600         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2601                                             sizeof *ipv6_key);
2602         memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2603         memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2604         ipv6_key->ipv6_label = data->ipv6_label;
2605         ipv6_key->ipv6_proto = data->nw_proto;
2606         ipv6_key->ipv6_tclass = data->nw_tos;
2607         ipv6_key->ipv6_hlimit = data->nw_ttl;
2608         ipv6_key->ipv6_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2609                                       : ovs_to_odp_frag(data->nw_frag);
2610     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2611                flow->dl_type == htons(ETH_TYPE_RARP)) {
2612         struct ovs_key_arp *arp_key;
2613
2614         arp_key = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_ARP,
2615                                          sizeof *arp_key);
2616         arp_key->arp_sip = data->nw_src;
2617         arp_key->arp_tip = data->nw_dst;
2618         arp_key->arp_op = htons(data->nw_proto);
2619         memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2620         memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2621     } else if (eth_type_mpls(flow->dl_type)) {
2622         struct ovs_key_mpls *mpls_key;
2623         int i, n;
2624
2625         n = flow_count_mpls_labels(flow, NULL);
2626         n = MIN(n, max_mpls_depth);
2627         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2628                                             n * sizeof *mpls_key);
2629         for (i = 0; i < n; i++) {
2630             mpls_key[i].mpls_lse = data->mpls_lse[i];
2631         }
2632     }
2633
2634     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2635         if (flow->nw_proto == IPPROTO_TCP) {
2636             struct ovs_key_tcp *tcp_key;
2637
2638             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2639                                                sizeof *tcp_key);
2640             tcp_key->tcp_src = data->tp_src;
2641             tcp_key->tcp_dst = data->tp_dst;
2642
2643             if (data->tcp_flags) {
2644                 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2645             }
2646         } else if (flow->nw_proto == IPPROTO_UDP) {
2647             struct ovs_key_udp *udp_key;
2648
2649             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2650                                                sizeof *udp_key);
2651             udp_key->udp_src = data->tp_src;
2652             udp_key->udp_dst = data->tp_dst;
2653         } else if (flow->nw_proto == IPPROTO_SCTP) {
2654             struct ovs_key_sctp *sctp_key;
2655
2656             sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2657                                                sizeof *sctp_key);
2658             sctp_key->sctp_src = data->tp_src;
2659             sctp_key->sctp_dst = data->tp_dst;
2660         } else if (flow->dl_type == htons(ETH_TYPE_IP)
2661                 && flow->nw_proto == IPPROTO_ICMP) {
2662             struct ovs_key_icmp *icmp_key;
2663
2664             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2665                                                 sizeof *icmp_key);
2666             icmp_key->icmp_type = ntohs(data->tp_src);
2667             icmp_key->icmp_code = ntohs(data->tp_dst);
2668         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2669                 && flow->nw_proto == IPPROTO_ICMPV6) {
2670             struct ovs_key_icmpv6 *icmpv6_key;
2671
2672             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2673                                                   sizeof *icmpv6_key);
2674             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2675             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2676
2677             if (flow->tp_dst == htons(0)
2678                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
2679                     || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
2680                 && (!export_mask || (data->tp_src == htons(0xffff)
2681                                      && data->tp_dst == htons(0xffff)))) {
2682
2683                 struct ovs_key_nd *nd_key;
2684
2685                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2686                                                     sizeof *nd_key);
2687                 memcpy(nd_key->nd_target, &data->nd_target,
2688                         sizeof nd_key->nd_target);
2689                 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2690                 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2691             }
2692         }
2693     }
2694
2695 unencap:
2696     if (encap) {
2697         nl_msg_end_nested(buf, encap);
2698     }
2699 }
2700
2701 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2702  * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2703  * number rather than a datapath port number).  Instead, if 'odp_in_port'
2704  * is anything other than ODPP_NONE, it is included in 'buf' as the input
2705  * port.
2706  *
2707  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2708  * capable of being expanded to allow for that much space.
2709  *
2710  * 'recirc' indicates support for recirculation fields. If this is true, then
2711  * these fields will always be serialised. */
2712 void
2713 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2714                        const struct flow *mask, odp_port_t odp_in_port,
2715                        bool recirc)
2716 {
2717     odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
2718                              false);
2719 }
2720
2721 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2722  * 'buf'.  'flow' is used as a template to determine how to interpret
2723  * 'mask'.  For example, the 'dl_type' of 'mask' describes the mask, but
2724  * it doesn't indicate whether the other fields should be interpreted as
2725  * ARP, IPv4, IPv6, etc.
2726  *
2727  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2728  * capable of being expanded to allow for that much space.
2729  *
2730  * 'recirc' indicates support for recirculation fields. If this is true, then
2731  * these fields will always be serialised. */
2732 void
2733 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2734                        const struct flow *flow, uint32_t odp_in_port_mask,
2735                        size_t max_mpls_depth, bool recirc)
2736 {
2737     odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
2738                              max_mpls_depth, recirc, true);
2739 }
2740
2741 /* Generate ODP flow key from the given packet metadata */
2742 void
2743 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
2744 {
2745     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
2746
2747     if (md->tunnel.ip_dst) {
2748         tun_key_to_attr(buf, &md->tunnel);
2749     }
2750
2751     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
2752
2753     /* Add an ingress port attribute if 'odp_in_port' is not the magical
2754      * value "ODPP_NONE". */
2755     if (md->in_port.odp_port != ODPP_NONE) {
2756         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
2757     }
2758 }
2759
2760 /* Generate packet metadata from the given ODP flow key. */
2761 void
2762 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
2763                         struct pkt_metadata *md)
2764 {
2765     const struct nlattr *nla;
2766     size_t left;
2767     uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
2768         1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
2769         1u << OVS_KEY_ATTR_IN_PORT;
2770
2771     *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
2772
2773     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2774         uint16_t type = nl_attr_type(nla);
2775         size_t len = nl_attr_get_size(nla);
2776         int expected_len = odp_flow_key_attr_len(type);
2777
2778         if (len != expected_len && expected_len >= 0) {
2779             continue;
2780         }
2781
2782         switch (type) {
2783         case OVS_KEY_ATTR_RECIRC_ID:
2784             md->recirc_id = nl_attr_get_u32(nla);
2785             wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
2786             break;
2787         case OVS_KEY_ATTR_DP_HASH:
2788             md->dp_hash = nl_attr_get_u32(nla);
2789             wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
2790             break;
2791         case OVS_KEY_ATTR_PRIORITY:
2792             md->skb_priority = nl_attr_get_u32(nla);
2793             wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
2794             break;
2795         case OVS_KEY_ATTR_SKB_MARK:
2796             md->pkt_mark = nl_attr_get_u32(nla);
2797             wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
2798             break;
2799         case OVS_KEY_ATTR_TUNNEL: {
2800             enum odp_key_fitness res;
2801
2802             res = odp_tun_key_from_attr(nla, &md->tunnel);
2803             if (res == ODP_FIT_ERROR) {
2804                 memset(&md->tunnel, 0, sizeof md->tunnel);
2805             } else if (res == ODP_FIT_PERFECT) {
2806                 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
2807             }
2808             break;
2809         }
2810         case OVS_KEY_ATTR_IN_PORT:
2811             md->in_port.odp_port = nl_attr_get_odp_port(nla);
2812             wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
2813             break;
2814         default:
2815             break;
2816         }
2817
2818         if (!wanted_attrs) {
2819             return; /* Have everything. */
2820         }
2821     }
2822 }
2823
2824 uint32_t
2825 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2826 {
2827     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2828     return hash_words(ALIGNED_CAST(const uint32_t *, key),
2829                       key_len / sizeof(uint32_t), 0);
2830 }
2831
2832 static void
2833 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2834                        uint64_t attrs, int out_of_range_attr,
2835                        const struct nlattr *key, size_t key_len)
2836 {
2837     struct ds s;
2838     int i;
2839
2840     if (VLOG_DROP_DBG(rl)) {
2841         return;
2842     }
2843
2844     ds_init(&s);
2845     for (i = 0; i < 64; i++) {
2846         if (attrs & (UINT64_C(1) << i)) {
2847             char namebuf[OVS_KEY_ATTR_BUFSIZE];
2848
2849             ds_put_format(&s, " %s",
2850                           ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2851         }
2852     }
2853     if (out_of_range_attr) {
2854         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2855     }
2856
2857     ds_put_cstr(&s, ": ");
2858     odp_flow_key_format(key, key_len, &s);
2859
2860     VLOG_DBG("%s:%s", title, ds_cstr(&s));
2861     ds_destroy(&s);
2862 }
2863
2864 static bool
2865 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2866 {
2867     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2868
2869     if (odp_frag > OVS_FRAG_TYPE_LATER) {
2870         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2871         return false;
2872     }
2873
2874     if (odp_frag != OVS_FRAG_TYPE_NONE) {
2875         flow->nw_frag |= FLOW_NW_FRAG_ANY;
2876         if (odp_frag == OVS_FRAG_TYPE_LATER) {
2877             flow->nw_frag |= FLOW_NW_FRAG_LATER;
2878         }
2879     }
2880     return true;
2881 }
2882
2883 static bool
2884 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2885                    const struct nlattr *attrs[], uint64_t *present_attrsp,
2886                    int *out_of_range_attrp)
2887 {
2888     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2889     const struct nlattr *nla;
2890     uint64_t present_attrs;
2891     size_t left;
2892
2893     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2894     present_attrs = 0;
2895     *out_of_range_attrp = 0;
2896     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2897         uint16_t type = nl_attr_type(nla);
2898         size_t len = nl_attr_get_size(nla);
2899         int expected_len = odp_flow_key_attr_len(type);
2900
2901         if (len != expected_len && expected_len >= 0) {
2902             char namebuf[OVS_KEY_ATTR_BUFSIZE];
2903
2904             VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
2905                         "length %d", ovs_key_attr_to_string(type, namebuf,
2906                                                             sizeof namebuf),
2907                         len, expected_len);
2908             return false;
2909         }
2910
2911         if (type > OVS_KEY_ATTR_MAX) {
2912             *out_of_range_attrp = type;
2913         } else {
2914             if (present_attrs & (UINT64_C(1) << type)) {
2915                 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2916
2917                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2918                             ovs_key_attr_to_string(type,
2919                                                    namebuf, sizeof namebuf));
2920                 return false;
2921             }
2922
2923             present_attrs |= UINT64_C(1) << type;
2924             attrs[type] = nla;
2925         }
2926     }
2927     if (left) {
2928         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2929         return false;
2930     }
2931
2932     *present_attrsp = present_attrs;
2933     return true;
2934 }
2935
2936 static enum odp_key_fitness
2937 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2938                    uint64_t expected_attrs,
2939                    const struct nlattr *key, size_t key_len)
2940 {
2941     uint64_t missing_attrs;
2942     uint64_t extra_attrs;
2943
2944     missing_attrs = expected_attrs & ~present_attrs;
2945     if (missing_attrs) {
2946         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2947         log_odp_key_attributes(&rl, "expected but not present",
2948                                missing_attrs, 0, key, key_len);
2949         return ODP_FIT_TOO_LITTLE;
2950     }
2951
2952     extra_attrs = present_attrs & ~expected_attrs;
2953     if (extra_attrs || out_of_range_attr) {
2954         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2955         log_odp_key_attributes(&rl, "present but not expected",
2956                                extra_attrs, out_of_range_attr, key, key_len);
2957         return ODP_FIT_TOO_MUCH;
2958     }
2959
2960     return ODP_FIT_PERFECT;
2961 }
2962
2963 static bool
2964 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2965                 uint64_t present_attrs, uint64_t *expected_attrs,
2966                 struct flow *flow, const struct flow *src_flow)
2967 {
2968     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2969     bool is_mask = flow != src_flow;
2970
2971     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2972         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2973         if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2974             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2975                         ntohs(flow->dl_type));
2976             return false;
2977         }
2978         if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2979             flow->dl_type != htons(0xffff)) {
2980             return false;
2981         }
2982         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2983     } else {
2984         if (!is_mask) {
2985             flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2986         } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2987             /* See comments in odp_flow_key_from_flow__(). */
2988             VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2989             return false;
2990         }
2991     }
2992     return true;
2993 }
2994
2995 static enum odp_key_fitness
2996 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2997                   uint64_t present_attrs, int out_of_range_attr,
2998                   uint64_t expected_attrs, struct flow *flow,
2999                   const struct nlattr *key, size_t key_len,
3000                   const struct flow *src_flow)
3001 {
3002     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3003     bool is_mask = src_flow != flow;
3004     const void *check_start = NULL;
3005     size_t check_len = 0;
3006     enum ovs_key_attr expected_bit = 0xff;
3007
3008     if (eth_type_mpls(src_flow->dl_type)) {
3009         size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3010         const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3011         int n = size / sizeof(ovs_be32);
3012         int i;
3013
3014         if (!size || size % sizeof(ovs_be32)) {
3015             return ODP_FIT_ERROR;
3016         }
3017
3018         if (!is_mask) {
3019             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3020
3021             if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
3022                 return ODP_FIT_TOO_LITTLE;
3023             }
3024         } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3025             if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3026                 return ODP_FIT_ERROR;
3027             }
3028             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3029         }
3030
3031         for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3032             flow->mpls_lse[i] = mpls_lse[i];
3033         }
3034         if (n > FLOW_MAX_MPLS_LABELS) {
3035             return ODP_FIT_TOO_MUCH;
3036         }
3037
3038         if (!is_mask) {
3039             /* BOS may be set only in the innermost label. */
3040             for (i = 0; i < n - 1; i++) {
3041                 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3042                     return ODP_FIT_ERROR;
3043                 }
3044             }
3045
3046             /* BOS must be set in the innermost label. */
3047             if (n < FLOW_MAX_MPLS_LABELS
3048                 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3049                 return ODP_FIT_TOO_LITTLE;
3050             }
3051         }
3052
3053         goto done;
3054     } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3055         if (!is_mask) {
3056             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3057         }
3058         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3059             const struct ovs_key_ipv4 *ipv4_key;
3060
3061             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3062             flow->nw_src = ipv4_key->ipv4_src;
3063             flow->nw_dst = ipv4_key->ipv4_dst;
3064             flow->nw_proto = ipv4_key->ipv4_proto;
3065             flow->nw_tos = ipv4_key->ipv4_tos;
3066             flow->nw_ttl = ipv4_key->ipv4_ttl;
3067             if (is_mask) {
3068                 flow->nw_frag = ipv4_key->ipv4_frag;
3069                 check_start = ipv4_key;
3070                 check_len = sizeof *ipv4_key;
3071                 expected_bit = OVS_KEY_ATTR_IPV4;
3072             } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
3073                 return ODP_FIT_ERROR;
3074             }
3075         }
3076     } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3077         if (!is_mask) {
3078             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3079         }
3080         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3081             const struct ovs_key_ipv6 *ipv6_key;
3082
3083             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3084             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
3085             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
3086             flow->ipv6_label = ipv6_key->ipv6_label;
3087             flow->nw_proto = ipv6_key->ipv6_proto;
3088             flow->nw_tos = ipv6_key->ipv6_tclass;
3089             flow->nw_ttl = ipv6_key->ipv6_hlimit;
3090             if (is_mask) {
3091                 flow->nw_frag = ipv6_key->ipv6_frag;
3092                 check_start = ipv6_key;
3093                 check_len = sizeof *ipv6_key;
3094                 expected_bit = OVS_KEY_ATTR_IPV6;
3095             } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
3096                 return ODP_FIT_ERROR;
3097             }
3098         }
3099     } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3100                src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3101         if (!is_mask) {
3102             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3103         }
3104         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3105             const struct ovs_key_arp *arp_key;
3106
3107             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3108             flow->nw_src = arp_key->arp_sip;
3109             flow->nw_dst = arp_key->arp_tip;
3110             if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3111                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3112                             "key", ntohs(arp_key->arp_op));
3113                 return ODP_FIT_ERROR;
3114             }
3115             flow->nw_proto = ntohs(arp_key->arp_op);
3116             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
3117             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
3118
3119             if (is_mask) {
3120                 check_start = arp_key;
3121                 check_len = sizeof *arp_key;
3122                 expected_bit = OVS_KEY_ATTR_ARP;
3123             }
3124         }
3125     } else {
3126         goto done;
3127     }
3128     if (check_len > 0) { /* Happens only when 'is_mask'. */
3129         if (!is_all_zeros(check_start, check_len) &&
3130             flow->dl_type != htons(0xffff)) {
3131             return ODP_FIT_ERROR;
3132         } else {
3133             expected_attrs |= UINT64_C(1) << expected_bit;
3134         }
3135     }
3136
3137     expected_bit = OVS_KEY_ATTR_UNSPEC;
3138     if (src_flow->nw_proto == IPPROTO_TCP
3139         && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3140             src_flow->dl_type == htons(ETH_TYPE_IPV6))
3141         && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3142         if (!is_mask) {
3143             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3144         }
3145         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3146             const struct ovs_key_tcp *tcp_key;
3147
3148             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3149             flow->tp_src = tcp_key->tcp_src;
3150             flow->tp_dst = tcp_key->tcp_dst;
3151             expected_bit = OVS_KEY_ATTR_TCP;
3152         }
3153         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3154             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3155             flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3156         }
3157     } else if (src_flow->nw_proto == IPPROTO_UDP
3158                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3159                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
3160                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3161         if (!is_mask) {
3162             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3163         }
3164         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3165             const struct ovs_key_udp *udp_key;
3166
3167             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3168             flow->tp_src = udp_key->udp_src;
3169             flow->tp_dst = udp_key->udp_dst;
3170             expected_bit = OVS_KEY_ATTR_UDP;
3171         }
3172     } else if (src_flow->nw_proto == IPPROTO_SCTP
3173                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3174                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
3175                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3176         if (!is_mask) {
3177             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3178         }
3179         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3180             const struct ovs_key_sctp *sctp_key;
3181
3182             sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3183             flow->tp_src = sctp_key->sctp_src;
3184             flow->tp_dst = sctp_key->sctp_dst;
3185             expected_bit = OVS_KEY_ATTR_SCTP;
3186         }
3187     } else if (src_flow->nw_proto == IPPROTO_ICMP
3188                && src_flow->dl_type == htons(ETH_TYPE_IP)
3189                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3190         if (!is_mask) {
3191             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3192         }
3193         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3194             const struct ovs_key_icmp *icmp_key;
3195
3196             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3197             flow->tp_src = htons(icmp_key->icmp_type);
3198             flow->tp_dst = htons(icmp_key->icmp_code);
3199             expected_bit = OVS_KEY_ATTR_ICMP;
3200         }
3201     } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3202                && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3203                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3204         if (!is_mask) {
3205             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3206         }
3207         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3208             const struct ovs_key_icmpv6 *icmpv6_key;
3209
3210             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3211             flow->tp_src = htons(icmpv6_key->icmpv6_type);
3212             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3213             expected_bit = OVS_KEY_ATTR_ICMPV6;
3214             if (src_flow->tp_dst == htons(0) &&
3215                 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3216                  src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3217                 if (!is_mask) {
3218                     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3219                 }
3220                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3221                     const struct ovs_key_nd *nd_key;
3222
3223                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3224                     memcpy(&flow->nd_target, nd_key->nd_target,
3225                            sizeof flow->nd_target);
3226                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3227                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3228                     if (is_mask) {
3229                         if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3230                             (flow->tp_src != htons(0xffff) ||
3231                              flow->tp_dst != htons(0xffff))) {
3232                             return ODP_FIT_ERROR;
3233                         } else {
3234                             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3235                         }
3236                     }
3237                 }
3238             }
3239         }
3240     }
3241     if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3242         if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3243             return ODP_FIT_ERROR;
3244         } else {
3245             expected_attrs |= UINT64_C(1) << expected_bit;
3246         }
3247     }
3248
3249 done:
3250     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3251                               key, key_len);
3252 }
3253
3254 /* Parse 802.1Q header then encapsulated L3 attributes. */
3255 static enum odp_key_fitness
3256 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3257                    uint64_t present_attrs, int out_of_range_attr,
3258                    uint64_t expected_attrs, struct flow *flow,
3259                    const struct nlattr *key, size_t key_len,
3260                    const struct flow *src_flow)
3261 {
3262     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3263     bool is_mask = src_flow != flow;
3264
3265     const struct nlattr *encap
3266         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3267            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3268     enum odp_key_fitness encap_fitness;
3269     enum odp_key_fitness fitness;
3270
3271     /* Calculate fitness of outer attributes. */
3272     if (!is_mask) {
3273         expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3274                           (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3275     } else {
3276         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3277             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3278         }
3279         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3280             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3281         }
3282     }
3283     fitness = check_expectations(present_attrs, out_of_range_attr,
3284                                  expected_attrs, key, key_len);
3285
3286     /* Set vlan_tci.
3287      * Remove the TPID from dl_type since it's not the real Ethertype.  */
3288     flow->dl_type = htons(0);
3289     flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3290                       ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3291                       : htons(0));
3292     if (!is_mask) {
3293         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3294             return ODP_FIT_TOO_LITTLE;
3295         } else if (flow->vlan_tci == htons(0)) {
3296             /* Corner case for a truncated 802.1Q header. */
3297             if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3298                 return ODP_FIT_TOO_MUCH;
3299             }
3300             return fitness;
3301         } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3302             VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3303                         "but CFI bit is not set", ntohs(flow->vlan_tci));
3304             return ODP_FIT_ERROR;
3305         }
3306     } else {
3307         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3308             return fitness;
3309         }
3310     }
3311
3312     /* Now parse the encapsulated attributes. */
3313     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3314                             attrs, &present_attrs, &out_of_range_attr)) {
3315         return ODP_FIT_ERROR;
3316     }
3317     expected_attrs = 0;
3318
3319     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3320         return ODP_FIT_ERROR;
3321     }
3322     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3323                                       expected_attrs, flow, key, key_len,
3324                                       src_flow);
3325
3326     /* The overall fitness is the worse of the outer and inner attributes. */
3327     return MAX(fitness, encap_fitness);
3328 }
3329
3330 static enum odp_key_fitness
3331 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3332                        struct flow *flow, const struct flow *src_flow)
3333 {
3334     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3335     uint64_t expected_attrs;
3336     uint64_t present_attrs;
3337     int out_of_range_attr;
3338     bool is_mask = src_flow != flow;
3339
3340     memset(flow, 0, sizeof *flow);
3341
3342     /* Parse attributes. */
3343     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3344                             &out_of_range_attr)) {
3345         return ODP_FIT_ERROR;
3346     }
3347     expected_attrs = 0;
3348
3349     /* Metadata. */
3350     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3351         flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3352         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3353     } else if (is_mask) {
3354         /* Always exact match recirc_id if it is not specified. */
3355         flow->recirc_id = UINT32_MAX;
3356     }
3357
3358     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3359         flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3360         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3361     }
3362     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3363         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3364         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3365     }
3366
3367     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3368         flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3369         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3370     }
3371
3372     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3373         enum odp_key_fitness res;
3374
3375         res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3376         if (res == ODP_FIT_ERROR) {
3377             return ODP_FIT_ERROR;
3378         } else if (res == ODP_FIT_PERFECT) {
3379             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3380         }
3381     }
3382
3383     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3384         flow->in_port.odp_port
3385             = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3386         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3387     } else if (!is_mask) {
3388         flow->in_port.odp_port = ODPP_NONE;
3389     }
3390
3391     /* Ethernet header. */
3392     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3393         const struct ovs_key_ethernet *eth_key;
3394
3395         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3396         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3397         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3398         if (is_mask) {
3399             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3400         }
3401     }
3402     if (!is_mask) {
3403         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3404     }
3405
3406     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3407     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3408         src_flow)) {
3409         return ODP_FIT_ERROR;
3410     }
3411
3412     if (is_mask
3413         ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3414         : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3415         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3416                                   expected_attrs, flow, key, key_len, src_flow);
3417     }
3418     if (is_mask) {
3419         flow->vlan_tci = htons(0xffff);
3420         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3421             flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3422             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3423         }
3424     }
3425     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3426                              expected_attrs, flow, key, key_len, src_flow);
3427 }
3428
3429 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3430  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
3431  * 'key' fits our expectations for what a flow key should contain.
3432  *
3433  * The 'in_port' will be the datapath's understanding of the port.  The
3434  * caller will need to translate with odp_port_to_ofp_port() if the
3435  * OpenFlow port is needed.
3436  *
3437  * This function doesn't take the packet itself as an argument because none of
3438  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
3439  * it is always possible to infer which additional attribute(s) should appear
3440  * by looking at the attributes for lower-level protocols, e.g. if the network
3441  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3442  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3443  * must be absent. */
3444 enum odp_key_fitness
3445 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3446                      struct flow *flow)
3447 {
3448    return odp_flow_key_to_flow__(key, key_len, flow, flow);
3449 }
3450
3451 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3452  * structure in 'mask'.  'flow' must be a previously translated flow
3453  * corresponding to 'mask'.  Returns an ODP_FIT_* value that indicates how well
3454  * 'key' fits our expectations for what a flow key should contain. */
3455 enum odp_key_fitness
3456 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3457                      struct flow *mask, const struct flow *flow)
3458 {
3459    return odp_flow_key_to_flow__(key, key_len, mask, flow);
3460 }
3461
3462 /* Returns 'fitness' as a string, for use in debug messages. */
3463 const char *
3464 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3465 {
3466     switch (fitness) {
3467     case ODP_FIT_PERFECT:
3468         return "OK";
3469     case ODP_FIT_TOO_MUCH:
3470         return "too_much";
3471     case ODP_FIT_TOO_LITTLE:
3472         return "too_little";
3473     case ODP_FIT_ERROR:
3474         return "error";
3475     default:
3476         return "<unknown>";
3477     }
3478 }
3479
3480 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3481  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
3482  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3483  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
3484  * null, then the return value is not meaningful.) */
3485 size_t
3486 odp_put_userspace_action(uint32_t pid,
3487                          const void *userdata, size_t userdata_size,
3488                          odp_port_t tunnel_out_port,
3489                          struct ofpbuf *odp_actions)
3490 {
3491     size_t userdata_ofs;
3492     size_t offset;
3493
3494     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3495     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3496     if (userdata) {
3497         userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN;
3498
3499         /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3500          * module before Linux 3.10 required the userdata to be exactly 8 bytes
3501          * long:
3502          *
3503          *   - The kernel rejected shorter userdata with -ERANGE.
3504          *
3505          *   - The kernel silently dropped userdata beyond the first 8 bytes.
3506          *
3507          * Thus, for maximum compatibility, always put at least 8 bytes.  (We
3508          * separately disable features that required more than 8 bytes.) */
3509         memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3510                                       MAX(8, userdata_size)),
3511                userdata, userdata_size);
3512     } else {
3513         userdata_ofs = 0;
3514     }
3515     if (tunnel_out_port != ODPP_NONE) {
3516         nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3517                             tunnel_out_port);
3518     }
3519     nl_msg_end_nested(odp_actions, offset);
3520
3521     return userdata_ofs;
3522 }
3523
3524 void
3525 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3526                       struct ofpbuf *odp_actions)
3527 {
3528     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3529     tun_key_to_attr(odp_actions, tunnel);
3530     nl_msg_end_nested(odp_actions, offset);
3531 }
3532 \f
3533 /* The commit_odp_actions() function and its helpers. */
3534
3535 static void
3536 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3537                   const void *key, size_t key_size)
3538 {
3539     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3540     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3541     nl_msg_end_nested(odp_actions, offset);
3542 }
3543
3544 /* Masked set actions have a mask following the data within the netlink
3545  * attribute.  The unmasked bits in the data will be cleared as the data
3546  * is copied to the action. */
3547 void
3548 commit_masked_set_action(struct ofpbuf *odp_actions,
3549                          enum ovs_key_attr key_type,
3550                          const void *key_, const void *mask_, size_t key_size)
3551 {
3552     size_t offset = nl_msg_start_nested(odp_actions,
3553                                         OVS_ACTION_ATTR_SET_MASKED);
3554     char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3555     const char *key = key_, *mask = mask_;
3556
3557     memcpy(data + key_size, mask, key_size);
3558     /* Clear unmasked bits while copying. */
3559     while (key_size--) {
3560         *data++ = *key++ & *mask++;
3561     }
3562     nl_msg_end_nested(odp_actions, offset);
3563 }
3564
3565 void
3566 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3567                         struct ofpbuf *odp_actions)
3568 {
3569     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3570                       sizeof(pkt_mark));
3571 }
3572
3573 /* If any of the flow key data that ODP actions can modify are different in
3574  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3575  * 'odp_actions' that change the flow tunneling information in key from
3576  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3577  * same way.  In other words, operates the same as commit_odp_actions(), but
3578  * only on tunneling information. */
3579 void
3580 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3581                          struct ofpbuf *odp_actions)
3582 {
3583     /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3584     if (flow->tunnel.ip_dst) {
3585         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3586             return;
3587         }
3588         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3589         odp_put_tunnel_action(&base->tunnel, odp_actions);
3590     }
3591 }
3592
3593 static void
3594 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3595                              struct ofpbuf *odp_actions,
3596                              struct flow_wildcards *wc)
3597 {
3598     struct ovs_key_ethernet eth_key;
3599
3600     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3601         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3602         return;
3603     }
3604
3605     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3606     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3607
3608     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3609     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3610
3611     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3612     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3613
3614     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3615                       &eth_key, sizeof(eth_key));
3616 }
3617
3618 static void
3619 pop_vlan(struct flow *base,
3620          struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3621 {
3622     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3623
3624     if (base->vlan_tci & htons(VLAN_CFI)) {
3625         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3626         base->vlan_tci = 0;
3627     }
3628 }
3629
3630 static void
3631 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3632                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3633 {
3634     if (base->vlan_tci == vlan_tci) {
3635         return;
3636     }
3637
3638     pop_vlan(base, odp_actions, wc);
3639     if (vlan_tci & htons(VLAN_CFI)) {
3640         struct ovs_action_push_vlan vlan;
3641
3642         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3643         vlan.vlan_tci = vlan_tci;
3644         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3645                           &vlan, sizeof vlan);
3646     }
3647     base->vlan_tci = vlan_tci;
3648 }
3649
3650 static void
3651 commit_mpls_action(const struct flow *flow, struct flow *base,
3652                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3653 {
3654     int base_n = flow_count_mpls_labels(base, wc);
3655     int flow_n = flow_count_mpls_labels(flow, wc);
3656     int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
3657                                                  wc);
3658
3659     while (base_n > common_n) {
3660         if (base_n - 1 == common_n && flow_n > common_n) {
3661             /* If there is only one more LSE in base than there are common
3662              * between base and flow; and flow has at least one more LSE than
3663              * is common then the topmost LSE of base may be updated using
3664              * set */
3665             struct ovs_key_mpls mpls_key;
3666
3667             mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
3668             commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3669                               &mpls_key, sizeof mpls_key);
3670             flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
3671             common_n++;
3672         } else {
3673             /* Otherwise, if there more LSEs in base than are common between
3674              * base and flow then pop the topmost one. */
3675             ovs_be16 dl_type;
3676             bool popped;
3677
3678             /* If all the LSEs are to be popped and this is not the outermost
3679              * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
3680              * POP_MPLS action instead of flow->dl_type.
3681              *
3682              * This is because the POP_MPLS action requires its ethertype
3683              * argument to be an MPLS ethernet type but in this case
3684              * flow->dl_type will be a non-MPLS ethernet type.
3685              *
3686              * When the final POP_MPLS action occurs it use flow->dl_type and
3687              * the and the resulting packet will have the desired dl_type. */
3688             if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
3689                 dl_type = htons(ETH_TYPE_MPLS);
3690             } else {
3691                 dl_type = flow->dl_type;
3692             }
3693             nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
3694             popped = flow_pop_mpls(base, base_n, flow->dl_type, wc);
3695             ovs_assert(popped);
3696             base_n--;
3697         }
3698     }
3699
3700     /* If, after the above popping and setting, there are more LSEs in flow
3701      * than base then some LSEs need to be pushed. */
3702     while (base_n < flow_n) {
3703         struct ovs_action_push_mpls *mpls;
3704
3705         mpls = nl_msg_put_unspec_zero(odp_actions,
3706                                       OVS_ACTION_ATTR_PUSH_MPLS,
3707                                       sizeof *mpls);
3708         mpls->mpls_ethertype = flow->dl_type;
3709         mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
3710         flow_push_mpls(base, base_n, mpls->mpls_ethertype, wc);
3711         flow_set_mpls_lse(base, 0, mpls->mpls_lse);
3712         base_n++;
3713     }
3714 }
3715
3716 static void
3717 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3718                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3719 {
3720     struct ovs_key_ipv4 ipv4_key;
3721
3722     if (base->nw_src == flow->nw_src &&
3723         base->nw_dst == flow->nw_dst &&
3724         base->nw_tos == flow->nw_tos &&
3725         base->nw_ttl == flow->nw_ttl &&
3726         base->nw_frag == flow->nw_frag) {
3727         return;
3728     }
3729
3730     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3731     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3732     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3733     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3734     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3735     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3736
3737     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3738     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3739     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3740     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3741     ipv4_key.ipv4_proto = base->nw_proto;
3742     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3743
3744     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3745                       &ipv4_key, sizeof(ipv4_key));
3746 }
3747
3748 static void
3749 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3750                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3751 {
3752     struct ovs_key_ipv6 ipv6_key;
3753
3754     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3755         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3756         base->ipv6_label == flow->ipv6_label &&
3757         base->nw_tos == flow->nw_tos &&
3758         base->nw_ttl == flow->nw_ttl &&
3759         base->nw_frag == flow->nw_frag) {
3760         return;
3761     }
3762
3763     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3764     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3765     memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3766     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3767     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3768     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3769     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3770
3771     base->ipv6_src = flow->ipv6_src;
3772     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3773     base->ipv6_dst = flow->ipv6_dst;
3774     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3775
3776     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3777     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3778     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3779     ipv6_key.ipv6_proto = base->nw_proto;
3780     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3781
3782     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3783                       &ipv6_key, sizeof(ipv6_key));
3784 }
3785
3786 static enum slow_path_reason
3787 commit_set_arp_action(const struct flow *flow, struct flow *base,
3788                       struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3789 {
3790     struct ovs_key_arp arp_key;
3791
3792     if (base->nw_src == flow->nw_src &&
3793         base->nw_dst == flow->nw_dst &&
3794         base->nw_proto == flow->nw_proto &&
3795         eth_addr_equals(base->arp_sha, flow->arp_sha) &&
3796         eth_addr_equals(base->arp_tha, flow->arp_tha)) {
3797         return 0;
3798     }
3799
3800     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3801     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3802     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3803     memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
3804     memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
3805
3806     base->nw_src = flow->nw_src;
3807     base->nw_dst = flow->nw_dst;
3808     base->nw_proto = flow->nw_proto;
3809     memcpy(base->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3810     memcpy(base->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3811
3812     arp_key.arp_sip = base->nw_src;
3813     arp_key.arp_tip = base->nw_dst;
3814     arp_key.arp_op = htons(base->nw_proto);
3815     memcpy(arp_key.arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3816     memcpy(arp_key.arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3817
3818     commit_set_action(odp_actions, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
3819
3820     return SLOW_ACTION;
3821 }
3822
3823 static enum slow_path_reason
3824 commit_set_nw_action(const struct flow *flow, struct flow *base,
3825                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3826 {
3827     /* Check if 'flow' really has an L3 header. */
3828     if (!flow->nw_proto) {
3829         return 0;
3830     }
3831
3832     switch (ntohs(base->dl_type)) {
3833     case ETH_TYPE_IP:
3834         commit_set_ipv4_action(flow, base, odp_actions, wc);
3835         break;
3836
3837     case ETH_TYPE_IPV6:
3838         commit_set_ipv6_action(flow, base, odp_actions, wc);
3839         break;
3840
3841     case ETH_TYPE_ARP:
3842         return commit_set_arp_action(flow, base, odp_actions, wc);
3843     }
3844
3845     return 0;
3846 }
3847
3848 static void
3849 commit_set_port_action(const struct flow *flow, struct flow *base,
3850                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3851 {
3852     /* Check if 'flow' really has an L3 header. */
3853     if (!flow->nw_proto) {
3854         return;
3855     }
3856
3857     if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3858         return;
3859     }
3860
3861     if (base->tp_src == flow->tp_src &&
3862         base->tp_dst == flow->tp_dst) {
3863         return;
3864     }
3865
3866     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3867     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3868
3869     if (flow->nw_proto == IPPROTO_TCP) {
3870         struct ovs_key_tcp port_key;
3871
3872         port_key.tcp_src = base->tp_src = flow->tp_src;
3873         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3874
3875         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3876                           &port_key, sizeof(port_key));
3877
3878     } else if (flow->nw_proto == IPPROTO_UDP) {
3879         struct ovs_key_udp port_key;
3880
3881         port_key.udp_src = base->tp_src = flow->tp_src;
3882         port_key.udp_dst = base->tp_dst = flow->tp_dst;
3883
3884         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3885                           &port_key, sizeof(port_key));
3886     } else if (flow->nw_proto == IPPROTO_SCTP) {
3887         struct ovs_key_sctp port_key;
3888
3889         port_key.sctp_src = base->tp_src = flow->tp_src;
3890         port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3891
3892         commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3893                           &port_key, sizeof(port_key));
3894     }
3895 }
3896
3897 static void
3898 commit_set_priority_action(const struct flow *flow, struct flow *base,
3899                            struct ofpbuf *odp_actions,
3900                            struct flow_wildcards *wc)
3901 {
3902     if (base->skb_priority == flow->skb_priority) {
3903         return;
3904     }
3905
3906     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3907     base->skb_priority = flow->skb_priority;
3908
3909     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3910                       &base->skb_priority, sizeof(base->skb_priority));
3911 }
3912
3913 static void
3914 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3915                            struct ofpbuf *odp_actions,
3916                            struct flow_wildcards *wc)
3917 {
3918     if (base->pkt_mark == flow->pkt_mark) {
3919         return;
3920     }
3921
3922     memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3923     base->pkt_mark = flow->pkt_mark;
3924
3925     odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3926 }
3927
3928 /* If any of the flow key data that ODP actions can modify are different in
3929  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3930  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
3931  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
3932  * in addition to this function if needed.  Sets fields in 'wc' that are
3933  * used as part of the action.
3934  *
3935  * Returns a reason to force processing the flow's packets into the userspace
3936  * slow path, if there is one, otherwise 0. */
3937 enum slow_path_reason
3938 commit_odp_actions(const struct flow *flow, struct flow *base,
3939                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3940 {
3941     enum slow_path_reason slow;
3942
3943     commit_set_ether_addr_action(flow, base, odp_actions, wc);
3944     slow = commit_set_nw_action(flow, base, odp_actions, wc);
3945     commit_set_port_action(flow, base, odp_actions, wc);
3946     commit_mpls_action(flow, base, odp_actions, wc);
3947     commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
3948     commit_set_priority_action(flow, base, odp_actions, wc);
3949     commit_set_pkt_mark_action(flow, base, odp_actions, wc);
3950
3951     return slow;
3952 }