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