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