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