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