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