2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
38 #include "tun-metadata.h"
39 #include "unaligned.h"
42 #include "openvswitch/vlog.h"
44 VLOG_DEFINE_THIS_MODULE(odp_util);
46 /* The interface between userspace and kernel uses an "OVS_*" prefix.
47 * Since this is fairly non-specific for the OVS userspace components,
48 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
49 * interactions with the datapath.
52 /* The set of characters that may separate one action or one key attribute
54 static const char *delimiters = ", \t\r\n";
55 static const char *delimiters_end = ", \t\r\n)";
59 const struct attr_len_tbl *next;
62 #define ATTR_LEN_INVALID -1
63 #define ATTR_LEN_VARIABLE -2
64 #define ATTR_LEN_NESTED -3
66 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
67 struct ofpbuf *, struct ofpbuf *);
68 static void format_odp_key_attr(const struct nlattr *a,
69 const struct nlattr *ma,
70 const struct hmap *portno_names, struct ds *ds,
74 struct geneve_opt d[63];
78 static int scan_geneve(const char *s, struct geneve_scan *key,
79 struct geneve_scan *mask);
80 static void format_geneve_opts(const struct geneve_opt *opt,
81 const struct geneve_opt *mask, int opts_len,
82 struct ds *, bool verbose);
84 static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
85 int max, struct ofpbuf *,
86 const struct nlattr *key);
87 static void format_u128(struct ds *ds, const ovs_u128 *value,
88 const ovs_u128 *mask, bool verbose);
89 static int scan_u128(const char *s, ovs_u128 *value, ovs_u128 *mask);
91 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
94 * - For an action whose argument has a fixed length, returned that
95 * nonnegative length in bytes.
97 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
99 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
101 odp_action_len(uint16_t type)
103 if (type > OVS_ACTION_ATTR_MAX) {
107 switch ((enum ovs_action_attr) type) {
108 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
109 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
110 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
111 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
112 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
113 case OVS_ACTION_ATTR_POP_VLAN: return 0;
114 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
115 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
116 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
117 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
118 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
119 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
120 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
121 case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE;
123 case OVS_ACTION_ATTR_UNSPEC:
124 case __OVS_ACTION_ATTR_MAX:
125 return ATTR_LEN_INVALID;
128 return ATTR_LEN_INVALID;
131 /* Returns a string form of 'attr'. The return value is either a statically
132 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
133 * should be at least OVS_KEY_ATTR_BUFSIZE. */
134 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
136 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
139 case OVS_KEY_ATTR_UNSPEC: return "unspec";
140 case OVS_KEY_ATTR_ENCAP: return "encap";
141 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
142 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
143 case OVS_KEY_ATTR_CT_STATE: return "ct_state";
144 case OVS_KEY_ATTR_CT_ZONE: return "ct_zone";
145 case OVS_KEY_ATTR_CT_MARK: return "ct_mark";
146 case OVS_KEY_ATTR_CT_LABELS: return "ct_label";
147 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
148 case OVS_KEY_ATTR_IN_PORT: return "in_port";
149 case OVS_KEY_ATTR_ETHERNET: return "eth";
150 case OVS_KEY_ATTR_VLAN: return "vlan";
151 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
152 case OVS_KEY_ATTR_IPV4: return "ipv4";
153 case OVS_KEY_ATTR_IPV6: return "ipv6";
154 case OVS_KEY_ATTR_TCP: return "tcp";
155 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
156 case OVS_KEY_ATTR_UDP: return "udp";
157 case OVS_KEY_ATTR_SCTP: return "sctp";
158 case OVS_KEY_ATTR_ICMP: return "icmp";
159 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
160 case OVS_KEY_ATTR_ARP: return "arp";
161 case OVS_KEY_ATTR_ND: return "nd";
162 case OVS_KEY_ATTR_MPLS: return "mpls";
163 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
164 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
166 case __OVS_KEY_ATTR_MAX:
168 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
174 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
176 size_t len = nl_attr_get_size(a);
178 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
180 const uint8_t *unspec;
183 unspec = nl_attr_get(a);
184 for (i = 0; i < len; i++) {
185 ds_put_char(ds, i ? ' ': '(');
186 ds_put_format(ds, "%02x", unspec[i]);
188 ds_put_char(ds, ')');
193 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
195 static const struct nl_policy ovs_sample_policy[] = {
196 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
197 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
199 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
201 const struct nlattr *nla_acts;
204 ds_put_cstr(ds, "sample");
206 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
207 ds_put_cstr(ds, "(error)");
211 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
214 ds_put_format(ds, "(sample=%.1f%%,", percentage);
216 ds_put_cstr(ds, "actions(");
217 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
218 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
219 format_odp_actions(ds, nla_acts, len);
220 ds_put_format(ds, "))");
224 slow_path_reason_to_string(uint32_t reason)
226 switch ((enum slow_path_reason) reason) {
227 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
236 slow_path_reason_to_explanation(enum slow_path_reason reason)
239 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
248 parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t),
249 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
251 return parse_flags(s, bit_to_string, ')', NULL, NULL,
252 res_flags, allowed, res_mask);
256 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
258 static const struct nl_policy ovs_userspace_policy[] = {
259 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
260 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
262 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
264 [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC,
267 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
268 const struct nlattr *userdata_attr;
269 const struct nlattr *tunnel_out_port_attr;
271 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
272 ds_put_cstr(ds, "userspace(error)");
276 ds_put_format(ds, "userspace(pid=%"PRIu32,
277 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
279 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
282 const uint8_t *userdata = nl_attr_get(userdata_attr);
283 size_t userdata_len = nl_attr_get_size(userdata_attr);
284 bool userdata_unspec = true;
285 union user_action_cookie cookie;
287 if (userdata_len >= sizeof cookie.type
288 && userdata_len <= sizeof cookie) {
290 memset(&cookie, 0, sizeof cookie);
291 memcpy(&cookie, userdata, userdata_len);
293 userdata_unspec = false;
295 if (userdata_len == sizeof cookie.sflow
296 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
297 ds_put_format(ds, ",sFlow("
298 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
299 vlan_tci_to_vid(cookie.sflow.vlan_tci),
300 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
301 cookie.sflow.output);
302 } else if (userdata_len == sizeof cookie.slow_path
303 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
304 ds_put_cstr(ds, ",slow_path(");
305 format_flags(ds, slow_path_reason_to_string,
306 cookie.slow_path.reason, ',');
307 ds_put_format(ds, ")");
308 } else if (userdata_len == sizeof cookie.flow_sample
309 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
310 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
311 ",collector_set_id=%"PRIu32
312 ",obs_domain_id=%"PRIu32
313 ",obs_point_id=%"PRIu32")",
314 cookie.flow_sample.probability,
315 cookie.flow_sample.collector_set_id,
316 cookie.flow_sample.obs_domain_id,
317 cookie.flow_sample.obs_point_id);
318 } else if (userdata_len >= sizeof cookie.ipfix
319 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
320 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
321 cookie.ipfix.output_odp_port);
323 userdata_unspec = true;
327 if (userdata_unspec) {
329 ds_put_format(ds, ",userdata(");
330 for (i = 0; i < userdata_len; i++) {
331 ds_put_format(ds, "%02x", userdata[i]);
333 ds_put_char(ds, ')');
337 if (a[OVS_USERSPACE_ATTR_ACTIONS]) {
338 ds_put_cstr(ds, ",actions");
341 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
342 if (tunnel_out_port_attr) {
343 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
344 nl_attr_get_u32(tunnel_out_port_attr));
347 ds_put_char(ds, ')');
351 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
353 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
354 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
355 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
356 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
358 ds_put_char(ds, ',');
360 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
361 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
362 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
363 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
365 ds_put_char(ds, ',');
367 if (!(tci & htons(VLAN_CFI))) {
368 ds_put_cstr(ds, "cfi=0");
369 ds_put_char(ds, ',');
375 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
377 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
378 mpls_lse_to_label(mpls_lse),
379 mpls_lse_to_tc(mpls_lse),
380 mpls_lse_to_ttl(mpls_lse),
381 mpls_lse_to_bos(mpls_lse));
385 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
386 const struct ovs_key_mpls *mpls_mask, int n)
389 ovs_be32 key = mpls_key->mpls_lse;
391 if (mpls_mask == NULL) {
392 format_mpls_lse(ds, key);
394 ovs_be32 mask = mpls_mask->mpls_lse;
396 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
397 mpls_lse_to_label(key), mpls_lse_to_label(mask),
398 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
399 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
400 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
405 for (i = 0; i < n; i++) {
406 ds_put_format(ds, "lse%d=%#"PRIx32,
407 i, ntohl(mpls_key[i].mpls_lse));
409 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
411 ds_put_char(ds, ',');
418 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
420 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
424 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
426 ds_put_format(ds, "hash(");
428 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
429 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
431 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
434 ds_put_format(ds, ")");
438 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
440 const struct udp_header *udp;
442 udp = (const struct udp_header *) (ip + 1);
443 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
444 ntohs(udp->udp_src), ntohs(udp->udp_dst),
445 ntohs(udp->udp_csum));
451 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
453 const struct eth_header *eth;
454 const struct ip_header *ip;
457 eth = (const struct eth_header *)data->header;
460 ip = (const struct ip_header *)l3;
463 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
464 data->header_len, data->tnl_type);
465 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
466 ds_put_format(ds, ",src=");
467 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
468 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
471 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
472 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
473 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
474 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
475 ip->ip_proto, ip->ip_tos,
479 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
480 const struct vxlanhdr *vxh;
482 vxh = format_udp_tnl_push_header(ds, ip);
484 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
485 ntohl(get_16aligned_be32(&vxh->vx_flags)),
486 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
487 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
488 const struct genevehdr *gnh;
490 gnh = format_udp_tnl_push_header(ds, ip);
492 ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
493 gnh->oam ? "oam," : "",
494 gnh->critical ? "crit," : "",
495 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
498 ds_put_cstr(ds, ",options(");
499 format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
501 ds_put_char(ds, ')');
504 ds_put_char(ds, ')');
505 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
506 const struct gre_base_hdr *greh;
507 ovs_16aligned_be32 *options;
510 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
511 greh = (const struct gre_base_hdr *) l4;
513 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
514 ntohs(greh->flags), ntohs(greh->protocol));
515 options = (ovs_16aligned_be32 *)(greh + 1);
516 if (greh->flags & htons(GRE_CSUM)) {
517 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
520 if (greh->flags & htons(GRE_KEY)) {
521 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
524 if (greh->flags & htons(GRE_SEQ)) {
525 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
528 ds_put_format(ds, ")");
530 ds_put_format(ds, ")");
534 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
536 struct ovs_action_push_tnl *data;
538 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
540 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
541 format_odp_tnl_push_header(ds, data);
542 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
545 static const struct nl_policy ovs_nat_policy[] = {
546 [OVS_NAT_ATTR_SRC] = { .type = NL_A_FLAG, .optional = true, },
547 [OVS_NAT_ATTR_DST] = { .type = NL_A_FLAG, .optional = true, },
548 [OVS_NAT_ATTR_IP_MIN] = { .type = NL_A_UNSPEC, .optional = true,
549 .min_len = sizeof(struct in_addr),
550 .max_len = sizeof(struct in6_addr)},
551 [OVS_NAT_ATTR_IP_MAX] = { .type = NL_A_UNSPEC, .optional = true,
552 .min_len = sizeof(struct in_addr),
553 .max_len = sizeof(struct in6_addr)},
554 [OVS_NAT_ATTR_PROTO_MIN] = { .type = NL_A_U16, .optional = true, },
555 [OVS_NAT_ATTR_PROTO_MAX] = { .type = NL_A_U16, .optional = true, },
556 [OVS_NAT_ATTR_PERSISTENT] = { .type = NL_A_FLAG, .optional = true, },
557 [OVS_NAT_ATTR_PROTO_HASH] = { .type = NL_A_FLAG, .optional = true, },
558 [OVS_NAT_ATTR_PROTO_RANDOM] = { .type = NL_A_FLAG, .optional = true, },
562 format_odp_ct_nat(struct ds *ds, const struct nlattr *attr)
564 struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)];
566 ovs_be32 ip_min, ip_max;
567 struct in6_addr ip6_min, ip6_max;
568 uint16_t proto_min, proto_max;
570 if (!nl_parse_nested(attr, ovs_nat_policy, a, ARRAY_SIZE(a))) {
571 ds_put_cstr(ds, "nat(error: nl_parse_nested() failed.)");
574 /* If no type, then nothing else either. */
575 if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST])
576 && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX]
577 || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX]
578 || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH]
579 || a[OVS_NAT_ATTR_PROTO_RANDOM])) {
580 ds_put_cstr(ds, "nat(error: options allowed only with \"src\" or \"dst\")");
583 /* Both SNAT & DNAT may not be specified. */
584 if (a[OVS_NAT_ATTR_SRC] && a[OVS_NAT_ATTR_DST]) {
585 ds_put_cstr(ds, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
588 /* proto may not appear without ip. */
589 if (!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_PROTO_MIN]) {
590 ds_put_cstr(ds, "nat(error: proto but no IP.)");
593 /* MAX may not appear without MIN. */
594 if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX])
595 || (!a[OVS_NAT_ATTR_PROTO_MIN] && a[OVS_NAT_ATTR_PROTO_MAX])) {
596 ds_put_cstr(ds, "nat(error: range max without min.)");
599 /* Address sizes must match. */
600 if ((a[OVS_NAT_ATTR_IP_MIN]
601 && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) &&
602 nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr)))
603 || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]
604 && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN])
605 != nl_attr_get_size(a[OVS_NAT_ATTR_IP_MAX])))) {
606 ds_put_cstr(ds, "nat(error: IP address sizes do not match)");
610 addr_len = a[OVS_NAT_ATTR_IP_MIN]
611 ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0;
612 ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN]
613 ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0;
614 ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX]
615 ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0;
616 if (addr_len == sizeof ip6_min) {
617 ip6_min = a[OVS_NAT_ATTR_IP_MIN]
618 ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN])
620 ip6_max = a[OVS_NAT_ATTR_IP_MAX]
621 ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX])
624 proto_min = a[OVS_NAT_ATTR_PROTO_MIN]
625 ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0;
626 proto_max = a[OVS_NAT_ATTR_PROTO_MAX]
627 ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0;
629 if ((addr_len == sizeof(ovs_be32)
630 && ip_max && ntohl(ip_min) > ntohl(ip_max))
631 || (addr_len == sizeof(struct in6_addr)
632 && !ipv6_mask_is_any(&ip6_max)
633 && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0)
634 || (proto_max && proto_min > proto_max)) {
635 ds_put_cstr(ds, "nat(range error)");
639 ds_put_cstr(ds, "nat");
640 if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) {
641 ds_put_char(ds, '(');
642 if (a[OVS_NAT_ATTR_SRC]) {
643 ds_put_cstr(ds, "src");
644 } else if (a[OVS_NAT_ATTR_DST]) {
645 ds_put_cstr(ds, "dst");
649 ds_put_cstr(ds, "=");
651 if (addr_len == sizeof ip_min) {
652 ds_put_format(ds, IP_FMT, IP_ARGS(ip_min));
654 if (ip_max && ip_max != ip_min) {
655 ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max));
657 } else if (addr_len == sizeof ip6_min) {
658 ipv6_format_addr_bracket(&ip6_min, ds, proto_min);
660 if (!ipv6_mask_is_any(&ip6_max) &&
661 memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) {
662 ds_put_char(ds, '-');
663 ipv6_format_addr_bracket(&ip6_max, ds, proto_min);
667 ds_put_format(ds, ":%"PRIu16, proto_min);
669 if (proto_max && proto_max != proto_min) {
670 ds_put_format(ds, "-%"PRIu16, proto_max);
674 ds_put_char(ds, ',');
675 if (a[OVS_NAT_ATTR_PERSISTENT]) {
676 ds_put_cstr(ds, "persistent,");
678 if (a[OVS_NAT_ATTR_PROTO_HASH]) {
679 ds_put_cstr(ds, "hash,");
681 if (a[OVS_NAT_ATTR_PROTO_RANDOM]) {
682 ds_put_cstr(ds, "random,");
685 ds_put_char(ds, ')');
689 static const struct nl_policy ovs_conntrack_policy[] = {
690 [OVS_CT_ATTR_COMMIT] = { .type = NL_A_FLAG, .optional = true, },
691 [OVS_CT_ATTR_ZONE] = { .type = NL_A_U16, .optional = true, },
692 [OVS_CT_ATTR_MARK] = { .type = NL_A_UNSPEC, .optional = true,
693 .min_len = sizeof(uint32_t) * 2 },
694 [OVS_CT_ATTR_LABELS] = { .type = NL_A_UNSPEC, .optional = true,
695 .min_len = sizeof(struct ovs_key_ct_labels) * 2 },
696 [OVS_CT_ATTR_HELPER] = { .type = NL_A_STRING, .optional = true,
697 .min_len = 1, .max_len = 16 },
698 [OVS_CT_ATTR_NAT] = { .type = NL_A_UNSPEC, .optional = true },
702 format_odp_conntrack_action(struct ds *ds, const struct nlattr *attr)
704 struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)];
705 const ovs_u128 *label;
706 const uint32_t *mark;
710 const struct nlattr *nat;
712 if (!nl_parse_nested(attr, ovs_conntrack_policy, a, ARRAY_SIZE(a))) {
713 ds_put_cstr(ds, "ct(error)");
717 commit = a[OVS_CT_ATTR_COMMIT] ? true : false;
718 zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0;
719 mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL;
720 label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL;
721 helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL;
722 nat = a[OVS_CT_ATTR_NAT];
724 ds_put_format(ds, "ct");
725 if (commit || zone || mark || label || helper || nat) {
726 ds_put_cstr(ds, "(");
728 ds_put_format(ds, "commit,");
731 ds_put_format(ds, "zone=%"PRIu16",", zone);
734 ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark,
738 ds_put_format(ds, "label=");
739 format_u128(ds, label, label + 1, true);
740 ds_put_char(ds, ',');
743 ds_put_format(ds, "helper=%s,", helper);
746 format_odp_ct_nat(ds, nat);
749 ds_put_cstr(ds, ")");
754 format_odp_action(struct ds *ds, const struct nlattr *a)
757 enum ovs_action_attr type = nl_attr_type(a);
760 expected_len = odp_action_len(nl_attr_type(a));
761 if (expected_len != ATTR_LEN_VARIABLE &&
762 nl_attr_get_size(a) != expected_len) {
763 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
764 nl_attr_get_size(a), expected_len);
765 format_generic_odp_action(ds, a);
770 case OVS_ACTION_ATTR_OUTPUT:
771 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
773 case OVS_ACTION_ATTR_TUNNEL_POP:
774 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
776 case OVS_ACTION_ATTR_TUNNEL_PUSH:
777 format_odp_tnl_push_action(ds, a);
779 case OVS_ACTION_ATTR_USERSPACE:
780 format_odp_userspace_action(ds, a);
782 case OVS_ACTION_ATTR_RECIRC:
783 format_odp_recirc_action(ds, nl_attr_get_u32(a));
785 case OVS_ACTION_ATTR_HASH:
786 format_odp_hash_action(ds, nl_attr_get(a));
788 case OVS_ACTION_ATTR_SET_MASKED:
790 size = nl_attr_get_size(a) / 2;
791 ds_put_cstr(ds, "set(");
793 /* Masked set action not supported for tunnel key, which is bigger. */
794 if (size <= sizeof(struct ovs_key_ipv6)) {
795 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
796 sizeof(struct nlattr))];
797 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
798 sizeof(struct nlattr))];
800 mask->nla_type = attr->nla_type = nl_attr_type(a);
801 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
802 memcpy(attr + 1, (char *)(a + 1), size);
803 memcpy(mask + 1, (char *)(a + 1) + size, size);
804 format_odp_key_attr(attr, mask, NULL, ds, false);
806 format_odp_key_attr(a, NULL, NULL, ds, false);
808 ds_put_cstr(ds, ")");
810 case OVS_ACTION_ATTR_SET:
811 ds_put_cstr(ds, "set(");
812 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
813 ds_put_cstr(ds, ")");
815 case OVS_ACTION_ATTR_PUSH_VLAN: {
816 const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
817 ds_put_cstr(ds, "push_vlan(");
818 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
819 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
821 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
822 ds_put_char(ds, ')');
825 case OVS_ACTION_ATTR_POP_VLAN:
826 ds_put_cstr(ds, "pop_vlan");
828 case OVS_ACTION_ATTR_PUSH_MPLS: {
829 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
830 ds_put_cstr(ds, "push_mpls(");
831 format_mpls_lse(ds, mpls->mpls_lse);
832 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
835 case OVS_ACTION_ATTR_POP_MPLS: {
836 ovs_be16 ethertype = nl_attr_get_be16(a);
837 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
840 case OVS_ACTION_ATTR_SAMPLE:
841 format_odp_sample_action(ds, a);
843 case OVS_ACTION_ATTR_CT:
844 format_odp_conntrack_action(ds, a);
846 case OVS_ACTION_ATTR_UNSPEC:
847 case __OVS_ACTION_ATTR_MAX:
849 format_generic_odp_action(ds, a);
855 format_odp_actions(struct ds *ds, const struct nlattr *actions,
859 const struct nlattr *a;
862 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
864 ds_put_char(ds, ',');
866 format_odp_action(ds, a);
871 if (left == actions_len) {
872 ds_put_cstr(ds, "<empty>");
874 ds_put_format(ds, ",***%u leftover bytes*** (", left);
875 for (i = 0; i < left; i++) {
876 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
878 ds_put_char(ds, ')');
881 ds_put_cstr(ds, "drop");
885 /* Separate out parse_odp_userspace_action() function. */
887 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
890 union user_action_cookie cookie;
892 odp_port_t tunnel_out_port;
894 void *user_data = NULL;
895 size_t user_data_size = 0;
896 bool include_actions = false;
898 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
904 uint32_t probability;
905 uint32_t collector_set_id;
906 uint32_t obs_domain_id;
907 uint32_t obs_point_id;
910 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
911 "pcp=%i,output=%"SCNi32")%n",
912 &vid, &pcp, &output, &n1)) {
916 tci = vid | (pcp << VLAN_PCP_SHIFT);
921 cookie.type = USER_ACTION_COOKIE_SFLOW;
922 cookie.sflow.vlan_tci = htons(tci);
923 cookie.sflow.output = output;
925 user_data_size = sizeof cookie.sflow;
926 } else if (ovs_scan(&s[n], ",slow_path(%n",
931 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
932 cookie.slow_path.unused = 0;
933 cookie.slow_path.reason = 0;
935 res = parse_odp_flags(&s[n], slow_path_reason_to_string,
936 &cookie.slow_path.reason,
937 SLOW_PATH_REASON_MASK, NULL);
938 if (res < 0 || s[n + res] != ')') {
944 user_data_size = sizeof cookie.slow_path;
945 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
946 "collector_set_id=%"SCNi32","
947 "obs_domain_id=%"SCNi32","
948 "obs_point_id=%"SCNi32")%n",
949 &probability, &collector_set_id,
950 &obs_domain_id, &obs_point_id, &n1)) {
953 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
954 cookie.flow_sample.probability = probability;
955 cookie.flow_sample.collector_set_id = collector_set_id;
956 cookie.flow_sample.obs_domain_id = obs_domain_id;
957 cookie.flow_sample.obs_point_id = obs_point_id;
959 user_data_size = sizeof cookie.flow_sample;
960 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
963 cookie.type = USER_ACTION_COOKIE_IPFIX;
964 cookie.ipfix.output_odp_port = u32_to_odp(output);
966 user_data_size = sizeof cookie.ipfix;
967 } else if (ovs_scan(&s[n], ",userdata(%n",
972 ofpbuf_init(&buf, 16);
973 end = ofpbuf_put_hex(&buf, &s[n], NULL);
977 user_data = buf.data;
978 user_data_size = buf.size;
985 if (ovs_scan(&s[n], ",actions%n", &n1)) {
987 include_actions = true;
993 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
994 &tunnel_out_port, &n1)) {
995 odp_put_userspace_action(pid, user_data, user_data_size,
996 tunnel_out_port, include_actions, actions);
998 } else if (s[n] == ')') {
999 odp_put_userspace_action(pid, user_data, user_data_size,
1000 ODPP_NONE, include_actions, actions);
1009 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
1011 struct eth_header *eth;
1012 struct ip_header *ip;
1013 struct udp_header *udp;
1014 struct gre_base_hdr *greh;
1015 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
1017 uint32_t tnl_type = 0, header_len = 0;
1021 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
1024 eth = (struct eth_header *) data->header;
1025 l3 = (data->header + sizeof *eth);
1026 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
1027 ip = (struct ip_header *) l3;
1028 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
1029 "eth(dst="ETH_ADDR_SCAN_FMT",",
1032 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
1036 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
1037 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
1040 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
1043 eth->eth_type = htons(dl_type);
1046 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
1047 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
1050 &ip->ip_proto, &ip->ip_tos,
1051 &ip->ip_ttl, &ip->ip_frag_off)) {
1054 put_16aligned_be32(&ip->ip_src, sip);
1055 put_16aligned_be32(&ip->ip_dst, dip);
1058 udp = (struct udp_header *) l4;
1059 greh = (struct gre_base_hdr *) l4;
1060 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
1061 &udp_src, &udp_dst, &csum)) {
1062 uint32_t vx_flags, vni;
1064 udp->udp_src = htons(udp_src);
1065 udp->udp_dst = htons(udp_dst);
1067 udp->udp_csum = htons(csum);
1069 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
1071 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
1073 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
1074 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
1075 tnl_type = OVS_VPORT_TYPE_VXLAN;
1076 header_len = sizeof *eth + sizeof *ip +
1077 sizeof *udp + sizeof *vxh;
1078 } else if (ovs_scan_len(s, &n, "geneve(")) {
1079 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
1081 memset(gnh, 0, sizeof *gnh);
1082 header_len = sizeof *eth + sizeof *ip +
1083 sizeof *udp + sizeof *gnh;
1085 if (ovs_scan_len(s, &n, "oam,")) {
1088 if (ovs_scan_len(s, &n, "crit,")) {
1091 if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
1094 if (ovs_scan_len(s, &n, ",options(")) {
1095 struct geneve_scan options;
1098 memset(&options, 0, sizeof options);
1099 len = scan_geneve(s + n, &options, NULL);
1104 memcpy(gnh->options, options.d, options.len);
1105 gnh->opt_len = options.len / 4;
1106 header_len += options.len;
1110 if (!ovs_scan_len(s, &n, "))")) {
1114 gnh->proto_type = htons(ETH_TYPE_TEB);
1115 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
1116 tnl_type = OVS_VPORT_TYPE_GENEVE;
1120 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
1121 &gre_flags, &gre_proto)){
1123 tnl_type = OVS_VPORT_TYPE_GRE;
1124 greh->flags = htons(gre_flags);
1125 greh->protocol = htons(gre_proto);
1126 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
1128 if (greh->flags & htons(GRE_CSUM)) {
1129 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
1133 memset(options, 0, sizeof *options);
1134 *((ovs_be16 *)options) = htons(csum);
1137 if (greh->flags & htons(GRE_KEY)) {
1140 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
1144 put_16aligned_be32(options, htonl(key));
1147 if (greh->flags & htons(GRE_SEQ)) {
1150 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
1153 put_16aligned_be32(options, htonl(seq));
1157 if (!ovs_scan_len(s, &n, "))")) {
1161 header_len = sizeof *eth + sizeof *ip +
1162 ((uint8_t *) options - (uint8_t *) greh);
1167 /* check tunnel meta data. */
1168 if (data->tnl_type != tnl_type) {
1171 if (data->header_len != header_len) {
1176 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1183 struct ct_nat_params {
1189 struct in6_addr ip6;
1193 struct in6_addr ip6;
1203 scan_ct_nat_range(const char *s, int *n, struct ct_nat_params *p)
1205 if (ovs_scan_len(s, n, "=")) {
1206 char ipv6_s[IPV6_SCAN_LEN + 1];
1207 struct in6_addr ipv6;
1209 if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) {
1210 p->addr_len = sizeof p->addr_min.ip;
1211 if (ovs_scan_len(s, n, "-")) {
1212 if (!ovs_scan_len(s, n, IP_SCAN_FMT,
1213 IP_SCAN_ARGS(&p->addr_max.ip))) {
1217 } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
1218 || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
1219 && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1220 p->addr_len = sizeof p->addr_min.ip6;
1221 p->addr_min.ip6 = ipv6;
1222 if (ovs_scan_len(s, n, "-")) {
1223 if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
1224 || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
1225 && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
1226 p->addr_max.ip6 = ipv6;
1234 if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) {
1235 if (ovs_scan_len(s, n, "-")) {
1236 if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) {
1246 scan_ct_nat(const char *s, struct ct_nat_params *p)
1250 if (ovs_scan_len(s, &n, "nat")) {
1251 memset(p, 0, sizeof *p);
1253 if (ovs_scan_len(s, &n, "(")) {
1257 end = strchr(s + n, ')');
1264 n += strspn(s + n, delimiters);
1265 if (ovs_scan_len(s, &n, "src")) {
1266 int err = scan_ct_nat_range(s, &n, p);
1273 if (ovs_scan_len(s, &n, "dst")) {
1274 int err = scan_ct_nat_range(s, &n, p);
1281 if (ovs_scan_len(s, &n, "persistent")) {
1282 p->persistent = true;
1285 if (ovs_scan_len(s, &n, "hash")) {
1286 p->proto_hash = true;
1289 if (ovs_scan_len(s, &n, "random")) {
1290 p->proto_random = true;
1296 if (p->snat && p->dnat) {
1299 if ((p->addr_len != 0 &&
1300 memcmp(&p->addr_max, &in6addr_any, p->addr_len) &&
1301 memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) ||
1302 (p->proto_max && p->proto_max < p->proto_min)) {
1305 if (p->proto_hash && p->proto_random) {
1315 nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions)
1317 size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT);
1320 nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC);
1321 } else if (p->dnat) {
1322 nl_msg_put_flag(actions, OVS_NAT_ATTR_DST);
1326 if (p->addr_len != 0) {
1327 nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min,
1329 if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) {
1330 nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max,
1334 nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min);
1335 if (p->proto_max && p->proto_max > p->proto_min) {
1336 nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max);
1339 if (p->persistent) {
1340 nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT);
1342 if (p->proto_hash) {
1343 nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH);
1345 if (p->proto_random) {
1346 nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM);
1350 nl_msg_end_nested(actions, start);
1354 parse_conntrack_action(const char *s_, struct ofpbuf *actions)
1358 if (ovs_scan(s, "ct")) {
1359 const char *helper = NULL;
1360 size_t helper_len = 0;
1361 bool commit = false;
1366 } ct_mark = { 0, 0 };
1371 struct ct_nat_params nat_params;
1372 bool have_nat = false;
1376 memset(&ct_label, 0, sizeof(ct_label));
1379 if (ovs_scan(s, "(")) {
1382 end = strchr(s, ')');
1390 s += strspn(s, delimiters);
1391 if (ovs_scan(s, "commit%n", &n)) {
1396 if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) {
1400 if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) {
1403 if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) {
1406 ct_mark.mask = UINT32_MAX;
1410 if (ovs_scan(s, "label=%n", &n)) {
1414 retval = scan_u128(s, &ct_label.value, &ct_label.mask);
1421 if (ovs_scan(s, "helper=%n", &n)) {
1423 helper_len = strcspn(s, delimiters_end);
1424 if (!helper_len || helper_len > 15) {
1432 n = scan_ct_nat(s, &nat_params);
1437 /* end points to the end of the nested, nat action.
1438 * find the real end. */
1441 /* Nothing matched. */
1447 start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT);
1449 nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT);
1452 nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone);
1455 nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark,
1458 if (!ovs_u128_is_zero(&ct_label.mask)) {
1459 nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label,
1463 nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper,
1467 nl_msg_put_ct_nat(&nat_params, actions);
1469 nl_msg_end_nested(actions, start);
1476 parse_odp_action(const char *s, const struct simap *port_names,
1477 struct ofpbuf *actions)
1483 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1484 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1490 int len = strcspn(s, delimiters);
1491 struct simap_node *node;
1493 node = simap_find_len(port_names, s, len);
1495 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1504 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1505 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1510 if (!strncmp(s, "userspace(", 10)) {
1511 return parse_odp_userspace_action(s, actions);
1514 if (!strncmp(s, "set(", 4)) {
1517 struct nlattr mask[128 / sizeof(struct nlattr)];
1518 struct ofpbuf maskbuf;
1519 struct nlattr *nested, *key;
1522 /* 'mask' is big enough to hold any key. */
1523 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1525 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1526 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1530 if (s[retval + 4] != ')') {
1534 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1537 size = nl_attr_get_size(mask);
1538 if (size == nl_attr_get_size(key)) {
1539 /* Change to masked set action if not fully masked. */
1540 if (!is_all_ones(mask + 1, size)) {
1541 key->nla_len += size;
1542 ofpbuf_put(actions, mask + 1, size);
1543 /* 'actions' may have been reallocated by ofpbuf_put(). */
1544 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1545 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1549 nl_msg_end_nested(actions, start_ofs);
1554 struct ovs_action_push_vlan push;
1555 int tpid = ETH_TYPE_VLAN;
1560 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1561 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1562 &vid, &pcp, &cfi, &n)
1563 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1564 &tpid, &vid, &pcp, &n)
1565 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1566 &tpid, &vid, &pcp, &cfi, &n)) {
1567 push.vlan_tpid = htons(tpid);
1568 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1569 | (pcp << VLAN_PCP_SHIFT)
1570 | (cfi ? VLAN_CFI : 0));
1571 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1572 &push, sizeof push);
1578 if (!strncmp(s, "pop_vlan", 8)) {
1579 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1587 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1588 && percentage >= 0. && percentage <= 100.0) {
1589 size_t sample_ofs, actions_ofs;
1592 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1593 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1594 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1595 (probability <= 0 ? 0
1596 : probability >= UINT32_MAX ? UINT32_MAX
1599 actions_ofs = nl_msg_start_nested(actions,
1600 OVS_SAMPLE_ATTR_ACTIONS);
1604 n += strspn(s + n, delimiters);
1609 retval = parse_odp_action(s + n, port_names, actions);
1615 nl_msg_end_nested(actions, actions_ofs);
1616 nl_msg_end_nested(actions, sample_ofs);
1618 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1626 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1627 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1635 retval = parse_conntrack_action(s, actions);
1642 struct ovs_action_push_tnl data;
1645 n = ovs_parse_tnl_push(s, &data);
1647 odp_put_tnl_push_action(actions, &data);
1656 /* Parses the string representation of datapath actions, in the format output
1657 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1658 * value. On success, the ODP actions are appended to 'actions' as a series of
1659 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1660 * way, 'actions''s data might be reallocated. */
1662 odp_actions_from_string(const char *s, const struct simap *port_names,
1663 struct ofpbuf *actions)
1667 if (!strcasecmp(s, "drop")) {
1671 old_size = actions->size;
1675 s += strspn(s, delimiters);
1680 retval = parse_odp_action(s, port_names, actions);
1681 if (retval < 0 || !strchr(delimiters, s[retval])) {
1682 actions->size = old_size;
1691 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1692 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1695 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1696 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1697 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1698 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1699 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1700 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1701 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1702 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1703 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1704 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1705 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1706 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1707 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1708 .next = ovs_vxlan_ext_attr_lens ,
1709 .next_max = OVS_VXLAN_EXT_MAX},
1710 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = 16 },
1711 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = 16 },
1714 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1715 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1716 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1717 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1718 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1719 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1720 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1721 .next = ovs_tun_key_attr_lens,
1722 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1723 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1724 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1725 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1726 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1727 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1728 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1729 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1730 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1731 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1732 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1733 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1734 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1735 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1736 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1737 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1738 [OVS_KEY_ATTR_CT_STATE] = { .len = 4 },
1739 [OVS_KEY_ATTR_CT_ZONE] = { .len = 2 },
1740 [OVS_KEY_ATTR_CT_MARK] = { .len = 4 },
1741 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
1744 /* Returns the correct length of the payload for a flow key attribute of the
1745 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1746 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1747 * payload is a nested type. */
1749 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1751 if (type > max_len) {
1752 return ATTR_LEN_INVALID;
1755 return tbl[type].len;
1759 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1761 size_t len = nl_attr_get_size(a);
1763 const uint8_t *unspec;
1766 unspec = nl_attr_get(a);
1767 for (i = 0; i < len; i++) {
1769 ds_put_char(ds, ' ');
1771 ds_put_format(ds, "%02x", unspec[i]);
1777 ovs_frag_type_to_string(enum ovs_frag_type type)
1780 case OVS_FRAG_TYPE_NONE:
1782 case OVS_FRAG_TYPE_FIRST:
1784 case OVS_FRAG_TYPE_LATER:
1786 case __OVS_FRAG_TYPE_MAX:
1792 static enum odp_key_fitness
1793 odp_tun_key_from_attr__(const struct nlattr *attr,
1794 const struct nlattr *flow_attrs, size_t flow_attr_len,
1795 const struct flow_tnl *src_tun, struct flow_tnl *tun,
1799 const struct nlattr *a;
1801 bool unknown = false;
1803 NL_NESTED_FOR_EACH(a, left, attr) {
1804 uint16_t type = nl_attr_type(a);
1805 size_t len = nl_attr_get_size(a);
1806 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1807 OVS_TUNNEL_ATTR_MAX, type);
1809 if (len != expected_len && expected_len >= 0) {
1810 return ODP_FIT_ERROR;
1814 case OVS_TUNNEL_KEY_ATTR_ID:
1815 tun->tun_id = nl_attr_get_be64(a);
1816 tun->flags |= FLOW_TNL_F_KEY;
1818 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1819 tun->ip_src = nl_attr_get_be32(a);
1821 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1822 tun->ip_dst = nl_attr_get_be32(a);
1824 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
1825 tun->ipv6_src = nl_attr_get_in6_addr(a);
1827 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
1828 tun->ipv6_dst = nl_attr_get_in6_addr(a);
1830 case OVS_TUNNEL_KEY_ATTR_TOS:
1831 tun->ip_tos = nl_attr_get_u8(a);
1833 case OVS_TUNNEL_KEY_ATTR_TTL:
1834 tun->ip_ttl = nl_attr_get_u8(a);
1837 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1838 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1840 case OVS_TUNNEL_KEY_ATTR_CSUM:
1841 tun->flags |= FLOW_TNL_F_CSUM;
1843 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1844 tun->tp_src = nl_attr_get_be16(a);
1846 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1847 tun->tp_dst = nl_attr_get_be16(a);
1849 case OVS_TUNNEL_KEY_ATTR_OAM:
1850 tun->flags |= FLOW_TNL_F_OAM;
1852 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1853 static const struct nl_policy vxlan_opts_policy[] = {
1854 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1856 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1858 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1859 return ODP_FIT_ERROR;
1862 if (ext[OVS_VXLAN_EXT_GBP]) {
1863 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1865 tun->gbp_id = htons(gbp & 0xFFFF);
1866 tun->gbp_flags = (gbp >> 16) & 0xFF;
1871 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1872 if (tun_metadata_from_geneve_nlattr(a, flow_attrs, flow_attr_len,
1873 src_tun, udpif, tun)) {
1874 return ODP_FIT_ERROR;
1879 /* Allow this to show up as unexpected, if there are unknown
1880 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1887 return ODP_FIT_ERROR;
1890 return ODP_FIT_TOO_MUCH;
1892 return ODP_FIT_PERFECT;
1895 enum odp_key_fitness
1896 odp_tun_key_from_attr(const struct nlattr *attr, bool udpif,
1897 struct flow_tnl *tun)
1899 memset(tun, 0, sizeof *tun);
1900 return odp_tun_key_from_attr__(attr, NULL, 0, NULL, tun, udpif);
1904 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
1905 const struct flow_tnl *tun_flow_key,
1906 const struct ofpbuf *key_buf)
1910 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1912 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1913 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1914 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1916 if (tun_key->ip_src) {
1917 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1919 if (tun_key->ip_dst) {
1920 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1922 if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
1923 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
1925 if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
1926 nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
1928 if (tun_key->ip_tos) {
1929 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1931 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1932 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1933 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1935 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1936 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1938 if (tun_key->tp_src) {
1939 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1941 if (tun_key->tp_dst) {
1942 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1944 if (tun_key->flags & FLOW_TNL_F_OAM) {
1945 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1947 if (tun_key->gbp_flags || tun_key->gbp_id) {
1948 size_t vxlan_opts_ofs;
1950 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1951 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1952 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1953 nl_msg_end_nested(a, vxlan_opts_ofs);
1955 tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
1957 nl_msg_end_nested(a, tun_key_ofs);
1961 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1963 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1967 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1969 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1970 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1972 if (attr == OVS_KEY_ATTR_IPV6) {
1973 const struct ovs_key_ipv6 *ipv6_mask = mask;
1976 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1977 == htonl(IPV6_LABEL_MASK))
1978 && ipv6_mask->ipv6_proto == UINT8_MAX
1979 && ipv6_mask->ipv6_tclass == UINT8_MAX
1980 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1981 && ipv6_mask->ipv6_frag == UINT8_MAX
1982 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1983 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1985 if (attr == OVS_KEY_ATTR_TUNNEL) {
1989 if (attr == OVS_KEY_ATTR_ARP) {
1990 /* ARP key has padding, ignore it. */
1991 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1992 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1993 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1994 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1997 return is_all_ones(mask, size);
2001 odp_mask_attr_is_exact(const struct nlattr *ma)
2003 enum ovs_key_attr attr = nl_attr_type(ma);
2007 if (attr == OVS_KEY_ATTR_TUNNEL) {
2010 mask = nl_attr_get(ma);
2011 size = nl_attr_get_size(ma);
2014 return odp_mask_is_exact(attr, mask, size);
2018 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
2021 struct odp_portno_names *odp_portno_names;
2023 odp_portno_names = xmalloc(sizeof *odp_portno_names);
2024 odp_portno_names->port_no = port_no;
2025 odp_portno_names->name = xstrdup(port_name);
2026 hmap_insert(portno_names, &odp_portno_names->hmap_node,
2027 hash_odp_port(port_no));
2031 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
2033 struct odp_portno_names *odp_portno_names;
2035 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
2036 hash_odp_port(port_no), portno_names) {
2037 if (odp_portno_names->port_no == port_no) {
2038 return odp_portno_names->name;
2045 odp_portno_names_destroy(struct hmap *portno_names)
2047 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
2048 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
2049 hmap_node, portno_names) {
2050 hmap_remove(portno_names, &odp_portno_names->hmap_node);
2051 free(odp_portno_names->name);
2052 free(odp_portno_names);
2056 /* Format helpers. */
2059 format_eth(struct ds *ds, const char *name, const struct eth_addr key,
2060 const struct eth_addr *mask, bool verbose)
2062 bool mask_empty = mask && eth_addr_is_zero(*mask);
2064 if (verbose || !mask_empty) {
2065 bool mask_full = !mask || eth_mask_is_exact(*mask);
2068 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
2070 ds_put_format(ds, "%s=", name);
2071 eth_format_masked(key, mask, ds);
2072 ds_put_char(ds, ',');
2078 format_be64(struct ds *ds, const char *name, ovs_be64 key,
2079 const ovs_be64 *mask, bool verbose)
2081 bool mask_empty = mask && !*mask;
2083 if (verbose || !mask_empty) {
2084 bool mask_full = !mask || *mask == OVS_BE64_MAX;
2086 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
2087 if (!mask_full) { /* Partially masked. */
2088 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
2090 ds_put_char(ds, ',');
2095 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
2096 const ovs_be32 *mask, bool verbose)
2098 bool mask_empty = mask && !*mask;
2100 if (verbose || !mask_empty) {
2101 bool mask_full = !mask || *mask == OVS_BE32_MAX;
2103 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
2104 if (!mask_full) { /* Partially masked. */
2105 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
2107 ds_put_char(ds, ',');
2112 format_in6_addr(struct ds *ds, const char *name,
2113 const struct in6_addr *key,
2114 const struct in6_addr *mask,
2117 char buf[INET6_ADDRSTRLEN];
2118 bool mask_empty = mask && ipv6_mask_is_any(mask);
2120 if (verbose || !mask_empty) {
2121 bool mask_full = !mask || ipv6_mask_is_exact(mask);
2123 inet_ntop(AF_INET6, key, buf, sizeof buf);
2124 ds_put_format(ds, "%s=%s", name, buf);
2125 if (!mask_full) { /* Partially masked. */
2126 inet_ntop(AF_INET6, mask, buf, sizeof buf);
2127 ds_put_format(ds, "/%s", buf);
2129 ds_put_char(ds, ',');
2134 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
2135 const ovs_be32 (*mask_)[4], bool verbose)
2137 format_in6_addr(ds, name,
2138 (const struct in6_addr *)key_,
2139 mask_ ? (const struct in6_addr *)*mask_ : NULL,
2144 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
2145 const ovs_be32 *mask, bool verbose)
2147 bool mask_empty = mask && !*mask;
2149 if (verbose || !mask_empty) {
2150 bool mask_full = !mask
2151 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
2153 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
2154 if (!mask_full) { /* Partially masked. */
2155 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
2157 ds_put_char(ds, ',');
2162 format_u8x(struct ds *ds, const char *name, uint8_t key,
2163 const uint8_t *mask, bool verbose)
2165 bool mask_empty = mask && !*mask;
2167 if (verbose || !mask_empty) {
2168 bool mask_full = !mask || *mask == UINT8_MAX;
2170 ds_put_format(ds, "%s=%#"PRIx8, name, key);
2171 if (!mask_full) { /* Partially masked. */
2172 ds_put_format(ds, "/%#"PRIx8, *mask);
2174 ds_put_char(ds, ',');
2179 format_u8u(struct ds *ds, const char *name, uint8_t key,
2180 const uint8_t *mask, bool verbose)
2182 bool mask_empty = mask && !*mask;
2184 if (verbose || !mask_empty) {
2185 bool mask_full = !mask || *mask == UINT8_MAX;
2187 ds_put_format(ds, "%s=%"PRIu8, name, key);
2188 if (!mask_full) { /* Partially masked. */
2189 ds_put_format(ds, "/%#"PRIx8, *mask);
2191 ds_put_char(ds, ',');
2196 format_be16(struct ds *ds, const char *name, ovs_be16 key,
2197 const ovs_be16 *mask, bool verbose)
2199 bool mask_empty = mask && !*mask;
2201 if (verbose || !mask_empty) {
2202 bool mask_full = !mask || *mask == OVS_BE16_MAX;
2204 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
2205 if (!mask_full) { /* Partially masked. */
2206 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2208 ds_put_char(ds, ',');
2213 format_be16x(struct ds *ds, const char *name, ovs_be16 key,
2214 const ovs_be16 *mask, bool verbose)
2216 bool mask_empty = mask && !*mask;
2218 if (verbose || !mask_empty) {
2219 bool mask_full = !mask || *mask == OVS_BE16_MAX;
2221 ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
2222 if (!mask_full) { /* Partially masked. */
2223 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
2225 ds_put_char(ds, ',');
2230 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
2231 const uint16_t *mask, bool verbose)
2233 bool mask_empty = mask && !*mask;
2235 if (verbose || !mask_empty) {
2236 ds_put_cstr(ds, name);
2237 ds_put_char(ds, '(');
2239 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
2240 *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
2241 } else { /* Fully masked. */
2242 format_flags(ds, flow_tun_flag_to_string, key, '|');
2244 ds_put_cstr(ds, "),");
2249 check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
2250 const struct attr_len_tbl tbl[], int max_len, bool need_key)
2254 expected_len = odp_key_attr_len(tbl, max_len, nl_attr_type(a));
2255 if (expected_len != ATTR_LEN_VARIABLE &&
2256 expected_len != ATTR_LEN_NESTED) {
2258 bool bad_key_len = nl_attr_get_size(a) != expected_len;
2259 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
2261 if (bad_key_len || bad_mask_len) {
2263 ds_put_format(ds, "key%u", nl_attr_type(a));
2266 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
2267 nl_attr_get_size(a), expected_len);
2269 format_generic_odp_key(a, ds);
2271 ds_put_char(ds, '/');
2273 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
2274 nl_attr_get_size(ma), expected_len);
2276 format_generic_odp_key(ma, ds);
2278 ds_put_char(ds, ')');
2287 format_unknown_key(struct ds *ds, const struct nlattr *a,
2288 const struct nlattr *ma)
2290 ds_put_format(ds, "key%u(", nl_attr_type(a));
2291 format_generic_odp_key(a, ds);
2292 if (ma && !odp_mask_attr_is_exact(ma)) {
2293 ds_put_char(ds, '/');
2294 format_generic_odp_key(ma, ds);
2296 ds_put_cstr(ds, "),");
2300 format_odp_tun_vxlan_opt(const struct nlattr *attr,
2301 const struct nlattr *mask_attr, struct ds *ds,
2305 const struct nlattr *a;
2308 ofpbuf_init(&ofp, 100);
2309 NL_NESTED_FOR_EACH(a, left, attr) {
2310 uint16_t type = nl_attr_type(a);
2311 const struct nlattr *ma = NULL;
2314 ma = nl_attr_find__(nl_attr_get(mask_attr),
2315 nl_attr_get_size(mask_attr), type);
2317 ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
2323 if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
2324 OVS_VXLAN_EXT_MAX, true)) {
2329 case OVS_VXLAN_EXT_GBP: {
2330 uint32_t key = nl_attr_get_u32(a);
2331 ovs_be16 id, id_mask;
2332 uint8_t flags, flags_mask;
2334 id = htons(key & 0xFFFF);
2335 flags = (key >> 16) & 0xFF;
2337 uint32_t mask = nl_attr_get_u32(ma);
2338 id_mask = htons(mask & 0xFFFF);
2339 flags_mask = (mask >> 16) & 0xFF;
2342 ds_put_cstr(ds, "gbp(");
2343 format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
2344 format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
2346 ds_put_cstr(ds, "),");
2351 format_unknown_key(ds, a, ma);
2357 ofpbuf_uninit(&ofp);
2360 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2363 format_geneve_opts(const struct geneve_opt *opt,
2364 const struct geneve_opt *mask, int opts_len,
2365 struct ds *ds, bool verbose)
2367 while (opts_len > 0) {
2369 uint8_t data_len, data_len_mask;
2371 if (opts_len < sizeof *opt) {
2372 ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
2373 opts_len, sizeof *opt);
2377 data_len = opt->length * 4;
2379 if (mask->length == 0x1f) {
2380 data_len_mask = UINT8_MAX;
2382 data_len_mask = mask->length;
2385 len = sizeof *opt + data_len;
2386 if (len > opts_len) {
2387 ds_put_format(ds, "opt len %u greater than remaining %u",
2392 ds_put_char(ds, '{');
2393 format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
2395 format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
2396 format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
2398 (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
2399 ds_put_hex(ds, opt + 1, data_len);
2400 if (mask && !is_all_ones(mask + 1, data_len)) {
2401 ds_put_char(ds, '/');
2402 ds_put_hex(ds, mask + 1, data_len);
2407 ds_put_char(ds, '}');
2409 opt += len / sizeof(*opt);
2411 mask += len / sizeof(*opt);
2418 format_odp_tun_geneve(const struct nlattr *attr,
2419 const struct nlattr *mask_attr, struct ds *ds,
2422 int opts_len = nl_attr_get_size(attr);
2423 const struct geneve_opt *opt = nl_attr_get(attr);
2424 const struct geneve_opt *mask = mask_attr ?
2425 nl_attr_get(mask_attr) : NULL;
2427 if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
2428 ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
2429 nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
2433 format_geneve_opts(opt, mask, opts_len, ds, verbose);
2437 format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
2438 struct ds *ds, bool verbose)
2441 const struct nlattr *a;
2443 uint16_t mask_flags = 0;
2446 ofpbuf_init(&ofp, 100);
2447 NL_NESTED_FOR_EACH(a, left, attr) {
2448 enum ovs_tunnel_key_attr type = nl_attr_type(a);
2449 const struct nlattr *ma = NULL;
2452 ma = nl_attr_find__(nl_attr_get(mask_attr),
2453 nl_attr_get_size(mask_attr), type);
2455 ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
2456 OVS_TUNNEL_KEY_ATTR_MAX,
2461 if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
2462 OVS_TUNNEL_KEY_ATTR_MAX, true)) {
2467 case OVS_TUNNEL_KEY_ATTR_ID:
2468 format_be64(ds, "tun_id", nl_attr_get_be64(a),
2469 ma ? nl_attr_get(ma) : NULL, verbose);
2470 flags |= FLOW_TNL_F_KEY;
2472 mask_flags |= FLOW_TNL_F_KEY;
2475 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
2476 format_ipv4(ds, "src", nl_attr_get_be32(a),
2477 ma ? nl_attr_get(ma) : NULL, verbose);
2479 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
2480 format_ipv4(ds, "dst", nl_attr_get_be32(a),
2481 ma ? nl_attr_get(ma) : NULL, verbose);
2483 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
2484 struct in6_addr ipv6_src;
2485 ipv6_src = nl_attr_get_in6_addr(a);
2486 format_in6_addr(ds, "ipv6_src", &ipv6_src,
2487 ma ? nl_attr_get(ma) : NULL, verbose);
2490 case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
2491 struct in6_addr ipv6_dst;
2492 ipv6_dst = nl_attr_get_in6_addr(a);
2493 format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
2494 ma ? nl_attr_get(ma) : NULL, verbose);
2497 case OVS_TUNNEL_KEY_ATTR_TOS:
2498 format_u8x(ds, "tos", nl_attr_get_u8(a),
2499 ma ? nl_attr_get(ma) : NULL, verbose);
2501 case OVS_TUNNEL_KEY_ATTR_TTL:
2502 format_u8u(ds, "ttl", nl_attr_get_u8(a),
2503 ma ? nl_attr_get(ma) : NULL, verbose);
2505 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2506 flags |= FLOW_TNL_F_DONT_FRAGMENT;
2508 case OVS_TUNNEL_KEY_ATTR_CSUM:
2509 flags |= FLOW_TNL_F_CSUM;
2511 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
2512 format_be16(ds, "tp_src", nl_attr_get_be16(a),
2513 ma ? nl_attr_get(ma) : NULL, verbose);
2515 case OVS_TUNNEL_KEY_ATTR_TP_DST:
2516 format_be16(ds, "tp_dst", nl_attr_get_be16(a),
2517 ma ? nl_attr_get(ma) : NULL, verbose);
2519 case OVS_TUNNEL_KEY_ATTR_OAM:
2520 flags |= FLOW_TNL_F_OAM;
2522 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2523 ds_put_cstr(ds, "vxlan(");
2524 format_odp_tun_vxlan_opt(a, ma, ds, verbose);
2525 ds_put_cstr(ds, "),");
2527 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2528 ds_put_cstr(ds, "geneve(");
2529 format_odp_tun_geneve(a, ma, ds, verbose);
2530 ds_put_cstr(ds, "),");
2532 case __OVS_TUNNEL_KEY_ATTR_MAX:
2534 format_unknown_key(ds, a, ma);
2539 /* Flags can have a valid mask even if the attribute is not set, so
2540 * we need to collect these separately. */
2542 NL_NESTED_FOR_EACH(a, left, mask_attr) {
2543 switch (nl_attr_type(a)) {
2544 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
2545 mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
2547 case OVS_TUNNEL_KEY_ATTR_CSUM:
2548 mask_flags |= FLOW_TNL_F_CSUM;
2550 case OVS_TUNNEL_KEY_ATTR_OAM:
2551 mask_flags |= FLOW_TNL_F_OAM;
2557 format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
2560 ofpbuf_uninit(&ofp);
2564 odp_ct_state_to_string(uint32_t flag)
2567 case OVS_CS_F_REPLY_DIR:
2569 case OVS_CS_F_TRACKED:
2573 case OVS_CS_F_ESTABLISHED:
2575 case OVS_CS_F_RELATED:
2577 case OVS_CS_F_INVALID:
2585 format_frag(struct ds *ds, const char *name, uint8_t key,
2586 const uint8_t *mask, bool verbose)
2588 bool mask_empty = mask && !*mask;
2590 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2591 if (verbose || !mask_empty) {
2592 bool mask_full = !mask || *mask == UINT8_MAX;
2594 if (!mask_full) { /* Partially masked. */
2595 ds_put_format(ds, "error: partial mask not supported for frag (%#"
2598 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
2604 mask_empty(const struct nlattr *ma)
2612 mask = nl_attr_get(ma);
2613 n = nl_attr_get_size(ma);
2615 return is_all_zeros(mask, n);
2619 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
2620 const struct hmap *portno_names, struct ds *ds,
2623 enum ovs_key_attr attr = nl_attr_type(a);
2624 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2627 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
2629 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
2631 if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
2632 OVS_KEY_ATTR_MAX, false)) {
2636 ds_put_char(ds, '(');
2638 case OVS_KEY_ATTR_ENCAP:
2639 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
2640 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
2641 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
2643 } else if (nl_attr_get_size(a)) {
2644 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
2649 case OVS_KEY_ATTR_PRIORITY:
2650 case OVS_KEY_ATTR_SKB_MARK:
2651 case OVS_KEY_ATTR_DP_HASH:
2652 case OVS_KEY_ATTR_RECIRC_ID:
2653 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2655 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2659 case OVS_KEY_ATTR_CT_MARK:
2660 if (verbose || !mask_empty(ma)) {
2661 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2663 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2668 case OVS_KEY_ATTR_CT_STATE:
2670 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
2672 ds_put_format(ds, "/%#"PRIx32,
2673 mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
2675 } else if (!is_exact) {
2676 format_flags_masked(ds, NULL, odp_ct_state_to_string,
2678 mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
2681 format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
2685 case OVS_KEY_ATTR_CT_ZONE:
2686 if (verbose || !mask_empty(ma)) {
2687 ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
2689 ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
2694 case OVS_KEY_ATTR_CT_LABELS: {
2695 const ovs_u128 *value = nl_attr_get(a);
2696 const ovs_u128 *mask = ma ? nl_attr_get(ma) : NULL;
2698 format_u128(ds, value, mask, verbose);
2702 case OVS_KEY_ATTR_TUNNEL:
2703 format_odp_tun_attr(a, ma, ds, verbose);
2706 case OVS_KEY_ATTR_IN_PORT:
2707 if (portno_names && verbose && is_exact) {
2708 char *name = odp_portno_names_get(portno_names,
2709 u32_to_odp(nl_attr_get_u32(a)));
2711 ds_put_format(ds, "%s", name);
2713 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2716 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
2718 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
2723 case OVS_KEY_ATTR_ETHERNET: {
2724 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
2725 const struct ovs_key_ethernet *key = nl_attr_get(a);
2727 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
2728 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
2732 case OVS_KEY_ATTR_VLAN:
2733 format_vlan_tci(ds, nl_attr_get_be16(a),
2734 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
2737 case OVS_KEY_ATTR_MPLS: {
2738 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
2739 const struct ovs_key_mpls *mpls_mask = NULL;
2740 size_t size = nl_attr_get_size(a);
2742 if (!size || size % sizeof *mpls_key) {
2743 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
2747 mpls_mask = nl_attr_get(ma);
2748 if (size != nl_attr_get_size(ma)) {
2749 ds_put_format(ds, "(key length %"PRIuSIZE" != "
2750 "mask length %"PRIuSIZE")",
2751 size, nl_attr_get_size(ma));
2755 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
2758 case OVS_KEY_ATTR_ETHERTYPE:
2759 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
2761 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
2765 case OVS_KEY_ATTR_IPV4: {
2766 const struct ovs_key_ipv4 *key = nl_attr_get(a);
2767 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
2769 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
2770 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
2771 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
2773 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
2774 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
2775 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
2780 case OVS_KEY_ATTR_IPV6: {
2781 const struct ovs_key_ipv6 *key = nl_attr_get(a);
2782 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
2784 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
2785 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
2786 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
2788 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
2790 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
2792 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
2794 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
2799 /* These have the same structure and format. */
2800 case OVS_KEY_ATTR_TCP:
2801 case OVS_KEY_ATTR_UDP:
2802 case OVS_KEY_ATTR_SCTP: {
2803 const struct ovs_key_tcp *key = nl_attr_get(a);
2804 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
2806 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
2807 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
2811 case OVS_KEY_ATTR_TCP_FLAGS:
2813 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
2814 ntohs(nl_attr_get_be16(a)),
2815 TCP_FLAGS(nl_attr_get_be16(ma)),
2816 TCP_FLAGS(OVS_BE16_MAX));
2818 format_flags(ds, packet_tcp_flag_to_string,
2819 ntohs(nl_attr_get_be16(a)), '|');
2823 case OVS_KEY_ATTR_ICMP: {
2824 const struct ovs_key_icmp *key = nl_attr_get(a);
2825 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2827 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2828 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2832 case OVS_KEY_ATTR_ICMPV6: {
2833 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2834 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2836 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2838 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2843 case OVS_KEY_ATTR_ARP: {
2844 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2845 const struct ovs_key_arp *key = nl_attr_get(a);
2847 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2848 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2849 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2850 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2851 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2855 case OVS_KEY_ATTR_ND: {
2856 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2857 const struct ovs_key_nd *key = nl_attr_get(a);
2859 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2861 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2862 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2867 case OVS_KEY_ATTR_UNSPEC:
2868 case __OVS_KEY_ATTR_MAX:
2870 format_generic_odp_key(a, ds);
2872 ds_put_char(ds, '/');
2873 format_generic_odp_key(ma, ds);
2877 ds_put_char(ds, ')');
2880 static struct nlattr *
2881 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2882 struct ofpbuf *ofp, const struct nlattr *key)
2884 const struct nlattr *a;
2886 int type = nl_attr_type(key);
2887 int size = nl_attr_get_size(key);
2889 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2890 nl_msg_put_unspec_zero(ofp, type, size);
2894 if (tbl[type].next) {
2895 tbl = tbl[type].next;
2896 max = tbl[type].next_max;
2899 nested_mask = nl_msg_start_nested(ofp, type);
2900 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2901 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2903 nl_msg_end_nested(ofp, nested_mask);
2910 format_u128(struct ds *ds, const ovs_u128 *key, const ovs_u128 *mask,
2913 if (verbose || (mask && !ovs_u128_is_zero(mask))) {
2916 value = hton128(*key);
2917 ds_put_hex(ds, &value, sizeof value);
2918 if (mask && !(ovs_u128_is_ones(mask))) {
2919 value = hton128(*mask);
2920 ds_put_char(ds, '/');
2921 ds_put_hex(ds, &value, sizeof value);
2927 scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
2929 char *s = CONST_CAST(char *, s_);
2933 if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
2934 *value = ntoh128(be_value);
2939 if (ovs_scan(s, "/%n", &n)) {
2943 error = parse_int_string(s, (uint8_t *)&be_mask,
2944 sizeof be_mask, &s);
2948 *mask = ntoh128(be_mask);
2950 *mask = OVS_U128_MAX;
2960 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2964 if (ovs_scan(s, "ufid:")) {
2967 if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
2979 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2981 ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
2984 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2985 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2986 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2987 * non-null and 'verbose' is true, translates odp port number to its name. */
2989 odp_flow_format(const struct nlattr *key, size_t key_len,
2990 const struct nlattr *mask, size_t mask_len,
2991 const struct hmap *portno_names, struct ds *ds, bool verbose)
2994 const struct nlattr *a;
2996 bool has_ethtype_key = false;
2997 const struct nlattr *ma = NULL;
2999 bool first_field = true;
3001 ofpbuf_init(&ofp, 100);
3002 NL_ATTR_FOR_EACH (a, left, key, key_len) {
3003 bool is_nested_attr;
3004 bool is_wildcard = false;
3005 int attr_type = nl_attr_type(a);
3007 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
3008 has_ethtype_key = true;
3011 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
3012 OVS_KEY_ATTR_MAX, attr_type) ==
3015 if (mask && mask_len) {
3016 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
3017 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
3020 if (verbose || !is_wildcard || is_nested_attr) {
3021 if (is_wildcard && !ma) {
3022 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
3027 ds_put_char(ds, ',');
3029 format_odp_key_attr(a, ma, portno_names, ds, verbose);
3030 first_field = false;
3034 ofpbuf_uninit(&ofp);
3039 if (left == key_len) {
3040 ds_put_cstr(ds, "<empty>");
3042 ds_put_format(ds, ",***%u leftover bytes*** (", left);
3043 for (i = 0; i < left; i++) {
3044 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
3046 ds_put_char(ds, ')');
3048 if (!has_ethtype_key) {
3049 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
3051 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
3052 ntohs(nl_attr_get_be16(ma)));
3056 ds_put_cstr(ds, "<empty>");
3060 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3061 * OVS_KEY_ATTR_* attributes in 'key'. */
3063 odp_flow_key_format(const struct nlattr *key,
3064 size_t key_len, struct ds *ds)
3066 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
3070 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
3072 if (!strcasecmp(s, "no")) {
3073 *type = OVS_FRAG_TYPE_NONE;
3074 } else if (!strcasecmp(s, "first")) {
3075 *type = OVS_FRAG_TYPE_FIRST;
3076 } else if (!strcasecmp(s, "later")) {
3077 *type = OVS_FRAG_TYPE_LATER;
3087 scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
3091 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
3092 ETH_ADDR_SCAN_ARGS(*key), &n)) {
3096 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
3097 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
3100 memset(mask, 0xff, sizeof *mask);
3109 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
3113 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
3117 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
3118 IP_SCAN_ARGS(mask), &n)) {
3121 *mask = OVS_BE32_MAX;
3130 scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
3133 char ipv6_s[IPV6_SCAN_LEN + 1];
3135 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
3136 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
3140 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
3141 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
3144 memset(mask, 0xff, sizeof *mask);
3153 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
3155 return scan_in6_addr(s, key ? (struct in6_addr *) *key : NULL,
3156 mask ? (struct in6_addr *) *mask : NULL);
3160 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3165 if (ovs_scan(s, "%i%n", &key_, &n)
3166 && (key_ & ~IPV6_LABEL_MASK) == 0) {
3171 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
3172 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
3174 *mask = htonl(mask_);
3176 *mask = htonl(IPV6_LABEL_MASK);
3185 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
3189 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
3193 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
3205 scan_u16(const char *s, uint16_t *key, uint16_t *mask)
3209 if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
3213 if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
3225 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
3229 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3233 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3245 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
3247 uint16_t key_, mask_;
3250 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3255 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3257 *mask = htons(mask_);
3259 *mask = OVS_BE16_MAX;
3268 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
3270 uint64_t key_, mask_;
3273 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
3276 *key = htonll(key_);
3278 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
3280 *mask = htonll(mask_);
3282 *mask = OVS_BE64_MAX;
3291 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
3293 uint32_t flags, fmask;
3296 n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
3297 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
3298 if (n >= 0 && s[n] == ')') {
3309 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
3311 uint32_t flags, fmask;
3314 n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
3315 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
3317 *key = htons(flags);
3319 *mask = htons(fmask);
3327 ovs_to_odp_ct_state(uint8_t state)
3331 if (state & CS_NEW) {
3332 odp |= OVS_CS_F_NEW;
3334 if (state & CS_ESTABLISHED) {
3335 odp |= OVS_CS_F_ESTABLISHED;
3337 if (state & CS_RELATED) {
3338 odp |= OVS_CS_F_RELATED;
3340 if (state & CS_INVALID) {
3341 odp |= OVS_CS_F_INVALID;
3343 if (state & CS_REPLY_DIR) {
3344 odp |= OVS_CS_F_REPLY_DIR;
3346 if (state & CS_TRACKED) {
3347 odp |= OVS_CS_F_TRACKED;
3354 odp_to_ovs_ct_state(uint32_t flags)
3358 if (flags & OVS_CS_F_NEW) {
3361 if (flags & OVS_CS_F_ESTABLISHED) {
3362 state |= CS_ESTABLISHED;
3364 if (flags & OVS_CS_F_RELATED) {
3365 state |= CS_RELATED;
3367 if (flags & OVS_CS_F_INVALID) {
3368 state |= CS_INVALID;
3370 if (flags & OVS_CS_F_REPLY_DIR) {
3371 state |= CS_REPLY_DIR;
3373 if (flags & OVS_CS_F_TRACKED) {
3374 state |= CS_TRACKED;
3381 scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
3383 uint32_t flags, fmask;
3386 n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
3387 ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
3388 mask ? &fmask : NULL);
3401 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
3405 enum ovs_frag_type frag_type;
3407 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
3408 && ovs_frag_type_from_string(frag, &frag_type)) {
3421 scan_port(const char *s, uint32_t *key, uint32_t *mask,
3422 const struct simap *port_names)
3426 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
3430 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
3437 } else if (port_names) {
3438 const struct simap_node *node;
3441 len = strcspn(s, ")");
3442 node = simap_find_len(port_names, s, len);
3455 /* Helper for vlan parsing. */
3456 struct ovs_key_vlan__ {
3461 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
3463 const uint16_t mask = ((1U << bits) - 1) << offset;
3465 if (value >> bits) {
3469 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
3474 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
3477 uint16_t key_, mask_;
3480 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
3483 if (set_be16_bf(key, bits, offset, key_)) {
3485 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
3488 if (!set_be16_bf(mask, bits, offset, mask_)) {
3492 *mask |= htons(((1U << bits) - 1) << offset);
3502 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
3504 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
3508 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
3510 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
3514 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
3516 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
3521 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
3523 const uint32_t mask = ((1U << bits) - 1) << offset;
3525 if (value >> bits) {
3529 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
3534 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
3537 uint32_t key_, mask_;
3540 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
3543 if (set_be32_bf(key, bits, offset, key_)) {
3545 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
3548 if (!set_be32_bf(mask, bits, offset, mask_)) {
3552 *mask |= htonl(((1U << bits) - 1) << offset);
3562 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
3564 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
3568 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
3570 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
3574 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
3576 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
3580 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
3582 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
3586 scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
3588 const char *s_base = s;
3589 ovs_be16 id = 0, id_mask = 0;
3590 uint8_t flags = 0, flags_mask = 0;
3592 if (!strncmp(s, "id=", 3)) {
3594 s += scan_be16(s, &id, mask ? &id_mask : NULL);
3600 if (!strncmp(s, "flags=", 6)) {
3602 s += scan_u8(s, &flags, mask ? &flags_mask : NULL);
3605 if (!strncmp(s, "))", 2)) {
3608 *key = (flags << 16) | ntohs(id);
3610 *mask = (flags_mask << 16) | ntohs(id_mask);
3620 scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
3622 const char *s_base = s;
3623 struct geneve_opt *opt = key->d;
3624 struct geneve_opt *opt_mask = mask ? mask->d : NULL;
3625 int len_remain = sizeof key->d;
3627 while (s[0] == '{' && len_remain >= sizeof *opt) {
3631 len_remain -= sizeof *opt;
3633 if (!strncmp(s, "class=", 6)) {
3635 s += scan_be16(s, &opt->opt_class,
3636 mask ? &opt_mask->opt_class : NULL);
3638 memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
3644 if (!strncmp(s, "type=", 5)) {
3646 s += scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
3648 memset(&opt_mask->type, 0, sizeof opt_mask->type);
3654 if (!strncmp(s, "len=", 4)) {
3655 uint8_t opt_len, opt_len_mask;
3657 s += scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
3659 if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
3662 opt->length = opt_len / 4;
3664 opt_mask->length = opt_len_mask;
3668 memset(&opt_mask->type, 0, sizeof opt_mask->type);
3674 if (parse_int_string(s, (uint8_t *)(opt + 1), data_len, (char **)&s)) {
3681 if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
3682 data_len, (char **)&s)) {
3693 opt += 1 + data_len / 4;
3695 opt_mask += 1 + data_len / 4;
3697 len_remain -= data_len;
3702 int len = sizeof key->d - len_remain;
3716 tun_flags_to_attr(struct ofpbuf *a, const void *data_)
3718 const uint16_t *flags = data_;
3720 if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
3721 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3723 if (*flags & FLOW_TNL_F_CSUM) {
3724 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3726 if (*flags & FLOW_TNL_F_OAM) {
3727 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3732 vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
3734 const uint32_t *gbp = data_;
3737 size_t vxlan_opts_ofs;
3739 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3740 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
3741 nl_msg_end_nested(a, vxlan_opts_ofs);
3746 geneve_to_attr(struct ofpbuf *a, const void *data_)
3748 const struct geneve_scan *geneve = data_;
3750 nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
3754 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3756 unsigned long call_fn = (unsigned long)FUNC; \
3758 typedef void (*fn)(struct ofpbuf *, const void *); \
3760 func(BUF, &(DATA)); \
3762 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3766 #define SCAN_IF(NAME) \
3767 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3768 const char *start = s; \
3773 /* Usually no special initialization is needed. */
3774 #define SCAN_BEGIN(NAME, TYPE) \
3777 memset(&skey, 0, sizeof skey); \
3778 memset(&smask, 0, sizeof smask); \
3782 /* Init as fully-masked as mask will not be scanned. */
3783 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3786 memset(&skey, 0, sizeof skey); \
3787 memset(&smask, 0xff, sizeof smask); \
3791 /* VLAN needs special initialization. */
3792 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3794 TYPE skey = KEY_INIT; \
3795 TYPE smask = MASK_INIT; \
3799 /* Scan unnamed entry as 'TYPE' */
3800 #define SCAN_TYPE(TYPE, KEY, MASK) \
3801 len = scan_##TYPE(s, KEY, MASK); \
3807 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3808 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3809 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3810 s += strlen(NAME); \
3811 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3815 #define SCAN_FINISH() \
3816 } while (*s++ == ',' && len != 0); \
3817 if (s[-1] != ')') { \
3821 #define SCAN_FINISH_SINGLE() \
3823 if (*s++ != ')') { \
3827 /* Beginning of nested attribute. */
3828 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3830 size_t key_offset, mask_offset; \
3831 key_offset = nl_msg_start_nested(key, ATTR); \
3833 mask_offset = nl_msg_start_nested(mask, ATTR); \
3838 #define SCAN_END_NESTED() \
3840 nl_msg_end_nested(key, key_offset); \
3842 nl_msg_end_nested(mask, mask_offset); \
3847 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3848 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3850 memset(&skey, 0, sizeof skey); \
3851 memset(&smask, 0xff, sizeof smask); \
3852 s += strlen(NAME); \
3853 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3854 SCAN_PUT(ATTR, FUNC); \
3858 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3859 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3861 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3862 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3864 #define SCAN_PUT(ATTR, FUNC) \
3865 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3866 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3868 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3872 #define SCAN_END(ATTR) \
3874 SCAN_PUT(ATTR, NULL); \
3878 #define SCAN_END_SINGLE(ATTR) \
3879 SCAN_FINISH_SINGLE(); \
3880 SCAN_PUT(ATTR, NULL); \
3884 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3885 SCAN_BEGIN(NAME, TYPE) { \
3886 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3887 } SCAN_END_SINGLE(ATTR)
3889 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3890 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3891 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3892 } SCAN_END_SINGLE(ATTR)
3894 /* scan_port needs one extra argument. */
3895 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3896 SCAN_BEGIN(NAME, TYPE) { \
3897 len = scan_port(s, &skey, &smask, port_names); \
3902 } SCAN_END_SINGLE(ATTR)
3905 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
3906 struct ofpbuf *key, struct ofpbuf *mask)
3912 len = odp_ufid_from_string(s, &ufid);
3917 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
3918 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
3919 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
3920 OVS_KEY_ATTR_RECIRC_ID);
3921 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
3923 SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
3924 SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
3925 SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
3926 SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
3928 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
3929 SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
3930 SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
3931 SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
3932 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
3933 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
3934 SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
3935 SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
3936 SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
3937 SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
3938 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
3939 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
3941 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
3942 } SCAN_END_NESTED();
3944 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
3946 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
3947 SCAN_FIELD("src=", eth, eth_src);
3948 SCAN_FIELD("dst=", eth, eth_dst);
3949 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
3951 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
3952 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
3953 SCAN_FIELD("vid=", vid, tci);
3954 SCAN_FIELD("pcp=", pcp, tci);
3955 SCAN_FIELD("cfi=", cfi, tci);
3956 } SCAN_END(OVS_KEY_ATTR_VLAN);
3958 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
3960 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
3961 SCAN_FIELD("label=", mpls_label, mpls_lse);
3962 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
3963 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
3964 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
3965 } SCAN_END(OVS_KEY_ATTR_MPLS);
3967 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
3968 SCAN_FIELD("src=", ipv4, ipv4_src);
3969 SCAN_FIELD("dst=", ipv4, ipv4_dst);
3970 SCAN_FIELD("proto=", u8, ipv4_proto);
3971 SCAN_FIELD("tos=", u8, ipv4_tos);
3972 SCAN_FIELD("ttl=", u8, ipv4_ttl);
3973 SCAN_FIELD("frag=", frag, ipv4_frag);
3974 } SCAN_END(OVS_KEY_ATTR_IPV4);
3976 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
3977 SCAN_FIELD("src=", ipv6, ipv6_src);
3978 SCAN_FIELD("dst=", ipv6, ipv6_dst);
3979 SCAN_FIELD("label=", ipv6_label, ipv6_label);
3980 SCAN_FIELD("proto=", u8, ipv6_proto);
3981 SCAN_FIELD("tclass=", u8, ipv6_tclass);
3982 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
3983 SCAN_FIELD("frag=", frag, ipv6_frag);
3984 } SCAN_END(OVS_KEY_ATTR_IPV6);
3986 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
3987 SCAN_FIELD("src=", be16, tcp_src);
3988 SCAN_FIELD("dst=", be16, tcp_dst);
3989 } SCAN_END(OVS_KEY_ATTR_TCP);
3991 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
3993 SCAN_BEGIN("udp(", struct ovs_key_udp) {
3994 SCAN_FIELD("src=", be16, udp_src);
3995 SCAN_FIELD("dst=", be16, udp_dst);
3996 } SCAN_END(OVS_KEY_ATTR_UDP);
3998 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
3999 SCAN_FIELD("src=", be16, sctp_src);
4000 SCAN_FIELD("dst=", be16, sctp_dst);
4001 } SCAN_END(OVS_KEY_ATTR_SCTP);
4003 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
4004 SCAN_FIELD("type=", u8, icmp_type);
4005 SCAN_FIELD("code=", u8, icmp_code);
4006 } SCAN_END(OVS_KEY_ATTR_ICMP);
4008 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
4009 SCAN_FIELD("type=", u8, icmpv6_type);
4010 SCAN_FIELD("code=", u8, icmpv6_code);
4011 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
4013 SCAN_BEGIN("arp(", struct ovs_key_arp) {
4014 SCAN_FIELD("sip=", ipv4, arp_sip);
4015 SCAN_FIELD("tip=", ipv4, arp_tip);
4016 SCAN_FIELD("op=", be16, arp_op);
4017 SCAN_FIELD("sha=", eth, arp_sha);
4018 SCAN_FIELD("tha=", eth, arp_tha);
4019 } SCAN_END(OVS_KEY_ATTR_ARP);
4021 SCAN_BEGIN("nd(", struct ovs_key_nd) {
4022 SCAN_FIELD("target=", ipv6, nd_target);
4023 SCAN_FIELD("sll=", eth, nd_sll);
4024 SCAN_FIELD("tll=", eth, nd_tll);
4025 } SCAN_END(OVS_KEY_ATTR_ND);
4027 /* Encap open-coded. */
4028 if (!strncmp(s, "encap(", 6)) {
4029 const char *start = s;
4030 size_t encap, encap_mask = 0;
4032 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
4034 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
4041 s += strspn(s, delimiters);
4044 } else if (*s == ')') {
4048 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4056 nl_msg_end_nested(key, encap);
4058 nl_msg_end_nested(mask, encap_mask);
4067 /* Parses the string representation of a datapath flow key, in the
4068 * format output by odp_flow_key_format(). Returns 0 if successful,
4069 * otherwise a positive errno value. On success, the flow key is
4070 * appended to 'key' as a series of Netlink attributes. On failure, no
4071 * data is appended to 'key'. Either way, 'key''s data might be
4074 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4075 * to a port number. (Port names may be used instead of port numbers in
4078 * On success, the attributes appended to 'key' are individually syntactically
4079 * valid, but they may not be valid as a sequence. 'key' might, for example,
4080 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4082 odp_flow_from_string(const char *s, const struct simap *port_names,
4083 struct ofpbuf *key, struct ofpbuf *mask)
4085 const size_t old_size = key->size;
4089 s += strspn(s, delimiters);
4094 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
4096 key->size = old_size;
4106 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
4109 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4110 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4111 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4112 * must use a zero mask for the netlink frag field, and all ones mask
4114 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
4116 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
4117 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
4118 : OVS_FRAG_TYPE_FIRST;
4121 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
4122 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
4123 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
4125 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
4127 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
4129 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
4131 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
4132 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
4133 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
4134 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
4136 /* These share the same layout. */
4138 struct ovs_key_tcp tcp;
4139 struct ovs_key_udp udp;
4140 struct ovs_key_sctp sctp;
4143 static void get_tp_key(const struct flow *, union ovs_key_tp *);
4144 static void put_tp_key(const union ovs_key_tp *, struct flow *);
4147 odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
4148 bool export_mask, struct ofpbuf *buf)
4150 struct ovs_key_ethernet *eth_key;
4152 const struct flow *flow = parms->flow;
4153 const struct flow *data = export_mask ? parms->mask : parms->flow;
4155 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
4157 if (flow_tnl_dst_is_set(&flow->tunnel) || export_mask) {
4158 tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
4162 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
4164 if (parms->support.ct_state) {
4165 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4166 ovs_to_odp_ct_state(data->ct_state));
4168 if (parms->support.ct_zone) {
4169 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
4171 if (parms->support.ct_mark) {
4172 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
4174 if (parms->support.ct_label) {
4175 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
4176 sizeof(data->ct_label));
4178 if (parms->support.recirc) {
4179 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
4180 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
4183 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
4184 * is not the magical value "ODPP_NONE". */
4185 if (export_mask || parms->odp_in_port != ODPP_NONE) {
4186 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, parms->odp_in_port);
4189 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
4191 get_ethernet_key(data, eth_key);
4193 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
4195 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4197 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
4199 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
4200 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
4201 if (flow->vlan_tci == htons(0)) {
4208 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4209 /* For backwards compatibility with kernels that don't support
4210 * wildcarding, the following convention is used to encode the
4211 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4214 * -------- -------- -------
4215 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4216 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4217 * <none> 0xffff Any non-Ethernet II frame (except valid
4218 * 802.3 SNAP packet with valid eth_type).
4221 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
4226 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
4228 if (flow->dl_type == htons(ETH_TYPE_IP)) {
4229 struct ovs_key_ipv4 *ipv4_key;
4231 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
4233 get_ipv4_key(data, ipv4_key, export_mask);
4234 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
4235 struct ovs_key_ipv6 *ipv6_key;
4237 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
4239 get_ipv6_key(data, ipv6_key, export_mask);
4240 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
4241 flow->dl_type == htons(ETH_TYPE_RARP)) {
4242 struct ovs_key_arp *arp_key;
4244 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
4246 get_arp_key(data, arp_key);
4247 } else if (eth_type_mpls(flow->dl_type)) {
4248 struct ovs_key_mpls *mpls_key;
4251 n = flow_count_mpls_labels(flow, NULL);
4253 n = MIN(n, parms->support.max_mpls_depth);
4255 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
4256 n * sizeof *mpls_key);
4257 for (i = 0; i < n; i++) {
4258 mpls_key[i].mpls_lse = data->mpls_lse[i];
4262 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4263 if (flow->nw_proto == IPPROTO_TCP) {
4264 union ovs_key_tp *tcp_key;
4266 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
4268 get_tp_key(data, tcp_key);
4269 if (data->tcp_flags) {
4270 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
4272 } else if (flow->nw_proto == IPPROTO_UDP) {
4273 union ovs_key_tp *udp_key;
4275 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
4277 get_tp_key(data, udp_key);
4278 } else if (flow->nw_proto == IPPROTO_SCTP) {
4279 union ovs_key_tp *sctp_key;
4281 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
4283 get_tp_key(data, sctp_key);
4284 } else if (flow->dl_type == htons(ETH_TYPE_IP)
4285 && flow->nw_proto == IPPROTO_ICMP) {
4286 struct ovs_key_icmp *icmp_key;
4288 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
4290 icmp_key->icmp_type = ntohs(data->tp_src);
4291 icmp_key->icmp_code = ntohs(data->tp_dst);
4292 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
4293 && flow->nw_proto == IPPROTO_ICMPV6) {
4294 struct ovs_key_icmpv6 *icmpv6_key;
4296 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
4297 sizeof *icmpv6_key);
4298 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
4299 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
4301 if (flow->tp_dst == htons(0)
4302 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
4303 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
4304 && (!export_mask || (data->tp_src == htons(0xffff)
4305 && data->tp_dst == htons(0xffff)))) {
4307 struct ovs_key_nd *nd_key;
4309 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
4311 memcpy(nd_key->nd_target, &data->nd_target,
4312 sizeof nd_key->nd_target);
4313 nd_key->nd_sll = data->arp_sha;
4314 nd_key->nd_tll = data->arp_tha;
4321 nl_msg_end_nested(buf, encap);
4325 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4327 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4328 * capable of being expanded to allow for that much space. */
4330 odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
4333 odp_flow_key_from_flow__(parms, false, buf);
4336 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4339 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4340 * capable of being expanded to allow for that much space. */
4342 odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
4345 odp_flow_key_from_flow__(parms, true, buf);
4348 /* Generate ODP flow key from the given packet metadata */
4350 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
4352 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
4354 if (flow_tnl_dst_is_set(&md->tunnel)) {
4355 tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL);
4358 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
4361 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
4362 ovs_to_odp_ct_state(md->ct_state));
4364 nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
4367 nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
4369 if (!ovs_u128_is_zero(&md->ct_label)) {
4370 nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
4371 sizeof(md->ct_label));
4375 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4376 * value "ODPP_NONE". */
4377 if (md->in_port.odp_port != ODPP_NONE) {
4378 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
4382 /* Generate packet metadata from the given ODP flow key. */
4384 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
4385 struct pkt_metadata *md)
4387 const struct nlattr *nla;
4389 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
4390 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
4391 1u << OVS_KEY_ATTR_IN_PORT;
4393 pkt_metadata_init(md, ODPP_NONE);
4395 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
4396 uint16_t type = nl_attr_type(nla);
4397 size_t len = nl_attr_get_size(nla);
4398 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
4399 OVS_KEY_ATTR_MAX, type);
4401 if (len != expected_len && expected_len >= 0) {
4406 case OVS_KEY_ATTR_RECIRC_ID:
4407 md->recirc_id = nl_attr_get_u32(nla);
4408 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
4410 case OVS_KEY_ATTR_DP_HASH:
4411 md->dp_hash = nl_attr_get_u32(nla);
4412 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
4414 case OVS_KEY_ATTR_PRIORITY:
4415 md->skb_priority = nl_attr_get_u32(nla);
4416 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
4418 case OVS_KEY_ATTR_SKB_MARK:
4419 md->pkt_mark = nl_attr_get_u32(nla);
4420 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
4422 case OVS_KEY_ATTR_CT_STATE:
4423 md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
4424 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_STATE);
4426 case OVS_KEY_ATTR_CT_ZONE:
4427 md->ct_zone = nl_attr_get_u16(nla);
4428 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_ZONE);
4430 case OVS_KEY_ATTR_CT_MARK:
4431 md->ct_mark = nl_attr_get_u32(nla);
4432 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_MARK);
4434 case OVS_KEY_ATTR_CT_LABELS: {
4435 const ovs_u128 *cl = nl_attr_get(nla);
4438 wanted_attrs &= ~(1u << OVS_KEY_ATTR_CT_LABELS);
4441 case OVS_KEY_ATTR_TUNNEL: {
4442 enum odp_key_fitness res;
4444 res = odp_tun_key_from_attr(nla, true, &md->tunnel);
4445 if (res == ODP_FIT_ERROR) {
4446 memset(&md->tunnel, 0, sizeof md->tunnel);
4447 } else if (res == ODP_FIT_PERFECT) {
4448 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
4452 case OVS_KEY_ATTR_IN_PORT:
4453 md->in_port.odp_port = nl_attr_get_odp_port(nla);
4454 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
4460 if (!wanted_attrs) {
4461 return; /* Have everything. */
4467 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
4469 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
4470 return hash_words(ALIGNED_CAST(const uint32_t *, key),
4471 key_len / sizeof(uint32_t), 0);
4475 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
4476 uint64_t attrs, int out_of_range_attr,
4477 const struct nlattr *key, size_t key_len)
4482 if (VLOG_DROP_DBG(rl)) {
4487 for (i = 0; i < 64; i++) {
4488 if (attrs & (UINT64_C(1) << i)) {
4489 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4491 ds_put_format(&s, " %s",
4492 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
4495 if (out_of_range_attr) {
4496 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
4499 ds_put_cstr(&s, ": ");
4500 odp_flow_key_format(key, key_len, &s);
4502 VLOG_DBG("%s:%s", title, ds_cstr(&s));
4507 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
4509 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4512 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
4515 if (odp_frag > OVS_FRAG_TYPE_LATER) {
4516 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
4517 return 0xff; /* Error. */
4520 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
4521 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
4522 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
4526 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
4527 const struct nlattr *attrs[], uint64_t *present_attrsp,
4528 int *out_of_range_attrp)
4530 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4531 const struct nlattr *nla;
4532 uint64_t present_attrs;
4535 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
4537 *out_of_range_attrp = 0;
4538 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
4539 uint16_t type = nl_attr_type(nla);
4540 size_t len = nl_attr_get_size(nla);
4541 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
4542 OVS_KEY_ATTR_MAX, type);
4544 if (len != expected_len && expected_len >= 0) {
4545 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4547 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
4548 "length %d", ovs_key_attr_to_string(type, namebuf,
4554 if (type > OVS_KEY_ATTR_MAX) {
4555 *out_of_range_attrp = type;
4557 if (present_attrs & (UINT64_C(1) << type)) {
4558 char namebuf[OVS_KEY_ATTR_BUFSIZE];
4560 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
4561 ovs_key_attr_to_string(type,
4562 namebuf, sizeof namebuf));
4566 present_attrs |= UINT64_C(1) << type;
4571 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
4575 *present_attrsp = present_attrs;
4579 static enum odp_key_fitness
4580 check_expectations(uint64_t present_attrs, int out_of_range_attr,
4581 uint64_t expected_attrs,
4582 const struct nlattr *key, size_t key_len)
4584 uint64_t missing_attrs;
4585 uint64_t extra_attrs;
4587 missing_attrs = expected_attrs & ~present_attrs;
4588 if (missing_attrs) {
4589 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4590 log_odp_key_attributes(&rl, "expected but not present",
4591 missing_attrs, 0, key, key_len);
4592 return ODP_FIT_TOO_LITTLE;
4595 extra_attrs = present_attrs & ~expected_attrs;
4596 if (extra_attrs || out_of_range_attr) {
4597 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
4598 log_odp_key_attributes(&rl, "present but not expected",
4599 extra_attrs, out_of_range_attr, key, key_len);
4600 return ODP_FIT_TOO_MUCH;
4603 return ODP_FIT_PERFECT;
4607 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4608 uint64_t present_attrs, uint64_t *expected_attrs,
4609 struct flow *flow, const struct flow *src_flow)
4611 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4612 bool is_mask = flow != src_flow;
4614 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
4615 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
4616 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
4617 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
4618 ntohs(flow->dl_type));
4621 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
4622 flow->dl_type != htons(0xffff)) {
4625 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
4628 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
4629 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
4630 /* See comments in odp_flow_key_from_flow__(). */
4631 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
4638 static enum odp_key_fitness
4639 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4640 uint64_t present_attrs, int out_of_range_attr,
4641 uint64_t expected_attrs, struct flow *flow,
4642 const struct nlattr *key, size_t key_len,
4643 const struct flow *src_flow)
4645 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4646 bool is_mask = src_flow != flow;
4647 const void *check_start = NULL;
4648 size_t check_len = 0;
4649 enum ovs_key_attr expected_bit = 0xff;
4651 if (eth_type_mpls(src_flow->dl_type)) {
4652 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
4653 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
4655 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
4656 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
4657 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
4658 int n = size / sizeof(ovs_be32);
4661 if (!size || size % sizeof(ovs_be32)) {
4662 return ODP_FIT_ERROR;
4664 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
4665 return ODP_FIT_ERROR;
4668 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
4669 flow->mpls_lse[i] = mpls_lse[i];
4671 if (n > FLOW_MAX_MPLS_LABELS) {
4672 return ODP_FIT_TOO_MUCH;
4676 /* BOS may be set only in the innermost label. */
4677 for (i = 0; i < n - 1; i++) {
4678 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
4679 return ODP_FIT_ERROR;
4683 /* BOS must be set in the innermost label. */
4684 if (n < FLOW_MAX_MPLS_LABELS
4685 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
4686 return ODP_FIT_TOO_LITTLE;
4692 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
4694 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
4696 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
4697 const struct ovs_key_ipv4 *ipv4_key;
4699 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
4700 put_ipv4_key(ipv4_key, flow, is_mask);
4701 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
4702 return ODP_FIT_ERROR;
4705 check_start = ipv4_key;
4706 check_len = sizeof *ipv4_key;
4707 expected_bit = OVS_KEY_ATTR_IPV4;
4710 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
4712 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
4714 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
4715 const struct ovs_key_ipv6 *ipv6_key;
4717 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
4718 put_ipv6_key(ipv6_key, flow, is_mask);
4719 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
4720 return ODP_FIT_ERROR;
4723 check_start = ipv6_key;
4724 check_len = sizeof *ipv6_key;
4725 expected_bit = OVS_KEY_ATTR_IPV6;
4728 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
4729 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
4731 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
4733 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
4734 const struct ovs_key_arp *arp_key;
4736 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
4737 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
4738 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
4739 "key", ntohs(arp_key->arp_op));
4740 return ODP_FIT_ERROR;
4742 put_arp_key(arp_key, flow);
4744 check_start = arp_key;
4745 check_len = sizeof *arp_key;
4746 expected_bit = OVS_KEY_ATTR_ARP;
4752 if (check_len > 0) { /* Happens only when 'is_mask'. */
4753 if (!is_all_zeros(check_start, check_len) &&
4754 flow->dl_type != htons(0xffff)) {
4755 return ODP_FIT_ERROR;
4757 expected_attrs |= UINT64_C(1) << expected_bit;
4761 expected_bit = OVS_KEY_ATTR_UNSPEC;
4762 if (src_flow->nw_proto == IPPROTO_TCP
4763 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4764 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4765 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4767 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
4769 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
4770 const union ovs_key_tp *tcp_key;
4772 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
4773 put_tp_key(tcp_key, flow);
4774 expected_bit = OVS_KEY_ATTR_TCP;
4776 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
4777 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
4778 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
4780 } else if (src_flow->nw_proto == IPPROTO_UDP
4781 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4782 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4783 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4785 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
4787 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
4788 const union ovs_key_tp *udp_key;
4790 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
4791 put_tp_key(udp_key, flow);
4792 expected_bit = OVS_KEY_ATTR_UDP;
4794 } else if (src_flow->nw_proto == IPPROTO_SCTP
4795 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
4796 src_flow->dl_type == htons(ETH_TYPE_IPV6))
4797 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4799 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
4801 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
4802 const union ovs_key_tp *sctp_key;
4804 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
4805 put_tp_key(sctp_key, flow);
4806 expected_bit = OVS_KEY_ATTR_SCTP;
4808 } else if (src_flow->nw_proto == IPPROTO_ICMP
4809 && src_flow->dl_type == htons(ETH_TYPE_IP)
4810 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4812 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
4814 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
4815 const struct ovs_key_icmp *icmp_key;
4817 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
4818 flow->tp_src = htons(icmp_key->icmp_type);
4819 flow->tp_dst = htons(icmp_key->icmp_code);
4820 expected_bit = OVS_KEY_ATTR_ICMP;
4822 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
4823 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
4824 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
4826 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
4828 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
4829 const struct ovs_key_icmpv6 *icmpv6_key;
4831 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
4832 flow->tp_src = htons(icmpv6_key->icmpv6_type);
4833 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
4834 expected_bit = OVS_KEY_ATTR_ICMPV6;
4835 if (src_flow->tp_dst == htons(0) &&
4836 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
4837 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
4839 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4841 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
4842 const struct ovs_key_nd *nd_key;
4844 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
4845 memcpy(&flow->nd_target, nd_key->nd_target,
4846 sizeof flow->nd_target);
4847 flow->arp_sha = nd_key->nd_sll;
4848 flow->arp_tha = nd_key->nd_tll;
4850 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
4851 (flow->tp_src != htons(0xffff) ||
4852 flow->tp_dst != htons(0xffff))) {
4853 return ODP_FIT_ERROR;
4855 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
4862 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
4863 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
4864 return ODP_FIT_ERROR;
4866 expected_attrs |= UINT64_C(1) << expected_bit;
4871 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
4875 /* Parse 802.1Q header then encapsulated L3 attributes. */
4876 static enum odp_key_fitness
4877 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
4878 uint64_t present_attrs, int out_of_range_attr,
4879 uint64_t expected_attrs, struct flow *flow,
4880 const struct nlattr *key, size_t key_len,
4881 const struct flow *src_flow)
4883 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4884 bool is_mask = src_flow != flow;
4886 const struct nlattr *encap
4887 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
4888 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
4889 enum odp_key_fitness encap_fitness;
4890 enum odp_key_fitness fitness;
4892 /* Calculate fitness of outer attributes. */
4894 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
4895 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
4897 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
4898 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
4900 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
4901 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
4904 fitness = check_expectations(present_attrs, out_of_range_attr,
4905 expected_attrs, key, key_len);
4908 * Remove the TPID from dl_type since it's not the real Ethertype. */
4909 flow->dl_type = htons(0);
4910 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
4911 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
4914 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
4915 return ODP_FIT_TOO_LITTLE;
4916 } else if (flow->vlan_tci == htons(0)) {
4917 /* Corner case for a truncated 802.1Q header. */
4918 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
4919 return ODP_FIT_TOO_MUCH;
4922 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4923 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
4924 "but CFI bit is not set", ntohs(flow->vlan_tci));
4925 return ODP_FIT_ERROR;
4928 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
4933 /* Now parse the encapsulated attributes. */
4934 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
4935 attrs, &present_attrs, &out_of_range_attr)) {
4936 return ODP_FIT_ERROR;
4940 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
4941 return ODP_FIT_ERROR;
4943 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
4944 expected_attrs, flow, key, key_len,
4947 /* The overall fitness is the worse of the outer and inner attributes. */
4948 return MAX(fitness, encap_fitness);
4951 static enum odp_key_fitness
4952 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
4953 const struct nlattr *src_key, size_t src_key_len,
4954 struct flow *flow, const struct flow *src_flow,
4957 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
4958 uint64_t expected_attrs;
4959 uint64_t present_attrs;
4960 int out_of_range_attr;
4961 bool is_mask = src_flow != flow;
4963 memset(flow, 0, sizeof *flow);
4965 /* Parse attributes. */
4966 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
4967 &out_of_range_attr)) {
4968 return ODP_FIT_ERROR;
4973 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
4974 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
4975 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
4976 } else if (is_mask) {
4977 /* Always exact match recirc_id if it is not specified. */
4978 flow->recirc_id = UINT32_MAX;
4981 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
4982 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
4983 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
4985 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
4986 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
4987 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
4990 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
4991 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
4992 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
4995 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
4996 uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
4998 flow->ct_state = odp_to_ovs_ct_state(odp_state);
4999 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
5001 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
5002 flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
5003 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
5005 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
5006 flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
5007 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
5009 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
5010 const ovs_u128 *cl = nl_attr_get(attrs[OVS_KEY_ATTR_CT_LABELS]);
5012 flow->ct_label = *cl;
5013 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
5016 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
5017 enum odp_key_fitness res;
5019 res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL],
5020 is_mask ? src_key : NULL,
5021 src_key_len, &src_flow->tunnel,
5022 &flow->tunnel, udpif);
5023 if (res == ODP_FIT_ERROR) {
5024 return ODP_FIT_ERROR;
5025 } else if (res == ODP_FIT_PERFECT) {
5026 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
5030 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
5031 flow->in_port.odp_port
5032 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
5033 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
5034 } else if (!is_mask) {
5035 flow->in_port.odp_port = ODPP_NONE;
5038 /* Ethernet header. */
5039 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
5040 const struct ovs_key_ethernet *eth_key;
5042 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
5043 put_ethernet_key(eth_key, flow);
5045 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5049 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
5052 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5053 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
5055 return ODP_FIT_ERROR;
5059 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
5060 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
5061 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
5062 expected_attrs, flow, key, key_len, src_flow);
5065 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5066 flow->vlan_tci = htons(0xffff);
5067 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
5068 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
5069 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
5072 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
5073 expected_attrs, flow, key, key_len, src_flow);
5076 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5077 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5078 * 'key' fits our expectations for what a flow key should contain.
5080 * The 'in_port' will be the datapath's understanding of the port. The
5081 * caller will need to translate with odp_port_to_ofp_port() if the
5082 * OpenFlow port is needed.
5084 * This function doesn't take the packet itself as an argument because none of
5085 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5086 * it is always possible to infer which additional attribute(s) should appear
5087 * by looking at the attributes for lower-level protocols, e.g. if the network
5088 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5089 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5090 * must be absent. */
5091 enum odp_key_fitness
5092 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
5095 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, false);
5098 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5099 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5100 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5101 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5102 * well 'key' fits our expectations for what a flow key should contain. */
5103 enum odp_key_fitness
5104 odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
5105 const struct nlattr *flow_key, size_t flow_key_len,
5106 struct flow *mask, const struct flow *flow)
5108 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
5112 /* These functions are similar to their non-"_udpif" variants but output a
5113 * 'flow' that is suitable for fast-path packet processing.
5115 * Some fields have different representation for flow setup and per-
5116 * packet processing (i.e. different between ofproto-dpif and userspace
5117 * datapath). In particular, with the non-"_udpif" functions, struct
5118 * tun_metadata is in the per-flow format (using 'present.map' and 'opts.u8');
5119 * with these functions, struct tun_metadata is in the per-packet format
5120 * (using 'present.len' and 'opts.gnv'). */
5121 enum odp_key_fitness
5122 odp_flow_key_to_flow_udpif(const struct nlattr *key, size_t key_len,
5125 return odp_flow_key_to_flow__(key, key_len, NULL, 0, flow, flow, true);
5128 enum odp_key_fitness
5129 odp_flow_key_to_mask_udpif(const struct nlattr *mask_key, size_t mask_key_len,
5130 const struct nlattr *flow_key, size_t flow_key_len,
5131 struct flow *mask, const struct flow *flow)
5133 return odp_flow_key_to_flow__(mask_key, mask_key_len, flow_key, flow_key_len,
5137 /* Returns 'fitness' as a string, for use in debug messages. */
5139 odp_key_fitness_to_string(enum odp_key_fitness fitness)
5142 case ODP_FIT_PERFECT:
5144 case ODP_FIT_TOO_MUCH:
5146 case ODP_FIT_TOO_LITTLE:
5147 return "too_little";
5155 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5156 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5157 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5158 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5159 * null, then the return value is not meaningful.) */
5161 odp_put_userspace_action(uint32_t pid,
5162 const void *userdata, size_t userdata_size,
5163 odp_port_t tunnel_out_port,
5164 bool include_actions,
5165 struct ofpbuf *odp_actions)
5167 size_t userdata_ofs;
5170 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
5171 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
5173 userdata_ofs = odp_actions->size + NLA_HDRLEN;
5175 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5176 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5179 * - The kernel rejected shorter userdata with -ERANGE.
5181 * - The kernel silently dropped userdata beyond the first 8 bytes.
5183 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5184 * separately disable features that required more than 8 bytes.) */
5185 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
5186 MAX(8, userdata_size)),
5187 userdata, userdata_size);
5191 if (tunnel_out_port != ODPP_NONE) {
5192 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
5195 if (include_actions) {
5196 nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
5198 nl_msg_end_nested(odp_actions, offset);
5200 return userdata_ofs;
5204 odp_put_tunnel_action(const struct flow_tnl *tunnel,
5205 struct ofpbuf *odp_actions)
5207 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
5208 tun_key_to_attr(odp_actions, tunnel, tunnel, NULL);
5209 nl_msg_end_nested(odp_actions, offset);
5213 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
5214 struct ovs_action_push_tnl *data)
5216 int size = offsetof(struct ovs_action_push_tnl, header);
5218 size += data->header_len;
5219 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
5223 /* The commit_odp_actions() function and its helpers. */
5226 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
5227 const void *key, size_t key_size)
5229 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
5230 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
5231 nl_msg_end_nested(odp_actions, offset);
5234 /* Masked set actions have a mask following the data within the netlink
5235 * attribute. The unmasked bits in the data will be cleared as the data
5236 * is copied to the action. */
5238 commit_masked_set_action(struct ofpbuf *odp_actions,
5239 enum ovs_key_attr key_type,
5240 const void *key_, const void *mask_, size_t key_size)
5242 size_t offset = nl_msg_start_nested(odp_actions,
5243 OVS_ACTION_ATTR_SET_MASKED);
5244 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
5245 const char *key = key_, *mask = mask_;
5247 memcpy(data + key_size, mask, key_size);
5248 /* Clear unmasked bits while copying. */
5249 while (key_size--) {
5250 *data++ = *key++ & *mask++;
5252 nl_msg_end_nested(odp_actions, offset);
5255 /* If any of the flow key data that ODP actions can modify are different in
5256 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5257 * 'odp_actions' that change the flow tunneling information in key from
5258 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5259 * same way. In other words, operates the same as commit_odp_actions(), but
5260 * only on tunneling information. */
5262 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
5263 struct ofpbuf *odp_actions)
5265 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5266 * must have non-zero ipv6_dst. */
5267 if (flow_tnl_dst_is_set(&flow->tunnel)) {
5268 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
5271 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
5272 odp_put_tunnel_action(&base->tunnel, odp_actions);
5277 commit(enum ovs_key_attr attr, bool use_masked_set,
5278 const void *key, void *base, void *mask, size_t size,
5279 struct ofpbuf *odp_actions)
5281 if (memcmp(key, base, size)) {
5282 bool fully_masked = odp_mask_is_exact(attr, mask, size);
5284 if (use_masked_set && !fully_masked) {
5285 commit_masked_set_action(odp_actions, attr, key, mask, size);
5287 if (!fully_masked) {
5288 memset(mask, 0xff, size);
5290 commit_set_action(odp_actions, attr, key, size);
5292 memcpy(base, key, size);
5295 /* Mask bits are set when we have either read or set the corresponding
5296 * values. Masked bits will be exact-matched, no need to set them
5297 * if the value did not actually change. */
5303 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
5305 eth->eth_src = flow->dl_src;
5306 eth->eth_dst = flow->dl_dst;
5310 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
5312 flow->dl_src = eth->eth_src;
5313 flow->dl_dst = eth->eth_dst;
5317 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
5318 struct ofpbuf *odp_actions,
5319 struct flow_wildcards *wc,
5322 struct ovs_key_ethernet key, base, mask;
5324 get_ethernet_key(flow, &key);
5325 get_ethernet_key(base_flow, &base);
5326 get_ethernet_key(&wc->masks, &mask);
5328 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
5329 &key, &base, &mask, sizeof key, odp_actions)) {
5330 put_ethernet_key(&base, base_flow);
5331 put_ethernet_key(&mask, &wc->masks);
5336 pop_vlan(struct flow *base,
5337 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5339 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
5341 if (base->vlan_tci & htons(VLAN_CFI)) {
5342 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
5348 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
5349 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5351 if (base->vlan_tci == vlan_tci) {
5355 pop_vlan(base, odp_actions, wc);
5356 if (vlan_tci & htons(VLAN_CFI)) {
5357 struct ovs_action_push_vlan vlan;
5359 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
5360 vlan.vlan_tci = vlan_tci;
5361 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
5362 &vlan, sizeof vlan);
5364 base->vlan_tci = vlan_tci;
5367 /* Wildcarding already done at action translation time. */
5369 commit_mpls_action(const struct flow *flow, struct flow *base,
5370 struct ofpbuf *odp_actions)
5372 int base_n = flow_count_mpls_labels(base, NULL);
5373 int flow_n = flow_count_mpls_labels(flow, NULL);
5374 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
5377 while (base_n > common_n) {
5378 if (base_n - 1 == common_n && flow_n > common_n) {
5379 /* If there is only one more LSE in base than there are common
5380 * between base and flow; and flow has at least one more LSE than
5381 * is common then the topmost LSE of base may be updated using
5383 struct ovs_key_mpls mpls_key;
5385 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
5386 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
5387 &mpls_key, sizeof mpls_key);
5388 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
5391 /* Otherwise, if there more LSEs in base than are common between
5392 * base and flow then pop the topmost one. */
5396 /* If all the LSEs are to be popped and this is not the outermost
5397 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5398 * POP_MPLS action instead of flow->dl_type.
5400 * This is because the POP_MPLS action requires its ethertype
5401 * argument to be an MPLS ethernet type but in this case
5402 * flow->dl_type will be a non-MPLS ethernet type.
5404 * When the final POP_MPLS action occurs it use flow->dl_type and
5405 * the and the resulting packet will have the desired dl_type. */
5406 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
5407 dl_type = htons(ETH_TYPE_MPLS);
5409 dl_type = flow->dl_type;
5411 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
5412 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
5418 /* If, after the above popping and setting, there are more LSEs in flow
5419 * than base then some LSEs need to be pushed. */
5420 while (base_n < flow_n) {
5421 struct ovs_action_push_mpls *mpls;
5423 mpls = nl_msg_put_unspec_zero(odp_actions,
5424 OVS_ACTION_ATTR_PUSH_MPLS,
5426 mpls->mpls_ethertype = flow->dl_type;
5427 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
5428 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
5429 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
5435 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
5437 ipv4->ipv4_src = flow->nw_src;
5438 ipv4->ipv4_dst = flow->nw_dst;
5439 ipv4->ipv4_proto = flow->nw_proto;
5440 ipv4->ipv4_tos = flow->nw_tos;
5441 ipv4->ipv4_ttl = flow->nw_ttl;
5442 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
5446 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
5448 flow->nw_src = ipv4->ipv4_src;
5449 flow->nw_dst = ipv4->ipv4_dst;
5450 flow->nw_proto = ipv4->ipv4_proto;
5451 flow->nw_tos = ipv4->ipv4_tos;
5452 flow->nw_ttl = ipv4->ipv4_ttl;
5453 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
5457 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
5458 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5461 struct ovs_key_ipv4 key, mask, base;
5463 /* Check that nw_proto and nw_frag remain unchanged. */
5464 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
5465 flow->nw_frag == base_flow->nw_frag);
5467 get_ipv4_key(flow, &key, false);
5468 get_ipv4_key(base_flow, &base, false);
5469 get_ipv4_key(&wc->masks, &mask, true);
5470 mask.ipv4_proto = 0; /* Not writeable. */
5471 mask.ipv4_frag = 0; /* Not writable. */
5473 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
5475 put_ipv4_key(&base, base_flow, false);
5476 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
5477 put_ipv4_key(&mask, &wc->masks, true);
5483 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
5485 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
5486 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
5487 ipv6->ipv6_label = flow->ipv6_label;
5488 ipv6->ipv6_proto = flow->nw_proto;
5489 ipv6->ipv6_tclass = flow->nw_tos;
5490 ipv6->ipv6_hlimit = flow->nw_ttl;
5491 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
5495 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
5497 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
5498 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
5499 flow->ipv6_label = ipv6->ipv6_label;
5500 flow->nw_proto = ipv6->ipv6_proto;
5501 flow->nw_tos = ipv6->ipv6_tclass;
5502 flow->nw_ttl = ipv6->ipv6_hlimit;
5503 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
5507 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
5508 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5511 struct ovs_key_ipv6 key, mask, base;
5513 /* Check that nw_proto and nw_frag remain unchanged. */
5514 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
5515 flow->nw_frag == base_flow->nw_frag);
5517 get_ipv6_key(flow, &key, false);
5518 get_ipv6_key(base_flow, &base, false);
5519 get_ipv6_key(&wc->masks, &mask, true);
5520 mask.ipv6_proto = 0; /* Not writeable. */
5521 mask.ipv6_frag = 0; /* Not writable. */
5523 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
5525 put_ipv6_key(&base, base_flow, false);
5526 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
5527 put_ipv6_key(&mask, &wc->masks, true);
5533 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
5535 /* ARP key has padding, clear it. */
5536 memset(arp, 0, sizeof *arp);
5538 arp->arp_sip = flow->nw_src;
5539 arp->arp_tip = flow->nw_dst;
5540 arp->arp_op = htons(flow->nw_proto);
5541 arp->arp_sha = flow->arp_sha;
5542 arp->arp_tha = flow->arp_tha;
5546 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
5548 flow->nw_src = arp->arp_sip;
5549 flow->nw_dst = arp->arp_tip;
5550 flow->nw_proto = ntohs(arp->arp_op);
5551 flow->arp_sha = arp->arp_sha;
5552 flow->arp_tha = arp->arp_tha;
5555 static enum slow_path_reason
5556 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
5557 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5559 struct ovs_key_arp key, mask, base;
5561 get_arp_key(flow, &key);
5562 get_arp_key(base_flow, &base);
5563 get_arp_key(&wc->masks, &mask);
5565 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
5567 put_arp_key(&base, base_flow);
5568 put_arp_key(&mask, &wc->masks);
5575 get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
5577 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5578 icmp->icmp_type = ntohs(flow->tp_src);
5579 icmp->icmp_code = ntohs(flow->tp_dst);
5583 put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
5585 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5586 flow->tp_src = htons(icmp->icmp_type);
5587 flow->tp_dst = htons(icmp->icmp_code);
5590 static enum slow_path_reason
5591 commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
5592 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
5594 struct ovs_key_icmp key, mask, base;
5595 enum ovs_key_attr attr;
5597 get_icmp_key(flow, &key);
5598 get_icmp_key(base_flow, &base);
5599 get_icmp_key(&wc->masks, &mask);
5601 attr = flow->dl_type == htons(ETH_TYPE_IP) ? OVS_KEY_ATTR_ICMP
5602 : OVS_KEY_ATTR_ICMPV6;
5603 if (commit(attr, false, &key, &base, &mask, sizeof key, odp_actions)) {
5604 put_icmp_key(&base, base_flow);
5605 put_icmp_key(&mask, &wc->masks);
5612 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
5614 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
5615 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5616 nd->nd_sll = flow->arp_sha;
5617 nd->nd_tll = flow->arp_tha;
5621 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
5623 memcpy(&flow->nd_target, nd->nd_target, sizeof flow->nd_target);
5624 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5625 flow->arp_sha = nd->nd_sll;
5626 flow->arp_tha = nd->nd_tll;
5629 static enum slow_path_reason
5630 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
5631 struct ofpbuf *odp_actions,
5632 struct flow_wildcards *wc, bool use_masked)
5634 struct ovs_key_nd key, mask, base;
5636 get_nd_key(flow, &key);
5637 get_nd_key(base_flow, &base);
5638 get_nd_key(&wc->masks, &mask);
5640 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
5642 put_nd_key(&base, base_flow);
5643 put_nd_key(&mask, &wc->masks);
5650 static enum slow_path_reason
5651 commit_set_nw_action(const struct flow *flow, struct flow *base,
5652 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5655 /* Check if 'flow' really has an L3 header. */
5656 if (!flow->nw_proto) {
5660 switch (ntohs(base->dl_type)) {
5662 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
5666 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
5667 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
5670 return commit_set_arp_action(flow, base, odp_actions, wc);
5676 /* TCP, UDP, and SCTP keys have the same layout. */
5677 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
5678 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
5681 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
5683 tp->tcp.tcp_src = flow->tp_src;
5684 tp->tcp.tcp_dst = flow->tp_dst;
5688 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
5690 flow->tp_src = tp->tcp.tcp_src;
5691 flow->tp_dst = tp->tcp.tcp_dst;
5695 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
5696 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5699 enum ovs_key_attr key_type;
5700 union ovs_key_tp key, mask, base;
5702 /* Check if 'flow' really has an L3 header. */
5703 if (!flow->nw_proto) {
5707 if (!is_ip_any(base_flow)) {
5711 if (flow->nw_proto == IPPROTO_TCP) {
5712 key_type = OVS_KEY_ATTR_TCP;
5713 } else if (flow->nw_proto == IPPROTO_UDP) {
5714 key_type = OVS_KEY_ATTR_UDP;
5715 } else if (flow->nw_proto == IPPROTO_SCTP) {
5716 key_type = OVS_KEY_ATTR_SCTP;
5721 get_tp_key(flow, &key);
5722 get_tp_key(base_flow, &base);
5723 get_tp_key(&wc->masks, &mask);
5725 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
5727 put_tp_key(&base, base_flow);
5728 put_tp_key(&mask, &wc->masks);
5733 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
5734 struct ofpbuf *odp_actions,
5735 struct flow_wildcards *wc,
5738 uint32_t key, mask, base;
5740 key = flow->skb_priority;
5741 base = base_flow->skb_priority;
5742 mask = wc->masks.skb_priority;
5744 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
5745 sizeof key, odp_actions)) {
5746 base_flow->skb_priority = base;
5747 wc->masks.skb_priority = mask;
5752 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
5753 struct ofpbuf *odp_actions,
5754 struct flow_wildcards *wc,
5757 uint32_t key, mask, base;
5759 key = flow->pkt_mark;
5760 base = base_flow->pkt_mark;
5761 mask = wc->masks.pkt_mark;
5763 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
5764 sizeof key, odp_actions)) {
5765 base_flow->pkt_mark = base;
5766 wc->masks.pkt_mark = mask;
5770 /* If any of the flow key data that ODP actions can modify are different in
5771 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5772 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5773 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5774 * in addition to this function if needed. Sets fields in 'wc' that are
5775 * used as part of the action.
5777 * Returns a reason to force processing the flow's packets into the userspace
5778 * slow path, if there is one, otherwise 0. */
5779 enum slow_path_reason
5780 commit_odp_actions(const struct flow *flow, struct flow *base,
5781 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
5784 enum slow_path_reason slow1, slow2;
5786 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
5787 slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
5788 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
5789 slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
5790 commit_mpls_action(flow, base, odp_actions);
5791 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
5792 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
5793 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
5795 return slow1 ? slow1 : slow2;