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