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