]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
odp: Use struct in6_addr for IPv6 addresses.
[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(&ipv6_mask->ipv6_src)
2063 && ipv6_mask_is_exact(&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_label(struct ds *ds, const char *name, ovs_be32 key,
2214 const ovs_be32 *mask, bool verbose)
2215 {
2216 bool mask_empty = mask && !*mask;
2217
2218 if (verbose || !mask_empty) {
2219 bool mask_full = !mask
2220 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
2221
2222 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
2223 if (!mask_full) { /* Partially masked. */
2224 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
2225 }
2226 ds_put_char(ds, ',');
2227 }
2228 }
2229
2230 static void
2231 format_u8x(struct ds *ds, const char *name, uint8_t key,
2232 const uint8_t *mask, bool verbose)
2233 {
2234 bool mask_empty = mask && !*mask;
2235
2236 if (verbose || !mask_empty) {
2237 bool mask_full = !mask || *mask == UINT8_MAX;
2238
2239 ds_put_format(ds, "%s=%#"PRIx8, name, key);
2240 if (!mask_full) { /* Partially masked. */
2241 ds_put_format(ds, "/%#"PRIx8, *mask);
2242 }
2243 ds_put_char(ds, ',');
2244 }
2245 }
2246
2247 static void
2248 format_u8u(struct ds *ds, const char *name, uint8_t key,
2249 const uint8_t *mask, bool verbose)
2250 {
2251 bool mask_empty = mask && !*mask;
2252
2253 if (verbose || !mask_empty) {
2254 bool mask_full = !mask || *mask == UINT8_MAX;
2255
2256 ds_put_format(ds, "%s=%"PRIu8, name, key);
2257 if (!mask_full) { /* Partially masked. */
2258 ds_put_format(ds, "/%#"PRIx8, *mask);
2259 }
2260 ds_put_char(ds, ',');
2261 }
2262 }
2263
2264 static void
2265 format_be16(struct ds *ds, const char *name, ovs_be16 key,
2266 const ovs_be16 *mask, bool verbose)
2267 {
2268 bool mask_empty = mask && !*mask;
2269
2270 if (verbose || !mask_empty) {
2271 bool mask_full = !mask || *mask == OVS_BE16_MAX;
2272
2273 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
2274 if (!mask_full) { /* Partially masked. */
2275 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2276 }
2277 ds_put_char(ds, ',');
2278 }
2279 }
2280
2281 static void
2282 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
2283 const ovs_be16 *mask, bool verbose)
2284 {
2285 bool mask_empty = mask && !*mask;
2286
2287 if (verbose || !mask_empty) {
2288 bool mask_full = !mask || *mask == OVS_BE16_MAX;
2289
2290 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
2291 if (!mask_full) { /* Partially masked. */
2292 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2293 }
2294 ds_put_char(ds, ',');
2295 }
2296 }
2297
2298 static void
2299 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
2300 const uint16_t *mask, bool verbose)
2301 {
2302 bool mask_empty = mask && !*mask;
2303
2304 if (verbose || !mask_empty) {
2305 ds_put_cstr(ds, name);
2306 ds_put_char(ds, '(');
2307 if (mask) {
2308 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
2309 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
2310 } else { /* Fully masked. */
2311 format_flags(ds, flow_tun_flag_to_string, key, '|');
2312 }
2313 ds_put_cstr(ds, "),");
2314 }
2315 }
2316
2317 static bool
2318 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
2319 const struct attr_len_tbl tbl[], int max_len, bool need_key)
2320 {
2321 int expected_len;
2322
2323 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
2324 if (expected_len != ATTR_LEN_VARIABLE &&
2325 expected_len != ATTR_LEN_NESTED) {
2326
2327 bool bad_key_len = nl_attr_get_size(a) != expected_len;
2328 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
2329
2330 if (bad_key_len || bad_mask_len) {
2331 if (need_key) {
2332 ds_put_format(ds, "key%u", nl_attr_type(a));
2333 }
2334 if (bad_key_len) {
2335 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
2336 nl_attr_get_size(a), expected_len);
2337 }
2338 format_generic_odp_key(a, ds);
2339 if (ma) {
2340 ds_put_char(ds, '/');
2341 if (bad_mask_len) {
2342 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
2343 nl_attr_get_size(ma), expected_len);
2344 }
2345 format_generic_odp_key(ma, ds);
2346 }
2347 ds_put_char(ds, ')');
2348 return false;
2349 }
2350 }
2351
2352 return true;
2353 }
2354
2355 static void
2356 format_unknown_key(struct ds *ds, const struct nlattr *a,
2357 const struct nlattr *ma)
2358 {
2359 ds_put_format(ds, "key%u(", nl_attr_type(a));
2360 format_generic_odp_key(a, ds);
2361 if (ma && !odp_mask_attr_is_exact(ma)) {
2362 ds_put_char(ds, '/');
2363 format_generic_odp_key(ma, ds);
2364 }
2365 ds_put_cstr(ds, "),");
2366 }
2367
2368 static void
2369 format_odp_tun_vxlan_opt(const struct nlattr *attr,
2370 const struct nlattr *mask_attr, struct ds *ds,
2371 bool verbose)
2372 {
2373 unsigned int left;
2374 const struct nlattr *a;
2375 struct ofpbuf ofp;
2376
2377 ofpbuf_init(&ofp, 100);
2378 NL_NESTED_FOR_EACH(a, left, attr) {
2379 uint16_t type = nl_attr_type(a);
2380 const struct nlattr *ma = NULL;
2381
2382 if (mask_attr) {
2383 ma = nl_attr_find__(nl_attr_get(mask_attr),
2384 nl_attr_get_size(mask_attr), type);
2385 if (!ma) {
2386 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
2387 OVS_VXLAN_EXT_MAX,
2388 &ofp, a);
2389 }
2390 }
2391
2392 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
2393 OVS_VXLAN_EXT_MAX, true)) {
2394 continue;
2395 }
2396
2397 switch (type) {
2398 case OVS_VXLAN_EXT_GBP: {
2399 uint32_t key = nl_attr_get_u32(a);
2400 ovs_be16 id, id_mask;
2401 uint8_t flags, flags_mask = 0;
2402
2403 id = htons(key & 0xFFFF);
2404 flags = (key >> 16) & 0xFF;
2405 if (ma) {
2406 uint32_t mask = nl_attr_get_u32(ma);
2407 id_mask = htons(mask & 0xFFFF);
2408 flags_mask = (mask >> 16) & 0xFF;
2409 }
2410
2411 ds_put_cstr(ds, "gbp(");
2412 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
2413 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
2414 ds_chomp(ds, ',');
2415 ds_put_cstr(ds, "),");
2416 break;
2417 }
2418
2419 default:
2420 format_unknown_key(ds, a, ma);
2421 }
2422 ofpbuf_clear(&ofp);
2423 }
2424
2425 ds_chomp(ds, ',');
2426 ofpbuf_uninit(&ofp);
2427 }
2428
2429 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2430
2431 static void
2432 format_geneve_opts(const struct geneve_opt *opt,
2433 const struct geneve_opt *mask, int opts_len,
2434 struct ds *ds, bool verbose)
2435 {
2436 while (opts_len > 0) {
2437 unsigned int len;
2438 uint8_t data_len, data_len_mask;
2439
2440 if (opts_len < sizeof *opt) {
2441 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
2442 opts_len, sizeof *opt);
2443 return;
2444 }
2445
2446 data_len = opt->length * 4;
2447 if (mask) {
2448 if (mask->length == 0x1f) {
2449 data_len_mask = UINT8_MAX;
2450 } else {
2451 data_len_mask = mask->length;
2452 }
2453 }
2454 len = sizeof *opt + data_len;
2455 if (len > opts_len) {
2456 ds_put_format(ds, "opt len %u greater than remaining %u",
2457 len, opts_len);
2458 return;
2459 }
2460
2461 ds_put_char(ds, '{');
2462 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
2463 verbose);
2464 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
2465 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
2466 if (data_len &&
2467 (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
2468 ds_put_hex(ds, opt + 1, data_len);
2469 if (mask && !is_all_ones(mask + 1, data_len)) {
2470 ds_put_char(ds, '/');
2471 ds_put_hex(ds, mask + 1, data_len);
2472 }
2473 } else {
2474 ds_chomp(ds, ',');
2475 }
2476 ds_put_char(ds, '}');
2477
2478 opt += len / sizeof(*opt);
2479 if (mask) {
2480 mask += len / sizeof(*opt);
2481 }
2482 opts_len -= len;
2483 };
2484 }
2485
2486 static void
2487 format_odp_tun_geneve(const struct nlattr *attr,
2488 const struct nlattr *mask_attr, struct ds *ds,
2489 bool verbose)
2490 {
2491 int opts_len = nl_attr_get_size(attr);
2492 const struct geneve_opt *opt = nl_attr_get(attr);
2493 const struct geneve_opt *mask = mask_attr ?
2494 nl_attr_get(mask_attr) : NULL;
2495
2496 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
2497 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
2498 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
2499 return;
2500 }
2501
2502 format_geneve_opts(opt, mask, opts_len, ds, verbose);
2503 }
2504
2505 static void
2506 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
2507 struct ds *ds, bool verbose)
2508 {
2509 unsigned int left;
2510 const struct nlattr *a;
2511 uint16_t flags = 0;
2512 uint16_t mask_flags = 0;
2513 struct ofpbuf ofp;
2514
2515 ofpbuf_init(&ofp, 100);
2516 NL_NESTED_FOR_EACH(a, left, attr) {
2517 enum ovs_tunnel_key_attr type = nl_attr_type(a);
2518 const struct nlattr *ma = NULL;
2519
2520 if (mask_attr) {
2521 ma = nl_attr_find__(nl_attr_get(mask_attr),
2522 nl_attr_get_size(mask_attr), type);
2523 if (!ma) {
2524 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
2525 OVS_TUNNEL_KEY_ATTR_MAX,
2526 &ofp, a);
2527 }
2528 }
2529
2530 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
2531 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2532 continue;
2533 }
2534
2535 switch (type) {
2536 case OVS_TUNNEL_KEY_ATTR_ID:
2537 format_be64(ds, "tun_id", nl_attr_get_be64(a),
2538 ma ? nl_attr_get(ma) : NULL, verbose);
2539 flags |= FLOW_TNL_F_KEY;
2540 if (ma) {
2541 mask_flags |= FLOW_TNL_F_KEY;
2542 }
2543 break;
2544 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2545 format_ipv4(ds, "src", nl_attr_get_be32(a),
2546 ma ? nl_attr_get(ma) : NULL, verbose);
2547 break;
2548 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2549 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2550 ma ? nl_attr_get(ma) : NULL, verbose);
2551 break;
2552 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
2553 struct in6_addr ipv6_src;
2554 ipv6_src = nl_attr_get_in6_addr(a);
2555 format_in6_addr(ds, "ipv6_src", &ipv6_src,
2556 ma ? nl_attr_get(ma) : NULL, verbose);
2557 break;
2558 }
2559 case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
2560 struct in6_addr ipv6_dst;
2561 ipv6_dst = nl_attr_get_in6_addr(a);
2562 format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
2563 ma ? nl_attr_get(ma) : NULL, verbose);
2564 break;
2565 }
2566 case OVS_TUNNEL_KEY_ATTR_TOS:
2567 format_u8x(ds, "tos", nl_attr_get_u8(a),
2568 ma ? nl_attr_get(ma) : NULL, verbose);
2569 break;
2570 case OVS_TUNNEL_KEY_ATTR_TTL:
2571 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2572 ma ? nl_attr_get(ma) : NULL, verbose);
2573 break;
2574 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2575 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2576 break;
2577 case OVS_TUNNEL_KEY_ATTR_CSUM:
2578 flags |= FLOW_TNL_F_CSUM;
2579 break;
2580 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2581 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2582 ma ? nl_attr_get(ma) : NULL, verbose);
2583 break;
2584 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2585 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2586 ma ? nl_attr_get(ma) : NULL, verbose);
2587 break;
2588 case OVS_TUNNEL_KEY_ATTR_OAM:
2589 flags |= FLOW_TNL_F_OAM;
2590 break;
2591 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2592 ds_put_cstr(ds, "vxlan(");
2593 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2594 ds_put_cstr(ds, "),");
2595 break;
2596 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2597 ds_put_cstr(ds, "geneve(");
2598 format_odp_tun_geneve(a, ma, ds, verbose);
2599 ds_put_cstr(ds, "),");
2600 break;
2601 case OVS_TUNNEL_KEY_ATTR_PAD:
2602 break;
2603 case __OVS_TUNNEL_KEY_ATTR_MAX:
2604 default:
2605 format_unknown_key(ds, a, ma);
2606 }
2607 ofpbuf_clear(&ofp);
2608 }
2609
2610 /* Flags can have a valid mask even if the attribute is not set, so
2611 * we need to collect these separately. */
2612 if (mask_attr) {
2613 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2614 switch (nl_attr_type(a)) {
2615 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2616 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2617 break;
2618 case OVS_TUNNEL_KEY_ATTR_CSUM:
2619 mask_flags |= FLOW_TNL_F_CSUM;
2620 break;
2621 case OVS_TUNNEL_KEY_ATTR_OAM:
2622 mask_flags |= FLOW_TNL_F_OAM;
2623 break;
2624 }
2625 }
2626 }
2627
2628 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2629 verbose);
2630 ds_chomp(ds, ',');
2631 ofpbuf_uninit(&ofp);
2632 }
2633
2634 static const char *
2635 odp_ct_state_to_string(uint32_t flag)
2636 {
2637 switch (flag) {
2638 case OVS_CS_F_REPLY_DIR:
2639 return "rpl";
2640 case OVS_CS_F_TRACKED:
2641 return "trk";
2642 case OVS_CS_F_NEW:
2643 return "new";
2644 case OVS_CS_F_ESTABLISHED:
2645 return "est";
2646 case OVS_CS_F_RELATED:
2647 return "rel";
2648 case OVS_CS_F_INVALID:
2649 return "inv";
2650 case OVS_CS_F_SRC_NAT:
2651 return "snat";
2652 case OVS_CS_F_DST_NAT:
2653 return "dnat";
2654 default:
2655 return NULL;
2656 }
2657 }
2658
2659 static void
2660 format_frag(struct ds *ds, const char *name, uint8_t key,
2661 const uint8_t *mask, bool verbose)
2662 {
2663 bool mask_empty = mask && !*mask;
2664
2665 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2666 if (verbose || !mask_empty) {
2667 bool mask_full = !mask || *mask == UINT8_MAX;
2668
2669 if (!mask_full) { /* Partially masked. */
2670 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2671 PRIx8"),", *mask);
2672 } else {
2673 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2674 }
2675 }
2676 }
2677
2678 static bool
2679 mask_empty(const struct nlattr *ma)
2680 {
2681 const void *mask;
2682 size_t n;
2683
2684 if (!ma) {
2685 return true;
2686 }
2687 mask = nl_attr_get(ma);
2688 n = nl_attr_get_size(ma);
2689
2690 return is_all_zeros(mask, n);
2691 }
2692
2693 static void
2694 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2695 const struct hmap *portno_names, struct ds *ds,
2696 bool verbose)
2697 {
2698 enum ovs_key_attr attr = nl_attr_type(a);
2699 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2700 bool is_exact;
2701
2702 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2703
2704 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2705
2706 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2707 OVS_KEY_ATTR_MAX, false)) {
2708 return;
2709 }
2710
2711 ds_put_char(ds, '(');
2712 switch (attr) {
2713 case OVS_KEY_ATTR_ENCAP:
2714 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2715 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2716 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2717 verbose);
2718 } else if (nl_attr_get_size(a)) {
2719 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2720 ds, verbose);
2721 }
2722 break;
2723
2724 case OVS_KEY_ATTR_PRIORITY:
2725 case OVS_KEY_ATTR_SKB_MARK:
2726 case OVS_KEY_ATTR_DP_HASH:
2727 case OVS_KEY_ATTR_RECIRC_ID:
2728 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2729 if (!is_exact) {
2730 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2731 }
2732 break;
2733
2734 case OVS_KEY_ATTR_CT_MARK:
2735 if (verbose || !mask_empty(ma)) {
2736 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2737 if (!is_exact) {
2738 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2739 }
2740 }
2741 break;
2742
2743 case OVS_KEY_ATTR_CT_STATE:
2744 if (verbose) {
2745 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2746 if (!is_exact) {
2747 ds_put_format(ds, "/%#"PRIx32,
2748 mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
2749 }
2750 } else if (!is_exact) {
2751 format_flags_masked(ds, NULL, odp_ct_state_to_string,
2752 nl_attr_get_u32(a),
2753 mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
2754 UINT32_MAX);
2755 } else {
2756 format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
2757 }
2758 break;
2759
2760 case OVS_KEY_ATTR_CT_ZONE:
2761 if (verbose || !mask_empty(ma)) {
2762 ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
2763 if (!is_exact) {
2764 ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
2765 }
2766 }
2767 break;
2768
2769 case OVS_KEY_ATTR_CT_LABELS: {
2770 const ovs_u128 *value = nl_attr_get(a);
2771 const ovs_u128 *mask = ma ? nl_attr_get(ma) : NULL;
2772
2773 format_u128(ds, value, mask, verbose);
2774 break;
2775 }
2776
2777 case OVS_KEY_ATTR_TUNNEL:
2778 format_odp_tun_attr(a, ma, ds, verbose);
2779 break;
2780
2781 case OVS_KEY_ATTR_IN_PORT:
2782 if (portno_names && verbose && is_exact) {
2783 char *name = odp_portno_names_get(portno_names,
2784 nl_attr_get_odp_port(a));
2785 if (name) {
2786 ds_put_format(ds, "%s", name);
2787 } else {
2788 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2789 }
2790 } else {
2791 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2792 if (!is_exact) {
2793 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2794 }
2795 }
2796 break;
2797
2798 case OVS_KEY_ATTR_ETHERNET: {
2799 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2800 const struct ovs_key_ethernet *key = nl_attr_get(a);
2801
2802 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2803 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2804 ds_chomp(ds, ',');
2805 break;
2806 }
2807 case OVS_KEY_ATTR_VLAN:
2808 format_vlan_tci(ds, nl_attr_get_be16(a),
2809 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2810 break;
2811
2812 case OVS_KEY_ATTR_MPLS: {
2813 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2814 const struct ovs_key_mpls *mpls_mask = NULL;
2815 size_t size = nl_attr_get_size(a);
2816
2817 if (!size || size % sizeof *mpls_key) {
2818 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2819 return;
2820 }
2821 if (!is_exact) {
2822 mpls_mask = nl_attr_get(ma);
2823 if (size != nl_attr_get_size(ma)) {
2824 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2825 "mask length %"PRIuSIZE")",
2826 size, nl_attr_get_size(ma));
2827 return;
2828 }
2829 }
2830 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2831 break;
2832 }
2833 case OVS_KEY_ATTR_ETHERTYPE:
2834 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2835 if (!is_exact) {
2836 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2837 }
2838 break;
2839
2840 case OVS_KEY_ATTR_IPV4: {
2841 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2842 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2843
2844 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2845 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2846 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2847 verbose);
2848 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2849 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2850 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2851 verbose);
2852 ds_chomp(ds, ',');
2853 break;
2854 }
2855 case OVS_KEY_ATTR_IPV6: {
2856 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2857 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2858
2859 format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
2860 verbose);
2861 format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
2862 verbose);
2863 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2864 verbose);
2865 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2866 verbose);
2867 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2868 verbose);
2869 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2870 verbose);
2871 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2872 verbose);
2873 ds_chomp(ds, ',');
2874 break;
2875 }
2876 /* These have the same structure and format. */
2877 case OVS_KEY_ATTR_TCP:
2878 case OVS_KEY_ATTR_UDP:
2879 case OVS_KEY_ATTR_SCTP: {
2880 const struct ovs_key_tcp *key = nl_attr_get(a);
2881 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2882
2883 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2884 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2885 ds_chomp(ds, ',');
2886 break;
2887 }
2888 case OVS_KEY_ATTR_TCP_FLAGS:
2889 if (!is_exact) {
2890 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2891 ntohs(nl_attr_get_be16(a)),
2892 TCP_FLAGS(nl_attr_get_be16(ma)),
2893 TCP_FLAGS(OVS_BE16_MAX));
2894 } else {
2895 format_flags(ds, packet_tcp_flag_to_string,
2896 ntohs(nl_attr_get_be16(a)), '|');
2897 }
2898 break;
2899
2900 case OVS_KEY_ATTR_ICMP: {
2901 const struct ovs_key_icmp *key = nl_attr_get(a);
2902 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2903
2904 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2905 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2906 ds_chomp(ds, ',');
2907 break;
2908 }
2909 case OVS_KEY_ATTR_ICMPV6: {
2910 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2911 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2912
2913 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2914 verbose);
2915 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2916 verbose);
2917 ds_chomp(ds, ',');
2918 break;
2919 }
2920 case OVS_KEY_ATTR_ARP: {
2921 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2922 const struct ovs_key_arp *key = nl_attr_get(a);
2923
2924 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2925 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2926 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2927 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2928 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2929 ds_chomp(ds, ',');
2930 break;
2931 }
2932 case OVS_KEY_ATTR_ND: {
2933 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2934 const struct ovs_key_nd *key = nl_attr_get(a);
2935
2936 format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
2937 verbose);
2938 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2939 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2940
2941 ds_chomp(ds, ',');
2942 break;
2943 }
2944 case OVS_KEY_ATTR_UNSPEC:
2945 case __OVS_KEY_ATTR_MAX:
2946 default:
2947 format_generic_odp_key(a, ds);
2948 if (!is_exact) {
2949 ds_put_char(ds, '/');
2950 format_generic_odp_key(ma, ds);
2951 }
2952 break;
2953 }
2954 ds_put_char(ds, ')');
2955 }
2956
2957 static struct nlattr *
2958 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2959 struct ofpbuf *ofp, const struct nlattr *key)
2960 {
2961 const struct nlattr *a;
2962 unsigned int left;
2963 int type = nl_attr_type(key);
2964 int size = nl_attr_get_size(key);
2965
2966 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2967 nl_msg_put_unspec_zero(ofp, type, size);
2968 } else {
2969 size_t nested_mask;
2970
2971 if (tbl[type].next) {
2972 tbl = tbl[type].next;
2973 max = tbl[type].next_max;
2974 }
2975
2976 nested_mask = nl_msg_start_nested(ofp, type);
2977 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2978 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2979 }
2980 nl_msg_end_nested(ofp, nested_mask);
2981 }
2982
2983 return ofp->base;
2984 }
2985
2986 static void
2987 format_u128(struct ds *ds, const ovs_u128 *key, const ovs_u128 *mask,
2988 bool verbose)
2989 {
2990 if (verbose || (mask && !ovs_u128_is_zero(*mask))) {
2991 ovs_be128 value;
2992
2993 value = hton128(*key);
2994 ds_put_hex(ds, &value, sizeof value);
2995 if (mask && !(ovs_u128_is_ones(*mask))) {
2996 value = hton128(*mask);
2997 ds_put_char(ds, '/');
2998 ds_put_hex(ds, &value, sizeof value);
2999 }
3000 }
3001 }
3002
3003 /* Read the string from 's_' as a 128-bit value. If the string contains
3004 * a "/", the rest of the string will be treated as a 128-bit mask.
3005 *
3006 * If either the value or mask is larger than 64 bits, the string must
3007 * be in hexadecimal.
3008 */
3009 static int
3010 scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
3011 {
3012 char *s = CONST_CAST(char *, s_);
3013 ovs_be128 be_value;
3014 ovs_be128 be_mask;
3015
3016 if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
3017 *value = ntoh128(be_value);
3018
3019 if (mask) {
3020 int n;
3021
3022 if (ovs_scan(s, "/%n", &n)) {
3023 int error;
3024
3025 s += n;
3026 error = parse_int_string(s, (uint8_t *)&be_mask,
3027 sizeof be_mask, &s);
3028 if (error) {
3029 return error;
3030 }
3031 *mask = ntoh128(be_mask);
3032 } else {
3033 *mask = OVS_U128_MAX;
3034 }
3035 }
3036 return s - s_;
3037 }
3038
3039 return 0;
3040 }
3041
3042 int
3043 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
3044 {
3045 const char *s = s_;
3046
3047 if (ovs_scan(s, "ufid:")) {
3048 s += 5;
3049
3050 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
3051 return -EINVAL;
3052 }
3053 s += UUID_LEN;
3054
3055 return s - s_;
3056 }
3057
3058 return 0;
3059 }
3060
3061 void
3062 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
3063 {
3064 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
3065 }
3066
3067 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3068 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3069 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3070 * non-null and 'verbose' is true, translates odp port number to its name. */
3071 void
3072 odp_flow_format(const struct nlattr *key, size_t key_len,
3073 const struct nlattr *mask, size_t mask_len,
3074 const struct hmap *portno_names, struct ds *ds, bool verbose)
3075 {
3076 if (key_len) {
3077 const struct nlattr *a;
3078 unsigned int left;
3079 bool has_ethtype_key = false;
3080 const struct nlattr *ma = NULL;
3081 struct ofpbuf ofp;
3082 bool first_field = true;
3083
3084 ofpbuf_init(&ofp, 100);
3085 NL_ATTR_FOR_EACH (a, left, key, key_len) {
3086 bool is_nested_attr;
3087 bool is_wildcard = false;
3088 int attr_type = nl_attr_type(a);
3089
3090 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
3091 has_ethtype_key = true;
3092 }
3093
3094 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
3095 OVS_KEY_ATTR_MAX, attr_type) ==
3096 ATTR_LEN_NESTED;
3097
3098 if (mask && mask_len) {
3099 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
3100 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
3101 }
3102
3103 if (verbose || !is_wildcard || is_nested_attr) {
3104 if (is_wildcard && !ma) {
3105 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
3106 OVS_KEY_ATTR_MAX,
3107 &ofp, a);
3108 }
3109 if (!first_field) {
3110 ds_put_char(ds, ',');
3111 }
3112 format_odp_key_attr(a, ma, portno_names, ds, verbose);
3113 first_field = false;
3114 }
3115 ofpbuf_clear(&ofp);
3116 }
3117 ofpbuf_uninit(&ofp);
3118
3119 if (left) {
3120 int i;
3121
3122 if (left == key_len) {
3123 ds_put_cstr(ds, "<empty>");
3124 }
3125 ds_put_format(ds, ",***%u leftover bytes*** (", left);
3126 for (i = 0; i < left; i++) {
3127 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
3128 }
3129 ds_put_char(ds, ')');
3130 }
3131 if (!has_ethtype_key) {
3132 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
3133 if (ma) {
3134 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
3135 ntohs(nl_attr_get_be16(ma)));
3136 }
3137 }
3138 } else {
3139 ds_put_cstr(ds, "<empty>");
3140 }
3141 }
3142
3143 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3144 * OVS_KEY_ATTR_* attributes in 'key'. */
3145 void
3146 odp_flow_key_format(const struct nlattr *key,
3147 size_t key_len, struct ds *ds)
3148 {
3149 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
3150 }
3151
3152 static bool
3153 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
3154 {
3155 if (!strcasecmp(s, "no")) {
3156 *type = OVS_FRAG_TYPE_NONE;
3157 } else if (!strcasecmp(s, "first")) {
3158 *type = OVS_FRAG_TYPE_FIRST;
3159 } else if (!strcasecmp(s, "later")) {
3160 *type = OVS_FRAG_TYPE_LATER;
3161 } else {
3162 return false;
3163 }
3164 return true;
3165 }
3166
3167 /* Parsing. */
3168
3169 static int
3170 scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
3171 {
3172 int n;
3173
3174 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
3175 ETH_ADDR_SCAN_ARGS(*key), &n)) {
3176 int len = n;
3177
3178 if (mask) {
3179 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
3180 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
3181 len += n;
3182 } else {
3183 memset(mask, 0xff, sizeof *mask);
3184 }
3185 }
3186 return len;
3187 }
3188 return 0;
3189 }
3190
3191 static int
3192 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
3193 {
3194 int n;
3195
3196 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
3197 int len = n;
3198
3199 if (mask) {
3200 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
3201 IP_SCAN_ARGS(mask), &n)) {
3202 len += n;
3203 } else {
3204 *mask = OVS_BE32_MAX;
3205 }
3206 }
3207 return len;
3208 }
3209 return 0;
3210 }
3211
3212 static int
3213 scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
3214 {
3215 int n;
3216 char ipv6_s[IPV6_SCAN_LEN + 1];
3217
3218 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
3219 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
3220 int len = n;
3221
3222 if (mask) {
3223 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
3224 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
3225 len += n;
3226 } else {
3227 memset(mask, 0xff, sizeof *mask);
3228 }
3229 }
3230 return len;
3231 }
3232 return 0;
3233 }
3234
3235 static int
3236 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3237 {
3238 int key_, mask_;
3239 int n;
3240
3241 if (ovs_scan(s, "%i%n", &key_, &n)
3242 && (key_ & ~IPV6_LABEL_MASK) == 0) {
3243 int len = n;
3244
3245 *key = htonl(key_);
3246 if (mask) {
3247 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
3248 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
3249 len += n;
3250 *mask = htonl(mask_);
3251 } else {
3252 *mask = htonl(IPV6_LABEL_MASK);
3253 }
3254 }
3255 return len;
3256 }
3257 return 0;
3258 }
3259
3260 static int
3261 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
3262 {
3263 int n;
3264
3265 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
3266 int len = n;
3267
3268 if (mask) {
3269 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
3270 len += n;
3271 } else {
3272 *mask = UINT8_MAX;
3273 }
3274 }
3275 return len;
3276 }
3277 return 0;
3278 }
3279
3280 static int
3281 scan_u16(const char *s, uint16_t *key, uint16_t *mask)
3282 {
3283 int n;
3284
3285 if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
3286 int len = n;
3287
3288 if (mask) {
3289 if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
3290 len += n;
3291 } else {
3292 *mask = UINT16_MAX;
3293 }
3294 }
3295 return len;
3296 }
3297 return 0;
3298 }
3299
3300 static int
3301 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
3302 {
3303 int n;
3304
3305 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3306 int len = n;
3307
3308 if (mask) {
3309 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3310 len += n;
3311 } else {
3312 *mask = UINT32_MAX;
3313 }
3314 }
3315 return len;
3316 }
3317 return 0;
3318 }
3319
3320 static int
3321 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
3322 {
3323 uint16_t key_, mask_;
3324 int n;
3325
3326 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3327 int len = n;
3328
3329 *key = htons(key_);
3330 if (mask) {
3331 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3332 len += n;
3333 *mask = htons(mask_);
3334 } else {
3335 *mask = OVS_BE16_MAX;
3336 }
3337 }
3338 return len;
3339 }
3340 return 0;
3341 }
3342
3343 static int
3344 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
3345 {
3346 uint64_t key_, mask_;
3347 int n;
3348
3349 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
3350 int len = n;
3351
3352 *key = htonll(key_);
3353 if (mask) {
3354 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
3355 len += n;
3356 *mask = htonll(mask_);
3357 } else {
3358 *mask = OVS_BE64_MAX;
3359 }
3360 }
3361 return len;
3362 }
3363 return 0;
3364 }
3365
3366 static int
3367 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
3368 {
3369 uint32_t flags, fmask;
3370 int n;
3371
3372 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
3373 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
3374 if (n >= 0 && s[n] == ')') {
3375 *key = flags;
3376 if (mask) {
3377 *mask = fmask;
3378 }
3379 return n + 1;
3380 }
3381 return 0;
3382 }
3383
3384 static int
3385 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
3386 {
3387 uint32_t flags, fmask;
3388 int n;
3389
3390 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
3391 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
3392 if (n >= 0) {
3393 *key = htons(flags);
3394 if (mask) {
3395 *mask = htons(fmask);
3396 }
3397 return n;
3398 }
3399 return 0;
3400 }
3401
3402 static uint32_t
3403 ovs_to_odp_ct_state(uint8_t state)
3404 {
3405 uint32_t odp = 0;
3406
3407 if (state & CS_NEW) {
3408 odp |= OVS_CS_F_NEW;
3409 }
3410 if (state & CS_ESTABLISHED) {
3411 odp |= OVS_CS_F_ESTABLISHED;
3412 }
3413 if (state & CS_RELATED) {
3414 odp |= OVS_CS_F_RELATED;
3415 }
3416 if (state & CS_INVALID) {
3417 odp |= OVS_CS_F_INVALID;
3418 }
3419 if (state & CS_REPLY_DIR) {
3420 odp |= OVS_CS_F_REPLY_DIR;
3421 }
3422 if (state & CS_TRACKED) {
3423 odp |= OVS_CS_F_TRACKED;
3424 }
3425 if (state & CS_SRC_NAT) {
3426 odp |= OVS_CS_F_SRC_NAT;
3427 }
3428 if (state & CS_DST_NAT) {
3429 odp |= OVS_CS_F_DST_NAT;
3430 }
3431
3432 return odp;
3433 }
3434
3435 static uint8_t
3436 odp_to_ovs_ct_state(uint32_t flags)
3437 {
3438 uint32_t state = 0;
3439
3440 if (flags & OVS_CS_F_NEW) {
3441 state |= CS_NEW;
3442 }
3443 if (flags & OVS_CS_F_ESTABLISHED) {
3444 state |= CS_ESTABLISHED;
3445 }
3446 if (flags & OVS_CS_F_RELATED) {
3447 state |= CS_RELATED;
3448 }
3449 if (flags & OVS_CS_F_INVALID) {
3450 state |= CS_INVALID;
3451 }
3452 if (flags & OVS_CS_F_REPLY_DIR) {
3453 state |= CS_REPLY_DIR;
3454 }
3455 if (flags & OVS_CS_F_TRACKED) {
3456 state |= CS_TRACKED;
3457 }
3458 if (flags & OVS_CS_F_SRC_NAT) {
3459 state |= CS_SRC_NAT;
3460 }
3461 if (flags & OVS_CS_F_DST_NAT) {
3462 state |= CS_DST_NAT;
3463 }
3464
3465 return state;
3466 }
3467
3468 static int
3469 scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
3470 {
3471 uint32_t flags, fmask;
3472 int n;
3473
3474 n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
3475 ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
3476 mask ? &fmask : NULL);
3477
3478 if (n >= 0) {
3479 *key = flags;
3480 if (mask) {
3481 *mask = fmask;
3482 }
3483 return n;
3484 }
3485 return 0;
3486 }
3487
3488 static int
3489 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
3490 {
3491 int n;
3492 char frag[8];
3493 enum ovs_frag_type frag_type;
3494
3495 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
3496 && ovs_frag_type_from_string(frag, &frag_type)) {
3497 int len = n;
3498
3499 *key = frag_type;
3500 if (mask) {
3501 *mask = UINT8_MAX;
3502 }
3503 return len;
3504 }
3505 return 0;
3506 }
3507
3508 static int
3509 scan_port(const char *s, uint32_t *key, uint32_t *mask,
3510 const struct simap *port_names)
3511 {
3512 int n;
3513
3514 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3515 int len = n;
3516
3517 if (mask) {
3518 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3519 len += n;
3520 } else {
3521 *mask = UINT32_MAX;
3522 }
3523 }
3524 return len;
3525 } else if (port_names) {
3526 const struct simap_node *node;
3527 int len;
3528
3529 len = strcspn(s, ")");
3530 node = simap_find_len(port_names, s, len);
3531 if (node) {
3532 *key = node->data;
3533
3534 if (mask) {
3535 *mask = UINT32_MAX;
3536 }
3537 return len;
3538 }
3539 }
3540 return 0;
3541 }
3542
3543 /* Helper for vlan parsing. */
3544 struct ovs_key_vlan__ {
3545 ovs_be16 tci;
3546 };
3547
3548 static bool
3549 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
3550 {
3551 const uint16_t mask = ((1U << bits) - 1) << offset;
3552
3553 if (value >> bits) {
3554 return false;
3555 }
3556
3557 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
3558 return true;
3559 }
3560
3561 static int
3562 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
3563 uint8_t offset)
3564 {
3565 uint16_t key_, mask_;
3566 int n;
3567
3568 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3569 int len = n;
3570
3571 if (set_be16_bf(key, bits, offset, key_)) {
3572 if (mask) {
3573 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3574 len += n;
3575
3576 if (!set_be16_bf(mask, bits, offset, mask_)) {
3577 return 0;
3578 }
3579 } else {
3580 *mask |= htons(((1U << bits) - 1) << offset);
3581 }
3582 }
3583 return len;
3584 }
3585 }
3586 return 0;
3587 }
3588
3589 static int
3590 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
3591 {
3592 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
3593 }
3594
3595 static int
3596 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
3597 {
3598 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
3599 }
3600
3601 static int
3602 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
3603 {
3604 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
3605 }
3606
3607 /* For MPLS. */
3608 static bool
3609 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
3610 {
3611 const uint32_t mask = ((1U << bits) - 1) << offset;
3612
3613 if (value >> bits) {
3614 return false;
3615 }
3616
3617 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
3618 return true;
3619 }
3620
3621 static int
3622 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
3623 uint8_t offset)
3624 {
3625 uint32_t key_, mask_;
3626 int n;
3627
3628 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
3629 int len = n;
3630
3631 if (set_be32_bf(key, bits, offset, key_)) {
3632 if (mask) {
3633 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
3634 len += n;
3635
3636 if (!set_be32_bf(mask, bits, offset, mask_)) {
3637 return 0;
3638 }
3639 } else {
3640 *mask |= htonl(((1U << bits) - 1) << offset);
3641 }
3642 }
3643 return len;
3644 }
3645 }
3646 return 0;
3647 }
3648
3649 static int
3650 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3651 {
3652 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
3653 }
3654
3655 static int
3656 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
3657 {
3658 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
3659 }
3660
3661 static int
3662 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
3663 {
3664 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
3665 }
3666
3667 static int
3668 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
3669 {
3670 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
3671 }
3672
3673 static int
3674 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
3675 {
3676 const char *s_base = s;
3677 ovs_be16 id = 0, id_mask = 0;
3678 uint8_t flags = 0, flags_mask = 0;
3679
3680 if (!strncmp(s, "id=", 3)) {
3681 s += 3;
3682 s += scan_be16(s, &id, mask ? &id_mask : NULL);
3683 }
3684
3685 if (s[0] == ',') {
3686 s++;
3687 }
3688 if (!strncmp(s, "flags=", 6)) {
3689 s += 6;
3690 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
3691 }
3692
3693 if (!strncmp(s, "))", 2)) {
3694 s += 2;
3695
3696 *key = (flags << 16) | ntohs(id);
3697 if (mask) {
3698 *mask = (flags_mask << 16) | ntohs(id_mask);
3699 }
3700
3701 return s - s_base;
3702 }
3703
3704 return 0;
3705 }
3706
3707 static int
3708 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
3709 {
3710 const char *s_base = s;
3711 struct geneve_opt *opt = key->d;
3712 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
3713 int len_remain = sizeof key->d;
3714
3715 while (s[0] == '{' && len_remain >= sizeof *opt) {
3716 int data_len = 0;
3717
3718 s++;
3719 len_remain -= sizeof *opt;
3720
3721 if (!strncmp(s, "class=", 6)) {
3722 s += 6;
3723 s += scan_be16(s, &opt->opt_class,
3724 mask ? &opt_mask->opt_class : NULL);
3725 } else if (mask) {
3726 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
3727 }
3728
3729 if (s[0] == ',') {
3730 s++;
3731 }
3732 if (!strncmp(s, "type=", 5)) {
3733 s += 5;
3734 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
3735 } else if (mask) {
3736 memset(&opt_mask->type, 0, sizeof opt_mask->type);
3737 }
3738
3739 if (s[0] == ',') {
3740 s++;
3741 }
3742 if (!strncmp(s, "len=", 4)) {
3743 uint8_t opt_len, opt_len_mask;
3744 s += 4;
3745 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
3746
3747 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
3748 return 0;
3749 }
3750 opt->length = opt_len / 4;
3751 if (mask) {
3752 opt_mask->length = opt_len_mask;
3753 }
3754 data_len = opt_len;
3755 } else if (mask) {
3756 memset(&opt_mask->type, 0, sizeof opt_mask->type);
3757 }
3758
3759 if (s[0] == ',') {
3760 s++;
3761 }
3762 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
3763 return 0;
3764 }
3765
3766 if (mask) {
3767 if (s[0] == '/') {
3768 s++;
3769 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
3770 data_len, (char **)&s)) {
3771 return 0;
3772 }
3773 }
3774 opt_mask->r1 = 0;
3775 opt_mask->r2 = 0;
3776 opt_mask->r3 = 0;
3777 }
3778
3779 if (s[0] == '}') {
3780 s++;
3781 opt += 1 + data_len / 4;
3782 if (mask) {
3783 opt_mask += 1 + data_len / 4;
3784 }
3785 len_remain -= data_len;
3786 }
3787 }
3788
3789 if (s[0] == ')') {
3790 int len = sizeof key->d - len_remain;
3791
3792 s++;
3793 key->len = len;
3794 if (mask) {
3795 mask->len = len;
3796 }
3797 return s - s_base;
3798 }
3799
3800 return 0;
3801 }
3802
3803 static void
3804 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3805 {
3806 const uint16_t *flags = data_;
3807
3808 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3809 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3810 }
3811 if (*flags & FLOW_TNL_F_CSUM) {
3812 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3813 }
3814 if (*flags & FLOW_TNL_F_OAM) {
3815 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3816 }
3817 }
3818
3819 static void
3820 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3821 {
3822 const uint32_t *gbp = data_;
3823
3824 if (*gbp) {
3825 size_t vxlan_opts_ofs;
3826
3827 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3828 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3829 nl_msg_end_nested(a, vxlan_opts_ofs);
3830 }
3831 }
3832
3833 static void
3834 geneve_to_attr(struct ofpbuf *a, const void *data_)
3835 {
3836 const struct geneve_scan *geneve = data_;
3837
3838 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3839 geneve->len);
3840 }
3841
3842 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3843 { \
3844 unsigned long call_fn = (unsigned long)FUNC; \
3845 if (call_fn) { \
3846 typedef void (*fn)(struct ofpbuf *, const void *); \
3847 fn func = FUNC; \
3848 func(BUF, &(DATA)); \
3849 } else { \
3850 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3851 } \
3852 }
3853
3854 #define SCAN_IF(NAME) \
3855 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3856 const char *start = s; \
3857 int len; \
3858 \
3859 s += strlen(NAME)
3860
3861 /* Usually no special initialization is needed. */
3862 #define SCAN_BEGIN(NAME, TYPE) \
3863 SCAN_IF(NAME); \
3864 TYPE skey, smask; \
3865 memset(&skey, 0, sizeof skey); \
3866 memset(&smask, 0, sizeof smask); \
3867 do { \
3868 len = 0;
3869
3870 /* Init as fully-masked as mask will not be scanned. */
3871 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3872 SCAN_IF(NAME); \
3873 TYPE skey, smask; \
3874 memset(&skey, 0, sizeof skey); \
3875 memset(&smask, 0xff, sizeof smask); \
3876 do { \
3877 len = 0;
3878
3879 /* VLAN needs special initialization. */
3880 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3881 SCAN_IF(NAME); \
3882 TYPE skey = KEY_INIT; \
3883 TYPE smask = MASK_INIT; \
3884 do { \
3885 len = 0;
3886
3887 /* Scan unnamed entry as 'TYPE' */
3888 #define SCAN_TYPE(TYPE, KEY, MASK) \
3889 len = scan_##TYPE(s, KEY, MASK); \
3890 if (len == 0) { \
3891 return -EINVAL; \
3892 } \
3893 s += len
3894
3895 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3896 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3897 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3898 s += strlen(NAME); \
3899 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3900 continue; \
3901 }
3902
3903 #define SCAN_FINISH() \
3904 } while (*s++ == ',' && len != 0); \
3905 if (s[-1] != ')') { \
3906 return -EINVAL; \
3907 }
3908
3909 #define SCAN_FINISH_SINGLE() \
3910 } while (false); \
3911 if (*s++ != ')') { \
3912 return -EINVAL; \
3913 }
3914
3915 /* Beginning of nested attribute. */
3916 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3917 SCAN_IF(NAME); \
3918 size_t key_offset, mask_offset; \
3919 key_offset = nl_msg_start_nested(key, ATTR); \
3920 if (mask) { \
3921 mask_offset = nl_msg_start_nested(mask, ATTR); \
3922 } \
3923 do { \
3924 len = 0;
3925
3926 #define SCAN_END_NESTED() \
3927 SCAN_FINISH(); \
3928 nl_msg_end_nested(key, key_offset); \
3929 if (mask) { \
3930 nl_msg_end_nested(mask, mask_offset); \
3931 } \
3932 return s - start; \
3933 }
3934
3935 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3936 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3937 TYPE skey, smask; \
3938 memset(&skey, 0, sizeof skey); \
3939 memset(&smask, 0xff, sizeof smask); \
3940 s += strlen(NAME); \
3941 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3942 SCAN_PUT(ATTR, FUNC); \
3943 continue; \
3944 }
3945
3946 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3947 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3948
3949 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3950 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3951
3952 #define SCAN_PUT(ATTR, FUNC) \
3953 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3954 if (mask) \
3955 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3956
3957 #define SCAN_END(ATTR) \
3958 SCAN_FINISH(); \
3959 SCAN_PUT(ATTR, NULL); \
3960 return s - start; \
3961 }
3962
3963 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
3964 SCAN_IF(NAME); \
3965 TYPE skey[CNT], smask[CNT]; \
3966 memset(&skey, 0, sizeof skey); \
3967 memset(&smask, 0, sizeof smask); \
3968 int idx = 0, cnt = CNT; \
3969 uint64_t fields = 0; \
3970 do { \
3971 int field = 0; \
3972 len = 0;
3973
3974 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3975 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
3976 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3977 if (fields & (1UL << field)) { \
3978 fields = 0; \
3979 if (++idx == cnt) { \
3980 break; \
3981 } \
3982 } \
3983 s += strlen(NAME); \
3984 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
3985 fields |= 1UL << field; \
3986 continue; \
3987 } \
3988 field++;
3989
3990 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
3991 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
3992
3993 #define SCAN_PUT_ARRAY(ATTR, CNT) \
3994 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
3995 if (mask) { \
3996 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
3997 }
3998
3999 #define SCAN_END_ARRAY(ATTR) \
4000 SCAN_FINISH(); \
4001 if (idx == cnt) { \
4002 return -EINVAL; \
4003 } \
4004 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4005 return s - start; \
4006 }
4007
4008 #define SCAN_END_SINGLE(ATTR) \
4009 SCAN_FINISH_SINGLE(); \
4010 SCAN_PUT(ATTR, NULL); \
4011 return s - start; \
4012 }
4013
4014 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4015 SCAN_BEGIN(NAME, TYPE) { \
4016 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4017 } SCAN_END_SINGLE(ATTR)
4018
4019 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4020 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4021 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4022 } SCAN_END_SINGLE(ATTR)
4023
4024 /* scan_port needs one extra argument. */
4025 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4026 SCAN_BEGIN(NAME, TYPE) { \
4027 len = scan_port(s, &skey, &smask, port_names); \
4028 if (len == 0) { \
4029 return -EINVAL; \
4030 } \
4031 s += len; \
4032 } SCAN_END_SINGLE(ATTR)
4033
4034 static int
4035 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
4036 struct ofpbuf *key, struct ofpbuf *mask)
4037 {
4038 ovs_u128 ufid;
4039 int len;
4040
4041 /* Skip UFID. */
4042 len = odp_ufid_from_string(s, &ufid);
4043 if (len) {
4044 return len;
4045 }
4046
4047 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
4048 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
4049 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
4050 OVS_KEY_ATTR_RECIRC_ID);
4051 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
4052
4053 SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
4054 SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
4055 SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
4056 SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
4057
4058 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
4059 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
4060 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
4061 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
4062 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
4063 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
4064 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
4065 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
4066 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
4067 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
4068 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
4069 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
4070 geneve_to_attr);
4071 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
4072 } SCAN_END_NESTED();
4073
4074 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
4075
4076 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
4077 SCAN_FIELD("src=", eth, eth_src);
4078 SCAN_FIELD("dst=", eth, eth_dst);
4079 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
4080
4081 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
4082 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
4083 SCAN_FIELD("vid=", vid, tci);
4084 SCAN_FIELD("pcp=", pcp, tci);
4085 SCAN_FIELD("cfi=", cfi, tci);
4086 } SCAN_END(OVS_KEY_ATTR_VLAN);
4087
4088 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
4089
4090 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
4091 SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
4092 SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
4093 SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
4094 SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
4095 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
4096
4097 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
4098 SCAN_FIELD("src=", ipv4, ipv4_src);
4099 SCAN_FIELD("dst=", ipv4, ipv4_dst);
4100 SCAN_FIELD("proto=", u8, ipv4_proto);
4101 SCAN_FIELD("tos=", u8, ipv4_tos);
4102 SCAN_FIELD("ttl=", u8, ipv4_ttl);
4103 SCAN_FIELD("frag=", frag, ipv4_frag);
4104 } SCAN_END(OVS_KEY_ATTR_IPV4);
4105
4106 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
4107 SCAN_FIELD("src=", in6_addr, ipv6_src);
4108 SCAN_FIELD("dst=", in6_addr, ipv6_dst);
4109 SCAN_FIELD("label=", ipv6_label, ipv6_label);
4110 SCAN_FIELD("proto=", u8, ipv6_proto);
4111 SCAN_FIELD("tclass=", u8, ipv6_tclass);
4112 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
4113 SCAN_FIELD("frag=", frag, ipv6_frag);
4114 } SCAN_END(OVS_KEY_ATTR_IPV6);
4115
4116 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
4117 SCAN_FIELD("src=", be16, tcp_src);
4118 SCAN_FIELD("dst=", be16, tcp_dst);
4119 } SCAN_END(OVS_KEY_ATTR_TCP);
4120
4121 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
4122
4123 SCAN_BEGIN("udp(", struct ovs_key_udp) {
4124 SCAN_FIELD("src=", be16, udp_src);
4125 SCAN_FIELD("dst=", be16, udp_dst);
4126 } SCAN_END(OVS_KEY_ATTR_UDP);
4127
4128 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
4129 SCAN_FIELD("src=", be16, sctp_src);
4130 SCAN_FIELD("dst=", be16, sctp_dst);
4131 } SCAN_END(OVS_KEY_ATTR_SCTP);
4132
4133 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
4134 SCAN_FIELD("type=", u8, icmp_type);
4135 SCAN_FIELD("code=", u8, icmp_code);
4136 } SCAN_END(OVS_KEY_ATTR_ICMP);
4137
4138 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
4139 SCAN_FIELD("type=", u8, icmpv6_type);
4140 SCAN_FIELD("code=", u8, icmpv6_code);
4141 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
4142
4143 SCAN_BEGIN("arp(", struct ovs_key_arp) {
4144 SCAN_FIELD("sip=", ipv4, arp_sip);
4145 SCAN_FIELD("tip=", ipv4, arp_tip);
4146 SCAN_FIELD("op=", be16, arp_op);
4147 SCAN_FIELD("sha=", eth, arp_sha);
4148 SCAN_FIELD("tha=", eth, arp_tha);
4149 } SCAN_END(OVS_KEY_ATTR_ARP);
4150
4151 SCAN_BEGIN("nd(", struct ovs_key_nd) {
4152 SCAN_FIELD("target=", in6_addr, nd_target);
4153 SCAN_FIELD("sll=", eth, nd_sll);
4154 SCAN_FIELD("tll=", eth, nd_tll);
4155 } SCAN_END(OVS_KEY_ATTR_ND);
4156
4157 /* Encap open-coded. */
4158 if (!strncmp(s, "encap(", 6)) {
4159 const char *start = s;
4160 size_t encap, encap_mask = 0;
4161
4162 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
4163 if (mask) {
4164 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
4165 }
4166
4167 s += 6;
4168 for (;;) {
4169 int retval;
4170
4171 s += strspn(s, delimiters);
4172 if (!*s) {
4173 return -EINVAL;
4174 } else if (*s == ')') {
4175 break;
4176 }
4177
4178 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4179 if (retval < 0) {
4180 return retval;
4181 }
4182 s += retval;
4183 }
4184 s++;
4185
4186 nl_msg_end_nested(key, encap);
4187 if (mask) {
4188 nl_msg_end_nested(mask, encap_mask);
4189 }
4190
4191 return s - start;
4192 }
4193
4194 return -EINVAL;
4195 }
4196
4197 /* Parses the string representation of a datapath flow key, in the
4198 * format output by odp_flow_key_format(). Returns 0 if successful,
4199 * otherwise a positive errno value. On success, the flow key is
4200 * appended to 'key' as a series of Netlink attributes. On failure, no
4201 * data is appended to 'key'. Either way, 'key''s data might be
4202 * reallocated.
4203 *
4204 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4205 * to a port number. (Port names may be used instead of port numbers in
4206 * in_port.)
4207 *
4208 * On success, the attributes appended to 'key' are individually syntactically
4209 * valid, but they may not be valid as a sequence. 'key' might, for example,
4210 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4211 int
4212 odp_flow_from_string(const char *s, const struct simap *port_names,
4213 struct ofpbuf *key, struct ofpbuf *mask)
4214 {
4215 const size_t old_size = key->size;
4216 for (;;) {
4217 int retval;
4218
4219 s += strspn(s, delimiters);
4220 if (!*s) {
4221 return 0;
4222 }
4223
4224 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4225 if (retval < 0) {
4226 key->size = old_size;
4227 return -retval;
4228 }
4229 s += retval;
4230 }
4231
4232 return 0;
4233 }
4234
4235 static uint8_t
4236 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
4237 {
4238 if (is_mask) {
4239 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4240 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4241 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4242 * must use a zero mask for the netlink frag field, and all ones mask
4243 * otherwise. */
4244 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
4245 }
4246 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
4247 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
4248 : OVS_FRAG_TYPE_FIRST;
4249 }
4250
4251 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
4252 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
4253 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
4254 bool is_mask);
4255 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
4256 bool is_mask);
4257 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
4258 bool is_mask);
4259 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
4260 bool is_mask);
4261 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
4262 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
4263 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
4264 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
4265
4266 /* These share the same layout. */
4267 union ovs_key_tp {
4268 struct ovs_key_tcp tcp;
4269 struct ovs_key_udp udp;
4270 struct ovs_key_sctp sctp;
4271 };
4272
4273 static void get_tp_key(const struct flow *, union ovs_key_tp *);
4274 static void put_tp_key(const union ovs_key_tp *, struct flow *);
4275
4276 static void
4277 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
4278 bool export_mask, struct ofpbuf *buf)
4279 {
4280 struct ovs_key_ethernet *eth_key;
4281 size_t encap;
4282 const struct flow *flow = parms->flow;
4283 const struct flow *data = export_mask ? parms->mask : parms->flow;
4284
4285 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
4286
4287 if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) {
4288 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
4289 parms->key_buf);
4290 }
4291
4292 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
4293
4294 if (parms->support.ct_state) {
4295 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4296 ovs_to_odp_ct_state(data->ct_state));
4297 }
4298 if (parms->support.ct_zone) {
4299 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
4300 }
4301 if (parms->support.ct_mark) {
4302 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
4303 }
4304 if (parms->support.ct_label) {
4305 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
4306 sizeof(data->ct_label));
4307 }
4308 if (parms->support.recirc) {
4309 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
4310 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
4311 }
4312
4313 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4314 * is not the magical value "ODPP_NONE". */
4315 if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
4316 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
4317 }
4318
4319 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
4320 sizeof *eth_key);
4321 get_ethernet_key(data, eth_key);
4322
4323 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
4324 if (export_mask) {
4325 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4326 } else {
4327 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
4328 }
4329 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
4330 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
4331 if (flow->vlan_tci == htons(0)) {
4332 goto unencap;
4333 }
4334 } else {
4335 encap = 0;
4336 }
4337
4338 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4339 /* For backwards compatibility with kernels that don't support
4340 * wildcarding, the following convention is used to encode the
4341 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4342 *
4343 * key mask matches
4344 * -------- -------- -------
4345 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4346 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4347 * <none> 0xffff Any non-Ethernet II frame (except valid
4348 * 802.3 SNAP packet with valid eth_type).
4349 */
4350 if (export_mask) {
4351 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4352 }
4353 goto unencap;
4354 }
4355
4356 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
4357
4358 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4359 struct ovs_key_ipv4 *ipv4_key;
4360
4361 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
4362 sizeof *ipv4_key);
4363 get_ipv4_key(data, ipv4_key, export_mask);
4364 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
4365 struct ovs_key_ipv6 *ipv6_key;
4366
4367 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
4368 sizeof *ipv6_key);
4369 get_ipv6_key(data, ipv6_key, export_mask);
4370 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
4371 flow->dl_type == htons(ETH_TYPE_RARP)) {
4372 struct ovs_key_arp *arp_key;
4373
4374 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
4375 sizeof *arp_key);
4376 get_arp_key(data, arp_key);
4377 } else if (eth_type_mpls(flow->dl_type)) {
4378 struct ovs_key_mpls *mpls_key;
4379 int i, n;
4380
4381 n = flow_count_mpls_labels(flow, NULL);
4382 if (export_mask) {
4383 n = MIN(n, parms->support.max_mpls_depth);
4384 }
4385 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
4386 n * sizeof *mpls_key);
4387 for (i = 0; i < n; i++) {
4388 mpls_key[i].mpls_lse = data->mpls_lse[i];
4389 }
4390 }
4391
4392 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4393 if (flow->nw_proto == IPPROTO_TCP) {
4394 union ovs_key_tp *tcp_key;
4395
4396 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
4397 sizeof *tcp_key);
4398 get_tp_key(data, tcp_key);
4399 if (data->tcp_flags) {
4400 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
4401 }
4402 } else if (flow->nw_proto == IPPROTO_UDP) {
4403 union ovs_key_tp *udp_key;
4404
4405 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
4406 sizeof *udp_key);
4407 get_tp_key(data, udp_key);
4408 } else if (flow->nw_proto == IPPROTO_SCTP) {
4409 union ovs_key_tp *sctp_key;
4410
4411 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
4412 sizeof *sctp_key);
4413 get_tp_key(data, sctp_key);
4414 } else if (flow->dl_type == htons(ETH_TYPE_IP)
4415 && flow->nw_proto == IPPROTO_ICMP) {
4416 struct ovs_key_icmp *icmp_key;
4417
4418 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
4419 sizeof *icmp_key);
4420 icmp_key->icmp_type = ntohs(data->tp_src);
4421 icmp_key->icmp_code = ntohs(data->tp_dst);
4422 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
4423 && flow->nw_proto == IPPROTO_ICMPV6) {
4424 struct ovs_key_icmpv6 *icmpv6_key;
4425
4426 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
4427 sizeof *icmpv6_key);
4428 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
4429 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
4430
4431 if (is_nd(flow, NULL)
4432 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4433 * type and code are 8 bits wide. Therefore, an exact match
4434 * looks like htons(0xff), not htons(0xffff). See
4435 * xlate_wc_finish() for details. */
4436 && (!export_mask || (data->tp_src == htons(0xff)
4437 && data->tp_dst == htons(0xff)))) {
4438
4439 struct ovs_key_nd *nd_key;
4440
4441 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
4442 sizeof *nd_key);
4443 nd_key->nd_target = data->nd_target;
4444 nd_key->nd_sll = data->arp_sha;
4445 nd_key->nd_tll = data->arp_tha;
4446 }
4447 }
4448 }
4449
4450 unencap:
4451 if (encap) {
4452 nl_msg_end_nested(buf, encap);
4453 }
4454 }
4455
4456 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4457 *
4458 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4459 * capable of being expanded to allow for that much space. */
4460 void
4461 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
4462 struct ofpbuf *buf)
4463 {
4464 odp_flow_key_from_flow__(parms, false, buf);
4465 }
4466
4467 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4468 * 'buf'.
4469 *
4470 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4471 * capable of being expanded to allow for that much space. */
4472 void
4473 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
4474 struct ofpbuf *buf)
4475 {
4476 odp_flow_key_from_flow__(parms, true, buf);
4477 }
4478
4479 /* Generate ODP flow key from the given packet metadata */
4480 void
4481 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
4482 {
4483 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
4484
4485 if (flow_tnl_dst_is_set(&md->tunnel)) {
4486 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
4487 }
4488
4489 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
4490
4491 if (md->ct_state) {
4492 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4493 ovs_to_odp_ct_state(md->ct_state));
4494 if (md->ct_zone) {
4495 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
4496 }
4497 if (md->ct_mark) {
4498 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
4499 }
4500 if (!ovs_u128_is_zero(md->ct_label)) {
4501 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
4502 sizeof(md->ct_label));
4503 }
4504 }
4505
4506 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4507 * value "ODPP_NONE". */
4508 if (md->in_port.odp_port != ODPP_NONE) {
4509 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
4510 }
4511 }
4512
4513 /* Generate packet metadata from the given ODP flow key. */
4514 void
4515 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
4516 struct pkt_metadata *md)
4517 {
4518 const struct nlattr *nla;
4519 size_t left;
4520 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
4521 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
4522 1u << OVS_KEY_ATTR_IN_PORT;
4523
4524 pkt_metadata_init(md, ODPP_NONE);
4525
4526 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
4527 uint16_t type = nl_attr_type(nla);
4528 size_t len = nl_attr_get_size(nla);
4529 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
4530 OVS_KEY_ATTR_MAX, type);
4531
4532 if (len != expected_len && expected_len >= 0) {
4533 continue;
4534 }
4535
4536 switch (type) {
4537 case OVS_KEY_ATTR_RECIRC_ID:
4538 md->recirc_id = nl_attr_get_u32(nla);
4539 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
4540 break;
4541 case OVS_KEY_ATTR_DP_HASH:
4542 md->dp_hash = nl_attr_get_u32(nla);
4543 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
4544 break;
4545 case OVS_KEY_ATTR_PRIORITY:
4546 md->skb_priority = nl_attr_get_u32(nla);
4547 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
4548 break;
4549 case OVS_KEY_ATTR_SKB_MARK:
4550 md->pkt_mark = nl_attr_get_u32(nla);
4551 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
4552 break;
4553 case OVS_KEY_ATTR_CT_STATE:
4554 md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
4555 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_STATE);
4556 break;
4557 case OVS_KEY_ATTR_CT_ZONE:
4558 md->ct_zone = nl_attr_get_u16(nla);
4559 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_ZONE);
4560 break;
4561 case OVS_KEY_ATTR_CT_MARK:
4562 md->ct_mark = nl_attr_get_u32(nla);
4563 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_MARK);
4564 break;
4565 case OVS_KEY_ATTR_CT_LABELS: {
4566 const ovs_u128 *cl = nl_attr_get(nla);
4567
4568 md->ct_label = *cl;
4569 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_LABELS);
4570 break;
4571 }
4572 case OVS_KEY_ATTR_TUNNEL: {
4573 enum odp_key_fitness res;
4574
4575 res = odp_tun_key_from_attr(nla, &md->tunnel);
4576 if (res == ODP_FIT_ERROR) {
4577 memset(&md->tunnel, 0, sizeof md->tunnel);
4578 } else if (res == ODP_FIT_PERFECT) {
4579 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
4580 }
4581 break;
4582 }
4583 case OVS_KEY_ATTR_IN_PORT:
4584 md->in_port.odp_port = nl_attr_get_odp_port(nla);
4585 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
4586 break;
4587 default:
4588 break;
4589 }
4590
4591 if (!wanted_attrs) {
4592 return; /* Have everything. */
4593 }
4594 }
4595 }
4596
4597 uint32_t
4598 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
4599 {
4600 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
4601 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key), key_len, 0);
4602 }
4603
4604 static void
4605 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
4606 uint64_t attrs, int out_of_range_attr,
4607 const struct nlattr *key, size_t key_len)
4608 {
4609 struct ds s;
4610 int i;
4611
4612 if (VLOG_DROP_DBG(rl)) {
4613 return;
4614 }
4615
4616 ds_init(&s);
4617 for (i = 0; i < 64; i++) {
4618 if (attrs & (UINT64_C(1) << i)) {
4619 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4620
4621 ds_put_format(&s, " %s",
4622 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
4623 }
4624 }
4625 if (out_of_range_attr) {
4626 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
4627 }
4628
4629 ds_put_cstr(&s, ": ");
4630 odp_flow_key_format(key, key_len, &s);
4631
4632 VLOG_DBG("%s:%s", title, ds_cstr(&s));
4633 ds_destroy(&s);
4634 }
4635
4636 static uint8_t
4637 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
4638 {
4639 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4640
4641 if (is_mask) {
4642 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
4643 }
4644
4645 if (odp_frag > OVS_FRAG_TYPE_LATER) {
4646 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
4647 return 0xff; /* Error. */
4648 }
4649
4650 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
4651 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
4652 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
4653 }
4654
4655 static bool
4656 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
4657 const struct nlattr *attrs[], uint64_t *present_attrsp,
4658 int *out_of_range_attrp)
4659 {
4660 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4661 const struct nlattr *nla;
4662 uint64_t present_attrs;
4663 size_t left;
4664
4665 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
4666 present_attrs = 0;
4667 *out_of_range_attrp = 0;
4668 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
4669 uint16_t type = nl_attr_type(nla);
4670 size_t len = nl_attr_get_size(nla);
4671 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
4672 OVS_KEY_ATTR_MAX, type);
4673
4674 if (len != expected_len && expected_len >= 0) {
4675 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4676
4677 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
4678 "length %d", ovs_key_attr_to_string(type, namebuf,
4679 sizeof namebuf),
4680 len, expected_len);
4681 return false;
4682 }
4683
4684 if (type > OVS_KEY_ATTR_MAX) {
4685 *out_of_range_attrp = type;
4686 } else {
4687 if (present_attrs & (UINT64_C(1) << type)) {
4688 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4689
4690 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
4691 ovs_key_attr_to_string(type,
4692 namebuf, sizeof namebuf));
4693 return false;
4694 }
4695
4696 present_attrs |= UINT64_C(1) << type;
4697 attrs[type] = nla;
4698 }
4699 }
4700 if (left) {
4701 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
4702 return false;
4703 }
4704
4705 *present_attrsp = present_attrs;
4706 return true;
4707 }
4708
4709 static enum odp_key_fitness
4710 check_expectations(uint64_t present_attrs, int out_of_range_attr,
4711 uint64_t expected_attrs,
4712 const struct nlattr *key, size_t key_len)
4713 {
4714 uint64_t missing_attrs;
4715 uint64_t extra_attrs;
4716
4717 missing_attrs = expected_attrs & ~present_attrs;
4718 if (missing_attrs) {
4719 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4720 log_odp_key_attributes(&rl, "expected but not present",
4721 missing_attrs, 0, key, key_len);
4722 return ODP_FIT_TOO_LITTLE;
4723 }
4724
4725 extra_attrs = present_attrs & ~expected_attrs;
4726 if (extra_attrs || out_of_range_attr) {
4727 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4728 log_odp_key_attributes(&rl, "present but not expected",
4729 extra_attrs, out_of_range_attr, key, key_len);
4730 return ODP_FIT_TOO_MUCH;
4731 }
4732
4733 return ODP_FIT_PERFECT;
4734 }
4735
4736 static bool
4737 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4738 uint64_t present_attrs, uint64_t *expected_attrs,
4739 struct flow *flow, const struct flow *src_flow)
4740 {
4741 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4742 bool is_mask = flow != src_flow;
4743
4744 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
4745 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
4746 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4747 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
4748 ntohs(flow->dl_type));
4749 return false;
4750 }
4751 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
4752 flow->dl_type != htons(0xffff)) {
4753 return false;
4754 }
4755 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
4756 } else {
4757 if (!is_mask) {
4758 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
4759 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
4760 /* See comments in odp_flow_key_from_flow__(). */
4761 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
4762 return false;
4763 }
4764 }
4765 return true;
4766 }
4767
4768 static enum odp_key_fitness
4769 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4770 uint64_t present_attrs, int out_of_range_attr,
4771 uint64_t expected_attrs, struct flow *flow,
4772 const struct nlattr *key, size_t key_len,
4773 const struct flow *src_flow)
4774 {
4775 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4776 bool is_mask = src_flow != flow;
4777 const void *check_start = NULL;
4778 size_t check_len = 0;
4779 enum ovs_key_attr expected_bit = 0xff;
4780
4781 if (eth_type_mpls(src_flow->dl_type)) {
4782 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
4783 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
4784 }
4785 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
4786 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
4787 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
4788 int n = size / sizeof(ovs_be32);
4789 int i;
4790
4791 if (!size || size % sizeof(ovs_be32)) {
4792 return ODP_FIT_ERROR;
4793 }
4794 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
4795 return ODP_FIT_ERROR;
4796 }
4797
4798 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
4799 flow->mpls_lse[i] = mpls_lse[i];
4800 }
4801 if (n > FLOW_MAX_MPLS_LABELS) {
4802 return ODP_FIT_TOO_MUCH;
4803 }
4804
4805 if (!is_mask) {
4806 /* BOS may be set only in the innermost label. */
4807 for (i = 0; i < n - 1; i++) {
4808 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
4809 return ODP_FIT_ERROR;
4810 }
4811 }
4812
4813 /* BOS must be set in the innermost label. */
4814 if (n < FLOW_MAX_MPLS_LABELS
4815 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
4816 return ODP_FIT_TOO_LITTLE;
4817 }
4818 }
4819 }
4820
4821 goto done;
4822 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
4823 if (!is_mask) {
4824 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
4825 }
4826 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
4827 const struct ovs_key_ipv4 *ipv4_key;
4828
4829 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
4830 put_ipv4_key(ipv4_key, flow, is_mask);
4831 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
4832 return ODP_FIT_ERROR;
4833 }
4834 if (is_mask) {
4835 check_start = ipv4_key;
4836 check_len = sizeof *ipv4_key;
4837 expected_bit = OVS_KEY_ATTR_IPV4;
4838 }
4839 }
4840 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
4841 if (!is_mask) {
4842 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
4843 }
4844 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
4845 const struct ovs_key_ipv6 *ipv6_key;
4846
4847 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
4848 put_ipv6_key(ipv6_key, flow, is_mask);
4849 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
4850 return ODP_FIT_ERROR;
4851 }
4852 if (is_mask) {
4853 check_start = ipv6_key;
4854 check_len = sizeof *ipv6_key;
4855 expected_bit = OVS_KEY_ATTR_IPV6;
4856 }
4857 }
4858 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
4859 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
4860 if (!is_mask) {
4861 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
4862 }
4863 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
4864 const struct ovs_key_arp *arp_key;
4865
4866 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
4867 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
4868 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
4869 "key", ntohs(arp_key->arp_op));
4870 return ODP_FIT_ERROR;
4871 }
4872 put_arp_key(arp_key, flow);
4873 if (is_mask) {
4874 check_start = arp_key;
4875 check_len = sizeof *arp_key;
4876 expected_bit = OVS_KEY_ATTR_ARP;
4877 }
4878 }
4879 } else {
4880 goto done;
4881 }
4882 if (check_len > 0) { /* Happens only when 'is_mask'. */
4883 if (!is_all_zeros(check_start, check_len) &&
4884 flow->dl_type != htons(0xffff)) {
4885 return ODP_FIT_ERROR;
4886 } else {
4887 expected_attrs |= UINT64_C(1) << expected_bit;
4888 }
4889 }
4890
4891 expected_bit = OVS_KEY_ATTR_UNSPEC;
4892 if (src_flow->nw_proto == IPPROTO_TCP
4893 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4894 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4895 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4896 if (!is_mask) {
4897 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4898 }
4899 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4900 const union ovs_key_tp *tcp_key;
4901
4902 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4903 put_tp_key(tcp_key, flow);
4904 expected_bit = OVS_KEY_ATTR_TCP;
4905 }
4906 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4907 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4908 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4909 }
4910 } else if (src_flow->nw_proto == IPPROTO_UDP
4911 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4912 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4913 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4914 if (!is_mask) {
4915 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4916 }
4917 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4918 const union ovs_key_tp *udp_key;
4919
4920 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4921 put_tp_key(udp_key, flow);
4922 expected_bit = OVS_KEY_ATTR_UDP;
4923 }
4924 } else if (src_flow->nw_proto == IPPROTO_SCTP
4925 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4926 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4927 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4928 if (!is_mask) {
4929 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4930 }
4931 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4932 const union ovs_key_tp *sctp_key;
4933
4934 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4935 put_tp_key(sctp_key, flow);
4936 expected_bit = OVS_KEY_ATTR_SCTP;
4937 }
4938 } else if (src_flow->nw_proto == IPPROTO_ICMP
4939 && src_flow->dl_type == htons(ETH_TYPE_IP)
4940 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4941 if (!is_mask) {
4942 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4943 }
4944 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4945 const struct ovs_key_icmp *icmp_key;
4946
4947 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4948 flow->tp_src = htons(icmp_key->icmp_type);
4949 flow->tp_dst = htons(icmp_key->icmp_code);
4950 expected_bit = OVS_KEY_ATTR_ICMP;
4951 }
4952 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4953 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4954 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4955 if (!is_mask) {
4956 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4957 }
4958 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4959 const struct ovs_key_icmpv6 *icmpv6_key;
4960
4961 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4962 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4963 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4964 expected_bit = OVS_KEY_ATTR_ICMPV6;
4965 if (is_nd(src_flow, NULL)) {
4966 if (!is_mask) {
4967 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4968 }
4969 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4970 const struct ovs_key_nd *nd_key;
4971
4972 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4973 flow->nd_target = nd_key->nd_target;
4974 flow->arp_sha = nd_key->nd_sll;
4975 flow->arp_tha = nd_key->nd_tll;
4976 if (is_mask) {
4977 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
4978 * ICMP type and code are 8 bits wide. Therefore, an
4979 * exact match looks like htons(0xff), not
4980 * htons(0xffff). See xlate_wc_finish() for details.
4981 * */
4982 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4983 (flow->tp_src != htons(0xff) ||
4984 flow->tp_dst != htons(0xff))) {
4985 return ODP_FIT_ERROR;
4986 } else {
4987 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4988 }
4989 }
4990 }
4991 }
4992 }
4993 }
4994 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4995 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4996 return ODP_FIT_ERROR;
4997 } else {
4998 expected_attrs |= UINT64_C(1) << expected_bit;
4999 }
5000 }
5001
5002 done:
5003 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
5004 key, key_len);
5005 }
5006
5007 /* Parse 802.1Q header then encapsulated L3 attributes. */
5008 static enum odp_key_fitness
5009 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
5010 uint64_t present_attrs, int out_of_range_attr,
5011 uint64_t expected_attrs, struct flow *flow,
5012 const struct nlattr *key, size_t key_len,
5013 const struct flow *src_flow)
5014 {
5015 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5016 bool is_mask = src_flow != flow;
5017
5018 const struct nlattr *encap
5019 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
5020 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
5021 enum odp_key_fitness encap_fitness;
5022 enum odp_key_fitness fitness;
5023
5024 /* Calculate fitness of outer attributes. */
5025 if (!is_mask) {
5026 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
5027 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
5028 } else {
5029 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
5030 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
5031 }
5032 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
5033 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
5034 }
5035 }
5036 fitness = check_expectations(present_attrs, out_of_range_attr,
5037 expected_attrs, key, key_len);
5038
5039 /* Set vlan_tci.
5040 * Remove the TPID from dl_type since it's not the real Ethertype. */
5041 flow->dl_type = htons(0);
5042 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
5043 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
5044 : htons(0));
5045 if (!is_mask) {
5046 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
5047 return ODP_FIT_TOO_LITTLE;
5048 } else if (flow->vlan_tci == htons(0)) {
5049 /* Corner case for a truncated 802.1Q header. */
5050 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
5051 return ODP_FIT_TOO_MUCH;
5052 }
5053 return fitness;
5054 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5055 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
5056 "but CFI bit is not set", ntohs(flow->vlan_tci));
5057 return ODP_FIT_ERROR;
5058 }
5059 } else {
5060 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
5061 return fitness;
5062 }
5063 }
5064
5065 /* Now parse the encapsulated attributes. */
5066 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
5067 attrs, &present_attrs, &out_of_range_attr)) {
5068 return ODP_FIT_ERROR;
5069 }
5070 expected_attrs = 0;
5071
5072 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
5073 return ODP_FIT_ERROR;
5074 }
5075 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
5076 expected_attrs, flow, key, key_len,
5077 src_flow);
5078
5079 /* The overall fitness is the worse of the outer and inner attributes. */
5080 return MAX(fitness, encap_fitness);
5081 }
5082
5083 static enum odp_key_fitness
5084 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
5085 struct flow *flow, const struct flow *src_flow)
5086 {
5087 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
5088 uint64_t expected_attrs;
5089 uint64_t present_attrs;
5090 int out_of_range_attr;
5091 bool is_mask = src_flow != flow;
5092
5093 memset(flow, 0, sizeof *flow);
5094
5095 /* Parse attributes. */
5096 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
5097 &out_of_range_attr)) {
5098 return ODP_FIT_ERROR;
5099 }
5100 expected_attrs = 0;
5101
5102 /* Metadata. */
5103 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
5104 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
5105 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
5106 } else if (is_mask) {
5107 /* Always exact match recirc_id if it is not specified. */
5108 flow->recirc_id = UINT32_MAX;
5109 }
5110
5111 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
5112 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
5113 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
5114 }
5115 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
5116 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
5117 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
5118 }
5119
5120 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
5121 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
5122 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
5123 }
5124
5125 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
5126 uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
5127
5128 flow->ct_state = odp_to_ovs_ct_state(odp_state);
5129 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
5130 }
5131 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
5132 flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
5133 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
5134 }
5135 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
5136 flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
5137 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
5138 }
5139 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
5140 const ovs_u128 *cl = nl_attr_get(attrs[OVS_KEY_ATTR_CT_LABELS]);
5141
5142 flow->ct_label = *cl;
5143 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
5144 }
5145
5146 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
5147 enum odp_key_fitness res;
5148
5149 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
5150 &flow->tunnel);
5151 if (res == ODP_FIT_ERROR) {
5152 return ODP_FIT_ERROR;
5153 } else if (res == ODP_FIT_PERFECT) {
5154 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
5155 }
5156 }
5157
5158 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
5159 flow->in_port.odp_port
5160 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
5161 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
5162 } else if (!is_mask) {
5163 flow->in_port.odp_port = ODPP_NONE;
5164 }
5165
5166 /* Ethernet header. */
5167 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
5168 const struct ovs_key_ethernet *eth_key;
5169
5170 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
5171 put_ethernet_key(eth_key, flow);
5172 if (is_mask) {
5173 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5174 }
5175 }
5176 if (!is_mask) {
5177 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5178 }
5179
5180 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5181 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
5182 src_flow)) {
5183 return ODP_FIT_ERROR;
5184 }
5185
5186 if (is_mask
5187 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
5188 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
5189 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
5190 expected_attrs, flow, key, key_len, src_flow);
5191 }
5192 if (is_mask) {
5193 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5194 flow->vlan_tci = htons(0xffff);
5195 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
5196 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
5197 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
5198 }
5199 }
5200 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
5201 expected_attrs, flow, key, key_len, src_flow);
5202 }
5203
5204 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5205 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5206 * 'key' fits our expectations for what a flow key should contain.
5207 *
5208 * The 'in_port' will be the datapath's understanding of the port. The
5209 * caller will need to translate with odp_port_to_ofp_port() if the
5210 * OpenFlow port is needed.
5211 *
5212 * This function doesn't take the packet itself as an argument because none of
5213 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5214 * it is always possible to infer which additional attribute(s) should appear
5215 * by looking at the attributes for lower-level protocols, e.g. if the network
5216 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5217 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5218 * must be absent. */
5219 enum odp_key_fitness
5220 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
5221 struct flow *flow)
5222 {
5223 return odp_flow_key_to_flow__(key, key_len, flow, flow);
5224 }
5225
5226 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5227 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5228 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5229 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5230 * well 'key' fits our expectations for what a flow key should contain. */
5231 enum odp_key_fitness
5232 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
5233 struct flow_wildcards *mask, const struct flow *src_flow)
5234 {
5235 if (mask_key_len) {
5236 return odp_flow_key_to_flow__(mask_key, mask_key_len,
5237 &mask->masks, src_flow);
5238
5239 } else {
5240 /* A missing mask means that the flow should be exact matched.
5241 * Generate an appropriate exact wildcard for the flow. */
5242 flow_wildcards_init_for_packet(mask, src_flow);
5243
5244 return ODP_FIT_PERFECT;
5245 }
5246 }
5247
5248 /* Returns 'fitness' as a string, for use in debug messages. */
5249 const char *
5250 odp_key_fitness_to_string(enum odp_key_fitness fitness)
5251 {
5252 switch (fitness) {
5253 case ODP_FIT_PERFECT:
5254 return "OK";
5255 case ODP_FIT_TOO_MUCH:
5256 return "too_much";
5257 case ODP_FIT_TOO_LITTLE:
5258 return "too_little";
5259 case ODP_FIT_ERROR:
5260 return "error";
5261 default:
5262 return "<unknown>";
5263 }
5264 }
5265
5266 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5267 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5268 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5269 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5270 * null, then the return value is not meaningful.) */
5271 size_t
5272 odp_put_userspace_action(uint32_t pid,
5273 const void *userdata, size_t userdata_size,
5274 odp_port_t tunnel_out_port,
5275 bool include_actions,
5276 struct ofpbuf *odp_actions)
5277 {
5278 size_t userdata_ofs;
5279 size_t offset;
5280
5281 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
5282 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
5283 if (userdata) {
5284 userdata_ofs = odp_actions->size + NLA_HDRLEN;
5285
5286 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5287 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5288 * long:
5289 *
5290 * - The kernel rejected shorter userdata with -ERANGE.
5291 *
5292 * - The kernel silently dropped userdata beyond the first 8 bytes.
5293 *
5294 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5295 * separately disable features that required more than 8 bytes.) */
5296 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
5297 MAX(8, userdata_size)),
5298 userdata, userdata_size);
5299 } else {
5300 userdata_ofs = 0;
5301 }
5302 if (tunnel_out_port != ODPP_NONE) {
5303 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
5304 tunnel_out_port);
5305 }
5306 if (include_actions) {
5307 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
5308 }
5309 nl_msg_end_nested(odp_actions, offset);
5310
5311 return userdata_ofs;
5312 }
5313
5314 void
5315 odp_put_tunnel_action(const struct flow_tnl *tunnel,
5316 struct ofpbuf *odp_actions)
5317 {
5318 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
5319 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
5320 nl_msg_end_nested(odp_actions, offset);
5321 }
5322
5323 void
5324 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
5325 struct ovs_action_push_tnl *data)
5326 {
5327 int size = offsetof(struct ovs_action_push_tnl, header);
5328
5329 size += data->header_len;
5330 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
5331 }
5332
5333 \f
5334 /* The commit_odp_actions() function and its helpers. */
5335
5336 static void
5337 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
5338 const void *key, size_t key_size)
5339 {
5340 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
5341 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
5342 nl_msg_end_nested(odp_actions, offset);
5343 }
5344
5345 /* Masked set actions have a mask following the data within the netlink
5346 * attribute. The unmasked bits in the data will be cleared as the data
5347 * is copied to the action. */
5348 void
5349 commit_masked_set_action(struct ofpbuf *odp_actions,
5350 enum ovs_key_attr key_type,
5351 const void *key_, const void *mask_, size_t key_size)
5352 {
5353 size_t offset = nl_msg_start_nested(odp_actions,
5354 OVS_ACTION_ATTR_SET_MASKED);
5355 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
5356 const char *key = key_, *mask = mask_;
5357
5358 memcpy(data + key_size, mask, key_size);
5359 /* Clear unmasked bits while copying. */
5360 while (key_size--) {
5361 *data++ = *key++ & *mask++;
5362 }
5363 nl_msg_end_nested(odp_actions, offset);
5364 }
5365
5366 /* If any of the flow key data that ODP actions can modify are different in
5367 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5368 * 'odp_actions' that change the flow tunneling information in key from
5369 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5370 * same way. In other words, operates the same as commit_odp_actions(), but
5371 * only on tunneling information. */
5372 void
5373 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
5374 struct ofpbuf *odp_actions)
5375 {
5376 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5377 * must have non-zero ipv6_dst. */
5378 if (flow_tnl_dst_is_set(&flow->tunnel)) {
5379 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
5380 return;
5381 }
5382 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
5383 odp_put_tunnel_action(&base->tunnel, odp_actions);
5384 }
5385 }
5386
5387 static bool
5388 commit(enum ovs_key_attr attr, bool use_masked_set,
5389 const void *key, void *base, void *mask, size_t size,
5390 struct ofpbuf *odp_actions)
5391 {
5392 if (memcmp(key, base, size)) {
5393 bool fully_masked = odp_mask_is_exact(attr, mask, size);
5394
5395 if (use_masked_set && !fully_masked) {
5396 commit_masked_set_action(odp_actions, attr, key, mask, size);
5397 } else {
5398 if (!fully_masked) {
5399 memset(mask, 0xff, size);
5400 }
5401 commit_set_action(odp_actions, attr, key, size);
5402 }
5403 memcpy(base, key, size);
5404 return true;
5405 } else {
5406 /* Mask bits are set when we have either read or set the corresponding
5407 * values. Masked bits will be exact-matched, no need to set them
5408 * if the value did not actually change. */
5409 return false;
5410 }
5411 }
5412
5413 static void
5414 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
5415 {
5416 eth->eth_src = flow->dl_src;
5417 eth->eth_dst = flow->dl_dst;
5418 }
5419
5420 static void
5421 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
5422 {
5423 flow->dl_src = eth->eth_src;
5424 flow->dl_dst = eth->eth_dst;
5425 }
5426
5427 static void
5428 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
5429 struct ofpbuf *odp_actions,
5430 struct flow_wildcards *wc,
5431 bool use_masked)
5432 {
5433 struct ovs_key_ethernet key, base, mask;
5434
5435 get_ethernet_key(flow, &key);
5436 get_ethernet_key(base_flow, &base);
5437 get_ethernet_key(&wc->masks, &mask);
5438
5439 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
5440 &key, &base, &mask, sizeof key, odp_actions)) {
5441 put_ethernet_key(&base, base_flow);
5442 put_ethernet_key(&mask, &wc->masks);
5443 }
5444 }
5445
5446 static void
5447 pop_vlan(struct flow *base,
5448 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5449 {
5450 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
5451
5452 if (base->vlan_tci & htons(VLAN_CFI)) {
5453 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
5454 base->vlan_tci = 0;
5455 }
5456 }
5457
5458 static void
5459 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
5460 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5461 {
5462 if (base->vlan_tci == vlan_tci) {
5463 return;
5464 }
5465
5466 pop_vlan(base, odp_actions, wc);
5467 if (vlan_tci & htons(VLAN_CFI)) {
5468 struct ovs_action_push_vlan vlan;
5469
5470 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
5471 vlan.vlan_tci = vlan_tci;
5472 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
5473 &vlan, sizeof vlan);
5474 }
5475 base->vlan_tci = vlan_tci;
5476 }
5477
5478 /* Wildcarding already done at action translation time. */
5479 static void
5480 commit_mpls_action(const struct flow *flow, struct flow *base,
5481 struct ofpbuf *odp_actions)
5482 {
5483 int base_n = flow_count_mpls_labels(base, NULL);
5484 int flow_n = flow_count_mpls_labels(flow, NULL);
5485 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
5486 NULL);
5487
5488 while (base_n > common_n) {
5489 if (base_n - 1 == common_n && flow_n > common_n) {
5490 /* If there is only one more LSE in base than there are common
5491 * between base and flow; and flow has at least one more LSE than
5492 * is common then the topmost LSE of base may be updated using
5493 * set */
5494 struct ovs_key_mpls mpls_key;
5495
5496 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
5497 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
5498 &mpls_key, sizeof mpls_key);
5499 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
5500 common_n++;
5501 } else {
5502 /* Otherwise, if there more LSEs in base than are common between
5503 * base and flow then pop the topmost one. */
5504 ovs_be16 dl_type;
5505 bool popped;
5506
5507 /* If all the LSEs are to be popped and this is not the outermost
5508 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5509 * POP_MPLS action instead of flow->dl_type.
5510 *
5511 * This is because the POP_MPLS action requires its ethertype
5512 * argument to be an MPLS ethernet type but in this case
5513 * flow->dl_type will be a non-MPLS ethernet type.
5514 *
5515 * When the final POP_MPLS action occurs it use flow->dl_type and
5516 * the and the resulting packet will have the desired dl_type. */
5517 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
5518 dl_type = htons(ETH_TYPE_MPLS);
5519 } else {
5520 dl_type = flow->dl_type;
5521 }
5522 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
5523 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
5524 ovs_assert(popped);
5525 base_n--;
5526 }
5527 }
5528
5529 /* If, after the above popping and setting, there are more LSEs in flow
5530 * than base then some LSEs need to be pushed. */
5531 while (base_n < flow_n) {
5532 struct ovs_action_push_mpls *mpls;
5533
5534 mpls = nl_msg_put_unspec_zero(odp_actions,
5535 OVS_ACTION_ATTR_PUSH_MPLS,
5536 sizeof *mpls);
5537 mpls->mpls_ethertype = flow->dl_type;
5538 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
5539 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
5540 * headers if the flow is restored later due to returning from a patch
5541 * port or group bucket. */
5542 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL, false);
5543 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
5544 base_n++;
5545 }
5546 }
5547
5548 static void
5549 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
5550 {
5551 ipv4->ipv4_src = flow->nw_src;
5552 ipv4->ipv4_dst = flow->nw_dst;
5553 ipv4->ipv4_proto = flow->nw_proto;
5554 ipv4->ipv4_tos = flow->nw_tos;
5555 ipv4->ipv4_ttl = flow->nw_ttl;
5556 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
5557 }
5558
5559 static void
5560 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
5561 {
5562 flow->nw_src = ipv4->ipv4_src;
5563 flow->nw_dst = ipv4->ipv4_dst;
5564 flow->nw_proto = ipv4->ipv4_proto;
5565 flow->nw_tos = ipv4->ipv4_tos;
5566 flow->nw_ttl = ipv4->ipv4_ttl;
5567 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
5568 }
5569
5570 static void
5571 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
5572 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5573 bool use_masked)
5574 {
5575 struct ovs_key_ipv4 key, mask, base;
5576
5577 /* Check that nw_proto and nw_frag remain unchanged. */
5578 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
5579 flow->nw_frag == base_flow->nw_frag);
5580
5581 get_ipv4_key(flow, &key, false);
5582 get_ipv4_key(base_flow, &base, false);
5583 get_ipv4_key(&wc->masks, &mask, true);
5584 mask.ipv4_proto = 0; /* Not writeable. */
5585 mask.ipv4_frag = 0; /* Not writable. */
5586
5587 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
5588 odp_actions)) {
5589 put_ipv4_key(&base, base_flow, false);
5590 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
5591 put_ipv4_key(&mask, &wc->masks, true);
5592 }
5593 }
5594 }
5595
5596 static void
5597 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
5598 {
5599 ipv6->ipv6_src = flow->ipv6_src;
5600 ipv6->ipv6_dst = flow->ipv6_dst;
5601 ipv6->ipv6_label = flow->ipv6_label;
5602 ipv6->ipv6_proto = flow->nw_proto;
5603 ipv6->ipv6_tclass = flow->nw_tos;
5604 ipv6->ipv6_hlimit = flow->nw_ttl;
5605 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
5606 }
5607
5608 static void
5609 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
5610 {
5611 flow->ipv6_src = ipv6->ipv6_src;
5612 flow->ipv6_dst = ipv6->ipv6_dst;
5613 flow->ipv6_label = ipv6->ipv6_label;
5614 flow->nw_proto = ipv6->ipv6_proto;
5615 flow->nw_tos = ipv6->ipv6_tclass;
5616 flow->nw_ttl = ipv6->ipv6_hlimit;
5617 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
5618 }
5619
5620 static void
5621 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
5622 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5623 bool use_masked)
5624 {
5625 struct ovs_key_ipv6 key, mask, base;
5626
5627 /* Check that nw_proto and nw_frag remain unchanged. */
5628 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
5629 flow->nw_frag == base_flow->nw_frag);
5630
5631 get_ipv6_key(flow, &key, false);
5632 get_ipv6_key(base_flow, &base, false);
5633 get_ipv6_key(&wc->masks, &mask, true);
5634 mask.ipv6_proto = 0; /* Not writeable. */
5635 mask.ipv6_frag = 0; /* Not writable. */
5636
5637 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
5638 odp_actions)) {
5639 put_ipv6_key(&base, base_flow, false);
5640 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
5641 put_ipv6_key(&mask, &wc->masks, true);
5642 }
5643 }
5644 }
5645
5646 static void
5647 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
5648 {
5649 /* ARP key has padding, clear it. */
5650 memset(arp, 0, sizeof *arp);
5651
5652 arp->arp_sip = flow->nw_src;
5653 arp->arp_tip = flow->nw_dst;
5654 arp->arp_op = htons(flow->nw_proto);
5655 arp->arp_sha = flow->arp_sha;
5656 arp->arp_tha = flow->arp_tha;
5657 }
5658
5659 static void
5660 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
5661 {
5662 flow->nw_src = arp->arp_sip;
5663 flow->nw_dst = arp->arp_tip;
5664 flow->nw_proto = ntohs(arp->arp_op);
5665 flow->arp_sha = arp->arp_sha;
5666 flow->arp_tha = arp->arp_tha;
5667 }
5668
5669 static enum slow_path_reason
5670 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
5671 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5672 {
5673 struct ovs_key_arp key, mask, base;
5674
5675 get_arp_key(flow, &key);
5676 get_arp_key(base_flow, &base);
5677 get_arp_key(&wc->masks, &mask);
5678
5679 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
5680 odp_actions)) {
5681 put_arp_key(&base, base_flow);
5682 put_arp_key(&mask, &wc->masks);
5683 return SLOW_ACTION;
5684 }
5685 return 0;
5686 }
5687
5688 static void
5689 get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
5690 {
5691 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5692 icmp->icmp_type = ntohs(flow->tp_src);
5693 icmp->icmp_code = ntohs(flow->tp_dst);
5694 }
5695
5696 static void
5697 put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
5698 {
5699 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5700 flow->tp_src = htons(icmp->icmp_type);
5701 flow->tp_dst = htons(icmp->icmp_code);
5702 }
5703
5704 static enum slow_path_reason
5705 commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
5706 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5707 {
5708 struct ovs_key_icmp key, mask, base;
5709 enum ovs_key_attr attr;
5710
5711 if (is_icmpv4(flow, NULL)) {
5712 attr = OVS_KEY_ATTR_ICMP;
5713 } else if (is_icmpv6(flow, NULL)) {
5714 attr = OVS_KEY_ATTR_ICMPV6;
5715 } else {
5716 return 0;
5717 }
5718
5719 get_icmp_key(flow, &key);
5720 get_icmp_key(base_flow, &base);
5721 get_icmp_key(&wc->masks, &mask);
5722
5723 if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) {
5724 put_icmp_key(&base, base_flow);
5725 put_icmp_key(&mask, &wc->masks);
5726 return SLOW_ACTION;
5727 }
5728 return 0;
5729 }
5730
5731 static void
5732 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
5733 {
5734 nd->nd_target = flow->nd_target;
5735 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5736 nd->nd_sll = flow->arp_sha;
5737 nd->nd_tll = flow->arp_tha;
5738 }
5739
5740 static void
5741 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
5742 {
5743 flow->nd_target = nd->nd_target;
5744 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5745 flow->arp_sha = nd->nd_sll;
5746 flow->arp_tha = nd->nd_tll;
5747 }
5748
5749 static enum slow_path_reason
5750 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
5751 struct ofpbuf *odp_actions,
5752 struct flow_wildcards *wc, bool use_masked)
5753 {
5754 struct ovs_key_nd key, mask, base;
5755
5756 get_nd_key(flow, &key);
5757 get_nd_key(base_flow, &base);
5758 get_nd_key(&wc->masks, &mask);
5759
5760 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
5761 odp_actions)) {
5762 put_nd_key(&base, base_flow);
5763 put_nd_key(&mask, &wc->masks);
5764 return SLOW_ACTION;
5765 }
5766
5767 return 0;
5768 }
5769
5770 static enum slow_path_reason
5771 commit_set_nw_action(const struct flow *flow, struct flow *base,
5772 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5773 bool use_masked)
5774 {
5775 /* Check if 'flow' really has an L3 header. */
5776 if (!flow->nw_proto) {
5777 return 0;
5778 }
5779
5780 switch (ntohs(base->dl_type)) {
5781 case ETH_TYPE_IP:
5782 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
5783 break;
5784
5785 case ETH_TYPE_IPV6:
5786 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
5787 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
5788
5789 case ETH_TYPE_ARP:
5790 return commit_set_arp_action(flow, base, odp_actions, wc);
5791 }
5792
5793 return 0;
5794 }
5795
5796 /* TCP, UDP, and SCTP keys have the same layout. */
5797 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
5798 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
5799
5800 static void
5801 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
5802 {
5803 tp->tcp.tcp_src = flow->tp_src;
5804 tp->tcp.tcp_dst = flow->tp_dst;
5805 }
5806
5807 static void
5808 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
5809 {
5810 flow->tp_src = tp->tcp.tcp_src;
5811 flow->tp_dst = tp->tcp.tcp_dst;
5812 }
5813
5814 static void
5815 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
5816 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5817 bool use_masked)
5818 {
5819 enum ovs_key_attr key_type;
5820 union ovs_key_tp key, mask, base;
5821
5822 /* Check if 'flow' really has an L3 header. */
5823 if (!flow->nw_proto) {
5824 return;
5825 }
5826
5827 if (!is_ip_any(base_flow)) {
5828 return;
5829 }
5830
5831 if (flow->nw_proto == IPPROTO_TCP) {
5832 key_type = OVS_KEY_ATTR_TCP;
5833 } else if (flow->nw_proto == IPPROTO_UDP) {
5834 key_type = OVS_KEY_ATTR_UDP;
5835 } else if (flow->nw_proto == IPPROTO_SCTP) {
5836 key_type = OVS_KEY_ATTR_SCTP;
5837 } else {
5838 return;
5839 }
5840
5841 get_tp_key(flow, &key);
5842 get_tp_key(base_flow, &base);
5843 get_tp_key(&wc->masks, &mask);
5844
5845 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
5846 odp_actions)) {
5847 put_tp_key(&base, base_flow);
5848 put_tp_key(&mask, &wc->masks);
5849 }
5850 }
5851
5852 static void
5853 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
5854 struct ofpbuf *odp_actions,
5855 struct flow_wildcards *wc,
5856 bool use_masked)
5857 {
5858 uint32_t key, mask, base;
5859
5860 key = flow->skb_priority;
5861 base = base_flow->skb_priority;
5862 mask = wc->masks.skb_priority;
5863
5864 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
5865 sizeof key, odp_actions)) {
5866 base_flow->skb_priority = base;
5867 wc->masks.skb_priority = mask;
5868 }
5869 }
5870
5871 static void
5872 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
5873 struct ofpbuf *odp_actions,
5874 struct flow_wildcards *wc,
5875 bool use_masked)
5876 {
5877 uint32_t key, mask, base;
5878
5879 key = flow->pkt_mark;
5880 base = base_flow->pkt_mark;
5881 mask = wc->masks.pkt_mark;
5882
5883 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
5884 sizeof key, odp_actions)) {
5885 base_flow->pkt_mark = base;
5886 wc->masks.pkt_mark = mask;
5887 }
5888 }
5889
5890 /* If any of the flow key data that ODP actions can modify are different in
5891 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5892 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5893 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5894 * in addition to this function if needed. Sets fields in 'wc' that are
5895 * used as part of the action.
5896 *
5897 * Returns a reason to force processing the flow's packets into the userspace
5898 * slow path, if there is one, otherwise 0. */
5899 enum slow_path_reason
5900 commit_odp_actions(const struct flow *flow, struct flow *base,
5901 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5902 bool use_masked)
5903 {
5904 enum slow_path_reason slow1, slow2;
5905 bool mpls_done = false;
5906
5907 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
5908 /* Make packet a non-MPLS packet before committing L3/4 actions,
5909 * which would otherwise do nothing. */
5910 if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) {
5911 commit_mpls_action(flow, base, odp_actions);
5912 mpls_done = true;
5913 }
5914 slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
5915 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
5916 slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
5917 if (!mpls_done) {
5918 commit_mpls_action(flow, base, odp_actions);
5919 }
5920 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
5921 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
5922 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
5923
5924 return slow1 ? slow1 : slow2;
5925 }