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