]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
NSH: Minor bugfixes
[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, padding;
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 /* Pad metadata to 4 bytes. */
1873 padding = PAD_SIZE(mdlen, 4);
1874 if (padding > 0) {
1875 ofpbuf_push_zeros(&b, padding);
1876 }
1877 encap_nsh.mdlen = mdlen + padding;
1878 ofpbuf_uninit(&b);
1879 }
1880 continue;
1881 }
1882 }
1883 out:
1884 if (ret < 0) {
1885 return ret;
1886 } else {
1887 size_t size = offsetof(struct ovs_action_encap_nsh, metadata)
1888 + ROUND_UP(encap_nsh.mdlen, 4);
1889 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_ENCAP_NSH,
1890 &encap_nsh, size);
1891 return n;
1892 }
1893 }
1894
1895 static int
1896 parse_action_list(const char *s, const struct simap *port_names,
1897 struct ofpbuf *actions)
1898 {
1899 int n = 0;
1900
1901 for (;;) {
1902 int retval;
1903
1904 n += strspn(s + n, delimiters);
1905 if (s[n] == ')') {
1906 break;
1907 }
1908 retval = parse_odp_action(s + n, port_names, actions);
1909 if (retval < 0) {
1910 return retval;
1911 }
1912 n += retval;
1913 }
1914
1915 return n;
1916 }
1917
1918 static int
1919 parse_odp_action(const char *s, const struct simap *port_names,
1920 struct ofpbuf *actions)
1921 {
1922 {
1923 uint32_t port;
1924 int n;
1925
1926 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1927 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1928 return n;
1929 }
1930 }
1931
1932 {
1933 uint32_t max_len;
1934 int n;
1935
1936 if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) {
1937 struct ovs_action_trunc *trunc;
1938
1939 trunc = nl_msg_put_unspec_uninit(actions,
1940 OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
1941 trunc->max_len = max_len;
1942 return n;
1943 }
1944 }
1945
1946 if (port_names) {
1947 int len = strcspn(s, delimiters);
1948 struct simap_node *node;
1949
1950 node = simap_find_len(port_names, s, len);
1951 if (node) {
1952 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1953 return len;
1954 }
1955 }
1956
1957 {
1958 uint32_t recirc_id;
1959 int n = -1;
1960
1961 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1962 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1963 return n;
1964 }
1965 }
1966
1967 if (!strncmp(s, "userspace(", 10)) {
1968 return parse_odp_userspace_action(s, actions);
1969 }
1970
1971 if (!strncmp(s, "set(", 4)) {
1972 size_t start_ofs;
1973 int retval;
1974 struct nlattr mask[128 / sizeof(struct nlattr)];
1975 struct ofpbuf maskbuf;
1976 struct nlattr *nested, *key;
1977 size_t size;
1978
1979 /* 'mask' is big enough to hold any key. */
1980 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1981
1982 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1983 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1984 if (retval < 0) {
1985 return retval;
1986 }
1987 if (s[retval + 4] != ')') {
1988 return -EINVAL;
1989 }
1990
1991 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1992 key = nested + 1;
1993
1994 size = nl_attr_get_size(mask);
1995 if (size == nl_attr_get_size(key)) {
1996 /* Change to masked set action if not fully masked. */
1997 if (!is_all_ones(mask + 1, size)) {
1998 /* Remove padding of eariler key payload */
1999 actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len;
2000
2001 /* Put mask payload right after key payload */
2002 key->nla_len += size;
2003 ofpbuf_put(actions, mask + 1, size);
2004
2005 /* Add new padding as needed */
2006 ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) -
2007 key->nla_len);
2008
2009 /* 'actions' may have been reallocated by ofpbuf_put(). */
2010 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2011 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
2012 }
2013 }
2014
2015 nl_msg_end_nested(actions, start_ofs);
2016 return retval + 5;
2017 }
2018
2019 {
2020 struct ovs_action_push_vlan push;
2021 int tpid = ETH_TYPE_VLAN;
2022 int vid, pcp;
2023 int cfi = 1;
2024 int n = -1;
2025
2026 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
2027 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2028 &vid, &pcp, &cfi, &n)
2029 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2030 &tpid, &vid, &pcp, &n)
2031 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2032 &tpid, &vid, &pcp, &cfi, &n)) {
2033 push.vlan_tpid = htons(tpid);
2034 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
2035 | (pcp << VLAN_PCP_SHIFT)
2036 | (cfi ? VLAN_CFI : 0));
2037 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
2038 &push, sizeof push);
2039
2040 return n;
2041 }
2042 }
2043
2044 if (!strncmp(s, "pop_vlan", 8)) {
2045 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
2046 return 8;
2047 }
2048
2049 {
2050 unsigned long long int meter_id;
2051 int n = -1;
2052
2053 if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) {
2054 nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id);
2055 return n;
2056 }
2057 }
2058
2059 {
2060 double percentage;
2061 int n = -1;
2062
2063 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
2064 && percentage >= 0. && percentage <= 100.0) {
2065 size_t sample_ofs, actions_ofs;
2066 double probability;
2067
2068 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
2069 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
2070 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
2071 (probability <= 0 ? 0
2072 : probability >= UINT32_MAX ? UINT32_MAX
2073 : probability));
2074
2075 actions_ofs = nl_msg_start_nested(actions,
2076 OVS_SAMPLE_ATTR_ACTIONS);
2077 int retval = parse_action_list(s + n, port_names, actions);
2078 if (retval < 0)
2079 return retval;
2080
2081 n += retval;
2082 nl_msg_end_nested(actions, actions_ofs);
2083 nl_msg_end_nested(actions, sample_ofs);
2084
2085 return s[n + 1] == ')' ? n + 2 : -EINVAL;
2086 }
2087 }
2088
2089 {
2090 if (!strncmp(s, "clone(", 6)) {
2091 size_t actions_ofs;
2092 int n = 6;
2093
2094 actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE);
2095 int retval = parse_action_list(s + n, port_names, actions);
2096 if (retval < 0) {
2097 return retval;
2098 }
2099 n += retval;
2100 nl_msg_end_nested(actions, actions_ofs);
2101 return n + 1;
2102 }
2103 }
2104
2105 {
2106 if (!strncmp(s, "encap_nsh(", 10)) {
2107 int retval = parse_odp_encap_nsh_action(s, actions);
2108 if (retval < 0) {
2109 return retval;
2110 }
2111 return retval + 1;
2112 }
2113 }
2114
2115 {
2116 int n;
2117 if (ovs_scan(s, "decap_nsh()%n", &n)) {
2118 nl_msg_put_flag(actions, OVS_ACTION_ATTR_DECAP_NSH);
2119 return n;
2120 }
2121 }
2122
2123 {
2124 uint32_t port;
2125 int n;
2126
2127 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
2128 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
2129 return n;
2130 }
2131 }
2132
2133 {
2134 int retval;
2135
2136 retval = parse_conntrack_action(s, actions);
2137 if (retval) {
2138 return retval;
2139 }
2140 }
2141
2142 {
2143 struct ovs_action_push_tnl data;
2144 int n;
2145
2146 n = ovs_parse_tnl_push(s, &data);
2147 if (n > 0) {
2148 odp_put_tnl_push_action(actions, &data);
2149 return n;
2150 } else if (n < 0) {
2151 return n;
2152 }
2153 }
2154 return -EINVAL;
2155 }
2156
2157 /* Parses the string representation of datapath actions, in the format output
2158 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2159 * value. On success, the ODP actions are appended to 'actions' as a series of
2160 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2161 * way, 'actions''s data might be reallocated. */
2162 int
2163 odp_actions_from_string(const char *s, const struct simap *port_names,
2164 struct ofpbuf *actions)
2165 {
2166 size_t old_size;
2167
2168 if (!strcasecmp(s, "drop")) {
2169 return 0;
2170 }
2171
2172 old_size = actions->size;
2173 for (;;) {
2174 int retval;
2175
2176 s += strspn(s, delimiters);
2177 if (!*s) {
2178 return 0;
2179 }
2180
2181 retval = parse_odp_action(s, port_names, actions);
2182 if (retval < 0 || !strchr(delimiters, s[retval])) {
2183 actions->size = old_size;
2184 return -retval;
2185 }
2186 s += retval;
2187 }
2188
2189 return 0;
2190 }
2191 \f
2192 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
2193 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
2194 };
2195
2196 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
2197 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
2198 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
2199 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
2200 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
2201 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
2202 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
2203 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
2204 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
2205 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
2206 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
2207 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
2208 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
2209 .next = ovs_vxlan_ext_attr_lens ,
2210 .next_max = OVS_VXLAN_EXT_MAX},
2211 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = 16 },
2212 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = 16 },
2213 };
2214
2215 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
2216 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
2217 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
2218 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
2219 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
2220 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
2221 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
2222 .next = ovs_tun_key_attr_lens,
2223 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
2224 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
2225 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
2226 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
2227 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
2228 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
2229 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
2230 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
2231 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
2232 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
2233 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
2234 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
2235 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
2236 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
2237 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
2238 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
2239 [OVS_KEY_ATTR_CT_STATE] = { .len = 4 },
2240 [OVS_KEY_ATTR_CT_ZONE] = { .len = 2 },
2241 [OVS_KEY_ATTR_CT_MARK] = { .len = 4 },
2242 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
2243 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
2244 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
2245 [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4 },
2246 [OVS_KEY_ATTR_NSH] = { .len = sizeof(struct ovs_key_nsh) },
2247 };
2248
2249 /* Returns the correct length of the payload for a flow key attribute of the
2250 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2251 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2252 * payload is a nested type. */
2253 static int
2254 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type)
2255 {
2256 if (type > max_type) {
2257 return ATTR_LEN_INVALID;
2258 }
2259
2260 return tbl[type].len;
2261 }
2262
2263 static void
2264 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
2265 {
2266 size_t len = nl_attr_get_size(a);
2267 if (len) {
2268 const uint8_t *unspec;
2269 unsigned int i;
2270
2271 unspec = nl_attr_get(a);
2272 for (i = 0; i < len; i++) {
2273 if (i) {
2274 ds_put_char(ds, ' ');
2275 }
2276 ds_put_format(ds, "%02x", unspec[i]);
2277 }
2278 }
2279 }
2280
2281 static const char *
2282 ovs_frag_type_to_string(enum ovs_frag_type type)
2283 {
2284 switch (type) {
2285 case OVS_FRAG_TYPE_NONE:
2286 return "no";
2287 case OVS_FRAG_TYPE_FIRST:
2288 return "first";
2289 case OVS_FRAG_TYPE_LATER:
2290 return "later";
2291 case __OVS_FRAG_TYPE_MAX:
2292 default:
2293 return "<error>";
2294 }
2295 }
2296
2297 static enum odp_key_fitness
2298 odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask,
2299 struct flow_tnl *tun)
2300 {
2301 unsigned int left;
2302 const struct nlattr *a;
2303 bool ttl = false;
2304 bool unknown = false;
2305
2306 NL_NESTED_FOR_EACH(a, left, attr) {
2307 uint16_t type = nl_attr_type(a);
2308 size_t len = nl_attr_get_size(a);
2309 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
2310 OVS_TUNNEL_ATTR_MAX, type);
2311
2312 if (len != expected_len && expected_len >= 0) {
2313 return ODP_FIT_ERROR;
2314 }
2315
2316 switch (type) {
2317 case OVS_TUNNEL_KEY_ATTR_ID:
2318 tun->tun_id = nl_attr_get_be64(a);
2319 tun->flags |= FLOW_TNL_F_KEY;
2320 break;
2321 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2322 tun->ip_src = nl_attr_get_be32(a);
2323 break;
2324 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2325 tun->ip_dst = nl_attr_get_be32(a);
2326 break;
2327 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
2328 tun->ipv6_src = nl_attr_get_in6_addr(a);
2329 break;
2330 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
2331 tun->ipv6_dst = nl_attr_get_in6_addr(a);
2332 break;
2333 case OVS_TUNNEL_KEY_ATTR_TOS:
2334 tun->ip_tos = nl_attr_get_u8(a);
2335 break;
2336 case OVS_TUNNEL_KEY_ATTR_TTL:
2337 tun->ip_ttl = nl_attr_get_u8(a);
2338 ttl = true;
2339 break;
2340 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2341 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
2342 break;
2343 case OVS_TUNNEL_KEY_ATTR_CSUM:
2344 tun->flags |= FLOW_TNL_F_CSUM;
2345 break;
2346 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2347 tun->tp_src = nl_attr_get_be16(a);
2348 break;
2349 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2350 tun->tp_dst = nl_attr_get_be16(a);
2351 break;
2352 case OVS_TUNNEL_KEY_ATTR_OAM:
2353 tun->flags |= FLOW_TNL_F_OAM;
2354 break;
2355 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
2356 static const struct nl_policy vxlan_opts_policy[] = {
2357 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
2358 };
2359 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
2360
2361 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
2362 return ODP_FIT_ERROR;
2363 }
2364
2365 if (ext[OVS_VXLAN_EXT_GBP]) {
2366 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
2367
2368 tun->gbp_id = htons(gbp & 0xFFFF);
2369 tun->gbp_flags = (gbp >> 16) & 0xFF;
2370 }
2371
2372 break;
2373 }
2374 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2375 tun_metadata_from_geneve_nlattr(a, is_mask, tun);
2376 break;
2377
2378 default:
2379 /* Allow this to show up as unexpected, if there are unknown
2380 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2381 unknown = true;
2382 break;
2383 }
2384 }
2385
2386 if (!ttl) {
2387 return ODP_FIT_ERROR;
2388 }
2389 if (unknown) {
2390 return ODP_FIT_TOO_MUCH;
2391 }
2392 return ODP_FIT_PERFECT;
2393 }
2394
2395 enum odp_key_fitness
2396 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
2397 {
2398 memset(tun, 0, sizeof *tun);
2399 return odp_tun_key_from_attr__(attr, false, tun);
2400 }
2401
2402 static void
2403 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
2404 const struct flow_tnl *tun_flow_key,
2405 const struct ofpbuf *key_buf)
2406 {
2407 size_t tun_key_ofs;
2408
2409 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
2410
2411 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2412 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
2413 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
2414 }
2415 if (tun_key->ip_src) {
2416 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
2417 }
2418 if (tun_key->ip_dst) {
2419 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
2420 }
2421 if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
2422 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
2423 }
2424 if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
2425 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
2426 }
2427 if (tun_key->ip_tos) {
2428 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
2429 }
2430 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
2431 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
2432 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
2433 }
2434 if (tun_key->flags & FLOW_TNL_F_CSUM) {
2435 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
2436 }
2437 if (tun_key->tp_src) {
2438 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
2439 }
2440 if (tun_key->tp_dst) {
2441 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
2442 }
2443 if (tun_key->flags & FLOW_TNL_F_OAM) {
2444 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
2445 }
2446 if (tun_key->gbp_flags || tun_key->gbp_id) {
2447 size_t vxlan_opts_ofs;
2448
2449 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
2450 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
2451 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
2452 nl_msg_end_nested(a, vxlan_opts_ofs);
2453 }
2454 tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
2455
2456 nl_msg_end_nested(a, tun_key_ofs);
2457 }
2458
2459 static bool
2460 odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size,
2461 int constant)
2462 {
2463 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2464 * that -1 becomes all-1-bits and 0 does not change. */
2465 ovs_be16 be16 = (OVS_FORCE ovs_be16) constant;
2466 uint32_t u32 = constant;
2467 uint8_t u8 = constant;
2468 const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any;
2469
2470 switch (attr) {
2471 case OVS_KEY_ATTR_UNSPEC:
2472 case OVS_KEY_ATTR_ENCAP:
2473 case __OVS_KEY_ATTR_MAX:
2474 default:
2475 return false;
2476
2477 case OVS_KEY_ATTR_PRIORITY:
2478 case OVS_KEY_ATTR_IN_PORT:
2479 case OVS_KEY_ATTR_ETHERNET:
2480 case OVS_KEY_ATTR_VLAN:
2481 case OVS_KEY_ATTR_ETHERTYPE:
2482 case OVS_KEY_ATTR_IPV4:
2483 case OVS_KEY_ATTR_TCP:
2484 case OVS_KEY_ATTR_UDP:
2485 case OVS_KEY_ATTR_ICMP:
2486 case OVS_KEY_ATTR_ICMPV6:
2487 case OVS_KEY_ATTR_ND:
2488 case OVS_KEY_ATTR_SKB_MARK:
2489 case OVS_KEY_ATTR_TUNNEL:
2490 case OVS_KEY_ATTR_SCTP:
2491 case OVS_KEY_ATTR_DP_HASH:
2492 case OVS_KEY_ATTR_RECIRC_ID:
2493 case OVS_KEY_ATTR_MPLS:
2494 case OVS_KEY_ATTR_CT_STATE:
2495 case OVS_KEY_ATTR_CT_ZONE:
2496 case OVS_KEY_ATTR_CT_MARK:
2497 case OVS_KEY_ATTR_CT_LABELS:
2498 case OVS_KEY_ATTR_PACKET_TYPE:
2499 case OVS_KEY_ATTR_NSH:
2500 return is_all_byte(mask, size, u8);
2501
2502 case OVS_KEY_ATTR_TCP_FLAGS:
2503 return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16);
2504
2505 case OVS_KEY_ATTR_IPV6: {
2506 const struct ovs_key_ipv6 *ipv6_mask = mask;
2507 return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
2508 == htonl(IPV6_LABEL_MASK & u32)
2509 && ipv6_mask->ipv6_proto == u8
2510 && ipv6_mask->ipv6_tclass == u8
2511 && ipv6_mask->ipv6_hlimit == u8
2512 && ipv6_mask->ipv6_frag == u8
2513 && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6)
2514 && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6));
2515 }
2516
2517 case OVS_KEY_ATTR_ARP:
2518 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8);
2519
2520 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
2521 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4,
2522 ipv4_proto), u8);
2523
2524 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
2525 return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6,
2526 ipv6_proto), u8);
2527 }
2528 }
2529
2530 /* The caller must already have verified that 'ma' has a correct length.
2531 *
2532 * The main purpose of this function is formatting, to allow code to figure out
2533 * whether the mask can be omitted. It doesn't try hard for attributes that
2534 * contain sub-attributes, etc., because normally those would be broken down
2535 * further for formatting. */
2536 static bool
2537 odp_mask_attr_is_wildcard(const struct nlattr *ma)
2538 {
2539 return odp_mask_is_constant__(nl_attr_type(ma),
2540 nl_attr_get(ma), nl_attr_get_size(ma), 0);
2541 }
2542
2543 /* The caller must already have verified that 'size' is a correct length for
2544 * 'attr'.
2545 *
2546 * The main purpose of this function is formatting, to allow code to figure out
2547 * whether the mask can be omitted. It doesn't try hard for attributes that
2548 * contain sub-attributes, etc., because normally those would be broken down
2549 * further for formatting. */
2550 static bool
2551 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
2552 {
2553 return odp_mask_is_constant__(attr, mask, size, -1);
2554 }
2555
2556 /* The caller must already have verified that 'ma' has a correct length. */
2557 static bool
2558 odp_mask_attr_is_exact(const struct nlattr *ma)
2559 {
2560 enum ovs_key_attr attr = nl_attr_type(ma);
2561 return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma));
2562 }
2563
2564 void
2565 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
2566 char *port_name)
2567 {
2568 struct odp_portno_names *odp_portno_names;
2569
2570 odp_portno_names = xmalloc(sizeof *odp_portno_names);
2571 odp_portno_names->port_no = port_no;
2572 odp_portno_names->name = xstrdup(port_name);
2573 hmap_insert(portno_names, &odp_portno_names->hmap_node,
2574 hash_odp_port(port_no));
2575 }
2576
2577 static char *
2578 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
2579 {
2580 if (portno_names) {
2581 struct odp_portno_names *odp_portno_names;
2582
2583 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
2584 hash_odp_port(port_no), portno_names) {
2585 if (odp_portno_names->port_no == port_no) {
2586 return odp_portno_names->name;
2587 }
2588 }
2589 }
2590 return NULL;
2591 }
2592
2593 void
2594 odp_portno_names_destroy(struct hmap *portno_names)
2595 {
2596 struct odp_portno_names *odp_portno_names;
2597
2598 HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) {
2599 free(odp_portno_names->name);
2600 free(odp_portno_names);
2601 }
2602 }
2603
2604 void
2605 odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no,
2606 struct ds *s)
2607 {
2608 const char *name = odp_portno_names_get(portno_names, port_no);
2609 if (name) {
2610 ds_put_cstr(s, name);
2611 } else {
2612 ds_put_format(s, "%"PRIu32, port_no);
2613 }
2614 }
2615
2616 /* Format helpers. */
2617
2618 static void
2619 format_eth(struct ds *ds, const char *name, const struct eth_addr key,
2620 const struct eth_addr *mask, bool verbose)
2621 {
2622 bool mask_empty = mask && eth_addr_is_zero(*mask);
2623
2624 if (verbose || !mask_empty) {
2625 bool mask_full = !mask || eth_mask_is_exact(*mask);
2626
2627 if (mask_full) {
2628 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
2629 } else {
2630 ds_put_format(ds, "%s=", name);
2631 eth_format_masked(key, mask, ds);
2632 ds_put_char(ds, ',');
2633 }
2634 }
2635 }
2636
2637
2638 static void
2639 format_be64(struct ds *ds, const char *name, ovs_be64 key,
2640 const ovs_be64 *mask, bool verbose)
2641 {
2642 bool mask_empty = mask && !*mask;
2643
2644 if (verbose || !mask_empty) {
2645 bool mask_full = !mask || *mask == OVS_BE64_MAX;
2646
2647 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
2648 if (!mask_full) { /* Partially masked. */
2649 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
2650 }
2651 ds_put_char(ds, ',');
2652 }
2653 }
2654
2655 static void
2656 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
2657 const ovs_be32 *mask, bool verbose)
2658 {
2659 bool mask_empty = mask && !*mask;
2660
2661 if (verbose || !mask_empty) {
2662 bool mask_full = !mask || *mask == OVS_BE32_MAX;
2663
2664 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
2665 if (!mask_full) { /* Partially masked. */
2666 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
2667 }
2668 ds_put_char(ds, ',');
2669 }
2670 }
2671
2672 static void
2673 format_in6_addr(struct ds *ds, const char *name,
2674 const struct in6_addr *key,
2675 const struct in6_addr *mask,
2676 bool verbose)
2677 {
2678 char buf[INET6_ADDRSTRLEN];
2679 bool mask_empty = mask && ipv6_mask_is_any(mask);
2680
2681 if (verbose || !mask_empty) {
2682 bool mask_full = !mask || ipv6_mask_is_exact(mask);
2683
2684 inet_ntop(AF_INET6, key, buf, sizeof buf);
2685 ds_put_format(ds, "%s=%s", name, buf);
2686 if (!mask_full) { /* Partially masked. */
2687 inet_ntop(AF_INET6, mask, buf, sizeof buf);
2688 ds_put_format(ds, "/%s", buf);
2689 }
2690 ds_put_char(ds, ',');
2691 }
2692 }
2693
2694 static void
2695 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
2696 const ovs_be32 *mask, bool verbose)
2697 {
2698 bool mask_empty = mask && !*mask;
2699
2700 if (verbose || !mask_empty) {
2701 bool mask_full = !mask
2702 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
2703
2704 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
2705 if (!mask_full) { /* Partially masked. */
2706 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
2707 }
2708 ds_put_char(ds, ',');
2709 }
2710 }
2711
2712 static void
2713 format_u8x(struct ds *ds, const char *name, uint8_t key,
2714 const uint8_t *mask, bool verbose)
2715 {
2716 bool mask_empty = mask && !*mask;
2717
2718 if (verbose || !mask_empty) {
2719 bool mask_full = !mask || *mask == UINT8_MAX;
2720
2721 ds_put_format(ds, "%s=%#"PRIx8, name, key);
2722 if (!mask_full) { /* Partially masked. */
2723 ds_put_format(ds, "/%#"PRIx8, *mask);
2724 }
2725 ds_put_char(ds, ',');
2726 }
2727 }
2728
2729 static void
2730 format_u8u(struct ds *ds, const char *name, uint8_t key,
2731 const uint8_t *mask, bool verbose)
2732 {
2733 bool mask_empty = mask && !*mask;
2734
2735 if (verbose || !mask_empty) {
2736 bool mask_full = !mask || *mask == UINT8_MAX;
2737
2738 ds_put_format(ds, "%s=%"PRIu8, name, key);
2739 if (!mask_full) { /* Partially masked. */
2740 ds_put_format(ds, "/%#"PRIx8, *mask);
2741 }
2742 ds_put_char(ds, ',');
2743 }
2744 }
2745
2746 static void
2747 format_be16(struct ds *ds, const char *name, ovs_be16 key,
2748 const ovs_be16 *mask, bool verbose)
2749 {
2750 bool mask_empty = mask && !*mask;
2751
2752 if (verbose || !mask_empty) {
2753 bool mask_full = !mask || *mask == OVS_BE16_MAX;
2754
2755 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
2756 if (!mask_full) { /* Partially masked. */
2757 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2758 }
2759 ds_put_char(ds, ',');
2760 }
2761 }
2762
2763 static void
2764 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
2765 const ovs_be16 *mask, bool verbose)
2766 {
2767 bool mask_empty = mask && !*mask;
2768
2769 if (verbose || !mask_empty) {
2770 bool mask_full = !mask || *mask == OVS_BE16_MAX;
2771
2772 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
2773 if (!mask_full) { /* Partially masked. */
2774 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2775 }
2776 ds_put_char(ds, ',');
2777 }
2778 }
2779
2780 static void
2781 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
2782 const uint16_t *mask, bool verbose)
2783 {
2784 bool mask_empty = mask && !*mask;
2785
2786 if (verbose || !mask_empty) {
2787 ds_put_cstr(ds, name);
2788 ds_put_char(ds, '(');
2789 if (mask) {
2790 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
2791 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
2792 } else { /* Fully masked. */
2793 format_flags(ds, flow_tun_flag_to_string, key, '|');
2794 }
2795 ds_put_cstr(ds, "),");
2796 }
2797 }
2798
2799 static bool
2800 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
2801 const struct attr_len_tbl tbl[], int max_type, bool need_key)
2802 {
2803 int expected_len;
2804
2805 expected_len = odp_key_attr_len(tbl, max_type, nl_attr_type(a));
2806 if (expected_len != ATTR_LEN_VARIABLE &&
2807 expected_len != ATTR_LEN_NESTED) {
2808
2809 bool bad_key_len = nl_attr_get_size(a) != expected_len;
2810 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
2811
2812 if (bad_key_len || bad_mask_len) {
2813 if (need_key) {
2814 ds_put_format(ds, "key%u", nl_attr_type(a));
2815 }
2816 if (bad_key_len) {
2817 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
2818 nl_attr_get_size(a), expected_len);
2819 }
2820 format_generic_odp_key(a, ds);
2821 if (ma) {
2822 ds_put_char(ds, '/');
2823 if (bad_mask_len) {
2824 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
2825 nl_attr_get_size(ma), expected_len);
2826 }
2827 format_generic_odp_key(ma, ds);
2828 }
2829 ds_put_char(ds, ')');
2830 return false;
2831 }
2832 }
2833
2834 return true;
2835 }
2836
2837 static void
2838 format_unknown_key(struct ds *ds, const struct nlattr *a,
2839 const struct nlattr *ma)
2840 {
2841 ds_put_format(ds, "key%u(", nl_attr_type(a));
2842 format_generic_odp_key(a, ds);
2843 if (ma && !odp_mask_attr_is_exact(ma)) {
2844 ds_put_char(ds, '/');
2845 format_generic_odp_key(ma, ds);
2846 }
2847 ds_put_cstr(ds, "),");
2848 }
2849
2850 static void
2851 format_odp_tun_vxlan_opt(const struct nlattr *attr,
2852 const struct nlattr *mask_attr, struct ds *ds,
2853 bool verbose)
2854 {
2855 unsigned int left;
2856 const struct nlattr *a;
2857 struct ofpbuf ofp;
2858
2859 ofpbuf_init(&ofp, 100);
2860 NL_NESTED_FOR_EACH(a, left, attr) {
2861 uint16_t type = nl_attr_type(a);
2862 const struct nlattr *ma = NULL;
2863
2864 if (mask_attr) {
2865 ma = nl_attr_find__(nl_attr_get(mask_attr),
2866 nl_attr_get_size(mask_attr), type);
2867 if (!ma) {
2868 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
2869 OVS_VXLAN_EXT_MAX,
2870 &ofp, a);
2871 }
2872 }
2873
2874 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
2875 OVS_VXLAN_EXT_MAX, true)) {
2876 continue;
2877 }
2878
2879 switch (type) {
2880 case OVS_VXLAN_EXT_GBP: {
2881 uint32_t key = nl_attr_get_u32(a);
2882 ovs_be16 id, id_mask;
2883 uint8_t flags, flags_mask = 0;
2884
2885 id = htons(key & 0xFFFF);
2886 flags = (key >> 16) & 0xFF;
2887 if (ma) {
2888 uint32_t mask = nl_attr_get_u32(ma);
2889 id_mask = htons(mask & 0xFFFF);
2890 flags_mask = (mask >> 16) & 0xFF;
2891 }
2892
2893 ds_put_cstr(ds, "gbp(");
2894 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
2895 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
2896 ds_chomp(ds, ',');
2897 ds_put_cstr(ds, "),");
2898 break;
2899 }
2900
2901 default:
2902 format_unknown_key(ds, a, ma);
2903 }
2904 ofpbuf_clear(&ofp);
2905 }
2906
2907 ds_chomp(ds, ',');
2908 ofpbuf_uninit(&ofp);
2909 }
2910
2911 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2912
2913 static void
2914 format_geneve_opts(const struct geneve_opt *opt,
2915 const struct geneve_opt *mask, int opts_len,
2916 struct ds *ds, bool verbose)
2917 {
2918 while (opts_len > 0) {
2919 unsigned int len;
2920 uint8_t data_len, data_len_mask;
2921
2922 if (opts_len < sizeof *opt) {
2923 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
2924 opts_len, sizeof *opt);
2925 return;
2926 }
2927
2928 data_len = opt->length * 4;
2929 if (mask) {
2930 if (mask->length == 0x1f) {
2931 data_len_mask = UINT8_MAX;
2932 } else {
2933 data_len_mask = mask->length;
2934 }
2935 }
2936 len = sizeof *opt + data_len;
2937 if (len > opts_len) {
2938 ds_put_format(ds, "opt len %u greater than remaining %u",
2939 len, opts_len);
2940 return;
2941 }
2942
2943 ds_put_char(ds, '{');
2944 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
2945 verbose);
2946 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
2947 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
2948 if (data_len &&
2949 (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
2950 ds_put_hex(ds, opt + 1, data_len);
2951 if (mask && !is_all_ones(mask + 1, data_len)) {
2952 ds_put_char(ds, '/');
2953 ds_put_hex(ds, mask + 1, data_len);
2954 }
2955 } else {
2956 ds_chomp(ds, ',');
2957 }
2958 ds_put_char(ds, '}');
2959
2960 opt += len / sizeof(*opt);
2961 if (mask) {
2962 mask += len / sizeof(*opt);
2963 }
2964 opts_len -= len;
2965 };
2966 }
2967
2968 static void
2969 format_odp_tun_geneve(const struct nlattr *attr,
2970 const struct nlattr *mask_attr, struct ds *ds,
2971 bool verbose)
2972 {
2973 int opts_len = nl_attr_get_size(attr);
2974 const struct geneve_opt *opt = nl_attr_get(attr);
2975 const struct geneve_opt *mask = mask_attr ?
2976 nl_attr_get(mask_attr) : NULL;
2977
2978 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
2979 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
2980 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
2981 return;
2982 }
2983
2984 format_geneve_opts(opt, mask, opts_len, ds, verbose);
2985 }
2986
2987 static void
2988 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
2989 struct ds *ds, bool verbose)
2990 {
2991 unsigned int left;
2992 const struct nlattr *a;
2993 uint16_t flags = 0;
2994 uint16_t mask_flags = 0;
2995 struct ofpbuf ofp;
2996
2997 ofpbuf_init(&ofp, 100);
2998 NL_NESTED_FOR_EACH(a, left, attr) {
2999 enum ovs_tunnel_key_attr type = nl_attr_type(a);
3000 const struct nlattr *ma = NULL;
3001
3002 if (mask_attr) {
3003 ma = nl_attr_find__(nl_attr_get(mask_attr),
3004 nl_attr_get_size(mask_attr), type);
3005 if (!ma) {
3006 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
3007 OVS_TUNNEL_KEY_ATTR_MAX,
3008 &ofp, a);
3009 }
3010 }
3011
3012 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
3013 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
3014 continue;
3015 }
3016
3017 switch (type) {
3018 case OVS_TUNNEL_KEY_ATTR_ID:
3019 format_be64(ds, "tun_id", nl_attr_get_be64(a),
3020 ma ? nl_attr_get(ma) : NULL, verbose);
3021 flags |= FLOW_TNL_F_KEY;
3022 if (ma) {
3023 mask_flags |= FLOW_TNL_F_KEY;
3024 }
3025 break;
3026 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
3027 format_ipv4(ds, "src", nl_attr_get_be32(a),
3028 ma ? nl_attr_get(ma) : NULL, verbose);
3029 break;
3030 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
3031 format_ipv4(ds, "dst", nl_attr_get_be32(a),
3032 ma ? nl_attr_get(ma) : NULL, verbose);
3033 break;
3034 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
3035 struct in6_addr ipv6_src;
3036 ipv6_src = nl_attr_get_in6_addr(a);
3037 format_in6_addr(ds, "ipv6_src", &ipv6_src,
3038 ma ? nl_attr_get(ma) : NULL, verbose);
3039 break;
3040 }
3041 case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
3042 struct in6_addr ipv6_dst;
3043 ipv6_dst = nl_attr_get_in6_addr(a);
3044 format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
3045 ma ? nl_attr_get(ma) : NULL, verbose);
3046 break;
3047 }
3048 case OVS_TUNNEL_KEY_ATTR_TOS:
3049 format_u8x(ds, "tos", nl_attr_get_u8(a),
3050 ma ? nl_attr_get(ma) : NULL, verbose);
3051 break;
3052 case OVS_TUNNEL_KEY_ATTR_TTL:
3053 format_u8u(ds, "ttl", nl_attr_get_u8(a),
3054 ma ? nl_attr_get(ma) : NULL, verbose);
3055 break;
3056 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3057 flags |= FLOW_TNL_F_DONT_FRAGMENT;
3058 break;
3059 case OVS_TUNNEL_KEY_ATTR_CSUM:
3060 flags |= FLOW_TNL_F_CSUM;
3061 break;
3062 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
3063 format_be16(ds, "tp_src", nl_attr_get_be16(a),
3064 ma ? nl_attr_get(ma) : NULL, verbose);
3065 break;
3066 case OVS_TUNNEL_KEY_ATTR_TP_DST:
3067 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
3068 ma ? nl_attr_get(ma) : NULL, verbose);
3069 break;
3070 case OVS_TUNNEL_KEY_ATTR_OAM:
3071 flags |= FLOW_TNL_F_OAM;
3072 break;
3073 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
3074 ds_put_cstr(ds, "vxlan(");
3075 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
3076 ds_put_cstr(ds, "),");
3077 break;
3078 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3079 ds_put_cstr(ds, "geneve(");
3080 format_odp_tun_geneve(a, ma, ds, verbose);
3081 ds_put_cstr(ds, "),");
3082 break;
3083 case OVS_TUNNEL_KEY_ATTR_PAD:
3084 break;
3085 case __OVS_TUNNEL_KEY_ATTR_MAX:
3086 default:
3087 format_unknown_key(ds, a, ma);
3088 }
3089 ofpbuf_clear(&ofp);
3090 }
3091
3092 /* Flags can have a valid mask even if the attribute is not set, so
3093 * we need to collect these separately. */
3094 if (mask_attr) {
3095 NL_NESTED_FOR_EACH(a, left, mask_attr) {
3096 switch (nl_attr_type(a)) {
3097 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3098 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
3099 break;
3100 case OVS_TUNNEL_KEY_ATTR_CSUM:
3101 mask_flags |= FLOW_TNL_F_CSUM;
3102 break;
3103 case OVS_TUNNEL_KEY_ATTR_OAM:
3104 mask_flags |= FLOW_TNL_F_OAM;
3105 break;
3106 }
3107 }
3108 }
3109
3110 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
3111 verbose);
3112 ds_chomp(ds, ',');
3113 ofpbuf_uninit(&ofp);
3114 }
3115
3116 static const char *
3117 odp_ct_state_to_string(uint32_t flag)
3118 {
3119 switch (flag) {
3120 case OVS_CS_F_REPLY_DIR:
3121 return "rpl";
3122 case OVS_CS_F_TRACKED:
3123 return "trk";
3124 case OVS_CS_F_NEW:
3125 return "new";
3126 case OVS_CS_F_ESTABLISHED:
3127 return "est";
3128 case OVS_CS_F_RELATED:
3129 return "rel";
3130 case OVS_CS_F_INVALID:
3131 return "inv";
3132 case OVS_CS_F_SRC_NAT:
3133 return "snat";
3134 case OVS_CS_F_DST_NAT:
3135 return "dnat";
3136 default:
3137 return NULL;
3138 }
3139 }
3140
3141 static void
3142 format_frag(struct ds *ds, const char *name, uint8_t key,
3143 const uint8_t *mask, bool verbose OVS_UNUSED)
3144 {
3145 bool mask_empty = mask && !*mask;
3146 bool mask_full = !mask || *mask == UINT8_MAX;
3147
3148 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3149 if (!mask_empty && !mask_full) {
3150 ds_put_format(ds, "error: partial mask not supported for frag (%#"
3151 PRIx8"),", *mask);
3152 } else if (!mask_empty) {
3153 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
3154 }
3155 }
3156
3157 static bool
3158 mask_empty(const struct nlattr *ma)
3159 {
3160 const void *mask;
3161 size_t n;
3162
3163 if (!ma) {
3164 return true;
3165 }
3166 mask = nl_attr_get(ma);
3167 n = nl_attr_get_size(ma);
3168
3169 return is_all_zeros(mask, n);
3170 }
3171
3172 /* The caller must have already verified that 'a' and 'ma' have correct
3173 * lengths. */
3174 static void
3175 format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma,
3176 const struct hmap *portno_names, struct ds *ds,
3177 bool verbose)
3178 {
3179 enum ovs_key_attr attr = nl_attr_type(a);
3180 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3181 bool is_exact;
3182
3183 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
3184
3185 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
3186
3187 ds_put_char(ds, '(');
3188 switch (attr) {
3189 case OVS_KEY_ATTR_ENCAP:
3190 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
3191 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
3192 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
3193 verbose);
3194 } else if (nl_attr_get_size(a)) {
3195 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
3196 ds, verbose);
3197 }
3198 break;
3199
3200 case OVS_KEY_ATTR_PRIORITY:
3201 case OVS_KEY_ATTR_SKB_MARK:
3202 case OVS_KEY_ATTR_DP_HASH:
3203 case OVS_KEY_ATTR_RECIRC_ID:
3204 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
3205 if (!is_exact) {
3206 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
3207 }
3208 break;
3209
3210 case OVS_KEY_ATTR_CT_MARK:
3211 if (verbose || !mask_empty(ma)) {
3212 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
3213 if (!is_exact) {
3214 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
3215 }
3216 }
3217 break;
3218
3219 case OVS_KEY_ATTR_CT_STATE:
3220 if (verbose) {
3221 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
3222 if (!is_exact) {
3223 ds_put_format(ds, "/%#"PRIx32,
3224 mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
3225 }
3226 } else if (!is_exact) {
3227 format_flags_masked(ds, NULL, odp_ct_state_to_string,
3228 nl_attr_get_u32(a),
3229 mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
3230 UINT32_MAX);
3231 } else {
3232 format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
3233 }
3234 break;
3235
3236 case OVS_KEY_ATTR_CT_ZONE:
3237 if (verbose || !mask_empty(ma)) {
3238 ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
3239 if (!is_exact) {
3240 ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
3241 }
3242 }
3243 break;
3244
3245 case OVS_KEY_ATTR_CT_LABELS: {
3246 const ovs_32aligned_u128 *value = nl_attr_get(a);
3247 const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL;
3248
3249 format_u128(ds, value, mask, verbose);
3250 break;
3251 }
3252
3253 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
3254 const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a);
3255 const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
3256
3257 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
3258 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
3259 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
3260 verbose);
3261 format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port),
3262 verbose);
3263 format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port),
3264 verbose);
3265 ds_chomp(ds, ',');
3266 break;
3267 }
3268
3269 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
3270 const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a);
3271 const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
3272
3273 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
3274 verbose);
3275 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
3276 verbose);
3277 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
3278 verbose);
3279 format_be16(ds, "src_port", key->src_port, MASK(mask, src_port),
3280 verbose);
3281 format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port),
3282 verbose);
3283 ds_chomp(ds, ',');
3284 break;
3285 }
3286
3287 case OVS_KEY_ATTR_TUNNEL:
3288 format_odp_tun_attr(a, ma, ds, verbose);
3289 break;
3290
3291 case OVS_KEY_ATTR_IN_PORT:
3292 if (is_exact) {
3293 odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
3294 } else {
3295 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
3296 if (!is_exact) {
3297 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
3298 }
3299 }
3300 break;
3301
3302 case OVS_KEY_ATTR_PACKET_TYPE: {
3303 ovs_be32 value = nl_attr_get_be32(a);
3304 ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX;
3305
3306 ovs_be16 ns = htons(pt_ns(value));
3307 ovs_be16 ns_mask = htons(pt_ns(mask));
3308 format_be16(ds, "ns", ns, &ns_mask, verbose);
3309
3310 ovs_be16 ns_type = pt_ns_type_be(value);
3311 ovs_be16 ns_type_mask = pt_ns_type_be(mask);
3312 format_be16x(ds, "id", ns_type, &ns_type_mask, verbose);
3313
3314 ds_chomp(ds, ',');
3315 break;
3316 }
3317
3318 case OVS_KEY_ATTR_ETHERNET: {
3319 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
3320 const struct ovs_key_ethernet *key = nl_attr_get(a);
3321
3322 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
3323 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
3324 ds_chomp(ds, ',');
3325 break;
3326 }
3327 case OVS_KEY_ATTR_VLAN:
3328 format_vlan_tci(ds, nl_attr_get_be16(a),
3329 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
3330 break;
3331
3332 case OVS_KEY_ATTR_MPLS: {
3333 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
3334 const struct ovs_key_mpls *mpls_mask = NULL;
3335 size_t size = nl_attr_get_size(a);
3336
3337 if (!size || size % sizeof *mpls_key) {
3338 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
3339 return;
3340 }
3341 if (!is_exact) {
3342 mpls_mask = nl_attr_get(ma);
3343 if (size != nl_attr_get_size(ma)) {
3344 ds_put_format(ds, "(key length %"PRIuSIZE" != "
3345 "mask length %"PRIuSIZE")",
3346 size, nl_attr_get_size(ma));
3347 return;
3348 }
3349 }
3350 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
3351 break;
3352 }
3353 case OVS_KEY_ATTR_ETHERTYPE:
3354 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
3355 if (!is_exact) {
3356 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
3357 }
3358 break;
3359
3360 case OVS_KEY_ATTR_IPV4: {
3361 const struct ovs_key_ipv4 *key = nl_attr_get(a);
3362 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
3363
3364 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
3365 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
3366 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
3367 verbose);
3368 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
3369 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
3370 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
3371 verbose);
3372 ds_chomp(ds, ',');
3373 break;
3374 }
3375 case OVS_KEY_ATTR_IPV6: {
3376 const struct ovs_key_ipv6 *key = nl_attr_get(a);
3377 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
3378
3379 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
3380 verbose);
3381 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
3382 verbose);
3383 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
3384 verbose);
3385 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
3386 verbose);
3387 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
3388 verbose);
3389 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
3390 verbose);
3391 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
3392 verbose);
3393 ds_chomp(ds, ',');
3394 break;
3395 }
3396 /* These have the same structure and format. */
3397 case OVS_KEY_ATTR_TCP:
3398 case OVS_KEY_ATTR_UDP:
3399 case OVS_KEY_ATTR_SCTP: {
3400 const struct ovs_key_tcp *key = nl_attr_get(a);
3401 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
3402
3403 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
3404 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
3405 ds_chomp(ds, ',');
3406 break;
3407 }
3408 case OVS_KEY_ATTR_TCP_FLAGS:
3409 if (!is_exact) {
3410 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
3411 ntohs(nl_attr_get_be16(a)),
3412 TCP_FLAGS(nl_attr_get_be16(ma)),
3413 TCP_FLAGS(OVS_BE16_MAX));
3414 } else {
3415 format_flags(ds, packet_tcp_flag_to_string,
3416 ntohs(nl_attr_get_be16(a)), '|');
3417 }
3418 break;
3419
3420 case OVS_KEY_ATTR_ICMP: {
3421 const struct ovs_key_icmp *key = nl_attr_get(a);
3422 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
3423
3424 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
3425 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
3426 ds_chomp(ds, ',');
3427 break;
3428 }
3429 case OVS_KEY_ATTR_ICMPV6: {
3430 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
3431 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
3432
3433 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
3434 verbose);
3435 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
3436 verbose);
3437 ds_chomp(ds, ',');
3438 break;
3439 }
3440 case OVS_KEY_ATTR_ARP: {
3441 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
3442 const struct ovs_key_arp *key = nl_attr_get(a);
3443
3444 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
3445 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
3446 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
3447 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
3448 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
3449 ds_chomp(ds, ',');
3450 break;
3451 }
3452 case OVS_KEY_ATTR_ND: {
3453 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
3454 const struct ovs_key_nd *key = nl_attr_get(a);
3455
3456 format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
3457 verbose);
3458 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
3459 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
3460
3461 ds_chomp(ds, ',');
3462 break;
3463 }
3464 case OVS_KEY_ATTR_NSH: {
3465 const struct ovs_key_nsh *mask = ma ? nl_attr_get(ma) : NULL;
3466 const struct ovs_key_nsh *key = nl_attr_get(a);
3467 format_nsh_key_mask(ds, key, mask);
3468 break;
3469 }
3470 case OVS_KEY_ATTR_UNSPEC:
3471 case __OVS_KEY_ATTR_MAX:
3472 default:
3473 format_generic_odp_key(a, ds);
3474 if (!is_exact) {
3475 ds_put_char(ds, '/');
3476 format_generic_odp_key(ma, ds);
3477 }
3478 break;
3479 }
3480 ds_put_char(ds, ')');
3481 }
3482
3483 static void
3484 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
3485 const struct hmap *portno_names, struct ds *ds,
3486 bool verbose)
3487 {
3488 if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
3489 OVS_KEY_ATTR_MAX, false)) {
3490 format_odp_key_attr__(a, ma, portno_names, ds, verbose);
3491 }
3492 }
3493
3494 static struct nlattr *
3495 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
3496 struct ofpbuf *ofp, const struct nlattr *key)
3497 {
3498 const struct nlattr *a;
3499 unsigned int left;
3500 int type = nl_attr_type(key);
3501 int size = nl_attr_get_size(key);
3502
3503 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
3504 nl_msg_put_unspec_zero(ofp, type, size);
3505 } else {
3506 size_t nested_mask;
3507
3508 if (tbl[type].next) {
3509 tbl = tbl[type].next;
3510 max = tbl[type].next_max;
3511 }
3512
3513 nested_mask = nl_msg_start_nested(ofp, type);
3514 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
3515 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
3516 }
3517 nl_msg_end_nested(ofp, nested_mask);
3518 }
3519
3520 return ofp->base;
3521 }
3522
3523 static void
3524 format_u128(struct ds *ds, const ovs_32aligned_u128 *key,
3525 const ovs_32aligned_u128 *mask, bool verbose)
3526 {
3527 if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) {
3528 ovs_be128 value = hton128(get_32aligned_u128(key));
3529 ds_put_hex(ds, &value, sizeof value);
3530 if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) {
3531 value = hton128(get_32aligned_u128(mask));
3532 ds_put_char(ds, '/');
3533 ds_put_hex(ds, &value, sizeof value);
3534 }
3535 }
3536 }
3537
3538 /* Read the string from 's_' as a 128-bit value. If the string contains
3539 * a "/", the rest of the string will be treated as a 128-bit mask.
3540 *
3541 * If either the value or mask is larger than 64 bits, the string must
3542 * be in hexadecimal.
3543 */
3544 static int
3545 scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
3546 {
3547 char *s = CONST_CAST(char *, s_);
3548 ovs_be128 be_value;
3549 ovs_be128 be_mask;
3550
3551 if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
3552 *value = ntoh128(be_value);
3553
3554 if (mask) {
3555 int n;
3556
3557 if (ovs_scan(s, "/%n", &n)) {
3558 int error;
3559
3560 s += n;
3561 error = parse_int_string(s, (uint8_t *)&be_mask,
3562 sizeof be_mask, &s);
3563 if (error) {
3564 return error;
3565 }
3566 *mask = ntoh128(be_mask);
3567 } else {
3568 *mask = OVS_U128_MAX;
3569 }
3570 }
3571 return s - s_;
3572 }
3573
3574 return 0;
3575 }
3576
3577 int
3578 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
3579 {
3580 const char *s = s_;
3581
3582 if (ovs_scan(s, "ufid:")) {
3583 s += 5;
3584
3585 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
3586 return -EINVAL;
3587 }
3588 s += UUID_LEN;
3589
3590 return s - s_;
3591 }
3592
3593 return 0;
3594 }
3595
3596 void
3597 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
3598 {
3599 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
3600 }
3601
3602 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3603 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3604 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3605 * non-null, translates odp port number to its name. */
3606 void
3607 odp_flow_format(const struct nlattr *key, size_t key_len,
3608 const struct nlattr *mask, size_t mask_len,
3609 const struct hmap *portno_names, struct ds *ds, bool verbose)
3610 {
3611 if (key_len) {
3612 const struct nlattr *a;
3613 unsigned int left;
3614 bool has_ethtype_key = false;
3615 struct ofpbuf ofp;
3616 bool first_field = true;
3617
3618 ofpbuf_init(&ofp, 100);
3619 NL_ATTR_FOR_EACH (a, left, key, key_len) {
3620 int attr_type = nl_attr_type(a);
3621 const struct nlattr *ma = (mask && mask_len
3622 ? nl_attr_find__(mask, mask_len,
3623 attr_type)
3624 : NULL);
3625 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
3626 OVS_KEY_ATTR_MAX, false)) {
3627 continue;
3628 }
3629
3630 bool is_nested_attr;
3631 bool is_wildcard = false;
3632
3633 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
3634 has_ethtype_key = true;
3635 }
3636
3637 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
3638 OVS_KEY_ATTR_MAX, attr_type) ==
3639 ATTR_LEN_NESTED;
3640
3641 if (mask && mask_len) {
3642 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
3643 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
3644 }
3645
3646 if (verbose || !is_wildcard || is_nested_attr) {
3647 if (is_wildcard && !ma) {
3648 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
3649 OVS_KEY_ATTR_MAX,
3650 &ofp, a);
3651 }
3652 if (!first_field) {
3653 ds_put_char(ds, ',');
3654 }
3655 format_odp_key_attr__(a, ma, portno_names, ds, verbose);
3656 first_field = false;
3657 }
3658 ofpbuf_clear(&ofp);
3659 }
3660 ofpbuf_uninit(&ofp);
3661
3662 if (left) {
3663 int i;
3664
3665 if (left == key_len) {
3666 ds_put_cstr(ds, "<empty>");
3667 }
3668 ds_put_format(ds, ",***%u leftover bytes*** (", left);
3669 for (i = 0; i < left; i++) {
3670 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
3671 }
3672 ds_put_char(ds, ')');
3673 }
3674 if (!has_ethtype_key) {
3675 const struct nlattr *ma = nl_attr_find__(mask, mask_len,
3676 OVS_KEY_ATTR_ETHERTYPE);
3677 if (ma) {
3678 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
3679 ntohs(nl_attr_get_be16(ma)));
3680 }
3681 }
3682 } else {
3683 ds_put_cstr(ds, "<empty>");
3684 }
3685 }
3686
3687 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3688 * OVS_KEY_ATTR_* attributes in 'key'. */
3689 void
3690 odp_flow_key_format(const struct nlattr *key,
3691 size_t key_len, struct ds *ds)
3692 {
3693 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
3694 }
3695
3696 static bool
3697 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
3698 {
3699 if (!strcasecmp(s, "no")) {
3700 *type = OVS_FRAG_TYPE_NONE;
3701 } else if (!strcasecmp(s, "first")) {
3702 *type = OVS_FRAG_TYPE_FIRST;
3703 } else if (!strcasecmp(s, "later")) {
3704 *type = OVS_FRAG_TYPE_LATER;
3705 } else {
3706 return false;
3707 }
3708 return true;
3709 }
3710
3711 /* Parsing. */
3712
3713 static int
3714 scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
3715 {
3716 int n;
3717
3718 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
3719 ETH_ADDR_SCAN_ARGS(*key), &n)) {
3720 int len = n;
3721
3722 if (mask) {
3723 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
3724 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
3725 len += n;
3726 } else {
3727 memset(mask, 0xff, sizeof *mask);
3728 }
3729 }
3730 return len;
3731 }
3732 return 0;
3733 }
3734
3735 static int
3736 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
3737 {
3738 int n;
3739
3740 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
3741 int len = n;
3742
3743 if (mask) {
3744 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
3745 IP_SCAN_ARGS(mask), &n)) {
3746 len += n;
3747 } else {
3748 *mask = OVS_BE32_MAX;
3749 }
3750 }
3751 return len;
3752 }
3753 return 0;
3754 }
3755
3756 static int
3757 scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
3758 {
3759 int n;
3760 char ipv6_s[IPV6_SCAN_LEN + 1];
3761
3762 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
3763 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
3764 int len = n;
3765
3766 if (mask) {
3767 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
3768 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
3769 len += n;
3770 } else {
3771 memset(mask, 0xff, sizeof *mask);
3772 }
3773 }
3774 return len;
3775 }
3776 return 0;
3777 }
3778
3779 static int
3780 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3781 {
3782 int key_, mask_;
3783 int n;
3784
3785 if (ovs_scan(s, "%i%n", &key_, &n)
3786 && (key_ & ~IPV6_LABEL_MASK) == 0) {
3787 int len = n;
3788
3789 *key = htonl(key_);
3790 if (mask) {
3791 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
3792 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
3793 len += n;
3794 *mask = htonl(mask_);
3795 } else {
3796 *mask = htonl(IPV6_LABEL_MASK);
3797 }
3798 }
3799 return len;
3800 }
3801 return 0;
3802 }
3803
3804 static int
3805 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
3806 {
3807 int n;
3808
3809 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
3810 int len = n;
3811
3812 if (mask) {
3813 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
3814 len += n;
3815 } else {
3816 *mask = UINT8_MAX;
3817 }
3818 }
3819 return len;
3820 }
3821 return 0;
3822 }
3823
3824 static int
3825 scan_u16(const char *s, uint16_t *key, uint16_t *mask)
3826 {
3827 int n;
3828
3829 if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
3830 int len = n;
3831
3832 if (mask) {
3833 if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
3834 len += n;
3835 } else {
3836 *mask = UINT16_MAX;
3837 }
3838 }
3839 return len;
3840 }
3841 return 0;
3842 }
3843
3844 static int
3845 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
3846 {
3847 int n;
3848
3849 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3850 int len = n;
3851
3852 if (mask) {
3853 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3854 len += n;
3855 } else {
3856 *mask = UINT32_MAX;
3857 }
3858 }
3859 return len;
3860 }
3861 return 0;
3862 }
3863
3864 static int
3865 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
3866 {
3867 uint16_t key_, mask_;
3868 int n;
3869
3870 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3871 int len = n;
3872
3873 *key = htons(key_);
3874 if (mask) {
3875 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3876 len += n;
3877 *mask = htons(mask_);
3878 } else {
3879 *mask = OVS_BE16_MAX;
3880 }
3881 }
3882 return len;
3883 }
3884 return 0;
3885 }
3886
3887 static int
3888 scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask)
3889 {
3890 uint32_t key_, mask_;
3891 int n;
3892
3893 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
3894 int len = n;
3895
3896 *key = htonl(key_);
3897 if (mask) {
3898 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
3899 len += n;
3900 *mask = htonl(mask_);
3901 } else {
3902 *mask = OVS_BE32_MAX;
3903 }
3904 }
3905 return len;
3906 }
3907 return 0;
3908 }
3909
3910 static int
3911 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
3912 {
3913 uint64_t key_, mask_;
3914 int n;
3915
3916 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
3917 int len = n;
3918
3919 *key = htonll(key_);
3920 if (mask) {
3921 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
3922 len += n;
3923 *mask = htonll(mask_);
3924 } else {
3925 *mask = OVS_BE64_MAX;
3926 }
3927 }
3928 return len;
3929 }
3930 return 0;
3931 }
3932
3933 static int
3934 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
3935 {
3936 uint32_t flags, fmask;
3937 int n;
3938
3939 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
3940 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
3941 if (n >= 0 && s[n] == ')') {
3942 *key = flags;
3943 if (mask) {
3944 *mask = fmask;
3945 }
3946 return n + 1;
3947 }
3948 return 0;
3949 }
3950
3951 static int
3952 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
3953 {
3954 uint32_t flags, fmask;
3955 int n;
3956
3957 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
3958 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
3959 if (n >= 0) {
3960 *key = htons(flags);
3961 if (mask) {
3962 *mask = htons(fmask);
3963 }
3964 return n;
3965 }
3966 return 0;
3967 }
3968
3969 static uint32_t
3970 ovs_to_odp_ct_state(uint8_t state)
3971 {
3972 uint32_t odp = 0;
3973
3974 #define CS_STATE(ENUM, INDEX, NAME) \
3975 if (state & CS_##ENUM) { \
3976 odp |= OVS_CS_F_##ENUM; \
3977 }
3978 CS_STATES
3979 #undef CS_STATE
3980
3981 return odp;
3982 }
3983
3984 static uint8_t
3985 odp_to_ovs_ct_state(uint32_t flags)
3986 {
3987 uint32_t state = 0;
3988
3989 #define CS_STATE(ENUM, INDEX, NAME) \
3990 if (flags & OVS_CS_F_##ENUM) { \
3991 state |= CS_##ENUM; \
3992 }
3993 CS_STATES
3994 #undef CS_STATE
3995
3996 return state;
3997 }
3998
3999 static int
4000 scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
4001 {
4002 uint32_t flags, fmask;
4003 int n;
4004
4005 n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
4006 ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
4007 mask ? &fmask : NULL);
4008
4009 if (n >= 0) {
4010 *key = flags;
4011 if (mask) {
4012 *mask = fmask;
4013 }
4014 return n;
4015 }
4016 return 0;
4017 }
4018
4019 static int
4020 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
4021 {
4022 int n;
4023 char frag[8];
4024 enum ovs_frag_type frag_type;
4025
4026 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
4027 && ovs_frag_type_from_string(frag, &frag_type)) {
4028 int len = n;
4029
4030 *key = frag_type;
4031 if (mask) {
4032 *mask = UINT8_MAX;
4033 }
4034 return len;
4035 }
4036 return 0;
4037 }
4038
4039 static int
4040 scan_port(const char *s, uint32_t *key, uint32_t *mask,
4041 const struct simap *port_names)
4042 {
4043 int n;
4044
4045 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
4046 int len = n;
4047
4048 if (mask) {
4049 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
4050 len += n;
4051 } else {
4052 *mask = UINT32_MAX;
4053 }
4054 }
4055 return len;
4056 } else if (port_names) {
4057 const struct simap_node *node;
4058 int len;
4059
4060 len = strcspn(s, ")");
4061 node = simap_find_len(port_names, s, len);
4062 if (node) {
4063 *key = node->data;
4064
4065 if (mask) {
4066 *mask = UINT32_MAX;
4067 }
4068 return len;
4069 }
4070 }
4071 return 0;
4072 }
4073
4074 /* Helper for vlan parsing. */
4075 struct ovs_key_vlan__ {
4076 ovs_be16 tci;
4077 };
4078
4079 static bool
4080 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
4081 {
4082 const uint16_t mask = ((1U << bits) - 1) << offset;
4083
4084 if (value >> bits) {
4085 return false;
4086 }
4087
4088 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
4089 return true;
4090 }
4091
4092 static int
4093 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
4094 uint8_t offset)
4095 {
4096 uint16_t key_, mask_;
4097 int n;
4098
4099 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
4100 int len = n;
4101
4102 if (set_be16_bf(key, bits, offset, key_)) {
4103 if (mask) {
4104 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
4105 len += n;
4106
4107 if (!set_be16_bf(mask, bits, offset, mask_)) {
4108 return 0;
4109 }
4110 } else {
4111 *mask |= htons(((1U << bits) - 1) << offset);
4112 }
4113 }
4114 return len;
4115 }
4116 }
4117 return 0;
4118 }
4119
4120 static int
4121 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
4122 {
4123 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
4124 }
4125
4126 static int
4127 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
4128 {
4129 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
4130 }
4131
4132 static int
4133 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
4134 {
4135 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
4136 }
4137
4138 /* For MPLS. */
4139 static bool
4140 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
4141 {
4142 const uint32_t mask = ((1U << bits) - 1) << offset;
4143
4144 if (value >> bits) {
4145 return false;
4146 }
4147
4148 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
4149 return true;
4150 }
4151
4152 static int
4153 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
4154 uint8_t offset)
4155 {
4156 uint32_t key_, mask_;
4157 int n;
4158
4159 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
4160 int len = n;
4161
4162 if (set_be32_bf(key, bits, offset, key_)) {
4163 if (mask) {
4164 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
4165 len += n;
4166
4167 if (!set_be32_bf(mask, bits, offset, mask_)) {
4168 return 0;
4169 }
4170 } else {
4171 *mask |= htonl(((1U << bits) - 1) << offset);
4172 }
4173 }
4174 return len;
4175 }
4176 }
4177 return 0;
4178 }
4179
4180 static int
4181 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
4182 {
4183 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
4184 }
4185
4186 static int
4187 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
4188 {
4189 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
4190 }
4191
4192 static int
4193 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
4194 {
4195 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
4196 }
4197
4198 static int
4199 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
4200 {
4201 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
4202 }
4203
4204 static int
4205 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
4206 {
4207 const char *s_base = s;
4208 ovs_be16 id = 0, id_mask = 0;
4209 uint8_t flags = 0, flags_mask = 0;
4210
4211 if (!strncmp(s, "id=", 3)) {
4212 s += 3;
4213 s += scan_be16(s, &id, mask ? &id_mask : NULL);
4214 }
4215
4216 if (s[0] == ',') {
4217 s++;
4218 }
4219 if (!strncmp(s, "flags=", 6)) {
4220 s += 6;
4221 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
4222 }
4223
4224 if (!strncmp(s, "))", 2)) {
4225 s += 2;
4226
4227 *key = (flags << 16) | ntohs(id);
4228 if (mask) {
4229 *mask = (flags_mask << 16) | ntohs(id_mask);
4230 }
4231
4232 return s - s_base;
4233 }
4234
4235 return 0;
4236 }
4237
4238 static int
4239 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
4240 {
4241 const char *s_base = s;
4242 struct geneve_opt *opt = key->d;
4243 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
4244 int len_remain = sizeof key->d;
4245
4246 while (s[0] == '{' && len_remain >= sizeof *opt) {
4247 int data_len = 0;
4248
4249 s++;
4250 len_remain -= sizeof *opt;
4251
4252 if (!strncmp(s, "class=", 6)) {
4253 s += 6;
4254 s += scan_be16(s, &opt->opt_class,
4255 mask ? &opt_mask->opt_class : NULL);
4256 } else if (mask) {
4257 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
4258 }
4259
4260 if (s[0] == ',') {
4261 s++;
4262 }
4263 if (!strncmp(s, "type=", 5)) {
4264 s += 5;
4265 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
4266 } else if (mask) {
4267 memset(&opt_mask->type, 0, sizeof opt_mask->type);
4268 }
4269
4270 if (s[0] == ',') {
4271 s++;
4272 }
4273 if (!strncmp(s, "len=", 4)) {
4274 uint8_t opt_len, opt_len_mask;
4275 s += 4;
4276 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
4277
4278 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
4279 return 0;
4280 }
4281 opt->length = opt_len / 4;
4282 if (mask) {
4283 opt_mask->length = opt_len_mask;
4284 }
4285 data_len = opt_len;
4286 } else if (mask) {
4287 memset(&opt_mask->type, 0, sizeof opt_mask->type);
4288 }
4289
4290 if (s[0] == ',') {
4291 s++;
4292 }
4293 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
4294 return 0;
4295 }
4296
4297 if (mask) {
4298 if (s[0] == '/') {
4299 s++;
4300 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
4301 data_len, (char **)&s)) {
4302 return 0;
4303 }
4304 }
4305 opt_mask->r1 = 0;
4306 opt_mask->r2 = 0;
4307 opt_mask->r3 = 0;
4308 }
4309
4310 if (s[0] == '}') {
4311 s++;
4312 opt += 1 + data_len / 4;
4313 if (mask) {
4314 opt_mask += 1 + data_len / 4;
4315 }
4316 len_remain -= data_len;
4317 }
4318 }
4319
4320 if (s[0] == ')') {
4321 int len = sizeof key->d - len_remain;
4322
4323 s++;
4324 key->len = len;
4325 if (mask) {
4326 mask->len = len;
4327 }
4328 return s - s_base;
4329 }
4330
4331 return 0;
4332 }
4333
4334 static void
4335 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
4336 {
4337 const uint16_t *flags = data_;
4338
4339 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
4340 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
4341 }
4342 if (*flags & FLOW_TNL_F_CSUM) {
4343 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
4344 }
4345 if (*flags & FLOW_TNL_F_OAM) {
4346 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
4347 }
4348 }
4349
4350 static void
4351 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
4352 {
4353 const uint32_t *gbp = data_;
4354
4355 if (*gbp) {
4356 size_t vxlan_opts_ofs;
4357
4358 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
4359 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
4360 nl_msg_end_nested(a, vxlan_opts_ofs);
4361 }
4362 }
4363
4364 static void
4365 geneve_to_attr(struct ofpbuf *a, const void *data_)
4366 {
4367 const struct geneve_scan *geneve = data_;
4368
4369 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
4370 geneve->len);
4371 }
4372
4373 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
4374 { \
4375 unsigned long call_fn = (unsigned long)FUNC; \
4376 if (call_fn) { \
4377 typedef void (*fn)(struct ofpbuf *, const void *); \
4378 fn func = FUNC; \
4379 func(BUF, &(DATA)); \
4380 } else { \
4381 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
4382 } \
4383 }
4384
4385 #define SCAN_IF(NAME) \
4386 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4387 const char *start = s; \
4388 int len; \
4389 \
4390 s += strlen(NAME)
4391
4392 /* Usually no special initialization is needed. */
4393 #define SCAN_BEGIN(NAME, TYPE) \
4394 SCAN_IF(NAME); \
4395 TYPE skey, smask; \
4396 memset(&skey, 0, sizeof skey); \
4397 memset(&smask, 0, sizeof smask); \
4398 do { \
4399 len = 0;
4400
4401 /* Init as fully-masked as mask will not be scanned. */
4402 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
4403 SCAN_IF(NAME); \
4404 TYPE skey, smask; \
4405 memset(&skey, 0, sizeof skey); \
4406 memset(&smask, 0xff, sizeof smask); \
4407 do { \
4408 len = 0;
4409
4410 /* VLAN needs special initialization. */
4411 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
4412 SCAN_IF(NAME); \
4413 TYPE skey = KEY_INIT; \
4414 TYPE smask = MASK_INIT; \
4415 do { \
4416 len = 0;
4417
4418 /* Scan unnamed entry as 'TYPE' */
4419 #define SCAN_TYPE(TYPE, KEY, MASK) \
4420 len = scan_##TYPE(s, KEY, MASK); \
4421 if (len == 0) { \
4422 return -EINVAL; \
4423 } \
4424 s += len
4425
4426 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4427 #define SCAN_FIELD(NAME, TYPE, FIELD) \
4428 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4429 s += strlen(NAME); \
4430 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
4431 continue; \
4432 }
4433
4434 #define SCAN_FINISH() \
4435 } while (*s++ == ',' && len != 0); \
4436 if (s[-1] != ')') { \
4437 return -EINVAL; \
4438 }
4439
4440 #define SCAN_FINISH_SINGLE() \
4441 } while (false); \
4442 if (*s++ != ')') { \
4443 return -EINVAL; \
4444 }
4445
4446 /* Beginning of nested attribute. */
4447 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
4448 SCAN_IF(NAME); \
4449 size_t key_offset, mask_offset; \
4450 key_offset = nl_msg_start_nested(key, ATTR); \
4451 if (mask) { \
4452 mask_offset = nl_msg_start_nested(mask, ATTR); \
4453 } \
4454 do { \
4455 len = 0;
4456
4457 #define SCAN_END_NESTED() \
4458 SCAN_FINISH(); \
4459 nl_msg_end_nested(key, key_offset); \
4460 if (mask) { \
4461 nl_msg_end_nested(mask, mask_offset); \
4462 } \
4463 return s - start; \
4464 }
4465
4466 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
4467 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4468 TYPE skey, smask; \
4469 memset(&skey, 0, sizeof skey); \
4470 memset(&smask, 0xff, sizeof smask); \
4471 s += strlen(NAME); \
4472 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4473 SCAN_PUT(ATTR, FUNC); \
4474 continue; \
4475 }
4476
4477 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
4478 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
4479
4480 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
4481 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
4482
4483 #define SCAN_PUT(ATTR, FUNC) \
4484 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
4485 if (mask) \
4486 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
4487
4488 #define SCAN_END(ATTR) \
4489 SCAN_FINISH(); \
4490 SCAN_PUT(ATTR, NULL); \
4491 return s - start; \
4492 }
4493
4494 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
4495 SCAN_IF(NAME); \
4496 TYPE skey[CNT], smask[CNT]; \
4497 memset(&skey, 0, sizeof skey); \
4498 memset(&smask, 0, sizeof smask); \
4499 int idx = 0, cnt = CNT; \
4500 uint64_t fields = 0; \
4501 do { \
4502 int field = 0; \
4503 len = 0;
4504
4505 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4506 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
4507 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4508 if (fields & (1UL << field)) { \
4509 fields = 0; \
4510 if (++idx == cnt) { \
4511 break; \
4512 } \
4513 } \
4514 s += strlen(NAME); \
4515 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
4516 fields |= 1UL << field; \
4517 continue; \
4518 } \
4519 field++;
4520
4521 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
4522 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
4523
4524 #define SCAN_PUT_ARRAY(ATTR, CNT) \
4525 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
4526 if (mask) { \
4527 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
4528 }
4529
4530 #define SCAN_END_ARRAY(ATTR) \
4531 SCAN_FINISH(); \
4532 if (idx == cnt) { \
4533 return -EINVAL; \
4534 } \
4535 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4536 return s - start; \
4537 }
4538
4539 #define SCAN_END_SINGLE(ATTR) \
4540 SCAN_FINISH_SINGLE(); \
4541 SCAN_PUT(ATTR, NULL); \
4542 return s - start; \
4543 }
4544
4545 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4546 SCAN_BEGIN(NAME, TYPE) { \
4547 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4548 } SCAN_END_SINGLE(ATTR)
4549
4550 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4551 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4552 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4553 } SCAN_END_SINGLE(ATTR)
4554
4555 /* scan_port needs one extra argument. */
4556 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4557 SCAN_BEGIN(NAME, TYPE) { \
4558 len = scan_port(s, &skey, &smask, port_names); \
4559 if (len == 0) { \
4560 return -EINVAL; \
4561 } \
4562 s += len; \
4563 } SCAN_END_SINGLE(ATTR)
4564
4565 static int
4566 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
4567 struct ofpbuf *key, struct ofpbuf *mask)
4568 {
4569 /* Skip UFID. */
4570 ovs_u128 ufid;
4571 int ufid_len = odp_ufid_from_string(s, &ufid);
4572 if (ufid_len) {
4573 return ufid_len;
4574 }
4575
4576 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
4577 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
4578 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
4579 OVS_KEY_ATTR_RECIRC_ID);
4580 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
4581
4582 SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
4583 SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
4584 SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
4585 SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
4586
4587 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) {
4588 SCAN_FIELD("src=", ipv4, ipv4_src);
4589 SCAN_FIELD("dst=", ipv4, ipv4_dst);
4590 SCAN_FIELD("proto=", u8, ipv4_proto);
4591 SCAN_FIELD("tp_src=", be16, src_port);
4592 SCAN_FIELD("tp_dst=", be16, dst_port);
4593 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
4594
4595 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) {
4596 SCAN_FIELD("src=", in6_addr, ipv6_src);
4597 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
4598 SCAN_FIELD("proto=", u8, ipv6_proto);
4599 SCAN_FIELD("tp_src=", be16, src_port);
4600 SCAN_FIELD("tp_dst=", be16, dst_port);
4601 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
4602
4603 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
4604 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
4605 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
4606 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
4607 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
4608 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
4609 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
4610 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
4611 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
4612 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
4613 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
4614 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
4615 geneve_to_attr);
4616 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
4617 } SCAN_END_NESTED();
4618
4619 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
4620
4621 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
4622 SCAN_FIELD("src=", eth, eth_src);
4623 SCAN_FIELD("dst=", eth, eth_dst);
4624 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
4625
4626 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
4627 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
4628 SCAN_FIELD("vid=", vid, tci);
4629 SCAN_FIELD("pcp=", pcp, tci);
4630 SCAN_FIELD("cfi=", cfi, tci);
4631 } SCAN_END(OVS_KEY_ATTR_VLAN);
4632
4633 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
4634
4635 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
4636 SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
4637 SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
4638 SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
4639 SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
4640 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
4641
4642 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
4643 SCAN_FIELD("src=", ipv4, ipv4_src);
4644 SCAN_FIELD("dst=", ipv4, ipv4_dst);
4645 SCAN_FIELD("proto=", u8, ipv4_proto);
4646 SCAN_FIELD("tos=", u8, ipv4_tos);
4647 SCAN_FIELD("ttl=", u8, ipv4_ttl);
4648 SCAN_FIELD("frag=", frag, ipv4_frag);
4649 } SCAN_END(OVS_KEY_ATTR_IPV4);
4650
4651 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
4652 SCAN_FIELD("src=", in6_addr, ipv6_src);
4653 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
4654 SCAN_FIELD("label=", ipv6_label, ipv6_label);
4655 SCAN_FIELD("proto=", u8, ipv6_proto);
4656 SCAN_FIELD("tclass=", u8, ipv6_tclass);
4657 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
4658 SCAN_FIELD("frag=", frag, ipv6_frag);
4659 } SCAN_END(OVS_KEY_ATTR_IPV6);
4660
4661 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
4662 SCAN_FIELD("src=", be16, tcp_src);
4663 SCAN_FIELD("dst=", be16, tcp_dst);
4664 } SCAN_END(OVS_KEY_ATTR_TCP);
4665
4666 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
4667
4668 SCAN_BEGIN("udp(", struct ovs_key_udp) {
4669 SCAN_FIELD("src=", be16, udp_src);
4670 SCAN_FIELD("dst=", be16, udp_dst);
4671 } SCAN_END(OVS_KEY_ATTR_UDP);
4672
4673 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
4674 SCAN_FIELD("src=", be16, sctp_src);
4675 SCAN_FIELD("dst=", be16, sctp_dst);
4676 } SCAN_END(OVS_KEY_ATTR_SCTP);
4677
4678 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
4679 SCAN_FIELD("type=", u8, icmp_type);
4680 SCAN_FIELD("code=", u8, icmp_code);
4681 } SCAN_END(OVS_KEY_ATTR_ICMP);
4682
4683 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
4684 SCAN_FIELD("type=", u8, icmpv6_type);
4685 SCAN_FIELD("code=", u8, icmpv6_code);
4686 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
4687
4688 SCAN_BEGIN("arp(", struct ovs_key_arp) {
4689 SCAN_FIELD("sip=", ipv4, arp_sip);
4690 SCAN_FIELD("tip=", ipv4, arp_tip);
4691 SCAN_FIELD("op=", be16, arp_op);
4692 SCAN_FIELD("sha=", eth, arp_sha);
4693 SCAN_FIELD("tha=", eth, arp_tha);
4694 } SCAN_END(OVS_KEY_ATTR_ARP);
4695
4696 SCAN_BEGIN("nd(", struct ovs_key_nd) {
4697 SCAN_FIELD("target=", in6_addr, nd_target);
4698 SCAN_FIELD("sll=", eth, nd_sll);
4699 SCAN_FIELD("tll=", eth, nd_tll);
4700 } SCAN_END(OVS_KEY_ATTR_ND);
4701
4702 struct packet_type {
4703 ovs_be16 ns;
4704 ovs_be16 id;
4705 };
4706 SCAN_BEGIN("packet_type(", struct packet_type) {
4707 SCAN_FIELD("ns=", be16, ns);
4708 SCAN_FIELD("id=", be16, id);
4709 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE);
4710
4711 SCAN_BEGIN("nsh(", struct ovs_key_nsh) {
4712 SCAN_FIELD("flags=", u8, flags);
4713 SCAN_FIELD("mdtype=", u8, mdtype);
4714 SCAN_FIELD("np=", u8, np);
4715 SCAN_FIELD("path_hdr=", be32, path_hdr);
4716 SCAN_FIELD("c1=", be32, c[0]);
4717 SCAN_FIELD("c2=", be32, c[1]);
4718 SCAN_FIELD("c3=", be32, c[2]);
4719 SCAN_FIELD("c4=", be32, c[2]);
4720 } SCAN_END(OVS_KEY_ATTR_NSH);
4721
4722 /* Encap open-coded. */
4723 if (!strncmp(s, "encap(", 6)) {
4724 const char *start = s;
4725 size_t encap, encap_mask = 0;
4726
4727 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
4728 if (mask) {
4729 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
4730 }
4731
4732 s += 6;
4733 for (;;) {
4734 int retval;
4735
4736 s += strspn(s, delimiters);
4737 if (!*s) {
4738 return -EINVAL;
4739 } else if (*s == ')') {
4740 break;
4741 }
4742
4743 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4744 if (retval < 0) {
4745 return retval;
4746 }
4747 s += retval;
4748 }
4749 s++;
4750
4751 nl_msg_end_nested(key, encap);
4752 if (mask) {
4753 nl_msg_end_nested(mask, encap_mask);
4754 }
4755
4756 return s - start;
4757 }
4758
4759 return -EINVAL;
4760 }
4761
4762 /* Parses the string representation of a datapath flow key, in the
4763 * format output by odp_flow_key_format(). Returns 0 if successful,
4764 * otherwise a positive errno value. On success, the flow key is
4765 * appended to 'key' as a series of Netlink attributes. On failure, no
4766 * data is appended to 'key'. Either way, 'key''s data might be
4767 * reallocated.
4768 *
4769 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4770 * to a port number. (Port names may be used instead of port numbers in
4771 * in_port.)
4772 *
4773 * On success, the attributes appended to 'key' are individually syntactically
4774 * valid, but they may not be valid as a sequence. 'key' might, for example,
4775 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4776 int
4777 odp_flow_from_string(const char *s, const struct simap *port_names,
4778 struct ofpbuf *key, struct ofpbuf *mask)
4779 {
4780 const size_t old_size = key->size;
4781 for (;;) {
4782 int retval;
4783
4784 s += strspn(s, delimiters);
4785 if (!*s) {
4786 return 0;
4787 }
4788
4789 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4790 if (retval < 0) {
4791 key->size = old_size;
4792 return -retval;
4793 }
4794 s += retval;
4795 }
4796
4797 return 0;
4798 }
4799
4800 static uint8_t
4801 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
4802 {
4803 if (is_mask) {
4804 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4805 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4806 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4807 * must use a zero mask for the netlink frag field, and all ones mask
4808 * otherwise. */
4809 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
4810 }
4811 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
4812 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
4813 : OVS_FRAG_TYPE_FIRST;
4814 }
4815
4816 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
4817 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
4818 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
4819 bool is_mask);
4820 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
4821 bool is_mask);
4822 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
4823 bool is_mask);
4824 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
4825 bool is_mask);
4826 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
4827 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
4828 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
4829 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
4830 static void get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh,
4831 bool is_mask);
4832 static void put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow,
4833 bool is_mask);
4834
4835 /* These share the same layout. */
4836 union ovs_key_tp {
4837 struct ovs_key_tcp tcp;
4838 struct ovs_key_udp udp;
4839 struct ovs_key_sctp sctp;
4840 };
4841
4842 static void get_tp_key(const struct flow *, union ovs_key_tp *);
4843 static void put_tp_key(const union ovs_key_tp *, struct flow *);
4844
4845 static void
4846 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
4847 bool export_mask, struct ofpbuf *buf)
4848 {
4849 struct ovs_key_ethernet *eth_key;
4850 size_t encap[FLOW_MAX_VLAN_HEADERS] = {0};
4851 size_t max_vlans;
4852 const struct flow *flow = parms->flow;
4853 const struct flow *mask = parms->mask;
4854 const struct flow *data = export_mask ? mask : flow;
4855
4856 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
4857
4858 if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) {
4859 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
4860 parms->key_buf);
4861 }
4862
4863 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
4864
4865 if (parms->support.ct_state) {
4866 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4867 ovs_to_odp_ct_state(data->ct_state));
4868 }
4869 if (parms->support.ct_zone) {
4870 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
4871 }
4872 if (parms->support.ct_mark) {
4873 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
4874 }
4875 if (parms->support.ct_label) {
4876 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
4877 sizeof(data->ct_label));
4878 }
4879 if (flow->ct_nw_proto) {
4880 if (parms->support.ct_orig_tuple
4881 && flow->dl_type == htons(ETH_TYPE_IP)) {
4882 struct ovs_key_ct_tuple_ipv4 ct = {
4883 data->ct_nw_src,
4884 data->ct_nw_dst,
4885 data->ct_tp_src,
4886 data->ct_tp_dst,
4887 data->ct_nw_proto,
4888 };
4889 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, &ct,
4890 sizeof ct);
4891 } else if (parms->support.ct_orig_tuple6
4892 && flow->dl_type == htons(ETH_TYPE_IPV6)) {
4893 struct ovs_key_ct_tuple_ipv6 ct = {
4894 data->ct_ipv6_src,
4895 data->ct_ipv6_dst,
4896 data->ct_tp_src,
4897 data->ct_tp_dst,
4898 data->ct_nw_proto,
4899 };
4900 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, &ct,
4901 sizeof ct);
4902 }
4903 }
4904 if (parms->support.recirc) {
4905 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
4906 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
4907 }
4908
4909 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4910 * is not the magical value "ODPP_NONE". */
4911 if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
4912 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
4913 }
4914
4915 nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type);
4916
4917 if (OVS_UNLIKELY(parms->probe)) {
4918 max_vlans = FLOW_MAX_VLAN_HEADERS;
4919 } else {
4920 max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit);
4921 }
4922
4923 /* Conditionally add L2 attributes for Ethernet packets */
4924 if (flow->packet_type == htonl(PT_ETH)) {
4925 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
4926 sizeof *eth_key);
4927 get_ethernet_key(data, eth_key);
4928
4929 for (int encaps = 0; encaps < max_vlans; encaps++) {
4930 ovs_be16 tpid = flow->vlans[encaps].tpid;
4931
4932 if (flow->vlans[encaps].tci == htons(0)) {
4933 if (eth_type_vlan(flow->dl_type)) {
4934 /* If VLAN was truncated the tpid is in dl_type */
4935 tpid = flow->dl_type;
4936 } else {
4937 break;
4938 }
4939 }
4940
4941 if (export_mask) {
4942 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4943 } else {
4944 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid);
4945 }
4946 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci);
4947 encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
4948 if (flow->vlans[encaps].tci == htons(0)) {
4949 goto unencap;
4950 }
4951 }
4952 }
4953
4954 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4955 /* For backwards compatibility with kernels that don't support
4956 * wildcarding, the following convention is used to encode the
4957 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4958 *
4959 * key mask matches
4960 * -------- -------- -------
4961 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4962 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4963 * <none> 0xffff Any non-Ethernet II frame (except valid
4964 * 802.3 SNAP packet with valid eth_type).
4965 */
4966 if (export_mask) {
4967 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4968 }
4969 goto unencap;
4970 }
4971
4972 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
4973
4974 if (eth_type_vlan(flow->dl_type)) {
4975 goto unencap;
4976 }
4977
4978 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4979 struct ovs_key_ipv4 *ipv4_key;
4980
4981 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
4982 sizeof *ipv4_key);
4983 get_ipv4_key(data, ipv4_key, export_mask);
4984 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
4985 struct ovs_key_ipv6 *ipv6_key;
4986
4987 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
4988 sizeof *ipv6_key);
4989 get_ipv6_key(data, ipv6_key, export_mask);
4990 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
4991 flow->dl_type == htons(ETH_TYPE_RARP)) {
4992 struct ovs_key_arp *arp_key;
4993
4994 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
4995 sizeof *arp_key);
4996 get_arp_key(data, arp_key);
4997 } else if (eth_type_mpls(flow->dl_type)) {
4998 struct ovs_key_mpls *mpls_key;
4999 int i, n;
5000
5001 n = flow_count_mpls_labels(flow, NULL);
5002 if (export_mask) {
5003 n = MIN(n, parms->support.max_mpls_depth);
5004 }
5005 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
5006 n * sizeof *mpls_key);
5007 for (i = 0; i < n; i++) {
5008 mpls_key[i].mpls_lse = data->mpls_lse[i];
5009 }
5010 } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
5011 struct ovs_key_nsh *nsh_key;
5012
5013 nsh_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_NSH,
5014 sizeof *nsh_key);
5015 get_nsh_key(data, nsh_key, export_mask);
5016 }
5017
5018 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5019 if (flow->nw_proto == IPPROTO_TCP) {
5020 union ovs_key_tp *tcp_key;
5021
5022 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
5023 sizeof *tcp_key);
5024 get_tp_key(data, tcp_key);
5025 if (data->tcp_flags || (mask && mask->tcp_flags)) {
5026 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
5027 }
5028 } else if (flow->nw_proto == IPPROTO_UDP) {
5029 union ovs_key_tp *udp_key;
5030
5031 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
5032 sizeof *udp_key);
5033 get_tp_key(data, udp_key);
5034 } else if (flow->nw_proto == IPPROTO_SCTP) {
5035 union ovs_key_tp *sctp_key;
5036
5037 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
5038 sizeof *sctp_key);
5039 get_tp_key(data, sctp_key);
5040 } else if (flow->dl_type == htons(ETH_TYPE_IP)
5041 && flow->nw_proto == IPPROTO_ICMP) {
5042 struct ovs_key_icmp *icmp_key;
5043
5044 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
5045 sizeof *icmp_key);
5046 icmp_key->icmp_type = ntohs(data->tp_src);
5047 icmp_key->icmp_code = ntohs(data->tp_dst);
5048 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
5049 && flow->nw_proto == IPPROTO_ICMPV6) {
5050 struct ovs_key_icmpv6 *icmpv6_key;
5051
5052 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
5053 sizeof *icmpv6_key);
5054 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
5055 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
5056
5057 if (is_nd(flow, NULL)
5058 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
5059 * type and code are 8 bits wide. Therefore, an exact match
5060 * looks like htons(0xff), not htons(0xffff). See
5061 * xlate_wc_finish() for details. */
5062 && (!export_mask || (data->tp_src == htons(0xff)
5063 && data->tp_dst == htons(0xff)))) {
5064
5065 struct ovs_key_nd *nd_key;
5066
5067 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
5068 sizeof *nd_key);
5069 nd_key->nd_target = data->nd_target;
5070 nd_key->nd_sll = data->arp_sha;
5071 nd_key->nd_tll = data->arp_tha;
5072 }
5073 }
5074 }
5075
5076 unencap:
5077 for (int encaps = max_vlans - 1; encaps >= 0; encaps--) {
5078 if (encap[encaps]) {
5079 nl_msg_end_nested(buf, encap[encaps]);
5080 }
5081 }
5082 }
5083
5084 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
5085 *
5086 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5087 * capable of being expanded to allow for that much space. */
5088 void
5089 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
5090 struct ofpbuf *buf)
5091 {
5092 odp_flow_key_from_flow__(parms, false, buf);
5093 }
5094
5095 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
5096 * 'buf'.
5097 *
5098 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5099 * capable of being expanded to allow for that much space. */
5100 void
5101 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
5102 struct ofpbuf *buf)
5103 {
5104 odp_flow_key_from_flow__(parms, true, buf);
5105 }
5106
5107 /* Generate ODP flow key from the given packet metadata */
5108 void
5109 odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet)
5110 {
5111 const struct pkt_metadata *md = &packet->md;
5112
5113 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
5114
5115 if (flow_tnl_dst_is_set(&md->tunnel)) {
5116 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
5117 }
5118
5119 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
5120
5121 if (md->ct_state) {
5122 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
5123 ovs_to_odp_ct_state(md->ct_state));
5124 if (md->ct_zone) {
5125 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
5126 }
5127 if (md->ct_mark) {
5128 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
5129 }
5130 if (!ovs_u128_is_zero(md->ct_label)) {
5131 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
5132 sizeof(md->ct_label));
5133 }
5134 if (md->ct_orig_tuple_ipv6) {
5135 if (md->ct_orig_tuple.ipv6.ipv6_proto) {
5136 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
5137 &md->ct_orig_tuple.ipv6,
5138 sizeof md->ct_orig_tuple.ipv6);
5139 }
5140 } else {
5141 if (md->ct_orig_tuple.ipv4.ipv4_proto) {
5142 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
5143 &md->ct_orig_tuple.ipv4,
5144 sizeof md->ct_orig_tuple.ipv4);
5145 }
5146 }
5147 }
5148
5149 /* Add an ingress port attribute if 'odp_in_port' is not the magical
5150 * value "ODPP_NONE". */
5151 if (md->in_port.odp_port != ODPP_NONE) {
5152 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
5153 }
5154
5155 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
5156 if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) {
5157 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE,
5158 pt_ns_type_be(packet->packet_type));
5159 }
5160 }
5161
5162 /* Generate packet metadata from the given ODP flow key. */
5163 void
5164 odp_key_to_dp_packet(const struct nlattr *key, size_t key_len,
5165 struct dp_packet *packet)
5166 {
5167 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5168 const struct nlattr *nla;
5169 struct pkt_metadata *md = &packet->md;
5170 ovs_be32 packet_type = htonl(PT_UNKNOWN);
5171 ovs_be16 ethertype = 0;
5172 size_t left;
5173
5174 pkt_metadata_init(md, ODPP_NONE);
5175
5176 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
5177 enum ovs_key_attr type = nl_attr_type(nla);
5178 size_t len = nl_attr_get_size(nla);
5179 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
5180 OVS_KEY_ATTR_MAX, type);
5181
5182 if (len != expected_len && expected_len >= 0) {
5183 continue;
5184 }
5185
5186 switch (type) {
5187 case OVS_KEY_ATTR_RECIRC_ID:
5188 md->recirc_id = nl_attr_get_u32(nla);
5189 break;
5190 case OVS_KEY_ATTR_DP_HASH:
5191 md->dp_hash = nl_attr_get_u32(nla);
5192 break;
5193 case OVS_KEY_ATTR_PRIORITY:
5194 md->skb_priority = nl_attr_get_u32(nla);
5195 break;
5196 case OVS_KEY_ATTR_SKB_MARK:
5197 md->pkt_mark = nl_attr_get_u32(nla);
5198 break;
5199 case OVS_KEY_ATTR_CT_STATE:
5200 md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
5201 break;
5202 case OVS_KEY_ATTR_CT_ZONE:
5203 md->ct_zone = nl_attr_get_u16(nla);
5204 break;
5205 case OVS_KEY_ATTR_CT_MARK:
5206 md->ct_mark = nl_attr_get_u32(nla);
5207 break;
5208 case OVS_KEY_ATTR_CT_LABELS: {
5209 md->ct_label = nl_attr_get_u128(nla);
5210 break;
5211 }
5212 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
5213 const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla);
5214 md->ct_orig_tuple.ipv4 = *ct;
5215 md->ct_orig_tuple_ipv6 = false;
5216 break;
5217 }
5218 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
5219 const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla);
5220
5221 md->ct_orig_tuple.ipv6 = *ct;
5222 md->ct_orig_tuple_ipv6 = true;
5223 break;
5224 }
5225 case OVS_KEY_ATTR_TUNNEL: {
5226 enum odp_key_fitness res;
5227
5228 res = odp_tun_key_from_attr(nla, &md->tunnel);
5229 if (res == ODP_FIT_ERROR) {
5230 memset(&md->tunnel, 0, sizeof md->tunnel);
5231 }
5232 break;
5233 }
5234 case OVS_KEY_ATTR_IN_PORT:
5235 md->in_port.odp_port = nl_attr_get_odp_port(nla);
5236 break;
5237 case OVS_KEY_ATTR_ETHERNET:
5238 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
5239 packet_type = htonl(PT_ETH);
5240 break;
5241 case OVS_KEY_ATTR_ETHERTYPE:
5242 ethertype = nl_attr_get_be16(nla);
5243 break;
5244 case OVS_KEY_ATTR_UNSPEC:
5245 case OVS_KEY_ATTR_ENCAP:
5246 case OVS_KEY_ATTR_VLAN:
5247 case OVS_KEY_ATTR_IPV4:
5248 case OVS_KEY_ATTR_IPV6:
5249 case OVS_KEY_ATTR_TCP:
5250 case OVS_KEY_ATTR_UDP:
5251 case OVS_KEY_ATTR_ICMP:
5252 case OVS_KEY_ATTR_ICMPV6:
5253 case OVS_KEY_ATTR_ARP:
5254 case OVS_KEY_ATTR_ND:
5255 case OVS_KEY_ATTR_SCTP:
5256 case OVS_KEY_ATTR_TCP_FLAGS:
5257 case OVS_KEY_ATTR_MPLS:
5258 case OVS_KEY_ATTR_PACKET_TYPE:
5259 case OVS_KEY_ATTR_NSH:
5260 case __OVS_KEY_ATTR_MAX:
5261 default:
5262 break;
5263 }
5264 }
5265
5266 if (packet_type == htonl(PT_ETH)) {
5267 packet->packet_type = htonl(PT_ETH);
5268 } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) {
5269 packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
5270 ntohs(ethertype));
5271 } else {
5272 VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type.");
5273 }
5274 }
5275
5276 uint32_t
5277 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
5278 {
5279 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
5280 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key), key_len, 0);
5281 }
5282
5283 static void
5284 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
5285 uint64_t attrs, int out_of_range_attr,
5286 const struct nlattr *key, size_t key_len)
5287 {
5288 struct ds s;
5289 int i;
5290
5291 if (VLOG_DROP_DBG(rl)) {
5292 return;
5293 }
5294
5295 ds_init(&s);
5296 for (i = 0; i < 64; i++) {
5297 if (attrs & (UINT64_C(1) << i)) {
5298 char namebuf[OVS_KEY_ATTR_BUFSIZE];
5299
5300 ds_put_format(&s, " %s",
5301 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
5302 }
5303 }
5304 if (out_of_range_attr) {
5305 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
5306 }
5307
5308 ds_put_cstr(&s, ": ");
5309 odp_flow_key_format(key, key_len, &s);
5310
5311 VLOG_DBG("%s:%s", title, ds_cstr(&s));
5312 ds_destroy(&s);
5313 }
5314
5315 static uint8_t
5316 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
5317 {
5318 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5319
5320 if (is_mask) {
5321 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
5322 }
5323
5324 if (odp_frag > OVS_FRAG_TYPE_LATER) {
5325 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
5326 return 0xff; /* Error. */
5327 }
5328
5329 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
5330 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
5331 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
5332 }
5333
5334 static bool
5335 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
5336 const struct nlattr *attrs[], uint64_t *present_attrsp,
5337 int *out_of_range_attrp)
5338 {
5339 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
5340 const struct nlattr *nla;
5341 uint64_t present_attrs;
5342 size_t left;
5343
5344 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
5345 present_attrs = 0;
5346 *out_of_range_attrp = 0;
5347 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
5348 uint16_t type = nl_attr_type(nla);
5349 size_t len = nl_attr_get_size(nla);
5350 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
5351 OVS_KEY_ATTR_MAX, type);
5352
5353 if (len != expected_len && expected_len >= 0) {
5354 char namebuf[OVS_KEY_ATTR_BUFSIZE];
5355
5356 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
5357 "length %d", ovs_key_attr_to_string(type, namebuf,
5358 sizeof namebuf),
5359 len, expected_len);
5360 return false;
5361 }
5362
5363 if (type > OVS_KEY_ATTR_MAX) {
5364 *out_of_range_attrp = type;
5365 } else {
5366 if (present_attrs & (UINT64_C(1) << type)) {
5367 char namebuf[OVS_KEY_ATTR_BUFSIZE];
5368
5369 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
5370 ovs_key_attr_to_string(type,
5371 namebuf, sizeof namebuf));
5372 return false;
5373 }
5374
5375 present_attrs |= UINT64_C(1) << type;
5376 attrs[type] = nla;
5377 }
5378 }
5379 if (left) {
5380 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
5381 return false;
5382 }
5383
5384 *present_attrsp = present_attrs;
5385 return true;
5386 }
5387
5388 static enum odp_key_fitness
5389 check_expectations(uint64_t present_attrs, int out_of_range_attr,
5390 uint64_t expected_attrs,
5391 const struct nlattr *key, size_t key_len)
5392 {
5393 uint64_t missing_attrs;
5394 uint64_t extra_attrs;
5395
5396 missing_attrs = expected_attrs & ~present_attrs;
5397 if (missing_attrs) {
5398 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
5399 log_odp_key_attributes(&rl, "expected but not present",
5400 missing_attrs, 0, key, key_len);
5401 return ODP_FIT_TOO_LITTLE;
5402 }
5403
5404 extra_attrs = present_attrs & ~expected_attrs;
5405 if (extra_attrs || out_of_range_attr) {
5406 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
5407 log_odp_key_attributes(&rl, "present but not expected",
5408 extra_attrs, out_of_range_attr, key, key_len);
5409 return ODP_FIT_TOO_MUCH;
5410 }
5411
5412 return ODP_FIT_PERFECT;
5413 }
5414
5415 static bool
5416 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
5417 uint64_t present_attrs, uint64_t *expected_attrs,
5418 struct flow *flow, const struct flow *src_flow)
5419 {
5420 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5421 bool is_mask = flow != src_flow;
5422
5423 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
5424 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
5425 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
5426 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
5427 ntohs(flow->dl_type));
5428 return false;
5429 }
5430 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
5431 flow->dl_type != htons(0xffff)) {
5432 return false;
5433 }
5434 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
5435 } else {
5436 if (!is_mask) {
5437 /* Default ethertype for well-known L3 packets. */
5438 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
5439 flow->dl_type = htons(ETH_TYPE_IP);
5440 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
5441 flow->dl_type = htons(ETH_TYPE_IPV6);
5442 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
5443 flow->dl_type = htons(ETH_TYPE_MPLS);
5444 } else {
5445 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
5446 }
5447 } else if (src_flow->packet_type != htonl(PT_ETH)) {
5448 /* dl_type is mandatory for non-Ethernet packets */
5449 flow->dl_type = htons(0xffff);
5450 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
5451 /* See comments in odp_flow_key_from_flow__(). */
5452 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
5453 return false;
5454 }
5455 }
5456 return true;
5457 }
5458
5459 static enum odp_key_fitness
5460 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
5461 uint64_t present_attrs, int out_of_range_attr,
5462 uint64_t expected_attrs, struct flow *flow,
5463 const struct nlattr *key, size_t key_len,
5464 const struct flow *src_flow)
5465 {
5466 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5467 bool is_mask = src_flow != flow;
5468 const void *check_start = NULL;
5469 size_t check_len = 0;
5470 enum ovs_key_attr expected_bit = 0xff;
5471
5472 if (eth_type_mpls(src_flow->dl_type)) {
5473 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
5474 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
5475 }
5476 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
5477 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
5478 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
5479 int n = size / sizeof(ovs_be32);
5480 int i;
5481
5482 if (!size || size % sizeof(ovs_be32)) {
5483 return ODP_FIT_ERROR;
5484 }
5485 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
5486 return ODP_FIT_ERROR;
5487 }
5488
5489 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
5490 flow->mpls_lse[i] = mpls_lse[i];
5491 }
5492 if (n > FLOW_MAX_MPLS_LABELS) {
5493 return ODP_FIT_TOO_MUCH;
5494 }
5495
5496 if (!is_mask) {
5497 /* BOS may be set only in the innermost label. */
5498 for (i = 0; i < n - 1; i++) {
5499 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
5500 return ODP_FIT_ERROR;
5501 }
5502 }
5503
5504 /* BOS must be set in the innermost label. */
5505 if (n < FLOW_MAX_MPLS_LABELS
5506 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
5507 return ODP_FIT_TOO_LITTLE;
5508 }
5509 }
5510 }
5511
5512 goto done;
5513 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
5514 if (!is_mask) {
5515 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
5516 }
5517 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
5518 const struct ovs_key_ipv4 *ipv4_key;
5519
5520 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
5521 put_ipv4_key(ipv4_key, flow, is_mask);
5522 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
5523 return ODP_FIT_ERROR;
5524 }
5525 if (is_mask) {
5526 check_start = ipv4_key;
5527 check_len = sizeof *ipv4_key;
5528 expected_bit = OVS_KEY_ATTR_IPV4;
5529 }
5530 }
5531 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
5532 if (!is_mask) {
5533 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
5534 }
5535 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
5536 const struct ovs_key_ipv6 *ipv6_key;
5537
5538 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
5539 put_ipv6_key(ipv6_key, flow, is_mask);
5540 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
5541 return ODP_FIT_ERROR;
5542 }
5543 if (is_mask) {
5544 check_start = ipv6_key;
5545 check_len = sizeof *ipv6_key;
5546 expected_bit = OVS_KEY_ATTR_IPV6;
5547 }
5548 }
5549 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
5550 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
5551 if (!is_mask) {
5552 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
5553 }
5554 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
5555 const struct ovs_key_arp *arp_key;
5556
5557 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
5558 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
5559 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
5560 "key", ntohs(arp_key->arp_op));
5561 return ODP_FIT_ERROR;
5562 }
5563 put_arp_key(arp_key, flow);
5564 if (is_mask) {
5565 check_start = arp_key;
5566 check_len = sizeof *arp_key;
5567 expected_bit = OVS_KEY_ATTR_ARP;
5568 }
5569 }
5570 } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) {
5571 if (!is_mask) {
5572 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH;
5573 }
5574 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) {
5575 const struct ovs_key_nsh *nsh_key;
5576
5577 nsh_key = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]);
5578 put_nsh_key(nsh_key, flow, false);
5579 if (is_mask) {
5580 check_start = nsh_key;
5581 check_len = sizeof *nsh_key;
5582 expected_bit = OVS_KEY_ATTR_NSH;
5583 }
5584 }
5585 } else {
5586 goto done;
5587 }
5588 if (check_len > 0) { /* Happens only when 'is_mask'. */
5589 if (!is_all_zeros(check_start, check_len) &&
5590 flow->dl_type != htons(0xffff)) {
5591 return ODP_FIT_ERROR;
5592 } else {
5593 expected_attrs |= UINT64_C(1) << expected_bit;
5594 }
5595 }
5596
5597 expected_bit = OVS_KEY_ATTR_UNSPEC;
5598 if (src_flow->nw_proto == IPPROTO_TCP
5599 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
5600 src_flow->dl_type == htons(ETH_TYPE_IPV6))
5601 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5602 if (!is_mask) {
5603 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
5604 }
5605 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
5606 const union ovs_key_tp *tcp_key;
5607
5608 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
5609 put_tp_key(tcp_key, flow);
5610 expected_bit = OVS_KEY_ATTR_TCP;
5611 }
5612 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
5613 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
5614 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
5615 }
5616 } else if (src_flow->nw_proto == IPPROTO_UDP
5617 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
5618 src_flow->dl_type == htons(ETH_TYPE_IPV6))
5619 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5620 if (!is_mask) {
5621 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
5622 }
5623 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
5624 const union ovs_key_tp *udp_key;
5625
5626 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
5627 put_tp_key(udp_key, flow);
5628 expected_bit = OVS_KEY_ATTR_UDP;
5629 }
5630 } else if (src_flow->nw_proto == IPPROTO_SCTP
5631 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
5632 src_flow->dl_type == htons(ETH_TYPE_IPV6))
5633 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5634 if (!is_mask) {
5635 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
5636 }
5637 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
5638 const union ovs_key_tp *sctp_key;
5639
5640 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
5641 put_tp_key(sctp_key, flow);
5642 expected_bit = OVS_KEY_ATTR_SCTP;
5643 }
5644 } else if (src_flow->nw_proto == IPPROTO_ICMP
5645 && src_flow->dl_type == htons(ETH_TYPE_IP)
5646 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5647 if (!is_mask) {
5648 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
5649 }
5650 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
5651 const struct ovs_key_icmp *icmp_key;
5652
5653 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
5654 flow->tp_src = htons(icmp_key->icmp_type);
5655 flow->tp_dst = htons(icmp_key->icmp_code);
5656 expected_bit = OVS_KEY_ATTR_ICMP;
5657 }
5658 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
5659 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
5660 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
5661 if (!is_mask) {
5662 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
5663 }
5664 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
5665 const struct ovs_key_icmpv6 *icmpv6_key;
5666
5667 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
5668 flow->tp_src = htons(icmpv6_key->icmpv6_type);
5669 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
5670 expected_bit = OVS_KEY_ATTR_ICMPV6;
5671 if (is_nd(src_flow, NULL)) {
5672 if (!is_mask) {
5673 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
5674 }
5675 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
5676 const struct ovs_key_nd *nd_key;
5677
5678 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
5679 flow->nd_target = nd_key->nd_target;
5680 flow->arp_sha = nd_key->nd_sll;
5681 flow->arp_tha = nd_key->nd_tll;
5682 if (is_mask) {
5683 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
5684 * ICMP type and code are 8 bits wide. Therefore, an
5685 * exact match looks like htons(0xff), not
5686 * htons(0xffff). See xlate_wc_finish() for details.
5687 * */
5688 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
5689 (flow->tp_src != htons(0xff) ||
5690 flow->tp_dst != htons(0xff))) {
5691 return ODP_FIT_ERROR;
5692 } else {
5693 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
5694 }
5695 }
5696 }
5697 }
5698 }
5699 }
5700 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
5701 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
5702 return ODP_FIT_ERROR;
5703 } else {
5704 expected_attrs |= UINT64_C(1) << expected_bit;
5705 }
5706 }
5707
5708 done:
5709 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
5710 key, key_len);
5711 }
5712
5713 /* Parse 802.1Q header then encapsulated L3 attributes. */
5714 static enum odp_key_fitness
5715 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
5716 uint64_t present_attrs, int out_of_range_attr,
5717 uint64_t expected_attrs, struct flow *flow,
5718 const struct nlattr *key, size_t key_len,
5719 const struct flow *src_flow)
5720 {
5721 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5722 bool is_mask = src_flow != flow;
5723
5724 const struct nlattr *encap;
5725 enum odp_key_fitness encap_fitness;
5726 enum odp_key_fitness fitness = ODP_FIT_ERROR;
5727 int encaps = 0;
5728
5729 while (encaps < flow_vlan_limit &&
5730 (is_mask
5731 ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0
5732 : eth_type_vlan(flow->dl_type))) {
5733
5734 encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
5735 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
5736
5737 /* Calculate fitness of outer attributes. */
5738 if (!is_mask) {
5739 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
5740 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
5741 } else {
5742 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
5743 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
5744 }
5745 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
5746 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
5747 }
5748 }
5749 fitness = check_expectations(present_attrs, out_of_range_attr,
5750 expected_attrs, key, key_len);
5751
5752 /* Set vlan_tci.
5753 * Remove the TPID from dl_type since it's not the real Ethertype. */
5754 flow->vlans[encaps].tpid = flow->dl_type;
5755 flow->dl_type = htons(0);
5756 flow->vlans[encaps].tci =
5757 (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
5758 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
5759 : htons(0));
5760 if (!is_mask) {
5761 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) ||
5762 !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
5763 return ODP_FIT_TOO_LITTLE;
5764 } else if (flow->vlans[encaps].tci == htons(0)) {
5765 /* Corner case for a truncated 802.1Q header. */
5766 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
5767 return ODP_FIT_TOO_MUCH;
5768 }
5769 return fitness;
5770 } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) {
5771 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
5772 "but CFI bit is not set",
5773 ntohs(flow->vlans[encaps].tci));
5774 return ODP_FIT_ERROR;
5775 }
5776 } else {
5777 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
5778 return fitness;
5779 }
5780 }
5781
5782 /* Now parse the encapsulated attributes. */
5783 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
5784 attrs, &present_attrs, &out_of_range_attr)) {
5785 return ODP_FIT_ERROR;
5786 }
5787 expected_attrs = 0;
5788
5789 if (!parse_ethertype(attrs, present_attrs, &expected_attrs,
5790 flow, src_flow)) {
5791 return ODP_FIT_ERROR;
5792 }
5793
5794 encaps++;
5795 }
5796
5797 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
5798 expected_attrs, flow, key, key_len,
5799 src_flow);
5800
5801 /* The overall fitness is the worse of the outer and inner attributes. */
5802 return MAX(fitness, encap_fitness);
5803 }
5804
5805 static enum odp_key_fitness
5806 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
5807 struct flow *flow, const struct flow *src_flow)
5808 {
5809 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
5810 uint64_t expected_attrs;
5811 uint64_t present_attrs;
5812 int out_of_range_attr;
5813 bool is_mask = src_flow != flow;
5814
5815 memset(flow, 0, sizeof *flow);
5816
5817 /* Parse attributes. */
5818 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
5819 &out_of_range_attr)) {
5820 return ODP_FIT_ERROR;
5821 }
5822 expected_attrs = 0;
5823
5824 /* Metadata. */
5825 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
5826 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
5827 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
5828 } else if (is_mask) {
5829 /* Always exact match recirc_id if it is not specified. */
5830 flow->recirc_id = UINT32_MAX;
5831 }
5832
5833 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
5834 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
5835 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
5836 }
5837 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
5838 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
5839 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
5840 }
5841
5842 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
5843 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
5844 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
5845 }
5846
5847 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
5848 uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
5849
5850 flow->ct_state = odp_to_ovs_ct_state(odp_state);
5851 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
5852 }
5853 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
5854 flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
5855 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
5856 }
5857 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
5858 flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
5859 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
5860 }
5861 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
5862 flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]);
5863 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
5864 }
5865 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
5866 const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
5867 flow->ct_nw_src = ct->ipv4_src;
5868 flow->ct_nw_dst = ct->ipv4_dst;
5869 flow->ct_nw_proto = ct->ipv4_proto;
5870 flow->ct_tp_src = ct->src_port;
5871 flow->ct_tp_dst = ct->dst_port;
5872 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
5873 }
5874 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
5875 const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
5876
5877 flow->ct_ipv6_src = ct->ipv6_src;
5878 flow->ct_ipv6_dst = ct->ipv6_dst;
5879 flow->ct_nw_proto = ct->ipv6_proto;
5880 flow->ct_tp_src = ct->src_port;
5881 flow->ct_tp_dst = ct->dst_port;
5882 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
5883 }
5884
5885 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
5886 enum odp_key_fitness res;
5887
5888 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
5889 &flow->tunnel);
5890 if (res == ODP_FIT_ERROR) {
5891 return ODP_FIT_ERROR;
5892 } else if (res == ODP_FIT_PERFECT) {
5893 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
5894 }
5895 }
5896
5897 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
5898 flow->in_port.odp_port
5899 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
5900 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
5901 } else if (!is_mask) {
5902 flow->in_port.odp_port = ODPP_NONE;
5903 }
5904
5905 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) {
5906 flow->packet_type
5907 = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]);
5908 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE;
5909 } else if (!is_mask) {
5910 flow->packet_type = htonl(PT_ETH);
5911 }
5912
5913 /* Check for Ethernet header. */
5914 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
5915 const struct ovs_key_ethernet *eth_key;
5916
5917 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
5918 put_ethernet_key(eth_key, flow);
5919 if (!is_mask) {
5920 flow->packet_type = htonl(PT_ETH);
5921 }
5922 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5923 }
5924 else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
5925 ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
5926 if (!is_mask) {
5927 flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
5928 ntohs(ethertype));
5929 }
5930 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
5931 }
5932
5933 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5934 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
5935 src_flow)) {
5936 return ODP_FIT_ERROR;
5937 }
5938
5939 if (is_mask
5940 ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0
5941 : eth_type_vlan(src_flow->dl_type)) {
5942 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
5943 expected_attrs, flow, key, key_len, src_flow);
5944 }
5945 if (is_mask) {
5946 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5947 flow->vlans[0].tpid = htons(0xffff);
5948 flow->vlans[0].tci = htons(0xffff);
5949 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
5950 flow->vlans[0].tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
5951 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
5952 }
5953 }
5954 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
5955 expected_attrs, flow, key, key_len, src_flow);
5956 }
5957
5958 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5959 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5960 * 'key' fits our expectations for what a flow key should contain.
5961 *
5962 * The 'in_port' will be the datapath's understanding of the port. The
5963 * caller will need to translate with odp_port_to_ofp_port() if the
5964 * OpenFlow port is needed.
5965 *
5966 * This function doesn't take the packet itself as an argument because none of
5967 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5968 * it is always possible to infer which additional attribute(s) should appear
5969 * by looking at the attributes for lower-level protocols, e.g. if the network
5970 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5971 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5972 * must be absent. */
5973 enum odp_key_fitness
5974 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
5975 struct flow *flow)
5976 {
5977 return odp_flow_key_to_flow__(key, key_len, flow, flow);
5978 }
5979
5980 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5981 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5982 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5983 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5984 * well 'key' fits our expectations for what a flow key should contain. */
5985 enum odp_key_fitness
5986 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
5987 struct flow_wildcards *mask, const struct flow *src_flow)
5988 {
5989 if (mask_key_len) {
5990 return odp_flow_key_to_flow__(mask_key, mask_key_len,
5991 &mask->masks, src_flow);
5992
5993 } else {
5994 /* A missing mask means that the flow should be exact matched.
5995 * Generate an appropriate exact wildcard for the flow. */
5996 flow_wildcards_init_for_packet(mask, src_flow);
5997
5998 return ODP_FIT_PERFECT;
5999 }
6000 }
6001
6002 /* Converts the netlink formated key/mask to match.
6003 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
6004 * disagree on the acceptable form of flow */
6005 int
6006 parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len,
6007 const struct nlattr *mask, size_t mask_len,
6008 struct match *match)
6009 {
6010 enum odp_key_fitness fitness;
6011
6012 fitness = odp_flow_key_to_flow(key, key_len, &match->flow);
6013 if (fitness) {
6014 /* This should not happen: it indicates that
6015 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
6016 * the acceptable form of a flow. Log the problem as an error,
6017 * with enough details to enable debugging. */
6018 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6019
6020 if (!VLOG_DROP_ERR(&rl)) {
6021 struct ds s;
6022
6023 ds_init(&s);
6024 odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
6025 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
6026 ds_destroy(&s);
6027 }
6028
6029 return EINVAL;
6030 }
6031
6032 fitness = odp_flow_key_to_mask(mask, mask_len, &match->wc, &match->flow);
6033 if (fitness) {
6034 /* This should not happen: it indicates that
6035 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
6036 * disagree on the acceptable form of a mask. Log the problem
6037 * as an error, with enough details to enable debugging. */
6038 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6039
6040 if (!VLOG_DROP_ERR(&rl)) {
6041 struct ds s;
6042
6043 ds_init(&s);
6044 odp_flow_format(key, key_len, mask, mask_len, NULL, &s,
6045 true);
6046 VLOG_ERR("internal error parsing flow mask %s (%s)",
6047 ds_cstr(&s), odp_key_fitness_to_string(fitness));
6048 ds_destroy(&s);
6049 }
6050
6051 return EINVAL;
6052 }
6053
6054 return 0;
6055 }
6056
6057 /* Returns 'fitness' as a string, for use in debug messages. */
6058 const char *
6059 odp_key_fitness_to_string(enum odp_key_fitness fitness)
6060 {
6061 switch (fitness) {
6062 case ODP_FIT_PERFECT:
6063 return "OK";
6064 case ODP_FIT_TOO_MUCH:
6065 return "too_much";
6066 case ODP_FIT_TOO_LITTLE:
6067 return "too_little";
6068 case ODP_FIT_ERROR:
6069 return "error";
6070 default:
6071 return "<unknown>";
6072 }
6073 }
6074
6075 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
6076 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
6077 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
6078 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
6079 * null, then the return value is not meaningful.) */
6080 size_t
6081 odp_put_userspace_action(uint32_t pid,
6082 const void *userdata, size_t userdata_size,
6083 odp_port_t tunnel_out_port,
6084 bool include_actions,
6085 struct ofpbuf *odp_actions)
6086 {
6087 size_t userdata_ofs;
6088 size_t offset;
6089
6090 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
6091 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
6092 if (userdata) {
6093 userdata_ofs = odp_actions->size + NLA_HDRLEN;
6094
6095 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
6096 * module before Linux 3.10 required the userdata to be exactly 8 bytes
6097 * long:
6098 *
6099 * - The kernel rejected shorter userdata with -ERANGE.
6100 *
6101 * - The kernel silently dropped userdata beyond the first 8 bytes.
6102 *
6103 * Thus, for maximum compatibility, always put at least 8 bytes. (We
6104 * separately disable features that required more than 8 bytes.) */
6105 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
6106 MAX(8, userdata_size)),
6107 userdata, userdata_size);
6108 } else {
6109 userdata_ofs = 0;
6110 }
6111 if (tunnel_out_port != ODPP_NONE) {
6112 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
6113 tunnel_out_port);
6114 }
6115 if (include_actions) {
6116 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
6117 }
6118 nl_msg_end_nested(odp_actions, offset);
6119
6120 return userdata_ofs;
6121 }
6122
6123 void
6124 odp_put_pop_eth_action(struct ofpbuf *odp_actions)
6125 {
6126 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH);
6127 }
6128
6129 void
6130 odp_put_push_eth_action(struct ofpbuf *odp_actions,
6131 const struct eth_addr *eth_src,
6132 const struct eth_addr *eth_dst)
6133 {
6134 struct ovs_action_push_eth eth;
6135
6136 memset(&eth, 0, sizeof eth);
6137 if (eth_src) {
6138 eth.addresses.eth_src = *eth_src;
6139 }
6140 if (eth_dst) {
6141 eth.addresses.eth_dst = *eth_dst;
6142 }
6143
6144 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH,
6145 &eth, sizeof eth);
6146 }
6147
6148 void
6149 odp_put_tunnel_action(const struct flow_tnl *tunnel,
6150 struct ofpbuf *odp_actions)
6151 {
6152 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
6153 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
6154 nl_msg_end_nested(odp_actions, offset);
6155 }
6156
6157 void
6158 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
6159 struct ovs_action_push_tnl *data)
6160 {
6161 int size = offsetof(struct ovs_action_push_tnl, header);
6162
6163 size += data->header_len;
6164 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
6165 }
6166
6167 \f
6168 /* The commit_odp_actions() function and its helpers. */
6169
6170 static void
6171 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
6172 const void *key, size_t key_size)
6173 {
6174 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
6175 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
6176 nl_msg_end_nested(odp_actions, offset);
6177 }
6178
6179 /* Masked set actions have a mask following the data within the netlink
6180 * attribute. The unmasked bits in the data will be cleared as the data
6181 * is copied to the action. */
6182 void
6183 commit_masked_set_action(struct ofpbuf *odp_actions,
6184 enum ovs_key_attr key_type,
6185 const void *key_, const void *mask_, size_t key_size)
6186 {
6187 size_t offset = nl_msg_start_nested(odp_actions,
6188 OVS_ACTION_ATTR_SET_MASKED);
6189 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
6190 const char *key = key_, *mask = mask_;
6191
6192 memcpy(data + key_size, mask, key_size);
6193 /* Clear unmasked bits while copying. */
6194 while (key_size--) {
6195 *data++ = *key++ & *mask++;
6196 }
6197 nl_msg_end_nested(odp_actions, offset);
6198 }
6199
6200 /* If any of the flow key data that ODP actions can modify are different in
6201 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
6202 * 'odp_actions' that change the flow tunneling information in key from
6203 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
6204 * same way. In other words, operates the same as commit_odp_actions(), but
6205 * only on tunneling information. */
6206 void
6207 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
6208 struct ofpbuf *odp_actions)
6209 {
6210 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
6211 * must have non-zero ipv6_dst. */
6212 if (flow_tnl_dst_is_set(&flow->tunnel)) {
6213 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
6214 return;
6215 }
6216 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
6217 odp_put_tunnel_action(&base->tunnel, odp_actions);
6218 }
6219 }
6220
6221 static bool
6222 commit(enum ovs_key_attr attr, bool use_masked_set,
6223 const void *key, void *base, void *mask, size_t size,
6224 struct ofpbuf *odp_actions)
6225 {
6226 if (memcmp(key, base, size)) {
6227 bool fully_masked = odp_mask_is_exact(attr, mask, size);
6228
6229 if (use_masked_set && !fully_masked) {
6230 commit_masked_set_action(odp_actions, attr, key, mask, size);
6231 } else {
6232 if (!fully_masked) {
6233 memset(mask, 0xff, size);
6234 }
6235 commit_set_action(odp_actions, attr, key, size);
6236 }
6237 memcpy(base, key, size);
6238 return true;
6239 } else {
6240 /* Mask bits are set when we have either read or set the corresponding
6241 * values. Masked bits will be exact-matched, no need to set them
6242 * if the value did not actually change. */
6243 return false;
6244 }
6245 }
6246
6247 static void
6248 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
6249 {
6250 eth->eth_src = flow->dl_src;
6251 eth->eth_dst = flow->dl_dst;
6252 }
6253
6254 static void
6255 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
6256 {
6257 flow->dl_src = eth->eth_src;
6258 flow->dl_dst = eth->eth_dst;
6259 }
6260
6261 static void
6262 commit_set_ether_action(const struct flow *flow, struct flow *base_flow,
6263 struct ofpbuf *odp_actions,
6264 struct flow_wildcards *wc,
6265 bool use_masked)
6266 {
6267 struct ovs_key_ethernet key, base, mask;
6268
6269 if (flow->packet_type != htonl(PT_ETH)) {
6270 return;
6271 }
6272
6273 get_ethernet_key(flow, &key);
6274 get_ethernet_key(base_flow, &base);
6275 get_ethernet_key(&wc->masks, &mask);
6276
6277 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
6278 &key, &base, &mask, sizeof key, odp_actions)) {
6279 put_ethernet_key(&base, base_flow);
6280 put_ethernet_key(&mask, &wc->masks);
6281 }
6282 }
6283
6284 static void
6285 commit_vlan_action(const struct flow* flow, struct flow *base,
6286 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
6287 {
6288 int base_n = flow_count_vlan_headers(base);
6289 int flow_n = flow_count_vlan_headers(flow);
6290 flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n);
6291
6292 /* Pop all mismatching vlan of base, push those of flow */
6293 for (; base_n >= 0; base_n--) {
6294 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
6295 wc->masks.vlans[base_n].qtag = OVS_BE32_MAX;
6296 }
6297
6298 for (; flow_n >= 0; flow_n--) {
6299 struct ovs_action_push_vlan vlan;
6300
6301 vlan.vlan_tpid = flow->vlans[flow_n].tpid;
6302 vlan.vlan_tci = flow->vlans[flow_n].tci;
6303 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
6304 &vlan, sizeof vlan);
6305 }
6306 memcpy(base->vlans, flow->vlans, sizeof(base->vlans));
6307 }
6308
6309 /* Wildcarding already done at action translation time. */
6310 static void
6311 commit_mpls_action(const struct flow *flow, struct flow *base,
6312 struct ofpbuf *odp_actions)
6313 {
6314 int base_n = flow_count_mpls_labels(base, NULL);
6315 int flow_n = flow_count_mpls_labels(flow, NULL);
6316 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
6317 NULL);
6318
6319 while (base_n > common_n) {
6320 if (base_n - 1 == common_n && flow_n > common_n) {
6321 /* If there is only one more LSE in base than there are common
6322 * between base and flow; and flow has at least one more LSE than
6323 * is common then the topmost LSE of base may be updated using
6324 * set */
6325 struct ovs_key_mpls mpls_key;
6326
6327 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
6328 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
6329 &mpls_key, sizeof mpls_key);
6330 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
6331 common_n++;
6332 } else {
6333 /* Otherwise, if there more LSEs in base than are common between
6334 * base and flow then pop the topmost one. */
6335 ovs_be16 dl_type;
6336 bool popped;
6337
6338 /* If all the LSEs are to be popped and this is not the outermost
6339 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
6340 * POP_MPLS action instead of flow->dl_type.
6341 *
6342 * This is because the POP_MPLS action requires its ethertype
6343 * argument to be an MPLS ethernet type but in this case
6344 * flow->dl_type will be a non-MPLS ethernet type.
6345 *
6346 * When the final POP_MPLS action occurs it use flow->dl_type and
6347 * the and the resulting packet will have the desired dl_type. */
6348 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
6349 dl_type = htons(ETH_TYPE_MPLS);
6350 } else {
6351 dl_type = flow->dl_type;
6352 }
6353 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
6354 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
6355 ovs_assert(popped);
6356 base_n--;
6357 }
6358 }
6359
6360 /* If, after the above popping and setting, there are more LSEs in flow
6361 * than base then some LSEs need to be pushed. */
6362 while (base_n < flow_n) {
6363 struct ovs_action_push_mpls *mpls;
6364
6365 mpls = nl_msg_put_unspec_zero(odp_actions,
6366 OVS_ACTION_ATTR_PUSH_MPLS,
6367 sizeof *mpls);
6368 mpls->mpls_ethertype = flow->dl_type;
6369 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
6370 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
6371 * headers if the flow is restored later due to returning from a patch
6372 * port or group bucket. */
6373 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL, false);
6374 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
6375 base_n++;
6376 }
6377 }
6378
6379 static void
6380 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
6381 {
6382 ipv4->ipv4_src = flow->nw_src;
6383 ipv4->ipv4_dst = flow->nw_dst;
6384 ipv4->ipv4_proto = flow->nw_proto;
6385 ipv4->ipv4_tos = flow->nw_tos;
6386 ipv4->ipv4_ttl = flow->nw_ttl;
6387 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
6388 }
6389
6390 static void
6391 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
6392 {
6393 flow->nw_src = ipv4->ipv4_src;
6394 flow->nw_dst = ipv4->ipv4_dst;
6395 flow->nw_proto = ipv4->ipv4_proto;
6396 flow->nw_tos = ipv4->ipv4_tos;
6397 flow->nw_ttl = ipv4->ipv4_ttl;
6398 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
6399 }
6400
6401 static void
6402 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
6403 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
6404 bool use_masked)
6405 {
6406 struct ovs_key_ipv4 key, mask, base;
6407
6408 /* Check that nw_proto and nw_frag remain unchanged. */
6409 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
6410 flow->nw_frag == base_flow->nw_frag);
6411
6412 get_ipv4_key(flow, &key, false);
6413 get_ipv4_key(base_flow, &base, false);
6414 get_ipv4_key(&wc->masks, &mask, true);
6415 mask.ipv4_proto = 0; /* Not writeable. */
6416 mask.ipv4_frag = 0; /* Not writable. */
6417
6418 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
6419 odp_actions)) {
6420 put_ipv4_key(&base, base_flow, false);
6421 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
6422 put_ipv4_key(&mask, &wc->masks, true);
6423 }
6424 }
6425 }
6426
6427 static void
6428 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
6429 {
6430 ipv6->ipv6_src = flow->ipv6_src;
6431 ipv6->ipv6_dst = flow->ipv6_dst;
6432 ipv6->ipv6_label = flow->ipv6_label;
6433 ipv6->ipv6_proto = flow->nw_proto;
6434 ipv6->ipv6_tclass = flow->nw_tos;
6435 ipv6->ipv6_hlimit = flow->nw_ttl;
6436 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
6437 }
6438
6439 static void
6440 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
6441 {
6442 flow->ipv6_src = ipv6->ipv6_src;
6443 flow->ipv6_dst = ipv6->ipv6_dst;
6444 flow->ipv6_label = ipv6->ipv6_label;
6445 flow->nw_proto = ipv6->ipv6_proto;
6446 flow->nw_tos = ipv6->ipv6_tclass;
6447 flow->nw_ttl = ipv6->ipv6_hlimit;
6448 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
6449 }
6450
6451 static void
6452 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
6453 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
6454 bool use_masked)
6455 {
6456 struct ovs_key_ipv6 key, mask, base;
6457
6458 /* Check that nw_proto and nw_frag remain unchanged. */
6459 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
6460 flow->nw_frag == base_flow->nw_frag);
6461
6462 get_ipv6_key(flow, &key, false);
6463 get_ipv6_key(base_flow, &base, false);
6464 get_ipv6_key(&wc->masks, &mask, true);
6465 mask.ipv6_proto = 0; /* Not writeable. */
6466 mask.ipv6_frag = 0; /* Not writable. */
6467
6468 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
6469 odp_actions)) {
6470 put_ipv6_key(&base, base_flow, false);
6471 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
6472 put_ipv6_key(&mask, &wc->masks, true);
6473 }
6474 }
6475 }
6476
6477 static void
6478 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
6479 {
6480 /* ARP key has padding, clear it. */
6481 memset(arp, 0, sizeof *arp);
6482
6483 arp->arp_sip = flow->nw_src;
6484 arp->arp_tip = flow->nw_dst;
6485 arp->arp_op = htons(flow->nw_proto);
6486 arp->arp_sha = flow->arp_sha;
6487 arp->arp_tha = flow->arp_tha;
6488 }
6489
6490 static void
6491 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
6492 {
6493 flow->nw_src = arp->arp_sip;
6494 flow->nw_dst = arp->arp_tip;
6495 flow->nw_proto = ntohs(arp->arp_op);
6496 flow->arp_sha = arp->arp_sha;
6497 flow->arp_tha = arp->arp_tha;
6498 }
6499
6500 static enum slow_path_reason
6501 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
6502 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
6503 {
6504 struct ovs_key_arp key, mask, base;
6505
6506 get_arp_key(flow, &key);
6507 get_arp_key(base_flow, &base);
6508 get_arp_key(&wc->masks, &mask);
6509
6510 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
6511 odp_actions)) {
6512 put_arp_key(&base, base_flow);
6513 put_arp_key(&mask, &wc->masks);
6514 return SLOW_ACTION;
6515 }
6516 return 0;
6517 }
6518
6519 static void
6520 get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
6521 {
6522 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
6523 icmp->icmp_type = ntohs(flow->tp_src);
6524 icmp->icmp_code = ntohs(flow->tp_dst);
6525 }
6526
6527 static void
6528 put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
6529 {
6530 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
6531 flow->tp_src = htons(icmp->icmp_type);
6532 flow->tp_dst = htons(icmp->icmp_code);
6533 }
6534
6535 static enum slow_path_reason
6536 commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
6537 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
6538 {
6539 struct ovs_key_icmp key, mask, base;
6540 enum ovs_key_attr attr;
6541
6542 if (is_icmpv4(flow, NULL)) {
6543 attr = OVS_KEY_ATTR_ICMP;
6544 } else if (is_icmpv6(flow, NULL)) {
6545 attr = OVS_KEY_ATTR_ICMPV6;
6546 } else {
6547 return 0;
6548 }
6549
6550 get_icmp_key(flow, &key);
6551 get_icmp_key(base_flow, &base);
6552 get_icmp_key(&wc->masks, &mask);
6553
6554 if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) {
6555 put_icmp_key(&base, base_flow);
6556 put_icmp_key(&mask, &wc->masks);
6557 return SLOW_ACTION;
6558 }
6559 return 0;
6560 }
6561
6562 static void
6563 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
6564 {
6565 nd->nd_target = flow->nd_target;
6566 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
6567 nd->nd_sll = flow->arp_sha;
6568 nd->nd_tll = flow->arp_tha;
6569 }
6570
6571 static void
6572 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
6573 {
6574 flow->nd_target = nd->nd_target;
6575 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
6576 flow->arp_sha = nd->nd_sll;
6577 flow->arp_tha = nd->nd_tll;
6578 }
6579
6580 static enum slow_path_reason
6581 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
6582 struct ofpbuf *odp_actions,
6583 struct flow_wildcards *wc, bool use_masked)
6584 {
6585 struct ovs_key_nd key, mask, base;
6586
6587 get_nd_key(flow, &key);
6588 get_nd_key(base_flow, &base);
6589 get_nd_key(&wc->masks, &mask);
6590
6591 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
6592 odp_actions)) {
6593 put_nd_key(&base, base_flow);
6594 put_nd_key(&mask, &wc->masks);
6595 return SLOW_ACTION;
6596 }
6597
6598 return 0;
6599 }
6600
6601 static enum slow_path_reason
6602 commit_set_nw_action(const struct flow *flow, struct flow *base,
6603 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
6604 bool use_masked)
6605 {
6606 /* Check if 'flow' really has an L3 header. */
6607 if (!flow->nw_proto) {
6608 return 0;
6609 }
6610
6611 switch (ntohs(base->dl_type)) {
6612 case ETH_TYPE_IP:
6613 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
6614 break;
6615
6616 case ETH_TYPE_IPV6:
6617 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
6618 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
6619
6620 case ETH_TYPE_ARP:
6621 return commit_set_arp_action(flow, base, odp_actions, wc);
6622 }
6623
6624 return 0;
6625 }
6626
6627 static void
6628 get_nsh_key(const struct flow *flow, struct ovs_key_nsh *nsh, bool is_mask)
6629 {
6630 nsh->flags = flow->nsh.flags;
6631 nsh->mdtype = flow->nsh.mdtype;
6632 nsh->np = flow->nsh.np;
6633 nsh->path_hdr = htonl((ntohl(flow->nsh.spi) << NSH_SPI_SHIFT) |
6634 flow->nsh.si);
6635 if (is_mask) {
6636 for (int i = 0; i < 4; i++) {
6637 nsh->c[i] = flow->nsh.c[i];
6638 }
6639 } else {
6640 switch (nsh->mdtype) {
6641 case NSH_M_TYPE1:
6642 for (int i = 0; i < 4; i++) {
6643 nsh->c[i] = flow->nsh.c[i];
6644 }
6645 break;
6646 case NSH_M_TYPE2:
6647 default:
6648 /* No match support for other MD formats yet. */
6649 break;
6650 }
6651 }
6652 }
6653
6654 static void
6655 put_nsh_key(const struct ovs_key_nsh *nsh, struct flow *flow,
6656 bool is_mask OVS_UNUSED)
6657 {
6658 flow->nsh.flags = nsh->flags;
6659 flow->nsh.mdtype = nsh->mdtype;
6660 flow->nsh.np = nsh->np;
6661 flow->nsh.spi = htonl((ntohl(nsh->path_hdr) & NSH_SPI_MASK) >>
6662 NSH_SPI_SHIFT);
6663 flow->nsh.si = (ntohl(nsh->path_hdr) & NSH_SI_MASK) >> NSH_SI_SHIFT;
6664 switch (nsh->mdtype) {
6665 case NSH_M_TYPE1:
6666 for (int i = 0; i < 4; i++) {
6667 flow->nsh.c[i] = nsh->c[i];
6668 }
6669 break;
6670 case NSH_M_TYPE2:
6671 default:
6672 /* No match support for other MD formats yet. */
6673 memset(flow->nsh.c, 0, sizeof flow->nsh.c);
6674 break;
6675 }
6676 }
6677
6678 static void
6679 commit_set_nsh_action(const struct flow *flow, struct flow *base_flow,
6680 struct ofpbuf *odp_actions,
6681 struct flow_wildcards *wc,
6682 bool use_masked)
6683 {
6684 struct ovs_key_nsh key, mask, base;
6685
6686 if (flow->dl_type != htons(ETH_TYPE_NSH) ||
6687 !memcmp(&base_flow->nsh, &flow->nsh, sizeof base_flow->nsh)) {
6688 return;
6689 }
6690
6691 /* Check that mdtype and np remain unchanged. */
6692 ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype &&
6693 flow->nsh.np == base_flow->nsh.np);
6694
6695 get_nsh_key(flow, &key, false);
6696 get_nsh_key(base_flow, &base, false);
6697 get_nsh_key(&wc->masks, &mask, true);
6698 mask.mdtype = 0; /* Not writable. */
6699 mask.np = 0; /* Not writable. */
6700
6701 if (commit(OVS_KEY_ATTR_NSH, use_masked, &key, &base, &mask, sizeof key,
6702 odp_actions)) {
6703 put_nsh_key(&base, base_flow, false);
6704 if (mask.mdtype != 0) { /* Mask was changed by commit(). */
6705 put_nsh_key(&mask, &wc->masks, true);
6706 }
6707 }
6708 }
6709
6710 /* TCP, UDP, and SCTP keys have the same layout. */
6711 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
6712 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
6713
6714 static void
6715 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
6716 {
6717 tp->tcp.tcp_src = flow->tp_src;
6718 tp->tcp.tcp_dst = flow->tp_dst;
6719 }
6720
6721 static void
6722 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
6723 {
6724 flow->tp_src = tp->tcp.tcp_src;
6725 flow->tp_dst = tp->tcp.tcp_dst;
6726 }
6727
6728 static void
6729 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
6730 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
6731 bool use_masked)
6732 {
6733 enum ovs_key_attr key_type;
6734 union ovs_key_tp key, mask, base;
6735
6736 /* Check if 'flow' really has an L3 header. */
6737 if (!flow->nw_proto) {
6738 return;
6739 }
6740
6741 if (!is_ip_any(base_flow)) {
6742 return;
6743 }
6744
6745 if (flow->nw_proto == IPPROTO_TCP) {
6746 key_type = OVS_KEY_ATTR_TCP;
6747 } else if (flow->nw_proto == IPPROTO_UDP) {
6748 key_type = OVS_KEY_ATTR_UDP;
6749 } else if (flow->nw_proto == IPPROTO_SCTP) {
6750 key_type = OVS_KEY_ATTR_SCTP;
6751 } else {
6752 return;
6753 }
6754
6755 get_tp_key(flow, &key);
6756 get_tp_key(base_flow, &base);
6757 get_tp_key(&wc->masks, &mask);
6758
6759 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
6760 odp_actions)) {
6761 put_tp_key(&base, base_flow);
6762 put_tp_key(&mask, &wc->masks);
6763 }
6764 }
6765
6766 static void
6767 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
6768 struct ofpbuf *odp_actions,
6769 struct flow_wildcards *wc,
6770 bool use_masked)
6771 {
6772 uint32_t key, mask, base;
6773
6774 key = flow->skb_priority;
6775 base = base_flow->skb_priority;
6776 mask = wc->masks.skb_priority;
6777
6778 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
6779 sizeof key, odp_actions)) {
6780 base_flow->skb_priority = base;
6781 wc->masks.skb_priority = mask;
6782 }
6783 }
6784
6785 static void
6786 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
6787 struct ofpbuf *odp_actions,
6788 struct flow_wildcards *wc,
6789 bool use_masked)
6790 {
6791 uint32_t key, mask, base;
6792
6793 key = flow->pkt_mark;
6794 base = base_flow->pkt_mark;
6795 mask = wc->masks.pkt_mark;
6796
6797 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
6798 sizeof key, odp_actions)) {
6799 base_flow->pkt_mark = base;
6800 wc->masks.pkt_mark = mask;
6801 }
6802 }
6803
6804 static void
6805 odp_put_decap_nsh_action(struct ofpbuf *odp_actions)
6806 {
6807 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_DECAP_NSH);
6808 }
6809
6810 static void
6811 odp_put_encap_nsh_action(struct ofpbuf *odp_actions,
6812 const struct flow *flow,
6813 struct ofpbuf *encap_data)
6814 {
6815 struct ovs_action_encap_nsh encap_nsh;
6816
6817 encap_nsh.flags = flow->nsh.flags;
6818 encap_nsh.mdtype = flow->nsh.mdtype;
6819 encap_nsh.np = flow->nsh.np;
6820 encap_nsh.path_hdr = htonl((ntohl(flow->nsh.spi) << NSH_SPI_SHIFT) |
6821 flow->nsh.si);
6822
6823 switch (encap_nsh.mdtype) {
6824 case NSH_M_TYPE1: {
6825 struct nsh_md1_ctx *md1 =
6826 ALIGNED_CAST(struct nsh_md1_ctx *, encap_nsh.metadata);
6827 encap_nsh.mdlen = NSH_M_TYPE1_MDLEN;
6828 for (int i = 0; i < 4; i++) {
6829 put_16aligned_be32(&md1->c[i], flow->nsh.c[i]);
6830 }
6831 break;
6832 }
6833 case NSH_M_TYPE2:
6834 if (encap_data) {
6835 ovs_assert(encap_data->size < OVS_ENCAP_NSH_MAX_MD_LEN);
6836 encap_nsh.mdlen = encap_data->size;
6837 memcpy(encap_nsh.metadata, encap_data->data, encap_data->size);
6838 } else {
6839 encap_nsh.mdlen = 0;
6840 }
6841 break;
6842 default:
6843 encap_nsh.mdlen = 0;
6844 break;
6845 }
6846 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_ENCAP_NSH,
6847 &encap_nsh, sizeof(encap_nsh));
6848 }
6849
6850 static void
6851 commit_packet_type_change(const struct flow *flow,
6852 struct flow *base_flow,
6853 struct ofpbuf *odp_actions,
6854 struct flow_wildcards *wc,
6855 bool pending_encap,
6856 struct ofpbuf *encap_data)
6857 {
6858 if (flow->packet_type == base_flow->packet_type) {
6859 return;
6860 }
6861
6862 if (pending_encap) {
6863 switch (ntohl(flow->packet_type)) {
6864 case PT_ETH: {
6865 /* push_eth */
6866 odp_put_push_eth_action(odp_actions, &flow->dl_src,
6867 &flow->dl_dst);
6868 base_flow->packet_type = flow->packet_type;
6869 base_flow->dl_src = flow->dl_src;
6870 base_flow->dl_dst = flow->dl_dst;
6871 break;
6872 }
6873 case PT_NSH:
6874 /* encap_nsh */
6875 odp_put_encap_nsh_action(odp_actions, flow, encap_data);
6876 base_flow->packet_type = flow->packet_type;
6877 /* Update all packet headers in base_flow. */
6878 memcpy(&base_flow->dl_dst, &flow->dl_dst,
6879 sizeof(*flow) - offsetof(struct flow, dl_dst));
6880 break;
6881 default:
6882 /* Only the above protocols are supported for encap.
6883 * The check is done at action translation. */
6884 OVS_NOT_REACHED();
6885 }
6886 } else {
6887 /* This is an explicit or implicit decap case. */
6888 if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE &&
6889 base_flow->packet_type == htonl(PT_ETH)) {
6890 /* Generate pop_eth and continue without recirculation. */
6891 odp_put_pop_eth_action(odp_actions);
6892 base_flow->packet_type = flow->packet_type;
6893 base_flow->dl_src = eth_addr_zero;
6894 base_flow->dl_dst = eth_addr_zero;
6895 } else {
6896 /* All other decap cases require recirculation.
6897 * No need to update the base flow here. */
6898 switch (ntohl(base_flow->packet_type)) {
6899 case PT_NSH:
6900 /* decap_nsh. */
6901 odp_put_decap_nsh_action(odp_actions);
6902 break;
6903 default:
6904 /* Checks are done during translation. */
6905 OVS_NOT_REACHED();
6906 }
6907 }
6908 }
6909
6910 wc->masks.packet_type = OVS_BE32_MAX;
6911 }
6912
6913 /* If any of the flow key data that ODP actions can modify are different in
6914 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
6915 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
6916 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
6917 * in addition to this function if needed. Sets fields in 'wc' that are
6918 * used as part of the action.
6919 *
6920 * Returns a reason to force processing the flow's packets into the userspace
6921 * slow path, if there is one, otherwise 0. */
6922 enum slow_path_reason
6923 commit_odp_actions(const struct flow *flow, struct flow *base,
6924 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
6925 bool use_masked, bool pending_encap,
6926 struct ofpbuf *encap_data)
6927 {
6928 enum slow_path_reason slow1, slow2;
6929 bool mpls_done = false;
6930
6931 commit_packet_type_change(flow, base, odp_actions, wc,
6932 pending_encap, encap_data);
6933 commit_set_ether_action(flow, base, odp_actions, wc, use_masked);
6934 /* Make packet a non-MPLS packet before committing L3/4 actions,
6935 * which would otherwise do nothing. */
6936 if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) {
6937 commit_mpls_action(flow, base, odp_actions);
6938 mpls_done = true;
6939 }
6940 commit_set_nsh_action(flow, base, odp_actions, wc, use_masked);
6941 slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
6942 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
6943 slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
6944 if (!mpls_done) {
6945 commit_mpls_action(flow, base, odp_actions);
6946 }
6947 commit_vlan_action(flow, base, odp_actions, wc);
6948 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
6949 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
6950
6951 return slow1 ? slow1 : slow2;
6952 }