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