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