c70ab119ac4d41b96b842fdca0fc6ba4bfff256a
[sliver-openvswitch.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 <netinet/in.h>
23 #include <netinet/icmp6.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "byte-order.h"
27 #include "coverage.h"
28 #include "dynamic-string.h"
29 #include "flow.h"
30 #include "netlink.h"
31 #include "ofpbuf.h"
32 #include "openvswitch/tunnel.h"
33 #include "packets.h"
34 #include "timeval.h"
35 #include "util.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(odp_util);
39
40 /* The interface between userspace and kernel uses an "OVS_*" prefix.
41  * Since this is fairly non-specific for the OVS userspace components,
42  * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
43  * interactions with the datapath.
44  */
45
46 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
47
48 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
49  * 'type':
50  *
51  *   - For an action whose argument has a fixed length, returned that
52  *     nonnegative length in bytes.
53  *
54  *   - For an action with a variable-length argument, returns -2.
55  *
56  *   - For an invalid 'type', returns -1. */
57 static int
58 odp_action_len(uint16_t type)
59 {
60     if (type > OVS_ACTION_ATTR_MAX) {
61         return -1;
62     }
63
64     switch ((enum ovs_action_attr) type) {
65     case OVS_ACTION_ATTR_OUTPUT: return 4;
66     case OVS_ACTION_ATTR_USERSPACE: return -2;
67     case OVS_ACTION_ATTR_PUSH: return -2;
68     case OVS_ACTION_ATTR_POP: return 2;
69     case OVS_ACTION_ATTR_SET: return -2;
70     case OVS_ACTION_ATTR_SAMPLE: return -2;
71
72     case OVS_ACTION_ATTR_UNSPEC:
73     case __OVS_ACTION_ATTR_MAX:
74         return -1;
75     }
76
77     return -1;
78 }
79
80 static const char *
81 ovs_key_attr_to_string(enum ovs_key_attr attr)
82 {
83     static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
84
85     switch (attr) {
86     case OVS_KEY_ATTR_UNSPEC: return "unspec";
87     case OVS_KEY_ATTR_PRIORITY: return "priority";
88     case OVS_KEY_ATTR_TUN_ID: return "tun_id";
89     case OVS_KEY_ATTR_IN_PORT: return "in_port";
90     case OVS_KEY_ATTR_ETHERNET: return "eth";
91     case OVS_KEY_ATTR_8021Q: return "vlan";
92     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
93     case OVS_KEY_ATTR_IPV4: return "ipv4";
94     case OVS_KEY_ATTR_IPV6: return "ipv6";
95     case OVS_KEY_ATTR_TCP: return "tcp";
96     case OVS_KEY_ATTR_UDP: return "udp";
97     case OVS_KEY_ATTR_ICMP: return "icmp";
98     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
99     case OVS_KEY_ATTR_ARP: return "arp";
100     case OVS_KEY_ATTR_ND: return "nd";
101
102     case __OVS_KEY_ATTR_MAX:
103     default:
104         snprintf(unknown_attr, sizeof unknown_attr, "key%u",
105                  (unsigned int) attr);
106         return unknown_attr;
107     }
108 }
109
110 static void
111 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
112 {
113     size_t len = nl_attr_get_size(a);
114
115     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
116     if (len) {
117         const uint8_t *unspec;
118         unsigned int i;
119
120         unspec = nl_attr_get(a);
121         for (i = 0; i < len; i++) {
122             ds_put_char(ds, i ? ' ': '(');
123             ds_put_format(ds, "%02x", unspec[i]);
124         }
125         ds_put_char(ds, ')');
126     }
127 }
128
129 static void
130 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
131 {
132     static const struct nl_policy ovs_sample_policy[] = {
133         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
134         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
135     };
136     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
137     double percentage;
138     const struct nlattr *nla_acts;
139     int len;
140
141     ds_put_cstr(ds, "sample");
142
143     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
144         ds_put_cstr(ds, "(error)");
145         return;
146     }
147
148     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
149                         UINT32_MAX;
150
151     ds_put_format(ds, "(sample=%.1f%%,", percentage);
152
153     ds_put_cstr(ds, "actions(");
154     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
155     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
156     format_odp_actions(ds, nla_acts, len);
157     ds_put_format(ds, "))");
158 }
159
160 static void
161 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
162 {
163     static const struct nl_policy ovs_userspace_policy[] = {
164         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
165         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
166     };
167     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
168
169     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
170         ds_put_cstr(ds, "userspace(error)");
171         return;
172     }
173
174     ds_put_format(ds, "userspace(pid=%"PRIu32,
175                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
176
177     if (a[OVS_USERSPACE_ATTR_USERDATA]) {
178         uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
179         struct user_action_cookie cookie;
180
181         memcpy(&cookie, &userdata, sizeof cookie);
182
183         if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
184             ds_put_format(ds, ",controller,length=%"PRIu32,
185                           cookie.data);
186         } else if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
187             ds_put_format(ds, ",sFlow,n_output=%"PRIu8","
188                           "vid=%"PRIu16",pcp=%"PRIu8",ifindex=%"PRIu32,
189                           cookie.n_output, vlan_tci_to_vid(cookie.vlan_tci),
190                           vlan_tci_to_pcp(cookie.vlan_tci), cookie.data);
191         } else {
192             ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
193         }
194     }
195
196     ds_put_char(ds, ')');
197 }
198
199 static void
200 format_odp_action(struct ds *ds, const struct nlattr *a)
201 {
202     int expected_len;
203     enum ovs_action_attr type = nl_attr_type(a);
204
205     expected_len = odp_action_len(nl_attr_type(a));
206     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
207         ds_put_format(ds, "bad length %zu, expected %d for: ",
208                       nl_attr_get_size(a), expected_len);
209         format_generic_odp_action(ds, a);
210         return;
211     }
212
213     switch (type) {
214
215     case OVS_ACTION_ATTR_OUTPUT:
216         ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
217         break;
218     case OVS_ACTION_ATTR_USERSPACE:
219         format_odp_userspace_action(ds, a);
220         break;
221     case OVS_ACTION_ATTR_SET:
222         ds_put_cstr(ds, "set(");
223         format_odp_key_attr(nl_attr_get(a), ds);
224         ds_put_cstr(ds, ")");
225         break;
226     case OVS_ACTION_ATTR_PUSH:
227         ds_put_cstr(ds, "push(");
228         format_odp_key_attr(nl_attr_get(a), ds);
229         ds_put_cstr(ds, ")");
230         break;
231     case OVS_ACTION_ATTR_POP:
232         ds_put_format(ds, "pop(%s)",
233                       ovs_key_attr_to_string(nl_attr_get_u16(a)));
234         break;
235     case OVS_ACTION_ATTR_SAMPLE:
236         format_odp_sample_action(ds, a);
237         break;
238     case OVS_ACTION_ATTR_UNSPEC:
239     case __OVS_ACTION_ATTR_MAX:
240     default:
241         format_generic_odp_action(ds, a);
242         break;
243     }
244 }
245
246 void
247 format_odp_actions(struct ds *ds, const struct nlattr *actions,
248                    size_t actions_len)
249 {
250     if (actions_len) {
251         const struct nlattr *a;
252         unsigned int left;
253
254         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
255             if (a != actions) {
256                 ds_put_char(ds, ',');
257             }
258             format_odp_action(ds, a);
259         }
260         if (left) {
261             if (left == actions_len) {
262                 ds_put_cstr(ds, "<empty>");
263             }
264             ds_put_format(ds, ",***%u leftover bytes***", left);
265         }
266     } else {
267         ds_put_cstr(ds, "drop");
268     }
269 }
270 \f
271 /* Returns the correct length of the payload for a flow key attribute of the
272  * specified 'type', or -1 if 'type' is unknown. */
273 static int
274 odp_flow_key_attr_len(uint16_t type)
275 {
276     if (type > OVS_KEY_ATTR_MAX) {
277         return -1;
278     }
279
280     switch ((enum ovs_key_attr) type) {
281     case OVS_KEY_ATTR_PRIORITY: return 4;
282     case OVS_KEY_ATTR_TUN_ID: return 8;
283     case OVS_KEY_ATTR_IN_PORT: return 4;
284     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
285     case OVS_KEY_ATTR_8021Q: return sizeof(struct ovs_key_8021q);
286     case OVS_KEY_ATTR_ETHERTYPE: return 2;
287     case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
288     case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
289     case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
290     case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
291     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
292     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
293     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
294     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
295
296     case OVS_KEY_ATTR_UNSPEC:
297     case __OVS_KEY_ATTR_MAX:
298         return -1;
299     }
300
301     return -1;
302 }
303
304 static void
305 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
306 {
307     size_t len = nl_attr_get_size(a);
308     if (len) {
309         const uint8_t *unspec;
310         unsigned int i;
311
312         unspec = nl_attr_get(a);
313         for (i = 0; i < len; i++) {
314             ds_put_char(ds, i ? ' ': '(');
315             ds_put_format(ds, "%02x", unspec[i]);
316         }
317         ds_put_char(ds, ')');
318     }
319 }
320
321 static const char *
322 ovs_frag_type_to_string(enum ovs_frag_type type)
323 {
324     switch (type) {
325     case OVS_FRAG_TYPE_NONE:
326         return "no";
327     case OVS_FRAG_TYPE_FIRST:
328         return "first";
329     case OVS_FRAG_TYPE_LATER:
330         return "later";
331     case __OVS_FRAG_TYPE_MAX:
332     default:
333         return "<error>";
334     }
335 }
336
337 static void
338 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
339 {
340     const struct ovs_key_ethernet *eth_key;
341     const struct ovs_key_8021q *q_key;
342     const struct ovs_key_ipv4 *ipv4_key;
343     const struct ovs_key_ipv6 *ipv6_key;
344     const struct ovs_key_tcp *tcp_key;
345     const struct ovs_key_udp *udp_key;
346     const struct ovs_key_icmp *icmp_key;
347     const struct ovs_key_icmpv6 *icmpv6_key;
348     const struct ovs_key_arp *arp_key;
349     const struct ovs_key_nd *nd_key;
350     enum ovs_key_attr attr = nl_attr_type(a);
351
352     ds_put_cstr(ds, ovs_key_attr_to_string(attr));
353     if (nl_attr_get_size(a) != odp_flow_key_attr_len(nl_attr_type(a))) {
354         ds_put_format(ds, "(bad length %zu, expected %d)",
355                       nl_attr_get_size(a),
356                       odp_flow_key_attr_len(nl_attr_type(a)));
357         format_generic_odp_key(a, ds);
358         return;
359     }
360
361     switch (attr) {
362     case OVS_KEY_ATTR_PRIORITY:
363         ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
364         break;
365
366     case OVS_KEY_ATTR_TUN_ID:
367         ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
368         break;
369
370     case OVS_KEY_ATTR_IN_PORT:
371         ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
372         break;
373
374     case OVS_KEY_ATTR_ETHERNET:
375         eth_key = nl_attr_get(a);
376         ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
377                       ETH_ADDR_ARGS(eth_key->eth_src),
378                       ETH_ADDR_ARGS(eth_key->eth_dst));
379         break;
380
381     case OVS_KEY_ATTR_8021Q:
382         q_key = nl_attr_get(a);
383         ds_put_cstr(ds, "(");
384         if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
385             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(q_key->q_tpid));
386         }
387         ds_put_format(ds, "vid=%"PRIu16",pcp=%d)",
388                       vlan_tci_to_vid(q_key->q_tci),
389                       vlan_tci_to_pcp(q_key->q_tci));
390         break;
391
392     case OVS_KEY_ATTR_ETHERTYPE:
393         ds_put_format(ds, "(0x%04"PRIx16")",
394                       ntohs(nl_attr_get_be16(a)));
395         break;
396
397     case OVS_KEY_ATTR_IPV4:
398         ipv4_key = nl_attr_get(a);
399         ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
400                       ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
401                       IP_ARGS(&ipv4_key->ipv4_src),
402                       IP_ARGS(&ipv4_key->ipv4_dst),
403                       ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
404                       ipv4_key->ipv4_ttl,
405                       ovs_frag_type_to_string(ipv4_key->ipv4_frag));
406         break;
407
408     case OVS_KEY_ATTR_IPV6: {
409         char src_str[INET6_ADDRSTRLEN];
410         char dst_str[INET6_ADDRSTRLEN];
411
412         ipv6_key = nl_attr_get(a);
413         inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
414         inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
415
416         ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
417                       ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
418                       src_str, dst_str, ntohl(ipv6_key->ipv6_label),
419                       ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
420                       ipv6_key->ipv6_hlimit,
421                       ovs_frag_type_to_string(ipv6_key->ipv6_frag));
422         break;
423     }
424
425     case OVS_KEY_ATTR_TCP:
426         tcp_key = nl_attr_get(a);
427         ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
428                       ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
429         break;
430
431     case OVS_KEY_ATTR_UDP:
432         udp_key = nl_attr_get(a);
433         ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
434                       ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
435         break;
436
437     case OVS_KEY_ATTR_ICMP:
438         icmp_key = nl_attr_get(a);
439         ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
440                       icmp_key->icmp_type, icmp_key->icmp_code);
441         break;
442
443     case OVS_KEY_ATTR_ICMPV6:
444         icmpv6_key = nl_attr_get(a);
445         ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
446                       icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
447         break;
448
449     case OVS_KEY_ATTR_ARP:
450         arp_key = nl_attr_get(a);
451         ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
452                       "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
453                       IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
454                       ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
455                       ETH_ADDR_ARGS(arp_key->arp_tha));
456         break;
457
458     case OVS_KEY_ATTR_ND: {
459         char target[INET6_ADDRSTRLEN];
460
461         nd_key = nl_attr_get(a);
462         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
463
464         ds_put_format(ds, "(target=%s", target);
465         if (!eth_addr_is_zero(nd_key->nd_sll)) {
466             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
467                           ETH_ADDR_ARGS(nd_key->nd_sll));
468         }
469         if (!eth_addr_is_zero(nd_key->nd_tll)) {
470             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
471                           ETH_ADDR_ARGS(nd_key->nd_tll));
472         }
473         ds_put_char(ds, ')');
474         break;
475     }
476
477     case OVS_KEY_ATTR_UNSPEC:
478     case __OVS_KEY_ATTR_MAX:
479     default:
480         format_generic_odp_key(a, ds);
481         break;
482     }
483 }
484
485 /* Appends to 'ds' a string representation of the 'key_len' bytes of
486  * OVS_KEY_ATTR_* attributes in 'key'. */
487 void
488 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
489 {
490     if (key_len) {
491         const struct nlattr *a;
492         unsigned int left;
493
494         NL_ATTR_FOR_EACH (a, left, key, key_len) {
495             if (a != key) {
496                 ds_put_char(ds, ',');
497             }
498             format_odp_key_attr(a, ds);
499         }
500         if (left) {
501             if (left == key_len) {
502                 ds_put_cstr(ds, "<empty>");
503             }
504             ds_put_format(ds, ",***%u leftover bytes***", left);
505         }
506     } else {
507         ds_put_cstr(ds, "<empty>");
508     }
509 }
510
511 static int
512 put_nd_key(int n, const char *nd_target_s,
513            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
514 {
515     struct ovs_key_nd nd_key;
516
517     memset(&nd_key, 0, sizeof nd_key);
518     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
519         return -EINVAL;
520     }
521     if (nd_sll) {
522         memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
523     }
524     if (nd_tll) {
525         memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
526     }
527     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
528     return n;
529 }
530
531 static bool
532 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
533 {
534     if (!strcasecmp(s, "no")) {
535         *type = OVS_FRAG_TYPE_NONE;
536     } else if (!strcasecmp(s, "first")) {
537         *type = OVS_FRAG_TYPE_FIRST;
538     } else if (!strcasecmp(s, "later")) {
539         *type = OVS_FRAG_TYPE_LATER;
540     } else {
541         return false;
542     }
543     return true;
544 }
545
546 static int
547 parse_odp_key_attr(const char *s, struct ofpbuf *key)
548 {
549     /* Many of the sscanf calls in this function use oversized destination
550      * fields because some sscanf() implementations truncate the range of %i
551      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
552      * value of 0x7fff.  The other alternatives are to allow only a single
553      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
554      * parsers.
555      *
556      * The tun_id parser has to use an alternative approach because there is no
557      * type larger than 64 bits. */
558
559     {
560         unsigned long long int priority;
561         int n = -1;
562
563         if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
564             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
565             return n;
566         }
567     }
568
569     {
570         char tun_id_s[32];
571         int n = -1;
572
573         if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
574                    tun_id_s, &n) > 0 && n > 0) {
575             uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
576             nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
577             return n;
578         }
579     }
580
581     {
582         unsigned long long int in_port;
583         int n = -1;
584
585         if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
586             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
587             return n;
588         }
589     }
590
591     {
592         struct ovs_key_ethernet eth_key;
593         int n = -1;
594
595         if (sscanf(s,
596                    "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
597                    ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
598                    ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
599             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
600                               &eth_key, sizeof eth_key);
601             return n;
602         }
603     }
604
605     {
606         uint16_t tpid = ETH_TYPE_VLAN;
607         uint16_t vid;
608         int pcp;
609         int n = -1;
610
611         if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
612                     &vid, &pcp, &n) > 0 && n > 0) ||
613             (sscanf(s, "vlan(tpid=%"SCNi16",vid=%"SCNi16",pcp=%i)%n",
614                     &tpid, &vid, &pcp, &n) > 0 && n > 0)) {
615             struct ovs_key_8021q q_key;
616
617             q_key.q_tpid = htons(tpid);
618             q_key.q_tci = htons((vid << VLAN_VID_SHIFT) |
619                                 (pcp << VLAN_PCP_SHIFT));
620             nl_msg_put_unspec(key, OVS_KEY_ATTR_8021Q, &q_key, sizeof q_key);
621             return n;
622         }
623     }
624
625     {
626         int eth_type;
627         int n = -1;
628
629         if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
630             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
631             return n;
632         }
633     }
634
635     {
636         ovs_be32 ipv4_src;
637         ovs_be32 ipv4_dst;
638         int ipv4_proto;
639         int ipv4_tos;
640         int ipv4_ttl;
641         char frag[8];
642         enum ovs_frag_type ipv4_frag;
643         int n = -1;
644
645         if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
646                    "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
647                    IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
648                    &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
649             && n > 0
650             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
651             struct ovs_key_ipv4 ipv4_key;
652
653             ipv4_key.ipv4_src = ipv4_src;
654             ipv4_key.ipv4_dst = ipv4_dst;
655             ipv4_key.ipv4_proto = ipv4_proto;
656             ipv4_key.ipv4_tos = ipv4_tos;
657             ipv4_key.ipv4_ttl = ipv4_ttl;
658             ipv4_key.ipv4_frag = ipv4_frag;
659             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
660                               &ipv4_key, sizeof ipv4_key);
661             return n;
662         }
663     }
664
665     {
666         char ipv6_src_s[IPV6_SCAN_LEN + 1];
667         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
668         int ipv6_label;
669         int ipv6_proto;
670         int ipv6_tclass;
671         int ipv6_hlimit;
672         char frag[8];
673         enum ovs_frag_type ipv6_frag;
674         int n = -1;
675
676         if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
677                    "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
678                    ipv6_src_s, ipv6_dst_s, &ipv6_label,
679                    &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
680             && n > 0
681             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
682             struct ovs_key_ipv6 ipv6_key;
683
684             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
685                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
686                 return -EINVAL;
687             }
688             ipv6_key.ipv6_label = htonl(ipv6_label);
689             ipv6_key.ipv6_proto = ipv6_proto;
690             ipv6_key.ipv6_tclass = ipv6_tclass;
691             ipv6_key.ipv6_hlimit = ipv6_hlimit;
692             ipv6_key.ipv6_frag = ipv6_frag;
693             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
694                               &ipv6_key, sizeof ipv6_key);
695             return n;
696         }
697     }
698
699     {
700         int tcp_src;
701         int tcp_dst;
702         int n = -1;
703
704         if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
705             && n > 0) {
706             struct ovs_key_tcp tcp_key;
707
708             tcp_key.tcp_src = htons(tcp_src);
709             tcp_key.tcp_dst = htons(tcp_dst);
710             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
711             return n;
712         }
713     }
714
715     {
716         int udp_src;
717         int udp_dst;
718         int n = -1;
719
720         if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
721             && n > 0) {
722             struct ovs_key_udp udp_key;
723
724             udp_key.udp_src = htons(udp_src);
725             udp_key.udp_dst = htons(udp_dst);
726             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
727             return n;
728         }
729     }
730
731     {
732         int icmp_type;
733         int icmp_code;
734         int n = -1;
735
736         if (sscanf(s, "icmp(type=%i,code=%i)%n",
737                    &icmp_type, &icmp_code, &n) > 0
738             && n > 0) {
739             struct ovs_key_icmp icmp_key;
740
741             icmp_key.icmp_type = icmp_type;
742             icmp_key.icmp_code = icmp_code;
743             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
744                               &icmp_key, sizeof icmp_key);
745             return n;
746         }
747     }
748
749     {
750         struct ovs_key_icmpv6 icmpv6_key;
751         int n = -1;
752
753         if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
754                    &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
755             && n > 0) {
756             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
757                               &icmpv6_key, sizeof icmpv6_key);
758             return n;
759         }
760     }
761
762     {
763         ovs_be32 arp_sip;
764         ovs_be32 arp_tip;
765         int arp_op;
766         uint8_t arp_sha[ETH_ADDR_LEN];
767         uint8_t arp_tha[ETH_ADDR_LEN];
768         int n = -1;
769
770         if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
771                    "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
772                    IP_SCAN_ARGS(&arp_sip),
773                    IP_SCAN_ARGS(&arp_tip),
774                    &arp_op,
775                    ETH_ADDR_SCAN_ARGS(arp_sha),
776                    ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
777             struct ovs_key_arp arp_key;
778
779             memset(&arp_key, 0, sizeof arp_key);
780             arp_key.arp_sip = arp_sip;
781             arp_key.arp_tip = arp_tip;
782             arp_key.arp_op = htons(arp_op);
783             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
784             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
785             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
786             return n;
787         }
788     }
789
790     {
791         char nd_target_s[IPV6_SCAN_LEN + 1];
792         uint8_t nd_sll[ETH_ADDR_LEN];
793         uint8_t nd_tll[ETH_ADDR_LEN];
794         int n = -1;
795
796         if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
797                    nd_target_s, &n) > 0 && n > 0) {
798             return put_nd_key(n, nd_target_s, NULL, NULL, key);
799         }
800         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
801                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
802             && n > 0) {
803             return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
804         }
805         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
806                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
807             && n > 0) {
808             return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
809         }
810         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
811                    "tll="ETH_ADDR_SCAN_FMT")%n",
812                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
813                    ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
814             && n > 0) {
815             return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
816         }
817     }
818
819     return -EINVAL;
820 }
821
822 /* Parses the string representation of a datapath flow key, in the
823  * format output by odp_flow_key_format().  Returns 0 if successful,
824  * otherwise a positive errno value.  On success, the flow key is
825  * appended to 'key' as a series of Netlink attributes.  On failure, no
826  * data is appended to 'key'.  Either way, 'key''s data might be
827  * reallocated.
828  *
829  * On success, the attributes appended to 'key' are individually syntactically
830  * valid, but they may not be valid as a sequence.  'key' might, for example,
831  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
832 int
833 odp_flow_key_from_string(const char *s, struct ofpbuf *key)
834 {
835     const size_t old_size = key->size;
836     for (;;) {
837         int retval;
838
839         s += strspn(s, ", \t\r\n");
840         if (!*s) {
841             return 0;
842         }
843
844         retval = parse_odp_key_attr(s, key);
845         if (retval < 0) {
846             key->size = old_size;
847             return -retval;
848         }
849         s += retval;
850     }
851
852     return 0;
853 }
854
855 static uint8_t
856 ovs_to_odp_frag(uint8_t ovs_frag)
857 {
858     return (ovs_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
859             : ovs_frag & FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
860             : OVS_FRAG_TYPE_NONE);
861 }
862
863 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
864 void
865 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
866 {
867     struct ovs_key_ethernet *eth_key;
868
869     if (flow->priority) {
870         nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->priority);
871     }
872
873     if (flow->tun_id != htonll(0)) {
874         nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tun_id);
875     }
876
877     if (flow->in_port != OFPP_NONE) {
878         nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
879                        ofp_port_to_odp_port(flow->in_port));
880     }
881
882     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
883                                        sizeof *eth_key);
884     memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
885     memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
886
887     if (flow->vlan_tci != htons(0)) {
888         struct ovs_key_8021q *q_key;
889
890         q_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_8021Q,
891                                          sizeof *q_key);
892         q_key->q_tpid = htons(ETH_TYPE_VLAN);
893         q_key->q_tci = flow->vlan_tci & ~htons(VLAN_CFI);
894     }
895
896     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
897         return;
898     }
899
900     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
901
902     if (flow->dl_type == htons(ETH_TYPE_IP)) {
903         struct ovs_key_ipv4 *ipv4_key;
904
905         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
906                                             sizeof *ipv4_key);
907         ipv4_key->ipv4_src = flow->nw_src;
908         ipv4_key->ipv4_dst = flow->nw_dst;
909         ipv4_key->ipv4_proto = flow->nw_proto;
910         ipv4_key->ipv4_tos = flow->nw_tos;
911         ipv4_key->ipv4_ttl = flow->nw_ttl;
912         ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
913     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
914         struct ovs_key_ipv6 *ipv6_key;
915
916         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
917                                             sizeof *ipv6_key);
918         memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
919         memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
920         ipv6_key->ipv6_label = flow->ipv6_label;
921         ipv6_key->ipv6_proto = flow->nw_proto;
922         ipv6_key->ipv6_tclass = flow->nw_tos;
923         ipv6_key->ipv6_hlimit = flow->nw_ttl;
924         ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
925     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
926         struct ovs_key_arp *arp_key;
927
928         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
929                                            sizeof *arp_key);
930         memset(arp_key, 0, sizeof *arp_key);
931         arp_key->arp_sip = flow->nw_src;
932         arp_key->arp_tip = flow->nw_dst;
933         arp_key->arp_op = htons(flow->nw_proto);
934         memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
935         memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
936     }
937
938     if ((flow->dl_type == htons(ETH_TYPE_IP)
939          || flow->dl_type == htons(ETH_TYPE_IPV6))
940         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
941
942         if (flow->nw_proto == IPPROTO_TCP) {
943             struct ovs_key_tcp *tcp_key;
944
945             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
946                                                sizeof *tcp_key);
947             tcp_key->tcp_src = flow->tp_src;
948             tcp_key->tcp_dst = flow->tp_dst;
949         } else if (flow->nw_proto == IPPROTO_UDP) {
950             struct ovs_key_udp *udp_key;
951
952             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
953                                                sizeof *udp_key);
954             udp_key->udp_src = flow->tp_src;
955             udp_key->udp_dst = flow->tp_dst;
956         } else if (flow->dl_type == htons(ETH_TYPE_IP)
957                 && flow->nw_proto == IPPROTO_ICMP) {
958             struct ovs_key_icmp *icmp_key;
959
960             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
961                                                 sizeof *icmp_key);
962             icmp_key->icmp_type = ntohs(flow->tp_src);
963             icmp_key->icmp_code = ntohs(flow->tp_dst);
964         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
965                 && flow->nw_proto == IPPROTO_ICMPV6) {
966             struct ovs_key_icmpv6 *icmpv6_key;
967
968             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
969                                                   sizeof *icmpv6_key);
970             icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
971             icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
972
973             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
974                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
975                 struct ovs_key_nd *nd_key;
976
977                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
978                                                     sizeof *nd_key);
979                 memcpy(nd_key->nd_target, &flow->nd_target,
980                         sizeof nd_key->nd_target);
981                 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
982                 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
983             }
984         }
985     }
986 }
987
988 static void
989 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
990                        uint32_t attrs,
991                        const struct nlattr *key, size_t key_len)
992 {
993     struct ds s;
994     int i;
995
996     if (VLOG_DROP_WARN(rl)) {
997         return;
998     }
999
1000     ds_init(&s);
1001     ds_put_format(&s, "%s:", title);
1002     for (i = 0; i < 32; i++) {
1003         if (attrs & (1u << i)) {
1004             ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1005         }
1006     }
1007
1008     ds_put_cstr(&s, ": ");
1009     odp_flow_key_format(key, key_len, &s);
1010
1011     VLOG_WARN("%s", ds_cstr(&s));
1012     ds_destroy(&s);
1013 }
1014
1015 static bool
1016 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1017 {
1018     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1019
1020     if (odp_frag > OVS_FRAG_TYPE_LATER) {
1021         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key",
1022                     odp_frag);
1023         return false;
1024     }
1025
1026     if (odp_frag != OVS_FRAG_TYPE_NONE) {
1027         flow->nw_frag |= FLOW_NW_FRAG_ANY;
1028         if (odp_frag == OVS_FRAG_TYPE_LATER) {
1029             flow->nw_frag |= FLOW_NW_FRAG_LATER;
1030         }
1031     }
1032     return true;
1033 }
1034
1035 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1036  * structure in 'flow'.  Returns 0 if successful, otherwise EINVAL. */
1037 int
1038 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1039                      struct flow *flow)
1040 {
1041     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1042     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1043     const struct nlattr *nla;
1044     uint64_t expected_attrs;
1045     uint64_t present_attrs;
1046     uint64_t missing_attrs;
1047     uint64_t extra_attrs;
1048     size_t left;
1049
1050     memset(flow, 0, sizeof *flow);
1051
1052     memset(attrs, 0, sizeof attrs);
1053
1054     present_attrs = 0;
1055     expected_attrs = 0;
1056     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1057         uint16_t type = nl_attr_type(nla);
1058         size_t len = nl_attr_get_size(nla);
1059         int expected_len = odp_flow_key_attr_len(type);
1060
1061         if (len != expected_len) {
1062             if (expected_len == -1) {
1063                 VLOG_ERR_RL(&rl, "unknown attribute %"PRIu16" in flow key",
1064                             type);
1065             } else {
1066                 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1067                             "length %d", ovs_key_attr_to_string(type),
1068                             len, expected_len);
1069             }
1070             return EINVAL;
1071         } else if (present_attrs & (UINT64_C(1) << type)) {
1072             VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1073                         ovs_key_attr_to_string(type));
1074             return EINVAL;
1075         }
1076
1077         present_attrs |= UINT64_C(1) << type;
1078         attrs[type] = nla;
1079     }
1080     if (left) {
1081         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1082         return EINVAL;
1083     }
1084
1085     if (attrs[OVS_KEY_ATTR_PRIORITY]) {
1086         flow->priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1087         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1088     }
1089
1090     if (attrs[OVS_KEY_ATTR_TUN_ID]) {
1091         flow->tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1092         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1093     }
1094
1095     if (attrs[OVS_KEY_ATTR_IN_PORT]) {
1096         uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1097         if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1098             VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1099                         in_port);
1100             return EINVAL;
1101         }
1102         flow->in_port = odp_port_to_ofp_port(in_port);
1103         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1104     } else {
1105         flow->in_port = OFPP_NONE;
1106     }
1107
1108     if (attrs[OVS_KEY_ATTR_ETHERNET]) {
1109         const struct ovs_key_ethernet *eth_key;
1110
1111         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1112         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1113         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1114     } else {
1115         VLOG_ERR_RL(&rl, "missing Ethernet attribute in flow key");
1116         return EINVAL;
1117     }
1118     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1119
1120     if (attrs[OVS_KEY_ATTR_8021Q]) {
1121         const struct ovs_key_8021q *q_key;
1122
1123         q_key = nl_attr_get(attrs[OVS_KEY_ATTR_8021Q]);
1124         if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
1125             VLOG_ERR_RL(&rl, "unsupported 802.1Q TPID %"PRIu16" in flow key",
1126                         ntohs(q_key->q_tpid));
1127             return EINVAL;
1128         }
1129         if (q_key->q_tci & htons(VLAN_CFI)) {
1130             VLOG_ERR_RL(&rl, "invalid 802.1Q TCI %"PRIu16" in flow key",
1131                         ntohs(q_key->q_tci));
1132             return EINVAL;
1133         }
1134         flow->vlan_tci = q_key->q_tci | htons(VLAN_CFI);
1135         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_8021Q;
1136     }
1137
1138     if (attrs[OVS_KEY_ATTR_ETHERTYPE]) {
1139         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1140         if (ntohs(flow->dl_type) < 1536) {
1141             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1142                         ntohs(flow->dl_type));
1143             return EINVAL;
1144         }
1145         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1146     } else {
1147         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1148     }
1149
1150     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1151         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1152         if (attrs[OVS_KEY_ATTR_IPV4]) {
1153             const struct ovs_key_ipv4 *ipv4_key;
1154
1155             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1156             flow->nw_src = ipv4_key->ipv4_src;
1157             flow->nw_dst = ipv4_key->ipv4_dst;
1158             flow->nw_proto = ipv4_key->ipv4_proto;
1159             flow->nw_tos = ipv4_key->ipv4_tos;
1160             flow->nw_ttl = ipv4_key->ipv4_ttl;
1161             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1162                 return EINVAL;
1163             }
1164         }
1165     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1166         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1167         if (attrs[OVS_KEY_ATTR_IPV6]) {
1168             const struct ovs_key_ipv6 *ipv6_key;
1169
1170             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1171             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1172             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1173             flow->ipv6_label = ipv6_key->ipv6_label;
1174             flow->nw_proto = ipv6_key->ipv6_proto;
1175             flow->nw_tos = ipv6_key->ipv6_tclass;
1176             flow->nw_ttl = ipv6_key->ipv6_hlimit;
1177             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1178                 return EINVAL;
1179             }
1180         }
1181     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1182         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1183         if (attrs[OVS_KEY_ATTR_ARP]) {
1184             const struct ovs_key_arp *arp_key;
1185
1186             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1187             flow->nw_src = arp_key->arp_sip;
1188             flow->nw_dst = arp_key->arp_tip;
1189             if (arp_key->arp_op & htons(0xff00)) {
1190                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1191                             "key", ntohs(arp_key->arp_op));
1192                 return EINVAL;
1193             }
1194             flow->nw_proto = ntohs(arp_key->arp_op);
1195             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1196             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1197         }
1198     }
1199
1200     if (flow->nw_proto == IPPROTO_TCP
1201         && (flow->dl_type == htons(ETH_TYPE_IP) ||
1202             flow->dl_type == htons(ETH_TYPE_IPV6))
1203         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1204         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1205         if (attrs[OVS_KEY_ATTR_TCP]) {
1206             const struct ovs_key_tcp *tcp_key;
1207
1208             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1209             flow->tp_src = tcp_key->tcp_src;
1210             flow->tp_dst = tcp_key->tcp_dst;
1211         }
1212     } else if (flow->nw_proto == IPPROTO_UDP
1213                && (flow->dl_type == htons(ETH_TYPE_IP) ||
1214                    flow->dl_type == htons(ETH_TYPE_IPV6))
1215                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1216         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1217         if (attrs[OVS_KEY_ATTR_UDP]) {
1218             const struct ovs_key_udp *udp_key;
1219
1220             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1221             flow->tp_src = udp_key->udp_src;
1222             flow->tp_dst = udp_key->udp_dst;
1223         }
1224     } else if (flow->nw_proto == IPPROTO_ICMP
1225                && flow->dl_type == htons(ETH_TYPE_IP)
1226                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1227         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1228         if (attrs[OVS_KEY_ATTR_ICMP]) {
1229             const struct ovs_key_icmp *icmp_key;
1230
1231             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1232             flow->tp_src = htons(icmp_key->icmp_type);
1233             flow->tp_dst = htons(icmp_key->icmp_code);
1234         }
1235     } else if (flow->nw_proto == IPPROTO_ICMPV6
1236                && flow->dl_type == htons(ETH_TYPE_IPV6)
1237                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1238         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1239         if (attrs[OVS_KEY_ATTR_ICMPV6]) {
1240             const struct ovs_key_icmpv6 *icmpv6_key;
1241
1242             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1243             flow->tp_src = htons(icmpv6_key->icmpv6_type);
1244             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1245
1246             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1247                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1248                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1249                 if (attrs[OVS_KEY_ATTR_ND]) {
1250                     const struct ovs_key_nd *nd_key;
1251
1252                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1253                     memcpy(&flow->nd_target, nd_key->nd_target,
1254                            sizeof flow->nd_target);
1255                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1256                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1257                 }
1258             }
1259         }
1260     }
1261
1262     missing_attrs = expected_attrs & ~present_attrs;
1263     if (missing_attrs) {
1264         static struct vlog_rate_limit miss_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1265         log_odp_key_attributes(&miss_rl, "expected but not present",
1266                                missing_attrs, key, key_len);
1267         return EINVAL;
1268     }
1269
1270     extra_attrs = present_attrs & ~expected_attrs;
1271     if (extra_attrs) {
1272         static struct vlog_rate_limit extra_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1273         log_odp_key_attributes(&extra_rl, "present but not expected",
1274                                extra_attrs, key, key_len);
1275         return EINVAL;
1276     }
1277
1278     return 0;
1279 }