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