b9aba0d83d31216ca4284a3f3e92f7aaec6d28d6
[cascardo/ovs.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "dynamic-string.h"
30 #include "flow.h"
31 #include "netlink.h"
32 #include "ofpbuf.h"
33 #include "packets.h"
34 #include "simap.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(odp_util);
40
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42  * Since this is fairly non-specific for the OVS userspace components,
43  * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44  * interactions with the datapath.
45  */
46
47 /* The set of characters that may separate one action or one key attribute
48  * from another. */
49 static const char *delimiters = ", \t\r\n";
50
51 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
52                               struct ofpbuf *, struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a,
54                                 const struct nlattr *ma, struct ds *ds);
55
56 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
57  * 'type':
58  *
59  *   - For an action whose argument has a fixed length, returned that
60  *     nonnegative length in bytes.
61  *
62  *   - For an action with a variable-length argument, returns -2.
63  *
64  *   - For an invalid 'type', returns -1. */
65 static int
66 odp_action_len(uint16_t type)
67 {
68     if (type > OVS_ACTION_ATTR_MAX) {
69         return -1;
70     }
71
72     switch ((enum ovs_action_attr) type) {
73     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
74     case OVS_ACTION_ATTR_USERSPACE: return -2;
75     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
76     case OVS_ACTION_ATTR_POP_VLAN: return 0;
77     case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
78     case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
79     case OVS_ACTION_ATTR_SET: return -2;
80     case OVS_ACTION_ATTR_SAMPLE: return -2;
81
82     case OVS_ACTION_ATTR_UNSPEC:
83     case __OVS_ACTION_ATTR_MAX:
84         return -1;
85     }
86
87     return -1;
88 }
89
90 static const char *
91 ovs_key_attr_to_string(enum ovs_key_attr attr)
92 {
93     static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
94
95     switch (attr) {
96     case OVS_KEY_ATTR_UNSPEC: return "unspec";
97     case OVS_KEY_ATTR_ENCAP: return "encap";
98     case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
99     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
100     case OVS_KEY_ATTR_TUNNEL: return "tunnel";
101     case OVS_KEY_ATTR_IN_PORT: return "in_port";
102     case OVS_KEY_ATTR_ETHERNET: return "eth";
103     case OVS_KEY_ATTR_VLAN: return "vlan";
104     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
105     case OVS_KEY_ATTR_IPV4: return "ipv4";
106     case OVS_KEY_ATTR_IPV6: return "ipv6";
107     case OVS_KEY_ATTR_TCP: return "tcp";
108     case OVS_KEY_ATTR_UDP: return "udp";
109     case OVS_KEY_ATTR_ICMP: return "icmp";
110     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
111     case OVS_KEY_ATTR_ARP: return "arp";
112     case OVS_KEY_ATTR_ND: return "nd";
113     case OVS_KEY_ATTR_MPLS: return "mpls";
114
115     case __OVS_KEY_ATTR_MAX:
116     default:
117         snprintf(unknown_attr, sizeof unknown_attr, "key%u",
118                  (unsigned int) attr);
119         return unknown_attr;
120     }
121 }
122
123 static void
124 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
125 {
126     size_t len = nl_attr_get_size(a);
127
128     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
129     if (len) {
130         const uint8_t *unspec;
131         unsigned int i;
132
133         unspec = nl_attr_get(a);
134         for (i = 0; i < len; i++) {
135             ds_put_char(ds, i ? ' ': '(');
136             ds_put_format(ds, "%02x", unspec[i]);
137         }
138         ds_put_char(ds, ')');
139     }
140 }
141
142 static void
143 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
144 {
145     static const struct nl_policy ovs_sample_policy[] = {
146         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
147         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
148     };
149     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
150     double percentage;
151     const struct nlattr *nla_acts;
152     int len;
153
154     ds_put_cstr(ds, "sample");
155
156     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
157         ds_put_cstr(ds, "(error)");
158         return;
159     }
160
161     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
162                         UINT32_MAX;
163
164     ds_put_format(ds, "(sample=%.1f%%,", percentage);
165
166     ds_put_cstr(ds, "actions(");
167     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
168     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
169     format_odp_actions(ds, nla_acts, len);
170     ds_put_format(ds, "))");
171 }
172
173 static const char *
174 slow_path_reason_to_string(enum slow_path_reason reason)
175 {
176     switch (reason) {
177     case SLOW_CFM:
178         return "cfm";
179     case SLOW_LACP:
180         return "lacp";
181     case SLOW_STP:
182         return "stp";
183     case SLOW_CONTROLLER:
184         return "controller";
185     case __SLOW_MAX:
186     default:
187         return NULL;
188     }
189 }
190
191 static enum slow_path_reason
192 string_to_slow_path_reason(const char *string)
193 {
194     enum slow_path_reason i;
195
196     for (i = 1; i < __SLOW_MAX; i++) {
197         if (!strcmp(string, slow_path_reason_to_string(i))) {
198             return i;
199         }
200     }
201
202     return 0;
203 }
204
205 static int
206 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
207             uint32_t *res)
208 {
209     uint32_t result = 0;
210     int n = 0;
211
212     if (s[n] != '(') {
213         return -EINVAL;
214     }
215     n++;
216
217     while (s[n] != ')') {
218         unsigned long long int flags;
219         uint32_t bit;
220         int n0;
221
222         if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
223             n += n0 + (s[n + n0] == ',');
224             result |= flags;
225             continue;
226         }
227
228         for (bit = 1; bit; bit <<= 1) {
229             const char *name = bit_to_string(bit);
230             size_t len;
231
232             if (!name) {
233                 continue;
234             }
235
236             len = strlen(name);
237             if (!strncmp(s + n, name, len) &&
238                 (s[n + len] == ',' || s[n + len] == ')')) {
239                 result |= bit;
240                 n += len + (s[n + len] == ',');
241                 break;
242             }
243         }
244
245         if (!bit) {
246             return -EINVAL;
247         }
248     }
249     n++;
250
251     *res = result;
252     return n;
253 }
254
255 static void
256 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
257 {
258     static const struct nl_policy ovs_userspace_policy[] = {
259         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
260         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
261                                           .optional = true },
262     };
263     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
264     const struct nlattr *userdata_attr;
265
266     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
267         ds_put_cstr(ds, "userspace(error)");
268         return;
269     }
270
271     ds_put_format(ds, "userspace(pid=%"PRIu32,
272                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
273
274     userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
275
276     if (userdata_attr) {
277         const uint8_t *userdata = nl_attr_get(userdata_attr);
278         size_t userdata_len = nl_attr_get_size(userdata_attr);
279         bool userdata_unspec = true;
280         union user_action_cookie cookie;
281
282         if (userdata_len >= sizeof cookie.type
283             && userdata_len <= sizeof cookie) {
284
285             memset(&cookie, 0, sizeof cookie);
286             memcpy(&cookie, userdata, userdata_len);
287
288             userdata_unspec = false;
289
290             if (userdata_len == sizeof cookie.sflow
291                 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
292                 ds_put_format(ds, ",sFlow("
293                               "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
294                               vlan_tci_to_vid(cookie.sflow.vlan_tci),
295                               vlan_tci_to_pcp(cookie.sflow.vlan_tci),
296                               cookie.sflow.output);
297             } else if (userdata_len == sizeof cookie.slow_path
298                        && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
299                 const char *reason;
300                 reason = slow_path_reason_to_string(cookie.slow_path.reason);
301                 reason = reason ? reason : "";
302                 ds_put_format(ds, ",slow_path(%s)", reason);
303             } else if (userdata_len == sizeof cookie.flow_sample
304                        && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
305                 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
306                               ",collector_set_id=%"PRIu32
307                               ",obs_domain_id=%"PRIu32
308                               ",obs_point_id=%"PRIu32")",
309                               cookie.flow_sample.probability,
310                               cookie.flow_sample.collector_set_id,
311                               cookie.flow_sample.obs_domain_id,
312                               cookie.flow_sample.obs_point_id);
313             } else if (userdata_len == sizeof cookie.ipfix
314                        && cookie.type == USER_ACTION_COOKIE_IPFIX) {
315                 ds_put_format(ds, ",ipfix");
316             } else {
317                 userdata_unspec = true;
318             }
319         }
320
321         if (userdata_unspec) {
322             size_t i;
323             ds_put_format(ds, ",userdata(");
324             for (i = 0; i < userdata_len; i++) {
325                 ds_put_format(ds, "%02x", userdata[i]);
326             }
327             ds_put_char(ds, ')');
328         }
329     }
330
331     ds_put_char(ds, ')');
332 }
333
334 static void
335 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
336 {
337     ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
338                   vlan_tci_to_vid(vlan_tci),
339                   vlan_tci_to_pcp(vlan_tci));
340     if (!(vlan_tci & htons(VLAN_CFI))) {
341         ds_put_cstr(ds, ",cfi=0");
342     }
343 }
344
345 static void
346 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
347 {
348     ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
349                   mpls_lse_to_label(mpls_lse),
350                   mpls_lse_to_tc(mpls_lse),
351                   mpls_lse_to_ttl(mpls_lse),
352                   mpls_lse_to_bos(mpls_lse));
353 }
354
355 static void
356 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
357             const struct ovs_key_mpls *mpls_mask)
358 {
359     ovs_be32 key = mpls_key->mpls_top_lse;
360
361     if (mpls_mask == NULL) {
362         format_mpls_lse(ds, key);
363     } else {
364         ovs_be32 mask = mpls_mask->mpls_top_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 }
373
374 static void
375 format_odp_action(struct ds *ds, const struct nlattr *a)
376 {
377     int expected_len;
378     enum ovs_action_attr type = nl_attr_type(a);
379     const struct ovs_action_push_vlan *vlan;
380
381     expected_len = odp_action_len(nl_attr_type(a));
382     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
383         ds_put_format(ds, "bad length %zu, expected %d for: ",
384                       nl_attr_get_size(a), expected_len);
385         format_generic_odp_action(ds, a);
386         return;
387     }
388
389     switch (type) {
390     case OVS_ACTION_ATTR_OUTPUT:
391         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
392         break;
393     case OVS_ACTION_ATTR_USERSPACE:
394         format_odp_userspace_action(ds, a);
395         break;
396     case OVS_ACTION_ATTR_SET:
397         ds_put_cstr(ds, "set(");
398         format_odp_key_attr(nl_attr_get(a), NULL, ds);
399         ds_put_cstr(ds, ")");
400         break;
401     case OVS_ACTION_ATTR_PUSH_VLAN:
402         vlan = nl_attr_get(a);
403         ds_put_cstr(ds, "push_vlan(");
404         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
405             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
406         }
407         format_vlan_tci(ds, vlan->vlan_tci);
408         ds_put_char(ds, ')');
409         break;
410     case OVS_ACTION_ATTR_POP_VLAN:
411         ds_put_cstr(ds, "pop_vlan");
412         break;
413     case OVS_ACTION_ATTR_PUSH_MPLS: {
414         const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
415         ds_put_cstr(ds, "push_mpls(");
416         format_mpls_lse(ds, mpls->mpls_lse);
417         ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
418         break;
419     }
420     case OVS_ACTION_ATTR_POP_MPLS: {
421         ovs_be16 ethertype = nl_attr_get_be16(a);
422         ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
423         break;
424     }
425     case OVS_ACTION_ATTR_SAMPLE:
426         format_odp_sample_action(ds, a);
427         break;
428     case OVS_ACTION_ATTR_UNSPEC:
429     case __OVS_ACTION_ATTR_MAX:
430     default:
431         format_generic_odp_action(ds, a);
432         break;
433     }
434 }
435
436 void
437 format_odp_actions(struct ds *ds, const struct nlattr *actions,
438                    size_t actions_len)
439 {
440     if (actions_len) {
441         const struct nlattr *a;
442         unsigned int left;
443
444         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
445             if (a != actions) {
446                 ds_put_char(ds, ',');
447             }
448             format_odp_action(ds, a);
449         }
450         if (left) {
451             int i;
452
453             if (left == actions_len) {
454                 ds_put_cstr(ds, "<empty>");
455             }
456             ds_put_format(ds, ",***%u leftover bytes*** (", left);
457             for (i = 0; i < left; i++) {
458                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
459             }
460             ds_put_char(ds, ')');
461         }
462     } else {
463         ds_put_cstr(ds, "drop");
464     }
465 }
466
467 static int
468 parse_odp_action(const char *s, const struct simap *port_names,
469                  struct ofpbuf *actions)
470 {
471     /* Many of the sscanf calls in this function use oversized destination
472      * fields because some sscanf() implementations truncate the range of %i
473      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
474      * value of 0x7fff.  The other alternatives are to allow only a single
475      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
476      * parsers.
477      *
478      * The tun_id parser has to use an alternative approach because there is no
479      * type larger than 64 bits. */
480
481     {
482         unsigned long long int port;
483         int n = -1;
484
485         if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
486             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
487             return n;
488         }
489     }
490
491     if (port_names) {
492         int len = strcspn(s, delimiters);
493         struct simap_node *node;
494
495         node = simap_find_len(port_names, s, len);
496         if (node) {
497             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
498             return len;
499         }
500     }
501
502     {
503         unsigned long long int pid;
504         unsigned long long int output;
505         unsigned long long int probability;
506         unsigned long long int collector_set_id;
507         unsigned long long int obs_domain_id;
508         unsigned long long int obs_point_id;
509         int vid, pcp;
510         int n = -1;
511
512         if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
513             odp_put_userspace_action(pid, NULL, 0, actions);
514             return n;
515         } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
516                           "pcp=%i,output=%lli))%n",
517                           &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
518             union user_action_cookie cookie;
519             uint16_t tci;
520
521             tci = vid | (pcp << VLAN_PCP_SHIFT);
522             if (tci) {
523                 tci |= VLAN_CFI;
524             }
525
526             cookie.type = USER_ACTION_COOKIE_SFLOW;
527             cookie.sflow.vlan_tci = htons(tci);
528             cookie.sflow.output = output;
529             odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
530                                      actions);
531             return n;
532         } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0
533                    && n > 0) {
534             union user_action_cookie cookie;
535             char reason[32];
536
537             if (s[n] == ')' && s[n + 1] == ')') {
538                 reason[0] = '\0';
539                 n += 2;
540             } else if (sscanf(s + n, "%31[^)]))", reason) > 0) {
541                 n += strlen(reason) + 2;
542             } else {
543                 return -EINVAL;
544             }
545
546             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
547             cookie.slow_path.unused = 0;
548             cookie.slow_path.reason = string_to_slow_path_reason(reason);
549
550             if (reason[0] && !cookie.slow_path.reason) {
551                 return -EINVAL;
552             }
553
554             odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
555                                      actions);
556             return n;
557         } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
558                           "collector_set_id=%lli,obs_domain_id=%lli,"
559                           "obs_point_id=%lli))%n",
560                           &pid, &probability, &collector_set_id,
561                           &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
562             union user_action_cookie cookie;
563
564             cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
565             cookie.flow_sample.probability = probability;
566             cookie.flow_sample.collector_set_id = collector_set_id;
567             cookie.flow_sample.obs_domain_id = obs_domain_id;
568             cookie.flow_sample.obs_point_id = obs_point_id;
569             odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
570                                      actions);
571             return n;
572         } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
573                    && n > 0) {
574             union user_action_cookie cookie;
575
576             cookie.type = USER_ACTION_COOKIE_IPFIX;
577             odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
578                                      actions);
579             return n;
580         } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
581                    && n > 0) {
582             struct ofpbuf buf;
583             char *end;
584
585             ofpbuf_init(&buf, 16);
586             end = ofpbuf_put_hex(&buf, &s[n], NULL);
587             if (end[0] == ')' && end[1] == ')') {
588                 odp_put_userspace_action(pid, buf.data, buf.size, actions);
589                 ofpbuf_uninit(&buf);
590                 return (end + 2) - s;
591             }
592         }
593     }
594
595     if (!strncmp(s, "set(", 4)) {
596         size_t start_ofs;
597         int retval;
598
599         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
600         retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
601         if (retval < 0) {
602             return retval;
603         }
604         if (s[retval + 4] != ')') {
605             return -EINVAL;
606         }
607         nl_msg_end_nested(actions, start_ofs);
608         return retval + 5;
609     }
610
611     {
612         struct ovs_action_push_vlan push;
613         int tpid = ETH_TYPE_VLAN;
614         int vid, pcp;
615         int cfi = 1;
616         int n = -1;
617
618         if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
619              && n > 0)
620             || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
621                        &vid, &pcp, &cfi, &n) > 0 && n > 0)
622             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
623                        &tpid, &vid, &pcp, &n) > 0 && n > 0)
624             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
625                        &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
626             push.vlan_tpid = htons(tpid);
627             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
628                                   | (pcp << VLAN_PCP_SHIFT)
629                                   | (cfi ? VLAN_CFI : 0));
630             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
631                               &push, sizeof push);
632
633             return n;
634         }
635     }
636
637     if (!strncmp(s, "pop_vlan", 8)) {
638         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
639         return 8;
640     }
641
642     {
643         double percentage;
644         int n = -1;
645
646         if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
647             && percentage >= 0. && percentage <= 100.0
648             && n > 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 sizeof(struct ovs_key_mpls);
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_UDP: return sizeof(struct ovs_key_udp);
744     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
745     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
746     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
747     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
748
749     case OVS_KEY_ATTR_UNSPEC:
750     case __OVS_KEY_ATTR_MAX:
751         return -1;
752     }
753
754     return -1;
755 }
756
757 static void
758 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
759 {
760     size_t len = nl_attr_get_size(a);
761     if (len) {
762         const uint8_t *unspec;
763         unsigned int i;
764
765         unspec = nl_attr_get(a);
766         for (i = 0; i < len; i++) {
767             if (i) {
768                 ds_put_char(ds, ' ');
769             }
770             ds_put_format(ds, "%02x", unspec[i]);
771         }
772     }
773 }
774
775 static const char *
776 ovs_frag_type_to_string(enum ovs_frag_type type)
777 {
778     switch (type) {
779     case OVS_FRAG_TYPE_NONE:
780         return "no";
781     case OVS_FRAG_TYPE_FIRST:
782         return "first";
783     case OVS_FRAG_TYPE_LATER:
784         return "later";
785     case __OVS_FRAG_TYPE_MAX:
786     default:
787         return "<error>";
788     }
789 }
790
791 static int
792 tunnel_key_attr_len(int type)
793 {
794     switch (type) {
795     case OVS_TUNNEL_KEY_ATTR_ID: return 8;
796     case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
797     case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
798     case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
799     case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
800     case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
801     case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
802     case __OVS_TUNNEL_KEY_ATTR_MAX:
803         return -1;
804     }
805     return -1;
806 }
807
808 static enum odp_key_fitness
809 tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
810 {
811     unsigned int left;
812     const struct nlattr *a;
813     bool ttl = false;
814     bool unknown = false;
815
816     NL_NESTED_FOR_EACH(a, left, attr) {
817         uint16_t type = nl_attr_type(a);
818         size_t len = nl_attr_get_size(a);
819         int expected_len = tunnel_key_attr_len(type);
820
821         if (len != expected_len && expected_len >= 0) {
822             return ODP_FIT_ERROR;
823         }
824
825         switch (type) {
826         case OVS_TUNNEL_KEY_ATTR_ID:
827             tun->tun_id = nl_attr_get_be64(a);
828             tun->flags |= FLOW_TNL_F_KEY;
829             break;
830         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
831             tun->ip_src = nl_attr_get_be32(a);
832             break;
833         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
834             tun->ip_dst = nl_attr_get_be32(a);
835             break;
836         case OVS_TUNNEL_KEY_ATTR_TOS:
837             tun->ip_tos = nl_attr_get_u8(a);
838             break;
839         case OVS_TUNNEL_KEY_ATTR_TTL:
840             tun->ip_ttl = nl_attr_get_u8(a);
841             ttl = true;
842             break;
843         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
844             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
845             break;
846         case OVS_TUNNEL_KEY_ATTR_CSUM:
847             tun->flags |= FLOW_TNL_F_CSUM;
848             break;
849         default:
850             /* Allow this to show up as unexpected, if there are unknown
851              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
852             unknown = true;
853             break;
854         }
855     }
856
857     if (!ttl) {
858         return ODP_FIT_ERROR;
859     }
860     if (unknown) {
861             return ODP_FIT_TOO_MUCH;
862     }
863     return ODP_FIT_PERFECT;
864 }
865
866 static void
867 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
868 {
869     size_t tun_key_ofs;
870
871     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
872
873     if (tun_key->flags & FLOW_TNL_F_KEY) {
874         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
875     }
876     if (tun_key->ip_src) {
877         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
878     }
879     if (tun_key->ip_dst) {
880         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
881     }
882     if (tun_key->ip_tos) {
883         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
884     }
885     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
886     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
887         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
888     }
889     if (tun_key->flags & FLOW_TNL_F_CSUM) {
890         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
891     }
892
893     nl_msg_end_nested(a, tun_key_ofs);
894 }
895
896 static bool
897 odp_mask_attr_is_exact(const struct nlattr *ma)
898 {
899     bool is_exact = false;
900     enum ovs_key_attr attr = nl_attr_type(ma);
901
902     if (attr == OVS_KEY_ATTR_TUNNEL) {
903         /* XXX this is a hack for now. Should change
904          * the exact match dection to per field
905          * instead of per attribute.
906          */
907         struct flow_tnl tun_mask;
908         memset(&tun_mask, 0, sizeof tun_mask);
909         tun_key_from_attr(ma, &tun_mask);
910         if (tun_mask.flags == (FLOW_TNL_F_KEY
911                                | FLOW_TNL_F_DONT_FRAGMENT
912                                | FLOW_TNL_F_CSUM)) {
913             /* The flags are exact match, check the remaining fields. */
914             tun_mask.flags = 0xffff;
915             is_exact = is_all_ones((uint8_t *)&tun_mask,
916                                    offsetof(struct flow_tnl, ip_ttl));
917         }
918     } else {
919         is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
920     }
921
922     return is_exact;
923 }
924
925
926 static void
927 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
928                     struct ds *ds)
929 {
930     struct flow_tnl tun_key;
931     enum ovs_key_attr attr = nl_attr_type(a);
932     int expected_len;
933
934     if (ma && odp_mask_attr_is_exact(ma)) {
935         ma = NULL;
936     }
937
938     ds_put_cstr(ds, ovs_key_attr_to_string(attr));
939
940     {
941         expected_len = odp_flow_key_attr_len(nl_attr_type(a));
942         if (expected_len != -2) {
943             bool bad_key_len = nl_attr_get_size(a) != expected_len;
944             bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
945
946             if (bad_key_len || bad_mask_len) {
947                 if (bad_key_len) {
948                     ds_put_format(ds, "(bad key length %zu, expected %d)(",
949                                   nl_attr_get_size(a),
950                                   odp_flow_key_attr_len(nl_attr_type(a)));
951                 }
952                 format_generic_odp_key(a, ds);
953                 if (bad_mask_len) {
954                     ds_put_char(ds, '/');
955                     ds_put_format(ds, "(bad mask length %zu, expected %d)(",
956                                   nl_attr_get_size(ma),
957                                   odp_flow_key_attr_len(nl_attr_type(ma)));
958                 }
959                 format_generic_odp_key(ma, ds);
960                 ds_put_char(ds, ')');
961                 return;
962             }
963         }
964     }
965
966     ds_put_char(ds, '(');
967     switch (attr) {
968     case OVS_KEY_ATTR_ENCAP:
969         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
970             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
971                             nl_attr_get(ma), nl_attr_get_size(ma), ds);
972         } else if (nl_attr_get_size(a)) {
973             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds);
974         }
975         break;
976
977     case OVS_KEY_ATTR_PRIORITY:
978     case OVS_KEY_ATTR_SKB_MARK:
979         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
980         if (ma) {
981             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
982         }
983         break;
984
985     case OVS_KEY_ATTR_TUNNEL:
986         memset(&tun_key, 0, sizeof tun_key);
987         if (tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
988             ds_put_format(ds, "error");
989         } else if (ma) {
990             struct flow_tnl tun_mask;
991
992             memset(&tun_mask, 0, sizeof tun_mask);
993             tun_key_from_attr(ma, &tun_mask);
994             ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
995                           ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
996                           ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
997                           ",flags(",
998                           ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
999                           IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1000                           IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1001                           tun_key.ip_tos, tun_mask.ip_tos,
1002                           tun_key.ip_ttl, tun_mask.ip_ttl);
1003
1004             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1005
1006             /* XXX This code is correct, but enabling it would break the unit
1007                test. Disable it for now until the input parser is fixed.
1008
1009                 ds_put_char(ds, '/');
1010                 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1011             */
1012             ds_put_char(ds, ')');
1013         } else {
1014             ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1015                           "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1016                           ntohll(tun_key.tun_id),
1017                           IP_ARGS(tun_key.ip_src),
1018                           IP_ARGS(tun_key.ip_dst),
1019                           tun_key.ip_tos, tun_key.ip_ttl);
1020
1021             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1022             ds_put_char(ds, ')');
1023         }
1024         break;
1025
1026     case OVS_KEY_ATTR_IN_PORT:
1027         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1028         if (ma) {
1029             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1030         }
1031         break;
1032
1033     case OVS_KEY_ATTR_ETHERNET:
1034         if (ma) {
1035             const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1036             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1037
1038             ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1039                           ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1040                           ETH_ADDR_ARGS(eth_key->eth_src),
1041                           ETH_ADDR_ARGS(eth_mask->eth_src),
1042                           ETH_ADDR_ARGS(eth_key->eth_dst),
1043                           ETH_ADDR_ARGS(eth_mask->eth_dst));
1044         } else {
1045             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1046
1047             ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1048                           ETH_ADDR_ARGS(eth_key->eth_src),
1049                           ETH_ADDR_ARGS(eth_key->eth_dst));
1050         }
1051         break;
1052
1053     case OVS_KEY_ATTR_VLAN:
1054         {
1055             ovs_be16 vlan_tci = nl_attr_get_be16(a);
1056             if (ma) {
1057                 ovs_be16 mask = nl_attr_get_be16(ma);
1058                 ds_put_format(ds, "vid=%"PRIu16"/%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1059                               vlan_tci_to_vid(vlan_tci),
1060                               vlan_tci_to_vid(mask),
1061                               vlan_tci_to_pcp(vlan_tci),
1062                               vlan_tci_to_pcp(mask),
1063                               vlan_tci_to_cfi(vlan_tci),
1064                               vlan_tci_to_cfi(mask));
1065             } else {
1066                 format_vlan_tci(ds, vlan_tci);
1067             }
1068         }
1069         break;
1070
1071     case OVS_KEY_ATTR_MPLS: {
1072         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1073         const struct ovs_key_mpls *mpls_mask = NULL;
1074         if (ma) {
1075             mpls_mask = nl_attr_get(ma);
1076         }
1077         format_mpls(ds, mpls_key, mpls_mask);
1078         break;
1079     }
1080
1081     case OVS_KEY_ATTR_ETHERTYPE:
1082         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1083         if (ma) {
1084             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1085         }
1086         break;
1087
1088     case OVS_KEY_ATTR_IPV4:
1089         if (ma) {
1090             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1091             const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1092
1093             ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1094                           ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1095                           ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1096                           IP_ARGS(ipv4_key->ipv4_src),
1097                           IP_ARGS(ipv4_mask->ipv4_src),
1098                           IP_ARGS(ipv4_key->ipv4_dst),
1099                           IP_ARGS(ipv4_mask->ipv4_dst),
1100                           ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1101                           ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1102                           ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1103                           ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1104                           ipv4_mask->ipv4_frag);
1105         } else {
1106             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1107
1108             ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1109                           ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1110                           IP_ARGS(ipv4_key->ipv4_src),
1111                           IP_ARGS(ipv4_key->ipv4_dst),
1112                           ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1113                           ipv4_key->ipv4_ttl,
1114                           ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1115         }
1116         break;
1117
1118     case OVS_KEY_ATTR_IPV6:
1119         if (ma) {
1120             const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1121             char src_str[INET6_ADDRSTRLEN];
1122             char dst_str[INET6_ADDRSTRLEN];
1123             char src_mask[INET6_ADDRSTRLEN];
1124             char dst_mask[INET6_ADDRSTRLEN];
1125
1126             ipv6_key = nl_attr_get(a);
1127             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1128             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1129
1130             ipv6_mask = nl_attr_get(ma);
1131             inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1132             inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1133
1134             ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1135                           ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1136                           ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1137                           src_str, src_mask, dst_str, dst_mask,
1138                           ntohl(ipv6_key->ipv6_label),
1139                           ntohl(ipv6_mask->ipv6_label),
1140                           ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1141                           ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1142                           ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1143                           ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1144                           ipv6_mask->ipv6_frag);
1145         } else {
1146             const struct ovs_key_ipv6 *ipv6_key;
1147             char src_str[INET6_ADDRSTRLEN];
1148             char dst_str[INET6_ADDRSTRLEN];
1149
1150             ipv6_key = nl_attr_get(a);
1151             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1152             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1153
1154             ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1155                           ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1156                           src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1157                           ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1158                           ipv6_key->ipv6_hlimit,
1159                           ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1160         }
1161         break;
1162
1163     case OVS_KEY_ATTR_TCP:
1164         if (ma) {
1165             const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1166             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1167
1168             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1169                           ",dst=%"PRIu16"/%#"PRIx16,
1170                           ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1171                           ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1172         } else {
1173             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1174
1175             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1176                           ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1177         }
1178         break;
1179
1180     case OVS_KEY_ATTR_UDP:
1181         if (ma) {
1182             const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1183             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1184
1185             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1186                           ",dst=%"PRIu16"/%#"PRIx16,
1187                           ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1188                           ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1189         } else {
1190             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1191
1192             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1193                           ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1194         }
1195         break;
1196
1197     case OVS_KEY_ATTR_ICMP:
1198         if (ma) {
1199             const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1200             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1201
1202             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1203                           icmp_key->icmp_type, icmp_mask->icmp_type,
1204                           icmp_key->icmp_code, icmp_mask->icmp_code);
1205         } else {
1206             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1207
1208             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1209                           icmp_key->icmp_type, icmp_key->icmp_code);
1210         }
1211         break;
1212
1213     case OVS_KEY_ATTR_ICMPV6:
1214         if (ma) {
1215             const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1216             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1217
1218             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1219                           icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1220                           icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1221         } else {
1222             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1223
1224             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1225                           icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1226         }
1227         break;
1228
1229     case OVS_KEY_ATTR_ARP:
1230         if (ma) {
1231             const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1232             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1233
1234             ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1235                           ",op=%"PRIu16"/%#"PRIx16
1236                           ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1237                           ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1238                           IP_ARGS(arp_key->arp_sip),
1239                           IP_ARGS(arp_mask->arp_sip),
1240                           IP_ARGS(arp_key->arp_tip),
1241                           IP_ARGS(arp_mask->arp_tip),
1242                           ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1243                           ETH_ADDR_ARGS(arp_key->arp_sha),
1244                           ETH_ADDR_ARGS(arp_mask->arp_sha),
1245                           ETH_ADDR_ARGS(arp_key->arp_tha),
1246                           ETH_ADDR_ARGS(arp_mask->arp_tha));
1247         } else {
1248             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1249
1250             ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1251                           "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1252                           IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1253                           ntohs(arp_key->arp_op),
1254                           ETH_ADDR_ARGS(arp_key->arp_sha),
1255                           ETH_ADDR_ARGS(arp_key->arp_tha));
1256         }
1257         break;
1258
1259     case OVS_KEY_ATTR_ND: {
1260         const struct ovs_key_nd *nd_key, *nd_mask;
1261         char target[INET6_ADDRSTRLEN];
1262
1263         nd_key = nl_attr_get(a);
1264         nd_mask = ma ? nl_attr_get(ma) : NULL;
1265
1266         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1267         ds_put_format(ds, "target=%s", target);
1268         if (nd_mask) {
1269             inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1270             ds_put_format(ds, "/%s", target);
1271         }
1272
1273         if (!eth_addr_is_zero(nd_key->nd_sll)) {
1274             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1275                           ETH_ADDR_ARGS(nd_key->nd_sll));
1276             if (nd_mask) {
1277                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1278                               ETH_ADDR_ARGS(nd_mask->nd_sll));
1279             }
1280         }
1281         if (!eth_addr_is_zero(nd_key->nd_tll)) {
1282             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1283                           ETH_ADDR_ARGS(nd_key->nd_tll));
1284             if (nd_mask) {
1285                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1286                               ETH_ADDR_ARGS(nd_mask->nd_tll));
1287             }
1288         }
1289         break;
1290     }
1291
1292     case OVS_KEY_ATTR_UNSPEC:
1293     case __OVS_KEY_ATTR_MAX:
1294     default:
1295         format_generic_odp_key(a, ds);
1296         if (ma) {
1297             ds_put_char(ds, '/');
1298             format_generic_odp_key(ma, ds);
1299         }
1300         break;
1301     }
1302     ds_put_char(ds, ')');
1303 }
1304
1305 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1306  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1307  * 'mask_len' bytes of 'mask' which apply to 'key'. */
1308 void
1309 odp_flow_format(const struct nlattr *key, size_t key_len,
1310                 const struct nlattr *mask, size_t mask_len,
1311                 struct ds *ds)
1312 {
1313     if (key_len) {
1314         const struct nlattr *a;
1315         unsigned int left;
1316         bool has_ethtype_key = false;
1317         const struct nlattr *ma = NULL;
1318
1319         NL_ATTR_FOR_EACH (a, left, key, key_len) {
1320             if (a != key) {
1321                 ds_put_char(ds, ',');
1322             }
1323             if (nl_attr_type(a) == OVS_KEY_ATTR_ETHERTYPE) {
1324                 has_ethtype_key = true;
1325             }
1326             if (mask && mask_len) {
1327                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1328             }
1329             format_odp_key_attr(a, ma, ds);
1330         }
1331         if (left) {
1332             int i;
1333             
1334             if (left == key_len) {
1335                 ds_put_cstr(ds, "<empty>");
1336             }
1337             ds_put_format(ds, ",***%u leftover bytes*** (", left);
1338             for (i = 0; i < left; i++) {
1339                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1340             }
1341             ds_put_char(ds, ')');
1342         }
1343         if (!has_ethtype_key) {
1344             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1345             if (ma) {
1346                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1347                               ntohs(nl_attr_get_be16(ma)));
1348             }
1349         }
1350     } else {
1351         ds_put_cstr(ds, "<empty>");
1352     }
1353 }
1354
1355 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1356  * OVS_KEY_ATTR_* attributes in 'key'. */
1357 void
1358 odp_flow_key_format(const struct nlattr *key,
1359                     size_t key_len, struct ds *ds)
1360 {
1361     odp_flow_format(key, key_len, NULL, 0, ds);
1362 }
1363
1364 static int
1365 put_nd_key(int n, const char *nd_target_s,
1366            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
1367 {
1368     struct ovs_key_nd nd_key;
1369
1370     memset(&nd_key, 0, sizeof nd_key);
1371     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1372         return -EINVAL;
1373     }
1374     if (nd_sll) {
1375         memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
1376     }
1377     if (nd_tll) {
1378         memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
1379     }
1380     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
1381     return n;
1382 }
1383
1384 static bool
1385 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1386 {
1387     if (!strcasecmp(s, "no")) {
1388         *type = OVS_FRAG_TYPE_NONE;
1389     } else if (!strcasecmp(s, "first")) {
1390         *type = OVS_FRAG_TYPE_FIRST;
1391     } else if (!strcasecmp(s, "later")) {
1392         *type = OVS_FRAG_TYPE_LATER;
1393     } else {
1394         return false;
1395     }
1396     return true;
1397 }
1398
1399 static ovs_be32
1400 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1401 {
1402     return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1403                   (mpls_tc << MPLS_TC_SHIFT)       |
1404                   (mpls_ttl << MPLS_TTL_SHIFT)     |
1405                   (mpls_bos << MPLS_BOS_SHIFT)));
1406 }
1407
1408 static int
1409 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1410                         struct ofpbuf *key, struct ofpbuf *mask)
1411 {
1412     /* Many of the sscanf calls in this function use oversized destination
1413      * fields because some sscanf() implementations truncate the range of %i
1414      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1415      * value of 0x7fff.  The other alternatives are to allow only a single
1416      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1417      * parsers.
1418      *
1419      * The tun_id parser has to use an alternative approach because there is no
1420      * type larger than 64 bits. */
1421
1422     {
1423         unsigned long long int priority;
1424         unsigned long long int priority_mask;
1425         int n = -1;
1426
1427         if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1428                    &priority_mask, &n) > 0 && n > 0) {
1429             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1430             nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1431             return n;
1432         } else if (sscanf(s, "skb_priority(%lli)%n",
1433                           &priority, &n) > 0 && n > 0) {
1434             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1435             if (mask) {
1436                 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1437             }
1438             return n;
1439         }
1440     }
1441
1442     {
1443         unsigned long long int mark;
1444         unsigned long long int mark_mask;
1445         int n = -1;
1446
1447         if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1448                    &mark_mask, &n) > 0 && n > 0) {
1449             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1450             nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1451             return n;
1452         } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1453             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1454             if (mask) {
1455                 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1456             }
1457             return n;
1458         }
1459     }
1460
1461     {
1462         char tun_id_s[32];
1463         int tos, tos_mask, ttl, ttl_mask;
1464         struct flow_tnl tun_key, tun_key_mask;
1465         unsigned long long tun_id_mask;
1466         int n = -1;
1467
1468         if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1469                    "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1470                    "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1471                    tun_id_s, &tun_id_mask,
1472                    IP_SCAN_ARGS(&tun_key.ip_src),
1473                    IP_SCAN_ARGS(&tun_key_mask.ip_src),
1474                    IP_SCAN_ARGS(&tun_key.ip_dst),
1475                    IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1476                    &tos, &tos_mask, &ttl, &ttl_mask,
1477                    &n) > 0 && n > 0) {
1478             int res;
1479             uint32_t flags;
1480
1481             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1482             tun_key_mask.tun_id = htonll(tun_id_mask);
1483             tun_key.ip_tos = tos;
1484             tun_key_mask.ip_tos = tos_mask;
1485             tun_key.ip_ttl = ttl;
1486             tun_key_mask.ip_ttl = ttl_mask;
1487             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1488             tun_key.flags = flags;
1489             tun_key_mask.flags = UINT16_MAX;
1490
1491             if (res < 0) {
1492                 return res;
1493             }
1494             n += res;
1495             if (s[n] != ')') {
1496                 return -EINVAL;
1497             }
1498             n++;
1499             tun_key_to_attr(key, &tun_key);
1500             if (mask) {
1501                 tun_key_to_attr(mask, &tun_key_mask);
1502             }
1503             return n;
1504         } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1505                    "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1506                    ",tos=%i,ttl=%i,flags%n", tun_id_s,
1507                     IP_SCAN_ARGS(&tun_key.ip_src),
1508                     IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1509                     &n) > 0 && n > 0) {
1510             int res;
1511             uint32_t flags;
1512
1513             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1514             tun_key.ip_tos = tos;
1515             tun_key.ip_ttl = ttl;
1516             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1517             tun_key.flags = flags;
1518
1519             if (res < 0) {
1520                 return res;
1521             }
1522             n += res;
1523             if (s[n] != ')') {
1524                 return -EINVAL;
1525             }
1526             n++;
1527             tun_key_to_attr(key, &tun_key);
1528
1529             if (mask) {
1530                 memset(&tun_key, 0xff, sizeof tun_key);
1531                 tun_key_to_attr(mask, &tun_key);
1532             }
1533             return n;
1534         }
1535     }
1536
1537     {
1538         unsigned long long int in_port;
1539         unsigned long long int in_port_mask;
1540         int n = -1;
1541
1542         if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1543                    &in_port_mask, &n) > 0 && n > 0) {
1544             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1545             nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1546             return n;
1547         } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1548             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1549             if (mask) {
1550                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1551             }
1552             return n;
1553         }
1554     }
1555
1556
1557     if (port_names && !strncmp(s, "in_port(", 8)) {
1558         const char *name;
1559         const struct simap_node *node;
1560         int name_len;
1561
1562         name = s + 8;
1563         name_len = strcspn(s, ")");
1564         node = simap_find_len(port_names, name, name_len);
1565         if (node) {
1566             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1567
1568             if (mask) {
1569                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1570             }
1571             return 8 + name_len + 1;
1572         }
1573     }
1574
1575     {
1576         struct ovs_key_ethernet eth_key;
1577         struct ovs_key_ethernet eth_key_mask;
1578         int n = -1;
1579
1580         if (mask && sscanf(s,
1581                    "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1582                         "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1583                 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1584                 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1585                 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1586                 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1587
1588             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1589                               &eth_key, sizeof eth_key);
1590             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1591                               &eth_key_mask, sizeof eth_key_mask);
1592             return n;
1593         } else if (sscanf(s,
1594                    "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1595                    ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1596                    ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1597             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1598                               &eth_key, sizeof eth_key);
1599
1600             if (mask) {
1601                 memset(&eth_key, 0xff, sizeof eth_key);
1602                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1603                               &eth_key, sizeof eth_key);
1604             }
1605             return n;
1606         }
1607     }
1608
1609     {
1610         uint16_t vid, vid_mask;
1611         int pcp, pcp_mask;
1612         int cfi, cfi_mask;
1613         int n = -1;
1614
1615         if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1616                             &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1617             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1618                             htons((vid << VLAN_VID_SHIFT) |
1619                                   (pcp << VLAN_PCP_SHIFT) |
1620                                   VLAN_CFI));
1621             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1622                             htons((vid_mask << VLAN_VID_SHIFT) |
1623                                   (pcp_mask << VLAN_PCP_SHIFT) |
1624                                   (1 << VLAN_CFI_SHIFT)));
1625             return n;
1626         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1627                            &vid, &pcp, &n) > 0 && n > 0)) {
1628             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1629                             htons((vid << VLAN_VID_SHIFT) |
1630                                   (pcp << VLAN_PCP_SHIFT) |
1631                                   VLAN_CFI));
1632             if (mask) {
1633                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1634             }
1635             return n;
1636         } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1637                                    &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1638             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1639                             htons((vid << VLAN_VID_SHIFT) |
1640                                   (pcp << VLAN_PCP_SHIFT) |
1641                                   (cfi ? VLAN_CFI : 0)));
1642             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1643                             htons((vid_mask << VLAN_VID_SHIFT) |
1644                                   (pcp_mask << VLAN_PCP_SHIFT) |
1645                                   (cfi_mask << VLAN_CFI_SHIFT)));
1646             return n;
1647         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1648                            &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1649             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1650                             htons((vid << VLAN_VID_SHIFT) |
1651                                   (pcp << VLAN_PCP_SHIFT) |
1652                                   (cfi ? VLAN_CFI : 0)));
1653             if (mask) {
1654                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1655             }
1656             return n;
1657         }
1658     }
1659
1660     {
1661         int eth_type;
1662         int eth_type_mask;
1663         int n = -1;
1664
1665         if (mask && sscanf(s, "eth_type(%i/%i)%n",
1666                    &eth_type, &eth_type_mask, &n) > 0 && n > 0) {
1667             if (eth_type != 0) {
1668                 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1669             }
1670             nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1671             return n;
1672         } else if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1673             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1674             if (mask) {
1675                 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE,
1676                                 htons(UINT16_MAX));
1677             }
1678             return n;
1679         }
1680     }
1681
1682     {
1683         int label, tc, ttl, bos;
1684         int label_mask, tc_mask, ttl_mask, bos_mask;
1685         int n = -1;
1686
1687         if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1688                     &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1689             struct ovs_key_mpls *mpls, *mpls_mask;
1690
1691             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1692                                             sizeof *mpls);
1693             mpls->mpls_top_lse = mpls_lse_from_components(label, tc, ttl, bos);
1694
1695             mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1696                                             sizeof *mpls_mask);
1697             mpls_mask->mpls_top_lse = mpls_lse_from_components(
1698                                       label_mask, tc_mask, ttl_mask, bos_mask);
1699             return n;
1700         } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1701                     &label, &tc, &ttl, &bos, &n) > 0 &&
1702                     n > 0) {
1703             struct ovs_key_mpls *mpls;
1704
1705             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1706                                             sizeof *mpls);
1707             mpls->mpls_top_lse = mpls_lse_from_components(label, tc, ttl, bos);
1708             if (mask) {
1709                 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1710                                             sizeof *mpls);
1711                 mpls->mpls_top_lse = htonl(UINT32_MAX);
1712             }
1713             return n;
1714         }
1715     }
1716
1717
1718     {
1719         ovs_be32 ipv4_src, ipv4_src_mask;
1720         ovs_be32 ipv4_dst, ipv4_dst_mask;
1721         int ipv4_proto, ipv4_proto_mask;
1722         int ipv4_tos, ipv4_tos_mask;
1723         int ipv4_ttl, ipv4_ttl_mask;
1724         char frag[8];
1725         int  ipv4_frag_mask;
1726         enum ovs_frag_type ipv4_frag;
1727         int n = -1;
1728
1729         if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1730                       "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1731                       "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1732                       "frag=%7[a-z]/%i)%n",
1733                       IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1734                       IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1735                       &ipv4_proto, &ipv4_proto_mask,
1736                       &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1737                       frag, &ipv4_frag_mask, &n) > 0
1738             && n > 0
1739             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1740             struct ovs_key_ipv4 ipv4_key;
1741             struct ovs_key_ipv4 ipv4_mask;
1742
1743             ipv4_key.ipv4_src = ipv4_src;
1744             ipv4_key.ipv4_dst = ipv4_dst;
1745             ipv4_key.ipv4_proto = ipv4_proto;
1746             ipv4_key.ipv4_tos = ipv4_tos;
1747             ipv4_key.ipv4_ttl = ipv4_ttl;
1748             ipv4_key.ipv4_frag = ipv4_frag;
1749             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1750                               &ipv4_key, sizeof ipv4_key);
1751
1752             ipv4_mask.ipv4_src = ipv4_src_mask;
1753             ipv4_mask.ipv4_dst = ipv4_dst_mask;
1754             ipv4_mask.ipv4_proto = ipv4_proto_mask;
1755             ipv4_mask.ipv4_tos = ipv4_tos_mask;
1756             ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1757             ipv4_mask.ipv4_frag = ipv4_frag_mask;
1758             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1759                               &ipv4_mask, sizeof ipv4_mask);
1760             return n;
1761         } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1762                    "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1763                    IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1764                    &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1765             && n > 0
1766             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1767             struct ovs_key_ipv4 ipv4_key;
1768
1769             ipv4_key.ipv4_src = ipv4_src;
1770             ipv4_key.ipv4_dst = ipv4_dst;
1771             ipv4_key.ipv4_proto = ipv4_proto;
1772             ipv4_key.ipv4_tos = ipv4_tos;
1773             ipv4_key.ipv4_ttl = ipv4_ttl;
1774             ipv4_key.ipv4_frag = ipv4_frag;
1775             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1776                               &ipv4_key, sizeof ipv4_key);
1777
1778             if (mask) {
1779                 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1780                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1781                               &ipv4_key, sizeof ipv4_key);
1782             }
1783             return n;
1784         }
1785     }
1786
1787     {
1788         char ipv6_src_s[IPV6_SCAN_LEN + 1];
1789         char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1790         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1791         char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1792         int ipv6_label, ipv6_label_mask;
1793         int ipv6_proto, ipv6_proto_mask;
1794         int ipv6_tclass, ipv6_tclass_mask;
1795         int ipv6_hlimit, ipv6_hlimit_mask;
1796         char frag[8];
1797         enum ovs_frag_type ipv6_frag;
1798         int ipv6_frag_mask;
1799         int n = -1;
1800
1801         if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1802                    IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1803                    "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1804                    "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1805                    ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1806                    &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1807                    &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1808                    &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1809                    &ipv6_frag_mask, &n) > 0
1810             && n > 0
1811             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1812             struct ovs_key_ipv6 ipv6_key;
1813             struct ovs_key_ipv6 ipv6_mask;
1814
1815             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1816                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1817                 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1818                 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1819                 return -EINVAL;
1820             }
1821
1822             ipv6_key.ipv6_label = htonl(ipv6_label);
1823             ipv6_key.ipv6_proto = ipv6_proto;
1824             ipv6_key.ipv6_tclass = ipv6_tclass;
1825             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1826             ipv6_key.ipv6_frag = ipv6_frag;
1827             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1828                               &ipv6_key, sizeof ipv6_key);
1829
1830             ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1831             ipv6_mask.ipv6_proto = ipv6_proto_mask;
1832             ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1833             ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1834             ipv6_mask.ipv6_frag = ipv6_frag_mask;
1835             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1836                               &ipv6_mask, sizeof ipv6_mask);
1837             return n;
1838         } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1839                    "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1840                    ipv6_src_s, ipv6_dst_s, &ipv6_label,
1841                    &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1842             && n > 0
1843             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1844             struct ovs_key_ipv6 ipv6_key;
1845
1846             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1847                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1848                 return -EINVAL;
1849             }
1850             ipv6_key.ipv6_label = htonl(ipv6_label);
1851             ipv6_key.ipv6_proto = ipv6_proto;
1852             ipv6_key.ipv6_tclass = ipv6_tclass;
1853             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1854             ipv6_key.ipv6_frag = ipv6_frag;
1855             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1856                               &ipv6_key, sizeof ipv6_key);
1857
1858             if (mask) {
1859                 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1860                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1861                               &ipv6_key, sizeof ipv6_key);
1862             }
1863             return n;
1864         }
1865     }
1866
1867     {
1868         int tcp_src;
1869         int tcp_dst;
1870         int tcp_src_mask;
1871         int tcp_dst_mask;
1872         int n = -1;
1873
1874         if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1875                    &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1876             && n > 0) {
1877             struct ovs_key_tcp tcp_key;
1878             struct ovs_key_tcp tcp_mask;
1879
1880             tcp_key.tcp_src = htons(tcp_src);
1881             tcp_key.tcp_dst = htons(tcp_dst);
1882             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1883
1884             tcp_mask.tcp_src = htons(tcp_src_mask);
1885             tcp_mask.tcp_dst = htons(tcp_dst_mask);
1886             nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1887                               &tcp_mask, sizeof tcp_mask);
1888             return n;
1889         } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1890             && n > 0) {
1891             struct ovs_key_tcp tcp_key;
1892
1893             tcp_key.tcp_src = htons(tcp_src);
1894             tcp_key.tcp_dst = htons(tcp_dst);
1895             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1896
1897             if (mask) {
1898                 memset(&tcp_key, 0xff, sizeof tcp_key);
1899                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1900                               &tcp_key, sizeof tcp_key);
1901             }
1902             return n;
1903         }
1904     }
1905
1906     {
1907         int udp_src;
1908         int udp_dst;
1909         int udp_src_mask;
1910         int udp_dst_mask;
1911         int n = -1;
1912
1913         if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
1914                    &udp_src, &udp_src_mask,
1915                    &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
1916             struct ovs_key_udp udp_key;
1917             struct ovs_key_udp udp_mask;
1918
1919             udp_key.udp_src = htons(udp_src);
1920             udp_key.udp_dst = htons(udp_dst);
1921             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1922
1923             udp_mask.udp_src = htons(udp_src_mask);
1924             udp_mask.udp_dst = htons(udp_dst_mask);
1925             nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
1926                               &udp_mask, sizeof udp_mask);
1927             return n;
1928         }
1929         if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1930             && n > 0) {
1931             struct ovs_key_udp udp_key;
1932
1933             udp_key.udp_src = htons(udp_src);
1934             udp_key.udp_dst = htons(udp_dst);
1935             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1936
1937             if (mask) {
1938                 memset(&udp_key, 0xff, sizeof udp_key);
1939                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1940             }
1941             return n;
1942         }
1943     }
1944
1945     {
1946         int icmp_type;
1947         int icmp_code;
1948         int icmp_type_mask;
1949         int icmp_code_mask;
1950         int n = -1;
1951
1952         if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
1953                    &icmp_type, &icmp_type_mask,
1954                    &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
1955             struct ovs_key_icmp icmp_key;
1956             struct ovs_key_icmp icmp_mask;
1957
1958             icmp_key.icmp_type = icmp_type;
1959             icmp_key.icmp_code = icmp_code;
1960             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1961                               &icmp_key, sizeof icmp_key);
1962
1963             icmp_mask.icmp_type = icmp_type_mask;
1964             icmp_mask.icmp_code = icmp_code_mask;
1965             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
1966                               &icmp_mask, sizeof icmp_mask);
1967             return n;
1968         } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
1969                    &icmp_type, &icmp_code, &n) > 0
1970             && n > 0) {
1971             struct ovs_key_icmp icmp_key;
1972
1973             icmp_key.icmp_type = icmp_type;
1974             icmp_key.icmp_code = icmp_code;
1975             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1976                               &icmp_key, sizeof icmp_key);
1977             if (mask) {
1978                 memset(&icmp_key, 0xff, sizeof icmp_key);
1979                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
1980                               sizeof icmp_key);
1981             }
1982             return n;
1983         }
1984     }
1985
1986     {
1987         struct ovs_key_icmpv6 icmpv6_key;
1988         struct ovs_key_icmpv6 icmpv6_mask;
1989         int icmpv6_type_mask;
1990         int icmpv6_code_mask;
1991         int n = -1;
1992
1993         if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
1994                    &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
1995                    &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
1996             && n > 0) {
1997             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1998                               &icmpv6_key, sizeof icmpv6_key);
1999
2000             icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2001             icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2002             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2003                               sizeof icmpv6_mask);
2004             return n;
2005         } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2006                    &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2007             && n > 0) {
2008             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2009                               &icmpv6_key, sizeof icmpv6_key);
2010
2011             if (mask) {
2012                 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2013                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2014                               sizeof icmpv6_key);
2015             }
2016             return n;
2017         }
2018     }
2019
2020     {
2021         ovs_be32 arp_sip, arp_sip_mask;
2022         ovs_be32 arp_tip, arp_tip_mask;
2023         int arp_op, arp_op_mask;
2024         uint8_t arp_sha[ETH_ADDR_LEN];
2025         uint8_t arp_sha_mask[ETH_ADDR_LEN];
2026         uint8_t arp_tha[ETH_ADDR_LEN];
2027         uint8_t arp_tha_mask[ETH_ADDR_LEN];
2028         int n = -1;
2029
2030         if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2031                    "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2032                    "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2033                    "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2034                    IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2035                    IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2036                    &arp_op, &arp_op_mask,
2037                    ETH_ADDR_SCAN_ARGS(arp_sha),
2038                    ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2039                    ETH_ADDR_SCAN_ARGS(arp_tha),
2040                    ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2041             struct ovs_key_arp arp_key;
2042             struct ovs_key_arp arp_mask;
2043
2044             memset(&arp_key, 0, sizeof arp_key);
2045             arp_key.arp_sip = arp_sip;
2046             arp_key.arp_tip = arp_tip;
2047             arp_key.arp_op = htons(arp_op);
2048             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2049             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2050             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2051
2052             arp_mask.arp_sip = arp_sip_mask;
2053             arp_mask.arp_tip = arp_tip_mask;
2054             arp_mask.arp_op = htons(arp_op_mask);
2055             memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2056             memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2057             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2058                               &arp_mask, sizeof arp_mask);
2059             return n;
2060         } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2061                    "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2062                    IP_SCAN_ARGS(&arp_sip),
2063                    IP_SCAN_ARGS(&arp_tip),
2064                    &arp_op,
2065                    ETH_ADDR_SCAN_ARGS(arp_sha),
2066                    ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2067             struct ovs_key_arp arp_key;
2068
2069             memset(&arp_key, 0, sizeof arp_key);
2070             arp_key.arp_sip = arp_sip;
2071             arp_key.arp_tip = arp_tip;
2072             arp_key.arp_op = htons(arp_op);
2073             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2074             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2075             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2076
2077             if (mask) {
2078                 memset(&arp_key, 0xff, sizeof arp_key);
2079                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2080                                   &arp_key, sizeof arp_key);
2081             }
2082             return n;
2083         }
2084     }
2085
2086     {
2087         char nd_target_s[IPV6_SCAN_LEN + 1];
2088         char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2089         uint8_t nd_sll[ETH_ADDR_LEN];
2090         uint8_t nd_sll_mask[ETH_ADDR_LEN];
2091         uint8_t nd_tll[ETH_ADDR_LEN];
2092         uint8_t nd_tll_mask[ETH_ADDR_LEN];
2093         int n = -1;
2094
2095         memset(&nd_target_mask_s[0], 0xff, sizeof nd_target_s);
2096         memset(&nd_sll_mask[0], 0xff, sizeof nd_sll);
2097         memset(&nd_tll_mask [0], 0xff, sizeof nd_tll);
2098
2099         if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2100                    nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2101                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2102                 put_nd_key(n, nd_target_mask_s, NULL, NULL, mask);
2103         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2104                    nd_target_s, &n) > 0 && n > 0) {
2105                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2106                 if (mask) {
2107                     put_nd_key(n, nd_target_mask_s, NULL, NULL, mask);
2108                 }
2109         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2110                          ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2111                    nd_target_s, nd_target_mask_s,
2112                    ETH_ADDR_SCAN_ARGS(nd_sll),
2113                    ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2114             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2115             put_nd_key(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2116         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2117                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2118             && n > 0) {
2119             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2120             if (mask) {
2121                 put_nd_key(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2122             }
2123         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2124                          ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2125                    nd_target_s, nd_target_mask_s,
2126                    ETH_ADDR_SCAN_ARGS(nd_tll),
2127                    ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2128             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2129             put_nd_key(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2130         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2131                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2132             && n > 0) {
2133             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2134             if (mask) {
2135                 put_nd_key(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2136             }
2137         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2138                    ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2139                    "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2140                    nd_target_s, nd_target_mask_s,
2141                    ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2142                    ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2143                    &n) > 0
2144             && n > 0) {
2145             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2146             put_nd_key(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2147         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2148                    "tll="ETH_ADDR_SCAN_FMT")%n",
2149                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2150                    ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2151             && n > 0) {
2152             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2153             if (mask) {
2154                 put_nd_key(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2155             }
2156         }
2157
2158         if (n != -1)
2159             return n;
2160
2161     }
2162
2163     if (!strncmp(s, "encap(", 6)) {
2164         const char *start = s;
2165         size_t encap, encap_mask = 0;
2166
2167         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2168         if (mask) {
2169             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2170         }
2171
2172         s += 6;
2173         for (;;) {
2174             int retval;
2175
2176             s += strspn(s, ", \t\r\n");
2177             if (!*s) {
2178                 return -EINVAL;
2179             } else if (*s == ')') {
2180                 break;
2181             }
2182
2183             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2184             if (retval < 0) {
2185                 return retval;
2186             }
2187             s += retval;
2188         }
2189         s++;
2190
2191         nl_msg_end_nested(key, encap);
2192         if (mask) {
2193             nl_msg_end_nested(mask, encap_mask);
2194         }
2195
2196         return s - start;
2197     }
2198
2199     return -EINVAL;
2200 }
2201
2202 /* Parses the string representation of a datapath flow key, in the
2203  * format output by odp_flow_key_format().  Returns 0 if successful,
2204  * otherwise a positive errno value.  On success, the flow key is
2205  * appended to 'key' as a series of Netlink attributes.  On failure, no
2206  * data is appended to 'key'.  Either way, 'key''s data might be
2207  * reallocated.
2208  *
2209  * If 'port_names' is nonnull, it points to an simap that maps from a port name
2210  * to a port number.  (Port names may be used instead of port numbers in
2211  * in_port.)
2212  *
2213  * On success, the attributes appended to 'key' are individually syntactically
2214  * valid, but they may not be valid as a sequence.  'key' might, for example,
2215  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
2216 int
2217 odp_flow_from_string(const char *s, const struct simap *port_names,
2218                      struct ofpbuf *key, struct ofpbuf *mask)
2219 {
2220     const size_t old_size = key->size;
2221     for (;;) {
2222         int retval;
2223
2224         s += strspn(s, delimiters);
2225         if (!*s) {
2226             return 0;
2227         }
2228
2229         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2230         if (retval < 0) {
2231             key->size = old_size;
2232             return -retval;
2233         }
2234         s += retval;
2235     }
2236
2237     return 0;
2238 }
2239
2240 static uint8_t
2241 ovs_to_odp_frag(uint8_t nw_frag)
2242 {
2243     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2244           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2245           : OVS_FRAG_TYPE_LATER);
2246 }
2247
2248 static void
2249 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2250                          const struct flow *flow, uint32_t odp_in_port)
2251 {
2252     bool is_mask;
2253     struct ovs_key_ethernet *eth_key;
2254     size_t encap;
2255
2256     /* We assume that if 'data' and 'flow' are not the same, we should
2257      * treat 'data' as a mask. */
2258     is_mask = (data != flow);
2259
2260     if (flow->skb_priority) {
2261         nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2262     }
2263
2264     if (flow->tunnel.ip_dst) {
2265         tun_key_to_attr(buf, &data->tunnel);
2266     }
2267
2268     if (flow->skb_mark) {
2269         nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->skb_mark);
2270     }
2271
2272     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2273      * is not the magical value "OVSP_NONE". */
2274     if (is_mask || odp_in_port != OVSP_NONE) {
2275         nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2276     }
2277
2278     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2279                                        sizeof *eth_key);
2280     memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2281     memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2282
2283     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2284         if (is_mask) {
2285             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2286         } else {
2287             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2288         }
2289         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2290         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2291         if (flow->vlan_tci == htons(0)) {
2292             goto unencap;
2293         }
2294     } else {
2295         encap = 0;
2296     }
2297
2298     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2299         /* For backwards compatibility with kernels that don't support
2300          * wildcarding, the following convention is used to encode the
2301          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2302          *
2303          *   key      mask    matches
2304          * -------- --------  -------
2305          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
2306          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
2307          *  <none>   0xffff   Any non-Ethernet II frame (except valid
2308          *                    802.3 SNAP packet with valid eth_type).
2309          */
2310         if (is_mask) {
2311             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2312         }
2313         goto unencap;
2314     }
2315
2316     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2317
2318     if (flow->dl_type == htons(ETH_TYPE_IP)) {
2319         struct ovs_key_ipv4 *ipv4_key;
2320
2321         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2322                                             sizeof *ipv4_key);
2323         ipv4_key->ipv4_src = data->nw_src;
2324         ipv4_key->ipv4_dst = data->nw_dst;
2325         ipv4_key->ipv4_proto = data->nw_proto;
2326         ipv4_key->ipv4_tos = data->nw_tos;
2327         ipv4_key->ipv4_ttl = data->nw_ttl;
2328         ipv4_key->ipv4_frag = ovs_to_odp_frag(data->nw_frag);
2329     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2330         struct ovs_key_ipv6 *ipv6_key;
2331
2332         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2333                                             sizeof *ipv6_key);
2334         memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2335         memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2336         ipv6_key->ipv6_label = data->ipv6_label;
2337         ipv6_key->ipv6_proto = data->nw_proto;
2338         ipv6_key->ipv6_tclass = data->nw_tos;
2339         ipv6_key->ipv6_hlimit = data->nw_ttl;
2340         ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
2341     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2342                flow->dl_type == htons(ETH_TYPE_RARP)) {
2343         struct ovs_key_arp *arp_key;
2344
2345         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2346                                            sizeof *arp_key);
2347         memset(arp_key, 0, sizeof *arp_key);
2348         arp_key->arp_sip = data->nw_src;
2349         arp_key->arp_tip = data->nw_dst;
2350         arp_key->arp_op = htons(data->nw_proto);
2351         memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2352         memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2353     }
2354
2355     if (flow->mpls_depth) {
2356         struct ovs_key_mpls *mpls_key;
2357
2358         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2359                                             sizeof *mpls_key);
2360         mpls_key->mpls_top_lse = flow->mpls_lse;
2361     }
2362
2363     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2364         if (flow->nw_proto == IPPROTO_TCP) {
2365             struct ovs_key_tcp *tcp_key;
2366
2367             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2368                                                sizeof *tcp_key);
2369             tcp_key->tcp_src = data->tp_src;
2370             tcp_key->tcp_dst = data->tp_dst;
2371         } else if (flow->nw_proto == IPPROTO_UDP) {
2372             struct ovs_key_udp *udp_key;
2373
2374             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2375                                                sizeof *udp_key);
2376             udp_key->udp_src = data->tp_src;
2377             udp_key->udp_dst = data->tp_dst;
2378         } else if (flow->dl_type == htons(ETH_TYPE_IP)
2379                 && flow->nw_proto == IPPROTO_ICMP) {
2380             struct ovs_key_icmp *icmp_key;
2381
2382             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2383                                                 sizeof *icmp_key);
2384             icmp_key->icmp_type = ntohs(data->tp_src);
2385             icmp_key->icmp_code = ntohs(data->tp_dst);
2386         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2387                 && flow->nw_proto == IPPROTO_ICMPV6) {
2388             struct ovs_key_icmpv6 *icmpv6_key;
2389
2390             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2391                                                   sizeof *icmpv6_key);
2392             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2393             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2394
2395             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
2396                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
2397                 struct ovs_key_nd *nd_key;
2398
2399                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2400                                                     sizeof *nd_key);
2401                 memcpy(nd_key->nd_target, &data->nd_target,
2402                         sizeof nd_key->nd_target);
2403                 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2404                 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2405             }
2406         }
2407     }
2408
2409 unencap:
2410     if (encap) {
2411         nl_msg_end_nested(buf, encap);
2412     }
2413 }
2414
2415 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2416  * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2417  * number rather than a datapath port number).  Instead, if 'odp_in_port'
2418  * is anything other than ODPP_NONE, it is included in 'buf' as the input
2419  * port.
2420  *
2421  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2422  * capable of being expanded to allow for that much space. */
2423 void
2424 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2425                        uint32_t odp_in_port)
2426 {
2427     odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2428 }
2429
2430 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2431  * 'buf'.  'flow' is used as a template to determine how to interpret
2432  * 'mask'.  For example, the 'dl_type' of 'mask' describes the mask, but
2433  * it doesn't indicate whether the other fields should be interpreted as
2434  * ARP, IPv4, IPv6, etc.
2435  *
2436  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2437  * capable of being expanded to allow for that much space. */
2438 void
2439 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2440                        const struct flow *flow, uint32_t odp_in_port_mask)
2441 {
2442     odp_flow_key_from_flow__(buf, mask, flow, odp_in_port_mask);
2443 }
2444
2445 uint32_t
2446 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2447 {
2448     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2449     return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
2450 }
2451
2452 static void
2453 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2454                        uint64_t attrs, int out_of_range_attr,
2455                        const struct nlattr *key, size_t key_len)
2456 {
2457     struct ds s;
2458     int i;
2459
2460     if (VLOG_DROP_DBG(rl)) {
2461         return;
2462     }
2463
2464     ds_init(&s);
2465     for (i = 0; i < 64; i++) {
2466         if (attrs & (UINT64_C(1) << i)) {
2467             ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
2468         }
2469     }
2470     if (out_of_range_attr) {
2471         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2472     }
2473
2474     ds_put_cstr(&s, ": ");
2475     odp_flow_key_format(key, key_len, &s);
2476
2477     VLOG_DBG("%s:%s", title, ds_cstr(&s));
2478     ds_destroy(&s);
2479 }
2480
2481 static bool
2482 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2483 {
2484     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2485
2486     if (odp_frag > OVS_FRAG_TYPE_LATER) {
2487         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2488         return false;
2489     }
2490
2491     if (odp_frag != OVS_FRAG_TYPE_NONE) {
2492         flow->nw_frag |= FLOW_NW_FRAG_ANY;
2493         if (odp_frag == OVS_FRAG_TYPE_LATER) {
2494             flow->nw_frag |= FLOW_NW_FRAG_LATER;
2495         }
2496     }
2497     return true;
2498 }
2499
2500 static bool
2501 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2502                    const struct nlattr *attrs[], uint64_t *present_attrsp,
2503                    int *out_of_range_attrp)
2504 {
2505     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2506     const struct nlattr *nla;
2507     uint64_t present_attrs;
2508     size_t left;
2509
2510     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2511     present_attrs = 0;
2512     *out_of_range_attrp = 0;
2513     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2514         uint16_t type = nl_attr_type(nla);
2515         size_t len = nl_attr_get_size(nla);
2516         int expected_len = odp_flow_key_attr_len(type);
2517
2518         if (len != expected_len && expected_len >= 0) {
2519             VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2520                         "length %d", ovs_key_attr_to_string(type),
2521                         len, expected_len);
2522             return false;
2523         }
2524
2525         if (type > OVS_KEY_ATTR_MAX) {
2526             *out_of_range_attrp = type;
2527         } else {
2528             if (present_attrs & (UINT64_C(1) << type)) {
2529                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2530                             ovs_key_attr_to_string(type));
2531                 return false;
2532             }
2533
2534             present_attrs |= UINT64_C(1) << type;
2535             attrs[type] = nla;
2536         }
2537     }
2538     if (left) {
2539         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2540         return false;
2541     }
2542
2543     *present_attrsp = present_attrs;
2544     return true;
2545 }
2546
2547 static enum odp_key_fitness
2548 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2549                    uint64_t expected_attrs,
2550                    const struct nlattr *key, size_t key_len)
2551 {
2552     uint64_t missing_attrs;
2553     uint64_t extra_attrs;
2554
2555     missing_attrs = expected_attrs & ~present_attrs;
2556     if (missing_attrs) {
2557         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2558         log_odp_key_attributes(&rl, "expected but not present",
2559                                missing_attrs, 0, key, key_len);
2560         return ODP_FIT_TOO_LITTLE;
2561     }
2562
2563     extra_attrs = present_attrs & ~expected_attrs;
2564     if (extra_attrs || out_of_range_attr) {
2565         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2566         log_odp_key_attributes(&rl, "present but not expected",
2567                                extra_attrs, out_of_range_attr, key, key_len);
2568         return ODP_FIT_TOO_MUCH;
2569     }
2570
2571     return ODP_FIT_PERFECT;
2572 }
2573
2574 static bool
2575 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2576                 uint64_t present_attrs, uint64_t *expected_attrs,
2577                 struct flow *flow)
2578 {
2579     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2580
2581     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2582         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2583         if (ntohs(flow->dl_type) < 1536) {
2584             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2585                         ntohs(flow->dl_type));
2586             return false;
2587         }
2588         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2589     } else {
2590         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2591     }
2592     return true;
2593 }
2594
2595 static enum odp_key_fitness
2596 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2597                   uint64_t present_attrs, int out_of_range_attr,
2598                   uint64_t expected_attrs, struct flow *flow,
2599                   const struct nlattr *key, size_t key_len)
2600 {
2601     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2602
2603     if (eth_type_mpls(flow->dl_type)) {
2604         expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2605
2606         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2607             return ODP_FIT_TOO_LITTLE;
2608         }
2609         flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2610         flow->mpls_depth++;
2611     } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
2612         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2613         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2614             const struct ovs_key_ipv4 *ipv4_key;
2615
2616             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2617             flow->nw_src = ipv4_key->ipv4_src;
2618             flow->nw_dst = ipv4_key->ipv4_dst;
2619             flow->nw_proto = ipv4_key->ipv4_proto;
2620             flow->nw_tos = ipv4_key->ipv4_tos;
2621             flow->nw_ttl = ipv4_key->ipv4_ttl;
2622             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2623                 return ODP_FIT_ERROR;
2624             }
2625         }
2626     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2627         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2628         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2629             const struct ovs_key_ipv6 *ipv6_key;
2630
2631             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2632             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2633             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2634             flow->ipv6_label = ipv6_key->ipv6_label;
2635             flow->nw_proto = ipv6_key->ipv6_proto;
2636             flow->nw_tos = ipv6_key->ipv6_tclass;
2637             flow->nw_ttl = ipv6_key->ipv6_hlimit;
2638             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2639                 return ODP_FIT_ERROR;
2640             }
2641         }
2642     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2643                flow->dl_type == htons(ETH_TYPE_RARP)) {
2644         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2645         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2646             const struct ovs_key_arp *arp_key;
2647
2648             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2649             flow->nw_src = arp_key->arp_sip;
2650             flow->nw_dst = arp_key->arp_tip;
2651             if (arp_key->arp_op & htons(0xff00)) {
2652                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2653                             "key", ntohs(arp_key->arp_op));
2654                 return ODP_FIT_ERROR;
2655             }
2656             flow->nw_proto = ntohs(arp_key->arp_op);
2657             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2658             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2659         }
2660     }
2661
2662     if (flow->nw_proto == IPPROTO_TCP
2663         && (flow->dl_type == htons(ETH_TYPE_IP) ||
2664             flow->dl_type == htons(ETH_TYPE_IPV6))
2665         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2666         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2667         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2668             const struct ovs_key_tcp *tcp_key;
2669
2670             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2671             flow->tp_src = tcp_key->tcp_src;
2672             flow->tp_dst = tcp_key->tcp_dst;
2673         }
2674     } else if (flow->nw_proto == IPPROTO_UDP
2675                && (flow->dl_type == htons(ETH_TYPE_IP) ||
2676                    flow->dl_type == htons(ETH_TYPE_IPV6))
2677                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2678         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2679         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2680             const struct ovs_key_udp *udp_key;
2681
2682             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2683             flow->tp_src = udp_key->udp_src;
2684             flow->tp_dst = udp_key->udp_dst;
2685         }
2686     } else if (flow->nw_proto == IPPROTO_ICMP
2687                && flow->dl_type == htons(ETH_TYPE_IP)
2688                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2689         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2690         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2691             const struct ovs_key_icmp *icmp_key;
2692
2693             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2694             flow->tp_src = htons(icmp_key->icmp_type);
2695             flow->tp_dst = htons(icmp_key->icmp_code);
2696         }
2697     } else if (flow->nw_proto == IPPROTO_ICMPV6
2698                && flow->dl_type == htons(ETH_TYPE_IPV6)
2699                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2700         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2701         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2702             const struct ovs_key_icmpv6 *icmpv6_key;
2703
2704             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2705             flow->tp_src = htons(icmpv6_key->icmpv6_type);
2706             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2707
2708             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2709                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2710                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2711                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2712                     const struct ovs_key_nd *nd_key;
2713
2714                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2715                     memcpy(&flow->nd_target, nd_key->nd_target,
2716                            sizeof flow->nd_target);
2717                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2718                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2719                 }
2720             }
2721         }
2722     }
2723
2724     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
2725                               key, key_len);
2726 }
2727
2728 /* Parse 802.1Q header then encapsulated L3 attributes. */
2729 static enum odp_key_fitness
2730 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2731                    uint64_t present_attrs, int out_of_range_attr,
2732                    uint64_t expected_attrs, struct flow *flow,
2733                    const struct nlattr *key, size_t key_len)
2734 {
2735     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2736
2737     const struct nlattr *encap
2738         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
2739            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
2740     enum odp_key_fitness encap_fitness;
2741     enum odp_key_fitness fitness;
2742     ovs_be16 tci;
2743
2744     /* Calulate fitness of outer attributes. */
2745     expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
2746                        (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
2747     fitness = check_expectations(present_attrs, out_of_range_attr,
2748                                  expected_attrs, key, key_len);
2749
2750     /* Get the VLAN TCI value. */
2751     if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
2752         return ODP_FIT_TOO_LITTLE;
2753     }
2754     tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
2755     if (tci == htons(0)) {
2756         /* Corner case for a truncated 802.1Q header. */
2757         if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
2758             return ODP_FIT_TOO_MUCH;
2759         }
2760         return fitness;
2761     } else if (!(tci & htons(VLAN_CFI))) {
2762         VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
2763                     "but CFI bit is not set", ntohs(tci));
2764         return ODP_FIT_ERROR;
2765     }
2766
2767     /* Set vlan_tci.
2768      * Remove the TPID from dl_type since it's not the real Ethertype.  */
2769     flow->vlan_tci = tci;
2770     flow->dl_type = htons(0);
2771
2772     /* Now parse the encapsulated attributes. */
2773     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
2774                             attrs, &present_attrs, &out_of_range_attr)) {
2775         return ODP_FIT_ERROR;
2776     }
2777     expected_attrs = 0;
2778
2779     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2780         return ODP_FIT_ERROR;
2781     }
2782     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2783                                       expected_attrs, flow, key, key_len);
2784
2785     /* The overall fitness is the worse of the outer and inner attributes. */
2786     return MAX(fitness, encap_fitness);
2787 }
2788
2789 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2790  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
2791  * 'key' fits our expectations for what a flow key should contain.
2792  *
2793  * The 'in_port' will be the datapath's understanding of the port.  The
2794  * caller will need to translate with odp_port_to_ofp_port() if the
2795  * OpenFlow port is needed.
2796  *
2797  * This function doesn't take the packet itself as an argument because none of
2798  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
2799  * it is always possible to infer which additional attribute(s) should appear
2800  * by looking at the attributes for lower-level protocols, e.g. if the network
2801  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2802  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2803  * must be absent. */
2804 enum odp_key_fitness
2805 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2806                      struct flow *flow)
2807 {
2808     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2809     uint64_t expected_attrs;
2810     uint64_t present_attrs;
2811     int out_of_range_attr;
2812
2813     memset(flow, 0, sizeof *flow);
2814
2815     /* Parse attributes. */
2816     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2817                             &out_of_range_attr)) {
2818         return ODP_FIT_ERROR;
2819     }
2820     expected_attrs = 0;
2821
2822     /* Metadata. */
2823     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2824         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2825         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2826     }
2827
2828     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2829         flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2830         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2831     }
2832
2833     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2834         enum odp_key_fitness res;
2835
2836         res = tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2837         if (res == ODP_FIT_ERROR) {
2838             return ODP_FIT_ERROR;
2839         } else if (res == ODP_FIT_PERFECT) {
2840             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2841         }
2842     }
2843
2844     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2845         flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
2846         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2847     } else {
2848         flow->in_port = OVSP_NONE;
2849     }
2850
2851     /* Ethernet header. */
2852     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2853         const struct ovs_key_ethernet *eth_key;
2854
2855         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2856         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2857         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2858     }
2859     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2860
2861     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2862     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2863         return ODP_FIT_ERROR;
2864     }
2865
2866     if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2867         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2868                                   expected_attrs, flow, key, key_len);
2869     }
2870     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2871                              expected_attrs, flow, key, key_len);
2872 }
2873
2874 /* Returns 'fitness' as a string, for use in debug messages. */
2875 const char *
2876 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2877 {
2878     switch (fitness) {
2879     case ODP_FIT_PERFECT:
2880         return "OK";
2881     case ODP_FIT_TOO_MUCH:
2882         return "too_much";
2883     case ODP_FIT_TOO_LITTLE:
2884         return "too_little";
2885     case ODP_FIT_ERROR:
2886         return "error";
2887     default:
2888         return "<unknown>";
2889     }
2890 }
2891
2892 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2893  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
2894  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2895  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
2896  * null, then the return value is not meaningful.) */
2897 size_t
2898 odp_put_userspace_action(uint32_t pid,
2899                          const void *userdata, size_t userdata_size,
2900                          struct ofpbuf *odp_actions)
2901 {
2902     size_t userdata_ofs;
2903     size_t offset;
2904
2905     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
2906     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
2907     if (userdata) {
2908         userdata_ofs = odp_actions->size + NLA_HDRLEN;
2909         nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
2910                           userdata, userdata_size);
2911     } else {
2912         userdata_ofs = 0;
2913     }
2914     nl_msg_end_nested(odp_actions, offset);
2915
2916     return userdata_ofs;
2917 }
2918
2919 void
2920 odp_put_tunnel_action(const struct flow_tnl *tunnel,
2921                       struct ofpbuf *odp_actions)
2922 {
2923     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2924     tun_key_to_attr(odp_actions, tunnel);
2925     nl_msg_end_nested(odp_actions, offset);
2926 }
2927 \f
2928 /* The commit_odp_actions() function and its helpers. */
2929
2930 static void
2931 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
2932                   const void *key, size_t key_size)
2933 {
2934     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2935     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
2936     nl_msg_end_nested(odp_actions, offset);
2937 }
2938
2939 void
2940 odp_put_skb_mark_action(const uint32_t skb_mark,
2941                         struct ofpbuf *odp_actions)
2942 {
2943     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
2944                       sizeof(skb_mark));
2945 }
2946
2947 /* If any of the flow key data that ODP actions can modify are different in
2948  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2949  * 'odp_actions' that change the flow tunneling information in key from
2950  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2951  * same way.  In other words, operates the same as commit_odp_actions(), but
2952  * only on tunneling information. */
2953 void
2954 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
2955                          struct ofpbuf *odp_actions)
2956 {
2957     /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2958     if (flow->tunnel.ip_dst) {
2959         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
2960             return;
2961         }
2962         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
2963         odp_put_tunnel_action(&base->tunnel, odp_actions);
2964     }
2965 }
2966
2967 static void
2968 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
2969                              struct ofpbuf *odp_actions,
2970                              struct flow_wildcards *wc)
2971 {
2972     struct ovs_key_ethernet eth_key;
2973
2974     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2975         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2976         return;
2977     }
2978
2979     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2980     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2981
2982     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2983     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2984
2985     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2986     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2987
2988     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2989                       &eth_key, sizeof(eth_key));
2990 }
2991
2992 static void
2993 commit_vlan_action(const struct flow *flow, struct flow *base,
2994                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
2995 {
2996     if (base->vlan_tci == flow->vlan_tci) {
2997         return;
2998     }
2999
3000     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3001
3002     if (base->vlan_tci & htons(VLAN_CFI)) {
3003         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3004     }
3005
3006     if (flow->vlan_tci & htons(VLAN_CFI)) {
3007         struct ovs_action_push_vlan vlan;
3008
3009         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3010         vlan.vlan_tci = flow->vlan_tci;
3011         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3012                           &vlan, sizeof vlan);
3013     }
3014     base->vlan_tci = flow->vlan_tci;
3015 }
3016
3017 static void
3018 commit_mpls_action(const struct flow *flow, struct flow *base,
3019                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3020 {
3021     if (flow->mpls_lse == base->mpls_lse &&
3022         flow->mpls_depth == base->mpls_depth) {
3023         return;
3024     }
3025
3026     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3027
3028     if (flow->mpls_depth < base->mpls_depth) {
3029         if (base->mpls_depth - flow->mpls_depth > 1) {
3030             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3031             VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3032                          " a single mpls_pop action");
3033         }
3034
3035         nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3036     } else if (flow->mpls_depth > base->mpls_depth) {
3037         struct ovs_action_push_mpls *mpls;
3038
3039         if (flow->mpls_depth - base->mpls_depth > 1) {
3040             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3041             VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3042                          " a single mpls_push action");
3043         }
3044
3045         mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3046                                         sizeof *mpls);
3047         memset(mpls, 0, sizeof *mpls);
3048         mpls->mpls_ethertype = flow->dl_type;
3049         mpls->mpls_lse = flow->mpls_lse;
3050     } else {
3051         struct ovs_key_mpls mpls_key;
3052
3053         mpls_key.mpls_top_lse = flow->mpls_lse;
3054         commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3055                           &mpls_key, sizeof(mpls_key));
3056     }
3057
3058     base->dl_type = flow->dl_type;
3059     base->mpls_lse = flow->mpls_lse;
3060     base->mpls_depth = flow->mpls_depth;
3061 }
3062
3063 static void
3064 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3065                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3066 {
3067     struct ovs_key_ipv4 ipv4_key;
3068
3069     if (base->nw_src == flow->nw_src &&
3070         base->nw_dst == flow->nw_dst &&
3071         base->nw_tos == flow->nw_tos &&
3072         base->nw_ttl == flow->nw_ttl &&
3073         base->nw_frag == flow->nw_frag) {
3074         return;
3075     }
3076
3077     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3078     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3079     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3080     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3081     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3082     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3083
3084     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3085     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3086     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3087     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3088     ipv4_key.ipv4_proto = base->nw_proto;
3089     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3090
3091     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3092                       &ipv4_key, sizeof(ipv4_key));
3093 }
3094
3095 static void
3096 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3097                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3098 {
3099     struct ovs_key_ipv6 ipv6_key;
3100
3101     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3102         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3103         base->ipv6_label == flow->ipv6_label &&
3104         base->nw_tos == flow->nw_tos &&
3105         base->nw_ttl == flow->nw_ttl &&
3106         base->nw_frag == flow->nw_frag) {
3107         return;
3108     }
3109
3110     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3111     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3112     memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3113     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3114     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3115     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3116     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3117
3118     base->ipv6_src = flow->ipv6_src;
3119     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3120     base->ipv6_dst = flow->ipv6_dst;
3121     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3122
3123     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3124     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3125     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3126     ipv6_key.ipv6_proto = base->nw_proto;
3127     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3128
3129     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3130                       &ipv6_key, sizeof(ipv6_key));
3131 }
3132
3133 static void
3134 commit_set_nw_action(const struct flow *flow, struct flow *base,
3135                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3136 {
3137     /* Check if flow really have an IP header. */
3138     if (!flow->nw_proto) {
3139         return;
3140     }
3141
3142     if (base->dl_type == htons(ETH_TYPE_IP)) {
3143         commit_set_ipv4_action(flow, base, odp_actions, wc);
3144     } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3145         commit_set_ipv6_action(flow, base, odp_actions, wc);
3146     }
3147 }
3148
3149 static void
3150 commit_set_port_action(const struct flow *flow, struct flow *base,
3151                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3152 {
3153     if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3154         return;
3155     }
3156
3157     if (base->tp_src == flow->tp_src &&
3158         base->tp_dst == flow->tp_dst) {
3159         return;
3160     }
3161
3162     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3163     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3164
3165     if (flow->nw_proto == IPPROTO_TCP) {
3166         struct ovs_key_tcp port_key;
3167
3168         port_key.tcp_src = base->tp_src = flow->tp_src;
3169         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3170
3171         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3172                           &port_key, sizeof(port_key));
3173
3174     } else if (flow->nw_proto == IPPROTO_UDP) {
3175         struct ovs_key_udp port_key;
3176
3177         port_key.udp_src = base->tp_src = flow->tp_src;
3178         port_key.udp_dst = base->tp_dst = flow->tp_dst;
3179
3180         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3181                           &port_key, sizeof(port_key));
3182     }
3183 }
3184
3185 static void
3186 commit_set_priority_action(const struct flow *flow, struct flow *base,
3187                            struct ofpbuf *odp_actions,
3188                            struct flow_wildcards *wc)
3189 {
3190     if (base->skb_priority == flow->skb_priority) {
3191         return;
3192     }
3193
3194     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3195     base->skb_priority = flow->skb_priority;
3196
3197     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3198                       &base->skb_priority, sizeof(base->skb_priority));
3199 }
3200
3201 static void
3202 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
3203                            struct ofpbuf *odp_actions,
3204                            struct flow_wildcards *wc)
3205 {
3206     if (base->skb_mark == flow->skb_mark) {
3207         return;
3208     }
3209
3210     memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
3211     base->skb_mark = flow->skb_mark;
3212
3213     odp_put_skb_mark_action(base->skb_mark, odp_actions);
3214 }
3215 /* If any of the flow key data that ODP actions can modify are different in
3216  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3217  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
3218  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
3219  * in addition to this function if needed.  Sets fields in 'wc' that are
3220  * used as part of the action. */
3221 void
3222 commit_odp_actions(const struct flow *flow, struct flow *base,
3223                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3224 {
3225     commit_set_ether_addr_action(flow, base, odp_actions, wc);
3226     commit_vlan_action(flow, base, odp_actions, wc);
3227     commit_set_nw_action(flow, base, odp_actions, wc);
3228     commit_set_port_action(flow, base, odp_actions, wc);
3229     /* Committing MPLS actions should occur after committing nw and port
3230      * actions. This is because committing MPLS actions may alter a packet so
3231      * that it is no longer IP and thus nw and port actions are no longer valid.
3232      */
3233     commit_mpls_action(flow, base, odp_actions, wc);
3234     commit_set_priority_action(flow, base, odp_actions, wc);
3235     commit_set_skb_mark_action(flow, base, odp_actions, wc);
3236 }