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