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