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