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