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