netdev-vport: Add IPv6 support for build/push/pop tunnel header
[cascardo/ovs.git] / lib / odp-util.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 <netinet/ip6.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "byte-order.h"
30 #include "coverage.h"
31 #include "dpif.h"
32 #include "dynamic-string.h"
33 #include "flow.h"
34 #include "netlink.h"
35 #include "ofpbuf.h"
36 #include "packets.h"
37 #include "simap.h"
38 #include "timeval.h"
39 #include "tun-metadata.h"
40 #include "unaligned.h"
41 #include "util.h"
42 #include "uuid.h"
43 #include "openvswitch/vlog.h"
44
45 VLOG_DEFINE_THIS_MODULE(odp_util);
46
47 /* The interface between userspace and kernel uses an "OVS_*" prefix.
48  * Since this is fairly non-specific for the OVS userspace components,
49  * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
50  * interactions with the datapath.
51  */
52
53 /* The set of characters that may separate one action or one key attribute
54  * from another. */
55 static const char *delimiters = ", \t\r\n";
56 static const char *delimiters_end = ", \t\r\n)";
57
58 struct attr_len_tbl {
59     int len;
60     const struct attr_len_tbl *next;
61     int next_max;
62 };
63 #define ATTR_LEN_INVALID  -1
64 #define ATTR_LEN_VARIABLE -2
65 #define ATTR_LEN_NESTED   -3
66
67 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
68                               struct ofpbuf *, struct ofpbuf *);
69 static void format_odp_key_attr(const struct nlattr *a,
70                                 const struct nlattr *ma,
71                                 const struct hmap *portno_names, struct ds *ds,
72                                 bool verbose);
73
74 struct geneve_scan {
75     struct geneve_opt d[63];
76     int len;
77 };
78
79 static int scan_geneve(const char *s, struct geneve_scan *key,
80                        struct geneve_scan *mask);
81 static void format_geneve_opts(const struct geneve_opt *opt,
82                                const struct geneve_opt *mask, int opts_len,
83                                struct ds *, bool verbose);
84
85 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
86                                                  int max, struct ofpbuf *,
87                                                  const struct nlattr *key);
88 static void format_u128(struct ds *ds, const ovs_u128 *value,
89                         const ovs_u128 *mask, bool verbose);
90 static int scan_u128(const char *s, ovs_u128 *value, ovs_u128 *mask);
91
92 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
93  * 'type':
94  *
95  *   - For an action whose argument has a fixed length, returned that
96  *     nonnegative length in bytes.
97  *
98  *   - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
99  *
100  *   - For an invalid 'type', returns ATTR_LEN_INVALID. */
101 static int
102 odp_action_len(uint16_t type)
103 {
104     if (type > OVS_ACTION_ATTR_MAX) {
105         return -1;
106     }
107
108     switch ((enum ovs_action_attr) type) {
109     case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
110     case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
111     case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
112     case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
113     case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
114     case OVS_ACTION_ATTR_POP_VLAN: return 0;
115     case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
116     case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
117     case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
118     case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
119     case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
120     case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
121     case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
122     case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE;
123
124     case OVS_ACTION_ATTR_UNSPEC:
125     case __OVS_ACTION_ATTR_MAX:
126         return ATTR_LEN_INVALID;
127     }
128
129     return ATTR_LEN_INVALID;
130 }
131
132 /* Returns a string form of 'attr'.  The return value is either a statically
133  * allocated constant string or the 'bufsize'-byte buffer 'namebuf'.  'bufsize'
134  * should be at least OVS_KEY_ATTR_BUFSIZE. */
135 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
136 static const char *
137 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
138 {
139     switch (attr) {
140     case OVS_KEY_ATTR_UNSPEC: return "unspec";
141     case OVS_KEY_ATTR_ENCAP: return "encap";
142     case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
143     case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
144     case OVS_KEY_ATTR_CT_STATE: return "ct_state";
145     case OVS_KEY_ATTR_CT_ZONE: return "ct_zone";
146     case OVS_KEY_ATTR_CT_MARK: return "ct_mark";
147     case OVS_KEY_ATTR_CT_LABELS: return "ct_label";
148     case OVS_KEY_ATTR_TUNNEL: return "tunnel";
149     case OVS_KEY_ATTR_IN_PORT: return "in_port";
150     case OVS_KEY_ATTR_ETHERNET: return "eth";
151     case OVS_KEY_ATTR_VLAN: return "vlan";
152     case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
153     case OVS_KEY_ATTR_IPV4: return "ipv4";
154     case OVS_KEY_ATTR_IPV6: return "ipv6";
155     case OVS_KEY_ATTR_TCP: return "tcp";
156     case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
157     case OVS_KEY_ATTR_UDP: return "udp";
158     case OVS_KEY_ATTR_SCTP: return "sctp";
159     case OVS_KEY_ATTR_ICMP: return "icmp";
160     case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
161     case OVS_KEY_ATTR_ARP: return "arp";
162     case OVS_KEY_ATTR_ND: return "nd";
163     case OVS_KEY_ATTR_MPLS: return "mpls";
164     case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
165     case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
166
167     case __OVS_KEY_ATTR_MAX:
168     default:
169         snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
170         return namebuf;
171     }
172 }
173
174 static void
175 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
176 {
177     size_t len = nl_attr_get_size(a);
178
179     ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
180     if (len) {
181         const uint8_t *unspec;
182         unsigned int i;
183
184         unspec = nl_attr_get(a);
185         for (i = 0; i < len; i++) {
186             ds_put_char(ds, i ? ' ': '(');
187             ds_put_format(ds, "%02x", unspec[i]);
188         }
189         ds_put_char(ds, ')');
190     }
191 }
192
193 static void
194 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
195 {
196     static const struct nl_policy ovs_sample_policy[] = {
197         [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
198         [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
199     };
200     struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
201     double percentage;
202     const struct nlattr *nla_acts;
203     int len;
204
205     ds_put_cstr(ds, "sample");
206
207     if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
208         ds_put_cstr(ds, "(error)");
209         return;
210     }
211
212     percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
213                         UINT32_MAX;
214
215     ds_put_format(ds, "(sample=%.1f%%,", percentage);
216
217     ds_put_cstr(ds, "actions(");
218     nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
219     len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
220     format_odp_actions(ds, nla_acts, len);
221     ds_put_format(ds, "))");
222 }
223
224 static const char *
225 slow_path_reason_to_string(uint32_t reason)
226 {
227     switch ((enum slow_path_reason) reason) {
228 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
229         SLOW_PATH_REASONS
230 #undef SPR
231     }
232
233     return NULL;
234 }
235
236 const char *
237 slow_path_reason_to_explanation(enum slow_path_reason reason)
238 {
239     switch (reason) {
240 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
241         SLOW_PATH_REASONS
242 #undef SPR
243     }
244
245     return "<unknown>";
246 }
247
248 static int
249 parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t),
250                 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
251 {
252     return parse_flags(s, bit_to_string, ')', NULL, NULL,
253                        res_flags, allowed, res_mask);
254 }
255
256 static void
257 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
258 {
259     static const struct nl_policy ovs_userspace_policy[] = {
260         [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
261         [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
262                                           .optional = true },
263         [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
264                                                  .optional = true },
265         [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC,
266                                                  .optional = true },
267     };
268     struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
269     const struct nlattr *userdata_attr;
270     const struct nlattr *tunnel_out_port_attr;
271
272     if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
273         ds_put_cstr(ds, "userspace(error)");
274         return;
275     }
276
277     ds_put_format(ds, "userspace(pid=%"PRIu32,
278                   nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
279
280     userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
281
282     if (userdata_attr) {
283         const uint8_t *userdata = nl_attr_get(userdata_attr);
284         size_t userdata_len = nl_attr_get_size(userdata_attr);
285         bool userdata_unspec = true;
286         union user_action_cookie cookie;
287
288         if (userdata_len >= sizeof cookie.type
289             && userdata_len <= sizeof cookie) {
290
291             memset(&cookie, 0, sizeof cookie);
292             memcpy(&cookie, userdata, userdata_len);
293
294             userdata_unspec = false;
295
296             if (userdata_len == sizeof cookie.sflow
297                 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
298                 ds_put_format(ds, ",sFlow("
299                               "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
300                               vlan_tci_to_vid(cookie.sflow.vlan_tci),
301                               vlan_tci_to_pcp(cookie.sflow.vlan_tci),
302                               cookie.sflow.output);
303             } else if (userdata_len == sizeof cookie.slow_path
304                        && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
305                 ds_put_cstr(ds, ",slow_path(");
306                 format_flags(ds, slow_path_reason_to_string,
307                              cookie.slow_path.reason, ',');
308                 ds_put_format(ds, ")");
309             } else if (userdata_len == sizeof cookie.flow_sample
310                        && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
311                 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
312                               ",collector_set_id=%"PRIu32
313                               ",obs_domain_id=%"PRIu32
314                               ",obs_point_id=%"PRIu32")",
315                               cookie.flow_sample.probability,
316                               cookie.flow_sample.collector_set_id,
317                               cookie.flow_sample.obs_domain_id,
318                               cookie.flow_sample.obs_point_id);
319             } else if (userdata_len >= sizeof cookie.ipfix
320                        && cookie.type == USER_ACTION_COOKIE_IPFIX) {
321                 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
322                               cookie.ipfix.output_odp_port);
323             } else {
324                 userdata_unspec = true;
325             }
326         }
327
328         if (userdata_unspec) {
329             size_t i;
330             ds_put_format(ds, ",userdata(");
331             for (i = 0; i < userdata_len; i++) {
332                 ds_put_format(ds, "%02x", userdata[i]);
333             }
334             ds_put_char(ds, ')');
335         }
336     }
337
338     if (a[OVS_USERSPACE_ATTR_ACTIONS]) {
339         ds_put_cstr(ds, ",actions");
340     }
341
342     tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
343     if (tunnel_out_port_attr) {
344         ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
345                       nl_attr_get_u32(tunnel_out_port_attr));
346     }
347
348     ds_put_char(ds, ')');
349 }
350
351 static void
352 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
353 {
354     if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
355         ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
356         if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
357             ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
358         };
359         ds_put_char(ds, ',');
360     }
361     if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
362         ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
363         if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
364             ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
365         }
366         ds_put_char(ds, ',');
367     }
368     if (!(tci & htons(VLAN_CFI))) {
369         ds_put_cstr(ds, "cfi=0");
370         ds_put_char(ds, ',');
371     }
372     ds_chomp(ds, ',');
373 }
374
375 static void
376 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
377 {
378     ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
379                   mpls_lse_to_label(mpls_lse),
380                   mpls_lse_to_tc(mpls_lse),
381                   mpls_lse_to_ttl(mpls_lse),
382                   mpls_lse_to_bos(mpls_lse));
383 }
384
385 static void
386 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
387             const struct ovs_key_mpls *mpls_mask, int n)
388 {
389     if (n == 1) {
390         ovs_be32 key = mpls_key->mpls_lse;
391
392         if (mpls_mask == NULL) {
393             format_mpls_lse(ds, key);
394         } else {
395             ovs_be32 mask = mpls_mask->mpls_lse;
396
397             ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
398                           mpls_lse_to_label(key), mpls_lse_to_label(mask),
399                           mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
400                           mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
401                           mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
402         }
403     } else {
404         int i;
405
406         for (i = 0; i < n; i++) {
407             ds_put_format(ds, "lse%d=%#"PRIx32,
408                           i, ntohl(mpls_key[i].mpls_lse));
409             if (mpls_mask) {
410                 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
411             }
412             ds_put_char(ds, ',');
413         }
414         ds_chomp(ds, ',');
415     }
416 }
417
418 static void
419 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
420 {
421     ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
422 }
423
424 static void
425 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
426 {
427     ds_put_format(ds, "hash(");
428
429     if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
430         ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
431     } else {
432         ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
433                       hash_act->hash_alg);
434     }
435     ds_put_format(ds, ")");
436 }
437
438 static const void *
439 format_udp_tnl_push_header(struct ds *ds, const struct udp_header *udp)
440 {
441     ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
442                   ntohs(udp->udp_src), ntohs(udp->udp_dst),
443                   ntohs(udp->udp_csum));
444
445     return udp + 1;
446 }
447
448 static void
449 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
450 {
451     const struct eth_header *eth;
452     const void *l3;
453     const void *l4;
454     const struct udp_header *udp;
455
456     eth = (const struct eth_header *)data->header;
457
458     l3 = eth + 1;
459
460     /* Ethernet */
461     ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
462                   data->header_len, data->tnl_type);
463     ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
464     ds_put_format(ds, ",src=");
465     ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
466     ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
467
468     if (eth->eth_type == htons(ETH_TYPE_IP)) {
469         /* IPv4 */
470         const struct ip_header *ip;
471         ip = (const struct ip_header *) l3;
472         ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
473                       ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
474                       IP_ARGS(get_16aligned_be32(&ip->ip_src)),
475                       IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
476                       ip->ip_proto, ip->ip_tos,
477                       ip->ip_ttl,
478                       ip->ip_frag_off);
479         l4 = (ip + 1);
480     } else {
481         const struct ip6_hdr *ip6;
482         ip6 = (const struct ip6_hdr *) l3;
483         ds_put_format(ds, "ipv6(src=");
484         ipv6_format_addr(&ip6->ip6_src, ds);
485         ds_put_format(ds, ",dst=");
486         ipv6_format_addr(&ip6->ip6_dst, ds);
487         ds_put_format(ds, ",label=%i,proto=%"PRIu8",tclass=0x%"PRIx8
488                           ",hlimit=%"PRIu8"),",
489                       ntohl(ip6->ip6_flow) & IPV6_LABEL_MASK, ip6->ip6_nxt,
490                       (ntohl(ip6->ip6_flow) >> 20) & 0xff, ip6->ip6_hlim);
491         l4 = (ip6 + 1);
492     }
493
494     udp = (const struct udp_header *) l4;
495
496     if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
497         const struct vxlanhdr *vxh;
498
499         vxh = format_udp_tnl_push_header(ds, udp);
500
501         ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
502                       ntohl(get_16aligned_be32(&vxh->vx_flags)),
503                       ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
504     } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
505         const struct genevehdr *gnh;
506
507         gnh = format_udp_tnl_push_header(ds, udp);
508
509         ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
510                       gnh->oam ? "oam," : "",
511                       gnh->critical ? "crit," : "",
512                       ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
513  
514         if (gnh->opt_len) {
515             ds_put_cstr(ds, ",options(");
516             format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
517                                ds, false);
518             ds_put_char(ds, ')');
519         }
520
521         ds_put_char(ds, ')');
522     } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
523         const struct gre_base_hdr *greh;
524         ovs_16aligned_be32 *options;
525
526         greh = (const struct gre_base_hdr *) l4;
527
528         ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
529                            ntohs(greh->flags), ntohs(greh->protocol));
530         options = (ovs_16aligned_be32 *)(greh + 1);
531         if (greh->flags & htons(GRE_CSUM)) {
532             ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
533             options++;
534         }
535         if (greh->flags & htons(GRE_KEY)) {
536             ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
537             options++;
538         }
539         if (greh->flags & htons(GRE_SEQ)) {
540             ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
541             options++;
542         }
543         ds_put_format(ds, ")");
544     }
545     ds_put_format(ds, ")");
546 }
547
548 static void
549 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
550 {
551     struct ovs_action_push_tnl *data;
552
553     data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
554
555     ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
556     format_odp_tnl_push_header(ds, data);
557     ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
558 }
559
560 static const struct nl_policy ovs_nat_policy[] = {
561     [OVS_NAT_ATTR_SRC] = { .type = NL_A_FLAG, .optional = true, },
562     [OVS_NAT_ATTR_DST] = { .type = NL_A_FLAG, .optional = true, },
563     [OVS_NAT_ATTR_IP_MIN] = { .type = NL_A_UNSPEC, .optional = true,
564                               .min_len = sizeof(struct in_addr),
565                               .max_len = sizeof(struct in6_addr)},
566     [OVS_NAT_ATTR_IP_MAX] = { .type = NL_A_UNSPEC, .optional = true,
567                               .min_len = sizeof(struct in_addr),
568                               .max_len = sizeof(struct in6_addr)},
569     [OVS_NAT_ATTR_PROTO_MIN] = { .type = NL_A_U16, .optional = true, },
570     [OVS_NAT_ATTR_PROTO_MAX] = { .type = NL_A_U16, .optional = true, },
571     [OVS_NAT_ATTR_PERSISTENT] = { .type = NL_A_FLAG, .optional = true, },
572     [OVS_NAT_ATTR_PROTO_HASH] = { .type = NL_A_FLAG, .optional = true, },
573     [OVS_NAT_ATTR_PROTO_RANDOM] = { .type = NL_A_FLAG, .optional = true, },
574 };
575
576 static void
577 format_odp_ct_nat(struct ds *ds, const struct nlattr *attr)
578 {
579     struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)];
580     size_t addr_len;
581     ovs_be32 ip_min, ip_max;
582     struct in6_addr ip6_min, ip6_max;
583     uint16_t proto_min, proto_max;
584
585     if (!nl_parse_nested(attr, ovs_nat_policy, a, ARRAY_SIZE(a))) {
586         ds_put_cstr(ds, "nat(error: nl_parse_nested() failed.)");
587         return;
588     }
589     /* If no type, then nothing else either. */
590     if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST])
591         && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX]
592             || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX]
593             || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH]
594             || a[OVS_NAT_ATTR_PROTO_RANDOM])) {
595         ds_put_cstr(ds, "nat(error: options allowed only with \"src\" or \"dst\")");
596         return;
597     }
598     /* Both SNAT & DNAT may not be specified. */
599     if (a[OVS_NAT_ATTR_SRC] && a[OVS_NAT_ATTR_DST]) {
600         ds_put_cstr(ds, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
601         return;
602     }
603     /* proto may not appear without ip. */
604     if (!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_PROTO_MIN]) {
605         ds_put_cstr(ds, "nat(error: proto but no IP.)");
606         return;
607     }
608     /* MAX may not appear without MIN. */
609     if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX])
610         || (!a[OVS_NAT_ATTR_PROTO_MIN] && a[OVS_NAT_ATTR_PROTO_MAX])) {
611         ds_put_cstr(ds, "nat(error: range max without min.)");
612         return;
613     }
614     /* Address sizes must match. */
615     if ((a[OVS_NAT_ATTR_IP_MIN]
616          && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) &&
617              nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr)))
618         || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]
619             && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN])
620                 != nl_attr_get_size(a[OVS_NAT_ATTR_IP_MAX])))) {
621         ds_put_cstr(ds, "nat(error: IP address sizes do not match)");
622         return;
623     }
624
625     addr_len = a[OVS_NAT_ATTR_IP_MIN]
626         ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0;
627     ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN]
628         ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0;
629     ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX]
630         ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0;
631     if (addr_len == sizeof ip6_min) {
632         ip6_min = a[OVS_NAT_ATTR_IP_MIN]
633             ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN])
634             : in6addr_any;
635         ip6_max = a[OVS_NAT_ATTR_IP_MAX]
636             ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX])
637             : in6addr_any;
638     }
639     proto_min = a[OVS_NAT_ATTR_PROTO_MIN]
640         ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0;
641     proto_max = a[OVS_NAT_ATTR_PROTO_MAX]
642         ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0;
643
644     if ((addr_len == sizeof(ovs_be32)
645          && ip_max && ntohl(ip_min) > ntohl(ip_max))
646         || (addr_len == sizeof(struct in6_addr)
647             && !ipv6_mask_is_any(&ip6_max)
648             && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0)
649         || (proto_max && proto_min > proto_max)) {
650         ds_put_cstr(ds, "nat(range error)");
651         return;
652     }
653
654     ds_put_cstr(ds, "nat");
655     if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) {
656         ds_put_char(ds, '(');
657         if (a[OVS_NAT_ATTR_SRC]) {
658             ds_put_cstr(ds, "src");
659         } else if (a[OVS_NAT_ATTR_DST]) {
660             ds_put_cstr(ds, "dst");
661         }
662
663         if (addr_len > 0) {
664             ds_put_cstr(ds, "=");
665
666             if (addr_len == sizeof ip_min) {
667                 ds_put_format(ds, IP_FMT, IP_ARGS(ip_min));
668
669                 if (ip_max && ip_max != ip_min) {
670                     ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max));
671                 }
672             } else if (addr_len == sizeof ip6_min) {
673                 ipv6_format_addr_bracket(&ip6_min, ds, proto_min);
674
675                 if (!ipv6_mask_is_any(&ip6_max) &&
676                     memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) {
677                     ds_put_char(ds, '-');
678                     ipv6_format_addr_bracket(&ip6_max, ds, proto_min);
679                 }
680             }
681             if (proto_min) {
682                 ds_put_format(ds, ":%"PRIu16, proto_min);
683
684                 if (proto_max && proto_max != proto_min) {
685                     ds_put_format(ds, "-%"PRIu16, proto_max);
686                 }
687             }
688         }
689         ds_put_char(ds, ',');
690         if (a[OVS_NAT_ATTR_PERSISTENT]) {
691             ds_put_cstr(ds, "persistent,");
692         }
693         if (a[OVS_NAT_ATTR_PROTO_HASH]) {
694             ds_put_cstr(ds, "hash,");
695         }
696         if (a[OVS_NAT_ATTR_PROTO_RANDOM]) {
697             ds_put_cstr(ds, "random,");
698         }
699         ds_chomp(ds, ',');
700         ds_put_char(ds, ')');
701     }
702 }
703
704 static const struct nl_policy ovs_conntrack_policy[] = {
705     [OVS_CT_ATTR_COMMIT] = { .type = NL_A_FLAG, .optional = true, },
706     [OVS_CT_ATTR_ZONE] = { .type = NL_A_U16, .optional = true, },
707     [OVS_CT_ATTR_MARK] = { .type = NL_A_UNSPEC, .optional = true,
708                            .min_len = sizeof(uint32_t) * 2 },
709     [OVS_CT_ATTR_LABELS] = { .type = NL_A_UNSPEC, .optional = true,
710                              .min_len = sizeof(struct ovs_key_ct_labels) * 2 },
711     [OVS_CT_ATTR_HELPER] = { .type = NL_A_STRING, .optional = true,
712                              .min_len = 1, .max_len = 16 },
713     [OVS_CT_ATTR_NAT] = { .type = NL_A_UNSPEC, .optional = true },
714 };
715
716 static void
717 format_odp_conntrack_action(struct ds *ds, const struct nlattr *attr)
718 {
719     struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)];
720     const ovs_u128 *label;
721     const uint32_t *mark;
722     const char *helper;
723     uint16_t zone;
724     bool commit;
725     const struct nlattr *nat;
726
727     if (!nl_parse_nested(attr, ovs_conntrack_policy, a, ARRAY_SIZE(a))) {
728         ds_put_cstr(ds, "ct(error)");
729         return;
730     }
731
732     commit = a[OVS_CT_ATTR_COMMIT] ? true : false;
733     zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0;
734     mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL;
735     label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL;
736     helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL;
737     nat = a[OVS_CT_ATTR_NAT];
738
739     ds_put_format(ds, "ct");
740     if (commit || zone || mark || label || helper || nat) {
741         ds_put_cstr(ds, "(");
742         if (commit) {
743             ds_put_format(ds, "commit,");
744         }
745         if (zone) {
746             ds_put_format(ds, "zone=%"PRIu16",", zone);
747         }
748         if (mark) {
749             ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark,
750                           *(mark + 1));
751         }
752         if (label) {
753             ds_put_format(ds, "label=");
754             format_u128(ds, label, label + 1, true);
755             ds_put_char(ds, ',');
756         }
757         if (helper) {
758             ds_put_format(ds, "helper=%s,", helper);
759         }
760         if (nat) {
761             format_odp_ct_nat(ds, nat);
762         }
763         ds_chomp(ds, ',');
764         ds_put_cstr(ds, ")");
765     }
766 }
767
768 static void
769 format_odp_action(struct ds *ds, const struct nlattr *a)
770 {
771     int expected_len;
772     enum ovs_action_attr type = nl_attr_type(a);
773     size_t size;
774
775     expected_len = odp_action_len(nl_attr_type(a));
776     if (expected_len != ATTR_LEN_VARIABLE &&
777         nl_attr_get_size(a) != expected_len) {
778         ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
779                       nl_attr_get_size(a), expected_len);
780         format_generic_odp_action(ds, a);
781         return;
782     }
783
784     switch (type) {
785     case OVS_ACTION_ATTR_OUTPUT:
786         ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
787         break;
788     case OVS_ACTION_ATTR_TUNNEL_POP:
789         ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
790         break;
791     case OVS_ACTION_ATTR_TUNNEL_PUSH:
792         format_odp_tnl_push_action(ds, a);
793         break;
794     case OVS_ACTION_ATTR_USERSPACE:
795         format_odp_userspace_action(ds, a);
796         break;
797     case OVS_ACTION_ATTR_RECIRC:
798         format_odp_recirc_action(ds, nl_attr_get_u32(a));
799         break;
800     case OVS_ACTION_ATTR_HASH:
801         format_odp_hash_action(ds, nl_attr_get(a));
802         break;
803     case OVS_ACTION_ATTR_SET_MASKED:
804         a = nl_attr_get(a);
805         size = nl_attr_get_size(a) / 2;
806         ds_put_cstr(ds, "set(");
807
808         /* Masked set action not supported for tunnel key, which is bigger. */
809         if (size <= sizeof(struct ovs_key_ipv6)) {
810             struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
811                                                 sizeof(struct nlattr))];
812             struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
813                                                 sizeof(struct nlattr))];
814
815             mask->nla_type = attr->nla_type = nl_attr_type(a);
816             mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
817             memcpy(attr + 1, (char *)(a + 1), size);
818             memcpy(mask + 1, (char *)(a + 1) + size, size);
819             format_odp_key_attr(attr, mask, NULL, ds, false);
820         } else {
821             format_odp_key_attr(a, NULL, NULL, ds, false);
822         }
823         ds_put_cstr(ds, ")");
824         break;
825     case OVS_ACTION_ATTR_SET:
826         ds_put_cstr(ds, "set(");
827         format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
828         ds_put_cstr(ds, ")");
829         break;
830     case OVS_ACTION_ATTR_PUSH_VLAN: {
831         const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
832         ds_put_cstr(ds, "push_vlan(");
833         if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
834             ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
835         }
836         format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
837         ds_put_char(ds, ')');
838         break;
839     }
840     case OVS_ACTION_ATTR_POP_VLAN:
841         ds_put_cstr(ds, "pop_vlan");
842         break;
843     case OVS_ACTION_ATTR_PUSH_MPLS: {
844         const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
845         ds_put_cstr(ds, "push_mpls(");
846         format_mpls_lse(ds, mpls->mpls_lse);
847         ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
848         break;
849     }
850     case OVS_ACTION_ATTR_POP_MPLS: {
851         ovs_be16 ethertype = nl_attr_get_be16(a);
852         ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
853         break;
854     }
855     case OVS_ACTION_ATTR_SAMPLE:
856         format_odp_sample_action(ds, a);
857         break;
858     case OVS_ACTION_ATTR_CT:
859         format_odp_conntrack_action(ds, a);
860         break;
861     case OVS_ACTION_ATTR_UNSPEC:
862     case __OVS_ACTION_ATTR_MAX:
863     default:
864         format_generic_odp_action(ds, a);
865         break;
866     }
867 }
868
869 void
870 format_odp_actions(struct ds *ds, const struct nlattr *actions,
871                    size_t actions_len)
872 {
873     if (actions_len) {
874         const struct nlattr *a;
875         unsigned int left;
876
877         NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
878             if (a != actions) {
879                 ds_put_char(ds, ',');
880             }
881             format_odp_action(ds, a);
882         }
883         if (left) {
884             int i;
885
886             if (left == actions_len) {
887                 ds_put_cstr(ds, "<empty>");
888             }
889             ds_put_format(ds, ",***%u leftover bytes*** (", left);
890             for (i = 0; i < left; i++) {
891                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
892             }
893             ds_put_char(ds, ')');
894         }
895     } else {
896         ds_put_cstr(ds, "drop");
897     }
898 }
899
900 /* Separate out parse_odp_userspace_action() function. */
901 static int
902 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
903 {
904     uint32_t pid;
905     union user_action_cookie cookie;
906     struct ofpbuf buf;
907     odp_port_t tunnel_out_port;
908     int n = -1;
909     void *user_data = NULL;
910     size_t user_data_size = 0;
911     bool include_actions = false;
912
913     if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
914         return -EINVAL;
915     }
916
917     {
918         uint32_t output;
919         uint32_t probability;
920         uint32_t collector_set_id;
921         uint32_t obs_domain_id;
922         uint32_t obs_point_id;
923         int vid, pcp;
924         int n1 = -1;
925         if (ovs_scan(&s[n], ",sFlow(vid=%i,"
926                      "pcp=%i,output=%"SCNi32")%n",
927                      &vid, &pcp, &output, &n1)) {
928             uint16_t tci;
929
930             n += n1;
931             tci = vid | (pcp << VLAN_PCP_SHIFT);
932             if (tci) {
933                 tci |= VLAN_CFI;
934             }
935
936             cookie.type = USER_ACTION_COOKIE_SFLOW;
937             cookie.sflow.vlan_tci = htons(tci);
938             cookie.sflow.output = output;
939             user_data = &cookie;
940             user_data_size = sizeof cookie.sflow;
941         } else if (ovs_scan(&s[n], ",slow_path(%n",
942                             &n1)) {
943             int res;
944
945             n += n1;
946             cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
947             cookie.slow_path.unused = 0;
948             cookie.slow_path.reason = 0;
949
950             res = parse_odp_flags(&s[n], slow_path_reason_to_string,
951                                   &cookie.slow_path.reason,
952                                   SLOW_PATH_REASON_MASK, NULL);
953             if (res < 0 || s[n + res] != ')') {
954                 return res;
955             }
956             n += res + 1;
957
958             user_data = &cookie;
959             user_data_size = sizeof cookie.slow_path;
960         } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
961                             "collector_set_id=%"SCNi32","
962                             "obs_domain_id=%"SCNi32","
963                             "obs_point_id=%"SCNi32")%n",
964                             &probability, &collector_set_id,
965                             &obs_domain_id, &obs_point_id, &n1)) {
966             n += n1;
967
968             cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
969             cookie.flow_sample.probability = probability;
970             cookie.flow_sample.collector_set_id = collector_set_id;
971             cookie.flow_sample.obs_domain_id = obs_domain_id;
972             cookie.flow_sample.obs_point_id = obs_point_id;
973             user_data = &cookie;
974             user_data_size = sizeof cookie.flow_sample;
975         } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
976                             &output, &n1) ) {
977             n += n1;
978             cookie.type = USER_ACTION_COOKIE_IPFIX;
979             cookie.ipfix.output_odp_port = u32_to_odp(output);
980             user_data = &cookie;
981             user_data_size = sizeof cookie.ipfix;
982         } else if (ovs_scan(&s[n], ",userdata(%n",
983                             &n1)) {
984             char *end;
985
986             n += n1;
987             ofpbuf_init(&buf, 16);
988             end = ofpbuf_put_hex(&buf, &s[n], NULL);
989             if (end[0] != ')') {
990                 return -EINVAL;
991             }
992             user_data = buf.data;
993             user_data_size = buf.size;
994             n = (end + 1) - s;
995         }
996     }
997
998     {
999         int n1 = -1;
1000         if (ovs_scan(&s[n], ",actions%n", &n1)) {
1001             n += n1;
1002             include_actions = true;
1003         }
1004     }
1005
1006     {
1007         int n1 = -1;
1008         if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
1009                      &tunnel_out_port, &n1)) {
1010             odp_put_userspace_action(pid, user_data, user_data_size,
1011                                      tunnel_out_port, include_actions, actions);
1012             return n + n1;
1013         } else if (s[n] == ')') {
1014             odp_put_userspace_action(pid, user_data, user_data_size,
1015                                      ODPP_NONE, include_actions, actions);
1016             return n + 1;
1017         }
1018     }
1019
1020     return -EINVAL;
1021 }
1022
1023 static int
1024 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
1025 {
1026     struct eth_header *eth;
1027     struct ip_header *ip;
1028     struct ovs_16aligned_ip6_hdr *ip6;
1029     struct udp_header *udp;
1030     struct gre_base_hdr *greh;
1031     uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
1032     ovs_be32 sip, dip;
1033     uint32_t tnl_type = 0, header_len = 0, ip_len = 0;
1034     void *l3, *l4;
1035     int n = 0;
1036
1037     if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
1038         return -EINVAL;
1039     }
1040     eth = (struct eth_header *) data->header;
1041     l3 = (data->header + sizeof *eth);
1042     ip = (struct ip_header *) l3;
1043     ip6 = (struct ovs_16aligned_ip6_hdr *) l3;
1044     if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
1045                       "eth(dst="ETH_ADDR_SCAN_FMT",",
1046                       &data->header_len,
1047                       &data->tnl_type,
1048                       ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
1049         return -EINVAL;
1050     }
1051
1052     if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
1053                       ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
1054         return -EINVAL;
1055     }
1056     if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
1057         return -EINVAL;
1058     }
1059     eth->eth_type = htons(dl_type);
1060
1061     if (eth->eth_type == htons(ETH_TYPE_IP)) {
1062         /* IPv4 */
1063         if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
1064                           ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
1065                           IP_SCAN_ARGS(&sip),
1066                           IP_SCAN_ARGS(&dip),
1067                           &ip->ip_proto, &ip->ip_tos,
1068                           &ip->ip_ttl, &ip->ip_frag_off)) {
1069             return -EINVAL;
1070         }
1071         put_16aligned_be32(&ip->ip_src, sip);
1072         put_16aligned_be32(&ip->ip_dst, dip);
1073         ip_len = sizeof *ip;
1074     } else {
1075         char sip6_s[IPV6_SCAN_LEN + 1];
1076         char dip6_s[IPV6_SCAN_LEN + 1];
1077         struct in6_addr sip6, dip6;
1078         uint8_t tclass;
1079         uint32_t label;
1080         if (!ovs_scan_len(s, &n, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT
1081                              ",label=%i,proto=%"SCNi8",tclass=0x%"SCNx8
1082                              ",hlimit=%"SCNi8"),",
1083                              sip6_s, dip6_s, &label, &ip6->ip6_nxt,
1084                              &tclass, &ip6->ip6_hlim)
1085             || (label & ~IPV6_LABEL_MASK) != 0
1086             || inet_pton(AF_INET6, sip6_s, &sip6) != 1
1087             || inet_pton(AF_INET6, dip6_s, &dip6) != 1) {
1088             return -EINVAL;
1089         }
1090         put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) |
1091                            htonl(tclass << 20) | htonl(label));
1092         memcpy(&ip6->ip6_src, &sip6, sizeof(ip6->ip6_src));
1093         memcpy(&ip6->ip6_dst, &dip6, sizeof(ip6->ip6_dst));
1094         ip_len = sizeof *ip6;
1095     }
1096
1097     /* Tunnel header */
1098     l4 = ((uint8_t *) l3 + ip_len);
1099     udp = (struct udp_header *) l4;
1100     greh = (struct gre_base_hdr *) l4;
1101     if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
1102                      &udp_src, &udp_dst, &csum)) {
1103         uint32_t vx_flags, vni;
1104
1105         udp->udp_src = htons(udp_src);
1106         udp->udp_dst = htons(udp_dst);
1107         udp->udp_len = 0;
1108         udp->udp_csum = htons(csum);
1109
1110         if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
1111                          &vx_flags, &vni)) {
1112             struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
1113
1114             put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
1115             put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
1116             tnl_type = OVS_VPORT_TYPE_VXLAN;
1117             header_len = sizeof *eth + ip_len +
1118                          sizeof *udp + sizeof *vxh;
1119         } else if (ovs_scan_len(s, &n, "geneve(")) {
1120             struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
1121
1122             memset(gnh, 0, sizeof *gnh);
1123             header_len = sizeof *eth + ip_len +
1124                          sizeof *udp + sizeof *gnh;
1125
1126             if (ovs_scan_len(s, &n, "oam,")) {
1127                 gnh->oam = 1;
1128             }
1129             if (ovs_scan_len(s, &n, "crit,")) {
1130                 gnh->critical = 1;
1131             }
1132             if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
1133                 return -EINVAL;
1134             }
1135             if (ovs_scan_len(s, &n, ",options(")) {
1136                 struct geneve_scan options;
1137                 int len;
1138
1139                 memset(&options, 0, sizeof options);
1140                 len = scan_geneve(s + n, &options, NULL);
1141                 if (!len) {
1142                     return -EINVAL;
1143                 }
1144
1145                 memcpy(gnh->options, options.d, options.len);
1146                 gnh->opt_len = options.len / 4;
1147                 header_len += options.len;
1148
1149                 n += len;
1150             }
1151             if (!ovs_scan_len(s, &n, "))")) {
1152                 return -EINVAL;
1153             }
1154
1155             gnh->proto_type = htons(ETH_TYPE_TEB);
1156             put_16aligned_be32(&gnh->vni, htonl(vni << 8));
1157             tnl_type = OVS_VPORT_TYPE_GENEVE;
1158         } else {
1159             return -EINVAL;
1160         }
1161     } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
1162                             &gre_flags, &gre_proto)){
1163
1164         tnl_type = OVS_VPORT_TYPE_GRE;
1165         greh->flags = htons(gre_flags);
1166         greh->protocol = htons(gre_proto);
1167         ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
1168
1169         if (greh->flags & htons(GRE_CSUM)) {
1170             if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
1171                 return -EINVAL;
1172             }
1173
1174             memset(options, 0, sizeof *options);
1175             *((ovs_be16 *)options) = htons(csum);
1176             options++;
1177         }
1178         if (greh->flags & htons(GRE_KEY)) {
1179             uint32_t key;
1180
1181             if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
1182                 return -EINVAL;
1183             }
1184
1185             put_16aligned_be32(options, htonl(key));
1186             options++;
1187         }
1188         if (greh->flags & htons(GRE_SEQ)) {
1189             uint32_t seq;
1190
1191             if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
1192                 return -EINVAL;
1193             }
1194             put_16aligned_be32(options, htonl(seq));
1195             options++;
1196         }
1197
1198         if (!ovs_scan_len(s, &n, "))")) {
1199             return -EINVAL;
1200         }
1201
1202         header_len = sizeof *eth + ip_len +
1203                      ((uint8_t *) options - (uint8_t *) greh);
1204     } else {
1205         return -EINVAL;
1206     }
1207
1208     /* check tunnel meta data. */
1209     if (data->tnl_type != tnl_type) {
1210         return -EINVAL;
1211     }
1212     if (data->header_len != header_len) {
1213         return -EINVAL;
1214     }
1215
1216     /* Out port */
1217     if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1218         return -EINVAL;
1219     }
1220
1221     return n;
1222 }
1223
1224 struct ct_nat_params {
1225     bool snat;
1226     bool dnat;
1227     size_t addr_len;
1228     union {
1229         ovs_be32 ip;
1230         struct in6_addr ip6;
1231     } addr_min;
1232     union {
1233         ovs_be32 ip;
1234         struct in6_addr ip6;
1235     } addr_max;
1236     uint16_t proto_min;
1237     uint16_t proto_max;
1238     bool persistent;
1239     bool proto_hash;
1240     bool proto_random;
1241 };
1242
1243 static int
1244 scan_ct_nat_range(const char *s, int *n, struct ct_nat_params *p)
1245 {
1246     if (ovs_scan_len(s, n, "=")) {
1247         char ipv6_s[IPV6_SCAN_LEN + 1];
1248         struct in6_addr ipv6;
1249
1250         if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) {
1251             p->addr_len = sizeof p->addr_min.ip;
1252             if (ovs_scan_len(s, n, "-")) {
1253                 if (!ovs_scan_len(s, n, IP_SCAN_FMT,
1254                                   IP_SCAN_ARGS(&p->addr_max.ip))) {
1255                     return -EINVAL;
1256                 }
1257             }
1258         } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
1259                     || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
1260                    && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1261             p->addr_len = sizeof p->addr_min.ip6;
1262             p->addr_min.ip6 = ipv6;
1263             if (ovs_scan_len(s, n, "-")) {
1264                 if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
1265                      || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
1266                     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1267                     p->addr_max.ip6 = ipv6;
1268                 } else {
1269                     return -EINVAL;
1270                 }
1271             }
1272         } else {
1273             return -EINVAL;
1274         }
1275         if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) {
1276             if (ovs_scan_len(s, n, "-")) {
1277                 if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) {
1278                     return -EINVAL;
1279                 }
1280             }
1281         }
1282     }
1283     return 0;
1284 }
1285
1286 static int
1287 scan_ct_nat(const char *s, struct ct_nat_params *p)
1288 {
1289     int n = 0;
1290
1291     if (ovs_scan_len(s, &n, "nat")) {
1292         memset(p, 0, sizeof *p);
1293
1294         if (ovs_scan_len(s, &n, "(")) {
1295             char *end;
1296             int end_n;
1297
1298             end = strchr(s + n, ')');
1299             if (!end) {
1300                 return -EINVAL;
1301             }
1302             end_n = end - s;
1303
1304             while (n < end_n) {
1305                 n += strspn(s + n, delimiters);
1306                 if (ovs_scan_len(s, &n, "src")) {
1307                     int err = scan_ct_nat_range(s, &n, p);
1308                     if (err) {
1309                         return err;
1310                     }
1311                     p->snat = true;
1312                     continue;
1313                 }
1314                 if (ovs_scan_len(s, &n, "dst")) {
1315                     int err = scan_ct_nat_range(s, &n, p);
1316                     if (err) {
1317                         return err;
1318                     }
1319                     p->dnat = true;
1320                     continue;
1321                 }
1322                 if (ovs_scan_len(s, &n, "persistent")) {
1323                     p->persistent = true;
1324                     continue;
1325                 }
1326                 if (ovs_scan_len(s, &n, "hash")) {
1327                     p->proto_hash = true;
1328                     continue;
1329                 }
1330                 if (ovs_scan_len(s, &n, "random")) {
1331                     p->proto_random = true;
1332                     continue;
1333                 }
1334                 return -EINVAL;
1335             }
1336
1337             if (p->snat && p->dnat) {
1338                 return -EINVAL;
1339             }
1340             if ((p->addr_len != 0 &&
1341                  memcmp(&p->addr_max, &in6addr_any, p->addr_len) &&
1342                  memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) ||
1343                 (p->proto_max && p->proto_max < p->proto_min)) {
1344                 return -EINVAL;
1345             }
1346             if (p->proto_hash && p->proto_random) {
1347                 return -EINVAL;
1348             }
1349             n++;
1350         }
1351     }
1352     return n;
1353 }
1354
1355 static void
1356 nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions)
1357 {
1358     size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT);
1359
1360     if (p->snat) {
1361         nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC);
1362     } else if (p->dnat) {
1363         nl_msg_put_flag(actions, OVS_NAT_ATTR_DST);
1364     } else {
1365         goto out;
1366     }
1367     if (p->addr_len != 0) {
1368         nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min,
1369                           p->addr_len);
1370         if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) {
1371             nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max,
1372                               p->addr_len);
1373         }
1374         if (p->proto_min) {
1375             nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min);
1376             if (p->proto_max && p->proto_max > p->proto_min) {
1377                 nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max);
1378             }
1379         }
1380         if (p->persistent) {
1381             nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT);
1382         }
1383         if (p->proto_hash) {
1384             nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH);
1385         }
1386         if (p->proto_random) {
1387             nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM);
1388         }
1389     }
1390 out:
1391     nl_msg_end_nested(actions, start);
1392 }
1393
1394 static int
1395 parse_conntrack_action(const char *s_, struct ofpbuf *actions)
1396 {
1397     const char *s = s_;
1398
1399     if (ovs_scan(s, "ct")) {
1400         const char *helper = NULL;
1401         size_t helper_len = 0;
1402         bool commit = false;
1403         uint16_t zone = 0;
1404         struct {
1405             uint32_t value;
1406             uint32_t mask;
1407         } ct_mark = { 0, 0 };
1408         struct {
1409             ovs_u128 value;
1410             ovs_u128 mask;
1411         } ct_label;
1412         struct ct_nat_params nat_params;
1413         bool have_nat = false;
1414         size_t start;
1415         char *end;
1416
1417         memset(&ct_label, 0, sizeof(ct_label));
1418
1419         s += 2;
1420         if (ovs_scan(s, "(")) {
1421             s++;
1422 find_end:
1423             end = strchr(s, ')');
1424             if (!end) {
1425                 return -EINVAL;
1426             }
1427
1428             while (s != end) {
1429                 int n;
1430
1431                 s += strspn(s, delimiters);
1432                 if (ovs_scan(s, "commit%n", &n)) {
1433                     commit = true;
1434                     s += n;
1435                     continue;
1436                 }
1437                 if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) {
1438                     s += n;
1439                     continue;
1440                 }
1441                 if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) {
1442                     s += n;
1443                     n = -1;
1444                     if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) {
1445                         s += n;
1446                     } else {
1447                         ct_mark.mask = UINT32_MAX;
1448                     }
1449                     continue;
1450                 }
1451                 if (ovs_scan(s, "label=%n", &n)) {
1452                     int retval;
1453
1454                     s += n;
1455                     retval = scan_u128(s, &ct_label.value, &ct_label.mask);
1456                     if (retval < 0) {
1457                         return retval;
1458                     }
1459                     s += retval;
1460                     continue;
1461                 }
1462                 if (ovs_scan(s, "helper=%n", &n)) {
1463                     s += n;
1464                     helper_len = strcspn(s, delimiters_end);
1465                     if (!helper_len || helper_len > 15) {
1466                         return -EINVAL;
1467                     }
1468                     helper = s;
1469                     s += helper_len;
1470                     continue;
1471                 }
1472
1473                 n = scan_ct_nat(s, &nat_params);
1474                 if (n > 0) {
1475                     s += n;
1476                     have_nat = true;
1477
1478                     /* end points to the end of the nested, nat action.
1479                      * find the real end. */
1480                     goto find_end;
1481                 }
1482                 /* Nothing matched. */
1483                 return -EINVAL;
1484             }
1485             s++;
1486         }
1487
1488         start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT);
1489         if (commit) {
1490             nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT);
1491         }
1492         if (zone) {
1493             nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone);
1494         }
1495         if (ct_mark.mask) {
1496             nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark,
1497                               sizeof(ct_mark));
1498         }
1499         if (!ovs_u128_is_zero(&ct_label.mask)) {
1500             nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label,
1501                               sizeof ct_label);
1502         }
1503         if (helper) {
1504             nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper,
1505                                 helper_len);
1506         }
1507         if (have_nat) {
1508             nl_msg_put_ct_nat(&nat_params, actions);
1509         }
1510         nl_msg_end_nested(actions, start);
1511     }
1512
1513     return s - s_;
1514 }
1515
1516 static int
1517 parse_odp_action(const char *s, const struct simap *port_names,
1518                  struct ofpbuf *actions)
1519 {
1520     {
1521         uint32_t port;
1522         int n;
1523
1524         if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1525             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1526             return n;
1527         }
1528     }
1529
1530     if (port_names) {
1531         int len = strcspn(s, delimiters);
1532         struct simap_node *node;
1533
1534         node = simap_find_len(port_names, s, len);
1535         if (node) {
1536             nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1537             return len;
1538         }
1539     }
1540
1541     {
1542         uint32_t recirc_id;
1543         int n = -1;
1544
1545         if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1546             nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1547             return n;
1548         }
1549     }
1550
1551     if (!strncmp(s, "userspace(", 10)) {
1552         return parse_odp_userspace_action(s, actions);
1553     }
1554
1555     if (!strncmp(s, "set(", 4)) {
1556         size_t start_ofs;
1557         int retval;
1558         struct nlattr mask[128 / sizeof(struct nlattr)];
1559         struct ofpbuf maskbuf;
1560         struct nlattr *nested, *key;
1561         size_t size;
1562
1563         /* 'mask' is big enough to hold any key. */
1564         ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1565
1566         start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1567         retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1568         if (retval < 0) {
1569             return retval;
1570         }
1571         if (s[retval + 4] != ')') {
1572             return -EINVAL;
1573         }
1574
1575         nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1576         key = nested + 1;
1577
1578         size = nl_attr_get_size(mask);
1579         if (size == nl_attr_get_size(key)) {
1580             /* Change to masked set action if not fully masked. */
1581             if (!is_all_ones(mask + 1, size)) {
1582                 key->nla_len += size;
1583                 ofpbuf_put(actions, mask + 1, size);
1584                 /* 'actions' may have been reallocated by ofpbuf_put(). */
1585                 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1586                 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1587             }
1588         }
1589
1590         nl_msg_end_nested(actions, start_ofs);
1591         return retval + 5;
1592     }
1593
1594     {
1595         struct ovs_action_push_vlan push;
1596         int tpid = ETH_TYPE_VLAN;
1597         int vid, pcp;
1598         int cfi = 1;
1599         int n = -1;
1600
1601         if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1602             || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1603                         &vid, &pcp, &cfi, &n)
1604             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1605                         &tpid, &vid, &pcp, &n)
1606             || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1607                         &tpid, &vid, &pcp, &cfi, &n)) {
1608             push.vlan_tpid = htons(tpid);
1609             push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1610                                   | (pcp << VLAN_PCP_SHIFT)
1611                                   | (cfi ? VLAN_CFI : 0));
1612             nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1613                               &push, sizeof push);
1614
1615             return n;
1616         }
1617     }
1618
1619     if (!strncmp(s, "pop_vlan", 8)) {
1620         nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1621         return 8;
1622     }
1623
1624     {
1625         double percentage;
1626         int n = -1;
1627
1628         if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1629             && percentage >= 0. && percentage <= 100.0) {
1630             size_t sample_ofs, actions_ofs;
1631             double probability;
1632
1633             probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1634             sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1635             nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1636                            (probability <= 0 ? 0
1637                             : probability >= UINT32_MAX ? UINT32_MAX
1638                             : probability));
1639
1640             actions_ofs = nl_msg_start_nested(actions,
1641                                               OVS_SAMPLE_ATTR_ACTIONS);
1642             for (;;) {
1643                 int retval;
1644
1645                 n += strspn(s + n, delimiters);
1646                 if (s[n] == ')') {
1647                     break;
1648                 }
1649
1650                 retval = parse_odp_action(s + n, port_names, actions);
1651                 if (retval < 0) {
1652                     return retval;
1653                 }
1654                 n += retval;
1655             }
1656             nl_msg_end_nested(actions, actions_ofs);
1657             nl_msg_end_nested(actions, sample_ofs);
1658
1659             return s[n + 1] == ')' ? n + 2 : -EINVAL;
1660         }
1661     }
1662
1663     {
1664         uint32_t port;
1665         int n;
1666
1667         if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1668             nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1669             return n;
1670         }
1671     }
1672
1673     {
1674         int retval;
1675
1676         retval = parse_conntrack_action(s, actions);
1677         if (retval) {
1678             return retval;
1679         }
1680     }
1681
1682     {
1683         struct ovs_action_push_tnl data;
1684         int n;
1685
1686         n = ovs_parse_tnl_push(s, &data);
1687         if (n > 0) {
1688             odp_put_tnl_push_action(actions, &data);
1689             return n;
1690         } else if (n < 0) {
1691             return n;
1692         }
1693     }
1694     return -EINVAL;
1695 }
1696
1697 /* Parses the string representation of datapath actions, in the format output
1698  * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
1699  * value.  On success, the ODP actions are appended to 'actions' as a series of
1700  * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
1701  * way, 'actions''s data might be reallocated. */
1702 int
1703 odp_actions_from_string(const char *s, const struct simap *port_names,
1704                         struct ofpbuf *actions)
1705 {
1706     size_t old_size;
1707
1708     if (!strcasecmp(s, "drop")) {
1709         return 0;
1710     }
1711
1712     old_size = actions->size;
1713     for (;;) {
1714         int retval;
1715
1716         s += strspn(s, delimiters);
1717         if (!*s) {
1718             return 0;
1719         }
1720
1721         retval = parse_odp_action(s, port_names, actions);
1722         if (retval < 0 || !strchr(delimiters, s[retval])) {
1723             actions->size = old_size;
1724             return -retval;
1725         }
1726         s += retval;
1727     }
1728
1729     return 0;
1730 }
1731 \f
1732 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1733     [OVS_VXLAN_EXT_GBP]                 = { .len = 4 },
1734 };
1735
1736 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1737     [OVS_TUNNEL_KEY_ATTR_ID]            = { .len = 8 },
1738     [OVS_TUNNEL_KEY_ATTR_IPV4_SRC]      = { .len = 4 },
1739     [OVS_TUNNEL_KEY_ATTR_IPV4_DST]      = { .len = 4 },
1740     [OVS_TUNNEL_KEY_ATTR_TOS]           = { .len = 1 },
1741     [OVS_TUNNEL_KEY_ATTR_TTL]           = { .len = 1 },
1742     [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1743     [OVS_TUNNEL_KEY_ATTR_CSUM]          = { .len = 0 },
1744     [OVS_TUNNEL_KEY_ATTR_TP_SRC]        = { .len = 2 },
1745     [OVS_TUNNEL_KEY_ATTR_TP_DST]        = { .len = 2 },
1746     [OVS_TUNNEL_KEY_ATTR_OAM]           = { .len = 0 },
1747     [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS]   = { .len = ATTR_LEN_VARIABLE },
1748     [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS]    = { .len = ATTR_LEN_NESTED,
1749                                             .next = ovs_vxlan_ext_attr_lens ,
1750                                             .next_max = OVS_VXLAN_EXT_MAX},
1751     [OVS_TUNNEL_KEY_ATTR_IPV6_SRC]      = { .len = 16 },
1752     [OVS_TUNNEL_KEY_ATTR_IPV6_DST]      = { .len = 16 },
1753 };
1754
1755 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1756     [OVS_KEY_ATTR_ENCAP]     = { .len = ATTR_LEN_NESTED },
1757     [OVS_KEY_ATTR_PRIORITY]  = { .len = 4 },
1758     [OVS_KEY_ATTR_SKB_MARK]  = { .len = 4 },
1759     [OVS_KEY_ATTR_DP_HASH]   = { .len = 4 },
1760     [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1761     [OVS_KEY_ATTR_TUNNEL]    = { .len = ATTR_LEN_NESTED,
1762                                  .next = ovs_tun_key_attr_lens,
1763                                  .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1764     [OVS_KEY_ATTR_IN_PORT]   = { .len = 4  },
1765     [OVS_KEY_ATTR_ETHERNET]  = { .len = sizeof(struct ovs_key_ethernet) },
1766     [OVS_KEY_ATTR_VLAN]      = { .len = 2 },
1767     [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1768     [OVS_KEY_ATTR_MPLS]      = { .len = ATTR_LEN_VARIABLE },
1769     [OVS_KEY_ATTR_IPV4]      = { .len = sizeof(struct ovs_key_ipv4) },
1770     [OVS_KEY_ATTR_IPV6]      = { .len = sizeof(struct ovs_key_ipv6) },
1771     [OVS_KEY_ATTR_TCP]       = { .len = sizeof(struct ovs_key_tcp) },
1772     [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1773     [OVS_KEY_ATTR_UDP]       = { .len = sizeof(struct ovs_key_udp) },
1774     [OVS_KEY_ATTR_SCTP]      = { .len = sizeof(struct ovs_key_sctp) },
1775     [OVS_KEY_ATTR_ICMP]      = { .len = sizeof(struct ovs_key_icmp) },
1776     [OVS_KEY_ATTR_ICMPV6]    = { .len = sizeof(struct ovs_key_icmpv6) },
1777     [OVS_KEY_ATTR_ARP]       = { .len = sizeof(struct ovs_key_arp) },
1778     [OVS_KEY_ATTR_ND]        = { .len = sizeof(struct ovs_key_nd) },
1779     [OVS_KEY_ATTR_CT_STATE]  = { .len = 4 },
1780     [OVS_KEY_ATTR_CT_ZONE]   = { .len = 2 },
1781     [OVS_KEY_ATTR_CT_MARK]   = { .len = 4 },
1782     [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
1783 };
1784
1785 /* Returns the correct length of the payload for a flow key attribute of the
1786  * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1787  * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1788  * payload is a nested type. */
1789 static int
1790 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1791 {
1792     if (type > max_len) {
1793         return ATTR_LEN_INVALID;
1794     }
1795
1796     return tbl[type].len;
1797 }
1798
1799 static void
1800 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1801 {
1802     size_t len = nl_attr_get_size(a);
1803     if (len) {
1804         const uint8_t *unspec;
1805         unsigned int i;
1806
1807         unspec = nl_attr_get(a);
1808         for (i = 0; i < len; i++) {
1809             if (i) {
1810                 ds_put_char(ds, ' ');
1811             }
1812             ds_put_format(ds, "%02x", unspec[i]);
1813         }
1814     }
1815 }
1816
1817 static const char *
1818 ovs_frag_type_to_string(enum ovs_frag_type type)
1819 {
1820     switch (type) {
1821     case OVS_FRAG_TYPE_NONE:
1822         return "no";
1823     case OVS_FRAG_TYPE_FIRST:
1824         return "first";
1825     case OVS_FRAG_TYPE_LATER:
1826         return "later";
1827     case __OVS_FRAG_TYPE_MAX:
1828     default:
1829         return "<error>";
1830     }
1831 }
1832
1833 static enum odp_key_fitness
1834 odp_tun_key_from_attr__(const struct nlattr *attr,
1835                         const struct nlattr *flow_attrs, size_t flow_attr_len,
1836                         const struct flow_tnl *src_tun, struct flow_tnl *tun,
1837                         bool udpif)
1838 {
1839     unsigned int left;
1840     const struct nlattr *a;
1841     bool ttl = false;
1842     bool unknown = false;
1843
1844     NL_NESTED_FOR_EACH(a, left, attr) {
1845         uint16_t type = nl_attr_type(a);
1846         size_t len = nl_attr_get_size(a);
1847         int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1848                                             OVS_TUNNEL_ATTR_MAX, type);
1849
1850         if (len != expected_len && expected_len >= 0) {
1851             return ODP_FIT_ERROR;
1852         }
1853
1854         switch (type) {
1855         case OVS_TUNNEL_KEY_ATTR_ID:
1856             tun->tun_id = nl_attr_get_be64(a);
1857             tun->flags |= FLOW_TNL_F_KEY;
1858             break;
1859         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1860             tun->ip_src = nl_attr_get_be32(a);
1861             break;
1862         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1863             tun->ip_dst = nl_attr_get_be32(a);
1864             break;
1865         case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
1866             tun->ipv6_src = nl_attr_get_in6_addr(a);
1867             break;
1868         case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
1869             tun->ipv6_dst = nl_attr_get_in6_addr(a);
1870             break;
1871         case OVS_TUNNEL_KEY_ATTR_TOS:
1872             tun->ip_tos = nl_attr_get_u8(a);
1873             break;
1874         case OVS_TUNNEL_KEY_ATTR_TTL:
1875             tun->ip_ttl = nl_attr_get_u8(a);
1876             ttl = true;
1877             break;
1878         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1879             tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1880             break;
1881         case OVS_TUNNEL_KEY_ATTR_CSUM:
1882             tun->flags |= FLOW_TNL_F_CSUM;
1883             break;
1884         case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1885             tun->tp_src = nl_attr_get_be16(a);
1886             break;
1887         case OVS_TUNNEL_KEY_ATTR_TP_DST:
1888             tun->tp_dst = nl_attr_get_be16(a);
1889             break;
1890         case OVS_TUNNEL_KEY_ATTR_OAM:
1891             tun->flags |= FLOW_TNL_F_OAM;
1892             break;
1893         case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1894             static const struct nl_policy vxlan_opts_policy[] = {
1895                 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1896             };
1897             struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1898
1899             if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1900                 return ODP_FIT_ERROR;
1901             }
1902
1903             if (ext[OVS_VXLAN_EXT_GBP]) {
1904                 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1905
1906                 tun->gbp_id = htons(gbp & 0xFFFF);
1907                 tun->gbp_flags = (gbp >> 16) & 0xFF;
1908             }
1909
1910             break;
1911         }
1912         case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1913             if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1914                                                 src_tun, udpif, tun)) {
1915                 return ODP_FIT_ERROR;
1916             }
1917             break;
1918
1919         default:
1920             /* Allow this to show up as unexpected, if there are unknown
1921              * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1922             unknown = true;
1923             break;
1924         }
1925     }
1926
1927     if (!ttl) {
1928         return ODP_FIT_ERROR;
1929     }
1930     if (unknown) {
1931         return ODP_FIT_TOO_MUCH;
1932     }
1933     return ODP_FIT_PERFECT;
1934 }
1935
1936 enum odp_key_fitness
1937 odp_tun_key_from_attr(const struct nlattr *attr, bool udpif,
1938                       struct flow_tnl *tun)
1939 {
1940     memset(tun, 0, sizeof *tun);
1941     return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun, udpif);
1942 }
1943
1944 static void
1945 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1946                 const struct flow_tnl *tun_flow_key,
1947                 const struct ofpbuf *key_buf)
1948 {
1949     size_t tun_key_ofs;
1950
1951     tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1952
1953     /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1954     if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1955         nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1956     }
1957     if (tun_key->ip_src) {
1958         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1959     }
1960     if (tun_key->ip_dst) {
1961         nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1962     }
1963     if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
1964         nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
1965     }
1966     if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
1967         nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
1968     }
1969     if (tun_key->ip_tos) {
1970         nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1971     }
1972     nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1973     if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1974         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1975     }
1976     if (tun_key->flags & FLOW_TNL_F_CSUM) {
1977         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1978     }
1979     if (tun_key->tp_src) {
1980         nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1981     }
1982     if (tun_key->tp_dst) {
1983         nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1984     }
1985     if (tun_key->flags & FLOW_TNL_F_OAM) {
1986         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1987     }
1988     if (tun_key->gbp_flags || tun_key->gbp_id) {
1989         size_t vxlan_opts_ofs;
1990
1991         vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1992         nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1993                        (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1994         nl_msg_end_nested(a, vxlan_opts_ofs);
1995     }
1996     tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
1997
1998     nl_msg_end_nested(a, tun_key_ofs);
1999 }
2000
2001 static bool
2002 odp_mask_attr_is_wildcard(const struct nlattr *ma)
2003 {
2004     return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
2005 }
2006
2007 static bool
2008 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
2009 {
2010     if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
2011         return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
2012     }
2013     if (attr == OVS_KEY_ATTR_IPV6) {
2014         const struct ovs_key_ipv6 *ipv6_mask = mask;
2015
2016         return
2017             ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
2018              == htonl(IPV6_LABEL_MASK))
2019             && ipv6_mask->ipv6_proto == UINT8_MAX
2020             && ipv6_mask->ipv6_tclass == UINT8_MAX
2021             && ipv6_mask->ipv6_hlimit == UINT8_MAX
2022             && ipv6_mask->ipv6_frag == UINT8_MAX
2023             && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
2024             && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
2025     }
2026     if (attr == OVS_KEY_ATTR_TUNNEL) {
2027         return false;
2028     }
2029
2030     if (attr == OVS_KEY_ATTR_ARP) {
2031         /* ARP key has padding, ignore it. */
2032         BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
2033         BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
2034         size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
2035         ovs_assert(((uint16_t *)mask)[size/2] == 0);
2036     }
2037
2038     return is_all_ones(mask, size);
2039 }
2040
2041 static bool
2042 odp_mask_attr_is_exact(const struct nlattr *ma)
2043 {
2044     enum ovs_key_attr attr = nl_attr_type(ma);
2045     const void *mask;
2046     size_t size;
2047
2048     if (attr == OVS_KEY_ATTR_TUNNEL) {
2049         return false;
2050     } else {
2051         mask = nl_attr_get(ma);
2052         size = nl_attr_get_size(ma);
2053     }
2054
2055     return odp_mask_is_exact(attr, mask, size);
2056 }
2057
2058 void
2059 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
2060                      char *port_name)
2061 {
2062     struct odp_portno_names *odp_portno_names;
2063
2064     odp_portno_names = xmalloc(sizeof *odp_portno_names);
2065     odp_portno_names->port_no = port_no;
2066     odp_portno_names->name = xstrdup(port_name);
2067     hmap_insert(portno_names, &odp_portno_names->hmap_node,
2068                 hash_odp_port(port_no));
2069 }
2070
2071 static char *
2072 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
2073 {
2074     struct odp_portno_names *odp_portno_names;
2075
2076     HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
2077                              hash_odp_port(port_no), portno_names) {
2078         if (odp_portno_names->port_no == port_no) {
2079             return odp_portno_names->name;
2080         }
2081     }
2082     return NULL;
2083 }
2084
2085 void
2086 odp_portno_names_destroy(struct hmap *portno_names)
2087 {
2088     struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
2089     HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
2090                         hmap_node, portno_names) {
2091         hmap_remove(portno_names, &odp_portno_names->hmap_node);
2092         free(odp_portno_names->name);
2093         free(odp_portno_names);
2094     }
2095 }
2096
2097 /* Format helpers. */
2098
2099 static void
2100 format_eth(struct ds *ds, const char *name, const struct eth_addr key,
2101            const struct eth_addr *mask, bool verbose)
2102 {
2103     bool mask_empty = mask && eth_addr_is_zero(*mask);
2104
2105     if (verbose || !mask_empty) {
2106         bool mask_full = !mask || eth_mask_is_exact(*mask);
2107
2108         if (mask_full) {
2109             ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
2110         } else {
2111             ds_put_format(ds, "%s=", name);
2112             eth_format_masked(key, mask, ds);
2113             ds_put_char(ds, ',');
2114         }
2115     }
2116 }
2117
2118 static void
2119 format_be64(struct ds *ds, const char *name, ovs_be64 key,
2120             const ovs_be64 *mask, bool verbose)
2121 {
2122     bool mask_empty = mask && !*mask;
2123
2124     if (verbose || !mask_empty) {
2125         bool mask_full = !mask || *mask == OVS_BE64_MAX;
2126
2127         ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
2128         if (!mask_full) { /* Partially masked. */
2129             ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
2130         }
2131         ds_put_char(ds, ',');
2132     }
2133 }
2134
2135 static void
2136 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
2137             const ovs_be32 *mask, bool verbose)
2138 {
2139     bool mask_empty = mask && !*mask;
2140
2141     if (verbose || !mask_empty) {
2142         bool mask_full = !mask || *mask == OVS_BE32_MAX;
2143
2144         ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
2145         if (!mask_full) { /* Partially masked. */
2146             ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
2147         }
2148         ds_put_char(ds, ',');
2149     }
2150 }
2151
2152 static void
2153 format_in6_addr(struct ds *ds, const char *name,
2154                 const struct in6_addr *key,
2155                 const struct in6_addr *mask,
2156                 bool verbose)
2157 {
2158     char buf[INET6_ADDRSTRLEN];
2159     bool mask_empty = mask && ipv6_mask_is_any(mask);
2160
2161     if (verbose || !mask_empty) {
2162         bool mask_full = !mask || ipv6_mask_is_exact(mask);
2163
2164         inet_ntop(AF_INET6, key, buf, sizeof buf);
2165         ds_put_format(ds, "%s=%s", name, buf);
2166         if (!mask_full) { /* Partially masked. */
2167             inet_ntop(AF_INET6, mask, buf, sizeof buf);
2168             ds_put_format(ds, "/%s", buf);
2169         }
2170         ds_put_char(ds, ',');
2171     }
2172 }
2173
2174 static void
2175 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
2176             const ovs_be32 (*mask_)[4], bool verbose)
2177 {
2178     format_in6_addr(ds, name,
2179                     (const struct in6_addr *)key_,
2180                     mask_ ? (const struct in6_addr *)*mask_ : NULL,
2181                     verbose);
2182 }
2183
2184 static void
2185 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
2186                   const ovs_be32 *mask, bool verbose)
2187 {
2188     bool mask_empty = mask && !*mask;
2189
2190     if (verbose || !mask_empty) {
2191         bool mask_full = !mask
2192             || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
2193
2194         ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
2195         if (!mask_full) { /* Partially masked. */
2196             ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
2197         }
2198         ds_put_char(ds, ',');
2199     }
2200 }
2201
2202 static void
2203 format_u8x(struct ds *ds, const char *name, uint8_t key,
2204            const uint8_t *mask, bool verbose)
2205 {
2206     bool mask_empty = mask && !*mask;
2207
2208     if (verbose || !mask_empty) {
2209         bool mask_full = !mask || *mask == UINT8_MAX;
2210
2211         ds_put_format(ds, "%s=%#"PRIx8, name, key);
2212         if (!mask_full) { /* Partially masked. */
2213             ds_put_format(ds, "/%#"PRIx8, *mask);
2214         }
2215         ds_put_char(ds, ',');
2216     }
2217 }
2218
2219 static void
2220 format_u8u(struct ds *ds, const char *name, uint8_t key,
2221            const uint8_t *mask, bool verbose)
2222 {
2223     bool mask_empty = mask && !*mask;
2224
2225     if (verbose || !mask_empty) {
2226         bool mask_full = !mask || *mask == UINT8_MAX;
2227
2228         ds_put_format(ds, "%s=%"PRIu8, name, key);
2229         if (!mask_full) { /* Partially masked. */
2230             ds_put_format(ds, "/%#"PRIx8, *mask);
2231         }
2232         ds_put_char(ds, ',');
2233     }
2234 }
2235
2236 static void
2237 format_be16(struct ds *ds, const char *name, ovs_be16 key,
2238             const ovs_be16 *mask, bool verbose)
2239 {
2240     bool mask_empty = mask && !*mask;
2241
2242     if (verbose || !mask_empty) {
2243         bool mask_full = !mask || *mask == OVS_BE16_MAX;
2244
2245         ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
2246         if (!mask_full) { /* Partially masked. */
2247             ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2248         }
2249         ds_put_char(ds, ',');
2250     }
2251 }
2252
2253 static void
2254 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
2255              const ovs_be16 *mask, bool verbose)
2256 {
2257     bool mask_empty = mask && !*mask;
2258
2259     if (verbose || !mask_empty) {
2260         bool mask_full = !mask || *mask == OVS_BE16_MAX;
2261
2262         ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
2263         if (!mask_full) { /* Partially masked. */
2264             ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2265         }
2266         ds_put_char(ds, ',');
2267     }
2268 }
2269
2270 static void
2271 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
2272                  const uint16_t *mask, bool verbose)
2273 {
2274     bool mask_empty = mask && !*mask;
2275
2276     if (verbose || !mask_empty) {
2277         ds_put_cstr(ds, name);
2278         ds_put_char(ds, '(');
2279         if (mask) {
2280             format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
2281                                 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
2282         } else { /* Fully masked. */
2283             format_flags(ds, flow_tun_flag_to_string, key, '|');
2284         }
2285         ds_put_cstr(ds, "),");
2286     }
2287 }
2288
2289 static bool
2290 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
2291                const struct attr_len_tbl tbl[], int max_len, bool need_key)
2292 {
2293     int expected_len;
2294
2295     expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
2296     if (expected_len != ATTR_LEN_VARIABLE &&
2297         expected_len != ATTR_LEN_NESTED) {
2298
2299         bool bad_key_len = nl_attr_get_size(a) != expected_len;
2300         bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
2301
2302         if (bad_key_len || bad_mask_len) {
2303             if (need_key) {
2304                 ds_put_format(ds, "key%u", nl_attr_type(a));
2305             }
2306             if (bad_key_len) {
2307                 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
2308                               nl_attr_get_size(a), expected_len);
2309             }
2310             format_generic_odp_key(a, ds);
2311             if (ma) {
2312                 ds_put_char(ds, '/');
2313                 if (bad_mask_len) {
2314                     ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
2315                                   nl_attr_get_size(ma), expected_len);
2316                 }
2317                 format_generic_odp_key(ma, ds);
2318             }
2319             ds_put_char(ds, ')');
2320             return false;
2321         }
2322     }
2323
2324     return true;
2325 }
2326
2327 static void
2328 format_unknown_key(struct ds *ds, const struct nlattr *a,
2329                    const struct nlattr *ma)
2330 {
2331     ds_put_format(ds, "key%u(", nl_attr_type(a));
2332     format_generic_odp_key(a, ds);
2333     if (ma && !odp_mask_attr_is_exact(ma)) {
2334         ds_put_char(ds, '/');
2335         format_generic_odp_key(ma, ds);
2336     }
2337     ds_put_cstr(ds, "),");
2338 }
2339
2340 static void
2341 format_odp_tun_vxlan_opt(const struct nlattr *attr,
2342                          const struct nlattr *mask_attr, struct ds *ds,
2343                          bool verbose)
2344 {
2345     unsigned int left;
2346     const struct nlattr *a;
2347     struct ofpbuf ofp;
2348
2349     ofpbuf_init(&ofp, 100);
2350     NL_NESTED_FOR_EACH(a, left, attr) {
2351         uint16_t type = nl_attr_type(a);
2352         const struct nlattr *ma = NULL;
2353
2354         if (mask_attr) {
2355             ma = nl_attr_find__(nl_attr_get(mask_attr),
2356                                 nl_attr_get_size(mask_attr), type);
2357             if (!ma) {
2358                 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
2359                                                 OVS_VXLAN_EXT_MAX,
2360                                                 &ofp, a);
2361             }
2362         }
2363
2364         if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
2365                             OVS_VXLAN_EXT_MAX, true)) {
2366             continue;
2367         }
2368
2369         switch (type) {
2370         case OVS_VXLAN_EXT_GBP: {
2371             uint32_t key = nl_attr_get_u32(a);
2372             ovs_be16 id, id_mask;
2373             uint8_t flags, flags_mask;
2374
2375             id = htons(key & 0xFFFF);
2376             flags = (key >> 16) & 0xFF;
2377             if (ma) {
2378                 uint32_t mask = nl_attr_get_u32(ma);
2379                 id_mask = htons(mask & 0xFFFF);
2380                 flags_mask = (mask >> 16) & 0xFF;
2381             }
2382
2383             ds_put_cstr(ds, "gbp(");
2384             format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
2385             format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
2386             ds_chomp(ds, ',');
2387             ds_put_cstr(ds, "),");
2388             break;
2389         }
2390
2391         default:
2392             format_unknown_key(ds, a, ma);
2393         }
2394         ofpbuf_clear(&ofp);
2395     }
2396
2397     ds_chomp(ds, ',');
2398     ofpbuf_uninit(&ofp);
2399 }
2400
2401 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2402
2403 static void
2404 format_geneve_opts(const struct geneve_opt *opt,
2405                    const struct geneve_opt *mask, int opts_len,
2406                    struct ds *ds, bool verbose)
2407 {
2408     while (opts_len > 0) {
2409         unsigned int len;
2410         uint8_t data_len, data_len_mask;
2411
2412         if (opts_len < sizeof *opt) {
2413             ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
2414                           opts_len, sizeof *opt);
2415             return;
2416         }
2417
2418         data_len = opt->length * 4;
2419         if (mask) {
2420             if (mask->length == 0x1f) {
2421                 data_len_mask = UINT8_MAX;
2422             } else {
2423                 data_len_mask = mask->length;
2424             }
2425         }
2426         len = sizeof *opt + data_len;
2427         if (len > opts_len) {
2428             ds_put_format(ds, "opt len %u greater than remaining %u",
2429                           len, opts_len);
2430             return;
2431         }
2432
2433         ds_put_char(ds, '{');
2434         format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
2435                     verbose);
2436         format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
2437         format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
2438         if (data_len &&
2439             (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
2440             ds_put_hex(ds, opt + 1, data_len);
2441             if (mask && !is_all_ones(mask + 1, data_len)) {
2442                 ds_put_char(ds, '/');
2443                 ds_put_hex(ds, mask + 1, data_len);
2444             }
2445         } else {
2446             ds_chomp(ds, ',');
2447         }
2448         ds_put_char(ds, '}');
2449
2450         opt += len / sizeof(*opt);
2451         if (mask) {
2452             mask += len / sizeof(*opt);
2453         }
2454         opts_len -= len;
2455     };
2456 }
2457
2458 static void
2459 format_odp_tun_geneve(const struct nlattr *attr,
2460                       const struct nlattr *mask_attr, struct ds *ds,
2461                       bool verbose)
2462 {
2463     int opts_len = nl_attr_get_size(attr);
2464     const struct geneve_opt *opt = nl_attr_get(attr);
2465     const struct geneve_opt *mask = mask_attr ?
2466                                     nl_attr_get(mask_attr) : NULL;
2467
2468     if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
2469         ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
2470                       nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
2471         return;
2472     }
2473
2474     format_geneve_opts(opt, mask, opts_len, ds, verbose);
2475 }
2476
2477 static void
2478 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
2479                     struct ds *ds, bool verbose)
2480 {
2481     unsigned int left;
2482     const struct nlattr *a;
2483     uint16_t flags = 0;
2484     uint16_t mask_flags = 0;
2485     struct ofpbuf ofp;
2486
2487     ofpbuf_init(&ofp, 100);
2488     NL_NESTED_FOR_EACH(a, left, attr) {
2489         enum ovs_tunnel_key_attr type = nl_attr_type(a);
2490         const struct nlattr *ma = NULL;
2491
2492         if (mask_attr) {
2493             ma = nl_attr_find__(nl_attr_get(mask_attr),
2494                                 nl_attr_get_size(mask_attr), type);
2495             if (!ma) {
2496                 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
2497                                                 OVS_TUNNEL_KEY_ATTR_MAX,
2498                                                 &ofp, a);
2499             }
2500         }
2501
2502         if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
2503                             OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2504             continue;
2505         }
2506
2507         switch (type) {
2508         case OVS_TUNNEL_KEY_ATTR_ID:
2509             format_be64(ds, "tun_id", nl_attr_get_be64(a),
2510                         ma ? nl_attr_get(ma) : NULL, verbose);
2511             flags |= FLOW_TNL_F_KEY;
2512             if (ma) {
2513                 mask_flags |= FLOW_TNL_F_KEY;
2514             }
2515             break;
2516         case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2517             format_ipv4(ds, "src", nl_attr_get_be32(a),
2518                         ma ? nl_attr_get(ma) : NULL, verbose);
2519             break;
2520         case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2521             format_ipv4(ds, "dst", nl_attr_get_be32(a),
2522                         ma ? nl_attr_get(ma) : NULL, verbose);
2523             break;
2524         case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
2525             struct in6_addr ipv6_src;
2526             ipv6_src = nl_attr_get_in6_addr(a);
2527             format_in6_addr(ds, "ipv6_src", &ipv6_src,
2528                             ma ? nl_attr_get(ma) : NULL, verbose);
2529             break;
2530         }
2531         case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
2532             struct in6_addr ipv6_dst;
2533             ipv6_dst = nl_attr_get_in6_addr(a);
2534             format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
2535                             ma ? nl_attr_get(ma) : NULL, verbose);
2536             break;
2537         }
2538         case OVS_TUNNEL_KEY_ATTR_TOS:
2539             format_u8x(ds, "tos", nl_attr_get_u8(a),
2540                        ma ? nl_attr_get(ma) : NULL, verbose);
2541             break;
2542         case OVS_TUNNEL_KEY_ATTR_TTL:
2543             format_u8u(ds, "ttl", nl_attr_get_u8(a),
2544                        ma ? nl_attr_get(ma) : NULL, verbose);
2545             break;
2546         case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2547             flags |= FLOW_TNL_F_DONT_FRAGMENT;
2548             break;
2549         case OVS_TUNNEL_KEY_ATTR_CSUM:
2550             flags |= FLOW_TNL_F_CSUM;
2551             break;
2552         case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2553             format_be16(ds, "tp_src", nl_attr_get_be16(a),
2554                         ma ? nl_attr_get(ma) : NULL, verbose);
2555             break;
2556         case OVS_TUNNEL_KEY_ATTR_TP_DST:
2557             format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2558                         ma ? nl_attr_get(ma) : NULL, verbose);
2559             break;
2560         case OVS_TUNNEL_KEY_ATTR_OAM:
2561             flags |= FLOW_TNL_F_OAM;
2562             break;
2563         case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2564             ds_put_cstr(ds, "vxlan(");
2565             format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2566             ds_put_cstr(ds, "),");
2567             break;
2568         case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2569             ds_put_cstr(ds, "geneve(");
2570             format_odp_tun_geneve(a, ma, ds, verbose);
2571             ds_put_cstr(ds, "),");
2572             break;
2573         case __OVS_TUNNEL_KEY_ATTR_MAX:
2574         default:
2575             format_unknown_key(ds, a, ma);
2576         }
2577         ofpbuf_clear(&ofp);
2578     }
2579
2580     /* Flags can have a valid mask even if the attribute is not set, so
2581      * we need to collect these separately. */
2582     if (mask_attr) {
2583         NL_NESTED_FOR_EACH(a, left, mask_attr) {
2584             switch (nl_attr_type(a)) {
2585             case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2586                 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2587                 break;
2588             case OVS_TUNNEL_KEY_ATTR_CSUM:
2589                 mask_flags |= FLOW_TNL_F_CSUM;
2590                 break;
2591             case OVS_TUNNEL_KEY_ATTR_OAM:
2592                 mask_flags |= FLOW_TNL_F_OAM;
2593                 break;
2594             }
2595         }
2596     }
2597
2598     format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2599                      verbose);
2600     ds_chomp(ds, ',');
2601     ofpbuf_uninit(&ofp);
2602 }
2603
2604 static const char *
2605 odp_ct_state_to_string(uint32_t flag)
2606 {
2607     switch (flag) {
2608     case OVS_CS_F_REPLY_DIR:
2609         return "rpl";
2610     case OVS_CS_F_TRACKED:
2611         return "trk";
2612     case OVS_CS_F_NEW:
2613         return "new";
2614     case OVS_CS_F_ESTABLISHED:
2615         return "est";
2616     case OVS_CS_F_RELATED:
2617         return "rel";
2618     case OVS_CS_F_INVALID:
2619         return "inv";
2620     default:
2621         return NULL;
2622     }
2623 }
2624
2625 static void
2626 format_frag(struct ds *ds, const char *name, uint8_t key,
2627             const uint8_t *mask, bool verbose)
2628 {
2629     bool mask_empty = mask && !*mask;
2630
2631     /* ODP frag is an enumeration field; partial masks are not meaningful. */
2632     if (verbose || !mask_empty) {
2633         bool mask_full = !mask || *mask == UINT8_MAX;
2634
2635         if (!mask_full) { /* Partially masked. */
2636             ds_put_format(ds, "error: partial mask not supported for frag (%#"
2637                           PRIx8"),", *mask);
2638         } else {
2639             ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2640         }
2641     }
2642 }
2643
2644 static bool
2645 mask_empty(const struct nlattr *ma)
2646 {
2647     const void *mask;
2648     size_t n;
2649
2650     if (!ma) {
2651         return true;
2652     }
2653     mask = nl_attr_get(ma);
2654     n = nl_attr_get_size(ma);
2655
2656     return is_all_zeros(mask, n);
2657 }
2658
2659 static void
2660 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2661                     const struct hmap *portno_names, struct ds *ds,
2662                     bool verbose)
2663 {
2664     enum ovs_key_attr attr = nl_attr_type(a);
2665     char namebuf[OVS_KEY_ATTR_BUFSIZE];
2666     bool is_exact;
2667
2668     is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2669
2670     ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2671
2672     if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2673                         OVS_KEY_ATTR_MAX, false)) {
2674         return;
2675     }
2676
2677     ds_put_char(ds, '(');
2678     switch (attr) {
2679     case OVS_KEY_ATTR_ENCAP:
2680         if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2681             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2682                             nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2683                             verbose);
2684         } else if (nl_attr_get_size(a)) {
2685             odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2686                             ds, verbose);
2687         }
2688         break;
2689
2690     case OVS_KEY_ATTR_PRIORITY:
2691     case OVS_KEY_ATTR_SKB_MARK:
2692     case OVS_KEY_ATTR_DP_HASH:
2693     case OVS_KEY_ATTR_RECIRC_ID:
2694         ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2695         if (!is_exact) {
2696             ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2697         }
2698         break;
2699
2700     case OVS_KEY_ATTR_CT_MARK:
2701         if (verbose || !mask_empty(ma)) {
2702             ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2703             if (!is_exact) {
2704                 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2705             }
2706         }
2707         break;
2708
2709     case OVS_KEY_ATTR_CT_STATE:
2710         if (verbose) {
2711                 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2712                 if (!is_exact) {
2713                     ds_put_format(ds, "/%#"PRIx32,
2714                                   mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
2715                 }
2716         } else if (!is_exact) {
2717             format_flags_masked(ds, NULL, odp_ct_state_to_string,
2718                                 nl_attr_get_u32(a),
2719                                 mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
2720                                 UINT32_MAX);
2721         } else {
2722             format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
2723         }
2724         break;
2725
2726     case OVS_KEY_ATTR_CT_ZONE:
2727         if (verbose || !mask_empty(ma)) {
2728             ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
2729             if (!is_exact) {
2730                 ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
2731             }
2732         }
2733         break;
2734
2735     case OVS_KEY_ATTR_CT_LABELS: {
2736         const ovs_u128 *value = nl_attr_get(a);
2737         const ovs_u128 *mask = ma ? nl_attr_get(ma) : NULL;
2738
2739         format_u128(ds, value, mask, verbose);
2740         break;
2741     }
2742
2743     case OVS_KEY_ATTR_TUNNEL:
2744         format_odp_tun_attr(a, ma, ds, verbose);
2745         break;
2746
2747     case OVS_KEY_ATTR_IN_PORT:
2748         if (portno_names && verbose && is_exact) {
2749             char *name = odp_portno_names_get(portno_names,
2750                             u32_to_odp(nl_attr_get_u32(a)));
2751             if (name) {
2752                 ds_put_format(ds, "%s", name);
2753             } else {
2754                 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2755             }
2756         } else {
2757             ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2758             if (!is_exact) {
2759                 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2760             }
2761         }
2762         break;
2763
2764     case OVS_KEY_ATTR_ETHERNET: {
2765         const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2766         const struct ovs_key_ethernet *key = nl_attr_get(a);
2767
2768         format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2769         format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2770         ds_chomp(ds, ',');
2771         break;
2772     }
2773     case OVS_KEY_ATTR_VLAN:
2774         format_vlan_tci(ds, nl_attr_get_be16(a),
2775                         ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2776         break;
2777
2778     case OVS_KEY_ATTR_MPLS: {
2779         const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2780         const struct ovs_key_mpls *mpls_mask = NULL;
2781         size_t size = nl_attr_get_size(a);
2782
2783         if (!size || size % sizeof *mpls_key) {
2784             ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2785             return;
2786         }
2787         if (!is_exact) {
2788             mpls_mask = nl_attr_get(ma);
2789             if (size != nl_attr_get_size(ma)) {
2790                 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2791                               "mask length %"PRIuSIZE")",
2792                               size, nl_attr_get_size(ma));
2793                 return;
2794             }
2795         }
2796         format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2797         break;
2798     }
2799     case OVS_KEY_ATTR_ETHERTYPE:
2800         ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2801         if (!is_exact) {
2802             ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2803         }
2804         break;
2805
2806     case OVS_KEY_ATTR_IPV4: {
2807         const struct ovs_key_ipv4 *key = nl_attr_get(a);
2808         const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2809
2810         format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2811         format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2812         format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2813                       verbose);
2814         format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2815         format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2816         format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2817                     verbose);
2818         ds_chomp(ds, ',');
2819         break;
2820     }
2821     case OVS_KEY_ATTR_IPV6: {
2822         const struct ovs_key_ipv6 *key = nl_attr_get(a);
2823         const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2824
2825         format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2826         format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2827         format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2828                           verbose);
2829         format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2830                       verbose);
2831         format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2832                       verbose);
2833         format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2834                       verbose);
2835         format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2836                     verbose);
2837         ds_chomp(ds, ',');
2838         break;
2839     }
2840         /* These have the same structure and format. */
2841     case OVS_KEY_ATTR_TCP:
2842     case OVS_KEY_ATTR_UDP:
2843     case OVS_KEY_ATTR_SCTP: {
2844         const struct ovs_key_tcp *key = nl_attr_get(a);
2845         const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2846
2847         format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2848         format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2849         ds_chomp(ds, ',');
2850         break;
2851     }
2852     case OVS_KEY_ATTR_TCP_FLAGS:
2853         if (!is_exact) {
2854             format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2855                                 ntohs(nl_attr_get_be16(a)),
2856                                 TCP_FLAGS(nl_attr_get_be16(ma)),
2857                                 TCP_FLAGS(OVS_BE16_MAX));
2858         } else {
2859             format_flags(ds, packet_tcp_flag_to_string,
2860                          ntohs(nl_attr_get_be16(a)), '|');
2861         }
2862         break;
2863
2864     case OVS_KEY_ATTR_ICMP: {
2865         const struct ovs_key_icmp *key = nl_attr_get(a);
2866         const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2867
2868         format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2869         format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2870         ds_chomp(ds, ',');
2871         break;
2872     }
2873     case OVS_KEY_ATTR_ICMPV6: {
2874         const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2875         const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2876
2877         format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2878                    verbose);
2879         format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2880                    verbose);
2881         ds_chomp(ds, ',');
2882         break;
2883     }
2884     case OVS_KEY_ATTR_ARP: {
2885         const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2886         const struct ovs_key_arp *key = nl_attr_get(a);
2887
2888         format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2889         format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2890         format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2891         format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2892         format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2893         ds_chomp(ds, ',');
2894         break;
2895     }
2896     case OVS_KEY_ATTR_ND: {
2897         const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2898         const struct ovs_key_nd *key = nl_attr_get(a);
2899
2900         format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2901                     verbose);
2902         format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2903         format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2904
2905         ds_chomp(ds, ',');
2906         break;
2907     }
2908     case OVS_KEY_ATTR_UNSPEC:
2909     case __OVS_KEY_ATTR_MAX:
2910     default:
2911         format_generic_odp_key(a, ds);
2912         if (!is_exact) {
2913             ds_put_char(ds, '/');
2914             format_generic_odp_key(ma, ds);
2915         }
2916         break;
2917     }
2918     ds_put_char(ds, ')');
2919 }
2920
2921 static struct nlattr *
2922 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2923                            struct ofpbuf *ofp, const struct nlattr *key)
2924 {
2925     const struct nlattr *a;
2926     unsigned int left;
2927     int type = nl_attr_type(key);
2928     int size = nl_attr_get_size(key);
2929
2930     if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2931         nl_msg_put_unspec_zero(ofp, type, size);
2932     } else {
2933         size_t nested_mask;
2934
2935         if (tbl[type].next) {
2936             tbl = tbl[type].next;
2937             max = tbl[type].next_max;
2938         }
2939
2940         nested_mask = nl_msg_start_nested(ofp, type);
2941         NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2942             generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2943         }
2944         nl_msg_end_nested(ofp, nested_mask);
2945     }
2946
2947     return ofp->base;
2948 }
2949
2950 static void
2951 format_u128(struct ds *ds, const ovs_u128 *key, const ovs_u128 *mask,
2952             bool verbose)
2953 {
2954     if (verbose || (mask && !ovs_u128_is_zero(mask))) {
2955         ovs_be128 value;
2956
2957         value = hton128(*key);
2958         ds_put_hex(ds, &value, sizeof value);
2959         if (mask && !(ovs_u128_is_ones(mask))) {
2960             value = hton128(*mask);
2961             ds_put_char(ds, '/');
2962             ds_put_hex(ds, &value, sizeof value);
2963         }
2964     }
2965 }
2966
2967 static int
2968 scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
2969 {
2970     char *s = CONST_CAST(char *, s_);
2971     ovs_be128 be_value;
2972     ovs_be128 be_mask;
2973
2974     if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
2975         *value = ntoh128(be_value);
2976
2977         if (mask) {
2978             int n;
2979
2980             if (ovs_scan(s, "/%n", &n)) {
2981                 int error;
2982
2983                 s += n;
2984                 error = parse_int_string(s, (uint8_t *)&be_mask,
2985                                          sizeof be_mask, &s);
2986                 if (error) {
2987                     return error;
2988                 }
2989                 *mask = ntoh128(be_mask);
2990             } else {
2991                 *mask = OVS_U128_MAX;
2992             }
2993         }
2994         return s - s_;
2995     }
2996
2997     return 0;
2998 }
2999
3000 int
3001 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
3002 {
3003     const char *s = s_;
3004
3005     if (ovs_scan(s, "ufid:")) {
3006         s += 5;
3007
3008         if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
3009             return -EINVAL;
3010         }
3011         s += UUID_LEN;
3012
3013         return s - s_;
3014     }
3015
3016     return 0;
3017 }
3018
3019 void
3020 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
3021 {
3022     ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
3023 }
3024
3025 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3026  * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3027  * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3028  * non-null and 'verbose' is true, translates odp port number to its name. */
3029 void
3030 odp_flow_format(const struct nlattr *key, size_t key_len,
3031                 const struct nlattr *mask, size_t mask_len,
3032                 const struct hmap *portno_names, struct ds *ds, bool verbose)
3033 {
3034     if (key_len) {
3035         const struct nlattr *a;
3036         unsigned int left;
3037         bool has_ethtype_key = false;
3038         const struct nlattr *ma = NULL;
3039         struct ofpbuf ofp;
3040         bool first_field = true;
3041
3042         ofpbuf_init(&ofp, 100);
3043         NL_ATTR_FOR_EACH (a, left, key, key_len) {
3044             bool is_nested_attr;
3045             bool is_wildcard = false;
3046             int attr_type = nl_attr_type(a);
3047
3048             if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
3049                 has_ethtype_key = true;
3050             }
3051
3052             is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
3053                                               OVS_KEY_ATTR_MAX, attr_type) ==
3054                              ATTR_LEN_NESTED;
3055
3056             if (mask && mask_len) {
3057                 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
3058                 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
3059             }
3060
3061             if (verbose || !is_wildcard  || is_nested_attr) {
3062                 if (is_wildcard && !ma) {
3063                     ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
3064                                                     OVS_KEY_ATTR_MAX,
3065                                                     &ofp, a);
3066                 }
3067                 if (!first_field) {
3068                     ds_put_char(ds, ',');
3069                 }
3070                 format_odp_key_attr(a, ma, portno_names, ds, verbose);
3071                 first_field = false;
3072             }
3073             ofpbuf_clear(&ofp);
3074         }
3075         ofpbuf_uninit(&ofp);
3076
3077         if (left) {
3078             int i;
3079
3080             if (left == key_len) {
3081                 ds_put_cstr(ds, "<empty>");
3082             }
3083             ds_put_format(ds, ",***%u leftover bytes*** (", left);
3084             for (i = 0; i < left; i++) {
3085                 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
3086             }
3087             ds_put_char(ds, ')');
3088         }
3089         if (!has_ethtype_key) {
3090             ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
3091             if (ma) {
3092                 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
3093                               ntohs(nl_attr_get_be16(ma)));
3094             }
3095         }
3096     } else {
3097         ds_put_cstr(ds, "<empty>");
3098     }
3099 }
3100
3101 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3102  * OVS_KEY_ATTR_* attributes in 'key'. */
3103 void
3104 odp_flow_key_format(const struct nlattr *key,
3105                     size_t key_len, struct ds *ds)
3106 {
3107     odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
3108 }
3109
3110 static bool
3111 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
3112 {
3113     if (!strcasecmp(s, "no")) {
3114         *type = OVS_FRAG_TYPE_NONE;
3115     } else if (!strcasecmp(s, "first")) {
3116         *type = OVS_FRAG_TYPE_FIRST;
3117     } else if (!strcasecmp(s, "later")) {
3118         *type = OVS_FRAG_TYPE_LATER;
3119     } else {
3120         return false;
3121     }
3122     return true;
3123 }
3124
3125 /* Parsing. */
3126
3127 static int
3128 scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
3129 {
3130     int n;
3131
3132     if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
3133                  ETH_ADDR_SCAN_ARGS(*key), &n)) {
3134         int len = n;
3135
3136         if (mask) {
3137             if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
3138                          ETH_ADDR_SCAN_ARGS(*mask), &n)) {
3139                 len += n;
3140             } else {
3141                 memset(mask, 0xff, sizeof *mask);
3142             }
3143         }
3144         return len;
3145     }
3146     return 0;
3147 }
3148
3149 static int
3150 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
3151 {
3152     int n;
3153
3154     if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
3155         int len = n;
3156
3157         if (mask) {
3158             if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
3159                          IP_SCAN_ARGS(mask), &n)) {
3160                 len += n;
3161             } else {
3162                 *mask = OVS_BE32_MAX;
3163             }
3164         }
3165         return len;
3166     }
3167     return 0;
3168 }
3169
3170 static int
3171 scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
3172 {
3173     int n;
3174     char ipv6_s[IPV6_SCAN_LEN + 1];
3175
3176     if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
3177         && inet_pton(AF_INET6, ipv6_s, key) == 1) {
3178         int len = n;
3179
3180         if (mask) {
3181             if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
3182                 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
3183                 len += n;
3184             } else {
3185                 memset(mask, 0xff, sizeof *mask);
3186             }
3187         }
3188         return len;
3189     }
3190     return 0;
3191 }
3192
3193 static int
3194 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
3195 {
3196     return scan_in6_addr(s, key ? (struct in6_addr *) *key : NULL,
3197                          mask ? (struct in6_addr *) *mask : NULL);
3198 }
3199
3200 static int
3201 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3202 {
3203     int key_, mask_;
3204     int n;
3205
3206     if (ovs_scan(s, "%i%n", &key_, &n)
3207         && (key_ & ~IPV6_LABEL_MASK) == 0) {
3208         int len = n;
3209
3210         *key = htonl(key_);
3211         if (mask) {
3212             if (ovs_scan(s + len, "/%i%n", &mask_, &n)
3213                 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
3214                 len += n;
3215                 *mask = htonl(mask_);
3216             } else {
3217                 *mask = htonl(IPV6_LABEL_MASK);
3218             }
3219         }
3220         return len;
3221     }
3222     return 0;
3223 }
3224
3225 static int
3226 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
3227 {
3228     int n;
3229
3230     if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
3231         int len = n;
3232
3233         if (mask) {
3234             if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
3235                 len += n;
3236             } else {
3237                 *mask = UINT8_MAX;
3238             }
3239         }
3240         return len;
3241     }
3242     return 0;
3243 }
3244
3245 static int
3246 scan_u16(const char *s, uint16_t *key, uint16_t *mask)
3247 {
3248     int n;
3249
3250     if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
3251         int len = n;
3252
3253         if (mask) {
3254             if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
3255                 len += n;
3256             } else {
3257                 *mask = UINT16_MAX;
3258             }
3259         }
3260         return len;
3261     }
3262     return 0;
3263 }
3264
3265 static int
3266 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
3267 {
3268     int n;
3269
3270     if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3271         int len = n;
3272
3273         if (mask) {
3274             if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3275                 len += n;
3276             } else {
3277                 *mask = UINT32_MAX;
3278             }
3279         }
3280         return len;
3281     }
3282     return 0;
3283 }
3284
3285 static int
3286 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
3287 {
3288     uint16_t key_, mask_;
3289     int n;
3290
3291     if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3292         int len = n;
3293
3294         *key = htons(key_);
3295         if (mask) {
3296             if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3297                 len += n;
3298                 *mask = htons(mask_);
3299             } else {
3300                 *mask = OVS_BE16_MAX;
3301             }
3302         }
3303         return len;
3304     }
3305     return 0;
3306 }
3307
3308 static int
3309 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
3310 {
3311     uint64_t key_, mask_;
3312     int n;
3313
3314     if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
3315         int len = n;
3316
3317         *key = htonll(key_);
3318         if (mask) {
3319             if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
3320                 len += n;
3321                 *mask = htonll(mask_);
3322             } else {
3323                 *mask = OVS_BE64_MAX;
3324             }
3325         }
3326         return len;
3327     }
3328     return 0;
3329 }
3330
3331 static int
3332 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
3333 {
3334     uint32_t flags, fmask;
3335     int n;
3336
3337     n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
3338                         FLOW_TNL_F_MASK, mask ? &fmask : NULL);
3339     if (n >= 0 && s[n] == ')') {
3340         *key = flags;
3341         if (mask) {
3342             *mask = fmask;
3343         }
3344         return n + 1;
3345     }
3346     return 0;
3347 }
3348
3349 static int
3350 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
3351 {
3352     uint32_t flags, fmask;
3353     int n;
3354
3355     n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
3356                         TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
3357     if (n >= 0) {
3358         *key = htons(flags);
3359         if (mask) {
3360             *mask = htons(fmask);
3361         }
3362         return n;
3363     }
3364     return 0;
3365 }
3366
3367 static uint32_t
3368 ovs_to_odp_ct_state(uint8_t state)
3369 {
3370     uint32_t odp = 0;
3371
3372     if (state & CS_NEW) {
3373         odp |= OVS_CS_F_NEW;
3374     }
3375     if (state & CS_ESTABLISHED) {
3376         odp |= OVS_CS_F_ESTABLISHED;
3377     }
3378     if (state & CS_RELATED) {
3379         odp |= OVS_CS_F_RELATED;
3380     }
3381     if (state & CS_INVALID) {
3382         odp |= OVS_CS_F_INVALID;
3383     }
3384     if (state & CS_REPLY_DIR) {
3385         odp |= OVS_CS_F_REPLY_DIR;
3386     }
3387     if (state & CS_TRACKED) {
3388         odp |= OVS_CS_F_TRACKED;
3389     }
3390
3391     return odp;
3392 }
3393
3394 static uint8_t
3395 odp_to_ovs_ct_state(uint32_t flags)
3396 {
3397     uint32_t state = 0;
3398
3399     if (flags & OVS_CS_F_NEW) {
3400         state |= CS_NEW;
3401     }
3402     if (flags & OVS_CS_F_ESTABLISHED) {
3403         state |= CS_ESTABLISHED;
3404     }
3405     if (flags & OVS_CS_F_RELATED) {
3406         state |= CS_RELATED;
3407     }
3408     if (flags & OVS_CS_F_INVALID) {
3409         state |= CS_INVALID;
3410     }
3411     if (flags & OVS_CS_F_REPLY_DIR) {
3412         state |= CS_REPLY_DIR;
3413     }
3414     if (flags & OVS_CS_F_TRACKED) {
3415         state |= CS_TRACKED;
3416     }
3417
3418     return state;
3419 }
3420
3421 static int
3422 scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
3423 {
3424     uint32_t flags, fmask;
3425     int n;
3426
3427     n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
3428                     ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
3429                     mask ? &fmask : NULL);
3430
3431     if (n >= 0) {
3432         *key = flags;
3433         if (mask) {
3434             *mask = fmask;
3435         }
3436         return n;
3437     }
3438     return 0;
3439 }
3440
3441 static int
3442 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
3443 {
3444     int n;
3445     char frag[8];
3446     enum ovs_frag_type frag_type;
3447
3448     if (ovs_scan(s, "%7[a-z]%n", frag, &n)
3449         && ovs_frag_type_from_string(frag, &frag_type)) {
3450         int len = n;
3451
3452         *key = frag_type;
3453         if (mask) {
3454             *mask = UINT8_MAX;
3455         }
3456         return len;
3457     }
3458     return 0;
3459 }
3460
3461 static int
3462 scan_port(const char *s, uint32_t *key, uint32_t *mask,
3463           const struct simap *port_names)
3464 {
3465     int n;
3466
3467     if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3468         int len = n;
3469
3470         if (mask) {
3471             if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3472                 len += n;
3473             } else {
3474                 *mask = UINT32_MAX;
3475             }
3476         }
3477         return len;
3478     } else if (port_names) {
3479         const struct simap_node *node;
3480         int len;
3481
3482         len = strcspn(s, ")");
3483         node = simap_find_len(port_names, s, len);
3484         if (node) {
3485             *key = node->data;
3486
3487             if (mask) {
3488                 *mask = UINT32_MAX;
3489             }
3490             return len;
3491         }
3492     }
3493     return 0;
3494 }
3495
3496 /* Helper for vlan parsing. */
3497 struct ovs_key_vlan__ {
3498     ovs_be16 tci;
3499 };
3500
3501 static bool
3502 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
3503 {
3504     const uint16_t mask = ((1U << bits) - 1) << offset;
3505
3506     if (value >> bits) {
3507         return false;
3508     }
3509
3510     *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
3511     return true;
3512 }
3513
3514 static int
3515 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
3516              uint8_t offset)
3517 {
3518     uint16_t key_, mask_;
3519     int n;
3520
3521     if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3522         int len = n;
3523
3524         if (set_be16_bf(key, bits, offset, key_)) {
3525             if (mask) {
3526                 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3527                     len += n;
3528
3529                     if (!set_be16_bf(mask, bits, offset, mask_)) {
3530                         return 0;
3531                     }
3532                 } else {
3533                     *mask |= htons(((1U << bits) - 1) << offset);
3534                 }
3535             }
3536             return len;
3537         }
3538     }
3539     return 0;
3540 }
3541
3542 static int
3543 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
3544 {
3545     return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
3546 }
3547
3548 static int
3549 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
3550 {
3551     return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
3552 }
3553
3554 static int
3555 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
3556 {
3557     return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
3558 }
3559
3560 /* For MPLS. */
3561 static bool
3562 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
3563 {
3564     const uint32_t mask = ((1U << bits) - 1) << offset;
3565
3566     if (value >> bits) {
3567         return false;
3568     }
3569
3570     *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
3571     return true;
3572 }
3573
3574 static int
3575 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
3576              uint8_t offset)
3577 {
3578     uint32_t key_, mask_;
3579     int n;
3580
3581     if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
3582         int len = n;
3583
3584         if (set_be32_bf(key, bits, offset, key_)) {
3585             if (mask) {
3586                 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
3587                     len += n;
3588
3589                     if (!set_be32_bf(mask, bits, offset, mask_)) {
3590                         return 0;
3591                     }
3592                 } else {
3593                     *mask |= htonl(((1U << bits) - 1) << offset);
3594                 }
3595             }
3596             return len;
3597         }
3598     }
3599     return 0;
3600 }
3601
3602 static int
3603 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3604 {
3605     return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
3606 }
3607
3608 static int
3609 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
3610 {
3611     return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
3612 }
3613
3614 static int
3615 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
3616 {
3617     return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
3618 }
3619
3620 static int
3621 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
3622 {
3623     return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
3624 }
3625
3626 static int
3627 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
3628 {
3629     const char *s_base = s;
3630     ovs_be16 id = 0, id_mask = 0;
3631     uint8_t flags = 0, flags_mask = 0;
3632
3633     if (!strncmp(s, "id=", 3)) {
3634         s += 3;
3635         s += scan_be16(s, &id, mask ? &id_mask : NULL);
3636     }
3637
3638     if (s[0] == ',') {
3639         s++;
3640     }
3641     if (!strncmp(s, "flags=", 6)) {
3642         s += 6;
3643         s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
3644     }
3645
3646     if (!strncmp(s, "))", 2)) {
3647         s += 2;
3648
3649         *key = (flags << 16) | ntohs(id);
3650         if (mask) {
3651             *mask = (flags_mask << 16) | ntohs(id_mask);
3652         }
3653
3654         return s - s_base;
3655     }
3656
3657     return 0;
3658 }
3659
3660 static int
3661 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
3662 {
3663     const char *s_base = s;
3664     struct geneve_opt *opt = key->d;
3665     struct geneve_opt *opt_mask = mask ? mask->d : NULL;
3666     int len_remain = sizeof key->d;
3667
3668     while (s[0] == '{' && len_remain >= sizeof *opt) {
3669         int data_len = 0;
3670
3671         s++;
3672         len_remain -= sizeof *opt;
3673
3674         if (!strncmp(s, "class=", 6)) {
3675             s += 6;
3676             s += scan_be16(s, &opt->opt_class,
3677                            mask ? &opt_mask->opt_class : NULL);
3678         } else if (mask) {
3679             memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
3680         }
3681
3682         if (s[0] == ',') {
3683             s++;
3684         }
3685         if (!strncmp(s, "type=", 5)) {
3686             s += 5;
3687             s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
3688         } else if (mask) {
3689             memset(&opt_mask->type, 0, sizeof opt_mask->type);
3690         }
3691
3692         if (s[0] == ',') {
3693             s++;
3694         }
3695         if (!strncmp(s, "len=", 4)) {
3696             uint8_t opt_len, opt_len_mask;
3697             s += 4;
3698             s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
3699
3700             if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
3701                 return 0;
3702             }
3703             opt->length = opt_len / 4;
3704             if (mask) {
3705                 opt_mask->length = opt_len_mask;
3706             }
3707             data_len = opt_len;
3708         } else if (mask) {
3709             memset(&opt_mask->type, 0, sizeof opt_mask->type);
3710         }
3711
3712         if (s[0] == ',') {
3713             s++;
3714         }
3715         if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
3716             return 0;
3717         }
3718
3719         if (mask) {
3720             if (s[0] == '/') {
3721                 s++;
3722                 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
3723                                      data_len, (char **)&s)) {
3724                     return 0;
3725                 }
3726             }
3727             opt_mask->r1 = 0;
3728             opt_mask->r2 = 0;
3729             opt_mask->r3 = 0;
3730         }
3731
3732         if (s[0] == '}') {
3733             s++;
3734             opt += 1 + data_len / 4;
3735             if (mask) {
3736                 opt_mask += 1 + data_len / 4;
3737             }
3738             len_remain -= data_len;
3739         }
3740     }
3741
3742     if (s[0] == ')') {
3743         int len = sizeof key->d - len_remain;
3744
3745         s++;
3746         key->len = len;
3747         if (mask) {
3748             mask->len = len;
3749         }
3750         return s - s_base;
3751     }
3752
3753     return 0;
3754 }
3755
3756 static void
3757 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3758 {
3759     const uint16_t *flags = data_;
3760
3761     if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3762         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3763     }
3764     if (*flags & FLOW_TNL_F_CSUM) {
3765         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3766     }
3767     if (*flags & FLOW_TNL_F_OAM) {
3768         nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3769     }
3770 }
3771
3772 static void
3773 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3774 {
3775     const uint32_t *gbp = data_;
3776
3777     if (*gbp) {
3778         size_t vxlan_opts_ofs;
3779
3780         vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3781         nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3782         nl_msg_end_nested(a, vxlan_opts_ofs);
3783     }
3784 }
3785
3786 static void
3787 geneve_to_attr(struct ofpbuf *a, const void *data_)
3788 {
3789     const struct geneve_scan *geneve = data_;
3790
3791     nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3792                       geneve->len);
3793 }
3794
3795 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC)                      \
3796     {                                                             \
3797         unsigned long call_fn = (unsigned long)FUNC;              \
3798         if (call_fn) {                                            \
3799             typedef void (*fn)(struct ofpbuf *, const void *);    \
3800             fn func = FUNC;                                       \
3801             func(BUF, &(DATA));                                   \
3802         } else {                                                  \
3803             nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3804         }                                                         \
3805     }
3806
3807 #define SCAN_IF(NAME)                           \
3808     if (strncmp(s, NAME, strlen(NAME)) == 0) {  \
3809         const char *start = s;                  \
3810         int len;                                \
3811                                                 \
3812         s += strlen(NAME)
3813
3814 /* Usually no special initialization is needed. */
3815 #define SCAN_BEGIN(NAME, TYPE)                  \
3816     SCAN_IF(NAME);                              \
3817         TYPE skey, smask;                       \
3818         memset(&skey, 0, sizeof skey);          \
3819         memset(&smask, 0, sizeof smask);        \
3820         do {                                    \
3821             len = 0;
3822
3823 /* Init as fully-masked as mask will not be scanned. */
3824 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE)     \
3825     SCAN_IF(NAME);                              \
3826         TYPE skey, smask;                       \
3827         memset(&skey, 0, sizeof skey);          \
3828         memset(&smask, 0xff, sizeof smask);     \
3829         do {                                    \
3830             len = 0;
3831
3832 /* VLAN needs special initialization. */
3833 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT)  \
3834     SCAN_IF(NAME);                                        \
3835         TYPE skey = KEY_INIT;                       \
3836         TYPE smask = MASK_INIT;                     \
3837         do {                                        \
3838             len = 0;
3839
3840 /* Scan unnamed entry as 'TYPE' */
3841 #define SCAN_TYPE(TYPE, KEY, MASK)              \
3842     len = scan_##TYPE(s, KEY, MASK);            \
3843     if (len == 0) {                             \
3844         return -EINVAL;                         \
3845     }                                           \
3846     s += len
3847
3848 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3849 #define SCAN_FIELD(NAME, TYPE, FIELD)                                   \
3850     if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
3851         s += strlen(NAME);                                              \
3852         SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL);       \
3853         continue;                                                       \
3854     }
3855
3856 #define SCAN_FINISH()                           \
3857         } while (*s++ == ',' && len != 0);      \
3858         if (s[-1] != ')') {                     \
3859             return -EINVAL;                     \
3860         }
3861
3862 #define SCAN_FINISH_SINGLE()                    \
3863         } while (false);                        \
3864         if (*s++ != ')') {                      \
3865             return -EINVAL;                     \
3866         }
3867
3868 /* Beginning of nested attribute. */
3869 #define SCAN_BEGIN_NESTED(NAME, ATTR)                      \
3870     SCAN_IF(NAME);                                         \
3871         size_t key_offset, mask_offset;                    \
3872         key_offset = nl_msg_start_nested(key, ATTR);       \
3873         if (mask) {                                        \
3874             mask_offset = nl_msg_start_nested(mask, ATTR); \
3875         }                                                  \
3876         do {                                               \
3877             len = 0;
3878
3879 #define SCAN_END_NESTED()                               \
3880         SCAN_FINISH();                                  \
3881         nl_msg_end_nested(key, key_offset);             \
3882         if (mask) {                                     \
3883             nl_msg_end_nested(mask, mask_offset);       \
3884         }                                               \
3885         return s - start;                               \
3886     }
3887
3888 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC)  \
3889     if (strncmp(s, NAME, strlen(NAME)) == 0) {                \
3890         TYPE skey, smask;                                     \
3891         memset(&skey, 0, sizeof skey);                        \
3892         memset(&smask, 0xff, sizeof smask);                   \
3893         s += strlen(NAME);                                    \
3894         SCAN_TYPE(SCAN_AS, &skey, &smask);                    \
3895         SCAN_PUT(ATTR, FUNC);                                 \
3896         continue;                                             \
3897     }
3898
3899 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR)  \
3900         SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3901
3902 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC)  \
3903         SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3904
3905 #define SCAN_PUT(ATTR, FUNC)                            \
3906         if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3907             SCAN_PUT_ATTR(key, ATTR, skey, FUNC);       \
3908             if (mask) {                                 \
3909                 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3910             }                                           \
3911         }
3912
3913 #define SCAN_END(ATTR)                                  \
3914         SCAN_FINISH();                                  \
3915         SCAN_PUT(ATTR, NULL);                           \
3916         return s - start;                               \
3917     }
3918
3919 #define SCAN_END_SINGLE(ATTR)                           \
3920         SCAN_FINISH_SINGLE();                           \
3921         SCAN_PUT(ATTR, NULL);                           \
3922         return s - start;                               \
3923     }
3924
3925 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR)       \
3926     SCAN_BEGIN(NAME, TYPE) {                         \
3927         SCAN_TYPE(SCAN_AS, &skey, &smask);           \
3928     } SCAN_END_SINGLE(ATTR)
3929
3930 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3931     SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) {                   \
3932         SCAN_TYPE(SCAN_AS, &skey, NULL);                    \
3933     } SCAN_END_SINGLE(ATTR)
3934
3935 /* scan_port needs one extra argument. */
3936 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR)  \
3937     SCAN_BEGIN(NAME, TYPE) {                            \
3938         len = scan_port(s, &skey, &smask, port_names);  \
3939         if (len == 0) {                                 \
3940             return -EINVAL;                             \
3941         }                                               \
3942         s += len;                                       \
3943     } SCAN_END_SINGLE(ATTR)
3944
3945 static int
3946 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3947                         struct ofpbuf *key, struct ofpbuf *mask)
3948 {
3949     ovs_u128 ufid;
3950     int len;
3951
3952     /* Skip UFID. */
3953     len = odp_ufid_from_string(s, &ufid);
3954     if (len) {
3955         return len;
3956     }
3957
3958     SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3959     SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3960     SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3961                              OVS_KEY_ATTR_RECIRC_ID);
3962     SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3963
3964     SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
3965     SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
3966     SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
3967     SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
3968
3969     SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3970         SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3971         SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3972         SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3973         SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
3974         SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
3975         SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3976         SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3977         SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3978         SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3979         SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3980         SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3981                                geneve_to_attr);
3982         SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3983     } SCAN_END_NESTED();
3984
3985     SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3986
3987     SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3988         SCAN_FIELD("src=", eth, eth_src);
3989         SCAN_FIELD("dst=", eth, eth_dst);
3990     } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3991
3992     SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3993                     { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3994         SCAN_FIELD("vid=", vid, tci);
3995         SCAN_FIELD("pcp=", pcp, tci);
3996         SCAN_FIELD("cfi=", cfi, tci);
3997     } SCAN_END(OVS_KEY_ATTR_VLAN);
3998
3999     SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
4000
4001     SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
4002         SCAN_FIELD("label=", mpls_label, mpls_lse);
4003         SCAN_FIELD("tc=", mpls_tc, mpls_lse);
4004         SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
4005         SCAN_FIELD("bos=", mpls_bos, mpls_lse);
4006     } SCAN_END(OVS_KEY_ATTR_MPLS);
4007
4008     SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
4009         SCAN_FIELD("src=", ipv4, ipv4_src);
4010         SCAN_FIELD("dst=", ipv4, ipv4_dst);
4011         SCAN_FIELD("proto=", u8, ipv4_proto);
4012         SCAN_FIELD("tos=", u8, ipv4_tos);
4013         SCAN_FIELD("ttl=", u8, ipv4_ttl);
4014         SCAN_FIELD("frag=", frag, ipv4_frag);
4015     } SCAN_END(OVS_KEY_ATTR_IPV4);
4016
4017     SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
4018         SCAN_FIELD("src=", ipv6, ipv6_src);
4019         SCAN_FIELD("dst=", ipv6, ipv6_dst);
4020         SCAN_FIELD("label=", ipv6_label, ipv6_label);
4021         SCAN_FIELD("proto=", u8, ipv6_proto);
4022         SCAN_FIELD("tclass=", u8, ipv6_tclass);
4023         SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
4024         SCAN_FIELD("frag=", frag, ipv6_frag);
4025     } SCAN_END(OVS_KEY_ATTR_IPV6);
4026
4027     SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
4028         SCAN_FIELD("src=", be16, tcp_src);
4029         SCAN_FIELD("dst=", be16, tcp_dst);
4030     } SCAN_END(OVS_KEY_ATTR_TCP);
4031
4032     SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
4033
4034     SCAN_BEGIN("udp(", struct ovs_key_udp) {
4035         SCAN_FIELD("src=", be16, udp_src);
4036         SCAN_FIELD("dst=", be16, udp_dst);
4037     } SCAN_END(OVS_KEY_ATTR_UDP);
4038
4039     SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
4040         SCAN_FIELD("src=", be16, sctp_src);
4041         SCAN_FIELD("dst=", be16, sctp_dst);
4042     } SCAN_END(OVS_KEY_ATTR_SCTP);
4043
4044     SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
4045         SCAN_FIELD("type=", u8, icmp_type);
4046         SCAN_FIELD("code=", u8, icmp_code);
4047     } SCAN_END(OVS_KEY_ATTR_ICMP);
4048
4049     SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
4050         SCAN_FIELD("type=", u8, icmpv6_type);
4051         SCAN_FIELD("code=", u8, icmpv6_code);
4052     } SCAN_END(OVS_KEY_ATTR_ICMPV6);
4053
4054     SCAN_BEGIN("arp(", struct ovs_key_arp) {
4055         SCAN_FIELD("sip=", ipv4, arp_sip);
4056         SCAN_FIELD("tip=", ipv4, arp_tip);
4057         SCAN_FIELD("op=", be16, arp_op);
4058         SCAN_FIELD("sha=", eth, arp_sha);
4059         SCAN_FIELD("tha=", eth, arp_tha);
4060     } SCAN_END(OVS_KEY_ATTR_ARP);
4061
4062     SCAN_BEGIN("nd(", struct ovs_key_nd) {
4063         SCAN_FIELD("target=", ipv6, nd_target);
4064         SCAN_FIELD("sll=", eth, nd_sll);
4065         SCAN_FIELD("tll=", eth, nd_tll);
4066     } SCAN_END(OVS_KEY_ATTR_ND);
4067
4068     /* Encap open-coded. */
4069     if (!strncmp(s, "encap(", 6)) {
4070         const char *start = s;
4071         size_t encap, encap_mask = 0;
4072
4073         encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
4074         if (mask) {
4075             encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
4076         }
4077
4078         s += 6;
4079         for (;;) {
4080             int retval;
4081
4082             s += strspn(s, delimiters);
4083             if (!*s) {
4084                 return -EINVAL;
4085             } else if (*s == ')') {
4086                 break;
4087             }
4088
4089             retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4090             if (retval < 0) {
4091                 return retval;
4092             }
4093             s += retval;
4094         }
4095         s++;
4096
4097         nl_msg_end_nested(key, encap);
4098         if (mask) {
4099             nl_msg_end_nested(mask, encap_mask);
4100         }
4101
4102         return s - start;
4103     }
4104
4105     return -EINVAL;
4106 }
4107
4108 /* Parses the string representation of a datapath flow key, in the
4109  * format output by odp_flow_key_format().  Returns 0 if successful,
4110  * otherwise a positive errno value.  On success, the flow key is
4111  * appended to 'key' as a series of Netlink attributes.  On failure, no
4112  * data is appended to 'key'.  Either way, 'key''s data might be
4113  * reallocated.
4114  *
4115  * If 'port_names' is nonnull, it points to an simap that maps from a port name
4116  * to a port number.  (Port names may be used instead of port numbers in
4117  * in_port.)
4118  *
4119  * On success, the attributes appended to 'key' are individually syntactically
4120  * valid, but they may not be valid as a sequence.  'key' might, for example,
4121  * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
4122 int
4123 odp_flow_from_string(const char *s, const struct simap *port_names,
4124                      struct ofpbuf *key, struct ofpbuf *mask)
4125 {
4126     const size_t old_size = key->size;
4127     for (;;) {
4128         int retval;
4129
4130         s += strspn(s, delimiters);
4131         if (!*s) {
4132             return 0;
4133         }
4134
4135         retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4136         if (retval < 0) {
4137             key->size = old_size;
4138             return -retval;
4139         }
4140         s += retval;
4141     }
4142
4143     return 0;
4144 }
4145
4146 static uint8_t
4147 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
4148 {
4149     if (is_mask) {
4150         /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4151          * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4152          * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4153          * must use a zero mask for the netlink frag field, and all ones mask
4154          * otherwise. */
4155         return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
4156     }
4157     return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
4158         : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
4159         : OVS_FRAG_TYPE_FIRST;
4160 }
4161
4162 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
4163 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
4164 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
4165                          bool is_mask);
4166 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
4167                          bool is_mask);
4168 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
4169                          bool is_mask);
4170 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
4171                          bool is_mask);
4172 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
4173 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
4174 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
4175 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
4176
4177 /* These share the same layout. */
4178 union ovs_key_tp {
4179     struct ovs_key_tcp tcp;
4180     struct ovs_key_udp udp;
4181     struct ovs_key_sctp sctp;
4182 };
4183
4184 static void get_tp_key(const struct flow *, union ovs_key_tp *);
4185 static void put_tp_key(const union ovs_key_tp *, struct flow *);
4186
4187 static void
4188 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
4189                          bool export_mask, struct ofpbuf *buf)
4190 {
4191     struct ovs_key_ethernet *eth_key;
4192     size_t encap;
4193     const struct flow *flow = parms->flow;
4194     const struct flow *data = export_mask ? parms->mask : parms->flow;
4195
4196     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
4197
4198     if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) {
4199         tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
4200                         parms->key_buf);
4201     }
4202
4203     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
4204
4205     if (parms->support.ct_state) {
4206         nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4207                        ovs_to_odp_ct_state(data->ct_state));
4208     }
4209     if (parms->support.ct_zone) {
4210         nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
4211     }
4212     if (parms->support.ct_mark) {
4213         nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
4214     }
4215     if (parms->support.ct_label) {
4216         nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
4217                           sizeof(data->ct_label));
4218     }
4219     if (parms->support.recirc) {
4220         nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
4221         nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
4222     }
4223
4224     /* Add an ingress port attribute if this is a mask or 'odp_in_port'
4225      * is not the magical value "ODPP_NONE". */
4226     if (export_mask || parms->odp_in_port != ODPP_NONE) {
4227         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
4228     }
4229
4230     eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
4231                                        sizeof *eth_key);
4232     get_ethernet_key(data, eth_key);
4233
4234     if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
4235         if (export_mask) {
4236             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4237         } else {
4238             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
4239         }
4240         nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
4241         encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
4242         if (flow->vlan_tci == htons(0)) {
4243             goto unencap;
4244         }
4245     } else {
4246         encap = 0;
4247     }
4248
4249     if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4250         /* For backwards compatibility with kernels that don't support
4251          * wildcarding, the following convention is used to encode the
4252          * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4253          *
4254          *   key      mask    matches
4255          * -------- --------  -------
4256          *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
4257          *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
4258          *  <none>   0xffff   Any non-Ethernet II frame (except valid
4259          *                    802.3 SNAP packet with valid eth_type).
4260          */
4261         if (export_mask) {
4262             nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4263         }
4264         goto unencap;
4265     }
4266
4267     nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
4268
4269     if (flow->dl_type == htons(ETH_TYPE_IP)) {
4270         struct ovs_key_ipv4 *ipv4_key;
4271
4272         ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
4273                                             sizeof *ipv4_key);
4274         get_ipv4_key(data, ipv4_key, export_mask);
4275     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
4276         struct ovs_key_ipv6 *ipv6_key;
4277
4278         ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
4279                                             sizeof *ipv6_key);
4280         get_ipv6_key(data, ipv6_key, export_mask);
4281     } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
4282                flow->dl_type == htons(ETH_TYPE_RARP)) {
4283         struct ovs_key_arp *arp_key;
4284
4285         arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
4286                                            sizeof *arp_key);
4287         get_arp_key(data, arp_key);
4288     } else if (eth_type_mpls(flow->dl_type)) {
4289         struct ovs_key_mpls *mpls_key;
4290         int i, n;
4291
4292         n = flow_count_mpls_labels(flow, NULL);
4293         if (export_mask) {
4294             n = MIN(n, parms->support.max_mpls_depth);
4295         }
4296         mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
4297                                             n * sizeof *mpls_key);
4298         for (i = 0; i < n; i++) {
4299             mpls_key[i].mpls_lse = data->mpls_lse[i];
4300         }
4301     }
4302
4303     if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4304         if (flow->nw_proto == IPPROTO_TCP) {
4305             union ovs_key_tp *tcp_key;
4306
4307             tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
4308                                                sizeof *tcp_key);
4309             get_tp_key(data, tcp_key);
4310             if (data->tcp_flags) {
4311                 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
4312             }
4313         } else if (flow->nw_proto == IPPROTO_UDP) {
4314             union ovs_key_tp *udp_key;
4315
4316             udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
4317                                                sizeof *udp_key);
4318             get_tp_key(data, udp_key);
4319         } else if (flow->nw_proto == IPPROTO_SCTP) {
4320             union ovs_key_tp *sctp_key;
4321
4322             sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
4323                                                sizeof *sctp_key);
4324             get_tp_key(data, sctp_key);
4325         } else if (flow->dl_type == htons(ETH_TYPE_IP)
4326                 && flow->nw_proto == IPPROTO_ICMP) {
4327             struct ovs_key_icmp *icmp_key;
4328
4329             icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
4330                                                 sizeof *icmp_key);
4331             icmp_key->icmp_type = ntohs(data->tp_src);
4332             icmp_key->icmp_code = ntohs(data->tp_dst);
4333         } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
4334                 && flow->nw_proto == IPPROTO_ICMPV6) {
4335             struct ovs_key_icmpv6 *icmpv6_key;
4336
4337             icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
4338                                                   sizeof *icmpv6_key);
4339             icmpv6_key->icmpv6_type = ntohs(data->tp_src);
4340             icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
4341
4342             if (flow->tp_dst == htons(0)
4343                 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
4344                     || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
4345                 && (!export_mask || (data->tp_src == htons(0xffff)
4346                                      && data->tp_dst == htons(0xffff)))) {
4347
4348                 struct ovs_key_nd *nd_key;
4349
4350                 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
4351                                                     sizeof *nd_key);
4352                 memcpy(nd_key->nd_target, &data->nd_target,
4353                         sizeof nd_key->nd_target);
4354                 nd_key->nd_sll = data->arp_sha;
4355                 nd_key->nd_tll = data->arp_tha;
4356             }
4357         }
4358     }
4359
4360 unencap:
4361     if (encap) {
4362         nl_msg_end_nested(buf, encap);
4363     }
4364 }
4365
4366 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4367  *
4368  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4369  * capable of being expanded to allow for that much space. */
4370 void
4371 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
4372                        struct ofpbuf *buf)
4373 {
4374     odp_flow_key_from_flow__(parms, false, buf);
4375 }
4376
4377 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4378  * 'buf'.
4379  *
4380  * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4381  * capable of being expanded to allow for that much space. */
4382 void
4383 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
4384                        struct ofpbuf *buf)
4385 {
4386     odp_flow_key_from_flow__(parms, true, buf);
4387 }
4388
4389 /* Generate ODP flow key from the given packet metadata */
4390 void
4391 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
4392 {
4393     nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
4394
4395     if (flow_tnl_dst_is_set(&md->tunnel)) {
4396         tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
4397     }
4398
4399     nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
4400
4401     if (md->ct_state) {
4402         nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4403                        ovs_to_odp_ct_state(md->ct_state));
4404         if (md->ct_zone) {
4405             nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
4406         }
4407         if (md->ct_mark) {
4408             nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
4409         }
4410         if (!ovs_u128_is_zero(&md->ct_label)) {
4411             nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
4412                               sizeof(md->ct_label));
4413         }
4414     }
4415
4416     /* Add an ingress port attribute if 'odp_in_port' is not the magical
4417      * value "ODPP_NONE". */
4418     if (md->in_port.odp_port != ODPP_NONE) {
4419         nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
4420     }
4421 }
4422
4423 /* Generate packet metadata from the given ODP flow key. */
4424 void
4425 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
4426                         struct pkt_metadata *md)
4427 {
4428     const struct nlattr *nla;
4429     size_t left;
4430     uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
4431         1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
4432         1u << OVS_KEY_ATTR_IN_PORT;
4433
4434     pkt_metadata_init(md, ODPP_NONE);
4435
4436     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
4437         uint16_t type = nl_attr_type(nla);
4438         size_t len = nl_attr_get_size(nla);
4439         int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
4440                                             OVS_KEY_ATTR_MAX, type);
4441
4442         if (len != expected_len && expected_len >= 0) {
4443             continue;
4444         }
4445
4446         switch (type) {
4447         case OVS_KEY_ATTR_RECIRC_ID:
4448             md->recirc_id = nl_attr_get_u32(nla);
4449             wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
4450             break;
4451         case OVS_KEY_ATTR_DP_HASH:
4452             md->dp_hash = nl_attr_get_u32(nla);
4453             wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
4454             break;
4455         case OVS_KEY_ATTR_PRIORITY:
4456             md->skb_priority = nl_attr_get_u32(nla);
4457             wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
4458             break;
4459         case OVS_KEY_ATTR_SKB_MARK:
4460             md->pkt_mark = nl_attr_get_u32(nla);
4461             wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
4462             break;
4463         case OVS_KEY_ATTR_CT_STATE:
4464             md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
4465             wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_STATE);
4466             break;
4467         case OVS_KEY_ATTR_CT_ZONE:
4468             md->ct_zone = nl_attr_get_u16(nla);
4469             wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_ZONE);
4470             break;
4471         case OVS_KEY_ATTR_CT_MARK:
4472             md->ct_mark = nl_attr_get_u32(nla);
4473             wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_MARK);
4474             break;
4475         case OVS_KEY_ATTR_CT_LABELS: {
4476             const ovs_u128 *cl = nl_attr_get(nla);
4477
4478             md->ct_label = *cl;
4479             wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_LABELS);
4480             break;
4481         }
4482         case OVS_KEY_ATTR_TUNNEL: {
4483             enum odp_key_fitness res;
4484
4485             res = odp_tun_key_from_attr(nla, true, &md->tunnel);
4486             if (res == ODP_FIT_ERROR) {
4487                 memset(&md->tunnel, 0, sizeof md->tunnel);
4488             } else if (res == ODP_FIT_PERFECT) {
4489                 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
4490             }
4491             break;
4492         }
4493         case OVS_KEY_ATTR_IN_PORT:
4494             md->in_port.odp_port = nl_attr_get_odp_port(nla);
4495             wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
4496             break;
4497         default:
4498             break;
4499         }
4500
4501         if (!wanted_attrs) {
4502             return; /* Have everything. */
4503         }
4504     }
4505 }
4506
4507 uint32_t
4508 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
4509 {
4510     BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
4511     return hash_words(ALIGNED_CAST(const uint32_t *, key),
4512                       key_len / sizeof(uint32_t), 0);
4513 }
4514
4515 static void
4516 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
4517                        uint64_t attrs, int out_of_range_attr,
4518                        const struct nlattr *key, size_t key_len)
4519 {
4520     struct ds s;
4521     int i;
4522
4523     if (VLOG_DROP_DBG(rl)) {
4524         return;
4525     }
4526
4527     ds_init(&s);
4528     for (i = 0; i < 64; i++) {
4529         if (attrs & (UINT64_C(1) << i)) {
4530             char namebuf[OVS_KEY_ATTR_BUFSIZE];
4531
4532             ds_put_format(&s, " %s",
4533                           ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
4534         }
4535     }
4536     if (out_of_range_attr) {
4537         ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
4538     }
4539
4540     ds_put_cstr(&s, ": ");
4541     odp_flow_key_format(key, key_len, &s);
4542
4543     VLOG_DBG("%s:%s", title, ds_cstr(&s));
4544     ds_destroy(&s);
4545 }
4546
4547 static uint8_t
4548 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
4549 {
4550     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4551
4552     if (is_mask) {
4553         return odp_frag ? FLOW_NW_FRAG_MASK : 0;
4554     }
4555
4556     if (odp_frag > OVS_FRAG_TYPE_LATER) {
4557         VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
4558         return 0xff; /* Error. */
4559     }
4560
4561     return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
4562         : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
4563         :  FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
4564 }
4565
4566 static bool
4567 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
4568                    const struct nlattr *attrs[], uint64_t *present_attrsp,
4569                    int *out_of_range_attrp)
4570 {
4571     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4572     const struct nlattr *nla;
4573     uint64_t present_attrs;
4574     size_t left;
4575
4576     BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
4577     present_attrs = 0;
4578     *out_of_range_attrp = 0;
4579     NL_ATTR_FOR_EACH (nla, left, key, key_len) {
4580         uint16_t type = nl_attr_type(nla);
4581         size_t len = nl_attr_get_size(nla);
4582         int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
4583                                             OVS_KEY_ATTR_MAX, type);
4584
4585         if (len != expected_len && expected_len >= 0) {
4586             char namebuf[OVS_KEY_ATTR_BUFSIZE];
4587
4588             VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
4589                         "length %d", ovs_key_attr_to_string(type, namebuf,
4590                                                             sizeof namebuf),
4591                         len, expected_len);
4592             return false;
4593         }
4594
4595         if (type > OVS_KEY_ATTR_MAX) {
4596             *out_of_range_attrp = type;
4597         } else {
4598             if (present_attrs & (UINT64_C(1) << type)) {
4599                 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4600
4601                 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
4602                             ovs_key_attr_to_string(type,
4603                                                    namebuf, sizeof namebuf));
4604                 return false;
4605             }
4606
4607             present_attrs |= UINT64_C(1) << type;
4608             attrs[type] = nla;
4609         }
4610     }
4611     if (left) {
4612         VLOG_ERR_RL(&rl, "trailing garbage in flow key");
4613         return false;
4614     }
4615
4616     *present_attrsp = present_attrs;
4617     return true;
4618 }
4619
4620 static enum odp_key_fitness
4621 check_expectations(uint64_t present_attrs, int out_of_range_attr,
4622                    uint64_t expected_attrs,
4623                    const struct nlattr *key, size_t key_len)
4624 {
4625     uint64_t missing_attrs;
4626     uint64_t extra_attrs;
4627
4628     missing_attrs = expected_attrs & ~present_attrs;
4629     if (missing_attrs) {
4630         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4631         log_odp_key_attributes(&rl, "expected but not present",
4632                                missing_attrs, 0, key, key_len);
4633         return ODP_FIT_TOO_LITTLE;
4634     }
4635
4636     extra_attrs = present_attrs & ~expected_attrs;
4637     if (extra_attrs || out_of_range_attr) {
4638         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4639         log_odp_key_attributes(&rl, "present but not expected",
4640                                extra_attrs, out_of_range_attr, key, key_len);
4641         return ODP_FIT_TOO_MUCH;
4642     }
4643
4644     return ODP_FIT_PERFECT;
4645 }
4646
4647 static bool
4648 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4649                 uint64_t present_attrs, uint64_t *expected_attrs,
4650                 struct flow *flow, const struct flow *src_flow)
4651 {
4652     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4653     bool is_mask = flow != src_flow;
4654
4655     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
4656         flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
4657         if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4658             VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
4659                         ntohs(flow->dl_type));
4660             return false;
4661         }
4662         if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
4663             flow->dl_type != htons(0xffff)) {
4664             return false;
4665         }
4666         *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
4667     } else {
4668         if (!is_mask) {
4669             flow->dl_type = htons(FLOW_DL_TYPE_NONE);
4670         } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
4671             /* See comments in odp_flow_key_from_flow__(). */
4672             VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
4673             return false;
4674         }
4675     }
4676     return true;
4677 }
4678
4679 static enum odp_key_fitness
4680 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4681                   uint64_t present_attrs, int out_of_range_attr,
4682                   uint64_t expected_attrs, struct flow *flow,
4683                   const struct nlattr *key, size_t key_len,
4684                   const struct flow *src_flow)
4685 {
4686     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4687     bool is_mask = src_flow != flow;
4688     const void *check_start = NULL;
4689     size_t check_len = 0;
4690     enum ovs_key_attr expected_bit = 0xff;
4691
4692     if (eth_type_mpls(src_flow->dl_type)) {
4693         if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
4694             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
4695         }
4696         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
4697             size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
4698             const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
4699             int n = size / sizeof(ovs_be32);
4700             int i;
4701
4702             if (!size || size % sizeof(ovs_be32)) {
4703                 return ODP_FIT_ERROR;
4704             }
4705             if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
4706                 return ODP_FIT_ERROR;
4707             }
4708
4709             for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
4710                 flow->mpls_lse[i] = mpls_lse[i];
4711             }
4712             if (n > FLOW_MAX_MPLS_LABELS) {
4713                 return ODP_FIT_TOO_MUCH;
4714             }
4715
4716             if (!is_mask) {
4717                 /* BOS may be set only in the innermost label. */
4718                 for (i = 0; i < n - 1; i++) {
4719                     if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
4720                         return ODP_FIT_ERROR;
4721                     }
4722                 }
4723
4724                 /* BOS must be set in the innermost label. */
4725                 if (n < FLOW_MAX_MPLS_LABELS
4726                     && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
4727                     return ODP_FIT_TOO_LITTLE;
4728                 }
4729             }
4730         }
4731
4732         goto done;
4733     } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
4734         if (!is_mask) {
4735             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
4736         }
4737         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
4738             const struct ovs_key_ipv4 *ipv4_key;
4739
4740             ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
4741             put_ipv4_key(ipv4_key, flow, is_mask);
4742             if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
4743                 return ODP_FIT_ERROR;
4744             }
4745             if (is_mask) {
4746                 check_start = ipv4_key;
4747                 check_len = sizeof *ipv4_key;
4748                 expected_bit = OVS_KEY_ATTR_IPV4;
4749             }
4750         }
4751     } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
4752         if (!is_mask) {
4753             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
4754         }
4755         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
4756             const struct ovs_key_ipv6 *ipv6_key;
4757
4758             ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
4759             put_ipv6_key(ipv6_key, flow, is_mask);
4760             if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
4761                 return ODP_FIT_ERROR;
4762             }
4763             if (is_mask) {
4764                 check_start = ipv6_key;
4765                 check_len = sizeof *ipv6_key;
4766                 expected_bit = OVS_KEY_ATTR_IPV6;
4767             }
4768         }
4769     } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
4770                src_flow->dl_type == htons(ETH_TYPE_RARP)) {
4771         if (!is_mask) {
4772             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
4773         }
4774         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
4775             const struct ovs_key_arp *arp_key;
4776
4777             arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
4778             if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
4779                 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
4780                             "key", ntohs(arp_key->arp_op));
4781                 return ODP_FIT_ERROR;
4782             }
4783             put_arp_key(arp_key, flow);
4784             if (is_mask) {
4785                 check_start = arp_key;
4786                 check_len = sizeof *arp_key;
4787                 expected_bit = OVS_KEY_ATTR_ARP;
4788             }
4789         }
4790     } else {
4791         goto done;
4792     }
4793     if (check_len > 0) { /* Happens only when 'is_mask'. */
4794         if (!is_all_zeros(check_start, check_len) &&
4795             flow->dl_type != htons(0xffff)) {
4796             return ODP_FIT_ERROR;
4797         } else {
4798             expected_attrs |= UINT64_C(1) << expected_bit;
4799         }
4800     }
4801
4802     expected_bit = OVS_KEY_ATTR_UNSPEC;
4803     if (src_flow->nw_proto == IPPROTO_TCP
4804         && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4805             src_flow->dl_type == htons(ETH_TYPE_IPV6))
4806         && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4807         if (!is_mask) {
4808             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4809         }
4810         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4811             const union ovs_key_tp *tcp_key;
4812
4813             tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4814             put_tp_key(tcp_key, flow);
4815             expected_bit = OVS_KEY_ATTR_TCP;
4816         }
4817         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4818             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4819             flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4820         }
4821     } else if (src_flow->nw_proto == IPPROTO_UDP
4822                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4823                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
4824                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4825         if (!is_mask) {
4826             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4827         }
4828         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4829             const union ovs_key_tp *udp_key;
4830
4831             udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4832             put_tp_key(udp_key, flow);
4833             expected_bit = OVS_KEY_ATTR_UDP;
4834         }
4835     } else if (src_flow->nw_proto == IPPROTO_SCTP
4836                && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4837                    src_flow->dl_type == htons(ETH_TYPE_IPV6))
4838                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4839         if (!is_mask) {
4840             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4841         }
4842         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4843             const union ovs_key_tp *sctp_key;
4844
4845             sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4846             put_tp_key(sctp_key, flow);
4847             expected_bit = OVS_KEY_ATTR_SCTP;
4848         }
4849     } else if (src_flow->nw_proto == IPPROTO_ICMP
4850                && src_flow->dl_type == htons(ETH_TYPE_IP)
4851                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4852         if (!is_mask) {
4853             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4854         }
4855         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4856             const struct ovs_key_icmp *icmp_key;
4857
4858             icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4859             flow->tp_src = htons(icmp_key->icmp_type);
4860             flow->tp_dst = htons(icmp_key->icmp_code);
4861             expected_bit = OVS_KEY_ATTR_ICMP;
4862         }
4863     } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4864                && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4865                && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4866         if (!is_mask) {
4867             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4868         }
4869         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4870             const struct ovs_key_icmpv6 *icmpv6_key;
4871
4872             icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4873             flow->tp_src = htons(icmpv6_key->icmpv6_type);
4874             flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4875             expected_bit = OVS_KEY_ATTR_ICMPV6;
4876             if (src_flow->tp_dst == htons(0) &&
4877                 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4878                  src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4879                 if (!is_mask) {
4880                     expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4881                 }
4882                 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4883                     const struct ovs_key_nd *nd_key;
4884
4885                     nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4886                     memcpy(&flow->nd_target, nd_key->nd_target,
4887                            sizeof flow->nd_target);
4888                     flow->arp_sha = nd_key->nd_sll;
4889                     flow->arp_tha = nd_key->nd_tll;
4890                     if (is_mask) {
4891                         if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4892                             (flow->tp_src != htons(0xffff) ||
4893                              flow->tp_dst != htons(0xffff))) {
4894                             return ODP_FIT_ERROR;
4895                         } else {
4896                             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4897                         }
4898                     }
4899                 }
4900             }
4901         }
4902     }
4903     if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4904         if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4905             return ODP_FIT_ERROR;
4906         } else {
4907             expected_attrs |= UINT64_C(1) << expected_bit;
4908         }
4909     }
4910
4911 done:
4912     return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4913                               key, key_len);
4914 }
4915
4916 /* Parse 802.1Q header then encapsulated L3 attributes. */
4917 static enum odp_key_fitness
4918 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4919                    uint64_t present_attrs, int out_of_range_attr,
4920                    uint64_t expected_attrs, struct flow *flow,
4921                    const struct nlattr *key, size_t key_len,
4922                    const struct flow *src_flow)
4923 {
4924     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4925     bool is_mask = src_flow != flow;
4926
4927     const struct nlattr *encap
4928         = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4929            ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4930     enum odp_key_fitness encap_fitness;
4931     enum odp_key_fitness fitness;
4932
4933     /* Calculate fitness of outer attributes. */
4934     if (!is_mask) {
4935         expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4936                           (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4937     } else {
4938         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4939             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4940         }
4941         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4942             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4943         }
4944     }
4945     fitness = check_expectations(present_attrs, out_of_range_attr,
4946                                  expected_attrs, key, key_len);
4947
4948     /* Set vlan_tci.
4949      * Remove the TPID from dl_type since it's not the real Ethertype.  */
4950     flow->dl_type = htons(0);
4951     flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4952                       ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4953                       : htons(0));
4954     if (!is_mask) {
4955         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4956             return ODP_FIT_TOO_LITTLE;
4957         } else if (flow->vlan_tci == htons(0)) {
4958             /* Corner case for a truncated 802.1Q header. */
4959             if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4960                 return ODP_FIT_TOO_MUCH;
4961             }
4962             return fitness;
4963         } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4964             VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4965                         "but CFI bit is not set", ntohs(flow->vlan_tci));
4966             return ODP_FIT_ERROR;
4967         }
4968     } else {
4969         if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4970             return fitness;
4971         }
4972     }
4973
4974     /* Now parse the encapsulated attributes. */
4975     if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4976                             attrs, &present_attrs, &out_of_range_attr)) {
4977         return ODP_FIT_ERROR;
4978     }
4979     expected_attrs = 0;
4980
4981     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4982         return ODP_FIT_ERROR;
4983     }
4984     encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4985                                       expected_attrs, flow, key, key_len,
4986                                       src_flow);
4987
4988     /* The overall fitness is the worse of the outer and inner attributes. */
4989     return MAX(fitness, encap_fitness);
4990 }
4991
4992 static enum odp_key_fitness
4993 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4994                        const struct nlattr *src_key, size_t src_key_len,
4995                        struct flow *flow, const struct flow *src_flow,
4996                        bool udpif)
4997 {
4998     const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4999     uint64_t expected_attrs;
5000     uint64_t present_attrs;
5001     int out_of_range_attr;
5002     bool is_mask = src_flow != flow;
5003
5004     memset(flow, 0, sizeof *flow);
5005
5006     /* Parse attributes. */
5007     if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
5008                             &out_of_range_attr)) {
5009         return ODP_FIT_ERROR;
5010     }
5011     expected_attrs = 0;
5012
5013     /* Metadata. */
5014     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
5015         flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
5016         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
5017     } else if (is_mask) {
5018         /* Always exact match recirc_id if it is not specified. */
5019         flow->recirc_id = UINT32_MAX;
5020     }
5021
5022     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
5023         flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
5024         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
5025     }
5026     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
5027         flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
5028         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
5029     }
5030
5031     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
5032         flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
5033         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
5034     }
5035
5036     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
5037         uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
5038
5039         flow->ct_state = odp_to_ovs_ct_state(odp_state);
5040         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
5041     }
5042     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
5043         flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
5044         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
5045     }
5046     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
5047         flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
5048         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
5049     }
5050     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
5051         const ovs_u128 *cl = nl_attr_get(attrs[OVS_KEY_ATTR_CT_LABELS]);
5052
5053         flow->ct_label = *cl;
5054         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
5055     }
5056
5057     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
5058         enum odp_key_fitness res;
5059
5060         res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL],
5061                                       is_mask ? src_key : NULL,
5062                                       src_key_len, &src_flow->tunnel,
5063                                       &flow->tunnel, udpif);
5064         if (res == ODP_FIT_ERROR) {
5065             return ODP_FIT_ERROR;
5066         } else if (res == ODP_FIT_PERFECT) {
5067             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
5068         }
5069     }
5070
5071     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
5072         flow->in_port.odp_port
5073             = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
5074         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
5075     } else if (!is_mask) {
5076         flow->in_port.odp_port = ODPP_NONE;
5077     }
5078
5079     /* Ethernet header. */
5080     if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
5081         const struct ovs_key_ethernet *eth_key;
5082
5083         eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
5084         put_ethernet_key(eth_key, flow);
5085         if (is_mask) {
5086             expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5087         }
5088     }
5089     if (!is_mask) {
5090         expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5091     }
5092
5093     /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5094     if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
5095         src_flow)) {
5096         return ODP_FIT_ERROR;
5097     }
5098
5099     if (is_mask
5100         ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
5101         : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
5102         return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
5103                                   expected_attrs, flow, key, key_len, src_flow);
5104     }
5105     if (is_mask) {
5106         /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5107         flow->vlan_tci = htons(0xffff);
5108         if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
5109             flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
5110             expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
5111         }
5112     }
5113     return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
5114                              expected_attrs, flow, key, key_len, src_flow);
5115 }
5116
5117 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5118  * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
5119  * 'key' fits our expectations for what a flow key should contain.
5120  *
5121  * The 'in_port' will be the datapath's understanding of the port.  The
5122  * caller will need to translate with odp_port_to_ofp_port() if the
5123  * OpenFlow port is needed.
5124  *
5125  * This function doesn't take the packet itself as an argument because none of
5126  * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
5127  * it is always possible to infer which additional attribute(s) should appear
5128  * by looking at the attributes for lower-level protocols, e.g. if the network
5129  * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5130  * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5131  * must be absent. */
5132 enum odp_key_fitness
5133 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
5134                      struct flow *flow)
5135 {
5136    return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, false);
5137 }
5138
5139 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5140  * to a mask structure in 'mask'.  'flow' must be a previously translated flow
5141  * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5142  * attributes from that flow.  Returns an ODP_FIT_* value that indicates how
5143  * well 'key' fits our expectations for what a flow key should contain. */
5144 enum odp_key_fitness
5145 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
5146                      const struct nlattr *flow_key, size_t flow_key_len,
5147                      struct flow *mask, const struct flow *flow)
5148 {
5149    return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
5150                                  mask, flow, false);
5151 }
5152
5153 /* These functions are similar to their non-"_udpif" variants but output a
5154  * 'flow' that is suitable for fast-path packet processing.
5155  *
5156  * Some fields have different representation for flow setup and per-
5157  * packet processing (i.e. different between ofproto-dpif and userspace
5158  * datapath). In particular, with the non-"_udpif" functions, struct
5159  * tun_metadata is in the per-flow format (using 'present.map' and 'opts.u8');
5160  * with these functions, struct tun_metadata is in the per-packet format
5161  * (using 'present.len' and 'opts.gnv'). */
5162 enum odp_key_fitness
5163 odp_flow_key_to_flow_udpif(const struct nlattr *key, size_t key_len,
5164                            struct flow *flow)
5165 {
5166    return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, true);
5167 }
5168
5169 enum odp_key_fitness
5170 odp_flow_key_to_mask_udpif(const struct nlattr *mask_key, size_t mask_key_len,
5171                            const struct nlattr *flow_key, size_t flow_key_len,
5172                            struct flow *mask, const struct flow *flow)
5173 {
5174    return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
5175                                  mask, flow, true);
5176 }
5177
5178 /* Returns 'fitness' as a string, for use in debug messages. */
5179 const char *
5180 odp_key_fitness_to_string(enum odp_key_fitness fitness)
5181 {
5182     switch (fitness) {
5183     case ODP_FIT_PERFECT:
5184         return "OK";
5185     case ODP_FIT_TOO_MUCH:
5186         return "too_much";
5187     case ODP_FIT_TOO_LITTLE:
5188         return "too_little";
5189     case ODP_FIT_ERROR:
5190         return "error";
5191     default:
5192         return "<unknown>";
5193     }
5194 }
5195
5196 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5197  * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
5198  * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5199  * offset within 'odp_actions' of the start of the cookie.  (If 'userdata' is
5200  * null, then the return value is not meaningful.) */
5201 size_t
5202 odp_put_userspace_action(uint32_t pid,
5203                          const void *userdata, size_t userdata_size,
5204                          odp_port_t tunnel_out_port,
5205                          bool include_actions,
5206                          struct ofpbuf *odp_actions)
5207 {
5208     size_t userdata_ofs;
5209     size_t offset;
5210
5211     offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
5212     nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
5213     if (userdata) {
5214         userdata_ofs = odp_actions->size + NLA_HDRLEN;
5215
5216         /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5217          * module before Linux 3.10 required the userdata to be exactly 8 bytes
5218          * long:
5219          *
5220          *   - The kernel rejected shorter userdata with -ERANGE.
5221          *
5222          *   - The kernel silently dropped userdata beyond the first 8 bytes.
5223          *
5224          * Thus, for maximum compatibility, always put at least 8 bytes.  (We
5225          * separately disable features that required more than 8 bytes.) */
5226         memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
5227                                       MAX(8, userdata_size)),
5228                userdata, userdata_size);
5229     } else {
5230         userdata_ofs = 0;
5231     }
5232     if (tunnel_out_port != ODPP_NONE) {
5233         nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
5234                             tunnel_out_port);
5235     }
5236     if (include_actions) {
5237         nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
5238     }
5239     nl_msg_end_nested(odp_actions, offset);
5240
5241     return userdata_ofs;
5242 }
5243
5244 void
5245 odp_put_tunnel_action(const struct flow_tnl *tunnel,
5246                       struct ofpbuf *odp_actions)
5247 {
5248     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
5249     tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
5250     nl_msg_end_nested(odp_actions, offset);
5251 }
5252
5253 void
5254 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
5255                         struct ovs_action_push_tnl *data)
5256 {
5257     int size = offsetof(struct ovs_action_push_tnl, header);
5258
5259     size += data->header_len;
5260     nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
5261 }
5262
5263 \f
5264 /* The commit_odp_actions() function and its helpers. */
5265
5266 static void
5267 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
5268                   const void *key, size_t key_size)
5269 {
5270     size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
5271     nl_msg_put_unspec(odp_actions, key_type, key, key_size);
5272     nl_msg_end_nested(odp_actions, offset);
5273 }
5274
5275 /* Masked set actions have a mask following the data within the netlink
5276  * attribute.  The unmasked bits in the data will be cleared as the data
5277  * is copied to the action. */
5278 void
5279 commit_masked_set_action(struct ofpbuf *odp_actions,
5280                          enum ovs_key_attr key_type,
5281                          const void *key_, const void *mask_, size_t key_size)
5282 {
5283     size_t offset = nl_msg_start_nested(odp_actions,
5284                                         OVS_ACTION_ATTR_SET_MASKED);
5285     char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
5286     const char *key = key_, *mask = mask_;
5287
5288     memcpy(data + key_size, mask, key_size);
5289     /* Clear unmasked bits while copying. */
5290     while (key_size--) {
5291         *data++ = *key++ & *mask++;
5292     }
5293     nl_msg_end_nested(odp_actions, offset);
5294 }
5295
5296 /* If any of the flow key data that ODP actions can modify are different in
5297  * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5298  * 'odp_actions' that change the flow tunneling information in key from
5299  * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5300  * same way.  In other words, operates the same as commit_odp_actions(), but
5301  * only on tunneling information. */
5302 void
5303 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
5304                          struct ofpbuf *odp_actions)
5305 {
5306     /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5307      * must have non-zero ipv6_dst. */
5308     if (flow_tnl_dst_is_set(&flow->tunnel)) {
5309         if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
5310             return;
5311         }
5312         memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
5313         odp_put_tunnel_action(&base->tunnel, odp_actions);
5314     }
5315 }
5316
5317 static bool
5318 commit(enum ovs_key_attr attr, bool use_masked_set,
5319        const void *key, void *base, void *mask, size_t size,
5320        struct ofpbuf *odp_actions)
5321 {
5322     if (memcmp(key, base, size)) {
5323         bool fully_masked = odp_mask_is_exact(attr, mask, size);
5324
5325         if (use_masked_set && !fully_masked) {
5326             commit_masked_set_action(odp_actions, attr, key, mask, size);
5327         } else {
5328             if (!fully_masked) {
5329                 memset(mask, 0xff, size);
5330             }
5331             commit_set_action(odp_actions, attr, key, size);
5332         }
5333         memcpy(base, key, size);
5334         return true;
5335     } else {
5336         /* Mask bits are set when we have either read or set the corresponding
5337          * values.  Masked bits will be exact-matched, no need to set them
5338          * if the value did not actually change. */
5339         return false;
5340     }
5341 }
5342
5343 static void
5344 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
5345 {
5346     eth->eth_src = flow->dl_src;
5347     eth->eth_dst = flow->dl_dst;
5348 }
5349
5350 static void
5351 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
5352 {
5353     flow->dl_src = eth->eth_src;
5354     flow->dl_dst = eth->eth_dst;
5355 }
5356
5357 static void
5358 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
5359                              struct ofpbuf *odp_actions,
5360                              struct flow_wildcards *wc,
5361                              bool use_masked)
5362 {
5363     struct ovs_key_ethernet key, base, mask;
5364
5365     get_ethernet_key(flow, &key);
5366     get_ethernet_key(base_flow, &base);
5367     get_ethernet_key(&wc->masks, &mask);
5368
5369     if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
5370                &key, &base, &mask, sizeof key, odp_actions)) {
5371         put_ethernet_key(&base, base_flow);
5372         put_ethernet_key(&mask, &wc->masks);
5373     }
5374 }
5375
5376 static void
5377 pop_vlan(struct flow *base,
5378          struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5379 {
5380     memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
5381
5382     if (base->vlan_tci & htons(VLAN_CFI)) {
5383         nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
5384         base->vlan_tci = 0;
5385     }
5386 }
5387
5388 static void
5389 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
5390                    struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5391 {
5392     if (base->vlan_tci == vlan_tci) {
5393         return;
5394     }
5395
5396     pop_vlan(base, odp_actions, wc);
5397     if (vlan_tci & htons(VLAN_CFI)) {
5398         struct ovs_action_push_vlan vlan;
5399
5400         vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
5401         vlan.vlan_tci = vlan_tci;
5402         nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
5403                           &vlan, sizeof vlan);
5404     }
5405     base->vlan_tci = vlan_tci;
5406 }
5407
5408 /* Wildcarding already done at action translation time. */
5409 static void
5410 commit_mpls_action(const struct flow *flow, struct flow *base,
5411                    struct ofpbuf *odp_actions)
5412 {
5413     int base_n = flow_count_mpls_labels(base, NULL);
5414     int flow_n = flow_count_mpls_labels(flow, NULL);
5415     int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
5416                                                  NULL);
5417
5418     while (base_n > common_n) {
5419         if (base_n - 1 == common_n && flow_n > common_n) {
5420             /* If there is only one more LSE in base than there are common
5421              * between base and flow; and flow has at least one more LSE than
5422              * is common then the topmost LSE of base may be updated using
5423              * set */
5424             struct ovs_key_mpls mpls_key;
5425
5426             mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
5427             commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
5428                               &mpls_key, sizeof mpls_key);
5429             flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
5430             common_n++;
5431         } else {
5432             /* Otherwise, if there more LSEs in base than are common between
5433              * base and flow then pop the topmost one. */
5434             ovs_be16 dl_type;
5435             bool popped;
5436
5437             /* If all the LSEs are to be popped and this is not the outermost
5438              * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5439              * POP_MPLS action instead of flow->dl_type.
5440              *
5441              * This is because the POP_MPLS action requires its ethertype
5442              * argument to be an MPLS ethernet type but in this case
5443              * flow->dl_type will be a non-MPLS ethernet type.
5444              *
5445              * When the final POP_MPLS action occurs it use flow->dl_type and
5446              * the and the resulting packet will have the desired dl_type. */
5447             if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
5448                 dl_type = htons(ETH_TYPE_MPLS);
5449             } else {
5450                 dl_type = flow->dl_type;
5451             }
5452             nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
5453             popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
5454             ovs_assert(popped);
5455             base_n--;
5456         }
5457     }
5458
5459     /* If, after the above popping and setting, there are more LSEs in flow
5460      * than base then some LSEs need to be pushed. */
5461     while (base_n < flow_n) {
5462         struct ovs_action_push_mpls *mpls;
5463
5464         mpls = nl_msg_put_unspec_zero(odp_actions,
5465                                       OVS_ACTION_ATTR_PUSH_MPLS,
5466                                       sizeof *mpls);
5467         mpls->mpls_ethertype = flow->dl_type;
5468         mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
5469         flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
5470         flow_set_mpls_lse(base, 0, mpls->mpls_lse);
5471         base_n++;
5472     }
5473 }
5474
5475 static void
5476 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
5477 {
5478     ipv4->ipv4_src = flow->nw_src;
5479     ipv4->ipv4_dst = flow->nw_dst;
5480     ipv4->ipv4_proto = flow->nw_proto;
5481     ipv4->ipv4_tos = flow->nw_tos;
5482     ipv4->ipv4_ttl = flow->nw_ttl;
5483     ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
5484 }
5485
5486 static void
5487 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
5488 {
5489     flow->nw_src = ipv4->ipv4_src;
5490     flow->nw_dst = ipv4->ipv4_dst;
5491     flow->nw_proto = ipv4->ipv4_proto;
5492     flow->nw_tos = ipv4->ipv4_tos;
5493     flow->nw_ttl = ipv4->ipv4_ttl;
5494     flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
5495 }
5496
5497 static void
5498 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
5499                        struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5500                        bool use_masked)
5501 {
5502     struct ovs_key_ipv4 key, mask, base;
5503
5504     /* Check that nw_proto and nw_frag remain unchanged. */
5505     ovs_assert(flow->nw_proto == base_flow->nw_proto &&
5506                flow->nw_frag == base_flow->nw_frag);
5507
5508     get_ipv4_key(flow, &key, false);
5509     get_ipv4_key(base_flow, &base, false);
5510     get_ipv4_key(&wc->masks, &mask, true);
5511     mask.ipv4_proto = 0;        /* Not writeable. */
5512     mask.ipv4_frag = 0;         /* Not writable. */
5513
5514     if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
5515                odp_actions)) {
5516         put_ipv4_key(&base, base_flow, false);
5517         if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
5518             put_ipv4_key(&mask, &wc->masks, true);
5519         }
5520    }
5521 }
5522
5523 static void
5524 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
5525 {
5526     memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
5527     memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
5528     ipv6->ipv6_label = flow->ipv6_label;
5529     ipv6->ipv6_proto = flow->nw_proto;
5530     ipv6->ipv6_tclass = flow->nw_tos;
5531     ipv6->ipv6_hlimit = flow->nw_ttl;
5532     ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
5533 }
5534
5535 static void
5536 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
5537 {
5538     memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
5539     memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
5540     flow->ipv6_label = ipv6->ipv6_label;
5541     flow->nw_proto = ipv6->ipv6_proto;
5542     flow->nw_tos = ipv6->ipv6_tclass;
5543     flow->nw_ttl = ipv6->ipv6_hlimit;
5544     flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
5545 }
5546
5547 static void
5548 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
5549                        struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5550                        bool use_masked)
5551 {
5552     struct ovs_key_ipv6 key, mask, base;
5553
5554     /* Check that nw_proto and nw_frag remain unchanged. */
5555     ovs_assert(flow->nw_proto == base_flow->nw_proto &&
5556                flow->nw_frag == base_flow->nw_frag);
5557
5558     get_ipv6_key(flow, &key, false);
5559     get_ipv6_key(base_flow, &base, false);
5560     get_ipv6_key(&wc->masks, &mask, true);
5561     mask.ipv6_proto = 0;        /* Not writeable. */
5562     mask.ipv6_frag = 0;         /* Not writable. */
5563
5564     if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
5565                odp_actions)) {
5566         put_ipv6_key(&base, base_flow, false);
5567         if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
5568             put_ipv6_key(&mask, &wc->masks, true);
5569         }
5570     }
5571 }
5572
5573 static void
5574 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
5575 {
5576     /* ARP key has padding, clear it. */
5577     memset(arp, 0, sizeof *arp);
5578
5579     arp->arp_sip = flow->nw_src;
5580     arp->arp_tip = flow->nw_dst;
5581     arp->arp_op = htons(flow->nw_proto);
5582     arp->arp_sha = flow->arp_sha;
5583     arp->arp_tha = flow->arp_tha;
5584 }
5585
5586 static void
5587 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
5588 {
5589     flow->nw_src = arp->arp_sip;
5590     flow->nw_dst = arp->arp_tip;
5591     flow->nw_proto = ntohs(arp->arp_op);
5592     flow->arp_sha = arp->arp_sha;
5593     flow->arp_tha = arp->arp_tha;
5594 }
5595
5596 static enum slow_path_reason
5597 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
5598                       struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5599 {
5600     struct ovs_key_arp key, mask, base;
5601
5602     get_arp_key(flow, &key);
5603     get_arp_key(base_flow, &base);
5604     get_arp_key(&wc->masks, &mask);
5605
5606     if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
5607                odp_actions)) {
5608         put_arp_key(&base, base_flow);
5609         put_arp_key(&mask, &wc->masks);
5610         return SLOW_ACTION;
5611     }
5612     return 0;
5613 }
5614
5615 static void
5616 get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
5617 {
5618     /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5619     icmp->icmp_type = ntohs(flow->tp_src);
5620     icmp->icmp_code = ntohs(flow->tp_dst);
5621 }
5622
5623 static void
5624 put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
5625 {
5626     /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5627     flow->tp_src = htons(icmp->icmp_type);
5628     flow->tp_dst = htons(icmp->icmp_code);
5629 }
5630
5631 static enum slow_path_reason
5632 commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
5633                        struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5634 {
5635     struct ovs_key_icmp key, mask, base;
5636     enum ovs_key_attr attr;
5637
5638     get_icmp_key(flow, &key);
5639     get_icmp_key(base_flow, &base);
5640     get_icmp_key(&wc->masks, &mask);
5641
5642     attr = flow->dl_type == htons(ETH_TYPE_IP) ? OVS_KEY_ATTR_ICMP
5643                                                : OVS_KEY_ATTR_ICMPV6;
5644     if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) {
5645         put_icmp_key(&base, base_flow);
5646         put_icmp_key(&mask, &wc->masks);
5647         return SLOW_ACTION;
5648     }
5649     return 0;
5650 }
5651
5652 static void
5653 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
5654 {
5655     memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
5656     /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5657     nd->nd_sll = flow->arp_sha;
5658     nd->nd_tll = flow->arp_tha;
5659 }
5660
5661 static void
5662 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
5663 {
5664     memcpy(&flow->nd_target, nd->nd_target, sizeof flow->nd_target);
5665     /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5666     flow->arp_sha = nd->nd_sll;
5667     flow->arp_tha = nd->nd_tll;
5668 }
5669
5670 static enum slow_path_reason
5671 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
5672                      struct ofpbuf *odp_actions,
5673                      struct flow_wildcards *wc, bool use_masked)
5674 {
5675     struct ovs_key_nd key, mask, base;
5676
5677     get_nd_key(flow, &key);
5678     get_nd_key(base_flow, &base);
5679     get_nd_key(&wc->masks, &mask);
5680
5681     if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
5682                odp_actions)) {
5683         put_nd_key(&base, base_flow);
5684         put_nd_key(&mask, &wc->masks);
5685         return SLOW_ACTION;
5686     }
5687
5688     return 0;
5689 }
5690
5691 static enum slow_path_reason
5692 commit_set_nw_action(const struct flow *flow, struct flow *base,
5693                      struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5694                      bool use_masked)
5695 {
5696     /* Check if 'flow' really has an L3 header. */
5697     if (!flow->nw_proto) {
5698         return 0;
5699     }
5700
5701     switch (ntohs(base->dl_type)) {
5702     case ETH_TYPE_IP:
5703         commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
5704         break;
5705
5706     case ETH_TYPE_IPV6:
5707         commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
5708         return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
5709
5710     case ETH_TYPE_ARP:
5711         return commit_set_arp_action(flow, base, odp_actions, wc);
5712     }
5713
5714     return 0;
5715 }
5716
5717 /* TCP, UDP, and SCTP keys have the same layout. */
5718 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
5719                   sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
5720
5721 static void
5722 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
5723 {
5724     tp->tcp.tcp_src = flow->tp_src;
5725     tp->tcp.tcp_dst = flow->tp_dst;
5726 }
5727
5728 static void
5729 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
5730 {
5731     flow->tp_src = tp->tcp.tcp_src;
5732     flow->tp_dst = tp->tcp.tcp_dst;
5733 }
5734
5735 static void
5736 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
5737                        struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5738                        bool use_masked)
5739 {
5740     enum ovs_key_attr key_type;
5741     union ovs_key_tp key, mask, base;
5742
5743     /* Check if 'flow' really has an L3 header. */
5744     if (!flow->nw_proto) {
5745         return;
5746     }
5747
5748     if (!is_ip_any(base_flow)) {
5749         return;
5750     }
5751
5752     if (flow->nw_proto == IPPROTO_TCP) {
5753         key_type = OVS_KEY_ATTR_TCP;
5754     } else if (flow->nw_proto == IPPROTO_UDP) {
5755         key_type = OVS_KEY_ATTR_UDP;
5756     } else if (flow->nw_proto == IPPROTO_SCTP) {
5757         key_type = OVS_KEY_ATTR_SCTP;
5758     } else {
5759         return;
5760     }
5761
5762     get_tp_key(flow, &key);
5763     get_tp_key(base_flow, &base);
5764     get_tp_key(&wc->masks, &mask);
5765
5766     if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
5767                odp_actions)) {
5768         put_tp_key(&base, base_flow);
5769         put_tp_key(&mask, &wc->masks);
5770     }
5771 }
5772
5773 static void
5774 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
5775                            struct ofpbuf *odp_actions,
5776                            struct flow_wildcards *wc,
5777                            bool use_masked)
5778 {
5779     uint32_t key, mask, base;
5780
5781     key = flow->skb_priority;
5782     base = base_flow->skb_priority;
5783     mask = wc->masks.skb_priority;
5784
5785     if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
5786                sizeof key, odp_actions)) {
5787         base_flow->skb_priority = base;
5788         wc->masks.skb_priority = mask;
5789     }
5790 }
5791
5792 static void
5793 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
5794                            struct ofpbuf *odp_actions,
5795                            struct flow_wildcards *wc,
5796                            bool use_masked)
5797 {
5798     uint32_t key, mask, base;
5799
5800     key = flow->pkt_mark;
5801     base = base_flow->pkt_mark;
5802     mask = wc->masks.pkt_mark;
5803
5804     if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
5805                sizeof key, odp_actions)) {
5806         base_flow->pkt_mark = base;
5807         wc->masks.pkt_mark = mask;
5808     }
5809 }
5810
5811 /* If any of the flow key data that ODP actions can modify are different in
5812  * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5813  * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
5814  * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
5815  * in addition to this function if needed.  Sets fields in 'wc' that are
5816  * used as part of the action.
5817  *
5818  * Returns a reason to force processing the flow's packets into the userspace
5819  * slow path, if there is one, otherwise 0. */
5820 enum slow_path_reason
5821 commit_odp_actions(const struct flow *flow, struct flow *base,
5822                    struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5823                    bool use_masked)
5824 {
5825     enum slow_path_reason slow1, slow2;
5826
5827     commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
5828     slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
5829     commit_set_port_action(flow, base, odp_actions, wc, use_masked);
5830     slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
5831     commit_mpls_action(flow, base, odp_actions);
5832     commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
5833     commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
5834     commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
5835
5836     return slow1 ? slow1 : slow2;
5837 }