]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
odp-util: Set a limit for nested parse_odp_key_mask_attr call
[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 ? 1 : 0,
500 cookie.controller.continuation ? 1 : 0,
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 ofpbuf_use_stub(&b, metadata,
2116 NSH_CTX_HDRS_MAX_LEN);
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_push_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 push.vlan_tpid = htons(tpid);
2286 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
2287 | (pcp << VLAN_PCP_SHIFT)
2288 | (cfi ? VLAN_CFI : 0));
2289 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
2290 &push, sizeof push);
2291
2292 return n;
2293 }
2294 }
2295
2296 if (!strncmp(s, "pop_vlan", 8)) {
2297 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
2298 return 8;
2299 }
2300
2301 {
2302 unsigned long long int meter_id;
2303 int n = -1;
2304
2305 if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) {
2306 nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id);
2307 return n;
2308 }
2309 }
2310
2311 {
2312 double percentage;
2313 int n = -1;
2314
2315 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
2316 && percentage >= 0. && percentage <= 100.0) {
2317 size_t sample_ofs, actions_ofs;
2318 double probability;
2319
2320 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
2321 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
2322 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
2323 (probability <= 0 ? 0
2324 : probability >= UINT32_MAX ? UINT32_MAX
2325 : probability));
2326
2327 actions_ofs = nl_msg_start_nested(actions,
2328 OVS_SAMPLE_ATTR_ACTIONS);
2329 int retval = parse_action_list(s + n, port_names, actions);
2330 if (retval < 0)
2331 return retval;
2332
2333 n += retval;
2334 nl_msg_end_nested(actions, actions_ofs);
2335 nl_msg_end_nested(actions, sample_ofs);
2336
2337 return s[n + 1] == ')' ? n + 2 : -EINVAL;
2338 }
2339 }
2340
2341 {
2342 if (!strncmp(s, "clone(", 6)) {
2343 size_t actions_ofs;
2344 int n = 6;
2345
2346 actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE);
2347 int retval = parse_action_list(s + n, port_names, actions);
2348 if (retval < 0) {
2349 return retval;
2350 }
2351 n += retval;
2352 nl_msg_end_nested(actions, actions_ofs);
2353 return n + 1;
2354 }
2355 }
2356
2357 {
2358 if (!strncmp(s, "push_nsh(", 9)) {
2359 int retval = parse_odp_push_nsh_action(s, actions);
2360 if (retval < 0) {
2361 return retval;
2362 }
2363 return retval + 1;
2364 }
2365 }
2366
2367 {
2368 int n;
2369 if (ovs_scan(s, "pop_nsh()%n", &n)) {
2370 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_NSH);
2371 return n;
2372 }
2373 }
2374
2375 {
2376 uint32_t port;
2377 int n;
2378
2379 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
2380 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
2381 return n;
2382 }
2383 }
2384
2385 {
2386 if (!strncmp(s, "ct_clear", 8)) {
2387 nl_msg_put_flag(actions, OVS_ACTION_ATTR_CT_CLEAR);
2388 return 8;
2389 }
2390 }
2391
2392 {
2393 int retval;
2394
2395 retval = parse_conntrack_action(s, actions);
2396 if (retval) {
2397 return retval;
2398 }
2399 }
2400
2401 {
2402 struct ovs_action_push_tnl data;
2403 int n;
2404
2405 n = ovs_parse_tnl_push(s, &data);
2406 if (n > 0) {
2407 odp_put_tnl_push_action(actions, &data);
2408 return n;
2409 } else if (n < 0) {
2410 return n;
2411 }
2412 }
2413 return -EINVAL;
2414 }
2415
2416 /* Parses the string representation of datapath actions, in the format output
2417 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2418 * value. On success, the ODP actions are appended to 'actions' as a series of
2419 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2420 * way, 'actions''s data might be reallocated. */
2421 int
2422 odp_actions_from_string(const char *s, const struct simap *port_names,
2423 struct ofpbuf *actions)
2424 {
2425 size_t old_size;
2426
2427 if (!strcasecmp(s, "drop")) {
2428 return 0;
2429 }
2430
2431 old_size = actions->size;
2432 for (;;) {
2433 int retval;
2434
2435 s += strspn(s, delimiters);
2436 if (!*s) {
2437 return 0;
2438 }
2439
2440 retval = parse_odp_action(s, port_names, actions);
2441 if (retval < 0 || !strchr(delimiters, s[retval])) {
2442 actions->size = old_size;
2443 return -retval;
2444 }
2445 s += retval;
2446 }
2447
2448 return 0;
2449 }
2450 \f
2451 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
2452 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
2453 };
2454
2455 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
2456 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
2457 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
2458 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
2459 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
2460 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
2461 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
2462 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
2463 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
2464 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
2465 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
2466 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
2467 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
2468 .next = ovs_vxlan_ext_attr_lens ,
2469 .next_max = OVS_VXLAN_EXT_MAX},
2470 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = 16 },
2471 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = 16 },
2472 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = ATTR_LEN_VARIABLE },
2473 };
2474
2475 const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
2476 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
2477 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
2478 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
2479 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
2480 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
2481 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
2482 .next = ovs_tun_key_attr_lens,
2483 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
2484 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
2485 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
2486 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
2487 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
2488 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
2489 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
2490 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
2491 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
2492 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
2493 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
2494 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
2495 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
2496 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
2497 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
2498 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
2499 [OVS_KEY_ATTR_CT_STATE] = { .len = 4 },
2500 [OVS_KEY_ATTR_CT_ZONE] = { .len = 2 },
2501 [OVS_KEY_ATTR_CT_MARK] = { .len = 4 },
2502 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
2503 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
2504 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
2505 [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4 },
2506 [OVS_KEY_ATTR_NSH] = { .len = ATTR_LEN_NESTED,
2507 .next = ovs_nsh_key_attr_lens,
2508 .next_max = OVS_NSH_KEY_ATTR_MAX },
2509 };
2510
2511 /* Returns the correct length of the payload for a flow key attribute of the
2512 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2513 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2514 * payload is a nested type. */
2515 static int
2516 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type)
2517 {
2518 if (type > max_type) {
2519 return ATTR_LEN_INVALID;
2520 }
2521
2522 return tbl[type].len;
2523 }
2524
2525 static void
2526 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
2527 {
2528 size_t len = nl_attr_get_size(a);
2529 if (len) {
2530 const uint8_t *unspec;
2531 unsigned int i;
2532
2533 unspec = nl_attr_get(a);
2534 for (i = 0; i < len; i++) {
2535 if (i) {
2536 ds_put_char(ds, ' ');
2537 }
2538 ds_put_format(ds, "%02x", unspec[i]);
2539 }
2540 }
2541 }
2542
2543 static const char *
2544 ovs_frag_type_to_string(enum ovs_frag_type type)
2545 {
2546 switch (type) {
2547 case OVS_FRAG_TYPE_NONE:
2548 return "no";
2549 case OVS_FRAG_TYPE_FIRST:
2550 return "first";
2551 case OVS_FRAG_TYPE_LATER:
2552 return "later";
2553 case __OVS_FRAG_TYPE_MAX:
2554 default:
2555 return "<error>";
2556 }
2557 }
2558
2559 enum odp_key_fitness
2560 odp_nsh_hdr_from_attr(const struct nlattr *attr,
2561 struct nsh_hdr *nsh_hdr, size_t size)
2562 {
2563 unsigned int left;
2564 const struct nlattr *a;
2565 bool unknown = false;
2566 uint8_t flags = 0;
2567 uint8_t ttl = 63;
2568 size_t mdlen = 0;
2569 bool has_md1 = false;
2570 bool has_md2 = false;
2571
2572 memset(nsh_hdr, 0, size);
2573
2574 NL_NESTED_FOR_EACH (a, left, attr) {
2575 uint16_t type = nl_attr_type(a);
2576 size_t len = nl_attr_get_size(a);
2577 int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
2578 OVS_NSH_KEY_ATTR_MAX, type);
2579
2580 if (len != expected_len && expected_len >= 0) {
2581 return ODP_FIT_ERROR;
2582 }
2583
2584 switch (type) {
2585 case OVS_NSH_KEY_ATTR_BASE: {
2586 const struct ovs_nsh_key_base *base = nl_attr_get(a);
2587 nsh_hdr->next_proto = base->np;
2588 nsh_hdr->md_type = base->mdtype;
2589 put_16aligned_be32(&nsh_hdr->path_hdr, base->path_hdr);
2590 flags = base->flags;
2591 ttl = base->ttl;
2592 break;
2593 }
2594 case OVS_NSH_KEY_ATTR_MD1: {
2595 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
2596 struct nsh_md1_ctx *md1_dst = &nsh_hdr->md1;
2597 has_md1 = true;
2598 mdlen = nl_attr_get_size(a);
2599 if ((mdlen + NSH_BASE_HDR_LEN != NSH_M_TYPE1_LEN) ||
2600 (mdlen + NSH_BASE_HDR_LEN > size)) {
2601 return ODP_FIT_ERROR;
2602 }
2603 memcpy(md1_dst, md1, mdlen);
2604 break;
2605 }
2606 case OVS_NSH_KEY_ATTR_MD2: {
2607 struct nsh_md2_tlv *md2_dst = &nsh_hdr->md2;
2608 const uint8_t *md2 = nl_attr_get(a);
2609 has_md2 = true;
2610 mdlen = nl_attr_get_size(a);
2611 if (mdlen + NSH_BASE_HDR_LEN > size) {
2612 return ODP_FIT_ERROR;
2613 }
2614 memcpy(md2_dst, md2, mdlen);
2615 break;
2616 }
2617 default:
2618 /* Allow this to show up as unexpected, if there are unknown
2619 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2620 unknown = true;
2621 break;
2622 }
2623 }
2624
2625 if (unknown) {
2626 return ODP_FIT_TOO_MUCH;
2627 }
2628
2629 if ((has_md1 && nsh_hdr->md_type != NSH_M_TYPE1)
2630 || (has_md2 && nsh_hdr->md_type != NSH_M_TYPE2)) {
2631 return ODP_FIT_ERROR;
2632 }
2633
2634 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2635 nsh_set_flags_ttl_len(nsh_hdr, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
2636
2637 return ODP_FIT_PERFECT;
2638 }
2639
2640 enum odp_key_fitness
2641 odp_nsh_key_from_attr(const struct nlattr *attr, struct ovs_key_nsh *nsh,
2642 struct ovs_key_nsh *nsh_mask)
2643 {
2644 unsigned int left;
2645 const struct nlattr *a;
2646 bool unknown = false;
2647 bool has_md1 = false;
2648
2649 NL_NESTED_FOR_EACH (a, left, attr) {
2650 uint16_t type = nl_attr_type(a);
2651 size_t len = nl_attr_get_size(a);
2652 int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
2653 OVS_NSH_KEY_ATTR_MAX, type);
2654
2655 /* the attribute can have mask, len is 2 * expected_len for that case.
2656 */
2657 if ((len != expected_len) && (len != 2 * expected_len) &&
2658 (expected_len >= 0)) {
2659 return ODP_FIT_ERROR;
2660 }
2661
2662 if ((nsh_mask && (expected_len >= 0) && (len != 2 * expected_len)) ||
2663 (!nsh_mask && (expected_len >= 0) && (len == 2 * expected_len))) {
2664 return ODP_FIT_ERROR;
2665 }
2666
2667 switch (type) {
2668 case OVS_NSH_KEY_ATTR_UNSPEC:
2669 break;
2670 case OVS_NSH_KEY_ATTR_BASE: {
2671 const struct ovs_nsh_key_base *base = nl_attr_get(a);
2672 nsh->flags = base->flags;
2673 nsh->ttl = base->ttl;
2674 nsh->mdtype = base->mdtype;
2675 nsh->np = base->np;
2676 nsh->path_hdr = base->path_hdr;
2677 if (nsh_mask && (len == 2 * sizeof(*base))) {
2678 const struct ovs_nsh_key_base *base_mask = base + 1;
2679 nsh_mask->flags = base_mask->flags;
2680 nsh_mask->ttl = base_mask->ttl;
2681 nsh_mask->mdtype = base_mask->mdtype;
2682 nsh_mask->np = base_mask->np;
2683 nsh_mask->path_hdr = base_mask->path_hdr;
2684 }
2685 break;
2686 }
2687 case OVS_NSH_KEY_ATTR_MD1: {
2688 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
2689 has_md1 = true;
2690 memcpy(nsh->context, md1->context, sizeof md1->context);
2691 if (len == 2 * sizeof(*md1)) {
2692 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
2693 memcpy(nsh_mask->context, md1_mask->context,
2694 sizeof(*md1_mask));
2695 }
2696 break;
2697 }
2698 case OVS_NSH_KEY_ATTR_MD2:
2699 default:
2700 /* Allow this to show up as unexpected, if there are unknown
2701 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2702 unknown = true;
2703 break;
2704 }
2705 }
2706
2707 if (unknown) {
2708 return ODP_FIT_TOO_MUCH;
2709 }
2710
2711 if (has_md1 && nsh->mdtype != NSH_M_TYPE1) {
2712 return ODP_FIT_ERROR;
2713 }
2714
2715 return ODP_FIT_PERFECT;
2716 }
2717
2718 static enum odp_key_fitness
2719 odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask,
2720 struct flow_tnl *tun)
2721 {
2722 unsigned int left;
2723 const struct nlattr *a;
2724 bool ttl = false;
2725 bool unknown = false;
2726
2727 NL_NESTED_FOR_EACH(a, left, attr) {
2728 uint16_t type = nl_attr_type(a);
2729 size_t len = nl_attr_get_size(a);
2730 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
2731 OVS_TUNNEL_ATTR_MAX, type);
2732
2733 if (len != expected_len && expected_len >= 0) {
2734 return ODP_FIT_ERROR;
2735 }
2736
2737 switch (type) {
2738 case OVS_TUNNEL_KEY_ATTR_ID:
2739 tun->tun_id = nl_attr_get_be64(a);
2740 tun->flags |= FLOW_TNL_F_KEY;
2741 break;
2742 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2743 tun->ip_src = nl_attr_get_be32(a);
2744 break;
2745 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2746 tun->ip_dst = nl_attr_get_be32(a);
2747 break;
2748 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
2749 tun->ipv6_src = nl_attr_get_in6_addr(a);
2750 break;
2751 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
2752 tun->ipv6_dst = nl_attr_get_in6_addr(a);
2753 break;
2754 case OVS_TUNNEL_KEY_ATTR_TOS:
2755 tun->ip_tos = nl_attr_get_u8(a);
2756 break;
2757 case OVS_TUNNEL_KEY_ATTR_TTL:
2758 tun->ip_ttl = nl_attr_get_u8(a);
2759 ttl = true;
2760 break;
2761 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2762 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
2763 break;
2764 case OVS_TUNNEL_KEY_ATTR_CSUM:
2765 tun->flags |= FLOW_TNL_F_CSUM;
2766 break;
2767 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2768 tun->tp_src = nl_attr_get_be16(a);
2769 break;
2770 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2771 tun->tp_dst = nl_attr_get_be16(a);
2772 break;
2773 case OVS_TUNNEL_KEY_ATTR_OAM:
2774 tun->flags |= FLOW_TNL_F_OAM;
2775 break;
2776 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
2777 static const struct nl_policy vxlan_opts_policy[] = {
2778 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
2779 };
2780 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
2781
2782 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
2783 return ODP_FIT_ERROR;
2784 }
2785
2786 if (ext[OVS_VXLAN_EXT_GBP]) {
2787 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
2788
2789 tun->gbp_id = htons(gbp & 0xFFFF);
2790 tun->gbp_flags = (gbp >> 16) & 0xFF;
2791 }
2792
2793 break;
2794 }
2795 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2796 tun_metadata_from_geneve_nlattr(a, is_mask, tun);
2797 break;
2798 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: {
2799 const struct erspan_metadata *opts = nl_attr_get(a);
2800
2801 tun->erspan_ver = opts->version;
2802 if (tun->erspan_ver == 1) {
2803 tun->erspan_idx = ntohl(opts->u.index);
2804 } else if (tun->erspan_ver == 2) {
2805 tun->erspan_dir = opts->u.md2.dir;
2806 tun->erspan_hwid = get_hwid(&opts->u.md2);
2807 } else {
2808 VLOG_WARN("%s invalid erspan version\n", __func__);
2809 }
2810 break;
2811 }
2812
2813 default:
2814 /* Allow this to show up as unexpected, if there are unknown
2815 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2816 unknown = true;
2817 break;
2818 }
2819 }
2820
2821 if (!ttl) {
2822 return ODP_FIT_ERROR;
2823 }
2824 if (unknown) {
2825 return ODP_FIT_TOO_MUCH;
2826 }
2827 return ODP_FIT_PERFECT;
2828 }
2829
2830 enum odp_key_fitness
2831 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
2832 {
2833 memset(tun, 0, sizeof *tun);
2834 return odp_tun_key_from_attr__(attr, false, tun);
2835 }
2836
2837 static void
2838 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
2839 const struct flow_tnl *tun_flow_key,
2840 const struct ofpbuf *key_buf, const char *tnl_type)
2841 {
2842 size_t tun_key_ofs;
2843
2844 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
2845
2846 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2847 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
2848 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
2849 }
2850 if (tun_key->ip_src) {
2851 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
2852 }
2853 if (tun_key->ip_dst) {
2854 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
2855 }
2856 if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
2857 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
2858 }
2859 if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
2860 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
2861 }
2862 if (tun_key->ip_tos) {
2863 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
2864 }
2865 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
2866 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
2867 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
2868 }
2869 if (tun_key->flags & FLOW_TNL_F_CSUM) {
2870 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
2871 }
2872 if (tun_key->tp_src) {
2873 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
2874 }
2875 if (tun_key->tp_dst) {
2876 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
2877 }
2878 if (tun_key->flags & FLOW_TNL_F_OAM) {
2879 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
2880 }
2881
2882 /* If tnl_type is set to a particular type of output tunnel,
2883 * only put its relevant tunnel metadata to the nlattr.
2884 * If tnl_type is NULL, put tunnel metadata according to the
2885 * 'tun_key'.
2886 */
2887 if ((!tnl_type || !strcmp(tnl_type, "vxlan")) &&
2888 (tun_key->gbp_flags || tun_key->gbp_id)) {
2889 size_t vxlan_opts_ofs;
2890
2891 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
2892 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
2893 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
2894 nl_msg_end_nested(a, vxlan_opts_ofs);
2895 }
2896
2897 if (!tnl_type || !strcmp(tnl_type, "geneve")) {
2898 tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
2899 }
2900
2901 if ((!tnl_type || !strcmp(tnl_type, "erspan") ||
2902 !strcmp(tnl_type, "ip6erspan")) &&
2903 (tun_key->erspan_ver == 1 || tun_key->erspan_ver == 2)) {
2904 struct erspan_metadata opts;
2905
2906 opts.version = tun_key->erspan_ver;
2907 if (opts.version == 1) {
2908 opts.u.index = htonl(tun_key->erspan_idx);
2909 } else {
2910 opts.u.md2.dir = tun_key->erspan_dir;
2911 set_hwid(&opts.u.md2, tun_key->erspan_hwid);
2912 }
2913 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
2914 &opts, sizeof(opts));
2915 }
2916
2917 nl_msg_end_nested(a, tun_key_ofs);
2918 }
2919
2920 static bool
2921 odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size,
2922 int constant)
2923 {
2924 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2925 * that -1 becomes all-1-bits and 0 does not change. */
2926 ovs_be16 be16 = (OVS_FORCE ovs_be16) constant;
2927 uint32_t u32 = constant;
2928 uint8_t u8 = constant;
2929 const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any;
2930
2931 switch (attr) {
2932 case OVS_KEY_ATTR_UNSPEC:
2933 case OVS_KEY_ATTR_ENCAP:
2934 case __OVS_KEY_ATTR_MAX:
2935 default:
2936 return false;
2937
2938 case OVS_KEY_ATTR_PRIORITY:
2939 case OVS_KEY_ATTR_IN_PORT:
2940 case OVS_KEY_ATTR_ETHERNET:
2941 case OVS_KEY_ATTR_VLAN:
2942 case OVS_KEY_ATTR_ETHERTYPE:
2943 case OVS_KEY_ATTR_IPV4:
2944 case OVS_KEY_ATTR_TCP:
2945 case OVS_KEY_ATTR_UDP:
2946 case OVS_KEY_ATTR_ICMP:
2947 case OVS_KEY_ATTR_ICMPV6:
2948 case OVS_KEY_ATTR_ND:
2949 case OVS_KEY_ATTR_SKB_MARK:
2950 case OVS_KEY_ATTR_TUNNEL:
2951 case OVS_KEY_ATTR_SCTP:
2952 case OVS_KEY_ATTR_DP_HASH:
2953 case OVS_KEY_ATTR_RECIRC_ID:
2954 case OVS_KEY_ATTR_MPLS:
2955 case OVS_KEY_ATTR_CT_STATE:
2956 case OVS_KEY_ATTR_CT_ZONE:
2957 case OVS_KEY_ATTR_CT_MARK:
2958 case OVS_KEY_ATTR_CT_LABELS:
2959 case OVS_KEY_ATTR_PACKET_TYPE:
2960 case OVS_KEY_ATTR_NSH:
2961 return is_all_byte(mask, size, u8);
2962
2963 case OVS_KEY_ATTR_TCP_FLAGS:
2964 return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16);
2965
2966 case OVS_KEY_ATTR_IPV6: {
2967 const struct ovs_key_ipv6 *ipv6_mask = mask;
2968 return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
2969 == htonl(IPV6_LABEL_MASK & u32)
2970 && ipv6_mask->ipv6_proto == u8
2971 && ipv6_mask->ipv6_tclass == u8
2972 && ipv6_mask->ipv6_hlimit == u8
2973 && ipv6_mask->ipv6_frag == u8
2974 && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6)
2975 && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6));
2976 }
2977
2978 case OVS_KEY_ATTR_ARP:
2979 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8);
2980
2981 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
2982 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4,
2983 ipv4_proto), u8);
2984
2985 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
2986 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6,
2987 ipv6_proto), u8);
2988 }
2989 }
2990
2991 /* The caller must already have verified that 'ma' has a correct length.
2992 *
2993 * The main purpose of this function is formatting, to allow code to figure out
2994 * whether the mask can be omitted. It doesn't try hard for attributes that
2995 * contain sub-attributes, etc., because normally those would be broken down
2996 * further for formatting. */
2997 static bool
2998 odp_mask_attr_is_wildcard(const struct nlattr *ma)
2999 {
3000 return odp_mask_is_constant__(nl_attr_type(ma),
3001 nl_attr_get(ma), nl_attr_get_size(ma), 0);
3002 }
3003
3004 /* The caller must already have verified that 'size' is a correct length for
3005 * 'attr'.
3006 *
3007 * The main purpose of this function is formatting, to allow code to figure out
3008 * whether the mask can be omitted. It doesn't try hard for attributes that
3009 * contain sub-attributes, etc., because normally those would be broken down
3010 * further for formatting. */
3011 static bool
3012 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
3013 {
3014 return odp_mask_is_constant__(attr, mask, size, -1);
3015 }
3016
3017 /* The caller must already have verified that 'ma' has a correct length. */
3018 static bool
3019 odp_mask_attr_is_exact(const struct nlattr *ma)
3020 {
3021 enum ovs_key_attr attr = nl_attr_type(ma);
3022 return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma));
3023 }
3024
3025 void
3026 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
3027 char *port_name)
3028 {
3029 struct odp_portno_names *odp_portno_names;
3030
3031 odp_portno_names = xmalloc(sizeof *odp_portno_names);
3032 odp_portno_names->port_no = port_no;
3033 odp_portno_names->name = xstrdup(port_name);
3034 hmap_insert(portno_names, &odp_portno_names->hmap_node,
3035 hash_odp_port(port_no));
3036 }
3037
3038 static char *
3039 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
3040 {
3041 if (portno_names) {
3042 struct odp_portno_names *odp_portno_names;
3043
3044 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
3045 hash_odp_port(port_no), portno_names) {
3046 if (odp_portno_names->port_no == port_no) {
3047 return odp_portno_names->name;
3048 }
3049 }
3050 }
3051 return NULL;
3052 }
3053
3054 void
3055 odp_portno_names_destroy(struct hmap *portno_names)
3056 {
3057 struct odp_portno_names *odp_portno_names;
3058
3059 HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) {
3060 free(odp_portno_names->name);
3061 free(odp_portno_names);
3062 }
3063 }
3064
3065 void
3066 odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no,
3067 struct ds *s)
3068 {
3069 const char *name = odp_portno_names_get(portno_names, port_no);
3070 if (name) {
3071 ds_put_cstr(s, name);
3072 } else {
3073 ds_put_format(s, "%"PRIu32, port_no);
3074 }
3075 }
3076
3077 /* Format helpers. */
3078
3079 static void
3080 format_eth(struct ds *ds, const char *name, const struct eth_addr key,
3081 const struct eth_addr *mask, bool verbose)
3082 {
3083 bool mask_empty = mask && eth_addr_is_zero(*mask);
3084
3085 if (verbose || !mask_empty) {
3086 bool mask_full = !mask || eth_mask_is_exact(*mask);
3087
3088 if (mask_full) {
3089 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
3090 } else {
3091 ds_put_format(ds, "%s=", name);
3092 eth_format_masked(key, mask, ds);
3093 ds_put_char(ds, ',');
3094 }
3095 }
3096 }
3097
3098
3099 static void
3100 format_be64(struct ds *ds, const char *name, ovs_be64 key,
3101 const ovs_be64 *mask, bool verbose)
3102 {
3103 bool mask_empty = mask && !*mask;
3104
3105 if (verbose || !mask_empty) {
3106 bool mask_full = !mask || *mask == OVS_BE64_MAX;
3107
3108 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
3109 if (!mask_full) { /* Partially masked. */
3110 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
3111 }
3112 ds_put_char(ds, ',');
3113 }
3114 }
3115
3116 static void
3117 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
3118 const ovs_be32 *mask, bool verbose)
3119 {
3120 bool mask_empty = mask && !*mask;
3121
3122 if (verbose || !mask_empty) {
3123 bool mask_full = !mask || *mask == OVS_BE32_MAX;
3124
3125 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
3126 if (!mask_full) { /* Partially masked. */
3127 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
3128 }
3129 ds_put_char(ds, ',');
3130 }
3131 }
3132
3133 static void
3134 format_in6_addr(struct ds *ds, const char *name,
3135 const struct in6_addr *key,
3136 const struct in6_addr *mask,
3137 bool verbose)
3138 {
3139 char buf[INET6_ADDRSTRLEN];
3140 bool mask_empty = mask && ipv6_mask_is_any(mask);
3141
3142 if (verbose || !mask_empty) {
3143 bool mask_full = !mask || ipv6_mask_is_exact(mask);
3144
3145 inet_ntop(AF_INET6, key, buf, sizeof buf);
3146 ds_put_format(ds, "%s=%s", name, buf);
3147 if (!mask_full) { /* Partially masked. */
3148 inet_ntop(AF_INET6, mask, buf, sizeof buf);
3149 ds_put_format(ds, "/%s", buf);
3150 }
3151 ds_put_char(ds, ',');
3152 }
3153 }
3154
3155 static void
3156 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
3157 const ovs_be32 *mask, bool verbose)
3158 {
3159 bool mask_empty = mask && !*mask;
3160
3161 if (verbose || !mask_empty) {
3162 bool mask_full = !mask
3163 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
3164
3165 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
3166 if (!mask_full) { /* Partially masked. */
3167 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
3168 }
3169 ds_put_char(ds, ',');
3170 }
3171 }
3172
3173 static void
3174 format_u8x(struct ds *ds, const char *name, uint8_t key,
3175 const uint8_t *mask, bool verbose)
3176 {
3177 bool mask_empty = mask && !*mask;
3178
3179 if (verbose || !mask_empty) {
3180 bool mask_full = !mask || *mask == UINT8_MAX;
3181
3182 ds_put_format(ds, "%s=%#"PRIx8, name, key);
3183 if (!mask_full) { /* Partially masked. */
3184 ds_put_format(ds, "/%#"PRIx8, *mask);
3185 }
3186 ds_put_char(ds, ',');
3187 }
3188 }
3189
3190 static void
3191 format_u8u(struct ds *ds, const char *name, uint8_t key,
3192 const uint8_t *mask, bool verbose)
3193 {
3194 bool mask_empty = mask && !*mask;
3195
3196 if (verbose || !mask_empty) {
3197 bool mask_full = !mask || *mask == UINT8_MAX;
3198
3199 ds_put_format(ds, "%s=%"PRIu8, name, key);
3200 if (!mask_full) { /* Partially masked. */
3201 ds_put_format(ds, "/%#"PRIx8, *mask);
3202 }
3203 ds_put_char(ds, ',');
3204 }
3205 }
3206
3207 static void
3208 format_be16(struct ds *ds, const char *name, ovs_be16 key,
3209 const ovs_be16 *mask, bool verbose)
3210 {
3211 bool mask_empty = mask && !*mask;
3212
3213 if (verbose || !mask_empty) {
3214 bool mask_full = !mask || *mask == OVS_BE16_MAX;
3215
3216 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
3217 if (!mask_full) { /* Partially masked. */
3218 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3219 }
3220 ds_put_char(ds, ',');
3221 }
3222 }
3223
3224 static void
3225 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
3226 const ovs_be16 *mask, bool verbose)
3227 {
3228 bool mask_empty = mask && !*mask;
3229
3230 if (verbose || !mask_empty) {
3231 bool mask_full = !mask || *mask == OVS_BE16_MAX;
3232
3233 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
3234 if (!mask_full) { /* Partially masked. */
3235 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3236 }
3237 ds_put_char(ds, ',');
3238 }
3239 }
3240
3241 static void
3242 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
3243 const uint16_t *mask, bool verbose)
3244 {
3245 bool mask_empty = mask && !*mask;
3246
3247 if (verbose || !mask_empty) {
3248 ds_put_cstr(ds, name);
3249 ds_put_char(ds, '(');
3250 if (mask) {
3251 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
3252 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
3253 } else { /* Fully masked. */
3254 format_flags(ds, flow_tun_flag_to_string, key, '|');
3255 }
3256 ds_put_cstr(ds, "),");
3257 }
3258 }
3259
3260 static bool
3261 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
3262 const struct attr_len_tbl tbl[], int max_type, bool need_key)
3263 {
3264 int expected_len;
3265
3266 expected_len = odp_key_attr_len(tbl, max_type, nl_attr_type(a));
3267 if (expected_len != ATTR_LEN_VARIABLE &&
3268 expected_len != ATTR_LEN_NESTED) {
3269
3270 bool bad_key_len = nl_attr_get_size(a) != expected_len;
3271 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
3272
3273 if (bad_key_len || bad_mask_len) {
3274 if (need_key) {
3275 ds_put_format(ds, "key%u", nl_attr_type(a));
3276 }
3277 if (bad_key_len) {
3278 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
3279 nl_attr_get_size(a), expected_len);
3280 }
3281 format_generic_odp_key(a, ds);
3282 if (ma) {
3283 ds_put_char(ds, '/');
3284 if (bad_mask_len) {
3285 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
3286 nl_attr_get_size(ma), expected_len);
3287 }
3288 format_generic_odp_key(ma, ds);
3289 }
3290 ds_put_char(ds, ')');
3291 return false;
3292 }
3293 }
3294
3295 return true;
3296 }
3297
3298 static void
3299 format_unknown_key(struct ds *ds, const struct nlattr *a,
3300 const struct nlattr *ma)
3301 {
3302 ds_put_format(ds, "key%u(", nl_attr_type(a));
3303 format_generic_odp_key(a, ds);
3304 if (ma && !odp_mask_attr_is_exact(ma)) {
3305 ds_put_char(ds, '/');
3306 format_generic_odp_key(ma, ds);
3307 }
3308 ds_put_cstr(ds, "),");
3309 }
3310
3311 static void
3312 format_odp_tun_vxlan_opt(const struct nlattr *attr,
3313 const struct nlattr *mask_attr, struct ds *ds,
3314 bool verbose)
3315 {
3316 unsigned int left;
3317 const struct nlattr *a;
3318 struct ofpbuf ofp;
3319
3320 ofpbuf_init(&ofp, 100);
3321 NL_NESTED_FOR_EACH(a, left, attr) {
3322 uint16_t type = nl_attr_type(a);
3323 const struct nlattr *ma = NULL;
3324
3325 if (mask_attr) {
3326 ma = nl_attr_find__(nl_attr_get(mask_attr),
3327 nl_attr_get_size(mask_attr), type);
3328 if (!ma) {
3329 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
3330 OVS_VXLAN_EXT_MAX,
3331 &ofp, a);
3332 }
3333 }
3334
3335 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
3336 OVS_VXLAN_EXT_MAX, true)) {
3337 continue;
3338 }
3339
3340 switch (type) {
3341 case OVS_VXLAN_EXT_GBP: {
3342 uint32_t key = nl_attr_get_u32(a);
3343 ovs_be16 id, id_mask;
3344 uint8_t flags, flags_mask = 0;
3345
3346 id = htons(key & 0xFFFF);
3347 flags = (key >> 16) & 0xFF;
3348 if (ma) {
3349 uint32_t mask = nl_attr_get_u32(ma);
3350 id_mask = htons(mask & 0xFFFF);
3351 flags_mask = (mask >> 16) & 0xFF;
3352 }
3353
3354 ds_put_cstr(ds, "gbp(");
3355 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
3356 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
3357 ds_chomp(ds, ',');
3358 ds_put_cstr(ds, "),");
3359 break;
3360 }
3361
3362 default:
3363 format_unknown_key(ds, a, ma);
3364 }
3365 ofpbuf_clear(&ofp);
3366 }
3367
3368 ds_chomp(ds, ',');
3369 ofpbuf_uninit(&ofp);
3370 }
3371
3372 static void
3373 format_odp_tun_erspan_opt(const struct nlattr *attr,
3374 const struct nlattr *mask_attr, struct ds *ds,
3375 bool verbose)
3376 {
3377 const struct erspan_metadata *opts, *mask;
3378 uint8_t ver, ver_ma, dir, dir_ma, hwid, hwid_ma;
3379
3380 opts = nl_attr_get(attr);
3381 mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
3382
3383 ver = (uint8_t)opts->version;
3384 if (mask) {
3385 ver_ma = (uint8_t)mask->version;
3386 }
3387
3388 format_u8u(ds, "ver", ver, mask ? &ver_ma : NULL, verbose);
3389
3390 if (opts->version == 1) {
3391 if (mask) {
3392 ds_put_format(ds, "idx=%#"PRIx32"/%#"PRIx32",",
3393 ntohl(opts->u.index),
3394 ntohl(mask->u.index));
3395 } else {
3396 ds_put_format(ds, "idx=%#"PRIx32",", ntohl(opts->u.index));
3397 }
3398 } else if (opts->version == 2) {
3399 dir = opts->u.md2.dir;
3400 hwid = opts->u.md2.hwid;
3401 if (mask) {
3402 dir_ma = mask->u.md2.dir;
3403 hwid_ma = mask->u.md2.hwid;
3404 }
3405
3406 format_u8u(ds, "dir", dir, mask ? &dir_ma : NULL, verbose);
3407 format_u8x(ds, "hwid", hwid, mask ? &hwid_ma : NULL, verbose);
3408 }
3409 ds_chomp(ds, ',');
3410 }
3411
3412 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3413
3414 static void
3415 format_geneve_opts(const struct geneve_opt *opt,
3416 const struct geneve_opt *mask, int opts_len,
3417 struct ds *ds, bool verbose)
3418 {
3419 while (opts_len > 0) {
3420 unsigned int len;
3421 uint8_t data_len, data_len_mask;
3422
3423 if (opts_len < sizeof *opt) {
3424 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
3425 opts_len, sizeof *opt);
3426 return;
3427 }
3428
3429 data_len = opt->length * 4;
3430 if (mask) {
3431 if (mask->length == 0x1f) {
3432 data_len_mask = UINT8_MAX;
3433 } else {
3434 data_len_mask = mask->length;
3435 }
3436 }
3437 len = sizeof *opt + data_len;
3438 if (len > opts_len) {
3439 ds_put_format(ds, "opt len %u greater than remaining %u",
3440 len, opts_len);
3441 return;
3442 }
3443
3444 ds_put_char(ds, '{');
3445 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
3446 verbose);
3447 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
3448 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
3449 if (data_len &&
3450 (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
3451 ds_put_hex(ds, opt + 1, data_len);
3452 if (mask && !is_all_ones(mask + 1, data_len)) {
3453 ds_put_char(ds, '/');
3454 ds_put_hex(ds, mask + 1, data_len);
3455 }
3456 } else {
3457 ds_chomp(ds, ',');
3458 }
3459 ds_put_char(ds, '}');
3460
3461 opt += len / sizeof(*opt);
3462 if (mask) {
3463 mask += len / sizeof(*opt);
3464 }
3465 opts_len -= len;
3466 };
3467 }
3468
3469 static void
3470 format_odp_tun_geneve(const struct nlattr *attr,
3471 const struct nlattr *mask_attr, struct ds *ds,
3472 bool verbose)
3473 {
3474 int opts_len = nl_attr_get_size(attr);
3475 const struct geneve_opt *opt = nl_attr_get(attr);
3476 const struct geneve_opt *mask = mask_attr ?
3477 nl_attr_get(mask_attr) : NULL;
3478
3479 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
3480 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
3481 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
3482 return;
3483 }
3484
3485 format_geneve_opts(opt, mask, opts_len, ds, verbose);
3486 }
3487
3488 static void
3489 format_odp_nsh_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
3490 struct ds *ds)
3491 {
3492 unsigned int left;
3493 const struct nlattr *a;
3494 struct ovs_key_nsh nsh;
3495 struct ovs_key_nsh nsh_mask;
3496
3497 memset(&nsh, 0, sizeof nsh);
3498 memset(&nsh_mask, 0xff, sizeof nsh_mask);
3499
3500 NL_NESTED_FOR_EACH (a, left, attr) {
3501 enum ovs_nsh_key_attr type = nl_attr_type(a);
3502 const struct nlattr *ma = NULL;
3503
3504 if (mask_attr) {
3505 ma = nl_attr_find__(nl_attr_get(mask_attr),
3506 nl_attr_get_size(mask_attr), type);
3507 }
3508
3509 if (!check_attr_len(ds, a, ma, ovs_nsh_key_attr_lens,
3510 OVS_NSH_KEY_ATTR_MAX, true)) {
3511 continue;
3512 }
3513
3514 switch (type) {
3515 case OVS_NSH_KEY_ATTR_UNSPEC:
3516 break;
3517 case OVS_NSH_KEY_ATTR_BASE: {
3518 const struct ovs_nsh_key_base *base = nl_attr_get(a);
3519 const struct ovs_nsh_key_base *base_mask
3520 = ma ? nl_attr_get(ma) : NULL;
3521 nsh.flags = base->flags;
3522 nsh.ttl = base->ttl;
3523 nsh.mdtype = base->mdtype;
3524 nsh.np = base->np;
3525 nsh.path_hdr = base->path_hdr;
3526 if (base_mask) {
3527 nsh_mask.flags = base_mask->flags;
3528 nsh_mask.ttl = base_mask->ttl;
3529 nsh_mask.mdtype = base_mask->mdtype;
3530 nsh_mask.np = base_mask->np;
3531 nsh_mask.path_hdr = base_mask->path_hdr;
3532 }
3533 break;
3534 }
3535 case OVS_NSH_KEY_ATTR_MD1: {
3536 const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3537 const struct ovs_nsh_key_md1 *md1_mask
3538 = ma ? nl_attr_get(ma) : NULL;
3539 memcpy(nsh.context, md1->context, sizeof md1->context);
3540 if (md1_mask) {
3541 memcpy(nsh_mask.context, md1_mask->context,
3542 sizeof md1_mask->context);
3543 }
3544 break;
3545 }
3546 case OVS_NSH_KEY_ATTR_MD2:
3547 case __OVS_NSH_KEY_ATTR_MAX:
3548 default:
3549 /* No support for matching other metadata formats yet. */
3550 break;
3551 }
3552 }
3553
3554 if (mask_attr) {
3555 format_nsh_key_mask(ds, &nsh, &nsh_mask);
3556 } else {
3557 format_nsh_key(ds, &nsh);
3558 }
3559 }
3560
3561 static void
3562 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
3563 struct ds *ds, bool verbose)
3564 {
3565 unsigned int left;
3566 const struct nlattr *a;
3567 uint16_t flags = 0;
3568 uint16_t mask_flags = 0;
3569 struct ofpbuf ofp;
3570
3571 ofpbuf_init(&ofp, 100);
3572 NL_NESTED_FOR_EACH(a, left, attr) {
3573 enum ovs_tunnel_key_attr type = nl_attr_type(a);
3574 const struct nlattr *ma = NULL;
3575
3576 if (mask_attr) {
3577 ma = nl_attr_find__(nl_attr_get(mask_attr),
3578 nl_attr_get_size(mask_attr), type);
3579 if (!ma) {
3580 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
3581 OVS_TUNNEL_KEY_ATTR_MAX,
3582 &ofp, a);
3583 }
3584 }
3585
3586 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
3587 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
3588 continue;
3589 }
3590
3591 switch (type) {
3592 case OVS_TUNNEL_KEY_ATTR_ID:
3593 format_be64(ds, "tun_id", nl_attr_get_be64(a),
3594 ma ? nl_attr_get(ma) : NULL, verbose);
3595 flags |= FLOW_TNL_F_KEY;
3596 if (ma) {
3597 mask_flags |= FLOW_TNL_F_KEY;
3598 }
3599 break;
3600 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
3601 format_ipv4(ds, "src", nl_attr_get_be32(a),
3602 ma ? nl_attr_get(ma) : NULL, verbose);
3603 break;
3604 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
3605 format_ipv4(ds, "dst", nl_attr_get_be32(a),
3606 ma ? nl_attr_get(ma) : NULL, verbose);
3607 break;
3608 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
3609 struct in6_addr ipv6_src;
3610 ipv6_src = nl_attr_get_in6_addr(a);
3611 format_in6_addr(ds, "ipv6_src", &ipv6_src,
3612 ma ? nl_attr_get(ma) : NULL, verbose);
3613 break;
3614 }
3615 case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
3616 struct in6_addr ipv6_dst;
3617 ipv6_dst = nl_attr_get_in6_addr(a);
3618 format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
3619 ma ? nl_attr_get(ma) : NULL, verbose);
3620 break;
3621 }
3622 case OVS_TUNNEL_KEY_ATTR_TOS:
3623 format_u8x(ds, "tos", nl_attr_get_u8(a),
3624 ma ? nl_attr_get(ma) : NULL, verbose);
3625 break;
3626 case OVS_TUNNEL_KEY_ATTR_TTL:
3627 format_u8u(ds, "ttl", nl_attr_get_u8(a),
3628 ma ? nl_attr_get(ma) : NULL, verbose);
3629 break;
3630 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3631 flags |= FLOW_TNL_F_DONT_FRAGMENT;
3632 break;
3633 case OVS_TUNNEL_KEY_ATTR_CSUM:
3634 flags |= FLOW_TNL_F_CSUM;
3635 break;
3636 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
3637 format_be16(ds, "tp_src", nl_attr_get_be16(a),
3638 ma ? nl_attr_get(ma) : NULL, verbose);
3639 break;
3640 case OVS_TUNNEL_KEY_ATTR_TP_DST:
3641 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
3642 ma ? nl_attr_get(ma) : NULL, verbose);
3643 break;
3644 case OVS_TUNNEL_KEY_ATTR_OAM:
3645 flags |= FLOW_TNL_F_OAM;
3646 break;
3647 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
3648 ds_put_cstr(ds, "vxlan(");
3649 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
3650 ds_put_cstr(ds, "),");
3651 break;
3652 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3653 ds_put_cstr(ds, "geneve(");
3654 format_odp_tun_geneve(a, ma, ds, verbose);
3655 ds_put_cstr(ds, "),");
3656 break;
3657 case OVS_TUNNEL_KEY_ATTR_PAD:
3658 break;
3659 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
3660 ds_put_cstr(ds, "erspan(");
3661 format_odp_tun_erspan_opt(a, ma, ds, verbose);
3662 ds_put_cstr(ds, "),");
3663 break;
3664 case __OVS_TUNNEL_KEY_ATTR_MAX:
3665 default:
3666 format_unknown_key(ds, a, ma);
3667 }
3668 ofpbuf_clear(&ofp);
3669 }
3670
3671 /* Flags can have a valid mask even if the attribute is not set, so
3672 * we need to collect these separately. */
3673 if (mask_attr) {
3674 NL_NESTED_FOR_EACH(a, left, mask_attr) {
3675 switch (nl_attr_type(a)) {
3676 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3677 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
3678 break;
3679 case OVS_TUNNEL_KEY_ATTR_CSUM:
3680 mask_flags |= FLOW_TNL_F_CSUM;
3681 break;
3682 case OVS_TUNNEL_KEY_ATTR_OAM:
3683 mask_flags |= FLOW_TNL_F_OAM;
3684 break;
3685 }
3686 }
3687 }
3688
3689 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
3690 verbose);
3691 ds_chomp(ds, ',');
3692 ofpbuf_uninit(&ofp);
3693 }
3694
3695 static const char *
3696 odp_ct_state_to_string(uint32_t flag)
3697 {
3698 switch (flag) {
3699 case OVS_CS_F_REPLY_DIR:
3700 return "rpl";
3701 case OVS_CS_F_TRACKED:
3702 return "trk";
3703 case OVS_CS_F_NEW:
3704 return "new";
3705 case OVS_CS_F_ESTABLISHED:
3706 return "est";
3707 case OVS_CS_F_RELATED:
3708 return "rel";
3709 case OVS_CS_F_INVALID:
3710 return "inv";
3711 case OVS_CS_F_SRC_NAT:
3712 return "snat";
3713 case OVS_CS_F_DST_NAT:
3714 return "dnat";
3715 default:
3716 return NULL;
3717 }
3718 }
3719
3720 static void
3721 format_frag(struct ds *ds, const char *name, uint8_t key,
3722 const uint8_t *mask, bool verbose OVS_UNUSED)
3723 {
3724 bool mask_empty = mask && !*mask;
3725 bool mask_full = !mask || *mask == UINT8_MAX;
3726
3727 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3728 if (!mask_empty && !mask_full) {
3729 ds_put_format(ds, "error: partial mask not supported for frag (%#"
3730 PRIx8"),", *mask);
3731 } else if (!mask_empty) {
3732 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
3733 }
3734 }
3735
3736 static bool
3737 mask_empty(const struct nlattr *ma)
3738 {
3739 const void *mask;
3740 size_t n;
3741
3742 if (!ma) {
3743 return true;
3744 }
3745 mask = nl_attr_get(ma);
3746 n = nl_attr_get_size(ma);
3747
3748 return is_all_zeros(mask, n);
3749 }
3750
3751 /* The caller must have already verified that 'a' and 'ma' have correct
3752 * lengths. */
3753 static void
3754 format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma,
3755 const struct hmap *portno_names, struct ds *ds,
3756 bool verbose)
3757 {
3758 enum ovs_key_attr attr = nl_attr_type(a);
3759 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3760 bool is_exact;
3761
3762 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
3763
3764 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
3765
3766 ds_put_char(ds, '(');
3767 switch (attr) {
3768 case OVS_KEY_ATTR_ENCAP:
3769 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
3770 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
3771 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
3772 verbose);
3773 } else if (nl_attr_get_size(a)) {
3774 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
3775 ds, verbose);
3776 }
3777 break;
3778
3779 case OVS_KEY_ATTR_PRIORITY:
3780 case OVS_KEY_ATTR_SKB_MARK:
3781 case OVS_KEY_ATTR_DP_HASH:
3782 case OVS_KEY_ATTR_RECIRC_ID:
3783 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
3784 if (!is_exact) {
3785 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
3786 }
3787 break;
3788
3789 case OVS_KEY_ATTR_CT_MARK:
3790 if (verbose || !mask_empty(ma)) {
3791 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
3792 if (!is_exact) {
3793 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
3794 }
3795 }
3796 break;
3797
3798 case OVS_KEY_ATTR_CT_STATE:
3799 if (verbose) {
3800 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
3801 if (!is_exact) {
3802 ds_put_format(ds, "/%#"PRIx32,
3803 mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
3804 }
3805 } else if (!is_exact) {
3806 format_flags_masked(ds, NULL, odp_ct_state_to_string,
3807 nl_attr_get_u32(a),
3808 mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
3809 UINT32_MAX);
3810 } else {
3811 format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
3812 }
3813 break;
3814
3815 case OVS_KEY_ATTR_CT_ZONE:
3816 if (verbose || !mask_empty(ma)) {
3817 ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
3818 if (!is_exact) {
3819 ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
3820 }
3821 }
3822 break;
3823
3824 case OVS_KEY_ATTR_CT_LABELS: {
3825 const ovs_32aligned_u128 *value = nl_attr_get(a);
3826 const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL;
3827
3828 format_u128(ds, value, mask, verbose);
3829 break;
3830 }
3831
3832 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
3833 const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a);
3834 const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
3835
3836 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
3837 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
3838 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
3839 verbose);
3840 format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port),
3841 verbose);
3842 format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port),
3843 verbose);
3844 ds_chomp(ds, ',');
3845 break;
3846 }
3847
3848 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
3849 const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a);
3850 const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
3851
3852 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
3853 verbose);
3854 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
3855 verbose);
3856 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
3857 verbose);
3858 format_be16(ds, "src_port", key->src_port, MASK(mask, src_port),
3859 verbose);
3860 format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port),
3861 verbose);
3862 ds_chomp(ds, ',');
3863 break;
3864 }
3865
3866 case OVS_KEY_ATTR_TUNNEL:
3867 format_odp_tun_attr(a, ma, ds, verbose);
3868 break;
3869
3870 case OVS_KEY_ATTR_IN_PORT:
3871 if (is_exact) {
3872 odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
3873 } else {
3874 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
3875 if (!is_exact) {
3876 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
3877 }
3878 }
3879 break;
3880
3881 case OVS_KEY_ATTR_PACKET_TYPE: {
3882 ovs_be32 value = nl_attr_get_be32(a);
3883 ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX;
3884
3885 ovs_be16 ns = htons(pt_ns(value));
3886 ovs_be16 ns_mask = htons(pt_ns(mask));
3887 format_be16(ds, "ns", ns, &ns_mask, verbose);
3888
3889 ovs_be16 ns_type = pt_ns_type_be(value);
3890 ovs_be16 ns_type_mask = pt_ns_type_be(mask);
3891 format_be16x(ds, "id", ns_type, &ns_type_mask, verbose);
3892
3893 ds_chomp(ds, ',');
3894 break;
3895 }
3896
3897 case OVS_KEY_ATTR_ETHERNET: {
3898 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
3899 const struct ovs_key_ethernet *key = nl_attr_get(a);
3900
3901 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
3902 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
3903 ds_chomp(ds, ',');
3904 break;
3905 }
3906 case OVS_KEY_ATTR_VLAN:
3907 format_vlan_tci(ds, nl_attr_get_be16(a),
3908 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
3909 break;
3910
3911 case OVS_KEY_ATTR_MPLS: {
3912 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
3913 const struct ovs_key_mpls *mpls_mask = NULL;
3914 size_t size = nl_attr_get_size(a);
3915
3916 if (!size || size % sizeof *mpls_key) {
3917 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
3918 return;
3919 }
3920 if (!is_exact) {
3921 mpls_mask = nl_attr_get(ma);
3922 if (size != nl_attr_get_size(ma)) {
3923 ds_put_format(ds, "(key length %"PRIuSIZE" != "
3924 "mask length %"PRIuSIZE")",
3925 size, nl_attr_get_size(ma));
3926 return;
3927 }
3928 }
3929 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
3930 break;
3931 }
3932 case OVS_KEY_ATTR_ETHERTYPE:
3933 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
3934 if (!is_exact) {
3935 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
3936 }
3937 break;
3938
3939 case OVS_KEY_ATTR_IPV4: {
3940 const struct ovs_key_ipv4 *key = nl_attr_get(a);
3941 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
3942
3943 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
3944 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
3945 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
3946 verbose);
3947 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
3948 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
3949 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
3950 verbose);
3951 ds_chomp(ds, ',');
3952 break;
3953 }
3954 case OVS_KEY_ATTR_IPV6: {
3955 const struct ovs_key_ipv6 *key = nl_attr_get(a);
3956 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
3957
3958 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
3959 verbose);
3960 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
3961 verbose);
3962 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
3963 verbose);
3964 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
3965 verbose);
3966 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
3967 verbose);
3968 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
3969 verbose);
3970 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
3971 verbose);
3972 ds_chomp(ds, ',');
3973 break;
3974 }
3975 /* These have the same structure and format. */
3976 case OVS_KEY_ATTR_TCP:
3977 case OVS_KEY_ATTR_UDP:
3978 case OVS_KEY_ATTR_SCTP: {
3979 const struct ovs_key_tcp *key = nl_attr_get(a);
3980 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
3981
3982 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
3983 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
3984 ds_chomp(ds, ',');
3985 break;
3986 }
3987 case OVS_KEY_ATTR_TCP_FLAGS:
3988 if (!is_exact) {
3989 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
3990 ntohs(nl_attr_get_be16(a)),
3991 TCP_FLAGS(nl_attr_get_be16(ma)),
3992 TCP_FLAGS(OVS_BE16_MAX));
3993 } else {
3994 format_flags(ds, packet_tcp_flag_to_string,
3995 ntohs(nl_attr_get_be16(a)), '|');
3996 }
3997 break;
3998
3999 case OVS_KEY_ATTR_ICMP: {
4000 const struct ovs_key_icmp *key = nl_attr_get(a);
4001 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
4002
4003 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
4004 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
4005 ds_chomp(ds, ',');
4006 break;
4007 }
4008 case OVS_KEY_ATTR_ICMPV6: {
4009 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
4010 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
4011
4012 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
4013 verbose);
4014 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
4015 verbose);
4016 ds_chomp(ds, ',');
4017 break;
4018 }
4019 case OVS_KEY_ATTR_ARP: {
4020 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
4021 const struct ovs_key_arp *key = nl_attr_get(a);
4022
4023 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
4024 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
4025 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
4026 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
4027 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
4028 ds_chomp(ds, ',');
4029 break;
4030 }
4031 case OVS_KEY_ATTR_ND: {
4032 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
4033 const struct ovs_key_nd *key = nl_attr_get(a);
4034
4035 format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
4036 verbose);
4037 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
4038 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
4039
4040 ds_chomp(ds, ',');
4041 break;
4042 }
4043 case OVS_KEY_ATTR_NSH: {
4044 format_odp_nsh_attr(a, ma, ds);
4045 break;
4046 }
4047 case OVS_KEY_ATTR_UNSPEC:
4048 case __OVS_KEY_ATTR_MAX:
4049 default:
4050 format_generic_odp_key(a, ds);
4051 if (!is_exact) {
4052 ds_put_char(ds, '/');
4053 format_generic_odp_key(ma, ds);
4054 }
4055 break;
4056 }
4057 ds_put_char(ds, ')');
4058 }
4059
4060 static void
4061 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
4062 const struct hmap *portno_names, struct ds *ds,
4063 bool verbose)
4064 {
4065 if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4066 OVS_KEY_ATTR_MAX, false)) {
4067 format_odp_key_attr__(a, ma, portno_names, ds, verbose);
4068 }
4069 }
4070
4071 static struct nlattr *
4072 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
4073 struct ofpbuf *ofp, const struct nlattr *key)
4074 {
4075 const struct nlattr *a;
4076 unsigned int left;
4077 int type = nl_attr_type(key);
4078 int size = nl_attr_get_size(key);
4079
4080 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
4081 nl_msg_put_unspec_zero(ofp, type, size);
4082 } else {
4083 size_t nested_mask;
4084
4085 if (tbl[type].next) {
4086 const struct attr_len_tbl *entry = &tbl[type];
4087 tbl = entry->next;
4088 max = entry->next_max;
4089 }
4090
4091 nested_mask = nl_msg_start_nested(ofp, type);
4092 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
4093 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
4094 }
4095 nl_msg_end_nested(ofp, nested_mask);
4096 }
4097
4098 return ofp->base;
4099 }
4100
4101 static void
4102 format_u128(struct ds *ds, const ovs_32aligned_u128 *key,
4103 const ovs_32aligned_u128 *mask, bool verbose)
4104 {
4105 if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) {
4106 ovs_be128 value = hton128(get_32aligned_u128(key));
4107 ds_put_hex(ds, &value, sizeof value);
4108 if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) {
4109 value = hton128(get_32aligned_u128(mask));
4110 ds_put_char(ds, '/');
4111 ds_put_hex(ds, &value, sizeof value);
4112 }
4113 }
4114 }
4115
4116 /* Read the string from 's_' as a 128-bit value. If the string contains
4117 * a "/", the rest of the string will be treated as a 128-bit mask.
4118 *
4119 * If either the value or mask is larger than 64 bits, the string must
4120 * be in hexadecimal.
4121 */
4122 static int
4123 scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
4124 {
4125 char *s = CONST_CAST(char *, s_);
4126 ovs_be128 be_value;
4127 ovs_be128 be_mask;
4128
4129 if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
4130 *value = ntoh128(be_value);
4131
4132 if (mask) {
4133 int n;
4134
4135 if (ovs_scan(s, "/%n", &n)) {
4136 int error;
4137
4138 s += n;
4139 error = parse_int_string(s, (uint8_t *)&be_mask,
4140 sizeof be_mask, &s);
4141 if (error) {
4142 return 0;
4143 }
4144 *mask = ntoh128(be_mask);
4145 } else {
4146 *mask = OVS_U128_MAX;
4147 }
4148 }
4149 return s - s_;
4150 }
4151
4152 return 0;
4153 }
4154
4155 int
4156 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
4157 {
4158 const char *s = s_;
4159
4160 if (ovs_scan(s, "ufid:")) {
4161 s += 5;
4162
4163 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
4164 return -EINVAL;
4165 }
4166 s += UUID_LEN;
4167
4168 return s - s_;
4169 }
4170
4171 return 0;
4172 }
4173
4174 void
4175 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
4176 {
4177 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
4178 }
4179
4180 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4181 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4182 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4183 * non-null, translates odp port number to its name. */
4184 void
4185 odp_flow_format(const struct nlattr *key, size_t key_len,
4186 const struct nlattr *mask, size_t mask_len,
4187 const struct hmap *portno_names, struct ds *ds, bool verbose)
4188 {
4189 if (key_len) {
4190 const struct nlattr *a;
4191 unsigned int left;
4192 bool has_ethtype_key = false;
4193 bool has_packet_type_key = false;
4194 struct ofpbuf ofp;
4195 bool first_field = true;
4196
4197 ofpbuf_init(&ofp, 100);
4198 NL_ATTR_FOR_EACH (a, left, key, key_len) {
4199 int attr_type = nl_attr_type(a);
4200 const struct nlattr *ma = (mask && mask_len
4201 ? nl_attr_find__(mask, mask_len,
4202 attr_type)
4203 : NULL);
4204 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4205 OVS_KEY_ATTR_MAX, false)) {
4206 continue;
4207 }
4208
4209 bool is_nested_attr;
4210 bool is_wildcard = false;
4211
4212 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
4213 has_ethtype_key = true;
4214 } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE) {
4215 has_packet_type_key = true;
4216 }
4217
4218 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
4219 OVS_KEY_ATTR_MAX, attr_type) ==
4220 ATTR_LEN_NESTED;
4221
4222 if (mask && mask_len) {
4223 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
4224 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
4225 }
4226
4227 if (verbose || !is_wildcard || is_nested_attr) {
4228 if (is_wildcard && !ma) {
4229 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
4230 OVS_KEY_ATTR_MAX,
4231 &ofp, a);
4232 }
4233 if (!first_field) {
4234 ds_put_char(ds, ',');
4235 }
4236 format_odp_key_attr__(a, ma, portno_names, ds, verbose);
4237 first_field = false;
4238 } else if (attr_type == OVS_KEY_ATTR_ETHERNET
4239 && !has_packet_type_key) {
4240 /* This special case reflects differences between the kernel
4241 * and userspace datapaths regarding the root type of the
4242 * packet being matched (typically Ethernet but some tunnels
4243 * can encapsulate IPv4 etc.). The kernel datapath does not
4244 * have an explicit way to indicate packet type; instead:
4245 *
4246 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4247 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4248 * Ethertype encoded in the Ethernet header.
4249 *
4250 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4251 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4252 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4253 * packet is an IPv4 packet).
4254 *
4255 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4256 * all-wildcarded, it is important to print it.
4257 *
4258 * On the other hand, the userspace datapath supports
4259 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4260 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4261 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4262 if (!first_field) {
4263 ds_put_char(ds, ',');
4264 }
4265 ds_put_cstr(ds, "eth()");
4266 }
4267 ofpbuf_clear(&ofp);
4268 }
4269 ofpbuf_uninit(&ofp);
4270
4271 if (left) {
4272 int i;
4273
4274 if (left == key_len) {
4275 ds_put_cstr(ds, "<empty>");
4276 }
4277 ds_put_format(ds, ",***%u leftover bytes*** (", left);
4278 for (i = 0; i < left; i++) {
4279 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
4280 }
4281 ds_put_char(ds, ')');
4282 }
4283 if (!has_ethtype_key) {
4284 const struct nlattr *ma = nl_attr_find__(mask, mask_len,
4285 OVS_KEY_ATTR_ETHERTYPE);
4286 if (ma) {
4287 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
4288 ntohs(nl_attr_get_be16(ma)));
4289 }
4290 }
4291 } else {
4292 ds_put_cstr(ds, "<empty>");
4293 }
4294 }
4295
4296 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4297 * OVS_KEY_ATTR_* attributes in 'key'. */
4298 void
4299 odp_flow_key_format(const struct nlattr *key,
4300 size_t key_len, struct ds *ds)
4301 {
4302 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
4303 }
4304
4305 static bool
4306 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
4307 {
4308 if (!strcasecmp(s, "no")) {
4309 *type = OVS_FRAG_TYPE_NONE;
4310 } else if (!strcasecmp(s, "first")) {
4311 *type = OVS_FRAG_TYPE_FIRST;
4312 } else if (!strcasecmp(s, "later")) {
4313 *type = OVS_FRAG_TYPE_LATER;
4314 } else {
4315 return false;
4316 }
4317 return true;
4318 }
4319
4320 /* Parsing. */
4321
4322 static int
4323 scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
4324 {
4325 int n;
4326
4327 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
4328 ETH_ADDR_SCAN_ARGS(*key), &n)) {
4329 int len = n;
4330
4331 if (mask) {
4332 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
4333 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
4334 len += n;
4335 } else {
4336 memset(mask, 0xff, sizeof *mask);
4337 }
4338 }
4339 return len;
4340 }
4341 return 0;
4342 }
4343
4344 static int
4345 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
4346 {
4347 int n;
4348
4349 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
4350 int len = n;
4351
4352 if (mask) {
4353 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
4354 IP_SCAN_ARGS(mask), &n)) {
4355 len += n;
4356 } else {
4357 *mask = OVS_BE32_MAX;
4358 }
4359 }
4360 return len;
4361 }
4362 return 0;
4363 }
4364
4365 static int
4366 scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
4367 {
4368 int n;
4369 char ipv6_s[IPV6_SCAN_LEN + 1];
4370
4371 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
4372 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
4373 int len = n;
4374
4375 if (mask) {
4376 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
4377 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
4378 len += n;
4379 } else {
4380 memset(mask, 0xff, sizeof *mask);
4381 }
4382 }
4383 return len;
4384 }
4385 return 0;
4386 }
4387
4388 static int
4389 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
4390 {
4391 int key_, mask_;
4392 int n;
4393
4394 if (ovs_scan(s, "%i%n", &key_, &n)
4395 && (key_ & ~IPV6_LABEL_MASK) == 0) {
4396 int len = n;
4397
4398 *key = htonl(key_);
4399 if (mask) {
4400 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
4401 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
4402 len += n;
4403 *mask = htonl(mask_);
4404 } else {
4405 *mask = htonl(IPV6_LABEL_MASK);
4406 }
4407 }
4408 return len;
4409 }
4410 return 0;
4411 }
4412
4413 static int
4414 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
4415 {
4416 int n;
4417
4418 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
4419 int len = n;
4420
4421 if (mask) {
4422 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
4423 len += n;
4424 } else {
4425 *mask = UINT8_MAX;
4426 }
4427 }
4428 return len;
4429 }
4430 return 0;
4431 }
4432
4433 static int
4434 scan_u16(const char *s, uint16_t *key, uint16_t *mask)
4435 {
4436 int n;
4437
4438 if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
4439 int len = n;
4440
4441 if (mask) {
4442 if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
4443 len += n;
4444 } else {
4445 *mask = UINT16_MAX;
4446 }
4447 }
4448 return len;
4449 }
4450 return 0;
4451 }
4452
4453 static int
4454 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
4455 {
4456 int n;
4457
4458 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
4459 int len = n;
4460
4461 if (mask) {
4462 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
4463 len += n;
4464 } else {
4465 *mask = UINT32_MAX;
4466 }
4467 }
4468 return len;
4469 }
4470 return 0;
4471 }
4472
4473 static int
4474 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
4475 {
4476 uint16_t key_, mask_;
4477 int n;
4478
4479 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
4480 int len = n;
4481
4482 *key = htons(key_);
4483 if (mask) {
4484 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
4485 len += n;
4486 *mask = htons(mask_);
4487 } else {
4488 *mask = OVS_BE16_MAX;
4489 }
4490 }
4491 return len;
4492 }
4493 return 0;
4494 }
4495
4496 static int
4497 scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask)
4498 {
4499 uint32_t key_, mask_;
4500 int n;
4501
4502 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
4503 int len = n;
4504
4505 *key = htonl(key_);
4506 if (mask) {
4507 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
4508 len += n;
4509 *mask = htonl(mask_);
4510 } else {
4511 *mask = OVS_BE32_MAX;
4512 }
4513 }
4514 return len;
4515 }
4516 return 0;
4517 }
4518
4519 static int
4520 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
4521 {
4522 uint64_t key_, mask_;
4523 int n;
4524
4525 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
4526 int len = n;
4527
4528 *key = htonll(key_);
4529 if (mask) {
4530 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
4531 len += n;
4532 *mask = htonll(mask_);
4533 } else {
4534 *mask = OVS_BE64_MAX;
4535 }
4536 }
4537 return len;
4538 }
4539 return 0;
4540 }
4541
4542 static int
4543 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
4544 {
4545 uint32_t flags, fmask;
4546 int n;
4547
4548 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
4549 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
4550 if (n >= 0 && s[n] == ')') {
4551 *key = flags;
4552 if (mask) {
4553 *mask = fmask;
4554 }
4555 return n + 1;
4556 }
4557 return 0;
4558 }
4559
4560 static int
4561 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
4562 {
4563 uint32_t flags, fmask;
4564 int n;
4565
4566 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
4567 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
4568 if (n >= 0) {
4569 *key = htons(flags);
4570 if (mask) {
4571 *mask = htons(fmask);
4572 }
4573 return n;
4574 }
4575 return 0;
4576 }
4577
4578 static uint32_t
4579 ovs_to_odp_ct_state(uint8_t state)
4580 {
4581 uint32_t odp = 0;
4582
4583 #define CS_STATE(ENUM, INDEX, NAME) \
4584 if (state & CS_##ENUM) { \
4585 odp |= OVS_CS_F_##ENUM; \
4586 }
4587 CS_STATES
4588 #undef CS_STATE
4589
4590 return odp;
4591 }
4592
4593 static uint8_t
4594 odp_to_ovs_ct_state(uint32_t flags)
4595 {
4596 uint32_t state = 0;
4597
4598 #define CS_STATE(ENUM, INDEX, NAME) \
4599 if (flags & OVS_CS_F_##ENUM) { \
4600 state |= CS_##ENUM; \
4601 }
4602 CS_STATES
4603 #undef CS_STATE
4604
4605 return state;
4606 }
4607
4608 static int
4609 scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
4610 {
4611 uint32_t flags, fmask;
4612 int n;
4613
4614 n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
4615 ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
4616 mask ? &fmask : NULL);
4617
4618 if (n >= 0) {
4619 *key = flags;
4620 if (mask) {
4621 *mask = fmask;
4622 }
4623 return n;
4624 }
4625 return 0;
4626 }
4627
4628 static int
4629 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
4630 {
4631 int n;
4632 char frag[8];
4633 enum ovs_frag_type frag_type;
4634
4635 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
4636 && ovs_frag_type_from_string(frag, &frag_type)) {
4637 int len = n;
4638
4639 *key = frag_type;
4640 if (mask) {
4641 *mask = UINT8_MAX;
4642 }
4643 return len;
4644 }
4645 return 0;
4646 }
4647
4648 static int
4649 scan_port(const char *s, uint32_t *key, uint32_t *mask,
4650 const struct simap *port_names)
4651 {
4652 int n;
4653
4654 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
4655 int len = n;
4656
4657 if (mask) {
4658 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
4659 len += n;
4660 } else {
4661 *mask = UINT32_MAX;
4662 }
4663 }
4664 return len;
4665 } else if (port_names) {
4666 const struct simap_node *node;
4667 int len;
4668
4669 len = strcspn(s, ")");
4670 node = simap_find_len(port_names, s, len);
4671 if (node) {
4672 *key = node->data;
4673
4674 if (mask) {
4675 *mask = UINT32_MAX;
4676 }
4677 return len;
4678 }
4679 }
4680 return 0;
4681 }
4682
4683 /* Helper for vlan parsing. */
4684 struct ovs_key_vlan__ {
4685 ovs_be16 tci;
4686 };
4687
4688 static bool
4689 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
4690 {
4691 const uint16_t mask = ((1U << bits) - 1) << offset;
4692
4693 if (value >> bits) {
4694 return false;
4695 }
4696
4697 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
4698 return true;
4699 }
4700
4701 static int
4702 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
4703 uint8_t offset)
4704 {
4705 uint16_t key_, mask_;
4706 int n;
4707
4708 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
4709 int len = n;
4710
4711 if (set_be16_bf(key, bits, offset, key_)) {
4712 if (mask) {
4713 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
4714 len += n;
4715
4716 if (!set_be16_bf(mask, bits, offset, mask_)) {
4717 return 0;
4718 }
4719 } else {
4720 *mask |= htons(((1U << bits) - 1) << offset);
4721 }
4722 }
4723 return len;
4724 }
4725 }
4726 return 0;
4727 }
4728
4729 static int
4730 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
4731 {
4732 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
4733 }
4734
4735 static int
4736 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
4737 {
4738 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
4739 }
4740
4741 static int
4742 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
4743 {
4744 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
4745 }
4746
4747 /* For MPLS. */
4748 static bool
4749 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
4750 {
4751 const uint32_t mask = ((1U << bits) - 1) << offset;
4752
4753 if (value >> bits) {
4754 return false;
4755 }
4756
4757 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
4758 return true;
4759 }
4760
4761 static int
4762 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
4763 uint8_t offset)
4764 {
4765 uint32_t key_, mask_;
4766 int n;
4767
4768 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
4769 int len = n;
4770
4771 if (set_be32_bf(key, bits, offset, key_)) {
4772 if (mask) {
4773 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
4774 len += n;
4775
4776 if (!set_be32_bf(mask, bits, offset, mask_)) {
4777 return 0;
4778 }
4779 } else {
4780 *mask |= htonl(((1U << bits) - 1) << offset);
4781 }
4782 }
4783 return len;
4784 }
4785 }
4786 return 0;
4787 }
4788
4789 static int
4790 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
4791 {
4792 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
4793 }
4794
4795 static int
4796 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
4797 {
4798 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
4799 }
4800
4801 static int
4802 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
4803 {
4804 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
4805 }
4806
4807 static int
4808 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
4809 {
4810 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
4811 }
4812
4813 static int
4814 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
4815 {
4816 const char *s_base = s;
4817 ovs_be16 id = 0, id_mask = 0;
4818 uint8_t flags = 0, flags_mask = 0;
4819 int len;
4820
4821 if (!strncmp(s, "id=", 3)) {
4822 s += 3;
4823 len = scan_be16(s, &id, mask ? &id_mask : NULL);
4824 if (len == 0) {
4825 return 0;
4826 }
4827 s += len;
4828 }
4829
4830 if (s[0] == ',') {
4831 s++;
4832 }
4833 if (!strncmp(s, "flags=", 6)) {
4834 s += 6;
4835 len = scan_u8(s, &flags, mask ? &flags_mask : NULL);
4836 if (len == 0) {
4837 return 0;
4838 }
4839 s += len;
4840 }
4841
4842 if (!strncmp(s, "))", 2)) {
4843 s += 2;
4844
4845 *key = (flags << 16) | ntohs(id);
4846 if (mask) {
4847 *mask = (flags_mask << 16) | ntohs(id_mask);
4848 }
4849
4850 return s - s_base;
4851 }
4852
4853 return 0;
4854 }
4855
4856 static int
4857 scan_erspan_metadata(const char *s,
4858 struct erspan_metadata *key,
4859 struct erspan_metadata *mask)
4860 {
4861 const char *s_base = s;
4862 uint32_t idx = 0, idx_mask = 0;
4863 uint8_t ver = 0, dir = 0, hwid = 0;
4864 uint8_t ver_mask = 0, dir_mask = 0, hwid_mask = 0;
4865 int len;
4866
4867 if (!strncmp(s, "ver=", 4)) {
4868 s += 4;
4869 len = scan_u8(s, &ver, mask ? &ver_mask : NULL);
4870 if (len == 0) {
4871 return 0;
4872 }
4873 s += len;
4874 }
4875
4876 if (s[0] == ',') {
4877 s++;
4878 }
4879
4880 if (ver == 1) {
4881 if (!strncmp(s, "idx=", 4)) {
4882 s += 4;
4883 len = scan_u32(s, &idx, mask ? &idx_mask : NULL);
4884 if (len == 0) {
4885 return 0;
4886 }
4887 s += len;
4888 }
4889
4890 if (!strncmp(s, ")", 1)) {
4891 s += 1;
4892 key->version = ver;
4893 key->u.index = htonl(idx);
4894 if (mask) {
4895 mask->u.index = htonl(idx_mask);
4896 }
4897 }
4898 return s - s_base;
4899
4900 } else if (ver == 2) {
4901 if (!strncmp(s, "dir=", 4)) {
4902 s += 4;
4903 len = scan_u8(s, &dir, mask ? &dir_mask : NULL);
4904 if (len == 0) {
4905 return 0;
4906 }
4907 s += len;
4908 }
4909 if (s[0] == ',') {
4910 s++;
4911 }
4912 if (!strncmp(s, "hwid=", 5)) {
4913 s += 5;
4914 len = scan_u8(s, &hwid, mask ? &hwid_mask : NULL);
4915 if (len == 0) {
4916 return 0;
4917 }
4918 s += len;
4919 }
4920
4921 if (!strncmp(s, ")", 1)) {
4922 s += 1;
4923 key->version = ver;
4924 key->u.md2.hwid = hwid;
4925 key->u.md2.dir = dir;
4926 if (mask) {
4927 mask->u.md2.hwid = hwid_mask;
4928 mask->u.md2.dir = dir_mask;
4929 }
4930 }
4931 return s - s_base;
4932 }
4933
4934 return 0;
4935 }
4936
4937 static int
4938 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
4939 {
4940 const char *s_base = s;
4941 struct geneve_opt *opt = key->d;
4942 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
4943 int len_remain = sizeof key->d;
4944 int len;
4945
4946 while (s[0] == '{' && len_remain >= sizeof *opt) {
4947 int data_len = 0;
4948
4949 s++;
4950 len_remain -= sizeof *opt;
4951
4952 if (!strncmp(s, "class=", 6)) {
4953 s += 6;
4954 len = scan_be16(s, &opt->opt_class,
4955 mask ? &opt_mask->opt_class : NULL);
4956 if (len == 0) {
4957 return 0;
4958 }
4959 s += len;
4960 } else if (mask) {
4961 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
4962 }
4963
4964 if (s[0] == ',') {
4965 s++;
4966 }
4967 if (!strncmp(s, "type=", 5)) {
4968 s += 5;
4969 len = scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
4970 if (len == 0) {
4971 return 0;
4972 }
4973 s += len;
4974 } else if (mask) {
4975 memset(&opt_mask->type, 0, sizeof opt_mask->type);
4976 }
4977
4978 if (s[0] == ',') {
4979 s++;
4980 }
4981 if (!strncmp(s, "len=", 4)) {
4982 uint8_t opt_len, opt_len_mask;
4983 s += 4;
4984 len = scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
4985 if (len == 0) {
4986 return 0;
4987 }
4988 s += len;
4989
4990 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
4991 return 0;
4992 }
4993 opt->length = opt_len / 4;
4994 if (mask) {
4995 opt_mask->length = opt_len_mask;
4996 }
4997 data_len = opt_len;
4998 } else if (mask) {
4999 memset(&opt_mask->type, 0, sizeof opt_mask->type);
5000 }
5001
5002 if (s[0] == ',') {
5003 s++;
5004 }
5005 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
5006 return 0;
5007 }
5008
5009 if (mask) {
5010 if (s[0] == '/') {
5011 s++;
5012 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
5013 data_len, (char **)&s)) {
5014 return 0;
5015 }
5016 }
5017 opt_mask->r1 = 0;
5018 opt_mask->r2 = 0;
5019 opt_mask->r3 = 0;
5020 }
5021
5022 if (s[0] == '}') {
5023 s++;
5024 opt += 1 + data_len / 4;
5025 if (mask) {
5026 opt_mask += 1 + data_len / 4;
5027 }
5028 len_remain -= data_len;
5029 } else {
5030 return 0;
5031 }
5032 }
5033
5034 if (s[0] == ')') {
5035 len = sizeof key->d - len_remain;
5036
5037 s++;
5038 key->len = len;
5039 if (mask) {
5040 mask->len = len;
5041 }
5042 return s - s_base;
5043 }
5044
5045 return 0;
5046 }
5047
5048 static void
5049 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
5050 {
5051 const uint16_t *flags = data_;
5052
5053 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
5054 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
5055 }
5056 if (*flags & FLOW_TNL_F_CSUM) {
5057 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
5058 }
5059 if (*flags & FLOW_TNL_F_OAM) {
5060 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
5061 }
5062 }
5063
5064 static void
5065 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
5066 {
5067 const uint32_t *gbp = data_;
5068
5069 if (*gbp) {
5070 size_t vxlan_opts_ofs;
5071
5072 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
5073 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
5074 nl_msg_end_nested(a, vxlan_opts_ofs);
5075 }
5076 }
5077
5078 static void
5079 geneve_to_attr(struct ofpbuf *a, const void *data_)
5080 {
5081 const struct geneve_scan *geneve = data_;
5082
5083 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
5084 geneve->len);
5085 }
5086
5087 static void
5088 erspan_to_attr(struct ofpbuf *a, const void *data_)
5089 {
5090 const struct erspan_metadata *md = data_;
5091
5092 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, md,
5093 sizeof *md);
5094 }
5095
5096 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5097 { \
5098 unsigned long call_fn = (unsigned long)FUNC; \
5099 if (call_fn) { \
5100 typedef void (*fn)(struct ofpbuf *, const void *); \
5101 fn func = FUNC; \
5102 func(BUF, &(DATA)); \
5103 } else { \
5104 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5105 } \
5106 }
5107
5108 #define SCAN_IF(NAME) \
5109 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5110 const char *start = s; \
5111 int len; \
5112 \
5113 s += strlen(NAME)
5114
5115 /* Usually no special initialization is needed. */
5116 #define SCAN_BEGIN(NAME, TYPE) \
5117 SCAN_IF(NAME); \
5118 TYPE skey, smask; \
5119 memset(&skey, 0, sizeof skey); \
5120 memset(&smask, 0, sizeof smask); \
5121 do { \
5122 len = 0;
5123
5124 /* Init as fully-masked as mask will not be scanned. */
5125 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5126 SCAN_IF(NAME); \
5127 TYPE skey, smask; \
5128 memset(&skey, 0, sizeof skey); \
5129 memset(&smask, 0xff, sizeof smask); \
5130 do { \
5131 len = 0;
5132
5133 /* VLAN needs special initialization. */
5134 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5135 SCAN_IF(NAME); \
5136 TYPE skey = KEY_INIT; \
5137 TYPE smask = MASK_INIT; \
5138 do { \
5139 len = 0;
5140
5141 /* Scan unnamed entry as 'TYPE' */
5142 #define SCAN_TYPE(TYPE, KEY, MASK) \
5143 len = scan_##TYPE(s, KEY, MASK); \
5144 if (len == 0) { \
5145 return -EINVAL; \
5146 } \
5147 s += len
5148
5149 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5150 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5151 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5152 s += strlen(NAME); \
5153 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5154 continue; \
5155 }
5156
5157 #define SCAN_FINISH() \
5158 } while (*s++ == ',' && len != 0); \
5159 if (s[-1] != ')') { \
5160 return -EINVAL; \
5161 }
5162
5163 #define SCAN_FINISH_SINGLE() \
5164 } while (false); \
5165 if (*s++ != ')') { \
5166 return -EINVAL; \
5167 }
5168
5169 /* Beginning of nested attribute. */
5170 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5171 SCAN_IF(NAME); \
5172 size_t key_offset, mask_offset; \
5173 key_offset = nl_msg_start_nested(key, ATTR); \
5174 if (mask) { \
5175 mask_offset = nl_msg_start_nested(mask, ATTR); \
5176 } \
5177 do { \
5178 len = 0;
5179
5180 #define SCAN_END_NESTED() \
5181 SCAN_FINISH(); \
5182 nl_msg_end_nested(key, key_offset); \
5183 if (mask) { \
5184 nl_msg_end_nested(mask, mask_offset); \
5185 } \
5186 return s - start; \
5187 }
5188
5189 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5190 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5191 TYPE skey, smask; \
5192 memset(&skey, 0, sizeof skey); \
5193 memset(&smask, 0xff, sizeof smask); \
5194 s += strlen(NAME); \
5195 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5196 SCAN_PUT(ATTR, FUNC); \
5197 continue; \
5198 }
5199
5200 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5201 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5202
5203 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5204 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5205
5206 #define SCAN_PUT(ATTR, FUNC) \
5207 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5208 if (mask) \
5209 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5210
5211 #define SCAN_END(ATTR) \
5212 SCAN_FINISH(); \
5213 SCAN_PUT(ATTR, NULL); \
5214 return s - start; \
5215 }
5216
5217 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5218 SCAN_IF(NAME); \
5219 TYPE skey[CNT], smask[CNT]; \
5220 memset(&skey, 0, sizeof skey); \
5221 memset(&smask, 0, sizeof smask); \
5222 int idx = 0, cnt = CNT; \
5223 uint64_t fields = 0; \
5224 do { \
5225 int field = 0; \
5226 len = 0;
5227
5228 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5229 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5230 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5231 if (fields & (1UL << field)) { \
5232 fields = 0; \
5233 if (++idx == cnt) { \
5234 break; \
5235 } \
5236 } \
5237 s += strlen(NAME); \
5238 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5239 fields |= 1UL << field; \
5240 continue; \
5241 } \
5242 field++;
5243
5244 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5245 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5246
5247 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5248 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5249 if (mask) { \
5250 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5251 }
5252
5253 #define SCAN_END_ARRAY(ATTR) \
5254 SCAN_FINISH(); \
5255 if (idx == cnt) { \
5256 return -EINVAL; \
5257 } \
5258 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5259 return s - start; \
5260 }
5261
5262 #define SCAN_END_SINGLE(ATTR) \
5263 SCAN_FINISH_SINGLE(); \
5264 SCAN_PUT(ATTR, NULL); \
5265 return s - start; \
5266 }
5267
5268 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5269 SCAN_BEGIN(NAME, TYPE) { \
5270 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5271 } SCAN_END_SINGLE(ATTR)
5272
5273 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5274 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5275 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5276 } SCAN_END_SINGLE(ATTR)
5277
5278 /* scan_port needs one extra argument. */
5279 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5280 SCAN_BEGIN(NAME, TYPE) { \
5281 len = scan_port(s, &skey, &smask, \
5282 context->port_names); \
5283 if (len == 0) { \
5284 return -EINVAL; \
5285 } \
5286 s += len; \
5287 } SCAN_END_SINGLE(ATTR)
5288
5289 static int
5290 parse_odp_nsh_key_mask_attr(const char *s, struct ofpbuf *key,
5291 struct ofpbuf *mask)
5292 {
5293 if (strncmp(s, "nsh(", 4) == 0) {
5294 const char *start = s;
5295 int len;
5296 struct ovs_key_nsh skey, smask;
5297 uint32_t spi = 0, spi_mask = 0;
5298 uint8_t si = 0, si_mask = 0;
5299
5300 s += 4;
5301
5302 memset(&skey, 0, sizeof skey);
5303 memset(&smask, 0, sizeof smask);
5304 do {
5305 len = 0;
5306
5307 if (strncmp(s, "flags=", 6) == 0) {
5308 s += 6;
5309 len = scan_u8(s, &skey.flags, mask ? &smask.flags : NULL);
5310 if (len == 0) {
5311 return -EINVAL;
5312 }
5313 s += len;
5314 continue;
5315 }
5316
5317 if (strncmp(s, "mdtype=", 7) == 0) {
5318 s += 7;
5319 len = scan_u8(s, &skey.mdtype, mask ? &smask.mdtype : NULL);
5320 if (len == 0) {
5321 return -EINVAL;
5322 }
5323 s += len;
5324 continue;
5325 }
5326
5327 if (strncmp(s, "np=", 3) == 0) {
5328 s += 3;
5329 len = scan_u8(s, &skey.np, mask ? &smask.np : NULL);
5330 if (len == 0) {
5331 return -EINVAL;
5332 }
5333 s += len;
5334 continue;
5335 }
5336
5337 if (strncmp(s, "spi=", 4) == 0) {
5338 s += 4;
5339 len = scan_u32(s, &spi, mask ? &spi_mask : NULL);
5340 if (len == 0) {
5341 return -EINVAL;
5342 }
5343 s += len;
5344 continue;
5345 }
5346
5347 if (strncmp(s, "si=", 3) == 0) {
5348 s += 3;
5349 len = scan_u8(s, &si, mask ? &si_mask : NULL);
5350 if (len == 0) {
5351 return -EINVAL;
5352 }
5353 s += len;
5354 continue;
5355 }
5356
5357 if (strncmp(s, "c1=", 3) == 0) {
5358 s += 3;
5359 len = scan_be32(s, &skey.context[0],
5360 mask ? &smask.context[0] : NULL);
5361 if (len == 0) {
5362 return -EINVAL;
5363 }
5364 s += len;
5365 continue;
5366 }
5367
5368 if (strncmp(s, "c2=", 3) == 0) {
5369 s += 3;
5370 len = scan_be32(s, &skey.context[1],
5371 mask ? &smask.context[1] : NULL);
5372 if (len == 0) {
5373 return -EINVAL;
5374 }
5375 s += len;
5376 continue;
5377 }
5378
5379 if (strncmp(s, "c3=", 3) == 0) {
5380 s += 3;
5381 len = scan_be32(s, &skey.context[2],
5382 mask ? &smask.context[2] : NULL);
5383 if (len == 0) {
5384 return -EINVAL;
5385 }
5386 s += len;
5387 continue;
5388 }
5389
5390 if (strncmp(s, "c4=", 3) == 0) {
5391 s += 3;
5392 len = scan_be32(s, &skey.context[3],
5393 mask ? &smask.context[3] : NULL);
5394 if (len == 0) {
5395 return -EINVAL;
5396 }
5397 s += len;
5398 continue;
5399 }
5400 } while (*s++ == ',' && len != 0);
5401 if (s[-1] != ')') {
5402 return -EINVAL;
5403 }
5404
5405 skey.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
5406 smask.path_hdr = nsh_spi_si_to_path_hdr(spi_mask, si_mask);
5407
5408 nsh_key_to_attr(key, &skey, NULL, 0, false);
5409 if (mask) {
5410 nsh_key_to_attr(mask, &smask, NULL, 0, true);
5411 }
5412 return s - start;
5413 }
5414 return 0;
5415 }
5416
5417 static int
5418 parse_odp_key_mask_attr(struct parse_odp_context *context, const char *s,
5419 struct ofpbuf *key, struct ofpbuf *mask)
5420 {
5421 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
5422 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
5423 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
5424 OVS_KEY_ATTR_RECIRC_ID);
5425 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
5426
5427 SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
5428 SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
5429 SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
5430 SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
5431
5432 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) {
5433 SCAN_FIELD("src=", ipv4, ipv4_src);
5434 SCAN_FIELD("dst=", ipv4, ipv4_dst);
5435 SCAN_FIELD("proto=", u8, ipv4_proto);
5436 SCAN_FIELD("tp_src=", be16, src_port);
5437 SCAN_FIELD("tp_dst=", be16, dst_port);
5438 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
5439
5440 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) {
5441 SCAN_FIELD("src=", in6_addr, ipv6_src);
5442 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
5443 SCAN_FIELD("proto=", u8, ipv6_proto);
5444 SCAN_FIELD("tp_src=", be16, src_port);
5445 SCAN_FIELD("tp_dst=", be16, dst_port);
5446 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
5447
5448 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
5449 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
5450 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
5451 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
5452 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
5453 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
5454 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
5455 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
5456 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
5457 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
5458 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata, erspan_metadata,
5459 erspan_to_attr);
5460 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
5461 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
5462 geneve_to_attr);
5463 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
5464 } SCAN_END_NESTED();
5465
5466 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
5467
5468 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
5469 SCAN_FIELD("src=", eth, eth_src);
5470 SCAN_FIELD("dst=", eth, eth_dst);
5471 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
5472
5473 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
5474 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
5475 SCAN_FIELD("vid=", vid, tci);
5476 SCAN_FIELD("pcp=", pcp, tci);
5477 SCAN_FIELD("cfi=", cfi, tci);
5478 } SCAN_END(OVS_KEY_ATTR_VLAN);
5479
5480 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
5481
5482 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
5483 SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
5484 SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
5485 SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
5486 SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
5487 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
5488
5489 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
5490 SCAN_FIELD("src=", ipv4, ipv4_src);
5491 SCAN_FIELD("dst=", ipv4, ipv4_dst);
5492 SCAN_FIELD("proto=", u8, ipv4_proto);
5493 SCAN_FIELD("tos=", u8, ipv4_tos);
5494 SCAN_FIELD("ttl=", u8, ipv4_ttl);
5495 SCAN_FIELD("frag=", frag, ipv4_frag);
5496 } SCAN_END(OVS_KEY_ATTR_IPV4);
5497
5498 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
5499 SCAN_FIELD("src=", in6_addr, ipv6_src);
5500 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
5501 SCAN_FIELD("label=", ipv6_label, ipv6_label);
5502 SCAN_FIELD("proto=", u8, ipv6_proto);
5503 SCAN_FIELD("tclass=", u8, ipv6_tclass);
5504 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
5505 SCAN_FIELD("frag=", frag, ipv6_frag);
5506 } SCAN_END(OVS_KEY_ATTR_IPV6);
5507
5508 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
5509 SCAN_FIELD("src=", be16, tcp_src);
5510 SCAN_FIELD("dst=", be16, tcp_dst);
5511 } SCAN_END(OVS_KEY_ATTR_TCP);
5512
5513 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
5514
5515 SCAN_BEGIN("udp(", struct ovs_key_udp) {
5516 SCAN_FIELD("src=", be16, udp_src);
5517 SCAN_FIELD("dst=", be16, udp_dst);
5518 } SCAN_END(OVS_KEY_ATTR_UDP);
5519
5520 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
5521 SCAN_FIELD("src=", be16, sctp_src);
5522 SCAN_FIELD("dst=", be16, sctp_dst);
5523 } SCAN_END(OVS_KEY_ATTR_SCTP);
5524
5525 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
5526 SCAN_FIELD("type=", u8, icmp_type);
5527 SCAN_FIELD("code=", u8, icmp_code);
5528 } SCAN_END(OVS_KEY_ATTR_ICMP);
5529
5530 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
5531 SCAN_FIELD("type=", u8, icmpv6_type);
5532 SCAN_FIELD("code=", u8, icmpv6_code);
5533 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
5534
5535 SCAN_BEGIN("arp(", struct ovs_key_arp) {
5536 SCAN_FIELD("sip=", ipv4, arp_sip);
5537 SCAN_FIELD("tip=", ipv4, arp_tip);
5538 SCAN_FIELD("op=", be16, arp_op);
5539 SCAN_FIELD("sha=", eth, arp_sha);
5540 SCAN_FIELD("tha=", eth, arp_tha);
5541 } SCAN_END(OVS_KEY_ATTR_ARP);
5542
5543 SCAN_BEGIN("nd(", struct ovs_key_nd) {
5544 SCAN_FIELD("target=", in6_addr, nd_target);
5545 SCAN_FIELD("sll=", eth, nd_sll);
5546 SCAN_FIELD("tll=", eth, nd_tll);
5547 } SCAN_END(OVS_KEY_ATTR_ND);
5548
5549 struct packet_type {
5550 ovs_be16 ns;
5551 ovs_be16 id;
5552 };
5553 SCAN_BEGIN("packet_type(", struct packet_type) {
5554 SCAN_FIELD("ns=", be16, ns);
5555 SCAN_FIELD("id=", be16, id);
5556 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE);
5557
5558 /* nsh is nested, it needs special process */
5559 int ret = parse_odp_nsh_key_mask_attr(s, key, mask);
5560 if (ret < 0) {
5561 return ret;
5562 } else {
5563 s += ret;
5564 }
5565
5566 /* Encap open-coded. */
5567 if (!strncmp(s, "encap(", 6)) {
5568 const char *start = s;
5569 size_t encap, encap_mask = 0;
5570
5571 if (context->depth + 1 == MAX_ODP_NESTED) {
5572 return -EINVAL;
5573 }
5574 context->depth++;
5575
5576 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
5577 if (mask) {
5578 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
5579 }
5580
5581 s += 6;
5582 for (;;) {
5583 int retval;
5584
5585 s += strspn(s, delimiters);
5586 if (!*s) {
5587 context->depth--;
5588 return -EINVAL;
5589 } else if (*s == ')') {
5590 break;
5591 }
5592
5593 retval = parse_odp_key_mask_attr(context, s, key, mask);
5594 if (retval < 0) {
5595 context->depth--;
5596 return retval;
5597 }
5598 s += retval;
5599 }
5600 s++;
5601
5602 nl_msg_end_nested(key, encap);
5603 if (mask) {
5604 nl_msg_end_nested(mask, encap_mask);
5605 }
5606 context->depth--;
5607
5608 return s - start;
5609 }
5610
5611 return -EINVAL;
5612 }
5613
5614 /* Parses the string representation of a datapath flow key, in the
5615 * format output by odp_flow_key_format(). Returns 0 if successful,
5616 * otherwise a positive errno value. On success, the flow key is
5617 * appended to 'key' as a series of Netlink attributes. On failure, no
5618 * data is appended to 'key'. Either way, 'key''s data might be
5619 * reallocated.
5620 *
5621 * If 'port_names' is nonnull, it points to an simap that maps from a port name
5622 * to a port number. (Port names may be used instead of port numbers in
5623 * in_port.)
5624 *
5625 * On success, the attributes appended to 'key' are individually syntactically
5626 * valid, but they may not be valid as a sequence. 'key' might, for example,
5627 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
5628 int
5629 odp_flow_from_string(const char *s, const struct simap *port_names,
5630 struct ofpbuf *key, struct ofpbuf *mask)
5631 {
5632 const size_t old_size = key->size;
5633 struct parse_odp_context context = (struct parse_odp_context) {
5634 .port_names = port_names,
5635 };
5636 for (;;) {
5637 int retval;
5638
5639 s += strspn(s, delimiters);
5640 if (!*s) {
5641 return 0;
5642 }
5643
5644 /* Skip UFID. */
5645 ovs_u128 ufid;
5646 retval = odp_ufid_from_string(s, &ufid);
5647 if (retval < 0) {
5648 key->size = old_size;
5649 return -retval;
5650 } else if (retval > 0) {
5651 s += retval;
5652 s += s[0] == ' ' ? 1 : 0;
5653 }
5654
5655 retval = parse_odp_key_mask_attr(&context, s, key, mask);
5656 if (retval < 0) {
5657 key->size = old_size;
5658 return -retval;
5659 }
5660 s += retval;
5661 }
5662
5663 return 0;
5664 }
5665
5666 static uint8_t
5667 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
5668 {
5669 if (is_mask) {
5670 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
5671 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
5672 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
5673 * must use a zero mask for the netlink frag field, and all ones mask
5674 * otherwise. */
5675 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
5676 }
5677 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
5678 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
5679 : OVS_FRAG_TYPE_FIRST;
5680 }
5681
5682 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
5683 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
5684 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
5685 bool is_mask);
5686 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
5687 bool is_mask);
5688 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
5689 bool is_mask);
5690 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
5691 bool is_mask);
5692 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
5693 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
5694 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
5695 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
5696 static void get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh,
5697 bool is_mask);
5698 static void put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow,
5699 bool is_mask);
5700
5701 /* These share the same layout. */
5702 union ovs_key_tp {
5703 struct ovs_key_tcp tcp;
5704 struct ovs_key_udp udp;
5705 struct ovs_key_sctp sctp;
5706 };
5707
5708 static void get_tp_key(const struct flow *, union ovs_key_tp *);
5709 static void put_tp_key(const union ovs_key_tp *, struct flow *);
5710
5711 static void
5712 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
5713 bool export_mask, struct ofpbuf *buf)
5714 {
5715 struct ovs_key_ethernet *eth_key;
5716 size_t encap[FLOW_MAX_VLAN_HEADERS] = {0};
5717 size_t max_vlans;
5718 const struct flow *flow = parms->flow;
5719 const struct flow *mask = parms->mask;
5720 const struct flow *data = export_mask ? mask : flow;
5721
5722 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
5723
5724 if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) {
5725 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
5726 parms->key_buf, NULL);
5727 }
5728
5729 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
5730
5731 if (parms->support.ct_state) {
5732 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
5733 ovs_to_odp_ct_state(data->ct_state));
5734 }
5735 if (parms->support.ct_zone) {
5736 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
5737 }
5738 if (parms->support.ct_mark) {
5739 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
5740 }
5741 if (parms->support.ct_label) {
5742 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
5743 sizeof(data->ct_label));
5744 }
5745 if (flow->ct_nw_proto) {
5746 if (parms->support.ct_orig_tuple
5747 && flow->dl_type == htons(ETH_TYPE_IP)) {
5748 struct ovs_key_ct_tuple_ipv4 ct = {
5749 data->ct_nw_src,
5750 data->ct_nw_dst,
5751 data->ct_tp_src,
5752 data->ct_tp_dst,
5753 data->ct_nw_proto,
5754 };
5755 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, &ct,
5756 sizeof ct);
5757 } else if (parms->support.ct_orig_tuple6
5758 && flow->dl_type == htons(ETH_TYPE_IPV6)) {
5759 struct ovs_key_ct_tuple_ipv6 ct = {
5760 data->ct_ipv6_src,
5761 data->ct_ipv6_dst,
5762 data->ct_tp_src,
5763 data->ct_tp_dst,
5764 data->ct_nw_proto,
5765 };
5766 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, &ct,
5767 sizeof ct);
5768 }
5769 }
5770 if (parms->support.recirc) {
5771 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
5772 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
5773 }
5774
5775 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
5776 * is not the magical value "ODPP_NONE". */
5777 if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
5778 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
5779 }
5780
5781 nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type);
5782
5783 if (OVS_UNLIKELY(parms->probe)) {
5784 max_vlans = FLOW_MAX_VLAN_HEADERS;
5785 } else {
5786 max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit);
5787 }
5788
5789 /* Conditionally add L2 attributes for Ethernet packets */
5790 if (flow->packet_type == htonl(PT_ETH)) {
5791 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
5792 sizeof *eth_key);
5793 get_ethernet_key(data, eth_key);
5794
5795 for (int encaps = 0; encaps < max_vlans; encaps++) {
5796 ovs_be16 tpid = flow->vlans[encaps].tpid;
5797
5798 if (flow->vlans[encaps].tci == htons(0)) {
5799 if (eth_type_vlan(flow->dl_type)) {
5800 /* If VLAN was truncated the tpid is in dl_type */
5801 tpid = flow->dl_type;
5802 } else {
5803 break;
5804 }
5805 }
5806
5807 if (export_mask) {
5808 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
5809 } else {
5810 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid);
5811 }
5812 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci);
5813 encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
5814 if (flow->vlans[encaps].tci == htons(0)) {
5815 goto unencap;
5816 }
5817 }
5818 }
5819
5820 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
5821 /* For backwards compatibility with kernels that don't support
5822 * wildcarding, the following convention is used to encode the
5823 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
5824 *
5825 * key mask matches
5826 * -------- -------- -------
5827 * >0x5ff 0xffff Specified Ethernet II Ethertype.
5828 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
5829 * <none> 0xffff Any non-Ethernet II frame (except valid
5830 * 802.3 SNAP packet with valid eth_type).
5831 */
5832 if (export_mask) {
5833 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
5834 }
5835 goto unencap;
5836 }
5837
5838 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
5839
5840 if (eth_type_vlan(flow->dl_type)) {
5841 goto unencap;
5842 }
5843
5844 if (flow->dl_type == htons(ETH_TYPE_IP)) {
5845 struct ovs_key_ipv4 *ipv4_key;
5846
5847 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
5848 sizeof *ipv4_key);
5849 get_ipv4_key(data, ipv4_key, export_mask);
5850 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
5851 struct ovs_key_ipv6 *ipv6_key;
5852
5853 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
5854 sizeof *ipv6_key);
5855 get_ipv6_key(data, ipv6_key, export_mask);
5856 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
5857 flow->dl_type == htons(ETH_TYPE_RARP)) {
5858 struct ovs_key_arp *arp_key;
5859
5860 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
5861 sizeof *arp_key);
5862 get_arp_key(data, arp_key);
5863 } else if (eth_type_mpls(flow->dl_type)) {
5864 struct ovs_key_mpls *mpls_key;
5865 int i, n;
5866
5867 n = flow_count_mpls_labels(flow, NULL);
5868 if (export_mask) {
5869 n = MIN(n, parms->support.max_mpls_depth);
5870 }
5871 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
5872 n * sizeof *mpls_key);
5873 for (i = 0; i < n; i++) {
5874 mpls_key[i].mpls_lse = data->mpls_lse[i];
5875 }
5876 } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
5877 nsh_key_to_attr(buf, &data->nsh, NULL, 0, export_mask);
5878 }
5879
5880 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5881 if (flow->nw_proto == IPPROTO_TCP) {
5882 union ovs_key_tp *tcp_key;
5883
5884 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
5885 sizeof *tcp_key);
5886 get_tp_key(data, tcp_key);
5887 if (data->tcp_flags || (mask && mask->tcp_flags)) {
5888 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
5889 }
5890 } else if (flow->nw_proto == IPPROTO_UDP) {
5891 union ovs_key_tp *udp_key;
5892
5893 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
5894 sizeof *udp_key);
5895 get_tp_key(data, udp_key);
5896 } else if (flow->nw_proto == IPPROTO_SCTP) {
5897 union ovs_key_tp *sctp_key;
5898
5899 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
5900 sizeof *sctp_key);
5901 get_tp_key(data, sctp_key);
5902 } else if (flow->dl_type == htons(ETH_TYPE_IP)
5903 && flow->nw_proto == IPPROTO_ICMP) {
5904 struct ovs_key_icmp *icmp_key;
5905
5906 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
5907 sizeof *icmp_key);
5908 icmp_key->icmp_type = ntohs(data->tp_src);
5909 icmp_key->icmp_code = ntohs(data->tp_dst);
5910 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
5911 && flow->nw_proto == IPPROTO_ICMPV6) {
5912 struct ovs_key_icmpv6 *icmpv6_key;
5913
5914 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
5915 sizeof *icmpv6_key);
5916 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
5917 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
5918
5919 if (is_nd(flow, NULL)
5920 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
5921 * type and code are 8 bits wide. Therefore, an exact match
5922 * looks like htons(0xff), not htons(0xffff). See
5923 * xlate_wc_finish() for details. */
5924 && (!export_mask || (data->tp_src == htons(0xff)
5925 && data->tp_dst == htons(0xff)))) {
5926
5927 struct ovs_key_nd *nd_key;
5928
5929 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
5930 sizeof *nd_key);
5931 nd_key->nd_target = data->nd_target;
5932 nd_key->nd_sll = data->arp_sha;
5933 nd_key->nd_tll = data->arp_tha;
5934 }
5935 }
5936 }
5937
5938 unencap:
5939 for (int encaps = max_vlans - 1; encaps >= 0; encaps--) {
5940 if (encap[encaps]) {
5941 nl_msg_end_nested(buf, encap[encaps]);
5942 }
5943 }
5944 }
5945
5946 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
5947 *
5948 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5949 * capable of being expanded to allow for that much space. */
5950 void
5951 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
5952 struct ofpbuf *buf)
5953 {
5954 odp_flow_key_from_flow__(parms, false, buf);
5955 }
5956
5957 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
5958 * 'buf'.
5959 *
5960 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5961 * capable of being expanded to allow for that much space. */
5962 void
5963 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
5964 struct ofpbuf *buf)
5965 {
5966 odp_flow_key_from_flow__(parms, true, buf);
5967 }
5968
5969 /* Generate ODP flow key from the given packet metadata */
5970 void
5971 odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet)
5972 {
5973 const struct pkt_metadata *md = &packet->md;
5974
5975 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
5976
5977 if (flow_tnl_dst_is_set(&md->tunnel)) {
5978 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL, NULL);
5979 }
5980
5981 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
5982
5983 if (md->ct_state) {
5984 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
5985 ovs_to_odp_ct_state(md->ct_state));
5986 if (md->ct_zone) {
5987 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
5988 }
5989 if (md->ct_mark) {
5990 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
5991 }
5992 if (!ovs_u128_is_zero(md->ct_label)) {
5993 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
5994 sizeof(md->ct_label));
5995 }
5996 if (md->ct_orig_tuple_ipv6) {
5997 if (md->ct_orig_tuple.ipv6.ipv6_proto) {
5998 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
5999 &md->ct_orig_tuple.ipv6,
6000 sizeof md->ct_orig_tuple.ipv6);
6001 }
6002 } else {
6003 if (md->ct_orig_tuple.ipv4.ipv4_proto) {
6004 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
6005 &md->ct_orig_tuple.ipv4,
6006 sizeof md->ct_orig_tuple.ipv4);
6007 }
6008 }
6009 }
6010
6011 /* Add an ingress port attribute if 'odp_in_port' is not the magical
6012 * value "ODPP_NONE". */
6013 if (md->in_port.odp_port != ODPP_NONE) {
6014 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
6015 }
6016
6017 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6018 if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) {
6019 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE,
6020 pt_ns_type_be(packet->packet_type));
6021 }
6022 }
6023
6024 /* Generate packet metadata from the given ODP flow key. */
6025 void
6026 odp_key_to_dp_packet(const struct nlattr *key, size_t key_len,
6027 struct dp_packet *packet)
6028 {
6029 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6030 const struct nlattr *nla;
6031 struct pkt_metadata *md = &packet->md;
6032 ovs_be32 packet_type = htonl(PT_UNKNOWN);
6033 ovs_be16 ethertype = 0;
6034 size_t left;
6035
6036 pkt_metadata_init(md, ODPP_NONE);
6037
6038 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
6039 enum ovs_key_attr type = nl_attr_type(nla);
6040 size_t len = nl_attr_get_size(nla);
6041 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
6042 OVS_KEY_ATTR_MAX, type);
6043
6044 if (len != expected_len && expected_len >= 0) {
6045 continue;
6046 }
6047
6048 switch (type) {
6049 case OVS_KEY_ATTR_RECIRC_ID:
6050 md->recirc_id = nl_attr_get_u32(nla);
6051 break;
6052 case OVS_KEY_ATTR_DP_HASH:
6053 md->dp_hash = nl_attr_get_u32(nla);
6054 break;
6055 case OVS_KEY_ATTR_PRIORITY:
6056 md->skb_priority = nl_attr_get_u32(nla);
6057 break;
6058 case OVS_KEY_ATTR_SKB_MARK:
6059 md->pkt_mark = nl_attr_get_u32(nla);
6060 break;
6061 case OVS_KEY_ATTR_CT_STATE:
6062 md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
6063 break;
6064 case OVS_KEY_ATTR_CT_ZONE:
6065 md->ct_zone = nl_attr_get_u16(nla);
6066 break;
6067 case OVS_KEY_ATTR_CT_MARK:
6068 md->ct_mark = nl_attr_get_u32(nla);
6069 break;
6070 case OVS_KEY_ATTR_CT_LABELS: {
6071 md->ct_label = nl_attr_get_u128(nla);
6072 break;
6073 }
6074 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
6075 const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla);
6076 md->ct_orig_tuple.ipv4 = *ct;
6077 md->ct_orig_tuple_ipv6 = false;
6078 break;
6079 }
6080 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
6081 const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla);
6082
6083 md->ct_orig_tuple.ipv6 = *ct;
6084 md->ct_orig_tuple_ipv6 = true;
6085 break;
6086 }
6087 case OVS_KEY_ATTR_TUNNEL: {
6088 enum odp_key_fitness res;
6089
6090 res = odp_tun_key_from_attr(nla, &md->tunnel);
6091 if (res == ODP_FIT_ERROR) {
6092 memset(&md->tunnel, 0, sizeof md->tunnel);
6093 }
6094 break;
6095 }
6096 case OVS_KEY_ATTR_IN_PORT:
6097 md->in_port.odp_port = nl_attr_get_odp_port(nla);
6098 break;
6099 case OVS_KEY_ATTR_ETHERNET:
6100 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6101 packet_type = htonl(PT_ETH);
6102 break;
6103 case OVS_KEY_ATTR_ETHERTYPE:
6104 ethertype = nl_attr_get_be16(nla);
6105 break;
6106 case OVS_KEY_ATTR_UNSPEC:
6107 case OVS_KEY_ATTR_ENCAP:
6108 case OVS_KEY_ATTR_VLAN:
6109 case OVS_KEY_ATTR_IPV4:
6110 case OVS_KEY_ATTR_IPV6:
6111 case OVS_KEY_ATTR_TCP:
6112 case OVS_KEY_ATTR_UDP:
6113 case OVS_KEY_ATTR_ICMP:
6114 case OVS_KEY_ATTR_ICMPV6:
6115 case OVS_KEY_ATTR_ARP:
6116 case OVS_KEY_ATTR_ND:
6117 case OVS_KEY_ATTR_SCTP:
6118 case OVS_KEY_ATTR_TCP_FLAGS:
6119 case OVS_KEY_ATTR_MPLS:
6120 case OVS_KEY_ATTR_PACKET_TYPE:
6121 case OVS_KEY_ATTR_NSH:
6122 case __OVS_KEY_ATTR_MAX:
6123 default:
6124 break;
6125 }
6126 }
6127
6128 if (packet_type == htonl(PT_ETH)) {
6129 packet->packet_type = htonl(PT_ETH);
6130 } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) {
6131 packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
6132 ntohs(ethertype));
6133 } else {
6134 VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type.");
6135 }
6136 }
6137
6138 uint32_t
6139 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
6140 {
6141 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
6142 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key), key_len, 0);
6143 }
6144
6145 static void
6146 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
6147 uint64_t attrs, int out_of_range_attr,
6148 const struct nlattr *key, size_t key_len)
6149 {
6150 struct ds s;
6151 int i;
6152
6153 if (VLOG_DROP_DBG(rl)) {
6154 return;
6155 }
6156
6157 ds_init(&s);
6158 for (i = 0; i < 64; i++) {
6159 if (attrs & (UINT64_C(1) << i)) {
6160 char namebuf[OVS_KEY_ATTR_BUFSIZE];
6161
6162 ds_put_format(&s, " %s",
6163 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
6164 }
6165 }
6166 if (out_of_range_attr) {
6167 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
6168 }
6169
6170 ds_put_cstr(&s, ": ");
6171 odp_flow_key_format(key, key_len, &s);
6172
6173 VLOG_DBG("%s:%s", title, ds_cstr(&s));
6174 ds_destroy(&s);
6175 }
6176
6177 static uint8_t
6178 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
6179 {
6180 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6181
6182 if (is_mask) {
6183 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
6184 }
6185
6186 if (odp_frag > OVS_FRAG_TYPE_LATER) {
6187 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
6188 return 0xff; /* Error. */
6189 }
6190
6191 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
6192 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
6193 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
6194 }
6195
6196 static bool
6197 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
6198 const struct nlattr *attrs[], uint64_t *present_attrsp,
6199 int *out_of_range_attrp)
6200 {
6201 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6202 const struct nlattr *nla;
6203 uint64_t present_attrs;
6204 size_t left;
6205
6206 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
6207 present_attrs = 0;
6208 *out_of_range_attrp = 0;
6209 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
6210 uint16_t type = nl_attr_type(nla);
6211 size_t len = nl_attr_get_size(nla);
6212 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
6213 OVS_KEY_ATTR_MAX, type);
6214
6215 if (len != expected_len && expected_len >= 0) {
6216 char namebuf[OVS_KEY_ATTR_BUFSIZE];
6217
6218 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
6219 "length %d", ovs_key_attr_to_string(type, namebuf,
6220 sizeof namebuf),
6221 len, expected_len);
6222 return false;
6223 }
6224
6225 if (type > OVS_KEY_ATTR_MAX) {
6226 *out_of_range_attrp = type;
6227 } else {
6228 if (present_attrs & (UINT64_C(1) << type)) {
6229 char namebuf[OVS_KEY_ATTR_BUFSIZE];
6230
6231 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
6232 ovs_key_attr_to_string(type,
6233 namebuf, sizeof namebuf));
6234 return false;
6235 }
6236
6237 present_attrs |= UINT64_C(1) << type;
6238 attrs[type] = nla;
6239 }
6240 }
6241 if (left) {
6242 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
6243 return false;
6244 }
6245
6246 *present_attrsp = present_attrs;
6247 return true;
6248 }
6249
6250 static enum odp_key_fitness
6251 check_expectations(uint64_t present_attrs, int out_of_range_attr,
6252 uint64_t expected_attrs,
6253 const struct nlattr *key, size_t key_len)
6254 {
6255 uint64_t missing_attrs;
6256 uint64_t extra_attrs;
6257
6258 missing_attrs = expected_attrs & ~present_attrs;
6259 if (missing_attrs) {
6260 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6261 log_odp_key_attributes(&rl, "expected but not present",
6262 missing_attrs, 0, key, key_len);
6263 return ODP_FIT_TOO_LITTLE;
6264 }
6265
6266 extra_attrs = present_attrs & ~expected_attrs;
6267 if (extra_attrs || out_of_range_attr) {
6268 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6269 log_odp_key_attributes(&rl, "present but not expected",
6270 extra_attrs, out_of_range_attr, key, key_len);
6271 return ODP_FIT_TOO_MUCH;
6272 }
6273
6274 return ODP_FIT_PERFECT;
6275 }
6276
6277 static bool
6278 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
6279 uint64_t present_attrs, uint64_t *expected_attrs,
6280 struct flow *flow, const struct flow *src_flow)
6281 {
6282 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6283 bool is_mask = flow != src_flow;
6284
6285 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
6286 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
6287 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
6288 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
6289 ntohs(flow->dl_type));
6290 return false;
6291 }
6292 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
6293 flow->dl_type != htons(0xffff)) {
6294 return false;
6295 }
6296 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
6297 } else {
6298 if (!is_mask) {
6299 /* Default ethertype for well-known L3 packets. */
6300 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
6301 flow->dl_type = htons(ETH_TYPE_IP);
6302 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
6303 flow->dl_type = htons(ETH_TYPE_IPV6);
6304 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
6305 flow->dl_type = htons(ETH_TYPE_MPLS);
6306 } else {
6307 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
6308 }
6309 } else if (src_flow->packet_type != htonl(PT_ETH)) {
6310 /* dl_type is mandatory for non-Ethernet packets */
6311 flow->dl_type = htons(0xffff);
6312 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
6313 /* See comments in odp_flow_key_from_flow__(). */
6314 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
6315 return false;
6316 }
6317 }
6318 return true;
6319 }
6320
6321 static enum odp_key_fitness
6322 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
6323 uint64_t present_attrs, int out_of_range_attr,
6324 uint64_t expected_attrs, struct flow *flow,
6325 const struct nlattr *key, size_t key_len,
6326 const struct flow *src_flow)
6327 {
6328 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6329 bool is_mask = src_flow != flow;
6330 const void *check_start = NULL;
6331 size_t check_len = 0;
6332 enum ovs_key_attr expected_bit = 0xff;
6333
6334 if (eth_type_mpls(src_flow->dl_type)) {
6335 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
6336 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
6337 }
6338 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
6339 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
6340 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
6341 int n = size / sizeof(ovs_be32);
6342 int i;
6343
6344 if (!size || size % sizeof(ovs_be32)) {
6345 return ODP_FIT_ERROR;
6346 }
6347 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
6348 return ODP_FIT_ERROR;
6349 }
6350
6351 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
6352 flow->mpls_lse[i] = mpls_lse[i];
6353 }
6354 if (n > FLOW_MAX_MPLS_LABELS) {
6355 return ODP_FIT_TOO_MUCH;
6356 }
6357
6358 if (!is_mask) {
6359 /* BOS may be set only in the innermost label. */
6360 for (i = 0; i < n - 1; i++) {
6361 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
6362 return ODP_FIT_ERROR;
6363 }
6364 }
6365
6366 /* BOS must be set in the innermost label. */
6367 if (n < FLOW_MAX_MPLS_LABELS
6368 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
6369 return ODP_FIT_TOO_LITTLE;
6370 }
6371 }
6372 }
6373
6374 goto done;
6375 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
6376 if (!is_mask) {
6377 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
6378 }
6379 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
6380 const struct ovs_key_ipv4 *ipv4_key;
6381
6382 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
6383 put_ipv4_key(ipv4_key, flow, is_mask);
6384 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
6385 return ODP_FIT_ERROR;
6386 }
6387 if (is_mask) {
6388 check_start = ipv4_key;
6389 check_len = sizeof *ipv4_key;
6390 expected_bit = OVS_KEY_ATTR_IPV4;
6391 }
6392 }
6393 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
6394 if (!is_mask) {
6395 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
6396 }
6397 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
6398 const struct ovs_key_ipv6 *ipv6_key;
6399
6400 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
6401 put_ipv6_key(ipv6_key, flow, is_mask);
6402 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
6403 return ODP_FIT_ERROR;
6404 }
6405 if (is_mask) {
6406 check_start = ipv6_key;
6407 check_len = sizeof *ipv6_key;
6408 expected_bit = OVS_KEY_ATTR_IPV6;
6409 }
6410 }
6411 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
6412 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
6413 if (!is_mask) {
6414 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
6415 }
6416 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
6417 const struct ovs_key_arp *arp_key;
6418
6419 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
6420 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
6421 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
6422 "key", ntohs(arp_key->arp_op));
6423 return ODP_FIT_ERROR;
6424 }
6425 put_arp_key(arp_key, flow);
6426 if (is_mask) {
6427 check_start = arp_key;
6428 check_len = sizeof *arp_key;
6429 expected_bit = OVS_KEY_ATTR_ARP;
6430 }
6431 }
6432 } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) {
6433 if (!is_mask) {
6434 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH;
6435 }
6436 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) {
6437 odp_nsh_key_from_attr(attrs[OVS_KEY_ATTR_NSH], &flow->nsh, NULL);
6438 if (is_mask) {
6439 check_start = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]);
6440 check_len = nl_attr_get_size(attrs[OVS_KEY_ATTR_NSH]);
6441 expected_bit = OVS_KEY_ATTR_NSH;
6442 }
6443 }
6444 } else {
6445 goto done;
6446 }
6447 if (check_len > 0) { /* Happens only when 'is_mask'. */
6448 if (!is_all_zeros(check_start, check_len) &&
6449 flow->dl_type != htons(0xffff)) {
6450 return ODP_FIT_ERROR;
6451 } else {
6452 expected_attrs |= UINT64_C(1) << expected_bit;
6453 }
6454 }
6455
6456 expected_bit = OVS_KEY_ATTR_UNSPEC;
6457 if (src_flow->nw_proto == IPPROTO_TCP
6458 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
6459 src_flow->dl_type == htons(ETH_TYPE_IPV6))
6460 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6461 if (!is_mask) {
6462 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
6463 }
6464 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
6465 const union ovs_key_tp *tcp_key;
6466
6467 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
6468 put_tp_key(tcp_key, flow);
6469 expected_bit = OVS_KEY_ATTR_TCP;
6470 }
6471 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
6472 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
6473 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
6474 }
6475 } else if (src_flow->nw_proto == IPPROTO_UDP
6476 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
6477 src_flow->dl_type == htons(ETH_TYPE_IPV6))
6478 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6479 if (!is_mask) {
6480 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
6481 }
6482 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
6483 const union ovs_key_tp *udp_key;
6484
6485 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
6486 put_tp_key(udp_key, flow);
6487 expected_bit = OVS_KEY_ATTR_UDP;
6488 }
6489 } else if (src_flow->nw_proto == IPPROTO_SCTP
6490 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
6491 src_flow->dl_type == htons(ETH_TYPE_IPV6))
6492 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6493 if (!is_mask) {
6494 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
6495 }
6496 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
6497 const union ovs_key_tp *sctp_key;
6498
6499 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
6500 put_tp_key(sctp_key, flow);
6501 expected_bit = OVS_KEY_ATTR_SCTP;
6502 }
6503 } else if (src_flow->nw_proto == IPPROTO_ICMP
6504 && src_flow->dl_type == htons(ETH_TYPE_IP)
6505 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6506 if (!is_mask) {
6507 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
6508 }
6509 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
6510 const struct ovs_key_icmp *icmp_key;
6511
6512 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
6513 flow->tp_src = htons(icmp_key->icmp_type);
6514 flow->tp_dst = htons(icmp_key->icmp_code);
6515 expected_bit = OVS_KEY_ATTR_ICMP;
6516 }
6517 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
6518 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
6519 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6520 if (!is_mask) {
6521 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
6522 }
6523 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
6524 const struct ovs_key_icmpv6 *icmpv6_key;
6525
6526 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
6527 flow->tp_src = htons(icmpv6_key->icmpv6_type);
6528 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
6529 expected_bit = OVS_KEY_ATTR_ICMPV6;
6530 if (is_nd(src_flow, NULL)) {
6531 if (!is_mask) {
6532 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
6533 }
6534 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
6535 const struct ovs_key_nd *nd_key;
6536
6537 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
6538 flow->nd_target = nd_key->nd_target;
6539 flow->arp_sha = nd_key->nd_sll;
6540 flow->arp_tha = nd_key->nd_tll;
6541 if (is_mask) {
6542 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6543 * ICMP type and code are 8 bits wide. Therefore, an
6544 * exact match looks like htons(0xff), not
6545 * htons(0xffff). See xlate_wc_finish() for details.
6546 * */
6547 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
6548 (flow->tp_src != htons(0xff) ||
6549 flow->tp_dst != htons(0xff))) {
6550 return ODP_FIT_ERROR;
6551 } else {
6552 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
6553 }
6554 }
6555 }
6556 }
6557 }
6558 } else if (src_flow->nw_proto == IPPROTO_IGMP
6559 && src_flow->dl_type == htons(ETH_TYPE_IP)) {
6560 /* OVS userspace parses the IGMP type, code, and group, but its
6561 * datapaths do not, so there is always missing information. */
6562 return ODP_FIT_TOO_LITTLE;
6563 }
6564 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
6565 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
6566 return ODP_FIT_ERROR;
6567 } else {
6568 expected_attrs |= UINT64_C(1) << expected_bit;
6569 }
6570 }
6571
6572 done:
6573 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
6574 key, key_len);
6575 }
6576
6577 /* Parse 802.1Q header then encapsulated L3 attributes. */
6578 static enum odp_key_fitness
6579 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
6580 uint64_t present_attrs, int out_of_range_attr,
6581 uint64_t expected_attrs, struct flow *flow,
6582 const struct nlattr *key, size_t key_len,
6583 const struct flow *src_flow)
6584 {
6585 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6586 bool is_mask = src_flow != flow;
6587
6588 const struct nlattr *encap;
6589 enum odp_key_fitness encap_fitness;
6590 enum odp_key_fitness fitness = ODP_FIT_ERROR;
6591 int encaps = 0;
6592
6593 while (encaps < flow_vlan_limit &&
6594 (is_mask
6595 ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0
6596 : eth_type_vlan(flow->dl_type))) {
6597
6598 encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
6599 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
6600
6601 /* Calculate fitness of outer attributes. */
6602 if (!is_mask) {
6603 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
6604 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
6605 } else {
6606 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
6607 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
6608 }
6609 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
6610 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
6611 }
6612 }
6613 fitness = check_expectations(present_attrs, out_of_range_attr,
6614 expected_attrs, key, key_len);
6615
6616 /* Set vlan_tci.
6617 * Remove the TPID from dl_type since it's not the real Ethertype. */
6618 flow->vlans[encaps].tpid = flow->dl_type;
6619 flow->dl_type = htons(0);
6620 flow->vlans[encaps].tci =
6621 (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
6622 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
6623 : htons(0));
6624 if (!is_mask) {
6625 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) ||
6626 !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
6627 return ODP_FIT_TOO_LITTLE;
6628 } else if (flow->vlans[encaps].tci == htons(0)) {
6629 /* Corner case for a truncated 802.1Q header. */
6630 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
6631 return ODP_FIT_TOO_MUCH;
6632 }
6633 return fitness;
6634 } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) {
6635 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
6636 "but CFI bit is not set",
6637 ntohs(flow->vlans[encaps].tci));
6638 return ODP_FIT_ERROR;
6639 }
6640 } else {
6641 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
6642 return fitness;
6643 }
6644 }
6645
6646 /* Now parse the encapsulated attributes. */
6647 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
6648 attrs, &present_attrs, &out_of_range_attr)) {
6649 return ODP_FIT_ERROR;
6650 }
6651 expected_attrs = 0;
6652
6653 if (!parse_ethertype(attrs, present_attrs, &expected_attrs,
6654 flow, src_flow)) {
6655 return ODP_FIT_ERROR;
6656 }
6657
6658 encaps++;
6659 }
6660
6661 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
6662 expected_attrs, flow, key, key_len,
6663 src_flow);
6664
6665 /* The overall fitness is the worse of the outer and inner attributes. */
6666 return MAX(fitness, encap_fitness);
6667 }
6668
6669 static enum odp_key_fitness
6670 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
6671 struct flow *flow, const struct flow *src_flow)
6672 {
6673 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
6674 uint64_t expected_attrs;
6675 uint64_t present_attrs;
6676 int out_of_range_attr;
6677 bool is_mask = src_flow != flow;
6678
6679 memset(flow, 0, sizeof *flow);
6680
6681 /* Parse attributes. */
6682 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
6683 &out_of_range_attr)) {
6684 return ODP_FIT_ERROR;
6685 }
6686 expected_attrs = 0;
6687
6688 /* Metadata. */
6689 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
6690 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
6691 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
6692 } else if (is_mask) {
6693 /* Always exact match recirc_id if it is not specified. */
6694 flow->recirc_id = UINT32_MAX;
6695 }
6696
6697 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
6698 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
6699 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
6700 }
6701 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
6702 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
6703 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
6704 }
6705
6706 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
6707 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
6708 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
6709 }
6710
6711 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
6712 uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
6713
6714 flow->ct_state = odp_to_ovs_ct_state(odp_state);
6715 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
6716 }
6717 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
6718 flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
6719 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
6720 }
6721 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
6722 flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
6723 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
6724 }
6725 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
6726 flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]);
6727 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
6728 }
6729 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
6730 const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
6731 flow->ct_nw_src = ct->ipv4_src;
6732 flow->ct_nw_dst = ct->ipv4_dst;
6733 flow->ct_nw_proto = ct->ipv4_proto;
6734 flow->ct_tp_src = ct->src_port;
6735 flow->ct_tp_dst = ct->dst_port;
6736 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
6737 }
6738 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
6739 const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
6740
6741 flow->ct_ipv6_src = ct->ipv6_src;
6742 flow->ct_ipv6_dst = ct->ipv6_dst;
6743 flow->ct_nw_proto = ct->ipv6_proto;
6744 flow->ct_tp_src = ct->src_port;
6745 flow->ct_tp_dst = ct->dst_port;
6746 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
6747 }
6748
6749 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
6750 enum odp_key_fitness res;
6751
6752 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
6753 &flow->tunnel);
6754 if (res == ODP_FIT_ERROR) {
6755 return ODP_FIT_ERROR;
6756 } else if (res == ODP_FIT_PERFECT) {
6757 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
6758 }
6759 }
6760
6761 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
6762 flow->in_port.odp_port
6763 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
6764 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
6765 } else if (!is_mask) {
6766 flow->in_port.odp_port = ODPP_NONE;
6767 }
6768
6769 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) {
6770 flow->packet_type
6771 = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]);
6772 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE;
6773 } else if (!is_mask) {
6774 flow->packet_type = htonl(PT_ETH);
6775 }
6776
6777 /* Check for Ethernet header. */
6778 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
6779 const struct ovs_key_ethernet *eth_key;
6780
6781 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
6782 put_ethernet_key(eth_key, flow);
6783 if (!is_mask) {
6784 flow->packet_type = htonl(PT_ETH);
6785 }
6786 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
6787 }
6788 else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
6789 ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
6790 if (!is_mask) {
6791 flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
6792 ntohs(ethertype));
6793 }
6794 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
6795 }
6796
6797 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
6798 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
6799 src_flow)) {
6800 return ODP_FIT_ERROR;
6801 }
6802
6803 if (is_mask
6804 ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0
6805 : eth_type_vlan(src_flow->dl_type)) {
6806 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
6807 expected_attrs, flow, key, key_len, src_flow);
6808 }
6809 if (is_mask) {
6810 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
6811 flow->vlans[0].tpid = htons(0xffff);
6812 flow->vlans[0].tci = htons(0xffff);
6813 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
6814 flow->vlans[0].tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
6815 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
6816 }
6817 }
6818 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
6819 expected_attrs, flow, key, key_len, src_flow);
6820 }
6821
6822 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
6823 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
6824 * 'key' fits our expectations for what a flow key should contain.
6825 *
6826 * The 'in_port' will be the datapath's understanding of the port. The
6827 * caller will need to translate with odp_port_to_ofp_port() if the
6828 * OpenFlow port is needed.
6829 *
6830 * This function doesn't take the packet itself as an argument because none of
6831 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
6832 * it is always possible to infer which additional attribute(s) should appear
6833 * by looking at the attributes for lower-level protocols, e.g. if the network
6834 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
6835 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
6836 * must be absent. */
6837 enum odp_key_fitness
6838 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
6839 struct flow *flow)
6840 {
6841 return odp_flow_key_to_flow__(key, key_len, flow, flow);
6842 }
6843
6844 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
6845 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
6846 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
6847 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
6848 * well 'key' fits our expectations for what a flow key should contain. */
6849 enum odp_key_fitness
6850 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
6851 struct flow_wildcards *mask, const struct flow *src_flow)
6852 {
6853 if (mask_key_len) {
6854 return odp_flow_key_to_flow__(mask_key, mask_key_len,
6855 &mask->masks, src_flow);
6856
6857 } else {
6858 /* A missing mask means that the flow should be exact matched.
6859 * Generate an appropriate exact wildcard for the flow. */
6860 flow_wildcards_init_for_packet(mask, src_flow);
6861
6862 return ODP_FIT_PERFECT;
6863 }
6864 }
6865
6866 /* Converts the netlink formated key/mask to match.
6867 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
6868 * disagree on the acceptable form of flow */
6869 int
6870 parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len,
6871 const struct nlattr *mask, size_t mask_len,
6872 struct match *match)
6873 {
6874 enum odp_key_fitness fitness;
6875
6876 fitness = odp_flow_key_to_flow(key, key_len, &match->flow);
6877 if (fitness) {
6878 /* This should not happen: it indicates that
6879 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
6880 * the acceptable form of a flow. Log the problem as an error,
6881 * with enough details to enable debugging. */
6882 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6883
6884 if (!VLOG_DROP_ERR(&rl)) {
6885 struct ds s;
6886
6887 ds_init(&s);
6888 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
6889 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
6890 ds_destroy(&s);
6891 }
6892
6893 return EINVAL;
6894 }
6895
6896 fitness = odp_flow_key_to_mask(mask, mask_len, &match->wc, &match->flow);
6897 if (fitness) {
6898 /* This should not happen: it indicates that
6899 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
6900 * disagree on the acceptable form of a mask. Log the problem
6901 * as an error, with enough details to enable debugging. */
6902 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6903
6904 if (!VLOG_DROP_ERR(&rl)) {
6905 struct ds s;
6906
6907 ds_init(&s);
6908 odp_flow_format(key, key_len, mask, mask_len, NULL, &s,
6909 true);
6910 VLOG_ERR("internal error parsing flow mask %s (%s)",
6911 ds_cstr(&s), odp_key_fitness_to_string(fitness));
6912 ds_destroy(&s);
6913 }
6914
6915 return EINVAL;
6916 }
6917
6918 return 0;
6919 }
6920
6921 /* Returns 'fitness' as a string, for use in debug messages. */
6922 const char *
6923 odp_key_fitness_to_string(enum odp_key_fitness fitness)
6924 {
6925 switch (fitness) {
6926 case ODP_FIT_PERFECT:
6927 return "OK";
6928 case ODP_FIT_TOO_MUCH:
6929 return "too_much";
6930 case ODP_FIT_TOO_LITTLE:
6931 return "too_little";
6932 case ODP_FIT_ERROR:
6933 return "error";
6934 default:
6935 return "<unknown>";
6936 }
6937 }
6938
6939 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
6940 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
6941 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
6942 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
6943 * null, then the return value is not meaningful.) */
6944 size_t
6945 odp_put_userspace_action(uint32_t pid,
6946 const void *userdata, size_t userdata_size,
6947 odp_port_t tunnel_out_port,
6948 bool include_actions,
6949 struct ofpbuf *odp_actions)
6950 {
6951 size_t userdata_ofs;
6952 size_t offset;
6953
6954 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
6955 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
6956 if (userdata) {
6957 userdata_ofs = odp_actions->size + NLA_HDRLEN;
6958
6959 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
6960 * module before Linux 3.10 required the userdata to be exactly 8 bytes
6961 * long:
6962 *
6963 * - The kernel rejected shorter userdata with -ERANGE.
6964 *
6965 * - The kernel silently dropped userdata beyond the first 8 bytes.
6966 *
6967 * Thus, for maximum compatibility, always put at least 8 bytes. (We
6968 * separately disable features that required more than 8 bytes.) */
6969 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
6970 MAX(8, userdata_size)),
6971 userdata, userdata_size);
6972 } else {
6973 userdata_ofs = 0;
6974 }
6975 if (tunnel_out_port != ODPP_NONE) {
6976 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
6977 tunnel_out_port);
6978 }
6979 if (include_actions) {
6980 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
6981 }
6982 nl_msg_end_nested(odp_actions, offset);
6983
6984 return userdata_ofs;
6985 }
6986
6987 void
6988 odp_put_pop_eth_action(struct ofpbuf *odp_actions)
6989 {
6990 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH);
6991 }
6992
6993 void
6994 odp_put_push_eth_action(struct ofpbuf *odp_actions,
6995 const struct eth_addr *eth_src,
6996 const struct eth_addr *eth_dst)
6997 {
6998 struct ovs_action_push_eth eth;
6999
7000 memset(&eth, 0, sizeof eth);
7001 if (eth_src) {
7002 eth.addresses.eth_src = *eth_src;
7003 }
7004 if (eth_dst) {
7005 eth.addresses.eth_dst = *eth_dst;
7006 }
7007
7008 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH,
7009 &eth, sizeof eth);
7010 }
7011
7012 void
7013 odp_put_tunnel_action(const struct flow_tnl *tunnel,
7014 struct ofpbuf *odp_actions, const char *tnl_type)
7015 {
7016 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
7017 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL, tnl_type);
7018 nl_msg_end_nested(odp_actions, offset);
7019 }
7020
7021 void
7022 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
7023 struct ovs_action_push_tnl *data)
7024 {
7025 int size = offsetof(struct ovs_action_push_tnl, header);
7026
7027 size += data->header_len;
7028 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
7029 }
7030
7031 \f
7032 /* The commit_odp_actions() function and its helpers. */
7033
7034 static void
7035 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
7036 const void *key, size_t key_size)
7037 {
7038 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
7039 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
7040 nl_msg_end_nested(odp_actions, offset);
7041 }
7042
7043 /* Masked set actions have a mask following the data within the netlink
7044 * attribute. The unmasked bits in the data will be cleared as the data
7045 * is copied to the action. */
7046 void
7047 commit_masked_set_action(struct ofpbuf *odp_actions,
7048 enum ovs_key_attr key_type,
7049 const void *key_, const void *mask_, size_t key_size)
7050 {
7051 size_t offset = nl_msg_start_nested(odp_actions,
7052 OVS_ACTION_ATTR_SET_MASKED);
7053 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
7054 const char *key = key_, *mask = mask_;
7055
7056 memcpy(data + key_size, mask, key_size);
7057 /* Clear unmasked bits while copying. */
7058 while (key_size--) {
7059 *data++ = *key++ & *mask++;
7060 }
7061 nl_msg_end_nested(odp_actions, offset);
7062 }
7063
7064 /* If any of the flow key data that ODP actions can modify are different in
7065 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
7066 * 'odp_actions' that change the flow tunneling information in key from
7067 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
7068 * same way. In other words, operates the same as commit_odp_actions(), but
7069 * only on tunneling information. */
7070 void
7071 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
7072 struct ofpbuf *odp_actions, const char *tnl_type)
7073 {
7074 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7075 * must have non-zero ipv6_dst. */
7076 if (flow_tnl_dst_is_set(&flow->tunnel)) {
7077 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
7078 return;
7079 }
7080 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
7081 odp_put_tunnel_action(&base->tunnel, odp_actions, tnl_type);
7082 }
7083 }
7084
7085 static bool
7086 commit(enum ovs_key_attr attr, bool use_masked_set,
7087 const void *key, void *base, void *mask, size_t size,
7088 struct ofpbuf *odp_actions)
7089 {
7090 if (memcmp(key, base, size)) {
7091 bool fully_masked = odp_mask_is_exact(attr, mask, size);
7092
7093 if (use_masked_set && !fully_masked) {
7094 commit_masked_set_action(odp_actions, attr, key, mask, size);
7095 } else {
7096 if (!fully_masked) {
7097 memset(mask, 0xff, size);
7098 }
7099 commit_set_action(odp_actions, attr, key, size);
7100 }
7101 memcpy(base, key, size);
7102 return true;
7103 } else {
7104 /* Mask bits are set when we have either read or set the corresponding
7105 * values. Masked bits will be exact-matched, no need to set them
7106 * if the value did not actually change. */
7107 return false;
7108 }
7109 }
7110
7111 static void
7112 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
7113 {
7114 eth->eth_src = flow->dl_src;
7115 eth->eth_dst = flow->dl_dst;
7116 }
7117
7118 static void
7119 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
7120 {
7121 flow->dl_src = eth->eth_src;
7122 flow->dl_dst = eth->eth_dst;
7123 }
7124
7125 static void
7126 commit_set_ether_action(const struct flow *flow, struct flow *base_flow,
7127 struct ofpbuf *odp_actions,
7128 struct flow_wildcards *wc,
7129 bool use_masked)
7130 {
7131 struct ovs_key_ethernet key, base, mask;
7132
7133 if (flow->packet_type != htonl(PT_ETH)) {
7134 return;
7135 }
7136
7137 get_ethernet_key(flow, &key);
7138 get_ethernet_key(base_flow, &base);
7139 get_ethernet_key(&wc->masks, &mask);
7140
7141 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
7142 &key, &base, &mask, sizeof key, odp_actions)) {
7143 put_ethernet_key(&base, base_flow);
7144 put_ethernet_key(&mask, &wc->masks);
7145 }
7146 }
7147
7148 static void
7149 commit_vlan_action(const struct flow* flow, struct flow *base,
7150 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
7151 {
7152 int base_n = flow_count_vlan_headers(base);
7153 int flow_n = flow_count_vlan_headers(flow);
7154 flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n);
7155
7156 /* Pop all mismatching vlan of base, push those of flow */
7157 for (; base_n >= 0; base_n--) {
7158 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
7159 wc->masks.vlans[base_n].qtag = OVS_BE32_MAX;
7160 }
7161
7162 for (; flow_n >= 0; flow_n--) {
7163 struct ovs_action_push_vlan vlan;
7164
7165 vlan.vlan_tpid = flow->vlans[flow_n].tpid;
7166 vlan.vlan_tci = flow->vlans[flow_n].tci;
7167 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
7168 &vlan, sizeof vlan);
7169 }
7170 memcpy(base->vlans, flow->vlans, sizeof(base->vlans));
7171 }
7172
7173 /* Wildcarding already done at action translation time. */
7174 static void
7175 commit_mpls_action(const struct flow *flow, struct flow *base,
7176 struct ofpbuf *odp_actions)
7177 {
7178 int base_n = flow_count_mpls_labels(base, NULL);
7179 int flow_n = flow_count_mpls_labels(flow, NULL);
7180 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
7181 NULL);
7182
7183 while (base_n > common_n) {
7184 if (base_n - 1 == common_n && flow_n > common_n) {
7185 /* If there is only one more LSE in base than there are common
7186 * between base and flow; and flow has at least one more LSE than
7187 * is common then the topmost LSE of base may be updated using
7188 * set */
7189 struct ovs_key_mpls mpls_key;
7190
7191 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
7192 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
7193 &mpls_key, sizeof mpls_key);
7194 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
7195 common_n++;
7196 } else {
7197 /* Otherwise, if there more LSEs in base than are common between
7198 * base and flow then pop the topmost one. */
7199 ovs_be16 dl_type;
7200 /* If all the LSEs are to be popped and this is not the outermost
7201 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7202 * POP_MPLS action instead of flow->dl_type.
7203 *
7204 * This is because the POP_MPLS action requires its ethertype
7205 * argument to be an MPLS ethernet type but in this case
7206 * flow->dl_type will be a non-MPLS ethernet type.
7207 *
7208 * When the final POP_MPLS action occurs it use flow->dl_type and
7209 * the and the resulting packet will have the desired dl_type. */
7210 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
7211 dl_type = htons(ETH_TYPE_MPLS);
7212 } else {
7213 dl_type = flow->dl_type;
7214 }
7215 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
7216 ovs_assert(flow_pop_mpls(base, base_n, flow->dl_type, NULL));
7217 base_n--;
7218 }
7219 }
7220
7221 /* If, after the above popping and setting, there are more LSEs in flow
7222 * than base then some LSEs need to be pushed. */
7223 while (base_n < flow_n) {
7224 struct ovs_action_push_mpls *mpls;
7225
7226 mpls = nl_msg_put_unspec_zero(odp_actions,
7227 OVS_ACTION_ATTR_PUSH_MPLS,
7228 sizeof *mpls);
7229 mpls->mpls_ethertype = flow->dl_type;
7230 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
7231 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7232 * headers if the flow is restored later due to returning from a patch
7233 * port or group bucket. */
7234 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL, false);
7235 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
7236 base_n++;
7237 }
7238 }
7239
7240 static void
7241 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
7242 {
7243 ipv4->ipv4_src = flow->nw_src;
7244 ipv4->ipv4_dst = flow->nw_dst;
7245 ipv4->ipv4_proto = flow->nw_proto;
7246 ipv4->ipv4_tos = flow->nw_tos;
7247 ipv4->ipv4_ttl = flow->nw_ttl;
7248 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
7249 }
7250
7251 static void
7252 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
7253 {
7254 flow->nw_src = ipv4->ipv4_src;
7255 flow->nw_dst = ipv4->ipv4_dst;
7256 flow->nw_proto = ipv4->ipv4_proto;
7257 flow->nw_tos = ipv4->ipv4_tos;
7258 flow->nw_ttl = ipv4->ipv4_ttl;
7259 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
7260 }
7261
7262 static void
7263 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
7264 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7265 bool use_masked)
7266 {
7267 struct ovs_key_ipv4 key, mask, base;
7268
7269 /* Check that nw_proto and nw_frag remain unchanged. */
7270 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
7271 flow->nw_frag == base_flow->nw_frag);
7272
7273 get_ipv4_key(flow, &key, false);
7274 get_ipv4_key(base_flow, &base, false);
7275 get_ipv4_key(&wc->masks, &mask, true);
7276 mask.ipv4_proto = 0; /* Not writeable. */
7277 mask.ipv4_frag = 0; /* Not writable. */
7278
7279 if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
7280 ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
7281 mask.ipv4_tos &= ~IP_ECN_MASK;
7282 }
7283
7284 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
7285 odp_actions)) {
7286 put_ipv4_key(&base, base_flow, false);
7287 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
7288 put_ipv4_key(&mask, &wc->masks, true);
7289 }
7290 }
7291 }
7292
7293 static void
7294 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
7295 {
7296 ipv6->ipv6_src = flow->ipv6_src;
7297 ipv6->ipv6_dst = flow->ipv6_dst;
7298 ipv6->ipv6_label = flow->ipv6_label;
7299 ipv6->ipv6_proto = flow->nw_proto;
7300 ipv6->ipv6_tclass = flow->nw_tos;
7301 ipv6->ipv6_hlimit = flow->nw_ttl;
7302 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
7303 }
7304
7305 static void
7306 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
7307 {
7308 flow->ipv6_src = ipv6->ipv6_src;
7309 flow->ipv6_dst = ipv6->ipv6_dst;
7310 flow->ipv6_label = ipv6->ipv6_label;
7311 flow->nw_proto = ipv6->ipv6_proto;
7312 flow->nw_tos = ipv6->ipv6_tclass;
7313 flow->nw_ttl = ipv6->ipv6_hlimit;
7314 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
7315 }
7316
7317 static void
7318 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
7319 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7320 bool use_masked)
7321 {
7322 struct ovs_key_ipv6 key, mask, base;
7323
7324 /* Check that nw_proto and nw_frag remain unchanged. */
7325 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
7326 flow->nw_frag == base_flow->nw_frag);
7327
7328 get_ipv6_key(flow, &key, false);
7329 get_ipv6_key(base_flow, &base, false);
7330 get_ipv6_key(&wc->masks, &mask, true);
7331 mask.ipv6_proto = 0; /* Not writeable. */
7332 mask.ipv6_frag = 0; /* Not writable. */
7333 mask.ipv6_label &= htonl(IPV6_LABEL_MASK); /* Not writable. */
7334
7335 if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
7336 ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
7337 mask.ipv6_tclass &= ~IP_ECN_MASK;
7338 }
7339
7340 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
7341 odp_actions)) {
7342 put_ipv6_key(&base, base_flow, false);
7343 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
7344 put_ipv6_key(&mask, &wc->masks, true);
7345 }
7346 }
7347 }
7348
7349 static void
7350 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
7351 {
7352 /* ARP key has padding, clear it. */
7353 memset(arp, 0, sizeof *arp);
7354
7355 arp->arp_sip = flow->nw_src;
7356 arp->arp_tip = flow->nw_dst;
7357 arp->arp_op = htons(flow->nw_proto);
7358 arp->arp_sha = flow->arp_sha;
7359 arp->arp_tha = flow->arp_tha;
7360 }
7361
7362 static void
7363 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
7364 {
7365 flow->nw_src = arp->arp_sip;
7366 flow->nw_dst = arp->arp_tip;
7367 flow->nw_proto = ntohs(arp->arp_op);
7368 flow->arp_sha = arp->arp_sha;
7369 flow->arp_tha = arp->arp_tha;
7370 }
7371
7372 static enum slow_path_reason
7373 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
7374 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
7375 {
7376 struct ovs_key_arp key, mask, base;
7377
7378 get_arp_key(flow, &key);
7379 get_arp_key(base_flow, &base);
7380 get_arp_key(&wc->masks, &mask);
7381
7382 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
7383 odp_actions)) {
7384 put_arp_key(&base, base_flow);
7385 put_arp_key(&mask, &wc->masks);
7386 return SLOW_ACTION;
7387 }
7388 return 0;
7389 }
7390
7391 static void
7392 get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
7393 {
7394 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7395 icmp->icmp_type = ntohs(flow->tp_src);
7396 icmp->icmp_code = ntohs(flow->tp_dst);
7397 }
7398
7399 static void
7400 put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
7401 {
7402 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7403 flow->tp_src = htons(icmp->icmp_type);
7404 flow->tp_dst = htons(icmp->icmp_code);
7405 }
7406
7407 static enum slow_path_reason
7408 commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
7409 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
7410 {
7411 struct ovs_key_icmp key, mask, base;
7412 enum ovs_key_attr attr;
7413
7414 if (is_icmpv4(flow, NULL)) {
7415 attr = OVS_KEY_ATTR_ICMP;
7416 } else if (is_icmpv6(flow, NULL)) {
7417 attr = OVS_KEY_ATTR_ICMPV6;
7418 } else {
7419 return 0;
7420 }
7421
7422 get_icmp_key(flow, &key);
7423 get_icmp_key(base_flow, &base);
7424 get_icmp_key(&wc->masks, &mask);
7425
7426 if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) {
7427 put_icmp_key(&base, base_flow);
7428 put_icmp_key(&mask, &wc->masks);
7429 return SLOW_ACTION;
7430 }
7431 return 0;
7432 }
7433
7434 static void
7435 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
7436 {
7437 nd->nd_target = flow->nd_target;
7438 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7439 nd->nd_sll = flow->arp_sha;
7440 nd->nd_tll = flow->arp_tha;
7441 }
7442
7443 static void
7444 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
7445 {
7446 flow->nd_target = nd->nd_target;
7447 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7448 flow->arp_sha = nd->nd_sll;
7449 flow->arp_tha = nd->nd_tll;
7450 }
7451
7452 static enum slow_path_reason
7453 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
7454 struct ofpbuf *odp_actions,
7455 struct flow_wildcards *wc, bool use_masked)
7456 {
7457 struct ovs_key_nd key, mask, base;
7458
7459 get_nd_key(flow, &key);
7460 get_nd_key(base_flow, &base);
7461 get_nd_key(&wc->masks, &mask);
7462
7463 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
7464 odp_actions)) {
7465 put_nd_key(&base, base_flow);
7466 put_nd_key(&mask, &wc->masks);
7467 return SLOW_ACTION;
7468 }
7469
7470 return 0;
7471 }
7472
7473 static enum slow_path_reason
7474 commit_set_nw_action(const struct flow *flow, struct flow *base,
7475 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7476 bool use_masked)
7477 {
7478 /* Check if 'flow' really has an L3 header. */
7479 if (!flow->nw_proto) {
7480 return 0;
7481 }
7482
7483 switch (ntohs(base->dl_type)) {
7484 case ETH_TYPE_IP:
7485 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
7486 break;
7487
7488 case ETH_TYPE_IPV6:
7489 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
7490 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
7491
7492 case ETH_TYPE_ARP:
7493 return commit_set_arp_action(flow, base, odp_actions, wc);
7494 }
7495
7496 return 0;
7497 }
7498
7499 static inline void
7500 get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, bool is_mask)
7501 {
7502 *nsh = flow->nsh;
7503 if (!is_mask) {
7504 if (nsh->mdtype != NSH_M_TYPE1) {
7505 memset(nsh->context, 0, sizeof(nsh->context));
7506 }
7507 }
7508 }
7509
7510 static inline void
7511 put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow,
7512 bool is_mask OVS_UNUSED)
7513 {
7514 flow->nsh = *nsh;
7515 if (flow->nsh.mdtype != NSH_M_TYPE1) {
7516 memset(flow->nsh.context, 0, sizeof(flow->nsh.context));
7517 }
7518 }
7519
7520 static bool
7521 commit_nsh(const struct ovs_key_nsh * flow_nsh, bool use_masked_set,
7522 const struct ovs_key_nsh *key, struct ovs_key_nsh *base,
7523 struct ovs_key_nsh *mask, size_t size,
7524 struct ofpbuf *odp_actions)
7525 {
7526 enum ovs_key_attr attr = OVS_KEY_ATTR_NSH;
7527
7528 if (memcmp(key, base, size) == 0) {
7529 /* Mask bits are set when we have either read or set the corresponding
7530 * values. Masked bits will be exact-matched, no need to set them
7531 * if the value did not actually change. */
7532 return false;
7533 }
7534
7535 bool fully_masked = odp_mask_is_exact(attr, mask, size);
7536
7537 if (use_masked_set && !fully_masked) {
7538 size_t nsh_key_ofs;
7539 struct ovs_nsh_key_base nsh_base;
7540 struct ovs_nsh_key_base nsh_base_mask;
7541 struct ovs_nsh_key_md1 md1;
7542 struct ovs_nsh_key_md1 md1_mask;
7543 size_t offset = nl_msg_start_nested(odp_actions,
7544 OVS_ACTION_ATTR_SET_MASKED);
7545
7546 nsh_base.flags = key->flags;
7547 nsh_base.ttl = key->ttl;
7548 nsh_base.mdtype = key->mdtype;
7549 nsh_base.np = key->np;
7550 nsh_base.path_hdr = key->path_hdr;
7551
7552 nsh_base_mask.flags = mask->flags;
7553 nsh_base_mask.ttl = mask->ttl;
7554 nsh_base_mask.mdtype = mask->mdtype;
7555 nsh_base_mask.np = mask->np;
7556 nsh_base_mask.path_hdr = mask->path_hdr;
7557
7558 /* OVS_KEY_ATTR_NSH keys */
7559 nsh_key_ofs = nl_msg_start_nested(odp_actions, OVS_KEY_ATTR_NSH);
7560
7561 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
7562 char *data = nl_msg_put_unspec_uninit(odp_actions,
7563 OVS_NSH_KEY_ATTR_BASE,
7564 2 * sizeof(nsh_base));
7565 const char *lkey = (char *)&nsh_base, *lmask = (char *)&nsh_base_mask;
7566 size_t lkey_size = sizeof(nsh_base);
7567
7568 while (lkey_size--) {
7569 *data++ = *lkey++ & *lmask++;
7570 }
7571 lmask = (char *)&nsh_base_mask;
7572 memcpy(data, lmask, sizeof(nsh_base_mask));
7573
7574 switch (key->mdtype) {
7575 case NSH_M_TYPE1:
7576 memcpy(md1.context, key->context, sizeof key->context);
7577 memcpy(md1_mask.context, mask->context, sizeof mask->context);
7578
7579 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
7580 data = nl_msg_put_unspec_uninit(odp_actions,
7581 OVS_NSH_KEY_ATTR_MD1,
7582 2 * sizeof(md1));
7583 lkey = (char *)&md1;
7584 lmask = (char *)&md1_mask;
7585 lkey_size = sizeof(md1);
7586
7587 while (lkey_size--) {
7588 *data++ = *lkey++ & *lmask++;
7589 }
7590 lmask = (char *)&md1_mask;
7591 memcpy(data, lmask, sizeof(md1_mask));
7592 break;
7593 case NSH_M_TYPE2:
7594 default:
7595 /* No match support for other MD formats yet. */
7596 break;
7597 }
7598
7599 nl_msg_end_nested(odp_actions, nsh_key_ofs);
7600
7601 nl_msg_end_nested(odp_actions, offset);
7602 } else {
7603 if (!fully_masked) {
7604 memset(mask, 0xff, size);
7605 }
7606 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
7607 nsh_key_to_attr(odp_actions, flow_nsh, NULL, 0, false);
7608 nl_msg_end_nested(odp_actions, offset);
7609 }
7610 memcpy(base, key, size);
7611 return true;
7612 }
7613
7614 static void
7615 commit_set_nsh_action(const struct flow *flow, struct flow *base_flow,
7616 struct ofpbuf *odp_actions,
7617 struct flow_wildcards *wc,
7618 bool use_masked)
7619 {
7620 struct ovs_key_nsh key, mask, base;
7621
7622 if (flow->dl_type != htons(ETH_TYPE_NSH) ||
7623 !memcmp(&base_flow->nsh, &flow->nsh, sizeof base_flow->nsh)) {
7624 return;
7625 }
7626
7627 /* Check that mdtype and np remain unchanged. */
7628 ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype &&
7629 flow->nsh.np == base_flow->nsh.np);
7630
7631 get_nsh_key(flow, &key, false);
7632 get_nsh_key(base_flow, &base, false);
7633 get_nsh_key(&wc->masks, &mask, true);
7634 mask.mdtype = 0; /* Not writable. */
7635 mask.np = 0; /* Not writable. */
7636
7637 if (commit_nsh(&base_flow->nsh, use_masked, &key, &base, &mask,
7638 sizeof key, odp_actions)) {
7639 put_nsh_key(&base, base_flow, false);
7640 if (mask.mdtype != 0) { /* Mask was changed by commit(). */
7641 put_nsh_key(&mask, &wc->masks, true);
7642 }
7643 }
7644 }
7645
7646 /* TCP, UDP, and SCTP keys have the same layout. */
7647 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
7648 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
7649
7650 static void
7651 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
7652 {
7653 tp->tcp.tcp_src = flow->tp_src;
7654 tp->tcp.tcp_dst = flow->tp_dst;
7655 }
7656
7657 static void
7658 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
7659 {
7660 flow->tp_src = tp->tcp.tcp_src;
7661 flow->tp_dst = tp->tcp.tcp_dst;
7662 }
7663
7664 static void
7665 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
7666 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7667 bool use_masked)
7668 {
7669 enum ovs_key_attr key_type;
7670 union ovs_key_tp key, mask, base;
7671
7672 /* Check if 'flow' really has an L3 header. */
7673 if (!flow->nw_proto) {
7674 return;
7675 }
7676
7677 if (!is_ip_any(base_flow)) {
7678 return;
7679 }
7680
7681 if (flow->nw_proto == IPPROTO_TCP) {
7682 key_type = OVS_KEY_ATTR_TCP;
7683 } else if (flow->nw_proto == IPPROTO_UDP) {
7684 key_type = OVS_KEY_ATTR_UDP;
7685 } else if (flow->nw_proto == IPPROTO_SCTP) {
7686 key_type = OVS_KEY_ATTR_SCTP;
7687 } else {
7688 return;
7689 }
7690
7691 get_tp_key(flow, &key);
7692 get_tp_key(base_flow, &base);
7693 get_tp_key(&wc->masks, &mask);
7694
7695 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
7696 odp_actions)) {
7697 put_tp_key(&base, base_flow);
7698 put_tp_key(&mask, &wc->masks);
7699 }
7700 }
7701
7702 static void
7703 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
7704 struct ofpbuf *odp_actions,
7705 struct flow_wildcards *wc,
7706 bool use_masked)
7707 {
7708 uint32_t key, mask, base;
7709
7710 key = flow->skb_priority;
7711 base = base_flow->skb_priority;
7712 mask = wc->masks.skb_priority;
7713
7714 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
7715 sizeof key, odp_actions)) {
7716 base_flow->skb_priority = base;
7717 wc->masks.skb_priority = mask;
7718 }
7719 }
7720
7721 static void
7722 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
7723 struct ofpbuf *odp_actions,
7724 struct flow_wildcards *wc,
7725 bool use_masked)
7726 {
7727 uint32_t key, mask, base;
7728
7729 key = flow->pkt_mark;
7730 base = base_flow->pkt_mark;
7731 mask = wc->masks.pkt_mark;
7732
7733 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
7734 sizeof key, odp_actions)) {
7735 base_flow->pkt_mark = base;
7736 wc->masks.pkt_mark = mask;
7737 }
7738 }
7739
7740 static void
7741 odp_put_pop_nsh_action(struct ofpbuf *odp_actions)
7742 {
7743 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_NSH);
7744 }
7745
7746 static void
7747 odp_put_push_nsh_action(struct ofpbuf *odp_actions,
7748 const struct flow *flow,
7749 struct ofpbuf *encap_data)
7750 {
7751 uint8_t * metadata = NULL;
7752 uint8_t md_size = 0;
7753
7754 switch (flow->nsh.mdtype) {
7755 case NSH_M_TYPE2:
7756 if (encap_data) {
7757 ovs_assert(encap_data->size < NSH_CTX_HDRS_MAX_LEN);
7758 metadata = encap_data->data;
7759 md_size = encap_data->size;
7760 } else {
7761 md_size = 0;
7762 }
7763 break;
7764 default:
7765 md_size = 0;
7766 break;
7767 }
7768 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_PUSH_NSH);
7769 nsh_key_to_attr(odp_actions, &flow->nsh, metadata, md_size, false);
7770 nl_msg_end_nested(odp_actions, offset);
7771 }
7772
7773 static void
7774 commit_encap_decap_action(const struct flow *flow,
7775 struct flow *base_flow,
7776 struct ofpbuf *odp_actions,
7777 struct flow_wildcards *wc,
7778 bool pending_encap, bool pending_decap,
7779 struct ofpbuf *encap_data)
7780 {
7781 if (pending_encap) {
7782 switch (ntohl(flow->packet_type)) {
7783 case PT_ETH: {
7784 /* push_eth */
7785 odp_put_push_eth_action(odp_actions, &flow->dl_src,
7786 &flow->dl_dst);
7787 base_flow->packet_type = flow->packet_type;
7788 base_flow->dl_src = flow->dl_src;
7789 base_flow->dl_dst = flow->dl_dst;
7790 break;
7791 }
7792 case PT_NSH:
7793 /* push_nsh */
7794 odp_put_push_nsh_action(odp_actions, flow, encap_data);
7795 base_flow->packet_type = flow->packet_type;
7796 /* Update all packet headers in base_flow. */
7797 memcpy(&base_flow->dl_dst, &flow->dl_dst,
7798 sizeof(*flow) - offsetof(struct flow, dl_dst));
7799 break;
7800 default:
7801 /* Only the above protocols are supported for encap.
7802 * The check is done at action translation. */
7803 OVS_NOT_REACHED();
7804 }
7805 } else if (pending_decap || flow->packet_type != base_flow->packet_type) {
7806 /* This is an explicit or implicit decap case. */
7807 if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE &&
7808 base_flow->packet_type == htonl(PT_ETH)) {
7809 /* Generate pop_eth and continue without recirculation. */
7810 odp_put_pop_eth_action(odp_actions);
7811 base_flow->packet_type = flow->packet_type;
7812 base_flow->dl_src = eth_addr_zero;
7813 base_flow->dl_dst = eth_addr_zero;
7814 } else {
7815 /* All other decap cases require recirculation.
7816 * No need to update the base flow here. */
7817 switch (ntohl(base_flow->packet_type)) {
7818 case PT_NSH:
7819 /* pop_nsh. */
7820 odp_put_pop_nsh_action(odp_actions);
7821 break;
7822 default:
7823 /* Checks are done during translation. */
7824 OVS_NOT_REACHED();
7825 }
7826 }
7827 }
7828
7829 wc->masks.packet_type = OVS_BE32_MAX;
7830 }
7831
7832 /* If any of the flow key data that ODP actions can modify are different in
7833 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
7834 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
7835 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
7836 * in addition to this function if needed. Sets fields in 'wc' that are
7837 * used as part of the action.
7838 *
7839 * Returns a reason to force processing the flow's packets into the userspace
7840 * slow path, if there is one, otherwise 0. */
7841 enum slow_path_reason
7842 commit_odp_actions(const struct flow *flow, struct flow *base,
7843 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
7844 bool use_masked, bool pending_encap, bool pending_decap,
7845 struct ofpbuf *encap_data)
7846 {
7847 enum slow_path_reason slow1, slow2;
7848 bool mpls_done = false;
7849
7850 commit_encap_decap_action(flow, base, odp_actions, wc,
7851 pending_encap, pending_decap, encap_data);
7852 commit_set_ether_action(flow, base, odp_actions, wc, use_masked);
7853 /* Make packet a non-MPLS packet before committing L3/4 actions,
7854 * which would otherwise do nothing. */
7855 if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) {
7856 commit_mpls_action(flow, base, odp_actions);
7857 mpls_done = true;
7858 }
7859 commit_set_nsh_action(flow, base, odp_actions, wc, use_masked);
7860 slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
7861 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
7862 slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
7863 if (!mpls_done) {
7864 commit_mpls_action(flow, base, odp_actions);
7865 }
7866 commit_vlan_action(flow, base, odp_actions, wc);
7867 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
7868 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
7869
7870 return slow1 ? slow1 : slow2;
7871 }