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