57393fb4092372b49db7432206ed50f5812b16be
[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     bool is_exact;
934
935     is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
936
937     ds_put_cstr(ds, ovs_key_attr_to_string(attr));
938
939     {
940         expected_len = odp_flow_key_attr_len(nl_attr_type(a));
941         if (expected_len != -2) {
942             bool bad_key_len = nl_attr_get_size(a) != expected_len;
943             bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
944
945             if (bad_key_len || bad_mask_len) {
946                 if (bad_key_len) {
947                     ds_put_format(ds, "(bad key length %zu, expected %d)(",
948                                   nl_attr_get_size(a),
949                                   odp_flow_key_attr_len(nl_attr_type(a)));
950                 }
951                 format_generic_odp_key(a, ds);
952                 if (bad_mask_len) {
953                     ds_put_char(ds, '/');
954                     ds_put_format(ds, "(bad mask length %zu, expected %d)(",
955                                   nl_attr_get_size(ma),
956                                   odp_flow_key_attr_len(nl_attr_type(ma)));
957                 }
958                 format_generic_odp_key(ma, ds);
959                 ds_put_char(ds, ')');
960                 return;
961             }
962         }
963     }
964
965     ds_put_char(ds, '(');
966     switch (attr) {
967     case OVS_KEY_ATTR_ENCAP:
968         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
969             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
970                             nl_attr_get(ma), nl_attr_get_size(ma), ds);
971         } else if (nl_attr_get_size(a)) {
972             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds);
973         }
974         break;
975
976     case OVS_KEY_ATTR_PRIORITY:
977     case OVS_KEY_ATTR_SKB_MARK:
978         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
979         if (!is_exact) {
980             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
981         }
982         break;
983
984     case OVS_KEY_ATTR_TUNNEL:
985         memset(&tun_key, 0, sizeof tun_key);
986         if (tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
987             ds_put_format(ds, "error");
988         } else if (!is_exact) {
989             struct flow_tnl tun_mask;
990
991             memset(&tun_mask, 0, sizeof tun_mask);
992             tun_key_from_attr(ma, &tun_mask);
993             ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
994                           ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
995                           ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
996                           ",flags(",
997                           ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
998                           IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
999                           IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1000                           tun_key.ip_tos, tun_mask.ip_tos,
1001                           tun_key.ip_ttl, tun_mask.ip_ttl);
1002
1003             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1004
1005             /* XXX This code is correct, but enabling it would break the unit
1006                test. Disable it for now until the input parser is fixed.
1007
1008                 ds_put_char(ds, '/');
1009                 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1010             */
1011             ds_put_char(ds, ')');
1012         } else {
1013             ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1014                           "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1015                           ntohll(tun_key.tun_id),
1016                           IP_ARGS(tun_key.ip_src),
1017                           IP_ARGS(tun_key.ip_dst),
1018                           tun_key.ip_tos, tun_key.ip_ttl);
1019
1020             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1021             ds_put_char(ds, ')');
1022         }
1023         break;
1024
1025     case OVS_KEY_ATTR_IN_PORT:
1026         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1027         if (!is_exact) {
1028             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1029         }
1030         break;
1031
1032     case OVS_KEY_ATTR_ETHERNET:
1033         if (!is_exact) {
1034             const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1035             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1036
1037             ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1038                           ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1039                           ETH_ADDR_ARGS(eth_key->eth_src),
1040                           ETH_ADDR_ARGS(eth_mask->eth_src),
1041                           ETH_ADDR_ARGS(eth_key->eth_dst),
1042                           ETH_ADDR_ARGS(eth_mask->eth_dst));
1043         } else {
1044             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1045
1046             ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1047                           ETH_ADDR_ARGS(eth_key->eth_src),
1048                           ETH_ADDR_ARGS(eth_key->eth_dst));
1049         }
1050         break;
1051
1052     case OVS_KEY_ATTR_VLAN:
1053         {
1054             ovs_be16 vlan_tci = nl_attr_get_be16(a);
1055             if (!is_exact) {
1056                 ovs_be16 mask = nl_attr_get_be16(ma);
1057                 ds_put_format(ds, "vid=%"PRIu16"/%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1058                               vlan_tci_to_vid(vlan_tci),
1059                               vlan_tci_to_vid(mask),
1060                               vlan_tci_to_pcp(vlan_tci),
1061                               vlan_tci_to_pcp(mask),
1062                               vlan_tci_to_cfi(vlan_tci),
1063                               vlan_tci_to_cfi(mask));
1064             } else {
1065                 format_vlan_tci(ds, vlan_tci);
1066             }
1067         }
1068         break;
1069
1070     case OVS_KEY_ATTR_MPLS: {
1071         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1072         const struct ovs_key_mpls *mpls_mask = NULL;
1073         if (!is_exact) {
1074             mpls_mask = nl_attr_get(ma);
1075         }
1076         format_mpls(ds, mpls_key, mpls_mask);
1077         break;
1078     }
1079
1080     case OVS_KEY_ATTR_ETHERTYPE:
1081         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1082         if (!is_exact) {
1083             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1084         }
1085         break;
1086
1087     case OVS_KEY_ATTR_IPV4:
1088         if (!is_exact) {
1089             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1090             const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1091
1092             ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1093                           ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1094                           ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1095                           IP_ARGS(ipv4_key->ipv4_src),
1096                           IP_ARGS(ipv4_mask->ipv4_src),
1097                           IP_ARGS(ipv4_key->ipv4_dst),
1098                           IP_ARGS(ipv4_mask->ipv4_dst),
1099                           ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1100                           ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1101                           ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1102                           ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1103                           ipv4_mask->ipv4_frag);
1104         } else {
1105             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1106
1107             ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1108                           ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1109                           IP_ARGS(ipv4_key->ipv4_src),
1110                           IP_ARGS(ipv4_key->ipv4_dst),
1111                           ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1112                           ipv4_key->ipv4_ttl,
1113                           ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1114         }
1115         break;
1116
1117     case OVS_KEY_ATTR_IPV6:
1118         if (!is_exact) {
1119             const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1120             char src_str[INET6_ADDRSTRLEN];
1121             char dst_str[INET6_ADDRSTRLEN];
1122             char src_mask[INET6_ADDRSTRLEN];
1123             char dst_mask[INET6_ADDRSTRLEN];
1124
1125             ipv6_key = nl_attr_get(a);
1126             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1127             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1128
1129             ipv6_mask = nl_attr_get(ma);
1130             inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1131             inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1132
1133             ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1134                           ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1135                           ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1136                           src_str, src_mask, dst_str, dst_mask,
1137                           ntohl(ipv6_key->ipv6_label),
1138                           ntohl(ipv6_mask->ipv6_label),
1139                           ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1140                           ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1141                           ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1142                           ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1143                           ipv6_mask->ipv6_frag);
1144         } else {
1145             const struct ovs_key_ipv6 *ipv6_key;
1146             char src_str[INET6_ADDRSTRLEN];
1147             char dst_str[INET6_ADDRSTRLEN];
1148
1149             ipv6_key = nl_attr_get(a);
1150             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1151             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1152
1153             ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1154                           ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1155                           src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1156                           ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1157                           ipv6_key->ipv6_hlimit,
1158                           ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1159         }
1160         break;
1161
1162     case OVS_KEY_ATTR_TCP:
1163         if (!is_exact) {
1164             const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1165             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1166
1167             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1168                           ",dst=%"PRIu16"/%#"PRIx16,
1169                           ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1170                           ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1171         } else {
1172             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1173
1174             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1175                           ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1176         }
1177         break;
1178
1179     case OVS_KEY_ATTR_UDP:
1180         if (!is_exact) {
1181             const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1182             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1183
1184             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1185                           ",dst=%"PRIu16"/%#"PRIx16,
1186                           ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1187                           ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1188         } else {
1189             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1190
1191             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1192                           ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1193         }
1194         break;
1195
1196     case OVS_KEY_ATTR_ICMP:
1197         if (!is_exact) {
1198             const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1199             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1200
1201             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1202                           icmp_key->icmp_type, icmp_mask->icmp_type,
1203                           icmp_key->icmp_code, icmp_mask->icmp_code);
1204         } else {
1205             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1206
1207             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1208                           icmp_key->icmp_type, icmp_key->icmp_code);
1209         }
1210         break;
1211
1212     case OVS_KEY_ATTR_ICMPV6:
1213         if (!is_exact) {
1214             const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1215             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1216
1217             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1218                           icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1219                           icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1220         } else {
1221             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1222
1223             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1224                           icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1225         }
1226         break;
1227
1228     case OVS_KEY_ATTR_ARP:
1229         if (!is_exact) {
1230             const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1231             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1232
1233             ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1234                           ",op=%"PRIu16"/%#"PRIx16
1235                           ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1236                           ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1237                           IP_ARGS(arp_key->arp_sip),
1238                           IP_ARGS(arp_mask->arp_sip),
1239                           IP_ARGS(arp_key->arp_tip),
1240                           IP_ARGS(arp_mask->arp_tip),
1241                           ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1242                           ETH_ADDR_ARGS(arp_key->arp_sha),
1243                           ETH_ADDR_ARGS(arp_mask->arp_sha),
1244                           ETH_ADDR_ARGS(arp_key->arp_tha),
1245                           ETH_ADDR_ARGS(arp_mask->arp_tha));
1246         } else {
1247             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1248
1249             ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1250                           "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1251                           IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1252                           ntohs(arp_key->arp_op),
1253                           ETH_ADDR_ARGS(arp_key->arp_sha),
1254                           ETH_ADDR_ARGS(arp_key->arp_tha));
1255         }
1256         break;
1257
1258     case OVS_KEY_ATTR_ND: {
1259         const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1260         char target[INET6_ADDRSTRLEN];
1261
1262         nd_key = nl_attr_get(a);
1263         if (!is_exact) {
1264             nd_mask = nl_attr_get(ma);
1265         }
1266
1267         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1268         ds_put_format(ds, "target=%s", target);
1269         if (!is_exact) {
1270             inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1271             ds_put_format(ds, "/%s", target);
1272         }
1273
1274         if (!eth_addr_is_zero(nd_key->nd_sll)) {
1275             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1276                           ETH_ADDR_ARGS(nd_key->nd_sll));
1277             if (!is_exact) {
1278                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1279                               ETH_ADDR_ARGS(nd_mask->nd_sll));
1280             }
1281         }
1282         if (!eth_addr_is_zero(nd_key->nd_tll)) {
1283             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1284                           ETH_ADDR_ARGS(nd_key->nd_tll));
1285             if (!is_exact) {
1286                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1287                               ETH_ADDR_ARGS(nd_mask->nd_tll));
1288             }
1289         }
1290         break;
1291     }
1292
1293     case OVS_KEY_ATTR_UNSPEC:
1294     case __OVS_KEY_ATTR_MAX:
1295     default:
1296         format_generic_odp_key(a, ds);
1297         if (!is_exact) {
1298             ds_put_char(ds, '/');
1299             format_generic_odp_key(ma, ds);
1300         }
1301         break;
1302     }
1303     ds_put_char(ds, ')');
1304 }
1305
1306 static struct nlattr *
1307 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1308 {
1309     const struct nlattr *a;
1310     unsigned int left;
1311     int type = nl_attr_type(key);
1312     int size = nl_attr_get_size(key);
1313
1314     if (odp_flow_key_attr_len(type) >=0) {
1315         memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size);
1316     } else {
1317         size_t nested_mask;
1318
1319         nested_mask = nl_msg_start_nested(ofp, type);
1320         NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1321             generate_all_wildcard_mask(ofp, nl_attr_get(a));
1322         }
1323         nl_msg_end_nested(ofp, nested_mask);
1324     }
1325
1326     return ofp->base;
1327 }
1328
1329 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1330  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1331  * 'mask_len' bytes of 'mask' which apply to 'key'. */
1332 void
1333 odp_flow_format(const struct nlattr *key, size_t key_len,
1334                 const struct nlattr *mask, size_t mask_len,
1335                 struct ds *ds)
1336 {
1337     if (key_len) {
1338         const struct nlattr *a;
1339         unsigned int left;
1340         bool has_ethtype_key = false;
1341         const struct nlattr *ma = NULL;
1342         struct ofpbuf ofp;
1343
1344         ofpbuf_init(&ofp, 100);
1345         NL_ATTR_FOR_EACH (a, left, key, key_len) {
1346             if (a != key) {
1347                 ds_put_char(ds, ',');
1348             }
1349             if (nl_attr_type(a) == OVS_KEY_ATTR_ETHERTYPE) {
1350                 has_ethtype_key = true;
1351             }
1352             if (mask && mask_len) {
1353                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1354                 if (!ma) {
1355                     ma = generate_all_wildcard_mask(&ofp, a);
1356                 }
1357             }
1358             format_odp_key_attr(a, ma, ds);
1359             ofpbuf_clear(&ofp);
1360         }
1361         ofpbuf_uninit(&ofp);
1362
1363         if (left) {
1364             int i;
1365             
1366             if (left == key_len) {
1367                 ds_put_cstr(ds, "<empty>");
1368             }
1369             ds_put_format(ds, ",***%u leftover bytes*** (", left);
1370             for (i = 0; i < left; i++) {
1371                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1372             }
1373             ds_put_char(ds, ')');
1374         }
1375         if (!has_ethtype_key) {
1376             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1377             if (ma) {
1378                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1379                               ntohs(nl_attr_get_be16(ma)));
1380             }
1381         }
1382     } else {
1383         ds_put_cstr(ds, "<empty>");
1384     }
1385 }
1386
1387 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1388  * OVS_KEY_ATTR_* attributes in 'key'. */
1389 void
1390 odp_flow_key_format(const struct nlattr *key,
1391                     size_t key_len, struct ds *ds)
1392 {
1393     odp_flow_format(key, key_len, NULL, 0, ds);
1394 }
1395
1396 static void
1397 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1398        const uint8_t *nd_tll, struct ofpbuf *key)
1399 {
1400     if (nd_sll) {
1401         memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1402     }
1403
1404     if (nd_tll) {
1405         memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1406     }
1407
1408     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1409 }
1410
1411 static int
1412 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1413            const uint8_t *nd_tll, struct ofpbuf *key)
1414 {
1415     struct ovs_key_nd nd_key;
1416
1417     memset(&nd_key, 0, sizeof nd_key);
1418
1419     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1420         return -EINVAL;
1421     }
1422
1423     put_nd(&nd_key, nd_sll, nd_tll, key);
1424     return n;
1425 }
1426
1427 static int
1428 put_nd_mask(int n, const char *nd_target_s,
1429            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1430 {
1431     struct ovs_key_nd nd_mask;
1432
1433     memset(&nd_mask, 0xff, sizeof nd_mask);
1434
1435     if (strlen(nd_target_s) != 0 &&
1436             inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1437         return -EINVAL;
1438     }
1439
1440     put_nd(&nd_mask, nd_sll, nd_tll, mask);
1441     return n;
1442 }
1443
1444 static bool
1445 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1446 {
1447     if (!strcasecmp(s, "no")) {
1448         *type = OVS_FRAG_TYPE_NONE;
1449     } else if (!strcasecmp(s, "first")) {
1450         *type = OVS_FRAG_TYPE_FIRST;
1451     } else if (!strcasecmp(s, "later")) {
1452         *type = OVS_FRAG_TYPE_LATER;
1453     } else {
1454         return false;
1455     }
1456     return true;
1457 }
1458
1459 static ovs_be32
1460 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1461 {
1462     return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1463                   (mpls_tc << MPLS_TC_SHIFT)       |
1464                   (mpls_ttl << MPLS_TTL_SHIFT)     |
1465                   (mpls_bos << MPLS_BOS_SHIFT)));
1466 }
1467
1468 static int
1469 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1470                         struct ofpbuf *key, struct ofpbuf *mask)
1471 {
1472     /* Many of the sscanf calls in this function use oversized destination
1473      * fields because some sscanf() implementations truncate the range of %i
1474      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1475      * value of 0x7fff.  The other alternatives are to allow only a single
1476      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1477      * parsers.
1478      *
1479      * The tun_id parser has to use an alternative approach because there is no
1480      * type larger than 64 bits. */
1481
1482     {
1483         unsigned long long int priority;
1484         unsigned long long int priority_mask;
1485         int n = -1;
1486
1487         if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1488                    &priority_mask, &n) > 0 && n > 0) {
1489             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1490             nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1491             return n;
1492         } else if (sscanf(s, "skb_priority(%lli)%n",
1493                           &priority, &n) > 0 && n > 0) {
1494             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1495             if (mask) {
1496                 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1497             }
1498             return n;
1499         }
1500     }
1501
1502     {
1503         unsigned long long int mark;
1504         unsigned long long int mark_mask;
1505         int n = -1;
1506
1507         if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1508                    &mark_mask, &n) > 0 && n > 0) {
1509             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1510             nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1511             return n;
1512         } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1513             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1514             if (mask) {
1515                 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1516             }
1517             return n;
1518         }
1519     }
1520
1521     {
1522         char tun_id_s[32];
1523         int tos, tos_mask, ttl, ttl_mask;
1524         struct flow_tnl tun_key, tun_key_mask;
1525         unsigned long long tun_id_mask;
1526         int n = -1;
1527
1528         if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1529                    "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1530                    "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1531                    tun_id_s, &tun_id_mask,
1532                    IP_SCAN_ARGS(&tun_key.ip_src),
1533                    IP_SCAN_ARGS(&tun_key_mask.ip_src),
1534                    IP_SCAN_ARGS(&tun_key.ip_dst),
1535                    IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1536                    &tos, &tos_mask, &ttl, &ttl_mask,
1537                    &n) > 0 && n > 0) {
1538             int res;
1539             uint32_t flags;
1540
1541             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1542             tun_key_mask.tun_id = htonll(tun_id_mask);
1543             tun_key.ip_tos = tos;
1544             tun_key_mask.ip_tos = tos_mask;
1545             tun_key.ip_ttl = ttl;
1546             tun_key_mask.ip_ttl = ttl_mask;
1547             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1548             tun_key.flags = flags;
1549             tun_key_mask.flags = UINT16_MAX;
1550
1551             if (res < 0) {
1552                 return res;
1553             }
1554             n += res;
1555             if (s[n] != ')') {
1556                 return -EINVAL;
1557             }
1558             n++;
1559             tun_key_to_attr(key, &tun_key);
1560             if (mask) {
1561                 tun_key_to_attr(mask, &tun_key_mask);
1562             }
1563             return n;
1564         } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1565                    "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1566                    ",tos=%i,ttl=%i,flags%n", tun_id_s,
1567                     IP_SCAN_ARGS(&tun_key.ip_src),
1568                     IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1569                     &n) > 0 && n > 0) {
1570             int res;
1571             uint32_t flags;
1572
1573             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1574             tun_key.ip_tos = tos;
1575             tun_key.ip_ttl = ttl;
1576             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1577             tun_key.flags = flags;
1578
1579             if (res < 0) {
1580                 return res;
1581             }
1582             n += res;
1583             if (s[n] != ')') {
1584                 return -EINVAL;
1585             }
1586             n++;
1587             tun_key_to_attr(key, &tun_key);
1588
1589             if (mask) {
1590                 memset(&tun_key, 0xff, sizeof tun_key);
1591                 tun_key_to_attr(mask, &tun_key);
1592             }
1593             return n;
1594         }
1595     }
1596
1597     {
1598         unsigned long long int in_port;
1599         unsigned long long int in_port_mask;
1600         int n = -1;
1601
1602         if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1603                    &in_port_mask, &n) > 0 && n > 0) {
1604             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1605             nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1606             return n;
1607         } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1608             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1609             if (mask) {
1610                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1611             }
1612             return n;
1613         }
1614     }
1615
1616
1617     if (port_names && !strncmp(s, "in_port(", 8)) {
1618         const char *name;
1619         const struct simap_node *node;
1620         int name_len;
1621
1622         name = s + 8;
1623         name_len = strcspn(s, ")");
1624         node = simap_find_len(port_names, name, name_len);
1625         if (node) {
1626             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1627
1628             if (mask) {
1629                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1630             }
1631             return 8 + name_len + 1;
1632         }
1633     }
1634
1635     {
1636         struct ovs_key_ethernet eth_key;
1637         struct ovs_key_ethernet eth_key_mask;
1638         int n = -1;
1639
1640         if (mask && sscanf(s,
1641                    "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1642                         "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1643                 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1644                 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1645                 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1646                 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1647
1648             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1649                               &eth_key, sizeof eth_key);
1650             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1651                               &eth_key_mask, sizeof eth_key_mask);
1652             return n;
1653         } else if (sscanf(s,
1654                    "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1655                    ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1656                    ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1657             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1658                               &eth_key, sizeof eth_key);
1659
1660             if (mask) {
1661                 memset(&eth_key, 0xff, sizeof eth_key);
1662                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1663                               &eth_key, sizeof eth_key);
1664             }
1665             return n;
1666         }
1667     }
1668
1669     {
1670         uint16_t vid, vid_mask;
1671         int pcp, pcp_mask;
1672         int cfi, cfi_mask;
1673         int n = -1;
1674
1675         if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1676                             &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1677             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1678                             htons((vid << VLAN_VID_SHIFT) |
1679                                   (pcp << VLAN_PCP_SHIFT) |
1680                                   VLAN_CFI));
1681             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1682                             htons((vid_mask << VLAN_VID_SHIFT) |
1683                                   (pcp_mask << VLAN_PCP_SHIFT) |
1684                                   (1 << VLAN_CFI_SHIFT)));
1685             return n;
1686         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1687                            &vid, &pcp, &n) > 0 && n > 0)) {
1688             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1689                             htons((vid << VLAN_VID_SHIFT) |
1690                                   (pcp << VLAN_PCP_SHIFT) |
1691                                   VLAN_CFI));
1692             if (mask) {
1693                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1694             }
1695             return n;
1696         } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1697                                    &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1698             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1699                             htons((vid << VLAN_VID_SHIFT) |
1700                                   (pcp << VLAN_PCP_SHIFT) |
1701                                   (cfi ? VLAN_CFI : 0)));
1702             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1703                             htons((vid_mask << VLAN_VID_SHIFT) |
1704                                   (pcp_mask << VLAN_PCP_SHIFT) |
1705                                   (cfi_mask << VLAN_CFI_SHIFT)));
1706             return n;
1707         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1708                            &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1709             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1710                             htons((vid << VLAN_VID_SHIFT) |
1711                                   (pcp << VLAN_PCP_SHIFT) |
1712                                   (cfi ? VLAN_CFI : 0)));
1713             if (mask) {
1714                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1715             }
1716             return n;
1717         }
1718     }
1719
1720     {
1721         int eth_type;
1722         int eth_type_mask;
1723         int n = -1;
1724
1725         if (mask && sscanf(s, "eth_type(%i/%i)%n",
1726                    &eth_type, &eth_type_mask, &n) > 0 && n > 0) {
1727             if (eth_type != 0) {
1728                 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1729             }
1730             nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1731             return n;
1732         } else if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1733             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1734             if (mask) {
1735                 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE,
1736                                 htons(UINT16_MAX));
1737             }
1738             return n;
1739         }
1740     }
1741
1742     {
1743         int label, tc, ttl, bos;
1744         int label_mask, tc_mask, ttl_mask, bos_mask;
1745         int n = -1;
1746
1747         if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1748                     &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1749             struct ovs_key_mpls *mpls, *mpls_mask;
1750
1751             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1752                                             sizeof *mpls);
1753             mpls->mpls_top_lse = mpls_lse_from_components(label, tc, ttl, bos);
1754
1755             mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1756                                             sizeof *mpls_mask);
1757             mpls_mask->mpls_top_lse = mpls_lse_from_components(
1758                                       label_mask, tc_mask, ttl_mask, bos_mask);
1759             return n;
1760         } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1761                     &label, &tc, &ttl, &bos, &n) > 0 &&
1762                     n > 0) {
1763             struct ovs_key_mpls *mpls;
1764
1765             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1766                                             sizeof *mpls);
1767             mpls->mpls_top_lse = mpls_lse_from_components(label, tc, ttl, bos);
1768             if (mask) {
1769                 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1770                                             sizeof *mpls);
1771                 mpls->mpls_top_lse = htonl(UINT32_MAX);
1772             }
1773             return n;
1774         }
1775     }
1776
1777
1778     {
1779         ovs_be32 ipv4_src, ipv4_src_mask;
1780         ovs_be32 ipv4_dst, ipv4_dst_mask;
1781         int ipv4_proto, ipv4_proto_mask;
1782         int ipv4_tos, ipv4_tos_mask;
1783         int ipv4_ttl, ipv4_ttl_mask;
1784         char frag[8];
1785         int  ipv4_frag_mask;
1786         enum ovs_frag_type ipv4_frag;
1787         int n = -1;
1788
1789         if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1790                       "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1791                       "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1792                       "frag=%7[a-z]/%i)%n",
1793                       IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1794                       IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1795                       &ipv4_proto, &ipv4_proto_mask,
1796                       &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1797                       frag, &ipv4_frag_mask, &n) > 0
1798             && n > 0
1799             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1800             struct ovs_key_ipv4 ipv4_key;
1801             struct ovs_key_ipv4 ipv4_mask;
1802
1803             ipv4_key.ipv4_src = ipv4_src;
1804             ipv4_key.ipv4_dst = ipv4_dst;
1805             ipv4_key.ipv4_proto = ipv4_proto;
1806             ipv4_key.ipv4_tos = ipv4_tos;
1807             ipv4_key.ipv4_ttl = ipv4_ttl;
1808             ipv4_key.ipv4_frag = ipv4_frag;
1809             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1810                               &ipv4_key, sizeof ipv4_key);
1811
1812             ipv4_mask.ipv4_src = ipv4_src_mask;
1813             ipv4_mask.ipv4_dst = ipv4_dst_mask;
1814             ipv4_mask.ipv4_proto = ipv4_proto_mask;
1815             ipv4_mask.ipv4_tos = ipv4_tos_mask;
1816             ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1817             ipv4_mask.ipv4_frag = ipv4_frag_mask;
1818             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1819                               &ipv4_mask, sizeof ipv4_mask);
1820             return n;
1821         } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1822                    "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1823                    IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1824                    &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1825             && n > 0
1826             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1827             struct ovs_key_ipv4 ipv4_key;
1828
1829             ipv4_key.ipv4_src = ipv4_src;
1830             ipv4_key.ipv4_dst = ipv4_dst;
1831             ipv4_key.ipv4_proto = ipv4_proto;
1832             ipv4_key.ipv4_tos = ipv4_tos;
1833             ipv4_key.ipv4_ttl = ipv4_ttl;
1834             ipv4_key.ipv4_frag = ipv4_frag;
1835             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1836                               &ipv4_key, sizeof ipv4_key);
1837
1838             if (mask) {
1839                 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1840                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1841                               &ipv4_key, sizeof ipv4_key);
1842             }
1843             return n;
1844         }
1845     }
1846
1847     {
1848         char ipv6_src_s[IPV6_SCAN_LEN + 1];
1849         char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1850         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1851         char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1852         int ipv6_label, ipv6_label_mask;
1853         int ipv6_proto, ipv6_proto_mask;
1854         int ipv6_tclass, ipv6_tclass_mask;
1855         int ipv6_hlimit, ipv6_hlimit_mask;
1856         char frag[8];
1857         enum ovs_frag_type ipv6_frag;
1858         int ipv6_frag_mask;
1859         int n = -1;
1860
1861         if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1862                    IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1863                    "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1864                    "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1865                    ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1866                    &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1867                    &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1868                    &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1869                    &ipv6_frag_mask, &n) > 0
1870             && n > 0
1871             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1872             struct ovs_key_ipv6 ipv6_key;
1873             struct ovs_key_ipv6 ipv6_mask;
1874
1875             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1876                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1877                 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1878                 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1879                 return -EINVAL;
1880             }
1881
1882             ipv6_key.ipv6_label = htonl(ipv6_label);
1883             ipv6_key.ipv6_proto = ipv6_proto;
1884             ipv6_key.ipv6_tclass = ipv6_tclass;
1885             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1886             ipv6_key.ipv6_frag = ipv6_frag;
1887             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1888                               &ipv6_key, sizeof ipv6_key);
1889
1890             ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1891             ipv6_mask.ipv6_proto = ipv6_proto_mask;
1892             ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1893             ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1894             ipv6_mask.ipv6_frag = ipv6_frag_mask;
1895             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1896                               &ipv6_mask, sizeof ipv6_mask);
1897             return n;
1898         } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1899                    "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1900                    ipv6_src_s, ipv6_dst_s, &ipv6_label,
1901                    &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1902             && n > 0
1903             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1904             struct ovs_key_ipv6 ipv6_key;
1905
1906             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1907                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1908                 return -EINVAL;
1909             }
1910             ipv6_key.ipv6_label = htonl(ipv6_label);
1911             ipv6_key.ipv6_proto = ipv6_proto;
1912             ipv6_key.ipv6_tclass = ipv6_tclass;
1913             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1914             ipv6_key.ipv6_frag = ipv6_frag;
1915             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1916                               &ipv6_key, sizeof ipv6_key);
1917
1918             if (mask) {
1919                 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1920                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1921                               &ipv6_key, sizeof ipv6_key);
1922             }
1923             return n;
1924         }
1925     }
1926
1927     {
1928         int tcp_src;
1929         int tcp_dst;
1930         int tcp_src_mask;
1931         int tcp_dst_mask;
1932         int n = -1;
1933
1934         if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1935                    &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1936             && n > 0) {
1937             struct ovs_key_tcp tcp_key;
1938             struct ovs_key_tcp tcp_mask;
1939
1940             tcp_key.tcp_src = htons(tcp_src);
1941             tcp_key.tcp_dst = htons(tcp_dst);
1942             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1943
1944             tcp_mask.tcp_src = htons(tcp_src_mask);
1945             tcp_mask.tcp_dst = htons(tcp_dst_mask);
1946             nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1947                               &tcp_mask, sizeof tcp_mask);
1948             return n;
1949         } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1950             && n > 0) {
1951             struct ovs_key_tcp tcp_key;
1952
1953             tcp_key.tcp_src = htons(tcp_src);
1954             tcp_key.tcp_dst = htons(tcp_dst);
1955             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1956
1957             if (mask) {
1958                 memset(&tcp_key, 0xff, sizeof tcp_key);
1959                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1960                               &tcp_key, sizeof tcp_key);
1961             }
1962             return n;
1963         }
1964     }
1965
1966     {
1967         int udp_src;
1968         int udp_dst;
1969         int udp_src_mask;
1970         int udp_dst_mask;
1971         int n = -1;
1972
1973         if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
1974                    &udp_src, &udp_src_mask,
1975                    &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
1976             struct ovs_key_udp udp_key;
1977             struct ovs_key_udp udp_mask;
1978
1979             udp_key.udp_src = htons(udp_src);
1980             udp_key.udp_dst = htons(udp_dst);
1981             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1982
1983             udp_mask.udp_src = htons(udp_src_mask);
1984             udp_mask.udp_dst = htons(udp_dst_mask);
1985             nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
1986                               &udp_mask, sizeof udp_mask);
1987             return n;
1988         }
1989         if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1990             && n > 0) {
1991             struct ovs_key_udp udp_key;
1992
1993             udp_key.udp_src = htons(udp_src);
1994             udp_key.udp_dst = htons(udp_dst);
1995             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1996
1997             if (mask) {
1998                 memset(&udp_key, 0xff, sizeof udp_key);
1999                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2000             }
2001             return n;
2002         }
2003     }
2004
2005     {
2006         int icmp_type;
2007         int icmp_code;
2008         int icmp_type_mask;
2009         int icmp_code_mask;
2010         int n = -1;
2011
2012         if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2013                    &icmp_type, &icmp_type_mask,
2014                    &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2015             struct ovs_key_icmp icmp_key;
2016             struct ovs_key_icmp icmp_mask;
2017
2018             icmp_key.icmp_type = icmp_type;
2019             icmp_key.icmp_code = icmp_code;
2020             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2021                               &icmp_key, sizeof icmp_key);
2022
2023             icmp_mask.icmp_type = icmp_type_mask;
2024             icmp_mask.icmp_code = icmp_code_mask;
2025             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2026                               &icmp_mask, sizeof icmp_mask);
2027             return n;
2028         } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2029                    &icmp_type, &icmp_code, &n) > 0
2030             && n > 0) {
2031             struct ovs_key_icmp icmp_key;
2032
2033             icmp_key.icmp_type = icmp_type;
2034             icmp_key.icmp_code = icmp_code;
2035             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2036                               &icmp_key, sizeof icmp_key);
2037             if (mask) {
2038                 memset(&icmp_key, 0xff, sizeof icmp_key);
2039                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2040                               sizeof icmp_key);
2041             }
2042             return n;
2043         }
2044     }
2045
2046     {
2047         struct ovs_key_icmpv6 icmpv6_key;
2048         struct ovs_key_icmpv6 icmpv6_mask;
2049         int icmpv6_type_mask;
2050         int icmpv6_code_mask;
2051         int n = -1;
2052
2053         if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2054                    &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2055                    &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2056             && n > 0) {
2057             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2058                               &icmpv6_key, sizeof icmpv6_key);
2059
2060             icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2061             icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2062             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2063                               sizeof icmpv6_mask);
2064             return n;
2065         } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2066                    &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2067             && n > 0) {
2068             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2069                               &icmpv6_key, sizeof icmpv6_key);
2070
2071             if (mask) {
2072                 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2073                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2074                               sizeof icmpv6_key);
2075             }
2076             return n;
2077         }
2078     }
2079
2080     {
2081         ovs_be32 arp_sip, arp_sip_mask;
2082         ovs_be32 arp_tip, arp_tip_mask;
2083         int arp_op, arp_op_mask;
2084         uint8_t arp_sha[ETH_ADDR_LEN];
2085         uint8_t arp_sha_mask[ETH_ADDR_LEN];
2086         uint8_t arp_tha[ETH_ADDR_LEN];
2087         uint8_t arp_tha_mask[ETH_ADDR_LEN];
2088         int n = -1;
2089
2090         if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2091                    "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2092                    "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2093                    "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2094                    IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2095                    IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2096                    &arp_op, &arp_op_mask,
2097                    ETH_ADDR_SCAN_ARGS(arp_sha),
2098                    ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2099                    ETH_ADDR_SCAN_ARGS(arp_tha),
2100                    ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2101             struct ovs_key_arp arp_key;
2102             struct ovs_key_arp arp_mask;
2103
2104             memset(&arp_key, 0, sizeof arp_key);
2105             arp_key.arp_sip = arp_sip;
2106             arp_key.arp_tip = arp_tip;
2107             arp_key.arp_op = htons(arp_op);
2108             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2109             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2110             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2111
2112             arp_mask.arp_sip = arp_sip_mask;
2113             arp_mask.arp_tip = arp_tip_mask;
2114             arp_mask.arp_op = htons(arp_op_mask);
2115             memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2116             memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2117             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2118                               &arp_mask, sizeof arp_mask);
2119             return n;
2120         } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2121                    "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2122                    IP_SCAN_ARGS(&arp_sip),
2123                    IP_SCAN_ARGS(&arp_tip),
2124                    &arp_op,
2125                    ETH_ADDR_SCAN_ARGS(arp_sha),
2126                    ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2127             struct ovs_key_arp arp_key;
2128
2129             memset(&arp_key, 0, sizeof arp_key);
2130             arp_key.arp_sip = arp_sip;
2131             arp_key.arp_tip = arp_tip;
2132             arp_key.arp_op = htons(arp_op);
2133             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2134             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2135             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2136
2137             if (mask) {
2138                 memset(&arp_key, 0xff, sizeof arp_key);
2139                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2140                                   &arp_key, sizeof arp_key);
2141             }
2142             return n;
2143         }
2144     }
2145
2146     {
2147         char nd_target_s[IPV6_SCAN_LEN + 1];
2148         char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2149         uint8_t nd_sll[ETH_ADDR_LEN];
2150         uint8_t nd_sll_mask[ETH_ADDR_LEN];
2151         uint8_t nd_tll[ETH_ADDR_LEN];
2152         uint8_t nd_tll_mask[ETH_ADDR_LEN];
2153         int n = -1;
2154
2155         nd_target_mask_s[0] = 0;
2156         memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2157         memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2158
2159         if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2160                    nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2161                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2162                 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2163         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2164                    nd_target_s, &n) > 0 && n > 0) {
2165                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2166                 if (mask) {
2167                     put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2168                 }
2169         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2170                          ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2171                    nd_target_s, nd_target_mask_s,
2172                    ETH_ADDR_SCAN_ARGS(nd_sll),
2173                    ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2174             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2175             put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2176         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2177                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2178             && n > 0) {
2179             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2180             if (mask) {
2181                 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2182             }
2183         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2184                          ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2185                    nd_target_s, nd_target_mask_s,
2186                    ETH_ADDR_SCAN_ARGS(nd_tll),
2187                    ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2188             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2189             put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2190         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2191                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2192             && n > 0) {
2193             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2194             if (mask) {
2195                 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2196             }
2197         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2198                    ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2199                    "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2200                    nd_target_s, nd_target_mask_s,
2201                    ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2202                    ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2203                    &n) > 0
2204             && n > 0) {
2205             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2206             put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2207         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2208                    "tll="ETH_ADDR_SCAN_FMT")%n",
2209                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2210                    ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2211             && n > 0) {
2212             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2213             if (mask) {
2214                 put_nd_mask(n, nd_target_mask_s,
2215                             nd_sll_mask, nd_tll_mask, mask);
2216             }
2217         }
2218
2219         if (n != -1)
2220             return n;
2221
2222     }
2223
2224     if (!strncmp(s, "encap(", 6)) {
2225         const char *start = s;
2226         size_t encap, encap_mask = 0;
2227
2228         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2229         if (mask) {
2230             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2231         }
2232
2233         s += 6;
2234         for (;;) {
2235             int retval;
2236
2237             s += strspn(s, ", \t\r\n");
2238             if (!*s) {
2239                 return -EINVAL;
2240             } else if (*s == ')') {
2241                 break;
2242             }
2243
2244             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2245             if (retval < 0) {
2246                 return retval;
2247             }
2248             s += retval;
2249         }
2250         s++;
2251
2252         nl_msg_end_nested(key, encap);
2253         if (mask) {
2254             nl_msg_end_nested(mask, encap_mask);
2255         }
2256
2257         return s - start;
2258     }
2259
2260     return -EINVAL;
2261 }
2262
2263 /* Parses the string representation of a datapath flow key, in the
2264  * format output by odp_flow_key_format().  Returns 0 if successful,
2265  * otherwise a positive errno value.  On success, the flow key is
2266  * appended to 'key' as a series of Netlink attributes.  On failure, no
2267  * data is appended to 'key'.  Either way, 'key''s data might be
2268  * reallocated.
2269  *
2270  * If 'port_names' is nonnull, it points to an simap that maps from a port name
2271  * to a port number.  (Port names may be used instead of port numbers in
2272  * in_port.)
2273  *
2274  * On success, the attributes appended to 'key' are individually syntactically
2275  * valid, but they may not be valid as a sequence.  'key' might, for example,
2276  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
2277 int
2278 odp_flow_from_string(const char *s, const struct simap *port_names,
2279                      struct ofpbuf *key, struct ofpbuf *mask)
2280 {
2281     const size_t old_size = key->size;
2282     for (;;) {
2283         int retval;
2284
2285         s += strspn(s, delimiters);
2286         if (!*s) {
2287             return 0;
2288         }
2289
2290         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2291         if (retval < 0) {
2292             key->size = old_size;
2293             return -retval;
2294         }
2295         s += retval;
2296     }
2297
2298     return 0;
2299 }
2300
2301 static uint8_t
2302 ovs_to_odp_frag(uint8_t nw_frag)
2303 {
2304     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2305           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2306           : OVS_FRAG_TYPE_LATER);
2307 }
2308
2309 static void
2310 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2311                          const struct flow *flow, uint32_t odp_in_port)
2312 {
2313     bool is_mask;
2314     struct ovs_key_ethernet *eth_key;
2315     size_t encap;
2316
2317     /* We assume that if 'data' and 'flow' are not the same, we should
2318      * treat 'data' as a mask. */
2319     is_mask = (data != flow);
2320
2321     if (flow->skb_priority) {
2322         nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2323     }
2324
2325     if (flow->tunnel.ip_dst) {
2326         tun_key_to_attr(buf, &data->tunnel);
2327     }
2328
2329     if (flow->skb_mark) {
2330         nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->skb_mark);
2331     }
2332
2333     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2334      * is not the magical value "OVSP_NONE". */
2335     if (is_mask || odp_in_port != OVSP_NONE) {
2336         nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2337     }
2338
2339     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2340                                        sizeof *eth_key);
2341     memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2342     memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2343
2344     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2345         if (is_mask) {
2346             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2347         } else {
2348             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2349         }
2350         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2351         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2352         if (flow->vlan_tci == htons(0)) {
2353             goto unencap;
2354         }
2355     } else {
2356         encap = 0;
2357     }
2358
2359     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2360         /* For backwards compatibility with kernels that don't support
2361          * wildcarding, the following convention is used to encode the
2362          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2363          *
2364          *   key      mask    matches
2365          * -------- --------  -------
2366          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
2367          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
2368          *  <none>   0xffff   Any non-Ethernet II frame (except valid
2369          *                    802.3 SNAP packet with valid eth_type).
2370          */
2371         if (is_mask) {
2372             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2373         }
2374         goto unencap;
2375     }
2376
2377     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2378
2379     if (flow->dl_type == htons(ETH_TYPE_IP)) {
2380         struct ovs_key_ipv4 *ipv4_key;
2381
2382         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2383                                             sizeof *ipv4_key);
2384         ipv4_key->ipv4_src = data->nw_src;
2385         ipv4_key->ipv4_dst = data->nw_dst;
2386         ipv4_key->ipv4_proto = data->nw_proto;
2387         ipv4_key->ipv4_tos = data->nw_tos;
2388         ipv4_key->ipv4_ttl = data->nw_ttl;
2389         ipv4_key->ipv4_frag = ovs_to_odp_frag(data->nw_frag);
2390     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2391         struct ovs_key_ipv6 *ipv6_key;
2392
2393         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2394                                             sizeof *ipv6_key);
2395         memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2396         memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2397         ipv6_key->ipv6_label = data->ipv6_label;
2398         ipv6_key->ipv6_proto = data->nw_proto;
2399         ipv6_key->ipv6_tclass = data->nw_tos;
2400         ipv6_key->ipv6_hlimit = data->nw_ttl;
2401         ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
2402     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2403                flow->dl_type == htons(ETH_TYPE_RARP)) {
2404         struct ovs_key_arp *arp_key;
2405
2406         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2407                                            sizeof *arp_key);
2408         memset(arp_key, 0, sizeof *arp_key);
2409         arp_key->arp_sip = data->nw_src;
2410         arp_key->arp_tip = data->nw_dst;
2411         arp_key->arp_op = htons(data->nw_proto);
2412         memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2413         memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2414     }
2415
2416     if (flow->mpls_depth) {
2417         struct ovs_key_mpls *mpls_key;
2418
2419         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2420                                             sizeof *mpls_key);
2421         mpls_key->mpls_top_lse = flow->mpls_lse;
2422     }
2423
2424     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2425         if (flow->nw_proto == IPPROTO_TCP) {
2426             struct ovs_key_tcp *tcp_key;
2427
2428             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2429                                                sizeof *tcp_key);
2430             tcp_key->tcp_src = data->tp_src;
2431             tcp_key->tcp_dst = data->tp_dst;
2432         } else if (flow->nw_proto == IPPROTO_UDP) {
2433             struct ovs_key_udp *udp_key;
2434
2435             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2436                                                sizeof *udp_key);
2437             udp_key->udp_src = data->tp_src;
2438             udp_key->udp_dst = data->tp_dst;
2439         } else if (flow->dl_type == htons(ETH_TYPE_IP)
2440                 && flow->nw_proto == IPPROTO_ICMP) {
2441             struct ovs_key_icmp *icmp_key;
2442
2443             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2444                                                 sizeof *icmp_key);
2445             icmp_key->icmp_type = ntohs(data->tp_src);
2446             icmp_key->icmp_code = ntohs(data->tp_dst);
2447         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2448                 && flow->nw_proto == IPPROTO_ICMPV6) {
2449             struct ovs_key_icmpv6 *icmpv6_key;
2450
2451             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2452                                                   sizeof *icmpv6_key);
2453             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2454             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2455
2456             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
2457                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
2458                 struct ovs_key_nd *nd_key;
2459
2460                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2461                                                     sizeof *nd_key);
2462                 memcpy(nd_key->nd_target, &data->nd_target,
2463                         sizeof nd_key->nd_target);
2464                 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2465                 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2466             }
2467         }
2468     }
2469
2470 unencap:
2471     if (encap) {
2472         nl_msg_end_nested(buf, encap);
2473     }
2474 }
2475
2476 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2477  * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2478  * number rather than a datapath port number).  Instead, if 'odp_in_port'
2479  * is anything other than ODPP_NONE, it is included in 'buf' as the input
2480  * port.
2481  *
2482  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2483  * capable of being expanded to allow for that much space. */
2484 void
2485 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2486                        uint32_t odp_in_port)
2487 {
2488     odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2489 }
2490
2491 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2492  * 'buf'.  'flow' is used as a template to determine how to interpret
2493  * 'mask'.  For example, the 'dl_type' of 'mask' describes the mask, but
2494  * it doesn't indicate whether the other fields should be interpreted as
2495  * ARP, IPv4, IPv6, etc.
2496  *
2497  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2498  * capable of being expanded to allow for that much space. */
2499 void
2500 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2501                        const struct flow *flow, uint32_t odp_in_port_mask)
2502 {
2503     odp_flow_key_from_flow__(buf, mask, flow, odp_in_port_mask);
2504 }
2505
2506 uint32_t
2507 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2508 {
2509     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2510     return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
2511 }
2512
2513 static void
2514 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2515                        uint64_t attrs, int out_of_range_attr,
2516                        const struct nlattr *key, size_t key_len)
2517 {
2518     struct ds s;
2519     int i;
2520
2521     if (VLOG_DROP_DBG(rl)) {
2522         return;
2523     }
2524
2525     ds_init(&s);
2526     for (i = 0; i < 64; i++) {
2527         if (attrs & (UINT64_C(1) << i)) {
2528             ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
2529         }
2530     }
2531     if (out_of_range_attr) {
2532         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2533     }
2534
2535     ds_put_cstr(&s, ": ");
2536     odp_flow_key_format(key, key_len, &s);
2537
2538     VLOG_DBG("%s:%s", title, ds_cstr(&s));
2539     ds_destroy(&s);
2540 }
2541
2542 static bool
2543 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2544 {
2545     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2546
2547     if (odp_frag > OVS_FRAG_TYPE_LATER) {
2548         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2549         return false;
2550     }
2551
2552     if (odp_frag != OVS_FRAG_TYPE_NONE) {
2553         flow->nw_frag |= FLOW_NW_FRAG_ANY;
2554         if (odp_frag == OVS_FRAG_TYPE_LATER) {
2555             flow->nw_frag |= FLOW_NW_FRAG_LATER;
2556         }
2557     }
2558     return true;
2559 }
2560
2561 static bool
2562 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2563                    const struct nlattr *attrs[], uint64_t *present_attrsp,
2564                    int *out_of_range_attrp)
2565 {
2566     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2567     const struct nlattr *nla;
2568     uint64_t present_attrs;
2569     size_t left;
2570
2571     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2572     present_attrs = 0;
2573     *out_of_range_attrp = 0;
2574     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2575         uint16_t type = nl_attr_type(nla);
2576         size_t len = nl_attr_get_size(nla);
2577         int expected_len = odp_flow_key_attr_len(type);
2578
2579         if (len != expected_len && expected_len >= 0) {
2580             VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2581                         "length %d", ovs_key_attr_to_string(type),
2582                         len, expected_len);
2583             return false;
2584         }
2585
2586         if (type > OVS_KEY_ATTR_MAX) {
2587             *out_of_range_attrp = type;
2588         } else {
2589             if (present_attrs & (UINT64_C(1) << type)) {
2590                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2591                             ovs_key_attr_to_string(type));
2592                 return false;
2593             }
2594
2595             present_attrs |= UINT64_C(1) << type;
2596             attrs[type] = nla;
2597         }
2598     }
2599     if (left) {
2600         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2601         return false;
2602     }
2603
2604     *present_attrsp = present_attrs;
2605     return true;
2606 }
2607
2608 static enum odp_key_fitness
2609 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2610                    uint64_t expected_attrs,
2611                    const struct nlattr *key, size_t key_len)
2612 {
2613     uint64_t missing_attrs;
2614     uint64_t extra_attrs;
2615
2616     missing_attrs = expected_attrs & ~present_attrs;
2617     if (missing_attrs) {
2618         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2619         log_odp_key_attributes(&rl, "expected but not present",
2620                                missing_attrs, 0, key, key_len);
2621         return ODP_FIT_TOO_LITTLE;
2622     }
2623
2624     extra_attrs = present_attrs & ~expected_attrs;
2625     if (extra_attrs || out_of_range_attr) {
2626         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2627         log_odp_key_attributes(&rl, "present but not expected",
2628                                extra_attrs, out_of_range_attr, key, key_len);
2629         return ODP_FIT_TOO_MUCH;
2630     }
2631
2632     return ODP_FIT_PERFECT;
2633 }
2634
2635 static bool
2636 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2637                 uint64_t present_attrs, uint64_t *expected_attrs,
2638                 struct flow *flow)
2639 {
2640     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2641
2642     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2643         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2644         if (ntohs(flow->dl_type) < 1536) {
2645             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2646                         ntohs(flow->dl_type));
2647             return false;
2648         }
2649         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2650     } else {
2651         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2652     }
2653     return true;
2654 }
2655
2656 static enum odp_key_fitness
2657 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2658                   uint64_t present_attrs, int out_of_range_attr,
2659                   uint64_t expected_attrs, struct flow *flow,
2660                   const struct nlattr *key, size_t key_len)
2661 {
2662     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2663
2664     if (eth_type_mpls(flow->dl_type)) {
2665         expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2666
2667         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2668             return ODP_FIT_TOO_LITTLE;
2669         }
2670         flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2671         flow->mpls_depth++;
2672     } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
2673         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2674         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2675             const struct ovs_key_ipv4 *ipv4_key;
2676
2677             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2678             flow->nw_src = ipv4_key->ipv4_src;
2679             flow->nw_dst = ipv4_key->ipv4_dst;
2680             flow->nw_proto = ipv4_key->ipv4_proto;
2681             flow->nw_tos = ipv4_key->ipv4_tos;
2682             flow->nw_ttl = ipv4_key->ipv4_ttl;
2683             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2684                 return ODP_FIT_ERROR;
2685             }
2686         }
2687     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2688         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2689         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2690             const struct ovs_key_ipv6 *ipv6_key;
2691
2692             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2693             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2694             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2695             flow->ipv6_label = ipv6_key->ipv6_label;
2696             flow->nw_proto = ipv6_key->ipv6_proto;
2697             flow->nw_tos = ipv6_key->ipv6_tclass;
2698             flow->nw_ttl = ipv6_key->ipv6_hlimit;
2699             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2700                 return ODP_FIT_ERROR;
2701             }
2702         }
2703     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2704                flow->dl_type == htons(ETH_TYPE_RARP)) {
2705         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2706         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2707             const struct ovs_key_arp *arp_key;
2708
2709             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2710             flow->nw_src = arp_key->arp_sip;
2711             flow->nw_dst = arp_key->arp_tip;
2712             if (arp_key->arp_op & htons(0xff00)) {
2713                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2714                             "key", ntohs(arp_key->arp_op));
2715                 return ODP_FIT_ERROR;
2716             }
2717             flow->nw_proto = ntohs(arp_key->arp_op);
2718             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2719             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2720         }
2721     }
2722
2723     if (flow->nw_proto == IPPROTO_TCP
2724         && (flow->dl_type == htons(ETH_TYPE_IP) ||
2725             flow->dl_type == htons(ETH_TYPE_IPV6))
2726         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2727         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2728         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2729             const struct ovs_key_tcp *tcp_key;
2730
2731             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2732             flow->tp_src = tcp_key->tcp_src;
2733             flow->tp_dst = tcp_key->tcp_dst;
2734         }
2735     } else if (flow->nw_proto == IPPROTO_UDP
2736                && (flow->dl_type == htons(ETH_TYPE_IP) ||
2737                    flow->dl_type == htons(ETH_TYPE_IPV6))
2738                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2739         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2740         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2741             const struct ovs_key_udp *udp_key;
2742
2743             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2744             flow->tp_src = udp_key->udp_src;
2745             flow->tp_dst = udp_key->udp_dst;
2746         }
2747     } else if (flow->nw_proto == IPPROTO_ICMP
2748                && flow->dl_type == htons(ETH_TYPE_IP)
2749                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2750         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2751         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2752             const struct ovs_key_icmp *icmp_key;
2753
2754             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2755             flow->tp_src = htons(icmp_key->icmp_type);
2756             flow->tp_dst = htons(icmp_key->icmp_code);
2757         }
2758     } else if (flow->nw_proto == IPPROTO_ICMPV6
2759                && flow->dl_type == htons(ETH_TYPE_IPV6)
2760                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2761         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2762         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2763             const struct ovs_key_icmpv6 *icmpv6_key;
2764
2765             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2766             flow->tp_src = htons(icmpv6_key->icmpv6_type);
2767             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2768
2769             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2770                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2771                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2772                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2773                     const struct ovs_key_nd *nd_key;
2774
2775                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2776                     memcpy(&flow->nd_target, nd_key->nd_target,
2777                            sizeof flow->nd_target);
2778                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2779                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2780                 }
2781             }
2782         }
2783     }
2784
2785     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
2786                               key, key_len);
2787 }
2788
2789 /* Parse 802.1Q header then encapsulated L3 attributes. */
2790 static enum odp_key_fitness
2791 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2792                    uint64_t present_attrs, int out_of_range_attr,
2793                    uint64_t expected_attrs, struct flow *flow,
2794                    const struct nlattr *key, size_t key_len)
2795 {
2796     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2797
2798     const struct nlattr *encap
2799         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
2800            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
2801     enum odp_key_fitness encap_fitness;
2802     enum odp_key_fitness fitness;
2803     ovs_be16 tci;
2804
2805     /* Calulate fitness of outer attributes. */
2806     expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
2807                        (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
2808     fitness = check_expectations(present_attrs, out_of_range_attr,
2809                                  expected_attrs, key, key_len);
2810
2811     /* Get the VLAN TCI value. */
2812     if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
2813         return ODP_FIT_TOO_LITTLE;
2814     }
2815     tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
2816     if (tci == htons(0)) {
2817         /* Corner case for a truncated 802.1Q header. */
2818         if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
2819             return ODP_FIT_TOO_MUCH;
2820         }
2821         return fitness;
2822     } else if (!(tci & htons(VLAN_CFI))) {
2823         VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
2824                     "but CFI bit is not set", ntohs(tci));
2825         return ODP_FIT_ERROR;
2826     }
2827
2828     /* Set vlan_tci.
2829      * Remove the TPID from dl_type since it's not the real Ethertype.  */
2830     flow->vlan_tci = tci;
2831     flow->dl_type = htons(0);
2832
2833     /* Now parse the encapsulated attributes. */
2834     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
2835                             attrs, &present_attrs, &out_of_range_attr)) {
2836         return ODP_FIT_ERROR;
2837     }
2838     expected_attrs = 0;
2839
2840     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2841         return ODP_FIT_ERROR;
2842     }
2843     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2844                                       expected_attrs, flow, key, key_len);
2845
2846     /* The overall fitness is the worse of the outer and inner attributes. */
2847     return MAX(fitness, encap_fitness);
2848 }
2849
2850 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2851  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
2852  * 'key' fits our expectations for what a flow key should contain.
2853  *
2854  * The 'in_port' will be the datapath's understanding of the port.  The
2855  * caller will need to translate with odp_port_to_ofp_port() if the
2856  * OpenFlow port is needed.
2857  *
2858  * This function doesn't take the packet itself as an argument because none of
2859  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
2860  * it is always possible to infer which additional attribute(s) should appear
2861  * by looking at the attributes for lower-level protocols, e.g. if the network
2862  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2863  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2864  * must be absent. */
2865 enum odp_key_fitness
2866 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2867                      struct flow *flow)
2868 {
2869     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2870     uint64_t expected_attrs;
2871     uint64_t present_attrs;
2872     int out_of_range_attr;
2873
2874     memset(flow, 0, sizeof *flow);
2875
2876     /* Parse attributes. */
2877     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2878                             &out_of_range_attr)) {
2879         return ODP_FIT_ERROR;
2880     }
2881     expected_attrs = 0;
2882
2883     /* Metadata. */
2884     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2885         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2886         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2887     }
2888
2889     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2890         flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2891         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2892     }
2893
2894     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2895         enum odp_key_fitness res;
2896
2897         res = tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2898         if (res == ODP_FIT_ERROR) {
2899             return ODP_FIT_ERROR;
2900         } else if (res == ODP_FIT_PERFECT) {
2901             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2902         }
2903     }
2904
2905     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2906         flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
2907         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2908     } else {
2909         flow->in_port = OVSP_NONE;
2910     }
2911
2912     /* Ethernet header. */
2913     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2914         const struct ovs_key_ethernet *eth_key;
2915
2916         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2917         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2918         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2919     }
2920     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2921
2922     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2923     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2924         return ODP_FIT_ERROR;
2925     }
2926
2927     if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2928         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2929                                   expected_attrs, flow, key, key_len);
2930     }
2931     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2932                              expected_attrs, flow, key, key_len);
2933 }
2934
2935 /* Returns 'fitness' as a string, for use in debug messages. */
2936 const char *
2937 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2938 {
2939     switch (fitness) {
2940     case ODP_FIT_PERFECT:
2941         return "OK";
2942     case ODP_FIT_TOO_MUCH:
2943         return "too_much";
2944     case ODP_FIT_TOO_LITTLE:
2945         return "too_little";
2946     case ODP_FIT_ERROR:
2947         return "error";
2948     default:
2949         return "<unknown>";
2950     }
2951 }
2952
2953 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2954  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
2955  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2956  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
2957  * null, then the return value is not meaningful.) */
2958 size_t
2959 odp_put_userspace_action(uint32_t pid,
2960                          const void *userdata, size_t userdata_size,
2961                          struct ofpbuf *odp_actions)
2962 {
2963     size_t userdata_ofs;
2964     size_t offset;
2965
2966     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
2967     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
2968     if (userdata) {
2969         userdata_ofs = odp_actions->size + NLA_HDRLEN;
2970         nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
2971                           userdata, userdata_size);
2972     } else {
2973         userdata_ofs = 0;
2974     }
2975     nl_msg_end_nested(odp_actions, offset);
2976
2977     return userdata_ofs;
2978 }
2979
2980 void
2981 odp_put_tunnel_action(const struct flow_tnl *tunnel,
2982                       struct ofpbuf *odp_actions)
2983 {
2984     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2985     tun_key_to_attr(odp_actions, tunnel);
2986     nl_msg_end_nested(odp_actions, offset);
2987 }
2988 \f
2989 /* The commit_odp_actions() function and its helpers. */
2990
2991 static void
2992 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
2993                   const void *key, size_t key_size)
2994 {
2995     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2996     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
2997     nl_msg_end_nested(odp_actions, offset);
2998 }
2999
3000 void
3001 odp_put_skb_mark_action(const uint32_t skb_mark,
3002                         struct ofpbuf *odp_actions)
3003 {
3004     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
3005                       sizeof(skb_mark));
3006 }
3007
3008 /* If any of the flow key data that ODP actions can modify are different in
3009  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3010  * 'odp_actions' that change the flow tunneling information in key from
3011  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3012  * same way.  In other words, operates the same as commit_odp_actions(), but
3013  * only on tunneling information. */
3014 void
3015 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3016                          struct ofpbuf *odp_actions)
3017 {
3018     /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3019     if (flow->tunnel.ip_dst) {
3020         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3021             return;
3022         }
3023         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3024         odp_put_tunnel_action(&base->tunnel, odp_actions);
3025     }
3026 }
3027
3028 static void
3029 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3030                              struct ofpbuf *odp_actions,
3031                              struct flow_wildcards *wc)
3032 {
3033     struct ovs_key_ethernet eth_key;
3034
3035     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3036         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3037         return;
3038     }
3039
3040     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3041     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3042
3043     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3044     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3045
3046     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3047     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3048
3049     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3050                       &eth_key, sizeof(eth_key));
3051 }
3052
3053 static void
3054 commit_vlan_action(const struct flow *flow, struct flow *base,
3055                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3056 {
3057     if (base->vlan_tci == flow->vlan_tci) {
3058         return;
3059     }
3060
3061     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3062
3063     if (base->vlan_tci & htons(VLAN_CFI)) {
3064         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3065     }
3066
3067     if (flow->vlan_tci & htons(VLAN_CFI)) {
3068         struct ovs_action_push_vlan vlan;
3069
3070         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3071         vlan.vlan_tci = flow->vlan_tci;
3072         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3073                           &vlan, sizeof vlan);
3074     }
3075     base->vlan_tci = flow->vlan_tci;
3076 }
3077
3078 static void
3079 commit_mpls_action(const struct flow *flow, struct flow *base,
3080                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3081 {
3082     if (flow->mpls_lse == base->mpls_lse &&
3083         flow->mpls_depth == base->mpls_depth) {
3084         return;
3085     }
3086
3087     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3088
3089     if (flow->mpls_depth < base->mpls_depth) {
3090         if (base->mpls_depth - flow->mpls_depth > 1) {
3091             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3092             VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3093                          " a single mpls_pop action");
3094         }
3095
3096         nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3097     } else if (flow->mpls_depth > base->mpls_depth) {
3098         struct ovs_action_push_mpls *mpls;
3099
3100         if (flow->mpls_depth - base->mpls_depth > 1) {
3101             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3102             VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3103                          " a single mpls_push action");
3104         }
3105
3106         mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3107                                         sizeof *mpls);
3108         memset(mpls, 0, sizeof *mpls);
3109         mpls->mpls_ethertype = flow->dl_type;
3110         mpls->mpls_lse = flow->mpls_lse;
3111     } else {
3112         struct ovs_key_mpls mpls_key;
3113
3114         mpls_key.mpls_top_lse = flow->mpls_lse;
3115         commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3116                           &mpls_key, sizeof(mpls_key));
3117     }
3118
3119     base->dl_type = flow->dl_type;
3120     base->mpls_lse = flow->mpls_lse;
3121     base->mpls_depth = flow->mpls_depth;
3122 }
3123
3124 static void
3125 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3126                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3127 {
3128     struct ovs_key_ipv4 ipv4_key;
3129
3130     if (base->nw_src == flow->nw_src &&
3131         base->nw_dst == flow->nw_dst &&
3132         base->nw_tos == flow->nw_tos &&
3133         base->nw_ttl == flow->nw_ttl &&
3134         base->nw_frag == flow->nw_frag) {
3135         return;
3136     }
3137
3138     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3139     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3140     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3141     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3142     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3143     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3144
3145     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3146     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3147     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3148     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3149     ipv4_key.ipv4_proto = base->nw_proto;
3150     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3151
3152     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3153                       &ipv4_key, sizeof(ipv4_key));
3154 }
3155
3156 static void
3157 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3158                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3159 {
3160     struct ovs_key_ipv6 ipv6_key;
3161
3162     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3163         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3164         base->ipv6_label == flow->ipv6_label &&
3165         base->nw_tos == flow->nw_tos &&
3166         base->nw_ttl == flow->nw_ttl &&
3167         base->nw_frag == flow->nw_frag) {
3168         return;
3169     }
3170
3171     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3172     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3173     memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3174     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3175     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3176     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3177     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3178
3179     base->ipv6_src = flow->ipv6_src;
3180     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3181     base->ipv6_dst = flow->ipv6_dst;
3182     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3183
3184     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3185     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3186     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3187     ipv6_key.ipv6_proto = base->nw_proto;
3188     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3189
3190     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3191                       &ipv6_key, sizeof(ipv6_key));
3192 }
3193
3194 static void
3195 commit_set_nw_action(const struct flow *flow, struct flow *base,
3196                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3197 {
3198     /* Check if flow really have an IP header. */
3199     if (!flow->nw_proto) {
3200         return;
3201     }
3202
3203     if (base->dl_type == htons(ETH_TYPE_IP)) {
3204         commit_set_ipv4_action(flow, base, odp_actions, wc);
3205     } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3206         commit_set_ipv6_action(flow, base, odp_actions, wc);
3207     }
3208 }
3209
3210 static void
3211 commit_set_port_action(const struct flow *flow, struct flow *base,
3212                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3213 {
3214     if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3215         return;
3216     }
3217
3218     if (base->tp_src == flow->tp_src &&
3219         base->tp_dst == flow->tp_dst) {
3220         return;
3221     }
3222
3223     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3224     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3225
3226     if (flow->nw_proto == IPPROTO_TCP) {
3227         struct ovs_key_tcp port_key;
3228
3229         port_key.tcp_src = base->tp_src = flow->tp_src;
3230         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3231
3232         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3233                           &port_key, sizeof(port_key));
3234
3235     } else if (flow->nw_proto == IPPROTO_UDP) {
3236         struct ovs_key_udp port_key;
3237
3238         port_key.udp_src = base->tp_src = flow->tp_src;
3239         port_key.udp_dst = base->tp_dst = flow->tp_dst;
3240
3241         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3242                           &port_key, sizeof(port_key));
3243     }
3244 }
3245
3246 static void
3247 commit_set_priority_action(const struct flow *flow, struct flow *base,
3248                            struct ofpbuf *odp_actions,
3249                            struct flow_wildcards *wc)
3250 {
3251     if (base->skb_priority == flow->skb_priority) {
3252         return;
3253     }
3254
3255     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3256     base->skb_priority = flow->skb_priority;
3257
3258     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3259                       &base->skb_priority, sizeof(base->skb_priority));
3260 }
3261
3262 static void
3263 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
3264                            struct ofpbuf *odp_actions,
3265                            struct flow_wildcards *wc)
3266 {
3267     if (base->skb_mark == flow->skb_mark) {
3268         return;
3269     }
3270
3271     memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
3272     base->skb_mark = flow->skb_mark;
3273
3274     odp_put_skb_mark_action(base->skb_mark, odp_actions);
3275 }
3276 /* If any of the flow key data that ODP actions can modify are different in
3277  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3278  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
3279  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
3280  * in addition to this function if needed.  Sets fields in 'wc' that are
3281  * used as part of the action. */
3282 void
3283 commit_odp_actions(const struct flow *flow, struct flow *base,
3284                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3285 {
3286     commit_set_ether_addr_action(flow, base, odp_actions, wc);
3287     commit_vlan_action(flow, base, odp_actions, wc);
3288     commit_set_nw_action(flow, base, odp_actions, wc);
3289     commit_set_port_action(flow, base, odp_actions, wc);
3290     /* Committing MPLS actions should occur after committing nw and port
3291      * actions. This is because committing MPLS actions may alter a packet so
3292      * that it is no longer IP and thus nw and port actions are no longer valid.
3293      */
3294     commit_mpls_action(flow, base, odp_actions, wc);
3295     commit_set_priority_action(flow, base, odp_actions, wc);
3296     commit_set_skb_mark_action(flow, base, odp_actions, wc);
3297 }