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