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