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