odp-util: Use proper formatting for ODP port number.
[cascardo/ovs.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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 <config.h>
18 #include <arpa/inet.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 "packets.h"
34 #include "simap.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(odp_util);
40
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42  * Since this is fairly non-specific for the OVS userspace components,
43  * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44  * interactions with the datapath.
45  */
46
47 /* The set of characters that may separate one action or one key attribute
48  * from another. */
49 static const char *delimiters = ", \t\r\n";
50
51 static int parse_odp_key_attr(const char *, const struct simap *port_names,
52                               struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
54
55 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
56  * 'type':
57  *
58  *   - For an action whose argument has a fixed length, returned that
59  *     nonnegative length in bytes.
60  *
61  *   - For an action with a variable-length argument, returns -2.
62  *
63  *   - For an invalid 'type', returns -1. */
64 static int
65 odp_action_len(uint16_t type)
66 {
67     if (type > OVS_ACTION_ATTR_MAX) {
68         return -1;
69     }
70
71     switch ((enum ovs_action_attr) type) {
72     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
73     case OVS_ACTION_ATTR_USERSPACE: return -2;
74     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75     case OVS_ACTION_ATTR_POP_VLAN: return 0;
76     case OVS_ACTION_ATTR_SET: return -2;
77     case OVS_ACTION_ATTR_SAMPLE: return -2;
78
79     case OVS_ACTION_ATTR_UNSPEC:
80     case __OVS_ACTION_ATTR_MAX:
81         return -1;
82     }
83
84     return -1;
85 }
86
87 static const char *
88 ovs_key_attr_to_string(enum ovs_key_attr attr)
89 {
90     static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
91
92     switch (attr) {
93     case OVS_KEY_ATTR_UNSPEC: return "unspec";
94     case OVS_KEY_ATTR_ENCAP: return "encap";
95     case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
96     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
97     case OVS_KEY_ATTR_TUN_ID: return "tun_id";
98     case OVS_KEY_ATTR_TUNNEL: return "tunnel";
99     case OVS_KEY_ATTR_IN_PORT: return "in_port";
100     case OVS_KEY_ATTR_ETHERNET: return "eth";
101     case OVS_KEY_ATTR_VLAN: return "vlan";
102     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
103     case OVS_KEY_ATTR_IPV4: return "ipv4";
104     case OVS_KEY_ATTR_IPV6: return "ipv6";
105     case OVS_KEY_ATTR_TCP: return "tcp";
106     case OVS_KEY_ATTR_UDP: return "udp";
107     case OVS_KEY_ATTR_ICMP: return "icmp";
108     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
109     case OVS_KEY_ATTR_ARP: return "arp";
110     case OVS_KEY_ATTR_ND: return "nd";
111
112     case __OVS_KEY_ATTR_MAX:
113     default:
114         snprintf(unknown_attr, sizeof unknown_attr, "key%u",
115                  (unsigned int) attr);
116         return unknown_attr;
117     }
118 }
119
120 static void
121 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
122 {
123     size_t len = nl_attr_get_size(a);
124
125     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
126     if (len) {
127         const uint8_t *unspec;
128         unsigned int i;
129
130         unspec = nl_attr_get(a);
131         for (i = 0; i < len; i++) {
132             ds_put_char(ds, i ? ' ': '(');
133             ds_put_format(ds, "%02x", unspec[i]);
134         }
135         ds_put_char(ds, ')');
136     }
137 }
138
139 static void
140 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
141 {
142     static const struct nl_policy ovs_sample_policy[] = {
143         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
144         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
145     };
146     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
147     double percentage;
148     const struct nlattr *nla_acts;
149     int len;
150
151     ds_put_cstr(ds, "sample");
152
153     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
154         ds_put_cstr(ds, "(error)");
155         return;
156     }
157
158     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
159                         UINT32_MAX;
160
161     ds_put_format(ds, "(sample=%.1f%%,", percentage);
162
163     ds_put_cstr(ds, "actions(");
164     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
165     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
166     format_odp_actions(ds, nla_acts, len);
167     ds_put_format(ds, "))");
168 }
169
170 static const char *
171 slow_path_reason_to_string(uint32_t data)
172 {
173     enum slow_path_reason bit = (enum slow_path_reason) data;
174
175     switch (bit) {
176     case SLOW_CFM:
177         return "cfm";
178     case SLOW_LACP:
179         return "lacp";
180     case SLOW_STP:
181         return "stp";
182     case SLOW_IN_BAND:
183         return "in_band";
184     case SLOW_CONTROLLER:
185         return "controller";
186     case SLOW_MATCH:
187         return "match";
188     default:
189         return NULL;
190     }
191 }
192
193 static int
194 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
195             uint32_t *res)
196 {
197     uint32_t result = 0;
198     int n = 0;
199
200     if (s[n] != '(') {
201         return -EINVAL;
202     }
203     n++;
204
205     while (s[n] != ')') {
206         unsigned long long int flags;
207         uint32_t bit;
208         int n0;
209
210         if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
211             n += n0 + (s[n + n0] == ',');
212             result |= flags;
213             continue;
214         }
215
216         for (bit = 1; bit; bit <<= 1) {
217             const char *name = bit_to_string(bit);
218             size_t len;
219
220             if (!name) {
221                 continue;
222             }
223
224             len = strlen(name);
225             if (!strncmp(s + n, name, len) &&
226                 (s[n + len] == ',' || s[n + len] == ')')) {
227                 result |= bit;
228                 n += len + (s[n + len] == ',');
229                 break;
230             }
231         }
232
233         if (!bit) {
234             return -EINVAL;
235         }
236     }
237     n++;
238
239     *res = result;
240     return n;
241 }
242
243 static void
244 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
245 {
246     static const struct nl_policy ovs_userspace_policy[] = {
247         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
248         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
249     };
250     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
251
252     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
253         ds_put_cstr(ds, "userspace(error)");
254         return;
255     }
256
257     ds_put_format(ds, "userspace(pid=%"PRIu32,
258                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
259
260     if (a[OVS_USERSPACE_ATTR_USERDATA]) {
261         uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
262         union user_action_cookie cookie;
263
264         memcpy(&cookie, &userdata, sizeof cookie);
265
266         switch (cookie.type) {
267         case USER_ACTION_COOKIE_SFLOW:
268             ds_put_format(ds, ",sFlow("
269                           "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
270                           vlan_tci_to_vid(cookie.sflow.vlan_tci),
271                           vlan_tci_to_pcp(cookie.sflow.vlan_tci),
272                           cookie.sflow.output);
273             break;
274
275         case USER_ACTION_COOKIE_SLOW_PATH:
276             ds_put_cstr(ds, ",slow_path(");
277             format_flags(ds, slow_path_reason_to_string,
278                          cookie.slow_path.reason, ',');
279             ds_put_format(ds, ")");
280             break;
281
282         case USER_ACTION_COOKIE_UNSPEC:
283         default:
284             ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
285             break;
286         }
287     }
288
289     ds_put_char(ds, ')');
290 }
291
292 static void
293 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
294 {
295     ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
296                   vlan_tci_to_vid(vlan_tci),
297                   vlan_tci_to_pcp(vlan_tci));
298     if (!(vlan_tci & htons(VLAN_CFI))) {
299         ds_put_cstr(ds, ",cfi=0");
300     }
301 }
302
303 static void
304 format_odp_action(struct ds *ds, const struct nlattr *a)
305 {
306     int expected_len;
307     enum ovs_action_attr type = nl_attr_type(a);
308     const struct ovs_action_push_vlan *vlan;
309
310     expected_len = odp_action_len(nl_attr_type(a));
311     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
312         ds_put_format(ds, "bad length %zu, expected %d for: ",
313                       nl_attr_get_size(a), expected_len);
314         format_generic_odp_action(ds, a);
315         return;
316     }
317
318     switch (type) {
319     case OVS_ACTION_ATTR_OUTPUT:
320         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
321         break;
322     case OVS_ACTION_ATTR_USERSPACE:
323         format_odp_userspace_action(ds, a);
324         break;
325     case OVS_ACTION_ATTR_SET:
326         ds_put_cstr(ds, "set(");
327         format_odp_key_attr(nl_attr_get(a), ds);
328         ds_put_cstr(ds, ")");
329         break;
330     case OVS_ACTION_ATTR_PUSH_VLAN:
331         vlan = nl_attr_get(a);
332         ds_put_cstr(ds, "push_vlan(");
333         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
334             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
335         }
336         format_vlan_tci(ds, vlan->vlan_tci);
337         ds_put_char(ds, ')');
338         break;
339     case OVS_ACTION_ATTR_POP_VLAN:
340         ds_put_cstr(ds, "pop_vlan");
341         break;
342     case OVS_ACTION_ATTR_SAMPLE:
343         format_odp_sample_action(ds, a);
344         break;
345     case OVS_ACTION_ATTR_UNSPEC:
346     case __OVS_ACTION_ATTR_MAX:
347     default:
348         format_generic_odp_action(ds, a);
349         break;
350     }
351 }
352
353 void
354 format_odp_actions(struct ds *ds, const struct nlattr *actions,
355                    size_t actions_len)
356 {
357     if (actions_len) {
358         const struct nlattr *a;
359         unsigned int left;
360
361         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
362             if (a != actions) {
363                 ds_put_char(ds, ',');
364             }
365             format_odp_action(ds, a);
366         }
367         if (left) {
368             int i;
369
370             if (left == actions_len) {
371                 ds_put_cstr(ds, "<empty>");
372             }
373             ds_put_format(ds, ",***%u leftover bytes*** (", left);
374             for (i = 0; i < left; i++) {
375                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
376             }
377             ds_put_char(ds, ')');
378         }
379     } else {
380         ds_put_cstr(ds, "drop");
381     }
382 }
383
384 static int
385 parse_odp_action(const char *s, const struct simap *port_names,
386                  struct ofpbuf *actions)
387 {
388     /* Many of the sscanf calls in this function use oversized destination
389      * fields because some sscanf() implementations truncate the range of %i
390      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
391      * value of 0x7fff.  The other alternatives are to allow only a single
392      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
393      * parsers.
394      *
395      * The tun_id parser has to use an alternative approach because there is no
396      * type larger than 64 bits. */
397
398     {
399         unsigned long long int port;
400         int n = -1;
401
402         if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
403             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
404             return n;
405         }
406     }
407
408     if (port_names) {
409         int len = strcspn(s, delimiters);
410         struct simap_node *node;
411
412         node = simap_find_len(port_names, s, len);
413         if (node) {
414             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
415             return len;
416         }
417     }
418
419     {
420         unsigned long long int pid;
421         unsigned long long int output;
422         char userdata_s[32];
423         int vid, pcp;
424         int n = -1;
425
426         if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
427             odp_put_userspace_action(pid, NULL, actions);
428             return n;
429         } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
430                           "pcp=%i,output=%lli))%n",
431                           &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
432             union user_action_cookie cookie;
433             uint16_t tci;
434
435             tci = vid | (pcp << VLAN_PCP_SHIFT);
436             if (tci) {
437                 tci |= VLAN_CFI;
438             }
439
440             cookie.type = USER_ACTION_COOKIE_SFLOW;
441             cookie.sflow.vlan_tci = htons(tci);
442             cookie.sflow.output = output;
443             odp_put_userspace_action(pid, &cookie, actions);
444             return n;
445         } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
446                    && n > 0) {
447             union user_action_cookie cookie;
448             int res;
449
450             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
451             cookie.slow_path.unused = 0;
452             cookie.slow_path.reason = 0;
453
454             res = parse_flags(&s[n], slow_path_reason_to_string,
455                               &cookie.slow_path.reason);
456             if (res < 0) {
457                 return res;
458             }
459             n += res;
460             if (s[n] != ')') {
461                 return -EINVAL;
462             }
463             n++;
464
465             odp_put_userspace_action(pid, &cookie, actions);
466             return n;
467         } else if (sscanf(s, "userspace(pid=%lli,userdata="
468                           "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
469                           &n) > 0 && n > 0) {
470             union user_action_cookie cookie;
471             uint64_t userdata;
472
473             userdata = strtoull(userdata_s, NULL, 0);
474             memcpy(&cookie, &userdata, sizeof cookie);
475             odp_put_userspace_action(pid, &cookie, actions);
476             return n;
477         }
478     }
479
480     if (!strncmp(s, "set(", 4)) {
481         size_t start_ofs;
482         int retval;
483
484         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
485         retval = parse_odp_key_attr(s + 4, port_names, actions);
486         if (retval < 0) {
487             return retval;
488         }
489         if (s[retval + 4] != ')') {
490             return -EINVAL;
491         }
492         nl_msg_end_nested(actions, start_ofs);
493         return retval + 5;
494     }
495
496     {
497         struct ovs_action_push_vlan push;
498         int tpid = ETH_TYPE_VLAN;
499         int vid, pcp;
500         int cfi = 1;
501         int n = -1;
502
503         if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
504              && n > 0)
505             || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
506                        &vid, &pcp, &cfi, &n) > 0 && n > 0)
507             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
508                        &tpid, &vid, &pcp, &n) > 0 && n > 0)
509             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
510                        &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
511             push.vlan_tpid = htons(tpid);
512             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
513                                   | (pcp << VLAN_PCP_SHIFT)
514                                   | (cfi ? VLAN_CFI : 0));
515             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
516                               &push, sizeof push);
517
518             return n;
519         }
520     }
521
522     if (!strncmp(s, "pop_vlan", 8)) {
523         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
524         return 8;
525     }
526
527     {
528         double percentage;
529         int n = -1;
530
531         if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
532             && percentage >= 0. && percentage <= 100.0
533             && n > 0) {
534             size_t sample_ofs, actions_ofs;
535             double probability;
536
537             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
538             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
539             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
540                            (probability <= 0 ? 0
541                             : probability >= UINT32_MAX ? UINT32_MAX
542                             : probability));
543
544             actions_ofs = nl_msg_start_nested(actions,
545                                               OVS_SAMPLE_ATTR_ACTIONS);
546             for (;;) {
547                 int retval;
548
549                 n += strspn(s + n, delimiters);
550                 if (s[n] == ')') {
551                     break;
552                 }
553
554                 retval = parse_odp_action(s + n, port_names, actions);
555                 if (retval < 0) {
556                     return retval;
557                 }
558                 n += retval;
559             }
560             nl_msg_end_nested(actions, actions_ofs);
561             nl_msg_end_nested(actions, sample_ofs);
562
563             return s[n + 1] == ')' ? n + 2 : -EINVAL;
564         }
565     }
566
567     return -EINVAL;
568 }
569
570 /* Parses the string representation of datapath actions, in the format output
571  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
572  * value.  On success, the ODP actions are appended to 'actions' as a series of
573  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
574  * way, 'actions''s data might be reallocated. */
575 int
576 odp_actions_from_string(const char *s, const struct simap *port_names,
577                         struct ofpbuf *actions)
578 {
579     size_t old_size;
580
581     if (!strcasecmp(s, "drop")) {
582         return 0;
583     }
584
585     old_size = actions->size;
586     for (;;) {
587         int retval;
588
589         s += strspn(s, delimiters);
590         if (!*s) {
591             return 0;
592         }
593
594         retval = parse_odp_action(s, port_names, actions);
595         if (retval < 0 || !strchr(delimiters, s[retval])) {
596             actions->size = old_size;
597             return -retval;
598         }
599         s += retval;
600     }
601
602     return 0;
603 }
604 \f
605 /* Returns the correct length of the payload for a flow key attribute of the
606  * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
607  * is variable length. */
608 static int
609 odp_flow_key_attr_len(uint16_t type)
610 {
611     if (type > OVS_KEY_ATTR_MAX) {
612         return -1;
613     }
614
615     switch ((enum ovs_key_attr) type) {
616     case OVS_KEY_ATTR_ENCAP: return -2;
617     case OVS_KEY_ATTR_PRIORITY: return 4;
618     case OVS_KEY_ATTR_SKB_MARK: return 4;
619     case OVS_KEY_ATTR_TUN_ID: return 8;
620     case OVS_KEY_ATTR_TUNNEL: return -2;
621     case OVS_KEY_ATTR_IN_PORT: return 4;
622     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
623     case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
624     case OVS_KEY_ATTR_ETHERTYPE: return 2;
625     case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
626     case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
627     case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
628     case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
629     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
630     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
631     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
632     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
633
634     case OVS_KEY_ATTR_UNSPEC:
635     case __OVS_KEY_ATTR_MAX:
636         return -1;
637     }
638
639     return -1;
640 }
641
642 static void
643 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
644 {
645     size_t len = nl_attr_get_size(a);
646     if (len) {
647         const uint8_t *unspec;
648         unsigned int i;
649
650         unspec = nl_attr_get(a);
651         for (i = 0; i < len; i++) {
652             ds_put_char(ds, i ? ' ': '(');
653             ds_put_format(ds, "%02x", unspec[i]);
654         }
655         ds_put_char(ds, ')');
656     }
657 }
658
659 static const char *
660 ovs_frag_type_to_string(enum ovs_frag_type type)
661 {
662     switch (type) {
663     case OVS_FRAG_TYPE_NONE:
664         return "no";
665     case OVS_FRAG_TYPE_FIRST:
666         return "first";
667     case OVS_FRAG_TYPE_LATER:
668         return "later";
669     case __OVS_FRAG_TYPE_MAX:
670     default:
671         return "<error>";
672     }
673 }
674
675 static int
676 tunnel_key_attr_len(int type)
677 {
678     switch (type) {
679     case OVS_TUNNEL_KEY_ATTR_ID: return 8;
680     case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
681     case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
682     case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
683     case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
684     case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
685     case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
686     case __OVS_TUNNEL_KEY_ATTR_MAX:
687         return -1;
688     }
689     return -1;
690 }
691
692 static enum odp_key_fitness
693 tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
694 {
695     unsigned int left;
696     const struct nlattr *a;
697     bool ttl = false;
698     bool unknown = false;
699
700     NL_NESTED_FOR_EACH(a, left, attr) {
701         uint16_t type = nl_attr_type(a);
702         size_t len = nl_attr_get_size(a);
703         int expected_len = tunnel_key_attr_len(type);
704
705         if (len != expected_len && expected_len >= 0) {
706             return ODP_FIT_ERROR;
707         }
708
709         switch (type) {
710         case OVS_TUNNEL_KEY_ATTR_ID:
711             tun->tun_id = nl_attr_get_be64(a);
712             tun->flags |= FLOW_TNL_F_KEY;
713             break;
714         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
715             tun->ip_src = nl_attr_get_be32(a);
716             break;
717         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
718             tun->ip_dst = nl_attr_get_be32(a);
719             break;
720         case OVS_TUNNEL_KEY_ATTR_TOS:
721             tun->ip_tos = nl_attr_get_u8(a);
722             break;
723         case OVS_TUNNEL_KEY_ATTR_TTL:
724             tun->ip_ttl = nl_attr_get_u8(a);
725             ttl = true;
726             break;
727         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
728             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
729             break;
730         case OVS_TUNNEL_KEY_ATTR_CSUM:
731             tun->flags |= FLOW_TNL_F_CSUM;
732             break;
733         default:
734             /* Allow this to show up as unexpected, if there are unknown
735              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
736             unknown = true;
737             break;
738         }
739     }
740
741     if (!ttl) {
742         return ODP_FIT_ERROR;
743     }
744     if (unknown) {
745             return ODP_FIT_TOO_MUCH;
746     }
747     return ODP_FIT_PERFECT;
748 }
749
750 static void
751 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
752 {
753     size_t tun_key_ofs;
754
755     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
756
757     if (tun_key->flags & FLOW_TNL_F_KEY) {
758         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
759     }
760     if (tun_key->ip_src) {
761         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
762     }
763     if (tun_key->ip_dst) {
764         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
765     }
766     if (tun_key->ip_tos) {
767         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
768     }
769     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
770     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
771         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
772     }
773     if (tun_key->flags & FLOW_TNL_F_CSUM) {
774         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
775     }
776
777     nl_msg_end_nested(a, tun_key_ofs);
778 }
779
780 static void
781 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
782 {
783     const struct ovs_key_ethernet *eth_key;
784     const struct ovs_key_ipv4 *ipv4_key;
785     const struct ovs_key_ipv6 *ipv6_key;
786     const struct ovs_key_tcp *tcp_key;
787     const struct ovs_key_udp *udp_key;
788     const struct ovs_key_icmp *icmp_key;
789     const struct ovs_key_icmpv6 *icmpv6_key;
790     const struct ovs_key_arp *arp_key;
791     const struct ovs_key_nd *nd_key;
792     struct flow_tnl tun_key;
793     enum ovs_key_attr attr = nl_attr_type(a);
794     int expected_len;
795
796     ds_put_cstr(ds, ovs_key_attr_to_string(attr));
797     expected_len = odp_flow_key_attr_len(nl_attr_type(a));
798     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
799         ds_put_format(ds, "(bad length %zu, expected %d)",
800                       nl_attr_get_size(a),
801                       odp_flow_key_attr_len(nl_attr_type(a)));
802         format_generic_odp_key(a, ds);
803         return;
804     }
805
806     switch (attr) {
807     case OVS_KEY_ATTR_ENCAP:
808         ds_put_cstr(ds, "(");
809         if (nl_attr_get_size(a)) {
810             odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
811         }
812         ds_put_char(ds, ')');
813         break;
814
815     case OVS_KEY_ATTR_PRIORITY:
816         ds_put_format(ds, "(%#"PRIx32")", nl_attr_get_u32(a));
817         break;
818
819     case OVS_KEY_ATTR_SKB_MARK:
820         ds_put_format(ds, "(%#"PRIx32")", nl_attr_get_u32(a));
821         break;
822
823     case OVS_KEY_ATTR_TUN_ID:
824         ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
825         break;
826
827     case OVS_KEY_ATTR_TUNNEL:
828         memset(&tun_key, 0, sizeof tun_key);
829         if (tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
830             ds_put_format(ds, "(error)");
831         } else {
832             ds_put_format(ds, "(tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
833                           "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
834                           ntohll(tun_key.tun_id),
835                           IP_ARGS(&tun_key.ip_src),
836                           IP_ARGS(&tun_key.ip_dst),
837                           tun_key.ip_tos, tun_key.ip_ttl);
838
839             format_flags(ds, flow_tun_flag_to_string,
840                          (uint32_t) tun_key.flags, ',');
841             ds_put_format(ds, "))");
842         }
843
844         break;
845
846     case OVS_KEY_ATTR_IN_PORT:
847         ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
848         break;
849
850     case OVS_KEY_ATTR_ETHERNET:
851         eth_key = nl_attr_get(a);
852         ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
853                       ETH_ADDR_ARGS(eth_key->eth_src),
854                       ETH_ADDR_ARGS(eth_key->eth_dst));
855         break;
856
857     case OVS_KEY_ATTR_VLAN:
858         ds_put_char(ds, '(');
859         format_vlan_tci(ds, nl_attr_get_be16(a));
860         ds_put_char(ds, ')');
861         break;
862
863     case OVS_KEY_ATTR_ETHERTYPE:
864         ds_put_format(ds, "(0x%04"PRIx16")",
865                       ntohs(nl_attr_get_be16(a)));
866         break;
867
868     case OVS_KEY_ATTR_IPV4:
869         ipv4_key = nl_attr_get(a);
870         ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
871                       ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
872                       IP_ARGS(&ipv4_key->ipv4_src),
873                       IP_ARGS(&ipv4_key->ipv4_dst),
874                       ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
875                       ipv4_key->ipv4_ttl,
876                       ovs_frag_type_to_string(ipv4_key->ipv4_frag));
877         break;
878
879     case OVS_KEY_ATTR_IPV6: {
880         char src_str[INET6_ADDRSTRLEN];
881         char dst_str[INET6_ADDRSTRLEN];
882
883         ipv6_key = nl_attr_get(a);
884         inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
885         inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
886
887         ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
888                       ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
889                       src_str, dst_str, ntohl(ipv6_key->ipv6_label),
890                       ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
891                       ipv6_key->ipv6_hlimit,
892                       ovs_frag_type_to_string(ipv6_key->ipv6_frag));
893         break;
894     }
895
896     case OVS_KEY_ATTR_TCP:
897         tcp_key = nl_attr_get(a);
898         ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
899                       ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
900         break;
901
902     case OVS_KEY_ATTR_UDP:
903         udp_key = nl_attr_get(a);
904         ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
905                       ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
906         break;
907
908     case OVS_KEY_ATTR_ICMP:
909         icmp_key = nl_attr_get(a);
910         ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
911                       icmp_key->icmp_type, icmp_key->icmp_code);
912         break;
913
914     case OVS_KEY_ATTR_ICMPV6:
915         icmpv6_key = nl_attr_get(a);
916         ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
917                       icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
918         break;
919
920     case OVS_KEY_ATTR_ARP:
921         arp_key = nl_attr_get(a);
922         ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
923                       "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
924                       IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
925                       ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
926                       ETH_ADDR_ARGS(arp_key->arp_tha));
927         break;
928
929     case OVS_KEY_ATTR_ND: {
930         char target[INET6_ADDRSTRLEN];
931
932         nd_key = nl_attr_get(a);
933         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
934
935         ds_put_format(ds, "(target=%s", target);
936         if (!eth_addr_is_zero(nd_key->nd_sll)) {
937             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
938                           ETH_ADDR_ARGS(nd_key->nd_sll));
939         }
940         if (!eth_addr_is_zero(nd_key->nd_tll)) {
941             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
942                           ETH_ADDR_ARGS(nd_key->nd_tll));
943         }
944         ds_put_char(ds, ')');
945         break;
946     }
947
948     case OVS_KEY_ATTR_UNSPEC:
949     case __OVS_KEY_ATTR_MAX:
950     default:
951         format_generic_odp_key(a, ds);
952         break;
953     }
954 }
955
956 /* Appends to 'ds' a string representation of the 'key_len' bytes of
957  * OVS_KEY_ATTR_* attributes in 'key'. */
958 void
959 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
960 {
961     if (key_len) {
962         const struct nlattr *a;
963         unsigned int left;
964
965         NL_ATTR_FOR_EACH (a, left, key, key_len) {
966             if (a != key) {
967                 ds_put_char(ds, ',');
968             }
969             format_odp_key_attr(a, ds);
970         }
971         if (left) {
972             int i;
973             
974             if (left == key_len) {
975                 ds_put_cstr(ds, "<empty>");
976             }
977             ds_put_format(ds, ",***%u leftover bytes*** (", left);
978             for (i = 0; i < left; i++) {
979                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
980             }
981             ds_put_char(ds, ')');
982         }
983     } else {
984         ds_put_cstr(ds, "<empty>");
985     }
986 }
987
988 static int
989 put_nd_key(int n, const char *nd_target_s,
990            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
991 {
992     struct ovs_key_nd nd_key;
993
994     memset(&nd_key, 0, sizeof nd_key);
995     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
996         return -EINVAL;
997     }
998     if (nd_sll) {
999         memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
1000     }
1001     if (nd_tll) {
1002         memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
1003     }
1004     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
1005     return n;
1006 }
1007
1008 static bool
1009 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1010 {
1011     if (!strcasecmp(s, "no")) {
1012         *type = OVS_FRAG_TYPE_NONE;
1013     } else if (!strcasecmp(s, "first")) {
1014         *type = OVS_FRAG_TYPE_FIRST;
1015     } else if (!strcasecmp(s, "later")) {
1016         *type = OVS_FRAG_TYPE_LATER;
1017     } else {
1018         return false;
1019     }
1020     return true;
1021 }
1022
1023 static int
1024 parse_odp_key_attr(const char *s, const struct simap *port_names,
1025                    struct ofpbuf *key)
1026 {
1027     /* Many of the sscanf calls in this function use oversized destination
1028      * fields because some sscanf() implementations truncate the range of %i
1029      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1030      * value of 0x7fff.  The other alternatives are to allow only a single
1031      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1032      * parsers.
1033      *
1034      * The tun_id parser has to use an alternative approach because there is no
1035      * type larger than 64 bits. */
1036
1037     {
1038         unsigned long long int priority;
1039         int n = -1;
1040
1041         if (sscanf(s, "skb_priority(%llx)%n", &priority, &n) > 0 && n > 0) {
1042             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1043             return n;
1044         }
1045     }
1046
1047     {
1048         unsigned long long int mark;
1049         int n = -1;
1050
1051         if (sscanf(s, "skb_mark(%llx)%n", &mark, &n) > 0 && n > 0) {
1052             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1053             return n;
1054         }
1055     }
1056
1057     {
1058         char tun_id_s[32];
1059         int n = -1;
1060
1061         if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
1062                    tun_id_s, &n) > 0 && n > 0) {
1063             uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
1064             nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
1065             return n;
1066         }
1067     }
1068
1069     {
1070         char tun_id_s[32];
1071         int tos, ttl;
1072         struct flow_tnl tun_key;
1073         int n = -1;
1074
1075         if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1076                    "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1077                    ",tos=%i,ttl=%i,flags%n", tun_id_s,
1078                     IP_SCAN_ARGS(&tun_key.ip_src),
1079                     IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1080                     &n) > 0 && n > 0) {
1081             int res;
1082             uint32_t flags;
1083
1084             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1085             tun_key.ip_tos = tos;
1086             tun_key.ip_ttl = ttl;
1087             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1088             tun_key.flags = (uint16_t) flags;
1089
1090             if (res < 0) {
1091                 return res;
1092             }
1093             n += res;
1094             if (s[n] != ')') {
1095                 return -EINVAL;
1096             }
1097             n++;
1098             tun_key_to_attr(key, &tun_key);
1099             return n;
1100         }
1101     }
1102
1103     {
1104         unsigned long long int in_port;
1105         int n = -1;
1106
1107         if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1108             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1109             return n;
1110         }
1111     }
1112
1113     if (port_names && !strncmp(s, "in_port(", 8)) {
1114         const char *name;
1115         const struct simap_node *node;
1116         int name_len;
1117
1118         name = s + 8;
1119         name_len = strcspn(s, ")");
1120         node = simap_find_len(port_names, name, name_len);
1121         if (node) {
1122             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1123             return 8 + name_len + 1;
1124         }
1125     }
1126
1127     {
1128         struct ovs_key_ethernet eth_key;
1129         int n = -1;
1130
1131         if (sscanf(s,
1132                    "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1133                    ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1134                    ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1135             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1136                               &eth_key, sizeof eth_key);
1137             return n;
1138         }
1139     }
1140
1141     {
1142         uint16_t vid;
1143         int pcp;
1144         int cfi;
1145         int n = -1;
1146
1147         if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1148              && n > 0)) {
1149             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1150                             htons((vid << VLAN_VID_SHIFT) |
1151                                   (pcp << VLAN_PCP_SHIFT) |
1152                                   VLAN_CFI));
1153             return n;
1154         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1155                            &vid, &pcp, &cfi, &n) > 0
1156              && n > 0)) {
1157             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1158                             htons((vid << VLAN_VID_SHIFT) |
1159                                   (pcp << VLAN_PCP_SHIFT) |
1160                                   (cfi ? VLAN_CFI : 0)));
1161             return n;
1162         }
1163     }
1164
1165     {
1166         int eth_type;
1167         int n = -1;
1168
1169         if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1170             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1171             return n;
1172         }
1173     }
1174
1175     {
1176         ovs_be32 ipv4_src;
1177         ovs_be32 ipv4_dst;
1178         int ipv4_proto;
1179         int ipv4_tos;
1180         int ipv4_ttl;
1181         char frag[8];
1182         enum ovs_frag_type ipv4_frag;
1183         int n = -1;
1184
1185         if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1186                    "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1187                    IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1188                    &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1189             && n > 0
1190             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1191             struct ovs_key_ipv4 ipv4_key;
1192
1193             ipv4_key.ipv4_src = ipv4_src;
1194             ipv4_key.ipv4_dst = ipv4_dst;
1195             ipv4_key.ipv4_proto = ipv4_proto;
1196             ipv4_key.ipv4_tos = ipv4_tos;
1197             ipv4_key.ipv4_ttl = ipv4_ttl;
1198             ipv4_key.ipv4_frag = ipv4_frag;
1199             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1200                               &ipv4_key, sizeof ipv4_key);
1201             return n;
1202         }
1203     }
1204
1205     {
1206         char ipv6_src_s[IPV6_SCAN_LEN + 1];
1207         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1208         int ipv6_label;
1209         int ipv6_proto;
1210         int ipv6_tclass;
1211         int ipv6_hlimit;
1212         char frag[8];
1213         enum ovs_frag_type ipv6_frag;
1214         int n = -1;
1215
1216         if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1217                    "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1218                    ipv6_src_s, ipv6_dst_s, &ipv6_label,
1219                    &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1220             && n > 0
1221             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1222             struct ovs_key_ipv6 ipv6_key;
1223
1224             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1225                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1226                 return -EINVAL;
1227             }
1228             ipv6_key.ipv6_label = htonl(ipv6_label);
1229             ipv6_key.ipv6_proto = ipv6_proto;
1230             ipv6_key.ipv6_tclass = ipv6_tclass;
1231             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1232             ipv6_key.ipv6_frag = ipv6_frag;
1233             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1234                               &ipv6_key, sizeof ipv6_key);
1235             return n;
1236         }
1237     }
1238
1239     {
1240         int tcp_src;
1241         int tcp_dst;
1242         int n = -1;
1243
1244         if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1245             && n > 0) {
1246             struct ovs_key_tcp tcp_key;
1247
1248             tcp_key.tcp_src = htons(tcp_src);
1249             tcp_key.tcp_dst = htons(tcp_dst);
1250             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1251             return n;
1252         }
1253     }
1254
1255     {
1256         int udp_src;
1257         int udp_dst;
1258         int n = -1;
1259
1260         if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1261             && n > 0) {
1262             struct ovs_key_udp udp_key;
1263
1264             udp_key.udp_src = htons(udp_src);
1265             udp_key.udp_dst = htons(udp_dst);
1266             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1267             return n;
1268         }
1269     }
1270
1271     {
1272         int icmp_type;
1273         int icmp_code;
1274         int n = -1;
1275
1276         if (sscanf(s, "icmp(type=%i,code=%i)%n",
1277                    &icmp_type, &icmp_code, &n) > 0
1278             && n > 0) {
1279             struct ovs_key_icmp icmp_key;
1280
1281             icmp_key.icmp_type = icmp_type;
1282             icmp_key.icmp_code = icmp_code;
1283             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1284                               &icmp_key, sizeof icmp_key);
1285             return n;
1286         }
1287     }
1288
1289     {
1290         struct ovs_key_icmpv6 icmpv6_key;
1291         int n = -1;
1292
1293         if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1294                    &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1295             && n > 0) {
1296             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1297                               &icmpv6_key, sizeof icmpv6_key);
1298             return n;
1299         }
1300     }
1301
1302     {
1303         ovs_be32 arp_sip;
1304         ovs_be32 arp_tip;
1305         int arp_op;
1306         uint8_t arp_sha[ETH_ADDR_LEN];
1307         uint8_t arp_tha[ETH_ADDR_LEN];
1308         int n = -1;
1309
1310         if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1311                    "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1312                    IP_SCAN_ARGS(&arp_sip),
1313                    IP_SCAN_ARGS(&arp_tip),
1314                    &arp_op,
1315                    ETH_ADDR_SCAN_ARGS(arp_sha),
1316                    ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1317             struct ovs_key_arp arp_key;
1318
1319             memset(&arp_key, 0, sizeof arp_key);
1320             arp_key.arp_sip = arp_sip;
1321             arp_key.arp_tip = arp_tip;
1322             arp_key.arp_op = htons(arp_op);
1323             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1324             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1325             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1326             return n;
1327         }
1328     }
1329
1330     {
1331         char nd_target_s[IPV6_SCAN_LEN + 1];
1332         uint8_t nd_sll[ETH_ADDR_LEN];
1333         uint8_t nd_tll[ETH_ADDR_LEN];
1334         int n = -1;
1335
1336         if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1337                    nd_target_s, &n) > 0 && n > 0) {
1338             return put_nd_key(n, nd_target_s, NULL, NULL, key);
1339         }
1340         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1341                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1342             && n > 0) {
1343             return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1344         }
1345         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1346                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1347             && n > 0) {
1348             return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1349         }
1350         if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1351                    "tll="ETH_ADDR_SCAN_FMT")%n",
1352                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1353                    ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1354             && n > 0) {
1355             return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1356         }
1357     }
1358
1359     if (!strncmp(s, "encap(", 6)) {
1360         const char *start = s;
1361         size_t encap;
1362
1363         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1364
1365         s += 6;
1366         for (;;) {
1367             int retval;
1368
1369             s += strspn(s, ", \t\r\n");
1370             if (!*s) {
1371                 return -EINVAL;
1372             } else if (*s == ')') {
1373                 break;
1374             }
1375
1376             retval = parse_odp_key_attr(s, port_names, key);
1377             if (retval < 0) {
1378                 return retval;
1379             }
1380             s += retval;
1381         }
1382         s++;
1383
1384         nl_msg_end_nested(key, encap);
1385
1386         return s - start;
1387     }
1388
1389     return -EINVAL;
1390 }
1391
1392 /* Parses the string representation of a datapath flow key, in the
1393  * format output by odp_flow_key_format().  Returns 0 if successful,
1394  * otherwise a positive errno value.  On success, the flow key is
1395  * appended to 'key' as a series of Netlink attributes.  On failure, no
1396  * data is appended to 'key'.  Either way, 'key''s data might be
1397  * reallocated.
1398  *
1399  * If 'port_names' is nonnull, it points to an simap that maps from a port name
1400  * to a port number.  (Port names may be used instead of port numbers in
1401  * in_port.)
1402  *
1403  * On success, the attributes appended to 'key' are individually syntactically
1404  * valid, but they may not be valid as a sequence.  'key' might, for example,
1405  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
1406 int
1407 odp_flow_key_from_string(const char *s, const struct simap *port_names,
1408                          struct ofpbuf *key)
1409 {
1410     const size_t old_size = key->size;
1411     for (;;) {
1412         int retval;
1413
1414         s += strspn(s, delimiters);
1415         if (!*s) {
1416             return 0;
1417         }
1418
1419         retval = parse_odp_key_attr(s, port_names, key);
1420         if (retval < 0) {
1421             key->size = old_size;
1422             return -retval;
1423         }
1424         s += retval;
1425     }
1426
1427     return 0;
1428 }
1429
1430 static uint8_t
1431 ovs_to_odp_frag(uint8_t nw_frag)
1432 {
1433     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1434           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1435           : OVS_FRAG_TYPE_LATER);
1436 }
1437
1438 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1439  *
1440  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1441  * capable of being expanded to allow for that much space. */
1442 void
1443 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
1444 {
1445     struct ovs_key_ethernet *eth_key;
1446     size_t encap;
1447
1448     if (flow->skb_priority) {
1449         nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1450     }
1451
1452     if (flow->tunnel.tun_id != htonll(0)) {
1453         nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tunnel.tun_id);
1454     }
1455
1456     if (flow->skb_mark) {
1457         nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, flow->skb_mark);
1458     }
1459
1460     if (flow->in_port != OFPP_NONE && flow->in_port != OFPP_CONTROLLER) {
1461         nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
1462                        ofp_port_to_odp_port(flow->in_port));
1463     }
1464
1465     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1466                                        sizeof *eth_key);
1467     memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1468     memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1469
1470     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1471         nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1472         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1473         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1474         if (flow->vlan_tci == htons(0)) {
1475             goto unencap;
1476         }
1477     } else {
1478         encap = 0;
1479     }
1480
1481     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1482         goto unencap;
1483     }
1484
1485     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1486
1487     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1488         struct ovs_key_ipv4 *ipv4_key;
1489
1490         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1491                                             sizeof *ipv4_key);
1492         ipv4_key->ipv4_src = flow->nw_src;
1493         ipv4_key->ipv4_dst = flow->nw_dst;
1494         ipv4_key->ipv4_proto = flow->nw_proto;
1495         ipv4_key->ipv4_tos = flow->nw_tos;
1496         ipv4_key->ipv4_ttl = flow->nw_ttl;
1497         ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1498     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1499         struct ovs_key_ipv6 *ipv6_key;
1500
1501         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1502                                             sizeof *ipv6_key);
1503         memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1504         memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1505         ipv6_key->ipv6_label = flow->ipv6_label;
1506         ipv6_key->ipv6_proto = flow->nw_proto;
1507         ipv6_key->ipv6_tclass = flow->nw_tos;
1508         ipv6_key->ipv6_hlimit = flow->nw_ttl;
1509         ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1510     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1511                flow->dl_type == htons(ETH_TYPE_RARP)) {
1512         struct ovs_key_arp *arp_key;
1513
1514         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1515                                            sizeof *arp_key);
1516         memset(arp_key, 0, sizeof *arp_key);
1517         arp_key->arp_sip = flow->nw_src;
1518         arp_key->arp_tip = flow->nw_dst;
1519         arp_key->arp_op = htons(flow->nw_proto);
1520         memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1521         memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1522     }
1523
1524     if ((flow->dl_type == htons(ETH_TYPE_IP)
1525          || flow->dl_type == htons(ETH_TYPE_IPV6))
1526         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1527
1528         if (flow->nw_proto == IPPROTO_TCP) {
1529             struct ovs_key_tcp *tcp_key;
1530
1531             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1532                                                sizeof *tcp_key);
1533             tcp_key->tcp_src = flow->tp_src;
1534             tcp_key->tcp_dst = flow->tp_dst;
1535         } else if (flow->nw_proto == IPPROTO_UDP) {
1536             struct ovs_key_udp *udp_key;
1537
1538             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1539                                                sizeof *udp_key);
1540             udp_key->udp_src = flow->tp_src;
1541             udp_key->udp_dst = flow->tp_dst;
1542         } else if (flow->dl_type == htons(ETH_TYPE_IP)
1543                 && flow->nw_proto == IPPROTO_ICMP) {
1544             struct ovs_key_icmp *icmp_key;
1545
1546             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1547                                                 sizeof *icmp_key);
1548             icmp_key->icmp_type = ntohs(flow->tp_src);
1549             icmp_key->icmp_code = ntohs(flow->tp_dst);
1550         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1551                 && flow->nw_proto == IPPROTO_ICMPV6) {
1552             struct ovs_key_icmpv6 *icmpv6_key;
1553
1554             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1555                                                   sizeof *icmpv6_key);
1556             icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1557             icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1558
1559             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1560                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1561                 struct ovs_key_nd *nd_key;
1562
1563                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1564                                                     sizeof *nd_key);
1565                 memcpy(nd_key->nd_target, &flow->nd_target,
1566                         sizeof nd_key->nd_target);
1567                 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1568                 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1569             }
1570         }
1571     }
1572
1573 unencap:
1574     if (encap) {
1575         nl_msg_end_nested(buf, encap);
1576     }
1577 }
1578
1579 uint32_t
1580 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1581 {
1582     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1583     return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1584 }
1585
1586 static void
1587 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1588                        uint64_t attrs, int out_of_range_attr,
1589                        const struct nlattr *key, size_t key_len)
1590 {
1591     struct ds s;
1592     int i;
1593
1594     if (VLOG_DROP_DBG(rl)) {
1595         return;
1596     }
1597
1598     ds_init(&s);
1599     for (i = 0; i < 64; i++) {
1600         if (attrs & (UINT64_C(1) << i)) {
1601             ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1602         }
1603     }
1604     if (out_of_range_attr) {
1605         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1606     }
1607
1608     ds_put_cstr(&s, ": ");
1609     odp_flow_key_format(key, key_len, &s);
1610
1611     VLOG_DBG("%s:%s", title, ds_cstr(&s));
1612     ds_destroy(&s);
1613 }
1614
1615 static bool
1616 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1617 {
1618     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1619
1620     if (odp_frag > OVS_FRAG_TYPE_LATER) {
1621         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1622         return false;
1623     }
1624
1625     if (odp_frag != OVS_FRAG_TYPE_NONE) {
1626         flow->nw_frag |= FLOW_NW_FRAG_ANY;
1627         if (odp_frag == OVS_FRAG_TYPE_LATER) {
1628             flow->nw_frag |= FLOW_NW_FRAG_LATER;
1629         }
1630     }
1631     return true;
1632 }
1633
1634 static bool
1635 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1636                    const struct nlattr *attrs[], uint64_t *present_attrsp,
1637                    int *out_of_range_attrp)
1638 {
1639     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1640     const struct nlattr *nla;
1641     uint64_t present_attrs;
1642     size_t left;
1643
1644     present_attrs = 0;
1645     *out_of_range_attrp = 0;
1646     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1647         uint16_t type = nl_attr_type(nla);
1648         size_t len = nl_attr_get_size(nla);
1649         int expected_len = odp_flow_key_attr_len(type);
1650
1651         if (len != expected_len && expected_len >= 0) {
1652             VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1653                         "length %d", ovs_key_attr_to_string(type),
1654                         len, expected_len);
1655             return false;
1656         }
1657
1658         if (type >= CHAR_BIT * sizeof present_attrs) {
1659             *out_of_range_attrp = type;
1660         } else {
1661             if (present_attrs & (UINT64_C(1) << type)) {
1662                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1663                             ovs_key_attr_to_string(type));
1664                 return false;
1665             }
1666
1667             present_attrs |= UINT64_C(1) << type;
1668             attrs[type] = nla;
1669         }
1670     }
1671     if (left) {
1672         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1673         return false;
1674     }
1675
1676     *present_attrsp = present_attrs;
1677     return true;
1678 }
1679
1680 static enum odp_key_fitness
1681 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1682                    uint64_t expected_attrs,
1683                    const struct nlattr *key, size_t key_len)
1684 {
1685     uint64_t missing_attrs;
1686     uint64_t extra_attrs;
1687
1688     missing_attrs = expected_attrs & ~present_attrs;
1689     if (missing_attrs) {
1690         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1691         log_odp_key_attributes(&rl, "expected but not present",
1692                                missing_attrs, 0, key, key_len);
1693         return ODP_FIT_TOO_LITTLE;
1694     }
1695
1696     extra_attrs = present_attrs & ~expected_attrs;
1697     if (extra_attrs || out_of_range_attr) {
1698         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1699         log_odp_key_attributes(&rl, "present but not expected",
1700                                extra_attrs, out_of_range_attr, key, key_len);
1701         return ODP_FIT_TOO_MUCH;
1702     }
1703
1704     return ODP_FIT_PERFECT;
1705 }
1706
1707 static bool
1708 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1709                 uint64_t present_attrs, uint64_t *expected_attrs,
1710                 struct flow *flow)
1711 {
1712     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1713
1714     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1715         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1716         if (ntohs(flow->dl_type) < 1536) {
1717             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1718                         ntohs(flow->dl_type));
1719             return false;
1720         }
1721         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1722     } else {
1723         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1724     }
1725     return true;
1726 }
1727
1728 static enum odp_key_fitness
1729 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1730                 uint64_t present_attrs, int out_of_range_attr,
1731                 uint64_t expected_attrs, struct flow *flow,
1732                 const struct nlattr *key, size_t key_len)
1733 {
1734     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1735
1736     if (flow->dl_type == htons(ETH_TYPE_IP)) {
1737         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1738         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1739             const struct ovs_key_ipv4 *ipv4_key;
1740
1741             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1742             flow->nw_src = ipv4_key->ipv4_src;
1743             flow->nw_dst = ipv4_key->ipv4_dst;
1744             flow->nw_proto = ipv4_key->ipv4_proto;
1745             flow->nw_tos = ipv4_key->ipv4_tos;
1746             flow->nw_ttl = ipv4_key->ipv4_ttl;
1747             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1748                 return ODP_FIT_ERROR;
1749             }
1750         }
1751     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1752         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1753         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1754             const struct ovs_key_ipv6 *ipv6_key;
1755
1756             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1757             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1758             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1759             flow->ipv6_label = ipv6_key->ipv6_label;
1760             flow->nw_proto = ipv6_key->ipv6_proto;
1761             flow->nw_tos = ipv6_key->ipv6_tclass;
1762             flow->nw_ttl = ipv6_key->ipv6_hlimit;
1763             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1764                 return ODP_FIT_ERROR;
1765             }
1766         }
1767     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1768                flow->dl_type == htons(ETH_TYPE_RARP)) {
1769         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1770         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1771             const struct ovs_key_arp *arp_key;
1772
1773             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1774             flow->nw_src = arp_key->arp_sip;
1775             flow->nw_dst = arp_key->arp_tip;
1776             if (arp_key->arp_op & htons(0xff00)) {
1777                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1778                             "key", ntohs(arp_key->arp_op));
1779                 return ODP_FIT_ERROR;
1780             }
1781             flow->nw_proto = ntohs(arp_key->arp_op);
1782             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1783             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1784         }
1785     }
1786
1787     if (flow->nw_proto == IPPROTO_TCP
1788         && (flow->dl_type == htons(ETH_TYPE_IP) ||
1789             flow->dl_type == htons(ETH_TYPE_IPV6))
1790         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1791         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1792         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1793             const struct ovs_key_tcp *tcp_key;
1794
1795             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1796             flow->tp_src = tcp_key->tcp_src;
1797             flow->tp_dst = tcp_key->tcp_dst;
1798         }
1799     } else if (flow->nw_proto == IPPROTO_UDP
1800                && (flow->dl_type == htons(ETH_TYPE_IP) ||
1801                    flow->dl_type == htons(ETH_TYPE_IPV6))
1802                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1803         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1804         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1805             const struct ovs_key_udp *udp_key;
1806
1807             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1808             flow->tp_src = udp_key->udp_src;
1809             flow->tp_dst = udp_key->udp_dst;
1810         }
1811     } else if (flow->nw_proto == IPPROTO_ICMP
1812                && flow->dl_type == htons(ETH_TYPE_IP)
1813                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1814         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1815         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1816             const struct ovs_key_icmp *icmp_key;
1817
1818             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1819             flow->tp_src = htons(icmp_key->icmp_type);
1820             flow->tp_dst = htons(icmp_key->icmp_code);
1821         }
1822     } else if (flow->nw_proto == IPPROTO_ICMPV6
1823                && flow->dl_type == htons(ETH_TYPE_IPV6)
1824                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1825         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1826         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1827             const struct ovs_key_icmpv6 *icmpv6_key;
1828
1829             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1830             flow->tp_src = htons(icmpv6_key->icmpv6_type);
1831             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1832
1833             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1834                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1835                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1836                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1837                     const struct ovs_key_nd *nd_key;
1838
1839                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1840                     memcpy(&flow->nd_target, nd_key->nd_target,
1841                            sizeof flow->nd_target);
1842                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1843                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1844                 }
1845             }
1846         }
1847     }
1848
1849     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1850                               key, key_len);
1851 }
1852
1853 /* Parse 802.1Q header then encapsulated L3 attributes. */
1854 static enum odp_key_fitness
1855 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1856                    uint64_t present_attrs, int out_of_range_attr,
1857                    uint64_t expected_attrs, struct flow *flow,
1858                    const struct nlattr *key, size_t key_len)
1859 {
1860     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1861
1862     const struct nlattr *encap
1863         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1864            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1865     enum odp_key_fitness encap_fitness;
1866     enum odp_key_fitness fitness;
1867     ovs_be16 tci;
1868
1869     /* Calulate fitness of outer attributes. */
1870     expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1871                        (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1872     fitness = check_expectations(present_attrs, out_of_range_attr,
1873                                  expected_attrs, key, key_len);
1874
1875     /* Get the VLAN TCI value. */
1876     if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1877         return ODP_FIT_TOO_LITTLE;
1878     }
1879     tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1880     if (tci == htons(0)) {
1881         /* Corner case for a truncated 802.1Q header. */
1882         if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1883             return ODP_FIT_TOO_MUCH;
1884         }
1885         return fitness;
1886     } else if (!(tci & htons(VLAN_CFI))) {
1887         VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1888                     "but CFI bit is not set", ntohs(tci));
1889         return ODP_FIT_ERROR;
1890     }
1891
1892     /* Set vlan_tci.
1893      * Remove the TPID from dl_type since it's not the real Ethertype.  */
1894     flow->vlan_tci = tci;
1895     flow->dl_type = htons(0);
1896
1897     /* Now parse the encapsulated attributes. */
1898     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1899                             attrs, &present_attrs, &out_of_range_attr)) {
1900         return ODP_FIT_ERROR;
1901     }
1902     expected_attrs = 0;
1903
1904     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1905         return ODP_FIT_ERROR;
1906     }
1907     encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1908                                     expected_attrs, flow, key, key_len);
1909
1910     /* The overall fitness is the worse of the outer and inner attributes. */
1911     return MAX(fitness, encap_fitness);
1912 }
1913
1914 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1915  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
1916  * 'key' fits our expectations for what a flow key should contain.
1917  *
1918  * This function doesn't take the packet itself as an argument because none of
1919  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
1920  * it is always possible to infer which additional attribute(s) should appear
1921  * by looking at the attributes for lower-level protocols, e.g. if the network
1922  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1923  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1924  * must be absent. */
1925 enum odp_key_fitness
1926 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1927                      struct flow *flow)
1928 {
1929     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1930     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1931     uint64_t expected_attrs;
1932     uint64_t present_attrs;
1933     int out_of_range_attr;
1934
1935     memset(flow, 0, sizeof *flow);
1936
1937     /* Parse attributes. */
1938     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1939                             &out_of_range_attr)) {
1940         return ODP_FIT_ERROR;
1941     }
1942     expected_attrs = 0;
1943
1944     /* Metadata. */
1945     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1946         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1947         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1948     }
1949
1950     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
1951         flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
1952         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
1953     }
1954
1955     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1956         flow->tunnel.tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1957         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1958     }
1959
1960     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1961         uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1962         if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1963             VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1964                         in_port);
1965             return ODP_FIT_ERROR;
1966         }
1967         flow->in_port = odp_port_to_ofp_port(in_port);
1968         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1969     } else {
1970         flow->in_port = OFPP_NONE;
1971     }
1972
1973     /* Ethernet header. */
1974     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1975         const struct ovs_key_ethernet *eth_key;
1976
1977         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1978         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1979         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1980     }
1981     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1982
1983     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1984     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1985         return ODP_FIT_ERROR;
1986     }
1987
1988     if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1989         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1990                                   expected_attrs, flow, key, key_len);
1991     }
1992     return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1993                            expected_attrs, flow, key, key_len);
1994 }
1995
1996 /* Returns 'fitness' as a string, for use in debug messages. */
1997 const char *
1998 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1999 {
2000     switch (fitness) {
2001     case ODP_FIT_PERFECT:
2002         return "OK";
2003     case ODP_FIT_TOO_MUCH:
2004         return "too_much";
2005     case ODP_FIT_TOO_LITTLE:
2006         return "too_little";
2007     case ODP_FIT_ERROR:
2008         return "error";
2009     default:
2010         return "<unknown>";
2011     }
2012 }
2013
2014 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2015  * Netlink PID 'pid'.  If 'cookie' is nonnull, adds a userdata attribute whose
2016  * contents contains 'cookie' and returns the offset within 'odp_actions' of
2017  * the start of the cookie.  (If 'cookie' is null, then the return value is not
2018  * meaningful.) */
2019 size_t
2020 odp_put_userspace_action(uint32_t pid, const union user_action_cookie *cookie,
2021                          struct ofpbuf *odp_actions)
2022 {
2023     size_t offset;
2024
2025     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
2026     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
2027     if (cookie) {
2028         nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
2029                           cookie, sizeof *cookie);
2030     }
2031     nl_msg_end_nested(odp_actions, offset);
2032
2033     return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
2034 }
2035 \f
2036 /* The commit_odp_actions() function and its helpers. */
2037
2038 static void
2039 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
2040                   const void *key, size_t key_size)
2041 {
2042     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
2043     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
2044     nl_msg_end_nested(odp_actions, offset);
2045 }
2046
2047 static void
2048 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
2049                          struct ofpbuf *odp_actions)
2050 {
2051     if (base->tunnel.tun_id == flow->tunnel.tun_id) {
2052         return;
2053     }
2054     base->tunnel.tun_id = flow->tunnel.tun_id;
2055
2056     commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
2057                       &base->tunnel.tun_id, sizeof(base->tunnel.tun_id));
2058 }
2059
2060 static void
2061 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
2062                              struct ofpbuf *odp_actions)
2063 {
2064     struct ovs_key_ethernet eth_key;
2065
2066     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
2067         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2068         return;
2069     }
2070
2071     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2072     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2073
2074     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
2075     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
2076
2077     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
2078                       &eth_key, sizeof(eth_key));
2079 }
2080
2081 static void
2082 commit_vlan_action(const struct flow *flow, struct flow *base,
2083                    struct ofpbuf *odp_actions)
2084 {
2085     if (base->vlan_tci == flow->vlan_tci) {
2086         return;
2087     }
2088
2089     if (base->vlan_tci & htons(VLAN_CFI)) {
2090         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2091     }
2092
2093     if (flow->vlan_tci & htons(VLAN_CFI)) {
2094         struct ovs_action_push_vlan vlan;
2095
2096         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2097         vlan.vlan_tci = flow->vlan_tci;
2098         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2099                           &vlan, sizeof vlan);
2100     }
2101     base->vlan_tci = flow->vlan_tci;
2102 }
2103
2104 static void
2105 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2106                      struct ofpbuf *odp_actions)
2107 {
2108     struct ovs_key_ipv4 ipv4_key;
2109
2110     if (base->nw_src == flow->nw_src &&
2111         base->nw_dst == flow->nw_dst &&
2112         base->nw_tos == flow->nw_tos &&
2113         base->nw_ttl == flow->nw_ttl &&
2114         base->nw_frag == flow->nw_frag) {
2115         return;
2116     }
2117
2118     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2119     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2120     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2121     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2122     ipv4_key.ipv4_proto = base->nw_proto;
2123     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2124
2125     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2126                       &ipv4_key, sizeof(ipv4_key));
2127 }
2128
2129 static void
2130 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2131                        struct ofpbuf *odp_actions)
2132 {
2133     struct ovs_key_ipv6 ipv6_key;
2134
2135     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2136         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2137         base->ipv6_label == flow->ipv6_label &&
2138         base->nw_tos == flow->nw_tos &&
2139         base->nw_ttl == flow->nw_ttl &&
2140         base->nw_frag == flow->nw_frag) {
2141         return;
2142     }
2143
2144     base->ipv6_src = flow->ipv6_src;
2145     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2146     base->ipv6_dst = flow->ipv6_dst;
2147     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2148
2149     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2150     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2151     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2152     ipv6_key.ipv6_proto = base->nw_proto;
2153     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2154
2155     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2156                       &ipv6_key, sizeof(ipv6_key));
2157 }
2158
2159 static void
2160 commit_set_nw_action(const struct flow *flow, struct flow *base,
2161                      struct ofpbuf *odp_actions)
2162 {
2163     /* Check if flow really have an IP header. */
2164     if (!flow->nw_proto) {
2165         return;
2166     }
2167
2168     if (base->dl_type == htons(ETH_TYPE_IP)) {
2169         commit_set_ipv4_action(flow, base, odp_actions);
2170     } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2171         commit_set_ipv6_action(flow, base, odp_actions);
2172     }
2173 }
2174
2175 static void
2176 commit_set_port_action(const struct flow *flow, struct flow *base,
2177                        struct ofpbuf *odp_actions)
2178 {
2179     if (!base->tp_src && !base->tp_dst) {
2180         return;
2181     }
2182
2183     if (base->tp_src == flow->tp_src &&
2184         base->tp_dst == flow->tp_dst) {
2185         return;
2186     }
2187
2188     if (flow->nw_proto == IPPROTO_TCP) {
2189         struct ovs_key_tcp port_key;
2190
2191         port_key.tcp_src = base->tp_src = flow->tp_src;
2192         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2193
2194         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2195                           &port_key, sizeof(port_key));
2196
2197     } else if (flow->nw_proto == IPPROTO_UDP) {
2198         struct ovs_key_udp port_key;
2199
2200         port_key.udp_src = base->tp_src = flow->tp_src;
2201         port_key.udp_dst = base->tp_dst = flow->tp_dst;
2202
2203         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2204                           &port_key, sizeof(port_key));
2205     }
2206 }
2207
2208 static void
2209 commit_set_priority_action(const struct flow *flow, struct flow *base,
2210                            struct ofpbuf *odp_actions)
2211 {
2212     if (base->skb_priority == flow->skb_priority) {
2213         return;
2214     }
2215     base->skb_priority = flow->skb_priority;
2216
2217     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2218                       &base->skb_priority, sizeof(base->skb_priority));
2219 }
2220
2221 static void
2222 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2223                            struct ofpbuf *odp_actions)
2224 {
2225     if (base->skb_mark == flow->skb_mark) {
2226         return;
2227     }
2228     base->skb_mark = flow->skb_mark;
2229
2230     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK,
2231                       &base->skb_mark, sizeof(base->skb_mark));
2232 }
2233 /* If any of the flow key data that ODP actions can modify are different in
2234  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2235  * key from 'base' into 'flow', and then changes 'base' the same way. */
2236 void
2237 commit_odp_actions(const struct flow *flow, struct flow *base,
2238                    struct ofpbuf *odp_actions)
2239 {
2240     commit_set_tun_id_action(flow, base, odp_actions);
2241     commit_set_ether_addr_action(flow, base, odp_actions);
2242     commit_vlan_action(flow, base, odp_actions);
2243     commit_set_nw_action(flow, base, odp_actions);
2244     commit_set_port_action(flow, base, odp_actions);
2245     commit_set_priority_action(flow, base, odp_actions);
2246     commit_set_skb_mark_action(flow, base, odp_actions);
2247 }