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