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