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