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