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