odp-util: Always export the priority and skb_mark netlink attributes.
[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_mask_attr(const char *, const struct simap *port_names,
52                               struct ofpbuf *, struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a,
54                                 const struct nlattr *ma, struct ds *ds,
55                                 bool verbose);
56
57 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
58  * 'type':
59  *
60  *   - For an action whose argument has a fixed length, returned that
61  *     nonnegative length in bytes.
62  *
63  *   - For an action with a variable-length argument, returns -2.
64  *
65  *   - For an invalid 'type', returns -1. */
66 static int
67 odp_action_len(uint16_t type)
68 {
69     if (type > OVS_ACTION_ATTR_MAX) {
70         return -1;
71     }
72
73     switch ((enum ovs_action_attr) type) {
74     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
75     case OVS_ACTION_ATTR_USERSPACE: return -2;
76     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
77     case OVS_ACTION_ATTR_POP_VLAN: return 0;
78     case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
79     case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
80     case OVS_ACTION_ATTR_SET: return -2;
81     case OVS_ACTION_ATTR_SAMPLE: return -2;
82
83     case OVS_ACTION_ATTR_UNSPEC:
84     case __OVS_ACTION_ATTR_MAX:
85         return -1;
86     }
87
88     return -1;
89 }
90
91 /* Returns a string form of 'attr'.  The return value is either a statically
92  * allocated constant string or the 'bufsize'-byte buffer 'namebuf'.  'bufsize'
93  * should be at least OVS_KEY_ATTR_BUFSIZE. */
94 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
95 static const char *
96 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
97 {
98     switch (attr) {
99     case OVS_KEY_ATTR_UNSPEC: return "unspec";
100     case OVS_KEY_ATTR_ENCAP: return "encap";
101     case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
102     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
103     case OVS_KEY_ATTR_TUNNEL: return "tunnel";
104     case OVS_KEY_ATTR_IN_PORT: return "in_port";
105     case OVS_KEY_ATTR_ETHERNET: return "eth";
106     case OVS_KEY_ATTR_VLAN: return "vlan";
107     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
108     case OVS_KEY_ATTR_IPV4: return "ipv4";
109     case OVS_KEY_ATTR_IPV6: return "ipv6";
110     case OVS_KEY_ATTR_TCP: return "tcp";
111     case OVS_KEY_ATTR_UDP: return "udp";
112     case OVS_KEY_ATTR_ICMP: return "icmp";
113     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
114     case OVS_KEY_ATTR_ARP: return "arp";
115     case OVS_KEY_ATTR_ND: return "nd";
116     case OVS_KEY_ATTR_MPLS: return "mpls";
117
118     case __OVS_KEY_ATTR_MAX:
119     default:
120         snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
121         return namebuf;
122     }
123 }
124
125 static void
126 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
127 {
128     size_t len = nl_attr_get_size(a);
129
130     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
131     if (len) {
132         const uint8_t *unspec;
133         unsigned int i;
134
135         unspec = nl_attr_get(a);
136         for (i = 0; i < len; i++) {
137             ds_put_char(ds, i ? ' ': '(');
138             ds_put_format(ds, "%02x", unspec[i]);
139         }
140         ds_put_char(ds, ')');
141     }
142 }
143
144 static void
145 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
146 {
147     static const struct nl_policy ovs_sample_policy[] = {
148         { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */
149         { NL_A_U32, 0, 0, false },     /* OVS_SAMPLE_ATTR_PROBABILITY */
150         { NL_A_NESTED, 0, 0, false },  /* OVS_SAMPLE_ATTR_ACTIONS */
151     };
152     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
153     double percentage;
154     const struct nlattr *nla_acts;
155     int len;
156
157     ds_put_cstr(ds, "sample");
158
159     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
160         ds_put_cstr(ds, "(error)");
161         return;
162     }
163
164     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
165                         UINT32_MAX;
166
167     ds_put_format(ds, "(sample=%.1f%%,", percentage);
168
169     ds_put_cstr(ds, "actions(");
170     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
171     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
172     format_odp_actions(ds, nla_acts, len);
173     ds_put_format(ds, "))");
174 }
175
176 static const char *
177 slow_path_reason_to_string(enum slow_path_reason reason)
178 {
179     switch (reason) {
180     case SLOW_CFM:
181         return "cfm";
182     case SLOW_LACP:
183         return "lacp";
184     case SLOW_STP:
185         return "stp";
186     case SLOW_BFD:
187         return "bfd";
188     case SLOW_CONTROLLER:
189         return "controller";
190     case __SLOW_MAX:
191     default:
192         return NULL;
193     }
194 }
195
196 static enum slow_path_reason
197 string_to_slow_path_reason(const char *string)
198 {
199     enum slow_path_reason i;
200
201     for (i = 1; i < __SLOW_MAX; i++) {
202         if (!strcmp(string, slow_path_reason_to_string(i))) {
203             return i;
204         }
205     }
206
207     return 0;
208 }
209
210 static int
211 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
212             uint32_t *res)
213 {
214     uint32_t result = 0;
215     int n = 0;
216
217     if (s[n] != '(') {
218         return -EINVAL;
219     }
220     n++;
221
222     while (s[n] != ')') {
223         unsigned long long int flags;
224         uint32_t bit;
225         int n0;
226
227         if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
228             n += n0 + (s[n + n0] == ',');
229             result |= flags;
230             continue;
231         }
232
233         for (bit = 1; bit; bit <<= 1) {
234             const char *name = bit_to_string(bit);
235             size_t len;
236
237             if (!name) {
238                 continue;
239             }
240
241             len = strlen(name);
242             if (!strncmp(s + n, name, len) &&
243                 (s[n + len] == ',' || s[n + len] == ')')) {
244                 result |= bit;
245                 n += len + (s[n + len] == ',');
246                 break;
247             }
248         }
249
250         if (!bit) {
251             return -EINVAL;
252         }
253     }
254     n++;
255
256     *res = result;
257     return n;
258 }
259
260 static void
261 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
262 {
263     static const struct nl_policy ovs_userspace_policy[] = {
264         { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */
265         { NL_A_U32, 0, 0, false },     /* OVS_USERSPACE_ATTR_PID */
266         { NL_A_UNSPEC, 0, 0, true },   /* OVS_USERSPACE_ATTR_USERDATA */
267     };
268     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
269     const struct nlattr *userdata_attr;
270
271     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
272         ds_put_cstr(ds, "userspace(error)");
273         return;
274     }
275
276     ds_put_format(ds, "userspace(pid=%"PRIu32,
277                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
278
279     userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
280
281     if (userdata_attr) {
282         const uint8_t *userdata = nl_attr_get(userdata_attr);
283         size_t userdata_len = nl_attr_get_size(userdata_attr);
284         bool userdata_unspec = true;
285         union user_action_cookie cookie;
286
287         if (userdata_len >= sizeof cookie.type
288             && userdata_len <= sizeof cookie) {
289
290             memset(&cookie, 0, sizeof cookie);
291             memcpy(&cookie, userdata, userdata_len);
292
293             userdata_unspec = false;
294
295             if (userdata_len == sizeof cookie.sflow
296                 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
297                 ds_put_format(ds, ",sFlow("
298                               "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
299                               vlan_tci_to_vid(cookie.sflow.vlan_tci),
300                               vlan_tci_to_pcp(cookie.sflow.vlan_tci),
301                               cookie.sflow.output);
302             } else if (userdata_len == sizeof cookie.slow_path
303                        && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
304                 const char *reason;
305                 reason = slow_path_reason_to_string(cookie.slow_path.reason);
306                 reason = reason ? reason : "";
307                 ds_put_format(ds, ",slow_path(%s)", reason);
308             } else if (userdata_len == sizeof cookie.flow_sample
309                        && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
310                 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
311                               ",collector_set_id=%"PRIu32
312                               ",obs_domain_id=%"PRIu32
313                               ",obs_point_id=%"PRIu32")",
314                               cookie.flow_sample.probability,
315                               cookie.flow_sample.collector_set_id,
316                               cookie.flow_sample.obs_domain_id,
317                               cookie.flow_sample.obs_point_id);
318             } else if (userdata_len == sizeof cookie.ipfix
319                        && cookie.type == USER_ACTION_COOKIE_IPFIX) {
320                 ds_put_format(ds, ",ipfix");
321             } else {
322                 userdata_unspec = true;
323             }
324         }
325
326         if (userdata_unspec) {
327             size_t i;
328             ds_put_format(ds, ",userdata(");
329             for (i = 0; i < userdata_len; i++) {
330                 ds_put_format(ds, "%02x", userdata[i]);
331             }
332             ds_put_char(ds, ')');
333         }
334     }
335
336     ds_put_char(ds, ')');
337 }
338
339 static void
340 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
341 {
342     ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
343                   vlan_tci_to_vid(vlan_tci),
344                   vlan_tci_to_pcp(vlan_tci));
345     if (!(vlan_tci & htons(VLAN_CFI))) {
346         ds_put_cstr(ds, ",cfi=0");
347     }
348 }
349
350 static void
351 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
352 {
353     ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
354                   mpls_lse_to_label(mpls_lse),
355                   mpls_lse_to_tc(mpls_lse),
356                   mpls_lse_to_ttl(mpls_lse),
357                   mpls_lse_to_bos(mpls_lse));
358 }
359
360 static void
361 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
362             const struct ovs_key_mpls *mpls_mask)
363 {
364     ovs_be32 key = mpls_key->mpls_lse;
365
366     if (mpls_mask == NULL) {
367         format_mpls_lse(ds, key);
368     } else {
369         ovs_be32 mask = mpls_mask->mpls_lse;
370
371         ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
372                   mpls_lse_to_label(key), mpls_lse_to_label(mask),
373                   mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
374                   mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
375                   mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
376     }
377 }
378
379 static void
380 format_odp_action(struct ds *ds, const struct nlattr *a)
381 {
382     int expected_len;
383     enum ovs_action_attr type = nl_attr_type(a);
384     const struct ovs_action_push_vlan *vlan;
385
386     expected_len = odp_action_len(nl_attr_type(a));
387     if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
388         ds_put_format(ds, "bad length %zu, expected %d for: ",
389                       nl_attr_get_size(a), expected_len);
390         format_generic_odp_action(ds, a);
391         return;
392     }
393
394     switch (type) {
395     case OVS_ACTION_ATTR_OUTPUT:
396         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
397         break;
398     case OVS_ACTION_ATTR_USERSPACE:
399         format_odp_userspace_action(ds, a);
400         break;
401     case OVS_ACTION_ATTR_SET:
402         ds_put_cstr(ds, "set(");
403         format_odp_key_attr(nl_attr_get(a), NULL, ds, true);
404         ds_put_cstr(ds, ")");
405         break;
406     case OVS_ACTION_ATTR_PUSH_VLAN:
407         vlan = nl_attr_get(a);
408         ds_put_cstr(ds, "push_vlan(");
409         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
410             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
411         }
412         format_vlan_tci(ds, vlan->vlan_tci);
413         ds_put_char(ds, ')');
414         break;
415     case OVS_ACTION_ATTR_POP_VLAN:
416         ds_put_cstr(ds, "pop_vlan");
417         break;
418     case OVS_ACTION_ATTR_PUSH_MPLS: {
419         const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
420         ds_put_cstr(ds, "push_mpls(");
421         format_mpls_lse(ds, mpls->mpls_lse);
422         ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
423         break;
424     }
425     case OVS_ACTION_ATTR_POP_MPLS: {
426         ovs_be16 ethertype = nl_attr_get_be16(a);
427         ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
428         break;
429     }
430     case OVS_ACTION_ATTR_SAMPLE:
431         format_odp_sample_action(ds, a);
432         break;
433     case OVS_ACTION_ATTR_UNSPEC:
434     case __OVS_ACTION_ATTR_MAX:
435     default:
436         format_generic_odp_action(ds, a);
437         break;
438     }
439 }
440
441 void
442 format_odp_actions(struct ds *ds, const struct nlattr *actions,
443                    size_t actions_len)
444 {
445     if (actions_len) {
446         const struct nlattr *a;
447         unsigned int left;
448
449         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
450             if (a != actions) {
451                 ds_put_char(ds, ',');
452             }
453             format_odp_action(ds, a);
454         }
455         if (left) {
456             int i;
457
458             if (left == actions_len) {
459                 ds_put_cstr(ds, "<empty>");
460             }
461             ds_put_format(ds, ",***%u leftover bytes*** (", left);
462             for (i = 0; i < left; i++) {
463                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
464             }
465             ds_put_char(ds, ')');
466         }
467     } else {
468         ds_put_cstr(ds, "drop");
469     }
470 }
471
472 static int
473 parse_odp_action(const char *s, const struct simap *port_names,
474                  struct ofpbuf *actions)
475 {
476     /* Many of the sscanf calls in this function use oversized destination
477      * fields because some sscanf() implementations truncate the range of %i
478      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
479      * value of 0x7fff.  The other alternatives are to allow only a single
480      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
481      * parsers.
482      *
483      * The tun_id parser has to use an alternative approach because there is no
484      * type larger than 64 bits. */
485
486     {
487         unsigned long long int port;
488         int n = -1;
489
490         if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
491             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
492             return n;
493         }
494     }
495
496     if (port_names) {
497         int len = strcspn(s, delimiters);
498         struct simap_node *node;
499
500         node = simap_find_len(port_names, s, len);
501         if (node) {
502             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
503             return len;
504         }
505     }
506
507     {
508         unsigned long long int pid;
509         unsigned long long int output;
510         unsigned long long int probability;
511         unsigned long long int collector_set_id;
512         unsigned long long int obs_domain_id;
513         unsigned long long int obs_point_id;
514         int vid, pcp;
515         int n = -1;
516
517         if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
518             odp_put_userspace_action(pid, NULL, 0, actions);
519             return n;
520         } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
521                           "pcp=%i,output=%lli))%n",
522                           &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
523             union user_action_cookie cookie;
524             uint16_t tci;
525
526             tci = vid | (pcp << VLAN_PCP_SHIFT);
527             if (tci) {
528                 tci |= VLAN_CFI;
529             }
530
531             cookie.type = USER_ACTION_COOKIE_SFLOW;
532             cookie.sflow.vlan_tci = htons(tci);
533             cookie.sflow.output = output;
534             odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
535                                      actions);
536             return n;
537         } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0
538                    && n > 0) {
539             union user_action_cookie cookie;
540             char reason[32];
541
542             if (s[n] == ')' && s[n + 1] == ')') {
543                 reason[0] = '\0';
544                 n += 2;
545             } else if (sscanf(s + n, "%31[^)]))", reason) > 0) {
546                 n += strlen(reason) + 2;
547             } else {
548                 return -EINVAL;
549             }
550
551             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
552             cookie.slow_path.unused = 0;
553             cookie.slow_path.reason = string_to_slow_path_reason(reason);
554
555             if (reason[0] && !cookie.slow_path.reason) {
556                 return -EINVAL;
557             }
558
559             odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
560                                      actions);
561             return n;
562         } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
563                           "collector_set_id=%lli,obs_domain_id=%lli,"
564                           "obs_point_id=%lli))%n",
565                           &pid, &probability, &collector_set_id,
566                           &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
567             union user_action_cookie cookie;
568
569             cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
570             cookie.flow_sample.probability = probability;
571             cookie.flow_sample.collector_set_id = collector_set_id;
572             cookie.flow_sample.obs_domain_id = obs_domain_id;
573             cookie.flow_sample.obs_point_id = obs_point_id;
574             odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
575                                      actions);
576             return n;
577         } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
578                    && n > 0) {
579             union user_action_cookie cookie;
580
581             cookie.type = USER_ACTION_COOKIE_IPFIX;
582             odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
583                                      actions);
584             return n;
585         } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
586                    && n > 0) {
587             struct ofpbuf buf;
588             char *end;
589
590             ofpbuf_init(&buf, 16);
591             end = ofpbuf_put_hex(&buf, &s[n], NULL);
592             if (end[0] == ')' && end[1] == ')') {
593                 odp_put_userspace_action(pid, buf.data, buf.size, actions);
594                 ofpbuf_uninit(&buf);
595                 return (end + 2) - s;
596             }
597         }
598     }
599
600     if (!strncmp(s, "set(", 4)) {
601         size_t start_ofs;
602         int retval;
603
604         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
605         retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
606         if (retval < 0) {
607             return retval;
608         }
609         if (s[retval + 4] != ')') {
610             return -EINVAL;
611         }
612         nl_msg_end_nested(actions, start_ofs);
613         return retval + 5;
614     }
615
616     {
617         struct ovs_action_push_vlan push;
618         int tpid = ETH_TYPE_VLAN;
619         int vid, pcp;
620         int cfi = 1;
621         int n = -1;
622
623         if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
624              && n > 0)
625             || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
626                        &vid, &pcp, &cfi, &n) > 0 && n > 0)
627             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
628                        &tpid, &vid, &pcp, &n) > 0 && n > 0)
629             || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
630                        &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
631             push.vlan_tpid = htons(tpid);
632             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
633                                   | (pcp << VLAN_PCP_SHIFT)
634                                   | (cfi ? VLAN_CFI : 0));
635             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
636                               &push, sizeof push);
637
638             return n;
639         }
640     }
641
642     if (!strncmp(s, "pop_vlan", 8)) {
643         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
644         return 8;
645     }
646
647     {
648         double percentage;
649         int n = -1;
650
651         if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
652             && percentage >= 0. && percentage <= 100.0
653             && n > 0) {
654             size_t sample_ofs, actions_ofs;
655             double probability;
656
657             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
658             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
659             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
660                            (probability <= 0 ? 0
661                             : probability >= UINT32_MAX ? UINT32_MAX
662                             : probability));
663
664             actions_ofs = nl_msg_start_nested(actions,
665                                               OVS_SAMPLE_ATTR_ACTIONS);
666             for (;;) {
667                 int retval;
668
669                 n += strspn(s + n, delimiters);
670                 if (s[n] == ')') {
671                     break;
672                 }
673
674                 retval = parse_odp_action(s + n, port_names, actions);
675                 if (retval < 0) {
676                     return retval;
677                 }
678                 n += retval;
679             }
680             nl_msg_end_nested(actions, actions_ofs);
681             nl_msg_end_nested(actions, sample_ofs);
682
683             return s[n + 1] == ')' ? n + 2 : -EINVAL;
684         }
685     }
686
687     return -EINVAL;
688 }
689
690 /* Parses the string representation of datapath actions, in the format output
691  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
692  * value.  On success, the ODP actions are appended to 'actions' as a series of
693  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
694  * way, 'actions''s data might be reallocated. */
695 int
696 odp_actions_from_string(const char *s, const struct simap *port_names,
697                         struct ofpbuf *actions)
698 {
699     size_t old_size;
700
701     if (!strcasecmp(s, "drop")) {
702         return 0;
703     }
704
705     old_size = actions->size;
706     for (;;) {
707         int retval;
708
709         s += strspn(s, delimiters);
710         if (!*s) {
711             return 0;
712         }
713
714         retval = parse_odp_action(s, port_names, actions);
715         if (retval < 0 || !strchr(delimiters, s[retval])) {
716             actions->size = old_size;
717             return -retval;
718         }
719         s += retval;
720     }
721
722     return 0;
723 }
724 \f
725 /* Returns the correct length of the payload for a flow key attribute of the
726  * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
727  * is variable length. */
728 static int
729 odp_flow_key_attr_len(uint16_t type)
730 {
731     if (type > OVS_KEY_ATTR_MAX) {
732         return -1;
733     }
734
735     switch ((enum ovs_key_attr) type) {
736     case OVS_KEY_ATTR_ENCAP: return -2;
737     case OVS_KEY_ATTR_PRIORITY: return 4;
738     case OVS_KEY_ATTR_SKB_MARK: return 4;
739     case OVS_KEY_ATTR_TUNNEL: return -2;
740     case OVS_KEY_ATTR_IN_PORT: return 4;
741     case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
742     case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
743     case OVS_KEY_ATTR_ETHERTYPE: return 2;
744     case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
745     case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
746     case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
747     case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
748     case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
749     case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
750     case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
751     case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
752     case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
753
754     case OVS_KEY_ATTR_UNSPEC:
755     case __OVS_KEY_ATTR_MAX:
756         return -1;
757     }
758
759     return -1;
760 }
761
762 static void
763 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
764 {
765     size_t len = nl_attr_get_size(a);
766     if (len) {
767         const uint8_t *unspec;
768         unsigned int i;
769
770         unspec = nl_attr_get(a);
771         for (i = 0; i < len; i++) {
772             if (i) {
773                 ds_put_char(ds, ' ');
774             }
775             ds_put_format(ds, "%02x", unspec[i]);
776         }
777     }
778 }
779
780 static const char *
781 ovs_frag_type_to_string(enum ovs_frag_type type)
782 {
783     switch (type) {
784     case OVS_FRAG_TYPE_NONE:
785         return "no";
786     case OVS_FRAG_TYPE_FIRST:
787         return "first";
788     case OVS_FRAG_TYPE_LATER:
789         return "later";
790     case __OVS_FRAG_TYPE_MAX:
791     default:
792         return "<error>";
793     }
794 }
795
796 static int
797 tunnel_key_attr_len(int type)
798 {
799     switch (type) {
800     case OVS_TUNNEL_KEY_ATTR_ID: return 8;
801     case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
802     case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
803     case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
804     case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
805     case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
806     case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
807     case __OVS_TUNNEL_KEY_ATTR_MAX:
808         return -1;
809     }
810     return -1;
811 }
812
813 enum odp_key_fitness
814 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
815 {
816     unsigned int left;
817     const struct nlattr *a;
818     bool ttl = false;
819     bool unknown = false;
820
821     NL_NESTED_FOR_EACH(a, left, attr) {
822         uint16_t type = nl_attr_type(a);
823         size_t len = nl_attr_get_size(a);
824         int expected_len = tunnel_key_attr_len(type);
825
826         if (len != expected_len && expected_len >= 0) {
827             return ODP_FIT_ERROR;
828         }
829
830         switch (type) {
831         case OVS_TUNNEL_KEY_ATTR_ID:
832             tun->tun_id = nl_attr_get_be64(a);
833             tun->flags |= FLOW_TNL_F_KEY;
834             break;
835         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
836             tun->ip_src = nl_attr_get_be32(a);
837             break;
838         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
839             tun->ip_dst = nl_attr_get_be32(a);
840             break;
841         case OVS_TUNNEL_KEY_ATTR_TOS:
842             tun->ip_tos = nl_attr_get_u8(a);
843             break;
844         case OVS_TUNNEL_KEY_ATTR_TTL:
845             tun->ip_ttl = nl_attr_get_u8(a);
846             ttl = true;
847             break;
848         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
849             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
850             break;
851         case OVS_TUNNEL_KEY_ATTR_CSUM:
852             tun->flags |= FLOW_TNL_F_CSUM;
853             break;
854         default:
855             /* Allow this to show up as unexpected, if there are unknown
856              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
857             unknown = true;
858             break;
859         }
860     }
861
862     if (!ttl) {
863         return ODP_FIT_ERROR;
864     }
865     if (unknown) {
866             return ODP_FIT_TOO_MUCH;
867     }
868     return ODP_FIT_PERFECT;
869 }
870
871 static void
872 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
873 {
874     size_t tun_key_ofs;
875
876     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
877
878     if (tun_key->flags & FLOW_TNL_F_KEY) {
879         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
880     }
881     if (tun_key->ip_src) {
882         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
883     }
884     if (tun_key->ip_dst) {
885         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
886     }
887     if (tun_key->ip_tos) {
888         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
889     }
890     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
891     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
892         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
893     }
894     if (tun_key->flags & FLOW_TNL_F_CSUM) {
895         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
896     }
897
898     nl_msg_end_nested(a, tun_key_ofs);
899 }
900
901 static bool
902 odp_mask_attr_is_wildcard(const struct nlattr *ma)
903 {
904     return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
905 }
906
907 static bool
908 odp_mask_attr_is_exact(const struct nlattr *ma)
909 {
910     bool is_exact = false;
911     enum ovs_key_attr attr = nl_attr_type(ma);
912
913     if (attr == OVS_KEY_ATTR_TUNNEL) {
914         /* XXX this is a hack for now. Should change
915          * the exact match dection to per field
916          * instead of per attribute.
917          */
918         struct flow_tnl tun_mask;
919         memset(&tun_mask, 0, sizeof tun_mask);
920         odp_tun_key_from_attr(ma, &tun_mask);
921         if (tun_mask.flags == (FLOW_TNL_F_KEY
922                                | FLOW_TNL_F_DONT_FRAGMENT
923                                | FLOW_TNL_F_CSUM)) {
924             /* The flags are exact match, check the remaining fields. */
925             tun_mask.flags = 0xffff;
926             is_exact = is_all_ones((uint8_t *)&tun_mask,
927                                    offsetof(struct flow_tnl, ip_ttl));
928         }
929     } else {
930         is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
931     }
932
933     return is_exact;
934 }
935
936
937 static void
938 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
939                     struct ds *ds, bool verbose)
940 {
941     struct flow_tnl tun_key;
942     enum ovs_key_attr attr = nl_attr_type(a);
943     char namebuf[OVS_KEY_ATTR_BUFSIZE];
944     int expected_len;
945     bool is_exact;
946
947     is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
948
949     ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
950
951     {
952         expected_len = odp_flow_key_attr_len(nl_attr_type(a));
953         if (expected_len != -2) {
954             bool bad_key_len = nl_attr_get_size(a) != expected_len;
955             bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
956
957             if (bad_key_len || bad_mask_len) {
958                 if (bad_key_len) {
959                     ds_put_format(ds, "(bad key length %zu, expected %d)(",
960                                   nl_attr_get_size(a),
961                                   odp_flow_key_attr_len(nl_attr_type(a)));
962                 }
963                 format_generic_odp_key(a, ds);
964                 if (bad_mask_len) {
965                     ds_put_char(ds, '/');
966                     ds_put_format(ds, "(bad mask length %zu, expected %d)(",
967                                   nl_attr_get_size(ma),
968                                   odp_flow_key_attr_len(nl_attr_type(ma)));
969                 }
970                 format_generic_odp_key(ma, ds);
971                 ds_put_char(ds, ')');
972                 return;
973             }
974         }
975     }
976
977     ds_put_char(ds, '(');
978     switch (attr) {
979     case OVS_KEY_ATTR_ENCAP:
980         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
981             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
982                             nl_attr_get(ma), nl_attr_get_size(ma), ds, verbose);
983         } else if (nl_attr_get_size(a)) {
984             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds,
985                             verbose);
986         }
987         break;
988
989     case OVS_KEY_ATTR_PRIORITY:
990     case OVS_KEY_ATTR_SKB_MARK:
991         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
992         if (!is_exact) {
993             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
994         }
995         break;
996
997     case OVS_KEY_ATTR_TUNNEL:
998         memset(&tun_key, 0, sizeof tun_key);
999         if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1000             ds_put_format(ds, "error");
1001         } else if (!is_exact) {
1002             struct flow_tnl tun_mask;
1003
1004             memset(&tun_mask, 0, sizeof tun_mask);
1005             odp_tun_key_from_attr(ma, &tun_mask);
1006             ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1007                           ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1008                           ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1009                           ",flags(",
1010                           ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1011                           IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1012                           IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1013                           tun_key.ip_tos, tun_mask.ip_tos,
1014                           tun_key.ip_ttl, tun_mask.ip_ttl);
1015
1016             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1017
1018             /* XXX This code is correct, but enabling it would break the unit
1019                test. Disable it for now until the input parser is fixed.
1020
1021                 ds_put_char(ds, '/');
1022                 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1023             */
1024             ds_put_char(ds, ')');
1025         } else {
1026             ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1027                           "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1028                           ntohll(tun_key.tun_id),
1029                           IP_ARGS(tun_key.ip_src),
1030                           IP_ARGS(tun_key.ip_dst),
1031                           tun_key.ip_tos, tun_key.ip_ttl);
1032
1033             format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1034             ds_put_char(ds, ')');
1035         }
1036         break;
1037
1038     case OVS_KEY_ATTR_IN_PORT:
1039         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1040         if (!is_exact) {
1041             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1042         }
1043         break;
1044
1045     case OVS_KEY_ATTR_ETHERNET:
1046         if (!is_exact) {
1047             const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1048             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1049
1050             ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1051                           ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1052                           ETH_ADDR_ARGS(eth_key->eth_src),
1053                           ETH_ADDR_ARGS(eth_mask->eth_src),
1054                           ETH_ADDR_ARGS(eth_key->eth_dst),
1055                           ETH_ADDR_ARGS(eth_mask->eth_dst));
1056         } else {
1057             const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1058
1059             ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1060                           ETH_ADDR_ARGS(eth_key->eth_src),
1061                           ETH_ADDR_ARGS(eth_key->eth_dst));
1062         }
1063         break;
1064
1065     case OVS_KEY_ATTR_VLAN:
1066         {
1067             ovs_be16 vlan_tci = nl_attr_get_be16(a);
1068             if (!is_exact) {
1069                 ovs_be16 mask = nl_attr_get_be16(ma);
1070                 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1071                               vlan_tci_to_vid(vlan_tci),
1072                               vlan_tci_to_vid(mask),
1073                               vlan_tci_to_pcp(vlan_tci),
1074                               vlan_tci_to_pcp(mask),
1075                               vlan_tci_to_cfi(vlan_tci),
1076                               vlan_tci_to_cfi(mask));
1077             } else {
1078                 format_vlan_tci(ds, vlan_tci);
1079             }
1080         }
1081         break;
1082
1083     case OVS_KEY_ATTR_MPLS: {
1084         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1085         const struct ovs_key_mpls *mpls_mask = NULL;
1086         if (!is_exact) {
1087             mpls_mask = nl_attr_get(ma);
1088         }
1089         format_mpls(ds, mpls_key, mpls_mask);
1090         break;
1091     }
1092
1093     case OVS_KEY_ATTR_ETHERTYPE:
1094         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1095         if (!is_exact) {
1096             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1097         }
1098         break;
1099
1100     case OVS_KEY_ATTR_IPV4:
1101         if (!is_exact) {
1102             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1103             const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1104
1105             ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1106                           ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1107                           ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1108                           IP_ARGS(ipv4_key->ipv4_src),
1109                           IP_ARGS(ipv4_mask->ipv4_src),
1110                           IP_ARGS(ipv4_key->ipv4_dst),
1111                           IP_ARGS(ipv4_mask->ipv4_dst),
1112                           ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1113                           ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1114                           ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1115                           ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1116                           ipv4_mask->ipv4_frag);
1117         } else {
1118             const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1119
1120             ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1121                           ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1122                           IP_ARGS(ipv4_key->ipv4_src),
1123                           IP_ARGS(ipv4_key->ipv4_dst),
1124                           ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1125                           ipv4_key->ipv4_ttl,
1126                           ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1127         }
1128         break;
1129
1130     case OVS_KEY_ATTR_IPV6:
1131         if (!is_exact) {
1132             const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1133             char src_str[INET6_ADDRSTRLEN];
1134             char dst_str[INET6_ADDRSTRLEN];
1135             char src_mask[INET6_ADDRSTRLEN];
1136             char dst_mask[INET6_ADDRSTRLEN];
1137
1138             ipv6_key = nl_attr_get(a);
1139             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1140             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1141
1142             ipv6_mask = nl_attr_get(ma);
1143             inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1144             inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1145
1146             ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1147                           ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1148                           ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1149                           src_str, src_mask, dst_str, dst_mask,
1150                           ntohl(ipv6_key->ipv6_label),
1151                           ntohl(ipv6_mask->ipv6_label),
1152                           ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1153                           ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1154                           ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1155                           ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1156                           ipv6_mask->ipv6_frag);
1157         } else {
1158             const struct ovs_key_ipv6 *ipv6_key;
1159             char src_str[INET6_ADDRSTRLEN];
1160             char dst_str[INET6_ADDRSTRLEN];
1161
1162             ipv6_key = nl_attr_get(a);
1163             inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1164             inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1165
1166             ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1167                           ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1168                           src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1169                           ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1170                           ipv6_key->ipv6_hlimit,
1171                           ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1172         }
1173         break;
1174
1175     case OVS_KEY_ATTR_TCP:
1176         if (!is_exact) {
1177             const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1178             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1179
1180             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1181                           ",dst=%"PRIu16"/%#"PRIx16,
1182                           ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1183                           ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1184         } else {
1185             const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1186
1187             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1188                           ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1189         }
1190         break;
1191
1192     case OVS_KEY_ATTR_UDP:
1193         if (!is_exact) {
1194             const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1195             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1196
1197             ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1198                           ",dst=%"PRIu16"/%#"PRIx16,
1199                           ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1200                           ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1201         } else {
1202             const struct ovs_key_udp *udp_key = nl_attr_get(a);
1203
1204             ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1205                           ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1206         }
1207         break;
1208
1209     case OVS_KEY_ATTR_ICMP:
1210         if (!is_exact) {
1211             const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1212             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1213
1214             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1215                           icmp_key->icmp_type, icmp_mask->icmp_type,
1216                           icmp_key->icmp_code, icmp_mask->icmp_code);
1217         } else {
1218             const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1219
1220             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1221                           icmp_key->icmp_type, icmp_key->icmp_code);
1222         }
1223         break;
1224
1225     case OVS_KEY_ATTR_ICMPV6:
1226         if (!is_exact) {
1227             const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1228             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1229
1230             ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1231                           icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1232                           icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1233         } else {
1234             const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1235
1236             ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1237                           icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1238         }
1239         break;
1240
1241     case OVS_KEY_ATTR_ARP:
1242         if (!is_exact) {
1243             const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1244             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1245
1246             ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1247                           ",op=%"PRIu16"/%#"PRIx16
1248                           ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1249                           ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1250                           IP_ARGS(arp_key->arp_sip),
1251                           IP_ARGS(arp_mask->arp_sip),
1252                           IP_ARGS(arp_key->arp_tip),
1253                           IP_ARGS(arp_mask->arp_tip),
1254                           ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1255                           ETH_ADDR_ARGS(arp_key->arp_sha),
1256                           ETH_ADDR_ARGS(arp_mask->arp_sha),
1257                           ETH_ADDR_ARGS(arp_key->arp_tha),
1258                           ETH_ADDR_ARGS(arp_mask->arp_tha));
1259         } else {
1260             const struct ovs_key_arp *arp_key = nl_attr_get(a);
1261
1262             ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1263                           "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1264                           IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1265                           ntohs(arp_key->arp_op),
1266                           ETH_ADDR_ARGS(arp_key->arp_sha),
1267                           ETH_ADDR_ARGS(arp_key->arp_tha));
1268         }
1269         break;
1270
1271     case OVS_KEY_ATTR_ND: {
1272         const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1273         char target[INET6_ADDRSTRLEN];
1274
1275         nd_key = nl_attr_get(a);
1276         if (!is_exact) {
1277             nd_mask = nl_attr_get(ma);
1278         }
1279
1280         inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1281         ds_put_format(ds, "target=%s", target);
1282         if (!is_exact) {
1283             inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1284             ds_put_format(ds, "/%s", target);
1285         }
1286
1287         if (!eth_addr_is_zero(nd_key->nd_sll)) {
1288             ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1289                           ETH_ADDR_ARGS(nd_key->nd_sll));
1290             if (!is_exact) {
1291                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1292                               ETH_ADDR_ARGS(nd_mask->nd_sll));
1293             }
1294         }
1295         if (!eth_addr_is_zero(nd_key->nd_tll)) {
1296             ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1297                           ETH_ADDR_ARGS(nd_key->nd_tll));
1298             if (!is_exact) {
1299                 ds_put_format(ds, "/"ETH_ADDR_FMT,
1300                               ETH_ADDR_ARGS(nd_mask->nd_tll));
1301             }
1302         }
1303         break;
1304     }
1305
1306     case OVS_KEY_ATTR_UNSPEC:
1307     case __OVS_KEY_ATTR_MAX:
1308     default:
1309         format_generic_odp_key(a, ds);
1310         if (!is_exact) {
1311             ds_put_char(ds, '/');
1312             format_generic_odp_key(ma, ds);
1313         }
1314         break;
1315     }
1316     ds_put_char(ds, ')');
1317 }
1318
1319 static struct nlattr *
1320 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1321 {
1322     const struct nlattr *a;
1323     unsigned int left;
1324     int type = nl_attr_type(key);
1325     int size = nl_attr_get_size(key);
1326
1327     if (odp_flow_key_attr_len(type) >=0) {
1328         memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size);
1329     } else {
1330         size_t nested_mask;
1331
1332         nested_mask = nl_msg_start_nested(ofp, type);
1333         NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1334             generate_all_wildcard_mask(ofp, nl_attr_get(a));
1335         }
1336         nl_msg_end_nested(ofp, nested_mask);
1337     }
1338
1339     return ofp->base;
1340 }
1341
1342 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1343  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1344  * 'mask_len' bytes of 'mask' which apply to 'key'. */
1345 void
1346 odp_flow_format(const struct nlattr *key, size_t key_len,
1347                 const struct nlattr *mask, size_t mask_len,
1348                 struct ds *ds, bool verbose)
1349 {
1350     if (key_len) {
1351         const struct nlattr *a;
1352         unsigned int left;
1353         bool has_ethtype_key = false;
1354         const struct nlattr *ma = NULL;
1355         struct ofpbuf ofp;
1356         bool first_field = true;
1357
1358         ofpbuf_init(&ofp, 100);
1359         NL_ATTR_FOR_EACH (a, left, key, key_len) {
1360             bool is_nested_attr;
1361             bool is_wildcard = false;
1362             int attr_type = nl_attr_type(a);
1363
1364             if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1365                 has_ethtype_key = true;
1366             }
1367
1368             is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1369
1370             if (mask && mask_len) {
1371                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1372                 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1373             }
1374
1375             if (verbose || !is_wildcard  || is_nested_attr) {
1376                 if (is_wildcard && !ma) {
1377                     ma = generate_all_wildcard_mask(&ofp, a);
1378                 }
1379                 if (!first_field) {
1380                     ds_put_char(ds, ',');
1381                 }
1382                 format_odp_key_attr(a, ma, ds, verbose);
1383                 first_field = false;
1384             }
1385             ofpbuf_clear(&ofp);
1386         }
1387         ofpbuf_uninit(&ofp);
1388
1389         if (left) {
1390             int i;
1391             
1392             if (left == key_len) {
1393                 ds_put_cstr(ds, "<empty>");
1394             }
1395             ds_put_format(ds, ",***%u leftover bytes*** (", left);
1396             for (i = 0; i < left; i++) {
1397                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1398             }
1399             ds_put_char(ds, ')');
1400         }
1401         if (!has_ethtype_key) {
1402             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1403             if (ma) {
1404                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1405                               ntohs(nl_attr_get_be16(ma)));
1406             }
1407         }
1408     } else {
1409         ds_put_cstr(ds, "<empty>");
1410     }
1411 }
1412
1413 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1414  * OVS_KEY_ATTR_* attributes in 'key'. */
1415 void
1416 odp_flow_key_format(const struct nlattr *key,
1417                     size_t key_len, struct ds *ds)
1418 {
1419     odp_flow_format(key, key_len, NULL, 0, ds, true);
1420 }
1421
1422 static void
1423 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1424        const uint8_t *nd_tll, struct ofpbuf *key)
1425 {
1426     if (nd_sll) {
1427         memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1428     }
1429
1430     if (nd_tll) {
1431         memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1432     }
1433
1434     nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1435 }
1436
1437 static int
1438 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1439            const uint8_t *nd_tll, struct ofpbuf *key)
1440 {
1441     struct ovs_key_nd nd_key;
1442
1443     memset(&nd_key, 0, sizeof nd_key);
1444
1445     if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1446         return -EINVAL;
1447     }
1448
1449     put_nd(&nd_key, nd_sll, nd_tll, key);
1450     return n;
1451 }
1452
1453 static int
1454 put_nd_mask(int n, const char *nd_target_s,
1455            const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1456 {
1457     struct ovs_key_nd nd_mask;
1458
1459     memset(&nd_mask, 0xff, sizeof nd_mask);
1460
1461     if (strlen(nd_target_s) != 0 &&
1462             inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1463         return -EINVAL;
1464     }
1465
1466     put_nd(&nd_mask, nd_sll, nd_tll, mask);
1467     return n;
1468 }
1469
1470 static bool
1471 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1472 {
1473     if (!strcasecmp(s, "no")) {
1474         *type = OVS_FRAG_TYPE_NONE;
1475     } else if (!strcasecmp(s, "first")) {
1476         *type = OVS_FRAG_TYPE_FIRST;
1477     } else if (!strcasecmp(s, "later")) {
1478         *type = OVS_FRAG_TYPE_LATER;
1479     } else {
1480         return false;
1481     }
1482     return true;
1483 }
1484
1485 static ovs_be32
1486 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1487 {
1488     return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1489                   (mpls_tc << MPLS_TC_SHIFT)       |
1490                   (mpls_ttl << MPLS_TTL_SHIFT)     |
1491                   (mpls_bos << MPLS_BOS_SHIFT)));
1492 }
1493
1494 static int
1495 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1496                         struct ofpbuf *key, struct ofpbuf *mask)
1497 {
1498     /* Many of the sscanf calls in this function use oversized destination
1499      * fields because some sscanf() implementations truncate the range of %i
1500      * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1501      * value of 0x7fff.  The other alternatives are to allow only a single
1502      * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1503      * parsers.
1504      *
1505      * The tun_id parser has to use an alternative approach because there is no
1506      * type larger than 64 bits. */
1507
1508     {
1509         unsigned long long int priority;
1510         unsigned long long int priority_mask;
1511         int n = -1;
1512
1513         if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1514                    &priority_mask, &n) > 0 && n > 0) {
1515             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1516             nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1517             return n;
1518         } else if (sscanf(s, "skb_priority(%lli)%n",
1519                           &priority, &n) > 0 && n > 0) {
1520             nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1521             if (mask) {
1522                 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1523             }
1524             return n;
1525         }
1526     }
1527
1528     {
1529         unsigned long long int mark;
1530         unsigned long long int mark_mask;
1531         int n = -1;
1532
1533         if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1534                    &mark_mask, &n) > 0 && n > 0) {
1535             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1536             nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1537             return n;
1538         } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1539             nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1540             if (mask) {
1541                 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1542             }
1543             return n;
1544         }
1545     }
1546
1547     {
1548         char tun_id_s[32];
1549         int tos, tos_mask, ttl, ttl_mask;
1550         struct flow_tnl tun_key, tun_key_mask;
1551         unsigned long long tun_id_mask;
1552         int n = -1;
1553
1554         if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1555                    "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1556                    "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1557                    tun_id_s, &tun_id_mask,
1558                    IP_SCAN_ARGS(&tun_key.ip_src),
1559                    IP_SCAN_ARGS(&tun_key_mask.ip_src),
1560                    IP_SCAN_ARGS(&tun_key.ip_dst),
1561                    IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1562                    &tos, &tos_mask, &ttl, &ttl_mask,
1563                    &n) > 0 && n > 0) {
1564             int res;
1565             uint32_t flags;
1566
1567             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1568             tun_key_mask.tun_id = htonll(tun_id_mask);
1569             tun_key.ip_tos = tos;
1570             tun_key_mask.ip_tos = tos_mask;
1571             tun_key.ip_ttl = ttl;
1572             tun_key_mask.ip_ttl = ttl_mask;
1573             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1574             tun_key.flags = flags;
1575             tun_key_mask.flags = UINT16_MAX;
1576
1577             if (res < 0) {
1578                 return res;
1579             }
1580             n += res;
1581             if (s[n] != ')') {
1582                 return -EINVAL;
1583             }
1584             n++;
1585             tun_key_to_attr(key, &tun_key);
1586             if (mask) {
1587                 tun_key_to_attr(mask, &tun_key_mask);
1588             }
1589             return n;
1590         } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1591                    "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1592                    ",tos=%i,ttl=%i,flags%n", tun_id_s,
1593                     IP_SCAN_ARGS(&tun_key.ip_src),
1594                     IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1595                     &n) > 0 && n > 0) {
1596             int res;
1597             uint32_t flags;
1598
1599             tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1600             tun_key.ip_tos = tos;
1601             tun_key.ip_ttl = ttl;
1602             res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1603             tun_key.flags = flags;
1604
1605             if (res < 0) {
1606                 return res;
1607             }
1608             n += res;
1609             if (s[n] != ')') {
1610                 return -EINVAL;
1611             }
1612             n++;
1613             tun_key_to_attr(key, &tun_key);
1614
1615             if (mask) {
1616                 memset(&tun_key, 0xff, sizeof tun_key);
1617                 tun_key_to_attr(mask, &tun_key);
1618             }
1619             return n;
1620         }
1621     }
1622
1623     {
1624         unsigned long long int in_port;
1625         unsigned long long int in_port_mask;
1626         int n = -1;
1627
1628         if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1629                    &in_port_mask, &n) > 0 && n > 0) {
1630             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1631             nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1632             return n;
1633         } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1634             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1635             if (mask) {
1636                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1637             }
1638             return n;
1639         }
1640     }
1641
1642
1643     if (port_names && !strncmp(s, "in_port(", 8)) {
1644         const char *name;
1645         const struct simap_node *node;
1646         int name_len;
1647
1648         name = s + 8;
1649         name_len = strcspn(s, ")");
1650         node = simap_find_len(port_names, name, name_len);
1651         if (node) {
1652             nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1653
1654             if (mask) {
1655                 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1656             }
1657             return 8 + name_len + 1;
1658         }
1659     }
1660
1661     {
1662         struct ovs_key_ethernet eth_key;
1663         struct ovs_key_ethernet eth_key_mask;
1664         int n = -1;
1665
1666         if (mask && sscanf(s,
1667                    "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1668                         "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1669                 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1670                 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1671                 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1672                 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1673
1674             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1675                               &eth_key, sizeof eth_key);
1676             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1677                               &eth_key_mask, sizeof eth_key_mask);
1678             return n;
1679         } else if (sscanf(s,
1680                    "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1681                    ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1682                    ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1683             nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1684                               &eth_key, sizeof eth_key);
1685
1686             if (mask) {
1687                 memset(&eth_key, 0xff, sizeof eth_key);
1688                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1689                               &eth_key, sizeof eth_key);
1690             }
1691             return n;
1692         }
1693     }
1694
1695     {
1696         uint16_t vid, vid_mask;
1697         int pcp, pcp_mask;
1698         int cfi, cfi_mask;
1699         int n = -1;
1700
1701         if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1702                             &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1703             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1704                             htons((vid << VLAN_VID_SHIFT) |
1705                                   (pcp << VLAN_PCP_SHIFT) |
1706                                   VLAN_CFI));
1707             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1708                             htons((vid_mask << VLAN_VID_SHIFT) |
1709                                   (pcp_mask << VLAN_PCP_SHIFT) |
1710                                   (1 << VLAN_CFI_SHIFT)));
1711             return n;
1712         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1713                            &vid, &pcp, &n) > 0 && n > 0)) {
1714             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1715                             htons((vid << VLAN_VID_SHIFT) |
1716                                   (pcp << VLAN_PCP_SHIFT) |
1717                                   VLAN_CFI));
1718             if (mask) {
1719                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1720             }
1721             return n;
1722         } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1723                                    &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1724             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1725                             htons((vid << VLAN_VID_SHIFT) |
1726                                   (pcp << VLAN_PCP_SHIFT) |
1727                                   (cfi ? VLAN_CFI : 0)));
1728             nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1729                             htons((vid_mask << VLAN_VID_SHIFT) |
1730                                   (pcp_mask << VLAN_PCP_SHIFT) |
1731                                   (cfi_mask << VLAN_CFI_SHIFT)));
1732             return n;
1733         } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1734                            &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1735             nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1736                             htons((vid << VLAN_VID_SHIFT) |
1737                                   (pcp << VLAN_PCP_SHIFT) |
1738                                   (cfi ? VLAN_CFI : 0)));
1739             if (mask) {
1740                 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1741             }
1742             return n;
1743         }
1744     }
1745
1746     {
1747         int eth_type;
1748         int eth_type_mask;
1749         int n = -1;
1750
1751         if (mask && sscanf(s, "eth_type(%i/%i)%n",
1752                    &eth_type, &eth_type_mask, &n) > 0 && n > 0) {
1753             if (eth_type != 0) {
1754                 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1755             }
1756             nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1757             return n;
1758         } else if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1759             nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1760             if (mask) {
1761                 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE,
1762                                 htons(UINT16_MAX));
1763             }
1764             return n;
1765         }
1766     }
1767
1768     {
1769         int label, tc, ttl, bos;
1770         int label_mask, tc_mask, ttl_mask, bos_mask;
1771         int n = -1;
1772
1773         if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1774                     &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1775             struct ovs_key_mpls *mpls, *mpls_mask;
1776
1777             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1778                                             sizeof *mpls);
1779             mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1780
1781             mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1782                                             sizeof *mpls_mask);
1783             mpls_mask->mpls_lse = mpls_lse_from_components(
1784                                   label_mask, tc_mask, ttl_mask, bos_mask);
1785             return n;
1786         } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1787                     &label, &tc, &ttl, &bos, &n) > 0 &&
1788                     n > 0) {
1789             struct ovs_key_mpls *mpls;
1790
1791             mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1792                                             sizeof *mpls);
1793             mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1794             if (mask) {
1795                 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1796                                             sizeof *mpls);
1797                 mpls->mpls_lse = htonl(UINT32_MAX);
1798             }
1799             return n;
1800         }
1801     }
1802
1803
1804     {
1805         ovs_be32 ipv4_src, ipv4_src_mask;
1806         ovs_be32 ipv4_dst, ipv4_dst_mask;
1807         int ipv4_proto, ipv4_proto_mask;
1808         int ipv4_tos, ipv4_tos_mask;
1809         int ipv4_ttl, ipv4_ttl_mask;
1810         char frag[8];
1811         int  ipv4_frag_mask;
1812         enum ovs_frag_type ipv4_frag;
1813         int n = -1;
1814
1815         if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1816                       "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1817                       "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1818                       "frag=%7[a-z]/%i)%n",
1819                       IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1820                       IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1821                       &ipv4_proto, &ipv4_proto_mask,
1822                       &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1823                       frag, &ipv4_frag_mask, &n) > 0
1824             && n > 0
1825             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1826             struct ovs_key_ipv4 ipv4_key;
1827             struct ovs_key_ipv4 ipv4_mask;
1828
1829             ipv4_key.ipv4_src = ipv4_src;
1830             ipv4_key.ipv4_dst = ipv4_dst;
1831             ipv4_key.ipv4_proto = ipv4_proto;
1832             ipv4_key.ipv4_tos = ipv4_tos;
1833             ipv4_key.ipv4_ttl = ipv4_ttl;
1834             ipv4_key.ipv4_frag = ipv4_frag;
1835             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1836                               &ipv4_key, sizeof ipv4_key);
1837
1838             ipv4_mask.ipv4_src = ipv4_src_mask;
1839             ipv4_mask.ipv4_dst = ipv4_dst_mask;
1840             ipv4_mask.ipv4_proto = ipv4_proto_mask;
1841             ipv4_mask.ipv4_tos = ipv4_tos_mask;
1842             ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1843             ipv4_mask.ipv4_frag = ipv4_frag_mask;
1844             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1845                               &ipv4_mask, sizeof ipv4_mask);
1846             return n;
1847         } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1848                    "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1849                    IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1850                    &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1851             && n > 0
1852             && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1853             struct ovs_key_ipv4 ipv4_key;
1854
1855             ipv4_key.ipv4_src = ipv4_src;
1856             ipv4_key.ipv4_dst = ipv4_dst;
1857             ipv4_key.ipv4_proto = ipv4_proto;
1858             ipv4_key.ipv4_tos = ipv4_tos;
1859             ipv4_key.ipv4_ttl = ipv4_ttl;
1860             ipv4_key.ipv4_frag = ipv4_frag;
1861             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1862                               &ipv4_key, sizeof ipv4_key);
1863
1864             if (mask) {
1865                 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1866                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1867                               &ipv4_key, sizeof ipv4_key);
1868             }
1869             return n;
1870         }
1871     }
1872
1873     {
1874         char ipv6_src_s[IPV6_SCAN_LEN + 1];
1875         char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1876         char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1877         char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1878         int ipv6_label, ipv6_label_mask;
1879         int ipv6_proto, ipv6_proto_mask;
1880         int ipv6_tclass, ipv6_tclass_mask;
1881         int ipv6_hlimit, ipv6_hlimit_mask;
1882         char frag[8];
1883         enum ovs_frag_type ipv6_frag;
1884         int ipv6_frag_mask;
1885         int n = -1;
1886
1887         if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1888                    IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1889                    "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1890                    "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1891                    ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1892                    &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1893                    &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1894                    &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1895                    &ipv6_frag_mask, &n) > 0
1896             && n > 0
1897             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1898             struct ovs_key_ipv6 ipv6_key;
1899             struct ovs_key_ipv6 ipv6_mask;
1900
1901             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1902                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1903                 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1904                 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1905                 return -EINVAL;
1906             }
1907
1908             ipv6_key.ipv6_label = htonl(ipv6_label);
1909             ipv6_key.ipv6_proto = ipv6_proto;
1910             ipv6_key.ipv6_tclass = ipv6_tclass;
1911             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1912             ipv6_key.ipv6_frag = ipv6_frag;
1913             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1914                               &ipv6_key, sizeof ipv6_key);
1915
1916             ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1917             ipv6_mask.ipv6_proto = ipv6_proto_mask;
1918             ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1919             ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1920             ipv6_mask.ipv6_frag = ipv6_frag_mask;
1921             nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1922                               &ipv6_mask, sizeof ipv6_mask);
1923             return n;
1924         } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1925                    "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1926                    ipv6_src_s, ipv6_dst_s, &ipv6_label,
1927                    &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1928             && n > 0
1929             && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1930             struct ovs_key_ipv6 ipv6_key;
1931
1932             if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1933                 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1934                 return -EINVAL;
1935             }
1936             ipv6_key.ipv6_label = htonl(ipv6_label);
1937             ipv6_key.ipv6_proto = ipv6_proto;
1938             ipv6_key.ipv6_tclass = ipv6_tclass;
1939             ipv6_key.ipv6_hlimit = ipv6_hlimit;
1940             ipv6_key.ipv6_frag = ipv6_frag;
1941             nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1942                               &ipv6_key, sizeof ipv6_key);
1943
1944             if (mask) {
1945                 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1946                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1947                               &ipv6_key, sizeof ipv6_key);
1948             }
1949             return n;
1950         }
1951     }
1952
1953     {
1954         int tcp_src;
1955         int tcp_dst;
1956         int tcp_src_mask;
1957         int tcp_dst_mask;
1958         int n = -1;
1959
1960         if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1961                    &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1962             && n > 0) {
1963             struct ovs_key_tcp tcp_key;
1964             struct ovs_key_tcp tcp_mask;
1965
1966             tcp_key.tcp_src = htons(tcp_src);
1967             tcp_key.tcp_dst = htons(tcp_dst);
1968             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1969
1970             tcp_mask.tcp_src = htons(tcp_src_mask);
1971             tcp_mask.tcp_dst = htons(tcp_dst_mask);
1972             nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1973                               &tcp_mask, sizeof tcp_mask);
1974             return n;
1975         } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1976             && n > 0) {
1977             struct ovs_key_tcp tcp_key;
1978
1979             tcp_key.tcp_src = htons(tcp_src);
1980             tcp_key.tcp_dst = htons(tcp_dst);
1981             nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1982
1983             if (mask) {
1984                 memset(&tcp_key, 0xff, sizeof tcp_key);
1985                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1986                               &tcp_key, sizeof tcp_key);
1987             }
1988             return n;
1989         }
1990     }
1991
1992     {
1993         int udp_src;
1994         int udp_dst;
1995         int udp_src_mask;
1996         int udp_dst_mask;
1997         int n = -1;
1998
1999         if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
2000                    &udp_src, &udp_src_mask,
2001                    &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
2002             struct ovs_key_udp udp_key;
2003             struct ovs_key_udp udp_mask;
2004
2005             udp_key.udp_src = htons(udp_src);
2006             udp_key.udp_dst = htons(udp_dst);
2007             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2008
2009             udp_mask.udp_src = htons(udp_src_mask);
2010             udp_mask.udp_dst = htons(udp_dst_mask);
2011             nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2012                               &udp_mask, sizeof udp_mask);
2013             return n;
2014         }
2015         if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
2016             && n > 0) {
2017             struct ovs_key_udp udp_key;
2018
2019             udp_key.udp_src = htons(udp_src);
2020             udp_key.udp_dst = htons(udp_dst);
2021             nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2022
2023             if (mask) {
2024                 memset(&udp_key, 0xff, sizeof udp_key);
2025                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2026             }
2027             return n;
2028         }
2029     }
2030
2031     {
2032         int icmp_type;
2033         int icmp_code;
2034         int icmp_type_mask;
2035         int icmp_code_mask;
2036         int n = -1;
2037
2038         if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2039                    &icmp_type, &icmp_type_mask,
2040                    &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2041             struct ovs_key_icmp icmp_key;
2042             struct ovs_key_icmp icmp_mask;
2043
2044             icmp_key.icmp_type = icmp_type;
2045             icmp_key.icmp_code = icmp_code;
2046             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2047                               &icmp_key, sizeof icmp_key);
2048
2049             icmp_mask.icmp_type = icmp_type_mask;
2050             icmp_mask.icmp_code = icmp_code_mask;
2051             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2052                               &icmp_mask, sizeof icmp_mask);
2053             return n;
2054         } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2055                    &icmp_type, &icmp_code, &n) > 0
2056             && n > 0) {
2057             struct ovs_key_icmp icmp_key;
2058
2059             icmp_key.icmp_type = icmp_type;
2060             icmp_key.icmp_code = icmp_code;
2061             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2062                               &icmp_key, sizeof icmp_key);
2063             if (mask) {
2064                 memset(&icmp_key, 0xff, sizeof icmp_key);
2065                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2066                               sizeof icmp_key);
2067             }
2068             return n;
2069         }
2070     }
2071
2072     {
2073         struct ovs_key_icmpv6 icmpv6_key;
2074         struct ovs_key_icmpv6 icmpv6_mask;
2075         int icmpv6_type_mask;
2076         int icmpv6_code_mask;
2077         int n = -1;
2078
2079         if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2080                    &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2081                    &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2082             && n > 0) {
2083             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2084                               &icmpv6_key, sizeof icmpv6_key);
2085
2086             icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2087             icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2088             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2089                               sizeof icmpv6_mask);
2090             return n;
2091         } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2092                    &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2093             && n > 0) {
2094             nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2095                               &icmpv6_key, sizeof icmpv6_key);
2096
2097             if (mask) {
2098                 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2099                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2100                               sizeof icmpv6_key);
2101             }
2102             return n;
2103         }
2104     }
2105
2106     {
2107         ovs_be32 arp_sip, arp_sip_mask;
2108         ovs_be32 arp_tip, arp_tip_mask;
2109         int arp_op, arp_op_mask;
2110         uint8_t arp_sha[ETH_ADDR_LEN];
2111         uint8_t arp_sha_mask[ETH_ADDR_LEN];
2112         uint8_t arp_tha[ETH_ADDR_LEN];
2113         uint8_t arp_tha_mask[ETH_ADDR_LEN];
2114         int n = -1;
2115
2116         if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2117                    "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2118                    "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2119                    "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2120                    IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2121                    IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2122                    &arp_op, &arp_op_mask,
2123                    ETH_ADDR_SCAN_ARGS(arp_sha),
2124                    ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2125                    ETH_ADDR_SCAN_ARGS(arp_tha),
2126                    ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2127             struct ovs_key_arp arp_key;
2128             struct ovs_key_arp arp_mask;
2129
2130             memset(&arp_key, 0, sizeof arp_key);
2131             arp_key.arp_sip = arp_sip;
2132             arp_key.arp_tip = arp_tip;
2133             arp_key.arp_op = htons(arp_op);
2134             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2135             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2136             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2137
2138             arp_mask.arp_sip = arp_sip_mask;
2139             arp_mask.arp_tip = arp_tip_mask;
2140             arp_mask.arp_op = htons(arp_op_mask);
2141             memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2142             memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2143             nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2144                               &arp_mask, sizeof arp_mask);
2145             return n;
2146         } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2147                    "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2148                    IP_SCAN_ARGS(&arp_sip),
2149                    IP_SCAN_ARGS(&arp_tip),
2150                    &arp_op,
2151                    ETH_ADDR_SCAN_ARGS(arp_sha),
2152                    ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2153             struct ovs_key_arp arp_key;
2154
2155             memset(&arp_key, 0, sizeof arp_key);
2156             arp_key.arp_sip = arp_sip;
2157             arp_key.arp_tip = arp_tip;
2158             arp_key.arp_op = htons(arp_op);
2159             memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2160             memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2161             nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2162
2163             if (mask) {
2164                 memset(&arp_key, 0xff, sizeof arp_key);
2165                 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2166                                   &arp_key, sizeof arp_key);
2167             }
2168             return n;
2169         }
2170     }
2171
2172     {
2173         char nd_target_s[IPV6_SCAN_LEN + 1];
2174         char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2175         uint8_t nd_sll[ETH_ADDR_LEN];
2176         uint8_t nd_sll_mask[ETH_ADDR_LEN];
2177         uint8_t nd_tll[ETH_ADDR_LEN];
2178         uint8_t nd_tll_mask[ETH_ADDR_LEN];
2179         int n = -1;
2180
2181         nd_target_mask_s[0] = 0;
2182         memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2183         memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2184
2185         if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2186                    nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2187                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2188                 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2189         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2190                    nd_target_s, &n) > 0 && n > 0) {
2191                 put_nd_key(n, nd_target_s, NULL, NULL, key);
2192                 if (mask) {
2193                     put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2194                 }
2195         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2196                          ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2197                    nd_target_s, nd_target_mask_s,
2198                    ETH_ADDR_SCAN_ARGS(nd_sll),
2199                    ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2200             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2201             put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2202         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2203                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2204             && n > 0) {
2205             put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2206             if (mask) {
2207                 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2208             }
2209         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2210                          ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2211                    nd_target_s, nd_target_mask_s,
2212                    ETH_ADDR_SCAN_ARGS(nd_tll),
2213                    ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2214             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2215             put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2216         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2217                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2218             && n > 0) {
2219             put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2220             if (mask) {
2221                 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2222             }
2223         } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2224                    ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2225                    "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2226                    nd_target_s, nd_target_mask_s,
2227                    ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2228                    ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2229                    &n) > 0
2230             && n > 0) {
2231             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2232             put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2233         } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2234                    "tll="ETH_ADDR_SCAN_FMT")%n",
2235                    nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2236                    ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2237             && n > 0) {
2238             put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2239             if (mask) {
2240                 put_nd_mask(n, nd_target_mask_s,
2241                             nd_sll_mask, nd_tll_mask, mask);
2242             }
2243         }
2244
2245         if (n != -1)
2246             return n;
2247
2248     }
2249
2250     if (!strncmp(s, "encap(", 6)) {
2251         const char *start = s;
2252         size_t encap, encap_mask = 0;
2253
2254         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2255         if (mask) {
2256             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2257         }
2258
2259         s += 6;
2260         for (;;) {
2261             int retval;
2262
2263             s += strspn(s, ", \t\r\n");
2264             if (!*s) {
2265                 return -EINVAL;
2266             } else if (*s == ')') {
2267                 break;
2268             }
2269
2270             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2271             if (retval < 0) {
2272                 return retval;
2273             }
2274             s += retval;
2275         }
2276         s++;
2277
2278         nl_msg_end_nested(key, encap);
2279         if (mask) {
2280             nl_msg_end_nested(mask, encap_mask);
2281         }
2282
2283         return s - start;
2284     }
2285
2286     return -EINVAL;
2287 }
2288
2289 /* Parses the string representation of a datapath flow key, in the
2290  * format output by odp_flow_key_format().  Returns 0 if successful,
2291  * otherwise a positive errno value.  On success, the flow key is
2292  * appended to 'key' as a series of Netlink attributes.  On failure, no
2293  * data is appended to 'key'.  Either way, 'key''s data might be
2294  * reallocated.
2295  *
2296  * If 'port_names' is nonnull, it points to an simap that maps from a port name
2297  * to a port number.  (Port names may be used instead of port numbers in
2298  * in_port.)
2299  *
2300  * On success, the attributes appended to 'key' are individually syntactically
2301  * valid, but they may not be valid as a sequence.  'key' might, for example,
2302  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
2303 int
2304 odp_flow_from_string(const char *s, const struct simap *port_names,
2305                      struct ofpbuf *key, struct ofpbuf *mask)
2306 {
2307     const size_t old_size = key->size;
2308     for (;;) {
2309         int retval;
2310
2311         s += strspn(s, delimiters);
2312         if (!*s) {
2313             return 0;
2314         }
2315
2316         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2317         if (retval < 0) {
2318             key->size = old_size;
2319             return -retval;
2320         }
2321         s += retval;
2322     }
2323
2324     return 0;
2325 }
2326
2327 static uint8_t
2328 ovs_to_odp_frag(uint8_t nw_frag)
2329 {
2330     return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2331           : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2332           : OVS_FRAG_TYPE_LATER);
2333 }
2334
2335 static uint8_t
2336 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2337 {
2338     uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2339
2340     frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2341     frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2342
2343     return frag_mask;
2344 }
2345
2346 static void
2347 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2348                          const struct flow *flow, odp_port_t odp_in_port)
2349 {
2350     bool is_mask;
2351     struct ovs_key_ethernet *eth_key;
2352     size_t encap;
2353
2354     /* We assume that if 'data' and 'flow' are not the same, we should
2355      * treat 'data' as a mask. */
2356     is_mask = (data != flow);
2357
2358     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2359
2360     if (flow->tunnel.ip_dst || is_mask) {
2361         tun_key_to_attr(buf, &data->tunnel);
2362     }
2363
2364     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->skb_mark);
2365
2366     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2367      * is not the magical value "ODPP_NONE". */
2368     if (is_mask || odp_in_port != ODPP_NONE) {
2369         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2370     }
2371
2372     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2373                                        sizeof *eth_key);
2374     memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2375     memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2376
2377     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2378         if (is_mask) {
2379             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2380         } else {
2381             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2382         }
2383         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2384         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2385         if (flow->vlan_tci == htons(0)) {
2386             goto unencap;
2387         }
2388     } else {
2389         encap = 0;
2390     }
2391
2392     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2393         /* For backwards compatibility with kernels that don't support
2394          * wildcarding, the following convention is used to encode the
2395          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2396          *
2397          *   key      mask    matches
2398          * -------- --------  -------
2399          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
2400          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
2401          *  <none>   0xffff   Any non-Ethernet II frame (except valid
2402          *                    802.3 SNAP packet with valid eth_type).
2403          */
2404         if (is_mask) {
2405             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2406         }
2407         goto unencap;
2408     }
2409
2410     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2411
2412     if (flow->dl_type == htons(ETH_TYPE_IP)) {
2413         struct ovs_key_ipv4 *ipv4_key;
2414
2415         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2416                                             sizeof *ipv4_key);
2417         ipv4_key->ipv4_src = data->nw_src;
2418         ipv4_key->ipv4_dst = data->nw_dst;
2419         ipv4_key->ipv4_proto = data->nw_proto;
2420         ipv4_key->ipv4_tos = data->nw_tos;
2421         ipv4_key->ipv4_ttl = data->nw_ttl;
2422         ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2423                                       : ovs_to_odp_frag(data->nw_frag);
2424     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2425         struct ovs_key_ipv6 *ipv6_key;
2426
2427         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2428                                             sizeof *ipv6_key);
2429         memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2430         memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2431         ipv6_key->ipv6_label = data->ipv6_label;
2432         ipv6_key->ipv6_proto = data->nw_proto;
2433         ipv6_key->ipv6_tclass = data->nw_tos;
2434         ipv6_key->ipv6_hlimit = data->nw_ttl;
2435         ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2436                                       : ovs_to_odp_frag(data->nw_frag);
2437     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2438                flow->dl_type == htons(ETH_TYPE_RARP)) {
2439         struct ovs_key_arp *arp_key;
2440
2441         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2442                                            sizeof *arp_key);
2443         memset(arp_key, 0, sizeof *arp_key);
2444         arp_key->arp_sip = data->nw_src;
2445         arp_key->arp_tip = data->nw_dst;
2446         arp_key->arp_op = htons(data->nw_proto);
2447         memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2448         memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2449     }
2450
2451     if (flow->mpls_depth) {
2452         struct ovs_key_mpls *mpls_key;
2453
2454         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2455                                             sizeof *mpls_key);
2456         mpls_key->mpls_lse = data->mpls_lse;
2457     }
2458
2459     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2460         if (flow->nw_proto == IPPROTO_TCP) {
2461             struct ovs_key_tcp *tcp_key;
2462
2463             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2464                                                sizeof *tcp_key);
2465             tcp_key->tcp_src = data->tp_src;
2466             tcp_key->tcp_dst = data->tp_dst;
2467         } else if (flow->nw_proto == IPPROTO_UDP) {
2468             struct ovs_key_udp *udp_key;
2469
2470             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2471                                                sizeof *udp_key);
2472             udp_key->udp_src = data->tp_src;
2473             udp_key->udp_dst = data->tp_dst;
2474         } else if (flow->dl_type == htons(ETH_TYPE_IP)
2475                 && flow->nw_proto == IPPROTO_ICMP) {
2476             struct ovs_key_icmp *icmp_key;
2477
2478             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2479                                                 sizeof *icmp_key);
2480             icmp_key->icmp_type = ntohs(data->tp_src);
2481             icmp_key->icmp_code = ntohs(data->tp_dst);
2482         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2483                 && flow->nw_proto == IPPROTO_ICMPV6) {
2484             struct ovs_key_icmpv6 *icmpv6_key;
2485
2486             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2487                                                   sizeof *icmpv6_key);
2488             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2489             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2490
2491             if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
2492                     || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
2493                 struct ovs_key_nd *nd_key;
2494
2495                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2496                                                     sizeof *nd_key);
2497                 memcpy(nd_key->nd_target, &data->nd_target,
2498                         sizeof nd_key->nd_target);
2499                 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2500                 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2501             }
2502         }
2503     }
2504
2505 unencap:
2506     if (encap) {
2507         nl_msg_end_nested(buf, encap);
2508     }
2509 }
2510
2511 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2512  * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2513  * number rather than a datapath port number).  Instead, if 'odp_in_port'
2514  * is anything other than ODPP_NONE, it is included in 'buf' as the input
2515  * port.
2516  *
2517  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2518  * capable of being expanded to allow for that much space. */
2519 void
2520 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2521                        odp_port_t odp_in_port)
2522 {
2523     odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2524 }
2525
2526 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2527  * 'buf'.  'flow' is used as a template to determine how to interpret
2528  * 'mask'.  For example, the 'dl_type' of 'mask' describes the mask, but
2529  * it doesn't indicate whether the other fields should be interpreted as
2530  * ARP, IPv4, IPv6, etc.
2531  *
2532  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2533  * capable of being expanded to allow for that much space. */
2534 void
2535 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2536                        const struct flow *flow, uint32_t odp_in_port_mask)
2537 {
2538     odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask));
2539 }
2540
2541 uint32_t
2542 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2543 {
2544     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2545     return hash_words(ALIGNED_CAST(const uint32_t *, key),
2546                       key_len / sizeof(uint32_t), 0);
2547 }
2548
2549 static void
2550 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2551                        uint64_t attrs, int out_of_range_attr,
2552                        const struct nlattr *key, size_t key_len)
2553 {
2554     struct ds s;
2555     int i;
2556
2557     if (VLOG_DROP_DBG(rl)) {
2558         return;
2559     }
2560
2561     ds_init(&s);
2562     for (i = 0; i < 64; i++) {
2563         if (attrs & (UINT64_C(1) << i)) {
2564             char namebuf[OVS_KEY_ATTR_BUFSIZE];
2565
2566             ds_put_format(&s, " %s",
2567                           ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2568         }
2569     }
2570     if (out_of_range_attr) {
2571         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2572     }
2573
2574     ds_put_cstr(&s, ": ");
2575     odp_flow_key_format(key, key_len, &s);
2576
2577     VLOG_DBG("%s:%s", title, ds_cstr(&s));
2578     ds_destroy(&s);
2579 }
2580
2581 static bool
2582 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2583 {
2584     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2585
2586     if (odp_frag > OVS_FRAG_TYPE_LATER) {
2587         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2588         return false;
2589     }
2590
2591     if (odp_frag != OVS_FRAG_TYPE_NONE) {
2592         flow->nw_frag |= FLOW_NW_FRAG_ANY;
2593         if (odp_frag == OVS_FRAG_TYPE_LATER) {
2594             flow->nw_frag |= FLOW_NW_FRAG_LATER;
2595         }
2596     }
2597     return true;
2598 }
2599
2600 static bool
2601 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2602                    const struct nlattr *attrs[], uint64_t *present_attrsp,
2603                    int *out_of_range_attrp)
2604 {
2605     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2606     const struct nlattr *nla;
2607     uint64_t present_attrs;
2608     size_t left;
2609
2610     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2611     present_attrs = 0;
2612     *out_of_range_attrp = 0;
2613     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2614         uint16_t type = nl_attr_type(nla);
2615         size_t len = nl_attr_get_size(nla);
2616         int expected_len = odp_flow_key_attr_len(type);
2617
2618         if (len != expected_len && expected_len >= 0) {
2619             char namebuf[OVS_KEY_ATTR_BUFSIZE];
2620
2621             VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2622                         "length %d", ovs_key_attr_to_string(type, namebuf,
2623                                                             sizeof namebuf),
2624                         len, expected_len);
2625             return false;
2626         }
2627
2628         if (type > OVS_KEY_ATTR_MAX) {
2629             *out_of_range_attrp = type;
2630         } else {
2631             if (present_attrs & (UINT64_C(1) << type)) {
2632                 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2633
2634                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2635                             ovs_key_attr_to_string(type,
2636                                                    namebuf, sizeof namebuf));
2637                 return false;
2638             }
2639
2640             present_attrs |= UINT64_C(1) << type;
2641             attrs[type] = nla;
2642         }
2643     }
2644     if (left) {
2645         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2646         return false;
2647     }
2648
2649     *present_attrsp = present_attrs;
2650     return true;
2651 }
2652
2653 static enum odp_key_fitness
2654 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2655                    uint64_t expected_attrs,
2656                    const struct nlattr *key, size_t key_len)
2657 {
2658     uint64_t missing_attrs;
2659     uint64_t extra_attrs;
2660
2661     missing_attrs = expected_attrs & ~present_attrs;
2662     if (missing_attrs) {
2663         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2664         log_odp_key_attributes(&rl, "expected but not present",
2665                                missing_attrs, 0, key, key_len);
2666         return ODP_FIT_TOO_LITTLE;
2667     }
2668
2669     extra_attrs = present_attrs & ~expected_attrs;
2670     if (extra_attrs || out_of_range_attr) {
2671         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2672         log_odp_key_attributes(&rl, "present but not expected",
2673                                extra_attrs, out_of_range_attr, key, key_len);
2674         return ODP_FIT_TOO_MUCH;
2675     }
2676
2677     return ODP_FIT_PERFECT;
2678 }
2679
2680 static bool
2681 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2682                 uint64_t present_attrs, uint64_t *expected_attrs,
2683                 struct flow *flow)
2684 {
2685     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2686
2687     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2688         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2689         if (ntohs(flow->dl_type) < 1536) {
2690             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2691                         ntohs(flow->dl_type));
2692             return false;
2693         }
2694         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2695     } else {
2696         flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2697     }
2698     return true;
2699 }
2700
2701 static enum odp_key_fitness
2702 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2703                   uint64_t present_attrs, int out_of_range_attr,
2704                   uint64_t expected_attrs, struct flow *flow,
2705                   const struct nlattr *key, size_t key_len)
2706 {
2707     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2708
2709     if (eth_type_mpls(flow->dl_type)) {
2710         expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2711
2712         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2713             return ODP_FIT_TOO_LITTLE;
2714         }
2715         flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2716         flow->mpls_depth++;
2717     } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
2718         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2719         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2720             const struct ovs_key_ipv4 *ipv4_key;
2721
2722             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2723             flow->nw_src = ipv4_key->ipv4_src;
2724             flow->nw_dst = ipv4_key->ipv4_dst;
2725             flow->nw_proto = ipv4_key->ipv4_proto;
2726             flow->nw_tos = ipv4_key->ipv4_tos;
2727             flow->nw_ttl = ipv4_key->ipv4_ttl;
2728             if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2729                 return ODP_FIT_ERROR;
2730             }
2731         }
2732     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2733         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2734         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2735             const struct ovs_key_ipv6 *ipv6_key;
2736
2737             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2738             memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2739             memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2740             flow->ipv6_label = ipv6_key->ipv6_label;
2741             flow->nw_proto = ipv6_key->ipv6_proto;
2742             flow->nw_tos = ipv6_key->ipv6_tclass;
2743             flow->nw_ttl = ipv6_key->ipv6_hlimit;
2744             if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2745                 return ODP_FIT_ERROR;
2746             }
2747         }
2748     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2749                flow->dl_type == htons(ETH_TYPE_RARP)) {
2750         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2751         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2752             const struct ovs_key_arp *arp_key;
2753
2754             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2755             flow->nw_src = arp_key->arp_sip;
2756             flow->nw_dst = arp_key->arp_tip;
2757             if (arp_key->arp_op & htons(0xff00)) {
2758                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2759                             "key", ntohs(arp_key->arp_op));
2760                 return ODP_FIT_ERROR;
2761             }
2762             flow->nw_proto = ntohs(arp_key->arp_op);
2763             memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2764             memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2765         }
2766     }
2767
2768     if (flow->nw_proto == IPPROTO_TCP
2769         && (flow->dl_type == htons(ETH_TYPE_IP) ||
2770             flow->dl_type == htons(ETH_TYPE_IPV6))
2771         && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2772         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2773         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2774             const struct ovs_key_tcp *tcp_key;
2775
2776             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2777             flow->tp_src = tcp_key->tcp_src;
2778             flow->tp_dst = tcp_key->tcp_dst;
2779         }
2780     } else if (flow->nw_proto == IPPROTO_UDP
2781                && (flow->dl_type == htons(ETH_TYPE_IP) ||
2782                    flow->dl_type == htons(ETH_TYPE_IPV6))
2783                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2784         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2785         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2786             const struct ovs_key_udp *udp_key;
2787
2788             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2789             flow->tp_src = udp_key->udp_src;
2790             flow->tp_dst = udp_key->udp_dst;
2791         }
2792     } else if (flow->nw_proto == IPPROTO_ICMP
2793                && flow->dl_type == htons(ETH_TYPE_IP)
2794                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2795         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2796         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2797             const struct ovs_key_icmp *icmp_key;
2798
2799             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2800             flow->tp_src = htons(icmp_key->icmp_type);
2801             flow->tp_dst = htons(icmp_key->icmp_code);
2802         }
2803     } else if (flow->nw_proto == IPPROTO_ICMPV6
2804                && flow->dl_type == htons(ETH_TYPE_IPV6)
2805                && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2806         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2807         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2808             const struct ovs_key_icmpv6 *icmpv6_key;
2809
2810             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2811             flow->tp_src = htons(icmpv6_key->icmpv6_type);
2812             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2813
2814             if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2815                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2816                 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2817                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2818                     const struct ovs_key_nd *nd_key;
2819
2820                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2821                     memcpy(&flow->nd_target, nd_key->nd_target,
2822                            sizeof flow->nd_target);
2823                     memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2824                     memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2825                 }
2826             }
2827         }
2828     }
2829
2830     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
2831                               key, key_len);
2832 }
2833
2834 /* Parse 802.1Q header then encapsulated L3 attributes. */
2835 static enum odp_key_fitness
2836 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2837                    uint64_t present_attrs, int out_of_range_attr,
2838                    uint64_t expected_attrs, struct flow *flow,
2839                    const struct nlattr *key, size_t key_len)
2840 {
2841     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2842
2843     const struct nlattr *encap
2844         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
2845            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
2846     enum odp_key_fitness encap_fitness;
2847     enum odp_key_fitness fitness;
2848     ovs_be16 tci;
2849
2850     /* Calculate fitness of outer attributes. */
2851     expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
2852                        (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
2853     fitness = check_expectations(present_attrs, out_of_range_attr,
2854                                  expected_attrs, key, key_len);
2855
2856     /* Get the VLAN TCI value. */
2857     if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
2858         return ODP_FIT_TOO_LITTLE;
2859     }
2860     tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
2861     if (tci == htons(0)) {
2862         /* Corner case for a truncated 802.1Q header. */
2863         if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
2864             return ODP_FIT_TOO_MUCH;
2865         }
2866         return fitness;
2867     } else if (!(tci & htons(VLAN_CFI))) {
2868         VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
2869                     "but CFI bit is not set", ntohs(tci));
2870         return ODP_FIT_ERROR;
2871     }
2872
2873     /* Set vlan_tci.
2874      * Remove the TPID from dl_type since it's not the real Ethertype.  */
2875     flow->vlan_tci = tci;
2876     flow->dl_type = htons(0);
2877
2878     /* Now parse the encapsulated attributes. */
2879     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
2880                             attrs, &present_attrs, &out_of_range_attr)) {
2881         return ODP_FIT_ERROR;
2882     }
2883     expected_attrs = 0;
2884
2885     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2886         return ODP_FIT_ERROR;
2887     }
2888     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2889                                       expected_attrs, flow, key, key_len);
2890
2891     /* The overall fitness is the worse of the outer and inner attributes. */
2892     return MAX(fitness, encap_fitness);
2893 }
2894
2895 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2896  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
2897  * 'key' fits our expectations for what a flow key should contain.
2898  *
2899  * The 'in_port' will be the datapath's understanding of the port.  The
2900  * caller will need to translate with odp_port_to_ofp_port() if the
2901  * OpenFlow port is needed.
2902  *
2903  * This function doesn't take the packet itself as an argument because none of
2904  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
2905  * it is always possible to infer which additional attribute(s) should appear
2906  * by looking at the attributes for lower-level protocols, e.g. if the network
2907  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2908  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2909  * must be absent. */
2910 enum odp_key_fitness
2911 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2912                      struct flow *flow)
2913 {
2914     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2915     uint64_t expected_attrs;
2916     uint64_t present_attrs;
2917     int out_of_range_attr;
2918
2919     memset(flow, 0, sizeof *flow);
2920
2921     /* Parse attributes. */
2922     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2923                             &out_of_range_attr)) {
2924         return ODP_FIT_ERROR;
2925     }
2926     expected_attrs = 0;
2927
2928     /* Metadata. */
2929     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2930         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2931         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2932     }
2933
2934     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2935         flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2936         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2937     }
2938
2939     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2940         enum odp_key_fitness res;
2941
2942         res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2943         if (res == ODP_FIT_ERROR) {
2944             return ODP_FIT_ERROR;
2945         } else if (res == ODP_FIT_PERFECT) {
2946             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2947         }
2948     }
2949
2950     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2951         flow->in_port.odp_port
2952             = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
2953         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2954     } else {
2955         flow->in_port.odp_port = ODPP_NONE;
2956     }
2957
2958     /* Ethernet header. */
2959     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2960         const struct ovs_key_ethernet *eth_key;
2961
2962         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2963         memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2964         memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2965     }
2966     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2967
2968     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2969     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2970         return ODP_FIT_ERROR;
2971     }
2972
2973     if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2974         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2975                                   expected_attrs, flow, key, key_len);
2976     }
2977     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2978                              expected_attrs, flow, key, key_len);
2979 }
2980
2981 /* Returns 'fitness' as a string, for use in debug messages. */
2982 const char *
2983 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2984 {
2985     switch (fitness) {
2986     case ODP_FIT_PERFECT:
2987         return "OK";
2988     case ODP_FIT_TOO_MUCH:
2989         return "too_much";
2990     case ODP_FIT_TOO_LITTLE:
2991         return "too_little";
2992     case ODP_FIT_ERROR:
2993         return "error";
2994     default:
2995         return "<unknown>";
2996     }
2997 }
2998
2999 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3000  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
3001  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3002  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
3003  * null, then the return value is not meaningful.) */
3004 size_t
3005 odp_put_userspace_action(uint32_t pid,
3006                          const void *userdata, size_t userdata_size,
3007                          struct ofpbuf *odp_actions)
3008 {
3009     size_t userdata_ofs;
3010     size_t offset;
3011
3012     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3013     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3014     if (userdata) {
3015         userdata_ofs = odp_actions->size + NLA_HDRLEN;
3016         nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3017                           userdata, userdata_size);
3018     } else {
3019         userdata_ofs = 0;
3020     }
3021     nl_msg_end_nested(odp_actions, offset);
3022
3023     return userdata_ofs;
3024 }
3025
3026 void
3027 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3028                       struct ofpbuf *odp_actions)
3029 {
3030     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3031     tun_key_to_attr(odp_actions, tunnel);
3032     nl_msg_end_nested(odp_actions, offset);
3033 }
3034 \f
3035 /* The commit_odp_actions() function and its helpers. */
3036
3037 static void
3038 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3039                   const void *key, size_t key_size)
3040 {
3041     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3042     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3043     nl_msg_end_nested(odp_actions, offset);
3044 }
3045
3046 void
3047 odp_put_skb_mark_action(const uint32_t skb_mark,
3048                         struct ofpbuf *odp_actions)
3049 {
3050     commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
3051                       sizeof(skb_mark));
3052 }
3053
3054 /* If any of the flow key data that ODP actions can modify are different in
3055  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3056  * 'odp_actions' that change the flow tunneling information in key from
3057  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3058  * same way.  In other words, operates the same as commit_odp_actions(), but
3059  * only on tunneling information. */
3060 void
3061 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3062                          struct ofpbuf *odp_actions)
3063 {
3064     /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3065     if (flow->tunnel.ip_dst) {
3066         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3067             return;
3068         }
3069         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3070         odp_put_tunnel_action(&base->tunnel, odp_actions);
3071     }
3072 }
3073
3074 static void
3075 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3076                              struct ofpbuf *odp_actions,
3077                              struct flow_wildcards *wc)
3078 {
3079     struct ovs_key_ethernet eth_key;
3080
3081     if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3082         eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3083         return;
3084     }
3085
3086     memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3087     memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3088
3089     memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3090     memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3091
3092     memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3093     memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3094
3095     commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3096                       &eth_key, sizeof(eth_key));
3097 }
3098
3099 static void
3100 commit_vlan_action(const struct flow *flow, struct flow *base,
3101                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3102 {
3103     if (base->vlan_tci == flow->vlan_tci) {
3104         return;
3105     }
3106
3107     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3108
3109     if (base->vlan_tci & htons(VLAN_CFI)) {
3110         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3111     }
3112
3113     if (flow->vlan_tci & htons(VLAN_CFI)) {
3114         struct ovs_action_push_vlan vlan;
3115
3116         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3117         vlan.vlan_tci = flow->vlan_tci;
3118         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3119                           &vlan, sizeof vlan);
3120     }
3121     base->vlan_tci = flow->vlan_tci;
3122 }
3123
3124 static void
3125 commit_mpls_action(const struct flow *flow, struct flow *base,
3126                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3127 {
3128     if (flow->mpls_lse == base->mpls_lse &&
3129         flow->mpls_depth == base->mpls_depth) {
3130         return;
3131     }
3132
3133     memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3134
3135     if (flow->mpls_depth < base->mpls_depth) {
3136         if (base->mpls_depth - flow->mpls_depth > 1) {
3137             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3138             VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3139                          " a single mpls_pop action");
3140         }
3141
3142         nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3143     } else if (flow->mpls_depth > base->mpls_depth) {
3144         struct ovs_action_push_mpls *mpls;
3145
3146         if (flow->mpls_depth - base->mpls_depth > 1) {
3147             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3148             VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3149                          " a single mpls_push action");
3150         }
3151
3152         mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3153                                         sizeof *mpls);
3154         memset(mpls, 0, sizeof *mpls);
3155         mpls->mpls_ethertype = flow->dl_type;
3156         mpls->mpls_lse = flow->mpls_lse;
3157     } else {
3158         struct ovs_key_mpls mpls_key;
3159
3160         mpls_key.mpls_lse = flow->mpls_lse;
3161         commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3162                           &mpls_key, sizeof(mpls_key));
3163     }
3164
3165     base->dl_type = flow->dl_type;
3166     base->mpls_lse = flow->mpls_lse;
3167     base->mpls_depth = flow->mpls_depth;
3168 }
3169
3170 static void
3171 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3172                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3173 {
3174     struct ovs_key_ipv4 ipv4_key;
3175
3176     if (base->nw_src == flow->nw_src &&
3177         base->nw_dst == flow->nw_dst &&
3178         base->nw_tos == flow->nw_tos &&
3179         base->nw_ttl == flow->nw_ttl &&
3180         base->nw_frag == flow->nw_frag) {
3181         return;
3182     }
3183
3184     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3185     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3186     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3187     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3188     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3189     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3190
3191     ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3192     ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3193     ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3194     ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3195     ipv4_key.ipv4_proto = base->nw_proto;
3196     ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3197
3198     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3199                       &ipv4_key, sizeof(ipv4_key));
3200 }
3201
3202 static void
3203 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3204                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3205 {
3206     struct ovs_key_ipv6 ipv6_key;
3207
3208     if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3209         ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3210         base->ipv6_label == flow->ipv6_label &&
3211         base->nw_tos == flow->nw_tos &&
3212         base->nw_ttl == flow->nw_ttl &&
3213         base->nw_frag == flow->nw_frag) {
3214         return;
3215     }
3216
3217     memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3218     memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3219     memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3220     memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3221     memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3222     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3223     memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3224
3225     base->ipv6_src = flow->ipv6_src;
3226     memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3227     base->ipv6_dst = flow->ipv6_dst;
3228     memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3229
3230     ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3231     ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3232     ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3233     ipv6_key.ipv6_proto = base->nw_proto;
3234     ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3235
3236     commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3237                       &ipv6_key, sizeof(ipv6_key));
3238 }
3239
3240 static void
3241 commit_set_nw_action(const struct flow *flow, struct flow *base,
3242                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3243 {
3244     /* Check if flow really have an IP header. */
3245     if (!flow->nw_proto) {
3246         return;
3247     }
3248
3249     if (base->dl_type == htons(ETH_TYPE_IP)) {
3250         commit_set_ipv4_action(flow, base, odp_actions, wc);
3251     } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3252         commit_set_ipv6_action(flow, base, odp_actions, wc);
3253     }
3254 }
3255
3256 static void
3257 commit_set_port_action(const struct flow *flow, struct flow *base,
3258                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3259 {
3260     if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3261         return;
3262     }
3263
3264     if (base->tp_src == flow->tp_src &&
3265         base->tp_dst == flow->tp_dst) {
3266         return;
3267     }
3268
3269     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3270     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3271
3272     if (flow->nw_proto == IPPROTO_TCP) {
3273         struct ovs_key_tcp port_key;
3274
3275         port_key.tcp_src = base->tp_src = flow->tp_src;
3276         port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3277
3278         commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3279                           &port_key, sizeof(port_key));
3280
3281     } else if (flow->nw_proto == IPPROTO_UDP) {
3282         struct ovs_key_udp port_key;
3283
3284         port_key.udp_src = base->tp_src = flow->tp_src;
3285         port_key.udp_dst = base->tp_dst = flow->tp_dst;
3286
3287         commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3288                           &port_key, sizeof(port_key));
3289     }
3290 }
3291
3292 static void
3293 commit_set_priority_action(const struct flow *flow, struct flow *base,
3294                            struct ofpbuf *odp_actions,
3295                            struct flow_wildcards *wc)
3296 {
3297     if (base->skb_priority == flow->skb_priority) {
3298         return;
3299     }
3300
3301     memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3302     base->skb_priority = flow->skb_priority;
3303
3304     commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3305                       &base->skb_priority, sizeof(base->skb_priority));
3306 }
3307
3308 static void
3309 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
3310                            struct ofpbuf *odp_actions,
3311                            struct flow_wildcards *wc)
3312 {
3313     if (base->skb_mark == flow->skb_mark) {
3314         return;
3315     }
3316
3317     memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
3318     base->skb_mark = flow->skb_mark;
3319
3320     odp_put_skb_mark_action(base->skb_mark, odp_actions);
3321 }
3322 /* If any of the flow key data that ODP actions can modify are different in
3323  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3324  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
3325  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
3326  * in addition to this function if needed.  Sets fields in 'wc' that are
3327  * used as part of the action. */
3328 void
3329 commit_odp_actions(const struct flow *flow, struct flow *base,
3330                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3331 {
3332     commit_set_ether_addr_action(flow, base, odp_actions, wc);
3333     commit_vlan_action(flow, base, odp_actions, wc);
3334     commit_set_nw_action(flow, base, odp_actions, wc);
3335     commit_set_port_action(flow, base, odp_actions, wc);
3336     /* Committing MPLS actions should occur after committing nw and port
3337      * actions. This is because committing MPLS actions may alter a packet so
3338      * that it is no longer IP and thus nw and port actions are no longer valid.
3339      */
3340     commit_mpls_action(flow, base, odp_actions, wc);
3341     commit_set_priority_action(flow, base, odp_actions, wc);
3342     commit_set_skb_mark_action(flow, base, odp_actions, wc);
3343 }