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