2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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>
25 #include <netinet/ip6.h>
29 #include "byte-order.h"
32 #include "openvswitch/dynamic-string.h"
35 #include "openvswitch/ofpbuf.h"
39 #include "tun-metadata.h"
40 #include "unaligned.h"
43 #include "openvswitch/vlog.h"
45 VLOG_DEFINE_THIS_MODULE(odp_util
);
47 /* The interface between userspace and kernel uses an "OVS_*" prefix.
48 * Since this is fairly non-specific for the OVS userspace components,
49 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
50 * interactions with the datapath.
53 /* The set of characters that may separate one action or one key attribute
55 static const char *delimiters
= ", \t\r\n";
56 static const char *delimiters_end
= ", \t\r\n)";
60 const struct attr_len_tbl
*next
;
63 #define ATTR_LEN_INVALID -1
64 #define ATTR_LEN_VARIABLE -2
65 #define ATTR_LEN_NESTED -3
67 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
68 struct ofpbuf
*, struct ofpbuf
*);
69 static void format_odp_key_attr(const struct nlattr
*a
,
70 const struct nlattr
*ma
,
71 const struct hmap
*portno_names
, struct ds
*ds
,
75 struct geneve_opt d
[63];
79 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
80 struct geneve_scan
*mask
);
81 static void format_geneve_opts(const struct geneve_opt
*opt
,
82 const struct geneve_opt
*mask
, int opts_len
,
83 struct ds
*, bool verbose
);
85 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
86 int max
, struct ofpbuf
*,
87 const struct nlattr
*key
);
88 static void format_u128(struct ds
*ds
, const ovs_u128
*value
,
89 const ovs_u128
*mask
, bool verbose
);
90 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
92 static int parse_odp_action(const char *s
, const struct simap
*port_names
,
93 struct ofpbuf
*actions
);
95 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
98 * - For an action whose argument has a fixed length, returned that
99 * nonnegative length in bytes.
101 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
103 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
105 odp_action_len(uint16_t type
)
107 if (type
> OVS_ACTION_ATTR_MAX
) {
111 switch ((enum ovs_action_attr
) type
) {
112 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
113 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
114 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
115 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
116 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
117 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
118 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
119 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
120 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
121 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
122 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
123 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
124 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
125 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
126 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
127 case OVS_ACTION_ATTR_CLONE
: return ATTR_LEN_VARIABLE
;
129 case OVS_ACTION_ATTR_UNSPEC
:
130 case __OVS_ACTION_ATTR_MAX
:
131 return ATTR_LEN_INVALID
;
134 return ATTR_LEN_INVALID
;
137 /* Returns a string form of 'attr'. The return value is either a statically
138 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
139 * should be at least OVS_KEY_ATTR_BUFSIZE. */
140 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
142 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
145 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
146 case OVS_KEY_ATTR_ENCAP
: return "encap";
147 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
148 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
149 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
150 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
151 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
152 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
153 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
154 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
155 case OVS_KEY_ATTR_ETHERNET
: return "eth";
156 case OVS_KEY_ATTR_VLAN
: return "vlan";
157 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
158 case OVS_KEY_ATTR_IPV4
: return "ipv4";
159 case OVS_KEY_ATTR_IPV6
: return "ipv6";
160 case OVS_KEY_ATTR_TCP
: return "tcp";
161 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
162 case OVS_KEY_ATTR_UDP
: return "udp";
163 case OVS_KEY_ATTR_SCTP
: return "sctp";
164 case OVS_KEY_ATTR_ICMP
: return "icmp";
165 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
166 case OVS_KEY_ATTR_ARP
: return "arp";
167 case OVS_KEY_ATTR_ND
: return "nd";
168 case OVS_KEY_ATTR_MPLS
: return "mpls";
169 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
170 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
172 case __OVS_KEY_ATTR_MAX
:
174 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
180 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
182 size_t len
= nl_attr_get_size(a
);
184 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
186 const uint8_t *unspec
;
189 unspec
= nl_attr_get(a
);
190 for (i
= 0; i
< len
; i
++) {
191 ds_put_char(ds
, i
? ' ': '(');
192 ds_put_format(ds
, "%02x", unspec
[i
]);
194 ds_put_char(ds
, ')');
199 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
201 static const struct nl_policy ovs_sample_policy
[] = {
202 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
203 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
205 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
207 const struct nlattr
*nla_acts
;
210 ds_put_cstr(ds
, "sample");
212 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
213 ds_put_cstr(ds
, "(error)");
217 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
220 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
222 ds_put_cstr(ds
, "actions(");
223 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
224 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
225 format_odp_actions(ds
, nla_acts
, len
);
226 ds_put_format(ds
, "))");
230 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
)
232 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
233 int len
= nl_attr_get_size(attr
);
235 ds_put_cstr(ds
, "clone");
236 ds_put_format(ds
, "(");
237 format_odp_actions(ds
, nla_acts
, len
);
238 ds_put_format(ds
, ")");
242 slow_path_reason_to_string(uint32_t reason
)
244 switch ((enum slow_path_reason
) reason
) {
245 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
254 slow_path_reason_to_explanation(enum slow_path_reason reason
)
257 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
266 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
267 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
269 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
270 res_flags
, allowed
, res_mask
);
274 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
276 static const struct nl_policy ovs_userspace_policy
[] = {
277 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
278 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
280 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
282 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
285 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
286 const struct nlattr
*userdata_attr
;
287 const struct nlattr
*tunnel_out_port_attr
;
289 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
290 ds_put_cstr(ds
, "userspace(error)");
294 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
295 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
297 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
300 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
301 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
302 bool userdata_unspec
= true;
303 union user_action_cookie cookie
;
305 if (userdata_len
>= sizeof cookie
.type
306 && userdata_len
<= sizeof cookie
) {
308 memset(&cookie
, 0, sizeof cookie
);
309 memcpy(&cookie
, userdata
, userdata_len
);
311 userdata_unspec
= false;
313 if (userdata_len
== sizeof cookie
.sflow
314 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
315 ds_put_format(ds
, ",sFlow("
316 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
317 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
318 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
319 cookie
.sflow
.output
);
320 } else if (userdata_len
== sizeof cookie
.slow_path
321 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
322 ds_put_cstr(ds
, ",slow_path(");
323 format_flags(ds
, slow_path_reason_to_string
,
324 cookie
.slow_path
.reason
, ',');
325 ds_put_format(ds
, ")");
326 } else if (userdata_len
== sizeof cookie
.flow_sample
327 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
328 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
329 ",collector_set_id=%"PRIu32
330 ",obs_domain_id=%"PRIu32
331 ",obs_point_id=%"PRIu32
332 ",output_port=%"PRIu32
,
333 cookie
.flow_sample
.probability
,
334 cookie
.flow_sample
.collector_set_id
,
335 cookie
.flow_sample
.obs_domain_id
,
336 cookie
.flow_sample
.obs_point_id
,
337 cookie
.flow_sample
.output_odp_port
);
338 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
339 ds_put_cstr(ds
, ",ingress");
340 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
341 ds_put_cstr(ds
, ",egress");
343 ds_put_char(ds
, ')');
344 } else if (userdata_len
>= sizeof cookie
.ipfix
345 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
346 ds_put_format(ds
, ",ipfix(output_port=%"PRIu32
")",
347 cookie
.ipfix
.output_odp_port
);
349 userdata_unspec
= true;
353 if (userdata_unspec
) {
355 ds_put_format(ds
, ",userdata(");
356 for (i
= 0; i
< userdata_len
; i
++) {
357 ds_put_format(ds
, "%02x", userdata
[i
]);
359 ds_put_char(ds
, ')');
363 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
364 ds_put_cstr(ds
, ",actions");
367 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
368 if (tunnel_out_port_attr
) {
369 ds_put_format(ds
, ",tunnel_out_port=%"PRIu32
,
370 nl_attr_get_u32(tunnel_out_port_attr
));
373 ds_put_char(ds
, ')');
377 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
379 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
380 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
381 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
382 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
384 ds_put_char(ds
, ',');
386 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
387 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
388 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
389 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
391 ds_put_char(ds
, ',');
393 if (!(tci
& htons(VLAN_CFI
))) {
394 ds_put_cstr(ds
, "cfi=0");
395 ds_put_char(ds
, ',');
401 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
403 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
404 mpls_lse_to_label(mpls_lse
),
405 mpls_lse_to_tc(mpls_lse
),
406 mpls_lse_to_ttl(mpls_lse
),
407 mpls_lse_to_bos(mpls_lse
));
411 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
412 const struct ovs_key_mpls
*mpls_mask
, int n
)
414 for (int i
= 0; i
< n
; i
++) {
415 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
417 if (mpls_mask
== NULL
) {
418 format_mpls_lse(ds
, key
);
420 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
422 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
423 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
424 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
425 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
426 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
428 ds_put_char(ds
, ',');
434 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
436 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
440 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
442 ds_put_format(ds
, "hash(");
444 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
445 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
447 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
450 ds_put_format(ds
, ")");
454 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
456 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
457 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
458 ntohs(udp
->udp_csum
));
464 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
466 const struct eth_header
*eth
;
469 const struct udp_header
*udp
;
471 eth
= (const struct eth_header
*)data
->header
;
476 ds_put_format(ds
, "header(size=%"PRIu8
",type=%"PRIu8
",eth(dst=",
477 data
->header_len
, data
->tnl_type
);
478 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
479 ds_put_format(ds
, ",src=");
480 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
481 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
483 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
485 const struct ip_header
*ip
;
486 ip
= (const struct ip_header
*) l3
;
487 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
488 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
489 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
490 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
491 ip
->ip_proto
, ip
->ip_tos
,
493 ntohs(ip
->ip_frag_off
));
496 const struct ip6_hdr
*ip6
;
497 ip6
= (const struct ip6_hdr
*) l3
;
498 ds_put_format(ds
, "ipv6(src=");
499 ipv6_format_addr(&ip6
->ip6_src
, ds
);
500 ds_put_format(ds
, ",dst=");
501 ipv6_format_addr(&ip6
->ip6_dst
, ds
);
502 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx8
503 ",hlimit=%"PRIu8
"),",
504 ntohl(ip6
->ip6_flow
) & IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
505 (ntohl(ip6
->ip6_flow
) >> 20) & 0xff, ip6
->ip6_hlim
);
509 udp
= (const struct udp_header
*) l4
;
511 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
512 const struct vxlanhdr
*vxh
;
514 vxh
= format_udp_tnl_push_header(ds
, udp
);
516 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
517 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
518 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
519 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
520 const struct genevehdr
*gnh
;
522 gnh
= format_udp_tnl_push_header(ds
, udp
);
524 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
525 gnh
->oam
? "oam," : "",
526 gnh
->critical
? "crit," : "",
527 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
530 ds_put_cstr(ds
, ",options(");
531 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
533 ds_put_char(ds
, ')');
536 ds_put_char(ds
, ')');
537 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
538 const struct gre_base_hdr
*greh
;
539 ovs_16aligned_be32
*options
;
541 greh
= (const struct gre_base_hdr
*) l4
;
543 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
544 ntohs(greh
->flags
), ntohs(greh
->protocol
));
545 options
= (ovs_16aligned_be32
*)(greh
+ 1);
546 if (greh
->flags
& htons(GRE_CSUM
)) {
547 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
550 if (greh
->flags
& htons(GRE_KEY
)) {
551 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
554 if (greh
->flags
& htons(GRE_SEQ
)) {
555 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
558 ds_put_format(ds
, ")");
560 ds_put_format(ds
, ")");
564 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
)
566 struct ovs_action_push_tnl
*data
;
568 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
570 ds_put_format(ds
, "tnl_push(tnl_port(%"PRIu32
"),", data
->tnl_port
);
571 format_odp_tnl_push_header(ds
, data
);
572 ds_put_format(ds
, ",out_port(%"PRIu32
"))", data
->out_port
);
575 static const struct nl_policy ovs_nat_policy
[] = {
576 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
577 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
578 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
579 .min_len
= sizeof(struct in_addr
),
580 .max_len
= sizeof(struct in6_addr
)},
581 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
582 .min_len
= sizeof(struct in_addr
),
583 .max_len
= sizeof(struct in6_addr
)},
584 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
585 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
586 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
587 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
588 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
592 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
594 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
596 ovs_be32 ip_min
, ip_max
;
597 struct in6_addr ip6_min
, ip6_max
;
598 uint16_t proto_min
, proto_max
;
600 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
601 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
604 /* If no type, then nothing else either. */
605 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
606 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
607 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
608 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
609 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
610 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
613 /* Both SNAT & DNAT may not be specified. */
614 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
615 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
618 /* proto may not appear without ip. */
619 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
620 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
623 /* MAX may not appear without MIN. */
624 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
625 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
626 ds_put_cstr(ds
, "nat(error: range max without min.)");
629 /* Address sizes must match. */
630 if ((a
[OVS_NAT_ATTR_IP_MIN
]
631 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
632 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
633 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
634 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
635 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
636 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
640 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
641 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
642 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
643 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
644 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
645 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
646 if (addr_len
== sizeof ip6_min
) {
647 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
648 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
650 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
651 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
654 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
655 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
656 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
657 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
659 if ((addr_len
== sizeof(ovs_be32
)
660 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
661 || (addr_len
== sizeof(struct in6_addr
)
662 && !ipv6_mask_is_any(&ip6_max
)
663 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
664 || (proto_max
&& proto_min
> proto_max
)) {
665 ds_put_cstr(ds
, "nat(range error)");
669 ds_put_cstr(ds
, "nat");
670 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
671 ds_put_char(ds
, '(');
672 if (a
[OVS_NAT_ATTR_SRC
]) {
673 ds_put_cstr(ds
, "src");
674 } else if (a
[OVS_NAT_ATTR_DST
]) {
675 ds_put_cstr(ds
, "dst");
679 ds_put_cstr(ds
, "=");
681 if (addr_len
== sizeof ip_min
) {
682 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
684 if (ip_max
&& ip_max
!= ip_min
) {
685 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
687 } else if (addr_len
== sizeof ip6_min
) {
688 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
690 if (!ipv6_mask_is_any(&ip6_max
) &&
691 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
692 ds_put_char(ds
, '-');
693 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
697 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
699 if (proto_max
&& proto_max
!= proto_min
) {
700 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
704 ds_put_char(ds
, ',');
705 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
706 ds_put_cstr(ds
, "persistent,");
708 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
709 ds_put_cstr(ds
, "hash,");
711 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
712 ds_put_cstr(ds
, "random,");
715 ds_put_char(ds
, ')');
719 static const struct nl_policy ovs_conntrack_policy
[] = {
720 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
721 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
722 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
723 .min_len
= sizeof(uint32_t) * 2 },
724 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
725 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
726 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
727 .min_len
= 1, .max_len
= 16 },
728 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
732 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
734 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
735 const ovs_u128
*label
;
736 const uint32_t *mark
;
740 const struct nlattr
*nat
;
742 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
743 ds_put_cstr(ds
, "ct(error)");
747 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
748 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
749 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
750 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
751 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
752 nat
= a
[OVS_CT_ATTR_NAT
];
754 ds_put_format(ds
, "ct");
755 if (commit
|| zone
|| mark
|| label
|| helper
|| nat
) {
756 ds_put_cstr(ds
, "(");
758 ds_put_format(ds
, "commit,");
761 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
764 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
768 ds_put_format(ds
, "label=");
769 format_u128(ds
, label
, label
+ 1, true);
770 ds_put_char(ds
, ',');
773 ds_put_format(ds
, "helper=%s,", helper
);
776 format_odp_ct_nat(ds
, nat
);
779 ds_put_cstr(ds
, ")");
784 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
787 enum ovs_action_attr type
= nl_attr_type(a
);
790 expected_len
= odp_action_len(nl_attr_type(a
));
791 if (expected_len
!= ATTR_LEN_VARIABLE
&&
792 nl_attr_get_size(a
) != expected_len
) {
793 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
794 nl_attr_get_size(a
), expected_len
);
795 format_generic_odp_action(ds
, a
);
800 case OVS_ACTION_ATTR_OUTPUT
:
801 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
803 case OVS_ACTION_ATTR_TRUNC
: {
804 const struct ovs_action_trunc
*trunc
=
805 nl_attr_get_unspec(a
, sizeof *trunc
);
807 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
811 case OVS_ACTION_ATTR_TUNNEL_POP
:
812 ds_put_format(ds
, "tnl_pop(%"PRIu32
")", nl_attr_get_u32(a
));
814 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
815 format_odp_tnl_push_action(ds
, a
);
817 case OVS_ACTION_ATTR_USERSPACE
:
818 format_odp_userspace_action(ds
, a
);
820 case OVS_ACTION_ATTR_RECIRC
:
821 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
823 case OVS_ACTION_ATTR_HASH
:
824 format_odp_hash_action(ds
, nl_attr_get(a
));
826 case OVS_ACTION_ATTR_SET_MASKED
:
828 size
= nl_attr_get_size(a
) / 2;
829 ds_put_cstr(ds
, "set(");
831 /* Masked set action not supported for tunnel key, which is bigger. */
832 if (size
<= sizeof(struct ovs_key_ipv6
)) {
833 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
834 sizeof(struct nlattr
))];
835 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
836 sizeof(struct nlattr
))];
838 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
839 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
840 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
841 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
842 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
844 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
846 ds_put_cstr(ds
, ")");
848 case OVS_ACTION_ATTR_SET
:
849 ds_put_cstr(ds
, "set(");
850 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
851 ds_put_cstr(ds
, ")");
853 case OVS_ACTION_ATTR_PUSH_VLAN
: {
854 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
855 ds_put_cstr(ds
, "push_vlan(");
856 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
857 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
859 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
860 ds_put_char(ds
, ')');
863 case OVS_ACTION_ATTR_POP_VLAN
:
864 ds_put_cstr(ds
, "pop_vlan");
866 case OVS_ACTION_ATTR_PUSH_MPLS
: {
867 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
868 ds_put_cstr(ds
, "push_mpls(");
869 format_mpls_lse(ds
, mpls
->mpls_lse
);
870 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
873 case OVS_ACTION_ATTR_POP_MPLS
: {
874 ovs_be16 ethertype
= nl_attr_get_be16(a
);
875 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
878 case OVS_ACTION_ATTR_SAMPLE
:
879 format_odp_sample_action(ds
, a
);
881 case OVS_ACTION_ATTR_CT
:
882 format_odp_conntrack_action(ds
, a
);
884 case OVS_ACTION_ATTR_CLONE
:
885 format_odp_clone_action(ds
, a
);
887 case OVS_ACTION_ATTR_UNSPEC
:
888 case __OVS_ACTION_ATTR_MAX
:
890 format_generic_odp_action(ds
, a
);
896 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
900 const struct nlattr
*a
;
903 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
905 ds_put_char(ds
, ',');
907 format_odp_action(ds
, a
);
912 if (left
== actions_len
) {
913 ds_put_cstr(ds
, "<empty>");
915 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
916 for (i
= 0; i
< left
; i
++) {
917 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
919 ds_put_char(ds
, ')');
922 ds_put_cstr(ds
, "drop");
926 /* Separate out parse_odp_userspace_action() function. */
928 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
931 union user_action_cookie cookie
;
933 odp_port_t tunnel_out_port
;
935 void *user_data
= NULL
;
936 size_t user_data_size
= 0;
937 bool include_actions
= false;
940 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
944 ofpbuf_init(&buf
, 16);
948 uint32_t probability
;
949 uint32_t collector_set_id
;
950 uint32_t obs_domain_id
;
951 uint32_t obs_point_id
;
954 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
955 "pcp=%i,output=%"SCNi32
")%n",
956 &vid
, &pcp
, &output
, &n1
)) {
960 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
965 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
966 cookie
.sflow
.vlan_tci
= htons(tci
);
967 cookie
.sflow
.output
= output
;
969 user_data_size
= sizeof cookie
.sflow
;
970 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
973 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
974 cookie
.slow_path
.unused
= 0;
975 cookie
.slow_path
.reason
= 0;
977 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
978 &cookie
.slow_path
.reason
,
979 SLOW_PATH_REASON_MASK
, NULL
);
980 if (res
< 0 || s
[n
+ res
] != ')') {
986 user_data_size
= sizeof cookie
.slow_path
;
987 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
988 "collector_set_id=%"SCNi32
","
989 "obs_domain_id=%"SCNi32
","
990 "obs_point_id=%"SCNi32
","
991 "output_port=%"SCNi32
"%n",
992 &probability
, &collector_set_id
,
993 &obs_domain_id
, &obs_point_id
,
997 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
998 cookie
.flow_sample
.probability
= probability
;
999 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1000 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1001 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1002 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1003 user_data
= &cookie
;
1004 user_data_size
= sizeof cookie
.flow_sample
;
1006 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1007 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1009 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1010 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1013 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1020 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1023 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1024 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1025 user_data
= &cookie
;
1026 user_data_size
= sizeof cookie
.ipfix
;
1027 } else if (ovs_scan(&s
[n
], ",userdata(%n",
1032 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1033 if (end
[0] != ')') {
1037 user_data
= buf
.data
;
1038 user_data_size
= buf
.size
;
1045 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1047 include_actions
= true;
1053 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1054 &tunnel_out_port
, &n1
)) {
1055 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1056 tunnel_out_port
, include_actions
, actions
);
1058 } else if (s
[n
] == ')') {
1059 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1060 ODPP_NONE
, include_actions
, actions
);
1067 ofpbuf_uninit(&buf
);
1072 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1074 struct eth_header
*eth
;
1075 struct ip_header
*ip
;
1076 struct ovs_16aligned_ip6_hdr
*ip6
;
1077 struct udp_header
*udp
;
1078 struct gre_base_hdr
*greh
;
1079 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1081 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1085 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1088 eth
= (struct eth_header
*) data
->header
;
1089 l3
= (data
->header
+ sizeof *eth
);
1090 ip
= (struct ip_header
*) l3
;
1091 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1092 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1093 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1096 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1100 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1101 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1104 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1107 eth
->eth_type
= htons(dl_type
);
1109 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1111 uint16_t ip_frag_off
;
1112 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1113 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1116 &ip
->ip_proto
, &ip
->ip_tos
,
1117 &ip
->ip_ttl
, &ip_frag_off
)) {
1120 put_16aligned_be32(&ip
->ip_src
, sip
);
1121 put_16aligned_be32(&ip
->ip_dst
, dip
);
1122 ip
->ip_frag_off
= htons(ip_frag_off
);
1123 ip_len
= sizeof *ip
;
1125 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1126 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1127 struct in6_addr sip6
, dip6
;
1130 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1131 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1132 ",hlimit=%"SCNi8
"),",
1133 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1134 &tclass
, &ip6
->ip6_hlim
)
1135 || (label
& ~IPV6_LABEL_MASK
) != 0
1136 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1137 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1140 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1141 htonl(tclass
<< 20) | htonl(label
));
1142 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1143 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1144 ip_len
= sizeof *ip6
;
1148 l4
= ((uint8_t *) l3
+ ip_len
);
1149 udp
= (struct udp_header
*) l4
;
1150 greh
= (struct gre_base_hdr
*) l4
;
1151 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1152 &udp_src
, &udp_dst
, &csum
)) {
1153 uint32_t vx_flags
, vni
;
1155 udp
->udp_src
= htons(udp_src
);
1156 udp
->udp_dst
= htons(udp_dst
);
1158 udp
->udp_csum
= htons(csum
);
1160 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1162 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1164 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1165 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1166 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1167 header_len
= sizeof *eth
+ ip_len
+
1168 sizeof *udp
+ sizeof *vxh
;
1169 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1170 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1172 memset(gnh
, 0, sizeof *gnh
);
1173 header_len
= sizeof *eth
+ ip_len
+
1174 sizeof *udp
+ sizeof *gnh
;
1176 if (ovs_scan_len(s
, &n
, "oam,")) {
1179 if (ovs_scan_len(s
, &n
, "crit,")) {
1182 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1185 if (ovs_scan_len(s
, &n
, ",options(")) {
1186 struct geneve_scan options
;
1189 memset(&options
, 0, sizeof options
);
1190 len
= scan_geneve(s
+ n
, &options
, NULL
);
1195 memcpy(gnh
->options
, options
.d
, options
.len
);
1196 gnh
->opt_len
= options
.len
/ 4;
1197 header_len
+= options
.len
;
1201 if (!ovs_scan_len(s
, &n
, "))")) {
1205 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1206 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1207 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1211 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1212 &gre_flags
, &gre_proto
)){
1214 tnl_type
= OVS_VPORT_TYPE_GRE
;
1215 greh
->flags
= htons(gre_flags
);
1216 greh
->protocol
= htons(gre_proto
);
1217 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1219 if (greh
->flags
& htons(GRE_CSUM
)) {
1220 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1224 memset(options
, 0, sizeof *options
);
1225 *((ovs_be16
*)options
) = htons(csum
);
1228 if (greh
->flags
& htons(GRE_KEY
)) {
1231 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1235 put_16aligned_be32(options
, htonl(key
));
1238 if (greh
->flags
& htons(GRE_SEQ
)) {
1241 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1244 put_16aligned_be32(options
, htonl(seq
));
1248 if (!ovs_scan_len(s
, &n
, "))")) {
1252 header_len
= sizeof *eth
+ ip_len
+
1253 ((uint8_t *) options
- (uint8_t *) greh
);
1258 /* check tunnel meta data. */
1259 if (data
->tnl_type
!= tnl_type
) {
1262 if (data
->header_len
!= header_len
) {
1267 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1274 struct ct_nat_params
{
1280 struct in6_addr ip6
;
1284 struct in6_addr ip6
;
1294 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1296 if (ovs_scan_len(s
, n
, "=")) {
1297 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1298 struct in6_addr ipv6
;
1300 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1301 p
->addr_len
= sizeof p
->addr_min
.ip
;
1302 if (ovs_scan_len(s
, n
, "-")) {
1303 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1304 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1308 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1309 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1310 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1311 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1312 p
->addr_min
.ip6
= ipv6
;
1313 if (ovs_scan_len(s
, n
, "-")) {
1314 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1315 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1316 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1317 p
->addr_max
.ip6
= ipv6
;
1325 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1326 if (ovs_scan_len(s
, n
, "-")) {
1327 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1337 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1341 if (ovs_scan_len(s
, &n
, "nat")) {
1342 memset(p
, 0, sizeof *p
);
1344 if (ovs_scan_len(s
, &n
, "(")) {
1348 end
= strchr(s
+ n
, ')');
1355 n
+= strspn(s
+ n
, delimiters
);
1356 if (ovs_scan_len(s
, &n
, "src")) {
1357 int err
= scan_ct_nat_range(s
, &n
, p
);
1364 if (ovs_scan_len(s
, &n
, "dst")) {
1365 int err
= scan_ct_nat_range(s
, &n
, p
);
1372 if (ovs_scan_len(s
, &n
, "persistent")) {
1373 p
->persistent
= true;
1376 if (ovs_scan_len(s
, &n
, "hash")) {
1377 p
->proto_hash
= true;
1380 if (ovs_scan_len(s
, &n
, "random")) {
1381 p
->proto_random
= true;
1387 if (p
->snat
&& p
->dnat
) {
1390 if ((p
->addr_len
!= 0 &&
1391 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1392 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1393 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1396 if (p
->proto_hash
&& p
->proto_random
) {
1406 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1408 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1411 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1412 } else if (p
->dnat
) {
1413 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1417 if (p
->addr_len
!= 0) {
1418 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1420 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1421 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1425 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1426 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1427 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1430 if (p
->persistent
) {
1431 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1433 if (p
->proto_hash
) {
1434 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1436 if (p
->proto_random
) {
1437 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1441 nl_msg_end_nested(actions
, start
);
1445 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1449 if (ovs_scan(s
, "ct")) {
1450 const char *helper
= NULL
;
1451 size_t helper_len
= 0;
1452 bool commit
= false;
1457 } ct_mark
= { 0, 0 };
1462 struct ct_nat_params nat_params
;
1463 bool have_nat
= false;
1467 memset(&ct_label
, 0, sizeof(ct_label
));
1470 if (ovs_scan(s
, "(")) {
1473 end
= strchr(s
, ')');
1481 s
+= strspn(s
, delimiters
);
1482 if (ovs_scan(s
, "commit%n", &n
)) {
1487 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1491 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1494 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1497 ct_mark
.mask
= UINT32_MAX
;
1501 if (ovs_scan(s
, "label=%n", &n
)) {
1505 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1512 if (ovs_scan(s
, "helper=%n", &n
)) {
1514 helper_len
= strcspn(s
, delimiters_end
);
1515 if (!helper_len
|| helper_len
> 15) {
1523 n
= scan_ct_nat(s
, &nat_params
);
1528 /* end points to the end of the nested, nat action.
1529 * find the real end. */
1532 /* Nothing matched. */
1538 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1540 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1543 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1546 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1549 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1550 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1554 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1558 nl_msg_put_ct_nat(&nat_params
, actions
);
1560 nl_msg_end_nested(actions
, start
);
1567 parse_action_list(const char *s
, const struct simap
*port_names
,
1568 struct ofpbuf
*actions
)
1575 n
+= strspn(s
+ n
, delimiters
);
1579 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1590 parse_odp_action(const char *s
, const struct simap
*port_names
,
1591 struct ofpbuf
*actions
)
1597 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1598 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1607 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
1608 struct ovs_action_trunc
*trunc
;
1610 trunc
= nl_msg_put_unspec_uninit(actions
,
1611 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1612 trunc
->max_len
= max_len
;
1618 int len
= strcspn(s
, delimiters
);
1619 struct simap_node
*node
;
1621 node
= simap_find_len(port_names
, s
, len
);
1623 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1632 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1633 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1638 if (!strncmp(s
, "userspace(", 10)) {
1639 return parse_odp_userspace_action(s
, actions
);
1642 if (!strncmp(s
, "set(", 4)) {
1645 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1646 struct ofpbuf maskbuf
;
1647 struct nlattr
*nested
, *key
;
1650 /* 'mask' is big enough to hold any key. */
1651 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1653 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1654 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1658 if (s
[retval
+ 4] != ')') {
1662 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1665 size
= nl_attr_get_size(mask
);
1666 if (size
== nl_attr_get_size(key
)) {
1667 /* Change to masked set action if not fully masked. */
1668 if (!is_all_ones(mask
+ 1, size
)) {
1669 key
->nla_len
+= size
;
1670 ofpbuf_put(actions
, mask
+ 1, size
);
1671 /* 'actions' may have been reallocated by ofpbuf_put(). */
1672 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1673 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1677 nl_msg_end_nested(actions
, start_ofs
);
1682 struct ovs_action_push_vlan push
;
1683 int tpid
= ETH_TYPE_VLAN
;
1688 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1689 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1690 &vid
, &pcp
, &cfi
, &n
)
1691 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1692 &tpid
, &vid
, &pcp
, &n
)
1693 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1694 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1695 push
.vlan_tpid
= htons(tpid
);
1696 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1697 | (pcp
<< VLAN_PCP_SHIFT
)
1698 | (cfi
? VLAN_CFI
: 0));
1699 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1700 &push
, sizeof push
);
1706 if (!strncmp(s
, "pop_vlan", 8)) {
1707 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1715 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1716 && percentage
>= 0. && percentage
<= 100.0) {
1717 size_t sample_ofs
, actions_ofs
;
1720 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1721 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1722 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1723 (probability
<= 0 ? 0
1724 : probability
>= UINT32_MAX
? UINT32_MAX
1727 actions_ofs
= nl_msg_start_nested(actions
,
1728 OVS_SAMPLE_ATTR_ACTIONS
);
1729 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
1734 nl_msg_end_nested(actions
, actions_ofs
);
1735 nl_msg_end_nested(actions
, sample_ofs
);
1737 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1742 if (!strncmp(s
, "clone(", 6)) {
1746 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
1747 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
1752 nl_msg_end_nested(actions
, actions_ofs
);
1761 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1762 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1770 retval
= parse_conntrack_action(s
, actions
);
1777 struct ovs_action_push_tnl data
;
1780 n
= ovs_parse_tnl_push(s
, &data
);
1782 odp_put_tnl_push_action(actions
, &data
);
1791 /* Parses the string representation of datapath actions, in the format output
1792 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1793 * value. On success, the ODP actions are appended to 'actions' as a series of
1794 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1795 * way, 'actions''s data might be reallocated. */
1797 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1798 struct ofpbuf
*actions
)
1802 if (!strcasecmp(s
, "drop")) {
1806 old_size
= actions
->size
;
1810 s
+= strspn(s
, delimiters
);
1815 retval
= parse_odp_action(s
, port_names
, actions
);
1816 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1817 actions
->size
= old_size
;
1826 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1827 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1830 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1831 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1832 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1833 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1834 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1835 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1836 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1837 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1838 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1839 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1840 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1841 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1842 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1843 .next
= ovs_vxlan_ext_attr_lens
,
1844 .next_max
= OVS_VXLAN_EXT_MAX
},
1845 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
1846 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
1849 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1850 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1851 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1852 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1853 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1854 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1855 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1856 .next
= ovs_tun_key_attr_lens
,
1857 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1858 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1859 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1860 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1861 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1862 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1863 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1864 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1865 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1866 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1867 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1868 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1869 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1870 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1871 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1872 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1873 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
1874 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
1875 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
1876 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
1879 /* Returns the correct length of the payload for a flow key attribute of the
1880 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1881 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1882 * payload is a nested type. */
1884 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_len
, uint16_t type
)
1886 if (type
> max_len
) {
1887 return ATTR_LEN_INVALID
;
1890 return tbl
[type
].len
;
1894 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1896 size_t len
= nl_attr_get_size(a
);
1898 const uint8_t *unspec
;
1901 unspec
= nl_attr_get(a
);
1902 for (i
= 0; i
< len
; i
++) {
1904 ds_put_char(ds
, ' ');
1906 ds_put_format(ds
, "%02x", unspec
[i
]);
1912 ovs_frag_type_to_string(enum ovs_frag_type type
)
1915 case OVS_FRAG_TYPE_NONE
:
1917 case OVS_FRAG_TYPE_FIRST
:
1919 case OVS_FRAG_TYPE_LATER
:
1921 case __OVS_FRAG_TYPE_MAX
:
1927 static enum odp_key_fitness
1928 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
1929 struct flow_tnl
*tun
)
1932 const struct nlattr
*a
;
1934 bool unknown
= false;
1936 NL_NESTED_FOR_EACH(a
, left
, attr
) {
1937 uint16_t type
= nl_attr_type(a
);
1938 size_t len
= nl_attr_get_size(a
);
1939 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
1940 OVS_TUNNEL_ATTR_MAX
, type
);
1942 if (len
!= expected_len
&& expected_len
>= 0) {
1943 return ODP_FIT_ERROR
;
1947 case OVS_TUNNEL_KEY_ATTR_ID
:
1948 tun
->tun_id
= nl_attr_get_be64(a
);
1949 tun
->flags
|= FLOW_TNL_F_KEY
;
1951 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
1952 tun
->ip_src
= nl_attr_get_be32(a
);
1954 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
1955 tun
->ip_dst
= nl_attr_get_be32(a
);
1957 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
1958 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
1960 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
1961 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
1963 case OVS_TUNNEL_KEY_ATTR_TOS
:
1964 tun
->ip_tos
= nl_attr_get_u8(a
);
1966 case OVS_TUNNEL_KEY_ATTR_TTL
:
1967 tun
->ip_ttl
= nl_attr_get_u8(a
);
1970 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1971 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
1973 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1974 tun
->flags
|= FLOW_TNL_F_CSUM
;
1976 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
1977 tun
->tp_src
= nl_attr_get_be16(a
);
1979 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
1980 tun
->tp_dst
= nl_attr_get_be16(a
);
1982 case OVS_TUNNEL_KEY_ATTR_OAM
:
1983 tun
->flags
|= FLOW_TNL_F_OAM
;
1985 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
1986 static const struct nl_policy vxlan_opts_policy
[] = {
1987 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
1989 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
1991 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
1992 return ODP_FIT_ERROR
;
1995 if (ext
[OVS_VXLAN_EXT_GBP
]) {
1996 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
1998 tun
->gbp_id
= htons(gbp
& 0xFFFF);
1999 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2004 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2005 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2009 /* Allow this to show up as unexpected, if there are unknown
2010 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2017 return ODP_FIT_ERROR
;
2020 return ODP_FIT_TOO_MUCH
;
2022 return ODP_FIT_PERFECT
;
2025 enum odp_key_fitness
2026 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
2028 memset(tun
, 0, sizeof *tun
);
2029 return odp_tun_key_from_attr__(attr
, false, tun
);
2033 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2034 const struct flow_tnl
*tun_flow_key
,
2035 const struct ofpbuf
*key_buf
)
2039 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2041 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2042 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2043 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2045 if (tun_key
->ip_src
) {
2046 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2048 if (tun_key
->ip_dst
) {
2049 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2051 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2052 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2054 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2055 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2057 if (tun_key
->ip_tos
) {
2058 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2060 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2061 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2062 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2064 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2065 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2067 if (tun_key
->tp_src
) {
2068 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2070 if (tun_key
->tp_dst
) {
2071 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2073 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2074 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2076 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
2077 size_t vxlan_opts_ofs
;
2079 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2080 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2081 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2082 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2084 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2086 nl_msg_end_nested(a
, tun_key_ofs
);
2090 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2092 return is_all_zeros(nl_attr_get(ma
), nl_attr_get_size(ma
));
2096 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2098 if (attr
== OVS_KEY_ATTR_TCP_FLAGS
) {
2099 return TCP_FLAGS(*(ovs_be16
*)mask
) == TCP_FLAGS(OVS_BE16_MAX
);
2101 if (attr
== OVS_KEY_ATTR_IPV6
) {
2102 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2105 ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2106 == htonl(IPV6_LABEL_MASK
))
2107 && ipv6_mask
->ipv6_proto
== UINT8_MAX
2108 && ipv6_mask
->ipv6_tclass
== UINT8_MAX
2109 && ipv6_mask
->ipv6_hlimit
== UINT8_MAX
2110 && ipv6_mask
->ipv6_frag
== UINT8_MAX
2111 && ipv6_mask_is_exact(&ipv6_mask
->ipv6_src
)
2112 && ipv6_mask_is_exact(&ipv6_mask
->ipv6_dst
);
2114 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2118 if (attr
== OVS_KEY_ATTR_ARP
) {
2119 /* ARP key has padding, ignore it. */
2120 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp
) == 24);
2121 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp
, arp_tha
) == 10 + 6);
2122 size
= offsetof(struct ovs_key_arp
, arp_tha
) + ETH_ADDR_LEN
;
2123 ovs_assert(((uint16_t *)mask
)[size
/2] == 0);
2126 return is_all_ones(mask
, size
);
2130 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2132 enum ovs_key_attr attr
= nl_attr_type(ma
);
2136 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2139 mask
= nl_attr_get(ma
);
2140 size
= nl_attr_get_size(ma
);
2143 return odp_mask_is_exact(attr
, mask
, size
);
2147 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2150 struct odp_portno_names
*odp_portno_names
;
2152 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2153 odp_portno_names
->port_no
= port_no
;
2154 odp_portno_names
->name
= xstrdup(port_name
);
2155 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2156 hash_odp_port(port_no
));
2160 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2162 struct odp_portno_names
*odp_portno_names
;
2164 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2165 hash_odp_port(port_no
), portno_names
) {
2166 if (odp_portno_names
->port_no
== port_no
) {
2167 return odp_portno_names
->name
;
2174 odp_portno_names_destroy(struct hmap
*portno_names
)
2176 struct odp_portno_names
*odp_portno_names
;
2178 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
2179 free(odp_portno_names
->name
);
2180 free(odp_portno_names
);
2184 /* Format helpers. */
2187 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2188 const struct eth_addr
*mask
, bool verbose
)
2190 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2192 if (verbose
|| !mask_empty
) {
2193 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2196 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2198 ds_put_format(ds
, "%s=", name
);
2199 eth_format_masked(key
, mask
, ds
);
2200 ds_put_char(ds
, ',');
2206 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2207 const ovs_be64
*mask
, bool verbose
)
2209 bool mask_empty
= mask
&& !*mask
;
2211 if (verbose
|| !mask_empty
) {
2212 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2214 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2215 if (!mask_full
) { /* Partially masked. */
2216 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2218 ds_put_char(ds
, ',');
2223 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2224 const ovs_be32
*mask
, bool verbose
)
2226 bool mask_empty
= mask
&& !*mask
;
2228 if (verbose
|| !mask_empty
) {
2229 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2231 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2232 if (!mask_full
) { /* Partially masked. */
2233 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2235 ds_put_char(ds
, ',');
2240 format_in6_addr(struct ds
*ds
, const char *name
,
2241 const struct in6_addr
*key
,
2242 const struct in6_addr
*mask
,
2245 char buf
[INET6_ADDRSTRLEN
];
2246 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2248 if (verbose
|| !mask_empty
) {
2249 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2251 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2252 ds_put_format(ds
, "%s=%s", name
, buf
);
2253 if (!mask_full
) { /* Partially masked. */
2254 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2255 ds_put_format(ds
, "/%s", buf
);
2257 ds_put_char(ds
, ',');
2262 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2263 const ovs_be32
*mask
, bool verbose
)
2265 bool mask_empty
= mask
&& !*mask
;
2267 if (verbose
|| !mask_empty
) {
2268 bool mask_full
= !mask
2269 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2271 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2272 if (!mask_full
) { /* Partially masked. */
2273 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2275 ds_put_char(ds
, ',');
2280 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2281 const uint8_t *mask
, bool verbose
)
2283 bool mask_empty
= mask
&& !*mask
;
2285 if (verbose
|| !mask_empty
) {
2286 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2288 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2289 if (!mask_full
) { /* Partially masked. */
2290 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2292 ds_put_char(ds
, ',');
2297 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2298 const uint8_t *mask
, bool verbose
)
2300 bool mask_empty
= mask
&& !*mask
;
2302 if (verbose
|| !mask_empty
) {
2303 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2305 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2306 if (!mask_full
) { /* Partially masked. */
2307 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2309 ds_put_char(ds
, ',');
2314 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2315 const ovs_be16
*mask
, bool verbose
)
2317 bool mask_empty
= mask
&& !*mask
;
2319 if (verbose
|| !mask_empty
) {
2320 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2322 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2323 if (!mask_full
) { /* Partially masked. */
2324 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2326 ds_put_char(ds
, ',');
2331 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2332 const ovs_be16
*mask
, bool verbose
)
2334 bool mask_empty
= mask
&& !*mask
;
2336 if (verbose
|| !mask_empty
) {
2337 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2339 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2340 if (!mask_full
) { /* Partially masked. */
2341 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2343 ds_put_char(ds
, ',');
2348 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2349 const uint16_t *mask
, bool verbose
)
2351 bool mask_empty
= mask
&& !*mask
;
2353 if (verbose
|| !mask_empty
) {
2354 ds_put_cstr(ds
, name
);
2355 ds_put_char(ds
, '(');
2357 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2358 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2359 } else { /* Fully masked. */
2360 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2362 ds_put_cstr(ds
, "),");
2367 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2368 const struct attr_len_tbl tbl
[], int max_len
, bool need_key
)
2372 expected_len
= odp_key_attr_len(tbl
, max_len
, nl_attr_type(a
));
2373 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2374 expected_len
!= ATTR_LEN_NESTED
) {
2376 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2377 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2379 if (bad_key_len
|| bad_mask_len
) {
2381 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2384 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2385 nl_attr_get_size(a
), expected_len
);
2387 format_generic_odp_key(a
, ds
);
2389 ds_put_char(ds
, '/');
2391 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2392 nl_attr_get_size(ma
), expected_len
);
2394 format_generic_odp_key(ma
, ds
);
2396 ds_put_char(ds
, ')');
2405 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2406 const struct nlattr
*ma
)
2408 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2409 format_generic_odp_key(a
, ds
);
2410 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2411 ds_put_char(ds
, '/');
2412 format_generic_odp_key(ma
, ds
);
2414 ds_put_cstr(ds
, "),");
2418 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2419 const struct nlattr
*mask_attr
, struct ds
*ds
,
2423 const struct nlattr
*a
;
2426 ofpbuf_init(&ofp
, 100);
2427 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2428 uint16_t type
= nl_attr_type(a
);
2429 const struct nlattr
*ma
= NULL
;
2432 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2433 nl_attr_get_size(mask_attr
), type
);
2435 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2441 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2442 OVS_VXLAN_EXT_MAX
, true)) {
2447 case OVS_VXLAN_EXT_GBP
: {
2448 uint32_t key
= nl_attr_get_u32(a
);
2449 ovs_be16 id
, id_mask
;
2450 uint8_t flags
, flags_mask
= 0;
2452 id
= htons(key
& 0xFFFF);
2453 flags
= (key
>> 16) & 0xFF;
2455 uint32_t mask
= nl_attr_get_u32(ma
);
2456 id_mask
= htons(mask
& 0xFFFF);
2457 flags_mask
= (mask
>> 16) & 0xFF;
2460 ds_put_cstr(ds
, "gbp(");
2461 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2462 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2464 ds_put_cstr(ds
, "),");
2469 format_unknown_key(ds
, a
, ma
);
2475 ofpbuf_uninit(&ofp
);
2478 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2481 format_geneve_opts(const struct geneve_opt
*opt
,
2482 const struct geneve_opt
*mask
, int opts_len
,
2483 struct ds
*ds
, bool verbose
)
2485 while (opts_len
> 0) {
2487 uint8_t data_len
, data_len_mask
;
2489 if (opts_len
< sizeof *opt
) {
2490 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2491 opts_len
, sizeof *opt
);
2495 data_len
= opt
->length
* 4;
2497 if (mask
->length
== 0x1f) {
2498 data_len_mask
= UINT8_MAX
;
2500 data_len_mask
= mask
->length
;
2503 len
= sizeof *opt
+ data_len
;
2504 if (len
> opts_len
) {
2505 ds_put_format(ds
, "opt len %u greater than remaining %u",
2510 ds_put_char(ds
, '{');
2511 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2513 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2514 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2516 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2517 ds_put_hex(ds
, opt
+ 1, data_len
);
2518 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2519 ds_put_char(ds
, '/');
2520 ds_put_hex(ds
, mask
+ 1, data_len
);
2525 ds_put_char(ds
, '}');
2527 opt
+= len
/ sizeof(*opt
);
2529 mask
+= len
/ sizeof(*opt
);
2536 format_odp_tun_geneve(const struct nlattr
*attr
,
2537 const struct nlattr
*mask_attr
, struct ds
*ds
,
2540 int opts_len
= nl_attr_get_size(attr
);
2541 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2542 const struct geneve_opt
*mask
= mask_attr
?
2543 nl_attr_get(mask_attr
) : NULL
;
2545 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2546 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2547 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2551 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2555 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2556 struct ds
*ds
, bool verbose
)
2559 const struct nlattr
*a
;
2561 uint16_t mask_flags
= 0;
2564 ofpbuf_init(&ofp
, 100);
2565 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2566 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2567 const struct nlattr
*ma
= NULL
;
2570 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2571 nl_attr_get_size(mask_attr
), type
);
2573 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2574 OVS_TUNNEL_KEY_ATTR_MAX
,
2579 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2580 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2585 case OVS_TUNNEL_KEY_ATTR_ID
:
2586 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2587 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2588 flags
|= FLOW_TNL_F_KEY
;
2590 mask_flags
|= FLOW_TNL_F_KEY
;
2593 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2594 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2595 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2597 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2598 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2599 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2601 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2602 struct in6_addr ipv6_src
;
2603 ipv6_src
= nl_attr_get_in6_addr(a
);
2604 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2605 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2608 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2609 struct in6_addr ipv6_dst
;
2610 ipv6_dst
= nl_attr_get_in6_addr(a
);
2611 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2612 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2615 case OVS_TUNNEL_KEY_ATTR_TOS
:
2616 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2617 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2619 case OVS_TUNNEL_KEY_ATTR_TTL
:
2620 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2621 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2623 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2624 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2626 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2627 flags
|= FLOW_TNL_F_CSUM
;
2629 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2630 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2631 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2633 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2634 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2635 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2637 case OVS_TUNNEL_KEY_ATTR_OAM
:
2638 flags
|= FLOW_TNL_F_OAM
;
2640 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2641 ds_put_cstr(ds
, "vxlan(");
2642 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2643 ds_put_cstr(ds
, "),");
2645 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2646 ds_put_cstr(ds
, "geneve(");
2647 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2648 ds_put_cstr(ds
, "),");
2650 case OVS_TUNNEL_KEY_ATTR_PAD
:
2652 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2654 format_unknown_key(ds
, a
, ma
);
2659 /* Flags can have a valid mask even if the attribute is not set, so
2660 * we need to collect these separately. */
2662 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2663 switch (nl_attr_type(a
)) {
2664 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2665 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2667 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2668 mask_flags
|= FLOW_TNL_F_CSUM
;
2670 case OVS_TUNNEL_KEY_ATTR_OAM
:
2671 mask_flags
|= FLOW_TNL_F_OAM
;
2677 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2680 ofpbuf_uninit(&ofp
);
2684 odp_ct_state_to_string(uint32_t flag
)
2687 case OVS_CS_F_REPLY_DIR
:
2689 case OVS_CS_F_TRACKED
:
2693 case OVS_CS_F_ESTABLISHED
:
2695 case OVS_CS_F_RELATED
:
2697 case OVS_CS_F_INVALID
:
2699 case OVS_CS_F_SRC_NAT
:
2701 case OVS_CS_F_DST_NAT
:
2709 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2710 const uint8_t *mask
, bool verbose
)
2712 bool mask_empty
= mask
&& !*mask
;
2714 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2715 if (verbose
|| !mask_empty
) {
2716 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2718 if (!mask_full
) { /* Partially masked. */
2719 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2722 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2728 mask_empty(const struct nlattr
*ma
)
2736 mask
= nl_attr_get(ma
);
2737 n
= nl_attr_get_size(ma
);
2739 return is_all_zeros(mask
, n
);
2743 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
2744 const struct hmap
*portno_names
, struct ds
*ds
,
2747 enum ovs_key_attr attr
= nl_attr_type(a
);
2748 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2751 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2753 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2755 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
2756 OVS_KEY_ATTR_MAX
, false)) {
2760 ds_put_char(ds
, '(');
2762 case OVS_KEY_ATTR_ENCAP
:
2763 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2764 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2765 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2767 } else if (nl_attr_get_size(a
)) {
2768 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2773 case OVS_KEY_ATTR_PRIORITY
:
2774 case OVS_KEY_ATTR_SKB_MARK
:
2775 case OVS_KEY_ATTR_DP_HASH
:
2776 case OVS_KEY_ATTR_RECIRC_ID
:
2777 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2779 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2783 case OVS_KEY_ATTR_CT_MARK
:
2784 if (verbose
|| !mask_empty(ma
)) {
2785 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2787 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2792 case OVS_KEY_ATTR_CT_STATE
:
2794 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2796 ds_put_format(ds
, "/%#"PRIx32
,
2797 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2799 } else if (!is_exact
) {
2800 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2802 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2805 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2809 case OVS_KEY_ATTR_CT_ZONE
:
2810 if (verbose
|| !mask_empty(ma
)) {
2811 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2813 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2818 case OVS_KEY_ATTR_CT_LABELS
: {
2819 const ovs_u128
*value
= nl_attr_get(a
);
2820 const ovs_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2822 format_u128(ds
, value
, mask
, verbose
);
2826 case OVS_KEY_ATTR_TUNNEL
:
2827 format_odp_tun_attr(a
, ma
, ds
, verbose
);
2830 case OVS_KEY_ATTR_IN_PORT
:
2831 if (portno_names
&& verbose
&& is_exact
) {
2832 char *name
= odp_portno_names_get(portno_names
,
2833 nl_attr_get_odp_port(a
));
2835 ds_put_format(ds
, "%s", name
);
2837 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2840 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2842 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2847 case OVS_KEY_ATTR_ETHERNET
: {
2848 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2849 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
2851 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
2852 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
2856 case OVS_KEY_ATTR_VLAN
:
2857 format_vlan_tci(ds
, nl_attr_get_be16(a
),
2858 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
2861 case OVS_KEY_ATTR_MPLS
: {
2862 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
2863 const struct ovs_key_mpls
*mpls_mask
= NULL
;
2864 size_t size
= nl_attr_get_size(a
);
2866 if (!size
|| size
% sizeof *mpls_key
) {
2867 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
2871 mpls_mask
= nl_attr_get(ma
);
2872 if (size
!= nl_attr_get_size(ma
)) {
2873 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
2874 "mask length %"PRIuSIZE
")",
2875 size
, nl_attr_get_size(ma
));
2879 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
2882 case OVS_KEY_ATTR_ETHERTYPE
:
2883 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
2885 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
2889 case OVS_KEY_ATTR_IPV4
: {
2890 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
2891 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2893 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2894 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2895 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2897 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
2898 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
2899 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
2904 case OVS_KEY_ATTR_IPV6
: {
2905 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
2906 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2908 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
2910 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
2912 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
2914 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
2916 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
2918 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
2920 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
2925 /* These have the same structure and format. */
2926 case OVS_KEY_ATTR_TCP
:
2927 case OVS_KEY_ATTR_UDP
:
2928 case OVS_KEY_ATTR_SCTP
: {
2929 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
2930 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2932 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
2933 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
2937 case OVS_KEY_ATTR_TCP_FLAGS
:
2939 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
2940 ntohs(nl_attr_get_be16(a
)),
2941 TCP_FLAGS(nl_attr_get_be16(ma
)),
2942 TCP_FLAGS(OVS_BE16_MAX
));
2944 format_flags(ds
, packet_tcp_flag_to_string
,
2945 ntohs(nl_attr_get_be16(a
)), '|');
2949 case OVS_KEY_ATTR_ICMP
: {
2950 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2951 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2953 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2954 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2958 case OVS_KEY_ATTR_ICMPV6
: {
2959 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2960 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2962 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2964 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2969 case OVS_KEY_ATTR_ARP
: {
2970 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2971 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2973 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2974 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2975 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2976 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2977 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2981 case OVS_KEY_ATTR_ND
: {
2982 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2983 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2985 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
2987 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2988 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2993 case OVS_KEY_ATTR_UNSPEC
:
2994 case __OVS_KEY_ATTR_MAX
:
2996 format_generic_odp_key(a
, ds
);
2998 ds_put_char(ds
, '/');
2999 format_generic_odp_key(ma
, ds
);
3003 ds_put_char(ds
, ')');
3006 static struct nlattr
*
3007 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
3008 struct ofpbuf
*ofp
, const struct nlattr
*key
)
3010 const struct nlattr
*a
;
3012 int type
= nl_attr_type(key
);
3013 int size
= nl_attr_get_size(key
);
3015 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
3016 nl_msg_put_unspec_zero(ofp
, type
, size
);
3020 if (tbl
[type
].next
) {
3021 tbl
= tbl
[type
].next
;
3022 max
= tbl
[type
].next_max
;
3025 nested_mask
= nl_msg_start_nested(ofp
, type
);
3026 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
3027 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
3029 nl_msg_end_nested(ofp
, nested_mask
);
3036 format_u128(struct ds
*ds
, const ovs_u128
*key
, const ovs_u128
*mask
,
3039 if (verbose
|| (mask
&& !ovs_u128_is_zero(*mask
))) {
3042 value
= hton128(*key
);
3043 ds_put_hex(ds
, &value
, sizeof value
);
3044 if (mask
&& !(ovs_u128_is_ones(*mask
))) {
3045 value
= hton128(*mask
);
3046 ds_put_char(ds
, '/');
3047 ds_put_hex(ds
, &value
, sizeof value
);
3052 /* Read the string from 's_' as a 128-bit value. If the string contains
3053 * a "/", the rest of the string will be treated as a 128-bit mask.
3055 * If either the value or mask is larger than 64 bits, the string must
3056 * be in hexadecimal.
3059 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
3061 char *s
= CONST_CAST(char *, s_
);
3065 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
3066 *value
= ntoh128(be_value
);
3071 if (ovs_scan(s
, "/%n", &n
)) {
3075 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
3076 sizeof be_mask
, &s
);
3080 *mask
= ntoh128(be_mask
);
3082 *mask
= OVS_U128_MAX
;
3092 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3096 if (ovs_scan(s
, "ufid:")) {
3099 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3111 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3113 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3116 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3117 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3118 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3119 * non-null and 'verbose' is true, translates odp port number to its name. */
3121 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3122 const struct nlattr
*mask
, size_t mask_len
,
3123 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3126 const struct nlattr
*a
;
3128 bool has_ethtype_key
= false;
3129 const struct nlattr
*ma
= NULL
;
3131 bool first_field
= true;
3133 ofpbuf_init(&ofp
, 100);
3134 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3135 bool is_nested_attr
;
3136 bool is_wildcard
= false;
3137 int attr_type
= nl_attr_type(a
);
3139 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3140 has_ethtype_key
= true;
3143 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3144 OVS_KEY_ATTR_MAX
, attr_type
) ==
3147 if (mask
&& mask_len
) {
3148 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3149 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3152 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3153 if (is_wildcard
&& !ma
) {
3154 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3159 ds_put_char(ds
, ',');
3161 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
3162 first_field
= false;
3166 ofpbuf_uninit(&ofp
);
3171 if (left
== key_len
) {
3172 ds_put_cstr(ds
, "<empty>");
3174 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3175 for (i
= 0; i
< left
; i
++) {
3176 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3178 ds_put_char(ds
, ')');
3180 if (!has_ethtype_key
) {
3181 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
3183 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3184 ntohs(nl_attr_get_be16(ma
)));
3188 ds_put_cstr(ds
, "<empty>");
3192 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3193 * OVS_KEY_ATTR_* attributes in 'key'. */
3195 odp_flow_key_format(const struct nlattr
*key
,
3196 size_t key_len
, struct ds
*ds
)
3198 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3202 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3204 if (!strcasecmp(s
, "no")) {
3205 *type
= OVS_FRAG_TYPE_NONE
;
3206 } else if (!strcasecmp(s
, "first")) {
3207 *type
= OVS_FRAG_TYPE_FIRST
;
3208 } else if (!strcasecmp(s
, "later")) {
3209 *type
= OVS_FRAG_TYPE_LATER
;
3219 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3223 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3224 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3228 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3229 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3232 memset(mask
, 0xff, sizeof *mask
);
3241 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3245 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3249 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3250 IP_SCAN_ARGS(mask
), &n
)) {
3253 *mask
= OVS_BE32_MAX
;
3262 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3265 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3267 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3268 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3272 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3273 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3276 memset(mask
, 0xff, sizeof *mask
);
3285 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3290 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3291 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3296 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3297 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3299 *mask
= htonl(mask_
);
3301 *mask
= htonl(IPV6_LABEL_MASK
);
3310 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3314 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3318 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3330 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3334 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3338 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3350 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3354 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3358 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3370 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3372 uint16_t key_
, mask_
;
3375 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3380 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3382 *mask
= htons(mask_
);
3384 *mask
= OVS_BE16_MAX
;
3393 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3395 uint64_t key_
, mask_
;
3398 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3401 *key
= htonll(key_
);
3403 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3405 *mask
= htonll(mask_
);
3407 *mask
= OVS_BE64_MAX
;
3416 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3418 uint32_t flags
, fmask
;
3421 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3422 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3423 if (n
>= 0 && s
[n
] == ')') {
3434 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3436 uint32_t flags
, fmask
;
3439 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3440 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3442 *key
= htons(flags
);
3444 *mask
= htons(fmask
);
3452 ovs_to_odp_ct_state(uint8_t state
)
3456 if (state
& CS_NEW
) {
3457 odp
|= OVS_CS_F_NEW
;
3459 if (state
& CS_ESTABLISHED
) {
3460 odp
|= OVS_CS_F_ESTABLISHED
;
3462 if (state
& CS_RELATED
) {
3463 odp
|= OVS_CS_F_RELATED
;
3465 if (state
& CS_INVALID
) {
3466 odp
|= OVS_CS_F_INVALID
;
3468 if (state
& CS_REPLY_DIR
) {
3469 odp
|= OVS_CS_F_REPLY_DIR
;
3471 if (state
& CS_TRACKED
) {
3472 odp
|= OVS_CS_F_TRACKED
;
3474 if (state
& CS_SRC_NAT
) {
3475 odp
|= OVS_CS_F_SRC_NAT
;
3477 if (state
& CS_DST_NAT
) {
3478 odp
|= OVS_CS_F_DST_NAT
;
3485 odp_to_ovs_ct_state(uint32_t flags
)
3489 if (flags
& OVS_CS_F_NEW
) {
3492 if (flags
& OVS_CS_F_ESTABLISHED
) {
3493 state
|= CS_ESTABLISHED
;
3495 if (flags
& OVS_CS_F_RELATED
) {
3496 state
|= CS_RELATED
;
3498 if (flags
& OVS_CS_F_INVALID
) {
3499 state
|= CS_INVALID
;
3501 if (flags
& OVS_CS_F_REPLY_DIR
) {
3502 state
|= CS_REPLY_DIR
;
3504 if (flags
& OVS_CS_F_TRACKED
) {
3505 state
|= CS_TRACKED
;
3507 if (flags
& OVS_CS_F_SRC_NAT
) {
3508 state
|= CS_SRC_NAT
;
3510 if (flags
& OVS_CS_F_DST_NAT
) {
3511 state
|= CS_DST_NAT
;
3518 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3520 uint32_t flags
, fmask
;
3523 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3524 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3525 mask
? &fmask
: NULL
);
3538 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3542 enum ovs_frag_type frag_type
;
3544 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3545 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3558 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3559 const struct simap
*port_names
)
3563 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3567 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3574 } else if (port_names
) {
3575 const struct simap_node
*node
;
3578 len
= strcspn(s
, ")");
3579 node
= simap_find_len(port_names
, s
, len
);
3592 /* Helper for vlan parsing. */
3593 struct ovs_key_vlan__
{
3598 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3600 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3602 if (value
>> bits
) {
3606 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3611 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3614 uint16_t key_
, mask_
;
3617 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3620 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3622 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3625 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3629 *mask
|= htons(((1U << bits
) - 1) << offset
);
3639 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3641 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3645 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3647 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3651 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3653 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3658 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3660 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3662 if (value
>> bits
) {
3666 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3671 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3674 uint32_t key_
, mask_
;
3677 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3680 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3682 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3685 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3689 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3699 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3701 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3705 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3707 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3711 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3713 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3717 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3719 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3723 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3725 const char *s_base
= s
;
3726 ovs_be16 id
= 0, id_mask
= 0;
3727 uint8_t flags
= 0, flags_mask
= 0;
3729 if (!strncmp(s
, "id=", 3)) {
3731 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3737 if (!strncmp(s
, "flags=", 6)) {
3739 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3742 if (!strncmp(s
, "))", 2)) {
3745 *key
= (flags
<< 16) | ntohs(id
);
3747 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3757 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3759 const char *s_base
= s
;
3760 struct geneve_opt
*opt
= key
->d
;
3761 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3762 int len_remain
= sizeof key
->d
;
3764 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3768 len_remain
-= sizeof *opt
;
3770 if (!strncmp(s
, "class=", 6)) {
3772 s
+= scan_be16(s
, &opt
->opt_class
,
3773 mask
? &opt_mask
->opt_class
: NULL
);
3775 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3781 if (!strncmp(s
, "type=", 5)) {
3783 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3785 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3791 if (!strncmp(s
, "len=", 4)) {
3792 uint8_t opt_len
, opt_len_mask
;
3794 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3796 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3799 opt
->length
= opt_len
/ 4;
3801 opt_mask
->length
= opt_len_mask
;
3805 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3811 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3818 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3819 data_len
, (char **)&s
)) {
3830 opt
+= 1 + data_len
/ 4;
3832 opt_mask
+= 1 + data_len
/ 4;
3834 len_remain
-= data_len
;
3839 int len
= sizeof key
->d
- len_remain
;
3853 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
3855 const uint16_t *flags
= data_
;
3857 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3858 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3860 if (*flags
& FLOW_TNL_F_CSUM
) {
3861 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3863 if (*flags
& FLOW_TNL_F_OAM
) {
3864 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3869 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
3871 const uint32_t *gbp
= data_
;
3874 size_t vxlan_opts_ofs
;
3876 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3877 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
3878 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3883 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
3885 const struct geneve_scan
*geneve
= data_
;
3887 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
3891 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3893 unsigned long call_fn = (unsigned long)FUNC; \
3895 typedef void (*fn)(struct ofpbuf *, const void *); \
3897 func(BUF, &(DATA)); \
3899 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3903 #define SCAN_IF(NAME) \
3904 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3905 const char *start = s; \
3910 /* Usually no special initialization is needed. */
3911 #define SCAN_BEGIN(NAME, TYPE) \
3914 memset(&skey, 0, sizeof skey); \
3915 memset(&smask, 0, sizeof smask); \
3919 /* Init as fully-masked as mask will not be scanned. */
3920 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3923 memset(&skey, 0, sizeof skey); \
3924 memset(&smask, 0xff, sizeof smask); \
3928 /* VLAN needs special initialization. */
3929 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3931 TYPE skey = KEY_INIT; \
3932 TYPE smask = MASK_INIT; \
3936 /* Scan unnamed entry as 'TYPE' */
3937 #define SCAN_TYPE(TYPE, KEY, MASK) \
3938 len = scan_##TYPE(s, KEY, MASK); \
3944 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3945 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3946 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3947 s += strlen(NAME); \
3948 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3952 #define SCAN_FINISH() \
3953 } while (*s++ == ',' && len != 0); \
3954 if (s[-1] != ')') { \
3958 #define SCAN_FINISH_SINGLE() \
3960 if (*s++ != ')') { \
3964 /* Beginning of nested attribute. */
3965 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3967 size_t key_offset, mask_offset; \
3968 key_offset = nl_msg_start_nested(key, ATTR); \
3970 mask_offset = nl_msg_start_nested(mask, ATTR); \
3975 #define SCAN_END_NESTED() \
3977 nl_msg_end_nested(key, key_offset); \
3979 nl_msg_end_nested(mask, mask_offset); \
3984 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3985 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3987 memset(&skey, 0, sizeof skey); \
3988 memset(&smask, 0xff, sizeof smask); \
3989 s += strlen(NAME); \
3990 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3991 SCAN_PUT(ATTR, FUNC); \
3995 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3996 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3998 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3999 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
4001 #define SCAN_PUT(ATTR, FUNC) \
4002 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
4004 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
4006 #define SCAN_END(ATTR) \
4008 SCAN_PUT(ATTR, NULL); \
4012 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
4014 TYPE skey[CNT], smask[CNT]; \
4015 memset(&skey, 0, sizeof skey); \
4016 memset(&smask, 0, sizeof smask); \
4017 int idx = 0, cnt = CNT; \
4018 uint64_t fields = 0; \
4023 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4024 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
4025 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4026 if (fields & (1UL << field)) { \
4028 if (++idx == cnt) { \
4032 s += strlen(NAME); \
4033 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
4034 fields |= 1UL << field; \
4039 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
4040 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
4042 #define SCAN_PUT_ARRAY(ATTR, CNT) \
4043 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
4045 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
4048 #define SCAN_END_ARRAY(ATTR) \
4053 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4057 #define SCAN_END_SINGLE(ATTR) \
4058 SCAN_FINISH_SINGLE(); \
4059 SCAN_PUT(ATTR, NULL); \
4063 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4064 SCAN_BEGIN(NAME, TYPE) { \
4065 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4066 } SCAN_END_SINGLE(ATTR)
4068 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4069 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4070 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4071 } SCAN_END_SINGLE(ATTR)
4073 /* scan_port needs one extra argument. */
4074 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4075 SCAN_BEGIN(NAME, TYPE) { \
4076 len = scan_port(s, &skey, &smask, port_names); \
4081 } SCAN_END_SINGLE(ATTR)
4084 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
4085 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4091 len
= odp_ufid_from_string(s
, &ufid
);
4096 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
4097 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
4098 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
4099 OVS_KEY_ATTR_RECIRC_ID
);
4100 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
4102 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
4103 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
4104 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
4105 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
4107 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
4108 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
4109 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
4110 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
4111 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
4112 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
4113 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
4114 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
4115 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
4116 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
4117 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
4118 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
4120 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4121 } SCAN_END_NESTED();
4123 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4125 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4126 SCAN_FIELD("src=", eth
, eth_src
);
4127 SCAN_FIELD("dst=", eth
, eth_dst
);
4128 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4130 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4131 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4132 SCAN_FIELD("vid=", vid
, tci
);
4133 SCAN_FIELD("pcp=", pcp
, tci
);
4134 SCAN_FIELD("cfi=", cfi
, tci
);
4135 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4137 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4139 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
4140 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
4141 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
4142 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
4143 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
4144 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
4146 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4147 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4148 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4149 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4150 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4151 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4152 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4153 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4155 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4156 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
4157 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
4158 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4159 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4160 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4161 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4162 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4163 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4165 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4166 SCAN_FIELD("src=", be16
, tcp_src
);
4167 SCAN_FIELD("dst=", be16
, tcp_dst
);
4168 } SCAN_END(OVS_KEY_ATTR_TCP
);
4170 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4172 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4173 SCAN_FIELD("src=", be16
, udp_src
);
4174 SCAN_FIELD("dst=", be16
, udp_dst
);
4175 } SCAN_END(OVS_KEY_ATTR_UDP
);
4177 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4178 SCAN_FIELD("src=", be16
, sctp_src
);
4179 SCAN_FIELD("dst=", be16
, sctp_dst
);
4180 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4182 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4183 SCAN_FIELD("type=", u8
, icmp_type
);
4184 SCAN_FIELD("code=", u8
, icmp_code
);
4185 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4187 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4188 SCAN_FIELD("type=", u8
, icmpv6_type
);
4189 SCAN_FIELD("code=", u8
, icmpv6_code
);
4190 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4192 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4193 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4194 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4195 SCAN_FIELD("op=", be16
, arp_op
);
4196 SCAN_FIELD("sha=", eth
, arp_sha
);
4197 SCAN_FIELD("tha=", eth
, arp_tha
);
4198 } SCAN_END(OVS_KEY_ATTR_ARP
);
4200 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4201 SCAN_FIELD("target=", in6_addr
, nd_target
);
4202 SCAN_FIELD("sll=", eth
, nd_sll
);
4203 SCAN_FIELD("tll=", eth
, nd_tll
);
4204 } SCAN_END(OVS_KEY_ATTR_ND
);
4206 /* Encap open-coded. */
4207 if (!strncmp(s
, "encap(", 6)) {
4208 const char *start
= s
;
4209 size_t encap
, encap_mask
= 0;
4211 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4213 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4220 s
+= strspn(s
, delimiters
);
4223 } else if (*s
== ')') {
4227 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4235 nl_msg_end_nested(key
, encap
);
4237 nl_msg_end_nested(mask
, encap_mask
);
4246 /* Parses the string representation of a datapath flow key, in the
4247 * format output by odp_flow_key_format(). Returns 0 if successful,
4248 * otherwise a positive errno value. On success, the flow key is
4249 * appended to 'key' as a series of Netlink attributes. On failure, no
4250 * data is appended to 'key'. Either way, 'key''s data might be
4253 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4254 * to a port number. (Port names may be used instead of port numbers in
4257 * On success, the attributes appended to 'key' are individually syntactically
4258 * valid, but they may not be valid as a sequence. 'key' might, for example,
4259 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4261 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4262 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4264 const size_t old_size
= key
->size
;
4268 s
+= strspn(s
, delimiters
);
4273 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4275 key
->size
= old_size
;
4285 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4288 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4289 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4290 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4291 * must use a zero mask for the netlink frag field, and all ones mask
4293 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4295 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4296 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4297 : OVS_FRAG_TYPE_FIRST
;
4300 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4301 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4302 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4304 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4306 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4308 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4310 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4311 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4312 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4313 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4315 /* These share the same layout. */
4317 struct ovs_key_tcp tcp
;
4318 struct ovs_key_udp udp
;
4319 struct ovs_key_sctp sctp
;
4322 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4323 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4326 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4327 bool export_mask
, struct ofpbuf
*buf
)
4329 struct ovs_key_ethernet
*eth_key
;
4331 const struct flow
*flow
= parms
->flow
;
4332 const struct flow
*data
= export_mask
? parms
->mask
: parms
->flow
;
4334 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4336 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4337 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4341 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4343 if (parms
->support
.ct_state
) {
4344 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4345 ovs_to_odp_ct_state(data
->ct_state
));
4347 if (parms
->support
.ct_zone
) {
4348 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4350 if (parms
->support
.ct_mark
) {
4351 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4353 if (parms
->support
.ct_label
) {
4354 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4355 sizeof(data
->ct_label
));
4357 if (parms
->support
.recirc
) {
4358 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4359 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4362 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4363 * is not the magical value "ODPP_NONE". */
4364 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
4365 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
4368 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4370 get_ethernet_key(data
, eth_key
);
4372 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
4374 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4376 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
4378 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
4379 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4380 if (flow
->vlan_tci
== htons(0)) {
4387 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4388 /* For backwards compatibility with kernels that don't support
4389 * wildcarding, the following convention is used to encode the
4390 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4393 * -------- -------- -------
4394 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4395 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4396 * <none> 0xffff Any non-Ethernet II frame (except valid
4397 * 802.3 SNAP packet with valid eth_type).
4400 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4405 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4407 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4408 struct ovs_key_ipv4
*ipv4_key
;
4410 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4412 get_ipv4_key(data
, ipv4_key
, export_mask
);
4413 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4414 struct ovs_key_ipv6
*ipv6_key
;
4416 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4418 get_ipv6_key(data
, ipv6_key
, export_mask
);
4419 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4420 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4421 struct ovs_key_arp
*arp_key
;
4423 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4425 get_arp_key(data
, arp_key
);
4426 } else if (eth_type_mpls(flow
->dl_type
)) {
4427 struct ovs_key_mpls
*mpls_key
;
4430 n
= flow_count_mpls_labels(flow
, NULL
);
4432 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4434 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4435 n
* sizeof *mpls_key
);
4436 for (i
= 0; i
< n
; i
++) {
4437 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4441 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4442 if (flow
->nw_proto
== IPPROTO_TCP
) {
4443 union ovs_key_tp
*tcp_key
;
4445 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4447 get_tp_key(data
, tcp_key
);
4448 if (data
->tcp_flags
) {
4449 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4451 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4452 union ovs_key_tp
*udp_key
;
4454 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4456 get_tp_key(data
, udp_key
);
4457 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4458 union ovs_key_tp
*sctp_key
;
4460 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4462 get_tp_key(data
, sctp_key
);
4463 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4464 && flow
->nw_proto
== IPPROTO_ICMP
) {
4465 struct ovs_key_icmp
*icmp_key
;
4467 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4469 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4470 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4471 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4472 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4473 struct ovs_key_icmpv6
*icmpv6_key
;
4475 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4476 sizeof *icmpv6_key
);
4477 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4478 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4480 if (is_nd(flow
, NULL
)
4481 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4482 * type and code are 8 bits wide. Therefore, an exact match
4483 * looks like htons(0xff), not htons(0xffff). See
4484 * xlate_wc_finish() for details. */
4485 && (!export_mask
|| (data
->tp_src
== htons(0xff)
4486 && data
->tp_dst
== htons(0xff)))) {
4488 struct ovs_key_nd
*nd_key
;
4490 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4492 nd_key
->nd_target
= data
->nd_target
;
4493 nd_key
->nd_sll
= data
->arp_sha
;
4494 nd_key
->nd_tll
= data
->arp_tha
;
4501 nl_msg_end_nested(buf
, encap
);
4505 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4507 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4508 * capable of being expanded to allow for that much space. */
4510 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4513 odp_flow_key_from_flow__(parms
, false, buf
);
4516 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4519 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4520 * capable of being expanded to allow for that much space. */
4522 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4525 odp_flow_key_from_flow__(parms
, true, buf
);
4528 /* Generate ODP flow key from the given packet metadata */
4530 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
4532 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4534 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4535 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4538 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4541 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4542 ovs_to_odp_ct_state(md
->ct_state
));
4544 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4547 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4549 if (!ovs_u128_is_zero(md
->ct_label
)) {
4550 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4551 sizeof(md
->ct_label
));
4555 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4556 * value "ODPP_NONE". */
4557 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4558 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4562 /* Generate packet metadata from the given ODP flow key. */
4564 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
4565 struct pkt_metadata
*md
)
4567 const struct nlattr
*nla
;
4569 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4570 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4571 1u << OVS_KEY_ATTR_IN_PORT
;
4573 pkt_metadata_init(md
, ODPP_NONE
);
4575 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4576 uint16_t type
= nl_attr_type(nla
);
4577 size_t len
= nl_attr_get_size(nla
);
4578 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4579 OVS_KEY_ATTR_MAX
, type
);
4581 if (len
!= expected_len
&& expected_len
>= 0) {
4586 case OVS_KEY_ATTR_RECIRC_ID
:
4587 md
->recirc_id
= nl_attr_get_u32(nla
);
4588 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4590 case OVS_KEY_ATTR_DP_HASH
:
4591 md
->dp_hash
= nl_attr_get_u32(nla
);
4592 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4594 case OVS_KEY_ATTR_PRIORITY
:
4595 md
->skb_priority
= nl_attr_get_u32(nla
);
4596 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4598 case OVS_KEY_ATTR_SKB_MARK
:
4599 md
->pkt_mark
= nl_attr_get_u32(nla
);
4600 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4602 case OVS_KEY_ATTR_CT_STATE
:
4603 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4604 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4606 case OVS_KEY_ATTR_CT_ZONE
:
4607 md
->ct_zone
= nl_attr_get_u16(nla
);
4608 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4610 case OVS_KEY_ATTR_CT_MARK
:
4611 md
->ct_mark
= nl_attr_get_u32(nla
);
4612 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4614 case OVS_KEY_ATTR_CT_LABELS
: {
4615 const ovs_u128
*cl
= nl_attr_get(nla
);
4618 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4621 case OVS_KEY_ATTR_TUNNEL
: {
4622 enum odp_key_fitness res
;
4624 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
4625 if (res
== ODP_FIT_ERROR
) {
4626 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4627 } else if (res
== ODP_FIT_PERFECT
) {
4628 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4632 case OVS_KEY_ATTR_IN_PORT
:
4633 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4634 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4640 if (!wanted_attrs
) {
4641 return; /* Have everything. */
4647 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4649 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4650 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
4654 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4655 uint64_t attrs
, int out_of_range_attr
,
4656 const struct nlattr
*key
, size_t key_len
)
4661 if (VLOG_DROP_DBG(rl
)) {
4666 for (i
= 0; i
< 64; i
++) {
4667 if (attrs
& (UINT64_C(1) << i
)) {
4668 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4670 ds_put_format(&s
, " %s",
4671 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4674 if (out_of_range_attr
) {
4675 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4678 ds_put_cstr(&s
, ": ");
4679 odp_flow_key_format(key
, key_len
, &s
);
4681 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
4686 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
4688 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4691 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
4694 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
4695 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
4696 return 0xff; /* Error. */
4699 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
4700 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
4701 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
4705 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
4706 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
4707 int *out_of_range_attrp
)
4709 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4710 const struct nlattr
*nla
;
4711 uint64_t present_attrs
;
4714 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
4716 *out_of_range_attrp
= 0;
4717 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4718 uint16_t type
= nl_attr_type(nla
);
4719 size_t len
= nl_attr_get_size(nla
);
4720 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4721 OVS_KEY_ATTR_MAX
, type
);
4723 if (len
!= expected_len
&& expected_len
>= 0) {
4724 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4726 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
4727 "length %d", ovs_key_attr_to_string(type
, namebuf
,
4733 if (type
> OVS_KEY_ATTR_MAX
) {
4734 *out_of_range_attrp
= type
;
4736 if (present_attrs
& (UINT64_C(1) << type
)) {
4737 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4739 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
4740 ovs_key_attr_to_string(type
,
4741 namebuf
, sizeof namebuf
));
4745 present_attrs
|= UINT64_C(1) << type
;
4750 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
4754 *present_attrsp
= present_attrs
;
4758 static enum odp_key_fitness
4759 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
4760 uint64_t expected_attrs
,
4761 const struct nlattr
*key
, size_t key_len
)
4763 uint64_t missing_attrs
;
4764 uint64_t extra_attrs
;
4766 missing_attrs
= expected_attrs
& ~present_attrs
;
4767 if (missing_attrs
) {
4768 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4769 log_odp_key_attributes(&rl
, "expected but not present",
4770 missing_attrs
, 0, key
, key_len
);
4771 return ODP_FIT_TOO_LITTLE
;
4774 extra_attrs
= present_attrs
& ~expected_attrs
;
4775 if (extra_attrs
|| out_of_range_attr
) {
4776 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4777 log_odp_key_attributes(&rl
, "present but not expected",
4778 extra_attrs
, out_of_range_attr
, key
, key_len
);
4779 return ODP_FIT_TOO_MUCH
;
4782 return ODP_FIT_PERFECT
;
4786 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4787 uint64_t present_attrs
, uint64_t *expected_attrs
,
4788 struct flow
*flow
, const struct flow
*src_flow
)
4790 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4791 bool is_mask
= flow
!= src_flow
;
4793 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
4794 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
4795 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4796 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
4797 ntohs(flow
->dl_type
));
4800 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
4801 flow
->dl_type
!= htons(0xffff)) {
4804 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
4807 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
4808 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
4809 /* See comments in odp_flow_key_from_flow__(). */
4810 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
4817 static enum odp_key_fitness
4818 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4819 uint64_t present_attrs
, int out_of_range_attr
,
4820 uint64_t expected_attrs
, struct flow
*flow
,
4821 const struct nlattr
*key
, size_t key_len
,
4822 const struct flow
*src_flow
)
4824 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4825 bool is_mask
= src_flow
!= flow
;
4826 const void *check_start
= NULL
;
4827 size_t check_len
= 0;
4828 enum ovs_key_attr expected_bit
= 0xff;
4830 if (eth_type_mpls(src_flow
->dl_type
)) {
4831 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4832 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
4834 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4835 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
4836 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
4837 int n
= size
/ sizeof(ovs_be32
);
4840 if (!size
|| size
% sizeof(ovs_be32
)) {
4841 return ODP_FIT_ERROR
;
4843 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
4844 return ODP_FIT_ERROR
;
4847 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
4848 flow
->mpls_lse
[i
] = mpls_lse
[i
];
4850 if (n
> FLOW_MAX_MPLS_LABELS
) {
4851 return ODP_FIT_TOO_MUCH
;
4855 /* BOS may be set only in the innermost label. */
4856 for (i
= 0; i
< n
- 1; i
++) {
4857 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
4858 return ODP_FIT_ERROR
;
4862 /* BOS must be set in the innermost label. */
4863 if (n
< FLOW_MAX_MPLS_LABELS
4864 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
4865 return ODP_FIT_TOO_LITTLE
;
4871 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4873 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
4875 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
4876 const struct ovs_key_ipv4
*ipv4_key
;
4878 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
4879 put_ipv4_key(ipv4_key
, flow
, is_mask
);
4880 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4881 return ODP_FIT_ERROR
;
4884 check_start
= ipv4_key
;
4885 check_len
= sizeof *ipv4_key
;
4886 expected_bit
= OVS_KEY_ATTR_IPV4
;
4889 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4891 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
4893 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
4894 const struct ovs_key_ipv6
*ipv6_key
;
4896 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
4897 put_ipv6_key(ipv6_key
, flow
, is_mask
);
4898 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4899 return ODP_FIT_ERROR
;
4902 check_start
= ipv6_key
;
4903 check_len
= sizeof *ipv6_key
;
4904 expected_bit
= OVS_KEY_ATTR_IPV6
;
4907 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4908 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4910 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
4912 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
4913 const struct ovs_key_arp
*arp_key
;
4915 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
4916 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
4917 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
4918 "key", ntohs(arp_key
->arp_op
));
4919 return ODP_FIT_ERROR
;
4921 put_arp_key(arp_key
, flow
);
4923 check_start
= arp_key
;
4924 check_len
= sizeof *arp_key
;
4925 expected_bit
= OVS_KEY_ATTR_ARP
;
4931 if (check_len
> 0) { /* Happens only when 'is_mask'. */
4932 if (!is_all_zeros(check_start
, check_len
) &&
4933 flow
->dl_type
!= htons(0xffff)) {
4934 return ODP_FIT_ERROR
;
4936 expected_attrs
|= UINT64_C(1) << expected_bit
;
4940 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
4941 if (src_flow
->nw_proto
== IPPROTO_TCP
4942 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4943 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4944 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4946 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
4948 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
4949 const union ovs_key_tp
*tcp_key
;
4951 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
4952 put_tp_key(tcp_key
, flow
);
4953 expected_bit
= OVS_KEY_ATTR_TCP
;
4955 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
4956 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
4957 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
4959 } else if (src_flow
->nw_proto
== IPPROTO_UDP
4960 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4961 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4962 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4964 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
4966 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
4967 const union ovs_key_tp
*udp_key
;
4969 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
4970 put_tp_key(udp_key
, flow
);
4971 expected_bit
= OVS_KEY_ATTR_UDP
;
4973 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
4974 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4975 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4976 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4978 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
4980 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
4981 const union ovs_key_tp
*sctp_key
;
4983 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
4984 put_tp_key(sctp_key
, flow
);
4985 expected_bit
= OVS_KEY_ATTR_SCTP
;
4987 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
4988 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
4989 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4991 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
4993 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
4994 const struct ovs_key_icmp
*icmp_key
;
4996 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
4997 flow
->tp_src
= htons(icmp_key
->icmp_type
);
4998 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
4999 expected_bit
= OVS_KEY_ATTR_ICMP
;
5001 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
5002 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5003 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5005 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
5007 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
5008 const struct ovs_key_icmpv6
*icmpv6_key
;
5010 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
5011 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
5012 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
5013 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
5014 if (is_nd(src_flow
, NULL
)) {
5016 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5018 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
5019 const struct ovs_key_nd
*nd_key
;
5021 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
5022 flow
->nd_target
= nd_key
->nd_target
;
5023 flow
->arp_sha
= nd_key
->nd_sll
;
5024 flow
->arp_tha
= nd_key
->nd_tll
;
5026 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
5027 * ICMP type and code are 8 bits wide. Therefore, an
5028 * exact match looks like htons(0xff), not
5029 * htons(0xffff). See xlate_wc_finish() for details.
5031 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
5032 (flow
->tp_src
!= htons(0xff) ||
5033 flow
->tp_dst
!= htons(0xff))) {
5034 return ODP_FIT_ERROR
;
5036 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5043 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
5044 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
5045 return ODP_FIT_ERROR
;
5047 expected_attrs
|= UINT64_C(1) << expected_bit
;
5052 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
5056 /* Parse 802.1Q header then encapsulated L3 attributes. */
5057 static enum odp_key_fitness
5058 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5059 uint64_t present_attrs
, int out_of_range_attr
,
5060 uint64_t expected_attrs
, struct flow
*flow
,
5061 const struct nlattr
*key
, size_t key_len
,
5062 const struct flow
*src_flow
)
5064 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5065 bool is_mask
= src_flow
!= flow
;
5067 const struct nlattr
*encap
5068 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
5069 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
5070 enum odp_key_fitness encap_fitness
;
5071 enum odp_key_fitness fitness
;
5073 /* Calculate fitness of outer attributes. */
5075 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
5076 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
5078 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5079 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5081 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
5082 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
5085 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
5086 expected_attrs
, key
, key_len
);
5089 * Remove the TPID from dl_type since it's not the real Ethertype. */
5090 flow
->dl_type
= htons(0);
5091 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
5092 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
5095 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
5096 return ODP_FIT_TOO_LITTLE
;
5097 } else if (flow
->vlan_tci
== htons(0)) {
5098 /* Corner case for a truncated 802.1Q header. */
5099 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
5100 return ODP_FIT_TOO_MUCH
;
5103 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
5104 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
5105 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
5106 return ODP_FIT_ERROR
;
5109 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5114 /* Now parse the encapsulated attributes. */
5115 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5116 attrs
, &present_attrs
, &out_of_range_attr
)) {
5117 return ODP_FIT_ERROR
;
5121 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
5122 return ODP_FIT_ERROR
;
5124 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5125 expected_attrs
, flow
, key
, key_len
,
5128 /* The overall fitness is the worse of the outer and inner attributes. */
5129 return MAX(fitness
, encap_fitness
);
5132 static enum odp_key_fitness
5133 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5134 struct flow
*flow
, const struct flow
*src_flow
)
5136 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5137 uint64_t expected_attrs
;
5138 uint64_t present_attrs
;
5139 int out_of_range_attr
;
5140 bool is_mask
= src_flow
!= flow
;
5142 memset(flow
, 0, sizeof *flow
);
5144 /* Parse attributes. */
5145 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5146 &out_of_range_attr
)) {
5147 return ODP_FIT_ERROR
;
5152 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5153 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5154 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5155 } else if (is_mask
) {
5156 /* Always exact match recirc_id if it is not specified. */
5157 flow
->recirc_id
= UINT32_MAX
;
5160 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5161 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5162 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5164 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5165 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5166 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5169 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5170 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5171 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5174 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5175 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5177 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5178 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5180 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5181 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5182 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5184 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5185 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5186 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5188 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5189 const ovs_u128
*cl
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5191 flow
->ct_label
= *cl
;
5192 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5195 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5196 enum odp_key_fitness res
;
5198 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
5200 if (res
== ODP_FIT_ERROR
) {
5201 return ODP_FIT_ERROR
;
5202 } else if (res
== ODP_FIT_PERFECT
) {
5203 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5207 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5208 flow
->in_port
.odp_port
5209 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5210 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5211 } else if (!is_mask
) {
5212 flow
->in_port
.odp_port
= ODPP_NONE
;
5215 /* Ethernet header. */
5216 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5217 const struct ovs_key_ethernet
*eth_key
;
5219 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5220 put_ethernet_key(eth_key
, flow
);
5222 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5226 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5229 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5230 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5232 return ODP_FIT_ERROR
;
5236 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
5237 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
5238 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5239 expected_attrs
, flow
, key
, key_len
, src_flow
);
5242 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5243 flow
->vlan_tci
= htons(0xffff);
5244 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5245 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5246 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5249 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5250 expected_attrs
, flow
, key
, key_len
, src_flow
);
5253 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5254 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5255 * 'key' fits our expectations for what a flow key should contain.
5257 * The 'in_port' will be the datapath's understanding of the port. The
5258 * caller will need to translate with odp_port_to_ofp_port() if the
5259 * OpenFlow port is needed.
5261 * This function doesn't take the packet itself as an argument because none of
5262 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5263 * it is always possible to infer which additional attribute(s) should appear
5264 * by looking at the attributes for lower-level protocols, e.g. if the network
5265 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5266 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5267 * must be absent. */
5268 enum odp_key_fitness
5269 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5272 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
5275 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5276 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5277 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5278 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5279 * well 'key' fits our expectations for what a flow key should contain. */
5280 enum odp_key_fitness
5281 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5282 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
5285 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5286 &mask
->masks
, src_flow
);
5289 /* A missing mask means that the flow should be exact matched.
5290 * Generate an appropriate exact wildcard for the flow. */
5291 flow_wildcards_init_for_packet(mask
, src_flow
);
5293 return ODP_FIT_PERFECT
;
5297 /* Returns 'fitness' as a string, for use in debug messages. */
5299 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5302 case ODP_FIT_PERFECT
:
5304 case ODP_FIT_TOO_MUCH
:
5306 case ODP_FIT_TOO_LITTLE
:
5307 return "too_little";
5315 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5316 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5317 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5318 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5319 * null, then the return value is not meaningful.) */
5321 odp_put_userspace_action(uint32_t pid
,
5322 const void *userdata
, size_t userdata_size
,
5323 odp_port_t tunnel_out_port
,
5324 bool include_actions
,
5325 struct ofpbuf
*odp_actions
)
5327 size_t userdata_ofs
;
5330 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5331 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5333 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5335 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5336 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5339 * - The kernel rejected shorter userdata with -ERANGE.
5341 * - The kernel silently dropped userdata beyond the first 8 bytes.
5343 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5344 * separately disable features that required more than 8 bytes.) */
5345 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5346 MAX(8, userdata_size
)),
5347 userdata
, userdata_size
);
5351 if (tunnel_out_port
!= ODPP_NONE
) {
5352 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5355 if (include_actions
) {
5356 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5358 nl_msg_end_nested(odp_actions
, offset
);
5360 return userdata_ofs
;
5364 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5365 struct ofpbuf
*odp_actions
)
5367 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5368 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5369 nl_msg_end_nested(odp_actions
, offset
);
5373 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5374 struct ovs_action_push_tnl
*data
)
5376 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5378 size
+= data
->header_len
;
5379 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5383 /* The commit_odp_actions() function and its helpers. */
5386 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5387 const void *key
, size_t key_size
)
5389 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5390 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5391 nl_msg_end_nested(odp_actions
, offset
);
5394 /* Masked set actions have a mask following the data within the netlink
5395 * attribute. The unmasked bits in the data will be cleared as the data
5396 * is copied to the action. */
5398 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5399 enum ovs_key_attr key_type
,
5400 const void *key_
, const void *mask_
, size_t key_size
)
5402 size_t offset
= nl_msg_start_nested(odp_actions
,
5403 OVS_ACTION_ATTR_SET_MASKED
);
5404 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5405 const char *key
= key_
, *mask
= mask_
;
5407 memcpy(data
+ key_size
, mask
, key_size
);
5408 /* Clear unmasked bits while copying. */
5409 while (key_size
--) {
5410 *data
++ = *key
++ & *mask
++;
5412 nl_msg_end_nested(odp_actions
, offset
);
5415 /* If any of the flow key data that ODP actions can modify are different in
5416 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5417 * 'odp_actions' that change the flow tunneling information in key from
5418 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5419 * same way. In other words, operates the same as commit_odp_actions(), but
5420 * only on tunneling information. */
5422 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5423 struct ofpbuf
*odp_actions
)
5425 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5426 * must have non-zero ipv6_dst. */
5427 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5428 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5431 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5432 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5437 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5438 const void *key
, void *base
, void *mask
, size_t size
,
5439 struct ofpbuf
*odp_actions
)
5441 if (memcmp(key
, base
, size
)) {
5442 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5444 if (use_masked_set
&& !fully_masked
) {
5445 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5447 if (!fully_masked
) {
5448 memset(mask
, 0xff, size
);
5450 commit_set_action(odp_actions
, attr
, key
, size
);
5452 memcpy(base
, key
, size
);
5455 /* Mask bits are set when we have either read or set the corresponding
5456 * values. Masked bits will be exact-matched, no need to set them
5457 * if the value did not actually change. */
5463 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5465 eth
->eth_src
= flow
->dl_src
;
5466 eth
->eth_dst
= flow
->dl_dst
;
5470 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5472 flow
->dl_src
= eth
->eth_src
;
5473 flow
->dl_dst
= eth
->eth_dst
;
5477 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5478 struct ofpbuf
*odp_actions
,
5479 struct flow_wildcards
*wc
,
5482 struct ovs_key_ethernet key
, base
, mask
;
5484 get_ethernet_key(flow
, &key
);
5485 get_ethernet_key(base_flow
, &base
);
5486 get_ethernet_key(&wc
->masks
, &mask
);
5488 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5489 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5490 put_ethernet_key(&base
, base_flow
);
5491 put_ethernet_key(&mask
, &wc
->masks
);
5496 pop_vlan(struct flow
*base
,
5497 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5499 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5501 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
5502 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5508 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
5509 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5511 if (base
->vlan_tci
== vlan_tci
) {
5515 pop_vlan(base
, odp_actions
, wc
);
5516 if (vlan_tci
& htons(VLAN_CFI
)) {
5517 struct ovs_action_push_vlan vlan
;
5519 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
5520 vlan
.vlan_tci
= vlan_tci
;
5521 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5522 &vlan
, sizeof vlan
);
5524 base
->vlan_tci
= vlan_tci
;
5527 /* Wildcarding already done at action translation time. */
5529 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
5530 struct ofpbuf
*odp_actions
)
5532 int base_n
= flow_count_mpls_labels(base
, NULL
);
5533 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
5534 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
5537 while (base_n
> common_n
) {
5538 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
5539 /* If there is only one more LSE in base than there are common
5540 * between base and flow; and flow has at least one more LSE than
5541 * is common then the topmost LSE of base may be updated using
5543 struct ovs_key_mpls mpls_key
;
5545 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
5546 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
5547 &mpls_key
, sizeof mpls_key
);
5548 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
5551 /* Otherwise, if there more LSEs in base than are common between
5552 * base and flow then pop the topmost one. */
5556 /* If all the LSEs are to be popped and this is not the outermost
5557 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5558 * POP_MPLS action instead of flow->dl_type.
5560 * This is because the POP_MPLS action requires its ethertype
5561 * argument to be an MPLS ethernet type but in this case
5562 * flow->dl_type will be a non-MPLS ethernet type.
5564 * When the final POP_MPLS action occurs it use flow->dl_type and
5565 * the and the resulting packet will have the desired dl_type. */
5566 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
5567 dl_type
= htons(ETH_TYPE_MPLS
);
5569 dl_type
= flow
->dl_type
;
5571 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
5572 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
5578 /* If, after the above popping and setting, there are more LSEs in flow
5579 * than base then some LSEs need to be pushed. */
5580 while (base_n
< flow_n
) {
5581 struct ovs_action_push_mpls
*mpls
;
5583 mpls
= nl_msg_put_unspec_zero(odp_actions
,
5584 OVS_ACTION_ATTR_PUSH_MPLS
,
5586 mpls
->mpls_ethertype
= flow
->dl_type
;
5587 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
5588 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
5589 * headers if the flow is restored later due to returning from a patch
5590 * port or group bucket. */
5591 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
5592 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
5598 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
5600 ipv4
->ipv4_src
= flow
->nw_src
;
5601 ipv4
->ipv4_dst
= flow
->nw_dst
;
5602 ipv4
->ipv4_proto
= flow
->nw_proto
;
5603 ipv4
->ipv4_tos
= flow
->nw_tos
;
5604 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
5605 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5609 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
5611 flow
->nw_src
= ipv4
->ipv4_src
;
5612 flow
->nw_dst
= ipv4
->ipv4_dst
;
5613 flow
->nw_proto
= ipv4
->ipv4_proto
;
5614 flow
->nw_tos
= ipv4
->ipv4_tos
;
5615 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
5616 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
5620 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
5621 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5624 struct ovs_key_ipv4 key
, mask
, base
;
5626 /* Check that nw_proto and nw_frag remain unchanged. */
5627 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5628 flow
->nw_frag
== base_flow
->nw_frag
);
5630 get_ipv4_key(flow
, &key
, false);
5631 get_ipv4_key(base_flow
, &base
, false);
5632 get_ipv4_key(&wc
->masks
, &mask
, true);
5633 mask
.ipv4_proto
= 0; /* Not writeable. */
5634 mask
.ipv4_frag
= 0; /* Not writable. */
5636 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5638 put_ipv4_key(&base
, base_flow
, false);
5639 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
5640 put_ipv4_key(&mask
, &wc
->masks
, true);
5646 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
5648 ipv6
->ipv6_src
= flow
->ipv6_src
;
5649 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
5650 ipv6
->ipv6_label
= flow
->ipv6_label
;
5651 ipv6
->ipv6_proto
= flow
->nw_proto
;
5652 ipv6
->ipv6_tclass
= flow
->nw_tos
;
5653 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
5654 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5658 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
5660 flow
->ipv6_src
= ipv6
->ipv6_src
;
5661 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
5662 flow
->ipv6_label
= ipv6
->ipv6_label
;
5663 flow
->nw_proto
= ipv6
->ipv6_proto
;
5664 flow
->nw_tos
= ipv6
->ipv6_tclass
;
5665 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
5666 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
5670 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
5671 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5674 struct ovs_key_ipv6 key
, mask
, base
;
5676 /* Check that nw_proto and nw_frag remain unchanged. */
5677 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5678 flow
->nw_frag
== base_flow
->nw_frag
);
5680 get_ipv6_key(flow
, &key
, false);
5681 get_ipv6_key(base_flow
, &base
, false);
5682 get_ipv6_key(&wc
->masks
, &mask
, true);
5683 mask
.ipv6_proto
= 0; /* Not writeable. */
5684 mask
.ipv6_frag
= 0; /* Not writable. */
5686 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5688 put_ipv6_key(&base
, base_flow
, false);
5689 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
5690 put_ipv6_key(&mask
, &wc
->masks
, true);
5696 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
5698 /* ARP key has padding, clear it. */
5699 memset(arp
, 0, sizeof *arp
);
5701 arp
->arp_sip
= flow
->nw_src
;
5702 arp
->arp_tip
= flow
->nw_dst
;
5703 arp
->arp_op
= htons(flow
->nw_proto
);
5704 arp
->arp_sha
= flow
->arp_sha
;
5705 arp
->arp_tha
= flow
->arp_tha
;
5709 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
5711 flow
->nw_src
= arp
->arp_sip
;
5712 flow
->nw_dst
= arp
->arp_tip
;
5713 flow
->nw_proto
= ntohs(arp
->arp_op
);
5714 flow
->arp_sha
= arp
->arp_sha
;
5715 flow
->arp_tha
= arp
->arp_tha
;
5718 static enum slow_path_reason
5719 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
5720 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5722 struct ovs_key_arp key
, mask
, base
;
5724 get_arp_key(flow
, &key
);
5725 get_arp_key(base_flow
, &base
);
5726 get_arp_key(&wc
->masks
, &mask
);
5728 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
5730 put_arp_key(&base
, base_flow
);
5731 put_arp_key(&mask
, &wc
->masks
);
5738 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
5740 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5741 icmp
->icmp_type
= ntohs(flow
->tp_src
);
5742 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
5746 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
5748 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5749 flow
->tp_src
= htons(icmp
->icmp_type
);
5750 flow
->tp_dst
= htons(icmp
->icmp_code
);
5753 static enum slow_path_reason
5754 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
5755 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5757 struct ovs_key_icmp key
, mask
, base
;
5758 enum ovs_key_attr attr
;
5760 if (is_icmpv4(flow
, NULL
)) {
5761 attr
= OVS_KEY_ATTR_ICMP
;
5762 } else if (is_icmpv6(flow
, NULL
)) {
5763 attr
= OVS_KEY_ATTR_ICMPV6
;
5768 get_icmp_key(flow
, &key
);
5769 get_icmp_key(base_flow
, &base
);
5770 get_icmp_key(&wc
->masks
, &mask
);
5772 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5773 put_icmp_key(&base
, base_flow
);
5774 put_icmp_key(&mask
, &wc
->masks
);
5781 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
5783 nd
->nd_target
= flow
->nd_target
;
5784 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5785 nd
->nd_sll
= flow
->arp_sha
;
5786 nd
->nd_tll
= flow
->arp_tha
;
5790 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
5792 flow
->nd_target
= nd
->nd_target
;
5793 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5794 flow
->arp_sha
= nd
->nd_sll
;
5795 flow
->arp_tha
= nd
->nd_tll
;
5798 static enum slow_path_reason
5799 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
5800 struct ofpbuf
*odp_actions
,
5801 struct flow_wildcards
*wc
, bool use_masked
)
5803 struct ovs_key_nd key
, mask
, base
;
5805 get_nd_key(flow
, &key
);
5806 get_nd_key(base_flow
, &base
);
5807 get_nd_key(&wc
->masks
, &mask
);
5809 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5811 put_nd_key(&base
, base_flow
);
5812 put_nd_key(&mask
, &wc
->masks
);
5819 static enum slow_path_reason
5820 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
5821 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5824 /* Check if 'flow' really has an L3 header. */
5825 if (!flow
->nw_proto
) {
5829 switch (ntohs(base
->dl_type
)) {
5831 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
5835 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
5836 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
5839 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
5845 /* TCP, UDP, and SCTP keys have the same layout. */
5846 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
5847 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
5850 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
5852 tp
->tcp
.tcp_src
= flow
->tp_src
;
5853 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
5857 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
5859 flow
->tp_src
= tp
->tcp
.tcp_src
;
5860 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
5864 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
5865 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5868 enum ovs_key_attr key_type
;
5869 union ovs_key_tp key
, mask
, base
;
5871 /* Check if 'flow' really has an L3 header. */
5872 if (!flow
->nw_proto
) {
5876 if (!is_ip_any(base_flow
)) {
5880 if (flow
->nw_proto
== IPPROTO_TCP
) {
5881 key_type
= OVS_KEY_ATTR_TCP
;
5882 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5883 key_type
= OVS_KEY_ATTR_UDP
;
5884 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5885 key_type
= OVS_KEY_ATTR_SCTP
;
5890 get_tp_key(flow
, &key
);
5891 get_tp_key(base_flow
, &base
);
5892 get_tp_key(&wc
->masks
, &mask
);
5894 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5896 put_tp_key(&base
, base_flow
);
5897 put_tp_key(&mask
, &wc
->masks
);
5902 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
5903 struct ofpbuf
*odp_actions
,
5904 struct flow_wildcards
*wc
,
5907 uint32_t key
, mask
, base
;
5909 key
= flow
->skb_priority
;
5910 base
= base_flow
->skb_priority
;
5911 mask
= wc
->masks
.skb_priority
;
5913 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
5914 sizeof key
, odp_actions
)) {
5915 base_flow
->skb_priority
= base
;
5916 wc
->masks
.skb_priority
= mask
;
5921 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
5922 struct ofpbuf
*odp_actions
,
5923 struct flow_wildcards
*wc
,
5926 uint32_t key
, mask
, base
;
5928 key
= flow
->pkt_mark
;
5929 base
= base_flow
->pkt_mark
;
5930 mask
= wc
->masks
.pkt_mark
;
5932 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
5933 sizeof key
, odp_actions
)) {
5934 base_flow
->pkt_mark
= base
;
5935 wc
->masks
.pkt_mark
= mask
;
5939 /* If any of the flow key data that ODP actions can modify are different in
5940 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5941 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5942 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5943 * in addition to this function if needed. Sets fields in 'wc' that are
5944 * used as part of the action.
5946 * Returns a reason to force processing the flow's packets into the userspace
5947 * slow path, if there is one, otherwise 0. */
5948 enum slow_path_reason
5949 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
5950 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5953 enum slow_path_reason slow1
, slow2
;
5954 bool mpls_done
= false;
5956 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
5957 /* Make packet a non-MPLS packet before committing L3/4 actions,
5958 * which would otherwise do nothing. */
5959 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
5960 commit_mpls_action(flow
, base
, odp_actions
);
5963 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
5964 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
5965 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
5967 commit_mpls_action(flow
, base
, odp_actions
);
5969 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
5970 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
5971 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
5973 return slow1
? slow1
: slow2
;