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