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