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