]> git.proxmox.com Git - ovs.git/blame - lib/odp-util.c
ovs-rcu: Comment fixes.
[ovs.git] / lib / odp-util.c
CommitLineData
064af421 1/*
8bfd0fda 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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>
064af421
BP
25#include <stdlib.h>
26#include <string.h>
a36de779 27
cdee00fd 28#include "byte-order.h"
064af421 29#include "coverage.h"
758c456d 30#include "dpif.h"
064af421
BP
31#include "dynamic-string.h"
32#include "flow.h"
cdee00fd 33#include "netlink.h"
3bffc610 34#include "ofpbuf.h"
064af421 35#include "packets.h"
44bac24b 36#include "simap.h"
064af421 37#include "timeval.h"
a36de779 38#include "unaligned.h"
064af421 39#include "util.h"
10e92b4f 40#include "uuid.h"
e6211adc 41#include "openvswitch/vlog.h"
34118cae
BP
42
43VLOG_DEFINE_THIS_MODULE(odp_util);
064af421 44
df2c07f4
JP
45/* The interface between userspace and kernel uses an "OVS_*" prefix.
46 * Since this is fairly non-specific for the OVS userspace components,
47 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
48 * interactions with the datapath.
49 */
50
7202cbe5
BP
51/* The set of characters that may separate one action or one key attribute
52 * from another. */
53static const char *delimiters = ", \t\r\n";
54
6b8da9e9
JG
55struct attr_len_tbl {
56 int len;
57 const struct attr_len_tbl *next;
58 int next_max;
59};
60#define ATTR_LEN_INVALID -1
61#define ATTR_LEN_VARIABLE -2
62#define ATTR_LEN_NESTED -3
63
e6cc0bab
AZ
64static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
65 struct ofpbuf *, struct ofpbuf *);
66static void format_odp_key_attr(const struct nlattr *a,
0a37839c
GS
67 const struct nlattr *ma,
68 const struct hmap *portno_names, struct ds *ds,
041e7168 69 bool verbose);
a052b51b 70
65da723b
JG
71static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
72 int max, struct ofpbuf *,
73 const struct nlattr *key);
98403001
BP
74/* Returns one the following for the action with the given OVS_ACTION_ATTR_*
75 * 'type':
76 *
77 * - For an action whose argument has a fixed length, returned that
78 * nonnegative length in bytes.
79 *
6b8da9e9 80 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
98403001 81 *
6b8da9e9 82 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
4edb9ae9 83static int
cdee00fd
BP
84odp_action_len(uint16_t type)
85{
df2c07f4 86 if (type > OVS_ACTION_ATTR_MAX) {
cdee00fd
BP
87 return -1;
88 }
89
09ded0ad 90 switch ((enum ovs_action_attr) type) {
fea393b1 91 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
6b8da9e9 92 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
a36de779 93 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
6b8da9e9 94 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
fea393b1
BP
95 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
96 case OVS_ACTION_ATTR_POP_VLAN: return 0;
b02475c5
SH
97 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
98 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
347bf289
AZ
99 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
100 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
6b8da9e9
JG
101 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
102 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
103 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
df2c07f4
JP
104
105 case OVS_ACTION_ATTR_UNSPEC:
106 case __OVS_ACTION_ATTR_MAX:
6b8da9e9 107 return ATTR_LEN_INVALID;
cdee00fd
BP
108 }
109
6b8da9e9 110 return ATTR_LEN_INVALID;
cdee00fd
BP
111}
112
e6603631
BP
113/* Returns a string form of 'attr'. The return value is either a statically
114 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
115 * should be at least OVS_KEY_ATTR_BUFSIZE. */
116enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
744791b5 117static const char *
e6603631 118ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
744791b5 119{
744791b5
BP
120 switch (attr) {
121 case OVS_KEY_ATTR_UNSPEC: return "unspec";
fea393b1 122 case OVS_KEY_ATTR_ENCAP: return "encap";
1b567fb9 123 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
72e8bf28 124 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
9b405f1a 125 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
744791b5
BP
126 case OVS_KEY_ATTR_IN_PORT: return "in_port";
127 case OVS_KEY_ATTR_ETHERNET: return "eth";
fea393b1 128 case OVS_KEY_ATTR_VLAN: return "vlan";
744791b5
BP
129 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
130 case OVS_KEY_ATTR_IPV4: return "ipv4";
131 case OVS_KEY_ATTR_IPV6: return "ipv6";
132 case OVS_KEY_ATTR_TCP: return "tcp";
dc235f7f 133 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
744791b5 134 case OVS_KEY_ATTR_UDP: return "udp";
c6bcb685 135 case OVS_KEY_ATTR_SCTP: return "sctp";
744791b5
BP
136 case OVS_KEY_ATTR_ICMP: return "icmp";
137 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
138 case OVS_KEY_ATTR_ARP: return "arp";
139 case OVS_KEY_ATTR_ND: return "nd";
b02475c5 140 case OVS_KEY_ATTR_MPLS: return "mpls";
572f732a
AZ
141 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
142 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
744791b5
BP
143
144 case __OVS_KEY_ATTR_MAX:
145 default:
e6603631
BP
146 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
147 return namebuf;
744791b5
BP
148 }
149}
150
cdee00fd
BP
151static void
152format_generic_odp_action(struct ds *ds, const struct nlattr *a)
153{
8ba43fbd
BP
154 size_t len = nl_attr_get_size(a);
155
cdee00fd 156 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
8ba43fbd 157 if (len) {
cdee00fd
BP
158 const uint8_t *unspec;
159 unsigned int i;
160
161 unspec = nl_attr_get(a);
8ba43fbd 162 for (i = 0; i < len; i++) {
cdee00fd
BP
163 ds_put_char(ds, i ? ' ': '(');
164 ds_put_format(ds, "%02x", unspec[i]);
165 }
166 ds_put_char(ds, ')');
167 }
168}
169
6ff686f2
PS
170static void
171format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
172{
173 static const struct nl_policy ovs_sample_policy[] = {
3548d242
BP
174 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
175 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
6ff686f2
PS
176 };
177 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
6ff686f2
PS
178 double percentage;
179 const struct nlattr *nla_acts;
180 int len;
181
182 ds_put_cstr(ds, "sample");
183
5621afff 184 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
6ff686f2
PS
185 ds_put_cstr(ds, "(error)");
186 return;
187 }
188
189 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
190 UINT32_MAX;
191
192 ds_put_format(ds, "(sample=%.1f%%,", percentage);
193
194 ds_put_cstr(ds, "actions(");
195 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
196 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
197 format_odp_actions(ds, nla_acts, len);
198 ds_put_format(ds, "))");
199}
200
6a7e895f 201static const char *
04594cd5 202slow_path_reason_to_string(uint32_t reason)
6a7e895f 203{
04594cd5
BP
204 switch ((enum slow_path_reason) reason) {
205#define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
206 SLOW_PATH_REASONS
207#undef SPR
6a7e895f 208 }
04594cd5
BP
209
210 return NULL;
6a7e895f
BP
211}
212
04594cd5
BP
213const char *
214slow_path_reason_to_explanation(enum slow_path_reason reason)
98f0520f 215{
04594cd5
BP
216 switch (reason) {
217#define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
218 SLOW_PATH_REASONS
219#undef SPR
98f0520f
EJ
220 }
221
04594cd5 222 return "<unknown>";
98f0520f
EJ
223}
224
ec956fc7
PS
225static int
226parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
ea2735d3 227 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
ec956fc7
PS
228{
229 uint32_t result = 0;
ea2735d3 230 int n;
ec956fc7 231
ea2735d3 232 /* Parse masked flags in numeric format? */
2d18eae8 233 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
ea2735d3
JR
234 res_flags, res_mask, &n) && n > 0) {
235 if (*res_flags & ~allowed || *res_mask & ~allowed) {
236 return -EINVAL;
237 }
238 return n;
239 }
240
2d18eae8 241 n = 0;
ec956fc7 242
2d18eae8 243 if (res_mask && (*s == '+' || *s == '-')) {
ea2735d3
JR
244 uint32_t flags = 0, mask = 0;
245
246 /* Parse masked flags. */
125fc637 247 while (s[0] != ')') {
ea2735d3
JR
248 bool set;
249 uint32_t bit;
250 int name_len;
251
125fc637 252 if (s[0] == '+') {
ea2735d3 253 set = true;
125fc637 254 } else if (s[0] == '-') {
ea2735d3
JR
255 set = false;
256 } else {
257 return -EINVAL;
258 }
125fc637 259 s++;
ea2735d3
JR
260 n++;
261
125fc637 262 name_len = strcspn(s, "+-)");
ea2735d3
JR
263
264 for (bit = 1; bit; bit <<= 1) {
265 const char *fname = bit_to_string(bit);
266 size_t len;
267
268 if (!fname) {
269 continue;
270 }
271
272 len = strlen(fname);
273 if (len != name_len) {
274 continue;
275 }
125fc637 276 if (!strncmp(s, fname, len)) {
ea2735d3
JR
277 if (mask & bit) {
278 /* bit already set. */
279 return -EINVAL;
280 }
281 if (!(bit & allowed)) {
282 return -EINVAL;
283 }
284 if (set) {
285 flags |= bit;
286 }
287 mask |= bit;
288 break;
289 }
290 }
291
292 if (!bit) {
293 return -EINVAL; /* Unknown flag name */
294 }
295 s += name_len;
125fc637 296 n += name_len;
ea2735d3 297 }
ea2735d3
JR
298
299 *res_flags = flags;
300 *res_mask = mask;
301 return n;
302 }
303
304 /* Parse unmasked flags. If a flag is present, it is set, otherwise
305 * it is not set. */
ec956fc7
PS
306 while (s[n] != ')') {
307 unsigned long long int flags;
308 uint32_t bit;
309 int n0;
310
c2c28dfd 311 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
ea2735d3
JR
312 if (flags & ~allowed) {
313 return -EINVAL;
314 }
ec956fc7
PS
315 n += n0 + (s[n + n0] == ',');
316 result |= flags;
317 continue;
318 }
319
320 for (bit = 1; bit; bit <<= 1) {
321 const char *name = bit_to_string(bit);
322 size_t len;
323
324 if (!name) {
325 continue;
326 }
327
328 len = strlen(name);
329 if (!strncmp(s + n, name, len) &&
330 (s[n + len] == ',' || s[n + len] == ')')) {
ea2735d3
JR
331 if (!(bit & allowed)) {
332 return -EINVAL;
333 }
ec956fc7
PS
334 result |= bit;
335 n += len + (s[n + len] == ',');
336 break;
337 }
338 }
339
340 if (!bit) {
341 return -EINVAL;
342 }
343 }
ec956fc7 344
ea2735d3
JR
345 *res_flags = result;
346 if (res_mask) {
347 *res_mask = UINT32_MAX;
348 }
ec956fc7 349 return n;
6a7e895f
BP
350}
351
98403001
BP
352static void
353format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
354{
355 static const struct nl_policy ovs_userspace_policy[] = {
3548d242
BP
356 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
357 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
358 .optional = true },
8b7ea2d4
WZ
359 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
360 .optional = true },
98403001
BP
361 };
362 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
e995e3df 363 const struct nlattr *userdata_attr;
8b7ea2d4 364 const struct nlattr *tunnel_out_port_attr;
98403001
BP
365
366 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
367 ds_put_cstr(ds, "userspace(error)");
368 return;
369 }
370
371 ds_put_format(ds, "userspace(pid=%"PRIu32,
372 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
373
e995e3df 374 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
29089a54
RL
375
376 if (userdata_attr) {
377 const uint8_t *userdata = nl_attr_get(userdata_attr);
378 size_t userdata_len = nl_attr_get_size(userdata_attr);
379 bool userdata_unspec = true;
1673e0e4 380 union user_action_cookie cookie;
98403001 381
29089a54
RL
382 if (userdata_len >= sizeof cookie.type
383 && userdata_len <= sizeof cookie) {
384
385 memset(&cookie, 0, sizeof cookie);
386 memcpy(&cookie, userdata, userdata_len);
387
388 userdata_unspec = false;
389
390 if (userdata_len == sizeof cookie.sflow
391 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
392 ds_put_format(ds, ",sFlow("
393 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
394 vlan_tci_to_vid(cookie.sflow.vlan_tci),
395 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
396 cookie.sflow.output);
397 } else if (userdata_len == sizeof cookie.slow_path
398 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
04594cd5
BP
399 ds_put_cstr(ds, ",slow_path(");
400 format_flags(ds, slow_path_reason_to_string,
401 cookie.slow_path.reason, ',');
402 ds_put_format(ds, ")");
29089a54
RL
403 } else if (userdata_len == sizeof cookie.flow_sample
404 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
405 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
406 ",collector_set_id=%"PRIu32
407 ",obs_domain_id=%"PRIu32
408 ",obs_point_id=%"PRIu32")",
409 cookie.flow_sample.probability,
410 cookie.flow_sample.collector_set_id,
411 cookie.flow_sample.obs_domain_id,
412 cookie.flow_sample.obs_point_id);
b075a9b8 413 } else if (userdata_len >= sizeof cookie.ipfix
29089a54 414 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
8b7ea2d4
WZ
415 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
416 cookie.ipfix.output_odp_port);
29089a54
RL
417 } else {
418 userdata_unspec = true;
419 }
98403001 420 }
e995e3df 421
29089a54
RL
422 if (userdata_unspec) {
423 size_t i;
424 ds_put_format(ds, ",userdata(");
425 for (i = 0; i < userdata_len; i++) {
426 ds_put_format(ds, "%02x", userdata[i]);
427 }
428 ds_put_char(ds, ')');
e995e3df 429 }
98403001
BP
430 }
431
8b7ea2d4
WZ
432 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
433 if (tunnel_out_port_attr) {
434 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
435 nl_attr_get_u32(tunnel_out_port_attr));
436 }
437
98403001
BP
438 ds_put_char(ds, ')');
439}
440
8ddc056d 441static void
2d18eae8 442format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
8ddc056d 443{
2d18eae8
JR
444 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
445 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
446 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
447 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
448 };
449 ds_put_char(ds, ',');
450 }
451 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
452 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
453 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
454 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
455 }
456 ds_put_char(ds, ',');
457 }
458 if (!(tci & htons(VLAN_CFI))) {
459 ds_put_cstr(ds, "cfi=0");
460 ds_put_char(ds, ',');
8ddc056d 461 }
2d18eae8 462 ds_chomp(ds, ',');
8ddc056d
BP
463}
464
b02475c5
SH
465static void
466format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
467{
468 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
469 mpls_lse_to_label(mpls_lse),
470 mpls_lse_to_tc(mpls_lse),
471 mpls_lse_to_ttl(mpls_lse),
472 mpls_lse_to_bos(mpls_lse));
473}
474
e6cc0bab
AZ
475static void
476format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
8bfd0fda 477 const struct ovs_key_mpls *mpls_mask, int n)
e6cc0bab 478{
8bfd0fda
BP
479 if (n == 1) {
480 ovs_be32 key = mpls_key->mpls_lse;
e6cc0bab 481
8bfd0fda
BP
482 if (mpls_mask == NULL) {
483 format_mpls_lse(ds, key);
484 } else {
485 ovs_be32 mask = mpls_mask->mpls_lse;
486
487 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
488 mpls_lse_to_label(key), mpls_lse_to_label(mask),
489 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
490 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
491 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
492 }
e6cc0bab 493 } else {
8bfd0fda 494 int i;
e6cc0bab 495
8bfd0fda
BP
496 for (i = 0; i < n; i++) {
497 ds_put_format(ds, "lse%d=%#"PRIx32,
498 i, ntohl(mpls_key[i].mpls_lse));
499 if (mpls_mask) {
500 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
501 }
502 ds_put_char(ds, ',');
503 }
504 ds_chomp(ds, ',');
e6cc0bab
AZ
505 }
506}
507
572f732a 508static void
347bf289 509format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
572f732a 510{
8f19f0a7 511 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
347bf289 512}
572f732a 513
347bf289
AZ
514static void
515format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
516{
517 ds_put_format(ds, "hash(");
572f732a 518
347bf289 519 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
62ac1f20 520 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
347bf289
AZ
521 } else {
522 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
523 hash_act->hash_alg);
524 }
525 ds_put_format(ds, ")");
572f732a
AZ
526}
527
e066f78f
JG
528static const void *
529format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
530{
531 const struct udp_header *udp;
532
533 udp = (const struct udp_header *) (ip + 1);
8e45fe7c
JG
534 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
535 ntohs(udp->udp_src), ntohs(udp->udp_dst),
536 ntohs(udp->udp_csum));
e066f78f
JG
537
538 return udp + 1;
539}
540
a36de779
PS
541static void
542format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
543{
544 const struct eth_header *eth;
545 const struct ip_header *ip;
546 const void *l3;
547
548 eth = (const struct eth_header *)data->header;
549
550 l3 = eth + 1;
551 ip = (const struct ip_header *)l3;
552
553 /* Ethernet */
554 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
555 data->header_len, data->tnl_type);
556 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
557 ds_put_format(ds, ",src=");
558 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
559 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
560
561 /* IPv4 */
562 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
563 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
564 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
565 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
566 ip->ip_proto, ip->ip_tos,
567 ip->ip_ttl,
568 ip->ip_frag_off);
569
570 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
571 const struct vxlanhdr *vxh;
a36de779 572
e066f78f 573 vxh = format_udp_tnl_push_header(ds, ip);
a36de779 574
a36de779
PS
575 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
576 ntohl(get_16aligned_be32(&vxh->vx_flags)),
a92a3aae 577 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
e5a1caee
JG
578 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
579 const struct genevehdr *gnh;
580
581 gnh = format_udp_tnl_push_header(ds, ip);
582
583 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
584 gnh->oam ? "oam," : "",
585 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
a36de779
PS
586 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
587 const struct gre_base_hdr *greh;
588 ovs_16aligned_be32 *options;
589 void *l4;
590
591 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
592 greh = (const struct gre_base_hdr *) l4;
593
594 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
e8fe6ad0 595 ntohs(greh->flags), ntohs(greh->protocol));
a36de779
PS
596 options = (ovs_16aligned_be32 *)(greh + 1);
597 if (greh->flags & htons(GRE_CSUM)) {
d804d31e 598 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
a36de779
PS
599 options++;
600 }
601 if (greh->flags & htons(GRE_KEY)) {
602 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
603 options++;
604 }
605 if (greh->flags & htons(GRE_SEQ)) {
606 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
607 options++;
608 }
609 ds_put_format(ds, ")");
610 }
611 ds_put_format(ds, ")");
612}
613
614static void
615format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
616{
617 struct ovs_action_push_tnl *data;
618
619 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
620
621 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
622 format_odp_tnl_push_header(ds, data);
623 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
624}
625
4edb9ae9 626static void
cdee00fd 627format_odp_action(struct ds *ds, const struct nlattr *a)
064af421 628{
98403001 629 int expected_len;
4edb9ae9 630 enum ovs_action_attr type = nl_attr_type(a);
fea393b1 631 const struct ovs_action_push_vlan *vlan;
6d670e7f 632 size_t size;
cdee00fd 633
98403001 634 expected_len = odp_action_len(nl_attr_type(a));
6b8da9e9
JG
635 if (expected_len != ATTR_LEN_VARIABLE &&
636 nl_attr_get_size(a) != expected_len) {
34582733 637 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
98403001 638 nl_attr_get_size(a), expected_len);
cdee00fd
BP
639 format_generic_odp_action(ds, a);
640 return;
641 }
642
4edb9ae9 643 switch (type) {
df2c07f4 644 case OVS_ACTION_ATTR_OUTPUT:
26ce7705 645 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
064af421 646 break;
a36de779
PS
647 case OVS_ACTION_ATTR_TUNNEL_POP:
648 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
649 break;
650 case OVS_ACTION_ATTR_TUNNEL_PUSH:
651 format_odp_tnl_push_action(ds, a);
652 break;
df2c07f4 653 case OVS_ACTION_ATTR_USERSPACE:
98403001 654 format_odp_userspace_action(ds, a);
064af421 655 break;
572f732a 656 case OVS_ACTION_ATTR_RECIRC:
347bf289
AZ
657 format_odp_recirc_action(ds, nl_attr_get_u32(a));
658 break;
659 case OVS_ACTION_ATTR_HASH:
660 format_odp_hash_action(ds, nl_attr_get(a));
572f732a 661 break;
6d670e7f
JR
662 case OVS_ACTION_ATTR_SET_MASKED:
663 a = nl_attr_get(a);
664 size = nl_attr_get_size(a) / 2;
665 ds_put_cstr(ds, "set(");
666
667 /* Masked set action not supported for tunnel key, which is bigger. */
668 if (size <= sizeof(struct ovs_key_ipv6)) {
669 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
670 sizeof(struct nlattr))];
671 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
672 sizeof(struct nlattr))];
673
674 mask->nla_type = attr->nla_type = nl_attr_type(a);
675 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
676 memcpy(attr + 1, (char *)(a + 1), size);
677 memcpy(mask + 1, (char *)(a + 1) + size, size);
2d18eae8 678 format_odp_key_attr(attr, mask, NULL, ds, false);
6d670e7f 679 } else {
2d18eae8 680 format_odp_key_attr(a, NULL, NULL, ds, false);
6d670e7f
JR
681 }
682 ds_put_cstr(ds, ")");
683 break;
4edb9ae9
PS
684 case OVS_ACTION_ATTR_SET:
685 ds_put_cstr(ds, "set(");
0a37839c 686 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
4edb9ae9 687 ds_put_cstr(ds, ")");
064af421 688 break;
fea393b1
BP
689 case OVS_ACTION_ATTR_PUSH_VLAN:
690 vlan = nl_attr_get(a);
691 ds_put_cstr(ds, "push_vlan(");
692 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
693 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
694 }
2d18eae8 695 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
8ddc056d 696 ds_put_char(ds, ')');
064af421 697 break;
fea393b1
BP
698 case OVS_ACTION_ATTR_POP_VLAN:
699 ds_put_cstr(ds, "pop_vlan");
064af421 700 break;
b02475c5
SH
701 case OVS_ACTION_ATTR_PUSH_MPLS: {
702 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
703 ds_put_cstr(ds, "push_mpls(");
704 format_mpls_lse(ds, mpls->mpls_lse);
705 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
706 break;
707 }
708 case OVS_ACTION_ATTR_POP_MPLS: {
709 ovs_be16 ethertype = nl_attr_get_be16(a);
710 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
711 break;
712 }
6ff686f2
PS
713 case OVS_ACTION_ATTR_SAMPLE:
714 format_odp_sample_action(ds, a);
715 break;
4edb9ae9
PS
716 case OVS_ACTION_ATTR_UNSPEC:
717 case __OVS_ACTION_ATTR_MAX:
064af421 718 default:
cdee00fd 719 format_generic_odp_action(ds, a);
064af421
BP
720 break;
721 }
722}
723
724void
cdee00fd 725format_odp_actions(struct ds *ds, const struct nlattr *actions,
cf22f8cb 726 size_t actions_len)
064af421 727{
cdee00fd
BP
728 if (actions_len) {
729 const struct nlattr *a;
730 unsigned int left;
731
732 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
733 if (a != actions) {
734 ds_put_char(ds, ',');
735 }
736 format_odp_action(ds, a);
064af421 737 }
cdee00fd 738 if (left) {
3a8cfc50
BP
739 int i;
740
3476fce3
BP
741 if (left == actions_len) {
742 ds_put_cstr(ds, "<empty>");
743 }
3a8cfc50
BP
744 ds_put_format(ds, ",***%u leftover bytes*** (", left);
745 for (i = 0; i < left; i++) {
746 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
747 }
748 ds_put_char(ds, ')');
cdee00fd
BP
749 }
750 } else {
064af421
BP
751 ds_put_cstr(ds, "drop");
752 }
753}
7202cbe5 754
8b7ea2d4 755/* Separate out parse_odp_userspace_action() function. */
7202cbe5 756static int
8b7ea2d4 757parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
7202cbe5 758{
8b7ea2d4
WZ
759 uint32_t pid;
760 union user_action_cookie cookie;
761 struct ofpbuf buf;
762 odp_port_t tunnel_out_port;
763 int n = -1;
764 void *user_data = NULL;
765 size_t user_data_size = 0;
766
767 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
768 return -EINVAL;
7202cbe5
BP
769 }
770
771 {
c2c28dfd
BP
772 uint32_t output;
773 uint32_t probability;
774 uint32_t collector_set_id;
775 uint32_t obs_domain_id;
776 uint32_t obs_point_id;
7202cbe5 777 int vid, pcp;
8b7ea2d4
WZ
778 int n1 = -1;
779 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
780 "pcp=%i,output=%"SCNi32")%n",
781 &vid, &pcp, &output, &n1)) {
7202cbe5
BP
782 uint16_t tci;
783
8b7ea2d4 784 n += n1;
7202cbe5
BP
785 tci = vid | (pcp << VLAN_PCP_SHIFT);
786 if (tci) {
787 tci |= VLAN_CFI;
788 }
789
790 cookie.type = USER_ACTION_COOKIE_SFLOW;
1673e0e4
BP
791 cookie.sflow.vlan_tci = htons(tci);
792 cookie.sflow.output = output;
8b7ea2d4
WZ
793 user_data = &cookie;
794 user_data_size = sizeof cookie.sflow;
2d18eae8 795 } else if (ovs_scan(&s[n], ",slow_path(%n",
8b7ea2d4 796 &n1)) {
04594cd5 797 int res;
6a7e895f 798
8b7ea2d4 799 n += n1;
6a7e895f
BP
800 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
801 cookie.slow_path.unused = 0;
04594cd5 802 cookie.slow_path.reason = 0;
6a7e895f 803
04594cd5 804 res = parse_flags(&s[n], slow_path_reason_to_string,
ea2735d3
JR
805 &cookie.slow_path.reason,
806 SLOW_PATH_REASON_MASK, NULL);
2d18eae8 807 if (res < 0 || s[n + res] != ')') {
04594cd5
BP
808 return res;
809 }
2d18eae8 810 n += res + 1;
6a7e895f 811
8b7ea2d4
WZ
812 user_data = &cookie;
813 user_data_size = sizeof cookie.slow_path;
814 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
c2c28dfd
BP
815 "collector_set_id=%"SCNi32","
816 "obs_domain_id=%"SCNi32","
8b7ea2d4
WZ
817 "obs_point_id=%"SCNi32")%n",
818 &probability, &collector_set_id,
819 &obs_domain_id, &obs_point_id, &n1)) {
820 n += n1;
29089a54
RL
821
822 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
823 cookie.flow_sample.probability = probability;
824 cookie.flow_sample.collector_set_id = collector_set_id;
825 cookie.flow_sample.obs_domain_id = obs_domain_id;
826 cookie.flow_sample.obs_point_id = obs_point_id;
8b7ea2d4
WZ
827 user_data = &cookie;
828 user_data_size = sizeof cookie.flow_sample;
829 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
830 &output, &n1) ) {
831 n += n1;
29089a54 832 cookie.type = USER_ACTION_COOKIE_IPFIX;
8b7ea2d4
WZ
833 cookie.ipfix.output_odp_port = u32_to_odp(output);
834 user_data = &cookie;
835 user_data_size = sizeof cookie.ipfix;
836 } else if (ovs_scan(&s[n], ",userdata(%n",
837 &n1)) {
e995e3df
BP
838 char *end;
839
8b7ea2d4 840 n += n1;
e995e3df
BP
841 ofpbuf_init(&buf, 16);
842 end = ofpbuf_put_hex(&buf, &s[n], NULL);
8b7ea2d4
WZ
843 if (end[0] != ')') {
844 return -EINVAL;
e995e3df 845 }
6fd6ed71
PS
846 user_data = buf.data;
847 user_data_size = buf.size;
8b7ea2d4
WZ
848 n = (end + 1) - s;
849 }
850 }
851
852 {
853 int n1 = -1;
854 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
855 &tunnel_out_port, &n1)) {
856 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
857 return n + n1;
858 } else if (s[n] == ')') {
859 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
860 return n + 1;
861 }
862 }
863
864 return -EINVAL;
865}
866
a36de779
PS
867static int
868ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
869{
870 struct eth_header *eth;
871 struct ip_header *ip;
872 struct udp_header *udp;
873 struct gre_base_hdr *greh;
e8fe6ad0 874 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
a36de779
PS
875 ovs_be32 sip, dip;
876 uint32_t tnl_type = 0, header_len = 0;
877 void *l3, *l4;
878 int n = 0;
879
880 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
881 return -EINVAL;
882 }
883 eth = (struct eth_header *) data->header;
884 l3 = (data->header + sizeof *eth);
885 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
886 ip = (struct ip_header *) l3;
887 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
888 "eth(dst="ETH_ADDR_SCAN_FMT",",
889 &data->header_len,
890 &data->tnl_type,
891 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
892 return -EINVAL;
893 }
894
895 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
896 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
897 return -EINVAL;
898 }
899 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
900 return -EINVAL;
901 }
902 eth->eth_type = htons(dl_type);
903
904 /* IPv4 */
905 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
906 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
907 IP_SCAN_ARGS(&sip),
908 IP_SCAN_ARGS(&dip),
909 &ip->ip_proto, &ip->ip_tos,
910 &ip->ip_ttl, &ip->ip_frag_off)) {
911 return -EINVAL;
912 }
913 put_16aligned_be32(&ip->ip_src, sip);
914 put_16aligned_be32(&ip->ip_dst, dip);
915
916 /* Tunnel header */
917 udp = (struct udp_header *) l4;
918 greh = (struct gre_base_hdr *) l4;
8e45fe7c
JG
919 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
920 &udp_src, &udp_dst, &csum)) {
e5a1caee 921 uint32_t vx_flags, vni;
a36de779
PS
922
923 udp->udp_src = htons(udp_src);
924 udp->udp_dst = htons(udp_dst);
925 udp->udp_len = 0;
8e45fe7c 926 udp->udp_csum = htons(csum);
a36de779 927
e066f78f 928 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
e5a1caee 929 &vx_flags, &vni)) {
e066f78f
JG
930 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
931
932 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
e5a1caee 933 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
e066f78f
JG
934 tnl_type = OVS_VPORT_TYPE_VXLAN;
935 header_len = sizeof *eth + sizeof *ip +
936 sizeof *udp + sizeof *vxh;
e5a1caee
JG
937 } else if (ovs_scan_len(s, &n, "geneve(")) {
938 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
939
46e7137c 940 memset(gnh, 0, sizeof *gnh);
e5a1caee
JG
941 if (ovs_scan_len(s, &n, "oam,")) {
942 gnh->oam = 1;
943 }
944 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
945 return -EINVAL;
946 }
947 gnh->proto_type = htons(ETH_TYPE_TEB);
948 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
949 tnl_type = OVS_VPORT_TYPE_GENEVE;
950 header_len = sizeof *eth + sizeof *ip +
951 sizeof *udp + sizeof *gnh;
e066f78f 952 } else {
a36de779
PS
953 return -EINVAL;
954 }
a36de779 955 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
e8fe6ad0 956 &gre_flags, &gre_proto)){
a36de779
PS
957
958 tnl_type = OVS_VPORT_TYPE_GRE;
e8fe6ad0 959 greh->flags = htons(gre_flags);
a36de779
PS
960 greh->protocol = htons(gre_proto);
961 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
962
963 if (greh->flags & htons(GRE_CSUM)) {
d804d31e 964 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
a36de779
PS
965 return -EINVAL;
966 }
d804d31e
JG
967
968 memset(options, 0, sizeof *options);
969 *((ovs_be16 *)options) = htons(csum);
a36de779
PS
970 options++;
971 }
972 if (greh->flags & htons(GRE_KEY)) {
973 uint32_t key;
974
975 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
976 return -EINVAL;
977 }
978
979 put_16aligned_be32(options, htonl(key));
980 options++;
981 }
982 if (greh->flags & htons(GRE_SEQ)) {
983 uint32_t seq;
984
985 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
986 return -EINVAL;
987 }
988 put_16aligned_be32(options, htonl(seq));
989 options++;
990 }
991
992 if (!ovs_scan_len(s, &n, "))")) {
993 return -EINVAL;
994 }
995
996 header_len = sizeof *eth + sizeof *ip +
997 ((uint8_t *) options - (uint8_t *) greh);
998 } else {
999 return -EINVAL;
1000 }
1001
1002 /* check tunnel meta data. */
1003 if (data->tnl_type != tnl_type) {
1004 return -EINVAL;
1005 }
1006 if (data->header_len != header_len) {
1007 return -EINVAL;
1008 }
1009
1010 /* Out port */
1011 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1012 return -EINVAL;
1013 }
1014
1015 return n;
1016}
1017
8b7ea2d4
WZ
1018static int
1019parse_odp_action(const char *s, const struct simap *port_names,
1020 struct ofpbuf *actions)
1021{
1022 {
1023 uint32_t port;
1024 int n;
1025
1026 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1027 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1028 return n;
7202cbe5
BP
1029 }
1030 }
1031
8b7ea2d4
WZ
1032 if (port_names) {
1033 int len = strcspn(s, delimiters);
1034 struct simap_node *node;
1035
1036 node = simap_find_len(port_names, s, len);
1037 if (node) {
1038 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1039 return len;
1040 }
1041 }
1042
d73803ca
DDP
1043 {
1044 uint32_t recirc_id;
1045 int n = -1;
1046
1047 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1048 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1049 return n;
1050 }
1051 }
1052
8b7ea2d4
WZ
1053 if (!strncmp(s, "userspace(", 10)) {
1054 return parse_odp_userspace_action(s, actions);
1055 }
1056
7202cbe5
BP
1057 if (!strncmp(s, "set(", 4)) {
1058 size_t start_ofs;
1059 int retval;
6d670e7f
JR
1060 struct nlattr mask[128 / sizeof(struct nlattr)];
1061 struct ofpbuf maskbuf;
1062 struct nlattr *nested, *key;
1063 size_t size;
1064
1065 /* 'mask' is big enough to hold any key. */
1066 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
7202cbe5
BP
1067
1068 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
6d670e7f 1069 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
7202cbe5
BP
1070 if (retval < 0) {
1071 return retval;
1072 }
1073 if (s[retval + 4] != ')') {
1074 return -EINVAL;
1075 }
6d670e7f
JR
1076
1077 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1078 key = nested + 1;
1079
1080 size = nl_attr_get_size(mask);
1081 if (size == nl_attr_get_size(key)) {
1082 /* Change to masked set action if not fully masked. */
53cb9c3e 1083 if (!is_all_ones(mask + 1, size)) {
6d670e7f
JR
1084 key->nla_len += size;
1085 ofpbuf_put(actions, mask + 1, size);
1086 /* 'actions' may have been reallocated by ofpbuf_put(). */
1087 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1088 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1089 }
1090 }
1091
7202cbe5
BP
1092 nl_msg_end_nested(actions, start_ofs);
1093 return retval + 5;
1094 }
1095
becffb86
BP
1096 {
1097 struct ovs_action_push_vlan push;
1098 int tpid = ETH_TYPE_VLAN;
1099 int vid, pcp;
1100 int cfi = 1;
1101 int n = -1;
7202cbe5 1102
c2c28dfd
BP
1103 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1104 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1105 &vid, &pcp, &cfi, &n)
1106 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1107 &tpid, &vid, &pcp, &n)
1108 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1109 &tpid, &vid, &pcp, &cfi, &n)) {
becffb86
BP
1110 push.vlan_tpid = htons(tpid);
1111 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1112 | (pcp << VLAN_PCP_SHIFT)
1113 | (cfi ? VLAN_CFI : 0));
1114 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1115 &push, sizeof push);
1116
1117 return n;
7202cbe5 1118 }
7202cbe5
BP
1119 }
1120
becffb86
BP
1121 if (!strncmp(s, "pop_vlan", 8)) {
1122 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1123 return 8;
7202cbe5
BP
1124 }
1125
1126 {
1127 double percentage;
1128 int n = -1;
1129
c2c28dfd
BP
1130 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1131 && percentage >= 0. && percentage <= 100.0) {
7202cbe5
BP
1132 size_t sample_ofs, actions_ofs;
1133 double probability;
1134
1135 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1136 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1137 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1138 (probability <= 0 ? 0
1139 : probability >= UINT32_MAX ? UINT32_MAX
1140 : probability));
1141
1142 actions_ofs = nl_msg_start_nested(actions,
1143 OVS_SAMPLE_ATTR_ACTIONS);
1144 for (;;) {
1145 int retval;
1146
79d4ffe2 1147 n += strspn(s + n, delimiters);
7202cbe5
BP
1148 if (s[n] == ')') {
1149 break;
1150 }
1151
1152 retval = parse_odp_action(s + n, port_names, actions);
1153 if (retval < 0) {
1154 return retval;
1155 }
1156 n += retval;
7202cbe5
BP
1157 }
1158 nl_msg_end_nested(actions, actions_ofs);
1159 nl_msg_end_nested(actions, sample_ofs);
1160
1161 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1162 }
1163 }
1164
a36de779
PS
1165 {
1166 uint32_t port;
1167 int n;
1168
1169 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1170 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1171 return n;
1172 }
1173 }
1174
1175 {
1176 struct ovs_action_push_tnl data;
1177 int n;
1178
1179 n = ovs_parse_tnl_push(s, &data);
1180 if (n > 0) {
1181 odp_put_tnl_push_action(actions, &data);
1182 return n;
1183 } else if (n < 0) {
1184 return n;
1185 }
1186 }
7202cbe5
BP
1187 return -EINVAL;
1188}
1189
1190/* Parses the string representation of datapath actions, in the format output
1191 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1192 * value. On success, the ODP actions are appended to 'actions' as a series of
1193 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1194 * way, 'actions''s data might be reallocated. */
1195int
44bac24b 1196odp_actions_from_string(const char *s, const struct simap *port_names,
7202cbe5
BP
1197 struct ofpbuf *actions)
1198{
1199 size_t old_size;
1200
1201 if (!strcasecmp(s, "drop")) {
1202 return 0;
1203 }
1204
6fd6ed71 1205 old_size = actions->size;
7202cbe5
BP
1206 for (;;) {
1207 int retval;
1208
1209 s += strspn(s, delimiters);
1210 if (!*s) {
1211 return 0;
1212 }
1213
1214 retval = parse_odp_action(s, port_names, actions);
1215 if (retval < 0 || !strchr(delimiters, s[retval])) {
6fd6ed71 1216 actions->size = old_size;
7202cbe5
BP
1217 return -retval;
1218 }
1219 s += retval;
1220 }
1221
1222 return 0;
1223}
14608a15 1224\f
6b8da9e9
JG
1225static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1226 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1227};
1228
1229static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1230 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1231 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1232 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1233 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1234 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1235 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1236 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1237 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1238 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1239 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1240 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1241 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1242 .next = ovs_vxlan_ext_attr_lens ,
1243 .next_max = OVS_VXLAN_EXT_MAX},
1244};
1245
1246static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1247 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1248 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1249 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1250 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1251 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1252 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1253 .next = ovs_tun_key_attr_lens,
1254 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1255 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1256 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1257 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1258 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1259 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1260 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1261 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1262 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1263 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1264 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1265 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1266 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1267 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1268 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1269 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1270};
1271
36956a7d 1272/* Returns the correct length of the payload for a flow key attribute of the
6b8da9e9
JG
1273 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1274 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1275 * payload is a nested type. */
36956a7d 1276static int
6b8da9e9 1277odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
36956a7d 1278{
6b8da9e9
JG
1279 if (type > max_len) {
1280 return ATTR_LEN_INVALID;
36956a7d
BP
1281 }
1282
6b8da9e9 1283 return tbl[type].len;
36956a7d
BP
1284}
1285
36956a7d
BP
1286static void
1287format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1288{
1289 size_t len = nl_attr_get_size(a);
36956a7d
BP
1290 if (len) {
1291 const uint8_t *unspec;
1292 unsigned int i;
1293
1294 unspec = nl_attr_get(a);
1295 for (i = 0; i < len; i++) {
e6cc0bab
AZ
1296 if (i) {
1297 ds_put_char(ds, ' ');
1298 }
36956a7d
BP
1299 ds_put_format(ds, "%02x", unspec[i]);
1300 }
36956a7d
BP
1301 }
1302}
1303
7257b535
BP
1304static const char *
1305ovs_frag_type_to_string(enum ovs_frag_type type)
1306{
1307 switch (type) {
1308 case OVS_FRAG_TYPE_NONE:
1309 return "no";
1310 case OVS_FRAG_TYPE_FIRST:
1311 return "first";
1312 case OVS_FRAG_TYPE_LATER:
1313 return "later";
1314 case __OVS_FRAG_TYPE_MAX:
1315 default:
1316 return "<error>";
1317 }
1318}
1319
c1fc1411
JG
1320#define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1321static int
1322parse_geneve_opts(const struct nlattr *attr)
1323{
1324 int opts_len = nl_attr_get_size(attr);
1325 const struct geneve_opt *opt = nl_attr_get(attr);
1326
1327 while (opts_len > 0) {
1328 int len;
1329
1330 if (opts_len < sizeof(*opt)) {
1331 return -EINVAL;
1332 }
1333
1334 len = sizeof(*opt) + opt->length * 4;
1335 if (len > opts_len) {
1336 return -EINVAL;
1337 }
1338
1339 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1340 default:
1341 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1342 return -EINVAL;
1343 }
1344 };
1345
1346 opt = opt + len / sizeof(*opt);
1347 opts_len -= len;
1348 };
1349
1350 return 0;
1351}
1352
617e10e7
SH
1353enum odp_key_fitness
1354odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
9b405f1a
PS
1355{
1356 unsigned int left;
1357 const struct nlattr *a;
1358 bool ttl = false;
1359 bool unknown = false;
1360
1361 NL_NESTED_FOR_EACH(a, left, attr) {
1362 uint16_t type = nl_attr_type(a);
1363 size_t len = nl_attr_get_size(a);
6b8da9e9
JG
1364 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1365 OVS_TUNNEL_ATTR_MAX, type);
9b405f1a
PS
1366
1367 if (len != expected_len && expected_len >= 0) {
1368 return ODP_FIT_ERROR;
1369 }
1370
1371 switch (type) {
1372 case OVS_TUNNEL_KEY_ATTR_ID:
1373 tun->tun_id = nl_attr_get_be64(a);
1374 tun->flags |= FLOW_TNL_F_KEY;
1375 break;
1376 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1377 tun->ip_src = nl_attr_get_be32(a);
1378 break;
1379 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1380 tun->ip_dst = nl_attr_get_be32(a);
1381 break;
1382 case OVS_TUNNEL_KEY_ATTR_TOS:
1383 tun->ip_tos = nl_attr_get_u8(a);
1384 break;
1385 case OVS_TUNNEL_KEY_ATTR_TTL:
1386 tun->ip_ttl = nl_attr_get_u8(a);
1387 ttl = true;
1388 break;
1389 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1390 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1391 break;
1392 case OVS_TUNNEL_KEY_ATTR_CSUM:
1393 tun->flags |= FLOW_TNL_F_CSUM;
1394 break;
8b7ea2d4
WZ
1395 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1396 tun->tp_src = nl_attr_get_be16(a);
1397 break;
1398 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1399 tun->tp_dst = nl_attr_get_be16(a);
1400 break;
94872594
JG
1401 case OVS_TUNNEL_KEY_ATTR_OAM:
1402 tun->flags |= FLOW_TNL_F_OAM;
1403 break;
ac6073e3
MC
1404 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1405 static const struct nl_policy vxlan_opts_policy[] = {
1406 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1407 };
1408 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1409
1410 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1411 return ODP_FIT_ERROR;
1412 }
1413
1414 if (ext[OVS_VXLAN_EXT_GBP]) {
1415 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1416
1417 tun->gbp_id = htons(gbp & 0xFFFF);
1418 tun->gbp_flags = (gbp >> 16) & 0xFF;
1419 }
1420
1421 break;
1422 }
c1fc1411
JG
1423 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1424 if (parse_geneve_opts(a)) {
1425 return ODP_FIT_ERROR;
1426 }
1427 /* It is necessary to reproduce options exactly (including order)
1428 * so it's easiest to just echo them back. */
1429 unknown = true;
1430 break;
1431 }
9b405f1a
PS
1432 default:
1433 /* Allow this to show up as unexpected, if there are unknown
1434 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1435 unknown = true;
1436 break;
1437 }
1438 }
1439
1440 if (!ttl) {
1441 return ODP_FIT_ERROR;
1442 }
1443 if (unknown) {
758c456d 1444 return ODP_FIT_TOO_MUCH;
9b405f1a
PS
1445 }
1446 return ODP_FIT_PERFECT;
1447}
1448
1449static void
1450tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1451{
1452 size_t tun_key_ofs;
1453
1454 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1455
27a8cbbc
BP
1456 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1457 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
9b405f1a
PS
1458 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1459 }
1460 if (tun_key->ip_src) {
1461 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1462 }
1463 if (tun_key->ip_dst) {
1464 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1465 }
1466 if (tun_key->ip_tos) {
1467 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1468 }
1469 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1470 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1471 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1472 }
1473 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1474 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1475 }
8b7ea2d4
WZ
1476 if (tun_key->tp_src) {
1477 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1478 }
1479 if (tun_key->tp_dst) {
1480 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1481 }
94872594
JG
1482 if (tun_key->flags & FLOW_TNL_F_OAM) {
1483 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1484 }
ac6073e3
MC
1485 if (tun_key->gbp_flags || tun_key->gbp_id) {
1486 size_t vxlan_opts_ofs;
1487
1488 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1489 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1490 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1491 nl_msg_end_nested(a, vxlan_opts_ofs);
1492 }
9b405f1a
PS
1493
1494 nl_msg_end_nested(a, tun_key_ofs);
29c7a2b2
PS
1495}
1496
041e7168
AZ
1497static bool
1498odp_mask_attr_is_wildcard(const struct nlattr *ma)
1499{
1500 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1501}
1502
e6cc0bab 1503static bool
d23df9a8 1504odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
e6cc0bab 1505{
260974dc 1506 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
d23df9a8
JR
1507 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1508 }
1509 if (attr == OVS_KEY_ATTR_IPV6) {
1510 const struct ovs_key_ipv6 *ipv6_mask = mask;
260974dc 1511
d23df9a8
JR
1512 return
1513 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
ea2735d3 1514 == htonl(IPV6_LABEL_MASK))
d23df9a8
JR
1515 && ipv6_mask->ipv6_proto == UINT8_MAX
1516 && ipv6_mask->ipv6_tclass == UINT8_MAX
1517 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1518 && ipv6_mask->ipv6_frag == UINT8_MAX
1519 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1520 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1521 }
1522 if (attr == OVS_KEY_ATTR_TUNNEL) {
48954dab 1523 return false;
d23df9a8
JR
1524 }
1525
1526 if (attr == OVS_KEY_ATTR_ARP) {
1527 /* ARP key has padding, ignore it. */
1528 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1529 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1530 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1531 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1532 }
260974dc 1533
d23df9a8
JR
1534 return is_all_ones(mask, size);
1535}
1536
1537static bool
1538odp_mask_attr_is_exact(const struct nlattr *ma)
1539{
d23df9a8
JR
1540 enum ovs_key_attr attr = nl_attr_type(ma);
1541 const void *mask;
1542 size_t size;
1543
1544 if (attr == OVS_KEY_ATTR_TUNNEL) {
48954dab 1545 return false;
e6cc0bab 1546 } else {
d23df9a8
JR
1547 mask = nl_attr_get(ma);
1548 size = nl_attr_get_size(ma);
e6cc0bab
AZ
1549 }
1550
d23df9a8 1551 return odp_mask_is_exact(attr, mask, size);
e6cc0bab
AZ
1552}
1553
0a37839c
GS
1554void
1555odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1556 char *port_name)
1557{
1558 struct odp_portno_names *odp_portno_names;
1559
1560 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1561 odp_portno_names->port_no = port_no;
1562 odp_portno_names->name = xstrdup(port_name);
1563 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1564 hash_odp_port(port_no));
1565}
1566
1567static char *
1568odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1569{
1570 struct odp_portno_names *odp_portno_names;
1571
1572 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1573 hash_odp_port(port_no), portno_names) {
1574 if (odp_portno_names->port_no == port_no) {
1575 return odp_portno_names->name;
1576 }
1577 }
1578 return NULL;
1579}
1580
1581void
1582odp_portno_names_destroy(struct hmap *portno_names)
1583{
1584 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1585 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1586 hmap_node, portno_names) {
1587 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1588 free(odp_portno_names->name);
1589 free(odp_portno_names);
1590 }
1591}
e6cc0bab 1592
2d18eae8
JR
1593/* Format helpers. */
1594
1595static void
1596format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1597 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1598{
1599 bool mask_empty = mask && eth_addr_is_zero(*mask);
1600
1601 if (verbose || !mask_empty) {
1602 bool mask_full = !mask || eth_mask_is_exact(*mask);
1603
1604 if (mask_full) {
1605 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1606 } else {
1607 ds_put_format(ds, "%s=", name);
1608 eth_format_masked(key, *mask, ds);
1609 ds_put_char(ds, ',');
1610 }
1611 }
1612}
1613
1614static void
1615format_be64(struct ds *ds, const char *name, ovs_be64 key,
1616 const ovs_be64 *mask, bool verbose)
1617{
1618 bool mask_empty = mask && !*mask;
1619
1620 if (verbose || !mask_empty) {
1621 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1622
1623 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1624 if (!mask_full) { /* Partially masked. */
1625 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1626 }
1627 ds_put_char(ds, ',');
1628 }
1629}
1630
1631static void
1632format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1633 const ovs_be32 *mask, bool verbose)
1634{
1635 bool mask_empty = mask && !*mask;
1636
1637 if (verbose || !mask_empty) {
1638 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1639
1640 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1641 if (!mask_full) { /* Partially masked. */
1642 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1643 }
1644 ds_put_char(ds, ',');
1645 }
1646}
1647
1648static void
1649format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1650 const ovs_be32 (*mask_)[4], bool verbose)
1651{
1652 char buf[INET6_ADDRSTRLEN];
1653 const struct in6_addr *key = (const struct in6_addr *)key_;
1654 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1655 : NULL;
1656 bool mask_empty = mask && ipv6_mask_is_any(mask);
1657
1658 if (verbose || !mask_empty) {
1659 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1660
1661 inet_ntop(AF_INET6, key, buf, sizeof buf);
1662 ds_put_format(ds, "%s=%s", name, buf);
1663 if (!mask_full) { /* Partially masked. */
1664 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1665 ds_put_format(ds, "/%s", buf);
1666 }
1667 ds_put_char(ds, ',');
1668 }
1669}
1670
1671static void
1672format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1673 const ovs_be32 *mask, bool verbose)
1674{
1675 bool mask_empty = mask && !*mask;
1676
1677 if (verbose || !mask_empty) {
1678 bool mask_full = !mask
1679 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1680
1681 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1682 if (!mask_full) { /* Partially masked. */
1683 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1684 }
1685 ds_put_char(ds, ',');
1686 }
1687}
1688
1689static void
1690format_u8x(struct ds *ds, const char *name, uint8_t key,
1691 const uint8_t *mask, bool verbose)
1692{
1693 bool mask_empty = mask && !*mask;
1694
1695 if (verbose || !mask_empty) {
1696 bool mask_full = !mask || *mask == UINT8_MAX;
1697
1698 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1699 if (!mask_full) { /* Partially masked. */
1700 ds_put_format(ds, "/%#"PRIx8, *mask);
1701 }
1702 ds_put_char(ds, ',');
1703 }
1704}
1705
1706static void
1707format_u8u(struct ds *ds, const char *name, uint8_t key,
1708 const uint8_t *mask, bool verbose)
1709{
1710 bool mask_empty = mask && !*mask;
1711
1712 if (verbose || !mask_empty) {
1713 bool mask_full = !mask || *mask == UINT8_MAX;
1714
1715 ds_put_format(ds, "%s=%"PRIu8, name, key);
1716 if (!mask_full) { /* Partially masked. */
1717 ds_put_format(ds, "/%#"PRIx8, *mask);
1718 }
1719 ds_put_char(ds, ',');
1720 }
1721}
1722
1723static void
1724format_be16(struct ds *ds, const char *name, ovs_be16 key,
1725 const ovs_be16 *mask, bool verbose)
1726{
1727 bool mask_empty = mask && !*mask;
1728
1729 if (verbose || !mask_empty) {
1730 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1731
1732 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1733 if (!mask_full) { /* Partially masked. */
1734 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1735 }
1736 ds_put_char(ds, ',');
1737 }
1738}
1739
622a0a8e
JG
1740static void
1741format_be16x(struct ds *ds, const char *name, ovs_be16 key,
1742 const ovs_be16 *mask, bool verbose)
1743{
1744 bool mask_empty = mask && !*mask;
1745
1746 if (verbose || !mask_empty) {
1747 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1748
1749 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
1750 if (!mask_full) { /* Partially masked. */
1751 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1752 }
1753 ds_put_char(ds, ',');
1754 }
1755}
1756
2d18eae8
JR
1757static void
1758format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1759 const uint16_t *mask, bool verbose)
1760{
1761 bool mask_empty = mask && !*mask;
1762
1763 if (verbose || !mask_empty) {
1764 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1765
1766 ds_put_cstr(ds, name);
1767 ds_put_char(ds, '(');
1768 if (!mask_full) { /* Partially masked. */
1769 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1770 } else { /* Fully masked. */
1771 format_flags(ds, flow_tun_flag_to_string, key, ',');
1772 }
1773 ds_put_cstr(ds, "),");
1774 }
1775}
1776
65da723b
JG
1777static bool
1778check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
1779 const struct attr_len_tbl tbl[], int max_len, bool need_key)
1780{
1781 int expected_len;
1782
1783 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
1784 if (expected_len != ATTR_LEN_VARIABLE &&
1785 expected_len != ATTR_LEN_NESTED) {
1786
1787 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1788 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1789
1790 if (bad_key_len || bad_mask_len) {
1791 if (need_key) {
1792 ds_put_format(ds, "key%u", nl_attr_type(a));
1793 }
1794 if (bad_key_len) {
1795 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1796 nl_attr_get_size(a), expected_len);
1797 }
1798 format_generic_odp_key(a, ds);
1799 if (ma) {
1800 ds_put_char(ds, '/');
1801 if (bad_mask_len) {
1802 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1803 nl_attr_get_size(ma), expected_len);
1804 }
1805 format_generic_odp_key(ma, ds);
1806 }
1807 ds_put_char(ds, ')');
1808 return false;
1809 }
1810 }
1811
1812 return true;
1813}
1814
1815static void
1816format_unknown_key(struct ds *ds, const struct nlattr *a,
1817 const struct nlattr *ma)
1818{
1819 ds_put_format(ds, "key%u(", nl_attr_type(a));
1820 format_generic_odp_key(a, ds);
1821 if (ma && !odp_mask_attr_is_exact(ma)) {
1822 ds_put_char(ds, '/');
1823 format_generic_odp_key(ma, ds);
1824 }
1825 ds_put_cstr(ds, "),");
1826}
1827
1828static void
1829format_odp_tun_vxlan_opt(const struct nlattr *attr,
1830 const struct nlattr *mask_attr, struct ds *ds,
1831 bool verbose)
1832{
1833 unsigned int left;
1834 const struct nlattr *a;
1835 struct ofpbuf ofp;
1836
1837 ofpbuf_init(&ofp, 100);
1838 NL_NESTED_FOR_EACH(a, left, attr) {
1839 uint16_t type = nl_attr_type(a);
1840 const struct nlattr *ma = NULL;
1841
1842 if (mask_attr) {
1843 ma = nl_attr_find__(nl_attr_get(mask_attr),
1844 nl_attr_get_size(mask_attr), type);
1845 if (!ma) {
1846 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
1847 OVS_VXLAN_EXT_MAX,
1848 &ofp, a);
1849 }
1850 }
1851
1852 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
1853 OVS_VXLAN_EXT_MAX, true)) {
1854 continue;
1855 }
1856
1857 switch (type) {
1858 case OVS_VXLAN_EXT_GBP: {
1859 uint32_t key = nl_attr_get_u32(a);
1860 ovs_be16 id, id_mask;
1861 uint8_t flags, flags_mask;
1862
1863 id = htons(key & 0xFFFF);
1864 flags = (key >> 16) & 0xFF;
1865 if (ma) {
1866 uint32_t mask = nl_attr_get_u32(ma);
1867 id_mask = htons(mask & 0xFFFF);
1868 flags_mask = (mask >> 16) & 0xFF;
1869 }
1870
1871 ds_put_cstr(ds, "gbp(");
1872 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
1873 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
1874 ds_chomp(ds, ',');
1875 ds_put_cstr(ds, "),");
1876 break;
1877 }
1878
1879 default:
1880 format_unknown_key(ds, a, ma);
1881 }
1882 ofpbuf_clear(&ofp);
1883 }
1884
1885 ds_chomp(ds, ',');
1886 ofpbuf_uninit(&ofp);
1887}
1888
622a0a8e
JG
1889#define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1890
1891static void
1892format_odp_tun_geneve(const struct nlattr *attr,
1893 const struct nlattr *mask_attr, struct ds *ds,
1894 bool verbose)
1895{
1896 int opts_len = nl_attr_get_size(attr);
1897 const struct geneve_opt *opt = nl_attr_get(attr);
1898 const struct geneve_opt *mask = mask_attr ?
1899 nl_attr_get(mask_attr) : NULL;
1900
1901 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
1902 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
1903 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
1904 return;
1905 }
1906
1907 while (opts_len > 0) {
1908 unsigned int len;
1909 uint8_t data_len, data_len_mask;
1910
1911 if (opts_len < sizeof *opt) {
1912 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
1913 opts_len, sizeof *opt);
1914 return;
1915 }
1916
1917 data_len = opt->length * 4;
1918 if (mask) {
1919 if (mask->length == 0x1f) {
1920 data_len_mask = UINT8_MAX;
1921 } else {
1922 data_len_mask = mask->length;
1923 }
1924 }
1925 len = sizeof *opt + data_len;
1926 if (len > opts_len) {
1927 ds_put_format(ds, "opt len %u greater than remaining %u",
1928 len, opts_len);
1929 return;
1930 }
1931
1932 ds_put_char(ds, '{');
1933 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
1934 verbose);
1935 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
1936 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
1937 if (verbose || !mask || !is_all_zeros(mask + 1, data_len)) {
1938 ds_put_hex(ds, opt + 1, data_len);
1939 if (mask && !is_all_ones(mask + 1, data_len)) {
1940 ds_put_char(ds, '/');
1941 ds_put_hex(ds, mask + 1, data_len);
1942 }
1943 } else {
1944 ds_chomp(ds, ',');
1945 }
1946 ds_put_char(ds, '}');
1947
1948 opt += len / sizeof(*opt);
1949 if (mask) {
1950 mask += len / sizeof(*opt);
1951 }
1952 opts_len -= len;
1953 };
1954}
1955
65da723b
JG
1956static void
1957format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
1958 struct ds *ds, bool verbose)
1959{
1960 unsigned int left;
1961 const struct nlattr *a;
1962 uint16_t flags = 0;
1963 uint16_t mask_flags = 0;
1964 struct ofpbuf ofp;
1965
1966 ofpbuf_init(&ofp, 100);
1967 NL_NESTED_FOR_EACH(a, left, attr) {
1968 enum ovs_tunnel_key_attr type = nl_attr_type(a);
1969 const struct nlattr *ma = NULL;
1970
1971 if (mask_attr) {
1972 ma = nl_attr_find__(nl_attr_get(mask_attr),
1973 nl_attr_get_size(mask_attr), type);
1974 if (!ma) {
1975 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
1976 OVS_TUNNEL_KEY_ATTR_MAX,
1977 &ofp, a);
1978 }
1979 }
1980
1981 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
1982 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
1983 continue;
1984 }
1985
1986 switch (type) {
1987 case OVS_TUNNEL_KEY_ATTR_ID:
1988 format_be64(ds, "tun_id", nl_attr_get_be64(a),
1989 ma ? nl_attr_get(ma) : NULL, verbose);
1990 flags |= FLOW_TNL_F_KEY;
1991 if (ma) {
1992 mask_flags |= FLOW_TNL_F_KEY;
1993 }
1994 break;
1995 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1996 format_ipv4(ds, "src", nl_attr_get_be32(a),
1997 ma ? nl_attr_get(ma) : NULL, verbose);
1998 break;
1999 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2000 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2001 ma ? nl_attr_get(ma) : NULL, verbose);
2002 break;
2003 case OVS_TUNNEL_KEY_ATTR_TOS:
2004 format_u8x(ds, "tos", nl_attr_get_u8(a),
2005 ma ? nl_attr_get(ma) : NULL, verbose);
2006 break;
2007 case OVS_TUNNEL_KEY_ATTR_TTL:
2008 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2009 ma ? nl_attr_get(ma) : NULL, verbose);
2010 break;
2011 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2012 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2013 break;
2014 case OVS_TUNNEL_KEY_ATTR_CSUM:
2015 flags |= FLOW_TNL_F_CSUM;
2016 break;
2017 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2018 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2019 ma ? nl_attr_get(ma) : NULL, verbose);
2020 break;
2021 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2022 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2023 ma ? nl_attr_get(ma) : NULL, verbose);
2024 break;
2025 case OVS_TUNNEL_KEY_ATTR_OAM:
2026 flags |= FLOW_TNL_F_OAM;
2027 break;
2028 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2029 ds_put_cstr(ds, "vxlan(");
2030 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2031 ds_put_cstr(ds, "),");
2032 break;
2033 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
622a0a8e
JG
2034 ds_put_cstr(ds, "geneve(");
2035 format_odp_tun_geneve(a, ma, ds, verbose);
2036 ds_put_cstr(ds, "),");
2037 break;
65da723b
JG
2038 case __OVS_TUNNEL_KEY_ATTR_MAX:
2039 default:
2040 format_unknown_key(ds, a, ma);
2041 }
2042 ofpbuf_clear(&ofp);
2043 }
2044
2045 /* Flags can have a valid mask even if the attribute is not set, so
2046 * we need to collect these separately. */
2047 if (mask_attr) {
2048 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2049 switch (nl_attr_type(a)) {
2050 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2051 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2052 break;
2053 case OVS_TUNNEL_KEY_ATTR_CSUM:
2054 mask_flags |= FLOW_TNL_F_CSUM;
2055 break;
2056 case OVS_TUNNEL_KEY_ATTR_OAM:
2057 mask_flags |= FLOW_TNL_F_OAM;
2058 break;
2059 }
2060 }
2061 }
2062
2063 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2064 verbose);
2065 ds_chomp(ds, ',');
2066 ofpbuf_uninit(&ofp);
2067}
2068
2d18eae8
JR
2069static void
2070format_frag(struct ds *ds, const char *name, uint8_t key,
2071 const uint8_t *mask, bool verbose)
2072{
2073 bool mask_empty = mask && !*mask;
2074
2075 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2076 if (verbose || !mask_empty) {
2077 bool mask_full = !mask || *mask == UINT8_MAX;
2078
2079 if (!mask_full) { /* Partially masked. */
2080 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2081 PRIx8"),", *mask);
2082 } else {
2083 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2084 }
2085 }
2086}
2087
36956a7d 2088static void
e6cc0bab 2089format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
0a37839c
GS
2090 const struct hmap *portno_names, struct ds *ds,
2091 bool verbose)
36956a7d 2092{
7a8e9ed2 2093 enum ovs_key_attr attr = nl_attr_type(a);
e6603631 2094 char namebuf[OVS_KEY_ATTR_BUFSIZE];
dc8c5408 2095 bool is_exact;
36956a7d 2096
dc8c5408 2097 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
e6cc0bab 2098
e6603631 2099 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
e6cc0bab 2100
65da723b
JG
2101 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2102 OVS_KEY_ATTR_MAX, false)) {
2103 return;
36956a7d
BP
2104 }
2105
e6cc0bab 2106 ds_put_char(ds, '(');
7a8e9ed2 2107 switch (attr) {
fea393b1 2108 case OVS_KEY_ATTR_ENCAP:
e6cc0bab
AZ
2109 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2110 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
0a37839c 2111 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
041e7168 2112 verbose);
0a37839c
GS
2113 } else if (nl_attr_get_size(a)) {
2114 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2115 ds, verbose);
fea393b1 2116 }
fea393b1
BP
2117 break;
2118
abff858b 2119 case OVS_KEY_ATTR_PRIORITY:
72e8bf28 2120 case OVS_KEY_ATTR_SKB_MARK:
572f732a
AZ
2121 case OVS_KEY_ATTR_DP_HASH:
2122 case OVS_KEY_ATTR_RECIRC_ID:
e6cc0bab 2123 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
dc8c5408 2124 if (!is_exact) {
e6cc0bab
AZ
2125 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2126 }
72e8bf28
AA
2127 break;
2128
65da723b
JG
2129 case OVS_KEY_ATTR_TUNNEL:
2130 format_odp_tun_attr(a, ma, ds, verbose);
356af50b 2131 break;
65da723b 2132
df2c07f4 2133 case OVS_KEY_ATTR_IN_PORT:
0a37839c
GS
2134 if (portno_names && verbose && is_exact) {
2135 char *name = odp_portno_names_get(portno_names,
2136 u32_to_odp(nl_attr_get_u32(a)));
2137 if (name) {
2138 ds_put_format(ds, "%s", name);
2139 } else {
2140 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2141 }
2142 } else {
2143 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2144 if (!is_exact) {
2145 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2146 }
e6cc0bab 2147 }
36956a7d
BP
2148 break;
2149
2d18eae8
JR
2150 case OVS_KEY_ATTR_ETHERNET: {
2151 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2152 const struct ovs_key_ethernet *key = nl_attr_get(a);
e6cc0bab 2153
2d18eae8
JR
2154 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2155 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2156 ds_chomp(ds, ',');
36956a7d 2157 break;
2d18eae8 2158 }
fea393b1 2159 case OVS_KEY_ATTR_VLAN:
2d18eae8
JR
2160 format_vlan_tci(ds, nl_attr_get_be16(a),
2161 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
36956a7d
BP
2162 break;
2163
b02475c5
SH
2164 case OVS_KEY_ATTR_MPLS: {
2165 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
e6cc0bab 2166 const struct ovs_key_mpls *mpls_mask = NULL;
8bfd0fda
BP
2167 size_t size = nl_attr_get_size(a);
2168
2169 if (!size || size % sizeof *mpls_key) {
6d670e7f 2170 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
8bfd0fda
BP
2171 return;
2172 }
dc8c5408 2173 if (!is_exact) {
e6cc0bab 2174 mpls_mask = nl_attr_get(ma);
6d670e7f 2175 if (size != nl_attr_get_size(ma)) {
8bfd0fda
BP
2176 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2177 "mask length %"PRIuSIZE")",
6d670e7f 2178 size, nl_attr_get_size(ma));
8bfd0fda
BP
2179 return;
2180 }
e6cc0bab 2181 }
8bfd0fda 2182 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
b02475c5
SH
2183 break;
2184 }
df2c07f4 2185 case OVS_KEY_ATTR_ETHERTYPE:
e6cc0bab 2186 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
dc8c5408 2187 if (!is_exact) {
e6cc0bab
AZ
2188 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2189 }
36956a7d
BP
2190 break;
2191
2d18eae8
JR
2192 case OVS_KEY_ATTR_IPV4: {
2193 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2194 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2195
2196 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2197 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2198 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2199 verbose);
2200 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2201 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2202 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2203 verbose);
2204 ds_chomp(ds, ',');
36956a7d 2205 break;
2d18eae8
JR
2206 }
2207 case OVS_KEY_ATTR_IPV6: {
2208 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2209 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2210
2211 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2212 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2213 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2214 verbose);
2215 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2216 verbose);
2217 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2218 verbose);
2219 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2220 verbose);
2221 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2222 verbose);
2223 ds_chomp(ds, ',');
d31f1109 2224 break;
2d18eae8
JR
2225 }
2226 /* These have the same structure and format. */
df2c07f4 2227 case OVS_KEY_ATTR_TCP:
2d18eae8
JR
2228 case OVS_KEY_ATTR_UDP:
2229 case OVS_KEY_ATTR_SCTP: {
2230 const struct ovs_key_tcp *key = nl_attr_get(a);
2231 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
e6cc0bab 2232
2d18eae8
JR
2233 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2234 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2235 ds_chomp(ds, ',');
36956a7d 2236 break;
2d18eae8 2237 }
dc235f7f 2238 case OVS_KEY_ATTR_TCP_FLAGS:
dc235f7f 2239 if (!is_exact) {
ea2735d3
JR
2240 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2241 ntohs(nl_attr_get_be16(a)),
2242 ntohs(nl_attr_get_be16(ma)));
2243 } else {
2244 format_flags(ds, packet_tcp_flag_to_string,
2245 ntohs(nl_attr_get_be16(a)), ',');
dc235f7f
JR
2246 }
2247 break;
2248
2d18eae8
JR
2249 case OVS_KEY_ATTR_ICMP: {
2250 const struct ovs_key_icmp *key = nl_attr_get(a);
2251 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
e6cc0bab 2252
2d18eae8
JR
2253 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2254 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2255 ds_chomp(ds, ',');
36956a7d 2256 break;
2d18eae8
JR
2257 }
2258 case OVS_KEY_ATTR_ICMPV6: {
2259 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2260 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
36956a7d 2261
2d18eae8
JR
2262 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2263 verbose);
2264 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2265 verbose);
2266 ds_chomp(ds, ',');
d31f1109 2267 break;
2d18eae8
JR
2268 }
2269 case OVS_KEY_ATTR_ARP: {
2270 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2271 const struct ovs_key_arp *key = nl_attr_get(a);
d31f1109 2272
2d18eae8
JR
2273 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2274 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2275 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2276 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2277 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2278 ds_chomp(ds, ',');
36956a7d 2279 break;
2d18eae8 2280 }
df2c07f4 2281 case OVS_KEY_ATTR_ND: {
2d18eae8
JR
2282 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2283 const struct ovs_key_nd *key = nl_attr_get(a);
e6cc0bab 2284
2d18eae8
JR
2285 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2286 verbose);
2287 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2288 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
685a51a5 2289
2d18eae8 2290 ds_chomp(ds, ',');
685a51a5
JP
2291 break;
2292 }
7a8e9ed2
BP
2293 case OVS_KEY_ATTR_UNSPEC:
2294 case __OVS_KEY_ATTR_MAX:
36956a7d
BP
2295 default:
2296 format_generic_odp_key(a, ds);
dc8c5408 2297 if (!is_exact) {
e6cc0bab
AZ
2298 ds_put_char(ds, '/');
2299 format_generic_odp_key(ma, ds);
2300 }
36956a7d
BP
2301 break;
2302 }
e6cc0bab 2303 ds_put_char(ds, ')');
36956a7d
BP
2304}
2305
dc8c5408 2306static struct nlattr *
6b8da9e9
JG
2307generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2308 struct ofpbuf *ofp, const struct nlattr *key)
dc8c5408
AZ
2309{
2310 const struct nlattr *a;
2311 unsigned int left;
2312 int type = nl_attr_type(key);
2313 int size = nl_attr_get_size(key);
2314
6b8da9e9 2315 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
9ddf12cc 2316 nl_msg_put_unspec_zero(ofp, type, size);
dc8c5408
AZ
2317 } else {
2318 size_t nested_mask;
2319
6b8da9e9
JG
2320 if (tbl[type].next) {
2321 tbl = tbl[type].next;
2322 max = tbl[type].next_max;
2323 }
2324
dc8c5408
AZ
2325 nested_mask = nl_msg_start_nested(ofp, type);
2326 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
6b8da9e9 2327 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
dc8c5408
AZ
2328 }
2329 nl_msg_end_nested(ofp, nested_mask);
2330 }
2331
6fd6ed71 2332 return ofp->base;
dc8c5408
AZ
2333}
2334
534a19b9
JS
2335int
2336odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2337{
2338 const char *s = s_;
2339
2340 if (ovs_scan(s, "ufid:")) {
534a19b9 2341 s += 5;
534a19b9 2342
10e92b4f 2343 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
534a19b9
JS
2344 return -EINVAL;
2345 }
10e92b4f 2346 s += UUID_LEN;
534a19b9
JS
2347
2348 return s - s_;
2349 }
2350
2351 return 0;
2352}
2353
70e5ed6f
JS
2354void
2355odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2356{
10e92b4f 2357 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
70e5ed6f
JS
2358}
2359
36956a7d 2360/* Appends to 'ds' a string representation of the 'key_len' bytes of
e6cc0bab 2361 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
73580638
GS
2362 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2363 * non-null and 'verbose' is true, translates odp port number to its name. */
14608a15 2364void
e6cc0bab
AZ
2365odp_flow_format(const struct nlattr *key, size_t key_len,
2366 const struct nlattr *mask, size_t mask_len,
0a37839c 2367 const struct hmap *portno_names, struct ds *ds, bool verbose)
14608a15 2368{
36956a7d
BP
2369 if (key_len) {
2370 const struct nlattr *a;
2371 unsigned int left;
e6cc0bab
AZ
2372 bool has_ethtype_key = false;
2373 const struct nlattr *ma = NULL;
dc8c5408 2374 struct ofpbuf ofp;
041e7168 2375 bool first_field = true;
36956a7d 2376
dc8c5408 2377 ofpbuf_init(&ofp, 100);
36956a7d 2378 NL_ATTR_FOR_EACH (a, left, key, key_len) {
041e7168
AZ
2379 bool is_nested_attr;
2380 bool is_wildcard = false;
2381 int attr_type = nl_attr_type(a);
2382
2383 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
e6cc0bab
AZ
2384 has_ethtype_key = true;
2385 }
041e7168 2386
6b8da9e9
JG
2387 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2388 OVS_KEY_ATTR_MAX, attr_type) ==
2389 ATTR_LEN_NESTED;
041e7168 2390
e6cc0bab
AZ
2391 if (mask && mask_len) {
2392 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
041e7168
AZ
2393 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2394 }
2395
2396 if (verbose || !is_wildcard || is_nested_attr) {
2397 if (is_wildcard && !ma) {
6b8da9e9
JG
2398 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2399 OVS_KEY_ATTR_MAX,
2400 &ofp, a);
dc8c5408 2401 }
041e7168
AZ
2402 if (!first_field) {
2403 ds_put_char(ds, ',');
2404 }
0a37839c 2405 format_odp_key_attr(a, ma, portno_names, ds, verbose);
041e7168 2406 first_field = false;
e6cc0bab 2407 }
dc8c5408 2408 ofpbuf_clear(&ofp);
36956a7d 2409 }
dc8c5408
AZ
2410 ofpbuf_uninit(&ofp);
2411
36956a7d 2412 if (left) {
3a8cfc50 2413 int i;
1394054e 2414
36956a7d
BP
2415 if (left == key_len) {
2416 ds_put_cstr(ds, "<empty>");
2417 }
3a8cfc50
BP
2418 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2419 for (i = 0; i < left; i++) {
2420 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2421 }
2422 ds_put_char(ds, ')');
36956a7d 2423 }
e6cc0bab
AZ
2424 if (!has_ethtype_key) {
2425 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2426 if (ma) {
2427 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2428 ntohs(nl_attr_get_be16(ma)));
2429 }
2430 }
36956a7d
BP
2431 } else {
2432 ds_put_cstr(ds, "<empty>");
2433 }
14608a15 2434}
064af421 2435
e6cc0bab
AZ
2436/* Appends to 'ds' a string representation of the 'key_len' bytes of
2437 * OVS_KEY_ATTR_* attributes in 'key'. */
2438void
2439odp_flow_key_format(const struct nlattr *key,
2440 size_t key_len, struct ds *ds)
2441{
0a37839c 2442 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
e6cc0bab
AZ
2443}
2444
7257b535
BP
2445static bool
2446ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2447{
2448 if (!strcasecmp(s, "no")) {
2449 *type = OVS_FRAG_TYPE_NONE;
2450 } else if (!strcasecmp(s, "first")) {
2451 *type = OVS_FRAG_TYPE_FIRST;
2452 } else if (!strcasecmp(s, "later")) {
2453 *type = OVS_FRAG_TYPE_LATER;
2454 } else {
2455 return false;
2456 }
2457 return true;
2458}
2459
2d18eae8 2460/* Parsing. */
b02475c5 2461
3bffc610 2462static int
2d18eae8
JR
2463scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2464 uint8_t (*mask)[ETH_ADDR_LEN])
3bffc610 2465{
2d18eae8 2466 int n;
abff858b 2467
2d18eae8
JR
2468 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2469 int len = n;
2470
2471 if (mask) {
2472 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2473 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2474 len += n;
2475 } else {
2476 memset(mask, 0xff, sizeof *mask);
e6cc0bab 2477 }
abff858b 2478 }
2d18eae8 2479 return len;
abff858b 2480 }
2d18eae8
JR
2481 return 0;
2482}
abff858b 2483
2d18eae8
JR
2484static int
2485scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2486{
2487 int n;
72e8bf28 2488
2d18eae8
JR
2489 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2490 int len = n;
2491
2492 if (mask) {
2493 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2494 IP_SCAN_ARGS(mask), &n)) {
2495 len += n;
2496 } else {
2497 *mask = OVS_BE32_MAX;
e6cc0bab 2498 }
72e8bf28 2499 }
2d18eae8 2500 return len;
72e8bf28 2501 }
2d18eae8
JR
2502 return 0;
2503}
72e8bf28 2504
2d18eae8
JR
2505static int
2506scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2507{
2508 int n;
2509 char ipv6_s[IPV6_SCAN_LEN + 1];
572f732a 2510
2d18eae8
JR
2511 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2512 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2513 int len = n;
2514
2515 if (mask) {
2516 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2517 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2518 len += n;
2519 } else {
2520 memset(mask, 0xff, sizeof *mask);
78669073 2521 }
572f732a 2522 }
2d18eae8 2523 return len;
572f732a 2524 }
2d18eae8
JR
2525 return 0;
2526}
572f732a 2527
2d18eae8
JR
2528static int
2529scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2530{
2531 int key_, mask_;
2532 int n;
572f732a 2533
2d18eae8
JR
2534 if (ovs_scan(s, "%i%n", &key_, &n)
2535 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2536 int len = n;
29c7a2b2 2537
2d18eae8
JR
2538 *key = htonl(key_);
2539 if (mask) {
2540 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2541 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2542 len += n;
2543 *mask = htonl(mask_);
2544 } else {
2545 *mask = htonl(IPV6_LABEL_MASK);
e6cc0bab 2546 }
35651d6a 2547 }
2d18eae8 2548 return len;
35651d6a 2549 }
2d18eae8
JR
2550 return 0;
2551}
35651d6a 2552
2d18eae8
JR
2553static int
2554scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2555{
2556 int n;
3bffc610 2557
2d18eae8
JR
2558 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2559 int len = n;
2560
2561 if (mask) {
2562 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2563 len += n;
2564 } else {
2565 *mask = UINT8_MAX;
e6cc0bab 2566 }
3bffc610 2567 }
2d18eae8 2568 return len;
3bffc610 2569 }
2d18eae8
JR
2570 return 0;
2571}
3bffc610 2572
2d18eae8
JR
2573static int
2574scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2575{
2576 int n;
e6cc0bab 2577
2d18eae8
JR
2578 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2579 int len = n;
e6cc0bab 2580
2d18eae8
JR
2581 if (mask) {
2582 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2583 len += n;
2584 } else {
2585 *mask = UINT32_MAX;
e6cc0bab 2586 }
b2a60db8 2587 }
2d18eae8 2588 return len;
b2a60db8 2589 }
2d18eae8
JR
2590 return 0;
2591}
b2a60db8 2592
2d18eae8
JR
2593static int
2594scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2595{
2596 uint16_t key_, mask_;
2597 int n;
3bffc610 2598
2d18eae8
JR
2599 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2600 int len = n;
e6cc0bab 2601
2d18eae8
JR
2602 *key = htons(key_);
2603 if (mask) {
2604 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2605 len += n;
2606 *mask = htons(mask_);
2607 } else {
2608 *mask = OVS_BE16_MAX;
e6cc0bab 2609 }
3bffc610 2610 }
2d18eae8 2611 return len;
3bffc610 2612 }
2d18eae8
JR
2613 return 0;
2614}
3bffc610 2615
2d18eae8
JR
2616static int
2617scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2618{
2619 uint64_t key_, mask_;
2620 int n;
3bffc610 2621
2d18eae8
JR
2622 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2623 int len = n;
2624
2625 *key = htonll(key_);
2626 if (mask) {
2627 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2628 len += n;
2629 *mask = htonll(mask_);
2630 } else {
2631 *mask = OVS_BE64_MAX;
e6cc0bab 2632 }
3bffc610 2633 }
2d18eae8 2634 return len;
3bffc610 2635 }
2d18eae8
JR
2636 return 0;
2637}
3bffc610 2638
2d18eae8
JR
2639static int
2640scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2641{
2642 uint32_t flags, fmask;
2643 int n;
3bffc610 2644
2d18eae8
JR
2645 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2646 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2647 if (n >= 0 && s[n] == ')') {
2648 *key = flags;
2649 if (mask) {
2650 *mask = fmask;
3bffc610 2651 }
2d18eae8 2652 return n + 1;
3bffc610 2653 }
2d18eae8
JR
2654 return 0;
2655}
3bffc610 2656
2d18eae8
JR
2657static int
2658scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2659{
2660 uint32_t flags, fmask;
2661 int n;
b02475c5 2662
2d18eae8
JR
2663 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2664 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2665 if (n >= 0) {
2666 *key = htons(flags);
2667 if (mask) {
2668 *mask = htons(fmask);
b02475c5 2669 }
2d18eae8 2670 return n;
b02475c5 2671 }
2d18eae8
JR
2672 return 0;
2673}
b02475c5 2674
2d18eae8
JR
2675static int
2676scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2677{
2678 int n;
2679 char frag[8];
2680 enum ovs_frag_type frag_type;
e6cc0bab 2681
2d18eae8
JR
2682 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2683 && ovs_frag_type_from_string(frag, &frag_type)) {
2684 int len = n;
e6cc0bab 2685
2d18eae8
JR
2686 *key = frag_type;
2687 if (mask) {
2688 *mask = UINT8_MAX;
3bffc610 2689 }
2d18eae8 2690 return len;
3bffc610 2691 }
2d18eae8
JR
2692 return 0;
2693}
3bffc610 2694
2d18eae8
JR
2695static int
2696scan_port(const char *s, uint32_t *key, uint32_t *mask,
2697 const struct simap *port_names)
2698{
2699 int n;
3bffc610 2700
2d18eae8
JR
2701 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2702 int len = n;
e6cc0bab 2703
2d18eae8
JR
2704 if (mask) {
2705 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2706 len += n;
2707 } else {
2708 *mask = UINT32_MAX;
e6cc0bab 2709 }
3bffc610 2710 }
2d18eae8
JR
2711 return len;
2712 } else if (port_names) {
2713 const struct simap_node *node;
2714 int len;
e6cc0bab 2715
2d18eae8
JR
2716 len = strcspn(s, ")");
2717 node = simap_find_len(port_names, s, len);
2718 if (node) {
2719 *key = node->data;
e6cc0bab
AZ
2720
2721 if (mask) {
2d18eae8 2722 *mask = UINT32_MAX;
e6cc0bab 2723 }
2d18eae8 2724 return len;
3bffc610
BP
2725 }
2726 }
2d18eae8
JR
2727 return 0;
2728}
3bffc610 2729
2d18eae8
JR
2730/* Helper for vlan parsing. */
2731struct ovs_key_vlan__ {
2732 ovs_be16 tci;
2733};
dc235f7f 2734
2d18eae8
JR
2735static bool
2736set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2737{
2738 const uint16_t mask = ((1U << bits) - 1) << offset;
ea2735d3 2739
2d18eae8
JR
2740 if (value >> bits) {
2741 return false;
dc235f7f
JR
2742 }
2743
2d18eae8
JR
2744 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2745 return true;
2746}
e6cc0bab 2747
2d18eae8
JR
2748static int
2749scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2750 uint8_t offset)
2751{
2752 uint16_t key_, mask_;
2753 int n;
3bffc610 2754
2d18eae8
JR
2755 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2756 int len = n;
e6cc0bab 2757
2d18eae8 2758 if (set_be16_bf(key, bits, offset, key_)) {
e6cc0bab 2759 if (mask) {
2d18eae8
JR
2760 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2761 len += n;
2762
2763 if (!set_be16_bf(mask, bits, offset, mask_)) {
2764 return 0;
2765 }
2766 } else {
2767 *mask |= htons(((1U << bits) - 1) << offset);
2768 }
e6cc0bab 2769 }
2d18eae8 2770 return len;
3bffc610
BP
2771 }
2772 }
2d18eae8
JR
2773 return 0;
2774}
3bffc610 2775
2d18eae8
JR
2776static int
2777scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2778{
2779 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2780}
c6bcb685 2781
2d18eae8
JR
2782static int
2783scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2784{
2785 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2786}
c6bcb685 2787
2d18eae8
JR
2788static int
2789scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2790{
2791 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2792}
c6bcb685 2793
2d18eae8
JR
2794/* For MPLS. */
2795static bool
2796set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2797{
2798 const uint32_t mask = ((1U << bits) - 1) << offset;
c6bcb685 2799
2d18eae8
JR
2800 if (value >> bits) {
2801 return false;
c6bcb685
JS
2802 }
2803
2d18eae8
JR
2804 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2805 return true;
2806}
3bffc610 2807
2d18eae8
JR
2808static int
2809scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2810 uint8_t offset)
2811{
2812 uint32_t key_, mask_;
2813 int n;
3bffc610 2814
2d18eae8
JR
2815 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2816 int len = n;
e6cc0bab 2817
2d18eae8 2818 if (set_be32_bf(key, bits, offset, key_)) {
e6cc0bab 2819 if (mask) {
2d18eae8
JR
2820 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2821 len += n;
3bffc610 2822
2d18eae8
JR
2823 if (!set_be32_bf(mask, bits, offset, mask_)) {
2824 return 0;
2825 }
2826 } else {
2827 *mask |= htonl(((1U << bits) - 1) << offset);
2828 }
e6cc0bab 2829 }
2d18eae8 2830 return len;
3bffc610
BP
2831 }
2832 }
2d18eae8
JR
2833 return 0;
2834}
3bffc610 2835
2d18eae8
JR
2836static int
2837scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2838{
2839 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2840}
3bffc610 2841
2d18eae8
JR
2842static int
2843scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2844{
2845 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2846}
e6cc0bab 2847
2d18eae8
JR
2848static int
2849scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2850{
2851 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2852}
e6cc0bab 2853
2d18eae8
JR
2854static int
2855scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2856{
2857 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2858}
2859
65da723b
JG
2860static int
2861scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
2862{
2863 const char *s_base = s;
3139b8e9
JR
2864 ovs_be16 id = 0, id_mask = 0;
2865 uint8_t flags = 0, flags_mask = 0;
65da723b
JG
2866
2867 if (!strncmp(s, "id=", 3)) {
2868 s += 3;
2869 s += scan_be16(s, &id, mask ? &id_mask : NULL);
65da723b
JG
2870 }
2871
2872 if (s[0] == ',') {
2873 s++;
2874 }
2875 if (!strncmp(s, "flags=", 6)) {
2876 s += 6;
2877 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
65da723b
JG
2878 }
2879
2880 if (!strncmp(s, "))", 2)) {
2881 s += 2;
2882
2883 *key = (flags << 16) | ntohs(id);
2884 if (mask) {
2885 *mask = (flags_mask << 16) | ntohs(id_mask);
2886 }
2887
2888 return s - s_base;
2889 }
2890
2891 return 0;
2892}
2893
622a0a8e 2894struct geneve_scan {
c05c01cd 2895 struct geneve_opt d[63];
622a0a8e
JG
2896 int len;
2897};
2898
2899static int
2900scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
2901{
2902 const char *s_base = s;
c05c01cd
JG
2903 struct geneve_opt *opt = key->d;
2904 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
622a0a8e
JG
2905 int len_remain = sizeof key->d;
2906
2907 while (s[0] == '{' && len_remain >= sizeof *opt) {
2908 int data_len = 0;
2909
2910 s++;
2911 len_remain -= sizeof *opt;
2912
2913 if (!strncmp(s, "class=", 6)) {
2914 s += 6;
2915 s += scan_be16(s, &opt->opt_class,
2916 mask ? &opt_mask->opt_class : NULL);
2917 } else if (mask) {
2918 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
2919 }
2920
2921 if (s[0] == ',') {
2922 s++;
2923 }
2924 if (!strncmp(s, "type=", 5)) {
2925 s += 5;
2926 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
2927 } else if (mask) {
2928 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2929 }
2930
2931 if (s[0] == ',') {
2932 s++;
2933 }
2934 if (!strncmp(s, "len=", 4)) {
2935 uint8_t opt_len, opt_len_mask;
2936 s += 4;
2937 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
2938
2939 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
2940 return 0;
2941 }
2942 opt->length = opt_len / 4;
2943 if (mask) {
2944 opt_mask->length = opt_len_mask;
2945 }
2946 data_len = opt_len;
2947 } else if (mask) {
2948 memset(&opt_mask->type, 0, sizeof opt_mask->type);
2949 }
2950
2951 if (s[0] == ',') {
2952 s++;
2953 }
2954 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
2955 return 0;
2956 }
2957
2958 if (mask) {
2959 if (s[0] == '/') {
2960 s++;
2961 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
2962 data_len, (char **)&s)) {
2963 return 0;
2964 }
2965 }
2966 opt_mask->r1 = 0;
2967 opt_mask->r2 = 0;
2968 opt_mask->r3 = 0;
2969 }
2970
2971 if (s[0] == '}') {
2972 s++;
2973 opt += 1 + data_len / 4;
2974 if (mask) {
2975 opt_mask += 1 + data_len / 4;
2976 }
2977 len_remain -= data_len;
2978 }
2979 }
2980
2981 if (s[0] == ')') {
2982 int len = sizeof key->d - len_remain;
2983
2984 s++;
2985 key->len = len;
2986 if (mask) {
2987 mask->len = len;
2988 }
2989 return s - s_base;
2990 }
2991
2992 return 0;
2993}
2994
65da723b
JG
2995static void
2996tun_flags_to_attr(struct ofpbuf *a, const void *data_)
2997{
2998 const uint16_t *flags = data_;
2999
3000 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3001 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3002 }
3003 if (*flags & FLOW_TNL_F_CSUM) {
3004 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3005 }
3006 if (*flags & FLOW_TNL_F_OAM) {
3007 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3008 }
3009}
3010
3011static void
3012vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3013{
3014 const uint32_t *gbp = data_;
3015
3016 if (*gbp) {
3017 size_t vxlan_opts_ofs;
3018
3019 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3020 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3021 nl_msg_end_nested(a, vxlan_opts_ofs);
3022 }
3023}
3024
622a0a8e
JG
3025static void
3026geneve_to_attr(struct ofpbuf *a, const void *data_)
3027{
3028 const struct geneve_scan *geneve = data_;
3029
3030 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3031 geneve->len);
3032}
3033
65da723b
JG
3034#define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3035 { \
3036 unsigned long call_fn = (unsigned long)FUNC; \
3037 if (call_fn) { \
3038 typedef void (*fn)(struct ofpbuf *, const void *); \
3039 fn func = FUNC; \
3040 func(BUF, &(DATA)); \
3041 } else { \
3042 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3043 } \
2d18eae8
JR
3044 }
3045
3046#define SCAN_IF(NAME) \
3047 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3048 const char *start = s; \
3049 int len; \
3050 \
3051 s += strlen(NAME)
3052
3053/* Usually no special initialization is needed. */
3054#define SCAN_BEGIN(NAME, TYPE) \
3055 SCAN_IF(NAME); \
3056 TYPE skey, smask; \
3057 memset(&skey, 0, sizeof skey); \
3058 memset(&smask, 0, sizeof smask); \
3059 do { \
3060 len = 0;
3061
657ac953
JR
3062/* Init as fully-masked as mask will not be scanned. */
3063#define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3064 SCAN_IF(NAME); \
3065 TYPE skey, smask; \
3066 memset(&skey, 0, sizeof skey); \
3067 memset(&smask, 0xff, sizeof smask); \
3068 do { \
3069 len = 0;
3070
2d18eae8
JR
3071/* VLAN needs special initialization. */
3072#define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3073 SCAN_IF(NAME); \
3074 TYPE skey = KEY_INIT; \
3075 TYPE smask = MASK_INIT; \
3076 do { \
3077 len = 0;
3078
3079/* Scan unnamed entry as 'TYPE' */
3080#define SCAN_TYPE(TYPE, KEY, MASK) \
3081 len = scan_##TYPE(s, KEY, MASK); \
3082 if (len == 0) { \
3083 return -EINVAL; \
3084 } \
3085 s += len
3086
3087/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3088#define SCAN_FIELD(NAME, TYPE, FIELD) \
3089 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3090 s += strlen(NAME); \
3091 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3092 continue; \
3093 }
3094
3095#define SCAN_FINISH() \
3096 } while (*s++ == ',' && len != 0); \
3097 if (s[-1] != ')') { \
3098 return -EINVAL; \
3099 }
3100
3101#define SCAN_FINISH_SINGLE() \
3102 } while (false); \
3103 if (*s++ != ')') { \
3104 return -EINVAL; \
3105 }
3106
65da723b
JG
3107/* Beginning of nested attribute. */
3108#define SCAN_BEGIN_NESTED(NAME, ATTR) \
3109 SCAN_IF(NAME); \
3110 size_t key_offset, mask_offset; \
3111 key_offset = nl_msg_start_nested(key, ATTR); \
3112 if (mask) { \
3113 mask_offset = nl_msg_start_nested(mask, ATTR); \
3114 } \
3115 do { \
3116 len = 0;
3117
3118#define SCAN_END_NESTED() \
3119 SCAN_FINISH(); \
3120 nl_msg_end_nested(key, key_offset); \
3121 if (mask) { \
3122 nl_msg_end_nested(mask, mask_offset); \
3123 } \
3124 return s - start; \
3125 }
3126
3127#define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3128 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3129 TYPE skey, smask; \
3130 memset(&skey, 0, sizeof skey); \
3131 memset(&smask, 0xff, sizeof smask); \
3132 s += strlen(NAME); \
3133 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3134 SCAN_PUT(ATTR, FUNC); \
3135 continue; \
3136 }
3137
3138#define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3139 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3140
3141#define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3142 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3143
3144#define SCAN_PUT(ATTR, FUNC) \
2d18eae8 3145 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
65da723b 3146 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
2d18eae8 3147 if (mask) { \
65da723b 3148 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
2d18eae8
JR
3149 } \
3150 }
3151
3152#define SCAN_END(ATTR) \
3153 SCAN_FINISH(); \
65da723b 3154 SCAN_PUT(ATTR, NULL); \
2d18eae8
JR
3155 return s - start; \
3156 }
3157
3158#define SCAN_END_SINGLE(ATTR) \
3159 SCAN_FINISH_SINGLE(); \
65da723b 3160 SCAN_PUT(ATTR, NULL); \
2d18eae8
JR
3161 return s - start; \
3162 }
3163
3164#define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3165 SCAN_BEGIN(NAME, TYPE) { \
3166 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3167 } SCAN_END_SINGLE(ATTR)
3168
657ac953
JR
3169#define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3170 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3171 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2d18eae8
JR
3172 } SCAN_END_SINGLE(ATTR)
3173
3174/* scan_port needs one extra argument. */
3175#define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3176 SCAN_BEGIN(NAME, TYPE) { \
3177 len = scan_port(s, &skey, &smask, port_names); \
3178 if (len == 0) { \
3179 return -EINVAL; \
3180 } \
3181 s += len; \
3182 } SCAN_END_SINGLE(ATTR)
3bffc610 3183
2d18eae8
JR
3184static int
3185parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3186 struct ofpbuf *key, struct ofpbuf *mask)
3187{
10e92b4f
JS
3188 ovs_u128 ufid;
3189 int len;
eb731b76 3190
10e92b4f
JS
3191 /* Skip UFID. */
3192 len = odp_ufid_from_string(s, &ufid);
3193 if (len) {
3194 return len;
eb731b76
JS
3195 }
3196
2d18eae8
JR
3197 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3198 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
657ac953
JR
3199 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3200 OVS_KEY_ATTR_RECIRC_ID);
2d18eae8
JR
3201 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3202
65da723b
JG
3203 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3204 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3205 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3206 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3207 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3208 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3209 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3210 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3211 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
622a0a8e
JG
3212 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3213 geneve_to_attr);
65da723b
JG
3214 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3215 } SCAN_END_NESTED();
2d18eae8
JR
3216
3217 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3218
3219 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3220 SCAN_FIELD("src=", eth, eth_src);
3221 SCAN_FIELD("dst=", eth, eth_dst);
3222 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3223
3224 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3225 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3226 SCAN_FIELD("vid=", vid, tci);
3227 SCAN_FIELD("pcp=", pcp, tci);
3228 SCAN_FIELD("cfi=", cfi, tci);
3229 } SCAN_END(OVS_KEY_ATTR_VLAN);
3230
3231 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3232
3233 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3234 SCAN_FIELD("label=", mpls_label, mpls_lse);
3235 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3236 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3237 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3238 } SCAN_END(OVS_KEY_ATTR_MPLS);
3239
3240 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3241 SCAN_FIELD("src=", ipv4, ipv4_src);
3242 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3243 SCAN_FIELD("proto=", u8, ipv4_proto);
3244 SCAN_FIELD("tos=", u8, ipv4_tos);
3245 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3246 SCAN_FIELD("frag=", frag, ipv4_frag);
3247 } SCAN_END(OVS_KEY_ATTR_IPV4);
3248
3249 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3250 SCAN_FIELD("src=", ipv6, ipv6_src);
3251 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3252 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3253 SCAN_FIELD("proto=", u8, ipv6_proto);
3254 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3255 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3256 SCAN_FIELD("frag=", frag, ipv6_frag);
3257 } SCAN_END(OVS_KEY_ATTR_IPV6);
3258
3259 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3260 SCAN_FIELD("src=", be16, tcp_src);
3261 SCAN_FIELD("dst=", be16, tcp_dst);
3262 } SCAN_END(OVS_KEY_ATTR_TCP);
3263
3264 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3265
3266 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3267 SCAN_FIELD("src=", be16, udp_src);
3268 SCAN_FIELD("dst=", be16, udp_dst);
3269 } SCAN_END(OVS_KEY_ATTR_UDP);
3270
3271 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3272 SCAN_FIELD("src=", be16, sctp_src);
3273 SCAN_FIELD("dst=", be16, sctp_dst);
3274 } SCAN_END(OVS_KEY_ATTR_SCTP);
3275
3276 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
3277 SCAN_FIELD("type=", u8, icmp_type);
3278 SCAN_FIELD("code=", u8, icmp_code);
3279 } SCAN_END(OVS_KEY_ATTR_ICMP);
3280
3281 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
3282 SCAN_FIELD("type=", u8, icmpv6_type);
3283 SCAN_FIELD("code=", u8, icmpv6_code);
3284 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
3285
3286 SCAN_BEGIN("arp(", struct ovs_key_arp) {
3287 SCAN_FIELD("sip=", ipv4, arp_sip);
3288 SCAN_FIELD("tip=", ipv4, arp_tip);
3289 SCAN_FIELD("op=", be16, arp_op);
3290 SCAN_FIELD("sha=", eth, arp_sha);
3291 SCAN_FIELD("tha=", eth, arp_tha);
3292 } SCAN_END(OVS_KEY_ATTR_ARP);
3293
3294 SCAN_BEGIN("nd(", struct ovs_key_nd) {
3295 SCAN_FIELD("target=", ipv6, nd_target);
3296 SCAN_FIELD("sll=", eth, nd_sll);
3297 SCAN_FIELD("tll=", eth, nd_tll);
3298 } SCAN_END(OVS_KEY_ATTR_ND);
3299
3300 /* Encap open-coded. */
fea393b1
BP
3301 if (!strncmp(s, "encap(", 6)) {
3302 const char *start = s;
e6cc0bab 3303 size_t encap, encap_mask = 0;
fea393b1
BP
3304
3305 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
e6cc0bab
AZ
3306 if (mask) {
3307 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
3308 }
fea393b1
BP
3309
3310 s += 6;
3311 for (;;) {
3312 int retval;
3313
70fbe375 3314 s += strspn(s, delimiters);
fea393b1
BP
3315 if (!*s) {
3316 return -EINVAL;
3317 } else if (*s == ')') {
3318 break;
3319 }
3320
e6cc0bab 3321 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
fea393b1
BP
3322 if (retval < 0) {
3323 return retval;
3324 }
3325 s += retval;
3326 }
3327 s++;
3328
3329 nl_msg_end_nested(key, encap);
e6cc0bab
AZ
3330 if (mask) {
3331 nl_msg_end_nested(mask, encap_mask);
3332 }
fea393b1
BP
3333
3334 return s - start;
3335 }
3336
3bffc610
BP
3337 return -EINVAL;
3338}
3339
df2c07f4
JP
3340/* Parses the string representation of a datapath flow key, in the
3341 * format output by odp_flow_key_format(). Returns 0 if successful,
3342 * otherwise a positive errno value. On success, the flow key is
3343 * appended to 'key' as a series of Netlink attributes. On failure, no
3344 * data is appended to 'key'. Either way, 'key''s data might be
3345 * reallocated.
3bffc610 3346 *
44bac24b
BP
3347 * If 'port_names' is nonnull, it points to an simap that maps from a port name
3348 * to a port number. (Port names may be used instead of port numbers in
3349 * in_port.)
b2a60db8 3350 *
3bffc610
BP
3351 * On success, the attributes appended to 'key' are individually syntactically
3352 * valid, but they may not be valid as a sequence. 'key' might, for example,
34118cae 3353 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
3bffc610 3354int
e6cc0bab
AZ
3355odp_flow_from_string(const char *s, const struct simap *port_names,
3356 struct ofpbuf *key, struct ofpbuf *mask)
3bffc610 3357{
6fd6ed71 3358 const size_t old_size = key->size;
3bffc610
BP
3359 for (;;) {
3360 int retval;
3361
7202cbe5 3362 s += strspn(s, delimiters);
3bffc610
BP
3363 if (!*s) {
3364 return 0;
3365 }
3366
e6cc0bab 3367 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
3bffc610 3368 if (retval < 0) {
6fd6ed71 3369 key->size = old_size;
3bffc610
BP
3370 return -retval;
3371 }
3372 s += retval;
3373 }
3374
3375 return 0;
3376}
3377
7257b535 3378static uint8_t
3cea18ec 3379ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
7257b535 3380{
3cea18ec
JR
3381 if (is_mask) {
3382 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
3383 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
3384 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
3385 * must use a zero mask for the netlink frag field, and all ones mask
3386 * otherwise. */
3387 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
3388 }
2c0f0be1
JR
3389 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
3390 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
3391 : OVS_FRAG_TYPE_FIRST;
7257b535
BP
3392}
3393
3cea18ec
JR
3394static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
3395static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
3396static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
3397 bool is_mask);
3398static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
3399 bool is_mask);
3400static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
3401 bool is_mask);
3402static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
3403 bool is_mask);
3404static void get_arp_key(const struct flow *, struct ovs_key_arp *);
3405static void put_arp_key(const struct ovs_key_arp *, struct flow *);
e60e935b
SRCSA
3406static void get_nd_key(const struct flow *, struct ovs_key_nd *);
3407static void put_nd_key(const struct ovs_key_nd *, struct flow *);
3cea18ec
JR
3408
3409/* These share the same layout. */
3410union ovs_key_tp {
3411 struct ovs_key_tcp tcp;
3412 struct ovs_key_udp udp;
3413 struct ovs_key_sctp sctp;
3414};
3415
3416static void get_tp_key(const struct flow *, union ovs_key_tp *);
3417static void put_tp_key(const union ovs_key_tp *, struct flow *);
431495b1 3418
661cbcd5 3419static void
fbfe01de
AZ
3420odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
3421 const struct flow *mask, odp_port_t odp_in_port,
7ce2769e 3422 size_t max_mpls_depth, bool recirc, bool export_mask)
14608a15 3423{
df2c07f4 3424 struct ovs_key_ethernet *eth_key;
fea393b1 3425 size_t encap;
fbfe01de 3426 const struct flow *data = export_mask ? mask : flow;
661cbcd5 3427
54bb0348 3428 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
abff858b 3429
fbfe01de 3430 if (flow->tunnel.ip_dst || export_mask) {
661cbcd5 3431 tun_key_to_attr(buf, &data->tunnel);
36956a7d
BP
3432 }
3433
1362e248 3434 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
72e8bf28 3435
7ce2769e 3436 if (recirc) {
572f732a 3437 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
572f732a
AZ
3438 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
3439 }
3440
661cbcd5
JP
3441 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
3442 * is not the magical value "ODPP_NONE". */
fbfe01de 3443 if (export_mask || odp_in_port != ODPP_NONE) {
4e022ec0 3444 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
18886b60 3445 }
36956a7d 3446
df2c07f4 3447 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
36956a7d 3448 sizeof *eth_key);
3cea18ec 3449 get_ethernet_key(data, eth_key);
36956a7d 3450
8ddc056d 3451 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
fbfe01de 3452 if (export_mask) {
b8266395 3453 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
bed7d6a1
JP
3454 } else {
3455 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3456 }
661cbcd5 3457 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
fea393b1 3458 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
8ddc056d
BP
3459 if (flow->vlan_tci == htons(0)) {
3460 goto unencap;
3461 }
fea393b1
BP
3462 } else {
3463 encap = 0;
36956a7d
BP
3464 }
3465
3466 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
661cbcd5
JP
3467 /* For backwards compatibility with kernels that don't support
3468 * wildcarding, the following convention is used to encode the
3469 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3470 *
3471 * key mask matches
3472 * -------- -------- -------
3473 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3474 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3475 * <none> 0xffff Any non-Ethernet II frame (except valid
3476 * 802.3 SNAP packet with valid eth_type).
3477 */
fbfe01de 3478 if (export_mask) {
b8266395 3479 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
661cbcd5 3480 }
fea393b1 3481 goto unencap;
36956a7d
BP
3482 }
3483
661cbcd5 3484 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
36956a7d
BP
3485
3486 if (flow->dl_type == htons(ETH_TYPE_IP)) {
df2c07f4 3487 struct ovs_key_ipv4 *ipv4_key;
36956a7d 3488
df2c07f4 3489 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
36956a7d 3490 sizeof *ipv4_key);
3cea18ec 3491 get_ipv4_key(data, ipv4_key, export_mask);
d31f1109 3492 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
df2c07f4 3493 struct ovs_key_ipv6 *ipv6_key;
d31f1109 3494
df2c07f4 3495 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
d31f1109 3496 sizeof *ipv6_key);
3cea18ec 3497 get_ipv6_key(data, ipv6_key, export_mask);
8087f5ff
MM
3498 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3499 flow->dl_type == htons(ETH_TYPE_RARP)) {
df2c07f4 3500 struct ovs_key_arp *arp_key;
d31f1109 3501
3cea18ec
JR
3502 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3503 sizeof *arp_key);
3504 get_arp_key(data, arp_key);
b0a17866 3505 } else if (eth_type_mpls(flow->dl_type)) {
b02475c5 3506 struct ovs_key_mpls *mpls_key;
8bfd0fda 3507 int i, n;
b02475c5 3508
8bfd0fda
BP
3509 n = flow_count_mpls_labels(flow, NULL);
3510 n = MIN(n, max_mpls_depth);
b02475c5 3511 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
8bfd0fda
BP
3512 n * sizeof *mpls_key);
3513 for (i = 0; i < n; i++) {
3514 mpls_key[i].mpls_lse = data->mpls_lse[i];
3515 }
b02475c5
SH
3516 }
3517
48cecbdc 3518 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6767a2cc 3519 if (flow->nw_proto == IPPROTO_TCP) {
3cea18ec 3520 union ovs_key_tp *tcp_key;
36956a7d 3521
df2c07f4 3522 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
36956a7d 3523 sizeof *tcp_key);
3cea18ec 3524 get_tp_key(data, tcp_key);
dc235f7f
JR
3525 if (data->tcp_flags) {
3526 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3527 }
6767a2cc 3528 } else if (flow->nw_proto == IPPROTO_UDP) {
3cea18ec 3529 union ovs_key_tp *udp_key;
36956a7d 3530
df2c07f4 3531 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
36956a7d 3532 sizeof *udp_key);
3cea18ec 3533 get_tp_key(data, udp_key);
c6bcb685 3534 } else if (flow->nw_proto == IPPROTO_SCTP) {
3cea18ec 3535 union ovs_key_tp *sctp_key;
c6bcb685
JS
3536
3537 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3538 sizeof *sctp_key);
3cea18ec 3539 get_tp_key(data, sctp_key);
d31f1109
JP
3540 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3541 && flow->nw_proto == IPPROTO_ICMP) {
df2c07f4 3542 struct ovs_key_icmp *icmp_key;
36956a7d 3543
df2c07f4 3544 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
36956a7d 3545 sizeof *icmp_key);
661cbcd5
JP
3546 icmp_key->icmp_type = ntohs(data->tp_src);
3547 icmp_key->icmp_code = ntohs(data->tp_dst);
d31f1109
JP
3548 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3549 && flow->nw_proto == IPPROTO_ICMPV6) {
df2c07f4 3550 struct ovs_key_icmpv6 *icmpv6_key;
d31f1109 3551
df2c07f4 3552 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
d31f1109 3553 sizeof *icmpv6_key);
661cbcd5
JP
3554 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3555 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
685a51a5 3556
d8efdda8
JS
3557 if (flow->tp_dst == htons(0)
3558 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3559 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3560 && (!export_mask || (data->tp_src == htons(0xffff)
3561 && data->tp_dst == htons(0xffff)))) {
6f8dbd27 3562
df2c07f4 3563 struct ovs_key_nd *nd_key;
685a51a5 3564
df2c07f4 3565 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
685a51a5 3566 sizeof *nd_key);
661cbcd5 3567 memcpy(nd_key->nd_target, &data->nd_target,
685a51a5 3568 sizeof nd_key->nd_target);
661cbcd5
JP
3569 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3570 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
685a51a5 3571 }
36956a7d 3572 }
36956a7d 3573 }
fea393b1
BP
3574
3575unencap:
3576 if (encap) {
3577 nl_msg_end_nested(buf, encap);
3578 }
36956a7d 3579}
661cbcd5
JP
3580
3581/* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3582 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3583 * number rather than a datapath port number). Instead, if 'odp_in_port'
3584 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3585 * port.
3586 *
3587 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
7ce2769e
JS
3588 * capable of being expanded to allow for that much space.
3589 *
3590 * 'recirc' indicates support for recirculation fields. If this is true, then
3591 * these fields will always be serialised. */
661cbcd5
JP
3592void
3593odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
7ce2769e
JS
3594 const struct flow *mask, odp_port_t odp_in_port,
3595 bool recirc)
661cbcd5 3596{
7ce2769e
JS
3597 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3598 false);
661cbcd5
JP
3599}
3600
3601/* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3602 * 'buf'. 'flow' is used as a template to determine how to interpret
3603 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3604 * it doesn't indicate whether the other fields should be interpreted as
3605 * ARP, IPv4, IPv6, etc.
3606 *
3607 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
7ce2769e
JS
3608 * capable of being expanded to allow for that much space.
3609 *
3610 * 'recirc' indicates support for recirculation fields. If this is true, then
3611 * these fields will always be serialised. */
661cbcd5
JP
3612void
3613odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
8bfd0fda 3614 const struct flow *flow, uint32_t odp_in_port_mask,
7ce2769e 3615 size_t max_mpls_depth, bool recirc)
661cbcd5 3616{
7ce2769e
JS
3617 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3618 max_mpls_depth, recirc, true);
661cbcd5 3619}
36956a7d 3620
758c456d
JR
3621/* Generate ODP flow key from the given packet metadata */
3622void
3623odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3624{
3625 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3626
3627 if (md->tunnel.ip_dst) {
3628 tun_key_to_attr(buf, &md->tunnel);
3629 }
3630
3631 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3632
3633 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3634 * value "ODPP_NONE". */
b5e7e61a
AZ
3635 if (md->in_port.odp_port != ODPP_NONE) {
3636 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
758c456d
JR
3637 }
3638}
3639
3640/* Generate packet metadata from the given ODP flow key. */
3641void
3642odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3643 struct pkt_metadata *md)
3644{
3645 const struct nlattr *nla;
3646 size_t left;
3647 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3648 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3649 1u << OVS_KEY_ATTR_IN_PORT;
3650
b5e7e61a 3651 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
758c456d
JR
3652
3653 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3654 uint16_t type = nl_attr_type(nla);
3655 size_t len = nl_attr_get_size(nla);
6b8da9e9
JG
3656 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3657 OVS_KEY_ATTR_MAX, type);
758c456d
JR
3658
3659 if (len != expected_len && expected_len >= 0) {
3660 continue;
3661 }
3662
572f732a
AZ
3663 switch (type) {
3664 case OVS_KEY_ATTR_RECIRC_ID:
3665 md->recirc_id = nl_attr_get_u32(nla);
3666 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3667 break;
3668 case OVS_KEY_ATTR_DP_HASH:
3669 md->dp_hash = nl_attr_get_u32(nla);
3670 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3671 break;
3672 case OVS_KEY_ATTR_PRIORITY:
758c456d
JR
3673 md->skb_priority = nl_attr_get_u32(nla);
3674 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
572f732a
AZ
3675 break;
3676 case OVS_KEY_ATTR_SKB_MARK:
758c456d
JR
3677 md->pkt_mark = nl_attr_get_u32(nla);
3678 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
572f732a
AZ
3679 break;
3680 case OVS_KEY_ATTR_TUNNEL: {
758c456d
JR
3681 enum odp_key_fitness res;
3682
3683 res = odp_tun_key_from_attr(nla, &md->tunnel);
3684 if (res == ODP_FIT_ERROR) {
3685 memset(&md->tunnel, 0, sizeof md->tunnel);
3686 } else if (res == ODP_FIT_PERFECT) {
3687 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3688 }
572f732a
AZ
3689 break;
3690 }
3691 case OVS_KEY_ATTR_IN_PORT:
b5e7e61a 3692 md->in_port.odp_port = nl_attr_get_odp_port(nla);
758c456d 3693 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
572f732a
AZ
3694 break;
3695 default:
3696 break;
758c456d
JR
3697 }
3698
3699 if (!wanted_attrs) {
3700 return; /* Have everything. */
3701 }
3702 }
3703}
3704
b0f7b9b5
BP
3705uint32_t
3706odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3707{
3708 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
db5a1019
AW
3709 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3710 key_len / sizeof(uint32_t), 0);
b0f7b9b5
BP
3711}
3712
34118cae
BP
3713static void
3714log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
b0f7b9b5 3715 uint64_t attrs, int out_of_range_attr,
34118cae
BP
3716 const struct nlattr *key, size_t key_len)
3717{
3718 struct ds s;
3719 int i;
3720
b0f7b9b5 3721 if (VLOG_DROP_DBG(rl)) {
34118cae
BP
3722 return;
3723 }
3724
3725 ds_init(&s);
b0f7b9b5
BP
3726 for (i = 0; i < 64; i++) {
3727 if (attrs & (UINT64_C(1) << i)) {
e6603631
BP
3728 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3729
3730 ds_put_format(&s, " %s",
3731 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
34118cae
BP
3732 }
3733 }
b0f7b9b5
BP
3734 if (out_of_range_attr) {
3735 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3736 }
34118cae
BP
3737
3738 ds_put_cstr(&s, ": ");
3739 odp_flow_key_format(key, key_len, &s);
3740
b0f7b9b5 3741 VLOG_DBG("%s:%s", title, ds_cstr(&s));
34118cae
BP
3742 ds_destroy(&s);
3743}
3744
3cea18ec
JR
3745static uint8_t
3746odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
7257b535 3747{
34118cae
BP
3748 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3749
3cea18ec
JR
3750 if (is_mask) {
3751 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3752 }
3753
9e44d715 3754 if (odp_frag > OVS_FRAG_TYPE_LATER) {
b0f7b9b5 3755 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3cea18ec 3756 return 0xff; /* Error. */
7257b535
BP
3757 }
3758
3cea18ec
JR
3759 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3760 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3761 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
7257b535
BP
3762}
3763
b0f7b9b5 3764static bool
fea393b1 3765parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
b0f7b9b5
BP
3766 const struct nlattr *attrs[], uint64_t *present_attrsp,
3767 int *out_of_range_attrp)
36956a7d 3768{
b0f7b9b5 3769 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
36956a7d 3770 const struct nlattr *nla;
34118cae 3771 uint64_t present_attrs;
36956a7d
BP
3772 size_t left;
3773
2aef1214 3774 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
34118cae 3775 present_attrs = 0;
b0f7b9b5 3776 *out_of_range_attrp = 0;
36956a7d 3777 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
34118cae
BP
3778 uint16_t type = nl_attr_type(nla);
3779 size_t len = nl_attr_get_size(nla);
6b8da9e9
JG
3780 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3781 OVS_KEY_ATTR_MAX, type);
34118cae 3782
b0f7b9b5 3783 if (len != expected_len && expected_len >= 0) {
e6603631
BP
3784 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3785
34582733 3786 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
e6603631
BP
3787 "length %d", ovs_key_attr_to_string(type, namebuf,
3788 sizeof namebuf),
b0f7b9b5
BP
3789 len, expected_len);
3790 return false;
34118cae
BP
3791 }
3792
2aef1214 3793 if (type > OVS_KEY_ATTR_MAX) {
b0f7b9b5
BP
3794 *out_of_range_attrp = type;
3795 } else {
3796 if (present_attrs & (UINT64_C(1) << type)) {
e6603631
BP
3797 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3798
b0f7b9b5 3799 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
e6603631
BP
3800 ovs_key_attr_to_string(type,
3801 namebuf, sizeof namebuf));
b0f7b9b5
BP
3802 return false;
3803 }
3804
3805 present_attrs |= UINT64_C(1) << type;
3806 attrs[type] = nla;
3807 }
34118cae
BP
3808 }
3809 if (left) {
3810 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
b0f7b9b5 3811 return false;
34118cae
BP
3812 }
3813
fea393b1 3814 *present_attrsp = present_attrs;
b0f7b9b5 3815 return true;
fea393b1
BP
3816}
3817
b0f7b9b5
BP
3818static enum odp_key_fitness
3819check_expectations(uint64_t present_attrs, int out_of_range_attr,
3820 uint64_t expected_attrs,
fea393b1
BP
3821 const struct nlattr *key, size_t key_len)
3822{
3823 uint64_t missing_attrs;
3824 uint64_t extra_attrs;
3825
3826 missing_attrs = expected_attrs & ~present_attrs;
3827 if (missing_attrs) {
b0f7b9b5
BP
3828 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3829 log_odp_key_attributes(&rl, "expected but not present",
3830 missing_attrs, 0, key, key_len);
3831 return ODP_FIT_TOO_LITTLE;
fea393b1
BP
3832 }
3833
3834 extra_attrs = present_attrs & ~expected_attrs;
b0f7b9b5
BP
3835 if (extra_attrs || out_of_range_attr) {
3836 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3837 log_odp_key_attributes(&rl, "present but not expected",
3838 extra_attrs, out_of_range_attr, key, key_len);
3839 return ODP_FIT_TOO_MUCH;
fea393b1
BP
3840 }
3841
b0f7b9b5 3842 return ODP_FIT_PERFECT;
fea393b1
BP
3843}
3844
b0f7b9b5
BP
3845static bool
3846parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3847 uint64_t present_attrs, uint64_t *expected_attrs,
4a221615 3848 struct flow *flow, const struct flow *src_flow)
fea393b1
BP
3849{
3850 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4a221615 3851 bool is_mask = flow != src_flow;
36956a7d 3852
fea393b1 3853 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
34118cae 3854 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
4a221615 3855 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
34118cae
BP
3856 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3857 ntohs(flow->dl_type));
b0f7b9b5 3858 return false;
34118cae 3859 }
4a221615
GY
3860 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3861 flow->dl_type != htons(0xffff)) {
3862 return false;
3863 }
b0f7b9b5 3864 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
34118cae 3865 } else {
4a221615
GY
3866 if (!is_mask) {
3867 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3868 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3869 /* See comments in odp_flow_key_from_flow__(). */
3870 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3871 return false;
3872 }
34118cae 3873 }
b0f7b9b5
BP
3874 return true;
3875}
3876
3877static enum odp_key_fitness
b02475c5
SH
3878parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3879 uint64_t present_attrs, int out_of_range_attr,
3880 uint64_t expected_attrs, struct flow *flow,
4a221615 3881 const struct nlattr *key, size_t key_len,
91a77332 3882 const struct flow *src_flow)
b0f7b9b5
BP
3883{
3884 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4a221615
GY
3885 bool is_mask = src_flow != flow;
3886 const void *check_start = NULL;
3887 size_t check_len = 0;
3888 enum ovs_key_attr expected_bit = 0xff;
3889
3890 if (eth_type_mpls(src_flow->dl_type)) {
7e6621e9 3891 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
91a77332 3892 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
7e6621e9
JS
3893 }
3894 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3895 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3896 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3897 int n = size / sizeof(ovs_be32);
3898 int i;
4a221615 3899
7e6621e9
JS
3900 if (!size || size % sizeof(ovs_be32)) {
3901 return ODP_FIT_ERROR;
91a77332 3902 }
8bfd0fda 3903 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
4a221615
GY
3904 return ODP_FIT_ERROR;
3905 }
8bfd0fda 3906
7e6621e9
JS
3907 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3908 flow->mpls_lse[i] = mpls_lse[i];
3909 }
3910 if (n > FLOW_MAX_MPLS_LABELS) {
3911 return ODP_FIT_TOO_MUCH;
3912 }
8bfd0fda 3913
7e6621e9
JS
3914 if (!is_mask) {
3915 /* BOS may be set only in the innermost label. */
3916 for (i = 0; i < n - 1; i++) {
3917 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3918 return ODP_FIT_ERROR;
3919 }
8bfd0fda 3920 }
8bfd0fda 3921
7e6621e9
JS
3922 /* BOS must be set in the innermost label. */
3923 if (n < FLOW_MAX_MPLS_LABELS
3924 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3925 return ODP_FIT_TOO_LITTLE;
3926 }
8bfd0fda
BP
3927 }
3928 }
3929
4a221615
GY
3930 goto done;
3931 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3932 if (!is_mask) {
3933 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
b02475c5 3934 }
fea393b1 3935 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
34118cae 3936 const struct ovs_key_ipv4 *ipv4_key;
36956a7d 3937
34118cae 3938 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3cea18ec
JR
3939 put_ipv4_key(ipv4_key, flow, is_mask);
3940 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3941 return ODP_FIT_ERROR;
3942 }
4a221615 3943 if (is_mask) {
4a221615
GY
3944 check_start = ipv4_key;
3945 check_len = sizeof *ipv4_key;
3946 expected_bit = OVS_KEY_ATTR_IPV4;
36956a7d 3947 }
34118cae 3948 }
4a221615
GY
3949 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3950 if (!is_mask) {
3951 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3952 }
fea393b1 3953 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
34118cae 3954 const struct ovs_key_ipv6 *ipv6_key;
36956a7d 3955
34118cae 3956 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3cea18ec
JR
3957 put_ipv6_key(ipv6_key, flow, is_mask);
3958 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3959 return ODP_FIT_ERROR;
3960 }
4a221615 3961 if (is_mask) {
4a221615
GY
3962 check_start = ipv6_key;
3963 check_len = sizeof *ipv6_key;
3964 expected_bit = OVS_KEY_ATTR_IPV6;
d31f1109 3965 }
34118cae 3966 }
4a221615
GY
3967 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3968 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3969 if (!is_mask) {
3970 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3971 }
fea393b1 3972 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
34118cae 3973 const struct ovs_key_arp *arp_key;
d31f1109 3974
34118cae 3975 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
4a221615 3976 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
34118cae
BP
3977 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3978 "key", ntohs(arp_key->arp_op));
b0f7b9b5 3979 return ODP_FIT_ERROR;
36956a7d 3980 }
3cea18ec 3981 put_arp_key(arp_key, flow);
4a221615
GY
3982 if (is_mask) {
3983 check_start = arp_key;
3984 check_len = sizeof *arp_key;
3985 expected_bit = OVS_KEY_ATTR_ARP;
3986 }
3987 }
3988 } else {
3989 goto done;
3990 }
df4eeb20 3991 if (check_len > 0) { /* Happens only when 'is_mask'. */
4a221615
GY
3992 if (!is_all_zeros(check_start, check_len) &&
3993 flow->dl_type != htons(0xffff)) {
3994 return ODP_FIT_ERROR;
3995 } else {
3996 expected_attrs |= UINT64_C(1) << expected_bit;
36956a7d 3997 }
36956a7d 3998 }
36956a7d 3999
4a221615
GY
4000 expected_bit = OVS_KEY_ATTR_UNSPEC;
4001 if (src_flow->nw_proto == IPPROTO_TCP
4002 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4003 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4004 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4005 if (!is_mask) {
4006 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4007 }
fea393b1 4008 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3cea18ec 4009 const union ovs_key_tp *tcp_key;
36956a7d 4010
34118cae 4011 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3cea18ec 4012 put_tp_key(tcp_key, flow);
4a221615
GY
4013 expected_bit = OVS_KEY_ATTR_TCP;
4014 }
dc235f7f
JR
4015 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4016 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4017 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4018 }
4a221615
GY
4019 } else if (src_flow->nw_proto == IPPROTO_UDP
4020 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4021 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4022 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4023 if (!is_mask) {
4024 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
7257b535 4025 }
fea393b1 4026 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3cea18ec 4027 const union ovs_key_tp *udp_key;
34118cae
BP
4028
4029 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3cea18ec 4030 put_tp_key(udp_key, flow);
4a221615
GY
4031 expected_bit = OVS_KEY_ATTR_UDP;
4032 }
12848ebf
GS
4033 } else if (src_flow->nw_proto == IPPROTO_SCTP
4034 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4035 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4036 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
c6bcb685
JS
4037 if (!is_mask) {
4038 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4039 }
4040 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3cea18ec 4041 const union ovs_key_tp *sctp_key;
c6bcb685
JS
4042
4043 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3cea18ec 4044 put_tp_key(sctp_key, flow);
c6bcb685
JS
4045 expected_bit = OVS_KEY_ATTR_SCTP;
4046 }
4a221615
GY
4047 } else if (src_flow->nw_proto == IPPROTO_ICMP
4048 && src_flow->dl_type == htons(ETH_TYPE_IP)
4049 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4050 if (!is_mask) {
4051 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
d31f1109 4052 }
fea393b1 4053 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
34118cae
BP
4054 const struct ovs_key_icmp *icmp_key;
4055
4056 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4057 flow->tp_src = htons(icmp_key->icmp_type);
4058 flow->tp_dst = htons(icmp_key->icmp_code);
4a221615
GY
4059 expected_bit = OVS_KEY_ATTR_ICMP;
4060 }
4061 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4062 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4063 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4064 if (!is_mask) {
4065 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
685a51a5 4066 }
fea393b1 4067 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
34118cae
BP
4068 const struct ovs_key_icmpv6 *icmpv6_key;
4069
4070 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4071 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4072 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4a221615 4073 expected_bit = OVS_KEY_ATTR_ICMPV6;
6f8dbd27
GY
4074 if (src_flow->tp_dst == htons(0) &&
4075 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4076 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4a221615
GY
4077 if (!is_mask) {
4078 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4079 }
fea393b1 4080 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
34118cae
BP
4081 const struct ovs_key_nd *nd_key;
4082
4083 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4084 memcpy(&flow->nd_target, nd_key->nd_target,
4085 sizeof flow->nd_target);
4086 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
4087 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
4a221615 4088 if (is_mask) {
53cb9c3e 4089 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
6f8dbd27
GY
4090 (flow->tp_src != htons(0xffff) ||
4091 flow->tp_dst != htons(0xffff))) {
4a221615
GY
4092 return ODP_FIT_ERROR;
4093 } else {
4094 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4095 }
4096 }
34118cae
BP
4097 }
4098 }
7257b535 4099 }
34118cae 4100 }
4a221615
GY
4101 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4102 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4103 return ODP_FIT_ERROR;
4104 } else {
4105 expected_attrs |= UINT64_C(1) << expected_bit;
4106 }
4107 }
7257b535 4108
4a221615 4109done:
b0f7b9b5
BP
4110 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4111 key, key_len);
4112}
4113
4114/* Parse 802.1Q header then encapsulated L3 attributes. */
4115static enum odp_key_fitness
4116parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4117 uint64_t present_attrs, int out_of_range_attr,
4118 uint64_t expected_attrs, struct flow *flow,
4a221615
GY
4119 const struct nlattr *key, size_t key_len,
4120 const struct flow *src_flow)
b0f7b9b5
BP
4121{
4122 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4a221615 4123 bool is_mask = src_flow != flow;
b0f7b9b5
BP
4124
4125 const struct nlattr *encap
4126 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4127 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4128 enum odp_key_fitness encap_fitness;
4129 enum odp_key_fitness fitness;
b0f7b9b5 4130
ec9f40dc 4131 /* Calculate fitness of outer attributes. */
4a221615
GY
4132 if (!is_mask) {
4133 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4134 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4135 } else {
4136 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4137 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4138 }
4139 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4140 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4141 }
4142 }
b0f7b9b5
BP
4143 fitness = check_expectations(present_attrs, out_of_range_attr,
4144 expected_attrs, key, key_len);
4145
3e634810
BP
4146 /* Set vlan_tci.
4147 * Remove the TPID from dl_type since it's not the real Ethertype. */
4148 flow->dl_type = htons(0);
4149 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4150 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4151 : htons(0));
4152 if (!is_mask) {
4153 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4154 return ODP_FIT_TOO_LITTLE;
4155 } else if (flow->vlan_tci == htons(0)) {
4156 /* Corner case for a truncated 802.1Q header. */
4157 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4158 return ODP_FIT_TOO_MUCH;
4159 }
4160 return fitness;
4161 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4162 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4163 "but CFI bit is not set", ntohs(flow->vlan_tci));
4164 return ODP_FIT_ERROR;
4165 }
4a221615 4166 } else {
3e634810
BP
4167 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4168 return fitness;
4a221615 4169 }
4a221615
GY
4170 }
4171
b0f7b9b5
BP
4172 /* Now parse the encapsulated attributes. */
4173 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4174 attrs, &present_attrs, &out_of_range_attr)) {
4175 return ODP_FIT_ERROR;
4176 }
4177 expected_attrs = 0;
4178
4a221615 4179 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
b0f7b9b5
BP
4180 return ODP_FIT_ERROR;
4181 }
b02475c5 4182 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4a221615
GY
4183 expected_attrs, flow, key, key_len,
4184 src_flow);
b0f7b9b5
BP
4185
4186 /* The overall fitness is the worse of the outer and inner attributes. */
4187 return MAX(fitness, encap_fitness);
4188}
4189
4a221615
GY
4190static enum odp_key_fitness
4191odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4192 struct flow *flow, const struct flow *src_flow)
b0f7b9b5 4193{
b0f7b9b5
BP
4194 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4195 uint64_t expected_attrs;
4196 uint64_t present_attrs;
4197 int out_of_range_attr;
4a221615 4198 bool is_mask = src_flow != flow;
b0f7b9b5
BP
4199
4200 memset(flow, 0, sizeof *flow);
4201
4202 /* Parse attributes. */
4203 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4204 &out_of_range_attr)) {
4205 return ODP_FIT_ERROR;
4206 }
4207 expected_attrs = 0;
4208
4209 /* Metadata. */
572f732a
AZ
4210 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4211 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4212 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4213 } else if (is_mask) {
8c1b077f 4214 /* Always exact match recirc_id if it is not specified. */
572f732a
AZ
4215 flow->recirc_id = UINT32_MAX;
4216 }
4217
4218 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4219 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4220 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4221 }
b0f7b9b5 4222 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
deedf7e7 4223 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
b0f7b9b5
BP
4224 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4225 }
4226
72e8bf28 4227 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
1362e248 4228 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
72e8bf28
AA
4229 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4230 }
4231
9b405f1a
PS
4232 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
4233 enum odp_key_fitness res;
05fb0928 4234
617e10e7 4235 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
9b405f1a
PS
4236 if (res == ODP_FIT_ERROR) {
4237 return ODP_FIT_ERROR;
4238 } else if (res == ODP_FIT_PERFECT) {
4239 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
05fb0928
JR
4240 }
4241 }
4242
b0f7b9b5 4243 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
4e022ec0
AW
4244 flow->in_port.odp_port
4245 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
b0f7b9b5 4246 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
4a221615 4247 } else if (!is_mask) {
4e022ec0 4248 flow->in_port.odp_port = ODPP_NONE;
b0f7b9b5
BP
4249 }
4250
4251 /* Ethernet header. */
4252 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
4253 const struct ovs_key_ethernet *eth_key;
4254
4255 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3cea18ec 4256 put_ethernet_key(eth_key, flow);
4a221615
GY
4257 if (is_mask) {
4258 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
4259 }
4260 }
4261 if (!is_mask) {
4262 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
b0f7b9b5 4263 }
b0f7b9b5
BP
4264
4265 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
4a221615
GY
4266 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
4267 src_flow)) {
b0f7b9b5
BP
4268 return ODP_FIT_ERROR;
4269 }
4270
21e70add
BP
4271 if (is_mask
4272 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
4273 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
b0f7b9b5 4274 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
4a221615
GY
4275 expected_attrs, flow, key, key_len, src_flow);
4276 }
4277 if (is_mask) {
4278 flow->vlan_tci = htons(0xffff);
4279 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4280 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
4281 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4282 }
b0f7b9b5 4283 }
b02475c5 4284 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4a221615
GY
4285 expected_attrs, flow, key, key_len, src_flow);
4286}
4287
4288/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
4289 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
4290 * 'key' fits our expectations for what a flow key should contain.
4291 *
4292 * The 'in_port' will be the datapath's understanding of the port. The
4293 * caller will need to translate with odp_port_to_ofp_port() if the
4294 * OpenFlow port is needed.
4295 *
4296 * This function doesn't take the packet itself as an argument because none of
4297 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
4298 * it is always possible to infer which additional attribute(s) should appear
4299 * by looking at the attributes for lower-level protocols, e.g. if the network
4300 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
4301 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
4302 * must be absent. */
4303enum odp_key_fitness
4304odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
4305 struct flow *flow)
4306{
4307 return odp_flow_key_to_flow__(key, key_len, flow, flow);
4308}
4309
4310/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
4311 * structure in 'mask'. 'flow' must be a previously translated flow
4312 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
4313 * 'key' fits our expectations for what a flow key should contain. */
4314enum odp_key_fitness
4315odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
4316 struct flow *mask, const struct flow *flow)
4317{
4318 return odp_flow_key_to_flow__(key, key_len, mask, flow);
14608a15 4319}
39db78a0 4320
6814e51f
BP
4321/* Returns 'fitness' as a string, for use in debug messages. */
4322const char *
4323odp_key_fitness_to_string(enum odp_key_fitness fitness)
4324{
4325 switch (fitness) {
4326 case ODP_FIT_PERFECT:
4327 return "OK";
4328 case ODP_FIT_TOO_MUCH:
4329 return "too_much";
4330 case ODP_FIT_TOO_LITTLE:
4331 return "too_little";
4332 case ODP_FIT_ERROR:
4333 return "error";
4334 default:
4335 return "<unknown>";
4336 }
4337}
4338
39db78a0 4339/* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
e995e3df
BP
4340 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
4341 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
4342 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
4343 * null, then the return value is not meaningful.) */
39db78a0 4344size_t
e995e3df
BP
4345odp_put_userspace_action(uint32_t pid,
4346 const void *userdata, size_t userdata_size,
8b7ea2d4 4347 odp_port_t tunnel_out_port,
39db78a0
BP
4348 struct ofpbuf *odp_actions)
4349{
e995e3df 4350 size_t userdata_ofs;
39db78a0
BP
4351 size_t offset;
4352
4353 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
4354 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
e995e3df 4355 if (userdata) {
6fd6ed71 4356 userdata_ofs = odp_actions->size + NLA_HDRLEN;
96ed775f
BP
4357
4358 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
4359 * module before Linux 3.10 required the userdata to be exactly 8 bytes
4360 * long:
4361 *
4362 * - The kernel rejected shorter userdata with -ERANGE.
4363 *
4364 * - The kernel silently dropped userdata beyond the first 8 bytes.
4365 *
4366 * Thus, for maximum compatibility, always put at least 8 bytes. (We
4367 * separately disable features that required more than 8 bytes.) */
4368 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
4369 MAX(8, userdata_size)),
4370 userdata, userdata_size);
e995e3df
BP
4371 } else {
4372 userdata_ofs = 0;
39db78a0 4373 }
8b7ea2d4
WZ
4374 if (tunnel_out_port != ODPP_NONE) {
4375 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
4376 tunnel_out_port);
4377 }
39db78a0
BP
4378 nl_msg_end_nested(odp_actions, offset);
4379
e995e3df 4380 return userdata_ofs;
39db78a0 4381}
b9ad7294
EJ
4382
4383void
4384odp_put_tunnel_action(const struct flow_tnl *tunnel,
4385 struct ofpbuf *odp_actions)
4386{
4387 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4388 tun_key_to_attr(odp_actions, tunnel);
4389 nl_msg_end_nested(odp_actions, offset);
4390}
a36de779
PS
4391
4392void
4393odp_put_tnl_push_action(struct ofpbuf *odp_actions,
4394 struct ovs_action_push_tnl *data)
4395{
4396 int size = offsetof(struct ovs_action_push_tnl, header);
4397
4398 size += data->header_len;
4399 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
4400}
4401
5bbda0aa
EJ
4402\f
4403/* The commit_odp_actions() function and its helpers. */
4404
4405static void
4406commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
4407 const void *key, size_t key_size)
4408{
4409 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
4410 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
4411 nl_msg_end_nested(odp_actions, offset);
4412}
4413
6d670e7f
JR
4414/* Masked set actions have a mask following the data within the netlink
4415 * attribute. The unmasked bits in the data will be cleared as the data
4416 * is copied to the action. */
4417void
4418commit_masked_set_action(struct ofpbuf *odp_actions,
4419 enum ovs_key_attr key_type,
4420 const void *key_, const void *mask_, size_t key_size)
4421{
4422 size_t offset = nl_msg_start_nested(odp_actions,
4423 OVS_ACTION_ATTR_SET_MASKED);
4424 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
4425 const char *key = key_, *mask = mask_;
4426
4427 memcpy(data + key_size, mask, key_size);
4428 /* Clear unmasked bits while copying. */
4429 while (key_size--) {
4430 *data++ = *key++ & *mask++;
4431 }
4432 nl_msg_end_nested(odp_actions, offset);
4433}
4434
b9ad7294
EJ
4435/* If any of the flow key data that ODP actions can modify are different in
4436 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
4437 * 'odp_actions' that change the flow tunneling information in key from
4438 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
4439 * same way. In other words, operates the same as commit_odp_actions(), but
4440 * only on tunneling information. */
4441void
4442commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
5bbda0aa
EJ
4443 struct ofpbuf *odp_actions)
4444{
05fb0928
JR
4445 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
4446 if (flow->tunnel.ip_dst) {
fc80de30
JR
4447 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4448 return;
4449 }
4450 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
b9ad7294 4451 odp_put_tunnel_action(&base->tunnel, odp_actions);
05fb0928 4452 }
5bbda0aa
EJ
4453}
4454
d23df9a8
JR
4455static bool
4456commit(enum ovs_key_attr attr, bool use_masked_set,
4457 const void *key, void *base, void *mask, size_t size,
4458 struct ofpbuf *odp_actions)
5bbda0aa 4459{
d23df9a8
JR
4460 if (memcmp(key, base, size)) {
4461 bool fully_masked = odp_mask_is_exact(attr, mask, size);
5bbda0aa 4462
d23df9a8
JR
4463 if (use_masked_set && !fully_masked) {
4464 commit_masked_set_action(odp_actions, attr, key, mask, size);
4465 } else {
4466 if (!fully_masked) {
4467 memset(mask, 0xff, size);
4468 }
4469 commit_set_action(odp_actions, attr, key, size);
4470 }
4471 memcpy(base, key, size);
4472 return true;
4473 } else {
4474 /* Mask bits are set when we have either read or set the corresponding
4475 * values. Masked bits will be exact-matched, no need to set them
4476 * if the value did not actually change. */
4477 return false;
5bbda0aa 4478 }
d23df9a8 4479}
5bbda0aa 4480
d23df9a8
JR
4481static void
4482get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4483{
4484 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4485 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4486}
1dd35f8a 4487
d23df9a8
JR
4488static void
4489put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4490{
4491 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4492 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4493}
5bbda0aa 4494
d23df9a8
JR
4495static void
4496commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4497 struct ofpbuf *odp_actions,
4498 struct flow_wildcards *wc,
4499 bool use_masked)
4500{
4501 struct ovs_key_ethernet key, base, mask;
5bbda0aa 4502
d23df9a8
JR
4503 get_ethernet_key(flow, &key);
4504 get_ethernet_key(base_flow, &base);
4505 get_ethernet_key(&wc->masks, &mask);
4506
4507 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4508 &key, &base, &mask, sizeof key, odp_actions)) {
4509 put_ethernet_key(&base, base_flow);
4510 put_ethernet_key(&mask, &wc->masks);
4511 }
5bbda0aa
EJ
4512}
4513
4514static void
8bfd0fda
BP
4515pop_vlan(struct flow *base,
4516 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5bbda0aa 4517{
1dd35f8a
JP
4518 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4519
5bbda0aa
EJ
4520 if (base->vlan_tci & htons(VLAN_CFI)) {
4521 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
8bfd0fda
BP
4522 base->vlan_tci = 0;
4523 }
4524}
4525
4526static void
4527commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4528 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4529{
4530 if (base->vlan_tci == vlan_tci) {
4531 return;
5bbda0aa
EJ
4532 }
4533
8bfd0fda 4534 pop_vlan(base, odp_actions, wc);
8fd16af2 4535 if (vlan_tci & htons(VLAN_CFI)) {
5bbda0aa
EJ
4536 struct ovs_action_push_vlan vlan;
4537
4538 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
8fd16af2 4539 vlan.vlan_tci = vlan_tci;
5bbda0aa
EJ
4540 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4541 &vlan, sizeof vlan);
4542 }
8fd16af2 4543 base->vlan_tci = vlan_tci;
5bbda0aa
EJ
4544}
4545
22d38fca 4546/* Wildcarding already done at action translation time. */
b02475c5
SH
4547static void
4548commit_mpls_action(const struct flow *flow, struct flow *base,
22d38fca 4549 struct ofpbuf *odp_actions)
b02475c5 4550{
22d38fca
JR
4551 int base_n = flow_count_mpls_labels(base, NULL);
4552 int flow_n = flow_count_mpls_labels(flow, NULL);
8bfd0fda 4553 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
22d38fca 4554 NULL);
8bfd0fda
BP
4555
4556 while (base_n > common_n) {
4557 if (base_n - 1 == common_n && flow_n > common_n) {
4558 /* If there is only one more LSE in base than there are common
4559 * between base and flow; and flow has at least one more LSE than
4560 * is common then the topmost LSE of base may be updated using
4561 * set */
4562 struct ovs_key_mpls mpls_key;
4563
4564 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4565 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4566 &mpls_key, sizeof mpls_key);
4567 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4568 common_n++;
4569 } else {
4570 /* Otherwise, if there more LSEs in base than are common between
4571 * base and flow then pop the topmost one. */
4572 ovs_be16 dl_type;
4573 bool popped;
4574
4575 /* If all the LSEs are to be popped and this is not the outermost
4576 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4577 * POP_MPLS action instead of flow->dl_type.
4578 *
4579 * This is because the POP_MPLS action requires its ethertype
4580 * argument to be an MPLS ethernet type but in this case
4581 * flow->dl_type will be a non-MPLS ethernet type.
4582 *
4583 * When the final POP_MPLS action occurs it use flow->dl_type and
4584 * the and the resulting packet will have the desired dl_type. */
4585 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4586 dl_type = htons(ETH_TYPE_MPLS);
4587 } else {
4588 dl_type = flow->dl_type;
4589 }
4590 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
22d38fca 4591 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
8bfd0fda
BP
4592 ovs_assert(popped);
4593 base_n--;
4594 }
b02475c5
SH
4595 }
4596
8bfd0fda
BP
4597 /* If, after the above popping and setting, there are more LSEs in flow
4598 * than base then some LSEs need to be pushed. */
4599 while (base_n < flow_n) {
b02475c5
SH
4600 struct ovs_action_push_mpls *mpls;
4601
8bfd0fda
BP
4602 mpls = nl_msg_put_unspec_zero(odp_actions,
4603 OVS_ACTION_ATTR_PUSH_MPLS,
9ddf12cc 4604 sizeof *mpls);
b02475c5 4605 mpls->mpls_ethertype = flow->dl_type;
8bfd0fda 4606 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
22d38fca 4607 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
8bfd0fda
BP
4608 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4609 base_n++;
b0a17866 4610 }
b02475c5
SH
4611}
4612
5bbda0aa 4613static void
3cea18ec 4614get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
5bbda0aa 4615{
d23df9a8
JR
4616 ipv4->ipv4_src = flow->nw_src;
4617 ipv4->ipv4_dst = flow->nw_dst;
4618 ipv4->ipv4_proto = flow->nw_proto;
4619 ipv4->ipv4_tos = flow->nw_tos;
4620 ipv4->ipv4_ttl = flow->nw_ttl;
3cea18ec 4621 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
d23df9a8 4622}
5bbda0aa 4623
d23df9a8 4624static void
3cea18ec 4625put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
d23df9a8
JR
4626{
4627 flow->nw_src = ipv4->ipv4_src;
4628 flow->nw_dst = ipv4->ipv4_dst;
4629 flow->nw_proto = ipv4->ipv4_proto;
4630 flow->nw_tos = ipv4->ipv4_tos;
4631 flow->nw_ttl = ipv4->ipv4_ttl;
3cea18ec 4632 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
d23df9a8
JR
4633}
4634
4635static void
4636commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4637 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4638 bool use_masked)
4639{
4640 struct ovs_key_ipv4 key, mask, base;
5bbda0aa 4641
d23df9a8
JR
4642 /* Check that nw_proto and nw_frag remain unchanged. */
4643 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4644 flow->nw_frag == base_flow->nw_frag);
1dd35f8a 4645
3cea18ec
JR
4646 get_ipv4_key(flow, &key, false);
4647 get_ipv4_key(base_flow, &base, false);
4648 get_ipv4_key(&wc->masks, &mask, true);
d23df9a8
JR
4649 mask.ipv4_proto = 0; /* Not writeable. */
4650 mask.ipv4_frag = 0; /* Not writable. */
5bbda0aa 4651
d23df9a8
JR
4652 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4653 odp_actions)) {
3cea18ec 4654 put_ipv4_key(&base, base_flow, false);
d23df9a8 4655 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
3cea18ec 4656 put_ipv4_key(&mask, &wc->masks, true);
d23df9a8
JR
4657 }
4658 }
5bbda0aa
EJ
4659}
4660
c4f2731d 4661static void
3cea18ec 4662get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
c4f2731d 4663{
d23df9a8
JR
4664 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4665 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4666 ipv6->ipv6_label = flow->ipv6_label;
4667 ipv6->ipv6_proto = flow->nw_proto;
4668 ipv6->ipv6_tclass = flow->nw_tos;
4669 ipv6->ipv6_hlimit = flow->nw_ttl;
3cea18ec 4670 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
d23df9a8 4671}
c4f2731d 4672
d23df9a8 4673static void
3cea18ec 4674put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
d23df9a8
JR
4675{
4676 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4677 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4678 flow->ipv6_label = ipv6->ipv6_label;
4679 flow->nw_proto = ipv6->ipv6_proto;
4680 flow->nw_tos = ipv6->ipv6_tclass;
4681 flow->nw_ttl = ipv6->ipv6_hlimit;
3cea18ec 4682 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
d23df9a8 4683}
c4f2731d 4684
d23df9a8
JR
4685static void
4686commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4687 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4688 bool use_masked)
4689{
4690 struct ovs_key_ipv6 key, mask, base;
1dd35f8a 4691
d23df9a8
JR
4692 /* Check that nw_proto and nw_frag remain unchanged. */
4693 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4694 flow->nw_frag == base_flow->nw_frag);
c4f2731d 4695
3cea18ec
JR
4696 get_ipv6_key(flow, &key, false);
4697 get_ipv6_key(base_flow, &base, false);
4698 get_ipv6_key(&wc->masks, &mask, true);
d23df9a8
JR
4699 mask.ipv6_proto = 0; /* Not writeable. */
4700 mask.ipv6_frag = 0; /* Not writable. */
c4f2731d 4701
d23df9a8
JR
4702 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4703 odp_actions)) {
3cea18ec 4704 put_ipv6_key(&base, base_flow, false);
d23df9a8 4705 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
3cea18ec 4706 put_ipv6_key(&mask, &wc->masks, true);
d23df9a8
JR
4707 }
4708 }
c4f2731d
PS
4709}
4710
d23df9a8
JR
4711static void
4712get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
f6c8a6b1 4713{
d23df9a8
JR
4714 /* ARP key has padding, clear it. */
4715 memset(arp, 0, sizeof *arp);
f6c8a6b1 4716
d23df9a8
JR
4717 arp->arp_sip = flow->nw_src;
4718 arp->arp_tip = flow->nw_dst;
4719 arp->arp_op = htons(flow->nw_proto);
4720 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4721 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4722}
f6c8a6b1 4723
d23df9a8
JR
4724static void
4725put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4726{
4727 flow->nw_src = arp->arp_sip;
4728 flow->nw_dst = arp->arp_tip;
4729 flow->nw_proto = ntohs(arp->arp_op);
4730 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4731 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4732}
f6c8a6b1 4733
d23df9a8
JR
4734static enum slow_path_reason
4735commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4736 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4737{
4738 struct ovs_key_arp key, mask, base;
f6c8a6b1 4739
d23df9a8
JR
4740 get_arp_key(flow, &key);
4741 get_arp_key(base_flow, &base);
4742 get_arp_key(&wc->masks, &mask);
f6c8a6b1 4743
d23df9a8
JR
4744 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4745 odp_actions)) {
4746 put_arp_key(&base, base_flow);
4747 put_arp_key(&mask, &wc->masks);
4748 return SLOW_ACTION;
4749 }
4750 return 0;
f6c8a6b1
BP
4751}
4752
e60e935b
SRCSA
4753static void
4754get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4755{
4756 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4757 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4758 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4759 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4760}
4761
4762static void
4763put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4764{
4765 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4766 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4767 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4768 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4769}
4770
4771static enum slow_path_reason
4772commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4773 struct ofpbuf *odp_actions,
4774 struct flow_wildcards *wc, bool use_masked)
4775{
4776 struct ovs_key_nd key, mask, base;
4777
4778 get_nd_key(flow, &key);
4779 get_nd_key(base_flow, &base);
4780 get_nd_key(&wc->masks, &mask);
4781
4782 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4783 odp_actions)) {
4784 put_nd_key(&base, base_flow);
4785 put_nd_key(&mask, &wc->masks);
4786 return SLOW_ACTION;
4787 }
4788
4789 return 0;
4790}
4791
f6c8a6b1 4792static enum slow_path_reason
c4f2731d 4793commit_set_nw_action(const struct flow *flow, struct flow *base,
d23df9a8
JR
4794 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4795 bool use_masked)
c4f2731d 4796{
f6c8a6b1 4797 /* Check if 'flow' really has an L3 header. */
c4f2731d 4798 if (!flow->nw_proto) {
f6c8a6b1 4799 return 0;
c4f2731d
PS
4800 }
4801
f6c8a6b1
BP
4802 switch (ntohs(base->dl_type)) {
4803 case ETH_TYPE_IP:
d23df9a8 4804 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
f6c8a6b1
BP
4805 break;
4806
4807 case ETH_TYPE_IPV6:
d23df9a8 4808 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
e60e935b 4809 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
f6c8a6b1
BP
4810
4811 case ETH_TYPE_ARP:
4812 return commit_set_arp_action(flow, base, odp_actions, wc);
c4f2731d 4813 }
f6c8a6b1
BP
4814
4815 return 0;
c4f2731d
PS
4816}
4817
d23df9a8
JR
4818/* TCP, UDP, and SCTP keys have the same layout. */
4819BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4820 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4821
5bbda0aa 4822static void
3cea18ec 4823get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
5bbda0aa 4824{
3cea18ec
JR
4825 tp->tcp.tcp_src = flow->tp_src;
4826 tp->tcp.tcp_dst = flow->tp_dst;
d23df9a8
JR
4827}
4828
4829static void
3cea18ec 4830put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
d23df9a8 4831{
3cea18ec
JR
4832 flow->tp_src = tp->tcp.tcp_src;
4833 flow->tp_dst = tp->tcp.tcp_dst;
d23df9a8
JR
4834}
4835
4836static void
4837commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4838 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4839 bool use_masked)
4840{
4841 enum ovs_key_attr key_type;
3cea18ec 4842 union ovs_key_tp key, mask, base;
d23df9a8 4843
791a09be
SH
4844 /* Check if 'flow' really has an L3 header. */
4845 if (!flow->nw_proto) {
4846 return;
4847 }
4848
d23df9a8 4849 if (!is_ip_any(base_flow)) {
5bbda0aa
EJ
4850 return;
4851 }
4852
5bbda0aa 4853 if (flow->nw_proto == IPPROTO_TCP) {
d23df9a8 4854 key_type = OVS_KEY_ATTR_TCP;
5bbda0aa 4855 } else if (flow->nw_proto == IPPROTO_UDP) {
d23df9a8 4856 key_type = OVS_KEY_ATTR_UDP;
c6bcb685 4857 } else if (flow->nw_proto == IPPROTO_SCTP) {
d23df9a8
JR
4858 key_type = OVS_KEY_ATTR_SCTP;
4859 } else {
4860 return;
4861 }
c6bcb685 4862
d23df9a8
JR
4863 get_tp_key(flow, &key);
4864 get_tp_key(base_flow, &base);
4865 get_tp_key(&wc->masks, &mask);
c6bcb685 4866
d23df9a8
JR
4867 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4868 odp_actions)) {
4869 put_tp_key(&base, base_flow);
4870 put_tp_key(&mask, &wc->masks);
5bbda0aa
EJ
4871 }
4872}
4873
4874static void
d23df9a8 4875commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
1dd35f8a 4876 struct ofpbuf *odp_actions,
d23df9a8
JR
4877 struct flow_wildcards *wc,
4878 bool use_masked)
5bbda0aa 4879{
d23df9a8 4880 uint32_t key, mask, base;
1dd35f8a 4881
d23df9a8
JR
4882 key = flow->skb_priority;
4883 base = base_flow->skb_priority;
4884 mask = wc->masks.skb_priority;
5bbda0aa 4885
d23df9a8
JR
4886 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4887 sizeof key, odp_actions)) {
4888 base_flow->skb_priority = base;
4889 wc->masks.skb_priority = mask;
4890 }
5bbda0aa
EJ
4891}
4892
72e8bf28 4893static void
d23df9a8 4894commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
1dd35f8a 4895 struct ofpbuf *odp_actions,
d23df9a8
JR
4896 struct flow_wildcards *wc,
4897 bool use_masked)
72e8bf28 4898{
d23df9a8 4899 uint32_t key, mask, base;
1dd35f8a 4900
d23df9a8
JR
4901 key = flow->pkt_mark;
4902 base = base_flow->pkt_mark;
4903 mask = wc->masks.pkt_mark;
72e8bf28 4904
d23df9a8
JR
4905 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4906 sizeof key, odp_actions)) {
4907 base_flow->pkt_mark = base;
4908 wc->masks.pkt_mark = mask;
4909 }
72e8bf28 4910}
7fd91025 4911
5bbda0aa
EJ
4912/* If any of the flow key data that ODP actions can modify are different in
4913 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
b9ad7294
EJ
4914 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4915 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
1dd35f8a 4916 * in addition to this function if needed. Sets fields in 'wc' that are
7fd91025
BP
4917 * used as part of the action.
4918 *
4919 * Returns a reason to force processing the flow's packets into the userspace
4920 * slow path, if there is one, otherwise 0. */
4921enum slow_path_reason
5bbda0aa 4922commit_odp_actions(const struct flow *flow, struct flow *base,
d23df9a8
JR
4923 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4924 bool use_masked)
5bbda0aa 4925{
f6c8a6b1
BP
4926 enum slow_path_reason slow;
4927
d23df9a8
JR
4928 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4929 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4930 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
22d38fca 4931 commit_mpls_action(flow, base, odp_actions);
8bfd0fda 4932 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
d23df9a8
JR
4933 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4934 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
7fd91025 4935
f6c8a6b1 4936 return slow;
5bbda0aa 4937}