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