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