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