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