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