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