]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
ofproto-dpif: Allow IPv6 ND Extensions only if supported
[mirror_ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019 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 <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include "odp-util.h"
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <math.h>
25 #include <netinet/icmp6.h>
26 #include <netinet/ip6.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "byte-order.h"
31 #include "coverage.h"
32 #include "dpif.h"
33 #include "openvswitch/dynamic-string.h"
34 #include "flow.h"
35 #include "netlink.h"
36 #include "openvswitch/ofpbuf.h"
37 #include "packets.h"
38 #include "simap.h"
39 #include "timeval.h"
40 #include "tun-metadata.h"
41 #include "unaligned.h"
42 #include "util.h"
43 #include "uuid.h"
44 #include "openvswitch/vlog.h"
45 #include "openvswitch/match.h"
46 #include "odp-netlink-macros.h"
47 #include "csum.h"
48
49 VLOG_DEFINE_THIS_MODULE(odp_util);
50
51 /* The interface between userspace and kernel uses an "OVS_*" prefix.
52 * Since this is fairly non-specific for the OVS userspace components,
53 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
54 * interactions with the datapath.
55 */
56
57 /* The set of characters that may separate one action or one key attribute
58 * from another. */
59 static const char *delimiters = ", \t\r\n";
60 static const char *delimiters_end = ", \t\r\n)";
61
62 #define MAX_ODP_NESTED 32
63
64 struct parse_odp_context {
65 const struct simap *port_names;
66 int depth; /* Current nested depth of odp string. */
67 };
68
69 static int parse_odp_key_mask_attr(struct parse_odp_context *, const char *,
70 struct ofpbuf *, struct ofpbuf *);
71
72 static int parse_odp_key_mask_attr__(struct parse_odp_context *, const char *,
73 struct ofpbuf *, struct ofpbuf *);
74
75 static void format_odp_key_attr(const struct nlattr *a,
76 const struct nlattr *ma,
77 const struct hmap *portno_names, struct ds *ds,
78 bool verbose);
79
80 struct geneve_scan {
81 struct geneve_opt d[63];
82 int len;
83 };
84
85 static int scan_geneve(const char *s, struct geneve_scan *key,
86 struct geneve_scan *mask);
87 static void format_geneve_opts(const struct geneve_opt *opt,
88 const struct geneve_opt *mask, int opts_len,
89 struct ds *, bool verbose);
90
91 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
92 int max, struct ofpbuf *,
93 const struct nlattr *key);
94 static void format_u128(struct ds *d, const ovs_32aligned_u128 *key,
95 const ovs_32aligned_u128 *mask, bool verbose);
96 static int scan_u128(const char *s, ovs_u128 *value, ovs_u128 *mask);
97
98 static int parse_odp_action(struct parse_odp_context *context, const char *s,
99 struct ofpbuf *actions);
100
101 static int parse_odp_action__(struct parse_odp_context *context, const char *s,
102 struct ofpbuf *actions);
103
104 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
105 * 'type':
106 *
107 * - For an action whose argument has a fixed length, returned that
108 * nonnegative length in bytes.
109 *
110 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
111 *
112 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
113 static int
114 odp_action_len(uint16_t type)
115 {
116 if (type > OVS_ACTION_ATTR_MAX) {
117 return -1;
118 }
119
120 switch ((enum ovs_action_attr) type) {
121 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
122 case OVS_ACTION_ATTR_TRUNC: return sizeof(struct ovs_action_trunc);
123 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
124 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
125 case OVS_ACTION_ATTR_METER: return sizeof(uint32_t);
126 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
127 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
128 case OVS_ACTION_ATTR_POP_VLAN: return 0;
129 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
130 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
131 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
132 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
133 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
134 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
135 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
136 case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE;
137 case OVS_ACTION_ATTR_CT_CLEAR: return 0;
138 case OVS_ACTION_ATTR_PUSH_ETH: return sizeof(struct ovs_action_push_eth);
139 case OVS_ACTION_ATTR_POP_ETH: return 0;
140 case OVS_ACTION_ATTR_CLONE: return ATTR_LEN_VARIABLE;
141 case OVS_ACTION_ATTR_PUSH_NSH: return ATTR_LEN_VARIABLE;
142 case OVS_ACTION_ATTR_POP_NSH: return 0;
143 case OVS_ACTION_ATTR_CHECK_PKT_LEN: return ATTR_LEN_VARIABLE;
144
145 case OVS_ACTION_ATTR_UNSPEC:
146 case __OVS_ACTION_ATTR_MAX:
147 return ATTR_LEN_INVALID;
148 }
149
150 return ATTR_LEN_INVALID;
151 }
152
153 /* Returns a string form of 'attr'. The return value is either a statically
154 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
155 * should be at least OVS_KEY_ATTR_BUFSIZE. */
156 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
157 static const char *
158 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
159 {
160 switch (attr) {
161 case OVS_KEY_ATTR_UNSPEC: return "unspec";
162 case OVS_KEY_ATTR_ENCAP: return "encap";
163 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
164 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
165 case OVS_KEY_ATTR_CT_STATE: return "ct_state";
166 case OVS_KEY_ATTR_CT_ZONE: return "ct_zone";
167 case OVS_KEY_ATTR_CT_MARK: return "ct_mark";
168 case OVS_KEY_ATTR_CT_LABELS: return "ct_label";
169 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: return "ct_tuple4";
170 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: return "ct_tuple6";
171 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
172 case OVS_KEY_ATTR_IN_PORT: return "in_port";
173 case OVS_KEY_ATTR_ETHERNET: return "eth";
174 case OVS_KEY_ATTR_VLAN: return "vlan";
175 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
176 case OVS_KEY_ATTR_IPV4: return "ipv4";
177 case OVS_KEY_ATTR_IPV6: return "ipv6";
178 case OVS_KEY_ATTR_TCP: return "tcp";
179 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
180 case OVS_KEY_ATTR_UDP: return "udp";
181 case OVS_KEY_ATTR_SCTP: return "sctp";
182 case OVS_KEY_ATTR_ICMP: return "icmp";
183 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
184 case OVS_KEY_ATTR_ARP: return "arp";
185 case OVS_KEY_ATTR_ND: return "nd";
186 case OVS_KEY_ATTR_ND_EXTENSIONS: return "nd_ext";
187 case OVS_KEY_ATTR_MPLS: return "mpls";
188 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
189 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
190 case OVS_KEY_ATTR_PACKET_TYPE: return "packet_type";
191 case OVS_KEY_ATTR_NSH: return "nsh";
192
193 case __OVS_KEY_ATTR_MAX:
194 default:
195 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
196 return namebuf;
197 }
198 }
199
200 static void
201 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
202 {
203 size_t len = nl_attr_get_size(a);
204
205 ds_put_format(ds, "action%d", nl_attr_type(a));
206 if (len) {
207 const uint8_t *unspec;
208 unsigned int i;
209
210 unspec = nl_attr_get(a);
211 for (i = 0; i < len; i++) {
212 ds_put_char(ds, i ? ' ': '(');
213 ds_put_format(ds, "%02x", unspec[i]);
214 }
215 ds_put_char(ds, ')');
216 }
217 }
218
219 static void
220 format_odp_sample_action(struct ds *ds, const struct nlattr *attr,
221 const struct hmap *portno_names)
222 {
223 static const struct nl_policy ovs_sample_policy[] = {
224 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
225 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
226 };
227 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
228 double percentage;
229 const struct nlattr *nla_acts;
230 int len;
231
232 ds_put_cstr(ds, "sample");
233
234 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
235 ds_put_cstr(ds, "(error)");
236 return;
237 }
238
239 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
240 UINT32_MAX;
241
242 ds_put_format(ds, "(sample=%.1f%%,", percentage);
243
244 ds_put_cstr(ds, "actions(");
245 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
246 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
247 format_odp_actions(ds, nla_acts, len, portno_names);
248 ds_put_format(ds, "))");
249 }
250
251 static void
252 format_odp_clone_action(struct ds *ds, const struct nlattr *attr,
253 const struct hmap *portno_names)
254 {
255 const struct nlattr *nla_acts = nl_attr_get(attr);
256 int len = nl_attr_get_size(attr);
257
258 ds_put_cstr(ds, "clone");
259 ds_put_format(ds, "(");
260 format_odp_actions(ds, nla_acts, len, portno_names);
261 ds_put_format(ds, ")");
262 }
263
264 static void
265 format_nsh_key(struct ds *ds, const struct ovs_key_nsh *key)
266 {
267 ds_put_format(ds, "flags=%d", key->flags);
268 ds_put_format(ds, ",ttl=%d", key->ttl);
269 ds_put_format(ds, ",mdtype=%d", key->mdtype);
270 ds_put_format(ds, ",np=%d", key->np);
271 ds_put_format(ds, ",spi=0x%x",
272 nsh_path_hdr_to_spi_uint32(key->path_hdr));
273 ds_put_format(ds, ",si=%d",
274 nsh_path_hdr_to_si(key->path_hdr));
275
276 switch (key->mdtype) {
277 case NSH_M_TYPE1:
278 for (int i = 0; i < 4; i++) {
279 ds_put_format(ds, ",c%d=0x%x", i + 1, ntohl(key->context[i]));
280 }
281 break;
282 case NSH_M_TYPE2:
283 default:
284 /* No support for matching other metadata formats yet. */
285 break;
286 }
287 }
288
289 static void
290 format_uint8_masked(struct ds *s, bool *first, const char *name,
291 uint8_t value, uint8_t mask)
292 {
293 if (mask != 0) {
294 if (!*first) {
295 ds_put_char(s, ',');
296 }
297 ds_put_format(s, "%s=", name);
298 if (mask == UINT8_MAX) {
299 ds_put_format(s, "%"PRIu8, value);
300 } else {
301 ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask);
302 }
303 *first = false;
304 }
305 }
306
307 static void
308 format_be32_masked(struct ds *s, bool *first, const char *name,
309 ovs_be32 value, ovs_be32 mask)
310 {
311 if (mask != htonl(0)) {
312 if (!*first) {
313 ds_put_char(s, ',');
314 }
315 ds_put_format(s, "%s=", name);
316 if (mask == OVS_BE32_MAX) {
317 ds_put_format(s, "0x%"PRIx32, ntohl(value));
318 } else {
319 ds_put_format(s, "0x%"PRIx32"/0x%08"PRIx32,
320 ntohl(value), ntohl(mask));
321 }
322 *first = false;
323 }
324 }
325
326 static void
327 format_nsh_key_mask(struct ds *ds, const struct ovs_key_nsh *key,
328 const struct ovs_key_nsh *mask)
329 {
330 if (!mask) {
331 format_nsh_key(ds, key);
332 } else {
333 bool first = true;
334 uint32_t spi = nsh_path_hdr_to_spi_uint32(key->path_hdr);
335 uint32_t spi_mask = nsh_path_hdr_to_spi_uint32(mask->path_hdr);
336 if (spi_mask == (NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
337 spi_mask = UINT32_MAX;
338 }
339 uint8_t si = nsh_path_hdr_to_si(key->path_hdr);
340 uint8_t si_mask = nsh_path_hdr_to_si(mask->path_hdr);
341
342 format_uint8_masked(ds, &first, "flags", key->flags, mask->flags);
343 format_uint8_masked(ds, &first, "ttl", key->ttl, mask->ttl);
344 format_uint8_masked(ds, &first, "mdtype", key->mdtype, mask->mdtype);
345 format_uint8_masked(ds, &first, "np", key->np, mask->np);
346 format_be32_masked(ds, &first, "spi", htonl(spi), htonl(spi_mask));
347 format_uint8_masked(ds, &first, "si", si, si_mask);
348 format_be32_masked(ds, &first, "c1", key->context[0],
349 mask->context[0]);
350 format_be32_masked(ds, &first, "c2", key->context[1],
351 mask->context[1]);
352 format_be32_masked(ds, &first, "c3", key->context[2],
353 mask->context[2]);
354 format_be32_masked(ds, &first, "c4", key->context[3],
355 mask->context[3]);
356 }
357 }
358
359 static void
360 format_odp_push_nsh_action(struct ds *ds,
361 const struct nsh_hdr *nsh_hdr)
362 {
363 size_t mdlen = nsh_hdr_len(nsh_hdr) - NSH_BASE_HDR_LEN;
364 uint32_t spi = ntohl(nsh_get_spi(nsh_hdr));
365 uint8_t si = nsh_get_si(nsh_hdr);
366 uint8_t flags = nsh_get_flags(nsh_hdr);
367 uint8_t ttl = nsh_get_ttl(nsh_hdr);
368
369 ds_put_cstr(ds, "push_nsh(");
370 ds_put_format(ds, "flags=%d", flags);
371 ds_put_format(ds, ",ttl=%d", ttl);
372 ds_put_format(ds, ",mdtype=%d", nsh_hdr->md_type);
373 ds_put_format(ds, ",np=%d", nsh_hdr->next_proto);
374 ds_put_format(ds, ",spi=0x%x", spi);
375 ds_put_format(ds, ",si=%d", si);
376 switch (nsh_hdr->md_type) {
377 case NSH_M_TYPE1: {
378 const struct nsh_md1_ctx *md1_ctx = &nsh_hdr->md1;
379 for (int i = 0; i < 4; i++) {
380 ds_put_format(ds, ",c%d=0x%x", i + 1,
381 ntohl(get_16aligned_be32(&md1_ctx->context[i])));
382 }
383 break;
384 }
385 case NSH_M_TYPE2: {
386 const struct nsh_md2_tlv *md2_ctx = &nsh_hdr->md2;
387 ds_put_cstr(ds, ",md2=");
388 ds_put_hex(ds, md2_ctx, mdlen);
389 break;
390 }
391 default:
392 OVS_NOT_REACHED();
393 }
394 ds_put_format(ds, ")");
395 }
396
397 static const char *
398 slow_path_reason_to_string(uint32_t reason)
399 {
400 switch ((enum slow_path_reason) reason) {
401 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
402 SLOW_PATH_REASONS
403 #undef SPR
404 }
405
406 return NULL;
407 }
408
409 const char *
410 slow_path_reason_to_explanation(enum slow_path_reason reason)
411 {
412 switch (reason) {
413 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
414 SLOW_PATH_REASONS
415 #undef SPR
416 }
417
418 return "<unknown>";
419 }
420
421 static int
422 parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t),
423 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
424 {
425 return parse_flags(s, bit_to_string, ')', NULL, NULL,
426 res_flags, allowed, res_mask);
427 }
428
429 static void
430 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr,
431 const struct hmap *portno_names)
432 {
433 static const struct nl_policy ovs_userspace_policy[] = {
434 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
435 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
436 .optional = true },
437 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
438 .optional = true },
439 [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC,
440 .optional = true },
441 };
442 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
443 const struct nlattr *userdata_attr;
444 const struct nlattr *tunnel_out_port_attr;
445
446 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
447 ds_put_cstr(ds, "userspace(error)");
448 return;
449 }
450
451 ds_put_format(ds, "userspace(pid=%"PRIu32,
452 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
453
454 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
455
456 if (userdata_attr) {
457 const uint8_t *userdata = nl_attr_get(userdata_attr);
458 size_t userdata_len = nl_attr_get_size(userdata_attr);
459 bool userdata_unspec = true;
460 struct user_action_cookie cookie;
461
462 if (userdata_len == sizeof cookie) {
463 memcpy(&cookie, userdata, sizeof cookie);
464
465 userdata_unspec = false;
466
467 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
468 ds_put_format(ds, ",sFlow("
469 "vid=%"PRIu16",pcp=%d,output=%"PRIu32")",
470 vlan_tci_to_vid(cookie.sflow.vlan_tci),
471 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
472 cookie.sflow.output);
473 } else if (cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
474 ds_put_cstr(ds, ",slow_path(");
475 format_flags(ds, slow_path_reason_to_string,
476 cookie.slow_path.reason, ',');
477 ds_put_format(ds, ")");
478 } else if (cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
479 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
480 ",collector_set_id=%"PRIu32
481 ",obs_domain_id=%"PRIu32
482 ",obs_point_id=%"PRIu32
483 ",output_port=",
484 cookie.flow_sample.probability,
485 cookie.flow_sample.collector_set_id,
486 cookie.flow_sample.obs_domain_id,
487 cookie.flow_sample.obs_point_id);
488 odp_portno_name_format(portno_names,
489 cookie.flow_sample.output_odp_port, ds);
490 if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_INGRESS) {
491 ds_put_cstr(ds, ",ingress");
492 } else if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_EGRESS) {
493 ds_put_cstr(ds, ",egress");
494 }
495 ds_put_char(ds, ')');
496 } else if (cookie.type == USER_ACTION_COOKIE_IPFIX) {
497 ds_put_format(ds, ",ipfix(output_port=");
498 odp_portno_name_format(portno_names,
499 cookie.ipfix.output_odp_port, ds);
500 ds_put_char(ds, ')');
501 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
502 ds_put_format(ds, ",controller(reason=%"PRIu16
503 ",dont_send=%d"
504 ",continuation=%d"
505 ",recirc_id=%"PRIu32
506 ",rule_cookie=%#"PRIx64
507 ",controller_id=%"PRIu16
508 ",max_len=%"PRIu16,
509 cookie.controller.reason,
510 !!cookie.controller.dont_send,
511 !!cookie.controller.continuation,
512 cookie.controller.recirc_id,
513 ntohll(get_32aligned_be64(
514 &cookie.controller.rule_cookie)),
515 cookie.controller.controller_id,
516 cookie.controller.max_len);
517 ds_put_char(ds, ')');
518 } else {
519 userdata_unspec = true;
520 }
521 }
522
523 if (userdata_unspec) {
524 size_t i;
525 ds_put_format(ds, ",userdata(");
526 for (i = 0; i < userdata_len; i++) {
527 ds_put_format(ds, "%02x", userdata[i]);
528 }
529 ds_put_char(ds, ')');
530 }
531 }
532
533 if (a[OVS_USERSPACE_ATTR_ACTIONS]) {
534 ds_put_cstr(ds, ",actions");
535 }
536
537 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
538 if (tunnel_out_port_attr) {
539 ds_put_format(ds, ",tunnel_out_port=");
540 odp_portno_name_format(portno_names,
541 nl_attr_get_odp_port(tunnel_out_port_attr), ds);
542 }
543
544 ds_put_char(ds, ')');
545 }
546
547 static void
548 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
549 {
550 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
551 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
552 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
553 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
554 };
555 ds_put_char(ds, ',');
556 }
557 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
558 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
559 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
560 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
561 }
562 ds_put_char(ds, ',');
563 }
564 if (!(tci & htons(VLAN_CFI))) {
565 ds_put_cstr(ds, "cfi=0");
566 ds_put_char(ds, ',');
567 }
568 ds_chomp(ds, ',');
569 }
570
571 static void
572 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
573 {
574 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
575 mpls_lse_to_label(mpls_lse),
576 mpls_lse_to_tc(mpls_lse),
577 mpls_lse_to_ttl(mpls_lse),
578 mpls_lse_to_bos(mpls_lse));
579 }
580
581 static void
582 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
583 const struct ovs_key_mpls *mpls_mask, int n)
584 {
585 for (int i = 0; i < n; i++) {
586 ovs_be32 key = mpls_key[i].mpls_lse;
587
588 if (mpls_mask == NULL) {
589 format_mpls_lse(ds, key);
590 } else {
591 ovs_be32 mask = mpls_mask[i].mpls_lse;
592
593 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
594 mpls_lse_to_label(key), mpls_lse_to_label(mask),
595 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
596 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
597 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
598 }
599 ds_put_char(ds, ',');
600 }
601 ds_chomp(ds, ',');
602 }
603
604 static void
605 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
606 {
607 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
608 }
609
610 static void
611 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
612 {
613 ds_put_format(ds, "hash(");
614
615 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
616 ds_put_format(ds, "l4(%"PRIu32")", hash_act->hash_basis);
617 } else if (hash_act->hash_alg == OVS_HASH_ALG_SYM_L4) {
618 ds_put_format(ds, "sym_l4(%"PRIu32")", hash_act->hash_basis);
619 } else {
620 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
621 hash_act->hash_alg);
622 }
623 ds_put_format(ds, ")");
624 }
625
626 static const void *
627 format_udp_tnl_push_header(struct ds *ds, const struct udp_header *udp)
628 {
629 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
630 ntohs(udp->udp_src), ntohs(udp->udp_dst),
631 ntohs(udp->udp_csum));
632
633 return udp + 1;
634 }
635
636 static void
637 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
638 {
639 const struct eth_header *eth;
640 const void *l3;
641 const void *l4;
642 const struct udp_header *udp;
643
644 eth = (const struct eth_header *)data->header;
645
646 l3 = eth + 1;
647
648 /* Ethernet */
649 ds_put_format(ds, "header(size=%"PRIu32",type=%"PRIu32",eth(dst=",
650 data->header_len, data->tnl_type);
651 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
652 ds_put_format(ds, ",src=");
653 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
654 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
655
656 if (eth->eth_type == htons(ETH_TYPE_IP)) {
657 /* IPv4 */
658 const struct ip_header *ip = l3;
659 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
660 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
661 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
662 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
663 ip->ip_proto, ip->ip_tos,
664 ip->ip_ttl,
665 ntohs(ip->ip_frag_off));
666 l4 = (ip + 1);
667 } else {
668 const struct ovs_16aligned_ip6_hdr *ip6 = l3;
669 struct in6_addr src, dst;
670 memcpy(&src, &ip6->ip6_src, sizeof src);
671 memcpy(&dst, &ip6->ip6_dst, sizeof dst);
672 uint32_t ipv6_flow = ntohl(get_16aligned_be32(&ip6->ip6_flow));
673
674 ds_put_format(ds, "ipv6(src=");
675 ipv6_format_addr(&src, ds);
676 ds_put_format(ds, ",dst=");
677 ipv6_format_addr(&dst, ds);
678 ds_put_format(ds, ",label=%i,proto=%"PRIu8",tclass=0x%"PRIx32
679 ",hlimit=%"PRIu8"),",
680 ipv6_flow & IPV6_LABEL_MASK, ip6->ip6_nxt,
681 (ipv6_flow >> 20) & 0xff, ip6->ip6_hlim);
682 l4 = (ip6 + 1);
683 }
684
685 udp = (const struct udp_header *) l4;
686
687 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
688 const struct vxlanhdr *vxh;
689
690 vxh = format_udp_tnl_push_header(ds, udp);
691
692 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
693 ntohl(get_16aligned_be32(&vxh->vx_flags)),
694 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
695 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
696 const struct genevehdr *gnh;
697
698 gnh = format_udp_tnl_push_header(ds, udp);
699
700 ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
701 gnh->oam ? "oam," : "",
702 gnh->critical ? "crit," : "",
703 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
704
705 if (gnh->opt_len) {
706 ds_put_cstr(ds, ",options(");
707 format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
708 ds, false);
709 ds_put_char(ds, ')');
710 }
711
712 ds_put_char(ds, ')');
713 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE ||
714 data->tnl_type == OVS_VPORT_TYPE_IP6GRE) {
715 const struct gre_base_hdr *greh;
716 ovs_16aligned_be32 *options;
717
718 greh = (const struct gre_base_hdr *) l4;
719
720 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
721 ntohs(greh->flags), ntohs(greh->protocol));
722 options = (ovs_16aligned_be32 *)(greh + 1);
723 if (greh->flags & htons(GRE_CSUM)) {
724 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
725 options++;
726 }
727 if (greh->flags & htons(GRE_KEY)) {
728 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
729 options++;
730 }
731 if (greh->flags & htons(GRE_SEQ)) {
732 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
733 options++;
734 }
735 ds_put_format(ds, ")");
736 } else if (data->tnl_type == OVS_VPORT_TYPE_ERSPAN ||
737 data->tnl_type == OVS_VPORT_TYPE_IP6ERSPAN) {
738 const struct gre_base_hdr *greh;
739 const struct erspan_base_hdr *ersh;
740
741 greh = (const struct gre_base_hdr *) l4;
742 ersh = ERSPAN_HDR(greh);
743
744 if (ersh->ver == 1) {
745 ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *,
746 ersh + 1);
747 ds_put_format(ds, "erspan(ver=1,sid=0x%"PRIx16",idx=0x%"PRIx32")",
748 get_sid(ersh), ntohl(get_16aligned_be32(index)));
749 } else if (ersh->ver == 2) {
750 struct erspan_md2 *md2 = ALIGNED_CAST(struct erspan_md2 *,
751 ersh + 1);
752 ds_put_format(ds, "erspan(ver=2,sid=0x%"PRIx16
753 ",dir=%"PRIu8",hwid=0x%"PRIx8")",
754 get_sid(ersh), md2->dir, get_hwid(md2));
755 } else {
756 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__, ersh->ver);
757 }
758 }
759 ds_put_format(ds, ")");
760 }
761
762 static void
763 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr,
764 const struct hmap *portno_names)
765 {
766 struct ovs_action_push_tnl *data;
767
768 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
769
770 ds_put_cstr(ds, "tnl_push(tnl_port(");
771 odp_portno_name_format(portno_names, data->tnl_port, ds);
772 ds_put_cstr(ds, "),");
773 format_odp_tnl_push_header(ds, data);
774 ds_put_format(ds, ",out_port(");
775 odp_portno_name_format(portno_names, data->out_port, ds);
776 ds_put_cstr(ds, "))");
777 }
778
779 static const struct nl_policy ovs_nat_policy[] = {
780 [OVS_NAT_ATTR_SRC] = { .type = NL_A_FLAG, .optional = true, },
781 [OVS_NAT_ATTR_DST] = { .type = NL_A_FLAG, .optional = true, },
782 [OVS_NAT_ATTR_IP_MIN] = { .type = NL_A_UNSPEC, .optional = true,
783 .min_len = sizeof(struct in_addr),
784 .max_len = sizeof(struct in6_addr)},
785 [OVS_NAT_ATTR_IP_MAX] = { .type = NL_A_UNSPEC, .optional = true,
786 .min_len = sizeof(struct in_addr),
787 .max_len = sizeof(struct in6_addr)},
788 [OVS_NAT_ATTR_PROTO_MIN] = { .type = NL_A_U16, .optional = true, },
789 [OVS_NAT_ATTR_PROTO_MAX] = { .type = NL_A_U16, .optional = true, },
790 [OVS_NAT_ATTR_PERSISTENT] = { .type = NL_A_FLAG, .optional = true, },
791 [OVS_NAT_ATTR_PROTO_HASH] = { .type = NL_A_FLAG, .optional = true, },
792 [OVS_NAT_ATTR_PROTO_RANDOM] = { .type = NL_A_FLAG, .optional = true, },
793 };
794
795 static void
796 format_odp_ct_nat(struct ds *ds, const struct nlattr *attr)
797 {
798 struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)];
799 size_t addr_len;
800 ovs_be32 ip_min, ip_max;
801 struct in6_addr ip6_min, ip6_max;
802 uint16_t proto_min, proto_max;
803
804 if (!nl_parse_nested(attr, ovs_nat_policy, a, ARRAY_SIZE(a))) {
805 ds_put_cstr(ds, "nat(error: nl_parse_nested() failed.)");
806 return;
807 }
808 /* If no type, then nothing else either. */
809 if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST])
810 && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX]
811 || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX]
812 || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH]
813 || a[OVS_NAT_ATTR_PROTO_RANDOM])) {
814 ds_put_cstr(ds, "nat(error: options allowed only with \"src\" or \"dst\")");
815 return;
816 }
817 /* Both SNAT & DNAT may not be specified. */
818 if (a[OVS_NAT_ATTR_SRC] && a[OVS_NAT_ATTR_DST]) {
819 ds_put_cstr(ds, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
820 return;
821 }
822 /* proto may not appear without ip. */
823 if (!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_PROTO_MIN]) {
824 ds_put_cstr(ds, "nat(error: proto but no IP.)");
825 return;
826 }
827 /* MAX may not appear without MIN. */
828 if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX])
829 || (!a[OVS_NAT_ATTR_PROTO_MIN] && a[OVS_NAT_ATTR_PROTO_MAX])) {
830 ds_put_cstr(ds, "nat(error: range max without min.)");
831 return;
832 }
833 /* Address sizes must match. */
834 if ((a[OVS_NAT_ATTR_IP_MIN]
835 && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) &&
836 nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr)))
837 || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]
838 && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN])
839 != nl_attr_get_size(a[OVS_NAT_ATTR_IP_MAX])))) {
840 ds_put_cstr(ds, "nat(error: IP address sizes do not match)");
841 return;
842 }
843
844 addr_len = a[OVS_NAT_ATTR_IP_MIN]
845 ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0;
846 ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN]
847 ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0;
848 ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX]
849 ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0;
850 if (addr_len == sizeof ip6_min) {
851 ip6_min = a[OVS_NAT_ATTR_IP_MIN]
852 ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN])
853 : in6addr_any;
854 ip6_max = a[OVS_NAT_ATTR_IP_MAX]
855 ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX])
856 : in6addr_any;
857 }
858 proto_min = a[OVS_NAT_ATTR_PROTO_MIN]
859 ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0;
860 proto_max = a[OVS_NAT_ATTR_PROTO_MAX]
861 ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0;
862
863 if ((addr_len == sizeof(ovs_be32)
864 && ip_max && ntohl(ip_min) > ntohl(ip_max))
865 || (addr_len == sizeof(struct in6_addr)
866 && !ipv6_mask_is_any(&ip6_max)
867 && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0)
868 || (proto_max && proto_min > proto_max)) {
869 ds_put_cstr(ds, "nat(range error)");
870 return;
871 }
872
873 ds_put_cstr(ds, "nat");
874 if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) {
875 ds_put_char(ds, '(');
876 if (a[OVS_NAT_ATTR_SRC]) {
877 ds_put_cstr(ds, "src");
878 } else if (a[OVS_NAT_ATTR_DST]) {
879 ds_put_cstr(ds, "dst");
880 }
881
882 if (addr_len > 0) {
883 ds_put_cstr(ds, "=");
884
885 if (addr_len == sizeof ip_min) {
886 ds_put_format(ds, IP_FMT, IP_ARGS(ip_min));
887
888 if (ip_max && ip_max != ip_min) {
889 ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max));
890 }
891 } else if (addr_len == sizeof ip6_min) {
892 ipv6_format_addr_bracket(&ip6_min, ds, proto_min);
893
894 if (!ipv6_mask_is_any(&ip6_max) &&
895 memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) {
896 ds_put_char(ds, '-');
897 ipv6_format_addr_bracket(&ip6_max, ds, proto_min);
898 }
899 }
900 if (proto_min) {
901 ds_put_format(ds, ":%"PRIu16, proto_min);
902
903 if (proto_max && proto_max != proto_min) {
904 ds_put_format(ds, "-%"PRIu16, proto_max);
905 }
906 }
907 }
908 ds_put_char(ds, ',');
909 if (a[OVS_NAT_ATTR_PERSISTENT]) {
910 ds_put_cstr(ds, "persistent,");
911 }
912 if (a[OVS_NAT_ATTR_PROTO_HASH]) {
913 ds_put_cstr(ds, "hash,");
914 }
915 if (a[OVS_NAT_ATTR_PROTO_RANDOM]) {
916 ds_put_cstr(ds, "random,");
917 }
918 ds_chomp(ds, ',');
919 ds_put_char(ds, ')');
920 }
921 }
922
923 static const struct nl_policy ovs_conntrack_policy[] = {
924 [OVS_CT_ATTR_COMMIT] = { .type = NL_A_FLAG, .optional = true, },
925 [OVS_CT_ATTR_FORCE_COMMIT] = { .type = NL_A_FLAG, .optional = true, },
926 [OVS_CT_ATTR_ZONE] = { .type = NL_A_U16, .optional = true, },
927 [OVS_CT_ATTR_MARK] = { .type = NL_A_UNSPEC, .optional = true,
928 .min_len = sizeof(uint32_t) * 2 },
929 [OVS_CT_ATTR_LABELS] = { .type = NL_A_UNSPEC, .optional = true,
930 .min_len = sizeof(struct ovs_key_ct_labels) * 2 },
931 [OVS_CT_ATTR_HELPER] = { .type = NL_A_STRING, .optional = true,
932 .min_len = 1, .max_len = 16 },
933 [OVS_CT_ATTR_NAT] = { .type = NL_A_UNSPEC, .optional = true },
934 [OVS_CT_ATTR_TIMEOUT] = { .type = NL_A_STRING, .optional = true,
935 .min_len = 1, .max_len = 32 },
936 };
937
938 static void
939 format_odp_conntrack_action(struct ds *ds, const struct nlattr *attr)
940 {
941 struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)];
942 const struct {
943 ovs_32aligned_u128 value;
944 ovs_32aligned_u128 mask;
945 } *label;
946 const uint32_t *mark;
947 const char *helper, *timeout;
948 uint16_t zone;
949 bool commit, force;
950 const struct nlattr *nat;
951
952 if (!nl_parse_nested(attr, ovs_conntrack_policy, a, ARRAY_SIZE(a))) {
953 ds_put_cstr(ds, "ct(error)");
954 return;
955 }
956
957 commit = a[OVS_CT_ATTR_COMMIT] ? true : false;
958 force = a[OVS_CT_ATTR_FORCE_COMMIT] ? true : false;
959 zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0;
960 mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL;
961 label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL;
962 helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL;
963 timeout = a[OVS_CT_ATTR_TIMEOUT] ?
964 nl_attr_get(a[OVS_CT_ATTR_TIMEOUT]) : NULL;
965 nat = a[OVS_CT_ATTR_NAT];
966
967 ds_put_format(ds, "ct");
968 if (commit || force || zone || mark || label || helper || timeout || nat) {
969 ds_put_cstr(ds, "(");
970 if (commit) {
971 ds_put_format(ds, "commit,");
972 }
973 if (force) {
974 ds_put_format(ds, "force_commit,");
975 }
976 if (zone) {
977 ds_put_format(ds, "zone=%"PRIu16",", zone);
978 }
979 if (mark) {
980 ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark,
981 *(mark + 1));
982 }
983 if (label) {
984 ds_put_format(ds, "label=");
985 format_u128(ds, &label->value, &label->mask, true);
986 ds_put_char(ds, ',');
987 }
988 if (helper) {
989 ds_put_format(ds, "helper=%s,", helper);
990 }
991 if (timeout) {
992 ds_put_format(ds, "timeout=%s", timeout);
993 }
994 if (nat) {
995 format_odp_ct_nat(ds, nat);
996 }
997 ds_chomp(ds, ',');
998 ds_put_cstr(ds, ")");
999 }
1000 }
1001
1002 static const struct attr_len_tbl
1003 ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
1004 [OVS_NSH_KEY_ATTR_BASE] = { .len = 8 },
1005 [OVS_NSH_KEY_ATTR_MD1] = { .len = 16 },
1006 [OVS_NSH_KEY_ATTR_MD2] = { .len = ATTR_LEN_VARIABLE },
1007 };
1008
1009 static void
1010 format_odp_set_nsh(struct ds *ds, const struct nlattr *attr)
1011 {
1012 unsigned int left;
1013 const struct nlattr *a;
1014 struct ovs_key_nsh nsh;
1015 struct ovs_key_nsh nsh_mask;
1016
1017 memset(&nsh, 0, sizeof nsh);
1018 memset(&nsh_mask, 0xff, sizeof nsh_mask);
1019
1020 NL_NESTED_FOR_EACH (a, left, attr) {
1021 enum ovs_nsh_key_attr type = nl_attr_type(a);
1022 size_t len = nl_attr_get_size(a);
1023
1024 if (type >= OVS_NSH_KEY_ATTR_MAX) {
1025 return;
1026 }
1027
1028 int expected_len = ovs_nsh_key_attr_lens[type].len;
1029 if ((expected_len != ATTR_LEN_VARIABLE) && (len != 2 * expected_len)) {
1030 return;
1031 }
1032
1033 switch (type) {
1034 case OVS_NSH_KEY_ATTR_UNSPEC:
1035 break;
1036 case OVS_NSH_KEY_ATTR_BASE: {
1037 const struct ovs_nsh_key_base *base = nl_attr_get(a);
1038 const struct ovs_nsh_key_base *base_mask = base + 1;
1039 memcpy(&nsh, base, sizeof(*base));
1040 memcpy(&nsh_mask, base_mask, sizeof(*base_mask));
1041 break;
1042 }
1043 case OVS_NSH_KEY_ATTR_MD1: {
1044 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
1045 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1046 memcpy(&nsh.context, &md1->context, sizeof(*md1));
1047 memcpy(&nsh_mask.context, &md1_mask->context, sizeof(*md1_mask));
1048 break;
1049 }
1050 case OVS_NSH_KEY_ATTR_MD2:
1051 case __OVS_NSH_KEY_ATTR_MAX:
1052 default:
1053 /* No support for matching other metadata formats yet. */
1054 break;
1055 }
1056 }
1057
1058 ds_put_cstr(ds, "set(nsh(");
1059 format_nsh_key_mask(ds, &nsh, &nsh_mask);
1060 ds_put_cstr(ds, "))");
1061 }
1062
1063 static void
1064 format_odp_check_pkt_len_action(struct ds *ds, const struct nlattr *attr,
1065 const struct hmap *portno_names OVS_UNUSED)
1066 {
1067 static const struct nl_policy ovs_cpl_policy[] = {
1068 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = { .type = NL_A_U16 },
1069 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = { .type = NL_A_NESTED },
1070 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]
1071 = { .type = NL_A_NESTED },
1072 };
1073 struct nlattr *a[ARRAY_SIZE(ovs_cpl_policy)];
1074 ds_put_cstr(ds, "check_pkt_len");
1075 if (!nl_parse_nested(attr, ovs_cpl_policy, a, ARRAY_SIZE(a))) {
1076 ds_put_cstr(ds, "(error)");
1077 return;
1078 }
1079
1080 if (!a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] ||
1081 !a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]) {
1082 ds_put_cstr(ds, "(error)");
1083 return;
1084 }
1085
1086 uint16_t pkt_len = nl_attr_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
1087 ds_put_format(ds, "(size=%u,gt(", pkt_len);
1088 const struct nlattr *acts;
1089 acts = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
1090 format_odp_actions(ds, nl_attr_get(acts), nl_attr_get_size(acts),
1091 portno_names);
1092
1093 ds_put_cstr(ds, "),le(");
1094 acts = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
1095 format_odp_actions(ds, nl_attr_get(acts), nl_attr_get_size(acts),
1096 portno_names);
1097 ds_put_cstr(ds, "))");
1098 }
1099
1100 static void
1101 format_odp_action(struct ds *ds, const struct nlattr *a,
1102 const struct hmap *portno_names)
1103 {
1104 int expected_len;
1105 enum ovs_action_attr type = nl_attr_type(a);
1106 size_t size;
1107
1108 expected_len = odp_action_len(nl_attr_type(a));
1109 if (expected_len != ATTR_LEN_VARIABLE &&
1110 nl_attr_get_size(a) != expected_len) {
1111 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
1112 nl_attr_get_size(a), expected_len);
1113 format_generic_odp_action(ds, a);
1114 return;
1115 }
1116
1117 switch (type) {
1118 case OVS_ACTION_ATTR_METER:
1119 ds_put_format(ds, "meter(%"PRIu32")", nl_attr_get_u32(a));
1120 break;
1121 case OVS_ACTION_ATTR_OUTPUT:
1122 odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
1123 break;
1124 case OVS_ACTION_ATTR_TRUNC: {
1125 const struct ovs_action_trunc *trunc =
1126 nl_attr_get_unspec(a, sizeof *trunc);
1127
1128 ds_put_format(ds, "trunc(%"PRIu32")", trunc->max_len);
1129 break;
1130 }
1131 case OVS_ACTION_ATTR_TUNNEL_POP:
1132 ds_put_cstr(ds, "tnl_pop(");
1133 odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
1134 ds_put_char(ds, ')');
1135 break;
1136 case OVS_ACTION_ATTR_TUNNEL_PUSH:
1137 format_odp_tnl_push_action(ds, a, portno_names);
1138 break;
1139 case OVS_ACTION_ATTR_USERSPACE:
1140 format_odp_userspace_action(ds, a, portno_names);
1141 break;
1142 case OVS_ACTION_ATTR_RECIRC:
1143 format_odp_recirc_action(ds, nl_attr_get_u32(a));
1144 break;
1145 case OVS_ACTION_ATTR_HASH:
1146 format_odp_hash_action(ds, nl_attr_get(a));
1147 break;
1148 case OVS_ACTION_ATTR_SET_MASKED:
1149 a = nl_attr_get(a);
1150 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1151 if (nl_attr_type(a) == OVS_KEY_ATTR_NSH) {
1152 format_odp_set_nsh(ds, a);
1153 break;
1154 }
1155 size = nl_attr_get_size(a) / 2;
1156 ds_put_cstr(ds, "set(");
1157
1158 /* Masked set action not supported for tunnel key, which is bigger. */
1159 if (size <= sizeof(struct ovs_key_ipv6)) {
1160 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
1161 sizeof(struct nlattr))];
1162 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
1163 sizeof(struct nlattr))];
1164
1165 mask->nla_type = attr->nla_type = nl_attr_type(a);
1166 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
1167 memcpy(attr + 1, (char *)(a + 1), size);
1168 memcpy(mask + 1, (char *)(a + 1) + size, size);
1169 format_odp_key_attr(attr, mask, NULL, ds, false);
1170 } else {
1171 format_odp_key_attr(a, NULL, NULL, ds, false);
1172 }
1173 ds_put_cstr(ds, ")");
1174 break;
1175 case OVS_ACTION_ATTR_SET:
1176 ds_put_cstr(ds, "set(");
1177 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
1178 ds_put_cstr(ds, ")");
1179 break;
1180 case OVS_ACTION_ATTR_PUSH_ETH: {
1181 const struct ovs_action_push_eth *eth = nl_attr_get(a);
1182 ds_put_format(ds, "push_eth(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
1183 ETH_ADDR_ARGS(eth->addresses.eth_src),
1184 ETH_ADDR_ARGS(eth->addresses.eth_dst));
1185 break;
1186 }
1187 case OVS_ACTION_ATTR_POP_ETH:
1188 ds_put_cstr(ds, "pop_eth");
1189 break;
1190 case OVS_ACTION_ATTR_PUSH_VLAN: {
1191 const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
1192 ds_put_cstr(ds, "push_vlan(");
1193 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
1194 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
1195 }
1196 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
1197 ds_put_char(ds, ')');
1198 break;
1199 }
1200 case OVS_ACTION_ATTR_POP_VLAN:
1201 ds_put_cstr(ds, "pop_vlan");
1202 break;
1203 case OVS_ACTION_ATTR_PUSH_MPLS: {
1204 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
1205 ds_put_cstr(ds, "push_mpls(");
1206 format_mpls_lse(ds, mpls->mpls_lse);
1207 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
1208 break;
1209 }
1210 case OVS_ACTION_ATTR_POP_MPLS: {
1211 ovs_be16 ethertype = nl_attr_get_be16(a);
1212 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
1213 break;
1214 }
1215 case OVS_ACTION_ATTR_SAMPLE:
1216 format_odp_sample_action(ds, a, portno_names);
1217 break;
1218 case OVS_ACTION_ATTR_CT:
1219 format_odp_conntrack_action(ds, a);
1220 break;
1221 case OVS_ACTION_ATTR_CT_CLEAR:
1222 ds_put_cstr(ds, "ct_clear");
1223 break;
1224 case OVS_ACTION_ATTR_CLONE:
1225 format_odp_clone_action(ds, a, portno_names);
1226 break;
1227 case OVS_ACTION_ATTR_PUSH_NSH: {
1228 uint32_t buffer[NSH_HDR_MAX_LEN / 4];
1229 struct nsh_hdr *nsh_hdr = ALIGNED_CAST(struct nsh_hdr *, buffer);
1230 nsh_reset_ver_flags_ttl_len(nsh_hdr);
1231 odp_nsh_hdr_from_attr(nl_attr_get(a), nsh_hdr, NSH_HDR_MAX_LEN);
1232 format_odp_push_nsh_action(ds, nsh_hdr);
1233 break;
1234 }
1235 case OVS_ACTION_ATTR_POP_NSH:
1236 ds_put_cstr(ds, "pop_nsh()");
1237 break;
1238 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
1239 format_odp_check_pkt_len_action(ds, a, portno_names);
1240 break;
1241 case OVS_ACTION_ATTR_UNSPEC:
1242 case __OVS_ACTION_ATTR_MAX:
1243 default:
1244 format_generic_odp_action(ds, a);
1245 break;
1246 }
1247 }
1248
1249 void
1250 format_odp_actions(struct ds *ds, const struct nlattr *actions,
1251 size_t actions_len, const struct hmap *portno_names)
1252 {
1253 if (actions_len) {
1254 const struct nlattr *a;
1255 unsigned int left;
1256
1257 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
1258 if (a != actions) {
1259 ds_put_char(ds, ',');
1260 }
1261 format_odp_action(ds, a, portno_names);
1262 }
1263 if (left) {
1264 int i;
1265
1266 if (left == actions_len) {
1267 ds_put_cstr(ds, "<empty>");
1268 }
1269 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1270 for (i = 0; i < left; i++) {
1271 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1272 }
1273 ds_put_char(ds, ')');
1274 }
1275 } else {
1276 ds_put_cstr(ds, "drop");
1277 }
1278 }
1279
1280 /* Separate out parse_odp_userspace_action() function. */
1281 static int
1282 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
1283 {
1284 uint32_t pid;
1285 struct user_action_cookie cookie;
1286 struct ofpbuf buf;
1287 odp_port_t tunnel_out_port;
1288 int n = -1;
1289 void *user_data = NULL;
1290 size_t user_data_size = 0;
1291 bool include_actions = false;
1292 int res;
1293
1294 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
1295 return -EINVAL;
1296 }
1297
1298 ofpbuf_init(&buf, 16);
1299 memset(&cookie, 0, sizeof cookie);
1300
1301 user_data = &cookie;
1302 user_data_size = sizeof cookie;
1303 {
1304 uint32_t output;
1305 uint32_t probability;
1306 uint32_t collector_set_id;
1307 uint32_t obs_domain_id;
1308 uint32_t obs_point_id;
1309
1310 /* USER_ACTION_COOKIE_CONTROLLER. */
1311 uint8_t dont_send;
1312 uint8_t continuation;
1313 uint16_t reason;
1314 uint32_t recirc_id;
1315 uint64_t rule_cookie;
1316 uint16_t controller_id;
1317 uint16_t max_len;
1318
1319 int vid, pcp;
1320 int n1 = -1;
1321 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
1322 "pcp=%i,output=%"SCNi32")%n",
1323 &vid, &pcp, &output, &n1)) {
1324 uint16_t tci;
1325
1326 n += n1;
1327 tci = vid | (pcp << VLAN_PCP_SHIFT);
1328 if (tci) {
1329 tci |= VLAN_CFI;
1330 }
1331
1332 cookie.type = USER_ACTION_COOKIE_SFLOW;
1333 cookie.ofp_in_port = OFPP_NONE;
1334 cookie.ofproto_uuid = UUID_ZERO;
1335 cookie.sflow.vlan_tci = htons(tci);
1336 cookie.sflow.output = output;
1337 } else if (ovs_scan(&s[n], ",slow_path(%n",
1338 &n1)) {
1339 n += n1;
1340 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
1341 cookie.ofp_in_port = OFPP_NONE;
1342 cookie.ofproto_uuid = UUID_ZERO;
1343 cookie.slow_path.reason = 0;
1344
1345 res = parse_odp_flags(&s[n], slow_path_reason_to_string,
1346 &cookie.slow_path.reason,
1347 SLOW_PATH_REASON_MASK, NULL);
1348 if (res < 0 || s[n + res] != ')') {
1349 goto out;
1350 }
1351 n += res + 1;
1352 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
1353 "collector_set_id=%"SCNi32","
1354 "obs_domain_id=%"SCNi32","
1355 "obs_point_id=%"SCNi32","
1356 "output_port=%"SCNi32"%n",
1357 &probability, &collector_set_id,
1358 &obs_domain_id, &obs_point_id,
1359 &output, &n1)) {
1360 n += n1;
1361
1362 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1363 cookie.ofp_in_port = OFPP_NONE;
1364 cookie.ofproto_uuid = UUID_ZERO;
1365 cookie.flow_sample.probability = probability;
1366 cookie.flow_sample.collector_set_id = collector_set_id;
1367 cookie.flow_sample.obs_domain_id = obs_domain_id;
1368 cookie.flow_sample.obs_point_id = obs_point_id;
1369 cookie.flow_sample.output_odp_port = u32_to_odp(output);
1370
1371 if (ovs_scan(&s[n], ",ingress%n", &n1)) {
1372 cookie.flow_sample.direction = NX_ACTION_SAMPLE_INGRESS;
1373 n += n1;
1374 } else if (ovs_scan(&s[n], ",egress%n", &n1)) {
1375 cookie.flow_sample.direction = NX_ACTION_SAMPLE_EGRESS;
1376 n += n1;
1377 } else {
1378 cookie.flow_sample.direction = NX_ACTION_SAMPLE_DEFAULT;
1379 }
1380 if (s[n] != ')') {
1381 res = -EINVAL;
1382 goto out;
1383 }
1384 n++;
1385 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
1386 &output, &n1) ) {
1387 n += n1;
1388 cookie.type = USER_ACTION_COOKIE_IPFIX;
1389 cookie.ofp_in_port = OFPP_NONE;
1390 cookie.ofproto_uuid = UUID_ZERO;
1391 cookie.ipfix.output_odp_port = u32_to_odp(output);
1392 } else if (ovs_scan(&s[n], ",controller(reason=%"SCNu16
1393 ",dont_send=%"SCNu8
1394 ",continuation=%"SCNu8
1395 ",recirc_id=%"SCNu32
1396 ",rule_cookie=%"SCNx64
1397 ",controller_id=%"SCNu16
1398 ",max_len=%"SCNu16")%n",
1399 &reason, &dont_send, &continuation, &recirc_id,
1400 &rule_cookie, &controller_id, &max_len, &n1)) {
1401 n += n1;
1402 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
1403 cookie.ofp_in_port = OFPP_NONE;
1404 cookie.ofproto_uuid = UUID_ZERO;
1405 cookie.controller.dont_send = dont_send ? true : false;
1406 cookie.controller.continuation = continuation ? true : false;
1407 cookie.controller.reason = reason;
1408 cookie.controller.recirc_id = recirc_id;
1409 put_32aligned_be64(&cookie.controller.rule_cookie,
1410 htonll(rule_cookie));
1411 cookie.controller.controller_id = controller_id;
1412 cookie.controller.max_len = max_len;
1413 } else if (ovs_scan(&s[n], ",userdata(%n", &n1)) {
1414 char *end;
1415
1416 n += n1;
1417 end = ofpbuf_put_hex(&buf, &s[n], NULL);
1418 if (end[0] != ')') {
1419 res = -EINVAL;
1420 goto out;
1421 }
1422 user_data = buf.data;
1423 user_data_size = buf.size;
1424 n = (end + 1) - s;
1425 }
1426 }
1427
1428 {
1429 int n1 = -1;
1430 if (ovs_scan(&s[n], ",actions%n", &n1)) {
1431 n += n1;
1432 include_actions = true;
1433 }
1434 }
1435
1436 {
1437 int n1 = -1;
1438 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
1439 &tunnel_out_port, &n1)) {
1440 odp_put_userspace_action(pid, user_data, user_data_size,
1441 tunnel_out_port, include_actions, actions);
1442 res = n + n1;
1443 goto out;
1444 } else if (s[n] == ')') {
1445 odp_put_userspace_action(pid, user_data, user_data_size,
1446 ODPP_NONE, include_actions, actions);
1447 res = n + 1;
1448 goto out;
1449 }
1450 }
1451
1452 {
1453 struct ovs_action_push_eth push;
1454 int eth_type = 0;
1455 int n1 = -1;
1456
1457 if (ovs_scan(&s[n], "push_eth(src="ETH_ADDR_SCAN_FMT","
1458 "dst="ETH_ADDR_SCAN_FMT",type=%i)%n",
1459 ETH_ADDR_SCAN_ARGS(push.addresses.eth_src),
1460 ETH_ADDR_SCAN_ARGS(push.addresses.eth_dst),
1461 &eth_type, &n1)) {
1462
1463 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_ETH,
1464 &push, sizeof push);
1465
1466 res = n + n1;
1467 goto out;
1468 }
1469 }
1470
1471 if (!strncmp(&s[n], "pop_eth", 7)) {
1472 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_ETH);
1473 res = 7;
1474 goto out;
1475 }
1476
1477 res = -EINVAL;
1478 out:
1479 ofpbuf_uninit(&buf);
1480 return res;
1481 }
1482
1483 static int
1484 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
1485 {
1486 struct eth_header *eth;
1487 struct ip_header *ip;
1488 struct ovs_16aligned_ip6_hdr *ip6;
1489 struct udp_header *udp;
1490 struct gre_base_hdr *greh;
1491 struct erspan_base_hdr *ersh;
1492 struct erspan_md2 *md2;
1493 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, udp_csum, sid;
1494 ovs_be32 sip, dip;
1495 uint32_t tnl_type = 0, header_len = 0, ip_len = 0, erspan_idx = 0;
1496 void *l3, *l4;
1497 int n = 0;
1498 uint8_t hwid, dir;
1499
1500 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
1501 return -EINVAL;
1502 }
1503 eth = (struct eth_header *) data->header;
1504 l3 = (struct ip_header *) (eth + 1);
1505 ip = (struct ip_header *) l3;
1506 ip6 = (struct ovs_16aligned_ip6_hdr *) l3;
1507 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
1508 "eth(dst="ETH_ADDR_SCAN_FMT",",
1509 &data->header_len,
1510 &data->tnl_type,
1511 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
1512 return -EINVAL;
1513 }
1514
1515 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
1516 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
1517 return -EINVAL;
1518 }
1519 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
1520 return -EINVAL;
1521 }
1522 eth->eth_type = htons(dl_type);
1523
1524 if (eth->eth_type == htons(ETH_TYPE_IP)) {
1525 /* IPv4 */
1526 uint16_t ip_frag_off;
1527 memset(ip, 0, sizeof(*ip));
1528 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
1529 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
1530 IP_SCAN_ARGS(&sip),
1531 IP_SCAN_ARGS(&dip),
1532 &ip->ip_proto, &ip->ip_tos,
1533 &ip->ip_ttl, &ip_frag_off)) {
1534 return -EINVAL;
1535 }
1536 put_16aligned_be32(&ip->ip_src, sip);
1537 put_16aligned_be32(&ip->ip_dst, dip);
1538 ip->ip_frag_off = htons(ip_frag_off);
1539 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
1540 ip_len = sizeof *ip;
1541 ip->ip_csum = csum(ip, ip_len);
1542 } else {
1543 char sip6_s[IPV6_SCAN_LEN + 1];
1544 char dip6_s[IPV6_SCAN_LEN + 1];
1545 struct in6_addr sip6, dip6;
1546 uint8_t tclass;
1547 uint32_t label;
1548 if (!ovs_scan_len(s, &n, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT
1549 ",label=%i,proto=%"SCNi8",tclass=0x%"SCNx8
1550 ",hlimit=%"SCNi8"),",
1551 sip6_s, dip6_s, &label, &ip6->ip6_nxt,
1552 &tclass, &ip6->ip6_hlim)
1553 || (label & ~IPV6_LABEL_MASK) != 0
1554 || inet_pton(AF_INET6, sip6_s, &sip6) != 1
1555 || inet_pton(AF_INET6, dip6_s, &dip6) != 1) {
1556 return -EINVAL;
1557 }
1558 put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) |
1559 htonl(tclass << 20) | htonl(label));
1560 memcpy(&ip6->ip6_src, &sip6, sizeof(ip6->ip6_src));
1561 memcpy(&ip6->ip6_dst, &dip6, sizeof(ip6->ip6_dst));
1562 ip_len = sizeof *ip6;
1563 }
1564
1565 /* Tunnel header */
1566 l4 = ((uint8_t *) l3 + ip_len);
1567 udp = (struct udp_header *) l4;
1568 greh = (struct gre_base_hdr *) l4;
1569 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
1570 &udp_src, &udp_dst, &udp_csum)) {
1571 uint32_t vx_flags, vni;
1572
1573 udp->udp_src = htons(udp_src);
1574 udp->udp_dst = htons(udp_dst);
1575 udp->udp_len = 0;
1576 udp->udp_csum = htons(udp_csum);
1577
1578 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
1579 &vx_flags, &vni)) {
1580 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
1581
1582 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
1583 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
1584 tnl_type = OVS_VPORT_TYPE_VXLAN;
1585 header_len = sizeof *eth + ip_len +
1586 sizeof *udp + sizeof *vxh;
1587 } else if (ovs_scan_len(s, &n, "geneve(")) {
1588 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
1589
1590 memset(gnh, 0, sizeof *gnh);
1591 header_len = sizeof *eth + ip_len +
1592 sizeof *udp + sizeof *gnh;
1593
1594 if (ovs_scan_len(s, &n, "oam,")) {
1595 gnh->oam = 1;
1596 }
1597 if (ovs_scan_len(s, &n, "crit,")) {
1598 gnh->critical = 1;
1599 }
1600 if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
1601 return -EINVAL;
1602 }
1603 if (ovs_scan_len(s, &n, ",options(")) {
1604 struct geneve_scan options;
1605 int len;
1606
1607 memset(&options, 0, sizeof options);
1608 len = scan_geneve(s + n, &options, NULL);
1609 if (!len) {
1610 return -EINVAL;
1611 }
1612
1613 memcpy(gnh->options, options.d, options.len);
1614 gnh->opt_len = options.len / 4;
1615 header_len += options.len;
1616
1617 n += len;
1618 }
1619 if (!ovs_scan_len(s, &n, "))")) {
1620 return -EINVAL;
1621 }
1622
1623 gnh->proto_type = htons(ETH_TYPE_TEB);
1624 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
1625 tnl_type = OVS_VPORT_TYPE_GENEVE;
1626 } else {
1627 return -EINVAL;
1628 }
1629 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
1630 &gre_flags, &gre_proto)){
1631
1632 if (eth->eth_type == htons(ETH_TYPE_IP)) {
1633 tnl_type = OVS_VPORT_TYPE_GRE;
1634 } else {
1635 tnl_type = OVS_VPORT_TYPE_IP6GRE;
1636 }
1637 greh->flags = htons(gre_flags);
1638 greh->protocol = htons(gre_proto);
1639 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
1640
1641 if (greh->flags & htons(GRE_CSUM)) {
1642 uint16_t csum;
1643 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
1644 return -EINVAL;
1645 }
1646
1647 memset(options, 0, sizeof *options);
1648 *((ovs_be16 *)options) = htons(csum);
1649 options++;
1650 }
1651 if (greh->flags & htons(GRE_KEY)) {
1652 uint32_t key;
1653
1654 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
1655 return -EINVAL;
1656 }
1657
1658 put_16aligned_be32(options, htonl(key));
1659 options++;
1660 }
1661 if (greh->flags & htons(GRE_SEQ)) {
1662 uint32_t seq;
1663
1664 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
1665 return -EINVAL;
1666 }
1667 put_16aligned_be32(options, htonl(seq));
1668 options++;
1669 }
1670
1671 if (!ovs_scan_len(s, &n, "))")) {
1672 return -EINVAL;
1673 }
1674
1675 header_len = sizeof *eth + ip_len +
1676 ((uint8_t *) options - (uint8_t *) greh);
1677 } else if (ovs_scan_len(s, &n, "erspan(ver=1,sid="SCNx16",idx=0x"SCNx32")",
1678 &sid, &erspan_idx)) {
1679 ersh = ERSPAN_HDR(greh);
1680 ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *,
1681 ersh + 1);
1682
1683 if (eth->eth_type == htons(ETH_TYPE_IP)) {
1684 tnl_type = OVS_VPORT_TYPE_ERSPAN;
1685 } else {
1686 tnl_type = OVS_VPORT_TYPE_IP6ERSPAN;
1687 }
1688
1689 greh->flags = htons(GRE_SEQ);
1690 greh->protocol = htons(ETH_TYPE_ERSPAN1);
1691
1692 ersh->ver = 1;
1693 set_sid(ersh, sid);
1694 put_16aligned_be32(index, htonl(erspan_idx));
1695
1696 if (!ovs_scan_len(s, &n, ")")) {
1697 return -EINVAL;
1698 }
1699 header_len = sizeof *eth + ip_len + ERSPAN_GREHDR_LEN +
1700 sizeof *ersh + ERSPAN_V1_MDSIZE;
1701
1702 } else if (ovs_scan_len(s, &n, "erspan(ver=2,sid="SCNx16"dir="SCNu8
1703 ",hwid=0x"SCNx8")", &sid, &dir, &hwid)) {
1704
1705 ersh = ERSPAN_HDR(greh);
1706 md2 = ALIGNED_CAST(struct erspan_md2 *, ersh + 1);
1707
1708 if (eth->eth_type == htons(ETH_TYPE_IP)) {
1709 tnl_type = OVS_VPORT_TYPE_ERSPAN;
1710 } else {
1711 tnl_type = OVS_VPORT_TYPE_IP6ERSPAN;
1712 }
1713
1714 greh->flags = htons(GRE_SEQ);
1715 greh->protocol = htons(ETH_TYPE_ERSPAN2);
1716
1717 ersh->ver = 2;
1718 set_sid(ersh, sid);
1719 set_hwid(md2, hwid);
1720 md2->dir = dir;
1721
1722 if (!ovs_scan_len(s, &n, ")")) {
1723 return -EINVAL;
1724 }
1725
1726 header_len = sizeof *eth + ip_len + ERSPAN_GREHDR_LEN +
1727 sizeof *ersh + ERSPAN_V2_MDSIZE;
1728 } else {
1729 return -EINVAL;
1730 }
1731
1732 /* check tunnel meta data. */
1733 if (data->tnl_type != tnl_type) {
1734 return -EINVAL;
1735 }
1736 if (data->header_len != header_len) {
1737 return -EINVAL;
1738 }
1739
1740 /* Out port */
1741 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1742 return -EINVAL;
1743 }
1744
1745 return n;
1746 }
1747
1748 struct ct_nat_params {
1749 bool snat;
1750 bool dnat;
1751 size_t addr_len;
1752 union {
1753 ovs_be32 ip;
1754 struct in6_addr ip6;
1755 } addr_min;
1756 union {
1757 ovs_be32 ip;
1758 struct in6_addr ip6;
1759 } addr_max;
1760 uint16_t proto_min;
1761 uint16_t proto_max;
1762 bool persistent;
1763 bool proto_hash;
1764 bool proto_random;
1765 };
1766
1767 static int
1768 scan_ct_nat_range(const char *s, int *n, struct ct_nat_params *p)
1769 {
1770 if (ovs_scan_len(s, n, "=")) {
1771 char ipv6_s[IPV6_SCAN_LEN + 1];
1772 struct in6_addr ipv6;
1773
1774 if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) {
1775 p->addr_len = sizeof p->addr_min.ip;
1776 if (ovs_scan_len(s, n, "-")) {
1777 if (!ovs_scan_len(s, n, IP_SCAN_FMT,
1778 IP_SCAN_ARGS(&p->addr_max.ip))) {
1779 return -EINVAL;
1780 }
1781 }
1782 } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
1783 || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
1784 && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1785 p->addr_len = sizeof p->addr_min.ip6;
1786 p->addr_min.ip6 = ipv6;
1787 if (ovs_scan_len(s, n, "-")) {
1788 if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
1789 || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
1790 && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1791 p->addr_max.ip6 = ipv6;
1792 } else {
1793 return -EINVAL;
1794 }
1795 }
1796 } else {
1797 return -EINVAL;
1798 }
1799 if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) {
1800 if (ovs_scan_len(s, n, "-")) {
1801 if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) {
1802 return -EINVAL;
1803 }
1804 }
1805 }
1806 }
1807 return 0;
1808 }
1809
1810 static int
1811 scan_ct_nat(const char *s, struct ct_nat_params *p)
1812 {
1813 int n = 0;
1814
1815 if (ovs_scan_len(s, &n, "nat")) {
1816 memset(p, 0, sizeof *p);
1817
1818 if (ovs_scan_len(s, &n, "(")) {
1819 char *end;
1820 int end_n;
1821
1822 end = strchr(s + n, ')');
1823 if (!end) {
1824 return -EINVAL;
1825 }
1826 end_n = end - s;
1827
1828 while (n < end_n) {
1829 n += strspn(s + n, delimiters);
1830 if (ovs_scan_len(s, &n, "src")) {
1831 int err = scan_ct_nat_range(s, &n, p);
1832 if (err) {
1833 return err;
1834 }
1835 p->snat = true;
1836 continue;
1837 }
1838 if (ovs_scan_len(s, &n, "dst")) {
1839 int err = scan_ct_nat_range(s, &n, p);
1840 if (err) {
1841 return err;
1842 }
1843 p->dnat = true;
1844 continue;
1845 }
1846 if (ovs_scan_len(s, &n, "persistent")) {
1847 p->persistent = true;
1848 continue;
1849 }
1850 if (ovs_scan_len(s, &n, "hash")) {
1851 p->proto_hash = true;
1852 continue;
1853 }
1854 if (ovs_scan_len(s, &n, "random")) {
1855 p->proto_random = true;
1856 continue;
1857 }
1858 return -EINVAL;
1859 }
1860
1861 if (p->snat && p->dnat) {
1862 return -EINVAL;
1863 }
1864 if ((p->addr_len != 0 &&
1865 memcmp(&p->addr_max, &in6addr_any, p->addr_len) &&
1866 memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) ||
1867 (p->proto_max && p->proto_max < p->proto_min)) {
1868 return -EINVAL;
1869 }
1870 if (p->proto_hash && p->proto_random) {
1871 return -EINVAL;
1872 }
1873 n++;
1874 }
1875 }
1876 return n;
1877 }
1878
1879 static void
1880 nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions)
1881 {
1882 size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT);
1883
1884 if (p->snat) {
1885 nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC);
1886 } else if (p->dnat) {
1887 nl_msg_put_flag(actions, OVS_NAT_ATTR_DST);
1888 } else {
1889 goto out;
1890 }
1891 if (p->addr_len != 0) {
1892 nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min,
1893 p->addr_len);
1894 if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) {
1895 nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max,
1896 p->addr_len);
1897 }
1898 if (p->proto_min) {
1899 nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min);
1900 if (p->proto_max && p->proto_max > p->proto_min) {
1901 nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max);
1902 }
1903 }
1904 if (p->persistent) {
1905 nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT);
1906 }
1907 if (p->proto_hash) {
1908 nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH);
1909 }
1910 if (p->proto_random) {
1911 nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM);
1912 }
1913 }
1914 out:
1915 nl_msg_end_nested(actions, start);
1916 }
1917
1918 static int
1919 parse_conntrack_action(const char *s_, struct ofpbuf *actions)
1920 {
1921 const char *s = s_;
1922
1923 if (ovs_scan(s, "ct")) {
1924 const char *helper = NULL, *timeout = NULL;
1925 size_t helper_len = 0, timeout_len = 0;
1926 bool commit = false;
1927 bool force_commit = false;
1928 uint16_t zone = 0;
1929 struct {
1930 uint32_t value;
1931 uint32_t mask;
1932 } ct_mark = { 0, 0 };
1933 struct {
1934 ovs_u128 value;
1935 ovs_u128 mask;
1936 } ct_label;
1937 struct ct_nat_params nat_params;
1938 bool have_nat = false;
1939 size_t start;
1940 char *end;
1941
1942 memset(&ct_label, 0, sizeof(ct_label));
1943
1944 s += 2;
1945 if (ovs_scan(s, "(")) {
1946 s++;
1947 find_end:
1948 end = strchr(s, ')');
1949 if (!end) {
1950 return -EINVAL;
1951 }
1952
1953 while (s != end) {
1954 int n;
1955
1956 s += strspn(s, delimiters);
1957 if (ovs_scan(s, "commit%n", &n)) {
1958 commit = true;
1959 s += n;
1960 continue;
1961 }
1962 if (ovs_scan(s, "force_commit%n", &n)) {
1963 force_commit = true;
1964 s += n;
1965 continue;
1966 }
1967 if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) {
1968 s += n;
1969 continue;
1970 }
1971 if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) {
1972 s += n;
1973 n = -1;
1974 if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) {
1975 s += n;
1976 } else {
1977 ct_mark.mask = UINT32_MAX;
1978 }
1979 continue;
1980 }
1981 if (ovs_scan(s, "label=%n", &n)) {
1982 int retval;
1983
1984 s += n;
1985 retval = scan_u128(s, &ct_label.value, &ct_label.mask);
1986 if (retval == 0) {
1987 return -EINVAL;
1988 }
1989 s += retval;
1990 continue;
1991 }
1992 if (ovs_scan(s, "helper=%n", &n)) {
1993 s += n;
1994 helper_len = strcspn(s, delimiters_end);
1995 if (!helper_len || helper_len > 15) {
1996 return -EINVAL;
1997 }
1998 helper = s;
1999 s += helper_len;
2000 continue;
2001 }
2002 if (ovs_scan(s, "timeout=%n", &n)) {
2003 s += n;
2004 timeout_len = strcspn(s, delimiters_end);
2005 if (!timeout_len || timeout_len > 31) {
2006 return -EINVAL;
2007 }
2008 timeout = s;
2009 s += timeout_len;
2010 continue;
2011 }
2012
2013 n = scan_ct_nat(s, &nat_params);
2014 if (n > 0) {
2015 s += n;
2016 have_nat = true;
2017
2018 /* end points to the end of the nested, nat action.
2019 * find the real end. */
2020 goto find_end;
2021 }
2022 /* Nothing matched. */
2023 return -EINVAL;
2024 }
2025 s++;
2026 }
2027 if (commit && force_commit) {
2028 return -EINVAL;
2029 }
2030
2031 start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT);
2032 if (commit) {
2033 nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT);
2034 } else if (force_commit) {
2035 nl_msg_put_flag(actions, OVS_CT_ATTR_FORCE_COMMIT);
2036 }
2037 if (zone) {
2038 nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone);
2039 }
2040 if (ct_mark.mask) {
2041 nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark,
2042 sizeof(ct_mark));
2043 }
2044 if (!ovs_u128_is_zero(ct_label.mask)) {
2045 nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label,
2046 sizeof ct_label);
2047 }
2048 if (helper) {
2049 nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper,
2050 helper_len);
2051 }
2052 if (timeout) {
2053 nl_msg_put_string__(actions, OVS_CT_ATTR_TIMEOUT, timeout,
2054 timeout_len);
2055 }
2056 if (have_nat) {
2057 nl_msg_put_ct_nat(&nat_params, actions);
2058 }
2059 nl_msg_end_nested(actions, start);
2060 }
2061
2062 return s - s_;
2063 }
2064
2065 static void
2066 nsh_key_to_attr(struct ofpbuf *buf, const struct ovs_key_nsh *nsh,
2067 uint8_t * metadata, size_t md_size,
2068 bool is_mask)
2069 {
2070 size_t nsh_key_ofs;
2071 struct ovs_nsh_key_base base;
2072
2073 base.flags = nsh->flags;
2074 base.ttl = nsh->ttl;
2075 base.mdtype = nsh->mdtype;
2076 base.np = nsh->np;
2077 base.path_hdr = nsh->path_hdr;
2078
2079 nsh_key_ofs = nl_msg_start_nested(buf, OVS_KEY_ATTR_NSH);
2080 nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_BASE, &base, sizeof base);
2081
2082 if (is_mask) {
2083 nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context,
2084 sizeof nsh->context);
2085 } else {
2086 switch (nsh->mdtype) {
2087 case NSH_M_TYPE1:
2088 nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context,
2089 sizeof nsh->context);
2090 break;
2091 case NSH_M_TYPE2:
2092 if (metadata && md_size > 0) {
2093 nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD2, metadata,
2094 md_size);
2095 }
2096 break;
2097 default:
2098 /* No match support for other MD formats yet. */
2099 break;
2100 }
2101 }
2102 nl_msg_end_nested(buf, nsh_key_ofs);
2103 }
2104
2105
2106 static int
2107 parse_odp_push_nsh_action(const char *s, struct ofpbuf *actions)
2108 {
2109 int n = 0;
2110 int ret = 0;
2111 uint32_t spi = 0;
2112 uint8_t si = 255;
2113 uint32_t cd;
2114 struct ovs_key_nsh nsh;
2115 uint8_t metadata[NSH_CTX_HDRS_MAX_LEN];
2116 uint8_t md_size = 0;
2117
2118 if (!ovs_scan_len(s, &n, "push_nsh(")) {
2119 ret = -EINVAL;
2120 goto out;
2121 }
2122
2123 /* The default is NSH_M_TYPE1 */
2124 nsh.flags = 0;
2125 nsh.ttl = 63;
2126 nsh.mdtype = NSH_M_TYPE1;
2127 nsh.np = NSH_P_ETHERNET;
2128 nsh.path_hdr = nsh_spi_si_to_path_hdr(0, 255);
2129 memset(nsh.context, 0, NSH_M_TYPE1_MDLEN);
2130
2131 for (;;) {
2132 n += strspn(s + n, delimiters);
2133 if (s[n] == ')') {
2134 break;
2135 }
2136
2137 if (ovs_scan_len(s, &n, "flags=%"SCNi8, &nsh.flags)) {
2138 continue;
2139 }
2140 if (ovs_scan_len(s, &n, "ttl=%"SCNi8, &nsh.ttl)) {
2141 continue;
2142 }
2143 if (ovs_scan_len(s, &n, "mdtype=%"SCNi8, &nsh.mdtype)) {
2144 switch (nsh.mdtype) {
2145 case NSH_M_TYPE1:
2146 /* This is the default format. */;
2147 break;
2148 case NSH_M_TYPE2:
2149 /* Length will be updated later. */
2150 md_size = 0;
2151 break;
2152 default:
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156 continue;
2157 }
2158 if (ovs_scan_len(s, &n, "np=%"SCNi8, &nsh.np)) {
2159 continue;
2160 }
2161 if (ovs_scan_len(s, &n, "spi=0x%"SCNx32, &spi)) {
2162 continue;
2163 }
2164 if (ovs_scan_len(s, &n, "si=%"SCNi8, &si)) {
2165 continue;
2166 }
2167 if (nsh.mdtype == NSH_M_TYPE1) {
2168 if (ovs_scan_len(s, &n, "c1=0x%"SCNx32, &cd)) {
2169 nsh.context[0] = htonl(cd);
2170 continue;
2171 }
2172 if (ovs_scan_len(s, &n, "c2=0x%"SCNx32, &cd)) {
2173 nsh.context[1] = htonl(cd);
2174 continue;
2175 }
2176 if (ovs_scan_len(s, &n, "c3=0x%"SCNx32, &cd)) {
2177 nsh.context[2] = htonl(cd);
2178 continue;
2179 }
2180 if (ovs_scan_len(s, &n, "c4=0x%"SCNx32, &cd)) {
2181 nsh.context[3] = htonl(cd);
2182 continue;
2183 }
2184 }
2185 else if (nsh.mdtype == NSH_M_TYPE2) {
2186 struct ofpbuf b;
2187 char buf[512];
2188 size_t mdlen, padding;
2189 if (ovs_scan_len(s, &n, "md2=0x%511[0-9a-fA-F]", buf)
2190 && n/2 <= sizeof metadata) {
2191 ofpbuf_use_stub(&b, metadata, sizeof metadata);
2192 ofpbuf_put_hex(&b, buf, &mdlen);
2193 /* Pad metadata to 4 bytes. */
2194 padding = PAD_SIZE(mdlen, 4);
2195 if (padding > 0) {
2196 ofpbuf_put_zeros(&b, padding);
2197 }
2198 md_size = mdlen + padding;
2199 ofpbuf_uninit(&b);
2200 continue;
2201 }
2202 }
2203
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207 out:
2208 if (ret >= 0) {
2209 nsh.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
2210 size_t offset = nl_msg_start_nested(actions, OVS_ACTION_ATTR_PUSH_NSH);
2211 nsh_key_to_attr(actions, &nsh, metadata, md_size, false);
2212 nl_msg_end_nested(actions, offset);
2213 ret = n;
2214 }
2215 return ret;
2216 }
2217
2218 static int
2219 parse_action_list(struct parse_odp_context *context, const char *s,
2220 struct ofpbuf *actions)
2221 {
2222 int n = 0;
2223
2224 for (;;) {
2225 int retval;
2226
2227 n += strspn(s + n, delimiters);
2228 if (s[n] == ')') {
2229 break;
2230 }
2231 retval = parse_odp_action(context, s + n, actions);
2232 if (retval < 0) {
2233 return retval;
2234 }
2235 n += retval;
2236 }
2237
2238 if (actions->size > UINT16_MAX) {
2239 return -EFBIG;
2240 }
2241
2242 return n;
2243 }
2244
2245
2246 static int
2247 parse_odp_action(struct parse_odp_context *context, const char *s,
2248 struct ofpbuf *actions)
2249 {
2250 int retval;
2251
2252 context->depth++;
2253
2254 if (context->depth == MAX_ODP_NESTED) {
2255 retval = -EINVAL;
2256 } else {
2257 retval = parse_odp_action__(context, s, actions);
2258 }
2259
2260 context->depth--;
2261
2262 return retval;
2263 }
2264
2265
2266 static int
2267 parse_odp_action__(struct parse_odp_context *context, const char *s,
2268 struct ofpbuf *actions)
2269 {
2270 {
2271 uint32_t port;
2272 int n;
2273
2274 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
2275 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
2276 return n;
2277 }
2278 }
2279
2280 {
2281 uint32_t max_len;
2282 int n;
2283
2284 if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) {
2285 struct ovs_action_trunc *trunc;
2286
2287 trunc = nl_msg_put_unspec_uninit(actions,
2288 OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
2289 trunc->max_len = max_len;
2290 return n;
2291 }
2292 }
2293
2294 if (context->port_names) {
2295 int len = strcspn(s, delimiters);
2296 struct simap_node *node;
2297
2298 node = simap_find_len(context->port_names, s, len);
2299 if (node) {
2300 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
2301 return len;
2302 }
2303 }
2304
2305 {
2306 uint32_t recirc_id;
2307 int n = -1;
2308
2309 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
2310 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
2311 return n;
2312 }
2313 }
2314
2315 if (!strncmp(s, "userspace(", 10)) {
2316 return parse_odp_userspace_action(s, actions);
2317 }
2318
2319 if (!strncmp(s, "set(", 4)) {
2320 size_t start_ofs;
2321 int retval;
2322 struct nlattr mask[1024 / sizeof(struct nlattr)];
2323 struct ofpbuf maskbuf = OFPBUF_STUB_INITIALIZER(mask);
2324 struct nlattr *nested, *key;
2325 size_t size;
2326
2327 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
2328 retval = parse_odp_key_mask_attr(context, s + 4, actions, &maskbuf);
2329 if (retval < 0) {
2330 ofpbuf_uninit(&maskbuf);
2331 return retval;
2332 }
2333 if (s[retval + 4] != ')') {
2334 ofpbuf_uninit(&maskbuf);
2335 return -EINVAL;
2336 }
2337
2338 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2339 key = nested + 1;
2340
2341 size = nl_attr_get_size(mask);
2342 if (size == nl_attr_get_size(key)) {
2343 /* Change to masked set action if not fully masked. */
2344 if (!is_all_ones(mask + 1, size)) {
2345 /* Remove padding of eariler key payload */
2346 actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len;
2347
2348 /* Put mask payload right after key payload */
2349 key->nla_len += size;
2350 ofpbuf_put(actions, mask + 1, size);
2351
2352 /* 'actions' may have been reallocated by ofpbuf_put(). */
2353 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2354 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
2355
2356 key = nested + 1;
2357 /* Add new padding as needed */
2358 ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) -
2359 key->nla_len);
2360 }
2361 }
2362 ofpbuf_uninit(&maskbuf);
2363
2364 nl_msg_end_nested(actions, start_ofs);
2365 return retval + 5;
2366 }
2367
2368 {
2369 struct ovs_action_push_vlan push;
2370 int tpid = ETH_TYPE_VLAN;
2371 int vid, pcp;
2372 int cfi = 1;
2373 int n = -1;
2374
2375 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
2376 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2377 &vid, &pcp, &cfi, &n)
2378 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2379 &tpid, &vid, &pcp, &n)
2380 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2381 &tpid, &vid, &pcp, &cfi, &n)) {
2382 if ((vid & ~(VLAN_VID_MASK >> VLAN_VID_SHIFT)) != 0
2383 || (pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) != 0) {
2384 return -EINVAL;
2385 }
2386 push.vlan_tpid = htons(tpid);
2387 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
2388 | (pcp << VLAN_PCP_SHIFT)
2389 | (cfi ? VLAN_CFI : 0));
2390 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
2391 &push, sizeof push);
2392
2393 return n;
2394 }
2395 }
2396
2397 if (!strncmp(s, "pop_vlan", 8)) {
2398 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
2399 return 8;
2400 }
2401
2402 {
2403 unsigned long long int meter_id;
2404 int n = -1;
2405
2406 if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) {
2407 nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id);
2408 return n;
2409 }
2410 }
2411
2412 {
2413 double percentage;
2414 int n = -1;
2415
2416 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
2417 && percentage >= 0. && percentage <= 100.0) {
2418 size_t sample_ofs, actions_ofs;
2419 double probability;
2420
2421 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
2422 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
2423 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
2424 (probability <= 0 ? 0
2425 : probability >= UINT32_MAX ? UINT32_MAX
2426 : probability));
2427
2428 actions_ofs = nl_msg_start_nested(actions,
2429 OVS_SAMPLE_ATTR_ACTIONS);
2430 int retval = parse_action_list(context, s + n, actions);
2431 if (retval < 0) {
2432 return retval;
2433 }
2434
2435
2436 n += retval;
2437 nl_msg_end_nested(actions, actions_ofs);
2438 nl_msg_end_nested(actions, sample_ofs);
2439
2440 return s[n + 1] == ')' ? n + 2 : -EINVAL;
2441 }
2442 }
2443
2444 {
2445 if (!strncmp(s, "clone(", 6)) {
2446 size_t actions_ofs;
2447 int n = 6;
2448
2449 actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE);
2450 int retval = parse_action_list(context, s + n, actions);
2451 if (retval < 0) {
2452 return retval;
2453 }
2454 n += retval;
2455 nl_msg_end_nested(actions, actions_ofs);
2456 return n + 1;
2457 }
2458 }
2459
2460 {
2461 if (!strncmp(s, "push_nsh(", 9)) {
2462 int retval = parse_odp_push_nsh_action(s, actions);
2463 if (retval < 0) {
2464 return retval;
2465 }
2466 return retval + 1;
2467 }
2468 }
2469
2470 {
2471 int n;
2472 if (ovs_scan(s, "pop_nsh()%n", &n)) {
2473 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_NSH);
2474 return n;
2475 }
2476 }
2477
2478 {
2479 uint32_t port;
2480 int n;
2481
2482 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
2483 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
2484 return n;
2485 }
2486 }
2487
2488 {
2489 if (!strncmp(s, "ct_clear", 8)) {
2490 nl_msg_put_flag(actions, OVS_ACTION_ATTR_CT_CLEAR);
2491 return 8;
2492 }
2493 }
2494
2495 {
2496 uint16_t pkt_len;
2497 int n = -1;
2498 if (ovs_scan(s, "check_pkt_len(size=%"SCNi16",gt(%n", &pkt_len, &n)) {
2499 size_t cpl_ofs, actions_ofs;
2500 cpl_ofs = nl_msg_start_nested(actions,
2501 OVS_ACTION_ATTR_CHECK_PKT_LEN);
2502 nl_msg_put_u16(actions, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, pkt_len);
2503 actions_ofs = nl_msg_start_nested(
2504 actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
2505
2506 int retval;
2507 if (!strncasecmp(s + n, "drop", 4)) {
2508 n += 4;
2509 } else {
2510 retval = parse_action_list(context, s + n, actions);
2511 if (retval < 0) {
2512 return retval;
2513 }
2514
2515 n += retval;
2516 }
2517 nl_msg_end_nested(actions, actions_ofs);
2518 retval = -1;
2519 if (!ovs_scan(s + n, "),le(%n", &retval)) {
2520 return -EINVAL;
2521 }
2522 n += retval;
2523
2524 actions_ofs = nl_msg_start_nested(
2525 actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
2526 if (!strncasecmp(s + n, "drop", 4)) {
2527 n += 4;
2528 } else {
2529 retval = parse_action_list(context, s + n, actions);
2530 if (retval < 0) {
2531 return retval;
2532 }
2533 n += retval;
2534 }
2535 nl_msg_end_nested(actions, actions_ofs);
2536 nl_msg_end_nested(actions, cpl_ofs);
2537 return s[n + 1] == ')' ? n + 2 : -EINVAL;
2538 }
2539 }
2540
2541 {
2542 int retval;
2543
2544 retval = parse_conntrack_action(s, actions);
2545 if (retval) {
2546 return retval;
2547 }
2548 }
2549
2550 {
2551 struct ovs_action_push_tnl data;
2552 int n;
2553
2554 n = ovs_parse_tnl_push(s, &data);
2555 if (n > 0) {
2556 odp_put_tnl_push_action(actions, &data);
2557 return n;
2558 } else if (n < 0) {
2559 return n;
2560 }
2561 }
2562
2563 return -EINVAL;
2564 }
2565
2566 /* Parses the string representation of datapath actions, in the format output
2567 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2568 * value. On success, the ODP actions are appended to 'actions' as a series of
2569 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2570 * way, 'actions''s data might be reallocated. */
2571 int
2572 odp_actions_from_string(const char *s, const struct simap *port_names,
2573 struct ofpbuf *actions)
2574 {
2575 size_t old_size;
2576
2577 if (!strcasecmp(s, "drop")) {
2578 return 0;
2579 }
2580
2581 struct parse_odp_context context = (struct parse_odp_context) {
2582 .port_names = port_names,
2583 };
2584
2585 old_size = actions->size;
2586 for (;;) {
2587 int retval;
2588
2589 s += strspn(s, delimiters);
2590 if (!*s) {
2591 return 0;
2592 }
2593
2594 retval = parse_odp_action(&context, s, actions);
2595
2596 if (retval < 0 || !strchr(delimiters, s[retval])) {
2597 actions->size = old_size;
2598 return -retval;
2599 }
2600 s += retval;
2601 }
2602
2603 return 0;
2604 }
2605 \f
2606 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
2607 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
2608 };
2609
2610 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
2611 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
2612 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
2613 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
2614 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
2615 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
2616 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
2617 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
2618 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
2619 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
2620 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
2621 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
2622 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
2623 .next = ovs_vxlan_ext_attr_lens ,
2624 .next_max = OVS_VXLAN_EXT_MAX},
2625 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = 16 },
2626 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = 16 },
2627 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = ATTR_LEN_VARIABLE },
2628 };
2629
2630 const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
2631 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
2632 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
2633 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
2634 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
2635 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
2636 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
2637 .next = ovs_tun_key_attr_lens,
2638 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
2639 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
2640 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
2641 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
2642 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
2643 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
2644 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
2645 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
2646 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
2647 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
2648 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
2649 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
2650 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
2651 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
2652 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
2653 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
2654 [OVS_KEY_ATTR_ND_EXTENSIONS] = { .len = sizeof(struct ovs_key_nd_extensions) },
2655 [OVS_KEY_ATTR_CT_STATE] = { .len = 4 },
2656 [OVS_KEY_ATTR_CT_ZONE] = { .len = 2 },
2657 [OVS_KEY_ATTR_CT_MARK] = { .len = 4 },
2658 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
2659 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
2660 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
2661 [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4 },
2662 [OVS_KEY_ATTR_NSH] = { .len = ATTR_LEN_NESTED,
2663 .next = ovs_nsh_key_attr_lens,
2664 .next_max = OVS_NSH_KEY_ATTR_MAX },
2665 };
2666
2667 /* Returns the correct length of the payload for a flow key attribute of the
2668 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2669 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2670 * payload is a nested type. */
2671 static int
2672 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type)
2673 {
2674 if (type > max_type) {
2675 return ATTR_LEN_INVALID;
2676 }
2677
2678 return tbl[type].len;
2679 }
2680
2681 static void
2682 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
2683 {
2684 size_t len = nl_attr_get_size(a);
2685 if (len) {
2686 const uint8_t *unspec;
2687 unsigned int i;
2688
2689 unspec = nl_attr_get(a);
2690 for (i = 0; i < len; i++) {
2691 if (i) {
2692 ds_put_char(ds, ' ');
2693 }
2694 ds_put_format(ds, "%02x", unspec[i]);
2695 }
2696 }
2697 }
2698
2699 static const char *
2700 ovs_frag_type_to_string(enum ovs_frag_type type)
2701 {
2702 switch (type) {
2703 case OVS_FRAG_TYPE_NONE:
2704 return "no";
2705 case OVS_FRAG_TYPE_FIRST:
2706 return "first";
2707 case OVS_FRAG_TYPE_LATER:
2708 return "later";
2709 case __OVS_FRAG_TYPE_MAX:
2710 default:
2711 return "<error>";
2712 }
2713 }
2714
2715 enum odp_key_fitness
2716 odp_nsh_hdr_from_attr(const struct nlattr *attr,
2717 struct nsh_hdr *nsh_hdr, size_t size)
2718 {
2719 unsigned int left;
2720 const struct nlattr *a;
2721 bool unknown = false;
2722 uint8_t flags = 0;
2723 uint8_t ttl = 63;
2724 size_t mdlen = 0;
2725 bool has_md1 = false;
2726 bool has_md2 = false;
2727
2728 memset(nsh_hdr, 0, size);
2729
2730 NL_NESTED_FOR_EACH (a, left, attr) {
2731 uint16_t type = nl_attr_type(a);
2732 size_t len = nl_attr_get_size(a);
2733 int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
2734 OVS_NSH_KEY_ATTR_MAX, type);
2735
2736 if (len != expected_len && expected_len >= 0) {
2737 return ODP_FIT_ERROR;
2738 }
2739
2740 switch (type) {
2741 case OVS_NSH_KEY_ATTR_BASE: {
2742 const struct ovs_nsh_key_base *base = nl_attr_get(a);
2743 nsh_hdr->next_proto = base->np;
2744 nsh_hdr->md_type = base->mdtype;
2745 put_16aligned_be32(&nsh_hdr->path_hdr, base->path_hdr);
2746 flags = base->flags;
2747 ttl = base->ttl;
2748 break;
2749 }
2750 case OVS_NSH_KEY_ATTR_MD1: {
2751 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
2752 struct nsh_md1_ctx *md1_dst = &nsh_hdr->md1;
2753 has_md1 = true;
2754 mdlen = nl_attr_get_size(a);
2755 if ((mdlen + NSH_BASE_HDR_LEN != NSH_M_TYPE1_LEN) ||
2756 (mdlen + NSH_BASE_HDR_LEN > size)) {
2757 return ODP_FIT_ERROR;
2758 }
2759 memcpy(md1_dst, md1, mdlen);
2760 break;
2761 }
2762 case OVS_NSH_KEY_ATTR_MD2: {
2763 struct nsh_md2_tlv *md2_dst = &nsh_hdr->md2;
2764 const uint8_t *md2 = nl_attr_get(a);
2765 has_md2 = true;
2766 mdlen = nl_attr_get_size(a);
2767 if (mdlen + NSH_BASE_HDR_LEN > size) {
2768 return ODP_FIT_ERROR;
2769 }
2770 memcpy(md2_dst, md2, mdlen);
2771 break;
2772 }
2773 default:
2774 /* Allow this to show up as unexpected, if there are unknown
2775 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2776 unknown = true;
2777 break;
2778 }
2779 }
2780
2781 if (unknown) {
2782 return ODP_FIT_TOO_MUCH;
2783 }
2784
2785 if ((has_md1 && nsh_hdr->md_type != NSH_M_TYPE1)
2786 || (has_md2 && nsh_hdr->md_type != NSH_M_TYPE2)) {
2787 return ODP_FIT_ERROR;
2788 }
2789
2790 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2791 nsh_set_flags_ttl_len(nsh_hdr, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
2792
2793 return ODP_FIT_PERFECT;
2794 }
2795
2796 /* Reports the error 'msg', which is formatted as with printf().
2797 *
2798 * If 'errorp' is nonnull, then some the wants the error report to come
2799 * directly back to it, so the function stores the error message into '*errorp'
2800 * (after first freeing it in case there's something there already).
2801 *
2802 * Otherwise, logs the message at WARN level, rate-limited. */
2803 static void OVS_PRINTF_FORMAT(3, 4)
2804 odp_parse_error(struct vlog_rate_limit *rl, char **errorp,
2805 const char *msg, ...)
2806 {
2807 if (OVS_UNLIKELY(errorp)) {
2808 free(*errorp);
2809
2810 va_list args;
2811 va_start(args, msg);
2812 *errorp = xvasprintf(msg, args);
2813 va_end(args);
2814 } else if (!VLOG_DROP_WARN(rl)) {
2815 va_list args;
2816 va_start(args, msg);
2817 char *error = xvasprintf(msg, args);
2818 va_end(args);
2819
2820 VLOG_WARN("%s", error);
2821
2822 free(error);
2823 }
2824 }
2825
2826 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2827 * returns fitness. If the attribute is a key, 'is_mask' should be false;
2828 * if it is a mask, 'is_mask' should be true. If 'errorp' is nonnull and the
2829 * function returns ODP_FIT_ERROR, stores a malloc()'d error message in
2830 * '*errorp'. */
2831 static enum odp_key_fitness
2832 odp_nsh_key_from_attr__(const struct nlattr *attr, bool is_mask,
2833 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask,
2834 char **errorp)
2835 {
2836 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2837 if (errorp) {
2838 *errorp = NULL;
2839 }
2840
2841 unsigned int left;
2842 const struct nlattr *a;
2843 bool unknown = false;
2844 bool has_md1 = false;
2845
2846 NL_NESTED_FOR_EACH (a, left, attr) {
2847 uint16_t type = nl_attr_type(a);
2848 size_t len = nl_attr_get_size(a);
2849 int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
2850 OVS_NSH_KEY_ATTR_MAX, type);
2851 if (expected_len) {
2852 if (nsh_mask) {
2853 expected_len *= 2;
2854 }
2855 if (len != expected_len) {
2856 odp_parse_error(&rl, errorp, "NSH %s attribute %"PRIu16" "
2857 "should have length %d but actually has "
2858 "%"PRIuSIZE,
2859 nsh_mask ? "mask" : "key",
2860 type, expected_len, len);
2861 return ODP_FIT_ERROR;
2862 }
2863 }
2864
2865 switch (type) {
2866 case OVS_NSH_KEY_ATTR_UNSPEC:
2867 break;
2868 case OVS_NSH_KEY_ATTR_BASE: {
2869 const struct ovs_nsh_key_base *base = nl_attr_get(a);
2870 nsh->flags = base->flags;
2871 nsh->ttl = base->ttl;
2872 nsh->mdtype = base->mdtype;
2873 nsh->np = base->np;
2874 nsh->path_hdr = base->path_hdr;
2875 if (nsh_mask && (len == 2 * sizeof(*base))) {
2876 const struct ovs_nsh_key_base *base_mask = base + 1;
2877 nsh_mask->flags = base_mask->flags;
2878 nsh_mask->ttl = base_mask->ttl;
2879 nsh_mask->mdtype = base_mask->mdtype;
2880 nsh_mask->np = base_mask->np;
2881 nsh_mask->path_hdr = base_mask->path_hdr;
2882 }
2883 break;
2884 }
2885 case OVS_NSH_KEY_ATTR_MD1: {
2886 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
2887 has_md1 = true;
2888 memcpy(nsh->context, md1->context, sizeof md1->context);
2889 if (len == 2 * sizeof(*md1)) {
2890 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
2891 memcpy(nsh_mask->context, md1_mask->context,
2892 sizeof(*md1_mask));
2893 }
2894 break;
2895 }
2896 case OVS_NSH_KEY_ATTR_MD2:
2897 default:
2898 /* Allow this to show up as unexpected, if there are unknown
2899 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2900 unknown = true;
2901 break;
2902 }
2903 }
2904
2905 if (unknown) {
2906 return ODP_FIT_TOO_MUCH;
2907 }
2908
2909 if (!is_mask && has_md1 && nsh->mdtype != NSH_M_TYPE1 && !nsh_mask) {
2910 odp_parse_error(&rl, errorp, "OVS_NSH_KEY_ATTR_MD1 present but "
2911 "declared mdtype %"PRIu8" is not %d (NSH_M_TYPE1)",
2912 nsh->mdtype, NSH_M_TYPE1);
2913 return ODP_FIT_ERROR;
2914 }
2915
2916 return ODP_FIT_PERFECT;
2917 }
2918
2919 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2920 * returns fitness. The attribute should be a key (not a mask). If 'errorp'
2921 * is nonnull and the function returns ODP_FIT_ERROR, stores a malloc()'d error
2922 * message in '*errorp'. */
2923 enum odp_key_fitness
2924 odp_nsh_key_from_attr(const struct nlattr *attr, struct ovs_key_nsh *nsh,
2925 struct ovs_key_nsh *nsh_mask, char **errorp)
2926 {
2927 return odp_nsh_key_from_attr__(attr, false, nsh, nsh_mask, errorp);
2928 }
2929
2930 /* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness.
2931 * If the attribute is a key, 'is_mask' should be false; if it is a mask,
2932 * 'is_mask' should be true. If 'errorp' is nonnull and the function returns
2933 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
2934 static enum odp_key_fitness
2935 odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask,
2936 struct flow_tnl *tun, char **errorp)
2937 {
2938 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2939 unsigned int left;
2940 const struct nlattr *a;
2941 bool ttl = false;
2942 bool unknown = false;
2943
2944 NL_NESTED_FOR_EACH(a, left, attr) {
2945 uint16_t type = nl_attr_type(a);
2946 size_t len = nl_attr_get_size(a);
2947 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
2948 OVS_TUNNEL_ATTR_MAX, type);
2949
2950 if (len != expected_len && expected_len >= 0) {
2951 odp_parse_error(&rl, errorp, "tunnel key attribute %"PRIu16" "
2952 "should have length %d but actually has %"PRIuSIZE,
2953 type, expected_len, len);
2954 return ODP_FIT_ERROR;
2955 }
2956
2957 switch (type) {
2958 case OVS_TUNNEL_KEY_ATTR_ID:
2959 tun->tun_id = nl_attr_get_be64(a);
2960 tun->flags |= FLOW_TNL_F_KEY;
2961 break;
2962 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2963 tun->ip_src = nl_attr_get_be32(a);
2964 break;
2965 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2966 tun->ip_dst = nl_attr_get_be32(a);
2967 break;
2968 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
2969 tun->ipv6_src = nl_attr_get_in6_addr(a);
2970 break;
2971 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
2972 tun->ipv6_dst = nl_attr_get_in6_addr(a);
2973 break;
2974 case OVS_TUNNEL_KEY_ATTR_TOS:
2975 tun->ip_tos = nl_attr_get_u8(a);
2976 break;
2977 case OVS_TUNNEL_KEY_ATTR_TTL:
2978 tun->ip_ttl = nl_attr_get_u8(a);
2979 ttl = true;
2980 break;
2981 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2982 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
2983 break;
2984 case OVS_TUNNEL_KEY_ATTR_CSUM:
2985 tun->flags |= FLOW_TNL_F_CSUM;
2986 break;
2987 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2988 tun->tp_src = nl_attr_get_be16(a);
2989 break;
2990 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2991 tun->tp_dst = nl_attr_get_be16(a);
2992 break;
2993 case OVS_TUNNEL_KEY_ATTR_OAM:
2994 tun->flags |= FLOW_TNL_F_OAM;
2995 break;
2996 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
2997 static const struct nl_policy vxlan_opts_policy[] = {
2998 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
2999 };
3000 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
3001
3002 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
3003 odp_parse_error(&rl, errorp, "error parsing VXLAN options");
3004 return ODP_FIT_ERROR;
3005 }
3006
3007 if (ext[OVS_VXLAN_EXT_GBP]) {
3008 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
3009
3010 tun->gbp_id = htons(gbp & 0xFFFF);
3011 tun->gbp_flags = (gbp >> 16) & 0xFF;
3012 }
3013
3014 break;
3015 }
3016 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3017 tun_metadata_from_geneve_nlattr(a, is_mask, tun);
3018 break;
3019 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: {
3020 const struct erspan_metadata *opts = nl_attr_get(a);
3021
3022 tun->erspan_ver = opts->version;
3023 if (tun->erspan_ver == 1) {
3024 tun->erspan_idx = ntohl(opts->u.index);
3025 } else if (tun->erspan_ver == 2) {
3026 tun->erspan_dir = opts->u.md2.dir;
3027 tun->erspan_hwid = get_hwid(&opts->u.md2);
3028 } else {
3029 VLOG_WARN("%s invalid erspan version\n", __func__);
3030 }
3031 break;
3032 }
3033
3034 default:
3035 /* Allow this to show up as unexpected, if there are unknown
3036 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3037 unknown = true;
3038 break;
3039 }
3040 }
3041
3042 if (!ttl) {
3043 odp_parse_error(&rl, errorp, "tunnel options missing TTL");
3044 return ODP_FIT_ERROR;
3045 }
3046 if (unknown) {
3047 return ODP_FIT_TOO_MUCH;
3048 }
3049 return ODP_FIT_PERFECT;
3050 }
3051
3052 /* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns
3053 * fitness. The attribute should be a key (not a mask). If 'errorp' is
3054 * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error
3055 * message. */
3056 enum odp_key_fitness
3057 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun,
3058 char **errorp)
3059 {
3060 if (errorp) {
3061 *errorp = NULL;
3062 }
3063 memset(tun, 0, sizeof *tun);
3064 return odp_tun_key_from_attr__(attr, false, tun, errorp);
3065 }
3066
3067 static void
3068 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
3069 const struct flow_tnl *tun_flow_key,
3070 const struct ofpbuf *key_buf, const char *tnl_type)
3071 {
3072 size_t tun_key_ofs;
3073
3074 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
3075
3076 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
3077 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
3078 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
3079 }
3080 if (tun_key->ip_src) {
3081 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
3082 }
3083 if (tun_key->ip_dst) {
3084 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
3085 }
3086 if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
3087 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
3088 }
3089 if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
3090 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
3091 }
3092 if (tun_key->ip_tos) {
3093 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
3094 }
3095 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
3096 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
3097 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3098 }
3099 if (tun_key->flags & FLOW_TNL_F_CSUM) {
3100 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3101 }
3102 if (tun_key->tp_src) {
3103 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
3104 }
3105 if (tun_key->tp_dst) {
3106 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
3107 }
3108 if (tun_key->flags & FLOW_TNL_F_OAM) {
3109 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3110 }
3111
3112 /* If tnl_type is set to a particular type of output tunnel,
3113 * only put its relevant tunnel metadata to the nlattr.
3114 * If tnl_type is NULL, put tunnel metadata according to the
3115 * 'tun_key'.
3116 */
3117 if ((!tnl_type || !strcmp(tnl_type, "vxlan")) &&
3118 (tun_key->gbp_flags || tun_key->gbp_id)) {
3119 size_t vxlan_opts_ofs;
3120
3121 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3122 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
3123 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
3124 nl_msg_end_nested(a, vxlan_opts_ofs);
3125 }
3126
3127 if (!tnl_type || !strcmp(tnl_type, "geneve")) {
3128 tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
3129 }
3130
3131 if ((!tnl_type || !strcmp(tnl_type, "erspan") ||
3132 !strcmp(tnl_type, "ip6erspan")) &&
3133 (tun_key->erspan_ver == 1 || tun_key->erspan_ver == 2)) {
3134 struct erspan_metadata opts;
3135
3136 opts.version = tun_key->erspan_ver;
3137 if (opts.version == 1) {
3138 opts.u.index = htonl(tun_key->erspan_idx);
3139 } else {
3140 opts.u.md2.dir = tun_key->erspan_dir;
3141 set_hwid(&opts.u.md2, tun_key->erspan_hwid);
3142 }
3143 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
3144 &opts, sizeof(opts));
3145 }
3146
3147 nl_msg_end_nested(a, tun_key_ofs);
3148 }
3149
3150 static bool
3151 odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size,
3152 int constant)
3153 {
3154 /* Convert 'constant' to all the widths we need. C conversion rules ensure
3155 * that -1 becomes all-1-bits and 0 does not change. */
3156 ovs_be16 be16 = (OVS_FORCE ovs_be16) constant;
3157 uint32_t u32 = constant;
3158 uint8_t u8 = constant;
3159 const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any;
3160
3161 switch (attr) {
3162 case OVS_KEY_ATTR_UNSPEC:
3163 case OVS_KEY_ATTR_ENCAP:
3164 case __OVS_KEY_ATTR_MAX:
3165 default:
3166 return false;
3167
3168 case OVS_KEY_ATTR_PRIORITY:
3169 case OVS_KEY_ATTR_IN_PORT:
3170 case OVS_KEY_ATTR_ETHERNET:
3171 case OVS_KEY_ATTR_VLAN:
3172 case OVS_KEY_ATTR_ETHERTYPE:
3173 case OVS_KEY_ATTR_IPV4:
3174 case OVS_KEY_ATTR_TCP:
3175 case OVS_KEY_ATTR_UDP:
3176 case OVS_KEY_ATTR_ICMP:
3177 case OVS_KEY_ATTR_ICMPV6:
3178 case OVS_KEY_ATTR_ND:
3179 case OVS_KEY_ATTR_ND_EXTENSIONS:
3180 case OVS_KEY_ATTR_SKB_MARK:
3181 case OVS_KEY_ATTR_TUNNEL:
3182 case OVS_KEY_ATTR_SCTP:
3183 case OVS_KEY_ATTR_DP_HASH:
3184 case OVS_KEY_ATTR_RECIRC_ID:
3185 case OVS_KEY_ATTR_MPLS:
3186 case OVS_KEY_ATTR_CT_STATE:
3187 case OVS_KEY_ATTR_CT_ZONE:
3188 case OVS_KEY_ATTR_CT_MARK:
3189 case OVS_KEY_ATTR_CT_LABELS:
3190 case OVS_KEY_ATTR_PACKET_TYPE:
3191 case OVS_KEY_ATTR_NSH:
3192 return is_all_byte(mask, size, u8);
3193
3194 case OVS_KEY_ATTR_TCP_FLAGS:
3195 return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16);
3196
3197 case OVS_KEY_ATTR_IPV6: {
3198 const struct ovs_key_ipv6 *ipv6_mask = mask;
3199 return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
3200 == htonl(IPV6_LABEL_MASK & u32)
3201 && ipv6_mask->ipv6_proto == u8
3202 && ipv6_mask->ipv6_tclass == u8
3203 && ipv6_mask->ipv6_hlimit == u8
3204 && ipv6_mask->ipv6_frag == u8
3205 && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6)
3206 && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6));
3207 }
3208
3209 case OVS_KEY_ATTR_ARP:
3210 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8);
3211
3212 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
3213 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4,
3214 ipv4_proto), u8);
3215
3216 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
3217 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6,
3218 ipv6_proto), u8);
3219 }
3220 }
3221
3222 /* The caller must already have verified that 'ma' has a correct length.
3223 *
3224 * The main purpose of this function is formatting, to allow code to figure out
3225 * whether the mask can be omitted. It doesn't try hard for attributes that
3226 * contain sub-attributes, etc., because normally those would be broken down
3227 * further for formatting. */
3228 static bool
3229 odp_mask_attr_is_wildcard(const struct nlattr *ma)
3230 {
3231 return odp_mask_is_constant__(nl_attr_type(ma),
3232 nl_attr_get(ma), nl_attr_get_size(ma), 0);
3233 }
3234
3235 /* The caller must already have verified that 'size' is a correct length for
3236 * 'attr'.
3237 *
3238 * The main purpose of this function is formatting, to allow code to figure out
3239 * whether the mask can be omitted. It doesn't try hard for attributes that
3240 * contain sub-attributes, etc., because normally those would be broken down
3241 * further for formatting. */
3242 static bool
3243 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
3244 {
3245 return odp_mask_is_constant__(attr, mask, size, -1);
3246 }
3247
3248 /* The caller must already have verified that 'ma' has a correct length. */
3249 static bool
3250 odp_mask_attr_is_exact(const struct nlattr *ma)
3251 {
3252 enum ovs_key_attr attr = nl_attr_type(ma);
3253 return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma));
3254 }
3255
3256 void
3257 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
3258 char *port_name)
3259 {
3260 struct odp_portno_names *odp_portno_names;
3261
3262 odp_portno_names = xmalloc(sizeof *odp_portno_names);
3263 odp_portno_names->port_no = port_no;
3264 odp_portno_names->name = xstrdup(port_name);
3265 hmap_insert(portno_names, &odp_portno_names->hmap_node,
3266 hash_odp_port(port_no));
3267 }
3268
3269 static char *
3270 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
3271 {
3272 if (portno_names) {
3273 struct odp_portno_names *odp_portno_names;
3274
3275 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
3276 hash_odp_port(port_no), portno_names) {
3277 if (odp_portno_names->port_no == port_no) {
3278 return odp_portno_names->name;
3279 }
3280 }
3281 }
3282 return NULL;
3283 }
3284
3285 void
3286 odp_portno_names_destroy(struct hmap *portno_names)
3287 {
3288 struct odp_portno_names *odp_portno_names;
3289
3290 HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) {
3291 free(odp_portno_names->name);
3292 free(odp_portno_names);
3293 }
3294 }
3295
3296 void
3297 odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no,
3298 struct ds *s)
3299 {
3300 const char *name = odp_portno_names_get(portno_names, port_no);
3301 if (name) {
3302 ds_put_cstr(s, name);
3303 } else {
3304 ds_put_format(s, "%"PRIu32, port_no);
3305 }
3306 }
3307
3308 /* Format helpers. */
3309
3310 static void
3311 format_eth(struct ds *ds, const char *name, const struct eth_addr key,
3312 const struct eth_addr *mask, bool verbose)
3313 {
3314 bool mask_empty = mask && eth_addr_is_zero(*mask);
3315
3316 if (verbose || !mask_empty) {
3317 bool mask_full = !mask || eth_mask_is_exact(*mask);
3318
3319 if (mask_full) {
3320 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
3321 } else {
3322 ds_put_format(ds, "%s=", name);
3323 eth_format_masked(key, mask, ds);
3324 ds_put_char(ds, ',');
3325 }
3326 }
3327 }
3328
3329
3330 static void
3331 format_be64(struct ds *ds, const char *name, ovs_be64 key,
3332 const ovs_be64 *mask, bool verbose)
3333 {
3334 bool mask_empty = mask && !*mask;
3335
3336 if (verbose || !mask_empty) {
3337 bool mask_full = !mask || *mask == OVS_BE64_MAX;
3338
3339 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
3340 if (!mask_full) { /* Partially masked. */
3341 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
3342 }
3343 ds_put_char(ds, ',');
3344 }
3345 }
3346
3347 static void
3348 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
3349 const ovs_be32 *mask, bool verbose)
3350 {
3351 bool mask_empty = mask && !*mask;
3352
3353 if (verbose || !mask_empty) {
3354 bool mask_full = !mask || *mask == OVS_BE32_MAX;
3355
3356 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
3357 if (!mask_full) { /* Partially masked. */
3358 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
3359 }
3360 ds_put_char(ds, ',');
3361 }
3362 }
3363
3364 static void
3365 format_in6_addr(struct ds *ds, const char *name,
3366 const struct in6_addr *key,
3367 const struct in6_addr *mask,
3368 bool verbose)
3369 {
3370 char buf[INET6_ADDRSTRLEN];
3371 bool mask_empty = mask && ipv6_mask_is_any(mask);
3372
3373 if (verbose || !mask_empty) {
3374 bool mask_full = !mask || ipv6_mask_is_exact(mask);
3375
3376 inet_ntop(AF_INET6, key, buf, sizeof buf);
3377 ds_put_format(ds, "%s=%s", name, buf);
3378 if (!mask_full) { /* Partially masked. */
3379 inet_ntop(AF_INET6, mask, buf, sizeof buf);
3380 ds_put_format(ds, "/%s", buf);
3381 }
3382 ds_put_char(ds, ',');
3383 }
3384 }
3385
3386 static void
3387 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
3388 const ovs_be32 *mask, bool verbose)
3389 {
3390 bool mask_empty = mask && !*mask;
3391
3392 if (verbose || !mask_empty) {
3393 bool mask_full = !mask
3394 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
3395
3396 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
3397 if (!mask_full) { /* Partially masked. */
3398 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
3399 }
3400 ds_put_char(ds, ',');
3401 }
3402 }
3403
3404 static void
3405 format_u8x(struct ds *ds, const char *name, uint8_t key,
3406 const uint8_t *mask, bool verbose)
3407 {
3408 bool mask_empty = mask && !*mask;
3409
3410 if (verbose || !mask_empty) {
3411 bool mask_full = !mask || *mask == UINT8_MAX;
3412
3413 ds_put_format(ds, "%s=%#"PRIx8, name, key);
3414 if (!mask_full) { /* Partially masked. */
3415 ds_put_format(ds, "/%#"PRIx8, *mask);
3416 }
3417 ds_put_char(ds, ',');
3418 }
3419 }
3420
3421 static void
3422 format_u8u(struct ds *ds, const char *name, uint8_t key,
3423 const uint8_t *mask, bool verbose)
3424 {
3425 bool mask_empty = mask && !*mask;
3426
3427 if (verbose || !mask_empty) {
3428 bool mask_full = !mask || *mask == UINT8_MAX;
3429
3430 ds_put_format(ds, "%s=%"PRIu8, name, key);
3431 if (!mask_full) { /* Partially masked. */
3432 ds_put_format(ds, "/%#"PRIx8, *mask);
3433 }
3434 ds_put_char(ds, ',');
3435 }
3436 }
3437
3438 static void
3439 format_be16(struct ds *ds, const char *name, ovs_be16 key,
3440 const ovs_be16 *mask, bool verbose)
3441 {
3442 bool mask_empty = mask && !*mask;
3443
3444 if (verbose || !mask_empty) {
3445 bool mask_full = !mask || *mask == OVS_BE16_MAX;
3446
3447 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
3448 if (!mask_full) { /* Partially masked. */
3449 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3450 }
3451 ds_put_char(ds, ',');
3452 }
3453 }
3454
3455 static void
3456 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
3457 const ovs_be16 *mask, bool verbose)
3458 {
3459 bool mask_empty = mask && !*mask;
3460
3461 if (verbose || !mask_empty) {
3462 bool mask_full = !mask || *mask == OVS_BE16_MAX;
3463
3464 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
3465 if (!mask_full) { /* Partially masked. */
3466 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3467 }
3468 ds_put_char(ds, ',');
3469 }
3470 }
3471
3472 static void
3473 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
3474 const uint16_t *mask, bool verbose)
3475 {
3476 bool mask_empty = mask && !*mask;
3477
3478 if (verbose || !mask_empty) {
3479 ds_put_cstr(ds, name);
3480 ds_put_char(ds, '(');
3481 if (mask) {
3482 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
3483 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
3484 } else { /* Fully masked. */
3485 format_flags(ds, flow_tun_flag_to_string, key, '|');
3486 }
3487 ds_put_cstr(ds, "),");
3488 }
3489 }
3490
3491 static bool
3492 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
3493 const struct attr_len_tbl tbl[], int max_type, bool need_key)
3494 {
3495 int expected_len;
3496
3497 expected_len = odp_key_attr_len(tbl, max_type, nl_attr_type(a));
3498 if (expected_len != ATTR_LEN_VARIABLE &&
3499 expected_len != ATTR_LEN_NESTED) {
3500
3501 bool bad_key_len = nl_attr_get_size(a) != expected_len;
3502 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
3503
3504 if (bad_key_len || bad_mask_len) {
3505 if (need_key) {
3506 ds_put_format(ds, "key%u", nl_attr_type(a));
3507 }
3508 if (bad_key_len) {
3509 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
3510 nl_attr_get_size(a), expected_len);
3511 }
3512 format_generic_odp_key(a, ds);
3513 if (ma) {
3514 ds_put_char(ds, '/');
3515 if (bad_mask_len) {
3516 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
3517 nl_attr_get_size(ma), expected_len);
3518 }
3519 format_generic_odp_key(ma, ds);
3520 }
3521 ds_put_char(ds, ')');
3522 return false;
3523 }
3524 }
3525
3526 return true;
3527 }
3528
3529 static void
3530 format_unknown_key(struct ds *ds, const struct nlattr *a,
3531 const struct nlattr *ma)
3532 {
3533 ds_put_format(ds, "key%u(", nl_attr_type(a));
3534 format_generic_odp_key(a, ds);
3535 if (ma && !odp_mask_attr_is_exact(ma)) {
3536 ds_put_char(ds, '/');
3537 format_generic_odp_key(ma, ds);
3538 }
3539 ds_put_cstr(ds, "),");
3540 }
3541
3542 static void
3543 format_odp_tun_vxlan_opt(const struct nlattr *attr,
3544 const struct nlattr *mask_attr, struct ds *ds,
3545 bool verbose)
3546 {
3547 unsigned int left;
3548 const struct nlattr *a;
3549 struct ofpbuf ofp;
3550
3551 ofpbuf_init(&ofp, 100);
3552 NL_NESTED_FOR_EACH(a, left, attr) {
3553 uint16_t type = nl_attr_type(a);
3554 const struct nlattr *ma = NULL;
3555
3556 if (mask_attr) {
3557 ma = nl_attr_find__(nl_attr_get(mask_attr),
3558 nl_attr_get_size(mask_attr), type);
3559 if (!ma) {
3560 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
3561 OVS_VXLAN_EXT_MAX,
3562 &ofp, a);
3563 }
3564 }
3565
3566 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
3567 OVS_VXLAN_EXT_MAX, true)) {
3568 continue;
3569 }
3570
3571 switch (type) {
3572 case OVS_VXLAN_EXT_GBP: {
3573 uint32_t key = nl_attr_get_u32(a);
3574 ovs_be16 id, id_mask;
3575 uint8_t flags, flags_mask = 0;
3576
3577 id = htons(key & 0xFFFF);
3578 flags = (key >> 16) & 0xFF;
3579 if (ma) {
3580 uint32_t mask = nl_attr_get_u32(ma);
3581 id_mask = htons(mask & 0xFFFF);
3582 flags_mask = (mask >> 16) & 0xFF;
3583 }
3584
3585 ds_put_cstr(ds, "gbp(");
3586 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
3587 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
3588 ds_chomp(ds, ',');
3589 ds_put_cstr(ds, "),");
3590 break;
3591 }
3592
3593 default:
3594 format_unknown_key(ds, a, ma);
3595 }
3596 ofpbuf_clear(&ofp);
3597 }
3598
3599 ds_chomp(ds, ',');
3600 ofpbuf_uninit(&ofp);
3601 }
3602
3603 static void
3604 format_odp_tun_erspan_opt(const struct nlattr *attr,
3605 const struct nlattr *mask_attr, struct ds *ds,
3606 bool verbose)
3607 {
3608 const struct erspan_metadata *opts, *mask;
3609 uint8_t ver, ver_ma, dir, dir_ma, hwid, hwid_ma;
3610
3611 opts = nl_attr_get(attr);
3612 mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
3613
3614 ver = (uint8_t)opts->version;
3615 if (mask) {
3616 ver_ma = (uint8_t)mask->version;
3617 }
3618
3619 format_u8u(ds, "ver", ver, mask ? &ver_ma : NULL, verbose);
3620
3621 if (opts->version == 1) {
3622 if (mask) {
3623 ds_put_format(ds, "idx=%#"PRIx32"/%#"PRIx32",",
3624 ntohl(opts->u.index),
3625 ntohl(mask->u.index));
3626 } else {
3627 ds_put_format(ds, "idx=%#"PRIx32",", ntohl(opts->u.index));
3628 }
3629 } else if (opts->version == 2) {
3630 dir = opts->u.md2.dir;
3631 hwid = opts->u.md2.hwid;
3632 if (mask) {
3633 dir_ma = mask->u.md2.dir;
3634 hwid_ma = mask->u.md2.hwid;
3635 }
3636
3637 format_u8u(ds, "dir", dir, mask ? &dir_ma : NULL, verbose);
3638 format_u8x(ds, "hwid", hwid, mask ? &hwid_ma : NULL, verbose);
3639 }
3640 ds_chomp(ds, ',');
3641 }
3642
3643 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3644
3645 static void
3646 format_geneve_opts(const struct geneve_opt *opt,
3647 const struct geneve_opt *mask, int opts_len,
3648 struct ds *ds, bool verbose)
3649 {
3650 while (opts_len > 0) {
3651 unsigned int len;
3652 uint8_t data_len, data_len_mask;
3653
3654 if (opts_len < sizeof *opt) {
3655 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
3656 opts_len, sizeof *opt);
3657 return;
3658 }
3659
3660 data_len = opt->length * 4;
3661 if (mask) {
3662 if (mask->length == 0x1f) {
3663 data_len_mask = UINT8_MAX;
3664 } else {
3665 data_len_mask = mask->length;
3666 }
3667 }
3668 len = sizeof *opt + data_len;
3669 if (len > opts_len) {
3670 ds_put_format(ds, "opt len %u greater than remaining %u",
3671 len, opts_len);
3672 return;
3673 }
3674
3675 ds_put_char(ds, '{');
3676 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
3677 verbose);
3678 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
3679 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
3680 if (data_len &&
3681 (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
3682 ds_put_hex(ds, opt + 1, data_len);
3683 if (mask && !is_all_ones(mask + 1, data_len)) {
3684 ds_put_char(ds, '/');
3685 ds_put_hex(ds, mask + 1, data_len);
3686 }
3687 } else {
3688 ds_chomp(ds, ',');
3689 }
3690 ds_put_char(ds, '}');
3691
3692 opt += len / sizeof(*opt);
3693 if (mask) {
3694 mask += len / sizeof(*opt);
3695 }
3696 opts_len -= len;
3697 };
3698 }
3699
3700 static void
3701 format_odp_tun_geneve(const struct nlattr *attr,
3702 const struct nlattr *mask_attr, struct ds *ds,
3703 bool verbose)
3704 {
3705 int opts_len = nl_attr_get_size(attr);
3706 const struct geneve_opt *opt = nl_attr_get(attr);
3707 const struct geneve_opt *mask = mask_attr ?
3708 nl_attr_get(mask_attr) : NULL;
3709
3710 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
3711 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
3712 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
3713 return;
3714 }
3715
3716 format_geneve_opts(opt, mask, opts_len, ds, verbose);
3717 }
3718
3719 static void
3720 format_odp_nsh_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
3721 struct ds *ds)
3722 {
3723 unsigned int left;
3724 const struct nlattr *a;
3725 struct ovs_key_nsh nsh;
3726 struct ovs_key_nsh nsh_mask;
3727
3728 memset(&nsh, 0, sizeof nsh);
3729 memset(&nsh_mask, 0xff, sizeof nsh_mask);
3730
3731 NL_NESTED_FOR_EACH (a, left, attr) {
3732 enum ovs_nsh_key_attr type = nl_attr_type(a);
3733 const struct nlattr *ma = NULL;
3734
3735 if (mask_attr) {
3736 ma = nl_attr_find__(nl_attr_get(mask_attr),
3737 nl_attr_get_size(mask_attr), type);
3738 }
3739
3740 if (!check_attr_len(ds, a, ma, ovs_nsh_key_attr_lens,
3741 OVS_NSH_KEY_ATTR_MAX, true)) {
3742 continue;
3743 }
3744
3745 switch (type) {
3746 case OVS_NSH_KEY_ATTR_UNSPEC:
3747 break;
3748 case OVS_NSH_KEY_ATTR_BASE: {
3749 const struct ovs_nsh_key_base *base = nl_attr_get(a);
3750 const struct ovs_nsh_key_base *base_mask
3751 = ma ? nl_attr_get(ma) : NULL;
3752 nsh.flags = base->flags;
3753 nsh.ttl = base->ttl;
3754 nsh.mdtype = base->mdtype;
3755 nsh.np = base->np;
3756 nsh.path_hdr = base->path_hdr;
3757 if (base_mask) {
3758 nsh_mask.flags = base_mask->flags;
3759 nsh_mask.ttl = base_mask->ttl;
3760 nsh_mask.mdtype = base_mask->mdtype;
3761 nsh_mask.np = base_mask->np;
3762 nsh_mask.path_hdr = base_mask->path_hdr;
3763 }
3764 break;
3765 }
3766 case OVS_NSH_KEY_ATTR_MD1: {
3767 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3768 const struct ovs_nsh_key_md1 *md1_mask
3769 = ma ? nl_attr_get(ma) : NULL;
3770 memcpy(nsh.context, md1->context, sizeof md1->context);
3771 if (md1_mask) {
3772 memcpy(nsh_mask.context, md1_mask->context,
3773 sizeof md1_mask->context);
3774 }
3775 break;
3776 }
3777 case OVS_NSH_KEY_ATTR_MD2:
3778 case __OVS_NSH_KEY_ATTR_MAX:
3779 default:
3780 /* No support for matching other metadata formats yet. */
3781 break;
3782 }
3783 }
3784
3785 if (mask_attr) {
3786 format_nsh_key_mask(ds, &nsh, &nsh_mask);
3787 } else {
3788 format_nsh_key(ds, &nsh);
3789 }
3790 }
3791
3792 static void
3793 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
3794 struct ds *ds, bool verbose)
3795 {
3796 unsigned int left;
3797 const struct nlattr *a;
3798 uint16_t flags = 0;
3799 uint16_t mask_flags = 0;
3800 struct ofpbuf ofp;
3801
3802 ofpbuf_init(&ofp, 100);
3803 NL_NESTED_FOR_EACH(a, left, attr) {
3804 enum ovs_tunnel_key_attr type = nl_attr_type(a);
3805 const struct nlattr *ma = NULL;
3806
3807 if (mask_attr) {
3808 ma = nl_attr_find__(nl_attr_get(mask_attr),
3809 nl_attr_get_size(mask_attr), type);
3810 if (!ma) {
3811 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
3812 OVS_TUNNEL_KEY_ATTR_MAX,
3813 &ofp, a);
3814 }
3815 }
3816
3817 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
3818 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
3819 continue;
3820 }
3821
3822 switch (type) {
3823 case OVS_TUNNEL_KEY_ATTR_ID:
3824 format_be64(ds, "tun_id", nl_attr_get_be64(a),
3825 ma ? nl_attr_get(ma) : NULL, verbose);
3826 flags |= FLOW_TNL_F_KEY;
3827 if (ma) {
3828 mask_flags |= FLOW_TNL_F_KEY;
3829 }
3830 break;
3831 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
3832 format_ipv4(ds, "src", nl_attr_get_be32(a),
3833 ma ? nl_attr_get(ma) : NULL, verbose);
3834 break;
3835 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
3836 format_ipv4(ds, "dst", nl_attr_get_be32(a),
3837 ma ? nl_attr_get(ma) : NULL, verbose);
3838 break;
3839 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
3840 struct in6_addr ipv6_src;
3841 ipv6_src = nl_attr_get_in6_addr(a);
3842 format_in6_addr(ds, "ipv6_src", &ipv6_src,
3843 ma ? nl_attr_get(ma) : NULL, verbose);
3844 break;
3845 }
3846 case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
3847 struct in6_addr ipv6_dst;
3848 ipv6_dst = nl_attr_get_in6_addr(a);
3849 format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
3850 ma ? nl_attr_get(ma) : NULL, verbose);
3851 break;
3852 }
3853 case OVS_TUNNEL_KEY_ATTR_TOS:
3854 format_u8x(ds, "tos", nl_attr_get_u8(a),
3855 ma ? nl_attr_get(ma) : NULL, verbose);
3856 break;
3857 case OVS_TUNNEL_KEY_ATTR_TTL:
3858 format_u8u(ds, "ttl", nl_attr_get_u8(a),
3859 ma ? nl_attr_get(ma) : NULL, verbose);
3860 break;
3861 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3862 flags |= FLOW_TNL_F_DONT_FRAGMENT;
3863 break;
3864 case OVS_TUNNEL_KEY_ATTR_CSUM:
3865 flags |= FLOW_TNL_F_CSUM;
3866 break;
3867 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
3868 format_be16(ds, "tp_src", nl_attr_get_be16(a),
3869 ma ? nl_attr_get(ma) : NULL, verbose);
3870 break;
3871 case OVS_TUNNEL_KEY_ATTR_TP_DST:
3872 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
3873 ma ? nl_attr_get(ma) : NULL, verbose);
3874 break;
3875 case OVS_TUNNEL_KEY_ATTR_OAM:
3876 flags |= FLOW_TNL_F_OAM;
3877 break;
3878 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
3879 ds_put_cstr(ds, "vxlan(");
3880 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
3881 ds_put_cstr(ds, "),");
3882 break;
3883 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3884 ds_put_cstr(ds, "geneve(");
3885 format_odp_tun_geneve(a, ma, ds, verbose);
3886 ds_put_cstr(ds, "),");
3887 break;
3888 case OVS_TUNNEL_KEY_ATTR_PAD:
3889 break;
3890 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
3891 ds_put_cstr(ds, "erspan(");
3892 format_odp_tun_erspan_opt(a, ma, ds, verbose);
3893 ds_put_cstr(ds, "),");
3894 break;
3895 case __OVS_TUNNEL_KEY_ATTR_MAX:
3896 default:
3897 format_unknown_key(ds, a, ma);
3898 }
3899 ofpbuf_clear(&ofp);
3900 }
3901
3902 /* Flags can have a valid mask even if the attribute is not set, so
3903 * we need to collect these separately. */
3904 if (mask_attr) {
3905 NL_NESTED_FOR_EACH(a, left, mask_attr) {
3906 switch (nl_attr_type(a)) {
3907 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3908 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
3909 break;
3910 case OVS_TUNNEL_KEY_ATTR_CSUM:
3911 mask_flags |= FLOW_TNL_F_CSUM;
3912 break;
3913 case OVS_TUNNEL_KEY_ATTR_OAM:
3914 mask_flags |= FLOW_TNL_F_OAM;
3915 break;
3916 }
3917 }
3918 }
3919
3920 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
3921 verbose);
3922 ds_chomp(ds, ',');
3923 ofpbuf_uninit(&ofp);
3924 }
3925
3926 static const char *
3927 odp_ct_state_to_string(uint32_t flag)
3928 {
3929 switch (flag) {
3930 case OVS_CS_F_REPLY_DIR:
3931 return "rpl";
3932 case OVS_CS_F_TRACKED:
3933 return "trk";
3934 case OVS_CS_F_NEW:
3935 return "new";
3936 case OVS_CS_F_ESTABLISHED:
3937 return "est";
3938 case OVS_CS_F_RELATED:
3939 return "rel";
3940 case OVS_CS_F_INVALID:
3941 return "inv";
3942 case OVS_CS_F_SRC_NAT:
3943 return "snat";
3944 case OVS_CS_F_DST_NAT:
3945 return "dnat";
3946 default:
3947 return NULL;
3948 }
3949 }
3950
3951 static void
3952 format_frag(struct ds *ds, const char *name, uint8_t key,
3953 const uint8_t *mask, bool verbose OVS_UNUSED)
3954 {
3955 bool mask_empty = mask && !*mask;
3956 bool mask_full = !mask || *mask == UINT8_MAX;
3957
3958 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3959 if (!mask_empty && !mask_full) {
3960 ds_put_format(ds, "error: partial mask not supported for frag (%#"
3961 PRIx8"),", *mask);
3962 } else if (!mask_empty) {
3963 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
3964 }
3965 }
3966
3967 static bool
3968 mask_empty(const struct nlattr *ma)
3969 {
3970 const void *mask;
3971 size_t n;
3972
3973 if (!ma) {
3974 return true;
3975 }
3976 mask = nl_attr_get(ma);
3977 n = nl_attr_get_size(ma);
3978
3979 return is_all_zeros(mask, n);
3980 }
3981
3982 /* The caller must have already verified that 'a' and 'ma' have correct
3983 * lengths. */
3984 static void
3985 format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma,
3986 const struct hmap *portno_names, struct ds *ds,
3987 bool verbose)
3988 {
3989 enum ovs_key_attr attr = nl_attr_type(a);
3990 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3991 bool is_exact;
3992
3993 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
3994
3995 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
3996
3997 ds_put_char(ds, '(');
3998 switch (attr) {
3999 case OVS_KEY_ATTR_ENCAP:
4000 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
4001 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
4002 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
4003 verbose);
4004 } else if (nl_attr_get_size(a)) {
4005 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
4006 ds, verbose);
4007 }
4008 break;
4009
4010 case OVS_KEY_ATTR_PRIORITY:
4011 case OVS_KEY_ATTR_SKB_MARK:
4012 case OVS_KEY_ATTR_DP_HASH:
4013 case OVS_KEY_ATTR_RECIRC_ID:
4014 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4015 if (!is_exact) {
4016 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4017 }
4018 break;
4019
4020 case OVS_KEY_ATTR_CT_MARK:
4021 if (verbose || !mask_empty(ma)) {
4022 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4023 if (!is_exact) {
4024 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4025 }
4026 }
4027 break;
4028
4029 case OVS_KEY_ATTR_CT_STATE:
4030 if (verbose) {
4031 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4032 if (!is_exact) {
4033 ds_put_format(ds, "/%#"PRIx32,
4034 mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
4035 }
4036 } else if (!is_exact) {
4037 format_flags_masked(ds, NULL, odp_ct_state_to_string,
4038 nl_attr_get_u32(a),
4039 mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
4040 UINT32_MAX);
4041 } else {
4042 format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
4043 }
4044 break;
4045
4046 case OVS_KEY_ATTR_CT_ZONE:
4047 if (verbose || !mask_empty(ma)) {
4048 ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
4049 if (!is_exact) {
4050 ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
4051 }
4052 }
4053 break;
4054
4055 case OVS_KEY_ATTR_CT_LABELS: {
4056 const ovs_32aligned_u128 *value = nl_attr_get(a);
4057 const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL;
4058
4059 format_u128(ds, value, mask, verbose);
4060 break;
4061 }
4062
4063 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
4064 const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a);
4065 const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4066
4067 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4068 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4069 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4070 verbose);
4071 format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port),
4072 verbose);
4073 format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port),
4074 verbose);
4075 ds_chomp(ds, ',');
4076 break;
4077 }
4078
4079 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
4080 const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a);
4081 const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4082
4083 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4084 verbose);
4085 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4086 verbose);
4087 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4088 verbose);
4089 format_be16(ds, "src_port", key->src_port, MASK(mask, src_port),
4090 verbose);
4091 format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port),
4092 verbose);
4093 ds_chomp(ds, ',');
4094 break;
4095 }
4096
4097 case OVS_KEY_ATTR_TUNNEL:
4098 format_odp_tun_attr(a, ma, ds, verbose);
4099 break;
4100
4101 case OVS_KEY_ATTR_IN_PORT:
4102 if (is_exact) {
4103 odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
4104 } else {
4105 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
4106 if (!is_exact) {
4107 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4108 }
4109 }
4110 break;
4111
4112 case OVS_KEY_ATTR_PACKET_TYPE: {
4113 ovs_be32 value = nl_attr_get_be32(a);
4114 ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX;
4115
4116 ovs_be16 ns = htons(pt_ns(value));
4117 ovs_be16 ns_mask = htons(pt_ns(mask));
4118 format_be16(ds, "ns", ns, &ns_mask, verbose);
4119
4120 ovs_be16 ns_type = pt_ns_type_be(value);
4121 ovs_be16 ns_type_mask = pt_ns_type_be(mask);
4122 format_be16x(ds, "id", ns_type, &ns_type_mask, verbose);
4123
4124 ds_chomp(ds, ',');
4125 break;
4126 }
4127
4128 case OVS_KEY_ATTR_ETHERNET: {
4129 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
4130 const struct ovs_key_ethernet *key = nl_attr_get(a);
4131
4132 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
4133 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
4134 ds_chomp(ds, ',');
4135 break;
4136 }
4137 case OVS_KEY_ATTR_VLAN:
4138 format_vlan_tci(ds, nl_attr_get_be16(a),
4139 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
4140 break;
4141
4142 case OVS_KEY_ATTR_MPLS: {
4143 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
4144 const struct ovs_key_mpls *mpls_mask = NULL;
4145 size_t size = nl_attr_get_size(a);
4146
4147 if (!size || size % sizeof *mpls_key) {
4148 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
4149 return;
4150 }
4151 if (!is_exact) {
4152 mpls_mask = nl_attr_get(ma);
4153 if (size != nl_attr_get_size(ma)) {
4154 ds_put_format(ds, "(key length %"PRIuSIZE" != "
4155 "mask length %"PRIuSIZE")",
4156 size, nl_attr_get_size(ma));
4157 return;
4158 }
4159 }
4160 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
4161 break;
4162 }
4163 case OVS_KEY_ATTR_ETHERTYPE:
4164 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
4165 if (!is_exact) {
4166 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
4167 }
4168 break;
4169
4170 case OVS_KEY_ATTR_IPV4: {
4171 const struct ovs_key_ipv4 *key = nl_attr_get(a);
4172 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4173
4174 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4175 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4176 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4177 verbose);
4178 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
4179 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
4180 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
4181 verbose);
4182 ds_chomp(ds, ',');
4183 break;
4184 }
4185 case OVS_KEY_ATTR_IPV6: {
4186 const struct ovs_key_ipv6 *key = nl_attr_get(a);
4187 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4188
4189 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4190 verbose);
4191 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4192 verbose);
4193 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
4194 verbose);
4195 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4196 verbose);
4197 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
4198 verbose);
4199 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
4200 verbose);
4201 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
4202 verbose);
4203 ds_chomp(ds, ',');
4204 break;
4205 }
4206 /* These have the same structure and format. */
4207 case OVS_KEY_ATTR_TCP:
4208 case OVS_KEY_ATTR_UDP:
4209 case OVS_KEY_ATTR_SCTP: {
4210 const struct ovs_key_tcp *key = nl_attr_get(a);
4211 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
4212
4213 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
4214 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
4215 ds_chomp(ds, ',');
4216 break;
4217 }
4218 case OVS_KEY_ATTR_TCP_FLAGS:
4219 if (!is_exact) {
4220 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
4221 ntohs(nl_attr_get_be16(a)),
4222 TCP_FLAGS(nl_attr_get_be16(ma)),
4223 TCP_FLAGS(OVS_BE16_MAX));
4224 } else {
4225 format_flags(ds, packet_tcp_flag_to_string,
4226 ntohs(nl_attr_get_be16(a)), '|');
4227 }
4228 break;
4229
4230 case OVS_KEY_ATTR_ICMP: {
4231 const struct ovs_key_icmp *key = nl_attr_get(a);
4232 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
4233
4234 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
4235 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
4236 ds_chomp(ds, ',');
4237 break;
4238 }
4239 case OVS_KEY_ATTR_ICMPV6: {
4240 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
4241 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
4242
4243 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
4244 verbose);
4245 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
4246 verbose);
4247 ds_chomp(ds, ',');
4248 break;
4249 }
4250 case OVS_KEY_ATTR_ARP: {
4251 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
4252 const struct ovs_key_arp *key = nl_attr_get(a);
4253
4254 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
4255 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
4256 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
4257 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
4258 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
4259 ds_chomp(ds, ',');
4260 break;
4261 }
4262 case OVS_KEY_ATTR_ND: {
4263 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
4264 const struct ovs_key_nd *key = nl_attr_get(a);
4265
4266 format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
4267 verbose);
4268 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
4269 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
4270
4271 ds_chomp(ds, ',');
4272 break;
4273 }
4274 case OVS_KEY_ATTR_ND_EXTENSIONS: {
4275 const struct ovs_key_nd_extensions *mask = ma ? nl_attr_get(ma) : NULL;
4276 const struct ovs_key_nd_extensions *key = nl_attr_get(a);
4277
4278 bool first = true;
4279 format_be32_masked(ds, &first, "nd_reserved", key->nd_reserved,
4280 OVS_BE32_MAX);
4281 ds_put_char(ds, ',');
4282
4283 format_u8u(ds, "nd_options_type", key->nd_options_type,
4284 MASK(mask, nd_options_type), verbose);
4285
4286 ds_chomp(ds, ',');
4287 break;
4288 }
4289 case OVS_KEY_ATTR_NSH: {
4290 format_odp_nsh_attr(a, ma, ds);
4291 break;
4292 }
4293 case OVS_KEY_ATTR_UNSPEC:
4294 case __OVS_KEY_ATTR_MAX:
4295 default:
4296 format_generic_odp_key(a, ds);
4297 if (!is_exact) {
4298 ds_put_char(ds, '/');
4299 format_generic_odp_key(ma, ds);
4300 }
4301 break;
4302 }
4303 ds_put_char(ds, ')');
4304 }
4305
4306 static void
4307 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
4308 const struct hmap *portno_names, struct ds *ds,
4309 bool verbose)
4310 {
4311 if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4312 OVS_KEY_ATTR_MAX, false)) {
4313 format_odp_key_attr__(a, ma, portno_names, ds, verbose);
4314 }
4315 }
4316
4317 static struct nlattr *
4318 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
4319 struct ofpbuf *ofp, const struct nlattr *key)
4320 {
4321 const struct nlattr *a;
4322 unsigned int left;
4323 int type = nl_attr_type(key);
4324 int size = nl_attr_get_size(key);
4325
4326 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
4327 nl_msg_put_unspec_zero(ofp, type, size);
4328 } else {
4329 size_t nested_mask;
4330
4331 if (tbl[type].next) {
4332 const struct attr_len_tbl *entry = &tbl[type];
4333 tbl = entry->next;
4334 max = entry->next_max;
4335 }
4336
4337 nested_mask = nl_msg_start_nested(ofp, type);
4338 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
4339 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
4340 }
4341 nl_msg_end_nested(ofp, nested_mask);
4342 }
4343
4344 return ofp->base;
4345 }
4346
4347 static void
4348 format_u128(struct ds *ds, const ovs_32aligned_u128 *key,
4349 const ovs_32aligned_u128 *mask, bool verbose)
4350 {
4351 if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) {
4352 ovs_be128 value = hton128(get_32aligned_u128(key));
4353 ds_put_hex(ds, &value, sizeof value);
4354 if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) {
4355 value = hton128(get_32aligned_u128(mask));
4356 ds_put_char(ds, '/');
4357 ds_put_hex(ds, &value, sizeof value);
4358 }
4359 }
4360 }
4361
4362 /* Read the string from 's_' as a 128-bit value. If the string contains
4363 * a "/", the rest of the string will be treated as a 128-bit mask.
4364 *
4365 * If either the value or mask is larger than 64 bits, the string must
4366 * be in hexadecimal.
4367 */
4368 static int
4369 scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
4370 {
4371 char *s = CONST_CAST(char *, s_);
4372 ovs_be128 be_value;
4373 ovs_be128 be_mask;
4374
4375 if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
4376 *value = ntoh128(be_value);
4377
4378 if (mask) {
4379 int n;
4380
4381 if (ovs_scan(s, "/%n", &n)) {
4382 int error;
4383
4384 s += n;
4385 error = parse_int_string(s, (uint8_t *)&be_mask,
4386 sizeof be_mask, &s);
4387 if (error) {
4388 return 0;
4389 }
4390 *mask = ntoh128(be_mask);
4391 } else {
4392 *mask = OVS_U128_MAX;
4393 }
4394 }
4395 return s - s_;
4396 }
4397
4398 return 0;
4399 }
4400
4401 int
4402 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
4403 {
4404 const char *s = s_;
4405
4406 if (ovs_scan(s, "ufid:")) {
4407 s += 5;
4408
4409 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
4410 return -EINVAL;
4411 }
4412 s += UUID_LEN;
4413
4414 return s - s_;
4415 }
4416
4417 return 0;
4418 }
4419
4420 void
4421 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
4422 {
4423 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
4424 }
4425
4426 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4427 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4428 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4429 * non-null, translates odp port number to its name. */
4430 void
4431 odp_flow_format(const struct nlattr *key, size_t key_len,
4432 const struct nlattr *mask, size_t mask_len,
4433 const struct hmap *portno_names, struct ds *ds, bool verbose)
4434 {
4435 if (key_len) {
4436 const struct nlattr *a;
4437 unsigned int left;
4438 bool has_ethtype_key = false;
4439 bool has_packet_type_key = false;
4440 struct ofpbuf ofp;
4441 bool first_field = true;
4442
4443 ofpbuf_init(&ofp, 100);
4444 NL_ATTR_FOR_EACH (a, left, key, key_len) {
4445 int attr_type = nl_attr_type(a);
4446 const struct nlattr *ma = (mask && mask_len
4447 ? nl_attr_find__(mask, mask_len,
4448 attr_type)
4449 : NULL);
4450 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4451 OVS_KEY_ATTR_MAX, false)) {
4452 continue;
4453 }
4454
4455 bool is_nested_attr;
4456 bool is_wildcard = false;
4457
4458 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
4459 has_ethtype_key = true;
4460 } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE) {
4461 has_packet_type_key = true;
4462 }
4463
4464 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
4465 OVS_KEY_ATTR_MAX, attr_type) ==
4466 ATTR_LEN_NESTED;
4467
4468 if (mask && mask_len) {
4469 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
4470 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
4471 }
4472
4473 if (verbose || !is_wildcard || is_nested_attr) {
4474 if (is_wildcard && !ma) {
4475 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
4476 OVS_KEY_ATTR_MAX,
4477 &ofp, a);
4478 }
4479 if (!first_field) {
4480 ds_put_char(ds, ',');
4481 }
4482 format_odp_key_attr__(a, ma, portno_names, ds, verbose);
4483 first_field = false;
4484 } else if (attr_type == OVS_KEY_ATTR_ETHERNET
4485 && !has_packet_type_key) {
4486 /* This special case reflects differences between the kernel
4487 * and userspace datapaths regarding the root type of the
4488 * packet being matched (typically Ethernet but some tunnels
4489 * can encapsulate IPv4 etc.). The kernel datapath does not
4490 * have an explicit way to indicate packet type; instead:
4491 *
4492 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4493 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4494 * Ethertype encoded in the Ethernet header.
4495 *
4496 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4497 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4498 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4499 * packet is an IPv4 packet).
4500 *
4501 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4502 * all-wildcarded, it is important to print it.
4503 *
4504 * On the other hand, the userspace datapath supports
4505 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4506 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4507 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4508 if (!first_field) {
4509 ds_put_char(ds, ',');
4510 }
4511 ds_put_cstr(ds, "eth()");
4512 }
4513 ofpbuf_clear(&ofp);
4514 }
4515 ofpbuf_uninit(&ofp);
4516
4517 if (left) {
4518 int i;
4519
4520 if (left == key_len) {
4521 ds_put_cstr(ds, "<empty>");
4522 }
4523 ds_put_format(ds, ",***%u leftover bytes*** (", left);
4524 for (i = 0; i < left; i++) {
4525 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
4526 }
4527 ds_put_char(ds, ')');
4528 }
4529 if (!has_ethtype_key) {
4530 const struct nlattr *ma = nl_attr_find__(mask, mask_len,
4531 OVS_KEY_ATTR_ETHERTYPE);
4532 if (ma) {
4533 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
4534 ntohs(nl_attr_get_be16(ma)));
4535 }
4536 }
4537 } else {
4538 ds_put_cstr(ds, "<empty>");
4539 }
4540 }
4541
4542 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4543 * OVS_KEY_ATTR_* attributes in 'key'. */
4544 void
4545 odp_flow_key_format(const struct nlattr *key,
4546 size_t key_len, struct ds *ds)
4547 {
4548 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
4549 }
4550
4551 static bool
4552 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
4553 {
4554 if (!strcasecmp(s, "no")) {
4555 *type = OVS_FRAG_TYPE_NONE;
4556 } else if (!strcasecmp(s, "first")) {
4557 *type = OVS_FRAG_TYPE_FIRST;
4558 } else if (!strcasecmp(s, "later")) {
4559 *type = OVS_FRAG_TYPE_LATER;
4560 } else {
4561 return false;
4562 }
4563 return true;
4564 }
4565
4566 /* Parsing. */
4567
4568 static int
4569 scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
4570 {
4571 int n;
4572
4573 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
4574 ETH_ADDR_SCAN_ARGS(*key), &n)) {
4575 int len = n;
4576
4577 if (mask) {
4578 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
4579 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
4580 len += n;
4581 } else {
4582 memset(mask, 0xff, sizeof *mask);
4583 }
4584 }
4585 return len;
4586 }
4587 return 0;
4588 }
4589
4590 static int
4591 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
4592 {
4593 int n;
4594
4595 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
4596 int len = n;
4597
4598 if (mask) {
4599 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
4600 IP_SCAN_ARGS(mask), &n)) {
4601 len += n;
4602 } else {
4603 *mask = OVS_BE32_MAX;
4604 }
4605 }
4606 return len;
4607 }
4608 return 0;
4609 }
4610
4611 static int
4612 scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
4613 {
4614 int n;
4615 char ipv6_s[IPV6_SCAN_LEN + 1];
4616
4617 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
4618 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
4619 int len = n;
4620
4621 if (mask) {
4622 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
4623 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
4624 len += n;
4625 } else {
4626 memset(mask, 0xff, sizeof *mask);
4627 }
4628 }
4629 return len;
4630 }
4631 return 0;
4632 }
4633
4634 static int
4635 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
4636 {
4637 int key_, mask_;
4638 int n;
4639
4640 if (ovs_scan(s, "%i%n", &key_, &n)
4641 && (key_ & ~IPV6_LABEL_MASK) == 0) {
4642 int len = n;
4643
4644 *key = htonl(key_);
4645 if (mask) {
4646 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
4647 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
4648 len += n;
4649 *mask = htonl(mask_);
4650 } else {
4651 *mask = htonl(IPV6_LABEL_MASK);
4652 }
4653 }
4654 return len;
4655 }
4656 return 0;
4657 }
4658
4659 static int
4660 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
4661 {
4662 int n;
4663
4664 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
4665 int len = n;
4666
4667 if (mask) {
4668 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
4669 len += n;
4670 } else {
4671 *mask = UINT8_MAX;
4672 }
4673 }
4674 return len;
4675 }
4676 return 0;
4677 }
4678
4679 static int
4680 scan_u16(const char *s, uint16_t *key, uint16_t *mask)
4681 {
4682 int n;
4683
4684 if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
4685 int len = n;
4686
4687 if (mask) {
4688 if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
4689 len += n;
4690 } else {
4691 *mask = UINT16_MAX;
4692 }
4693 }
4694 return len;
4695 }
4696 return 0;
4697 }
4698
4699 static int
4700 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
4701 {
4702 int n;
4703
4704 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
4705 int len = n;
4706
4707 if (mask) {
4708 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
4709 len += n;
4710 } else {
4711 *mask = UINT32_MAX;
4712 }
4713 }
4714 return len;
4715 }
4716 return 0;
4717 }
4718
4719 static int
4720 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
4721 {
4722 uint16_t key_, mask_;
4723 int n;
4724
4725 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
4726 int len = n;
4727
4728 *key = htons(key_);
4729 if (mask) {
4730 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
4731 len += n;
4732 *mask = htons(mask_);
4733 } else {
4734 *mask = OVS_BE16_MAX;
4735 }
4736 }
4737 return len;
4738 }
4739 return 0;
4740 }
4741
4742 static int
4743 scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask)
4744 {
4745 uint32_t key_, mask_;
4746 int n;
4747
4748 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
4749 int len = n;
4750
4751 *key = htonl(key_);
4752 if (mask) {
4753 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
4754 len += n;
4755 *mask = htonl(mask_);
4756 } else {
4757 *mask = OVS_BE32_MAX;
4758 }
4759 }
4760 return len;
4761 }
4762 return 0;
4763 }
4764
4765 static int
4766 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
4767 {
4768 uint64_t key_, mask_;
4769 int n;
4770
4771 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
4772 int len = n;
4773
4774 *key = htonll(key_);
4775 if (mask) {
4776 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
4777 len += n;
4778 *mask = htonll(mask_);
4779 } else {
4780 *mask = OVS_BE64_MAX;
4781 }
4782 }
4783 return len;
4784 }
4785 return 0;
4786 }
4787
4788 static int
4789 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
4790 {
4791 uint32_t flags, fmask;
4792 int n;
4793
4794 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
4795 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
4796 if (n >= 0 && s[n] == ')') {
4797 *key = flags;
4798 if (mask) {
4799 *mask = fmask;
4800 }
4801 return n + 1;
4802 }
4803 return 0;
4804 }
4805
4806 static int
4807 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
4808 {
4809 uint32_t flags, fmask;
4810 int n;
4811
4812 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
4813 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
4814 if (n >= 0) {
4815 *key = htons(flags);
4816 if (mask) {
4817 *mask = htons(fmask);
4818 }
4819 return n;
4820 }
4821 return 0;
4822 }
4823
4824 static uint32_t
4825 ovs_to_odp_ct_state(uint8_t state)
4826 {
4827 uint32_t odp = 0;
4828
4829 #define CS_STATE(ENUM, INDEX, NAME) \
4830 if (state & CS_##ENUM) { \
4831 odp |= OVS_CS_F_##ENUM; \
4832 }
4833 CS_STATES
4834 #undef CS_STATE
4835
4836 return odp;
4837 }
4838
4839 static uint8_t
4840 odp_to_ovs_ct_state(uint32_t flags)
4841 {
4842 uint32_t state = 0;
4843
4844 #define CS_STATE(ENUM, INDEX, NAME) \
4845 if (flags & OVS_CS_F_##ENUM) { \
4846 state |= CS_##ENUM; \
4847 }
4848 CS_STATES
4849 #undef CS_STATE
4850
4851 return state;
4852 }
4853
4854 static int
4855 scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
4856 {
4857 uint32_t flags, fmask;
4858 int n;
4859
4860 n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
4861 ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
4862 mask ? &fmask : NULL);
4863
4864 if (n >= 0) {
4865 *key = flags;
4866 if (mask) {
4867 *mask = fmask;
4868 }
4869 return n;
4870 }
4871 return 0;
4872 }
4873
4874 static int
4875 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
4876 {
4877 int n;
4878 char frag[8];
4879 enum ovs_frag_type frag_type;
4880
4881 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
4882 && ovs_frag_type_from_string(frag, &frag_type)) {
4883 int len = n;
4884
4885 *key = frag_type;
4886 if (mask) {
4887 *mask = UINT8_MAX;
4888 }
4889 return len;
4890 }
4891 return 0;
4892 }
4893
4894 static int
4895 scan_port(const char *s, uint32_t *key, uint32_t *mask,
4896 const struct simap *port_names)
4897 {
4898 int n;
4899
4900 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
4901 int len = n;
4902
4903 if (mask) {
4904 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
4905 len += n;
4906 } else {
4907 *mask = UINT32_MAX;
4908 }
4909 }
4910 return len;
4911 } else if (port_names) {
4912 const struct simap_node *node;
4913 int len;
4914
4915 len = strcspn(s, ")");
4916 node = simap_find_len(port_names, s, len);
4917 if (node) {
4918 *key = node->data;
4919
4920 if (mask) {
4921 *mask = UINT32_MAX;
4922 }
4923 return len;
4924 }
4925 }
4926 return 0;
4927 }
4928
4929 /* Helper for vlan parsing. */
4930 struct ovs_key_vlan__ {
4931 ovs_be16 tci;
4932 };
4933
4934 static bool
4935 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
4936 {
4937 const uint16_t mask = ((1U << bits) - 1) << offset;
4938
4939 if (value >> bits) {
4940 return false;
4941 }
4942
4943 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
4944 return true;
4945 }
4946
4947 static int
4948 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
4949 uint8_t offset)
4950 {
4951 uint16_t key_, mask_;
4952 int n;
4953
4954 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
4955 int len = n;
4956
4957 if (set_be16_bf(key, bits, offset, key_)) {
4958 if (mask) {
4959 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
4960 len += n;
4961
4962 if (!set_be16_bf(mask, bits, offset, mask_)) {
4963 return 0;
4964 }
4965 } else {
4966 *mask |= htons(((1U << bits) - 1) << offset);
4967 }
4968 }
4969 return len;
4970 }
4971 }
4972 return 0;
4973 }
4974
4975 static int
4976 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
4977 {
4978 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
4979 }
4980
4981 static int
4982 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
4983 {
4984 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
4985 }
4986
4987 static int
4988 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
4989 {
4990 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
4991 }
4992
4993 /* For MPLS. */
4994 static bool
4995 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
4996 {
4997 const uint32_t mask = ((1U << bits) - 1) << offset;
4998
4999 if (value >> bits) {
5000 return false;
5001 }
5002
5003 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
5004 return true;
5005 }
5006
5007 static int
5008 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
5009 uint8_t offset)
5010 {
5011 uint32_t key_, mask_;
5012 int n;
5013
5014 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5015 int len = n;
5016
5017 if (set_be32_bf(key, bits, offset, key_)) {
5018 if (mask) {
5019 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5020 len += n;
5021
5022 if (!set_be32_bf(mask, bits, offset, mask_)) {
5023 return 0;
5024 }
5025 } else {
5026 *mask |= htonl(((1U << bits) - 1) << offset);
5027 }
5028 }
5029 return len;
5030 }
5031 }
5032 return 0;
5033 }
5034
5035 static int
5036 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5037 {
5038 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
5039 }
5040
5041 static int
5042 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
5043 {
5044 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
5045 }
5046
5047 static int
5048 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
5049 {
5050 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
5051 }
5052
5053 static int
5054 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
5055 {
5056 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
5057 }
5058
5059 static int
5060 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
5061 {
5062 const char *s_base = s;
5063 ovs_be16 id = 0, id_mask = 0;
5064 uint8_t flags = 0, flags_mask = 0;
5065 int len;
5066
5067 if (!strncmp(s, "id=", 3)) {
5068 s += 3;
5069 len = scan_be16(s, &id, mask ? &id_mask : NULL);
5070 if (len == 0) {
5071 return 0;
5072 }
5073 s += len;
5074 }
5075
5076 if (s[0] == ',') {
5077 s++;
5078 }
5079 if (!strncmp(s, "flags=", 6)) {
5080 s += 6;
5081 len = scan_u8(s, &flags, mask ? &flags_mask : NULL);
5082 if (len == 0) {
5083 return 0;
5084 }
5085 s += len;
5086 }
5087
5088 if (!strncmp(s, "))", 2)) {
5089 s += 2;
5090
5091 *key = (flags << 16) | ntohs(id);
5092 if (mask) {
5093 *mask = (flags_mask << 16) | ntohs(id_mask);
5094 }
5095
5096 return s - s_base;
5097 }
5098
5099 return 0;
5100 }
5101
5102 static int
5103 scan_erspan_metadata(const char *s,
5104 struct erspan_metadata *key,
5105 struct erspan_metadata *mask)
5106 {
5107 const char *s_base = s;
5108 uint32_t idx = 0, idx_mask = 0;
5109 uint8_t ver = 0, dir = 0, hwid = 0;
5110 uint8_t ver_mask = 0, dir_mask = 0, hwid_mask = 0;
5111 int len;
5112
5113 if (!strncmp(s, "ver=", 4)) {
5114 s += 4;
5115 len = scan_u8(s, &ver, mask ? &ver_mask : NULL);
5116 if (len == 0) {
5117 return 0;
5118 }
5119 s += len;
5120 }
5121
5122 if (s[0] == ',') {
5123 s++;
5124 }
5125
5126 if (ver == 1) {
5127 if (!strncmp(s, "idx=", 4)) {
5128 s += 4;
5129 len = scan_u32(s, &idx, mask ? &idx_mask : NULL);
5130 if (len == 0) {
5131 return 0;
5132 }
5133 s += len;
5134 }
5135
5136 if (!strncmp(s, ")", 1)) {
5137 s += 1;
5138 key->version = ver;
5139 key->u.index = htonl(idx);
5140 if (mask) {
5141 mask->u.index = htonl(idx_mask);
5142 }
5143 }
5144 return s - s_base;
5145
5146 } else if (ver == 2) {
5147 if (!strncmp(s, "dir=", 4)) {
5148 s += 4;
5149 len = scan_u8(s, &dir, mask ? &dir_mask : NULL);
5150 if (len == 0) {
5151 return 0;
5152 }
5153 s += len;
5154 }
5155 if (s[0] == ',') {
5156 s++;
5157 }
5158 if (!strncmp(s, "hwid=", 5)) {
5159 s += 5;
5160 len = scan_u8(s, &hwid, mask ? &hwid_mask : NULL);
5161 if (len == 0) {
5162 return 0;
5163 }
5164 s += len;
5165 }
5166
5167 if (!strncmp(s, ")", 1)) {
5168 s += 1;
5169 key->version = ver;
5170 key->u.md2.hwid = hwid;
5171 key->u.md2.dir = dir;
5172 if (mask) {
5173 mask->u.md2.hwid = hwid_mask;
5174 mask->u.md2.dir = dir_mask;
5175 }
5176 }
5177 return s - s_base;
5178 }
5179
5180 return 0;
5181 }
5182
5183 static int
5184 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
5185 {
5186 const char *s_base = s;
5187 struct geneve_opt *opt = key->d;
5188 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
5189 int len_remain = sizeof key->d;
5190 int len;
5191
5192 while (s[0] == '{' && len_remain >= sizeof *opt) {
5193 int data_len = 0;
5194
5195 s++;
5196 len_remain -= sizeof *opt;
5197
5198 if (!strncmp(s, "class=", 6)) {
5199 s += 6;
5200 len = scan_be16(s, &opt->opt_class,
5201 mask ? &opt_mask->opt_class : NULL);
5202 if (len == 0) {
5203 return 0;
5204 }
5205 s += len;
5206 } else if (mask) {
5207 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
5208 }
5209
5210 if (s[0] == ',') {
5211 s++;
5212 }
5213 if (!strncmp(s, "type=", 5)) {
5214 s += 5;
5215 len = scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
5216 if (len == 0) {
5217 return 0;
5218 }
5219 s += len;
5220 } else if (mask) {
5221 memset(&opt_mask->type, 0, sizeof opt_mask->type);
5222 }
5223
5224 if (s[0] == ',') {
5225 s++;
5226 }
5227 if (!strncmp(s, "len=", 4)) {
5228 uint8_t opt_len, opt_len_mask;
5229 s += 4;
5230 len = scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
5231 if (len == 0) {
5232 return 0;
5233 }
5234 s += len;
5235
5236 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
5237 return 0;
5238 }
5239 opt->length = opt_len / 4;
5240 if (mask) {
5241 opt_mask->length = opt_len_mask;
5242 }
5243 data_len = opt_len;
5244 } else if (mask) {
5245 memset(&opt_mask->type, 0, sizeof opt_mask->type);
5246 }
5247
5248 if (s[0] == ',') {
5249 s++;
5250 if (parse_int_string(s, (uint8_t *)(opt + 1),
5251 data_len, (char **)&s)) {
5252 return 0;
5253 }
5254 }
5255 if (mask) {
5256 if (s[0] == '/') {
5257 s++;
5258 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
5259 data_len, (char **)&s)) {
5260 return 0;
5261 }
5262 }
5263 opt_mask->r1 = 0;
5264 opt_mask->r2 = 0;
5265 opt_mask->r3 = 0;
5266 }
5267
5268 if (s[0] == '}') {
5269 s++;
5270 opt += 1 + data_len / 4;
5271 if (mask) {
5272 opt_mask += 1 + data_len / 4;
5273 }
5274 len_remain -= data_len;
5275 } else {
5276 return 0;
5277 }
5278 }
5279
5280 if (s[0] == ')') {
5281 len = sizeof key->d - len_remain;
5282
5283 s++;
5284 key->len = len;
5285 if (mask) {
5286 mask->len = len;
5287 }
5288 return s - s_base;
5289 }
5290
5291 return 0;
5292 }
5293
5294 static void
5295 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
5296 {
5297 const uint16_t *flags = data_;
5298
5299 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
5300 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
5301 }
5302 if (*flags & FLOW_TNL_F_CSUM) {
5303 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
5304 }
5305 if (*flags & FLOW_TNL_F_OAM) {
5306 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
5307 }
5308 }
5309
5310 static void
5311 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
5312 {
5313 const uint32_t *gbp = data_;
5314
5315 if (*gbp) {
5316 size_t vxlan_opts_ofs;
5317
5318 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
5319 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
5320 nl_msg_end_nested(a, vxlan_opts_ofs);
5321 }
5322 }
5323
5324 static void
5325 geneve_to_attr(struct ofpbuf *a, const void *data_)
5326 {
5327 const struct geneve_scan *geneve = data_;
5328
5329 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
5330 geneve->len);
5331 }
5332
5333 static void
5334 erspan_to_attr(struct ofpbuf *a, const void *data_)
5335 {
5336 const struct erspan_metadata *md = data_;
5337
5338 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, md,
5339 sizeof *md);
5340 }
5341
5342 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5343 { \
5344 unsigned long call_fn = (unsigned long)FUNC; \
5345 if (call_fn) { \
5346 typedef void (*fn)(struct ofpbuf *, const void *); \
5347 fn func = FUNC; \
5348 func(BUF, &(DATA)); \
5349 } else { \
5350 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5351 } \
5352 }
5353
5354 #define SCAN_IF(NAME) \
5355 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5356 const char *start = s; \
5357 int len; \
5358 \
5359 s += strlen(NAME)
5360
5361 /* Usually no special initialization is needed. */
5362 #define SCAN_BEGIN(NAME, TYPE) \
5363 SCAN_IF(NAME); \
5364 TYPE skey, smask; \
5365 memset(&skey, 0, sizeof skey); \
5366 memset(&smask, 0, sizeof smask); \
5367 do { \
5368 len = 0;
5369
5370 /* Init as fully-masked as mask will not be scanned. */
5371 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5372 SCAN_IF(NAME); \
5373 TYPE skey, smask; \
5374 memset(&skey, 0, sizeof skey); \
5375 memset(&smask, 0xff, sizeof smask); \
5376 do { \
5377 len = 0;
5378
5379 /* VLAN needs special initialization. */
5380 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5381 SCAN_IF(NAME); \
5382 TYPE skey = KEY_INIT; \
5383 TYPE smask = MASK_INIT; \
5384 do { \
5385 len = 0;
5386
5387 /* Scan unnamed entry as 'TYPE' */
5388 #define SCAN_TYPE(TYPE, KEY, MASK) \
5389 len = scan_##TYPE(s, KEY, MASK); \
5390 if (len == 0) { \
5391 return -EINVAL; \
5392 } \
5393 s += len
5394
5395 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5396 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5397 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5398 s += strlen(NAME); \
5399 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5400 continue; \
5401 }
5402
5403 #define SCAN_FINISH() \
5404 } while (*s++ == ',' && len != 0); \
5405 if (s[-1] != ')') { \
5406 return -EINVAL; \
5407 }
5408
5409 #define SCAN_FINISH_SINGLE() \
5410 } while (false); \
5411 if (*s++ != ')') { \
5412 return -EINVAL; \
5413 }
5414
5415 /* Beginning of nested attribute. */
5416 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5417 SCAN_IF(NAME); \
5418 size_t key_offset, mask_offset = 0; \
5419 key_offset = nl_msg_start_nested(key, ATTR); \
5420 if (mask) { \
5421 mask_offset = nl_msg_start_nested(mask, ATTR); \
5422 } \
5423 do { \
5424 len = 0;
5425
5426 #define SCAN_END_NESTED() \
5427 SCAN_FINISH(); \
5428 nl_msg_end_nested(key, key_offset); \
5429 if (mask) { \
5430 nl_msg_end_nested(mask, mask_offset); \
5431 } \
5432 return s - start; \
5433 }
5434
5435 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5436 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5437 TYPE skey, smask; \
5438 memset(&skey, 0, sizeof skey); \
5439 memset(&smask, 0xff, sizeof smask); \
5440 s += strlen(NAME); \
5441 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5442 SCAN_PUT(ATTR, FUNC); \
5443 continue; \
5444 }
5445
5446 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5447 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5448
5449 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5450 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5451
5452 #define SCAN_PUT(ATTR, FUNC) \
5453 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5454 if (mask) \
5455 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5456
5457 #define SCAN_END(ATTR) \
5458 SCAN_FINISH(); \
5459 SCAN_PUT(ATTR, NULL); \
5460 return s - start; \
5461 }
5462
5463 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5464 SCAN_IF(NAME); \
5465 TYPE skey[CNT], smask[CNT]; \
5466 memset(&skey, 0, sizeof skey); \
5467 memset(&smask, 0, sizeof smask); \
5468 int idx = 0, cnt = CNT; \
5469 uint64_t fields = 0; \
5470 do { \
5471 int field = 0; \
5472 len = 0;
5473
5474 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5475 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5476 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5477 if (fields & (1UL << field)) { \
5478 fields = 0; \
5479 if (++idx == cnt) { \
5480 break; \
5481 } \
5482 } \
5483 s += strlen(NAME); \
5484 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5485 fields |= 1UL << field; \
5486 continue; \
5487 } \
5488 field++;
5489
5490 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5491 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5492
5493 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5494 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5495 if (mask) { \
5496 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5497 }
5498
5499 #define SCAN_END_ARRAY(ATTR) \
5500 SCAN_FINISH(); \
5501 if (idx == cnt) { \
5502 return -EINVAL; \
5503 } \
5504 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5505 return s - start; \
5506 }
5507
5508 #define SCAN_END_SINGLE(ATTR) \
5509 SCAN_FINISH_SINGLE(); \
5510 SCAN_PUT(ATTR, NULL); \
5511 return s - start; \
5512 }
5513
5514 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5515 SCAN_BEGIN(NAME, TYPE) { \
5516 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5517 } SCAN_END_SINGLE(ATTR)
5518
5519 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5520 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5521 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5522 } SCAN_END_SINGLE(ATTR)
5523
5524 /* scan_port needs one extra argument. */
5525 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5526 SCAN_BEGIN(NAME, TYPE) { \
5527 len = scan_port(s, &skey, &smask, \
5528 context->port_names); \
5529 if (len == 0) { \
5530 return -EINVAL; \
5531 } \
5532 s += len; \
5533 } SCAN_END_SINGLE(ATTR)
5534
5535 static int
5536 parse_odp_nsh_key_mask_attr(const char *s, struct ofpbuf *key,
5537 struct ofpbuf *mask)
5538 {
5539 if (strncmp(s, "nsh(", 4) == 0) {
5540 const char *start = s;
5541 int len;
5542 struct ovs_key_nsh skey, smask;
5543 uint32_t spi = 0, spi_mask = 0;
5544 uint8_t si = 0, si_mask = 0;
5545
5546 s += 4;
5547
5548 memset(&skey, 0, sizeof skey);
5549 memset(&smask, 0, sizeof smask);
5550 do {
5551 len = 0;
5552
5553 if (strncmp(s, "flags=", 6) == 0) {
5554 s += 6;
5555 len = scan_u8(s, &skey.flags, mask ? &smask.flags : NULL);
5556 if (len == 0) {
5557 return -EINVAL;
5558 }
5559 s += len;
5560 continue;
5561 }
5562
5563 if (strncmp(s, "mdtype=", 7) == 0) {
5564 s += 7;
5565 len = scan_u8(s, &skey.mdtype, mask ? &smask.mdtype : NULL);
5566 if (len == 0) {
5567 return -EINVAL;
5568 }
5569 s += len;
5570 continue;
5571 }
5572
5573 if (strncmp(s, "np=", 3) == 0) {
5574 s += 3;
5575 len = scan_u8(s, &skey.np, mask ? &smask.np : NULL);
5576 if (len == 0) {
5577 return -EINVAL;
5578 }
5579 s += len;
5580 continue;
5581 }
5582
5583 if (strncmp(s, "spi=", 4) == 0) {
5584 s += 4;
5585 len = scan_u32(s, &spi, mask ? &spi_mask : NULL);
5586 if (len == 0) {
5587 return -EINVAL;
5588 }
5589 s += len;
5590 continue;
5591 }
5592
5593 if (strncmp(s, "si=", 3) == 0) {
5594 s += 3;
5595 len = scan_u8(s, &si, mask ? &si_mask : NULL);
5596 if (len == 0) {
5597 return -EINVAL;
5598 }
5599 s += len;
5600 continue;
5601 }
5602
5603 if (strncmp(s, "c1=", 3) == 0) {
5604 s += 3;
5605 len = scan_be32(s, &skey.context[0],
5606 mask ? &smask.context[0] : NULL);
5607 if (len == 0) {
5608 return -EINVAL;
5609 }
5610 s += len;
5611 continue;
5612 }
5613
5614 if (strncmp(s, "c2=", 3) == 0) {
5615 s += 3;
5616 len = scan_be32(s, &skey.context[1],
5617 mask ? &smask.context[1] : NULL);
5618 if (len == 0) {
5619 return -EINVAL;
5620 }
5621 s += len;
5622 continue;
5623 }
5624
5625 if (strncmp(s, "c3=", 3) == 0) {
5626 s += 3;
5627 len = scan_be32(s, &skey.context[2],
5628 mask ? &smask.context[2] : NULL);
5629 if (len == 0) {
5630 return -EINVAL;
5631 }
5632 s += len;
5633 continue;
5634 }
5635
5636 if (strncmp(s, "c4=", 3) == 0) {
5637 s += 3;
5638 len = scan_be32(s, &skey.context[3],
5639 mask ? &smask.context[3] : NULL);
5640 if (len == 0) {
5641 return -EINVAL;
5642 }
5643 s += len;
5644 continue;
5645 }
5646 } while (*s++ == ',' && len != 0);
5647 if (s[-1] != ')') {
5648 return -EINVAL;
5649 }
5650
5651 skey.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
5652 smask.path_hdr = nsh_spi_si_to_path_hdr(spi_mask, si_mask);
5653
5654 nsh_key_to_attr(key, &skey, NULL, 0, false);
5655 if (mask) {
5656 nsh_key_to_attr(mask, &smask, NULL, 0, true);
5657 }
5658 return s - start;
5659 }
5660 return 0;
5661 }
5662
5663 static int
5664 parse_odp_key_mask_attr(struct parse_odp_context *context, const char *s,
5665 struct ofpbuf *key, struct ofpbuf *mask)
5666 {
5667 int retval;
5668
5669 context->depth++;
5670
5671 if (context->depth == MAX_ODP_NESTED) {
5672 retval = -EINVAL;
5673 } else {
5674 retval = parse_odp_key_mask_attr__(context, s, key, mask);
5675 }
5676
5677 context->depth--;
5678
5679 return retval;
5680 }
5681
5682 static int
5683 parse_odp_key_mask_attr__(struct parse_odp_context *context, const char *s,
5684 struct ofpbuf *key, struct ofpbuf *mask)
5685 {
5686 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
5687 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
5688 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
5689 OVS_KEY_ATTR_RECIRC_ID);
5690 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
5691
5692 SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
5693 SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
5694 SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
5695 SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
5696
5697 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) {
5698 SCAN_FIELD("src=", ipv4, ipv4_src);
5699 SCAN_FIELD("dst=", ipv4, ipv4_dst);
5700 SCAN_FIELD("proto=", u8, ipv4_proto);
5701 SCAN_FIELD("tp_src=", be16, src_port);
5702 SCAN_FIELD("tp_dst=", be16, dst_port);
5703 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
5704
5705 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) {
5706 SCAN_FIELD("src=", in6_addr, ipv6_src);
5707 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
5708 SCAN_FIELD("proto=", u8, ipv6_proto);
5709 SCAN_FIELD("tp_src=", be16, src_port);
5710 SCAN_FIELD("tp_dst=", be16, dst_port);
5711 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
5712
5713 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
5714 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
5715 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
5716 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
5717 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
5718 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
5719 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
5720 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
5721 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
5722 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
5723 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata, erspan_metadata,
5724 erspan_to_attr);
5725 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
5726 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
5727 geneve_to_attr);
5728 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
5729 } SCAN_END_NESTED();
5730
5731 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
5732
5733 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
5734 SCAN_FIELD("src=", eth, eth_src);
5735 SCAN_FIELD("dst=", eth, eth_dst);
5736 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
5737
5738 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
5739 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
5740 SCAN_FIELD("vid=", vid, tci);
5741 SCAN_FIELD("pcp=", pcp, tci);
5742 SCAN_FIELD("cfi=", cfi, tci);
5743 } SCAN_END(OVS_KEY_ATTR_VLAN);
5744
5745 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
5746
5747 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
5748 SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
5749 SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
5750 SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
5751 SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
5752 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
5753
5754 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
5755 SCAN_FIELD("src=", ipv4, ipv4_src);
5756 SCAN_FIELD("dst=", ipv4, ipv4_dst);
5757 SCAN_FIELD("proto=", u8, ipv4_proto);
5758 SCAN_FIELD("tos=", u8, ipv4_tos);
5759 SCAN_FIELD("ttl=", u8, ipv4_ttl);
5760 SCAN_FIELD("frag=", frag, ipv4_frag);
5761 } SCAN_END(OVS_KEY_ATTR_IPV4);
5762
5763 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
5764 SCAN_FIELD("src=", in6_addr, ipv6_src);
5765 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
5766 SCAN_FIELD("label=", ipv6_label, ipv6_label);
5767 SCAN_FIELD("proto=", u8, ipv6_proto);
5768 SCAN_FIELD("tclass=", u8, ipv6_tclass);
5769 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
5770 SCAN_FIELD("frag=", frag, ipv6_frag);
5771 } SCAN_END(OVS_KEY_ATTR_IPV6);
5772
5773 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
5774 SCAN_FIELD("src=", be16, tcp_src);
5775 SCAN_FIELD("dst=", be16, tcp_dst);
5776 } SCAN_END(OVS_KEY_ATTR_TCP);
5777
5778 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
5779
5780 SCAN_BEGIN("udp(", struct ovs_key_udp) {
5781 SCAN_FIELD("src=", be16, udp_src);
5782 SCAN_FIELD("dst=", be16, udp_dst);
5783 } SCAN_END(OVS_KEY_ATTR_UDP);
5784
5785 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
5786 SCAN_FIELD("src=", be16, sctp_src);
5787 SCAN_FIELD("dst=", be16, sctp_dst);
5788 } SCAN_END(OVS_KEY_ATTR_SCTP);
5789
5790 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
5791 SCAN_FIELD("type=", u8, icmp_type);
5792 SCAN_FIELD("code=", u8, icmp_code);
5793 } SCAN_END(OVS_KEY_ATTR_ICMP);
5794
5795 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
5796 SCAN_FIELD("type=", u8, icmpv6_type);
5797 SCAN_FIELD("code=", u8, icmpv6_code);
5798 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
5799
5800 SCAN_BEGIN("arp(", struct ovs_key_arp) {
5801 SCAN_FIELD("sip=", ipv4, arp_sip);
5802 SCAN_FIELD("tip=", ipv4, arp_tip);
5803 SCAN_FIELD("op=", be16, arp_op);
5804 SCAN_FIELD("sha=", eth, arp_sha);
5805 SCAN_FIELD("tha=", eth, arp_tha);
5806 } SCAN_END(OVS_KEY_ATTR_ARP);
5807
5808 SCAN_BEGIN("nd(", struct ovs_key_nd) {
5809 SCAN_FIELD("target=", in6_addr, nd_target);
5810 SCAN_FIELD("sll=", eth, nd_sll);
5811 SCAN_FIELD("tll=", eth, nd_tll);
5812 } SCAN_END(OVS_KEY_ATTR_ND);
5813
5814 SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions) {
5815 SCAN_FIELD("nd_reserved=", be32, nd_reserved);
5816 SCAN_FIELD("nd_options_type=", u8, nd_options_type);
5817 } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS);
5818
5819 struct packet_type {
5820 ovs_be16 ns;
5821 ovs_be16 id;
5822 };
5823 SCAN_BEGIN("packet_type(", struct packet_type) {
5824 SCAN_FIELD("ns=", be16, ns);
5825 SCAN_FIELD("id=", be16, id);
5826 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE);
5827
5828 /* nsh is nested, it needs special process */
5829 int ret = parse_odp_nsh_key_mask_attr(s, key, mask);
5830 if (ret < 0) {
5831 return ret;
5832 } else {
5833 s += ret;
5834 }
5835
5836 /* Encap open-coded. */
5837 if (!strncmp(s, "encap(", 6)) {
5838 const char *start = s;
5839 size_t encap, encap_mask = 0;
5840
5841 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
5842 if (mask) {
5843 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
5844 }
5845
5846 s += 6;
5847 for (;;) {
5848 int retval;
5849
5850 s += strspn(s, delimiters);
5851 if (!*s) {
5852 return -EINVAL;
5853 } else if (*s == ')') {
5854 break;
5855 }
5856
5857 retval = parse_odp_key_mask_attr(context, s, key, mask);
5858 if (retval < 0) {
5859 return retval;
5860 }
5861
5862 if (nl_attr_oversized(key->size - encap - NLA_HDRLEN)) {
5863 return -E2BIG;
5864 }
5865 s += retval;
5866 }
5867 s++;
5868
5869 nl_msg_end_nested(key, encap);
5870 if (mask) {
5871 nl_msg_end_nested(mask, encap_mask);
5872 }
5873
5874 return s - start;
5875 }
5876
5877 return -EINVAL;
5878 }
5879
5880 /* Parses the string representation of a datapath flow key, in the format
5881 * output by odp_flow_key_format(). Returns 0 if successful, otherwise a
5882 * positive errno value. On success, stores NULL into '*errorp' and the flow
5883 * key is appended to 'key' as a series of Netlink attributes. On failure,
5884 * stores a malloc()'d error message in '*errorp' without changing the data in
5885 * 'key'. Either way, 'key''s data might be reallocated.
5886 *
5887 * If 'port_names' is nonnull, it points to an simap that maps from a port name
5888 * to a port number. (Port names may be used instead of port numbers in
5889 * in_port.)
5890 *
5891 * On success, the attributes appended to 'key' are individually syntactically
5892 * valid, but they may not be valid as a sequence. 'key' might, for example,
5893 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
5894 int
5895 odp_flow_from_string(const char *s, const struct simap *port_names,
5896 struct ofpbuf *key, struct ofpbuf *mask,
5897 char **errorp)
5898 {
5899 if (errorp) {
5900 *errorp = NULL;
5901 }
5902
5903 const size_t old_size = key->size;
5904 struct parse_odp_context context = (struct parse_odp_context) {
5905 .port_names = port_names,
5906 };
5907 for (;;) {
5908 int retval;
5909
5910 s += strspn(s, delimiters);
5911 if (!*s) {
5912 return 0;
5913 }
5914
5915 /* Skip UFID. */
5916 ovs_u128 ufid;
5917 retval = odp_ufid_from_string(s, &ufid);
5918 if (retval < 0) {
5919 if (errorp) {
5920 *errorp = xasprintf("syntax error at %s", s);
5921 }
5922 key->size = old_size;
5923 return -retval;
5924 } else if (retval > 0) {
5925 s += retval;
5926 s += s[0] == ' ' ? 1 : 0;
5927 }
5928
5929 retval = parse_odp_key_mask_attr(&context, s, key, mask);
5930 if (retval < 0) {
5931 if (errorp) {
5932 *errorp = xasprintf("syntax error at %s", s);
5933 }
5934 key->size = old_size;
5935 return -retval;
5936 }
5937 s += retval;
5938 }
5939
5940 return 0;
5941 }
5942
5943 static uint8_t
5944 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
5945 {
5946 if (is_mask) {
5947 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
5948 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
5949 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
5950 * must use a zero mask for the netlink frag field, and all ones mask
5951 * otherwise. */
5952 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
5953 }
5954 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
5955 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
5956 : OVS_FRAG_TYPE_FIRST;
5957 }
5958
5959 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
5960 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
5961 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
5962 bool is_mask);
5963 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
5964 bool is_mask);
5965 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
5966 bool is_mask);
5967 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
5968 bool is_mask);
5969 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
5970 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
5971 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
5972 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
5973 static void get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh,
5974 bool is_mask);
5975 static void put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow,
5976 bool is_mask);
5977
5978 /* These share the same layout. */
5979 union ovs_key_tp {
5980 struct ovs_key_tcp tcp;
5981 struct ovs_key_udp udp;
5982 struct ovs_key_sctp sctp;
5983 };
5984
5985 static void get_tp_key(const struct flow *, union ovs_key_tp *);
5986 static void put_tp_key(const union ovs_key_tp *, struct flow *);
5987
5988 static void
5989 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
5990 bool export_mask, struct ofpbuf *buf)
5991 {
5992 /* New "struct flow" fields that are visible to the datapath (including all
5993 * data fields) should be translated into equivalent datapath flow fields
5994 * here (you will have to add a OVS_KEY_ATTR_* for them). */
5995 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
5996
5997 struct ovs_key_ethernet *eth_key;
5998 size_t encap[FLOW_MAX_VLAN_HEADERS] = {0};
5999 size_t max_vlans;
6000 const struct flow *flow = parms->flow;
6001 const struct flow *mask = parms->mask;
6002 const struct flow *data = export_mask ? mask : flow;
6003
6004 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
6005
6006 if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) {
6007 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
6008 parms->key_buf, NULL);
6009 }
6010
6011 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
6012
6013 if (parms->support.ct_state) {
6014 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6015 ovs_to_odp_ct_state(data->ct_state));
6016 }
6017 if (parms->support.ct_zone) {
6018 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
6019 }
6020 if (parms->support.ct_mark) {
6021 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
6022 }
6023 if (parms->support.ct_label) {
6024 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
6025 sizeof(data->ct_label));
6026 }
6027 if (flow->ct_nw_proto) {
6028 if (parms->support.ct_orig_tuple
6029 && flow->dl_type == htons(ETH_TYPE_IP)) {
6030 struct ovs_key_ct_tuple_ipv4 ct = {
6031 data->ct_nw_src,
6032 data->ct_nw_dst,
6033 data->ct_tp_src,
6034 data->ct_tp_dst,
6035 data->ct_nw_proto,
6036 };
6037 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, &ct,
6038 sizeof ct);
6039 } else if (parms->support.ct_orig_tuple6
6040 && flow->dl_type == htons(ETH_TYPE_IPV6)) {
6041 struct ovs_key_ct_tuple_ipv6 ct = {
6042 data->ct_ipv6_src,
6043 data->ct_ipv6_dst,
6044 data->ct_tp_src,
6045 data->ct_tp_dst,
6046 data->ct_nw_proto,
6047 };
6048 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, &ct,
6049 sizeof ct);
6050 }
6051 }
6052 if (parms->support.recirc) {
6053 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
6054 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
6055 }
6056
6057 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
6058 * is not the magical value "ODPP_NONE". */
6059 if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
6060 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
6061 }
6062
6063 nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type);
6064
6065 if (OVS_UNLIKELY(parms->probe)) {
6066 max_vlans = FLOW_MAX_VLAN_HEADERS;
6067 } else {
6068 max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit);
6069 }
6070
6071 /* Conditionally add L2 attributes for Ethernet packets */
6072 if (flow->packet_type == htonl(PT_ETH)) {
6073 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
6074 sizeof *eth_key);
6075 get_ethernet_key(data, eth_key);
6076
6077 for (int encaps = 0; encaps < max_vlans; encaps++) {
6078 ovs_be16 tpid = flow->vlans[encaps].tpid;
6079
6080 if (flow->vlans[encaps].tci == htons(0)) {
6081 if (eth_type_vlan(flow->dl_type)) {
6082 /* If VLAN was truncated the tpid is in dl_type */
6083 tpid = flow->dl_type;
6084 } else {
6085 break;
6086 }
6087 }
6088
6089 if (export_mask) {
6090 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6091 } else {
6092 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid);
6093 }
6094 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci);
6095 encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
6096 if (flow->vlans[encaps].tci == htons(0)) {
6097 goto unencap;
6098 }
6099 }
6100 }
6101
6102 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
6103 /* For backwards compatibility with kernels that don't support
6104 * wildcarding, the following convention is used to encode the
6105 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
6106 *
6107 * key mask matches
6108 * -------- -------- -------
6109 * >0x5ff 0xffff Specified Ethernet II Ethertype.
6110 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
6111 * <none> 0xffff Any non-Ethernet II frame (except valid
6112 * 802.3 SNAP packet with valid eth_type).
6113 */
6114 if (export_mask) {
6115 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6116 }
6117 goto unencap;
6118 }
6119
6120 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
6121
6122 if (eth_type_vlan(flow->dl_type)) {
6123 goto unencap;
6124 }
6125
6126 if (flow->dl_type == htons(ETH_TYPE_IP)) {
6127 struct ovs_key_ipv4 *ipv4_key;
6128
6129 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
6130 sizeof *ipv4_key);
6131 get_ipv4_key(data, ipv4_key, export_mask);
6132 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6133 struct ovs_key_ipv6 *ipv6_key;
6134
6135 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
6136 sizeof *ipv6_key);
6137 get_ipv6_key(data, ipv6_key, export_mask);
6138 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
6139 flow->dl_type == htons(ETH_TYPE_RARP)) {
6140 struct ovs_key_arp *arp_key;
6141
6142 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
6143 sizeof *arp_key);
6144 get_arp_key(data, arp_key);
6145 } else if (eth_type_mpls(flow->dl_type)) {
6146 struct ovs_key_mpls *mpls_key;
6147 int i, n;
6148
6149 n = flow_count_mpls_labels(flow, NULL);
6150 if (export_mask) {
6151 n = MIN(n, parms->support.max_mpls_depth);
6152 }
6153 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
6154 n * sizeof *mpls_key);
6155 for (i = 0; i < n; i++) {
6156 mpls_key[i].mpls_lse = data->mpls_lse[i];
6157 }
6158 } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
6159 nsh_key_to_attr(buf, &data->nsh, NULL, 0, export_mask);
6160 }
6161
6162 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6163 if (flow->nw_proto == IPPROTO_TCP) {
6164 union ovs_key_tp *tcp_key;
6165
6166 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
6167 sizeof *tcp_key);
6168 get_tp_key(data, tcp_key);
6169 if (data->tcp_flags || (mask && mask->tcp_flags)) {
6170 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
6171 }
6172 } else if (flow->nw_proto == IPPROTO_UDP) {
6173 union ovs_key_tp *udp_key;
6174
6175 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
6176 sizeof *udp_key);
6177 get_tp_key(data, udp_key);
6178 } else if (flow->nw_proto == IPPROTO_SCTP) {
6179 union ovs_key_tp *sctp_key;
6180
6181 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
6182 sizeof *sctp_key);
6183 get_tp_key(data, sctp_key);
6184 } else if (flow->dl_type == htons(ETH_TYPE_IP)
6185 && flow->nw_proto == IPPROTO_ICMP) {
6186 struct ovs_key_icmp *icmp_key;
6187
6188 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
6189 sizeof *icmp_key);
6190 icmp_key->icmp_type = ntohs(data->tp_src);
6191 icmp_key->icmp_code = ntohs(data->tp_dst);
6192 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
6193 && flow->nw_proto == IPPROTO_ICMPV6) {
6194 struct ovs_key_icmpv6 *icmpv6_key;
6195
6196 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
6197 sizeof *icmpv6_key);
6198 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
6199 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
6200
6201 if (is_nd(flow, NULL)
6202 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
6203 * type and code are 8 bits wide. Therefore, an exact match
6204 * looks like htons(0xff), not htons(0xffff). See
6205 * xlate_wc_finish() for details. */
6206 && (!export_mask || (data->tp_src == htons(0xff)
6207 && data->tp_dst == htons(0xff)))) {
6208 struct ovs_key_nd *nd_key;
6209 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
6210 sizeof *nd_key);
6211 nd_key->nd_target = data->nd_target;
6212 nd_key->nd_sll = data->arp_sha;
6213 nd_key->nd_tll = data->arp_tha;
6214
6215 /* Add ND Extensions Attr only if supported and reserved field
6216 * or options type is set. */
6217 if (parms->support.nd_ext) {
6218 struct ovs_key_nd_extensions *nd_ext_key;
6219
6220 if (data->igmp_group_ip4 != 0 || data->tcp_flags != 0) {
6221 nd_ext_key = nl_msg_put_unspec_uninit(buf,
6222 OVS_KEY_ATTR_ND_EXTENSIONS,
6223 sizeof *nd_ext_key);
6224 nd_ext_key->nd_reserved = data->igmp_group_ip4;
6225 nd_ext_key->nd_options_type = ntohs(data->tcp_flags);
6226 }
6227 }
6228 }
6229 }
6230 }
6231
6232 unencap:
6233 for (int encaps = max_vlans - 1; encaps >= 0; encaps--) {
6234 if (encap[encaps]) {
6235 nl_msg_end_nested(buf, encap[encaps]);
6236 }
6237 }
6238 }
6239
6240 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
6241 *
6242 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6243 * capable of being expanded to allow for that much space. */
6244 void
6245 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
6246 struct ofpbuf *buf)
6247 {
6248 odp_flow_key_from_flow__(parms, false, buf);
6249 }
6250
6251 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
6252 * 'buf'.
6253 *
6254 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6255 * capable of being expanded to allow for that much space. */
6256 void
6257 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
6258 struct ofpbuf *buf)
6259 {
6260 odp_flow_key_from_flow__(parms, true, buf);
6261 }
6262
6263 /* Generate ODP flow key from the given packet metadata */
6264 void
6265 odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet)
6266 {
6267 const struct pkt_metadata *md = &packet->md;
6268
6269 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
6270
6271 if (flow_tnl_dst_is_set(&md->tunnel)) {
6272 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL, NULL);
6273 }
6274
6275 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
6276
6277 if (md->ct_state) {
6278 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6279 ovs_to_odp_ct_state(md->ct_state));
6280 if (md->ct_zone) {
6281 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
6282 }
6283 if (md->ct_mark) {
6284 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
6285 }
6286 if (!ovs_u128_is_zero(md->ct_label)) {
6287 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
6288 sizeof(md->ct_label));
6289 }
6290 if (md->ct_orig_tuple_ipv6) {
6291 if (md->ct_orig_tuple.ipv6.ipv6_proto) {
6292 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
6293 &md->ct_orig_tuple.ipv6,
6294 sizeof md->ct_orig_tuple.ipv6);
6295 }
6296 } else {
6297 if (md->ct_orig_tuple.ipv4.ipv4_proto) {
6298 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
6299 &md->ct_orig_tuple.ipv4,
6300 sizeof md->ct_orig_tuple.ipv4);
6301 }
6302 }
6303 }
6304
6305 /* Add an ingress port attribute if 'odp_in_port' is not the magical
6306 * value "ODPP_NONE". */
6307 if (md->in_port.odp_port != ODPP_NONE) {
6308 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
6309 }
6310
6311 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6312 if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) {
6313 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE,
6314 pt_ns_type_be(packet->packet_type));
6315 }
6316 }
6317
6318 /* Generate packet metadata from the given ODP flow key. */
6319 void
6320 odp_key_to_dp_packet(const struct nlattr *key, size_t key_len,
6321 struct dp_packet *packet)
6322 {
6323 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6324 const struct nlattr *nla;
6325 struct pkt_metadata *md = &packet->md;
6326 ovs_be32 packet_type = htonl(PT_UNKNOWN);
6327 ovs_be16 ethertype = 0;
6328 size_t left;
6329
6330 pkt_metadata_init(md, ODPP_NONE);
6331
6332 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
6333 enum ovs_key_attr type = nl_attr_type(nla);
6334 size_t len = nl_attr_get_size(nla);
6335 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
6336 OVS_KEY_ATTR_MAX, type);
6337
6338 if (len != expected_len && expected_len >= 0) {
6339 continue;
6340 }
6341
6342 switch (type) {
6343 case OVS_KEY_ATTR_RECIRC_ID:
6344 md->recirc_id = nl_attr_get_u32(nla);
6345 break;
6346 case OVS_KEY_ATTR_DP_HASH:
6347 md->dp_hash = nl_attr_get_u32(nla);
6348 break;
6349 case OVS_KEY_ATTR_PRIORITY:
6350 md->skb_priority = nl_attr_get_u32(nla);
6351 break;
6352 case OVS_KEY_ATTR_SKB_MARK:
6353 md->pkt_mark = nl_attr_get_u32(nla);
6354 break;
6355 case OVS_KEY_ATTR_CT_STATE:
6356 md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
6357 break;
6358 case OVS_KEY_ATTR_CT_ZONE:
6359 md->ct_zone = nl_attr_get_u16(nla);
6360 break;
6361 case OVS_KEY_ATTR_CT_MARK:
6362 md->ct_mark = nl_attr_get_u32(nla);
6363 break;
6364 case OVS_KEY_ATTR_CT_LABELS: {
6365 md->ct_label = nl_attr_get_u128(nla);
6366 break;
6367 }
6368 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
6369 const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla);
6370 md->ct_orig_tuple.ipv4 = *ct;
6371 md->ct_orig_tuple_ipv6 = false;
6372 break;
6373 }
6374 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
6375 const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla);
6376
6377 md->ct_orig_tuple.ipv6 = *ct;
6378 md->ct_orig_tuple_ipv6 = true;
6379 break;
6380 }
6381 case OVS_KEY_ATTR_TUNNEL: {
6382 enum odp_key_fitness res;
6383
6384 res = odp_tun_key_from_attr(nla, &md->tunnel, NULL);
6385 if (res == ODP_FIT_ERROR) {
6386 memset(&md->tunnel, 0, sizeof md->tunnel);
6387 }
6388 break;
6389 }
6390 case OVS_KEY_ATTR_IN_PORT:
6391 md->in_port.odp_port = nl_attr_get_odp_port(nla);
6392 break;
6393 case OVS_KEY_ATTR_ETHERNET:
6394 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6395 packet_type = htonl(PT_ETH);
6396 break;
6397 case OVS_KEY_ATTR_ETHERTYPE:
6398 ethertype = nl_attr_get_be16(nla);
6399 break;
6400 case OVS_KEY_ATTR_UNSPEC:
6401 case OVS_KEY_ATTR_ENCAP:
6402 case OVS_KEY_ATTR_VLAN:
6403 case OVS_KEY_ATTR_IPV4:
6404 case OVS_KEY_ATTR_IPV6:
6405 case OVS_KEY_ATTR_TCP:
6406 case OVS_KEY_ATTR_UDP:
6407 case OVS_KEY_ATTR_ICMP:
6408 case OVS_KEY_ATTR_ICMPV6:
6409 case OVS_KEY_ATTR_ARP:
6410 case OVS_KEY_ATTR_ND:
6411 case OVS_KEY_ATTR_ND_EXTENSIONS:
6412 case OVS_KEY_ATTR_SCTP:
6413 case OVS_KEY_ATTR_TCP_FLAGS:
6414 case OVS_KEY_ATTR_MPLS:
6415 case OVS_KEY_ATTR_PACKET_TYPE:
6416 case OVS_KEY_ATTR_NSH:
6417 case __OVS_KEY_ATTR_MAX:
6418 default:
6419 break;
6420 }
6421 }
6422
6423 if (packet_type == htonl(PT_ETH)) {
6424 packet->packet_type = htonl(PT_ETH);
6425 } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) {
6426 packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
6427 ntohs(ethertype));
6428 } else {
6429 VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type.");
6430 }
6431 }
6432
6433 uint32_t
6434 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
6435 {
6436 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
6437 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key), key_len, 0);
6438 }
6439
6440 static void
6441 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
6442 uint64_t attrs, int out_of_range_attr,
6443 const struct nlattr *key, size_t key_len)
6444 {
6445 struct ds s;
6446 int i;
6447
6448 if (VLOG_DROP_DBG(rl)) {
6449 return;
6450 }
6451
6452 ds_init(&s);
6453 for (i = 0; i < 64; i++) {
6454 if (attrs & (UINT64_C(1) << i)) {
6455 char namebuf[OVS_KEY_ATTR_BUFSIZE];
6456
6457 ds_put_format(&s, " %s",
6458 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
6459 }
6460 }
6461 if (out_of_range_attr) {
6462 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
6463 }
6464
6465 ds_put_cstr(&s, ": ");
6466 odp_flow_key_format(key, key_len, &s);
6467
6468 VLOG_DBG("%s:%s", title, ds_cstr(&s));
6469 ds_destroy(&s);
6470 }
6471
6472 static uint8_t
6473 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
6474 {
6475 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6476
6477 if (is_mask) {
6478 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
6479 }
6480
6481 if (odp_frag > OVS_FRAG_TYPE_LATER) {
6482 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
6483 return 0xff; /* Error. */
6484 }
6485
6486 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
6487 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
6488 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
6489 }
6490
6491 /* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which
6492 * must have OVS_KEY_ATTR_MAX + 1 elements. Stores each attribute in 'key'
6493 * into the corresponding element of 'attrs'.
6494 *
6495 * Stores a bitmask of the attributes' indexes found in 'key' into
6496 * '*present_attrsp'.
6497 *
6498 * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type
6499 * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0.
6500 *
6501 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6502 * error message in '*errorp'. */
6503 static bool
6504 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
6505 const struct nlattr *attrs[], uint64_t *present_attrsp,
6506 int *out_of_range_attrp, char **errorp)
6507 {
6508 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6509 const struct nlattr *nla;
6510 uint64_t present_attrs;
6511 size_t left;
6512
6513 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
6514 present_attrs = 0;
6515 *out_of_range_attrp = 0;
6516 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
6517 uint16_t type = nl_attr_type(nla);
6518 size_t len = nl_attr_get_size(nla);
6519 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
6520 OVS_KEY_ATTR_MAX, type);
6521
6522 if (len != expected_len && expected_len >= 0) {
6523 char namebuf[OVS_KEY_ATTR_BUFSIZE];
6524
6525 odp_parse_error(&rl, errorp, "attribute %s has length %"PRIuSIZE" "
6526 "but should have length %d",
6527 ovs_key_attr_to_string(type, namebuf,
6528 sizeof namebuf),
6529 len, expected_len);
6530 return false;
6531 }
6532
6533 if (type > OVS_KEY_ATTR_MAX) {
6534 *out_of_range_attrp = type;
6535 } else {
6536 if (present_attrs & (UINT64_C(1) << type)) {
6537 char namebuf[OVS_KEY_ATTR_BUFSIZE];
6538
6539 odp_parse_error(&rl, errorp,
6540 "duplicate %s attribute in flow key",
6541 ovs_key_attr_to_string(type, namebuf,
6542 sizeof namebuf));
6543 return false;
6544 }
6545
6546 present_attrs |= UINT64_C(1) << type;
6547 attrs[type] = nla;
6548 }
6549 }
6550 if (left) {
6551 odp_parse_error(&rl, errorp, "trailing garbage in flow key");
6552 return false;
6553 }
6554
6555 *present_attrsp = present_attrs;
6556 return true;
6557 }
6558
6559 static enum odp_key_fitness
6560 check_expectations(uint64_t present_attrs, int out_of_range_attr,
6561 uint64_t expected_attrs,
6562 const struct nlattr *key, size_t key_len)
6563 {
6564 uint64_t missing_attrs;
6565 uint64_t extra_attrs;
6566
6567 missing_attrs = expected_attrs & ~present_attrs;
6568 if (missing_attrs) {
6569 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6570 log_odp_key_attributes(&rl, "expected but not present",
6571 missing_attrs, 0, key, key_len);
6572 return ODP_FIT_TOO_LITTLE;
6573 }
6574
6575 extra_attrs = present_attrs & ~expected_attrs;
6576 if (extra_attrs || out_of_range_attr) {
6577 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6578 log_odp_key_attributes(&rl, "present but not expected",
6579 extra_attrs, out_of_range_attr, key, key_len);
6580 return ODP_FIT_TOO_MUCH;
6581 }
6582
6583 return ODP_FIT_PERFECT;
6584 }
6585
6586 /* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the
6587 * attributes in the bit-mask 'present_attrs' are present. Returns true if
6588 * successful, false on failure.
6589 *
6590 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6591 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6592 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6593 * previously parsed flow key.
6594 *
6595 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6596 * error message in '*errorp'. */
6597 static bool
6598 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
6599 uint64_t present_attrs, uint64_t *expected_attrs,
6600 struct flow *flow, const struct flow *src_flow,
6601 char **errorp)
6602 {
6603 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6604 bool is_mask = flow != src_flow;
6605
6606 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
6607 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
6608 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
6609 odp_parse_error(&rl, errorp,
6610 "invalid Ethertype %"PRIu16" in flow key",
6611 ntohs(flow->dl_type));
6612 return false;
6613 }
6614 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
6615 flow->dl_type != htons(0xffff)) {
6616 odp_parse_error(&rl, errorp, "can't bitwise match non-Ethernet II "
6617 "\"Ethertype\" %#"PRIx16" (with mask %#"PRIx16")",
6618 ntohs(src_flow->dl_type), ntohs(flow->dl_type));
6619 return false;
6620 }
6621 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
6622 } else {
6623 if (!is_mask) {
6624 /* Default ethertype for well-known L3 packets. */
6625 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
6626 flow->dl_type = htons(ETH_TYPE_IP);
6627 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
6628 flow->dl_type = htons(ETH_TYPE_IPV6);
6629 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
6630 flow->dl_type = htons(ETH_TYPE_MPLS);
6631 } else {
6632 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
6633 }
6634 } else if (src_flow->packet_type != htonl(PT_ETH)) {
6635 /* dl_type is mandatory for non-Ethernet packets */
6636 flow->dl_type = htons(0xffff);
6637 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
6638 /* See comments in odp_flow_key_from_flow__(). */
6639 odp_parse_error(&rl, errorp,
6640 "mask expected for non-Ethernet II frame");
6641 return false;
6642 }
6643 }
6644 return true;
6645 }
6646
6647 /* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in
6648 * 'attrs', in which the attributes in the bit-mask 'present_attrs' are
6649 * present. The caller also indicates an out-of-range attribute
6650 * 'out_of_range_attr' if one was present when parsing (if so, the fitness
6651 * cannot be perfect).
6652 *
6653 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6654 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6655 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6656 * previously parsed flow key.
6657 *
6658 * Returns fitness based on any discrepancies between present and expected
6659 * attributes, except that a 'need_check' of false overrides this.
6660 *
6661 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6662 * error message in '*errorp'. 'key' and 'key_len' are just used for error
6663 * reporting in this case. */
6664 static enum odp_key_fitness
6665 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
6666 uint64_t present_attrs, int out_of_range_attr,
6667 uint64_t *expected_attrs, struct flow *flow,
6668 const struct nlattr *key, size_t key_len,
6669 const struct flow *src_flow, bool need_check, char **errorp)
6670 {
6671 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6672 bool is_mask = src_flow != flow;
6673 const void *check_start = NULL;
6674 size_t check_len = 0;
6675 enum ovs_key_attr expected_bit = 0xff;
6676
6677 if (eth_type_mpls(src_flow->dl_type)) {
6678 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
6679 *expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
6680 }
6681 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
6682 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
6683 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
6684 int n = size / sizeof(ovs_be32);
6685 int i;
6686
6687 if (!size || size % sizeof(ovs_be32)) {
6688 odp_parse_error(&rl, errorp,
6689 "MPLS LSEs have invalid length %"PRIuSIZE,
6690 size);
6691 return ODP_FIT_ERROR;
6692 }
6693 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
6694 odp_parse_error(&rl, errorp,
6695 "unexpected MPLS Ethertype mask %x"PRIx16,
6696 ntohs(flow->dl_type));
6697 return ODP_FIT_ERROR;
6698 }
6699
6700 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
6701 flow->mpls_lse[i] = mpls_lse[i];
6702 }
6703 if (n > FLOW_MAX_MPLS_LABELS) {
6704 return ODP_FIT_TOO_MUCH;
6705 }
6706
6707 if (!is_mask) {
6708 /* BOS may be set only in the innermost label. */
6709 for (i = 0; i < n - 1; i++) {
6710 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
6711 odp_parse_error(&rl, errorp,
6712 "MPLS BOS set in non-innermost label");
6713 return ODP_FIT_ERROR;
6714 }
6715 }
6716
6717 /* BOS must be set in the innermost label. */
6718 if (n < FLOW_MAX_MPLS_LABELS
6719 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
6720 return ODP_FIT_TOO_LITTLE;
6721 }
6722 }
6723 }
6724
6725 goto done;
6726 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
6727 if (!is_mask) {
6728 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
6729 }
6730 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
6731 const struct ovs_key_ipv4 *ipv4_key;
6732
6733 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
6734 put_ipv4_key(ipv4_key, flow, is_mask);
6735 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
6736 odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV4 has invalid "
6737 "nw_frag %#"PRIx8, flow->nw_frag);
6738 return ODP_FIT_ERROR;
6739 }
6740
6741 if (is_mask) {
6742 check_start = ipv4_key;
6743 check_len = sizeof *ipv4_key;
6744 expected_bit = OVS_KEY_ATTR_IPV4;
6745 }
6746 }
6747 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
6748 if (!is_mask) {
6749 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
6750 }
6751 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
6752 const struct ovs_key_ipv6 *ipv6_key;
6753
6754 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
6755 put_ipv6_key(ipv6_key, flow, is_mask);
6756 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
6757 odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV6 has invalid "
6758 "nw_frag %#"PRIx8, flow->nw_frag);
6759 return ODP_FIT_ERROR;
6760 }
6761 if (is_mask) {
6762 check_start = ipv6_key;
6763 check_len = sizeof *ipv6_key;
6764 expected_bit = OVS_KEY_ATTR_IPV6;
6765 }
6766 }
6767 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
6768 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
6769 if (!is_mask) {
6770 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
6771 }
6772 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
6773 const struct ovs_key_arp *arp_key;
6774
6775 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
6776 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
6777 odp_parse_error(&rl, errorp,
6778 "unsupported ARP opcode %"PRIu16" in flow "
6779 "key", ntohs(arp_key->arp_op));
6780 return ODP_FIT_ERROR;
6781 }
6782 put_arp_key(arp_key, flow);
6783 if (is_mask) {
6784 check_start = arp_key;
6785 check_len = sizeof *arp_key;
6786 expected_bit = OVS_KEY_ATTR_ARP;
6787 }
6788 }
6789 } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) {
6790 if (!is_mask) {
6791 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH;
6792 }
6793 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) {
6794 if (odp_nsh_key_from_attr__(attrs[OVS_KEY_ATTR_NSH],
6795 is_mask, &flow->nsh,
6796 NULL, errorp) == ODP_FIT_ERROR) {
6797 return ODP_FIT_ERROR;
6798 }
6799 if (is_mask) {
6800 check_start = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]);
6801 check_len = nl_attr_get_size(attrs[OVS_KEY_ATTR_NSH]);
6802 expected_bit = OVS_KEY_ATTR_NSH;
6803 }
6804 }
6805 } else {
6806 goto done;
6807 }
6808 if (check_len > 0) { /* Happens only when 'is_mask'. */
6809 if (!is_all_zeros(check_start, check_len) &&
6810 flow->dl_type != htons(0xffff)) {
6811 odp_parse_error(&rl, errorp, "unexpected L3 matching with "
6812 "masked Ethertype %#"PRIx16"/%#"PRIx16,
6813 ntohs(src_flow->dl_type),
6814 ntohs(flow->dl_type));
6815 return ODP_FIT_ERROR;
6816 } else {
6817 *expected_attrs |= UINT64_C(1) << expected_bit;
6818 }
6819 }
6820
6821 expected_bit = OVS_KEY_ATTR_UNSPEC;
6822 if (src_flow->nw_proto == IPPROTO_TCP
6823 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
6824 src_flow->dl_type == htons(ETH_TYPE_IPV6))
6825 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6826 if (!is_mask) {
6827 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
6828 }
6829 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
6830 const union ovs_key_tp *tcp_key;
6831
6832 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
6833 put_tp_key(tcp_key, flow);
6834 expected_bit = OVS_KEY_ATTR_TCP;
6835 }
6836 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
6837 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
6838 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
6839 }
6840 } else if (src_flow->nw_proto == IPPROTO_UDP
6841 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
6842 src_flow->dl_type == htons(ETH_TYPE_IPV6))
6843 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6844 if (!is_mask) {
6845 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
6846 }
6847 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
6848 const union ovs_key_tp *udp_key;
6849
6850 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
6851 put_tp_key(udp_key, flow);
6852 expected_bit = OVS_KEY_ATTR_UDP;
6853 }
6854 } else if (src_flow->nw_proto == IPPROTO_SCTP
6855 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
6856 src_flow->dl_type == htons(ETH_TYPE_IPV6))
6857 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6858 if (!is_mask) {
6859 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
6860 }
6861 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
6862 const union ovs_key_tp *sctp_key;
6863
6864 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
6865 put_tp_key(sctp_key, flow);
6866 expected_bit = OVS_KEY_ATTR_SCTP;
6867 }
6868 } else if (src_flow->nw_proto == IPPROTO_ICMP
6869 && src_flow->dl_type == htons(ETH_TYPE_IP)
6870 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6871 if (!is_mask) {
6872 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
6873 }
6874 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
6875 const struct ovs_key_icmp *icmp_key;
6876
6877 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
6878 flow->tp_src = htons(icmp_key->icmp_type);
6879 flow->tp_dst = htons(icmp_key->icmp_code);
6880 expected_bit = OVS_KEY_ATTR_ICMP;
6881 }
6882 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
6883 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
6884 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6885 if (!is_mask) {
6886 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
6887 }
6888 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
6889 const struct ovs_key_icmpv6 *icmpv6_key;
6890
6891 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
6892 flow->tp_src = htons(icmpv6_key->icmpv6_type);
6893 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
6894 expected_bit = OVS_KEY_ATTR_ICMPV6;
6895 if (is_nd(src_flow, NULL)) {
6896 if (!is_mask) {
6897 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
6898 }
6899 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
6900 const struct ovs_key_nd *nd_key;
6901
6902 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
6903 flow->nd_target = nd_key->nd_target;
6904 flow->arp_sha = nd_key->nd_sll;
6905 flow->arp_tha = nd_key->nd_tll;
6906 if (is_mask) {
6907 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6908 * ICMP type and code are 8 bits wide. Therefore, an
6909 * exact match looks like htons(0xff), not
6910 * htons(0xffff). See xlate_wc_finish() for details.
6911 * */
6912 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
6913 (flow->tp_src != htons(0xff) ||
6914 flow->tp_dst != htons(0xff))) {
6915 odp_parse_error(&rl, errorp,
6916 "ICMP (src,dst) masks should be "
6917 "(0xff,0xff) but are actually "
6918 "(%#"PRIx16",%#"PRIx16")",
6919 ntohs(flow->tp_src),
6920 ntohs(flow->tp_dst));
6921 return ODP_FIT_ERROR;
6922 } else {
6923 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
6924 }
6925 }
6926 }
6927 if (present_attrs &
6928 (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS)) {
6929 const struct ovs_key_nd_extensions *nd_ext_key;
6930 if (!is_mask) {
6931 *expected_attrs |=
6932 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
6933 }
6934
6935 nd_ext_key =
6936 nl_attr_get(attrs[OVS_KEY_ATTR_ND_EXTENSIONS]);
6937 flow->igmp_group_ip4 = nd_ext_key->nd_reserved;
6938 flow->tcp_flags = htons(nd_ext_key->nd_options_type);
6939
6940 if (is_mask) {
6941 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6942 * ICMP type and code are 8 bits wide. Therefore, an
6943 * exact match looks like htons(0xff), not
6944 * htons(0xffff). See xlate_wc_finish() for details.
6945 * */
6946 if (!is_all_zeros(nd_ext_key, sizeof *nd_ext_key) &&
6947 (flow->tp_src != htons(0xff) ||
6948 flow->tp_dst != htons(0xff))) {
6949 return ODP_FIT_ERROR;
6950 } else {
6951 *expected_attrs |=
6952 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
6953 }
6954 }
6955 }
6956 }
6957 }
6958 } else if (src_flow->nw_proto == IPPROTO_IGMP
6959 && src_flow->dl_type == htons(ETH_TYPE_IP)) {
6960 /* OVS userspace parses the IGMP type, code, and group, but its
6961 * datapaths do not, so there is always missing information. */
6962 return ODP_FIT_TOO_LITTLE;
6963 }
6964 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
6965 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
6966 odp_parse_error(&rl, errorp, "flow matches on L4 ports but does "
6967 "not define an L4 protocol");
6968 return ODP_FIT_ERROR;
6969 } else {
6970 *expected_attrs |= UINT64_C(1) << expected_bit;
6971 }
6972 }
6973
6974 done:
6975 return need_check ? check_expectations(present_attrs, out_of_range_attr,
6976 *expected_attrs, key, key_len) : ODP_FIT_PERFECT;
6977 }
6978
6979 /* Parse 802.1Q header then encapsulated L3 attributes. */
6980 static enum odp_key_fitness
6981 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
6982 uint64_t present_attrs, int out_of_range_attr,
6983 uint64_t expected_attrs, struct flow *flow,
6984 const struct nlattr *key, size_t key_len,
6985 const struct flow *src_flow, char **errorp)
6986 {
6987 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6988 bool is_mask = src_flow != flow;
6989
6990 const struct nlattr *encap;
6991 enum odp_key_fitness encap_fitness;
6992 enum odp_key_fitness fitness = ODP_FIT_ERROR;
6993 int encaps = 0;
6994
6995 while (encaps < flow_vlan_limit &&
6996 (is_mask
6997 ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0
6998 : eth_type_vlan(flow->dl_type))) {
6999
7000 encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
7001 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
7002
7003 /* Calculate fitness of outer attributes. */
7004 if (!is_mask) {
7005 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
7006 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
7007 } else {
7008 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
7009 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
7010 }
7011 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
7012 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
7013 }
7014 }
7015 fitness = check_expectations(present_attrs, out_of_range_attr,
7016 expected_attrs, key, key_len);
7017
7018 /* Set vlan_tci.
7019 * Remove the TPID from dl_type since it's not the real Ethertype. */
7020 flow->vlans[encaps].tpid = flow->dl_type;
7021 flow->dl_type = htons(0);
7022 flow->vlans[encaps].tci =
7023 (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
7024 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
7025 : htons(0));
7026 if (!is_mask) {
7027 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) ||
7028 !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7029 return ODP_FIT_TOO_LITTLE;
7030 } else if (flow->vlans[encaps].tci == htons(0)) {
7031 /* Corner case for a truncated 802.1Q header. */
7032 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
7033 return ODP_FIT_TOO_MUCH;
7034 }
7035 return fitness;
7036 } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) {
7037 odp_parse_error(
7038 &rl, errorp, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
7039 "but CFI bit is not set", ntohs(flow->vlans[encaps].tci));
7040 return ODP_FIT_ERROR;
7041 }
7042 } else {
7043 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7044 return fitness;
7045 }
7046 }
7047
7048 /* Now parse the encapsulated attributes. */
7049 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
7050 attrs, &present_attrs, &out_of_range_attr,
7051 errorp)) {
7052 return ODP_FIT_ERROR;
7053 }
7054 expected_attrs = 0;
7055
7056 if (!parse_ethertype(attrs, present_attrs, &expected_attrs,
7057 flow, src_flow, errorp)) {
7058 return ODP_FIT_ERROR;
7059 }
7060 encap_fitness = parse_l2_5_onward(attrs, present_attrs,
7061 out_of_range_attr,
7062 &expected_attrs,
7063 flow, key, key_len,
7064 src_flow, false, errorp);
7065 if (encap_fitness != ODP_FIT_PERFECT) {
7066 return encap_fitness;
7067 }
7068 encaps++;
7069 }
7070
7071 return check_expectations(present_attrs, out_of_range_attr,
7072 expected_attrs, key, key_len);
7073 }
7074
7075 static enum odp_key_fitness
7076 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
7077 struct flow *flow, const struct flow *src_flow,
7078 char **errorp)
7079 {
7080 /* New "struct flow" fields that are visible to the datapath (including all
7081 * data fields) should be translated from equivalent datapath flow fields
7082 * here (you will have to add a OVS_KEY_ATTR_* for them). */
7083 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
7084
7085 enum odp_key_fitness fitness = ODP_FIT_ERROR;
7086 if (errorp) {
7087 *errorp = NULL;
7088 }
7089
7090 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
7091 uint64_t expected_attrs;
7092 uint64_t present_attrs;
7093 int out_of_range_attr;
7094 bool is_mask = src_flow != flow;
7095
7096 memset(flow, 0, sizeof *flow);
7097
7098 /* Parse attributes. */
7099 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
7100 &out_of_range_attr, errorp)) {
7101 goto exit;
7102 }
7103 expected_attrs = 0;
7104
7105 /* Metadata. */
7106 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
7107 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
7108 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
7109 } else if (is_mask) {
7110 /* Always exact match recirc_id if it is not specified. */
7111 flow->recirc_id = UINT32_MAX;
7112 }
7113
7114 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
7115 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
7116 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
7117 }
7118 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
7119 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
7120 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
7121 }
7122
7123 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
7124 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
7125 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
7126 }
7127
7128 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
7129 uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
7130
7131 flow->ct_state = odp_to_ovs_ct_state(odp_state);
7132 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
7133 }
7134 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
7135 flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
7136 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
7137 }
7138 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
7139 flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
7140 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
7141 }
7142 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
7143 flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]);
7144 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
7145 }
7146 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
7147 const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
7148 flow->ct_nw_src = ct->ipv4_src;
7149 flow->ct_nw_dst = ct->ipv4_dst;
7150 flow->ct_nw_proto = ct->ipv4_proto;
7151 flow->ct_tp_src = ct->src_port;
7152 flow->ct_tp_dst = ct->dst_port;
7153 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
7154 }
7155 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
7156 const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
7157
7158 flow->ct_ipv6_src = ct->ipv6_src;
7159 flow->ct_ipv6_dst = ct->ipv6_dst;
7160 flow->ct_nw_proto = ct->ipv6_proto;
7161 flow->ct_tp_src = ct->src_port;
7162 flow->ct_tp_dst = ct->dst_port;
7163 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
7164 }
7165
7166 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
7167 enum odp_key_fitness res;
7168
7169 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
7170 &flow->tunnel, errorp);
7171 if (res == ODP_FIT_ERROR) {
7172 goto exit;
7173 } else if (res == ODP_FIT_PERFECT) {
7174 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
7175 }
7176 }
7177
7178 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
7179 flow->in_port.odp_port
7180 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
7181 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
7182 } else if (!is_mask) {
7183 flow->in_port.odp_port = ODPP_NONE;
7184 }
7185
7186 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) {
7187 flow->packet_type
7188 = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]);
7189 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE;
7190 if (pt_ns(src_flow->packet_type) == OFPHTN_ETHERTYPE) {
7191 flow->dl_type = pt_ns_type_be(flow->packet_type);
7192 }
7193 } else if (!is_mask) {
7194 flow->packet_type = htonl(PT_ETH);
7195 }
7196
7197 /* Check for Ethernet header. */
7198 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
7199 const struct ovs_key_ethernet *eth_key;
7200
7201 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
7202 put_ethernet_key(eth_key, flow);
7203 if (!is_mask) {
7204 flow->packet_type = htonl(PT_ETH);
7205 }
7206 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
7207 }
7208 else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7209 ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7210 if (!is_mask) {
7211 flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
7212 ntohs(ethertype));
7213 }
7214 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7215 }
7216
7217 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7218 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
7219 src_flow, errorp)) {
7220 goto exit;
7221 }
7222
7223 if (is_mask
7224 ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0
7225 : eth_type_vlan(src_flow->dl_type)) {
7226 fitness = parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
7227 expected_attrs, flow, key, key_len,
7228 src_flow, errorp);
7229 } else {
7230 if (is_mask) {
7231 /* A missing VLAN mask means exact match on vlan_tci 0 (== no
7232 * VLAN). */
7233 flow->vlans[0].tpid = htons(0xffff);
7234 flow->vlans[0].tci = htons(0xffff);
7235 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
7236 flow->vlans[0].tci = nl_attr_get_be16(
7237 attrs[OVS_KEY_ATTR_VLAN]);
7238 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
7239 }
7240 }
7241 fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
7242 &expected_attrs, flow, key, key_len,
7243 src_flow, true, errorp);
7244 }
7245
7246 exit:;
7247 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7248 if (fitness == ODP_FIT_ERROR && (errorp || !VLOG_DROP_WARN(&rl))) {
7249 struct ds s = DS_EMPTY_INITIALIZER;
7250 if (is_mask) {
7251 ds_put_cstr(&s, "the flow mask in error is: ");
7252 odp_flow_key_format(key, key_len, &s);
7253 ds_put_cstr(&s, ", for the following flow key: ");
7254 flow_format(&s, src_flow, NULL);
7255 } else {
7256 ds_put_cstr(&s, "the flow key in error is: ");
7257 odp_flow_key_format(key, key_len, &s);
7258 }
7259 if (errorp) {
7260 char *old_error = *errorp;
7261 *errorp = xasprintf("%s; %s", old_error, ds_cstr(&s));
7262 free(old_error);
7263 } else {
7264 VLOG_WARN("%s", ds_cstr(&s));
7265 }
7266 ds_destroy(&s);
7267 }
7268 return fitness;
7269 }
7270
7271 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
7272 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
7273 * 'key' fits our expectations for what a flow key should contain.
7274 *
7275 * The 'in_port' will be the datapath's understanding of the port. The
7276 * caller will need to translate with odp_port_to_ofp_port() if the
7277 * OpenFlow port is needed.
7278 *
7279 * This function doesn't take the packet itself as an argument because none of
7280 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
7281 * it is always possible to infer which additional attribute(s) should appear
7282 * by looking at the attributes for lower-level protocols, e.g. if the network
7283 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
7284 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
7285 * must be absent.
7286 *
7287 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7288 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7289 * '*errorp', otherwise NULL. */
7290 enum odp_key_fitness
7291 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
7292 struct flow *flow, char **errorp)
7293 {
7294 return odp_flow_key_to_flow__(key, key_len, flow, flow, errorp);
7295 }
7296
7297 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
7298 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
7299 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
7300 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
7301 * well 'key' fits our expectations for what a flow key should contain.
7302 *
7303 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7304 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7305 * '*errorp', otherwise NULL. */
7306 enum odp_key_fitness
7307 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
7308 struct flow_wildcards *mask, const struct flow *src_flow,
7309 char **errorp)
7310 {
7311 if (mask_key_len) {
7312 return odp_flow_key_to_flow__(mask_key, mask_key_len,
7313 &mask->masks, src_flow, errorp);
7314 } else {
7315 if (errorp) {
7316 *errorp = NULL;
7317 }
7318
7319 /* A missing mask means that the flow should be exact matched.
7320 * Generate an appropriate exact wildcard for the flow. */
7321 flow_wildcards_init_for_packet(mask, src_flow);
7322
7323 return ODP_FIT_PERFECT;
7324 }
7325 }
7326
7327 /* Converts the netlink formated key/mask to match.
7328 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
7329 * disagree on the acceptable form of flow */
7330 int
7331 parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len,
7332 const struct nlattr *mask, size_t mask_len,
7333 struct match *match)
7334 {
7335 enum odp_key_fitness fitness;
7336
7337 fitness = odp_flow_key_to_flow(key, key_len, &match->flow, NULL);
7338 if (fitness) {
7339 /* This should not happen: it indicates that
7340 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
7341 * the acceptable form of a flow. Log the problem as an error,
7342 * with enough details to enable debugging. */
7343 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7344
7345 if (!VLOG_DROP_ERR(&rl)) {
7346 struct ds s;
7347
7348 ds_init(&s);
7349 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
7350 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
7351 ds_destroy(&s);
7352 }
7353
7354 return EINVAL;
7355 }
7356
7357 fitness = odp_flow_key_to_mask(mask, mask_len, &match->wc, &match->flow,
7358 NULL);
7359 if (fitness) {
7360 /* This should not happen: it indicates that
7361 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
7362 * disagree on the acceptable form of a mask. Log the problem
7363 * as an error, with enough details to enable debugging. */
7364 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7365
7366 if (!VLOG_DROP_ERR(&rl)) {
7367 struct ds s;
7368
7369 ds_init(&s);
7370 odp_flow_format(key, key_len, mask, mask_len, NULL, &s,
7371 true);
7372 VLOG_ERR("internal error parsing flow mask %s (%s)",
7373 ds_cstr(&s), odp_key_fitness_to_string(fitness));
7374 ds_destroy(&s);
7375 }
7376
7377 return EINVAL;
7378 }
7379
7380 return 0;
7381 }
7382
7383 /* Returns 'fitness' as a string, for use in debug messages. */
7384 const char *
7385 odp_key_fitness_to_string(enum odp_key_fitness fitness)
7386 {
7387 switch (fitness) {
7388 case ODP_FIT_PERFECT:
7389 return "OK";
7390 case ODP_FIT_TOO_MUCH:
7391 return "too_much";
7392 case ODP_FIT_TOO_LITTLE:
7393 return "too_little";
7394 case ODP_FIT_ERROR:
7395 return "error";
7396 default:
7397 return "<unknown>";
7398 }
7399 }
7400
7401 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
7402 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
7403 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
7404 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
7405 * null, then the return value is not meaningful.) */
7406 size_t
7407 odp_put_userspace_action(uint32_t pid,
7408 const void *userdata, size_t userdata_size,
7409 odp_port_t tunnel_out_port,
7410 bool include_actions,
7411 struct ofpbuf *odp_actions)
7412 {
7413 size_t userdata_ofs;
7414 size_t offset;
7415
7416 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
7417 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
7418 if (userdata) {
7419 userdata_ofs = odp_actions->size + NLA_HDRLEN;
7420
7421 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
7422 * module before Linux 3.10 required the userdata to be exactly 8 bytes
7423 * long:
7424 *
7425 * - The kernel rejected shorter userdata with -ERANGE.
7426 *
7427 * - The kernel silently dropped userdata beyond the first 8 bytes.
7428 *
7429 * Thus, for maximum compatibility, always put at least 8 bytes. (We
7430 * separately disable features that required more than 8 bytes.) */
7431 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
7432 MAX(8, userdata_size)),
7433 userdata, userdata_size);
7434 } else {
7435 userdata_ofs = 0;
7436 }
7437 if (tunnel_out_port != ODPP_NONE) {
7438 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
7439 tunnel_out_port);
7440 }
7441 if (include_actions) {
7442 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
7443 }
7444 nl_msg_end_nested(odp_actions, offset);
7445
7446 return userdata_ofs;
7447 }
7448
7449 void
7450 odp_put_pop_eth_action(struct ofpbuf *odp_actions)
7451 {
7452 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH);
7453 }
7454
7455 void
7456 odp_put_push_eth_action(struct ofpbuf *odp_actions,
7457 const struct eth_addr *eth_src,
7458 const struct eth_addr *eth_dst)
7459 {
7460 struct ovs_action_push_eth eth;
7461
7462 memset(&eth, 0, sizeof eth);
7463 if (eth_src) {
7464 eth.addresses.eth_src = *eth_src;
7465 }
7466 if (eth_dst) {
7467 eth.addresses.eth_dst = *eth_dst;
7468 }
7469
7470 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH,
7471 &eth, sizeof eth);
7472 }
7473
7474 void
7475 odp_put_tunnel_action(const struct flow_tnl *tunnel,
7476 struct ofpbuf *odp_actions, const char *tnl_type)
7477 {
7478 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
7479 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL, tnl_type);
7480 nl_msg_end_nested(odp_actions, offset);
7481 }
7482
7483 void
7484 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
7485 struct ovs_action_push_tnl *data)
7486 {
7487 int size = offsetof(struct ovs_action_push_tnl, header);
7488
7489 size += data->header_len;
7490 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
7491 }
7492
7493 \f
7494 /* The commit_odp_actions() function and its helpers. */
7495
7496 static void
7497 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
7498 const void *key, size_t key_size)
7499 {
7500 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
7501 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
7502 nl_msg_end_nested(odp_actions, offset);
7503 }
7504
7505 /* Masked set actions have a mask following the data within the netlink
7506 * attribute. The unmasked bits in the data will be cleared as the data
7507 * is copied to the action. */
7508 void
7509 commit_masked_set_action(struct ofpbuf *odp_actions,
7510 enum ovs_key_attr key_type,
7511 const void *key_, const void *mask_, size_t key_size)
7512 {
7513 size_t offset = nl_msg_start_nested(odp_actions,
7514 OVS_ACTION_ATTR_SET_MASKED);
7515 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
7516 const char *key = key_, *mask = mask_;
7517
7518 memcpy(data + key_size, mask, key_size);
7519 /* Clear unmasked bits while copying. */
7520 while (key_size--) {
7521 *data++ = *key++ & *mask++;
7522 }
7523 nl_msg_end_nested(odp_actions, offset);
7524 }
7525
7526 /* If any of the flow key data that ODP actions can modify are different in
7527 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
7528 * 'odp_actions' that change the flow tunneling information in key from
7529 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
7530 * same way. In other words, operates the same as commit_odp_actions(), but
7531 * only on tunneling information. */
7532 void
7533 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
7534 struct ofpbuf *odp_actions, const char *tnl_type)
7535 {
7536 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7537 * must have non-zero ipv6_dst. */
7538 if (flow_tnl_dst_is_set(&flow->tunnel)) {
7539 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
7540 return;
7541 }
7542 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
7543 odp_put_tunnel_action(&base->tunnel, odp_actions, tnl_type);
7544 }
7545 }
7546
7547 struct offsetof_sizeof {
7548 int offset;
7549 int size;
7550 };
7551
7552 /* Compares each of the fields in 'key0' and 'key1'. The fields are specified
7553 * in 'offsetof_sizeof_arr', which is an array terminated by a 0-size field.
7554 * Returns true if all of the fields are equal, false if at least one differs.
7555 * As a side effect, for each field that is the same in 'key0' and 'key1',
7556 * zeros the corresponding bytes in 'mask'. */
7557 static bool
7558 keycmp_mask(const void *key0, const void *key1,
7559 struct offsetof_sizeof *offsetof_sizeof_arr, void *mask)
7560 {
7561 bool differ = false;
7562
7563 for (int field = 0 ; ; field++) {
7564 int size = offsetof_sizeof_arr[field].size;
7565 int offset = offsetof_sizeof_arr[field].offset;
7566 if (size == 0) {
7567 break;
7568 }
7569
7570 char *pkey0 = ((char *)key0) + offset;
7571 char *pkey1 = ((char *)key1) + offset;
7572 char *pmask = ((char *)mask) + offset;
7573 if (memcmp(pkey0, pkey1, size) == 0) {
7574 memset(pmask, 0, size);
7575 } else {
7576 differ = true;
7577 }
7578 }
7579
7580 return differ;
7581 }
7582
7583 static bool
7584 commit(enum ovs_key_attr attr, bool use_masked_set,
7585 const void *key, void *base, void *mask, size_t size,
7586 struct offsetof_sizeof *offsetof_sizeof_arr,
7587 struct ofpbuf *odp_actions)
7588 {
7589 if (keycmp_mask(key, base, offsetof_sizeof_arr, mask)) {
7590 bool fully_masked = odp_mask_is_exact(attr, mask, size);
7591
7592 if (use_masked_set && !fully_masked) {
7593 commit_masked_set_action(odp_actions, attr, key, mask, size);
7594 } else {
7595 if (!fully_masked) {
7596 memset(mask, 0xff, size);
7597 }
7598 commit_set_action(odp_actions, attr, key, size);
7599 }
7600 memcpy(base, key, size);
7601 return true;
7602 } else {
7603 /* Mask bits are set when we have either read or set the corresponding
7604 * values. Masked bits will be exact-matched, no need to set them
7605 * if the value did not actually change. */
7606 return false;
7607 }
7608 }
7609
7610 static void
7611 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
7612 {
7613 eth->eth_src = flow->dl_src;
7614 eth->eth_dst = flow->dl_dst;
7615 }
7616
7617 static void
7618 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
7619 {
7620 flow->dl_src = eth->eth_src;
7621 flow->dl_dst = eth->eth_dst;
7622 }
7623
7624 static void
7625 commit_set_ether_action(const struct flow *flow, struct flow *base_flow,
7626 struct ofpbuf *odp_actions,
7627 struct flow_wildcards *wc,
7628 bool use_masked)
7629 {
7630 struct ovs_key_ethernet key, base, mask;
7631 struct offsetof_sizeof ovs_key_ethernet_offsetof_sizeof_arr[] =
7632 OVS_KEY_ETHERNET_OFFSETOF_SIZEOF_ARR;
7633 if (flow->packet_type != htonl(PT_ETH)) {
7634 return;
7635 }
7636
7637 get_ethernet_key(flow, &key);
7638 get_ethernet_key(base_flow, &base);
7639 get_ethernet_key(&wc->masks, &mask);
7640
7641 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
7642 &key, &base, &mask, sizeof key,
7643 ovs_key_ethernet_offsetof_sizeof_arr, odp_actions)) {
7644 put_ethernet_key(&base, base_flow);
7645 put_ethernet_key(&mask, &wc->masks);
7646 }
7647 }
7648
7649 static void
7650 commit_vlan_action(const struct flow* flow, struct flow *base,
7651 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
7652 {
7653 int base_n = flow_count_vlan_headers(base);
7654 int flow_n = flow_count_vlan_headers(flow);
7655 flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n);
7656
7657 /* Pop all mismatching vlan of base, push those of flow */
7658 for (; base_n >= 0; base_n--) {
7659 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
7660 wc->masks.vlans[base_n].qtag = OVS_BE32_MAX;
7661 }
7662
7663 for (; flow_n >= 0; flow_n--) {
7664 struct ovs_action_push_vlan vlan;
7665
7666 vlan.vlan_tpid = flow->vlans[flow_n].tpid;
7667 vlan.vlan_tci = flow->vlans[flow_n].tci;
7668 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
7669 &vlan, sizeof vlan);
7670 }
7671 memcpy(base->vlans, flow->vlans, sizeof(base->vlans));
7672 }
7673
7674 /* Wildcarding already done at action translation time. */
7675 static void
7676 commit_mpls_action(const struct flow *flow, struct flow *base,
7677 struct ofpbuf *odp_actions)
7678 {
7679 int base_n = flow_count_mpls_labels(base, NULL);
7680 int flow_n = flow_count_mpls_labels(flow, NULL);
7681 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
7682 NULL);
7683
7684 while (base_n > common_n) {
7685 if (base_n - 1 == common_n && flow_n > common_n) {
7686 /* If there is only one more LSE in base than there are common
7687 * between base and flow; and flow has at least one more LSE than
7688 * is common then the topmost LSE of base may be updated using
7689 * set */
7690 struct ovs_key_mpls mpls_key;
7691
7692 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
7693 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
7694 &mpls_key, sizeof mpls_key);
7695 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
7696 common_n++;
7697 } else {
7698 /* Otherwise, if there more LSEs in base than are common between
7699 * base and flow then pop the topmost one. */
7700 ovs_be16 dl_type;
7701 /* If all the LSEs are to be popped and this is not the outermost
7702 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7703 * POP_MPLS action instead of flow->dl_type.
7704 *
7705 * This is because the POP_MPLS action requires its ethertype
7706 * argument to be an MPLS ethernet type but in this case
7707 * flow->dl_type will be a non-MPLS ethernet type.
7708 *
7709 * When the final POP_MPLS action occurs it use flow->dl_type and
7710 * the and the resulting packet will have the desired dl_type. */
7711 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
7712 dl_type = htons(ETH_TYPE_MPLS);
7713 } else {
7714 dl_type = flow->dl_type;
7715 }
7716 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
7717 ovs_assert(flow_pop_mpls(base, base_n, flow->dl_type, NULL));
7718 base_n--;
7719 }
7720 }
7721
7722 /* If, after the above popping and setting, there are more LSEs in flow
7723 * than base then some LSEs need to be pushed. */
7724 while (base_n < flow_n) {
7725 struct ovs_action_push_mpls *mpls;
7726
7727 mpls = nl_msg_put_unspec_zero(odp_actions,
7728 OVS_ACTION_ATTR_PUSH_MPLS,
7729 sizeof *mpls);
7730 mpls->mpls_ethertype = flow->dl_type;
7731 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
7732 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7733 * headers if the flow is restored later due to returning from a patch
7734 * port or group bucket. */
7735 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL, false);
7736 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
7737 base_n++;
7738 }
7739 }
7740
7741 static void
7742 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
7743 {
7744 ipv4->ipv4_src = flow->nw_src;
7745 ipv4->ipv4_dst = flow->nw_dst;
7746 ipv4->ipv4_proto = flow->nw_proto;
7747 ipv4->ipv4_tos = flow->nw_tos;
7748 ipv4->ipv4_ttl = flow->nw_ttl;
7749 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
7750 }
7751
7752 static void
7753 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
7754 {
7755 flow->nw_src = ipv4->ipv4_src;
7756 flow->nw_dst = ipv4->ipv4_dst;
7757 flow->nw_proto = ipv4->ipv4_proto;
7758 flow->nw_tos = ipv4->ipv4_tos;
7759 flow->nw_ttl = ipv4->ipv4_ttl;
7760 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
7761 }
7762
7763 static void
7764 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
7765 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7766 bool use_masked)
7767 {
7768 struct ovs_key_ipv4 key, mask, base;
7769 struct offsetof_sizeof ovs_key_ipv4_offsetof_sizeof_arr[] =
7770 OVS_KEY_IPV4_OFFSETOF_SIZEOF_ARR;
7771
7772 /* Check that nw_proto and nw_frag remain unchanged. */
7773 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
7774 flow->nw_frag == base_flow->nw_frag);
7775
7776 get_ipv4_key(flow, &key, false);
7777 get_ipv4_key(base_flow, &base, false);
7778 get_ipv4_key(&wc->masks, &mask, true);
7779 mask.ipv4_proto = 0; /* Not writeable. */
7780 mask.ipv4_frag = 0; /* Not writable. */
7781
7782 if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
7783 ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
7784 mask.ipv4_tos &= ~IP_ECN_MASK;
7785 }
7786
7787 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
7788 ovs_key_ipv4_offsetof_sizeof_arr, odp_actions)) {
7789 put_ipv4_key(&base, base_flow, false);
7790 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
7791 put_ipv4_key(&mask, &wc->masks, true);
7792 }
7793 }
7794 }
7795
7796 static void
7797 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
7798 {
7799 ipv6->ipv6_src = flow->ipv6_src;
7800 ipv6->ipv6_dst = flow->ipv6_dst;
7801 ipv6->ipv6_label = flow->ipv6_label;
7802 ipv6->ipv6_proto = flow->nw_proto;
7803 ipv6->ipv6_tclass = flow->nw_tos;
7804 ipv6->ipv6_hlimit = flow->nw_ttl;
7805 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
7806 }
7807
7808 static void
7809 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
7810 {
7811 flow->ipv6_src = ipv6->ipv6_src;
7812 flow->ipv6_dst = ipv6->ipv6_dst;
7813 flow->ipv6_label = ipv6->ipv6_label;
7814 flow->nw_proto = ipv6->ipv6_proto;
7815 flow->nw_tos = ipv6->ipv6_tclass;
7816 flow->nw_ttl = ipv6->ipv6_hlimit;
7817 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
7818 }
7819
7820 static void
7821 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
7822 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7823 bool use_masked)
7824 {
7825 struct ovs_key_ipv6 key, mask, base;
7826 struct offsetof_sizeof ovs_key_ipv6_offsetof_sizeof_arr[] =
7827 OVS_KEY_IPV6_OFFSETOF_SIZEOF_ARR;
7828
7829 /* Check that nw_proto and nw_frag remain unchanged. */
7830 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
7831 flow->nw_frag == base_flow->nw_frag);
7832
7833 get_ipv6_key(flow, &key, false);
7834 get_ipv6_key(base_flow, &base, false);
7835 get_ipv6_key(&wc->masks, &mask, true);
7836 mask.ipv6_proto = 0; /* Not writeable. */
7837 mask.ipv6_frag = 0; /* Not writable. */
7838 mask.ipv6_label &= htonl(IPV6_LABEL_MASK); /* Not writable. */
7839
7840 if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
7841 ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
7842 mask.ipv6_tclass &= ~IP_ECN_MASK;
7843 }
7844
7845 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
7846 ovs_key_ipv6_offsetof_sizeof_arr, odp_actions)) {
7847 put_ipv6_key(&base, base_flow, false);
7848 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
7849 put_ipv6_key(&mask, &wc->masks, true);
7850 }
7851 }
7852 }
7853
7854 static void
7855 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
7856 {
7857 /* ARP key has padding, clear it. */
7858 memset(arp, 0, sizeof *arp);
7859
7860 arp->arp_sip = flow->nw_src;
7861 arp->arp_tip = flow->nw_dst;
7862 arp->arp_op = htons(flow->nw_proto);
7863 arp->arp_sha = flow->arp_sha;
7864 arp->arp_tha = flow->arp_tha;
7865 }
7866
7867 static void
7868 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
7869 {
7870 flow->nw_src = arp->arp_sip;
7871 flow->nw_dst = arp->arp_tip;
7872 flow->nw_proto = ntohs(arp->arp_op);
7873 flow->arp_sha = arp->arp_sha;
7874 flow->arp_tha = arp->arp_tha;
7875 }
7876
7877 static enum slow_path_reason
7878 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
7879 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
7880 {
7881 struct ovs_key_arp key, mask, base;
7882 struct offsetof_sizeof ovs_key_arp_offsetof_sizeof_arr[] =
7883 OVS_KEY_ARP_OFFSETOF_SIZEOF_ARR;
7884
7885 get_arp_key(flow, &key);
7886 get_arp_key(base_flow, &base);
7887 get_arp_key(&wc->masks, &mask);
7888
7889 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
7890 ovs_key_arp_offsetof_sizeof_arr, odp_actions)) {
7891 put_arp_key(&base, base_flow);
7892 put_arp_key(&mask, &wc->masks);
7893 return SLOW_ACTION;
7894 }
7895 return 0;
7896 }
7897
7898 static void
7899 get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
7900 {
7901 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7902 icmp->icmp_type = ntohs(flow->tp_src);
7903 icmp->icmp_code = ntohs(flow->tp_dst);
7904 }
7905
7906 static void
7907 put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
7908 {
7909 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7910 flow->tp_src = htons(icmp->icmp_type);
7911 flow->tp_dst = htons(icmp->icmp_code);
7912 }
7913
7914 static enum slow_path_reason
7915 commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
7916 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
7917 {
7918 struct ovs_key_icmp key, mask, base;
7919 struct offsetof_sizeof ovs_key_icmp_offsetof_sizeof_arr[] =
7920 OVS_KEY_ICMP_OFFSETOF_SIZEOF_ARR;
7921 enum ovs_key_attr attr;
7922
7923 if (is_icmpv4(flow, NULL)) {
7924 attr = OVS_KEY_ATTR_ICMP;
7925 } else if (is_icmpv6(flow, NULL)) {
7926 attr = OVS_KEY_ATTR_ICMPV6;
7927 } else {
7928 return 0;
7929 }
7930
7931 get_icmp_key(flow, &key);
7932 get_icmp_key(base_flow, &base);
7933 get_icmp_key(&wc->masks, &mask);
7934
7935 if (commit(attr, false, &key, &base, &mask, sizeof key,
7936 ovs_key_icmp_offsetof_sizeof_arr, odp_actions)) {
7937 put_icmp_key(&base, base_flow);
7938 put_icmp_key(&mask, &wc->masks);
7939 return SLOW_ACTION;
7940 }
7941 return 0;
7942 }
7943
7944 static void
7945 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
7946 {
7947 nd->nd_target = flow->nd_target;
7948 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7949 nd->nd_sll = flow->arp_sha;
7950 nd->nd_tll = flow->arp_tha;
7951 }
7952
7953 static void
7954 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
7955 {
7956 flow->nd_target = nd->nd_target;
7957 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7958 flow->arp_sha = nd->nd_sll;
7959 flow->arp_tha = nd->nd_tll;
7960 }
7961
7962 static void
7963 get_nd_extensions_key(const struct flow *flow,
7964 struct ovs_key_nd_extensions *nd_ext)
7965 {
7966 /* ND Extensions key has padding, clear it. */
7967 memset(nd_ext, 0, sizeof *nd_ext);
7968 nd_ext->nd_reserved = flow->igmp_group_ip4;
7969 nd_ext->nd_options_type = ntohs(flow->tcp_flags);
7970 }
7971
7972 static void
7973 put_nd_extensions_key(const struct ovs_key_nd_extensions *nd_ext,
7974 struct flow *flow)
7975 {
7976 flow->igmp_group_ip4 = nd_ext->nd_reserved;
7977 flow->tcp_flags = htons(nd_ext->nd_options_type);
7978 }
7979
7980 static enum slow_path_reason
7981 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
7982 struct ofpbuf *odp_actions,
7983 struct flow_wildcards *wc, bool use_masked)
7984 {
7985 struct ovs_key_nd key, mask, base;
7986 struct offsetof_sizeof ovs_key_nd_offsetof_sizeof_arr[] =
7987 OVS_KEY_ND_OFFSETOF_SIZEOF_ARR;
7988
7989 get_nd_key(flow, &key);
7990 get_nd_key(base_flow, &base);
7991 get_nd_key(&wc->masks, &mask);
7992
7993 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
7994 ovs_key_nd_offsetof_sizeof_arr, odp_actions)) {
7995 put_nd_key(&base, base_flow);
7996 put_nd_key(&mask, &wc->masks);
7997 return SLOW_ACTION;
7998 }
7999
8000 return 0;
8001 }
8002
8003 static enum slow_path_reason
8004 commit_set_nd_extensions_action(const struct flow *flow,
8005 struct flow *base_flow,
8006 struct ofpbuf *odp_actions,
8007 struct flow_wildcards *wc, bool use_masked)
8008 {
8009 struct ovs_key_nd_extensions key, mask, base;
8010 struct offsetof_sizeof ovs_key_nd_extensions_offsetof_sizeof_arr[] =
8011 OVS_KEY_ND_EXTENSIONS_OFFSETOF_SIZEOF_ARR;
8012
8013 get_nd_extensions_key(flow, &key);
8014 get_nd_extensions_key(base_flow, &base);
8015 get_nd_extensions_key(&wc->masks, &mask);
8016
8017 if (commit(OVS_KEY_ATTR_ND_EXTENSIONS, use_masked, &key, &base, &mask,
8018 sizeof key, ovs_key_nd_extensions_offsetof_sizeof_arr,
8019 odp_actions)) {
8020 put_nd_extensions_key(&base, base_flow);
8021 put_nd_extensions_key(&mask, &wc->masks);
8022 return SLOW_ACTION;
8023 }
8024 return 0;
8025 }
8026
8027 static enum slow_path_reason
8028 commit_set_nw_action(const struct flow *flow, struct flow *base,
8029 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8030 bool use_masked)
8031 {
8032 uint32_t reason;
8033
8034 /* Check if 'flow' really has an L3 header. */
8035 if (!flow->nw_proto) {
8036 return 0;
8037 }
8038
8039 switch (ntohs(base->dl_type)) {
8040 case ETH_TYPE_IP:
8041 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
8042 break;
8043
8044 case ETH_TYPE_IPV6:
8045 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
8046 if (base->nw_proto == IPPROTO_ICMPV6) {
8047 /* Commit extended attrs first to make sure
8048 correct options are added.*/
8049 reason = commit_set_nd_extensions_action(flow, base,
8050 odp_actions, wc, use_masked);
8051 reason |= commit_set_nd_action(flow, base, odp_actions,
8052 wc, use_masked);
8053 return reason;
8054 }
8055 break;
8056
8057 case ETH_TYPE_ARP:
8058 return commit_set_arp_action(flow, base, odp_actions, wc);
8059 }
8060
8061 return 0;
8062 }
8063
8064 static inline void
8065 get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, bool is_mask)
8066 {
8067 *nsh = flow->nsh;
8068 if (!is_mask) {
8069 if (nsh->mdtype != NSH_M_TYPE1) {
8070 memset(nsh->context, 0, sizeof(nsh->context));
8071 }
8072 }
8073 }
8074
8075 static inline void
8076 put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow,
8077 bool is_mask OVS_UNUSED)
8078 {
8079 flow->nsh = *nsh;
8080 if (flow->nsh.mdtype != NSH_M_TYPE1) {
8081 memset(flow->nsh.context, 0, sizeof(flow->nsh.context));
8082 }
8083 }
8084
8085 static bool
8086 commit_nsh(const struct ovs_key_nsh * flow_nsh, bool use_masked_set,
8087 const struct ovs_key_nsh *key, struct ovs_key_nsh *base,
8088 struct ovs_key_nsh *mask, size_t size,
8089 struct ofpbuf *odp_actions)
8090 {
8091 enum ovs_key_attr attr = OVS_KEY_ATTR_NSH;
8092
8093 if (memcmp(key, base, size) == 0) {
8094 /* Mask bits are set when we have either read or set the corresponding
8095 * values. Masked bits will be exact-matched, no need to set them
8096 * if the value did not actually change. */
8097 return false;
8098 }
8099
8100 bool fully_masked = odp_mask_is_exact(attr, mask, size);
8101
8102 if (use_masked_set && !fully_masked) {
8103 size_t nsh_key_ofs;
8104 struct ovs_nsh_key_base nsh_base;
8105 struct ovs_nsh_key_base nsh_base_mask;
8106 struct ovs_nsh_key_md1 md1;
8107 struct ovs_nsh_key_md1 md1_mask;
8108 size_t offset = nl_msg_start_nested(odp_actions,
8109 OVS_ACTION_ATTR_SET_MASKED);
8110
8111 nsh_base.flags = key->flags;
8112 nsh_base.ttl = key->ttl;
8113 nsh_base.mdtype = key->mdtype;
8114 nsh_base.np = key->np;
8115 nsh_base.path_hdr = key->path_hdr;
8116
8117 nsh_base_mask.flags = mask->flags;
8118 nsh_base_mask.ttl = mask->ttl;
8119 nsh_base_mask.mdtype = mask->mdtype;
8120 nsh_base_mask.np = mask->np;
8121 nsh_base_mask.path_hdr = mask->path_hdr;
8122
8123 /* OVS_KEY_ATTR_NSH keys */
8124 nsh_key_ofs = nl_msg_start_nested(odp_actions, OVS_KEY_ATTR_NSH);
8125
8126 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
8127 char *data = nl_msg_put_unspec_uninit(odp_actions,
8128 OVS_NSH_KEY_ATTR_BASE,
8129 2 * sizeof(nsh_base));
8130 const char *lkey = (char *)&nsh_base, *lmask = (char *)&nsh_base_mask;
8131 size_t lkey_size = sizeof(nsh_base);
8132
8133 while (lkey_size--) {
8134 *data++ = *lkey++ & *lmask++;
8135 }
8136 lmask = (char *)&nsh_base_mask;
8137 memcpy(data, lmask, sizeof(nsh_base_mask));
8138
8139 switch (key->mdtype) {
8140 case NSH_M_TYPE1:
8141 memcpy(md1.context, key->context, sizeof key->context);
8142 memcpy(md1_mask.context, mask->context, sizeof mask->context);
8143
8144 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
8145 data = nl_msg_put_unspec_uninit(odp_actions,
8146 OVS_NSH_KEY_ATTR_MD1,
8147 2 * sizeof(md1));
8148 lkey = (char *)&md1;
8149 lmask = (char *)&md1_mask;
8150 lkey_size = sizeof(md1);
8151
8152 while (lkey_size--) {
8153 *data++ = *lkey++ & *lmask++;
8154 }
8155 lmask = (char *)&md1_mask;
8156 memcpy(data, lmask, sizeof(md1_mask));
8157 break;
8158 case NSH_M_TYPE2:
8159 default:
8160 /* No match support for other MD formats yet. */
8161 break;
8162 }
8163
8164 nl_msg_end_nested(odp_actions, nsh_key_ofs);
8165
8166 nl_msg_end_nested(odp_actions, offset);
8167 } else {
8168 if (!fully_masked) {
8169 memset(mask, 0xff, size);
8170 }
8171 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8172 nsh_key_to_attr(odp_actions, flow_nsh, NULL, 0, false);
8173 nl_msg_end_nested(odp_actions, offset);
8174 }
8175 memcpy(base, key, size);
8176 return true;
8177 }
8178
8179 static void
8180 commit_set_nsh_action(const struct flow *flow, struct flow *base_flow,
8181 struct ofpbuf *odp_actions,
8182 struct flow_wildcards *wc,
8183 bool use_masked)
8184 {
8185 struct ovs_key_nsh key, mask, base;
8186
8187 if (flow->dl_type != htons(ETH_TYPE_NSH) ||
8188 !memcmp(&base_flow->nsh, &flow->nsh, sizeof base_flow->nsh)) {
8189 return;
8190 }
8191
8192 /* Check that mdtype and np remain unchanged. */
8193 ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype &&
8194 flow->nsh.np == base_flow->nsh.np);
8195
8196 get_nsh_key(flow, &key, false);
8197 get_nsh_key(base_flow, &base, false);
8198 get_nsh_key(&wc->masks, &mask, true);
8199 mask.mdtype = 0; /* Not writable. */
8200 mask.np = 0; /* Not writable. */
8201
8202 if (commit_nsh(&base_flow->nsh, use_masked, &key, &base, &mask,
8203 sizeof key, odp_actions)) {
8204 put_nsh_key(&base, base_flow, false);
8205 if (mask.mdtype != 0) { /* Mask was changed by commit(). */
8206 put_nsh_key(&mask, &wc->masks, true);
8207 }
8208 }
8209 }
8210
8211 /* TCP, UDP, and SCTP keys have the same layout. */
8212 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
8213 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
8214
8215 static void
8216 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
8217 {
8218 tp->tcp.tcp_src = flow->tp_src;
8219 tp->tcp.tcp_dst = flow->tp_dst;
8220 }
8221
8222 static void
8223 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
8224 {
8225 flow->tp_src = tp->tcp.tcp_src;
8226 flow->tp_dst = tp->tcp.tcp_dst;
8227 }
8228
8229 static void
8230 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
8231 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8232 bool use_masked)
8233 {
8234 enum ovs_key_attr key_type;
8235 union ovs_key_tp key, mask, base;
8236 struct offsetof_sizeof ovs_key_tp_offsetof_sizeof_arr[] =
8237 OVS_KEY_TCP_OFFSETOF_SIZEOF_ARR;
8238
8239 /* Check if 'flow' really has an L3 header. */
8240 if (!flow->nw_proto) {
8241 return;
8242 }
8243
8244 if (!is_ip_any(base_flow)) {
8245 return;
8246 }
8247
8248 if (flow->nw_proto == IPPROTO_TCP) {
8249 key_type = OVS_KEY_ATTR_TCP;
8250 } else if (flow->nw_proto == IPPROTO_UDP) {
8251 key_type = OVS_KEY_ATTR_UDP;
8252 } else if (flow->nw_proto == IPPROTO_SCTP) {
8253 key_type = OVS_KEY_ATTR_SCTP;
8254 } else {
8255 return;
8256 }
8257
8258 get_tp_key(flow, &key);
8259 get_tp_key(base_flow, &base);
8260 get_tp_key(&wc->masks, &mask);
8261
8262 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
8263 ovs_key_tp_offsetof_sizeof_arr, odp_actions)) {
8264 put_tp_key(&base, base_flow);
8265 put_tp_key(&mask, &wc->masks);
8266 }
8267 }
8268
8269 static void
8270 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
8271 struct ofpbuf *odp_actions,
8272 struct flow_wildcards *wc,
8273 bool use_masked)
8274 {
8275 uint32_t key, mask, base;
8276 struct offsetof_sizeof ovs_key_prio_offsetof_sizeof_arr[] = {
8277 {0, sizeof(uint32_t)},
8278 {0, 0}
8279 };
8280
8281 key = flow->skb_priority;
8282 base = base_flow->skb_priority;
8283 mask = wc->masks.skb_priority;
8284
8285 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
8286 sizeof key, ovs_key_prio_offsetof_sizeof_arr, odp_actions)) {
8287 base_flow->skb_priority = base;
8288 wc->masks.skb_priority = mask;
8289 }
8290 }
8291
8292 static void
8293 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
8294 struct ofpbuf *odp_actions,
8295 struct flow_wildcards *wc,
8296 bool use_masked)
8297 {
8298 uint32_t key, mask, base;
8299 struct offsetof_sizeof ovs_key_pkt_mark_offsetof_sizeof_arr[] = {
8300 {0, sizeof(uint32_t)},
8301 {0, 0}
8302 };
8303
8304 key = flow->pkt_mark;
8305 base = base_flow->pkt_mark;
8306 mask = wc->masks.pkt_mark;
8307
8308 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
8309 sizeof key, ovs_key_pkt_mark_offsetof_sizeof_arr,
8310 odp_actions)) {
8311 base_flow->pkt_mark = base;
8312 wc->masks.pkt_mark = mask;
8313 }
8314 }
8315
8316 static void
8317 odp_put_pop_nsh_action(struct ofpbuf *odp_actions)
8318 {
8319 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_NSH);
8320 }
8321
8322 static void
8323 odp_put_push_nsh_action(struct ofpbuf *odp_actions,
8324 const struct flow *flow,
8325 struct ofpbuf *encap_data)
8326 {
8327 uint8_t * metadata = NULL;
8328 uint8_t md_size = 0;
8329
8330 switch (flow->nsh.mdtype) {
8331 case NSH_M_TYPE2:
8332 if (encap_data) {
8333 ovs_assert(encap_data->size < NSH_CTX_HDRS_MAX_LEN);
8334 metadata = encap_data->data;
8335 md_size = encap_data->size;
8336 } else {
8337 md_size = 0;
8338 }
8339 break;
8340 default:
8341 md_size = 0;
8342 break;
8343 }
8344 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_PUSH_NSH);
8345 nsh_key_to_attr(odp_actions, &flow->nsh, metadata, md_size, false);
8346 nl_msg_end_nested(odp_actions, offset);
8347 }
8348
8349 static void
8350 commit_encap_decap_action(const struct flow *flow,
8351 struct flow *base_flow,
8352 struct ofpbuf *odp_actions,
8353 struct flow_wildcards *wc,
8354 bool pending_encap, bool pending_decap,
8355 struct ofpbuf *encap_data)
8356 {
8357 if (pending_encap) {
8358 switch (ntohl(flow->packet_type)) {
8359 case PT_ETH: {
8360 /* push_eth */
8361 odp_put_push_eth_action(odp_actions, &flow->dl_src,
8362 &flow->dl_dst);
8363 base_flow->packet_type = flow->packet_type;
8364 base_flow->dl_src = flow->dl_src;
8365 base_flow->dl_dst = flow->dl_dst;
8366 break;
8367 }
8368 case PT_NSH:
8369 /* push_nsh */
8370 odp_put_push_nsh_action(odp_actions, flow, encap_data);
8371 base_flow->packet_type = flow->packet_type;
8372 /* Update all packet headers in base_flow. */
8373 memcpy(&base_flow->dl_dst, &flow->dl_dst,
8374 sizeof(*flow) - offsetof(struct flow, dl_dst));
8375 break;
8376 default:
8377 /* Only the above protocols are supported for encap.
8378 * The check is done at action translation. */
8379 OVS_NOT_REACHED();
8380 }
8381 } else if (pending_decap || flow->packet_type != base_flow->packet_type) {
8382 /* This is an explicit or implicit decap case. */
8383 if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE &&
8384 base_flow->packet_type == htonl(PT_ETH)) {
8385 /* Generate pop_eth and continue without recirculation. */
8386 odp_put_pop_eth_action(odp_actions);
8387 base_flow->packet_type = flow->packet_type;
8388 base_flow->dl_src = eth_addr_zero;
8389 base_flow->dl_dst = eth_addr_zero;
8390 } else {
8391 /* All other decap cases require recirculation.
8392 * No need to update the base flow here. */
8393 switch (ntohl(base_flow->packet_type)) {
8394 case PT_NSH:
8395 /* pop_nsh. */
8396 odp_put_pop_nsh_action(odp_actions);
8397 break;
8398 default:
8399 /* Checks are done during translation. */
8400 OVS_NOT_REACHED();
8401 }
8402 }
8403 }
8404
8405 wc->masks.packet_type = OVS_BE32_MAX;
8406 }
8407
8408 /* If any of the flow key data that ODP actions can modify are different in
8409 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
8410 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
8411 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
8412 * in addition to this function if needed. Sets fields in 'wc' that are
8413 * used as part of the action.
8414 *
8415 * In the common case, this function returns 0. If the flow key modification
8416 * requires the flow's packets to be forced into the userspace slow path, this
8417 * function returns SLOW_ACTION. This only happens when there is no ODP action
8418 * to modify some field that was actually modified. For example, there is no
8419 * ODP action to modify any ARP field, so such a modification triggers
8420 * SLOW_ACTION. (When this happens, packets that need such modification get
8421 * flushed to userspace and handled there, which works OK but much more slowly
8422 * than if the datapath handled it directly.) */
8423 enum slow_path_reason
8424 commit_odp_actions(const struct flow *flow, struct flow *base,
8425 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8426 bool use_masked, bool pending_encap, bool pending_decap,
8427 struct ofpbuf *encap_data)
8428 {
8429 /* If you add a field that OpenFlow actions can change, and that is visible
8430 * to the datapath (including all data fields), then you should also add
8431 * code here to commit changes to the field. */
8432 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
8433
8434 enum slow_path_reason slow1, slow2;
8435 bool mpls_done = false;
8436
8437 commit_encap_decap_action(flow, base, odp_actions, wc,
8438 pending_encap, pending_decap, encap_data);
8439 commit_set_ether_action(flow, base, odp_actions, wc, use_masked);
8440 /* Make packet a non-MPLS packet before committing L3/4 actions,
8441 * which would otherwise do nothing. */
8442 if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) {
8443 commit_mpls_action(flow, base, odp_actions);
8444 mpls_done = true;
8445 }
8446 commit_set_nsh_action(flow, base, odp_actions, wc, use_masked);
8447 slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
8448 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
8449 slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
8450 if (!mpls_done) {
8451 commit_mpls_action(flow, base, odp_actions);
8452 }
8453 commit_vlan_action(flow, base, odp_actions, wc);
8454 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
8455 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
8456
8457 return slow1 ? slow1 : slow2;
8458 }