datapath: add skb mark matching and set action
[openvswitch] / 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  * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1375  * number rather than a datapath port number).  Instead, if 'odp_in_port'
1376  * is anything other than OVSP_NONE, it is included in 'buf' as the input
1377  * port.
1378  *
1379  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1380  * capable of being expanded to allow for that much space. */
1381 void
1382 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
1383                        uint32_t odp_in_port)
1384 {
1385     struct ovs_key_ethernet *eth_key;
1386     size_t encap;
1387
1388     if (flow->skb_priority) {
1389         nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1390     }
1391
1392     if (flow->tunnel.tun_id != htonll(0)) {
1393         nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tunnel.tun_id);
1394     }
1395
1396     if (flow->skb_mark) {
1397         nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, flow->skb_mark);
1398     }
1399
1400     if (odp_in_port != OVSP_NONE) {
1401         nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
1402     }
1403
1404     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1405                                        sizeof *eth_key);
1406     memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1407     memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1408
1409     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1410         nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1411         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1412         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1413         if (flow->vlan_tci == htons(0)) {
1414             goto unencap;
1415         }
1416     } else {
1417         encap = 0;
1418     }
1419
1420     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1421         goto unencap;
1422     }
1423
1424     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1425
1426     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1427         struct ovs_key_ipv4 *ipv4_key;
1428
1429         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1430                                             sizeof *ipv4_key);
1431         ipv4_key->ipv4_src = flow->nw_src;
1432         ipv4_key->ipv4_dst = flow->nw_dst;
1433         ipv4_key->ipv4_proto = flow->nw_proto;
1434         ipv4_key->ipv4_tos = flow->nw_tos;
1435         ipv4_key->ipv4_ttl = flow->nw_ttl;
1436         ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1437     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1438         struct ovs_key_ipv6 *ipv6_key;
1439
1440         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1441                                             sizeof *ipv6_key);
1442         memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1443         memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1444         ipv6_key->ipv6_label = flow->ipv6_label;
1445         ipv6_key->ipv6_proto = flow->nw_proto;
1446         ipv6_key->ipv6_tclass = flow->nw_tos;
1447         ipv6_key->ipv6_hlimit = flow->nw_ttl;
1448         ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1449     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1450                flow->dl_type == htons(ETH_TYPE_RARP)) {
1451         struct ovs_key_arp *arp_key;
1452
1453         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1454                                            sizeof *arp_key);
1455         memset(arp_key, 0, sizeof *arp_key);
1456         arp_key->arp_sip = flow->nw_src;
1457         arp_key->arp_tip = flow->nw_dst;
1458         arp_key->arp_op = htons(flow->nw_proto);
1459         memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1460         memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1461     }
1462
1463     if ((flow->dl_type == htons(ETH_TYPE_IP)
1464          || flow->dl_type == htons(ETH_TYPE_IPV6))
1465         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1466
1467         if (flow->nw_proto == IPPROTO_TCP) {
1468             struct ovs_key_tcp *tcp_key;
1469
1470             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1471                                                sizeof *tcp_key);
1472             tcp_key->tcp_src = flow->tp_src;
1473             tcp_key->tcp_dst = flow->tp_dst;
1474         } else if (flow->nw_proto == IPPROTO_UDP) {
1475             struct ovs_key_udp *udp_key;
1476
1477             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1478                                                sizeof *udp_key);
1479             udp_key->udp_src = flow->tp_src;
1480             udp_key->udp_dst = flow->tp_dst;
1481         } else if (flow->dl_type == htons(ETH_TYPE_IP)
1482                 && flow->nw_proto == IPPROTO_ICMP) {
1483             struct ovs_key_icmp *icmp_key;
1484
1485             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1486                                                 sizeof *icmp_key);
1487             icmp_key->icmp_type = ntohs(flow->tp_src);
1488             icmp_key->icmp_code = ntohs(flow->tp_dst);
1489         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1490                 && flow->nw_proto == IPPROTO_ICMPV6) {
1491             struct ovs_key_icmpv6 *icmpv6_key;
1492
1493             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1494                                                   sizeof *icmpv6_key);
1495             icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1496             icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1497
1498             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1499                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1500                 struct ovs_key_nd *nd_key;
1501
1502                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1503                                                     sizeof *nd_key);
1504                 memcpy(nd_key->nd_target, &flow->nd_target,
1505                         sizeof nd_key->nd_target);
1506                 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1507                 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1508             }
1509         }
1510     }
1511
1512 unencap:
1513     if (encap) {
1514         nl_msg_end_nested(buf, encap);
1515     }
1516 }
1517
1518 uint32_t
1519 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1520 {
1521     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1522     return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1523 }
1524
1525 static void
1526 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1527                        uint64_t attrs, int out_of_range_attr,
1528                        const struct nlattr *key, size_t key_len)
1529 {
1530     struct ds s;
1531     int i;
1532
1533     if (VLOG_DROP_DBG(rl)) {
1534         return;
1535     }
1536
1537     ds_init(&s);
1538     for (i = 0; i < 64; i++) {
1539         if (attrs & (UINT64_C(1) << i)) {
1540             ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1541         }
1542     }
1543     if (out_of_range_attr) {
1544         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1545     }
1546
1547     ds_put_cstr(&s, ": ");
1548     odp_flow_key_format(key, key_len, &s);
1549
1550     VLOG_DBG("%s:%s", title, ds_cstr(&s));
1551     ds_destroy(&s);
1552 }
1553
1554 static bool
1555 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1556 {
1557     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1558
1559     if (odp_frag > OVS_FRAG_TYPE_LATER) {
1560         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1561         return false;
1562     }
1563
1564     if (odp_frag != OVS_FRAG_TYPE_NONE) {
1565         flow->nw_frag |= FLOW_NW_FRAG_ANY;
1566         if (odp_frag == OVS_FRAG_TYPE_LATER) {
1567             flow->nw_frag |= FLOW_NW_FRAG_LATER;
1568         }
1569     }
1570     return true;
1571 }
1572
1573 static bool
1574 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1575                    const struct nlattr *attrs[], uint64_t *present_attrsp,
1576                    int *out_of_range_attrp)
1577 {
1578     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1579     const struct nlattr *nla;
1580     uint64_t present_attrs;
1581     size_t left;
1582
1583     present_attrs = 0;
1584     *out_of_range_attrp = 0;
1585     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1586         uint16_t type = nl_attr_type(nla);
1587         size_t len = nl_attr_get_size(nla);
1588         int expected_len = odp_flow_key_attr_len(type);
1589
1590         if (len != expected_len && expected_len >= 0) {
1591             VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1592                         "length %d", ovs_key_attr_to_string(type),
1593                         len, expected_len);
1594             return false;
1595         }
1596
1597         if (type >= CHAR_BIT * sizeof present_attrs) {
1598             *out_of_range_attrp = type;
1599         } else {
1600             if (present_attrs & (UINT64_C(1) << type)) {
1601                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1602                             ovs_key_attr_to_string(type));
1603                 return false;
1604             }
1605
1606             present_attrs |= UINT64_C(1) << type;
1607             attrs[type] = nla;
1608         }
1609     }
1610     if (left) {
1611         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1612         return false;
1613     }
1614
1615     *present_attrsp = present_attrs;
1616     return true;
1617 }
1618
1619 static enum odp_key_fitness
1620 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1621                    uint64_t expected_attrs,
1622                    const struct nlattr *key, size_t key_len)
1623 {
1624     uint64_t missing_attrs;
1625     uint64_t extra_attrs;
1626
1627     missing_attrs = expected_attrs & ~present_attrs;
1628     if (missing_attrs) {
1629         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1630         log_odp_key_attributes(&rl, "expected but not present",
1631                                missing_attrs, 0, key, key_len);
1632         return ODP_FIT_TOO_LITTLE;
1633     }
1634
1635     extra_attrs = present_attrs & ~expected_attrs;
1636     if (extra_attrs || out_of_range_attr) {
1637         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1638         log_odp_key_attributes(&rl, "present but not expected",
1639                                extra_attrs, out_of_range_attr, key, key_len);
1640         return ODP_FIT_TOO_MUCH;
1641     }
1642
1643     return ODP_FIT_PERFECT;
1644 }
1645
1646 static bool
1647 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1648                 uint64_t present_attrs, uint64_t *expected_attrs,
1649                 struct flow *flow)
1650 {
1651     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1652
1653     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1654         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1655         if (ntohs(flow->dl_type) < 1536) {
1656             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1657                         ntohs(flow->dl_type));
1658             return false;
1659         }
1660         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1661     } else {
1662         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1663     }
1664     return true;
1665 }
1666
1667 static enum odp_key_fitness
1668 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1669                 uint64_t present_attrs, int out_of_range_attr,
1670                 uint64_t expected_attrs, struct flow *flow,
1671                 const struct nlattr *key, size_t key_len)
1672 {
1673     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1674
1675     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1676         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1677         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1678             const struct ovs_key_ipv4 *ipv4_key;
1679
1680             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1681             flow->nw_src = ipv4_key->ipv4_src;
1682             flow->nw_dst = ipv4_key->ipv4_dst;
1683             flow->nw_proto = ipv4_key->ipv4_proto;
1684             flow->nw_tos = ipv4_key->ipv4_tos;
1685             flow->nw_ttl = ipv4_key->ipv4_ttl;
1686             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1687                 return ODP_FIT_ERROR;
1688             }
1689         }
1690     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1691         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1692         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1693             const struct ovs_key_ipv6 *ipv6_key;
1694
1695             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1696             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1697             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1698             flow->ipv6_label = ipv6_key->ipv6_label;
1699             flow->nw_proto = ipv6_key->ipv6_proto;
1700             flow->nw_tos = ipv6_key->ipv6_tclass;
1701             flow->nw_ttl = ipv6_key->ipv6_hlimit;
1702             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1703                 return ODP_FIT_ERROR;
1704             }
1705         }
1706     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1707                flow->dl_type == htons(ETH_TYPE_RARP)) {
1708         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1709         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1710             const struct ovs_key_arp *arp_key;
1711
1712             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1713             flow->nw_src = arp_key->arp_sip;
1714             flow->nw_dst = arp_key->arp_tip;
1715             if (arp_key->arp_op & htons(0xff00)) {
1716                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1717                             "key", ntohs(arp_key->arp_op));
1718                 return ODP_FIT_ERROR;
1719             }
1720             flow->nw_proto = ntohs(arp_key->arp_op);
1721             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1722             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1723         }
1724     }
1725
1726     if (flow->nw_proto == IPPROTO_TCP
1727         && (flow->dl_type == htons(ETH_TYPE_IP) ||
1728             flow->dl_type == htons(ETH_TYPE_IPV6))
1729         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1730         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1731         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1732             const struct ovs_key_tcp *tcp_key;
1733
1734             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1735             flow->tp_src = tcp_key->tcp_src;
1736             flow->tp_dst = tcp_key->tcp_dst;
1737         }
1738     } else if (flow->nw_proto == IPPROTO_UDP
1739                && (flow->dl_type == htons(ETH_TYPE_IP) ||
1740                    flow->dl_type == htons(ETH_TYPE_IPV6))
1741                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1742         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1743         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1744             const struct ovs_key_udp *udp_key;
1745
1746             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1747             flow->tp_src = udp_key->udp_src;
1748             flow->tp_dst = udp_key->udp_dst;
1749         }
1750     } else if (flow->nw_proto == IPPROTO_ICMP
1751                && flow->dl_type == htons(ETH_TYPE_IP)
1752                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1753         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1754         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1755             const struct ovs_key_icmp *icmp_key;
1756
1757             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1758             flow->tp_src = htons(icmp_key->icmp_type);
1759             flow->tp_dst = htons(icmp_key->icmp_code);
1760         }
1761     } else if (flow->nw_proto == IPPROTO_ICMPV6
1762                && flow->dl_type == htons(ETH_TYPE_IPV6)
1763                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1764         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1765         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1766             const struct ovs_key_icmpv6 *icmpv6_key;
1767
1768             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1769             flow->tp_src = htons(icmpv6_key->icmpv6_type);
1770             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1771
1772             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1773                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1774                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1775                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1776                     const struct ovs_key_nd *nd_key;
1777
1778                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1779                     memcpy(&flow->nd_target, nd_key->nd_target,
1780                            sizeof flow->nd_target);
1781                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1782                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1783                 }
1784             }
1785         }
1786     }
1787
1788     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1789                               key, key_len);
1790 }
1791
1792 /* Parse 802.1Q header then encapsulated L3 attributes. */
1793 static enum odp_key_fitness
1794 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1795                    uint64_t present_attrs, int out_of_range_attr,
1796                    uint64_t expected_attrs, struct flow *flow,
1797                    const struct nlattr *key, size_t key_len)
1798 {
1799     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1800
1801     const struct nlattr *encap
1802         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1803            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1804     enum odp_key_fitness encap_fitness;
1805     enum odp_key_fitness fitness;
1806     ovs_be16 tci;
1807
1808     /* Calulate fitness of outer attributes. */
1809     expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1810                        (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1811     fitness = check_expectations(present_attrs, out_of_range_attr,
1812                                  expected_attrs, key, key_len);
1813
1814     /* Get the VLAN TCI value. */
1815     if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1816         return ODP_FIT_TOO_LITTLE;
1817     }
1818     tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1819     if (tci == htons(0)) {
1820         /* Corner case for a truncated 802.1Q header. */
1821         if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1822             return ODP_FIT_TOO_MUCH;
1823         }
1824         return fitness;
1825     } else if (!(tci & htons(VLAN_CFI))) {
1826         VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1827                     "but CFI bit is not set", ntohs(tci));
1828         return ODP_FIT_ERROR;
1829     }
1830
1831     /* Set vlan_tci.
1832      * Remove the TPID from dl_type since it's not the real Ethertype.  */
1833     flow->vlan_tci = tci;
1834     flow->dl_type = htons(0);
1835
1836     /* Now parse the encapsulated attributes. */
1837     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1838                             attrs, &present_attrs, &out_of_range_attr)) {
1839         return ODP_FIT_ERROR;
1840     }
1841     expected_attrs = 0;
1842
1843     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1844         return ODP_FIT_ERROR;
1845     }
1846     encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1847                                     expected_attrs, flow, key, key_len);
1848
1849     /* The overall fitness is the worse of the outer and inner attributes. */
1850     return MAX(fitness, encap_fitness);
1851 }
1852
1853 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1854  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
1855  * 'key' fits our expectations for what a flow key should contain.
1856  *
1857  * The 'in_port' will be the datapath's understanding of the port.  The
1858  * caller will need to translate with odp_port_to_ofp_port() if the
1859  * OpenFlow port is needed.
1860  *
1861  * This function doesn't take the packet itself as an argument because none of
1862  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
1863  * it is always possible to infer which additional attribute(s) should appear
1864  * by looking at the attributes for lower-level protocols, e.g. if the network
1865  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1866  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1867  * must be absent. */
1868 enum odp_key_fitness
1869 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1870                      struct flow *flow)
1871 {
1872     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1873     uint64_t expected_attrs;
1874     uint64_t present_attrs;
1875     int out_of_range_attr;
1876
1877     memset(flow, 0, sizeof *flow);
1878
1879     /* Parse attributes. */
1880     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1881                             &out_of_range_attr)) {
1882         return ODP_FIT_ERROR;
1883     }
1884     expected_attrs = 0;
1885
1886     /* Metadata. */
1887     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1888         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1889         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1890     }
1891
1892     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
1893         flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
1894         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
1895     }
1896
1897     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1898         flow->tunnel.tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1899         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1900     }
1901
1902     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1903         flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1904         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1905     } else {
1906         flow->in_port = OVSP_NONE;
1907     }
1908
1909     /* Ethernet header. */
1910     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1911         const struct ovs_key_ethernet *eth_key;
1912
1913         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1914         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1915         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1916     }
1917     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1918
1919     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1920     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1921         return ODP_FIT_ERROR;
1922     }
1923
1924     if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1925         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1926                                   expected_attrs, flow, key, key_len);
1927     }
1928     return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1929                            expected_attrs, flow, key, key_len);
1930 }
1931
1932 /* Returns 'fitness' as a string, for use in debug messages. */
1933 const char *
1934 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1935 {
1936     switch (fitness) {
1937     case ODP_FIT_PERFECT:
1938         return "OK";
1939     case ODP_FIT_TOO_MUCH:
1940         return "too_much";
1941     case ODP_FIT_TOO_LITTLE:
1942         return "too_little";
1943     case ODP_FIT_ERROR:
1944         return "error";
1945     default:
1946         return "<unknown>";
1947     }
1948 }
1949
1950 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1951  * Netlink PID 'pid'.  If 'cookie' is nonnull, adds a userdata attribute whose
1952  * contents contains 'cookie' and returns the offset within 'odp_actions' of
1953  * the start of the cookie.  (If 'cookie' is null, then the return value is not
1954  * meaningful.) */
1955 size_t
1956 odp_put_userspace_action(uint32_t pid, const union user_action_cookie *cookie,
1957                          struct ofpbuf *odp_actions)
1958 {
1959     size_t offset;
1960
1961     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1962     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1963     if (cookie) {
1964         nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1965                           cookie, sizeof *cookie);
1966     }
1967     nl_msg_end_nested(odp_actions, offset);
1968
1969     return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1970 }
1971 \f
1972 /* The commit_odp_actions() function and its helpers. */
1973
1974 static void
1975 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1976                   const void *key, size_t key_size)
1977 {
1978     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1979     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1980     nl_msg_end_nested(odp_actions, offset);
1981 }
1982
1983 static void
1984 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1985                          struct ofpbuf *odp_actions)
1986 {
1987     if (base->tunnel.tun_id == flow->tunnel.tun_id) {
1988         return;
1989     }
1990     base->tunnel.tun_id = flow->tunnel.tun_id;
1991
1992     commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
1993                       &base->tunnel.tun_id, sizeof(base->tunnel.tun_id));
1994 }
1995
1996 static void
1997 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1998                              struct ofpbuf *odp_actions)
1999 {
2000     struct ovs_key_ethernet eth_key;
2001
2002     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2003         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2004         return;
2005     }
2006
2007     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2008     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2009
2010     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2011     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2012
2013     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2014                       &eth_key, sizeof(eth_key));
2015 }
2016
2017 static void
2018 commit_vlan_action(const struct flow *flow, struct flow *base,
2019                    struct ofpbuf *odp_actions)
2020 {
2021     if (base->vlan_tci == flow->vlan_tci) {
2022         return;
2023     }
2024
2025     if (base->vlan_tci & htons(VLAN_CFI)) {
2026         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2027     }
2028
2029     if (flow->vlan_tci & htons(VLAN_CFI)) {
2030         struct ovs_action_push_vlan vlan;
2031
2032         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2033         vlan.vlan_tci = flow->vlan_tci;
2034         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2035                           &vlan, sizeof vlan);
2036     }
2037     base->vlan_tci = flow->vlan_tci;
2038 }
2039
2040 static void
2041 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2042                      struct ofpbuf *odp_actions)
2043 {
2044     struct ovs_key_ipv4 ipv4_key;
2045
2046     if (base->nw_src == flow->nw_src &&
2047         base->nw_dst == flow->nw_dst &&
2048         base->nw_tos == flow->nw_tos &&
2049         base->nw_ttl == flow->nw_ttl &&
2050         base->nw_frag == flow->nw_frag) {
2051         return;
2052     }
2053
2054     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2055     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2056     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2057     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2058     ipv4_key.ipv4_proto = base->nw_proto;
2059     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2060
2061     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2062                       &ipv4_key, sizeof(ipv4_key));
2063 }
2064
2065 static void
2066 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2067                        struct ofpbuf *odp_actions)
2068 {
2069     struct ovs_key_ipv6 ipv6_key;
2070
2071     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2072         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2073         base->ipv6_label == flow->ipv6_label &&
2074         base->nw_tos == flow->nw_tos &&
2075         base->nw_ttl == flow->nw_ttl &&
2076         base->nw_frag == flow->nw_frag) {
2077         return;
2078     }
2079
2080     base->ipv6_src = flow->ipv6_src;
2081     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2082     base->ipv6_dst = flow->ipv6_dst;
2083     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2084
2085     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2086     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2087     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2088     ipv6_key.ipv6_proto = base->nw_proto;
2089     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2090
2091     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2092                       &ipv6_key, sizeof(ipv6_key));
2093 }
2094
2095 static void
2096 commit_set_nw_action(const struct flow *flow, struct flow *base,
2097                      struct ofpbuf *odp_actions)
2098 {
2099     /* Check if flow really have an IP header. */
2100     if (!flow->nw_proto) {
2101         return;
2102     }
2103
2104     if (base->dl_type == htons(ETH_TYPE_IP)) {
2105         commit_set_ipv4_action(flow, base, odp_actions);
2106     } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2107         commit_set_ipv6_action(flow, base, odp_actions);
2108     }
2109 }
2110
2111 static void
2112 commit_set_port_action(const struct flow *flow, struct flow *base,
2113                        struct ofpbuf *odp_actions)
2114 {
2115     if (!base->tp_src && !base->tp_dst) {
2116         return;
2117     }
2118
2119     if (base->tp_src == flow->tp_src &&
2120         base->tp_dst == flow->tp_dst) {
2121         return;
2122     }
2123
2124     if (flow->nw_proto == IPPROTO_TCP) {
2125         struct ovs_key_tcp port_key;
2126
2127         port_key.tcp_src = base->tp_src = flow->tp_src;
2128         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2129
2130         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2131                           &port_key, sizeof(port_key));
2132
2133     } else if (flow->nw_proto == IPPROTO_UDP) {
2134         struct ovs_key_udp port_key;
2135
2136         port_key.udp_src = base->tp_src = flow->tp_src;
2137         port_key.udp_dst = base->tp_dst = flow->tp_dst;
2138
2139         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2140                           &port_key, sizeof(port_key));
2141     }
2142 }
2143
2144 static void
2145 commit_set_priority_action(const struct flow *flow, struct flow *base,
2146                            struct ofpbuf *odp_actions)
2147 {
2148     if (base->skb_priority == flow->skb_priority) {
2149         return;
2150     }
2151     base->skb_priority = flow->skb_priority;
2152
2153     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2154                       &base->skb_priority, sizeof(base->skb_priority));
2155 }
2156
2157 static void
2158 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2159                            struct ofpbuf *odp_actions)
2160 {
2161     if (base->skb_mark == flow->skb_mark) {
2162         return;
2163     }
2164     base->skb_mark = flow->skb_mark;
2165
2166     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK,
2167                       &base->skb_mark, sizeof(base->skb_mark));
2168 }
2169 /* If any of the flow key data that ODP actions can modify are different in
2170  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2171  * key from 'base' into 'flow', and then changes 'base' the same way. */
2172 void
2173 commit_odp_actions(const struct flow *flow, struct flow *base,
2174                    struct ofpbuf *odp_actions)
2175 {
2176     commit_set_tun_id_action(flow, base, odp_actions);
2177     commit_set_ether_addr_action(flow, base, odp_actions);
2178     commit_vlan_action(flow, base, odp_actions);
2179     commit_set_nw_action(flow, base, odp_actions);
2180     commit_set_port_action(flow, base, odp_actions);
2181     commit_set_priority_action(flow, base, odp_actions);
2182     commit_set_skb_mark_action(flow, base, odp_actions);
2183 }