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