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