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