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