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