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