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