39d4e34de5d2f5033781c6c2a1e36be9b1c2ac80
[cascardo/ovs.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 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_attr(const char *, const struct simap *port_names,
52                               struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
54
55 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
56  * 'type':
57  *
58  *   - For an action whose argument has a fixed length, returned that
59  *     nonnegative length in bytes.
60  *
61  *   - For an action with a variable-length argument, returns -2.
62  *
63  *   - For an invalid 'type', returns -1. */
64 static int
65 odp_action_len(uint16_t type)
66 {
67     if (type > OVS_ACTION_ATTR_MAX) {
68         return -1;
69     }
70
71     switch ((enum ovs_action_attr) type) {
72     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
73     case OVS_ACTION_ATTR_USERSPACE: return -2;
74     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75     case OVS_ACTION_ATTR_POP_VLAN: return 0;
76     case OVS_ACTION_ATTR_SET: return -2;
77     case OVS_ACTION_ATTR_SAMPLE: return -2;
78
79     case OVS_ACTION_ATTR_UNSPEC:
80     case __OVS_ACTION_ATTR_MAX:
81         return -1;
82     }
83
84     return -1;
85 }
86
87 static const char *
88 ovs_key_attr_to_string(enum ovs_key_attr attr)
89 {
90     static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
91
92     switch (attr) {
93     case OVS_KEY_ATTR_UNSPEC: return "unspec";
94     case OVS_KEY_ATTR_ENCAP: return "encap";
95     case OVS_KEY_ATTR_PRIORITY: return "priority";
96     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
97     case OVS_KEY_ATTR_TUN_ID: return "tun_id";
98     case OVS_KEY_ATTR_IPV4_TUNNEL: return "ipv4_tunnel";
99     case OVS_KEY_ATTR_IN_PORT: return "in_port";
100     case OVS_KEY_ATTR_ETHERNET: return "eth";
101     case OVS_KEY_ATTR_VLAN: return "vlan";
102     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
103     case OVS_KEY_ATTR_IPV4: return "ipv4";
104     case OVS_KEY_ATTR_IPV6: return "ipv6";
105     case OVS_KEY_ATTR_TCP: return "tcp";
106     case OVS_KEY_ATTR_UDP: return "udp";
107     case OVS_KEY_ATTR_ICMP: return "icmp";
108     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
109     case OVS_KEY_ATTR_ARP: return "arp";
110     case OVS_KEY_ATTR_ND: return "nd";
111
112     case __OVS_KEY_ATTR_MAX:
113     default:
114         snprintf(unknown_attr, sizeof unknown_attr, "key%u",
115                  (unsigned int) attr);
116         return unknown_attr;
117     }
118 }
119
120 static void
121 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
122 {
123     size_t len = nl_attr_get_size(a);
124
125     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
126     if (len) {
127         const uint8_t *unspec;
128         unsigned int i;
129
130         unspec = nl_attr_get(a);
131         for (i = 0; i < len; i++) {
132             ds_put_char(ds, i ? ' ': '(');
133             ds_put_format(ds, "%02x", unspec[i]);
134         }
135         ds_put_char(ds, ')');
136     }
137 }
138
139 static void
140 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
141 {
142     static const struct nl_policy ovs_sample_policy[] = {
143         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
144         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
145     };
146     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
147     double percentage;
148     const struct nlattr *nla_acts;
149     int len;
150
151     ds_put_cstr(ds, "sample");
152
153     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
154         ds_put_cstr(ds, "(error)");
155         return;
156     }
157
158     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
159                         UINT32_MAX;
160
161     ds_put_format(ds, "(sample=%.1f%%,", percentage);
162
163     ds_put_cstr(ds, "actions(");
164     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
165     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
166     format_odp_actions(ds, nla_acts, len);
167     ds_put_format(ds, "))");
168 }
169
170 static const char *
171 slow_path_reason_to_string(uint32_t data)
172 {
173     enum slow_path_reason bit = (enum slow_path_reason) data;
174
175     switch (bit) {
176     case SLOW_CFM:
177         return "cfm";
178     case SLOW_LACP:
179         return "lacp";
180     case SLOW_STP:
181         return "stp";
182     case SLOW_IN_BAND:
183         return "in_band";
184     case SLOW_CONTROLLER:
185         return "controller";
186     case SLOW_MATCH:
187         return "match";
188     default:
189         return NULL;
190     }
191 }
192
193 static void
194 format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
195              uint32_t flags)
196 {
197     uint32_t bad = 0;
198
199     ds_put_format(ds, "(");
200     if (!flags) {
201         goto out;
202     }
203     while (flags) {
204         uint32_t bit = rightmost_1bit(flags);
205         const char *s;
206
207         s = bit_to_string(bit);
208         if (s) {
209             ds_put_format(ds, "%s,", s);
210         } else {
211             bad |= bit;
212         }
213
214         flags &= ~bit;
215     }
216
217     if (bad) {
218         ds_put_format(ds, "0x%"PRIx32",", bad);
219     }
220     ds_chomp(ds, ',');
221 out:
222     ds_put_format(ds, ")");
223 }
224
225 static int
226 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
227             uint32_t *res)
228 {
229     uint32_t result = 0;
230     int n = 0;
231
232     if (s[n] != '(') {
233         return -EINVAL;
234     }
235     n++;
236
237     while (s[n] != ')') {
238         unsigned long long int flags;
239         uint32_t bit;
240         int n0;
241
242         if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
243             n += n0 + (s[n + n0] == ',');
244             result |= flags;
245             continue;
246         }
247
248         for (bit = 1; bit; bit <<= 1) {
249             const char *name = bit_to_string(bit);
250             size_t len;
251
252             if (!name) {
253                 continue;
254             }
255
256             len = strlen(name);
257             if (!strncmp(s + n, name, len) &&
258                 (s[n + len] == ',' || s[n + len] == ')')) {
259                 result |= bit;
260                 n += len + (s[n + len] == ',');
261                 break;
262             }
263         }
264
265         if (!bit) {
266             return -EINVAL;
267         }
268     }
269     n++;
270
271     *res = result;
272     return n;
273 }
274
275 static void
276 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
277 {
278     static const struct nl_policy ovs_userspace_policy[] = {
279         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
280         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
281     };
282     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
283
284     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
285         ds_put_cstr(ds, "userspace(error)");
286         return;
287     }
288
289     ds_put_format(ds, "userspace(pid=%"PRIu32,
290                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
291
292     if (a[OVS_USERSPACE_ATTR_USERDATA]) {
293         uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
294         union user_action_cookie cookie;
295
296         memcpy(&cookie, &userdata, sizeof cookie);
297
298         switch (cookie.type) {
299         case USER_ACTION_COOKIE_SFLOW:
300             ds_put_format(ds, ",sFlow("
301                           "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
302                           vlan_tci_to_vid(cookie.sflow.vlan_tci),
303                           vlan_tci_to_pcp(cookie.sflow.vlan_tci),
304                           cookie.sflow.output);
305             break;
306
307         case USER_ACTION_COOKIE_SLOW_PATH:
308             ds_put_cstr(ds, ",slow_path");
309             format_flags(ds, slow_path_reason_to_string, cookie.slow_path.reason);
310             break;
311
312         case USER_ACTION_COOKIE_UNSPEC:
313         default:
314             ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
315             break;
316         }
317     }
318
319     ds_put_char(ds, ')');
320 }
321
322 static void
323 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
324 {
325     ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
326                   vlan_tci_to_vid(vlan_tci),
327                   vlan_tci_to_pcp(vlan_tci));
328     if (!(vlan_tci & htons(VLAN_CFI))) {
329         ds_put_cstr(ds, ",cfi=0");
330     }
331 }
332
333 static void
334 format_odp_action(struct ds *ds, const struct nlattr *a)
335 {
336     int expected_len;
337     enum ovs_action_attr type = nl_attr_type(a);
338     const struct ovs_action_push_vlan *vlan;
339
340     expected_len = odp_action_len(nl_attr_type(a));
341     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
342         ds_put_format(ds, "bad length %zu, expected %d for: ",
343                       nl_attr_get_size(a), expected_len);
344         format_generic_odp_action(ds, a);
345         return;
346     }
347
348     switch (type) {
349     case OVS_ACTION_ATTR_OUTPUT:
350         ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
351         break;
352     case OVS_ACTION_ATTR_USERSPACE:
353         format_odp_userspace_action(ds, a);
354         break;
355     case OVS_ACTION_ATTR_SET:
356         ds_put_cstr(ds, "set(");
357         format_odp_key_attr(nl_attr_get(a), ds);
358         ds_put_cstr(ds, ")");
359         break;
360     case OVS_ACTION_ATTR_PUSH_VLAN:
361         vlan = nl_attr_get(a);
362         ds_put_cstr(ds, "push_vlan(");
363         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
364             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
365         }
366         format_vlan_tci(ds, vlan->vlan_tci);
367         ds_put_char(ds, ')');
368         break;
369     case OVS_ACTION_ATTR_POP_VLAN:
370         ds_put_cstr(ds, "pop_vlan");
371         break;
372     case OVS_ACTION_ATTR_SAMPLE:
373         format_odp_sample_action(ds, a);
374         break;
375     case OVS_ACTION_ATTR_UNSPEC:
376     case __OVS_ACTION_ATTR_MAX:
377     default:
378         format_generic_odp_action(ds, a);
379         break;
380     }
381 }
382
383 void
384 format_odp_actions(struct ds *ds, const struct nlattr *actions,
385                    size_t actions_len)
386 {
387     if (actions_len) {
388         const struct nlattr *a;
389         unsigned int left;
390
391         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
392             if (a != actions) {
393                 ds_put_char(ds, ',');
394             }
395             format_odp_action(ds, a);
396         }
397         if (left) {
398             int i;
399
400             if (left == actions_len) {
401                 ds_put_cstr(ds, "<empty>");
402             }
403             ds_put_format(ds, ",***%u leftover bytes*** (", left);
404             for (i = 0; i < left; i++) {
405                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
406             }
407             ds_put_char(ds, ')');
408         }
409     } else {
410         ds_put_cstr(ds, "drop");
411     }
412 }
413
414 static int
415 parse_odp_action(const char *s, const struct simap *port_names,
416                  struct ofpbuf *actions)
417 {
418     /* Many of the sscanf calls in this function use oversized destination
419      * fields because some sscanf() implementations truncate the range of %i
420      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
421      * value of 0x7fff.  The other alternatives are to allow only a single
422      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
423      * parsers.
424      *
425      * The tun_id parser has to use an alternative approach because there is no
426      * type larger than 64 bits. */
427
428     {
429         unsigned long long int port;
430         int n = -1;
431
432         if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
433             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
434             return n;
435         }
436     }
437
438     if (port_names) {
439         int len = strcspn(s, delimiters);
440         struct simap_node *node;
441
442         node = simap_find_len(port_names, s, len);
443         if (node) {
444             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
445             return len;
446         }
447     }
448
449     {
450         unsigned long long int pid;
451         unsigned long long int output;
452         char userdata_s[32];
453         int vid, pcp;
454         int n = -1;
455
456         if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
457             odp_put_userspace_action(pid, NULL, actions);
458             return n;
459         } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
460                           "pcp=%i,output=%lli))%n",
461                           &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
462             union user_action_cookie cookie;
463             uint16_t tci;
464
465             tci = vid | (pcp << VLAN_PCP_SHIFT);
466             if (tci) {
467                 tci |= VLAN_CFI;
468             }
469
470             cookie.type = USER_ACTION_COOKIE_SFLOW;
471             cookie.sflow.vlan_tci = htons(tci);
472             cookie.sflow.output = output;
473             odp_put_userspace_action(pid, &cookie, actions);
474             return n;
475         } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
476                    && n > 0) {
477             union user_action_cookie cookie;
478             int res;
479
480             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
481             cookie.slow_path.unused = 0;
482             cookie.slow_path.reason = 0;
483
484             res = parse_flags(&s[n], slow_path_reason_to_string,
485                               &cookie.slow_path.reason);
486             if (res < 0) {
487                 return res;
488             }
489             n += res;
490             if (s[n] != ')') {
491                 return -EINVAL;
492             }
493             n++;
494
495             odp_put_userspace_action(pid, &cookie, actions);
496             return n;
497         } else if (sscanf(s, "userspace(pid=%lli,userdata="
498                           "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
499                           &n) > 0 && n > 0) {
500             union user_action_cookie cookie;
501             uint64_t userdata;
502
503             userdata = strtoull(userdata_s, NULL, 0);
504             memcpy(&cookie, &userdata, sizeof cookie);
505             odp_put_userspace_action(pid, &cookie, actions);
506             return n;
507         }
508     }
509
510     if (!strncmp(s, "set(", 4)) {
511         size_t start_ofs;
512         int retval;
513
514         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
515         retval = parse_odp_key_attr(s + 4, port_names, actions);
516         if (retval < 0) {
517             return retval;
518         }
519         if (s[retval + 4] != ')') {
520             return -EINVAL;
521         }
522         nl_msg_end_nested(actions, start_ofs);
523         return retval + 5;
524     }
525
526     {
527         struct ovs_action_push_vlan push;
528         int tpid = ETH_TYPE_VLAN;
529         int vid, pcp;
530         int cfi = 1;
531         int n = -1;
532
533         if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
534              && n > 0)
535             || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
536                        &vid, &pcp, &cfi, &n) > 0 && n > 0)
537             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
538                        &tpid, &vid, &pcp, &n) > 0 && n > 0)
539             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
540                        &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
541             push.vlan_tpid = htons(tpid);
542             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
543                                   | (pcp << VLAN_PCP_SHIFT)
544                                   | (cfi ? VLAN_CFI : 0));
545             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
546                               &push, sizeof push);
547
548             return n;
549         }
550     }
551
552     if (!strncmp(s, "pop_vlan", 8)) {
553         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
554         return 8;
555     }
556
557     {
558         double percentage;
559         int n = -1;
560
561         if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
562             && percentage >= 0. && percentage <= 100.0
563             && n > 0) {
564             size_t sample_ofs, actions_ofs;
565             double probability;
566
567             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
568             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
569             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
570                            (probability <= 0 ? 0
571                             : probability >= UINT32_MAX ? UINT32_MAX
572                             : probability));
573
574             actions_ofs = nl_msg_start_nested(actions,
575                                               OVS_SAMPLE_ATTR_ACTIONS);
576             for (;;) {
577                 int retval;
578
579                 n += strspn(s + n, delimiters);
580                 if (s[n] == ')') {
581                     break;
582                 }
583
584                 retval = parse_odp_action(s + n, port_names, actions);
585                 if (retval < 0) {
586                     return retval;
587                 }
588                 n += retval;
589             }
590             nl_msg_end_nested(actions, actions_ofs);
591             nl_msg_end_nested(actions, sample_ofs);
592
593             return s[n + 1] == ')' ? n + 2 : -EINVAL;
594         }
595     }
596
597     return -EINVAL;
598 }
599
600 /* Parses the string representation of datapath actions, in the format output
601  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
602  * value.  On success, the ODP actions are appended to 'actions' as a series of
603  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
604  * way, 'actions''s data might be reallocated. */
605 int
606 odp_actions_from_string(const char *s, const struct simap *port_names,
607                         struct ofpbuf *actions)
608 {
609     size_t old_size;
610
611     if (!strcasecmp(s, "drop")) {
612         return 0;
613     }
614
615     old_size = actions->size;
616     for (;;) {
617         int retval;
618
619         s += strspn(s, delimiters);
620         if (!*s) {
621             return 0;
622         }
623
624         retval = parse_odp_action(s, port_names, actions);
625         if (retval < 0 || !strchr(delimiters, s[retval])) {
626             actions->size = old_size;
627             return -retval;
628         }
629         s += retval;
630     }
631
632     return 0;
633 }
634 \f
635 /* Returns the correct length of the payload for a flow key attribute of the
636  * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
637  * is variable length. */
638 static int
639 odp_flow_key_attr_len(uint16_t type)
640 {
641     if (type > OVS_KEY_ATTR_MAX) {
642         return -1;
643     }
644
645     switch ((enum ovs_key_attr) type) {
646     case OVS_KEY_ATTR_ENCAP: return -2;
647     case OVS_KEY_ATTR_PRIORITY: return 4;
648     case OVS_KEY_ATTR_SKB_MARK: return 4;
649     case OVS_KEY_ATTR_TUN_ID: return 8;
650     case OVS_KEY_ATTR_IPV4_TUNNEL: return sizeof(struct ovs_key_ipv4_tunnel);
651     case OVS_KEY_ATTR_IN_PORT: return 4;
652     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
653     case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
654     case OVS_KEY_ATTR_ETHERTYPE: return 2;
655     case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
656     case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
657     case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
658     case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
659     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
660     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
661     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
662     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
663
664     case OVS_KEY_ATTR_UNSPEC:
665     case __OVS_KEY_ATTR_MAX:
666         return -1;
667     }
668
669     return -1;
670 }
671
672 static void
673 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
674 {
675     size_t len = nl_attr_get_size(a);
676     if (len) {
677         const uint8_t *unspec;
678         unsigned int i;
679
680         unspec = nl_attr_get(a);
681         for (i = 0; i < len; i++) {
682             ds_put_char(ds, i ? ' ': '(');
683             ds_put_format(ds, "%02x", unspec[i]);
684         }
685         ds_put_char(ds, ')');
686     }
687 }
688
689 static const char *
690 ovs_frag_type_to_string(enum ovs_frag_type type)
691 {
692     switch (type) {
693     case OVS_FRAG_TYPE_NONE:
694         return "no";
695     case OVS_FRAG_TYPE_FIRST:
696         return "first";
697     case OVS_FRAG_TYPE_LATER:
698         return "later";
699     case __OVS_FRAG_TYPE_MAX:
700     default:
701         return "<error>";
702     }
703 }
704
705 static const char *
706 tun_flag_to_string(uint32_t flags)
707 {
708     switch (flags) {
709     case OVS_TNL_F_DONT_FRAGMENT:
710         return "df";
711     case OVS_TNL_F_CSUM:
712         return "csum";
713     case OVS_TNL_F_KEY:
714         return "key";
715     default:
716         return NULL;
717     }
718 }
719
720 static void
721 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
722 {
723     const struct ovs_key_ethernet *eth_key;
724     const struct ovs_key_ipv4 *ipv4_key;
725     const struct ovs_key_ipv6 *ipv6_key;
726     const struct ovs_key_tcp *tcp_key;
727     const struct ovs_key_udp *udp_key;
728     const struct ovs_key_icmp *icmp_key;
729     const struct ovs_key_icmpv6 *icmpv6_key;
730     const struct ovs_key_arp *arp_key;
731     const struct ovs_key_nd *nd_key;
732     const struct ovs_key_ipv4_tunnel *ipv4_tun_key;
733     enum ovs_key_attr attr = nl_attr_type(a);
734     int expected_len;
735
736     ds_put_cstr(ds, ovs_key_attr_to_string(attr));
737     expected_len = odp_flow_key_attr_len(nl_attr_type(a));
738     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
739         ds_put_format(ds, "(bad length %zu, expected %d)",
740                       nl_attr_get_size(a),
741                       odp_flow_key_attr_len(nl_attr_type(a)));
742         format_generic_odp_key(a, ds);
743         return;
744     }
745
746     switch (attr) {
747     case OVS_KEY_ATTR_ENCAP:
748         ds_put_cstr(ds, "(");
749         if (nl_attr_get_size(a)) {
750             odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
751         }
752         ds_put_char(ds, ')');
753         break;
754
755     case OVS_KEY_ATTR_PRIORITY:
756         ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
757         break;
758
759     case OVS_KEY_ATTR_SKB_MARK:
760         ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
761         break;
762
763     case OVS_KEY_ATTR_TUN_ID:
764         ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
765         break;
766
767     case OVS_KEY_ATTR_IPV4_TUNNEL:
768         ipv4_tun_key = nl_attr_get(a);
769         ds_put_format(ds, "(tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
770                       "tos=0x%"PRIx8",ttl=%"PRIu8",flags",
771                       ntohll(ipv4_tun_key->tun_id),
772                       IP_ARGS(&ipv4_tun_key->ipv4_src),
773                       IP_ARGS(&ipv4_tun_key->ipv4_dst),
774                       ipv4_tun_key->ipv4_tos, ipv4_tun_key->ipv4_ttl);
775
776         format_flags(ds, tun_flag_to_string, ipv4_tun_key->tun_flags);
777         ds_put_format(ds, ")");
778         break;
779
780     case OVS_KEY_ATTR_IN_PORT:
781         ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
782         break;
783
784     case OVS_KEY_ATTR_ETHERNET:
785         eth_key = nl_attr_get(a);
786         ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
787                       ETH_ADDR_ARGS(eth_key->eth_src),
788                       ETH_ADDR_ARGS(eth_key->eth_dst));
789         break;
790
791     case OVS_KEY_ATTR_VLAN:
792         ds_put_char(ds, '(');
793         format_vlan_tci(ds, nl_attr_get_be16(a));
794         ds_put_char(ds, ')');
795         break;
796
797     case OVS_KEY_ATTR_ETHERTYPE:
798         ds_put_format(ds, "(0x%04"PRIx16")",
799                       ntohs(nl_attr_get_be16(a)));
800         break;
801
802     case OVS_KEY_ATTR_IPV4:
803         ipv4_key = nl_attr_get(a);
804         ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
805                       ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
806                       IP_ARGS(&ipv4_key->ipv4_src),
807                       IP_ARGS(&ipv4_key->ipv4_dst),
808                       ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
809                       ipv4_key->ipv4_ttl,
810                       ovs_frag_type_to_string(ipv4_key->ipv4_frag));
811         break;
812
813     case OVS_KEY_ATTR_IPV6: {
814         char src_str[INET6_ADDRSTRLEN];
815         char dst_str[INET6_ADDRSTRLEN];
816
817         ipv6_key = nl_attr_get(a);
818         inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
819         inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
820
821         ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
822                       ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
823                       src_str, dst_str, ntohl(ipv6_key->ipv6_label),
824                       ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
825                       ipv6_key->ipv6_hlimit,
826                       ovs_frag_type_to_string(ipv6_key->ipv6_frag));
827         break;
828     }
829
830     case OVS_KEY_ATTR_TCP:
831         tcp_key = nl_attr_get(a);
832         ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
833                       ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
834         break;
835
836     case OVS_KEY_ATTR_UDP:
837         udp_key = nl_attr_get(a);
838         ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
839                       ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
840         break;
841
842     case OVS_KEY_ATTR_ICMP:
843         icmp_key = nl_attr_get(a);
844         ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
845                       icmp_key->icmp_type, icmp_key->icmp_code);
846         break;
847
848     case OVS_KEY_ATTR_ICMPV6:
849         icmpv6_key = nl_attr_get(a);
850         ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
851                       icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
852         break;
853
854     case OVS_KEY_ATTR_ARP:
855         arp_key = nl_attr_get(a);
856         ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
857                       "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
858                       IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
859                       ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
860                       ETH_ADDR_ARGS(arp_key->arp_tha));
861         break;
862
863     case OVS_KEY_ATTR_ND: {
864         char target[INET6_ADDRSTRLEN];
865
866         nd_key = nl_attr_get(a);
867         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
868
869         ds_put_format(ds, "(target=%s", target);
870         if (!eth_addr_is_zero(nd_key->nd_sll)) {
871             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
872                           ETH_ADDR_ARGS(nd_key->nd_sll));
873         }
874         if (!eth_addr_is_zero(nd_key->nd_tll)) {
875             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
876                           ETH_ADDR_ARGS(nd_key->nd_tll));
877         }
878         ds_put_char(ds, ')');
879         break;
880     }
881
882     case OVS_KEY_ATTR_UNSPEC:
883     case __OVS_KEY_ATTR_MAX:
884     default:
885         format_generic_odp_key(a, ds);
886         break;
887     }
888 }
889
890 /* Appends to 'ds' a string representation of the 'key_len' bytes of
891  * OVS_KEY_ATTR_* attributes in 'key'. */
892 void
893 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
894 {
895     if (key_len) {
896         const struct nlattr *a;
897         unsigned int left;
898
899         NL_ATTR_FOR_EACH (a, left, key, key_len) {
900             if (a != key) {
901                 ds_put_char(ds, ',');
902             }
903             format_odp_key_attr(a, ds);
904         }
905         if (left) {
906             int i;
907             
908             if (left == key_len) {
909                 ds_put_cstr(ds, "<empty>");
910             }
911             ds_put_format(ds, ",***%u leftover bytes*** (", left);
912             for (i = 0; i < left; i++) {
913                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
914             }
915             ds_put_char(ds, ')');
916         }
917     } else {
918         ds_put_cstr(ds, "<empty>");
919     }
920 }
921
922 static int
923 put_nd_key(int n, const char *nd_target_s,
924            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
925 {
926     struct ovs_key_nd nd_key;
927
928     memset(&nd_key, 0, sizeof nd_key);
929     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
930         return -EINVAL;
931     }
932     if (nd_sll) {
933         memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
934     }
935     if (nd_tll) {
936         memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
937     }
938     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
939     return n;
940 }
941
942 static bool
943 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
944 {
945     if (!strcasecmp(s, "no")) {
946         *type = OVS_FRAG_TYPE_NONE;
947     } else if (!strcasecmp(s, "first")) {
948         *type = OVS_FRAG_TYPE_FIRST;
949     } else if (!strcasecmp(s, "later")) {
950         *type = OVS_FRAG_TYPE_LATER;
951     } else {
952         return false;
953     }
954     return true;
955 }
956
957 static int
958 parse_odp_key_attr(const char *s, const struct simap *port_names,
959                    struct ofpbuf *key)
960 {
961     /* Many of the sscanf calls in this function use oversized destination
962      * fields because some sscanf() implementations truncate the range of %i
963      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
964      * value of 0x7fff.  The other alternatives are to allow only a single
965      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
966      * parsers.
967      *
968      * The tun_id parser has to use an alternative approach because there is no
969      * type larger than 64 bits. */
970
971     {
972         unsigned long long int priority;
973         int n = -1;
974
975         if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
976             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
977             return n;
978         }
979     }
980
981     {
982         unsigned long long int mark;
983         int n = -1;
984
985         if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
986             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
987             return n;
988         }
989     }
990
991     {
992         char tun_id_s[32];
993         int n = -1;
994
995         if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
996                    tun_id_s, &n) > 0 && n > 0) {
997             uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
998             nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
999             return n;
1000         }
1001     }
1002
1003     {
1004         char tun_id_s[32];
1005         int tos, ttl;
1006         struct ovs_key_ipv4_tunnel tun_key;
1007         int n = -1;
1008
1009         if (sscanf(s, "ipv4_tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1010                    "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1011                    ",tos=%i,ttl=%i,flags%n", tun_id_s,
1012                     IP_SCAN_ARGS(&tun_key.ipv4_src),
1013                     IP_SCAN_ARGS(&tun_key.ipv4_dst), &tos, &ttl,
1014                     &n) > 0 && n > 0) {
1015             int res;
1016
1017             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1018             tun_key.ipv4_tos = tos;
1019             tun_key.ipv4_ttl = ttl;
1020
1021             res = parse_flags(&s[n], tun_flag_to_string, &tun_key.tun_flags);
1022             if (res < 0) {
1023                 return res;
1024             }
1025             n += res;
1026             if (s[n] != ')') {
1027                 return -EINVAL;
1028             }
1029             n++;
1030
1031             memset(&tun_key.pad, 0, sizeof tun_key.pad);
1032             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key,
1033                               sizeof tun_key);
1034             return n;
1035         }
1036     }
1037
1038     {
1039         unsigned long long int in_port;
1040         int n = -1;
1041
1042         if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1043             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1044             return n;
1045         }
1046     }
1047
1048     if (port_names && !strncmp(s, "in_port(", 8)) {
1049         const char *name;
1050         const struct simap_node *node;
1051         int name_len;
1052
1053         name = s + 8;
1054         name_len = strcspn(s, ")");
1055         node = simap_find_len(port_names, name, name_len);
1056         if (node) {
1057             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1058             return 8 + name_len + 1;
1059         }
1060     }
1061
1062     {
1063         struct ovs_key_ethernet eth_key;
1064         int n = -1;
1065
1066         if (sscanf(s,
1067                    "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1068                    ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1069                    ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1070             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1071                               &eth_key, sizeof eth_key);
1072             return n;
1073         }
1074     }
1075
1076     {
1077         uint16_t vid;
1078         int pcp;
1079         int cfi;
1080         int n = -1;
1081
1082         if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1083              && n > 0)) {
1084             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1085                             htons((vid << VLAN_VID_SHIFT) |
1086                                   (pcp << VLAN_PCP_SHIFT) |
1087                                   VLAN_CFI));
1088             return n;
1089         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1090                            &vid, &pcp, &cfi, &n) > 0
1091              && n > 0)) {
1092             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1093                             htons((vid << VLAN_VID_SHIFT) |
1094                                   (pcp << VLAN_PCP_SHIFT) |
1095                                   (cfi ? VLAN_CFI : 0)));
1096             return n;
1097         }
1098     }
1099
1100     {
1101         int eth_type;
1102         int n = -1;
1103
1104         if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1105             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1106             return n;
1107         }
1108     }
1109
1110     {
1111         ovs_be32 ipv4_src;
1112         ovs_be32 ipv4_dst;
1113         int ipv4_proto;
1114         int ipv4_tos;
1115         int ipv4_ttl;
1116         char frag[8];
1117         enum ovs_frag_type ipv4_frag;
1118         int n = -1;
1119
1120         if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1121                    "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1122                    IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1123                    &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1124             && n > 0
1125             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1126             struct ovs_key_ipv4 ipv4_key;
1127
1128             ipv4_key.ipv4_src = ipv4_src;
1129             ipv4_key.ipv4_dst = ipv4_dst;
1130             ipv4_key.ipv4_proto = ipv4_proto;
1131             ipv4_key.ipv4_tos = ipv4_tos;
1132             ipv4_key.ipv4_ttl = ipv4_ttl;
1133             ipv4_key.ipv4_frag = ipv4_frag;
1134             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1135                               &ipv4_key, sizeof ipv4_key);
1136             return n;
1137         }
1138     }
1139
1140     {
1141         char ipv6_src_s[IPV6_SCAN_LEN + 1];
1142         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1143         int ipv6_label;
1144         int ipv6_proto;
1145         int ipv6_tclass;
1146         int ipv6_hlimit;
1147         char frag[8];
1148         enum ovs_frag_type ipv6_frag;
1149         int n = -1;
1150
1151         if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1152                    "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1153                    ipv6_src_s, ipv6_dst_s, &ipv6_label,
1154                    &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1155             && n > 0
1156             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1157             struct ovs_key_ipv6 ipv6_key;
1158
1159             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1160                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1161                 return -EINVAL;
1162             }
1163             ipv6_key.ipv6_label = htonl(ipv6_label);
1164             ipv6_key.ipv6_proto = ipv6_proto;
1165             ipv6_key.ipv6_tclass = ipv6_tclass;
1166             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1167             ipv6_key.ipv6_frag = ipv6_frag;
1168             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1169                               &ipv6_key, sizeof ipv6_key);
1170             return n;
1171         }
1172     }
1173
1174     {
1175         int tcp_src;
1176         int tcp_dst;
1177         int n = -1;
1178
1179         if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1180             && n > 0) {
1181             struct ovs_key_tcp tcp_key;
1182
1183             tcp_key.tcp_src = htons(tcp_src);
1184             tcp_key.tcp_dst = htons(tcp_dst);
1185             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1186             return n;
1187         }
1188     }
1189
1190     {
1191         int udp_src;
1192         int udp_dst;
1193         int n = -1;
1194
1195         if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1196             && n > 0) {
1197             struct ovs_key_udp udp_key;
1198
1199             udp_key.udp_src = htons(udp_src);
1200             udp_key.udp_dst = htons(udp_dst);
1201             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1202             return n;
1203         }
1204     }
1205
1206     {
1207         int icmp_type;
1208         int icmp_code;
1209         int n = -1;
1210
1211         if (sscanf(s, "icmp(type=%i,code=%i)%n",
1212                    &icmp_type, &icmp_code, &n) > 0
1213             && n > 0) {
1214             struct ovs_key_icmp icmp_key;
1215
1216             icmp_key.icmp_type = icmp_type;
1217             icmp_key.icmp_code = icmp_code;
1218             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1219                               &icmp_key, sizeof icmp_key);
1220             return n;
1221         }
1222     }
1223
1224     {
1225         struct ovs_key_icmpv6 icmpv6_key;
1226         int n = -1;
1227
1228         if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1229                    &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1230             && n > 0) {
1231             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1232                               &icmpv6_key, sizeof icmpv6_key);
1233             return n;
1234         }
1235     }
1236
1237     {
1238         ovs_be32 arp_sip;
1239         ovs_be32 arp_tip;
1240         int arp_op;
1241         uint8_t arp_sha[ETH_ADDR_LEN];
1242         uint8_t arp_tha[ETH_ADDR_LEN];
1243         int n = -1;
1244
1245         if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1246                    "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1247                    IP_SCAN_ARGS(&arp_sip),
1248                    IP_SCAN_ARGS(&arp_tip),
1249                    &arp_op,
1250                    ETH_ADDR_SCAN_ARGS(arp_sha),
1251                    ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1252             struct ovs_key_arp arp_key;
1253
1254             memset(&arp_key, 0, sizeof arp_key);
1255             arp_key.arp_sip = arp_sip;
1256             arp_key.arp_tip = arp_tip;
1257             arp_key.arp_op = htons(arp_op);
1258             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1259             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1260             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1261             return n;
1262         }
1263     }
1264
1265     {
1266         char nd_target_s[IPV6_SCAN_LEN + 1];
1267         uint8_t nd_sll[ETH_ADDR_LEN];
1268         uint8_t nd_tll[ETH_ADDR_LEN];
1269         int n = -1;
1270
1271         if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1272                    nd_target_s, &n) > 0 && n > 0) {
1273             return put_nd_key(n, nd_target_s, NULL, NULL, key);
1274         }
1275         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1276                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1277             && n > 0) {
1278             return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1279         }
1280         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1281                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1282             && n > 0) {
1283             return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1284         }
1285         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1286                    "tll="ETH_ADDR_SCAN_FMT")%n",
1287                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1288                    ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1289             && n > 0) {
1290             return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1291         }
1292     }
1293
1294     if (!strncmp(s, "encap(", 6)) {
1295         const char *start = s;
1296         size_t encap;
1297
1298         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1299
1300         s += 6;
1301         for (;;) {
1302             int retval;
1303
1304             s += strspn(s, ", \t\r\n");
1305             if (!*s) {
1306                 return -EINVAL;
1307             } else if (*s == ')') {
1308                 break;
1309             }
1310
1311             retval = parse_odp_key_attr(s, port_names, key);
1312             if (retval < 0) {
1313                 return retval;
1314             }
1315             s += retval;
1316         }
1317         s++;
1318
1319         nl_msg_end_nested(key, encap);
1320
1321         return s - start;
1322     }
1323
1324     return -EINVAL;
1325 }
1326
1327 /* Parses the string representation of a datapath flow key, in the
1328  * format output by odp_flow_key_format().  Returns 0 if successful,
1329  * otherwise a positive errno value.  On success, the flow key is
1330  * appended to 'key' as a series of Netlink attributes.  On failure, no
1331  * data is appended to 'key'.  Either way, 'key''s data might be
1332  * reallocated.
1333  *
1334  * If 'port_names' is nonnull, it points to an simap that maps from a port name
1335  * to a port number.  (Port names may be used instead of port numbers in
1336  * in_port.)
1337  *
1338  * On success, the attributes appended to 'key' are individually syntactically
1339  * valid, but they may not be valid as a sequence.  'key' might, for example,
1340  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
1341 int
1342 odp_flow_key_from_string(const char *s, const struct simap *port_names,
1343                          struct ofpbuf *key)
1344 {
1345     const size_t old_size = key->size;
1346     for (;;) {
1347         int retval;
1348
1349         s += strspn(s, delimiters);
1350         if (!*s) {
1351             return 0;
1352         }
1353
1354         retval = parse_odp_key_attr(s, port_names, key);
1355         if (retval < 0) {
1356             key->size = old_size;
1357             return -retval;
1358         }
1359         s += retval;
1360     }
1361
1362     return 0;
1363 }
1364
1365 static uint8_t
1366 ovs_to_odp_frag(uint8_t nw_frag)
1367 {
1368     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1369           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1370           : OVS_FRAG_TYPE_LATER);
1371 }
1372
1373 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1374  *
1375  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1376  * capable of being expanded to allow for that much space. */
1377 void
1378 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
1379 {
1380     struct ovs_key_ethernet *eth_key;
1381     size_t encap;
1382
1383     if (flow->skb_priority) {
1384         nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1385     }
1386
1387     if (flow->tunnel.tun_id != htonll(0)) {
1388         nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tunnel.tun_id);
1389     }
1390
1391     if (flow->skb_mark) {
1392         nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, flow->skb_mark);
1393     }
1394
1395     if (flow->in_port != OFPP_NONE && flow->in_port != OFPP_CONTROLLER) {
1396         nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
1397                        ofp_port_to_odp_port(flow->in_port));
1398     }
1399
1400     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1401                                        sizeof *eth_key);
1402     memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1403     memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1404
1405     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1406         nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1407         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1408         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1409         if (flow->vlan_tci == htons(0)) {
1410             goto unencap;
1411         }
1412     } else {
1413         encap = 0;
1414     }
1415
1416     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1417         goto unencap;
1418     }
1419
1420     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1421
1422     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1423         struct ovs_key_ipv4 *ipv4_key;
1424
1425         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1426                                             sizeof *ipv4_key);
1427         ipv4_key->ipv4_src = flow->nw_src;
1428         ipv4_key->ipv4_dst = flow->nw_dst;
1429         ipv4_key->ipv4_proto = flow->nw_proto;
1430         ipv4_key->ipv4_tos = flow->nw_tos;
1431         ipv4_key->ipv4_ttl = flow->nw_ttl;
1432         ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1433     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1434         struct ovs_key_ipv6 *ipv6_key;
1435
1436         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1437                                             sizeof *ipv6_key);
1438         memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1439         memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1440         ipv6_key->ipv6_label = flow->ipv6_label;
1441         ipv6_key->ipv6_proto = flow->nw_proto;
1442         ipv6_key->ipv6_tclass = flow->nw_tos;
1443         ipv6_key->ipv6_hlimit = flow->nw_ttl;
1444         ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1445     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1446                flow->dl_type == htons(ETH_TYPE_RARP)) {
1447         struct ovs_key_arp *arp_key;
1448
1449         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1450                                            sizeof *arp_key);
1451         memset(arp_key, 0, sizeof *arp_key);
1452         arp_key->arp_sip = flow->nw_src;
1453         arp_key->arp_tip = flow->nw_dst;
1454         arp_key->arp_op = htons(flow->nw_proto);
1455         memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1456         memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1457     }
1458
1459     if ((flow->dl_type == htons(ETH_TYPE_IP)
1460          || flow->dl_type == htons(ETH_TYPE_IPV6))
1461         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1462
1463         if (flow->nw_proto == IPPROTO_TCP) {
1464             struct ovs_key_tcp *tcp_key;
1465
1466             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1467                                                sizeof *tcp_key);
1468             tcp_key->tcp_src = flow->tp_src;
1469             tcp_key->tcp_dst = flow->tp_dst;
1470         } else if (flow->nw_proto == IPPROTO_UDP) {
1471             struct ovs_key_udp *udp_key;
1472
1473             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1474                                                sizeof *udp_key);
1475             udp_key->udp_src = flow->tp_src;
1476             udp_key->udp_dst = flow->tp_dst;
1477         } else if (flow->dl_type == htons(ETH_TYPE_IP)
1478                 && flow->nw_proto == IPPROTO_ICMP) {
1479             struct ovs_key_icmp *icmp_key;
1480
1481             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1482                                                 sizeof *icmp_key);
1483             icmp_key->icmp_type = ntohs(flow->tp_src);
1484             icmp_key->icmp_code = ntohs(flow->tp_dst);
1485         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1486                 && flow->nw_proto == IPPROTO_ICMPV6) {
1487             struct ovs_key_icmpv6 *icmpv6_key;
1488
1489             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1490                                                   sizeof *icmpv6_key);
1491             icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1492             icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1493
1494             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1495                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1496                 struct ovs_key_nd *nd_key;
1497
1498                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1499                                                     sizeof *nd_key);
1500                 memcpy(nd_key->nd_target, &flow->nd_target,
1501                         sizeof nd_key->nd_target);
1502                 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1503                 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1504             }
1505         }
1506     }
1507
1508 unencap:
1509     if (encap) {
1510         nl_msg_end_nested(buf, encap);
1511     }
1512 }
1513
1514 uint32_t
1515 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1516 {
1517     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1518     return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1519 }
1520
1521 static void
1522 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1523                        uint64_t attrs, int out_of_range_attr,
1524                        const struct nlattr *key, size_t key_len)
1525 {
1526     struct ds s;
1527     int i;
1528
1529     if (VLOG_DROP_DBG(rl)) {
1530         return;
1531     }
1532
1533     ds_init(&s);
1534     for (i = 0; i < 64; i++) {
1535         if (attrs & (UINT64_C(1) << i)) {
1536             ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1537         }
1538     }
1539     if (out_of_range_attr) {
1540         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1541     }
1542
1543     ds_put_cstr(&s, ": ");
1544     odp_flow_key_format(key, key_len, &s);
1545
1546     VLOG_DBG("%s:%s", title, ds_cstr(&s));
1547     ds_destroy(&s);
1548 }
1549
1550 static bool
1551 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1552 {
1553     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1554
1555     if (odp_frag > OVS_FRAG_TYPE_LATER) {
1556         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1557         return false;
1558     }
1559
1560     if (odp_frag != OVS_FRAG_TYPE_NONE) {
1561         flow->nw_frag |= FLOW_NW_FRAG_ANY;
1562         if (odp_frag == OVS_FRAG_TYPE_LATER) {
1563             flow->nw_frag |= FLOW_NW_FRAG_LATER;
1564         }
1565     }
1566     return true;
1567 }
1568
1569 static bool
1570 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1571                    const struct nlattr *attrs[], uint64_t *present_attrsp,
1572                    int *out_of_range_attrp)
1573 {
1574     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1575     const struct nlattr *nla;
1576     uint64_t present_attrs;
1577     size_t left;
1578
1579     present_attrs = 0;
1580     *out_of_range_attrp = 0;
1581     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1582         uint16_t type = nl_attr_type(nla);
1583         size_t len = nl_attr_get_size(nla);
1584         int expected_len = odp_flow_key_attr_len(type);
1585
1586         if (len != expected_len && expected_len >= 0) {
1587             VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1588                         "length %d", ovs_key_attr_to_string(type),
1589                         len, expected_len);
1590             return false;
1591         }
1592
1593         if (type >= CHAR_BIT * sizeof present_attrs) {
1594             *out_of_range_attrp = type;
1595         } else {
1596             if (present_attrs & (UINT64_C(1) << type)) {
1597                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1598                             ovs_key_attr_to_string(type));
1599                 return false;
1600             }
1601
1602             present_attrs |= UINT64_C(1) << type;
1603             attrs[type] = nla;
1604         }
1605     }
1606     if (left) {
1607         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1608         return false;
1609     }
1610
1611     *present_attrsp = present_attrs;
1612     return true;
1613 }
1614
1615 static enum odp_key_fitness
1616 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1617                    uint64_t expected_attrs,
1618                    const struct nlattr *key, size_t key_len)
1619 {
1620     uint64_t missing_attrs;
1621     uint64_t extra_attrs;
1622
1623     missing_attrs = expected_attrs & ~present_attrs;
1624     if (missing_attrs) {
1625         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1626         log_odp_key_attributes(&rl, "expected but not present",
1627                                missing_attrs, 0, key, key_len);
1628         return ODP_FIT_TOO_LITTLE;
1629     }
1630
1631     extra_attrs = present_attrs & ~expected_attrs;
1632     if (extra_attrs || out_of_range_attr) {
1633         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1634         log_odp_key_attributes(&rl, "present but not expected",
1635                                extra_attrs, out_of_range_attr, key, key_len);
1636         return ODP_FIT_TOO_MUCH;
1637     }
1638
1639     return ODP_FIT_PERFECT;
1640 }
1641
1642 static bool
1643 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1644                 uint64_t present_attrs, uint64_t *expected_attrs,
1645                 struct flow *flow)
1646 {
1647     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1648
1649     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1650         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1651         if (ntohs(flow->dl_type) < 1536) {
1652             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1653                         ntohs(flow->dl_type));
1654             return false;
1655         }
1656         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1657     } else {
1658         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1659     }
1660     return true;
1661 }
1662
1663 static enum odp_key_fitness
1664 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1665                 uint64_t present_attrs, int out_of_range_attr,
1666                 uint64_t expected_attrs, struct flow *flow,
1667                 const struct nlattr *key, size_t key_len)
1668 {
1669     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1670
1671     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1672         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1673         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1674             const struct ovs_key_ipv4 *ipv4_key;
1675
1676             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1677             flow->nw_src = ipv4_key->ipv4_src;
1678             flow->nw_dst = ipv4_key->ipv4_dst;
1679             flow->nw_proto = ipv4_key->ipv4_proto;
1680             flow->nw_tos = ipv4_key->ipv4_tos;
1681             flow->nw_ttl = ipv4_key->ipv4_ttl;
1682             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1683                 return ODP_FIT_ERROR;
1684             }
1685         }
1686     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1687         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1688         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1689             const struct ovs_key_ipv6 *ipv6_key;
1690
1691             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1692             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1693             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1694             flow->ipv6_label = ipv6_key->ipv6_label;
1695             flow->nw_proto = ipv6_key->ipv6_proto;
1696             flow->nw_tos = ipv6_key->ipv6_tclass;
1697             flow->nw_ttl = ipv6_key->ipv6_hlimit;
1698             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1699                 return ODP_FIT_ERROR;
1700             }
1701         }
1702     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1703                flow->dl_type == htons(ETH_TYPE_RARP)) {
1704         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1705         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1706             const struct ovs_key_arp *arp_key;
1707
1708             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1709             flow->nw_src = arp_key->arp_sip;
1710             flow->nw_dst = arp_key->arp_tip;
1711             if (arp_key->arp_op & htons(0xff00)) {
1712                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1713                             "key", ntohs(arp_key->arp_op));
1714                 return ODP_FIT_ERROR;
1715             }
1716             flow->nw_proto = ntohs(arp_key->arp_op);
1717             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1718             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1719         }
1720     }
1721
1722     if (flow->nw_proto == IPPROTO_TCP
1723         && (flow->dl_type == htons(ETH_TYPE_IP) ||
1724             flow->dl_type == htons(ETH_TYPE_IPV6))
1725         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1726         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1727         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1728             const struct ovs_key_tcp *tcp_key;
1729
1730             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1731             flow->tp_src = tcp_key->tcp_src;
1732             flow->tp_dst = tcp_key->tcp_dst;
1733         }
1734     } else if (flow->nw_proto == IPPROTO_UDP
1735                && (flow->dl_type == htons(ETH_TYPE_IP) ||
1736                    flow->dl_type == htons(ETH_TYPE_IPV6))
1737                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1738         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1739         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1740             const struct ovs_key_udp *udp_key;
1741
1742             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1743             flow->tp_src = udp_key->udp_src;
1744             flow->tp_dst = udp_key->udp_dst;
1745         }
1746     } else if (flow->nw_proto == IPPROTO_ICMP
1747                && flow->dl_type == htons(ETH_TYPE_IP)
1748                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1749         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1750         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1751             const struct ovs_key_icmp *icmp_key;
1752
1753             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1754             flow->tp_src = htons(icmp_key->icmp_type);
1755             flow->tp_dst = htons(icmp_key->icmp_code);
1756         }
1757     } else if (flow->nw_proto == IPPROTO_ICMPV6
1758                && flow->dl_type == htons(ETH_TYPE_IPV6)
1759                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1760         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1761         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1762             const struct ovs_key_icmpv6 *icmpv6_key;
1763
1764             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1765             flow->tp_src = htons(icmpv6_key->icmpv6_type);
1766             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1767
1768             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1769                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1770                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1771                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1772                     const struct ovs_key_nd *nd_key;
1773
1774                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1775                     memcpy(&flow->nd_target, nd_key->nd_target,
1776                            sizeof flow->nd_target);
1777                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1778                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1779                 }
1780             }
1781         }
1782     }
1783
1784     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1785                               key, key_len);
1786 }
1787
1788 /* Parse 802.1Q header then encapsulated L3 attributes. */
1789 static enum odp_key_fitness
1790 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1791                    uint64_t present_attrs, int out_of_range_attr,
1792                    uint64_t expected_attrs, struct flow *flow,
1793                    const struct nlattr *key, size_t key_len)
1794 {
1795     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1796
1797     const struct nlattr *encap
1798         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1799            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1800     enum odp_key_fitness encap_fitness;
1801     enum odp_key_fitness fitness;
1802     ovs_be16 tci;
1803
1804     /* Calulate fitness of outer attributes. */
1805     expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1806                        (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1807     fitness = check_expectations(present_attrs, out_of_range_attr,
1808                                  expected_attrs, key, key_len);
1809
1810     /* Get the VLAN TCI value. */
1811     if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1812         return ODP_FIT_TOO_LITTLE;
1813     }
1814     tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1815     if (tci == htons(0)) {
1816         /* Corner case for a truncated 802.1Q header. */
1817         if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1818             return ODP_FIT_TOO_MUCH;
1819         }
1820         return fitness;
1821     } else if (!(tci & htons(VLAN_CFI))) {
1822         VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1823                     "but CFI bit is not set", ntohs(tci));
1824         return ODP_FIT_ERROR;
1825     }
1826
1827     /* Set vlan_tci.
1828      * Remove the TPID from dl_type since it's not the real Ethertype.  */
1829     flow->vlan_tci = tci;
1830     flow->dl_type = htons(0);
1831
1832     /* Now parse the encapsulated attributes. */
1833     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1834                             attrs, &present_attrs, &out_of_range_attr)) {
1835         return ODP_FIT_ERROR;
1836     }
1837     expected_attrs = 0;
1838
1839     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1840         return ODP_FIT_ERROR;
1841     }
1842     encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1843                                     expected_attrs, flow, key, key_len);
1844
1845     /* The overall fitness is the worse of the outer and inner attributes. */
1846     return MAX(fitness, encap_fitness);
1847 }
1848
1849 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1850  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
1851  * 'key' fits our expectations for what a flow key should contain.
1852  *
1853  * This function doesn't take the packet itself as an argument because none of
1854  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
1855  * it is always possible to infer which additional attribute(s) should appear
1856  * by looking at the attributes for lower-level protocols, e.g. if the network
1857  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1858  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1859  * must be absent. */
1860 enum odp_key_fitness
1861 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1862                      struct flow *flow)
1863 {
1864     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1865     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1866     uint64_t expected_attrs;
1867     uint64_t present_attrs;
1868     int out_of_range_attr;
1869
1870     memset(flow, 0, sizeof *flow);
1871
1872     /* Parse attributes. */
1873     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1874                             &out_of_range_attr)) {
1875         return ODP_FIT_ERROR;
1876     }
1877     expected_attrs = 0;
1878
1879     /* Metadata. */
1880     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1881         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1882         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1883     }
1884
1885     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
1886         flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
1887         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
1888     }
1889
1890     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1891         flow->tunnel.tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1892         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1893     }
1894
1895     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1896         uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1897         if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1898             VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1899                         in_port);
1900             return ODP_FIT_ERROR;
1901         }
1902         flow->in_port = odp_port_to_ofp_port(in_port);
1903         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1904     } else {
1905         flow->in_port = OFPP_NONE;
1906     }
1907
1908     /* Ethernet header. */
1909     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1910         const struct ovs_key_ethernet *eth_key;
1911
1912         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1913         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1914         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1915     }
1916     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1917
1918     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1919     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1920         return ODP_FIT_ERROR;
1921     }
1922
1923     if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1924         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1925                                   expected_attrs, flow, key, key_len);
1926     }
1927     return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1928                            expected_attrs, flow, key, key_len);
1929 }
1930
1931 /* Returns 'fitness' as a string, for use in debug messages. */
1932 const char *
1933 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1934 {
1935     switch (fitness) {
1936     case ODP_FIT_PERFECT:
1937         return "OK";
1938     case ODP_FIT_TOO_MUCH:
1939         return "too_much";
1940     case ODP_FIT_TOO_LITTLE:
1941         return "too_little";
1942     case ODP_FIT_ERROR:
1943         return "error";
1944     default:
1945         return "<unknown>";
1946     }
1947 }
1948
1949 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1950  * Netlink PID 'pid'.  If 'cookie' is nonnull, adds a userdata attribute whose
1951  * contents contains 'cookie' and returns the offset within 'odp_actions' of
1952  * the start of the cookie.  (If 'cookie' is null, then the return value is not
1953  * meaningful.) */
1954 size_t
1955 odp_put_userspace_action(uint32_t pid, const union user_action_cookie *cookie,
1956                          struct ofpbuf *odp_actions)
1957 {
1958     size_t offset;
1959
1960     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1961     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1962     if (cookie) {
1963         nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1964                           cookie, sizeof *cookie);
1965     }
1966     nl_msg_end_nested(odp_actions, offset);
1967
1968     return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1969 }
1970 \f
1971 /* The commit_odp_actions() function and its helpers. */
1972
1973 static void
1974 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1975                   const void *key, size_t key_size)
1976 {
1977     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1978     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1979     nl_msg_end_nested(odp_actions, offset);
1980 }
1981
1982 static void
1983 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1984                          struct ofpbuf *odp_actions)
1985 {
1986     if (base->tunnel.tun_id == flow->tunnel.tun_id) {
1987         return;
1988     }
1989     base->tunnel.tun_id = flow->tunnel.tun_id;
1990
1991     commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
1992                       &base->tunnel.tun_id, sizeof(base->tunnel.tun_id));
1993 }
1994
1995 static void
1996 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1997                              struct ofpbuf *odp_actions)
1998 {
1999     struct ovs_key_ethernet eth_key;
2000
2001     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2002         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2003         return;
2004     }
2005
2006     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2007     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2008
2009     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2010     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2011
2012     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2013                       &eth_key, sizeof(eth_key));
2014 }
2015
2016 static void
2017 commit_vlan_action(const struct flow *flow, struct flow *base,
2018                    struct ofpbuf *odp_actions)
2019 {
2020     if (base->vlan_tci == flow->vlan_tci) {
2021         return;
2022     }
2023
2024     if (base->vlan_tci & htons(VLAN_CFI)) {
2025         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2026     }
2027
2028     if (flow->vlan_tci & htons(VLAN_CFI)) {
2029         struct ovs_action_push_vlan vlan;
2030
2031         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2032         vlan.vlan_tci = flow->vlan_tci;
2033         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2034                           &vlan, sizeof vlan);
2035     }
2036     base->vlan_tci = flow->vlan_tci;
2037 }
2038
2039 static void
2040 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2041                      struct ofpbuf *odp_actions)
2042 {
2043     struct ovs_key_ipv4 ipv4_key;
2044
2045     if (base->nw_src == flow->nw_src &&
2046         base->nw_dst == flow->nw_dst &&
2047         base->nw_tos == flow->nw_tos &&
2048         base->nw_ttl == flow->nw_ttl &&
2049         base->nw_frag == flow->nw_frag) {
2050         return;
2051     }
2052
2053     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2054     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2055     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2056     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2057     ipv4_key.ipv4_proto = base->nw_proto;
2058     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2059
2060     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2061                       &ipv4_key, sizeof(ipv4_key));
2062 }
2063
2064 static void
2065 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2066                        struct ofpbuf *odp_actions)
2067 {
2068     struct ovs_key_ipv6 ipv6_key;
2069
2070     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2071         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2072         base->ipv6_label == flow->ipv6_label &&
2073         base->nw_tos == flow->nw_tos &&
2074         base->nw_ttl == flow->nw_ttl &&
2075         base->nw_frag == flow->nw_frag) {
2076         return;
2077     }
2078
2079     base->ipv6_src = flow->ipv6_src;
2080     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2081     base->ipv6_dst = flow->ipv6_dst;
2082     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2083
2084     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2085     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2086     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2087     ipv6_key.ipv6_proto = base->nw_proto;
2088     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2089
2090     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2091                       &ipv6_key, sizeof(ipv6_key));
2092 }
2093
2094 static void
2095 commit_set_nw_action(const struct flow *flow, struct flow *base,
2096                      struct ofpbuf *odp_actions)
2097 {
2098     /* Check if flow really have an IP header. */
2099     if (!flow->nw_proto) {
2100         return;
2101     }
2102
2103     if (base->dl_type == htons(ETH_TYPE_IP)) {
2104         commit_set_ipv4_action(flow, base, odp_actions);
2105     } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2106         commit_set_ipv6_action(flow, base, odp_actions);
2107     }
2108 }
2109
2110 static void
2111 commit_set_port_action(const struct flow *flow, struct flow *base,
2112                        struct ofpbuf *odp_actions)
2113 {
2114     if (!base->tp_src && !base->tp_dst) {
2115         return;
2116     }
2117
2118     if (base->tp_src == flow->tp_src &&
2119         base->tp_dst == flow->tp_dst) {
2120         return;
2121     }
2122
2123     if (flow->nw_proto == IPPROTO_TCP) {
2124         struct ovs_key_tcp port_key;
2125
2126         port_key.tcp_src = base->tp_src = flow->tp_src;
2127         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2128
2129         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2130                           &port_key, sizeof(port_key));
2131
2132     } else if (flow->nw_proto == IPPROTO_UDP) {
2133         struct ovs_key_udp port_key;
2134
2135         port_key.udp_src = base->tp_src = flow->tp_src;
2136         port_key.udp_dst = base->tp_dst = flow->tp_dst;
2137
2138         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2139                           &port_key, sizeof(port_key));
2140     }
2141 }
2142
2143 static void
2144 commit_set_priority_action(const struct flow *flow, struct flow *base,
2145                            struct ofpbuf *odp_actions)
2146 {
2147     if (base->skb_priority == flow->skb_priority) {
2148         return;
2149     }
2150     base->skb_priority = flow->skb_priority;
2151
2152     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2153                       &base->skb_priority, sizeof(base->skb_priority));
2154 }
2155
2156 static void
2157 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2158                            struct ofpbuf *odp_actions)
2159 {
2160     if (base->skb_mark == flow->skb_mark) {
2161         return;
2162     }
2163     base->skb_mark = flow->skb_mark;
2164
2165     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK,
2166                       &base->skb_mark, sizeof(base->skb_mark));
2167 }
2168 /* If any of the flow key data that ODP actions can modify are different in
2169  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2170  * key from 'base' into 'flow', and then changes 'base' the same way. */
2171 void
2172 commit_odp_actions(const struct flow *flow, struct flow *base,
2173                    struct ofpbuf *odp_actions)
2174 {
2175     commit_set_tun_id_action(flow, base, odp_actions);
2176     commit_set_ether_addr_action(flow, base, odp_actions);
2177     commit_vlan_action(flow, base, odp_actions);
2178     commit_set_nw_action(flow, base, odp_actions);
2179     commit_set_port_action(flow, base, odp_actions);
2180     commit_set_priority_action(flow, base, odp_actions);
2181     commit_set_skb_mark_action(flow, base, odp_actions);
2182 }