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