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