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