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