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