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