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