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