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