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