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