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