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