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