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